scorpiox-bot-plugin-fetch
←ecp_invasive.hc
305 lines · 12 KBHEADBlameDownload
1/**
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 OR GPL-2.0-or-later
13 */
14#ifndef MBEDTLS_ECP_INVASIVE_H
15#define MBEDTLS_ECP_INVASIVE_H
16
17#include "common.h"
18#include "mbedtls/bignum.h"
19#include "bignum_mod.h"
20#include "mbedtls/ecp.h"
21
22/*
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
31#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ECP_LIGHT)
32
33#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
34/** Generate a private key on a Montgomery curve (Curve25519 or Curve448).
35 *
36 * This function implements key generation for the set of secret keys
37 * specified in [Curve25519] p. 5 and in [Curve448]. The resulting value
38 * has the lower bits masked but is not necessarily canonical.
39 *
40 * \note - [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
41 * - [RFC7748] https://tools.ietf.org/html/rfc7748
42 *
43 * \p high_bit The position of the high-order bit of the key to generate.
44 * This is the bit-size of the key minus 1:
45 * 254 for Curve25519 or 447 for Curve448.
46 * \param d The randomly generated key. This is a number of size
47 * exactly \p high_bit + 1 bits, with the least significant bits
48 * masked as specified in [Curve25519] and in [RFC7748] §5.
49 * \param f_rng The RNG function.
50 * \param p_rng The RNG context to be passed to \p f_rng.
51 *
52 * \return \c 0 on success.
53 * \return \c MBEDTLS_ERR_ECP_xxx or MBEDTLS_ERR_MPI_xxx on failure.
54 */
55int mbedtls_ecp_gen_privkey_mx(size_t high_bit,
56 mbedtls_mpi *d,
57 int (*f_rng)(void *, unsigned char *, size_t),
58 void *p_rng);
59
60#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
61
62#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
63
64/** Fast quasi-reduction modulo p192
65 *
66 * This operation expects a 384 bit MPI and the result of the reduction
67 * is a 192 bit MPI.
68 *
69 * \param[in,out] Np The address of the MPI to be converted.
70 * Must have twice as many limbs as the modulus.
71 * Upon return this holds the reduced value. The bitlength
72 * of the reduced value is the same as that of the modulus
73 * (192 bits).
74 * \param[in] Nn The length of \p Np in limbs.
75 */
76MBEDTLS_STATIC_TESTABLE
77int mbedtls_ecp_mod_p192_raw(mbedtls_mpi_uint *Np, size_t Nn);
78
79#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
80
81#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
82
83/** Fast quasi-reduction modulo p224
84 *
85 * \param[in,out] X The address of the MPI to be converted.
86 * Must have exact limb size that stores a 448-bit MPI
87 * (double the bitlength of the modulus).
88 * Upon return holds the reduced value which is
89 * in range `0 <= X < 2 * N` (where N is the modulus).
90 * The bitlength of the reduced value is the same as
91 * that of the modulus (224 bits).
92 * \param[in] X_limbs The length of \p X in limbs.
93 *
94 * \return \c 0 on success.
95 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the
96 * limb size that sores a 448-bit MPI.
97 */
98MBEDTLS_STATIC_TESTABLE
99int mbedtls_ecp_mod_p224_raw(mbedtls_mpi_uint *X, size_t X_limbs);
100
101#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
102
103#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
104
105/** Fast quasi-reduction modulo p256
106 *
107 * \param[in,out] X The address of the MPI to be converted.
108 * Must have exact limb size that stores a 512-bit MPI
109 * (double the bitlength of the modulus).
110 * Upon return holds the reduced value which is
111 * in range `0 <= X < 2 * N` (where N is the modulus).
112 * The bitlength of the reduced value is the same as
113 * that of the modulus (256 bits).
114 * \param[in] X_limbs The length of \p X in limbs.
115 *
116 * \return \c 0 on success.
117 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the
118 * limb size that sores a 512-bit MPI.
119 */
120MBEDTLS_STATIC_TESTABLE
121int mbedtls_ecp_mod_p256_raw(mbedtls_mpi_uint *X, size_t X_limbs);
122
123#endif
124
125#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
126
127/** Fast quasi-reduction modulo p521 = 2^521 - 1
128 *
129 * \param[in,out] X The address of the MPI to be converted.
130 * Must have twice as many limbs as the modulus
131 * (the modulus is 521 bits long). Upon return this
132 * holds the reduced value. The reduced value is
133 * in range `0 <= X < 2 * N` (where N is the modulus).
134 * and its the bitlength is one plus the bitlength
135 * of the modulus.
136 * \param[in] X_limbs The length of \p X in limbs.
137 *
138 * \return \c 0 on success.
139 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs does not have
140 * twice as many limbs as the modulus.
141 */
142MBEDTLS_STATIC_TESTABLE
143int mbedtls_ecp_mod_p521_raw(mbedtls_mpi_uint *X, size_t X_limbs);
144
145#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
146
147#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
148
149/** Fast quasi-reduction modulo p384
150 *
151 * \param[in,out] X The address of the MPI to be converted.
152 * Must have exact limb size that stores a 768-bit MPI
153 * (double the bitlength of the modulus).
154 * Upon return holds the reduced value which is
155 * in range `0 <= X < 2 * N` (where N is the modulus).
156 * The bitlength of the reduced value is the same as
157 * that of the modulus (384 bits).
158 * \param[in] X_limbs The length of \p N in limbs.
159 *
160 * \return \c 0 on success.
161 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p N_n does not have
162 * twice as many limbs as the modulus.
163 */
164MBEDTLS_STATIC_TESTABLE
165int mbedtls_ecp_mod_p384_raw(mbedtls_mpi_uint *X, size_t X_limbs);
166
167#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
168
169#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
170
171/** Fast quasi-reduction modulo p192k1 = 2^192 - 0x01000011C9
172 *
173 * \param[in,out] X The address of the MPI to be converted.
174 * Must have exact limb size that stores a 384-bit MPI
175 * (double the bitlength of the modulus).
176 * Upon return holds the reduced value which is
177 * in range `0 <= X < 2 * N` (where N is the modulus).
178 * The bitlength of the reduced value is the same as
179 * that of the modulus (192 bits).
180 * \param[in] X_limbs The length of \p X in limbs.
181 *
182 * \return \c 0 on success.
183 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
184 * twice as many limbs as the modulus.
185 */
186MBEDTLS_STATIC_TESTABLE
187int mbedtls_ecp_mod_p192k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
188
189#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
190
191#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
192
193/** Fast quasi-reduction modulo p224k1 = 2^224 - 0x0100001A93
194 *
195 * \param[in,out] X The address of the MPI to be converted.
196 * Must have exact limb size that stores a 448-bit MPI
197 * (double the bitlength of the modulus).
198 * Upon return holds the reduced value which is
199 * in range `0 <= X < 2 * N` (where N is the modulus).
200 * The bitlength of the reduced value is the same as
201 * that of the modulus (224 bits).
202 * \param[in] X_limbs The length of \p X in limbs.
203 *
204 * \return \c 0 on success.
205 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
206 * twice as many limbs as the modulus.
207 */
208MBEDTLS_STATIC_TESTABLE
209int mbedtls_ecp_mod_p224k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
210
211#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
212
213#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
214
215/** Fast quasi-reduction modulo p256k1 = 2^256 - 0x01000003D1
216 *
217 * \param[in,out] X The address of the MPI to be converted.
218 * Must have exact limb size that stores a 512-bit MPI
219 * (double the bitlength of the modulus).
220 * Upon return holds the reduced value which is
221 * in range `0 <= X < 2 * N` (where N is the modulus).
222 * The bitlength of the reduced value is the same as
223 * that of the modulus (256 bits).
224 * \param[in] X_limbs The length of \p X in limbs.
225 *
226 * \return \c 0 on success.
227 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
228 * twice as many limbs as the modulus.
229 */
230MBEDTLS_STATIC_TESTABLE
231int mbedtls_ecp_mod_p256k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
232
233#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
234
235#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
236
237/** Fast quasi-reduction modulo p255 = 2^255 - 19
238 *
239 * \param[in,out] X The address of the MPI to be converted.
240 * Must have exact limb size that stores a 510-bit MPI
241 * (double the bitlength of the modulus).
242 * Upon return holds the reduced value which is
243 * in range `0 <= X < 2 * N` (where N is the modulus).
244 * The bitlength of the reduced value is at most 256
245 * (that is, 1 more than that of the modulus).
246 * \param[in] X_limbs The length of \p X in limbs.
247 *
248 * \return \c 0 on success.
249 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
250 * twice as many limbs as the modulus.
251 */
252MBEDTLS_STATIC_TESTABLE
253int mbedtls_ecp_mod_p255_raw(mbedtls_mpi_uint *X, size_t X_limbs);
254
255#endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
256
257#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
258
259/** Fast quasi-reduction modulo p448 = 2^448 - 2^224 - 1
260 *
261 * \param[in,out] X The address of the MPI to be converted.
262 * Must have exact limb size that stores a 896-bit MPI
263 * (double the bitlength of the modulus).
264 * Upon return holds the reduced value which is in
265 * range `0 <= X < 2 * N` (where N is the modulus).
266 * The bitlength of the reduced value is the same as
267 * that of the modulus (448 bits).
268 * \param[in] X_limbs The length of \p X in limbs.
269 *
270 * \return \c 0 on Success.
271 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
272 * twice as many limbs as the modulus.
273 */
274MBEDTLS_STATIC_TESTABLE
275int mbedtls_ecp_mod_p448_raw(mbedtls_mpi_uint *X, size_t X_limbs);
276
277#endif /* MBEDTLS_ECP_DP_CURVE448_ENABLED */
278
279#if defined(MBEDTLS_ECP_WITH_MPI_UINT)
280/** Initialise a modulus with hard-coded const curve data.
281 *
282 * \note The caller is responsible for the \p N modulus' memory.
283 * mbedtls_mpi_mod_modulus_free(&N) should be invoked at the
284 * end of its lifecycle.
285 *
286 * \param[in,out] N The address of the modulus structure to populate.
287 * Must be initialized.
288 * \param[in] id The mbedtls_ecp_group_id for which to initialise the modulus.
289 * \param[in] ctype The mbedtls_ecp_modulus_type identifier for a coordinate modulus (P)
290 * or a scalar modulus (N).
291 *
292 * \return \c 0 if successful.
293 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the given MPIs do not
294 * have the correct number of limbs.
295 *
296 */
297MBEDTLS_STATIC_TESTABLE
298int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N,
299 const mbedtls_ecp_group_id id,
300 const mbedtls_ecp_modulus_type ctype);
301#endif /* MBEDTLS_ECP_WITH_MPI_UINT */
302
303#endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_LIGHT */
304
305#endif /* MBEDTLS_ECP_INVASIVE_H */