Jaeden Amero | e54e693 | 2018-08-06 16:19:58 +0100 | [diff] [blame^] | 1 | /** |
| 2 | * \file ecp.h |
| 3 | * |
| 4 | * \brief This file provides an API for Elliptic Curves over GF(P) (ECP). |
| 5 | * |
| 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 |
| 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 | * |
| 33 | * This file is part of Mbed Crypto (https://tls.mbed.org) |
| 34 | */ |
| 35 | |
| 36 | #ifndef MBEDCRYPTO_ECP_H |
| 37 | #define MBEDCRYPTO_ECP_H |
| 38 | |
| 39 | #include "bignum.h" |
| 40 | |
| 41 | /* |
| 42 | * ECP error codes |
| 43 | */ |
| 44 | #define MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */ |
| 45 | #define MBEDCRYPTO_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */ |
| 46 | #define MBEDCRYPTO_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< The requested feature is not available, for example, the requested curve is not supported. */ |
| 47 | #define MBEDCRYPTO_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */ |
| 48 | #define MBEDCRYPTO_ERR_ECP_ALLOC_FAILED -0x4D80 /**< Memory allocation failed. */ |
| 49 | #define MBEDCRYPTO_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as ephemeral key, failed. */ |
| 50 | #define MBEDCRYPTO_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */ |
| 51 | #define MBEDCRYPTO_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 /**< The buffer contains a valid signature followed by more data. */ |
| 52 | #define MBEDCRYPTO_ERR_ECP_HW_ACCEL_FAILED -0x4B80 /**< The ECP hardware accelerator failed. */ |
| 53 | |
| 54 | #ifdef __cplusplus |
| 55 | extern "C" { |
| 56 | #endif |
| 57 | |
| 58 | /** |
| 59 | * Domain-parameter identifiers: curve, subgroup, and generator. |
| 60 | * |
| 61 | * \note Only curves over prime fields are supported. |
| 62 | * |
| 63 | * \warning This library does not support validation of arbitrary domain |
| 64 | * parameters. Therefore, only standardized domain parameters from trusted |
| 65 | * sources should be used. See mbedcrypto_ecp_group_load(). |
| 66 | */ |
| 67 | typedef enum |
| 68 | { |
| 69 | MBEDCRYPTO_ECP_DP_NONE = 0, /*!< Curve not defined. */ |
| 70 | MBEDCRYPTO_ECP_DP_SECP192R1, /*!< Domain parameters for the 192-bit curve defined by FIPS 186-4 and SEC1. */ |
| 71 | MBEDCRYPTO_ECP_DP_SECP224R1, /*!< Domain parameters for the 224-bit curve defined by FIPS 186-4 and SEC1. */ |
| 72 | MBEDCRYPTO_ECP_DP_SECP256R1, /*!< Domain parameters for the 256-bit curve defined by FIPS 186-4 and SEC1. */ |
| 73 | MBEDCRYPTO_ECP_DP_SECP384R1, /*!< Domain parameters for the 384-bit curve defined by FIPS 186-4 and SEC1. */ |
| 74 | MBEDCRYPTO_ECP_DP_SECP521R1, /*!< Domain parameters for the 521-bit curve defined by FIPS 186-4 and SEC1. */ |
| 75 | MBEDCRYPTO_ECP_DP_BP256R1, /*!< Domain parameters for 256-bit Brainpool curve. */ |
| 76 | MBEDCRYPTO_ECP_DP_BP384R1, /*!< Domain parameters for 384-bit Brainpool curve. */ |
| 77 | MBEDCRYPTO_ECP_DP_BP512R1, /*!< Domain parameters for 512-bit Brainpool curve. */ |
| 78 | MBEDCRYPTO_ECP_DP_CURVE25519, /*!< Domain parameters for Curve25519. */ |
| 79 | MBEDCRYPTO_ECP_DP_SECP192K1, /*!< Domain parameters for 192-bit "Koblitz" curve. */ |
| 80 | MBEDCRYPTO_ECP_DP_SECP224K1, /*!< Domain parameters for 224-bit "Koblitz" curve. */ |
| 81 | MBEDCRYPTO_ECP_DP_SECP256K1, /*!< Domain parameters for 256-bit "Koblitz" curve. */ |
| 82 | MBEDCRYPTO_ECP_DP_CURVE448, /*!< Domain parameters for Curve448. */ |
| 83 | } mbedcrypto_ecp_group_id; |
| 84 | |
| 85 | /** |
| 86 | * The number of supported curves, plus one for #MBEDCRYPTO_ECP_DP_NONE. |
| 87 | * |
| 88 | * \note Montgomery curves are currently excluded. |
| 89 | */ |
| 90 | #define MBEDCRYPTO_ECP_DP_MAX 12 |
| 91 | |
| 92 | /** |
| 93 | * Curve information, for use by other modules. |
| 94 | */ |
| 95 | typedef struct |
| 96 | { |
| 97 | mbedcrypto_ecp_group_id grp_id; /*!< An internal identifier. */ |
| 98 | uint16_t tls_id; /*!< The TLS NamedCurve identifier. */ |
| 99 | uint16_t bit_size; /*!< The curve size in bits. */ |
| 100 | const char *name; /*!< A human-friendly name. */ |
| 101 | } mbedcrypto_ecp_curve_info; |
| 102 | |
| 103 | /** |
| 104 | * \brief The ECP point structure, in Jacobian coordinates. |
| 105 | * |
| 106 | * \note All functions expect and return points satisfying |
| 107 | * the following condition: <code>Z == 0</code> or |
| 108 | * <code>Z == 1</code>. Other values of \p Z are |
| 109 | * used only by internal functions. |
| 110 | * The point is zero, or "at infinity", if <code>Z == 0</code>. |
| 111 | * Otherwise, \p X and \p Y are its standard (affine) |
| 112 | * coordinates. |
| 113 | */ |
| 114 | typedef struct |
| 115 | { |
| 116 | mbedcrypto_mpi X; /*!< The X coordinate of the ECP point. */ |
| 117 | mbedcrypto_mpi Y; /*!< The Y coordinate of the ECP point. */ |
| 118 | mbedcrypto_mpi Z; /*!< The Z coordinate of the ECP point. */ |
| 119 | } |
| 120 | mbedcrypto_ecp_point; |
| 121 | |
| 122 | #if !defined(MBEDCRYPTO_ECP_ALT) |
| 123 | /* |
| 124 | * default Mbed Crypto elliptic curve arithmetic implementation |
| 125 | * |
| 126 | * (in case MBEDCRYPTO_ECP_ALT is defined then the developer has to provide an |
| 127 | * alternative implementation for the whole module and it will replace this |
| 128 | * one.) |
| 129 | */ |
| 130 | |
| 131 | /** |
| 132 | * \brief The ECP group structure. |
| 133 | * |
| 134 | * We consider two types of curve equations: |
| 135 | * <ul><li>Short Weierstrass: <code>y^2 = x^3 + A x + B mod P</code> |
| 136 | * (SEC1 + RFC-4492)</li> |
| 137 | * <li>Montgomery: <code>y^2 = x^3 + A x^2 + x mod P</code> (Curve25519, |
| 138 | * Curve448)</li></ul> |
| 139 | * In both cases, the generator (\p G) for a prime-order subgroup is fixed. |
| 140 | * |
| 141 | * For Short Weierstrass, this subgroup is the whole curve, and its |
| 142 | * cardinality is denoted by \p N. Our code requires that \p N is an |
| 143 | * odd prime as mbedcrypto_ecp_mul() requires an odd number, and |
| 144 | * mbedcrypto_ecdsa_sign() requires that it is prime for blinding purposes. |
| 145 | * |
| 146 | * For Montgomery curves, we do not store \p A, but <code>(A + 2) / 4</code>, |
| 147 | * which is the quantity used in the formulas. Additionally, \p nbits is |
| 148 | * not the size of \p N but the required size for private keys. |
| 149 | * |
| 150 | * If \p modp is NULL, reduction modulo \p P is done using a generic algorithm. |
| 151 | * Otherwise, \p modp must point to a function that takes an \p mbedcrypto_mpi in the |
| 152 | * range of <code>0..2^(2*pbits)-1</code>, and transforms it in-place to an integer |
| 153 | * which is congruent mod \p P to the given MPI, and is close enough to \p pbits |
| 154 | * in size, so that it may be efficiently brought in the 0..P-1 range by a few |
| 155 | * additions or subtractions. Therefore, it is only an approximative modular |
| 156 | * reduction. It must return 0 on success and non-zero on failure. |
| 157 | * |
| 158 | */ |
| 159 | typedef struct |
| 160 | { |
| 161 | mbedcrypto_ecp_group_id id; /*!< An internal group identifier. */ |
| 162 | mbedcrypto_mpi P; /*!< The prime modulus of the base field. */ |
| 163 | mbedcrypto_mpi A; /*!< For Short Weierstrass: \p A in the equation. For |
| 164 | Montgomery curves: <code>(A + 2) / 4</code>. */ |
| 165 | mbedcrypto_mpi B; /*!< For Short Weierstrass: \p B in the equation. |
| 166 | For Montgomery curves: unused. */ |
| 167 | mbedcrypto_ecp_point G; /*!< The generator of the subgroup used. */ |
| 168 | mbedcrypto_mpi N; /*!< The order of \p G. */ |
| 169 | size_t pbits; /*!< The number of bits in \p P.*/ |
| 170 | size_t nbits; /*!< For Short Weierstrass: The number of bits in \p P. |
| 171 | For Montgomery curves: the number of bits in the |
| 172 | private keys. */ |
| 173 | unsigned int h; /*!< \internal 1 if the constants are static. */ |
| 174 | int (*modp)(mbedcrypto_mpi *); /*!< The function for fast pseudo-reduction |
| 175 | mod \p P (see above).*/ |
| 176 | int (*t_pre)(mbedcrypto_ecp_point *, void *); /*!< Unused. */ |
| 177 | int (*t_post)(mbedcrypto_ecp_point *, void *); /*!< Unused. */ |
| 178 | void *t_data; /*!< Unused. */ |
| 179 | mbedcrypto_ecp_point *T; /*!< Pre-computed points for ecp_mul_comb(). */ |
| 180 | size_t T_size; /*!< The number of pre-computed points. */ |
| 181 | } |
| 182 | mbedcrypto_ecp_group; |
| 183 | |
| 184 | /** |
| 185 | * \name SECTION: Module settings |
| 186 | * |
| 187 | * The configuration options you can set for this module are in this section. |
| 188 | * Either change them in config.h, or define them using the compiler command line. |
| 189 | * \{ |
| 190 | */ |
| 191 | |
| 192 | #if !defined(MBEDCRYPTO_ECP_MAX_BITS) |
| 193 | /** |
| 194 | * The maximum size of the groups, that is, of \c N and \c P. |
| 195 | */ |
| 196 | #define MBEDCRYPTO_ECP_MAX_BITS 521 /**< The maximum size of groups, in bits. */ |
| 197 | #endif |
| 198 | |
| 199 | #define MBEDCRYPTO_ECP_MAX_BYTES ( ( MBEDCRYPTO_ECP_MAX_BITS + 7 ) / 8 ) |
| 200 | #define MBEDCRYPTO_ECP_MAX_PT_LEN ( 2 * MBEDCRYPTO_ECP_MAX_BYTES + 1 ) |
| 201 | |
| 202 | #if !defined(MBEDCRYPTO_ECP_WINDOW_SIZE) |
| 203 | /* |
| 204 | * Maximum "window" size used for point multiplication. |
| 205 | * Default: 6. |
| 206 | * Minimum value: 2. Maximum value: 7. |
| 207 | * |
| 208 | * Result is an array of at most ( 1 << ( MBEDCRYPTO_ECP_WINDOW_SIZE - 1 ) ) |
| 209 | * points used for point multiplication. This value is directly tied to EC |
| 210 | * peak memory usage, so decreasing it by one should roughly cut memory usage |
| 211 | * by two (if large curves are in use). |
| 212 | * |
| 213 | * Reduction in size may reduce speed, but larger curves are impacted first. |
| 214 | * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1): |
| 215 | * w-size: 6 5 4 3 2 |
| 216 | * 521 145 141 135 120 97 |
| 217 | * 384 214 209 198 177 146 |
| 218 | * 256 320 320 303 262 226 |
| 219 | * 224 475 475 453 398 342 |
| 220 | * 192 640 640 633 587 476 |
| 221 | */ |
| 222 | #define MBEDCRYPTO_ECP_WINDOW_SIZE 6 /**< The maximum window size used. */ |
| 223 | #endif /* MBEDCRYPTO_ECP_WINDOW_SIZE */ |
| 224 | |
| 225 | #if !defined(MBEDCRYPTO_ECP_FIXED_POINT_OPTIM) |
| 226 | /* |
| 227 | * Trade memory for speed on fixed-point multiplication. |
| 228 | * |
| 229 | * This speeds up repeated multiplication of the generator (that is, the |
| 230 | * multiplication in ECDSA signatures, and half of the multiplications in |
| 231 | * ECDSA verification and ECDHE) by a factor roughly 3 to 4. |
| 232 | * |
| 233 | * The cost is increasing EC peak memory usage by a factor roughly 2. |
| 234 | * |
| 235 | * Change this value to 0 to reduce peak memory usage. |
| 236 | */ |
| 237 | #define MBEDCRYPTO_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up. */ |
| 238 | #endif /* MBEDCRYPTO_ECP_FIXED_POINT_OPTIM */ |
| 239 | |
| 240 | /* \} name SECTION: Module settings */ |
| 241 | |
| 242 | #else /* MBEDCRYPTO_ECP_ALT */ |
| 243 | #include "ecp_alt.h" |
| 244 | #endif /* MBEDCRYPTO_ECP_ALT */ |
| 245 | |
| 246 | /** |
| 247 | * \brief The ECP key-pair structure. |
| 248 | * |
| 249 | * A generic key-pair that may be used for ECDSA and fixed ECDH, for example. |
| 250 | * |
| 251 | * \note Members are deliberately in the same order as in the |
| 252 | * ::mbedcrypto_ecdsa_context structure. |
| 253 | */ |
| 254 | typedef struct |
| 255 | { |
| 256 | mbedcrypto_ecp_group grp; /*!< Elliptic curve and base point */ |
| 257 | mbedcrypto_mpi d; /*!< our secret value */ |
| 258 | mbedcrypto_ecp_point Q; /*!< our public value */ |
| 259 | } |
| 260 | mbedcrypto_ecp_keypair; |
| 261 | |
| 262 | /* |
| 263 | * Point formats, from RFC 4492's enum ECPointFormat |
| 264 | */ |
| 265 | #define MBEDCRYPTO_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format. */ |
| 266 | #define MBEDCRYPTO_ECP_PF_COMPRESSED 1 /**< Compressed point format. */ |
| 267 | |
| 268 | /* |
| 269 | * Some other constants from RFC 4492 |
| 270 | */ |
| 271 | #define MBEDCRYPTO_ECP_TLS_NAMED_CURVE 3 /**< The named_curve of ECCurveType. */ |
| 272 | |
| 273 | /** |
| 274 | * \brief This function retrieves the information defined in |
| 275 | * mbedcrypto_ecp_curve_info() for all supported curves in order |
| 276 | * of preference. |
| 277 | * |
| 278 | * \return A statically allocated array. The last entry is 0. |
| 279 | */ |
| 280 | const mbedcrypto_ecp_curve_info *mbedcrypto_ecp_curve_list( void ); |
| 281 | |
| 282 | /** |
| 283 | * \brief This function retrieves the list of internal group |
| 284 | * identifiers of all supported curves in the order of |
| 285 | * preference. |
| 286 | * |
| 287 | * \return A statically allocated array, |
| 288 | * terminated with MBEDCRYPTO_ECP_DP_NONE. |
| 289 | */ |
| 290 | const mbedcrypto_ecp_group_id *mbedcrypto_ecp_grp_id_list( void ); |
| 291 | |
| 292 | /** |
| 293 | * \brief This function retrieves curve information from an internal |
| 294 | * group identifier. |
| 295 | * |
| 296 | * \param grp_id An \c MBEDCRYPTO_ECP_DP_XXX value. |
| 297 | * |
| 298 | * \return The associated curve information on success. |
| 299 | * \return NULL on failure. |
| 300 | */ |
| 301 | const mbedcrypto_ecp_curve_info *mbedcrypto_ecp_curve_info_from_grp_id( mbedcrypto_ecp_group_id grp_id ); |
| 302 | |
| 303 | /** |
| 304 | * \brief This function retrieves curve information from a TLS |
| 305 | * NamedCurve value. |
| 306 | * |
| 307 | * \param tls_id An \c MBEDCRYPTO_ECP_DP_XXX value. |
| 308 | * |
| 309 | * \return The associated curve information on success. |
| 310 | * \return NULL on failure. |
| 311 | */ |
| 312 | const mbedcrypto_ecp_curve_info *mbedcrypto_ecp_curve_info_from_tls_id( uint16_t tls_id ); |
| 313 | |
| 314 | /** |
| 315 | * \brief This function retrieves curve information from a |
| 316 | * human-readable name. |
| 317 | * |
| 318 | * \param name The human-readable name. |
| 319 | * |
| 320 | * \return The associated curve information on success. |
| 321 | * \return NULL on failure. |
| 322 | */ |
| 323 | const mbedcrypto_ecp_curve_info *mbedcrypto_ecp_curve_info_from_name( const char *name ); |
| 324 | |
| 325 | /** |
| 326 | * \brief This function initializes a point as zero. |
| 327 | * |
| 328 | * \param pt The point to initialize. |
| 329 | */ |
| 330 | void mbedcrypto_ecp_point_init( mbedcrypto_ecp_point *pt ); |
| 331 | |
| 332 | /** |
| 333 | * \brief This function initializes an ECP group context |
| 334 | * without loading any domain parameters. |
| 335 | * |
| 336 | * \note After this function is called, domain parameters |
| 337 | * for various ECP groups can be loaded through the |
| 338 | * mbedcrypto_ecp_load() or mbedcrypto_ecp_tls_read_group() |
| 339 | * functions. |
| 340 | */ |
| 341 | void mbedcrypto_ecp_group_init( mbedcrypto_ecp_group *grp ); |
| 342 | |
| 343 | /** |
| 344 | * \brief This function initializes a key pair as an invalid one. |
| 345 | * |
| 346 | * \param key The key pair to initialize. |
| 347 | */ |
| 348 | void mbedcrypto_ecp_keypair_init( mbedcrypto_ecp_keypair *key ); |
| 349 | |
| 350 | /** |
| 351 | * \brief This function frees the components of a point. |
| 352 | * |
| 353 | * \param pt The point to free. |
| 354 | */ |
| 355 | void mbedcrypto_ecp_point_free( mbedcrypto_ecp_point *pt ); |
| 356 | |
| 357 | /** |
| 358 | * \brief This function frees the components of an ECP group. |
| 359 | * \param grp The group to free. |
| 360 | */ |
| 361 | void mbedcrypto_ecp_group_free( mbedcrypto_ecp_group *grp ); |
| 362 | |
| 363 | /** |
| 364 | * \brief This function frees the components of a key pair. |
| 365 | * \param key The key pair to free. |
| 366 | */ |
| 367 | void mbedcrypto_ecp_keypair_free( mbedcrypto_ecp_keypair *key ); |
| 368 | |
| 369 | /** |
| 370 | * \brief This function copies the contents of point \p Q into |
| 371 | * point \p P. |
| 372 | * |
| 373 | * \param P The destination point. |
| 374 | * \param Q The source point. |
| 375 | * |
| 376 | * \return \c 0 on success. |
| 377 | * \return #MBEDCRYPTO_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
| 378 | */ |
| 379 | int mbedcrypto_ecp_copy( mbedcrypto_ecp_point *P, const mbedcrypto_ecp_point *Q ); |
| 380 | |
| 381 | /** |
| 382 | * \brief This function copies the contents of group \p src into |
| 383 | * group \p dst. |
| 384 | * |
| 385 | * \param dst The destination group. |
| 386 | * \param src The source group. |
| 387 | * |
| 388 | * \return \c 0 on success. |
| 389 | * \return #MBEDCRYPTO_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
| 390 | */ |
| 391 | int mbedcrypto_ecp_group_copy( mbedcrypto_ecp_group *dst, const mbedcrypto_ecp_group *src ); |
| 392 | |
| 393 | /** |
| 394 | * \brief This function sets a point to zero. |
| 395 | * |
| 396 | * \param pt The point to set. |
| 397 | * |
| 398 | * \return \c 0 on success. |
| 399 | * \return #MBEDCRYPTO_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
| 400 | */ |
| 401 | int mbedcrypto_ecp_set_zero( mbedcrypto_ecp_point *pt ); |
| 402 | |
| 403 | /** |
| 404 | * \brief This function checks if a point is zero. |
| 405 | * |
| 406 | * \param pt The point to test. |
| 407 | * |
| 408 | * \return \c 1 if the point is zero. |
| 409 | * \return \c 0 if the point is non-zero. |
| 410 | */ |
| 411 | int mbedcrypto_ecp_is_zero( mbedcrypto_ecp_point *pt ); |
| 412 | |
| 413 | /** |
| 414 | * \brief This function compares two points. |
| 415 | * |
| 416 | * \note This assumes that the points are normalized. Otherwise, |
| 417 | * they may compare as "not equal" even if they are. |
| 418 | * |
| 419 | * \param P The first point to compare. |
| 420 | * \param Q The second point to compare. |
| 421 | * |
| 422 | * \return \c 0 if the points are equal. |
| 423 | * \return #MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA if the points are not equal. |
| 424 | */ |
| 425 | int mbedcrypto_ecp_point_cmp( const mbedcrypto_ecp_point *P, |
| 426 | const mbedcrypto_ecp_point *Q ); |
| 427 | |
| 428 | /** |
| 429 | * \brief This function imports a non-zero point from two ASCII |
| 430 | * strings. |
| 431 | * |
| 432 | * \param P The destination point. |
| 433 | * \param radix The numeric base of the input. |
| 434 | * \param x The first affine coordinate, as a null-terminated string. |
| 435 | * \param y The second affine coordinate, as a null-terminated string. |
| 436 | * |
| 437 | * \return \c 0 on success. |
| 438 | * \return An \c MBEDCRYPTO_ERR_MPI_XXX error code on failure. |
| 439 | */ |
| 440 | int mbedcrypto_ecp_point_read_string( mbedcrypto_ecp_point *P, int radix, |
| 441 | const char *x, const char *y ); |
| 442 | |
| 443 | /** |
| 444 | * \brief This function exports a point into unsigned binary data. |
| 445 | * |
| 446 | * \param grp The group to which the point should belong. |
| 447 | * \param P The point to export. |
| 448 | * \param format The point format. Should be an \c MBEDCRYPTO_ECP_PF_XXX macro. |
| 449 | * \param olen The length of the output. |
| 450 | * \param buf The output buffer. |
| 451 | * \param buflen The length of the output buffer. |
| 452 | * |
| 453 | * \return \c 0 on success. |
| 454 | * \return #MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA |
| 455 | * or #MBEDCRYPTO_ERR_ECP_BUFFER_TOO_SMALL on failure. |
| 456 | */ |
| 457 | int mbedcrypto_ecp_point_write_binary( const mbedcrypto_ecp_group *grp, const mbedcrypto_ecp_point *P, |
| 458 | int format, size_t *olen, |
| 459 | unsigned char *buf, size_t buflen ); |
| 460 | |
| 461 | /** |
| 462 | * \brief This function imports a point from unsigned binary data. |
| 463 | * |
| 464 | * \note This function does not check that the point actually |
| 465 | * belongs to the given group, see mbedcrypto_ecp_check_pubkey() |
| 466 | * for that. |
| 467 | * |
| 468 | * \param grp The group to which the point should belong. |
| 469 | * \param P The point to import. |
| 470 | * \param buf The input buffer. |
| 471 | * \param ilen The length of the input. |
| 472 | * |
| 473 | * \return \c 0 on success. |
| 474 | * \return #MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA if input is invalid. |
| 475 | * \return #MBEDCRYPTO_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
| 476 | * \return #MBEDCRYPTO_ERR_ECP_FEATURE_UNAVAILABLE if the point format |
| 477 | * is not implemented. |
| 478 | * |
| 479 | */ |
| 480 | int mbedcrypto_ecp_point_read_binary( const mbedcrypto_ecp_group *grp, mbedcrypto_ecp_point *P, |
| 481 | const unsigned char *buf, size_t ilen ); |
| 482 | |
| 483 | /** |
| 484 | * \brief This function imports a point from a TLS ECPoint record. |
| 485 | * |
| 486 | * \note On function return, \p buf is updated to point to immediately |
| 487 | * after the ECPoint record. |
| 488 | * |
| 489 | * \param grp The ECP group used. |
| 490 | * \param pt The destination point. |
| 491 | * \param buf The address of the pointer to the start of the input buffer. |
| 492 | * \param len The length of the buffer. |
| 493 | * |
| 494 | * \return \c 0 on success. |
| 495 | * \return An \c MBEDCRYPTO_ERR_MPI_XXX error code on initialization failure. |
| 496 | * \return #MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA if input is invalid. |
| 497 | */ |
| 498 | int mbedcrypto_ecp_tls_read_point( const mbedcrypto_ecp_group *grp, mbedcrypto_ecp_point *pt, |
| 499 | const unsigned char **buf, size_t len ); |
| 500 | |
| 501 | /** |
| 502 | * \brief This function exports a point as a TLS ECPoint record. |
| 503 | * |
| 504 | * \param grp The ECP group used. |
| 505 | * \param pt The point format to export to. The point format is an |
| 506 | * \c MBEDCRYPTO_ECP_PF_XXX constant. |
| 507 | * \param format The export format. |
| 508 | * \param olen The length of the data written. |
| 509 | * \param buf The buffer to write to. |
| 510 | * \param blen The length of the buffer. |
| 511 | * |
| 512 | * \return \c 0 on success. |
| 513 | * \return #MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA or |
| 514 | * #MBEDCRYPTO_ERR_ECP_BUFFER_TOO_SMALL on failure. |
| 515 | */ |
| 516 | int mbedcrypto_ecp_tls_write_point( const mbedcrypto_ecp_group *grp, const mbedcrypto_ecp_point *pt, |
| 517 | int format, size_t *olen, |
| 518 | unsigned char *buf, size_t blen ); |
| 519 | |
| 520 | /** |
| 521 | * \brief This function sets a group using standardized domain parameters. |
| 522 | * |
| 523 | * \note The index should be a value of the NamedCurve enum, |
| 524 | * as defined in <em>RFC-4492: Elliptic Curve Cryptography |
| 525 | * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>, |
| 526 | * usually in the form of an \c MBEDCRYPTO_ECP_DP_XXX macro. |
| 527 | * |
| 528 | * \param grp The destination group. |
| 529 | * \param id The identifier of the domain parameter set to load. |
| 530 | * |
| 531 | * \return \c 0 on success, |
| 532 | * \return An \c MBEDCRYPTO_ERR_MPI_XXX error code on initialization failure. |
| 533 | * \return #MBEDCRYPTO_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups. |
| 534 | |
| 535 | */ |
| 536 | int mbedcrypto_ecp_group_load( mbedcrypto_ecp_group *grp, mbedcrypto_ecp_group_id id ); |
| 537 | |
| 538 | /** |
| 539 | * \brief This function sets a group from a TLS ECParameters record. |
| 540 | * |
| 541 | * \note \p buf is updated to point right after the ECParameters record |
| 542 | * on exit. |
| 543 | * |
| 544 | * \param grp The destination group. |
| 545 | * \param buf The address of the pointer to the start of the input buffer. |
| 546 | * \param len The length of the buffer. |
| 547 | * |
| 548 | * \return \c 0 on success. |
| 549 | * \return An \c MBEDCRYPTO_ERR_MPI_XXX error code on initialization failure. |
| 550 | * \return #MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA if input is invalid. |
| 551 | */ |
| 552 | int mbedcrypto_ecp_tls_read_group( mbedcrypto_ecp_group *grp, const unsigned char **buf, size_t len ); |
| 553 | |
| 554 | /** |
| 555 | * \brief This function writes the TLS ECParameters record for a group. |
| 556 | * |
| 557 | * \param grp The ECP group used. |
| 558 | * \param olen The number of Bytes written. |
| 559 | * \param buf The buffer to write to. |
| 560 | * \param blen The length of the buffer. |
| 561 | * |
| 562 | * \return \c 0 on success. |
| 563 | * \return #MBEDCRYPTO_ERR_ECP_BUFFER_TOO_SMALL on failure. |
| 564 | */ |
| 565 | int mbedcrypto_ecp_tls_write_group( const mbedcrypto_ecp_group *grp, size_t *olen, |
| 566 | unsigned char *buf, size_t blen ); |
| 567 | |
| 568 | /** |
| 569 | * \brief This function performs multiplication of a point by |
| 570 | * an integer: \p R = \p m * \p P. |
| 571 | * |
| 572 | * It is not thread-safe to use same group in multiple threads. |
| 573 | * |
| 574 | * \note To prevent timing attacks, this function |
| 575 | * executes the exact same sequence of base-field |
| 576 | * operations for any valid \p m. It avoids any if-branch or |
| 577 | * array index depending on the value of \p m. |
| 578 | * |
| 579 | * \note If \p f_rng is not NULL, it is used to randomize |
| 580 | * intermediate results to prevent potential timing attacks |
| 581 | * targeting these results. We recommend always providing |
| 582 | * a non-NULL \p f_rng. The overhead is negligible. |
| 583 | * |
| 584 | * \param grp The ECP group. |
| 585 | * \param R The destination point. |
| 586 | * \param m The integer by which to multiply. |
| 587 | * \param P The point to multiply. |
| 588 | * \param f_rng The RNG function. |
| 589 | * \param p_rng The RNG context. |
| 590 | * |
| 591 | * \return \c 0 on success. |
| 592 | * \return #MBEDCRYPTO_ERR_ECP_INVALID_KEY if \p m is not a valid private |
| 593 | * key, or \p P is not a valid public key. |
| 594 | * \return #MBEDCRYPTO_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
| 595 | */ |
| 596 | int mbedcrypto_ecp_mul( mbedcrypto_ecp_group *grp, mbedcrypto_ecp_point *R, |
| 597 | const mbedcrypto_mpi *m, const mbedcrypto_ecp_point *P, |
| 598 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 599 | |
| 600 | /** |
| 601 | * \brief This function performs multiplication and addition of two |
| 602 | * points by integers: \p R = \p m * \p P + \p n * \p Q |
| 603 | * |
| 604 | * It is not thread-safe to use same group in multiple threads. |
| 605 | * |
| 606 | * \note In contrast to mbedcrypto_ecp_mul(), this function does not |
| 607 | * guarantee a constant execution flow and timing. |
| 608 | * |
| 609 | * \param grp The ECP group. |
| 610 | * \param R The destination point. |
| 611 | * \param m The integer by which to multiply \p P. |
| 612 | * \param P The point to multiply by \p m. |
| 613 | * \param n The integer by which to multiply \p Q. |
| 614 | * \param Q The point to be multiplied by \p n. |
| 615 | * |
| 616 | * \return \c 0 on success. |
| 617 | * \return #MBEDCRYPTO_ERR_ECP_INVALID_KEY if \p m or \p n are not |
| 618 | * valid private keys, or \p P or \p Q are not valid public |
| 619 | * keys. |
| 620 | * \return #MBEDCRYPTO_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
| 621 | */ |
| 622 | int mbedcrypto_ecp_muladd( mbedcrypto_ecp_group *grp, mbedcrypto_ecp_point *R, |
| 623 | const mbedcrypto_mpi *m, const mbedcrypto_ecp_point *P, |
| 624 | const mbedcrypto_mpi *n, const mbedcrypto_ecp_point *Q ); |
| 625 | |
| 626 | /** |
| 627 | * \brief This function checks that a point is a valid public key |
| 628 | * on this curve. |
| 629 | * |
| 630 | * It only checks that the point is non-zero, has |
| 631 | * valid coordinates and lies on the curve. It does not verify |
| 632 | * that it is indeed a multiple of \p G. This additional |
| 633 | * check is computationally more expensive, is not required |
| 634 | * by standards, and should not be necessary if the group |
| 635 | * used has a small cofactor. In particular, it is useless for |
| 636 | * the NIST groups which all have a cofactor of 1. |
| 637 | * |
| 638 | * \note This function uses bare components rather than an |
| 639 | * ::mbedcrypto_ecp_keypair structure, to ease use with other |
| 640 | * structures, such as ::mbedcrypto_ecdh_context or |
| 641 | * ::mbedcrypto_ecdsa_context. |
| 642 | * |
| 643 | * \param grp The curve the point should lie on. |
| 644 | * \param pt The point to check. |
| 645 | * |
| 646 | * \return \c 0 if the point is a valid public key. |
| 647 | * \return #MBEDCRYPTO_ERR_ECP_INVALID_KEY on failure. |
| 648 | */ |
| 649 | int mbedcrypto_ecp_check_pubkey( const mbedcrypto_ecp_group *grp, const mbedcrypto_ecp_point *pt ); |
| 650 | |
| 651 | /** |
| 652 | * \brief This function checks that an \p mbedcrypto_mpi is a valid private |
| 653 | * key for this curve. |
| 654 | * |
| 655 | * \note This function uses bare components rather than an |
| 656 | * ::mbedcrypto_ecp_keypair structure to ease use with other |
| 657 | * structures, such as ::mbedcrypto_ecdh_context or |
| 658 | * ::mbedcrypto_ecdsa_context. |
| 659 | * |
| 660 | * \param grp The group used. |
| 661 | * \param d The integer to check. |
| 662 | * |
| 663 | * \return \c 0 if the point is a valid private key. |
| 664 | * \return #MBEDCRYPTO_ERR_ECP_INVALID_KEY on failure. |
| 665 | */ |
| 666 | int mbedcrypto_ecp_check_privkey( const mbedcrypto_ecp_group *grp, const mbedcrypto_mpi *d ); |
| 667 | |
| 668 | /** |
| 669 | * \brief This function generates a keypair with a configurable base |
| 670 | * point. |
| 671 | * |
| 672 | * \note This function uses bare components rather than an |
| 673 | * ::mbedcrypto_ecp_keypair structure to ease use with other |
| 674 | * structures, such as ::mbedcrypto_ecdh_context or |
| 675 | * ::mbedcrypto_ecdsa_context. |
| 676 | * |
| 677 | * \param grp The ECP group. |
| 678 | * \param G The chosen base point. |
| 679 | * \param d The destination MPI (secret part). |
| 680 | * \param Q The destination point (public part). |
| 681 | * \param f_rng The RNG function. |
| 682 | * \param p_rng The RNG context. |
| 683 | * |
| 684 | * \return \c 0 on success. |
| 685 | * \return An \c MBEDCRYPTO_ERR_ECP_XXX or \c MBEDCRYPTO_MPI_XXX error code |
| 686 | * on failure. |
| 687 | */ |
| 688 | int mbedcrypto_ecp_gen_keypair_base( mbedcrypto_ecp_group *grp, |
| 689 | const mbedcrypto_ecp_point *G, |
| 690 | mbedcrypto_mpi *d, mbedcrypto_ecp_point *Q, |
| 691 | int (*f_rng)(void *, unsigned char *, size_t), |
| 692 | void *p_rng ); |
| 693 | |
| 694 | /** |
| 695 | * \brief This function generates an ECP keypair. |
| 696 | * |
| 697 | * \note This function uses bare components rather than an |
| 698 | * ::mbedcrypto_ecp_keypair structure to ease use with other |
| 699 | * structures, such as ::mbedcrypto_ecdh_context or |
| 700 | * ::mbedcrypto_ecdsa_context. |
| 701 | * |
| 702 | * \param grp The ECP group. |
| 703 | * \param d The destination MPI (secret part). |
| 704 | * \param Q The destination point (public part). |
| 705 | * \param f_rng The RNG function. |
| 706 | * \param p_rng The RNG context. |
| 707 | * |
| 708 | * \return \c 0 on success. |
| 709 | * \return An \c MBEDCRYPTO_ERR_ECP_XXX or \c MBEDCRYPTO_MPI_XXX error code |
| 710 | * on failure. |
| 711 | */ |
| 712 | int mbedcrypto_ecp_gen_keypair( mbedcrypto_ecp_group *grp, mbedcrypto_mpi *d, mbedcrypto_ecp_point *Q, |
| 713 | int (*f_rng)(void *, unsigned char *, size_t), |
| 714 | void *p_rng ); |
| 715 | |
| 716 | /** |
| 717 | * \brief This function generates an ECP key. |
| 718 | * |
| 719 | * \param grp_id The ECP group identifier. |
| 720 | * \param key The destination key. |
| 721 | * \param f_rng The RNG function. |
| 722 | * \param p_rng The RNG context. |
| 723 | * |
| 724 | * \return \c 0 on success. |
| 725 | * \return An \c MBEDCRYPTO_ERR_ECP_XXX or \c MBEDCRYPTO_MPI_XXX error code |
| 726 | * on failure. |
| 727 | */ |
| 728 | int mbedcrypto_ecp_gen_key( mbedcrypto_ecp_group_id grp_id, mbedcrypto_ecp_keypair *key, |
| 729 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 730 | |
| 731 | /** |
| 732 | * \brief This function checks that the keypair objects |
| 733 | * \p pub and \p prv have the same group and the |
| 734 | * same public point, and that the private key in |
| 735 | * \p prv is consistent with the public key. |
| 736 | * |
| 737 | * \param pub The keypair structure holding the public key. |
| 738 | * If it contains a private key, that part is ignored. |
| 739 | * \param prv The keypair structure holding the full keypair. |
| 740 | * |
| 741 | * \return \c 0 on success, meaning that the keys are valid and match. |
| 742 | * \return #MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA if the keys are invalid or do not match. |
| 743 | * \return An \c MBEDCRYPTO_ERR_ECP_XXX or an \c MBEDCRYPTO_ERR_MPI_XXX |
| 744 | * error code on calculation failure. |
| 745 | */ |
| 746 | int mbedcrypto_ecp_check_pub_priv( const mbedcrypto_ecp_keypair *pub, const mbedcrypto_ecp_keypair *prv ); |
| 747 | |
| 748 | #if defined(MBEDCRYPTO_SELF_TEST) |
| 749 | |
| 750 | /** |
| 751 | * \brief The ECP checkup routine. |
| 752 | * |
| 753 | * \return \c 0 on success. |
| 754 | * \return \c 1 on failure. |
| 755 | */ |
| 756 | int mbedcrypto_ecp_self_test( int verbose ); |
| 757 | |
| 758 | #endif /* MBEDCRYPTO_SELF_TEST */ |
| 759 | |
| 760 | #ifdef __cplusplus |
| 761 | } |
| 762 | #endif |
| 763 | |
| 764 | #endif /* ecp.h */ |