blob: 7adbe7e9b23487697f96e9f44f2723054e959f83 [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
34#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ECP_C)
35
Gabor Mezeiab6ac912023-03-01 16:01:52 +010036#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Gilles Peskine618be2e2021-04-03 21:47:53 +020037/* Preconditions:
38 * - bits is a multiple of 64 or is 224
39 * - c is -1 or -2
40 * - 0 <= N < 2^bits
Gilles Peskine392d1012021-04-09 15:46:51 +020041 * - N has room for bits plus one limb
Gilles Peskine618be2e2021-04-03 21:47:53 +020042 *
Gilles Peskine392d1012021-04-09 15:46:51 +020043 * Behavior:
44 * Set N to c * 2^bits + old_value_of_N.
Gilles Peskine618be2e2021-04-03 21:47:53 +020045 */
Gilles Peskine449bd832023-01-11 14:50:10 +010046void mbedtls_ecp_fix_negative(mbedtls_mpi *N, signed char c, size_t bits);
Gilles Peskine618be2e2021-04-03 21:47:53 +020047#endif
48
Gilles Peskine72fcc982021-03-23 22:31:31 +010049#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
50/** Generate a private key on a Montgomery curve (Curve25519 or Curve448).
51 *
52 * This function implements key generation for the set of secret keys
53 * specified in [Curve25519] p. 5 and in [Curve448]. The resulting value
54 * has the lower bits masked but is not necessarily canonical.
55 *
56 * \note - [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
57 * - [RFC7748] https://tools.ietf.org/html/rfc7748
58 *
Gilles Peskine55c46042021-03-24 12:34:40 +010059 * \p high_bit The position of the high-order bit of the key to generate.
Gilles Peskine72fcc982021-03-23 22:31:31 +010060 * This is the bit-size of the key minus 1:
61 * 254 for Curve25519 or 447 for Curve448.
62 * \param d The randomly generated key. This is a number of size
63 * exactly \p n_bits + 1 bits, with the least significant bits
64 * masked as specified in [Curve25519] and in [RFC7748] ยง5.
65 * \param f_rng The RNG function.
66 * \param p_rng The RNG context to be passed to \p f_rng.
67 *
68 * \return \c 0 on success.
69 * \return \c MBEDTLS_ERR_ECP_xxx or MBEDTLS_ERR_MPI_xxx on failure.
70 */
Gilles Peskine449bd832023-01-11 14:50:10 +010071int mbedtls_ecp_gen_privkey_mx(size_t n_bits,
72 mbedtls_mpi *d,
73 int (*f_rng)(void *, unsigned char *, size_t),
74 void *p_rng);
Gilles Peskine72fcc982021-03-23 22:31:31 +010075
76#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
77
Gabor Mezeideece2b2023-01-25 17:57:36 +010078#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
79
Gabor Mezei9b290b32023-01-27 11:00:51 +010080/** Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
81 *
Gabor Mezeia2648312023-02-13 16:29:05 +010082 * This operation expects a 384 bit MPI and the result of the reduction
83 * is a 192 bit MPI.
84 *
Gabor Mezei9b290b32023-01-27 11:00:51 +010085 * \param[in,out] Np The address of the MPI to be converted.
Gabor Mezei0b4b8e32023-02-14 16:36:38 +010086 * Must have twice as many limbs as the modulus.
87 * Upon return this holds the reduced value. The bitlength
88 * of the reduced value is the same as that of the modulus
89 * (192 bits).
Gabor Mezei63aae682023-02-06 16:24:08 +010090 * \param[in] Nn The length of \p Np in limbs.
Gabor Mezei9b290b32023-01-27 11:00:51 +010091 */
Gabor Mezeideece2b2023-01-25 17:57:36 +010092MBEDTLS_STATIC_TESTABLE
Gabor Mezei2038ce92023-01-31 14:33:12 +010093int mbedtls_ecp_mod_p192_raw(mbedtls_mpi_uint *Np, size_t Nn);
Gabor Mezeideece2b2023-01-25 17:57:36 +010094
95#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
96
Gabor Mezeie14b5bd2023-02-08 17:23:03 +010097#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
98
Gabor Mezeia835d202023-02-23 17:38:00 +010099/** Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
100 *
Gabor Mezei08a94952023-02-28 18:40:57 +0100101 * \param[in,out] X The address of the MPI to be converted.
102 * Must have exact limb size that stores a 448-bit MPI
103 * (double the bitlength of the modulus).
104 * Upon return holds the reduced value which is
105 * in range `0 <= X < 2 * N` (where N is the modulus).
106 * The bitlength of the reduced value is the same as
107 * that of the modulus (224 bits).
108 * \param[in] X_limbs The length of \p X in limbs.
Gabor Mezeia835d202023-02-23 17:38:00 +0100109 *
110 * \return \c 0 on success.
Gabor Mezei08a94952023-02-28 18:40:57 +0100111 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the
112 * limb size that sores a 448-bit MPI.
Gabor Mezeia835d202023-02-23 17:38:00 +0100113 */
Gabor Mezeie14b5bd2023-02-08 17:23:03 +0100114MBEDTLS_STATIC_TESTABLE
Gabor Mezei08a94952023-02-28 18:40:57 +0100115int mbedtls_ecp_mod_p224_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezeie14b5bd2023-02-08 17:23:03 +0100116
117#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
118
Gabor Mezei5221c042023-03-01 16:05:21 +0100119#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
120
121MBEDTLS_STATIC_TESTABLE
Gabor Mezeied1acf62023-03-01 16:09:13 +0100122int mbedtls_ecp_mod_p256_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezei5221c042023-03-01 16:05:21 +0100123
124#endif
125
Gabor Mezei2cb630e2023-02-01 14:02:16 +0100126#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
127
Gabor Mezeib1c62ca2023-02-06 16:02:05 +0100128/** Fast quasi-reduction modulo p521 = 2^521 - 1 (FIPS 186-3 D.2.5)
129 *
Gabor Mezei7e6fcc12023-02-15 17:51:59 +0100130 * \param[in,out] X The address of the MPI to be converted.
131 * Must have twice as many limbs as the modulus
132 * (the modulus is 521 bits long). Upon return this
133 * holds the reduced value. The reduced value is
134 * in range `0 <= X < 2 * N` (where N is the modulus).
135 * and its the bitlength is one plus the bitlength
136 * of the modulus.
137 * \param[in] X_limbs The length of \p X in limbs.
138 *
139 * \return \c 0 on success.
140 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs does not have
141 * twice as many limbs as the modulus.
Gabor Mezeib1c62ca2023-02-06 16:02:05 +0100142 */
Gabor Mezei2cb630e2023-02-01 14:02:16 +0100143MBEDTLS_STATIC_TESTABLE
Gabor Mezei7e6fcc12023-02-15 17:51:59 +0100144int mbedtls_ecp_mod_p521_raw(mbedtls_mpi_uint *X, size_t X_limbs);
Gabor Mezei2cb630e2023-02-01 14:02:16 +0100145
146#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
147
Minos Galanakisa30afe22023-02-15 15:36:29 +0000148/** Initialise a modulus with hard-coded const curve data.
149 *
150 * \note The caller is responsible for the \p N modulus' memory.
151 * mbedtls_mpi_mod_modulus_free(&N) should be invoked at the
152 * end of its lifecycle.
153 *
154 * \param[in,out] N The address of the modulus structure to populate.
155 * Must be initialized.
156 * \param[in] id The mbedtls_ecp_group_id for which to initialise the modulus.
157 * \param[in] ctype The mbedtls_ecp_curve_type identifier for a coordinate modulus (P)
158 * or a scalar modulus (N).
159 *
160 * \return \c 0 if successful.
161 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the given MPIs do not
162 * have the correct number of limbs.
163 *
164 */
Minos Galanakisdd556922023-02-03 19:12:21 +0000165MBEDTLS_STATIC_TESTABLE
166int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N,
167 const mbedtls_ecp_group_id id,
168 const mbedtls_ecp_curve_type ctype);
169
Gilles Peskine80ba8502021-04-03 20:36:37 +0200170#endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_C */
171
172#endif /* MBEDTLS_ECP_INVASIVE_H */