blob: 456ac21c96593c9410d73ed5fce042a6c6fbff66 [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 Mezeif4aab6f2023-06-30 14:50:03 +020043/* Requred macros for ECP split.
44 * If MBEDTLS_ECP_WITH_MPI_UINT is defined the new bignum interface is used.
45 */
Gabor Mezeid6789f12023-07-05 16:08:22 +020046#if !defined(MBEDTLS_ECP_WITH_MPI_UINT)
Gabor Mezeia306d202023-06-06 17:15:52 +020047
Gabor Mezei1a729dc2023-07-05 16:08:57 +020048/* Provide a commented-out definition so that `check_names.py` knows that
49 * it's not a typo.
50 * MBEDTLS_ECP_WITH_MPI_UINT must not be defined within config files, but
51 * only on the command line, as otherwise both ECP implementations will be
52 * built.
53 */
Gabor Mezeif4aab6f2023-06-30 14:50:03 +020054//#define MBEDTLS_ECP_WITH_MPI_UINT
Gabor Mezeia306d202023-06-06 17:15:52 +020055
Gabor Mezeif4aab6f2023-06-30 14:50:03 +020056/* Enable the old bignum interface. */
Gabor Mezeia306d202023-06-06 17:15:52 +020057#define MBEDTLS_ECP_WITH_MPI_STRUCT
58#endif
59
Gabor Mezeic8107072023-06-06 17:24:35 +020060typedef enum {
61 MBEDTLS_ECP_VARIANT_NONE = 0,
62 MBEDTLS_ECP_VARIANT_WITH_MPI_STRUCT = 1,
63 MBEDTLS_ECP_VARIANT_WITH_MPI_UINT = 2
64} mbedtls_ecp_variant;
65
Valerio Setti0c477d32023-04-07 15:54:20 +020066#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine80ba8502021-04-03 20:36:37 +020067
Gabor Mezeic8107072023-06-06 17:24:35 +020068/** Queries the ecp variant.
69 *
70 * \return The id of the ecp variant.
71 */
72MBEDTLS_STATIC_TESTABLE
73mbedtls_ecp_variant mbedtls_ecp_get_variant(void);
74
Gilles Peskine72fcc982021-03-23 22:31:31 +010075#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
76/** Generate a private key on a Montgomery curve (Curve25519 or Curve448).
77 *
78 * This function implements key generation for the set of secret keys
79 * specified in [Curve25519] p. 5 and in [Curve448]. The resulting value
80 * has the lower bits masked but is not necessarily canonical.
81 *
82 * \note - [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
83 * - [RFC7748] https://tools.ietf.org/html/rfc7748
84 *
Gilles Peskine55c46042021-03-24 12:34:40 +010085 * \p high_bit The position of the high-order bit of the key to generate.
Gilles Peskine72fcc982021-03-23 22:31:31 +010086 * This is the bit-size of the key minus 1:
87 * 254 for Curve25519 or 447 for Curve448.
88 * \param d The randomly generated key. This is a number of size
Xiaokang Qiana0896142023-04-18 06:49:55 +000089 * exactly \p high_bit + 1 bits, with the least significant bits
Gilles Peskine72fcc982021-03-23 22:31:31 +010090 * masked as specified in [Curve25519] and in [RFC7748] ยง5.
91 * \param f_rng The RNG function.
92 * \param p_rng The RNG context to be passed to \p f_rng.
93 *
94 * \return \c 0 on success.
95 * \return \c MBEDTLS_ERR_ECP_xxx or MBEDTLS_ERR_MPI_xxx on failure.
96 */
Xiaokang Qiana0896142023-04-18 06:49:55 +000097int mbedtls_ecp_gen_privkey_mx(size_t high_bit,
Gilles Peskine449bd832023-01-11 14:50:10 +010098 mbedtls_mpi *d,
99 int (*f_rng)(void *, unsigned char *, size_t),
100 void *p_rng);
Gilles Peskine72fcc982021-03-23 22:31:31 +0100101
102#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
103
Gabor Mezeideece2b2023-01-25 17:57:36 +0100104#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
105
Gabor Mezei9b290b32023-01-27 11:00:51 +0100106/** Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
107 *
Gabor Mezeia2648312023-02-13 16:29:05 +0100108 * This operation expects a 384 bit MPI and the result of the reduction
109 * is a 192 bit MPI.
110 *
Gabor Mezei9b290b32023-01-27 11:00:51 +0100111 * \param[in,out] Np The address of the MPI to be converted.
Gabor Mezei0b4b8e32023-02-14 16:36:38 +0100112 * Must have twice as many limbs as the modulus.
113 * Upon return this holds the reduced value. The bitlength
114 * of the reduced value is the same as that of the modulus
115 * (192 bits).
Gabor Mezei63aae682023-02-06 16:24:08 +0100116 * \param[in] Nn The length of \p Np in limbs.
Gabor Mezei9b290b32023-01-27 11:00:51 +0100117 */
Gabor Mezeideece2b2023-01-25 17:57:36 +0100118MBEDTLS_STATIC_TESTABLE
Gabor Mezei2038ce92023-01-31 14:33:12 +0100119int mbedtls_ecp_mod_p192_raw(mbedtls_mpi_uint *Np, size_t Nn);
Gabor Mezeideece2b2023-01-25 17:57:36 +0100120
121#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
122
Gabor Mezeie14b5bd2023-02-08 17:23:03 +0100123#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
124
Gabor Mezeia835d202023-02-23 17:38:00 +0100125/** Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
126 *
Gabor Mezei08a94952023-02-28 18:40:57 +0100127 * \param[in,out] X The address of the MPI to be converted.
128 * Must have exact limb size that stores a 448-bit MPI
129 * (double the bitlength of the modulus).
130 * Upon return holds the reduced value which is
131 * in range `0 <= X < 2 * N` (where N is the modulus).
132 * The bitlength of the reduced value is the same as
133 * that of the modulus (224 bits).
134 * \param[in] X_limbs The length of \p X in limbs.
Gabor Mezeia835d202023-02-23 17:38:00 +0100135 *
136 * \return \c 0 on success.
Gabor Mezei08a94952023-02-28 18:40:57 +0100137 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the
138 * limb size that sores a 448-bit MPI.
Gabor Mezeia835d202023-02-23 17:38:00 +0100139 */
Gabor Mezeie14b5bd2023-02-08 17:23:03 +0100140MBEDTLS_STATIC_TESTABLE
Gabor Mezei08a94952023-02-28 18:40:57 +0100141int mbedtls_ecp_mod_p224_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezeie14b5bd2023-02-08 17:23:03 +0100142
143#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
144
Gabor Mezei5221c042023-03-01 16:05:21 +0100145#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
146
Gabor Mezeid1f16b92023-03-08 15:26:32 +0100147/** Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
148 *
149 * \param[in,out] X The address of the MPI to be converted.
150 * Must have exact limb size that stores a 512-bit MPI
151 * (double the bitlength of the modulus).
152 * Upon return holds the reduced value which is
153 * in range `0 <= X < 2 * N` (where N is the modulus).
154 * The bitlength of the reduced value is the same as
155 * that of the modulus (256 bits).
156 * \param[in] X_limbs The length of \p X in limbs.
157 *
158 * \return \c 0 on success.
159 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the
160 * limb size that sores a 512-bit MPI.
161 */
Gabor Mezei5221c042023-03-01 16:05:21 +0100162MBEDTLS_STATIC_TESTABLE
Gabor Mezeied1acf62023-03-01 16:09:13 +0100163int mbedtls_ecp_mod_p256_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezei5221c042023-03-01 16:05:21 +0100164
165#endif
166
Gabor Mezei2cb630e2023-02-01 14:02:16 +0100167#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
168
Gabor Mezeib1c62ca2023-02-06 16:02:05 +0100169/** Fast quasi-reduction modulo p521 = 2^521 - 1 (FIPS 186-3 D.2.5)
170 *
Gabor Mezei7e6fcc12023-02-15 17:51:59 +0100171 * \param[in,out] X The address of the MPI to be converted.
172 * Must have twice as many limbs as the modulus
173 * (the modulus is 521 bits long). Upon return this
174 * holds the reduced value. The reduced value is
175 * in range `0 <= X < 2 * N` (where N is the modulus).
176 * and its the bitlength is one plus the bitlength
177 * of the modulus.
178 * \param[in] X_limbs The length of \p X in limbs.
179 *
180 * \return \c 0 on success.
181 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs does not have
182 * twice as many limbs as the modulus.
Gabor Mezeib1c62ca2023-02-06 16:02:05 +0100183 */
Gabor Mezei2cb630e2023-02-01 14:02:16 +0100184MBEDTLS_STATIC_TESTABLE
Gabor Mezei7e6fcc12023-02-15 17:51:59 +0100185int mbedtls_ecp_mod_p521_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezei2cb630e2023-02-01 14:02:16 +0100186
187#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
188
Minos Galanakis6fb105f2023-02-22 15:28:20 +0000189#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
190
191/** Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
192 *
193 * \param[in,out] X The address of the MPI to be converted.
Minos Galanakisf9fca532023-03-23 10:36:53 +0000194 * Must have exact limb size that stores a 768-bit MPI
195 * (double the bitlength of the modulus).
Minos Galanakis6fb105f2023-02-22 15:28:20 +0000196 * Upon return holds the reduced value which is
197 * in range `0 <= X < 2 * N` (where N is the modulus).
198 * The bitlength of the reduced value is the same as
199 * that of the modulus (384 bits).
200 * \param[in] X_limbs The length of \p N in limbs.
201 *
202 * \return \c 0 on success.
203 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p N_n does not have
204 * twice as many limbs as the modulus.
205 */
206MBEDTLS_STATIC_TESTABLE
207int mbedtls_ecp_mod_p384_raw(mbedtls_mpi_uint *X, size_t X_limbs);
208
209#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
210
Gabor Mezei1237a342023-04-11 16:22:35 +0200211#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
212
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200213/** Fast quasi-reduction modulo p192k1 = 2^192 - R,
214 * with R = 2^32 + 2^12 + 2^8 + 2^7 + 2^6 + 2^3 + 1 = 0x01000011C9
215 *
216 * \param[in,out] X The address of the MPI to be converted.
217 * Must have exact limb size that stores a 384-bit MPI
218 * (double the bitlength of the modulus).
219 * Upon return holds the reduced value which is
220 * in range `0 <= X < 2 * N` (where N is the modulus).
221 * The bitlength of the reduced value is the same as
222 * that of the modulus (192 bits).
223 * \param[in] X_limbs The length of \p X in limbs.
224 *
225 * \return \c 0 on success.
Gabor Mezeid56e6e02023-05-17 17:51:19 +0200226 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
227 * twice as many limbs as the modulus.
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200228 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
Gabor Mezei1237a342023-04-11 16:22:35 +0200229 */
230MBEDTLS_STATIC_TESTABLE
Gabor Mezeidacfe562023-05-02 14:05:13 +0200231int mbedtls_ecp_mod_p192k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezei1237a342023-04-11 16:22:35 +0200232
233#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
Gabor Mezeie42bb622023-05-02 14:10:57 +0200234
Minos Galanakise5dab972023-04-11 16:42:06 +0100235#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
236
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200237/** Fast quasi-reduction modulo p224k1 = 2^224 - R,
238 * with R = 2^32 + 2^12 + 2^11 + 2^9 + 2^7 + 2^4 + 2 + 1 = 0x0100001A93
239 *
240 * \param[in,out] X The address of the MPI to be converted.
241 * Must have exact limb size that stores a 448-bit MPI
242 * (double the bitlength of the modulus).
243 * Upon return holds the reduced value which is
244 * in range `0 <= X < 2 * N` (where N is the modulus).
245 * The bitlength of the reduced value is the same as
246 * that of the modulus (224 bits).
247 * \param[in] X_limbs The length of \p X in limbs.
248 *
249 * \return \c 0 on success.
Gabor Mezeid56e6e02023-05-17 17:51:19 +0200250 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
251 * twice as many limbs as the modulus.
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200252 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
253 */
Minos Galanakise5dab972023-04-11 16:42:06 +0100254MBEDTLS_STATIC_TESTABLE
Gabor Mezeie42bb622023-05-02 14:10:57 +0200255int mbedtls_ecp_mod_p224k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Minos Galanakise5dab972023-04-11 16:42:06 +0100256
257#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
Gabor Mezei1237a342023-04-11 16:22:35 +0200258
Minos Galanakisd6751dc2023-04-11 17:25:31 +0100259#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
260
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200261/** Fast quasi-reduction modulo p256k1 = 2^256 - R,
262 * with R = 2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1 = 0x01000003D1
263 *
264 * \param[in,out] X The address of the MPI to be converted.
265 * Must have exact limb size that stores a 512-bit MPI
266 * (double the bitlength of the modulus).
267 * Upon return holds the reduced value which is
268 * in range `0 <= X < 2 * N` (where N is the modulus).
269 * The bitlength of the reduced value is the same as
270 * that of the modulus (256 bits).
271 * \param[in] X_limbs The length of \p X in limbs.
272 *
273 * \return \c 0 on success.
Gabor Mezeid56e6e02023-05-17 17:51:19 +0200274 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
275 * twice as many limbs as the modulus.
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200276 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
277 */
Minos Galanakisd6751dc2023-04-11 17:25:31 +0100278MBEDTLS_STATIC_TESTABLE
Gabor Mezei03558b82023-05-02 14:12:25 +0200279int mbedtls_ecp_mod_p256k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Minos Galanakisd6751dc2023-04-11 17:25:31 +0100280
281#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
282
Minos Galanakisd0292c22023-05-10 15:46:47 +0100283#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
284
Minos Galanakis47249fd2023-05-18 16:16:17 +0100285/** Fast quasi-reduction modulo p255 = 2^255 - 19
286 *
287 * \param[in,out] X The address of the MPI to be converted.
288 * Must have exact limb size that stores a 510-bit MPI
289 * (double the bitlength of the modulus).
290 * Upon return holds the reduced value which is
291 * in range `0 <= X < 2 * N` (where N is the modulus).
Minos Galanakis47249fd2023-05-18 16:16:17 +0100292 * \param[in] X_limbs The length of \p X in limbs.
293 *
294 * \return \c 0 on success.
295 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
296 * twice as many limbs as the modulus.
297 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
298 */
Minos Galanakisd0292c22023-05-10 15:46:47 +0100299MBEDTLS_STATIC_TESTABLE
300int mbedtls_ecp_mod_p255_raw(mbedtls_mpi_uint *X, size_t X_limbs);
301
302#endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
303
Paul Elliott47a3c822023-04-23 23:18:50 +0100304#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
305
Paul Elliottee861002023-05-31 12:12:22 +0100306/** Fast quasi-reduction modulo p448 = 2^448 - 2^224 - 1
307 * Write X as A0 + 2^448 A1 and A1 as B0 + 2^224 B1, and return A0 + A1 + B1 +
308 * (B0 + B1) * 2^224.
309 *
310 * \param[in,out] X The address of the MPI to be converted.
311 * Must have exact limb size that stores a 896-bit MPI
312 * (double the bitlength of the modulus). Upon return
313 * holds the reduced value which is in range `0 <= X <
314 * N` (where N is the modulus). The bitlength of the
315 * reduced value is the same as that of the modulus
316 * (448 bits).
317 * \param[in] X_limbs The length of \p X in limbs.
318 *
319 * \return \c 0 on Success.
320 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
321 * twice as many limbs as the modulus.
322 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation
323 * failed.
324 */
Paul Elliott47a3c822023-04-23 23:18:50 +0100325MBEDTLS_STATIC_TESTABLE
Paul Elliotta2e48f72023-06-02 16:00:05 +0100326int mbedtls_ecp_mod_p448_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Paul Elliott47a3c822023-04-23 23:18:50 +0100327
328#endif /* MBEDTLS_ECP_DP_CURVE448_ENABLED */
329
Minos Galanakisa30afe22023-02-15 15:36:29 +0000330/** Initialise a modulus with hard-coded const curve data.
331 *
332 * \note The caller is responsible for the \p N modulus' memory.
333 * mbedtls_mpi_mod_modulus_free(&N) should be invoked at the
334 * end of its lifecycle.
335 *
336 * \param[in,out] N The address of the modulus structure to populate.
337 * Must be initialized.
338 * \param[in] id The mbedtls_ecp_group_id for which to initialise the modulus.
Minos Galanakis1d3e3322023-06-09 14:53:30 +0100339 * \param[in] ctype The mbedtls_ecp_modulus_type identifier for a coordinate modulus (P)
Minos Galanakisa30afe22023-02-15 15:36:29 +0000340 * or a scalar modulus (N).
341 *
342 * \return \c 0 if successful.
343 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the given MPIs do not
344 * have the correct number of limbs.
345 *
346 */
Minos Galanakisdd556922023-02-03 19:12:21 +0000347MBEDTLS_STATIC_TESTABLE
348int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N,
349 const mbedtls_ecp_group_id id,
Minos Galanakis1d3e3322023-06-09 14:53:30 +0100350 const mbedtls_ecp_modulus_type ctype);
Minos Galanakisdd556922023-02-03 19:12:21 +0000351
Gilles Peskine80ba8502021-04-03 20:36:37 +0200352#endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_C */
353
354#endif /* MBEDTLS_ECP_INVASIVE_H */