blob: aadcdbc78e6f275183854f9d2cefaccae37818ac [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
Valerio Setti0c477d32023-04-07 15:54:20 +020034#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine80ba8502021-04-03 20:36:37 +020035
Gilles Peskine72fcc982021-03-23 22:31:31 +010036#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
37/** Generate a private key on a Montgomery curve (Curve25519 or Curve448).
38 *
39 * This function implements key generation for the set of secret keys
40 * specified in [Curve25519] p. 5 and in [Curve448]. The resulting value
41 * has the lower bits masked but is not necessarily canonical.
42 *
43 * \note - [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
44 * - [RFC7748] https://tools.ietf.org/html/rfc7748
45 *
Gilles Peskine55c46042021-03-24 12:34:40 +010046 * \p high_bit The position of the high-order bit of the key to generate.
Gilles Peskine72fcc982021-03-23 22:31:31 +010047 * This is the bit-size of the key minus 1:
48 * 254 for Curve25519 or 447 for Curve448.
49 * \param d The randomly generated key. This is a number of size
Xiaokang Qiana0896142023-04-18 06:49:55 +000050 * exactly \p high_bit + 1 bits, with the least significant bits
Gilles Peskine72fcc982021-03-23 22:31:31 +010051 * masked as specified in [Curve25519] and in [RFC7748] ยง5.
52 * \param f_rng The RNG function.
53 * \param p_rng The RNG context to be passed to \p f_rng.
54 *
55 * \return \c 0 on success.
56 * \return \c MBEDTLS_ERR_ECP_xxx or MBEDTLS_ERR_MPI_xxx on failure.
57 */
Xiaokang Qiana0896142023-04-18 06:49:55 +000058int mbedtls_ecp_gen_privkey_mx(size_t high_bit,
Gilles Peskine449bd832023-01-11 14:50:10 +010059 mbedtls_mpi *d,
60 int (*f_rng)(void *, unsigned char *, size_t),
61 void *p_rng);
Gilles Peskine72fcc982021-03-23 22:31:31 +010062
63#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
64
Gabor Mezeideece2b2023-01-25 17:57:36 +010065#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
66
Gabor Mezei9b290b32023-01-27 11:00:51 +010067/** Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
68 *
Gabor Mezeia2648312023-02-13 16:29:05 +010069 * This operation expects a 384 bit MPI and the result of the reduction
70 * is a 192 bit MPI.
71 *
Gabor Mezei9b290b32023-01-27 11:00:51 +010072 * \param[in,out] Np The address of the MPI to be converted.
Gabor Mezei0b4b8e32023-02-14 16:36:38 +010073 * Must have twice as many limbs as the modulus.
74 * Upon return this holds the reduced value. The bitlength
75 * of the reduced value is the same as that of the modulus
76 * (192 bits).
Gabor Mezei63aae682023-02-06 16:24:08 +010077 * \param[in] Nn The length of \p Np in limbs.
Gabor Mezei9b290b32023-01-27 11:00:51 +010078 */
Gabor Mezeideece2b2023-01-25 17:57:36 +010079MBEDTLS_STATIC_TESTABLE
Gabor Mezei2038ce92023-01-31 14:33:12 +010080int mbedtls_ecp_mod_p192_raw(mbedtls_mpi_uint *Np, size_t Nn);
Gabor Mezeideece2b2023-01-25 17:57:36 +010081
82#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
83
Gabor Mezeie14b5bd2023-02-08 17:23:03 +010084#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
85
Gabor Mezeia835d202023-02-23 17:38:00 +010086/** Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
87 *
Gabor Mezei08a94952023-02-28 18:40:57 +010088 * \param[in,out] X The address of the MPI to be converted.
89 * Must have exact limb size that stores a 448-bit MPI
90 * (double the bitlength of the modulus).
91 * Upon return holds the reduced value which is
92 * in range `0 <= X < 2 * N` (where N is the modulus).
93 * The bitlength of the reduced value is the same as
94 * that of the modulus (224 bits).
95 * \param[in] X_limbs The length of \p X in limbs.
Gabor Mezeia835d202023-02-23 17:38:00 +010096 *
97 * \return \c 0 on success.
Gabor Mezei08a94952023-02-28 18:40:57 +010098 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the
99 * limb size that sores a 448-bit MPI.
Gabor Mezeia835d202023-02-23 17:38:00 +0100100 */
Gabor Mezeie14b5bd2023-02-08 17:23:03 +0100101MBEDTLS_STATIC_TESTABLE
Gabor Mezei08a94952023-02-28 18:40:57 +0100102int mbedtls_ecp_mod_p224_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezeie14b5bd2023-02-08 17:23:03 +0100103
104#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
105
Gabor Mezei5221c042023-03-01 16:05:21 +0100106#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
107
Gabor Mezeid1f16b92023-03-08 15:26:32 +0100108/** Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
109 *
110 * \param[in,out] X The address of the MPI to be converted.
111 * Must have exact limb size that stores a 512-bit MPI
112 * (double the bitlength of the modulus).
113 * Upon return holds the reduced value which is
114 * in range `0 <= X < 2 * N` (where N is the modulus).
115 * The bitlength of the reduced value is the same as
116 * that of the modulus (256 bits).
117 * \param[in] X_limbs The length of \p X in limbs.
118 *
119 * \return \c 0 on success.
120 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the
121 * limb size that sores a 512-bit MPI.
122 */
Gabor Mezei5221c042023-03-01 16:05:21 +0100123MBEDTLS_STATIC_TESTABLE
Gabor Mezeied1acf62023-03-01 16:09:13 +0100124int mbedtls_ecp_mod_p256_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezei5221c042023-03-01 16:05:21 +0100125
126#endif
127
Gabor Mezei2cb630e2023-02-01 14:02:16 +0100128#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
129
Gabor Mezeib1c62ca2023-02-06 16:02:05 +0100130/** Fast quasi-reduction modulo p521 = 2^521 - 1 (FIPS 186-3 D.2.5)
131 *
Gabor Mezei7e6fcc12023-02-15 17:51:59 +0100132 * \param[in,out] X The address of the MPI to be converted.
133 * Must have twice as many limbs as the modulus
134 * (the modulus is 521 bits long). Upon return this
135 * holds the reduced value. The reduced value is
136 * in range `0 <= X < 2 * N` (where N is the modulus).
137 * and its the bitlength is one plus the bitlength
138 * of the modulus.
139 * \param[in] X_limbs The length of \p X in limbs.
140 *
141 * \return \c 0 on success.
142 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs does not have
143 * twice as many limbs as the modulus.
Gabor Mezeib1c62ca2023-02-06 16:02:05 +0100144 */
Gabor Mezei2cb630e2023-02-01 14:02:16 +0100145MBEDTLS_STATIC_TESTABLE
Gabor Mezei7e6fcc12023-02-15 17:51:59 +0100146int mbedtls_ecp_mod_p521_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezei2cb630e2023-02-01 14:02:16 +0100147
148#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
149
Minos Galanakis6fb105f2023-02-22 15:28:20 +0000150#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
151
152/** Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
153 *
154 * \param[in,out] X The address of the MPI to be converted.
Minos Galanakisf9fca532023-03-23 10:36:53 +0000155 * Must have exact limb size that stores a 768-bit MPI
156 * (double the bitlength of the modulus).
Minos Galanakis6fb105f2023-02-22 15:28:20 +0000157 * Upon return holds the reduced value which is
158 * in range `0 <= X < 2 * N` (where N is the modulus).
159 * The bitlength of the reduced value is the same as
160 * that of the modulus (384 bits).
161 * \param[in] X_limbs The length of \p N in limbs.
162 *
163 * \return \c 0 on success.
164 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p N_n does not have
165 * twice as many limbs as the modulus.
166 */
167MBEDTLS_STATIC_TESTABLE
168int mbedtls_ecp_mod_p384_raw(mbedtls_mpi_uint *X, size_t X_limbs);
169
170#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
171
Gabor Mezei1237a342023-04-11 16:22:35 +0200172#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
173
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200174/** Fast quasi-reduction modulo p192k1 = 2^192 - R,
175 * with R = 2^32 + 2^12 + 2^8 + 2^7 + 2^6 + 2^3 + 1 = 0x01000011C9
176 *
177 * \param[in,out] X The address of the MPI to be converted.
178 * Must have exact limb size that stores a 384-bit MPI
179 * (double the bitlength of the modulus).
180 * Upon return holds the reduced value which is
181 * in range `0 <= X < 2 * N` (where N is the modulus).
182 * The bitlength of the reduced value is the same as
183 * that of the modulus (192 bits).
184 * \param[in] X_limbs The length of \p X in limbs.
185 *
186 * \return \c 0 on success.
Gabor Mezeid56e6e02023-05-17 17:51:19 +0200187 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
188 * twice as many limbs as the modulus.
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200189 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
Gabor Mezei1237a342023-04-11 16:22:35 +0200190 */
191MBEDTLS_STATIC_TESTABLE
Gabor Mezeidacfe562023-05-02 14:05:13 +0200192int mbedtls_ecp_mod_p192k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezei1237a342023-04-11 16:22:35 +0200193
194#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
Gabor Mezeie42bb622023-05-02 14:10:57 +0200195
Minos Galanakise5dab972023-04-11 16:42:06 +0100196#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
197
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200198/** Fast quasi-reduction modulo p224k1 = 2^224 - R,
199 * with R = 2^32 + 2^12 + 2^11 + 2^9 + 2^7 + 2^4 + 2 + 1 = 0x0100001A93
200 *
201 * \param[in,out] X The address of the MPI to be converted.
202 * Must have exact limb size that stores a 448-bit MPI
203 * (double the bitlength of the modulus).
204 * Upon return holds the reduced value which is
205 * in range `0 <= X < 2 * N` (where N is the modulus).
206 * The bitlength of the reduced value is the same as
207 * that of the modulus (224 bits).
208 * \param[in] X_limbs The length of \p X in limbs.
209 *
210 * \return \c 0 on success.
Gabor Mezeid56e6e02023-05-17 17:51:19 +0200211 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
212 * twice as many limbs as the modulus.
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200213 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
214 */
Minos Galanakise5dab972023-04-11 16:42:06 +0100215MBEDTLS_STATIC_TESTABLE
Gabor Mezeie42bb622023-05-02 14:10:57 +0200216int mbedtls_ecp_mod_p224k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Minos Galanakise5dab972023-04-11 16:42:06 +0100217
218#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
Gabor Mezei1237a342023-04-11 16:22:35 +0200219
Minos Galanakisd6751dc2023-04-11 17:25:31 +0100220#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
221
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200222/** Fast quasi-reduction modulo p256k1 = 2^256 - R,
223 * with R = 2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1 = 0x01000003D1
224 *
225 * \param[in,out] X The address of the MPI to be converted.
226 * Must have exact limb size that stores a 512-bit MPI
227 * (double the bitlength of the modulus).
228 * Upon return holds the reduced value which is
229 * in range `0 <= X < 2 * N` (where N is the modulus).
230 * The bitlength of the reduced value is the same as
231 * that of the modulus (256 bits).
232 * \param[in] X_limbs The length of \p X in limbs.
233 *
234 * \return \c 0 on success.
Gabor Mezeid56e6e02023-05-17 17:51:19 +0200235 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
236 * twice as many limbs as the modulus.
Gabor Mezeifa3f7412023-05-17 17:35:47 +0200237 * \return #MBEDTLS_ERR_ECP_ALLOC_FAILED if memory allocation failed.
238 */
Minos Galanakisd6751dc2023-04-11 17:25:31 +0100239MBEDTLS_STATIC_TESTABLE
Gabor Mezei03558b82023-05-02 14:12:25 +0200240int mbedtls_ecp_mod_p256k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Minos Galanakisd6751dc2023-04-11 17:25:31 +0100241
242#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
243
Paul Elliott47a3c822023-04-23 23:18:50 +0100244#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
245
246MBEDTLS_STATIC_TESTABLE
247int mbedtls_ecp_mod_p448(mbedtls_mpi *N);
248
249#endif /* MBEDTLS_ECP_DP_CURVE448_ENABLED */
250
Minos Galanakisa30afe22023-02-15 15:36:29 +0000251/** Initialise a modulus with hard-coded const curve data.
252 *
253 * \note The caller is responsible for the \p N modulus' memory.
254 * mbedtls_mpi_mod_modulus_free(&N) should be invoked at the
255 * end of its lifecycle.
256 *
257 * \param[in,out] N The address of the modulus structure to populate.
258 * Must be initialized.
259 * \param[in] id The mbedtls_ecp_group_id for which to initialise the modulus.
260 * \param[in] ctype The mbedtls_ecp_curve_type identifier for a coordinate modulus (P)
261 * or a scalar modulus (N).
262 *
263 * \return \c 0 if successful.
264 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the given MPIs do not
265 * have the correct number of limbs.
266 *
267 */
Minos Galanakisdd556922023-02-03 19:12:21 +0000268MBEDTLS_STATIC_TESTABLE
269int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N,
270 const mbedtls_ecp_group_id id,
271 const mbedtls_ecp_curve_type ctype);
272
Gilles Peskine80ba8502021-04-03 20:36:37 +0200273#endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_C */
274
275#endif /* MBEDTLS_ECP_INVASIVE_H */