blob: 0a0d361d29fedf7ae79fffb854c5cb70da7cd9ca [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 Mezeia306d202023-06-06 17:15:52 +020043#ifndef MBEDTLS_ECP_WITH_MPI_UINT
44
45#define MBEDTLS_ECP_WITH_MPI_UINT
46#undef MBEDTLS_ECP_WITH_MPI_UINT
47
48#define MBEDTLS_ECP_WITH_MPI_STRUCT
49#endif
50
Gabor Mezeic8107072023-06-06 17:24:35 +020051typedef enum {
52 MBEDTLS_ECP_VARIANT_NONE = 0,
53 MBEDTLS_ECP_VARIANT_WITH_MPI_STRUCT = 1,
54 MBEDTLS_ECP_VARIANT_WITH_MPI_UINT = 2
55} mbedtls_ecp_variant;
56
Valerio Setti0c477d32023-04-07 15:54:20 +020057#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine80ba8502021-04-03 20:36:37 +020058
Gabor Mezeic8107072023-06-06 17:24:35 +020059/** Queries the ecp variant.
60 *
61 * \return The id of the ecp variant.
62 */
63MBEDTLS_STATIC_TESTABLE
64mbedtls_ecp_variant mbedtls_ecp_get_variant(void);
65
Gilles Peskine72fcc982021-03-23 22:31:31 +010066#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
67/** Generate a private key on a Montgomery curve (Curve25519 or Curve448).
68 *
69 * This function implements key generation for the set of secret keys
70 * specified in [Curve25519] p. 5 and in [Curve448]. The resulting value
71 * has the lower bits masked but is not necessarily canonical.
72 *
73 * \note - [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
74 * - [RFC7748] https://tools.ietf.org/html/rfc7748
75 *
Gilles Peskine55c46042021-03-24 12:34:40 +010076 * \p high_bit The position of the high-order bit of the key to generate.
Gilles Peskine72fcc982021-03-23 22:31:31 +010077 * This is the bit-size of the key minus 1:
78 * 254 for Curve25519 or 447 for Curve448.
79 * \param d The randomly generated key. This is a number of size
Xiaokang Qiana0896142023-04-18 06:49:55 +000080 * exactly \p high_bit + 1 bits, with the least significant bits
Gilles Peskine72fcc982021-03-23 22:31:31 +010081 * masked as specified in [Curve25519] and in [RFC7748] ยง5.
82 * \param f_rng The RNG function.
83 * \param p_rng The RNG context to be passed to \p f_rng.
84 *
85 * \return \c 0 on success.
86 * \return \c MBEDTLS_ERR_ECP_xxx or MBEDTLS_ERR_MPI_xxx on failure.
87 */
Xiaokang Qiana0896142023-04-18 06:49:55 +000088int mbedtls_ecp_gen_privkey_mx(size_t high_bit,
Gilles Peskine449bd832023-01-11 14:50:10 +010089 mbedtls_mpi *d,
90 int (*f_rng)(void *, unsigned char *, size_t),
91 void *p_rng);
Gilles Peskine72fcc982021-03-23 22:31:31 +010092
93#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
94
Gabor Mezeideece2b2023-01-25 17:57:36 +010095#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
96
Gabor Mezei9b290b32023-01-27 11:00:51 +010097/** Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
98 *
Gabor Mezeia2648312023-02-13 16:29:05 +010099 * This operation expects a 384 bit MPI and the result of the reduction
100 * is a 192 bit MPI.
101 *
Gabor Mezei9b290b32023-01-27 11:00:51 +0100102 * \param[in,out] Np The address of the MPI to be converted.
Gabor Mezei0b4b8e32023-02-14 16:36:38 +0100103 * Must have twice as many limbs as the modulus.
104 * Upon return this holds the reduced value. The bitlength
105 * of the reduced value is the same as that of the modulus
106 * (192 bits).
Gabor Mezei63aae682023-02-06 16:24:08 +0100107 * \param[in] Nn The length of \p Np in limbs.
Gabor Mezei9b290b32023-01-27 11:00:51 +0100108 */
Gabor Mezeideece2b2023-01-25 17:57:36 +0100109MBEDTLS_STATIC_TESTABLE
Gabor Mezei2038ce92023-01-31 14:33:12 +0100110int mbedtls_ecp_mod_p192_raw(mbedtls_mpi_uint *Np, size_t Nn);
Gabor Mezeideece2b2023-01-25 17:57:36 +0100111
112#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
113
Gabor Mezeie14b5bd2023-02-08 17:23:03 +0100114#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
115
Gabor Mezeia835d202023-02-23 17:38:00 +0100116/** Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
117 *
Gabor Mezei08a94952023-02-28 18:40:57 +0100118 * \param[in,out] X The address of the MPI to be converted.
119 * Must have exact limb size that stores a 448-bit MPI
120 * (double the bitlength of the modulus).
121 * Upon return holds the reduced value which is
122 * in range `0 <= X < 2 * N` (where N is the modulus).
123 * The bitlength of the reduced value is the same as
124 * that of the modulus (224 bits).
125 * \param[in] X_limbs The length of \p X in limbs.
Gabor Mezeia835d202023-02-23 17:38:00 +0100126 *
127 * \return \c 0 on success.
Gabor Mezei08a94952023-02-28 18:40:57 +0100128 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the
129 * limb size that sores a 448-bit MPI.
Gabor Mezeia835d202023-02-23 17:38:00 +0100130 */
Gabor Mezeie14b5bd2023-02-08 17:23:03 +0100131MBEDTLS_STATIC_TESTABLE
Gabor Mezei08a94952023-02-28 18:40:57 +0100132int mbedtls_ecp_mod_p224_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezeie14b5bd2023-02-08 17:23:03 +0100133
134#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
135
Gabor Mezei5221c042023-03-01 16:05:21 +0100136#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
137
Gabor Mezeid1f16b92023-03-08 15:26:32 +0100138/** Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
139 *
140 * \param[in,out] X The address of the MPI to be converted.
141 * Must have exact limb size that stores a 512-bit MPI
142 * (double the bitlength of the modulus).
143 * Upon return holds the reduced value which is
144 * in range `0 <= X < 2 * N` (where N is the modulus).
145 * The bitlength of the reduced value is the same as
146 * that of the modulus (256 bits).
147 * \param[in] X_limbs The length of \p X in limbs.
148 *
149 * \return \c 0 on success.
150 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the
151 * limb size that sores a 512-bit MPI.
152 */
Gabor Mezei5221c042023-03-01 16:05:21 +0100153MBEDTLS_STATIC_TESTABLE
Gabor Mezeied1acf62023-03-01 16:09:13 +0100154int mbedtls_ecp_mod_p256_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezei5221c042023-03-01 16:05:21 +0100155
156#endif
157
Gabor Mezei2cb630e2023-02-01 14:02:16 +0100158#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
159
Gabor Mezeib1c62ca2023-02-06 16:02:05 +0100160/** Fast quasi-reduction modulo p521 = 2^521 - 1 (FIPS 186-3 D.2.5)
161 *
Gabor Mezei7e6fcc12023-02-15 17:51:59 +0100162 * \param[in,out] X The address of the MPI to be converted.
163 * Must have twice as many limbs as the modulus
164 * (the modulus is 521 bits long). Upon return this
165 * holds the reduced value. The reduced value is
166 * in range `0 <= X < 2 * N` (where N is the modulus).
167 * and its the bitlength is one plus the bitlength
168 * of the modulus.
169 * \param[in] X_limbs The length of \p X in limbs.
170 *
171 * \return \c 0 on success.
172 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs does not have
173 * twice as many limbs as the modulus.
Gabor Mezeib1c62ca2023-02-06 16:02:05 +0100174 */
Gabor Mezei2cb630e2023-02-01 14:02:16 +0100175MBEDTLS_STATIC_TESTABLE
Gabor Mezei7e6fcc12023-02-15 17:51:59 +0100176int mbedtls_ecp_mod_p521_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezei2cb630e2023-02-01 14:02:16 +0100177
178#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
179
Minos Galanakis6fb105f2023-02-22 15:28:20 +0000180#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
181
182/** Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
183 *
184 * \param[in,out] X The address of the MPI to be converted.
Minos Galanakisf9fca532023-03-23 10:36:53 +0000185 * Must have exact limb size that stores a 768-bit MPI
186 * (double the bitlength of the modulus).
Minos Galanakis6fb105f2023-02-22 15:28:20 +0000187 * Upon return holds the reduced value which is
188 * in range `0 <= X < 2 * N` (where N is the modulus).
189 * The bitlength of the reduced value is the same as
190 * that of the modulus (384 bits).
191 * \param[in] X_limbs The length of \p N in limbs.
192 *
193 * \return \c 0 on success.
194 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p N_n does not have
195 * twice as many limbs as the modulus.
196 */
197MBEDTLS_STATIC_TESTABLE
198int mbedtls_ecp_mod_p384_raw(mbedtls_mpi_uint *X, size_t X_limbs);
199
200#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
201
Gabor Mezei1237a342023-04-11 16:22:35 +0200202#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
203
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200204/** Fast quasi-reduction modulo p192k1 = 2^192 - R,
205 * with R = 2^32 + 2^12 + 2^8 + 2^7 + 2^6 + 2^3 + 1 = 0x01000011C9
206 *
207 * \param[in,out] X The address of the MPI to be converted.
208 * Must have exact limb size that stores a 384-bit MPI
209 * (double the bitlength of the modulus).
210 * Upon return holds the reduced value which is
211 * in range `0 <= X < 2 * N` (where N is the modulus).
212 * The bitlength of the reduced value is the same as
213 * that of the modulus (192 bits).
214 * \param[in] X_limbs The length of \p X in limbs.
215 *
216 * \return \c 0 on success.
Gabor Mezeid56e6e02023-05-17 17:51:19 +0200217 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
218 * twice as many limbs as the modulus.
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200219 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
Gabor Mezei1237a342023-04-11 16:22:35 +0200220 */
221MBEDTLS_STATIC_TESTABLE
Gabor Mezeidacfe562023-05-02 14:05:13 +0200222int mbedtls_ecp_mod_p192k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezei1237a342023-04-11 16:22:35 +0200223
224#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
Gabor Mezeie42bb622023-05-02 14:10:57 +0200225
Minos Galanakise5dab972023-04-11 16:42:06 +0100226#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
227
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200228/** Fast quasi-reduction modulo p224k1 = 2^224 - R,
229 * with R = 2^32 + 2^12 + 2^11 + 2^9 + 2^7 + 2^4 + 2 + 1 = 0x0100001A93
230 *
231 * \param[in,out] X The address of the MPI to be converted.
232 * Must have exact limb size that stores a 448-bit MPI
233 * (double the bitlength of the modulus).
234 * Upon return holds the reduced value which is
235 * in range `0 <= X < 2 * N` (where N is the modulus).
236 * The bitlength of the reduced value is the same as
237 * that of the modulus (224 bits).
238 * \param[in] X_limbs The length of \p X in limbs.
239 *
240 * \return \c 0 on success.
Gabor Mezeid56e6e02023-05-17 17:51:19 +0200241 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
242 * twice as many limbs as the modulus.
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200243 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
244 */
Minos Galanakise5dab972023-04-11 16:42:06 +0100245MBEDTLS_STATIC_TESTABLE
Gabor Mezeie42bb622023-05-02 14:10:57 +0200246int mbedtls_ecp_mod_p224k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Minos Galanakise5dab972023-04-11 16:42:06 +0100247
248#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
Gabor Mezei1237a342023-04-11 16:22:35 +0200249
Minos Galanakisd6751dc2023-04-11 17:25:31 +0100250#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
251
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200252/** Fast quasi-reduction modulo p256k1 = 2^256 - R,
253 * with R = 2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1 = 0x01000003D1
254 *
255 * \param[in,out] X The address of the MPI to be converted.
256 * Must have exact limb size that stores a 512-bit MPI
257 * (double the bitlength of the modulus).
258 * Upon return holds the reduced value which is
259 * in range `0 <= X < 2 * N` (where N is the modulus).
260 * The bitlength of the reduced value is the same as
261 * that of the modulus (256 bits).
262 * \param[in] X_limbs The length of \p X in limbs.
263 *
264 * \return \c 0 on success.
Gabor Mezeid56e6e02023-05-17 17:51:19 +0200265 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
266 * twice as many limbs as the modulus.
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200267 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
268 */
Minos Galanakisd6751dc2023-04-11 17:25:31 +0100269MBEDTLS_STATIC_TESTABLE
Gabor Mezei03558b82023-05-02 14:12:25 +0200270int mbedtls_ecp_mod_p256k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Minos Galanakisd6751dc2023-04-11 17:25:31 +0100271
272#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
273
Minos Galanakisd0292c22023-05-10 15:46:47 +0100274#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
275
Minos Galanakis47249fd2023-05-18 16:16:17 +0100276/** Fast quasi-reduction modulo p255 = 2^255 - 19
277 *
278 * \param[in,out] X The address of the MPI to be converted.
279 * Must have exact limb size that stores a 510-bit MPI
280 * (double the bitlength of the modulus).
281 * Upon return holds the reduced value which is
282 * in range `0 <= X < 2 * N` (where N is the modulus).
Minos Galanakis47249fd2023-05-18 16:16:17 +0100283 * \param[in] X_limbs The length of \p X in limbs.
284 *
285 * \return \c 0 on success.
286 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
287 * twice as many limbs as the modulus.
288 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
289 */
Minos Galanakisd0292c22023-05-10 15:46:47 +0100290MBEDTLS_STATIC_TESTABLE
291int mbedtls_ecp_mod_p255_raw(mbedtls_mpi_uint *X, size_t X_limbs);
292
293#endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
294
Paul Elliott47a3c822023-04-23 23:18:50 +0100295#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
296
Paul Elliottee861002023-05-31 12:12:22 +0100297/** Fast quasi-reduction modulo p448 = 2^448 - 2^224 - 1
298 * Write X as A0 + 2^448 A1 and A1 as B0 + 2^224 B1, and return A0 + A1 + B1 +
299 * (B0 + B1) * 2^224.
300 *
301 * \param[in,out] X The address of the MPI to be converted.
302 * Must have exact limb size that stores a 896-bit MPI
303 * (double the bitlength of the modulus). Upon return
304 * holds the reduced value which is in range `0 <= X <
305 * N` (where N is the modulus). The bitlength of the
306 * reduced value is the same as that of the modulus
307 * (448 bits).
308 * \param[in] X_limbs The length of \p X in limbs.
309 *
310 * \return \c 0 on Success.
311 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
312 * twice as many limbs as the modulus.
313 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation
314 * failed.
315 */
Paul Elliott47a3c822023-04-23 23:18:50 +0100316MBEDTLS_STATIC_TESTABLE
Paul Elliotta2e48f72023-06-02 16:00:05 +0100317int mbedtls_ecp_mod_p448_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Paul Elliott47a3c822023-04-23 23:18:50 +0100318
319#endif /* MBEDTLS_ECP_DP_CURVE448_ENABLED */
320
Minos Galanakisa30afe22023-02-15 15:36:29 +0000321/** Initialise a modulus with hard-coded const curve data.
322 *
323 * \note The caller is responsible for the \p N modulus' memory.
324 * mbedtls_mpi_mod_modulus_free(&N) should be invoked at the
325 * end of its lifecycle.
326 *
327 * \param[in,out] N The address of the modulus structure to populate.
328 * Must be initialized.
329 * \param[in] id The mbedtls_ecp_group_id for which to initialise the modulus.
Minos Galanakis1d3e3322023-06-09 14:53:30 +0100330 * \param[in] ctype The mbedtls_ecp_modulus_type identifier for a coordinate modulus (P)
Minos Galanakisa30afe22023-02-15 15:36:29 +0000331 * or a scalar modulus (N).
332 *
333 * \return \c 0 if successful.
334 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the given MPIs do not
335 * have the correct number of limbs.
336 *
337 */
Minos Galanakisdd556922023-02-03 19:12:21 +0000338MBEDTLS_STATIC_TESTABLE
339int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N,
340 const mbedtls_ecp_group_id id,
Minos Galanakis1d3e3322023-06-09 14:53:30 +0100341 const mbedtls_ecp_modulus_type ctype);
Minos Galanakisdd556922023-02-03 19:12:21 +0000342
Gilles Peskine80ba8502021-04-03 20:36:37 +0200343#endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_C */
344
345#endif /* MBEDTLS_ECP_INVASIVE_H */