blob: 1ca65fd78ee2ca4e7571cffdf9ccbc088cd8e210 [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 Peskinede332132021-03-23 22:31:31 +010030#include "mbedtls/bignum.h"
Gilles Peskine80ba8502021-04-03 20:36:37 +020031#include "mbedtls/ecp.h"
32
33#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ECP_C)
34
Gilles Peskine618be2e2021-04-03 21:47:53 +020035#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
36 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
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 Peskine392d1012021-04-09 15:46:51 +020042 * - N has room for bits plus one limb
Gilles Peskine618be2e2021-04-03 21:47:53 +020043 *
Gilles Peskine392d1012021-04-09 15:46:51 +020044 * Behavior:
45 * Set N to c * 2^bits + old_value_of_N.
Gilles Peskine618be2e2021-04-03 21:47:53 +020046 */
47void mbedtls_ecp_fix_negative( mbedtls_mpi *N, signed char c, size_t bits );
48#endif
49
Gilles Peskinede332132021-03-23 22:31:31 +010050#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 Peskine3838f282021-03-24 12:34:40 +010060 * \p high_bit The position of the high-order bit of the key to generate.
Gilles Peskinede332132021-03-23 22:31:31 +010061 * 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 */
72int 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 );
76
77#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
78
79#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
Gilles Peskine7967ec52021-03-29 21:53:58 +020080/** Generate a random number uniformly in a range.
81 *
82 * This function generates a random number between \p min inclusive and
83 * \p N exclusive.
Gilles Peskinede332132021-03-23 22:31:31 +010084 *
85 * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
Gilles Peskine7967ec52021-03-29 21:53:58 +020086 * when the RNG is a suitably parametrized instance of HMAC_DRBG
87 * and \p min is \c 1.
Gilles Peskinede332132021-03-23 22:31:31 +010088 *
Gilles Peskine7967ec52021-03-29 21:53:58 +020089 * \note There are `N - min` possible outputs. The lower bound
90 * \p min can be reached, but the upper bound \p N cannot.
Gilles Peskinede332132021-03-23 22:31:31 +010091 *
Gilles Peskine7967ec52021-03-29 21:53:58 +020092 * \param X The destination MPI. This must point to an initialized MPI.
93 * \param min The minimum value to return.
94 * It must be nonnegative.
95 * \param N The upper bound of the range, exclusive.
96 * In other words, this is one plus the maximum value to return.
97 * \p N must be strictly larger than \p min.
98 * \param f_rng The RNG function to use. This must not be \c NULL.
99 * \param p_rng The RNG parameter to be passed to \p f_rng.
100 *
101 * \return \c 0 if successful.
102 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
103 * \return Another negative error code on failure.
Gilles Peskinede332132021-03-23 22:31:31 +0100104 */
Gilles Peskine7967ec52021-03-29 21:53:58 +0200105int mbedtls_mpi_random( mbedtls_mpi *X,
106 mbedtls_mpi_sint min,
107 const mbedtls_mpi *N,
108 int (*f_rng)(void *, unsigned char *, size_t),
109 void *p_rng );
Gilles Peskinede332132021-03-23 22:31:31 +0100110#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
111
Gilles Peskine80ba8502021-04-03 20:36:37 +0200112#endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_C */
113
114#endif /* MBEDTLS_ECP_INVASIVE_H */