1 /* 2 * Copyright The Mbed TLS Contributors 3 * SPDX-License-Identifier: Apache-2.0 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may 6 * not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * Copyright (c) 2023 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") 17 */ 18 #ifndef MBEDTLS_ECP_INVASIVE_H 19 #define MBEDTLS_ECP_INVASIVE_H 20 21 #include "../common.h" 22 #include "mbedtls/bignum.h" 23 #include "mbedtls/ecp.h" 24 25 #if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ECP_C) 26 27 #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \ 28 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) 29 /* Preconditions: 30 * - bits is a multiple of 64 or is 224 31 * - c is -1 or -2 32 * - 0 <= N < 2^bits 33 * - N has room for bits plus one limb 34 * 35 * Behavior: 36 * Set N to c * 2^bits + old_value_of_N. 37 */ 38 void mbedtls_ecp_fix_negative(mbedtls_mpi *N, signed char c, size_t bits); 39 #endif 40 41 #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) 42 /** Generate a private key on a Montgomery curve (Curve25519 or Curve448). 43 * 44 * This function implements key generation for the set of secret keys 45 * specified in [Curve25519] p. 5 and in [Curve448]. The resulting value 46 * has the lower bits masked but is not necessarily canonical. 47 * 48 * \note - [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf 49 * - [RFC7748] https://tools.ietf.org/html/rfc7748 50 * 51 * \p high_bit The position of the high-order bit of the key to generate. 52 * This is the bit-size of the key minus 1: 53 * 254 for Curve25519 or 447 for Curve448. 54 * \param d The randomly generated key. This is a number of size 55 * exactly \p n_bits + 1 bits, with the least significant bits 56 * masked as specified in [Curve25519] and in [RFC7748] §5. 57 * \param f_rng The RNG function. 58 * \param p_rng The RNG context to be passed to \p f_rng. 59 * 60 * \return \c 0 on success. 61 * \return \c MBEDTLS_ERR_ECP_xxx or MBEDTLS_ERR_MPI_xxx on failure. 62 */ 63 int mbedtls_ecp_gen_privkey_mx( 64 size_t n_bits, mbedtls_mpi *d, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); 65 66 #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */ 67 68 #endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_C */ 69 70 #endif /* MBEDTLS_ECP_INVASIVE_H */ 71