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