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 The Mbed TLS Contributors 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 34 #ifndef MBEDTLS_ECP_H 35 #define MBEDTLS_ECP_H 36 #include "mbedtls/private_access.h" 37 38 #include "mbedtls/build_info.h" 39 40 #include "mbedtls/bignum.h" 41 42 /* 43 * ECP error codes 44 */ 45 /** Bad input parameters to function. */ 46 #define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80 47 /** The buffer is too small to write to. */ 48 #define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 49 /** The requested feature is not available, for example, the requested curve is not supported. */ 50 #define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 51 /** The signature is not valid. */ 52 #define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00 53 /** Memory allocation failed. */ 54 #define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80 55 /** Generation of random value, such as ephemeral key, failed. */ 56 #define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00 57 /** Invalid private or public key. */ 58 #define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 59 /** The buffer contains a valid signature followed by more data. */ 60 #define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 61 /** Operation in progress, call again with the same parameters to continue. */ 62 #define MBEDTLS_ERR_ECP_IN_PROGRESS -0x4B00 63 64 /* Flags indicating whether to include code that is specific to certain 65 * types of curves. These flags are for internal library use only. */ 66 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \ 67 defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \ 68 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \ 69 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \ 70 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \ 71 defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \ 72 defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \ 73 defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \ 74 defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \ 75 defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \ 76 defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) 77 #define MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED 78 #endif 79 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \ 80 defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) 81 #define MBEDTLS_ECP_MONTGOMERY_ENABLED 82 #endif 83 84 #ifdef __cplusplus 85 extern "C" { 86 #endif 87 88 /** 89 * Domain-parameter identifiers: curve, subgroup, and generator. 90 * 91 * \note Only curves over prime fields are supported. 92 * 93 * \warning This library does not support validation of arbitrary domain 94 * parameters. Therefore, only standardized domain parameters from trusted 95 * sources should be used. See mbedtls_ecp_group_load(). 96 */ 97 /* Note: when adding a new curve: 98 * - Add it at the end of this enum, otherwise you'll break the ABI by 99 * changing the numerical value for existing curves. 100 * - Increment MBEDTLS_ECP_DP_MAX below if needed. 101 * - Update the calculation of MBEDTLS_ECP_MAX_BITS below. 102 * - Add the corresponding MBEDTLS_ECP_DP_xxx_ENABLED macro definition to 103 * mbedtls_config.h. 104 * - List the curve as a dependency of MBEDTLS_ECP_C and 105 * MBEDTLS_ECDSA_C if supported in check_config.h. 106 * - Add the curve to the appropriate curve type macro 107 * MBEDTLS_ECP_yyy_ENABLED above. 108 * - Add the necessary definitions to ecp_curves.c. 109 * - Add the curve to the ecp_supported_curves array in ecp.c. 110 * - Add the curve to applicable profiles in x509_crt.c. 111 * - Add the curve to applicable presets in ssl_tls.c. 112 */ 113 typedef enum { 114 MBEDTLS_ECP_DP_NONE = 0, /*!< Curve not defined. */ 115 MBEDTLS_ECP_DP_SECP192R1, /*!< Domain parameters for the 192-bit curve defined by FIPS 186-4 and SEC1. */ 116 MBEDTLS_ECP_DP_SECP224R1, /*!< Domain parameters for the 224-bit curve defined by FIPS 186-4 and SEC1. */ 117 MBEDTLS_ECP_DP_SECP256R1, /*!< Domain parameters for the 256-bit curve defined by FIPS 186-4 and SEC1. */ 118 MBEDTLS_ECP_DP_SECP384R1, /*!< Domain parameters for the 384-bit curve defined by FIPS 186-4 and SEC1. */ 119 MBEDTLS_ECP_DP_SECP521R1, /*!< Domain parameters for the 521-bit curve defined by FIPS 186-4 and SEC1. */ 120 MBEDTLS_ECP_DP_BP256R1, /*!< Domain parameters for 256-bit Brainpool curve. */ 121 MBEDTLS_ECP_DP_BP384R1, /*!< Domain parameters for 384-bit Brainpool curve. */ 122 MBEDTLS_ECP_DP_BP512R1, /*!< Domain parameters for 512-bit Brainpool curve. */ 123 MBEDTLS_ECP_DP_CURVE25519, /*!< Domain parameters for Curve25519. */ 124 MBEDTLS_ECP_DP_SECP192K1, /*!< Domain parameters for 192-bit "Koblitz" curve. */ 125 MBEDTLS_ECP_DP_SECP224K1, /*!< Domain parameters for 224-bit "Koblitz" curve. */ 126 MBEDTLS_ECP_DP_SECP256K1, /*!< Domain parameters for 256-bit "Koblitz" curve. */ 127 MBEDTLS_ECP_DP_CURVE448, /*!< Domain parameters for Curve448. */ 128 } mbedtls_ecp_group_id; 129 130 /** 131 * The number of supported curves, plus one for #MBEDTLS_ECP_DP_NONE. 132 */ 133 #define MBEDTLS_ECP_DP_MAX 14 134 135 /* 136 * Curve types 137 */ 138 typedef enum { 139 MBEDTLS_ECP_TYPE_NONE = 0, 140 MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */ 141 MBEDTLS_ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */ 142 } mbedtls_ecp_curve_type; 143 144 /* 145 * Curve modulus types 146 */ 147 typedef enum { 148 MBEDTLS_ECP_MOD_NONE = 0, 149 MBEDTLS_ECP_MOD_COORDINATE, 150 MBEDTLS_ECP_MOD_SCALAR 151 } mbedtls_ecp_modulus_type; 152 153 /** 154 * Curve information, for use by other modules. 155 * 156 * The fields of this structure are part of the public API and can be 157 * accessed directly by applications. Future versions of the library may 158 * add extra fields or reorder existing fields. 159 */ 160 typedef struct mbedtls_ecp_curve_info { 161 mbedtls_ecp_group_id grp_id; /*!< An internal identifier. */ 162 uint16_t tls_id; /*!< The TLS NamedCurve identifier. */ 163 uint16_t bit_size; /*!< The curve size in bits. */ 164 const char *name; /*!< A human-friendly name. */ 165 } mbedtls_ecp_curve_info; 166 167 /** 168 * \brief The ECP point structure, in Jacobian coordinates. 169 * 170 * \note All functions expect and return points satisfying 171 * the following condition: <code>Z == 0</code> or 172 * <code>Z == 1</code>. Other values of \p Z are 173 * used only by internal functions. 174 * The point is zero, or "at infinity", if <code>Z == 0</code>. 175 * Otherwise, \p X and \p Y are its standard (affine) 176 * coordinates. 177 */ 178 typedef struct mbedtls_ecp_point { 179 mbedtls_mpi MBEDTLS_PRIVATE(X); /*!< The X coordinate of the ECP point. */ 180 mbedtls_mpi MBEDTLS_PRIVATE(Y); /*!< The Y coordinate of the ECP point. */ 181 mbedtls_mpi MBEDTLS_PRIVATE(Z); /*!< The Z coordinate of the ECP point. */ 182 } 183 mbedtls_ecp_point; 184 185 #if !defined(MBEDTLS_ECP_ALT) 186 /* 187 * default mbed TLS elliptic curve arithmetic implementation 188 * 189 * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an 190 * alternative implementation for the whole module and it will replace this 191 * one.) 192 */ 193 194 /** 195 * \brief The ECP group structure. 196 * 197 * We consider two types of curve equations: 198 * <ul><li>Short Weierstrass: <code>y^2 = x^3 + A x + B mod P</code> 199 * (SEC1 + RFC-4492)</li> 200 * <li>Montgomery: <code>y^2 = x^3 + A x^2 + x mod P</code> (Curve25519, 201 * Curve448)</li></ul> 202 * In both cases, the generator (\p G) for a prime-order subgroup is fixed. 203 * 204 * For Short Weierstrass, this subgroup is the whole curve, and its 205 * cardinality is denoted by \p N. Our code requires that \p N is an 206 * odd prime as mbedtls_ecp_mul() requires an odd number, and 207 * mbedtls_ecdsa_sign() requires that it is prime for blinding purposes. 208 * 209 * For Montgomery curves, we do not store \p A, but <code>(A + 2) / 4</code>, 210 * which is the quantity used in the formulas. Additionally, \p nbits is 211 * not the size of \p N but the required size for private keys. 212 * 213 * If \p modp is NULL, reduction modulo \p P is done using a generic algorithm. 214 * Otherwise, \p modp must point to a function that takes an \p mbedtls_mpi in the 215 * range of <code>0..2^(2*pbits)-1</code>, and transforms it in-place to an integer 216 * which is congruent mod \p P to the given MPI, and is close enough to \p pbits 217 * in size, so that it may be efficiently brought in the 0..P-1 range by a few 218 * additions or subtractions. Therefore, it is only an approximative modular 219 * reduction. It must return 0 on success and non-zero on failure. 220 * 221 * \note Alternative implementations of the ECP module must obey the 222 * following constraints. 223 * * Group IDs must be distinct: if two group structures have 224 * the same ID, then they must be identical. 225 * * The fields \c id, \c P, \c A, \c B, \c G, \c N, 226 * \c pbits and \c nbits must have the same type and semantics 227 * as in the built-in implementation. 228 * They must be available for reading, but direct modification 229 * of these fields does not need to be supported. 230 * They do not need to be at the same offset in the structure. 231 */ 232 typedef struct mbedtls_ecp_group { 233 mbedtls_ecp_group_id id; /*!< An internal group identifier. */ 234 mbedtls_mpi P; /*!< The prime modulus of the base field. */ 235 mbedtls_mpi A; /*!< For Short Weierstrass: \p A in the equation. For 236 Montgomery curves: <code>(A + 2) / 4</code>. */ 237 mbedtls_mpi B; /*!< For Short Weierstrass: \p B in the equation. 238 For Montgomery curves: unused. */ 239 mbedtls_ecp_point G; /*!< The generator of the subgroup used. */ 240 mbedtls_mpi N; /*!< The order of \p G. */ 241 size_t pbits; /*!< The number of bits in \p P.*/ 242 size_t nbits; /*!< For Short Weierstrass: The number of bits in \p P. 243 For Montgomery curves: the number of bits in the 244 private keys. */ 245 /* End of public fields */ 246 247 unsigned int MBEDTLS_PRIVATE(h); /*!< \internal 1 if the constants are static. */ 248 int(*MBEDTLS_PRIVATE(modp))(mbedtls_mpi *); /*!< The function for fast pseudo-reduction 249 mod \p P (see above).*/ 250 int(*MBEDTLS_PRIVATE(t_pre))(mbedtls_ecp_point *, void *); /*!< Unused. */ 251 int(*MBEDTLS_PRIVATE(t_post))(mbedtls_ecp_point *, void *); /*!< Unused. */ 252 void *MBEDTLS_PRIVATE(t_data); /*!< Unused. */ 253 mbedtls_ecp_point *MBEDTLS_PRIVATE(T); /*!< Pre-computed points for ecp_mul_comb(). */ 254 size_t MBEDTLS_PRIVATE(T_size); /*!< The number of dynamic allocated pre-computed points. */ 255 } 256 mbedtls_ecp_group; 257 258 /** 259 * \name SECTION: Module settings 260 * 261 * The configuration options you can set for this module are in this section. 262 * Either change them in mbedtls_config.h, or define them using the compiler command line. 263 * \{ 264 */ 265 266 #if !defined(MBEDTLS_ECP_WINDOW_SIZE) 267 /* 268 * Maximum "window" size used for point multiplication. 269 * Default: a point where higher memory usage yields diminishing performance 270 * returns. 271 * Minimum value: 2. Maximum value: 7. 272 * 273 * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) ) 274 * points used for point multiplication. This value is directly tied to EC 275 * peak memory usage, so decreasing it by one should roughly cut memory usage 276 * by two (if large curves are in use). 277 * 278 * Reduction in size may reduce speed, but larger curves are impacted first. 279 * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1): 280 * w-size: 6 5 4 3 2 281 * 521 145 141 135 120 97 282 * 384 214 209 198 177 146 283 * 256 320 320 303 262 226 284 * 224 475 475 453 398 342 285 * 192 640 640 633 587 476 286 */ 287 #define MBEDTLS_ECP_WINDOW_SIZE 4 /**< The maximum window size used. */ 288 #endif /* MBEDTLS_ECP_WINDOW_SIZE */ 289 290 #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM) 291 /* 292 * Trade code size for speed on fixed-point multiplication. 293 * 294 * This speeds up repeated multiplication of the generator (that is, the 295 * multiplication in ECDSA signatures, and half of the multiplications in 296 * ECDSA verification and ECDHE) by a factor roughly 3 to 4. 297 * 298 * For each n-bit Short Weierstrass curve that is enabled, this adds 4n bytes 299 * of code size if n < 384 and 8n otherwise. 300 * 301 * Change this value to 0 to reduce code size. 302 */ 303 #define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up. */ 304 #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */ 305 306 /** \} name SECTION: Module settings */ 307 308 #else /* MBEDTLS_ECP_ALT */ 309 #include "ecp_alt.h" 310 #endif /* MBEDTLS_ECP_ALT */ 311 312 /** 313 * The maximum size of the groups, that is, of \c N and \c P. 314 */ 315 #if !defined(MBEDTLS_ECP_C) 316 /* Dummy definition to help code that has optional ECP support and 317 * defines an MBEDTLS_ECP_MAX_BYTES-sized array unconditionally. */ 318 #define MBEDTLS_ECP_MAX_BITS 1 319 /* Note: the curves must be listed in DECREASING size! */ 320 #elif defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) 321 #define MBEDTLS_ECP_MAX_BITS 521 322 #elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) 323 #define MBEDTLS_ECP_MAX_BITS 512 324 #elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) 325 #define MBEDTLS_ECP_MAX_BITS 448 326 #elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) 327 #define MBEDTLS_ECP_MAX_BITS 384 328 #elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) 329 #define MBEDTLS_ECP_MAX_BITS 384 330 #elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) 331 #define MBEDTLS_ECP_MAX_BITS 256 332 #elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) 333 #define MBEDTLS_ECP_MAX_BITS 256 334 #elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) 335 #define MBEDTLS_ECP_MAX_BITS 256 336 #elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) 337 #define MBEDTLS_ECP_MAX_BITS 255 338 #elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) 339 #define MBEDTLS_ECP_MAX_BITS 225 // n is slightly above 2^224 340 #elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) 341 #define MBEDTLS_ECP_MAX_BITS 224 342 #elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) 343 #define MBEDTLS_ECP_MAX_BITS 192 344 #elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) 345 #define MBEDTLS_ECP_MAX_BITS 192 346 #else 347 #error "Missing definition of MBEDTLS_ECP_MAX_BITS" 348 #endif 349 350 #define MBEDTLS_ECP_MAX_BYTES ((MBEDTLS_ECP_MAX_BITS + 7) / 8) 351 #define MBEDTLS_ECP_MAX_PT_LEN (2 * MBEDTLS_ECP_MAX_BYTES + 1) 352 353 #if defined(MBEDTLS_ECP_RESTARTABLE) 354 355 /** 356 * \brief Internal restart context for multiplication 357 * 358 * \note Opaque struct 359 */ 360 typedef struct mbedtls_ecp_restart_mul mbedtls_ecp_restart_mul_ctx; 361 362 /** 363 * \brief Internal restart context for ecp_muladd() 364 * 365 * \note Opaque struct 366 */ 367 typedef struct mbedtls_ecp_restart_muladd mbedtls_ecp_restart_muladd_ctx; 368 369 /** 370 * \brief General context for resuming ECC operations 371 */ 372 typedef struct { 373 unsigned MBEDTLS_PRIVATE(ops_done); /*!< current ops count */ 374 unsigned MBEDTLS_PRIVATE(depth); /*!< call depth (0 = top-level) */ 375 mbedtls_ecp_restart_mul_ctx *MBEDTLS_PRIVATE(rsm); /*!< ecp_mul_comb() sub-context */ 376 mbedtls_ecp_restart_muladd_ctx *MBEDTLS_PRIVATE(ma); /*!< ecp_muladd() sub-context */ 377 } mbedtls_ecp_restart_ctx; 378 379 /* 380 * Operation counts for restartable functions 381 */ 382 #define MBEDTLS_ECP_OPS_CHK 3 /*!< basic ops count for ecp_check_pubkey() */ 383 #define MBEDTLS_ECP_OPS_DBL 8 /*!< basic ops count for ecp_double_jac() */ 384 #define MBEDTLS_ECP_OPS_ADD 11 /*!< basic ops count for see ecp_add_mixed() */ 385 #define MBEDTLS_ECP_OPS_INV 120 /*!< empirical equivalent for mpi_mod_inv() */ 386 387 /** 388 * \brief Internal; for restartable functions in other modules. 389 * Check and update basic ops budget. 390 * 391 * \param grp Group structure 392 * \param rs_ctx Restart context 393 * \param ops Number of basic ops to do 394 * 395 * \return \c 0 if doing \p ops basic ops is still allowed, 396 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS otherwise. 397 */ 398 int mbedtls_ecp_check_budget(const mbedtls_ecp_group *grp, 399 mbedtls_ecp_restart_ctx *rs_ctx, 400 unsigned ops); 401 402 /* Utility macro for checking and updating ops budget */ 403 #define MBEDTLS_ECP_BUDGET(ops) \ 404 MBEDTLS_MPI_CHK(mbedtls_ecp_check_budget(grp, rs_ctx, \ 405 (unsigned) (ops))); 406 407 #else /* MBEDTLS_ECP_RESTARTABLE */ 408 409 #define MBEDTLS_ECP_BUDGET(ops) /* no-op; for compatibility */ 410 411 /* We want to declare restartable versions of existing functions anyway */ 412 typedef void mbedtls_ecp_restart_ctx; 413 414 #endif /* MBEDTLS_ECP_RESTARTABLE */ 415 416 /** 417 * \brief The ECP key-pair structure. 418 * 419 * A generic key-pair that may be used for ECDSA and fixed ECDH, for example. 420 * 421 * \note Members are deliberately in the same order as in the 422 * ::mbedtls_ecdsa_context structure. 423 */ 424 typedef struct mbedtls_ecp_keypair { 425 mbedtls_ecp_group MBEDTLS_PRIVATE(grp); /*!< Elliptic curve and base point */ 426 mbedtls_mpi MBEDTLS_PRIVATE(d); /*!< our secret value */ 427 mbedtls_ecp_point MBEDTLS_PRIVATE(Q); /*!< our public value */ 428 } 429 mbedtls_ecp_keypair; 430 431 /** 432 * The uncompressed point format for Short Weierstrass curves 433 * (MBEDTLS_ECP_DP_SECP_XXX and MBEDTLS_ECP_DP_BP_XXX). 434 */ 435 #define MBEDTLS_ECP_PF_UNCOMPRESSED 0 436 /** 437 * The compressed point format for Short Weierstrass curves 438 * (MBEDTLS_ECP_DP_SECP_XXX and MBEDTLS_ECP_DP_BP_XXX). 439 * 440 * \warning While this format is supported for all concerned curves for 441 * writing, when it comes to parsing, it is not supported for all 442 * curves. Specifically, parsing compressed points on 443 * MBEDTLS_ECP_DP_SECP224R1 and MBEDTLS_ECP_DP_SECP224K1 is not 444 * supported. 445 */ 446 #define MBEDTLS_ECP_PF_COMPRESSED 1 447 448 /* 449 * Some other constants from RFC 4492 450 */ 451 #define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< The named_curve of ECCurveType. */ 452 453 #if defined(MBEDTLS_ECP_RESTARTABLE) 454 /** 455 * \brief Set the maximum number of basic operations done in a row. 456 * 457 * If more operations are needed to complete a computation, 458 * #MBEDTLS_ERR_ECP_IN_PROGRESS will be returned by the 459 * function performing the computation. It is then the 460 * caller's responsibility to either call again with the same 461 * parameters until it returns 0 or an error code; or to free 462 * the restart context if the operation is to be aborted. 463 * 464 * It is strictly required that all input parameters and the 465 * restart context be the same on successive calls for the 466 * same operation, but output parameters need not be the 467 * same; they must not be used until the function finally 468 * returns 0. 469 * 470 * This only applies to functions whose documentation 471 * mentions they may return #MBEDTLS_ERR_ECP_IN_PROGRESS (or 472 * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS for functions in the 473 * SSL module). For functions that accept a "restart context" 474 * argument, passing NULL disables restart and makes the 475 * function equivalent to the function with the same name 476 * with \c _restartable removed. For functions in the ECDH 477 * module, restart is disabled unless the function accepts 478 * an "ECDH context" argument and 479 * mbedtls_ecdh_enable_restart() was previously called on 480 * that context. For function in the SSL module, restart is 481 * only enabled for specific sides and key exchanges 482 * (currently only for clients and ECDHE-ECDSA). 483 * 484 * \warning Using the PSA interruptible interfaces with keys in local 485 * storage and no accelerator driver will also call this 486 * function to set the values specified via those interfaces, 487 * overwriting values previously set. Care should be taken if 488 * mixing these two interfaces. 489 * 490 * \param max_ops Maximum number of basic operations done in a row. 491 * Default: 0 (unlimited). 492 * Lower (non-zero) values mean ECC functions will block for 493 * a lesser maximum amount of time. 494 * 495 * \note A "basic operation" is defined as a rough equivalent of a 496 * multiplication in GF(p) for the NIST P-256 curve. 497 * As an indication, with default settings, a scalar 498 * multiplication (full run of \c mbedtls_ecp_mul()) is: 499 * - about 3300 basic operations for P-256 500 * - about 9400 basic operations for P-384 501 * 502 * \note Very low values are not always respected: sometimes 503 * functions need to block for a minimum number of 504 * operations, and will do so even if max_ops is set to a 505 * lower value. That minimum depends on the curve size, and 506 * can be made lower by decreasing the value of 507 * \c MBEDTLS_ECP_WINDOW_SIZE. As an indication, here is the 508 * lowest effective value for various curves and values of 509 * that parameter (w for short): 510 * w=6 w=5 w=4 w=3 w=2 511 * P-256 208 208 160 136 124 512 * P-384 682 416 320 272 248 513 * P-521 1364 832 640 544 496 514 * 515 * \note This setting is currently ignored by Curve25519. 516 */ 517 void mbedtls_ecp_set_max_ops(unsigned max_ops); 518 519 /** 520 * \brief Check if restart is enabled (max_ops != 0) 521 * 522 * \return \c 0 if \c max_ops == 0 (restart disabled) 523 * \return \c 1 otherwise (restart enabled) 524 */ 525 int mbedtls_ecp_restart_is_enabled(void); 526 #endif /* MBEDTLS_ECP_RESTARTABLE */ 527 528 /* 529 * Get the type of a curve 530 */ 531 mbedtls_ecp_curve_type mbedtls_ecp_get_type(const mbedtls_ecp_group *grp); 532 533 /** 534 * \brief This function retrieves the information defined in 535 * mbedtls_ecp_curve_info() for all supported curves. 536 * 537 * \note This function returns information about all curves 538 * supported by the library. Some curves may not be 539 * supported for all algorithms. Call mbedtls_ecdh_can_do() 540 * or mbedtls_ecdsa_can_do() to check if a curve is 541 * supported for ECDH or ECDSA. 542 * 543 * \return A statically allocated array. The last entry is 0. 544 */ 545 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list(void); 546 547 /** 548 * \brief This function retrieves the list of internal group 549 * identifiers of all supported curves in the order of 550 * preference. 551 * 552 * \note This function returns information about all curves 553 * supported by the library. Some curves may not be 554 * supported for all algorithms. Call mbedtls_ecdh_can_do() 555 * or mbedtls_ecdsa_can_do() to check if a curve is 556 * supported for ECDH or ECDSA. 557 * 558 * \return A statically allocated array, 559 * terminated with MBEDTLS_ECP_DP_NONE. 560 */ 561 const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void); 562 563 /** 564 * \brief This function retrieves curve information from an internal 565 * group identifier. 566 * 567 * \param grp_id An \c MBEDTLS_ECP_DP_XXX value. 568 * 569 * \return The associated curve information on success. 570 * \return NULL on failure. 571 */ 572 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_group_id grp_id); 573 574 /** 575 * \brief This function retrieves curve information from a TLS 576 * NamedCurve value. 577 * 578 * \param tls_id An \c MBEDTLS_ECP_DP_XXX value. 579 * 580 * \return The associated curve information on success. 581 * \return NULL on failure. 582 */ 583 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id); 584 585 /** 586 * \brief This function retrieves curve information from a 587 * human-readable name. 588 * 589 * \param name The human-readable name. 590 * 591 * \return The associated curve information on success. 592 * \return NULL on failure. 593 */ 594 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name(const char *name); 595 596 /** 597 * \brief This function initializes a point as zero. 598 * 599 * \param pt The point to initialize. 600 */ 601 void mbedtls_ecp_point_init(mbedtls_ecp_point *pt); 602 603 /** 604 * \brief This function initializes an ECP group context 605 * without loading any domain parameters. 606 * 607 * \note After this function is called, domain parameters 608 * for various ECP groups can be loaded through the 609 * mbedtls_ecp_group_load() or mbedtls_ecp_tls_read_group() 610 * functions. 611 */ 612 void mbedtls_ecp_group_init(mbedtls_ecp_group *grp); 613 614 /** 615 * \brief This function initializes a key pair as an invalid one. 616 * 617 * \param key The key pair to initialize. 618 */ 619 void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key); 620 621 /** 622 * \brief This function frees the components of a point. 623 * 624 * \param pt The point to free. 625 */ 626 void mbedtls_ecp_point_free(mbedtls_ecp_point *pt); 627 628 /** 629 * \brief This function frees the components of an ECP group. 630 * 631 * \param grp The group to free. This may be \c NULL, in which 632 * case this function returns immediately. If it is not 633 * \c NULL, it must point to an initialized ECP group. 634 */ 635 void mbedtls_ecp_group_free(mbedtls_ecp_group *grp); 636 637 /** 638 * \brief This function frees the components of a key pair. 639 * 640 * \param key The key pair to free. This may be \c NULL, in which 641 * case this function returns immediately. If it is not 642 * \c NULL, it must point to an initialized ECP key pair. 643 */ 644 void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key); 645 646 #if defined(MBEDTLS_ECP_RESTARTABLE) 647 /** 648 * \brief Initialize a restart context. 649 * 650 * \param ctx The restart context to initialize. This must 651 * not be \c NULL. 652 */ 653 void mbedtls_ecp_restart_init(mbedtls_ecp_restart_ctx *ctx); 654 655 /** 656 * \brief Free the components of a restart context. 657 * 658 * \param ctx The restart context to free. This may be \c NULL, in which 659 * case this function returns immediately. If it is not 660 * \c NULL, it must point to an initialized restart context. 661 */ 662 void mbedtls_ecp_restart_free(mbedtls_ecp_restart_ctx *ctx); 663 #endif /* MBEDTLS_ECP_RESTARTABLE */ 664 665 /** 666 * \brief This function copies the contents of point \p Q into 667 * point \p P. 668 * 669 * \param P The destination point. This must be initialized. 670 * \param Q The source point. This must be initialized. 671 * 672 * \return \c 0 on success. 673 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. 674 * \return Another negative error code for other kinds of failure. 675 */ 676 int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q); 677 678 /** 679 * \brief This function copies the contents of group \p src into 680 * group \p dst. 681 * 682 * \param dst The destination group. This must be initialized. 683 * \param src The source group. This must be initialized. 684 * 685 * \return \c 0 on success. 686 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. 687 * \return Another negative error code on other kinds of failure. 688 */ 689 int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst, 690 const mbedtls_ecp_group *src); 691 692 /** 693 * \brief This function sets a point to the point at infinity. 694 * 695 * \param pt The point to set. This must be initialized. 696 * 697 * \return \c 0 on success. 698 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. 699 * \return Another negative error code on other kinds of failure. 700 */ 701 int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt); 702 703 /** 704 * \brief This function checks if a point is the point at infinity. 705 * 706 * \param pt The point to test. This must be initialized. 707 * 708 * \return \c 1 if the point is zero. 709 * \return \c 0 if the point is non-zero. 710 * \return A negative error code on failure. 711 */ 712 int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt); 713 714 /** 715 * \brief This function compares two points. 716 * 717 * \note This assumes that the points are normalized. Otherwise, 718 * they may compare as "not equal" even if they are. 719 * 720 * \param P The first point to compare. This must be initialized. 721 * \param Q The second point to compare. This must be initialized. 722 * 723 * \return \c 0 if the points are equal. 724 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the points are not equal. 725 */ 726 int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P, 727 const mbedtls_ecp_point *Q); 728 729 /** 730 * \brief This function imports a non-zero point from two ASCII 731 * strings. 732 * 733 * \param P The destination point. This must be initialized. 734 * \param radix The numeric base of the input. 735 * \param x The first affine coordinate, as a null-terminated string. 736 * \param y The second affine coordinate, as a null-terminated string. 737 * 738 * \return \c 0 on success. 739 * \return An \c MBEDTLS_ERR_MPI_XXX error code on failure. 740 */ 741 int mbedtls_ecp_point_read_string(mbedtls_ecp_point *P, int radix, 742 const char *x, const char *y); 743 744 /** 745 * \brief This function exports a point into unsigned binary data. 746 * 747 * \param grp The group to which the point should belong. 748 * This must be initialized and have group parameters 749 * set, for example through mbedtls_ecp_group_load(). 750 * \param P The point to export. This must be initialized. 751 * \param format The point format. This must be either 752 * #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED. 753 * (For groups without these formats, this parameter is 754 * ignored. But it still has to be either of the above 755 * values.) 756 * \param olen The address at which to store the length of 757 * the output in Bytes. This must not be \c NULL. 758 * \param buf The output buffer. This must be a writable buffer 759 * of length \p buflen Bytes. 760 * \param buflen The length of the output buffer \p buf in Bytes. 761 * 762 * \return \c 0 on success. 763 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output buffer 764 * is too small to hold the point. 765 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format 766 * or the export for the given group is not implemented. 767 * \return Another negative error code on other kinds of failure. 768 */ 769 int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp, 770 const mbedtls_ecp_point *P, 771 int format, size_t *olen, 772 unsigned char *buf, size_t buflen); 773 774 /** 775 * \brief This function imports a point from unsigned binary data. 776 * 777 * \note This function does not check that the point actually 778 * belongs to the given group, see mbedtls_ecp_check_pubkey() 779 * for that. 780 * 781 * \note For compressed points, see #MBEDTLS_ECP_PF_COMPRESSED for 782 * limitations. 783 * 784 * \param grp The group to which the point should belong. 785 * This must be initialized and have group parameters 786 * set, for example through mbedtls_ecp_group_load(). 787 * \param P The destination context to import the point to. 788 * This must be initialized. 789 * \param buf The input buffer. This must be a readable buffer 790 * of length \p ilen Bytes. 791 * \param ilen The length of the input buffer \p buf in Bytes. 792 * 793 * \return \c 0 on success. 794 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid. 795 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. 796 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the import for the 797 * given group is not implemented. 798 */ 799 int mbedtls_ecp_point_read_binary(const mbedtls_ecp_group *grp, 800 mbedtls_ecp_point *P, 801 const unsigned char *buf, size_t ilen); 802 803 /** 804 * \brief This function imports a point from a TLS ECPoint record. 805 * 806 * \note On function return, \p *buf is updated to point immediately 807 * after the ECPoint record. 808 * 809 * \param grp The ECP group to use. 810 * This must be initialized and have group parameters 811 * set, for example through mbedtls_ecp_group_load(). 812 * \param pt The destination point. 813 * \param buf The address of the pointer to the start of the input buffer. 814 * \param len The length of the buffer. 815 * 816 * \return \c 0 on success. 817 * \return An \c MBEDTLS_ERR_MPI_XXX error code on initialization 818 * failure. 819 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid. 820 */ 821 int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp, 822 mbedtls_ecp_point *pt, 823 const unsigned char **buf, size_t len); 824 825 /** 826 * \brief This function exports a point as a TLS ECPoint record 827 * defined in RFC 4492, Section 5.4. 828 * 829 * \param grp The ECP group to use. 830 * This must be initialized and have group parameters 831 * set, for example through mbedtls_ecp_group_load(). 832 * \param pt The point to be exported. This must be initialized. 833 * \param format The point format to use. This must be either 834 * #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED. 835 * \param olen The address at which to store the length in Bytes 836 * of the data written. 837 * \param buf The target buffer. This must be a writable buffer of 838 * length \p blen Bytes. 839 * \param blen The length of the target buffer \p buf in Bytes. 840 * 841 * \return \c 0 on success. 842 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid. 843 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the target buffer 844 * is too small to hold the exported point. 845 * \return Another negative error code on other kinds of failure. 846 */ 847 int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp, 848 const mbedtls_ecp_point *pt, 849 int format, size_t *olen, 850 unsigned char *buf, size_t blen); 851 852 /** 853 * \brief This function sets up an ECP group context 854 * from a standardized set of domain parameters. 855 * 856 * \note The index should be a value of the NamedCurve enum, 857 * as defined in <em>RFC-4492: Elliptic Curve Cryptography 858 * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>, 859 * usually in the form of an \c MBEDTLS_ECP_DP_XXX macro. 860 * 861 * \param grp The group context to setup. This must be initialized. 862 * \param id The identifier of the domain parameter set to load. 863 * 864 * \return \c 0 on success. 865 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p id doesn't 866 * correspond to a known group. 867 * \return Another negative error code on other kinds of failure. 868 */ 869 int mbedtls_ecp_group_load(mbedtls_ecp_group *grp, mbedtls_ecp_group_id id); 870 871 /** 872 * \brief This function sets up an ECP group context from a TLS 873 * ECParameters record as defined in RFC 4492, Section 5.4. 874 * 875 * \note The read pointer \p buf is updated to point right after 876 * the ECParameters record on exit. 877 * 878 * \param grp The group context to setup. This must be initialized. 879 * \param buf The address of the pointer to the start of the input buffer. 880 * \param len The length of the input buffer \c *buf in Bytes. 881 * 882 * \return \c 0 on success. 883 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid. 884 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not 885 * recognized. 886 * \return Another negative error code on other kinds of failure. 887 */ 888 int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp, 889 const unsigned char **buf, size_t len); 890 891 /** 892 * \brief This function extracts an elliptic curve group ID from a 893 * TLS ECParameters record as defined in RFC 4492, Section 5.4. 894 * 895 * \note The read pointer \p buf is updated to point right after 896 * the ECParameters record on exit. 897 * 898 * \param grp The address at which to store the group id. 899 * This must not be \c NULL. 900 * \param buf The address of the pointer to the start of the input buffer. 901 * \param len The length of the input buffer \c *buf in Bytes. 902 * 903 * \return \c 0 on success. 904 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid. 905 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not 906 * recognized. 907 * \return Another negative error code on other kinds of failure. 908 */ 909 int mbedtls_ecp_tls_read_group_id(mbedtls_ecp_group_id *grp, 910 const unsigned char **buf, 911 size_t len); 912 /** 913 * \brief This function exports an elliptic curve as a TLS 914 * ECParameters record as defined in RFC 4492, Section 5.4. 915 * 916 * \param grp The ECP group to be exported. 917 * This must be initialized and have group parameters 918 * set, for example through mbedtls_ecp_group_load(). 919 * \param olen The address at which to store the number of Bytes written. 920 * This must not be \c NULL. 921 * \param buf The buffer to write to. This must be a writable buffer 922 * of length \p blen Bytes. 923 * \param blen The length of the output buffer \p buf in Bytes. 924 * 925 * \return \c 0 on success. 926 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output 927 * buffer is too small to hold the exported group. 928 * \return Another negative error code on other kinds of failure. 929 */ 930 int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp, 931 size_t *olen, 932 unsigned char *buf, size_t blen); 933 934 /** 935 * \brief This function performs a scalar multiplication of a point 936 * by an integer: \p R = \p m * \p P. 937 * 938 * It is not thread-safe to use same group in multiple threads. 939 * 940 * \note To prevent timing attacks, this function 941 * executes the exact same sequence of base-field 942 * operations for any valid \p m. It avoids any if-branch or 943 * array index depending on the value of \p m. It also uses 944 * \p f_rng to randomize some intermediate results. 945 * 946 * \param grp The ECP group to use. 947 * This must be initialized and have group parameters 948 * set, for example through mbedtls_ecp_group_load(). 949 * \param R The point in which to store the result of the calculation. 950 * This must be initialized. 951 * \param m The integer by which to multiply. This must be initialized. 952 * \param P The point to multiply. This must be initialized. 953 * \param f_rng The RNG function. This must not be \c NULL. 954 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c 955 * NULL if \p f_rng doesn't need a context. 956 * 957 * \return \c 0 on success. 958 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private 959 * key, or \p P is not a valid public key. 960 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. 961 * \return Another negative error code on other kinds of failure. 962 */ 963 int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R, 964 const mbedtls_mpi *m, const mbedtls_ecp_point *P, 965 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); 966 967 /** 968 * \brief This function performs multiplication of a point by 969 * an integer: \p R = \p m * \p P in a restartable way. 970 * 971 * \see mbedtls_ecp_mul() 972 * 973 * \note This function does the same as \c mbedtls_ecp_mul(), but 974 * it can return early and restart according to the limit set 975 * with \c mbedtls_ecp_set_max_ops() to reduce blocking. 976 * 977 * \param grp The ECP group to use. 978 * This must be initialized and have group parameters 979 * set, for example through mbedtls_ecp_group_load(). 980 * \param R The point in which to store the result of the calculation. 981 * This must be initialized. 982 * \param m The integer by which to multiply. This must be initialized. 983 * \param P The point to multiply. This must be initialized. 984 * \param f_rng The RNG function. This must not be \c NULL. 985 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c 986 * NULL if \p f_rng doesn't need a context. 987 * \param rs_ctx The restart context (NULL disables restart). 988 * 989 * \return \c 0 on success. 990 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private 991 * key, or \p P is not a valid public key. 992 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. 993 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of 994 * operations was reached: see \c mbedtls_ecp_set_max_ops(). 995 * \return Another negative error code on other kinds of failure. 996 */ 997 int mbedtls_ecp_mul_restartable(mbedtls_ecp_group *grp, mbedtls_ecp_point *R, 998 const mbedtls_mpi *m, const mbedtls_ecp_point *P, 999 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, 1000 mbedtls_ecp_restart_ctx *rs_ctx); 1001 1002 #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) 1003 /** 1004 * \brief This function performs multiplication and addition of two 1005 * points by integers: \p R = \p m * \p P + \p n * \p Q 1006 * 1007 * It is not thread-safe to use same group in multiple threads. 1008 * 1009 * \note In contrast to mbedtls_ecp_mul(), this function does not 1010 * guarantee a constant execution flow and timing. 1011 * 1012 * \note This function is only defined for short Weierstrass curves. 1013 * It may not be included in builds without any short 1014 * Weierstrass curve. 1015 * 1016 * \param grp The ECP group to use. 1017 * This must be initialized and have group parameters 1018 * set, for example through mbedtls_ecp_group_load(). 1019 * \param R The point in which to store the result of the calculation. 1020 * This must be initialized. 1021 * \param m The integer by which to multiply \p P. 1022 * This must be initialized. 1023 * \param P The point to multiply by \p m. This must be initialized. 1024 * \param n The integer by which to multiply \p Q. 1025 * This must be initialized. 1026 * \param Q The point to be multiplied by \p n. 1027 * This must be initialized. 1028 * 1029 * \return \c 0 on success. 1030 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not 1031 * valid private keys, or \p P or \p Q are not valid public 1032 * keys. 1033 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. 1034 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p grp does not 1035 * designate a short Weierstrass curve. 1036 * \return Another negative error code on other kinds of failure. 1037 */ 1038 int mbedtls_ecp_muladd(mbedtls_ecp_group *grp, mbedtls_ecp_point *R, 1039 const mbedtls_mpi *m, const mbedtls_ecp_point *P, 1040 const mbedtls_mpi *n, const mbedtls_ecp_point *Q); 1041 1042 /** 1043 * \brief This function performs multiplication and addition of two 1044 * points by integers: \p R = \p m * \p P + \p n * \p Q in a 1045 * restartable way. 1046 * 1047 * \see \c mbedtls_ecp_muladd() 1048 * 1049 * \note This function works the same as \c mbedtls_ecp_muladd(), 1050 * but it can return early and restart according to the limit 1051 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking. 1052 * 1053 * \note This function is only defined for short Weierstrass curves. 1054 * It may not be included in builds without any short 1055 * Weierstrass curve. 1056 * 1057 * \param grp The ECP group to use. 1058 * This must be initialized and have group parameters 1059 * set, for example through mbedtls_ecp_group_load(). 1060 * \param R The point in which to store the result of the calculation. 1061 * This must be initialized. 1062 * \param m The integer by which to multiply \p P. 1063 * This must be initialized. 1064 * \param P The point to multiply by \p m. This must be initialized. 1065 * \param n The integer by which to multiply \p Q. 1066 * This must be initialized. 1067 * \param Q The point to be multiplied by \p n. 1068 * This must be initialized. 1069 * \param rs_ctx The restart context (NULL disables restart). 1070 * 1071 * \return \c 0 on success. 1072 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not 1073 * valid private keys, or \p P or \p Q are not valid public 1074 * keys. 1075 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. 1076 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p grp does not 1077 * designate a short Weierstrass curve. 1078 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of 1079 * operations was reached: see \c mbedtls_ecp_set_max_ops(). 1080 * \return Another negative error code on other kinds of failure. 1081 */ 1082 int mbedtls_ecp_muladd_restartable( 1083 mbedtls_ecp_group *grp, mbedtls_ecp_point *R, 1084 const mbedtls_mpi *m, const mbedtls_ecp_point *P, 1085 const mbedtls_mpi *n, const mbedtls_ecp_point *Q, 1086 mbedtls_ecp_restart_ctx *rs_ctx); 1087 #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */ 1088 1089 /** 1090 * \brief This function checks that a point is a valid public key 1091 * on this curve. 1092 * 1093 * It only checks that the point is non-zero, has 1094 * valid coordinates and lies on the curve. It does not verify 1095 * that it is indeed a multiple of \p G. This additional 1096 * check is computationally more expensive, is not required 1097 * by standards, and should not be necessary if the group 1098 * used has a small cofactor. In particular, it is useless for 1099 * the NIST groups which all have a cofactor of 1. 1100 * 1101 * \note This function uses bare components rather than an 1102 * ::mbedtls_ecp_keypair structure, to ease use with other 1103 * structures, such as ::mbedtls_ecdh_context or 1104 * ::mbedtls_ecdsa_context. 1105 * 1106 * \param grp The ECP group the point should belong to. 1107 * This must be initialized and have group parameters 1108 * set, for example through mbedtls_ecp_group_load(). 1109 * \param pt The point to check. This must be initialized. 1110 * 1111 * \return \c 0 if the point is a valid public key. 1112 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not 1113 * a valid public key for the given curve. 1114 * \return Another negative error code on other kinds of failure. 1115 */ 1116 int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp, 1117 const mbedtls_ecp_point *pt); 1118 1119 /** 1120 * \brief This function checks that an \p mbedtls_mpi is a 1121 * valid private key for this curve. 1122 * 1123 * \note This function uses bare components rather than an 1124 * ::mbedtls_ecp_keypair structure to ease use with other 1125 * structures, such as ::mbedtls_ecdh_context or 1126 * ::mbedtls_ecdsa_context. 1127 * 1128 * \param grp The ECP group the private key should belong to. 1129 * This must be initialized and have group parameters 1130 * set, for example through mbedtls_ecp_group_load(). 1131 * \param d The integer to check. This must be initialized. 1132 * 1133 * \return \c 0 if the point is a valid private key. 1134 * \return #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not a valid 1135 * private key for the given curve. 1136 * \return Another negative error code on other kinds of failure. 1137 */ 1138 int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp, 1139 const mbedtls_mpi *d); 1140 1141 /** 1142 * \brief This function generates a private key. 1143 * 1144 * \param grp The ECP group to generate a private key for. 1145 * This must be initialized and have group parameters 1146 * set, for example through mbedtls_ecp_group_load(). 1147 * \param d The destination MPI (secret part). This must be initialized. 1148 * \param f_rng The RNG function. This must not be \c NULL. 1149 * \param p_rng The RNG parameter to be passed to \p f_rng. This may be 1150 * \c NULL if \p f_rng doesn't need a context argument. 1151 * 1152 * \return \c 0 on success. 1153 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code 1154 * on failure. 1155 */ 1156 int mbedtls_ecp_gen_privkey(const mbedtls_ecp_group *grp, 1157 mbedtls_mpi *d, 1158 int (*f_rng)(void *, unsigned char *, size_t), 1159 void *p_rng); 1160 1161 /** 1162 * \brief This function generates a keypair with a configurable base 1163 * point. 1164 * 1165 * \note This function uses bare components rather than an 1166 * ::mbedtls_ecp_keypair structure to ease use with other 1167 * structures, such as ::mbedtls_ecdh_context or 1168 * ::mbedtls_ecdsa_context. 1169 * 1170 * \param grp The ECP group to generate a key pair for. 1171 * This must be initialized and have group parameters 1172 * set, for example through mbedtls_ecp_group_load(). 1173 * \param G The base point to use. This must be initialized 1174 * and belong to \p grp. It replaces the default base 1175 * point \c grp->G used by mbedtls_ecp_gen_keypair(). 1176 * \param d The destination MPI (secret part). 1177 * This must be initialized. 1178 * \param Q The destination point (public part). 1179 * This must be initialized. 1180 * \param f_rng The RNG function. This must not be \c NULL. 1181 * \param p_rng The RNG context to be passed to \p f_rng. This may 1182 * be \c NULL if \p f_rng doesn't need a context argument. 1183 * 1184 * \return \c 0 on success. 1185 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code 1186 * on failure. 1187 */ 1188 int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp, 1189 const mbedtls_ecp_point *G, 1190 mbedtls_mpi *d, mbedtls_ecp_point *Q, 1191 int (*f_rng)(void *, unsigned char *, size_t), 1192 void *p_rng); 1193 1194 /** 1195 * \brief This function generates an ECP keypair. 1196 * 1197 * \note This function uses bare components rather than an 1198 * ::mbedtls_ecp_keypair structure to ease use with other 1199 * structures, such as ::mbedtls_ecdh_context or 1200 * ::mbedtls_ecdsa_context. 1201 * 1202 * \param grp The ECP group to generate a key pair for. 1203 * This must be initialized and have group parameters 1204 * set, for example through mbedtls_ecp_group_load(). 1205 * \param d The destination MPI (secret part). 1206 * This must be initialized. 1207 * \param Q The destination point (public part). 1208 * This must be initialized. 1209 * \param f_rng The RNG function. This must not be \c NULL. 1210 * \param p_rng The RNG context to be passed to \p f_rng. This may 1211 * be \c NULL if \p f_rng doesn't need a context argument. 1212 * 1213 * \return \c 0 on success. 1214 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code 1215 * on failure. 1216 */ 1217 int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp, mbedtls_mpi *d, 1218 mbedtls_ecp_point *Q, 1219 int (*f_rng)(void *, unsigned char *, size_t), 1220 void *p_rng); 1221 1222 /** 1223 * \brief This function generates an ECP key. 1224 * 1225 * \param grp_id The ECP group identifier. 1226 * \param key The destination key. This must be initialized. 1227 * \param f_rng The RNG function to use. This must not be \c NULL. 1228 * \param p_rng The RNG context to be passed to \p f_rng. This may 1229 * be \c NULL if \p f_rng doesn't need a context argument. 1230 * 1231 * \return \c 0 on success. 1232 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code 1233 * on failure. 1234 */ 1235 int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, 1236 int (*f_rng)(void *, unsigned char *, size_t), 1237 void *p_rng); 1238 1239 /** 1240 * \brief This function reads an elliptic curve private key. 1241 * 1242 * \param grp_id The ECP group identifier. 1243 * \param key The destination key. 1244 * \param buf The buffer containing the binary representation of the 1245 * key. (Big endian integer for Weierstrass curves, byte 1246 * string for Montgomery curves.) 1247 * \param buflen The length of the buffer in bytes. 1248 * 1249 * \return \c 0 on success. 1250 * \return #MBEDTLS_ERR_ECP_INVALID_KEY error if the key is 1251 * invalid. 1252 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed. 1253 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the operation for 1254 * the group is not implemented. 1255 * \return Another negative error code on different kinds of failure. 1256 */ 1257 int mbedtls_ecp_read_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, 1258 const unsigned char *buf, size_t buflen); 1259 1260 /** 1261 * \brief This function exports an elliptic curve private key. 1262 * 1263 * \param key The private key. 1264 * \param buf The output buffer for containing the binary representation 1265 * of the key. (Big endian integer for Weierstrass curves, byte 1266 * string for Montgomery curves.) 1267 * \param buflen The total length of the buffer in bytes. 1268 * 1269 * \return \c 0 on success. 1270 * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the \p key 1271 representation is larger than the available space in \p buf. 1272 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the operation for 1273 * the group is not implemented. 1274 * \return Another negative error code on different kinds of failure. 1275 */ 1276 int mbedtls_ecp_write_key(mbedtls_ecp_keypair *key, 1277 unsigned char *buf, size_t buflen); 1278 1279 /** 1280 * \brief This function checks that the keypair objects 1281 * \p pub and \p prv have the same group and the 1282 * same public point, and that the private key in 1283 * \p prv is consistent with the public key. 1284 * 1285 * \param pub The keypair structure holding the public key. This 1286 * must be initialized. If it contains a private key, that 1287 * part is ignored. 1288 * \param prv The keypair structure holding the full keypair. 1289 * This must be initialized. 1290 * \param f_rng The RNG function. This must not be \c NULL. 1291 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c 1292 * NULL if \p f_rng doesn't need a context. 1293 * 1294 * \return \c 0 on success, meaning that the keys are valid and match. 1295 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the keys are invalid or do not match. 1296 * \return An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX 1297 * error code on calculation failure. 1298 */ 1299 int mbedtls_ecp_check_pub_priv( 1300 const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv, 1301 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); 1302 1303 /** 1304 * \brief This function exports generic key-pair parameters. 1305 * 1306 * \param key The key pair to export from. 1307 * \param grp Slot for exported ECP group. 1308 * It must point to an initialized ECP group. 1309 * \param d Slot for the exported secret value. 1310 * It must point to an initialized mpi. 1311 * \param Q Slot for the exported public value. 1312 * It must point to an initialized ECP point. 1313 * 1314 * \return \c 0 on success, 1315 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. 1316 * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if key id doesn't 1317 * correspond to a known group. 1318 * \return Another negative error code on other kinds of failure. 1319 */ 1320 int mbedtls_ecp_export(const mbedtls_ecp_keypair *key, mbedtls_ecp_group *grp, 1321 mbedtls_mpi *d, mbedtls_ecp_point *Q); 1322 1323 #if defined(MBEDTLS_SELF_TEST) 1324 1325 /** 1326 * \brief The ECP checkup routine. 1327 * 1328 * \return \c 0 on success. 1329 * \return \c 1 on failure. 1330 */ 1331 int mbedtls_ecp_self_test(int verbose); 1332 1333 #endif /* MBEDTLS_SELF_TEST */ 1334 1335 #ifdef __cplusplus 1336 } 1337 #endif 1338 1339 #endif /* ecp.h */ 1340