blob: ff9f9ecf1d1c83fede1a56f50f9adf10ad80db87 [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
Dave Rodgman16799db2023-11-02 19:47:20 +000012 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine80ba8502021-04-03 20:36:37 +020013 */
14#ifndef MBEDTLS_ECP_INVASIVE_H
15#define MBEDTLS_ECP_INVASIVE_H
16
17#include "common.h"
Gilles Peskine72fcc982021-03-23 22:31:31 +010018#include "mbedtls/bignum.h"
Minos Galanakisdd556922023-02-03 19:12:21 +000019#include "bignum_mod.h"
Gilles Peskine80ba8502021-04-03 20:36:37 +020020#include "mbedtls/ecp.h"
21
Gilles Peskine637c0492023-06-15 19:07:41 +020022/*
23 * Curve modulus types
24 */
25typedef enum {
26 MBEDTLS_ECP_MOD_NONE = 0,
27 MBEDTLS_ECP_MOD_COORDINATE,
28 MBEDTLS_ECP_MOD_SCALAR
29} mbedtls_ecp_modulus_type;
30
Gabor Mezeic8107072023-06-06 17:24:35 +020031typedef enum {
Gabor Mezeic97a4072023-07-06 10:54:41 +020032 MBEDTLS_ECP_VARIANT_NONE = 0,
33 MBEDTLS_ECP_VARIANT_WITH_MPI_STRUCT,
34 MBEDTLS_ECP_VARIANT_WITH_MPI_UINT
Gabor Mezeic8107072023-06-06 17:24:35 +020035} mbedtls_ecp_variant;
36
Valerio Setti0c477d32023-04-07 15:54:20 +020037#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine80ba8502021-04-03 20:36:37 +020038
Gabor Mezeic8107072023-06-06 17:24:35 +020039/** Queries the ecp variant.
40 *
41 * \return The id of the ecp variant.
42 */
43MBEDTLS_STATIC_TESTABLE
44mbedtls_ecp_variant mbedtls_ecp_get_variant(void);
45
Gilles Peskine72fcc982021-03-23 22:31:31 +010046#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
47/** Generate a private key on a Montgomery curve (Curve25519 or Curve448).
48 *
49 * This function implements key generation for the set of secret keys
50 * specified in [Curve25519] p. 5 and in [Curve448]. The resulting value
51 * has the lower bits masked but is not necessarily canonical.
52 *
53 * \note - [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
54 * - [RFC7748] https://tools.ietf.org/html/rfc7748
55 *
Gilles Peskine55c46042021-03-24 12:34:40 +010056 * \p high_bit The position of the high-order bit of the key to generate.
Gilles Peskine72fcc982021-03-23 22:31:31 +010057 * This is the bit-size of the key minus 1:
58 * 254 for Curve25519 or 447 for Curve448.
59 * \param d The randomly generated key. This is a number of size
Xiaokang Qiana0896142023-04-18 06:49:55 +000060 * exactly \p high_bit + 1 bits, with the least significant bits
Gilles Peskine72fcc982021-03-23 22:31:31 +010061 * masked as specified in [Curve25519] and in [RFC7748] ยง5.
62 * \param f_rng The RNG function.
63 * \param p_rng The RNG context to be passed to \p f_rng.
64 *
65 * \return \c 0 on success.
66 * \return \c MBEDTLS_ERR_ECP_xxx or MBEDTLS_ERR_MPI_xxx on failure.
67 */
Xiaokang Qiana0896142023-04-18 06:49:55 +000068int mbedtls_ecp_gen_privkey_mx(size_t high_bit,
Gilles Peskine449bd832023-01-11 14:50:10 +010069 mbedtls_mpi *d,
70 int (*f_rng)(void *, unsigned char *, size_t),
71 void *p_rng);
Gilles Peskine72fcc982021-03-23 22:31:31 +010072
73#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
74
Gabor Mezeideece2b2023-01-25 17:57:36 +010075#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
76
Gabor Mezei9b290b32023-01-27 11:00:51 +010077/** Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
78 *
Gabor Mezeia2648312023-02-13 16:29:05 +010079 * This operation expects a 384 bit MPI and the result of the reduction
80 * is a 192 bit MPI.
81 *
Gabor Mezei9b290b32023-01-27 11:00:51 +010082 * \param[in,out] Np The address of the MPI to be converted.
Gabor Mezei0b4b8e32023-02-14 16:36:38 +010083 * Must have twice as many limbs as the modulus.
84 * Upon return this holds the reduced value. The bitlength
85 * of the reduced value is the same as that of the modulus
86 * (192 bits).
Gabor Mezei63aae682023-02-06 16:24:08 +010087 * \param[in] Nn The length of \p Np in limbs.
Gabor Mezei9b290b32023-01-27 11:00:51 +010088 */
Gabor Mezeideece2b2023-01-25 17:57:36 +010089MBEDTLS_STATIC_TESTABLE
Gabor Mezei2038ce92023-01-31 14:33:12 +010090int mbedtls_ecp_mod_p192_raw(mbedtls_mpi_uint *Np, size_t Nn);
Gabor Mezeideece2b2023-01-25 17:57:36 +010091
92#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
93
Gabor Mezeie14b5bd2023-02-08 17:23:03 +010094#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
95
Gabor Mezeia835d202023-02-23 17:38:00 +010096/** Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
97 *
Gabor Mezei08a94952023-02-28 18:40:57 +010098 * \param[in,out] X The address of the MPI to be converted.
99 * Must have exact limb size that stores a 448-bit MPI
100 * (double the bitlength of the modulus).
101 * Upon return holds the reduced value which is
102 * in range `0 <= X < 2 * N` (where N is the modulus).
103 * The bitlength of the reduced value is the same as
104 * that of the modulus (224 bits).
105 * \param[in] X_limbs The length of \p X in limbs.
Gabor Mezeia835d202023-02-23 17:38:00 +0100106 *
107 * \return \c 0 on success.
Gabor Mezei08a94952023-02-28 18:40:57 +0100108 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the
109 * limb size that sores a 448-bit MPI.
Gabor Mezeia835d202023-02-23 17:38:00 +0100110 */
Gabor Mezeie14b5bd2023-02-08 17:23:03 +0100111MBEDTLS_STATIC_TESTABLE
Gabor Mezei08a94952023-02-28 18:40:57 +0100112int mbedtls_ecp_mod_p224_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezeie14b5bd2023-02-08 17:23:03 +0100113
114#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
115
Gabor Mezei5221c042023-03-01 16:05:21 +0100116#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
117
Gabor Mezeid1f16b92023-03-08 15:26:32 +0100118/** Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
119 *
120 * \param[in,out] X The address of the MPI to be converted.
121 * Must have exact limb size that stores a 512-bit MPI
122 * (double the bitlength of the modulus).
123 * Upon return holds the reduced value which is
124 * in range `0 <= X < 2 * N` (where N is the modulus).
125 * The bitlength of the reduced value is the same as
126 * that of the modulus (256 bits).
127 * \param[in] X_limbs The length of \p X in limbs.
128 *
129 * \return \c 0 on success.
130 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the
131 * limb size that sores a 512-bit MPI.
132 */
Gabor Mezei5221c042023-03-01 16:05:21 +0100133MBEDTLS_STATIC_TESTABLE
Gabor Mezeied1acf62023-03-01 16:09:13 +0100134int mbedtls_ecp_mod_p256_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezei5221c042023-03-01 16:05:21 +0100135
136#endif
137
Gabor Mezei2cb630e2023-02-01 14:02:16 +0100138#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
139
Gabor Mezeib1c62ca2023-02-06 16:02:05 +0100140/** Fast quasi-reduction modulo p521 = 2^521 - 1 (FIPS 186-3 D.2.5)
141 *
Gabor Mezei7e6fcc12023-02-15 17:51:59 +0100142 * \param[in,out] X The address of the MPI to be converted.
143 * Must have twice as many limbs as the modulus
144 * (the modulus is 521 bits long). Upon return this
145 * holds the reduced value. The reduced value is
146 * in range `0 <= X < 2 * N` (where N is the modulus).
147 * and its the bitlength is one plus the bitlength
148 * of the modulus.
149 * \param[in] X_limbs The length of \p X in limbs.
150 *
151 * \return \c 0 on success.
152 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs does not have
153 * twice as many limbs as the modulus.
Gabor Mezeib1c62ca2023-02-06 16:02:05 +0100154 */
Gabor Mezei2cb630e2023-02-01 14:02:16 +0100155MBEDTLS_STATIC_TESTABLE
Gabor Mezei7e6fcc12023-02-15 17:51:59 +0100156int mbedtls_ecp_mod_p521_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezei2cb630e2023-02-01 14:02:16 +0100157
158#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
159
Minos Galanakis6fb105f2023-02-22 15:28:20 +0000160#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
161
162/** Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
163 *
164 * \param[in,out] X The address of the MPI to be converted.
Minos Galanakisf9fca532023-03-23 10:36:53 +0000165 * Must have exact limb size that stores a 768-bit MPI
166 * (double the bitlength of the modulus).
Minos Galanakis6fb105f2023-02-22 15:28:20 +0000167 * Upon return holds the reduced value which is
168 * in range `0 <= X < 2 * N` (where N is the modulus).
169 * The bitlength of the reduced value is the same as
170 * that of the modulus (384 bits).
171 * \param[in] X_limbs The length of \p N in limbs.
172 *
173 * \return \c 0 on success.
174 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p N_n does not have
175 * twice as many limbs as the modulus.
176 */
177MBEDTLS_STATIC_TESTABLE
178int mbedtls_ecp_mod_p384_raw(mbedtls_mpi_uint *X, size_t X_limbs);
179
180#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
181
Gabor Mezei1237a342023-04-11 16:22:35 +0200182#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
183
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200184/** Fast quasi-reduction modulo p192k1 = 2^192 - R,
185 * with R = 2^32 + 2^12 + 2^8 + 2^7 + 2^6 + 2^3 + 1 = 0x01000011C9
186 *
187 * \param[in,out] X The address of the MPI to be converted.
188 * Must have exact limb size that stores a 384-bit MPI
189 * (double the bitlength of the modulus).
190 * Upon return holds the reduced value which is
191 * in range `0 <= X < 2 * N` (where N is the modulus).
192 * The bitlength of the reduced value is the same as
193 * that of the modulus (192 bits).
194 * \param[in] X_limbs The length of \p X in limbs.
195 *
196 * \return \c 0 on success.
Gabor Mezeid56e6e02023-05-17 17:51:19 +0200197 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
198 * twice as many limbs as the modulus.
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200199 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
Gabor Mezei1237a342023-04-11 16:22:35 +0200200 */
201MBEDTLS_STATIC_TESTABLE
Gabor Mezeidacfe562023-05-02 14:05:13 +0200202int mbedtls_ecp_mod_p192k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezei1237a342023-04-11 16:22:35 +0200203
204#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
Gabor Mezeie42bb622023-05-02 14:10:57 +0200205
Minos Galanakise5dab972023-04-11 16:42:06 +0100206#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
207
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200208/** Fast quasi-reduction modulo p224k1 = 2^224 - R,
209 * with R = 2^32 + 2^12 + 2^11 + 2^9 + 2^7 + 2^4 + 2 + 1 = 0x0100001A93
210 *
211 * \param[in,out] X The address of the MPI to be converted.
212 * Must have exact limb size that stores a 448-bit MPI
213 * (double the bitlength of the modulus).
214 * Upon return holds the reduced value which is
215 * in range `0 <= X < 2 * N` (where N is the modulus).
216 * The bitlength of the reduced value is the same as
217 * that of the modulus (224 bits).
218 * \param[in] X_limbs The length of \p X in limbs.
219 *
220 * \return \c 0 on success.
Gabor Mezeid56e6e02023-05-17 17:51:19 +0200221 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
222 * twice as many limbs as the modulus.
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200223 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
224 */
Minos Galanakise5dab972023-04-11 16:42:06 +0100225MBEDTLS_STATIC_TESTABLE
Gabor Mezeie42bb622023-05-02 14:10:57 +0200226int mbedtls_ecp_mod_p224k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Minos Galanakise5dab972023-04-11 16:42:06 +0100227
228#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
Gabor Mezei1237a342023-04-11 16:22:35 +0200229
Minos Galanakisd6751dc2023-04-11 17:25:31 +0100230#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
231
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200232/** Fast quasi-reduction modulo p256k1 = 2^256 - R,
233 * with R = 2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1 = 0x01000003D1
234 *
235 * \param[in,out] X The address of the MPI to be converted.
236 * Must have exact limb size that stores a 512-bit MPI
237 * (double the bitlength of the modulus).
238 * Upon return holds the reduced value which is
239 * in range `0 <= X < 2 * N` (where N is the modulus).
240 * The bitlength of the reduced value is the same as
241 * that of the modulus (256 bits).
242 * \param[in] X_limbs The length of \p X in limbs.
243 *
244 * \return \c 0 on success.
Gabor Mezeid56e6e02023-05-17 17:51:19 +0200245 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
246 * twice as many limbs as the modulus.
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200247 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
248 */
Minos Galanakisd6751dc2023-04-11 17:25:31 +0100249MBEDTLS_STATIC_TESTABLE
Gabor Mezei03558b82023-05-02 14:12:25 +0200250int mbedtls_ecp_mod_p256k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Minos Galanakisd6751dc2023-04-11 17:25:31 +0100251
252#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
253
Minos Galanakisd0292c22023-05-10 15:46:47 +0100254#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
255
Minos Galanakis47249fd2023-05-18 16:16:17 +0100256/** Fast quasi-reduction modulo p255 = 2^255 - 19
257 *
258 * \param[in,out] X The address of the MPI to be converted.
259 * Must have exact limb size that stores a 510-bit MPI
260 * (double the bitlength of the modulus).
261 * Upon return holds the reduced value which is
262 * in range `0 <= X < 2 * N` (where N is the modulus).
Minos Galanakis47249fd2023-05-18 16:16:17 +0100263 * \param[in] X_limbs The length of \p X in limbs.
264 *
265 * \return \c 0 on success.
266 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
267 * twice as many limbs as the modulus.
268 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
269 */
Minos Galanakisd0292c22023-05-10 15:46:47 +0100270MBEDTLS_STATIC_TESTABLE
271int mbedtls_ecp_mod_p255_raw(mbedtls_mpi_uint *X, size_t X_limbs);
272
273#endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
274
Paul Elliott47a3c822023-04-23 23:18:50 +0100275#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
276
Paul Elliottee861002023-05-31 12:12:22 +0100277/** Fast quasi-reduction modulo p448 = 2^448 - 2^224 - 1
278 * Write X as A0 + 2^448 A1 and A1 as B0 + 2^224 B1, and return A0 + A1 + B1 +
279 * (B0 + B1) * 2^224.
280 *
281 * \param[in,out] X The address of the MPI to be converted.
282 * Must have exact limb size that stores a 896-bit MPI
283 * (double the bitlength of the modulus). Upon return
284 * holds the reduced value which is in range `0 <= X <
285 * N` (where N is the modulus). The bitlength of the
286 * reduced value is the same as that of the modulus
287 * (448 bits).
288 * \param[in] X_limbs The length of \p X in limbs.
289 *
290 * \return \c 0 on Success.
291 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
292 * twice as many limbs as the modulus.
293 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation
294 * failed.
295 */
Paul Elliott47a3c822023-04-23 23:18:50 +0100296MBEDTLS_STATIC_TESTABLE
Paul Elliotta2e48f72023-06-02 16:00:05 +0100297int mbedtls_ecp_mod_p448_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Paul Elliott47a3c822023-04-23 23:18:50 +0100298
299#endif /* MBEDTLS_ECP_DP_CURVE448_ENABLED */
300
Minos Galanakisa30afe22023-02-15 15:36:29 +0000301/** Initialise a modulus with hard-coded const curve data.
302 *
303 * \note The caller is responsible for the \p N modulus' memory.
304 * mbedtls_mpi_mod_modulus_free(&N) should be invoked at the
305 * end of its lifecycle.
306 *
307 * \param[in,out] N The address of the modulus structure to populate.
308 * Must be initialized.
309 * \param[in] id The mbedtls_ecp_group_id for which to initialise the modulus.
Minos Galanakis1d3e3322023-06-09 14:53:30 +0100310 * \param[in] ctype The mbedtls_ecp_modulus_type identifier for a coordinate modulus (P)
Minos Galanakisa30afe22023-02-15 15:36:29 +0000311 * or a scalar modulus (N).
312 *
313 * \return \c 0 if successful.
314 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the given MPIs do not
315 * have the correct number of limbs.
316 *
317 */
Minos Galanakisdd556922023-02-03 19:12:21 +0000318MBEDTLS_STATIC_TESTABLE
319int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N,
320 const mbedtls_ecp_group_id id,
Minos Galanakis1d3e3322023-06-09 14:53:30 +0100321 const mbedtls_ecp_modulus_type ctype);
Minos Galanakisdd556922023-02-03 19:12:21 +0000322
Gilles Peskine80ba8502021-04-03 20:36:37 +0200323#endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_C */
324
325#endif /* MBEDTLS_ECP_INVASIVE_H */