blob: db9dee3f474d66e496b0b2e76a8dbae927330f45 [file] [log] [blame]
Gilles Peskine80ba8502021-04-03 20:36:37 +02001/**
2 * \file ecp_invasive.h
3 *
4 * \brief ECP module: interfaces for invasive testing only.
5 *
6 * The interfaces in this file are intended for testing purposes only.
7 * They SHOULD NOT be made available in library integrations except when
8 * building the library for testing.
9 */
10/*
11 * Copyright The Mbed TLS Contributors
12 * SPDX-License-Identifier: Apache-2.0
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License"); you may
15 * not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
25 */
26#ifndef MBEDTLS_ECP_INVASIVE_H
27#define MBEDTLS_ECP_INVASIVE_H
28
29#include "common.h"
Gilles Peskine72fcc982021-03-23 22:31:31 +010030#include "mbedtls/bignum.h"
Minos Galanakisdd556922023-02-03 19:12:21 +000031#include "bignum_mod.h"
Gilles Peskine80ba8502021-04-03 20:36:37 +020032#include "mbedtls/ecp.h"
33
Gilles Peskine637c0492023-06-15 19:07:41 +020034/*
35 * Curve modulus types
36 */
37typedef enum {
38 MBEDTLS_ECP_MOD_NONE = 0,
39 MBEDTLS_ECP_MOD_COORDINATE,
40 MBEDTLS_ECP_MOD_SCALAR
41} mbedtls_ecp_modulus_type;
42
Gabor Mezei1a729dc2023-07-05 16:08:57 +020043/* Provide a commented-out definition so that `check_names.py` knows that
44 * it's not a typo.
Gabor Mezei1a729dc2023-07-05 16:08:57 +020045 */
Gabor Mezeif4aab6f2023-06-30 14:50:03 +020046//#define MBEDTLS_ECP_WITH_MPI_UINT
Gabor Mezeia306d202023-06-06 17:15:52 +020047
Gabor Mezeic8107072023-06-06 17:24:35 +020048typedef enum {
Gabor Mezeic97a4072023-07-06 10:54:41 +020049 MBEDTLS_ECP_VARIANT_NONE = 0,
50 MBEDTLS_ECP_VARIANT_WITH_MPI_STRUCT,
51 MBEDTLS_ECP_VARIANT_WITH_MPI_UINT
Gabor Mezeic8107072023-06-06 17:24:35 +020052} mbedtls_ecp_variant;
53
Valerio Setti0c477d32023-04-07 15:54:20 +020054#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine80ba8502021-04-03 20:36:37 +020055
Gabor Mezeic8107072023-06-06 17:24:35 +020056/** Queries the ecp variant.
57 *
58 * \return The id of the ecp variant.
59 */
60MBEDTLS_STATIC_TESTABLE
61mbedtls_ecp_variant mbedtls_ecp_get_variant(void);
62
Gilles Peskine72fcc982021-03-23 22:31:31 +010063#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
64/** Generate a private key on a Montgomery curve (Curve25519 or Curve448).
65 *
66 * This function implements key generation for the set of secret keys
67 * specified in [Curve25519] p. 5 and in [Curve448]. The resulting value
68 * has the lower bits masked but is not necessarily canonical.
69 *
70 * \note - [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
71 * - [RFC7748] https://tools.ietf.org/html/rfc7748
72 *
Gilles Peskine55c46042021-03-24 12:34:40 +010073 * \p high_bit The position of the high-order bit of the key to generate.
Gilles Peskine72fcc982021-03-23 22:31:31 +010074 * This is the bit-size of the key minus 1:
75 * 254 for Curve25519 or 447 for Curve448.
76 * \param d The randomly generated key. This is a number of size
Xiaokang Qiana0896142023-04-18 06:49:55 +000077 * exactly \p high_bit + 1 bits, with the least significant bits
Gilles Peskine72fcc982021-03-23 22:31:31 +010078 * masked as specified in [Curve25519] and in [RFC7748] ยง5.
79 * \param f_rng The RNG function.
80 * \param p_rng The RNG context to be passed to \p f_rng.
81 *
82 * \return \c 0 on success.
83 * \return \c MBEDTLS_ERR_ECP_xxx or MBEDTLS_ERR_MPI_xxx on failure.
84 */
Xiaokang Qiana0896142023-04-18 06:49:55 +000085int mbedtls_ecp_gen_privkey_mx(size_t high_bit,
Gilles Peskine449bd832023-01-11 14:50:10 +010086 mbedtls_mpi *d,
87 int (*f_rng)(void *, unsigned char *, size_t),
88 void *p_rng);
Gilles Peskine72fcc982021-03-23 22:31:31 +010089
90#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
91
Gabor Mezeideece2b2023-01-25 17:57:36 +010092#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
93
Gabor Mezei9b290b32023-01-27 11:00:51 +010094/** Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
95 *
Gabor Mezeia2648312023-02-13 16:29:05 +010096 * This operation expects a 384 bit MPI and the result of the reduction
97 * is a 192 bit MPI.
98 *
Gabor Mezei9b290b32023-01-27 11:00:51 +010099 * \param[in,out] Np The address of the MPI to be converted.
Gabor Mezei0b4b8e32023-02-14 16:36:38 +0100100 * Must have twice as many limbs as the modulus.
101 * Upon return this holds the reduced value. The bitlength
102 * of the reduced value is the same as that of the modulus
103 * (192 bits).
Gabor Mezei63aae682023-02-06 16:24:08 +0100104 * \param[in] Nn The length of \p Np in limbs.
Gabor Mezei9b290b32023-01-27 11:00:51 +0100105 */
Gabor Mezeideece2b2023-01-25 17:57:36 +0100106MBEDTLS_STATIC_TESTABLE
Gabor Mezei2038ce92023-01-31 14:33:12 +0100107int mbedtls_ecp_mod_p192_raw(mbedtls_mpi_uint *Np, size_t Nn);
Gabor Mezeideece2b2023-01-25 17:57:36 +0100108
109#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
110
Gabor Mezeie14b5bd2023-02-08 17:23:03 +0100111#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
112
Gabor Mezeia835d202023-02-23 17:38:00 +0100113/** Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
114 *
Gabor Mezei08a94952023-02-28 18:40:57 +0100115 * \param[in,out] X The address of the MPI to be converted.
116 * Must have exact limb size that stores a 448-bit MPI
117 * (double the bitlength of the modulus).
118 * Upon return holds the reduced value which is
119 * in range `0 <= X < 2 * N` (where N is the modulus).
120 * The bitlength of the reduced value is the same as
121 * that of the modulus (224 bits).
122 * \param[in] X_limbs The length of \p X in limbs.
Gabor Mezeia835d202023-02-23 17:38:00 +0100123 *
124 * \return \c 0 on success.
Gabor Mezei08a94952023-02-28 18:40:57 +0100125 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the
126 * limb size that sores a 448-bit MPI.
Gabor Mezeia835d202023-02-23 17:38:00 +0100127 */
Gabor Mezeie14b5bd2023-02-08 17:23:03 +0100128MBEDTLS_STATIC_TESTABLE
Gabor Mezei08a94952023-02-28 18:40:57 +0100129int mbedtls_ecp_mod_p224_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezeie14b5bd2023-02-08 17:23:03 +0100130
131#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
132
Gabor Mezei5221c042023-03-01 16:05:21 +0100133#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
134
Gabor Mezeid1f16b92023-03-08 15:26:32 +0100135/** Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
136 *
137 * \param[in,out] X The address of the MPI to be converted.
138 * Must have exact limb size that stores a 512-bit MPI
139 * (double the bitlength of the modulus).
140 * Upon return holds the reduced value which is
141 * in range `0 <= X < 2 * N` (where N is the modulus).
142 * The bitlength of the reduced value is the same as
143 * that of the modulus (256 bits).
144 * \param[in] X_limbs The length of \p X in limbs.
145 *
146 * \return \c 0 on success.
147 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the
148 * limb size that sores a 512-bit MPI.
149 */
Gabor Mezei5221c042023-03-01 16:05:21 +0100150MBEDTLS_STATIC_TESTABLE
Gabor Mezeied1acf62023-03-01 16:09:13 +0100151int mbedtls_ecp_mod_p256_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezei5221c042023-03-01 16:05:21 +0100152
153#endif
154
Gabor Mezei2cb630e2023-02-01 14:02:16 +0100155#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
156
Gabor Mezeib1c62ca2023-02-06 16:02:05 +0100157/** Fast quasi-reduction modulo p521 = 2^521 - 1 (FIPS 186-3 D.2.5)
158 *
Gabor Mezei7e6fcc12023-02-15 17:51:59 +0100159 * \param[in,out] X The address of the MPI to be converted.
160 * Must have twice as many limbs as the modulus
161 * (the modulus is 521 bits long). Upon return this
162 * holds the reduced value. The reduced value is
163 * in range `0 <= X < 2 * N` (where N is the modulus).
164 * and its the bitlength is one plus the bitlength
165 * of the modulus.
166 * \param[in] X_limbs The length of \p X in limbs.
167 *
168 * \return \c 0 on success.
169 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs does not have
170 * twice as many limbs as the modulus.
Gabor Mezeib1c62ca2023-02-06 16:02:05 +0100171 */
Gabor Mezei2cb630e2023-02-01 14:02:16 +0100172MBEDTLS_STATIC_TESTABLE
Gabor Mezei7e6fcc12023-02-15 17:51:59 +0100173int mbedtls_ecp_mod_p521_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezei2cb630e2023-02-01 14:02:16 +0100174
175#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
176
Minos Galanakis6fb105f2023-02-22 15:28:20 +0000177#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
178
179/** Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
180 *
181 * \param[in,out] X The address of the MPI to be converted.
Minos Galanakisf9fca532023-03-23 10:36:53 +0000182 * Must have exact limb size that stores a 768-bit MPI
183 * (double the bitlength of the modulus).
Minos Galanakis6fb105f2023-02-22 15:28:20 +0000184 * Upon return holds the reduced value which is
185 * in range `0 <= X < 2 * N` (where N is the modulus).
186 * The bitlength of the reduced value is the same as
187 * that of the modulus (384 bits).
188 * \param[in] X_limbs The length of \p N in limbs.
189 *
190 * \return \c 0 on success.
191 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p N_n does not have
192 * twice as many limbs as the modulus.
193 */
194MBEDTLS_STATIC_TESTABLE
195int mbedtls_ecp_mod_p384_raw(mbedtls_mpi_uint *X, size_t X_limbs);
196
197#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
198
Gabor Mezei1237a342023-04-11 16:22:35 +0200199#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
200
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200201/** Fast quasi-reduction modulo p192k1 = 2^192 - R,
202 * with R = 2^32 + 2^12 + 2^8 + 2^7 + 2^6 + 2^3 + 1 = 0x01000011C9
203 *
204 * \param[in,out] X The address of the MPI to be converted.
205 * Must have exact limb size that stores a 384-bit MPI
206 * (double the bitlength of the modulus).
207 * Upon return holds the reduced value which is
208 * in range `0 <= X < 2 * N` (where N is the modulus).
209 * The bitlength of the reduced value is the same as
210 * that of the modulus (192 bits).
211 * \param[in] X_limbs The length of \p X in limbs.
212 *
213 * \return \c 0 on success.
Gabor Mezeid56e6e02023-05-17 17:51:19 +0200214 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
215 * twice as many limbs as the modulus.
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200216 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
Gabor Mezei1237a342023-04-11 16:22:35 +0200217 */
218MBEDTLS_STATIC_TESTABLE
Gabor Mezeidacfe562023-05-02 14:05:13 +0200219int mbedtls_ecp_mod_p192k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezei1237a342023-04-11 16:22:35 +0200220
221#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
Gabor Mezeie42bb622023-05-02 14:10:57 +0200222
Minos Galanakise5dab972023-04-11 16:42:06 +0100223#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
224
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200225/** Fast quasi-reduction modulo p224k1 = 2^224 - R,
226 * with R = 2^32 + 2^12 + 2^11 + 2^9 + 2^7 + 2^4 + 2 + 1 = 0x0100001A93
227 *
228 * \param[in,out] X The address of the MPI to be converted.
229 * Must have exact limb size that stores a 448-bit MPI
230 * (double the bitlength of the modulus).
231 * Upon return holds the reduced value which is
232 * in range `0 <= X < 2 * N` (where N is the modulus).
233 * The bitlength of the reduced value is the same as
234 * that of the modulus (224 bits).
235 * \param[in] X_limbs The length of \p X in limbs.
236 *
237 * \return \c 0 on success.
Gabor Mezeid56e6e02023-05-17 17:51:19 +0200238 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
239 * twice as many limbs as the modulus.
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200240 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
241 */
Minos Galanakise5dab972023-04-11 16:42:06 +0100242MBEDTLS_STATIC_TESTABLE
Gabor Mezeie42bb622023-05-02 14:10:57 +0200243int mbedtls_ecp_mod_p224k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Minos Galanakise5dab972023-04-11 16:42:06 +0100244
245#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
Gabor Mezei1237a342023-04-11 16:22:35 +0200246
Minos Galanakisd6751dc2023-04-11 17:25:31 +0100247#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
248
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200249/** Fast quasi-reduction modulo p256k1 = 2^256 - R,
250 * with R = 2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1 = 0x01000003D1
251 *
252 * \param[in,out] X The address of the MPI to be converted.
253 * Must have exact limb size that stores a 512-bit MPI
254 * (double the bitlength of the modulus).
255 * Upon return holds the reduced value which is
256 * in range `0 <= X < 2 * N` (where N is the modulus).
257 * The bitlength of the reduced value is the same as
258 * that of the modulus (256 bits).
259 * \param[in] X_limbs The length of \p X in limbs.
260 *
261 * \return \c 0 on success.
Gabor Mezeid56e6e02023-05-17 17:51:19 +0200262 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
263 * twice as many limbs as the modulus.
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200264 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
265 */
Minos Galanakisd6751dc2023-04-11 17:25:31 +0100266MBEDTLS_STATIC_TESTABLE
Gabor Mezei03558b82023-05-02 14:12:25 +0200267int mbedtls_ecp_mod_p256k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Minos Galanakisd6751dc2023-04-11 17:25:31 +0100268
269#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
270
Minos Galanakisd0292c22023-05-10 15:46:47 +0100271#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
272
Minos Galanakis47249fd2023-05-18 16:16:17 +0100273/** Fast quasi-reduction modulo p255 = 2^255 - 19
274 *
275 * \param[in,out] X The address of the MPI to be converted.
276 * Must have exact limb size that stores a 510-bit MPI
277 * (double the bitlength of the modulus).
278 * Upon return holds the reduced value which is
279 * in range `0 <= X < 2 * N` (where N is the modulus).
Minos Galanakis47249fd2023-05-18 16:16:17 +0100280 * \param[in] X_limbs The length of \p X in limbs.
281 *
282 * \return \c 0 on success.
283 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
284 * twice as many limbs as the modulus.
285 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
286 */
Minos Galanakisd0292c22023-05-10 15:46:47 +0100287MBEDTLS_STATIC_TESTABLE
288int mbedtls_ecp_mod_p255_raw(mbedtls_mpi_uint *X, size_t X_limbs);
289
290#endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
291
Paul Elliott47a3c822023-04-23 23:18:50 +0100292#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
293
Paul Elliottee861002023-05-31 12:12:22 +0100294/** Fast quasi-reduction modulo p448 = 2^448 - 2^224 - 1
295 * Write X as A0 + 2^448 A1 and A1 as B0 + 2^224 B1, and return A0 + A1 + B1 +
296 * (B0 + B1) * 2^224.
297 *
298 * \param[in,out] X The address of the MPI to be converted.
299 * Must have exact limb size that stores a 896-bit MPI
300 * (double the bitlength of the modulus). Upon return
301 * holds the reduced value which is in range `0 <= X <
302 * N` (where N is the modulus). The bitlength of the
303 * reduced value is the same as that of the modulus
304 * (448 bits).
305 * \param[in] X_limbs The length of \p X in limbs.
306 *
307 * \return \c 0 on Success.
308 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
309 * twice as many limbs as the modulus.
310 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation
311 * failed.
312 */
Paul Elliott47a3c822023-04-23 23:18:50 +0100313MBEDTLS_STATIC_TESTABLE
Paul Elliotta2e48f72023-06-02 16:00:05 +0100314int mbedtls_ecp_mod_p448_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Paul Elliott47a3c822023-04-23 23:18:50 +0100315
316#endif /* MBEDTLS_ECP_DP_CURVE448_ENABLED */
317
Minos Galanakisa30afe22023-02-15 15:36:29 +0000318/** Initialise a modulus with hard-coded const curve data.
319 *
320 * \note The caller is responsible for the \p N modulus' memory.
321 * mbedtls_mpi_mod_modulus_free(&N) should be invoked at the
322 * end of its lifecycle.
323 *
324 * \param[in,out] N The address of the modulus structure to populate.
325 * Must be initialized.
326 * \param[in] id The mbedtls_ecp_group_id for which to initialise the modulus.
Minos Galanakis1d3e3322023-06-09 14:53:30 +0100327 * \param[in] ctype The mbedtls_ecp_modulus_type identifier for a coordinate modulus (P)
Minos Galanakisa30afe22023-02-15 15:36:29 +0000328 * or a scalar modulus (N).
329 *
330 * \return \c 0 if successful.
331 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the given MPIs do not
332 * have the correct number of limbs.
333 *
334 */
Minos Galanakisdd556922023-02-03 19:12:21 +0000335MBEDTLS_STATIC_TESTABLE
336int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N,
337 const mbedtls_ecp_group_id id,
Minos Galanakis1d3e3322023-06-09 14:53:30 +0100338 const mbedtls_ecp_modulus_type ctype);
Minos Galanakisdd556922023-02-03 19:12:21 +0000339
Gilles Peskine80ba8502021-04-03 20:36:37 +0200340#endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_C */
341
342#endif /* MBEDTLS_ECP_INVASIVE_H */