1 /* 2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved. 3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. 4 * 5 * Licensed under the OpenSSL license (the "License"). You may not use 6 * this file except in compliance with the License. You can obtain a copy 7 * in the file LICENSE in the source distribution or at 8 * https://www.openssl.org/source/license.html 9 */ 10 11 #ifndef OPENSSL_HEADER_EC_H 12 #define OPENSSL_HEADER_EC_H 13 14 #include <openssl/base.h> 15 16 #if defined(__cplusplus) 17 extern "C" { 18 #endif 19 20 21 // Low-level operations on elliptic curves. 22 23 24 // point_conversion_form_t enumerates forms, as defined in X9.62 (ECDSA), for 25 // the encoding of a elliptic curve point (x,y) 26 typedef enum { 27 // POINT_CONVERSION_COMPRESSED indicates that the point is encoded as z||x, 28 // where the octet z specifies which solution of the quadratic equation y 29 // is. 30 POINT_CONVERSION_COMPRESSED = 2, 31 32 // POINT_CONVERSION_UNCOMPRESSED indicates that the point is encoded as 33 // z||x||y, where z is the octet 0x04. 34 POINT_CONVERSION_UNCOMPRESSED = 4, 35 36 // POINT_CONVERSION_HYBRID indicates that the point is encoded as z||x||y, 37 // where z specifies which solution of the quadratic equation y is. This is 38 // not supported by the code and has never been observed in use. 39 // 40 // TODO(agl): remove once node.js no longer references this. 41 POINT_CONVERSION_HYBRID = 6, 42 } point_conversion_form_t; 43 44 45 // Elliptic curve groups. 46 // 47 // Elliptic curve groups are represented by |EC_GROUP| objects. Unlike OpenSSL, 48 // if limited to the APIs in this section, callers may treat |EC_GROUP|s as 49 // static, immutable objects which do not need to be copied or released. In 50 // BoringSSL, only custom |EC_GROUP|s created by |EC_GROUP_new_curve_GFp| 51 // (deprecated) are dynamic. 52 // 53 // Callers may cast away |const| and use |EC_GROUP_dup| and |EC_GROUP_free| with 54 // static groups, for compatibility with OpenSSL or dynamic groups, but it is 55 // otherwise unnecessary. 56 57 // EC_group_p224 returns an |EC_GROUP| for P-224, also known as secp224r1. 58 OPENSSL_EXPORT const EC_GROUP *EC_group_p224(void); 59 60 // EC_group_p256 returns an |EC_GROUP| for P-256, also known as secp256r1 or 61 // prime256v1. 62 OPENSSL_EXPORT const EC_GROUP *EC_group_p256(void); 63 64 // EC_group_p384 returns an |EC_GROUP| for P-384, also known as secp384r1. 65 OPENSSL_EXPORT const EC_GROUP *EC_group_p384(void); 66 67 // EC_group_p521 returns an |EC_GROUP| for P-521, also known as secp521r1. 68 OPENSSL_EXPORT const EC_GROUP *EC_group_p521(void); 69 70 // EC_GROUP_new_by_curve_name returns the |EC_GROUP| object for the elliptic 71 // curve specified by |nid|, or NULL on unsupported NID. For OpenSSL 72 // compatibility, this function returns a non-const pointer which may be passed 73 // to |EC_GROUP_free|. However, the resulting object is actually static and 74 // calling |EC_GROUP_free| is optional. 75 // 76 // The supported NIDs are: 77 // - |NID_secp224r1| (P-224) 78 // - |NID_X9_62_prime256v1| (P-256) 79 // - |NID_secp384r1| (P-384) 80 // - |NID_secp521r1| (P-521) 81 // 82 // Calling this function causes all four curves to be linked into the binary. 83 // Prefer calling |EC_group_*| to allow the static linker to drop unused curves. 84 // 85 // If in doubt, use |NID_X9_62_prime256v1|, or see the curve25519.h header for 86 // more modern primitives. 87 OPENSSL_EXPORT EC_GROUP *EC_GROUP_new_by_curve_name(int nid); 88 89 // EC_GROUP_cmp returns zero if |a| and |b| are the same group and non-zero 90 // otherwise. 91 OPENSSL_EXPORT int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, 92 BN_CTX *ignored); 93 94 // EC_GROUP_get0_generator returns a pointer to the internal |EC_POINT| object 95 // in |group| that specifies the generator for the group. 96 OPENSSL_EXPORT const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group); 97 98 // EC_GROUP_get0_order returns a pointer to the internal |BIGNUM| object in 99 // |group| that specifies the order of the group. 100 OPENSSL_EXPORT const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group); 101 102 // EC_GROUP_order_bits returns the number of bits of the order of |group|. 103 OPENSSL_EXPORT int EC_GROUP_order_bits(const EC_GROUP *group); 104 105 // EC_GROUP_get_cofactor sets |*cofactor| to the cofactor of |group| using 106 // |ctx|, if it's not NULL. It returns one on success and zero otherwise. 107 OPENSSL_EXPORT int EC_GROUP_get_cofactor(const EC_GROUP *group, 108 BIGNUM *cofactor, BN_CTX *ctx); 109 110 // EC_GROUP_get_curve_GFp gets various parameters about a group. It sets 111 // |*out_p| to the order of the coordinate field and |*out_a| and |*out_b| to 112 // the parameters of the curve when expressed as y² = x³ + ax + b. Any of the 113 // output parameters can be NULL. It returns one on success and zero on 114 // error. 115 OPENSSL_EXPORT int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *out_p, 116 BIGNUM *out_a, BIGNUM *out_b, 117 BN_CTX *ctx); 118 119 // EC_GROUP_get_curve_name returns a NID that identifies |group|. 120 OPENSSL_EXPORT int EC_GROUP_get_curve_name(const EC_GROUP *group); 121 122 // EC_GROUP_get_degree returns the number of bits needed to represent an 123 // element of the field underlying |group|. 124 OPENSSL_EXPORT unsigned EC_GROUP_get_degree(const EC_GROUP *group); 125 126 // EC_curve_nid2nist returns the NIST name of the elliptic curve specified by 127 // |nid|, or NULL if |nid| is not a NIST curve. For example, it returns "P-256" 128 // for |NID_X9_62_prime256v1|. 129 OPENSSL_EXPORT const char *EC_curve_nid2nist(int nid); 130 131 // EC_curve_nist2nid returns the NID of the elliptic curve specified by the NIST 132 // name |name|, or |NID_undef| if |name| is not a recognized name. For example, 133 // it returns |NID_X9_62_prime256v1| for "P-256". 134 OPENSSL_EXPORT int EC_curve_nist2nid(const char *name); 135 136 137 // Points on elliptic curves. 138 139 // EC_POINT_new returns a fresh |EC_POINT| object in the given group, or NULL 140 // on error. 141 OPENSSL_EXPORT EC_POINT *EC_POINT_new(const EC_GROUP *group); 142 143 // EC_POINT_free frees |point| and the data that it points to. 144 OPENSSL_EXPORT void EC_POINT_free(EC_POINT *point); 145 146 // EC_POINT_copy sets |*dest| equal to |*src|. It returns one on success and 147 // zero otherwise. 148 OPENSSL_EXPORT int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src); 149 150 // EC_POINT_dup returns a fresh |EC_POINT| that contains the same values as 151 // |src|, or NULL on error. 152 OPENSSL_EXPORT EC_POINT *EC_POINT_dup(const EC_POINT *src, 153 const EC_GROUP *group); 154 155 // EC_POINT_set_to_infinity sets |point| to be the "point at infinity" for the 156 // given group. 157 OPENSSL_EXPORT int EC_POINT_set_to_infinity(const EC_GROUP *group, 158 EC_POINT *point); 159 160 // EC_POINT_is_at_infinity returns one iff |point| is the point at infinity and 161 // zero otherwise. 162 OPENSSL_EXPORT int EC_POINT_is_at_infinity(const EC_GROUP *group, 163 const EC_POINT *point); 164 165 // EC_POINT_is_on_curve returns one if |point| is an element of |group| and 166 // and zero otherwise or when an error occurs. This is different from OpenSSL, 167 // which returns -1 on error. If |ctx| is non-NULL, it may be used. 168 OPENSSL_EXPORT int EC_POINT_is_on_curve(const EC_GROUP *group, 169 const EC_POINT *point, BN_CTX *ctx); 170 171 // EC_POINT_cmp returns zero if |a| is equal to |b|, greater than zero if 172 // not equal and -1 on error. If |ctx| is not NULL, it may be used. 173 OPENSSL_EXPORT int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, 174 const EC_POINT *b, BN_CTX *ctx); 175 176 177 // Point conversion. 178 179 // EC_POINT_get_affine_coordinates_GFp sets |x| and |y| to the affine value of 180 // |point| using |ctx|, if it's not NULL. It returns one on success and zero 181 // otherwise. 182 // 183 // Either |x| or |y| may be NULL to skip computing that coordinate. This is 184 // slightly faster in the common case where only the x-coordinate is needed. 185 OPENSSL_EXPORT int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, 186 const EC_POINT *point, 187 BIGNUM *x, BIGNUM *y, 188 BN_CTX *ctx); 189 190 // EC_POINT_get_affine_coordinates is an alias of 191 // |EC_POINT_get_affine_coordinates_GFp|. 192 OPENSSL_EXPORT int EC_POINT_get_affine_coordinates(const EC_GROUP *group, 193 const EC_POINT *point, 194 BIGNUM *x, BIGNUM *y, 195 BN_CTX *ctx); 196 197 // EC_POINT_set_affine_coordinates_GFp sets the value of |point| to be 198 // (|x|, |y|). The |ctx| argument may be used if not NULL. It returns one 199 // on success or zero on error. It's considered an error if the point is not on 200 // the curve. 201 // 202 // Note that the corresponding function in OpenSSL versions prior to 1.0.2s does 203 // not check if the point is on the curve. This is a security-critical check, so 204 // code additionally supporting OpenSSL should repeat the check with 205 // |EC_POINT_is_on_curve| or check for older OpenSSL versions with 206 // |OPENSSL_VERSION_NUMBER|. 207 OPENSSL_EXPORT int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, 208 EC_POINT *point, 209 const BIGNUM *x, 210 const BIGNUM *y, 211 BN_CTX *ctx); 212 213 // EC_POINT_set_affine_coordinates is an alias of 214 // |EC_POINT_set_affine_coordinates_GFp|. 215 OPENSSL_EXPORT int EC_POINT_set_affine_coordinates(const EC_GROUP *group, 216 EC_POINT *point, 217 const BIGNUM *x, 218 const BIGNUM *y, 219 BN_CTX *ctx); 220 221 // EC_POINT_point2oct serialises |point| into the X9.62 form given by |form| 222 // into, at most, |max_out| bytes at |buf|. It returns the number of bytes 223 // written or zero on error if |buf| is non-NULL, else the number of bytes 224 // needed. The |ctx| argument may be used if not NULL. 225 OPENSSL_EXPORT size_t EC_POINT_point2oct(const EC_GROUP *group, 226 const EC_POINT *point, 227 point_conversion_form_t form, 228 uint8_t *buf, size_t max_out, 229 BN_CTX *ctx); 230 231 // EC_POINT_point2buf serialises |point| into the X9.62 form given by |form| to 232 // a newly-allocated buffer and sets |*out_buf| to point to it. It returns the 233 // length of the result on success or zero on error. The caller must release 234 // |*out_buf| with |OPENSSL_free| when done. 235 OPENSSL_EXPORT size_t EC_POINT_point2buf(const EC_GROUP *group, 236 const EC_POINT *point, 237 point_conversion_form_t form, 238 uint8_t **out_buf, BN_CTX *ctx); 239 240 // EC_POINT_point2cbb behaves like |EC_POINT_point2oct| but appends the 241 // serialised point to |cbb|. It returns one on success and zero on error. 242 OPENSSL_EXPORT int EC_POINT_point2cbb(CBB *out, const EC_GROUP *group, 243 const EC_POINT *point, 244 point_conversion_form_t form, 245 BN_CTX *ctx); 246 247 // EC_POINT_oct2point sets |point| from |len| bytes of X9.62 format 248 // serialisation in |buf|. It returns one on success and zero on error. The 249 // |ctx| argument may be used if not NULL. It's considered an error if |buf| 250 // does not represent a point on the curve. 251 OPENSSL_EXPORT int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point, 252 const uint8_t *buf, size_t len, 253 BN_CTX *ctx); 254 255 // EC_POINT_set_compressed_coordinates_GFp sets |point| to equal the point with 256 // the given |x| coordinate and the y coordinate specified by |y_bit| (see 257 // X9.62). It returns one on success and zero otherwise. 258 OPENSSL_EXPORT int EC_POINT_set_compressed_coordinates_GFp( 259 const EC_GROUP *group, EC_POINT *point, const BIGNUM *x, int y_bit, 260 BN_CTX *ctx); 261 262 263 // Group operations. 264 265 // EC_POINT_add sets |r| equal to |a| plus |b|. It returns one on success and 266 // zero otherwise. If |ctx| is not NULL, it may be used. 267 OPENSSL_EXPORT int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, 268 const EC_POINT *a, const EC_POINT *b, 269 BN_CTX *ctx); 270 271 // EC_POINT_dbl sets |r| equal to |a| plus |a|. It returns one on success and 272 // zero otherwise. If |ctx| is not NULL, it may be used. 273 OPENSSL_EXPORT int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, 274 const EC_POINT *a, BN_CTX *ctx); 275 276 // EC_POINT_invert sets |a| equal to minus |a|. It returns one on success and 277 // zero otherwise. If |ctx| is not NULL, it may be used. 278 OPENSSL_EXPORT int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, 279 BN_CTX *ctx); 280 281 // EC_POINT_mul sets r = generator*n + q*m. It returns one on success and zero 282 // otherwise. If |ctx| is not NULL, it may be used. 283 OPENSSL_EXPORT int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, 284 const BIGNUM *n, const EC_POINT *q, 285 const BIGNUM *m, BN_CTX *ctx); 286 287 288 // Hash-to-curve. 289 // 290 // The following functions implement primitives from RFC 9380. The |dst| 291 // parameter in each function is the domain separation tag and must be unique 292 // for each protocol and between the |hash_to_curve| and |hash_to_scalar| 293 // variants. See section 3.1 of the spec for additional guidance on this 294 // parameter. 295 296 // EC_hash_to_curve_p256_xmd_sha256_sswu hashes |msg| to a point on |group| and 297 // writes the result to |out|, implementing the P256_XMD:SHA-256_SSWU_RO_ suite 298 // from RFC 9380. It returns one on success and zero on error. 299 OPENSSL_EXPORT int EC_hash_to_curve_p256_xmd_sha256_sswu( 300 const EC_GROUP *group, EC_POINT *out, const uint8_t *dst, size_t dst_len, 301 const uint8_t *msg, size_t msg_len); 302 303 // EC_hash_to_curve_p384_xmd_sha384_sswu hashes |msg| to a point on |group| and 304 // writes the result to |out|, implementing the P384_XMD:SHA-384_SSWU_RO_ suite 305 // from RFC 9380. It returns one on success and zero on error. 306 OPENSSL_EXPORT int EC_hash_to_curve_p384_xmd_sha384_sswu( 307 const EC_GROUP *group, EC_POINT *out, const uint8_t *dst, size_t dst_len, 308 const uint8_t *msg, size_t msg_len); 309 310 311 // Deprecated functions. 312 313 // EC_GROUP_free releases a reference to |group|, if |group| was created by 314 // |EC_GROUP_new_curve_GFp|. If |group| is static, it does nothing. 315 // 316 // This function exists for OpenSSL compatibilty, and to manage dynamic 317 // |EC_GROUP|s constructed by |EC_GROUP_new_curve_GFp|. Callers that do not need 318 // either may ignore this function. 319 OPENSSL_EXPORT void EC_GROUP_free(EC_GROUP *group); 320 321 // EC_GROUP_dup increments |group|'s reference count and returns it, if |group| 322 // was created by |EC_GROUP_new_curve_GFp|. If |group| is static, it simply 323 // returns |group|. 324 // 325 // This function exists for OpenSSL compatibilty, and to manage dynamic 326 // |EC_GROUP|s constructed by |EC_GROUP_new_curve_GFp|. Callers that do not need 327 // either may ignore this function. 328 OPENSSL_EXPORT EC_GROUP *EC_GROUP_dup(const EC_GROUP *group); 329 330 // EC_GROUP_new_curve_GFp creates a new, arbitrary elliptic curve group based 331 // on the equation y² = x³ + a·x + b. It returns the new group or NULL on 332 // error. The lifetime of the resulting object must be managed with 333 // |EC_GROUP_dup| and |EC_GROUP_free|. 334 // 335 // This new group has no generator. It is an error to use a generator-less group 336 // with any functions except for |EC_GROUP_free|, |EC_POINT_new|, 337 // |EC_POINT_set_affine_coordinates_GFp|, and |EC_GROUP_set_generator|. 338 // 339 // |EC_GROUP|s returned by this function will always compare as unequal via 340 // |EC_GROUP_cmp| (even to themselves). |EC_GROUP_get_curve_name| will always 341 // return |NID_undef|. 342 // 343 // This function is provided for compatibility with some legacy applications 344 // only. Avoid using arbitrary curves and use |EC_GROUP_new_by_curve_name| 345 // instead. This ensures the result meets preconditions necessary for 346 // elliptic curve algorithms to function correctly and securely. 347 // 348 // Given invalid parameters, this function may fail or it may return an 349 // |EC_GROUP| which breaks these preconditions. Subsequent operations may then 350 // return arbitrary, incorrect values. Callers should not pass 351 // attacker-controlled values to this function. 352 OPENSSL_EXPORT EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, 353 const BIGNUM *a, 354 const BIGNUM *b, BN_CTX *ctx); 355 356 // EC_GROUP_set_generator sets the generator for |group| to |generator|, which 357 // must have the given order and cofactor. It may only be used with |EC_GROUP| 358 // objects returned by |EC_GROUP_new_curve_GFp| and may only be used once on 359 // each group. |generator| must have been created using |group|. 360 OPENSSL_EXPORT int EC_GROUP_set_generator(EC_GROUP *group, 361 const EC_POINT *generator, 362 const BIGNUM *order, 363 const BIGNUM *cofactor); 364 365 // EC_GROUP_get_order sets |*order| to the order of |group|, if it's not 366 // NULL. It returns one on success and zero otherwise. |ctx| is ignored. Use 367 // |EC_GROUP_get0_order| instead. 368 OPENSSL_EXPORT int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, 369 BN_CTX *ctx); 370 371 #define OPENSSL_EC_EXPLICIT_CURVE 0 372 #define OPENSSL_EC_NAMED_CURVE 1 373 374 // EC_GROUP_set_asn1_flag does nothing. 375 OPENSSL_EXPORT void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag); 376 377 // EC_GROUP_get_asn1_flag returns |OPENSSL_EC_NAMED_CURVE|. 378 OPENSSL_EXPORT int EC_GROUP_get_asn1_flag(const EC_GROUP *group); 379 380 typedef struct ec_method_st EC_METHOD; 381 382 // EC_GROUP_method_of returns a dummy non-NULL pointer. 383 OPENSSL_EXPORT const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group); 384 385 // EC_METHOD_get_field_type returns NID_X9_62_prime_field. 386 OPENSSL_EXPORT int EC_METHOD_get_field_type(const EC_METHOD *meth); 387 388 // EC_GROUP_set_point_conversion_form aborts the process if |form| is not 389 // |POINT_CONVERSION_UNCOMPRESSED| and otherwise does nothing. 390 OPENSSL_EXPORT void EC_GROUP_set_point_conversion_form( 391 EC_GROUP *group, point_conversion_form_t form); 392 393 // EC_builtin_curve describes a supported elliptic curve. 394 typedef struct { 395 int nid; 396 const char *comment; 397 } EC_builtin_curve; 398 399 // EC_get_builtin_curves writes at most |max_num_curves| elements to 400 // |out_curves| and returns the total number that it would have written, had 401 // |max_num_curves| been large enough. 402 // 403 // The |EC_builtin_curve| items describe the supported elliptic curves. 404 OPENSSL_EXPORT size_t EC_get_builtin_curves(EC_builtin_curve *out_curves, 405 size_t max_num_curves); 406 407 // EC_POINT_clear_free calls |EC_POINT_free|. 408 OPENSSL_EXPORT void EC_POINT_clear_free(EC_POINT *point); 409 410 411 #if defined(__cplusplus) 412 } // extern C 413 #endif 414 415 // Old code expects to get EC_KEY from ec.h. 416 #include <openssl/ec_key.h> 417 418 #if defined(__cplusplus) 419 extern "C++" { 420 421 BSSL_NAMESPACE_BEGIN 422 423 BORINGSSL_MAKE_DELETER(EC_POINT, EC_POINT_free) 424 BORINGSSL_MAKE_DELETER(EC_GROUP, EC_GROUP_free) 425 426 BSSL_NAMESPACE_END 427 428 } // extern C++ 429 430 #endif 431 432 #define EC_R_BUFFER_TOO_SMALL 100 433 #define EC_R_COORDINATES_OUT_OF_RANGE 101 434 #define EC_R_D2I_ECPKPARAMETERS_FAILURE 102 435 #define EC_R_EC_GROUP_NEW_BY_NAME_FAILURE 103 436 #define EC_R_GROUP2PKPARAMETERS_FAILURE 104 437 #define EC_R_I2D_ECPKPARAMETERS_FAILURE 105 438 #define EC_R_INCOMPATIBLE_OBJECTS 106 439 #define EC_R_INVALID_COMPRESSED_POINT 107 440 #define EC_R_INVALID_COMPRESSION_BIT 108 441 #define EC_R_INVALID_ENCODING 109 442 #define EC_R_INVALID_FIELD 110 443 #define EC_R_INVALID_FORM 111 444 #define EC_R_INVALID_GROUP_ORDER 112 445 #define EC_R_INVALID_PRIVATE_KEY 113 446 #define EC_R_MISSING_PARAMETERS 114 447 #define EC_R_MISSING_PRIVATE_KEY 115 448 #define EC_R_NON_NAMED_CURVE 116 449 #define EC_R_NOT_INITIALIZED 117 450 #define EC_R_PKPARAMETERS2GROUP_FAILURE 118 451 #define EC_R_POINT_AT_INFINITY 119 452 #define EC_R_POINT_IS_NOT_ON_CURVE 120 453 #define EC_R_SLOT_FULL 121 454 #define EC_R_UNDEFINED_GENERATOR 122 455 #define EC_R_UNKNOWN_GROUP 123 456 #define EC_R_UNKNOWN_ORDER 124 457 #define EC_R_WRONG_ORDER 125 458 #define EC_R_BIGNUM_OUT_OF_RANGE 126 459 #define EC_R_WRONG_CURVE_PARAMETERS 127 460 #define EC_R_DECODE_ERROR 128 461 #define EC_R_ENCODE_ERROR 129 462 #define EC_R_GROUP_MISMATCH 130 463 #define EC_R_INVALID_COFACTOR 131 464 #define EC_R_PUBLIC_KEY_VALIDATION_FAILED 132 465 #define EC_R_INVALID_SCALAR 133 466 467 #endif // OPENSSL_HEADER_EC_H 468