| Gilles Peskine | 80ba850 | 2021-04-03 20:36:37 +0200 | [diff] [blame] | 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 | 
|  | 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 Peskine | 72fcc98 | 2021-03-23 22:31:31 +0100 | [diff] [blame] | 30 | #include "mbedtls/bignum.h" | 
| Minos Galanakis | dd55692 | 2023-02-03 19:12:21 +0000 | [diff] [blame] | 31 | #include "bignum_mod.h" | 
| Gilles Peskine | 80ba850 | 2021-04-03 20:36:37 +0200 | [diff] [blame] | 32 | #include "mbedtls/ecp.h" | 
|  | 33 |  | 
|  | 34 | #if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ECP_C) | 
|  | 35 |  | 
| Gabor Mezei | 66f88a9 | 2023-02-08 17:11:13 +0100 | [diff] [blame] | 36 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) ||   \ | 
| Gilles Peskine | 618be2e | 2021-04-03 21:47:53 +0200 | [diff] [blame] | 37 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) | 
|  | 38 | /* Preconditions: | 
|  | 39 | *   - bits is a multiple of 64 or is 224 | 
|  | 40 | *   - c is -1 or -2 | 
|  | 41 | *   - 0 <= N < 2^bits | 
| Gilles Peskine | 392d101 | 2021-04-09 15:46:51 +0200 | [diff] [blame] | 42 | *   - N has room for bits plus one limb | 
| Gilles Peskine | 618be2e | 2021-04-03 21:47:53 +0200 | [diff] [blame] | 43 | * | 
| Gilles Peskine | 392d101 | 2021-04-09 15:46:51 +0200 | [diff] [blame] | 44 | * Behavior: | 
|  | 45 | * Set N to c * 2^bits + old_value_of_N. | 
| Gilles Peskine | 618be2e | 2021-04-03 21:47:53 +0200 | [diff] [blame] | 46 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 47 | void mbedtls_ecp_fix_negative(mbedtls_mpi *N, signed char c, size_t bits); | 
| Gilles Peskine | 618be2e | 2021-04-03 21:47:53 +0200 | [diff] [blame] | 48 | #endif | 
|  | 49 |  | 
| Gilles Peskine | 72fcc98 | 2021-03-23 22:31:31 +0100 | [diff] [blame] | 50 | #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) | 
|  | 51 | /** Generate a private key on a Montgomery curve (Curve25519 or Curve448). | 
|  | 52 | * | 
|  | 53 | * This function implements key generation for the set of secret keys | 
|  | 54 | * specified in [Curve25519] p. 5 and in [Curve448]. The resulting value | 
|  | 55 | * has the lower bits masked but is not necessarily canonical. | 
|  | 56 | * | 
|  | 57 | * \note            - [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf | 
|  | 58 | *                  - [RFC7748] https://tools.ietf.org/html/rfc7748 | 
|  | 59 | * | 
| Gilles Peskine | 55c4604 | 2021-03-24 12:34:40 +0100 | [diff] [blame] | 60 | * \p high_bit      The position of the high-order bit of the key to generate. | 
| Gilles Peskine | 72fcc98 | 2021-03-23 22:31:31 +0100 | [diff] [blame] | 61 | *                  This is the bit-size of the key minus 1: | 
|  | 62 | *                  254 for Curve25519 or 447 for Curve448. | 
|  | 63 | * \param d         The randomly generated key. This is a number of size | 
|  | 64 | *                  exactly \p n_bits + 1 bits, with the least significant bits | 
|  | 65 | *                  masked as specified in [Curve25519] and in [RFC7748] ยง5. | 
|  | 66 | * \param f_rng     The RNG function. | 
|  | 67 | * \param p_rng     The RNG context to be passed to \p f_rng. | 
|  | 68 | * | 
|  | 69 | * \return          \c 0 on success. | 
|  | 70 | * \return          \c MBEDTLS_ERR_ECP_xxx or MBEDTLS_ERR_MPI_xxx on failure. | 
|  | 71 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | int mbedtls_ecp_gen_privkey_mx(size_t n_bits, | 
|  | 73 | mbedtls_mpi *d, | 
|  | 74 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 75 | void *p_rng); | 
| Gilles Peskine | 72fcc98 | 2021-03-23 22:31:31 +0100 | [diff] [blame] | 76 |  | 
|  | 77 | #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */ | 
|  | 78 |  | 
| Gabor Mezei | deece2b | 2023-01-25 17:57:36 +0100 | [diff] [blame] | 79 | #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) | 
|  | 80 |  | 
| Gabor Mezei | 9b290b3 | 2023-01-27 11:00:51 +0100 | [diff] [blame] | 81 | /** Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1) | 
|  | 82 | * | 
| Gabor Mezei | a264831 | 2023-02-13 16:29:05 +0100 | [diff] [blame] | 83 | * This operation expects a 384 bit MPI and the result of the reduction | 
|  | 84 | * is a 192 bit MPI. | 
|  | 85 | * | 
| Gabor Mezei | 9b290b3 | 2023-01-27 11:00:51 +0100 | [diff] [blame] | 86 | * \param[in,out]   Np  The address of the MPI to be converted. | 
| Gabor Mezei | 0b4b8e3 | 2023-02-14 16:36:38 +0100 | [diff] [blame] | 87 | *                      Must have twice as many limbs as the modulus. | 
|  | 88 | *                      Upon return this holds the reduced value. The bitlength | 
|  | 89 | *                      of the reduced value is the same as that of the modulus | 
|  | 90 | *                      (192 bits). | 
| Gabor Mezei | 63aae68 | 2023-02-06 16:24:08 +0100 | [diff] [blame] | 91 | * \param[in]       Nn  The length of \p Np in limbs. | 
| Gabor Mezei | 9b290b3 | 2023-01-27 11:00:51 +0100 | [diff] [blame] | 92 | */ | 
| Gabor Mezei | deece2b | 2023-01-25 17:57:36 +0100 | [diff] [blame] | 93 | MBEDTLS_STATIC_TESTABLE | 
| Gabor Mezei | 2038ce9 | 2023-01-31 14:33:12 +0100 | [diff] [blame] | 94 | int mbedtls_ecp_mod_p192_raw(mbedtls_mpi_uint *Np, size_t Nn); | 
| Gabor Mezei | deece2b | 2023-01-25 17:57:36 +0100 | [diff] [blame] | 95 |  | 
|  | 96 | #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */ | 
|  | 97 |  | 
| Gabor Mezei | e14b5bd | 2023-02-08 17:23:03 +0100 | [diff] [blame] | 98 | #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) | 
|  | 99 |  | 
| Gabor Mezei | a835d20 | 2023-02-23 17:38:00 +0100 | [diff] [blame] | 100 | /** Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2) | 
|  | 101 | * | 
| Gabor Mezei | 08a9495 | 2023-02-28 18:40:57 +0100 | [diff] [blame] | 102 | * \param[in,out]   X       The address of the MPI to be converted. | 
|  | 103 | *                          Must have exact limb size that stores a 448-bit MPI | 
|  | 104 | *                          (double the bitlength of the modulus). | 
|  | 105 | *                          Upon return holds the reduced value which is | 
|  | 106 | *                          in range `0 <= X < 2 * N` (where N is the modulus). | 
|  | 107 | *                          The bitlength of the reduced value is the same as | 
|  | 108 | *                          that of the modulus (224 bits). | 
|  | 109 | * \param[in]       X_limbs The length of \p X in limbs. | 
| Gabor Mezei | a835d20 | 2023-02-23 17:38:00 +0100 | [diff] [blame] | 110 | * | 
|  | 111 | * \return          \c 0 on success. | 
| Gabor Mezei | 08a9495 | 2023-02-28 18:40:57 +0100 | [diff] [blame] | 112 | * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs is not the | 
|  | 113 | *                  limb size that sores a 448-bit MPI. | 
| Gabor Mezei | a835d20 | 2023-02-23 17:38:00 +0100 | [diff] [blame] | 114 | */ | 
| Gabor Mezei | e14b5bd | 2023-02-08 17:23:03 +0100 | [diff] [blame] | 115 | MBEDTLS_STATIC_TESTABLE | 
| Gabor Mezei | 08a9495 | 2023-02-28 18:40:57 +0100 | [diff] [blame] | 116 | int mbedtls_ecp_mod_p224_raw(mbedtls_mpi_uint *X, size_t X_limbs); | 
| Gabor Mezei | e14b5bd | 2023-02-08 17:23:03 +0100 | [diff] [blame] | 117 |  | 
|  | 118 | #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */ | 
|  | 119 |  | 
| Gabor Mezei | 2cb630e | 2023-02-01 14:02:16 +0100 | [diff] [blame] | 120 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) | 
|  | 121 |  | 
| Gabor Mezei | b1c62ca | 2023-02-06 16:02:05 +0100 | [diff] [blame] | 122 | /** Fast quasi-reduction modulo p521 = 2^521 - 1 (FIPS 186-3 D.2.5) | 
|  | 123 | * | 
| Gabor Mezei | 7e6fcc1 | 2023-02-15 17:51:59 +0100 | [diff] [blame] | 124 | * \param[in,out]   X       The address of the MPI to be converted. | 
|  | 125 | *                          Must have twice as many limbs as the modulus | 
|  | 126 | *                          (the modulus is 521 bits long). Upon return this | 
|  | 127 | *                          holds the reduced value. The reduced value is | 
|  | 128 | *                          in range `0 <= X < 2 * N` (where N is the modulus). | 
|  | 129 | *                          and its the bitlength is one plus the bitlength | 
|  | 130 | *                          of the modulus. | 
|  | 131 | * \param[in]       X_limbs The length of \p X in limbs. | 
|  | 132 | * | 
|  | 133 | * \return          \c 0 on success. | 
|  | 134 | * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p X_limbs does not have | 
|  | 135 | *                  twice as many limbs as the modulus. | 
| Gabor Mezei | b1c62ca | 2023-02-06 16:02:05 +0100 | [diff] [blame] | 136 | */ | 
| Gabor Mezei | 2cb630e | 2023-02-01 14:02:16 +0100 | [diff] [blame] | 137 | MBEDTLS_STATIC_TESTABLE | 
| Gabor Mezei | 7e6fcc1 | 2023-02-15 17:51:59 +0100 | [diff] [blame] | 138 | int mbedtls_ecp_mod_p521_raw(mbedtls_mpi_uint *X, size_t X_limbs); | 
| Gabor Mezei | 2cb630e | 2023-02-01 14:02:16 +0100 | [diff] [blame] | 139 |  | 
|  | 140 | #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */ | 
|  | 141 |  | 
| Minos Galanakis | a30afe2 | 2023-02-15 15:36:29 +0000 | [diff] [blame] | 142 | /** Initialise a modulus with hard-coded const curve data. | 
|  | 143 | * | 
|  | 144 | * \note            The caller is responsible for the \p N modulus' memory. | 
|  | 145 | *                  mbedtls_mpi_mod_modulus_free(&N) should be invoked at the | 
|  | 146 | *                  end of its lifecycle. | 
|  | 147 | * | 
|  | 148 | * \param[in,out] N The address of the modulus structure to populate. | 
|  | 149 | *                  Must be initialized. | 
|  | 150 | * \param[in] id    The mbedtls_ecp_group_id for which to initialise the modulus. | 
|  | 151 | * \param[in] ctype The mbedtls_ecp_curve_type identifier for a coordinate modulus (P) | 
|  | 152 | *                  or a scalar modulus (N). | 
|  | 153 | * | 
|  | 154 | * \return          \c 0 if successful. | 
|  | 155 | * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the given MPIs do not | 
|  | 156 | *                  have the correct number of limbs. | 
|  | 157 | * | 
|  | 158 | */ | 
| Minos Galanakis | dd55692 | 2023-02-03 19:12:21 +0000 | [diff] [blame] | 159 | MBEDTLS_STATIC_TESTABLE | 
|  | 160 | int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N, | 
|  | 161 | const mbedtls_ecp_group_id id, | 
|  | 162 | const mbedtls_ecp_curve_type ctype); | 
|  | 163 |  | 
| Gilles Peskine | 80ba850 | 2021-04-03 20:36:37 +0200 | [diff] [blame] | 164 | #endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_C */ | 
|  | 165 |  | 
|  | 166 | #endif /* MBEDTLS_ECP_INVASIVE_H */ |