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