Skip to content
0.2.0-dev Download ZIP
220 lines ·9.0 KB Raw
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 TF_PSA_CRYPTO_ECP_INVASIVE_H
15#define TF_PSA_CRYPTO_ECP_INVASIVE_H
16
17#include "tf_psa_crypto_common.h"
18#include "mbedtls/private/bignum.h"
19#include "bignum_mod.h"
20#include "mbedtls/private/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_SECP256R1_ENABLED)
63
64/** Fast quasi-reduction modulo p256
65 *
66 * \param[in,out] X The address of the MPI to be converted.
67 * Must have exact limb size that stores a 512-bit MPI
68 * (double the bitlength of the modulus).
69 * Upon return holds the reduced value which is
70 * in range `0 <= X < 2 * N` (where N is the modulus).
71 * The bitlength of the reduced value is the same as
72 * that of the modulus (256 bits).
73 * \param[in] X_limbs The length of \p X in limbs.
74 *
75 * \return \c 0 on success.
76 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the
77 * limb size that sores a 512-bit MPI.
78 */
79MBEDTLS_STATIC_TESTABLE
80int mbedtls_ecp_mod_p256_raw(mbedtls_mpi_uint *X, size_t X_limbs);
81
82#endif
83
84#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
85
86/** Fast quasi-reduction modulo p521 = 2^521 - 1
87 *
88 * \param[in,out] X The address of the MPI to be converted.
89 * Must have twice as many limbs as the modulus
90 * (the modulus is 521 bits long). Upon return this
91 * holds the reduced value. The reduced value is
92 * in range `0 <= X < 2 * N` (where N is the modulus).
93 * and its the bitlength is one plus the bitlength
94 * of the modulus.
95 * \param[in] X_limbs The length of \p X in limbs.
96 *
97 * \return \c 0 on success.
98 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs does not have
99 * twice as many limbs as the modulus.
100 */
101MBEDTLS_STATIC_TESTABLE
102int mbedtls_ecp_mod_p521_raw(mbedtls_mpi_uint *X, size_t X_limbs);
103
104#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
105
106#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
107
108/** Fast quasi-reduction modulo p384
109 *
110 * \param[in,out] X The address of the MPI to be converted.
111 * Must have exact limb size that stores a 768-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 (384 bits).
117 * \param[in] X_limbs The length of \p N in limbs.
118 *
119 * \return \c 0 on success.
120 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p N_n does not have
121 * twice as many limbs as the modulus.
122 */
123MBEDTLS_STATIC_TESTABLE
124int mbedtls_ecp_mod_p384_raw(mbedtls_mpi_uint *X, size_t X_limbs);
125
126#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
127
128#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
129
130/** Fast quasi-reduction modulo p256k1 = 2^256 - 0x01000003D1
131 *
132 * \param[in,out] X The address of the MPI to be converted.
133 * Must have exact limb size that stores a 512-bit MPI
134 * (double the bitlength of the modulus).
135 * Upon return holds the reduced value which is
136 * in range `0 <= X < 2 * N` (where N is the modulus).
137 * The bitlength of the reduced value is the same as
138 * that of the modulus (256 bits).
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 does not have
143 * twice as many limbs as the modulus.
144 */
145MBEDTLS_STATIC_TESTABLE
146int mbedtls_ecp_mod_p256k1_raw(mbedtls_mpi_uint *X, size_t X_limbs);
147
148#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
149
150#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
151
152/** Fast quasi-reduction modulo p255 = 2^255 - 19
153 *
154 * \param[in,out] X The address of the MPI to be converted.
155 * Must have exact limb size that stores a 510-bit MPI
156 * (double the bitlength of the modulus).
157 * 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 at most 256
160 * (that is, 1 more than that of the modulus).
161 * \param[in] X_limbs The length of \p X in limbs.
162 *
163 * \return \c 0 on success.
164 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
165 * twice as many limbs as the modulus.
166 */
167MBEDTLS_STATIC_TESTABLE
168int mbedtls_ecp_mod_p255_raw(mbedtls_mpi_uint *X, size_t X_limbs);
169
170#endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
171
172#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
173
174/** Fast quasi-reduction modulo p448 = 2^448 - 2^224 - 1
175 *
176 * \param[in,out] X The address of the MPI to be converted.
177 * Must have exact limb size that stores a 896-bit MPI
178 * (double the bitlength of the modulus).
179 * Upon return holds the reduced value which is in
180 * range `0 <= X < 2 * N` (where N is the modulus).
181 * The bitlength of the reduced value is the same as
182 * that of the modulus (448 bits).
183 * \param[in] X_limbs The length of \p X in limbs.
184 *
185 * \return \c 0 on Success.
186 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X does not have
187 * twice as many limbs as the modulus.
188 */
189MBEDTLS_STATIC_TESTABLE
190int mbedtls_ecp_mod_p448_raw(mbedtls_mpi_uint *X, size_t X_limbs);
191
192#endif /* MBEDTLS_ECP_DP_CURVE448_ENABLED */
193
194#if defined(MBEDTLS_ECP_WITH_MPI_UINT)
195/** Initialise a modulus with hard-coded const curve data.
196 *
197 * \note The caller is responsible for the \p N modulus' memory.
198 * mbedtls_mpi_mod_modulus_free(&N) should be invoked at the
199 * end of its lifecycle.
200 *
201 * \param[in,out] N The address of the modulus structure to populate.
202 * Must be initialized.
203 * \param[in] id The mbedtls_ecp_group_id for which to initialise the modulus.
204 * \param[in] ctype The mbedtls_ecp_modulus_type identifier for a coordinate modulus (P)
205 * or a scalar modulus (N).
206 *
207 * \return \c 0 if successful.
208 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the given MPIs do not
209 * have the correct number of limbs.
210 *
211 */
212MBEDTLS_STATIC_TESTABLE
213int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N,
214 const mbedtls_ecp_group_id id,
215 const mbedtls_ecp_modulus_type ctype);
216#endif /* MBEDTLS_ECP_WITH_MPI_UINT */
217
218#endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_LIGHT */
219
220#endif /* TF_PSA_CRYPTO_ECP_INVASIVE_H */