Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 1 | /** |
| 2 | * \file ecp.h |
| 3 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 4 | * \brief This file provides an API for Elliptic Curves over GF(P) (ECP). |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 5 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 6 | * The use of ECP in cryptography and TLS is defined in |
| 7 | * <em>Standards for Efficient Cryptography Group (SECG): SEC1 |
| 8 | * Elliptic Curve Cryptography</em> and |
| 9 | * <em>RFC-4492: Elliptic Curve Cryptography (ECC) Cipher Suites |
| 10 | * for Transport Layer Security (TLS)</em>. |
| 11 | * |
| 12 | * <em>RFC-2409: The Internet Key Exchange (IKE)</em> defines ECP |
| 13 | * group types. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 19 | * SPDX-License-Identifier: Apache-2.0 |
| 20 | * |
| 21 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 22 | * not use this file except in compliance with the License. |
| 23 | * You may obtain a copy of the License at |
| 24 | * |
| 25 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 26 | * |
| 27 | * Unless required by applicable law or agreed to in writing, software |
| 28 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 29 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 30 | * See the License for the specific language governing permissions and |
| 31 | * limitations under the License. |
| 32 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 33 | * This file is part of Mbed TLS (https://tls.mbed.org) |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 34 | */ |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 35 | |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 36 | #ifndef MBEDTLS_ECP_H |
| 37 | #define MBEDTLS_ECP_H |
| 38 | |
| 39 | #include "bignum.h" |
| 40 | |
| 41 | /* |
| 42 | * ECP error codes |
| 43 | */ |
| 44 | #define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */ |
| 45 | #define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */ |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 46 | #define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< The requested feature is not available, for example, the requested curve is not supported. */ |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 47 | #define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */ |
| 48 | #define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80 /**< Memory allocation failed. */ |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 49 | #define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as ephemeral key, failed. */ |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 50 | #define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */ |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 51 | #define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 /**< The buffer contains a valid signature followed by more data. */ |
| 52 | |
| 53 | /* MBEDTLS_ERR_ECP_HW_ACCEL_FAILED is deprecated and should not be used. */ |
| 54 | #define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED -0x4B80 /**< The ECP hardware accelerator failed. */ |
| 55 | |
| 56 | #define MBEDTLS_ERR_ECP_IN_PROGRESS -0x4B00 /**< Operation in progress, call again with the same parameters to continue. */ |
| 57 | |
| 58 | #ifdef __cplusplus |
| 59 | extern "C" { |
| 60 | #endif |
| 61 | |
| 62 | /** |
| 63 | * Domain-parameter identifiers: curve, subgroup, and generator. |
| 64 | * |
| 65 | * \note Only curves over prime fields are supported. |
| 66 | * |
| 67 | * \warning This library does not support validation of arbitrary domain |
| 68 | * parameters. Therefore, only standardized domain parameters from trusted |
| 69 | * sources should be used. See mbedtls_ecp_group_load(). |
| 70 | */ |
| 71 | typedef enum |
| 72 | { |
| 73 | MBEDTLS_ECP_DP_NONE = 0, /*!< Curve not defined. */ |
| 74 | MBEDTLS_ECP_DP_SECP192R1, /*!< Domain parameters for the 192-bit curve defined by FIPS 186-4 and SEC1. */ |
| 75 | MBEDTLS_ECP_DP_SECP224R1, /*!< Domain parameters for the 224-bit curve defined by FIPS 186-4 and SEC1. */ |
| 76 | MBEDTLS_ECP_DP_SECP256R1, /*!< Domain parameters for the 256-bit curve defined by FIPS 186-4 and SEC1. */ |
| 77 | MBEDTLS_ECP_DP_SECP384R1, /*!< Domain parameters for the 384-bit curve defined by FIPS 186-4 and SEC1. */ |
| 78 | MBEDTLS_ECP_DP_SECP521R1, /*!< Domain parameters for the 521-bit curve defined by FIPS 186-4 and SEC1. */ |
| 79 | MBEDTLS_ECP_DP_BP256R1, /*!< Domain parameters for 256-bit Brainpool curve. */ |
| 80 | MBEDTLS_ECP_DP_BP384R1, /*!< Domain parameters for 384-bit Brainpool curve. */ |
| 81 | MBEDTLS_ECP_DP_BP512R1, /*!< Domain parameters for 512-bit Brainpool curve. */ |
| 82 | MBEDTLS_ECP_DP_CURVE25519, /*!< Domain parameters for Curve25519. */ |
| 83 | MBEDTLS_ECP_DP_SECP192K1, /*!< Domain parameters for 192-bit "Koblitz" curve. */ |
| 84 | MBEDTLS_ECP_DP_SECP224K1, /*!< Domain parameters for 224-bit "Koblitz" curve. */ |
| 85 | MBEDTLS_ECP_DP_SECP256K1, /*!< Domain parameters for 256-bit "Koblitz" curve. */ |
| 86 | MBEDTLS_ECP_DP_CURVE448, /*!< Domain parameters for Curve448. */ |
| 87 | } mbedtls_ecp_group_id; |
| 88 | |
| 89 | /** |
| 90 | * The number of supported curves, plus one for #MBEDTLS_ECP_DP_NONE. |
| 91 | * |
| 92 | * \note Montgomery curves are currently excluded. |
| 93 | */ |
| 94 | #define MBEDTLS_ECP_DP_MAX 12 |
| 95 | |
| 96 | /** |
| 97 | * Curve information, for use by other modules. |
| 98 | */ |
| 99 | typedef struct mbedtls_ecp_curve_info |
| 100 | { |
| 101 | mbedtls_ecp_group_id grp_id; /*!< An internal identifier. */ |
| 102 | uint16_t tls_id; /*!< The TLS NamedCurve identifier. */ |
| 103 | uint16_t bit_size; /*!< The curve size in bits. */ |
| 104 | const char *name; /*!< A human-friendly name. */ |
| 105 | } mbedtls_ecp_curve_info; |
| 106 | |
| 107 | /** |
| 108 | * \brief The ECP point structure, in Jacobian coordinates. |
| 109 | * |
| 110 | * \note All functions expect and return points satisfying |
| 111 | * the following condition: <code>Z == 0</code> or |
| 112 | * <code>Z == 1</code>. Other values of \p Z are |
| 113 | * used only by internal functions. |
| 114 | * The point is zero, or "at infinity", if <code>Z == 0</code>. |
| 115 | * Otherwise, \p X and \p Y are its standard (affine) |
| 116 | * coordinates. |
| 117 | */ |
| 118 | typedef struct mbedtls_ecp_point |
| 119 | { |
| 120 | mbedtls_mpi X; /*!< The X coordinate of the ECP point. */ |
| 121 | mbedtls_mpi Y; /*!< The Y coordinate of the ECP point. */ |
| 122 | mbedtls_mpi Z; /*!< The Z coordinate of the ECP point. */ |
| 123 | } |
| 124 | mbedtls_ecp_point; |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 125 | |
| 126 | #if !defined(MBEDTLS_ECP_ALT) |
| 127 | /* |
| 128 | * default mbed TLS elliptic curve arithmetic implementation |
| 129 | * |
| 130 | * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an |
| 131 | * alternative implementation for the whole module and it will replace this |
| 132 | * one.) |
| 133 | */ |
| 134 | |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 135 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 136 | * \brief The ECP group structure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 137 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 138 | * We consider two types of curve equations: |
| 139 | * <ul><li>Short Weierstrass: <code>y^2 = x^3 + A x + B mod P</code> |
| 140 | * (SEC1 + RFC-4492)</li> |
| 141 | * <li>Montgomery: <code>y^2 = x^3 + A x^2 + x mod P</code> (Curve25519, |
| 142 | * Curve448)</li></ul> |
| 143 | * In both cases, the generator (\p G) for a prime-order subgroup is fixed. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 144 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 145 | * For Short Weierstrass, this subgroup is the whole curve, and its |
| 146 | * cardinality is denoted by \p N. Our code requires that \p N is an |
| 147 | * odd prime as mbedtls_ecp_mul() requires an odd number, and |
| 148 | * mbedtls_ecdsa_sign() requires that it is prime for blinding purposes. |
| 149 | * |
| 150 | * For Montgomery curves, we do not store \p A, but <code>(A + 2) / 4</code>, |
| 151 | * which is the quantity used in the formulas. Additionally, \p nbits is |
| 152 | * not the size of \p N but the required size for private keys. |
| 153 | * |
| 154 | * If \p modp is NULL, reduction modulo \p P is done using a generic algorithm. |
| 155 | * Otherwise, \p modp must point to a function that takes an \p mbedtls_mpi in the |
| 156 | * range of <code>0..2^(2*pbits)-1</code>, and transforms it in-place to an integer |
| 157 | * which is congruent mod \p P to the given MPI, and is close enough to \p pbits |
| 158 | * in size, so that it may be efficiently brought in the 0..P-1 range by a few |
| 159 | * additions or subtractions. Therefore, it is only an approximative modular |
| 160 | * reduction. It must return 0 on success and non-zero on failure. |
| 161 | * |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 162 | */ |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 163 | typedef struct mbedtls_ecp_group |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 164 | { |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 165 | mbedtls_ecp_group_id id; /*!< An internal group identifier. */ |
| 166 | mbedtls_mpi P; /*!< The prime modulus of the base field. */ |
| 167 | mbedtls_mpi A; /*!< For Short Weierstrass: \p A in the equation. For |
| 168 | Montgomery curves: <code>(A + 2) / 4</code>. */ |
| 169 | mbedtls_mpi B; /*!< For Short Weierstrass: \p B in the equation. |
| 170 | For Montgomery curves: unused. */ |
| 171 | mbedtls_ecp_point G; /*!< The generator of the subgroup used. */ |
| 172 | mbedtls_mpi N; /*!< The order of \p G. */ |
| 173 | size_t pbits; /*!< The number of bits in \p P.*/ |
| 174 | size_t nbits; /*!< For Short Weierstrass: The number of bits in \p P. |
| 175 | For Montgomery curves: the number of bits in the |
| 176 | private keys. */ |
| 177 | unsigned int h; /*!< \internal 1 if the constants are static. */ |
| 178 | int (*modp)(mbedtls_mpi *); /*!< The function for fast pseudo-reduction |
| 179 | mod \p P (see above).*/ |
| 180 | int (*t_pre)(mbedtls_ecp_point *, void *); /*!< Unused. */ |
| 181 | int (*t_post)(mbedtls_ecp_point *, void *); /*!< Unused. */ |
| 182 | void *t_data; /*!< Unused. */ |
| 183 | mbedtls_ecp_point *T; /*!< Pre-computed points for ecp_mul_comb(). */ |
| 184 | size_t T_size; /*!< The number of pre-computed points. */ |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 185 | } |
| 186 | mbedtls_ecp_group; |
| 187 | |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 188 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 189 | |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 190 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 191 | * \brief Internal restart context for multiplication |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 192 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 193 | * \note Opaque struct |
| 194 | */ |
| 195 | typedef struct mbedtls_ecp_restart_mul mbedtls_ecp_restart_mul_ctx; |
| 196 | |
| 197 | /** |
| 198 | * \brief Internal restart context for ecp_muladd() |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 199 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 200 | * \note Opaque struct |
| 201 | */ |
| 202 | typedef struct mbedtls_ecp_restart_muladd mbedtls_ecp_restart_muladd_ctx; |
| 203 | |
| 204 | /** |
| 205 | * \brief General context for resuming ECC operations |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 206 | */ |
| 207 | typedef struct |
| 208 | { |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 209 | unsigned ops_done; /*!< current ops count */ |
| 210 | unsigned depth; /*!< call depth (0 = top-level) */ |
| 211 | mbedtls_ecp_restart_mul_ctx *rsm; /*!< ecp_mul_comb() sub-context */ |
| 212 | mbedtls_ecp_restart_muladd_ctx *ma; /*!< ecp_muladd() sub-context */ |
| 213 | } mbedtls_ecp_restart_ctx; |
| 214 | |
| 215 | /* |
| 216 | * Operation counts for restartable functions |
| 217 | */ |
| 218 | #define MBEDTLS_ECP_OPS_CHK 3 /*!< basic ops count for ecp_check_pubkey() */ |
| 219 | #define MBEDTLS_ECP_OPS_DBL 8 /*!< basic ops count for ecp_double_jac() */ |
| 220 | #define MBEDTLS_ECP_OPS_ADD 11 /*!< basic ops count for see ecp_add_mixed() */ |
| 221 | #define MBEDTLS_ECP_OPS_INV 120 /*!< empirical equivalent for mpi_mod_inv() */ |
| 222 | |
| 223 | /** |
| 224 | * \brief Internal; for restartable functions in other modules. |
| 225 | * Check and update basic ops budget. |
| 226 | * |
| 227 | * \param grp Group structure |
| 228 | * \param rs_ctx Restart context |
| 229 | * \param ops Number of basic ops to do |
| 230 | * |
| 231 | * \return \c 0 if doing \p ops basic ops is still allowed, |
| 232 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS otherwise. |
| 233 | */ |
| 234 | int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp, |
| 235 | mbedtls_ecp_restart_ctx *rs_ctx, |
| 236 | unsigned ops ); |
| 237 | |
| 238 | /* Utility macro for checking and updating ops budget */ |
| 239 | #define MBEDTLS_ECP_BUDGET( ops ) \ |
| 240 | MBEDTLS_MPI_CHK( mbedtls_ecp_check_budget( grp, rs_ctx, \ |
| 241 | (unsigned) (ops) ) ); |
| 242 | |
| 243 | #else /* MBEDTLS_ECP_RESTARTABLE */ |
| 244 | |
| 245 | #define MBEDTLS_ECP_BUDGET( ops ) /* no-op; for compatibility */ |
| 246 | |
| 247 | /* We want to declare restartable versions of existing functions anyway */ |
| 248 | typedef void mbedtls_ecp_restart_ctx; |
| 249 | |
| 250 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 251 | |
| 252 | /** |
| 253 | * \name SECTION: Module settings |
| 254 | * |
| 255 | * The configuration options you can set for this module are in this section. |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 256 | * Either change them in config.h, or define them using the compiler command line. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 257 | * \{ |
| 258 | */ |
| 259 | |
| 260 | #if !defined(MBEDTLS_ECP_MAX_BITS) |
| 261 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 262 | * The maximum size of the groups, that is, of \c N and \c P. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 263 | */ |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 264 | #define MBEDTLS_ECP_MAX_BITS 521 /**< The maximum size of groups, in bits. */ |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 265 | #endif |
| 266 | |
| 267 | #define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 ) |
| 268 | #define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 ) |
| 269 | |
| 270 | #if !defined(MBEDTLS_ECP_WINDOW_SIZE) |
| 271 | /* |
| 272 | * Maximum "window" size used for point multiplication. |
| 273 | * Default: 6. |
| 274 | * Minimum value: 2. Maximum value: 7. |
| 275 | * |
| 276 | * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) ) |
| 277 | * points used for point multiplication. This value is directly tied to EC |
| 278 | * peak memory usage, so decreasing it by one should roughly cut memory usage |
| 279 | * by two (if large curves are in use). |
| 280 | * |
| 281 | * Reduction in size may reduce speed, but larger curves are impacted first. |
| 282 | * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1): |
| 283 | * w-size: 6 5 4 3 2 |
| 284 | * 521 145 141 135 120 97 |
| 285 | * 384 214 209 198 177 146 |
| 286 | * 256 320 320 303 262 226 |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 287 | * 224 475 475 453 398 342 |
| 288 | * 192 640 640 633 587 476 |
| 289 | */ |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 290 | #define MBEDTLS_ECP_WINDOW_SIZE 6 /**< The maximum window size used. */ |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 291 | #endif /* MBEDTLS_ECP_WINDOW_SIZE */ |
| 292 | |
| 293 | #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM) |
| 294 | /* |
| 295 | * Trade memory for speed on fixed-point multiplication. |
| 296 | * |
| 297 | * This speeds up repeated multiplication of the generator (that is, the |
| 298 | * multiplication in ECDSA signatures, and half of the multiplications in |
| 299 | * ECDSA verification and ECDHE) by a factor roughly 3 to 4. |
| 300 | * |
| 301 | * The cost is increasing EC peak memory usage by a factor roughly 2. |
| 302 | * |
| 303 | * Change this value to 0 to reduce peak memory usage. |
| 304 | */ |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 305 | #define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up. */ |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 306 | #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */ |
| 307 | |
| 308 | /* \} name SECTION: Module settings */ |
| 309 | |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 310 | #else /* MBEDTLS_ECP_ALT */ |
| 311 | #include "ecp_alt.h" |
| 312 | #endif /* MBEDTLS_ECP_ALT */ |
| 313 | |
| 314 | /** |
| 315 | * \brief The ECP key-pair structure. |
| 316 | * |
| 317 | * A generic key-pair that may be used for ECDSA and fixed ECDH, for example. |
| 318 | * |
| 319 | * \note Members are deliberately in the same order as in the |
| 320 | * ::mbedtls_ecdsa_context structure. |
| 321 | */ |
| 322 | typedef struct mbedtls_ecp_keypair |
| 323 | { |
| 324 | mbedtls_ecp_group grp; /*!< Elliptic curve and base point */ |
| 325 | mbedtls_mpi d; /*!< our secret value */ |
| 326 | mbedtls_ecp_point Q; /*!< our public value */ |
| 327 | } |
| 328 | mbedtls_ecp_keypair; |
| 329 | |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 330 | /* |
| 331 | * Point formats, from RFC 4492's enum ECPointFormat |
| 332 | */ |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 333 | #define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format. */ |
| 334 | #define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format. */ |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 335 | |
| 336 | /* |
| 337 | * Some other constants from RFC 4492 |
| 338 | */ |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 339 | #define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< The named_curve of ECCurveType. */ |
| 340 | |
| 341 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 342 | /** |
| 343 | * \brief Set the maximum number of basic operations done in a row. |
| 344 | * |
| 345 | * If more operations are needed to complete a computation, |
| 346 | * #MBEDTLS_ERR_ECP_IN_PROGRESS will be returned by the |
| 347 | * function performing the computation. It is then the |
| 348 | * caller's responsibility to either call again with the same |
| 349 | * parameters until it returns 0 or an error code; or to free |
| 350 | * the restart context if the operation is to be aborted. |
| 351 | * |
| 352 | * It is strictly required that all input parameters and the |
| 353 | * restart context be the same on successive calls for the |
| 354 | * same operation, but output parameters need not be the |
| 355 | * same; they must not be used until the function finally |
| 356 | * returns 0. |
| 357 | * |
| 358 | * This only applies to functions whose documentation |
| 359 | * mentions they may return #MBEDTLS_ERR_ECP_IN_PROGRESS (or |
| 360 | * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS for functions in the |
| 361 | * SSL module). For functions that accept a "restart context" |
| 362 | * argument, passing NULL disables restart and makes the |
| 363 | * function equivalent to the function with the same name |
| 364 | * with \c _restartable removed. For functions in the ECDH |
| 365 | * module, restart is disabled unless the function accepts |
| 366 | * an "ECDH context" argument and |
| 367 | * mbedtls_ecdh_enable_restart() was previously called on |
| 368 | * that context. For function in the SSL module, restart is |
| 369 | * only enabled for specific sides and key exchanges |
| 370 | * (currently only for clients and ECDHE-ECDSA). |
| 371 | * |
| 372 | * \param max_ops Maximum number of basic operations done in a row. |
| 373 | * Default: 0 (unlimited). |
| 374 | * Lower (non-zero) values mean ECC functions will block for |
| 375 | * a lesser maximum amount of time. |
| 376 | * |
| 377 | * \note A "basic operation" is defined as a rough equivalent of a |
| 378 | * multiplication in GF(p) for the NIST P-256 curve. |
| 379 | * As an indication, with default settings, a scalar |
| 380 | * multiplication (full run of \c mbedtls_ecp_mul()) is: |
| 381 | * - about 3300 basic operations for P-256 |
| 382 | * - about 9400 basic operations for P-384 |
| 383 | * |
| 384 | * \note Very low values are not always respected: sometimes |
| 385 | * functions need to block for a minimum number of |
| 386 | * operations, and will do so even if max_ops is set to a |
| 387 | * lower value. That minimum depends on the curve size, and |
| 388 | * can be made lower by decreasing the value of |
| 389 | * \c MBEDTLS_ECP_WINDOW_SIZE. As an indication, here is the |
| 390 | * lowest effective value for various curves and values of |
| 391 | * that parameter (w for short): |
| 392 | * w=6 w=5 w=4 w=3 w=2 |
| 393 | * P-256 208 208 160 136 124 |
| 394 | * P-384 682 416 320 272 248 |
| 395 | * P-521 1364 832 640 544 496 |
| 396 | * |
| 397 | * \note This setting is currently ignored by Curve25519. |
| 398 | */ |
| 399 | void mbedtls_ecp_set_max_ops( unsigned max_ops ); |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 400 | |
| 401 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 402 | * \brief Check if restart is enabled (max_ops != 0) |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 403 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 404 | * \return \c 0 if \c max_ops == 0 (restart disabled) |
| 405 | * \return \c 1 otherwise (restart enabled) |
| 406 | */ |
| 407 | int mbedtls_ecp_restart_is_enabled( void ); |
| 408 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 409 | |
| 410 | /** |
| 411 | * \brief This function retrieves the information defined in |
| 412 | * mbedtls_ecp_curve_info() for all supported curves in order |
| 413 | * of preference. |
| 414 | * |
| 415 | * \return A statically allocated array. The last entry is 0. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 416 | */ |
| 417 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void ); |
| 418 | |
| 419 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 420 | * \brief This function retrieves the list of internal group |
| 421 | * identifiers of all supported curves in the order of |
| 422 | * preference. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 423 | * |
| 424 | * \return A statically allocated array, |
| 425 | * terminated with MBEDTLS_ECP_DP_NONE. |
| 426 | */ |
| 427 | const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void ); |
| 428 | |
| 429 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 430 | * \brief This function retrieves curve information from an internal |
| 431 | * group identifier. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 432 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 433 | * \param grp_id An \c MBEDTLS_ECP_DP_XXX value. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 434 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 435 | * \return The associated curve information on success. |
| 436 | * \return NULL on failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 437 | */ |
| 438 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id ); |
| 439 | |
| 440 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 441 | * \brief This function retrieves curve information from a TLS |
| 442 | * NamedCurve value. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 443 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 444 | * \param tls_id An \c MBEDTLS_ECP_DP_XXX value. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 445 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 446 | * \return The associated curve information on success. |
| 447 | * \return NULL on failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 448 | */ |
| 449 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id ); |
| 450 | |
| 451 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 452 | * \brief This function retrieves curve information from a |
| 453 | * human-readable name. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 454 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 455 | * \param name The human-readable name. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 456 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 457 | * \return The associated curve information on success. |
| 458 | * \return NULL on failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 459 | */ |
| 460 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name ); |
| 461 | |
| 462 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 463 | * \brief This function initializes a point as zero. |
| 464 | * |
| 465 | * \param pt The point to initialize. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 466 | */ |
| 467 | void mbedtls_ecp_point_init( mbedtls_ecp_point *pt ); |
| 468 | |
| 469 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 470 | * \brief This function initializes an ECP group context |
| 471 | * without loading any domain parameters. |
| 472 | * |
| 473 | * \note After this function is called, domain parameters |
| 474 | * for various ECP groups can be loaded through the |
| 475 | * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group() |
| 476 | * functions. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 477 | */ |
| 478 | void mbedtls_ecp_group_init( mbedtls_ecp_group *grp ); |
| 479 | |
| 480 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 481 | * \brief This function initializes a key pair as an invalid one. |
| 482 | * |
| 483 | * \param key The key pair to initialize. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 484 | */ |
| 485 | void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key ); |
| 486 | |
| 487 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 488 | * \brief This function frees the components of a point. |
| 489 | * |
| 490 | * \param pt The point to free. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 491 | */ |
| 492 | void mbedtls_ecp_point_free( mbedtls_ecp_point *pt ); |
| 493 | |
| 494 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 495 | * \brief This function frees the components of an ECP group. |
| 496 | * \param grp The group to free. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 497 | */ |
| 498 | void mbedtls_ecp_group_free( mbedtls_ecp_group *grp ); |
| 499 | |
| 500 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 501 | * \brief This function frees the components of a key pair. |
| 502 | * \param key The key pair to free. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 503 | */ |
| 504 | void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key ); |
| 505 | |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 506 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 507 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 508 | * \brief Initialize a restart context |
| 509 | */ |
| 510 | void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx ); |
| 511 | |
| 512 | /** |
| 513 | * \brief Free the components of a restart context |
| 514 | */ |
| 515 | void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx ); |
| 516 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 517 | |
| 518 | /** |
| 519 | * \brief This function copies the contents of point \p Q into |
| 520 | * point \p P. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 521 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 522 | * \param P The destination point. |
| 523 | * \param Q The source point. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 524 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 525 | * \return \c 0 on success. |
| 526 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 527 | */ |
| 528 | int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q ); |
| 529 | |
| 530 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 531 | * \brief This function copies the contents of group \p src into |
| 532 | * group \p dst. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 533 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 534 | * \param dst The destination group. |
| 535 | * \param src The source group. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 536 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 537 | * \return \c 0 on success. |
| 538 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 539 | */ |
| 540 | int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src ); |
| 541 | |
| 542 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 543 | * \brief This function sets a point to zero. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 544 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 545 | * \param pt The point to set. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 546 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 547 | * \return \c 0 on success. |
| 548 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 549 | */ |
| 550 | int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt ); |
| 551 | |
| 552 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 553 | * \brief This function checks if a point is zero. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 554 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 555 | * \param pt The point to test. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 556 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 557 | * \return \c 1 if the point is zero. |
| 558 | * \return \c 0 if the point is non-zero. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 559 | */ |
| 560 | int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt ); |
| 561 | |
| 562 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 563 | * \brief This function compares two points. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 564 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 565 | * \note This assumes that the points are normalized. Otherwise, |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 566 | * they may compare as "not equal" even if they are. |
| 567 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 568 | * \param P The first point to compare. |
| 569 | * \param Q The second point to compare. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 570 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 571 | * \return \c 0 if the points are equal. |
| 572 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the points are not equal. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 573 | */ |
| 574 | int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P, |
| 575 | const mbedtls_ecp_point *Q ); |
| 576 | |
| 577 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 578 | * \brief This function imports a non-zero point from two ASCII |
| 579 | * strings. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 580 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 581 | * \param P The destination point. |
| 582 | * \param radix The numeric base of the input. |
| 583 | * \param x The first affine coordinate, as a null-terminated string. |
| 584 | * \param y The second affine coordinate, as a null-terminated string. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 585 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 586 | * \return \c 0 on success. |
| 587 | * \return An \c MBEDTLS_ERR_MPI_XXX error code on failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 588 | */ |
| 589 | int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix, |
| 590 | const char *x, const char *y ); |
| 591 | |
| 592 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 593 | * \brief This function exports a point into unsigned binary data. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 594 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 595 | * \param grp The group to which the point should belong. |
| 596 | * \param P The point to export. |
| 597 | * \param format The point format. Should be an \c MBEDTLS_ECP_PF_XXX macro. |
| 598 | * \param olen The length of the output. |
| 599 | * \param buf The output buffer. |
| 600 | * \param buflen The length of the output buffer. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 601 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 602 | * \return \c 0 on success. |
| 603 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA |
| 604 | * or #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 605 | */ |
| 606 | int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P, |
| 607 | int format, size_t *olen, |
| 608 | unsigned char *buf, size_t buflen ); |
| 609 | |
| 610 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 611 | * \brief This function imports a point from unsigned binary data. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 612 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 613 | * \note This function does not check that the point actually |
| 614 | * belongs to the given group, see mbedtls_ecp_check_pubkey() |
| 615 | * for that. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 616 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 617 | * \param grp The group to which the point should belong. |
| 618 | * \param P The point to import. |
| 619 | * \param buf The input buffer. |
| 620 | * \param ilen The length of the input. |
| 621 | * |
| 622 | * \return \c 0 on success. |
| 623 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid. |
| 624 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
| 625 | * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 626 | * is not implemented. |
| 627 | * |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 628 | */ |
| 629 | int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P, |
| 630 | const unsigned char *buf, size_t ilen ); |
| 631 | |
| 632 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 633 | * \brief This function imports a point from a TLS ECPoint record. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 634 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 635 | * \note On function return, \p buf is updated to point to immediately |
| 636 | * after the ECPoint record. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 637 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 638 | * \param grp The ECP group used. |
| 639 | * \param pt The destination point. |
| 640 | * \param buf The address of the pointer to the start of the input buffer. |
| 641 | * \param len The length of the buffer. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 642 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 643 | * \return \c 0 on success. |
| 644 | * \return An \c MBEDTLS_ERR_MPI_XXX error code on initialization failure. |
| 645 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 646 | */ |
| 647 | int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt, |
| 648 | const unsigned char **buf, size_t len ); |
| 649 | |
| 650 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 651 | * \brief This function exports a point as a TLS ECPoint record. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 652 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 653 | * \param grp The ECP group used. |
| 654 | * \param pt The point format to export to. The point format is an |
| 655 | * \c MBEDTLS_ECP_PF_XXX constant. |
| 656 | * \param format The export format. |
| 657 | * \param olen The length of the data written. |
| 658 | * \param buf The buffer to write to. |
| 659 | * \param blen The length of the buffer. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 660 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 661 | * \return \c 0 on success. |
| 662 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA or |
| 663 | * #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 664 | */ |
| 665 | int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt, |
| 666 | int format, size_t *olen, |
| 667 | unsigned char *buf, size_t blen ); |
| 668 | |
| 669 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 670 | * \brief This function sets a group using standardized domain parameters. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 671 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 672 | * \note The index should be a value of the NamedCurve enum, |
| 673 | * as defined in <em>RFC-4492: Elliptic Curve Cryptography |
| 674 | * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>, |
| 675 | * usually in the form of an \c MBEDTLS_ECP_DP_XXX macro. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 676 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 677 | * \param grp The destination group. |
| 678 | * \param id The identifier of the domain parameter set to load. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 679 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 680 | * \return \c 0 on success, |
| 681 | * \return An \c MBEDTLS_ERR_MPI_XXX error code on initialization failure. |
| 682 | * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups. |
| 683 | |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 684 | */ |
| 685 | int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id ); |
| 686 | |
| 687 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 688 | * \brief This function sets a group from a TLS ECParameters record. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 689 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 690 | * \note \p buf is updated to point right after the ECParameters record |
| 691 | * on exit. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 692 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 693 | * \param grp The destination group. |
| 694 | * \param buf The address of the pointer to the start of the input buffer. |
| 695 | * \param len The length of the buffer. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 696 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 697 | * \return \c 0 on success. |
| 698 | * \return An \c MBEDTLS_ERR_MPI_XXX error code on initialization failure. |
| 699 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 700 | */ |
| 701 | int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len ); |
| 702 | |
| 703 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 704 | * \brief This function writes the TLS ECParameters record for a group. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 705 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 706 | * \param grp The ECP group used. |
| 707 | * \param olen The number of Bytes written. |
| 708 | * \param buf The buffer to write to. |
| 709 | * \param blen The length of the buffer. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 710 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 711 | * \return \c 0 on success. |
| 712 | * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 713 | */ |
| 714 | int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen, |
| 715 | unsigned char *buf, size_t blen ); |
| 716 | |
| 717 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 718 | * \brief This function performs multiplication of a point by |
| 719 | * an integer: \p R = \p m * \p P. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 720 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 721 | * It is not thread-safe to use same group in multiple threads. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 722 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 723 | * \note To prevent timing attacks, this function |
| 724 | * executes the exact same sequence of base-field |
| 725 | * operations for any valid \p m. It avoids any if-branch or |
| 726 | * array index depending on the value of \p m. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 727 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 728 | * \note If \p f_rng is not NULL, it is used to randomize |
| 729 | * intermediate results to prevent potential timing attacks |
| 730 | * targeting these results. We recommend always providing |
| 731 | * a non-NULL \p f_rng. The overhead is negligible. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 732 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 733 | * \param grp The ECP group. |
| 734 | * \param R The destination point. |
| 735 | * \param m The integer by which to multiply. |
| 736 | * \param P The point to multiply. |
| 737 | * \param f_rng The RNG function. |
| 738 | * \param p_rng The RNG context. |
| 739 | * |
| 740 | * \return \c 0 on success. |
| 741 | * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private |
| 742 | * key, or \p P is not a valid public key. |
| 743 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 744 | */ |
| 745 | int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 746 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
| 747 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 748 | |
| 749 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 750 | * \brief This function performs multiplication of a point by |
| 751 | * an integer: \p R = \p m * \p P in a restartable way. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 752 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 753 | * \see mbedtls_ecp_mul() |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 754 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 755 | * \note This function does the same as \c mbedtls_ecp_mul(), but |
| 756 | * it can return early and restart according to the limit set |
| 757 | * with \c mbedtls_ecp_set_max_ops() to reduce blocking. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 758 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 759 | * \param grp The ECP group. |
| 760 | * \param R The destination point. |
| 761 | * \param m The integer by which to multiply. |
| 762 | * \param P The point to multiply. |
| 763 | * \param f_rng The RNG function. |
| 764 | * \param p_rng The RNG context. |
| 765 | * \param rs_ctx The restart context (NULL disables restart). |
| 766 | * |
| 767 | * \return \c 0 on success. |
| 768 | * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private |
| 769 | * key, or \p P is not a valid public key. |
| 770 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
| 771 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
| 772 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
| 773 | */ |
| 774 | int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 775 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
| 776 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 777 | mbedtls_ecp_restart_ctx *rs_ctx ); |
| 778 | |
| 779 | /** |
| 780 | * \brief This function performs multiplication and addition of two |
| 781 | * points by integers: \p R = \p m * \p P + \p n * \p Q |
| 782 | * |
| 783 | * It is not thread-safe to use same group in multiple threads. |
| 784 | * |
| 785 | * \note In contrast to mbedtls_ecp_mul(), this function does not |
| 786 | * guarantee a constant execution flow and timing. |
| 787 | * |
| 788 | * \param grp The ECP group. |
| 789 | * \param R The destination point. |
| 790 | * \param m The integer by which to multiply \p P. |
| 791 | * \param P The point to multiply by \p m. |
| 792 | * \param n The integer by which to multiply \p Q. |
| 793 | * \param Q The point to be multiplied by \p n. |
| 794 | * |
| 795 | * \return \c 0 on success. |
| 796 | * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not |
| 797 | * valid private keys, or \p P or \p Q are not valid public |
| 798 | * keys. |
| 799 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 800 | */ |
| 801 | int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 802 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
| 803 | const mbedtls_mpi *n, const mbedtls_ecp_point *Q ); |
| 804 | |
| 805 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 806 | * \brief This function performs multiplication and addition of two |
| 807 | * points by integers: \p R = \p m * \p P + \p n * \p Q in a |
| 808 | * restartable way. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 809 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 810 | * \see \c mbedtls_ecp_muladd() |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 811 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 812 | * \note This function works the same as \c mbedtls_ecp_muladd(), |
| 813 | * but it can return early and restart according to the limit |
| 814 | * set with \c mbedtls_ecp_set_max_ops() to reduce blocking. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 815 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 816 | * \param grp The ECP group. |
| 817 | * \param R The destination point. |
| 818 | * \param m The integer by which to multiply \p P. |
| 819 | * \param P The point to multiply by \p m. |
| 820 | * \param n The integer by which to multiply \p Q. |
| 821 | * \param Q The point to be multiplied by \p n. |
| 822 | * \param rs_ctx The restart context (NULL disables restart). |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 823 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 824 | * \return \c 0 on success. |
| 825 | * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not |
| 826 | * valid private keys, or \p P or \p Q are not valid public |
| 827 | * keys. |
| 828 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
| 829 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
| 830 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
| 831 | */ |
| 832 | int mbedtls_ecp_muladd_restartable( |
| 833 | mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 834 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
| 835 | const mbedtls_mpi *n, const mbedtls_ecp_point *Q, |
| 836 | mbedtls_ecp_restart_ctx *rs_ctx ); |
| 837 | |
| 838 | /** |
| 839 | * \brief This function checks that a point is a valid public key |
| 840 | * on this curve. |
| 841 | * |
| 842 | * It only checks that the point is non-zero, has |
| 843 | * valid coordinates and lies on the curve. It does not verify |
| 844 | * that it is indeed a multiple of \p G. This additional |
| 845 | * check is computationally more expensive, is not required |
| 846 | * by standards, and should not be necessary if the group |
| 847 | * used has a small cofactor. In particular, it is useless for |
| 848 | * the NIST groups which all have a cofactor of 1. |
| 849 | * |
| 850 | * \note This function uses bare components rather than an |
| 851 | * ::mbedtls_ecp_keypair structure, to ease use with other |
| 852 | * structures, such as ::mbedtls_ecdh_context or |
| 853 | * ::mbedtls_ecdsa_context. |
| 854 | * |
| 855 | * \param grp The curve the point should lie on. |
| 856 | * \param pt The point to check. |
| 857 | * |
| 858 | * \return \c 0 if the point is a valid public key. |
| 859 | * \return #MBEDTLS_ERR_ECP_INVALID_KEY on failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 860 | */ |
| 861 | int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt ); |
| 862 | |
| 863 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 864 | * \brief This function checks that an \p mbedtls_mpi is a valid private |
| 865 | * key for this curve. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 866 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 867 | * \note This function uses bare components rather than an |
| 868 | * ::mbedtls_ecp_keypair structure to ease use with other |
| 869 | * structures, such as ::mbedtls_ecdh_context or |
| 870 | * ::mbedtls_ecdsa_context. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 871 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 872 | * \param grp The group used. |
| 873 | * \param d The integer to check. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 874 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 875 | * \return \c 0 if the point is a valid private key. |
| 876 | * \return #MBEDTLS_ERR_ECP_INVALID_KEY on failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 877 | */ |
| 878 | int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d ); |
| 879 | |
| 880 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 881 | * \brief This function generates a private key. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 882 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 883 | * \param grp The ECP group. |
| 884 | * \param d The destination MPI (secret part). |
| 885 | * \param f_rng The RNG function. |
| 886 | * \param p_rng The RNG parameter. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 887 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 888 | * \return \c 0 on success. |
| 889 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code |
| 890 | * on failure. |
| 891 | */ |
| 892 | int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp, |
| 893 | mbedtls_mpi *d, |
| 894 | int (*f_rng)(void *, unsigned char *, size_t), |
| 895 | void *p_rng ); |
| 896 | |
| 897 | /** |
| 898 | * \brief This function generates a keypair with a configurable base |
| 899 | * point. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 900 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 901 | * \note This function uses bare components rather than an |
| 902 | * ::mbedtls_ecp_keypair structure to ease use with other |
| 903 | * structures, such as ::mbedtls_ecdh_context or |
| 904 | * ::mbedtls_ecdsa_context. |
| 905 | * |
| 906 | * \param grp The ECP group. |
| 907 | * \param G The chosen base point. |
| 908 | * \param d The destination MPI (secret part). |
| 909 | * \param Q The destination point (public part). |
| 910 | * \param f_rng The RNG function. |
| 911 | * \param p_rng The RNG context. |
| 912 | * |
| 913 | * \return \c 0 on success. |
| 914 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code |
| 915 | * on failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 916 | */ |
| 917 | int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp, |
| 918 | const mbedtls_ecp_point *G, |
| 919 | mbedtls_mpi *d, mbedtls_ecp_point *Q, |
| 920 | int (*f_rng)(void *, unsigned char *, size_t), |
| 921 | void *p_rng ); |
| 922 | |
| 923 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 924 | * \brief This function generates an ECP keypair. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 925 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 926 | * \note This function uses bare components rather than an |
| 927 | * ::mbedtls_ecp_keypair structure to ease use with other |
| 928 | * structures, such as ::mbedtls_ecdh_context or |
| 929 | * ::mbedtls_ecdsa_context. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 930 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 931 | * \param grp The ECP group. |
| 932 | * \param d The destination MPI (secret part). |
| 933 | * \param Q The destination point (public part). |
| 934 | * \param f_rng The RNG function. |
| 935 | * \param p_rng The RNG context. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 936 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 937 | * \return \c 0 on success. |
| 938 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code |
| 939 | * on failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 940 | */ |
| 941 | int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, |
| 942 | int (*f_rng)(void *, unsigned char *, size_t), |
| 943 | void *p_rng ); |
| 944 | |
| 945 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 946 | * \brief This function generates an ECP key. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 947 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 948 | * \param grp_id The ECP group identifier. |
| 949 | * \param key The destination key. |
| 950 | * \param f_rng The RNG function. |
| 951 | * \param p_rng The RNG context. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 952 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 953 | * \return \c 0 on success. |
| 954 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code |
| 955 | * on failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 956 | */ |
| 957 | int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, |
| 958 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 959 | |
| 960 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 961 | * \brief This function checks that the keypair objects |
| 962 | * \p pub and \p prv have the same group and the |
| 963 | * same public point, and that the private key in |
| 964 | * \p prv is consistent with the public key. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 965 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 966 | * \param pub The keypair structure holding the public key. |
| 967 | * If it contains a private key, that part is ignored. |
| 968 | * \param prv The keypair structure holding the full keypair. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 969 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 970 | * \return \c 0 on success, meaning that the keys are valid and match. |
| 971 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the keys are invalid or do not match. |
| 972 | * \return An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX |
| 973 | * error code on calculation failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 974 | */ |
| 975 | int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv ); |
| 976 | |
| 977 | #if defined(MBEDTLS_SELF_TEST) |
| 978 | |
| 979 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 980 | * \brief The ECP checkup routine. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 981 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 982 | * \return \c 0 on success. |
| 983 | * \return \c 1 on failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 984 | */ |
| 985 | int mbedtls_ecp_self_test( int verbose ); |
| 986 | |
| 987 | #endif /* MBEDTLS_SELF_TEST */ |
| 988 | |
| 989 | #ifdef __cplusplus |
| 990 | } |
| 991 | #endif |
| 992 | |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 993 | #endif /* ecp.h */ |