1 /* 2 * Copyright 2002-2021 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 HEADER_EC_H 12 # define HEADER_EC_H 13 14 # include <openssl/opensslconf.h> 15 16 # ifndef OPENSSL_NO_EC 17 # include <openssl/asn1.h> 18 # include <openssl/symhacks.h> 19 # if OPENSSL_API_COMPAT < 0x10100000L 20 # include <openssl/bn.h> 21 # endif 22 # include <openssl/ecerr.h> 23 # ifdef __cplusplus 24 extern "C" { 25 # endif 26 27 # ifndef OPENSSL_ECC_MAX_FIELD_BITS 28 # define OPENSSL_ECC_MAX_FIELD_BITS 661 29 # endif 30 31 /** Enum for the point conversion form as defined in X9.62 (ECDSA) 32 * for the encoding of a elliptic curve point (x,y) */ 33 typedef enum { 34 /** the point is encoded as z||x, where the octet z specifies 35 * which solution of the quadratic equation y is */ 36 POINT_CONVERSION_COMPRESSED = 2, 37 /** the point is encoded as z||x||y, where z is the octet 0x04 */ 38 POINT_CONVERSION_UNCOMPRESSED = 4, 39 /** the point is encoded as z||x||y, where the octet z specifies 40 * which solution of the quadratic equation y is */ 41 POINT_CONVERSION_HYBRID = 6 42 } point_conversion_form_t; 43 44 typedef struct ec_method_st EC_METHOD; 45 typedef struct ec_group_st EC_GROUP; 46 typedef struct ec_point_st EC_POINT; 47 typedef struct ecpk_parameters_st ECPKPARAMETERS; 48 typedef struct ec_parameters_st ECPARAMETERS; 49 50 /********************************************************************/ 51 /* EC_METHODs for curves over GF(p) */ 52 /********************************************************************/ 53 54 /** Returns the basic GFp ec methods which provides the basis for the 55 * optimized methods. 56 * \return EC_METHOD object 57 */ 58 const EC_METHOD *EC_GFp_simple_method(void); 59 60 /** Returns GFp methods using montgomery multiplication. 61 * \return EC_METHOD object 62 */ 63 const EC_METHOD *EC_GFp_mont_method(void); 64 65 /** Returns GFp methods using optimized methods for NIST recommended curves 66 * \return EC_METHOD object 67 */ 68 const EC_METHOD *EC_GFp_nist_method(void); 69 70 # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 71 /** Returns 64-bit optimized methods for nistp224 72 * \return EC_METHOD object 73 */ 74 const EC_METHOD *EC_GFp_nistp224_method(void); 75 76 /** Returns 64-bit optimized methods for nistp256 77 * \return EC_METHOD object 78 */ 79 const EC_METHOD *EC_GFp_nistp256_method(void); 80 81 /** Returns 64-bit optimized methods for nistp521 82 * \return EC_METHOD object 83 */ 84 const EC_METHOD *EC_GFp_nistp521_method(void); 85 # endif 86 87 # ifndef OPENSSL_NO_EC2M 88 /********************************************************************/ 89 /* EC_METHOD for curves over GF(2^m) */ 90 /********************************************************************/ 91 92 /** Returns the basic GF2m ec method 93 * \return EC_METHOD object 94 */ 95 const EC_METHOD *EC_GF2m_simple_method(void); 96 97 # endif 98 99 /********************************************************************/ 100 /* EC_GROUP functions */ 101 /********************************************************************/ 102 103 /** Creates a new EC_GROUP object 104 * \param meth EC_METHOD to use 105 * \return newly created EC_GROUP object or NULL in case of an error. 106 */ 107 EC_GROUP *EC_GROUP_new(const EC_METHOD *meth); 108 109 /** Frees a EC_GROUP object 110 * \param group EC_GROUP object to be freed. 111 */ 112 void EC_GROUP_free(EC_GROUP *group); 113 114 /** Clears and frees a EC_GROUP object 115 * \param group EC_GROUP object to be cleared and freed. 116 */ 117 void EC_GROUP_clear_free(EC_GROUP *group); 118 119 /** Copies EC_GROUP objects. Note: both EC_GROUPs must use the same EC_METHOD. 120 * \param dst destination EC_GROUP object 121 * \param src source EC_GROUP object 122 * \return 1 on success and 0 if an error occurred. 123 */ 124 int EC_GROUP_copy(EC_GROUP *dst, const EC_GROUP *src); 125 126 /** Creates a new EC_GROUP object and copies the copies the content 127 * form src to the newly created EC_KEY object 128 * \param src source EC_GROUP object 129 * \return newly created EC_GROUP object or NULL in case of an error. 130 */ 131 EC_GROUP *EC_GROUP_dup(const EC_GROUP *src); 132 133 /** Returns the EC_METHOD of the EC_GROUP object. 134 * \param group EC_GROUP object 135 * \return EC_METHOD used in this EC_GROUP object. 136 */ 137 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group); 138 139 /** Returns the field type of the EC_METHOD. 140 * \param meth EC_METHOD object 141 * \return NID of the underlying field type OID. 142 */ 143 int EC_METHOD_get_field_type(const EC_METHOD *meth); 144 145 /** Sets the generator and its order/cofactor of a EC_GROUP object. 146 * \param group EC_GROUP object 147 * \param generator EC_POINT object with the generator. 148 * \param order the order of the group generated by the generator. 149 * \param cofactor the index of the sub-group generated by the generator 150 * in the group of all points on the elliptic curve. 151 * \return 1 on success and 0 if an error occurred 152 */ 153 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator, 154 const BIGNUM *order, const BIGNUM *cofactor); 155 156 /** Returns the generator of a EC_GROUP object. 157 * \param group EC_GROUP object 158 * \return the currently used generator (possibly NULL). 159 */ 160 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group); 161 162 /** Returns the montgomery data for order(Generator) 163 * \param group EC_GROUP object 164 * \return the currently used montgomery data (possibly NULL). 165 */ 166 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group); 167 168 /** Gets the order of a EC_GROUP 169 * \param group EC_GROUP object 170 * \param order BIGNUM to which the order is copied 171 * \param ctx unused 172 * \return 1 on success and 0 if an error occurred 173 */ 174 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx); 175 176 /** Gets the order of an EC_GROUP 177 * \param group EC_GROUP object 178 * \return the group order 179 */ 180 const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group); 181 182 /** Gets the number of bits of the order of an EC_GROUP 183 * \param group EC_GROUP object 184 * \return number of bits of group order. 185 */ 186 int EC_GROUP_order_bits(const EC_GROUP *group); 187 188 /** Gets the cofactor of a EC_GROUP 189 * \param group EC_GROUP object 190 * \param cofactor BIGNUM to which the cofactor is copied 191 * \param ctx unused 192 * \return 1 on success and 0 if an error occurred 193 */ 194 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, 195 BN_CTX *ctx); 196 197 /** Gets the cofactor of an EC_GROUP 198 * \param group EC_GROUP object 199 * \return the group cofactor 200 */ 201 const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group); 202 203 /** Sets the name of a EC_GROUP object 204 * \param group EC_GROUP object 205 * \param nid NID of the curve name OID 206 */ 207 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid); 208 209 /** Returns the curve name of a EC_GROUP object 210 * \param group EC_GROUP object 211 * \return NID of the curve name OID or 0 if not set. 212 */ 213 int EC_GROUP_get_curve_name(const EC_GROUP *group); 214 215 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag); 216 int EC_GROUP_get_asn1_flag(const EC_GROUP *group); 217 218 void EC_GROUP_set_point_conversion_form(EC_GROUP *group, 219 point_conversion_form_t form); 220 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP *); 221 222 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *x); 223 size_t EC_GROUP_get_seed_len(const EC_GROUP *); 224 size_t EC_GROUP_set_seed(EC_GROUP *, const unsigned char *, size_t len); 225 226 /** Sets the parameters of a ec curve defined by y^2 = x^3 + a*x + b (for GFp) 227 * or y^2 + x*y = x^3 + a*x^2 + b (for GF2m) 228 * \param group EC_GROUP object 229 * \param p BIGNUM with the prime number (GFp) or the polynomial 230 * defining the underlying field (GF2m) 231 * \param a BIGNUM with parameter a of the equation 232 * \param b BIGNUM with parameter b of the equation 233 * \param ctx BN_CTX object (optional) 234 * \return 1 on success and 0 if an error occurred 235 */ 236 int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, 237 const BIGNUM *b, BN_CTX *ctx); 238 239 /** Gets the parameters of the ec curve defined by y^2 = x^3 + a*x + b (for GFp) 240 * or y^2 + x*y = x^3 + a*x^2 + b (for GF2m) 241 * \param group EC_GROUP object 242 * \param p BIGNUM with the prime number (GFp) or the polynomial 243 * defining the underlying field (GF2m) 244 * \param a BIGNUM for parameter a of the equation 245 * \param b BIGNUM for parameter b of the equation 246 * \param ctx BN_CTX object (optional) 247 * \return 1 on success and 0 if an error occurred 248 */ 249 int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, 250 BN_CTX *ctx); 251 252 /** Sets the parameters of an ec curve. Synonym for EC_GROUP_set_curve 253 * \param group EC_GROUP object 254 * \param p BIGNUM with the prime number (GFp) or the polynomial 255 * defining the underlying field (GF2m) 256 * \param a BIGNUM with parameter a of the equation 257 * \param b BIGNUM with parameter b of the equation 258 * \param ctx BN_CTX object (optional) 259 * \return 1 on success and 0 if an error occurred 260 */ 261 DEPRECATEDIN_1_2_0(int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, 262 const BIGNUM *a, const BIGNUM *b, 263 BN_CTX *ctx)) 264 265 /** Gets the parameters of an ec curve. Synonym for EC_GROUP_get_curve 266 * \param group EC_GROUP object 267 * \param p BIGNUM with the prime number (GFp) or the polynomial 268 * defining the underlying field (GF2m) 269 * \param a BIGNUM for parameter a of the equation 270 * \param b BIGNUM for parameter b of the equation 271 * \param ctx BN_CTX object (optional) 272 * \return 1 on success and 0 if an error occurred 273 */ 274 DEPRECATEDIN_1_2_0(int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, 275 BIGNUM *a, BIGNUM *b, 276 BN_CTX *ctx)) 277 278 # ifndef OPENSSL_NO_EC2M 279 /** Sets the parameter of an ec curve. Synonym for EC_GROUP_set_curve 280 * \param group EC_GROUP object 281 * \param p BIGNUM with the prime number (GFp) or the polynomial 282 * defining the underlying field (GF2m) 283 * \param a BIGNUM with parameter a of the equation 284 * \param b BIGNUM with parameter b of the equation 285 * \param ctx BN_CTX object (optional) 286 * \return 1 on success and 0 if an error occurred 287 */ 288 DEPRECATEDIN_1_2_0(int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, 289 const BIGNUM *a, const BIGNUM *b, 290 BN_CTX *ctx)) 291 292 /** Gets the parameters of an ec curve. Synonym for EC_GROUP_get_curve 293 * \param group EC_GROUP object 294 * \param p BIGNUM with the prime number (GFp) or the polynomial 295 * defining the underlying field (GF2m) 296 * \param a BIGNUM for parameter a of the equation 297 * \param b BIGNUM for parameter b of the equation 298 * \param ctx BN_CTX object (optional) 299 * \return 1 on success and 0 if an error occurred 300 */ 301 DEPRECATEDIN_1_2_0(int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, 302 BIGNUM *a, BIGNUM *b, 303 BN_CTX *ctx)) 304 # endif 305 /** Returns the number of bits needed to represent a field element 306 * \param group EC_GROUP object 307 * \return number of bits needed to represent a field element 308 */ 309 int EC_GROUP_get_degree(const EC_GROUP *group); 310 311 /** Checks whether the parameter in the EC_GROUP define a valid ec group 312 * \param group EC_GROUP object 313 * \param ctx BN_CTX object (optional) 314 * \return 1 if group is a valid ec group and 0 otherwise 315 */ 316 int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx); 317 318 /** Checks whether the discriminant of the elliptic curve is zero or not 319 * \param group EC_GROUP object 320 * \param ctx BN_CTX object (optional) 321 * \return 1 if the discriminant is not zero and 0 otherwise 322 */ 323 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx); 324 325 /** Compares two EC_GROUP objects 326 * \param a first EC_GROUP object 327 * \param b second EC_GROUP object 328 * \param ctx BN_CTX object (optional) 329 * \return 0 if the groups are equal, 1 if not, or -1 on error 330 */ 331 int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx); 332 333 /* 334 * EC_GROUP_new_GF*() calls EC_GROUP_new() and EC_GROUP_set_GF*() after 335 * choosing an appropriate EC_METHOD 336 */ 337 338 /** Creates a new EC_GROUP object with the specified parameters defined 339 * over GFp (defined by the equation y^2 = x^3 + a*x + b) 340 * \param p BIGNUM with the prime number 341 * \param a BIGNUM with the parameter a of the equation 342 * \param b BIGNUM with the parameter b of the equation 343 * \param ctx BN_CTX object (optional) 344 * \return newly created EC_GROUP object with the specified parameters 345 */ 346 EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a, 347 const BIGNUM *b, BN_CTX *ctx); 348 # ifndef OPENSSL_NO_EC2M 349 /** Creates a new EC_GROUP object with the specified parameters defined 350 * over GF2m (defined by the equation y^2 + x*y = x^3 + a*x^2 + b) 351 * \param p BIGNUM with the polynomial defining the underlying field 352 * \param a BIGNUM with the parameter a of the equation 353 * \param b BIGNUM with the parameter b of the equation 354 * \param ctx BN_CTX object (optional) 355 * \return newly created EC_GROUP object with the specified parameters 356 */ 357 EC_GROUP *EC_GROUP_new_curve_GF2m(const BIGNUM *p, const BIGNUM *a, 358 const BIGNUM *b, BN_CTX *ctx); 359 # endif 360 361 /** Creates a EC_GROUP object with a curve specified by a NID 362 * \param nid NID of the OID of the curve name 363 * \return newly created EC_GROUP object with specified curve or NULL 364 * if an error occurred 365 */ 366 EC_GROUP *EC_GROUP_new_by_curve_name(int nid); 367 368 /** Creates a new EC_GROUP object from an ECPARAMETERS object 369 * \param params pointer to the ECPARAMETERS object 370 * \return newly created EC_GROUP object with specified curve or NULL 371 * if an error occurred 372 */ 373 EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params); 374 375 /** Creates an ECPARAMETERS object for the given EC_GROUP object. 376 * \param group pointer to the EC_GROUP object 377 * \param params pointer to an existing ECPARAMETERS object or NULL 378 * \return pointer to the new ECPARAMETERS object or NULL 379 * if an error occurred. 380 */ 381 ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group, 382 ECPARAMETERS *params); 383 384 /** Creates a new EC_GROUP object from an ECPKPARAMETERS object 385 * \param params pointer to an existing ECPKPARAMETERS object, or NULL 386 * \return newly created EC_GROUP object with specified curve, or NULL 387 * if an error occurred 388 */ 389 EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params); 390 391 /** Creates an ECPKPARAMETERS object for the given EC_GROUP object. 392 * \param group pointer to the EC_GROUP object 393 * \param params pointer to an existing ECPKPARAMETERS object or NULL 394 * \return pointer to the new ECPKPARAMETERS object or NULL 395 * if an error occurred. 396 */ 397 ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group, 398 ECPKPARAMETERS *params); 399 400 /********************************************************************/ 401 /* handling of internal curves */ 402 /********************************************************************/ 403 404 typedef struct { 405 int nid; 406 const char *comment; 407 } EC_builtin_curve; 408 409 /* 410 * EC_builtin_curves(EC_builtin_curve *r, size_t size) returns number of all 411 * available curves or zero if a error occurred. In case r is not zero, 412 * nitems EC_builtin_curve structures are filled with the data of the first 413 * nitems internal groups 414 */ 415 size_t EC_get_builtin_curves(EC_builtin_curve *r, size_t nitems); 416 417 const char *EC_curve_nid2nist(int nid); 418 int EC_curve_nist2nid(const char *name); 419 420 /********************************************************************/ 421 /* EC_POINT functions */ 422 /********************************************************************/ 423 424 /** Creates a new EC_POINT object for the specified EC_GROUP 425 * \param group EC_GROUP the underlying EC_GROUP object 426 * \return newly created EC_POINT object or NULL if an error occurred 427 */ 428 EC_POINT *EC_POINT_new(const EC_GROUP *group); 429 430 /** Frees a EC_POINT object 431 * \param point EC_POINT object to be freed 432 */ 433 void EC_POINT_free(EC_POINT *point); 434 435 /** Clears and frees a EC_POINT object 436 * \param point EC_POINT object to be cleared and freed 437 */ 438 void EC_POINT_clear_free(EC_POINT *point); 439 440 /** Copies EC_POINT object 441 * \param dst destination EC_POINT object 442 * \param src source EC_POINT object 443 * \return 1 on success and 0 if an error occurred 444 */ 445 int EC_POINT_copy(EC_POINT *dst, const EC_POINT *src); 446 447 /** Creates a new EC_POINT object and copies the content of the supplied 448 * EC_POINT 449 * \param src source EC_POINT object 450 * \param group underlying the EC_GROUP object 451 * \return newly created EC_POINT object or NULL if an error occurred 452 */ 453 EC_POINT *EC_POINT_dup(const EC_POINT *src, const EC_GROUP *group); 454 455 /** Returns the EC_METHOD used in EC_POINT object 456 * \param point EC_POINT object 457 * \return the EC_METHOD used 458 */ 459 const EC_METHOD *EC_POINT_method_of(const EC_POINT *point); 460 461 /** Sets a point to infinity (neutral element) 462 * \param group underlying EC_GROUP object 463 * \param point EC_POINT to set to infinity 464 * \return 1 on success and 0 if an error occurred 465 */ 466 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point); 467 468 /** Sets the jacobian projective coordinates of a EC_POINT over GFp 469 * \param group underlying EC_GROUP object 470 * \param p EC_POINT object 471 * \param x BIGNUM with the x-coordinate 472 * \param y BIGNUM with the y-coordinate 473 * \param z BIGNUM with the z-coordinate 474 * \param ctx BN_CTX object (optional) 475 * \return 1 on success and 0 if an error occurred 476 */ 477 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group, 478 EC_POINT *p, const BIGNUM *x, 479 const BIGNUM *y, const BIGNUM *z, 480 BN_CTX *ctx); 481 482 /** Gets the jacobian projective coordinates of a EC_POINT over GFp 483 * \param group underlying EC_GROUP object 484 * \param p EC_POINT object 485 * \param x BIGNUM for the x-coordinate 486 * \param y BIGNUM for the y-coordinate 487 * \param z BIGNUM for the z-coordinate 488 * \param ctx BN_CTX object (optional) 489 * \return 1 on success and 0 if an error occurred 490 */ 491 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group, 492 const EC_POINT *p, BIGNUM *x, 493 BIGNUM *y, BIGNUM *z, 494 BN_CTX *ctx); 495 496 /** Sets the affine coordinates of an EC_POINT 497 * \param group underlying EC_GROUP object 498 * \param p EC_POINT object 499 * \param x BIGNUM with the x-coordinate 500 * \param y BIGNUM with the y-coordinate 501 * \param ctx BN_CTX object (optional) 502 * \return 1 on success and 0 if an error occurred 503 */ 504 int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *p, 505 const BIGNUM *x, const BIGNUM *y, 506 BN_CTX *ctx); 507 508 /** Gets the affine coordinates of an EC_POINT. 509 * \param group underlying EC_GROUP object 510 * \param p EC_POINT object 511 * \param x BIGNUM for the x-coordinate 512 * \param y BIGNUM for the y-coordinate 513 * \param ctx BN_CTX object (optional) 514 * \return 1 on success and 0 if an error occurred 515 */ 516 int EC_POINT_get_affine_coordinates(const EC_GROUP *group, const EC_POINT *p, 517 BIGNUM *x, BIGNUM *y, BN_CTX *ctx); 518 519 /** Sets the affine coordinates of an EC_POINT. A synonym of 520 * EC_POINT_set_affine_coordinates 521 * \param group underlying EC_GROUP object 522 * \param p EC_POINT object 523 * \param x BIGNUM with the x-coordinate 524 * \param y BIGNUM with the y-coordinate 525 * \param ctx BN_CTX object (optional) 526 * \return 1 on success and 0 if an error occurred 527 */ 528 DEPRECATEDIN_1_2_0(int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, 529 EC_POINT *p, 530 const BIGNUM *x, 531 const BIGNUM *y, 532 BN_CTX *ctx)) 533 534 /** Gets the affine coordinates of an EC_POINT. A synonym of 535 * EC_POINT_get_affine_coordinates 536 * \param group underlying EC_GROUP object 537 * \param p EC_POINT object 538 * \param x BIGNUM for the x-coordinate 539 * \param y BIGNUM for the y-coordinate 540 * \param ctx BN_CTX object (optional) 541 * \return 1 on success and 0 if an error occurred 542 */ 543 DEPRECATEDIN_1_2_0(int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, 544 const EC_POINT *p, 545 BIGNUM *x, 546 BIGNUM *y, 547 BN_CTX *ctx)) 548 549 /** Sets the x9.62 compressed coordinates of a EC_POINT 550 * \param group underlying EC_GROUP object 551 * \param p EC_POINT object 552 * \param x BIGNUM with x-coordinate 553 * \param y_bit integer with the y-Bit (either 0 or 1) 554 * \param ctx BN_CTX object (optional) 555 * \return 1 on success and 0 if an error occurred 556 */ 557 int EC_POINT_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *p, 558 const BIGNUM *x, int y_bit, 559 BN_CTX *ctx); 560 561 /** Sets the x9.62 compressed coordinates of a EC_POINT. A synonym of 562 * EC_POINT_set_compressed_coordinates 563 * \param group underlying EC_GROUP object 564 * \param p EC_POINT object 565 * \param x BIGNUM with x-coordinate 566 * \param y_bit integer with the y-Bit (either 0 or 1) 567 * \param ctx BN_CTX object (optional) 568 * \return 1 on success and 0 if an error occurred 569 */ 570 DEPRECATEDIN_1_2_0(int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group, 571 EC_POINT *p, 572 const BIGNUM *x, 573 int y_bit, 574 BN_CTX *ctx)) 575 # ifndef OPENSSL_NO_EC2M 576 /** Sets the affine coordinates of an EC_POINT. A synonym of 577 * EC_POINT_set_affine_coordinates 578 * \param group underlying EC_GROUP object 579 * \param p EC_POINT object 580 * \param x BIGNUM with the x-coordinate 581 * \param y BIGNUM with the y-coordinate 582 * \param ctx BN_CTX object (optional) 583 * \return 1 on success and 0 if an error occurred 584 */ 585 DEPRECATEDIN_1_2_0(int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, 586 EC_POINT *p, 587 const BIGNUM *x, 588 const BIGNUM *y, 589 BN_CTX *ctx)) 590 591 /** Gets the affine coordinates of an EC_POINT. A synonym of 592 * EC_POINT_get_affine_coordinates 593 * \param group underlying EC_GROUP object 594 * \param p EC_POINT object 595 * \param x BIGNUM for the x-coordinate 596 * \param y BIGNUM for the y-coordinate 597 * \param ctx BN_CTX object (optional) 598 * \return 1 on success and 0 if an error occurred 599 */ 600 DEPRECATEDIN_1_2_0(int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group, 601 const EC_POINT *p, 602 BIGNUM *x, 603 BIGNUM *y, 604 BN_CTX *ctx)) 605 606 /** Sets the x9.62 compressed coordinates of a EC_POINT. A synonym of 607 * EC_POINT_set_compressed_coordinates 608 * \param group underlying EC_GROUP object 609 * \param p EC_POINT object 610 * \param x BIGNUM with x-coordinate 611 * \param y_bit integer with the y-Bit (either 0 or 1) 612 * \param ctx BN_CTX object (optional) 613 * \return 1 on success and 0 if an error occurred 614 */ 615 DEPRECATEDIN_1_2_0(int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group, 616 EC_POINT *p, 617 const BIGNUM *x, 618 int y_bit, 619 BN_CTX *ctx)) 620 # endif 621 /** Encodes a EC_POINT object to a octet string 622 * \param group underlying EC_GROUP object 623 * \param p EC_POINT object 624 * \param form point conversion form 625 * \param buf memory buffer for the result. If NULL the function returns 626 * required buffer size. 627 * \param len length of the memory buffer 628 * \param ctx BN_CTX object (optional) 629 * \return the length of the encoded octet string or 0 if an error occurred 630 */ 631 size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *p, 632 point_conversion_form_t form, 633 unsigned char *buf, size_t len, BN_CTX *ctx); 634 635 /** Decodes a EC_POINT from a octet string 636 * \param group underlying EC_GROUP object 637 * \param p EC_POINT object 638 * \param buf memory buffer with the encoded ec point 639 * \param len length of the encoded ec point 640 * \param ctx BN_CTX object (optional) 641 * \return 1 on success and 0 if an error occurred 642 */ 643 int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *p, 644 const unsigned char *buf, size_t len, BN_CTX *ctx); 645 646 /** Encodes an EC_POINT object to an allocated octet string 647 * \param group underlying EC_GROUP object 648 * \param point EC_POINT object 649 * \param form point conversion form 650 * \param pbuf returns pointer to allocated buffer 651 * \param ctx BN_CTX object (optional) 652 * \return the length of the encoded octet string or 0 if an error occurred 653 */ 654 size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point, 655 point_conversion_form_t form, 656 unsigned char **pbuf, BN_CTX *ctx); 657 658 /* other interfaces to point2oct/oct2point: */ 659 BIGNUM *EC_POINT_point2bn(const EC_GROUP *, const EC_POINT *, 660 point_conversion_form_t form, BIGNUM *, BN_CTX *); 661 EC_POINT *EC_POINT_bn2point(const EC_GROUP *, const BIGNUM *, 662 EC_POINT *, BN_CTX *); 663 char *EC_POINT_point2hex(const EC_GROUP *, const EC_POINT *, 664 point_conversion_form_t form, BN_CTX *); 665 EC_POINT *EC_POINT_hex2point(const EC_GROUP *, const char *, 666 EC_POINT *, BN_CTX *); 667 668 /********************************************************************/ 669 /* functions for doing EC_POINT arithmetic */ 670 /********************************************************************/ 671 672 /** Computes the sum of two EC_POINT 673 * \param group underlying EC_GROUP object 674 * \param r EC_POINT object for the result (r = a + b) 675 * \param a EC_POINT object with the first summand 676 * \param b EC_POINT object with the second summand 677 * \param ctx BN_CTX object (optional) 678 * \return 1 on success and 0 if an error occurred 679 */ 680 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, 681 const EC_POINT *b, BN_CTX *ctx); 682 683 /** Computes the double of a EC_POINT 684 * \param group underlying EC_GROUP object 685 * \param r EC_POINT object for the result (r = 2 * a) 686 * \param a EC_POINT object 687 * \param ctx BN_CTX object (optional) 688 * \return 1 on success and 0 if an error occurred 689 */ 690 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, 691 BN_CTX *ctx); 692 693 /** Computes the inverse of a EC_POINT 694 * \param group underlying EC_GROUP object 695 * \param a EC_POINT object to be inverted (it's used for the result as well) 696 * \param ctx BN_CTX object (optional) 697 * \return 1 on success and 0 if an error occurred 698 */ 699 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx); 700 701 /** Checks whether the point is the neutral element of the group 702 * \param group the underlying EC_GROUP object 703 * \param p EC_POINT object 704 * \return 1 if the point is the neutral element and 0 otherwise 705 */ 706 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *p); 707 708 /** Checks whether the point is on the curve 709 * \param group underlying EC_GROUP object 710 * \param point EC_POINT object to check 711 * \param ctx BN_CTX object (optional) 712 * \return 1 if the point is on the curve, 0 if not, or -1 on error 713 */ 714 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point, 715 BN_CTX *ctx); 716 717 /** Compares two EC_POINTs 718 * \param group underlying EC_GROUP object 719 * \param a first EC_POINT object 720 * \param b second EC_POINT object 721 * \param ctx BN_CTX object (optional) 722 * \return 1 if the points are not equal, 0 if they are, or -1 on error 723 */ 724 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, 725 BN_CTX *ctx); 726 727 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx); 728 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num, 729 EC_POINT *points[], BN_CTX *ctx); 730 731 /** Computes r = generator * n + sum_{i=0}^{num-1} p[i] * m[i] 732 * \param group underlying EC_GROUP object 733 * \param r EC_POINT object for the result 734 * \param n BIGNUM with the multiplier for the group generator (optional) 735 * \param num number further summands 736 * \param p array of size num of EC_POINT objects 737 * \param m array of size num of BIGNUM objects 738 * \param ctx BN_CTX object (optional) 739 * \return 1 on success and 0 if an error occurred 740 */ 741 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n, 742 size_t num, const EC_POINT *p[], const BIGNUM *m[], 743 BN_CTX *ctx); 744 745 /** Computes r = generator * n + q * m 746 * \param group underlying EC_GROUP object 747 * \param r EC_POINT object for the result 748 * \param n BIGNUM with the multiplier for the group generator (optional) 749 * \param q EC_POINT object with the first factor of the second summand 750 * \param m BIGNUM with the second factor of the second summand 751 * \param ctx BN_CTX object (optional) 752 * \return 1 on success and 0 if an error occurred 753 */ 754 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n, 755 const EC_POINT *q, const BIGNUM *m, BN_CTX *ctx); 756 757 /** Stores multiples of generator for faster point multiplication 758 * \param group EC_GROUP object 759 * \param ctx BN_CTX object (optional) 760 * \return 1 on success and 0 if an error occurred 761 */ 762 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx); 763 764 /** Reports whether a precomputation has been done 765 * \param group EC_GROUP object 766 * \return 1 if a pre-computation has been done and 0 otherwise 767 */ 768 int EC_GROUP_have_precompute_mult(const EC_GROUP *group); 769 770 /********************************************************************/ 771 /* ASN1 stuff */ 772 /********************************************************************/ 773 774 DECLARE_ASN1_ITEM(ECPKPARAMETERS) 775 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPKPARAMETERS) 776 DECLARE_ASN1_ITEM(ECPARAMETERS) 777 DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS) 778 779 /* 780 * EC_GROUP_get_basis_type() returns the NID of the basis type used to 781 * represent the field elements 782 */ 783 int EC_GROUP_get_basis_type(const EC_GROUP *); 784 # ifndef OPENSSL_NO_EC2M 785 int EC_GROUP_get_trinomial_basis(const EC_GROUP *, unsigned int *k); 786 int EC_GROUP_get_pentanomial_basis(const EC_GROUP *, unsigned int *k1, 787 unsigned int *k2, unsigned int *k3); 788 # endif 789 790 # define OPENSSL_EC_EXPLICIT_CURVE 0x000 791 # define OPENSSL_EC_NAMED_CURVE 0x001 792 793 EC_GROUP *d2i_ECPKParameters(EC_GROUP **, const unsigned char **in, long len); 794 int i2d_ECPKParameters(const EC_GROUP *, unsigned char **out); 795 796 # define d2i_ECPKParameters_bio(bp,x) \ 797 ASN1_d2i_bio_of(EC_GROUP, NULL, d2i_ECPKParameters, bp, x) 798 # define i2d_ECPKParameters_bio(bp,x) \ 799 ASN1_i2d_bio_of_const(EC_GROUP, i2d_ECPKParameters, bp, x) 800 # define d2i_ECPKParameters_fp(fp,x) \ 801 (EC_GROUP *)ASN1_d2i_fp(NULL, (d2i_of_void *)d2i_ECPKParameters, (fp), \ 802 (void **)(x)) 803 # define i2d_ECPKParameters_fp(fp,x) \ 804 ASN1_i2d_fp((i2d_of_void *)i2d_ECPKParameters, (fp), (void *)(x)) 805 806 int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off); 807 # ifndef OPENSSL_NO_STDIO 808 int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off); 809 # endif 810 811 /********************************************************************/ 812 /* EC_KEY functions */ 813 /********************************************************************/ 814 815 /* some values for the encoding_flag */ 816 # define EC_PKEY_NO_PARAMETERS 0x001 817 # define EC_PKEY_NO_PUBKEY 0x002 818 819 /* some values for the flags field */ 820 # define EC_FLAG_NON_FIPS_ALLOW 0x1 821 # define EC_FLAG_FIPS_CHECKED 0x2 822 # define EC_FLAG_COFACTOR_ECDH 0x1000 823 824 /** Creates a new EC_KEY object. 825 * \return EC_KEY object or NULL if an error occurred. 826 */ 827 EC_KEY *EC_KEY_new(void); 828 829 int EC_KEY_get_flags(const EC_KEY *key); 830 831 void EC_KEY_set_flags(EC_KEY *key, int flags); 832 833 void EC_KEY_clear_flags(EC_KEY *key, int flags); 834 835 int EC_KEY_decoded_from_explicit_params(const EC_KEY *key); 836 837 /** Creates a new EC_KEY object using a named curve as underlying 838 * EC_GROUP object. 839 * \param nid NID of the named curve. 840 * \return EC_KEY object or NULL if an error occurred. 841 */ 842 EC_KEY *EC_KEY_new_by_curve_name(int nid); 843 844 /** Frees a EC_KEY object. 845 * \param key EC_KEY object to be freed. 846 */ 847 void EC_KEY_free(EC_KEY *key); 848 849 /** Copies a EC_KEY object. 850 * \param dst destination EC_KEY object 851 * \param src src EC_KEY object 852 * \return dst or NULL if an error occurred. 853 */ 854 EC_KEY *EC_KEY_copy(EC_KEY *dst, const EC_KEY *src); 855 856 /** Creates a new EC_KEY object and copies the content from src to it. 857 * \param src the source EC_KEY object 858 * \return newly created EC_KEY object or NULL if an error occurred. 859 */ 860 EC_KEY *EC_KEY_dup(const EC_KEY *src); 861 862 /** Increases the internal reference count of a EC_KEY object. 863 * \param key EC_KEY object 864 * \return 1 on success and 0 if an error occurred. 865 */ 866 int EC_KEY_up_ref(EC_KEY *key); 867 868 /** Returns the ENGINE object of a EC_KEY object 869 * \param eckey EC_KEY object 870 * \return the ENGINE object (possibly NULL). 871 */ 872 ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey); 873 874 /** Returns the EC_GROUP object of a EC_KEY object 875 * \param key EC_KEY object 876 * \return the EC_GROUP object (possibly NULL). 877 */ 878 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key); 879 880 /** Sets the EC_GROUP of a EC_KEY object. 881 * \param key EC_KEY object 882 * \param group EC_GROUP to use in the EC_KEY object (note: the EC_KEY 883 * object will use an own copy of the EC_GROUP). 884 * \return 1 on success and 0 if an error occurred. 885 */ 886 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group); 887 888 /** Returns the private key of a EC_KEY object. 889 * \param key EC_KEY object 890 * \return a BIGNUM with the private key (possibly NULL). 891 */ 892 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key); 893 894 /** Sets the private key of a EC_KEY object. 895 * \param key EC_KEY object 896 * \param prv BIGNUM with the private key (note: the EC_KEY object 897 * will use an own copy of the BIGNUM). 898 * \return 1 on success and 0 if an error occurred. 899 */ 900 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *prv); 901 902 /** Returns the public key of a EC_KEY object. 903 * \param key the EC_KEY object 904 * \return a EC_POINT object with the public key (possibly NULL) 905 */ 906 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key); 907 908 /** Sets the public key of a EC_KEY object. 909 * \param key EC_KEY object 910 * \param pub EC_POINT object with the public key (note: the EC_KEY object 911 * will use an own copy of the EC_POINT object). 912 * \return 1 on success and 0 if an error occurred. 913 */ 914 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub); 915 916 unsigned EC_KEY_get_enc_flags(const EC_KEY *key); 917 void EC_KEY_set_enc_flags(EC_KEY *eckey, unsigned int flags); 918 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key); 919 void EC_KEY_set_conv_form(EC_KEY *eckey, point_conversion_form_t cform); 920 921 #define EC_KEY_get_ex_new_index(l, p, newf, dupf, freef) \ 922 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_EC_KEY, l, p, newf, dupf, freef) 923 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg); 924 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx); 925 926 /* wrapper functions for the underlying EC_GROUP object */ 927 void EC_KEY_set_asn1_flag(EC_KEY *eckey, int asn1_flag); 928 929 /** Creates a table of pre-computed multiples of the generator to 930 * accelerate further EC_KEY operations. 931 * \param key EC_KEY object 932 * \param ctx BN_CTX object (optional) 933 * \return 1 on success and 0 if an error occurred. 934 */ 935 int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx); 936 937 /** Creates a new ec private (and optional a new public) key. 938 * \param key EC_KEY object 939 * \return 1 on success and 0 if an error occurred. 940 */ 941 int EC_KEY_generate_key(EC_KEY *key); 942 943 /** Verifies that a private and/or public key is valid. 944 * \param key the EC_KEY object 945 * \return 1 on success and 0 otherwise. 946 */ 947 int EC_KEY_check_key(const EC_KEY *key); 948 949 /** Indicates if an EC_KEY can be used for signing. 950 * \param eckey the EC_KEY object 951 * \return 1 if can can sign and 0 otherwise. 952 */ 953 int EC_KEY_can_sign(const EC_KEY *eckey); 954 955 /** Sets a public key from affine coordinates performing 956 * necessary NIST PKV tests. 957 * \param key the EC_KEY object 958 * \param x public key x coordinate 959 * \param y public key y coordinate 960 * \return 1 on success and 0 otherwise. 961 */ 962 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x, 963 BIGNUM *y); 964 965 /** Encodes an EC_KEY public key to an allocated octet string 966 * \param key key to encode 967 * \param form point conversion form 968 * \param pbuf returns pointer to allocated buffer 969 * \param ctx BN_CTX object (optional) 970 * \return the length of the encoded octet string or 0 if an error occurred 971 */ 972 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form, 973 unsigned char **pbuf, BN_CTX *ctx); 974 975 /** Decodes a EC_KEY public key from a octet string 976 * \param key key to decode 977 * \param buf memory buffer with the encoded ec point 978 * \param len length of the encoded ec point 979 * \param ctx BN_CTX object (optional) 980 * \return 1 on success and 0 if an error occurred 981 */ 982 983 int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len, 984 BN_CTX *ctx); 985 986 /** Decodes an EC_KEY private key from an octet string 987 * \param key key to decode 988 * \param buf memory buffer with the encoded private key 989 * \param len length of the encoded key 990 * \return 1 on success and 0 if an error occurred 991 */ 992 993 int EC_KEY_oct2priv(EC_KEY *key, const unsigned char *buf, size_t len); 994 995 /** Encodes a EC_KEY private key to an octet string 996 * \param key key to encode 997 * \param buf memory buffer for the result. If NULL the function returns 998 * required buffer size. 999 * \param len length of the memory buffer 1000 * \return the length of the encoded octet string or 0 if an error occurred 1001 */ 1002 1003 size_t EC_KEY_priv2oct(const EC_KEY *key, unsigned char *buf, size_t len); 1004 1005 /** Encodes an EC_KEY private key to an allocated octet string 1006 * \param eckey key to encode 1007 * \param pbuf returns pointer to allocated buffer 1008 * \return the length of the encoded octet string or 0 if an error occurred 1009 */ 1010 size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf); 1011 1012 /********************************************************************/ 1013 /* de- and encoding functions for SEC1 ECPrivateKey */ 1014 /********************************************************************/ 1015 1016 /** Decodes a private key from a memory buffer. 1017 * \param key a pointer to a EC_KEY object which should be used (or NULL) 1018 * \param in pointer to memory with the DER encoded private key 1019 * \param len length of the DER encoded private key 1020 * \return the decoded private key or NULL if an error occurred. 1021 */ 1022 EC_KEY *d2i_ECPrivateKey(EC_KEY **key, const unsigned char **in, long len); 1023 1024 /** Encodes a private key object and stores the result in a buffer. 1025 * \param key the EC_KEY object to encode 1026 * \param out the buffer for the result (if NULL the function returns number 1027 * of bytes needed). 1028 * \return 1 on success and 0 if an error occurred. 1029 */ 1030 int i2d_ECPrivateKey(EC_KEY *key, unsigned char **out); 1031 1032 /********************************************************************/ 1033 /* de- and encoding functions for EC parameters */ 1034 /********************************************************************/ 1035 1036 /** Decodes ec parameter from a memory buffer. 1037 * \param key a pointer to a EC_KEY object which should be used (or NULL) 1038 * \param in pointer to memory with the DER encoded ec parameters 1039 * \param len length of the DER encoded ec parameters 1040 * \return a EC_KEY object with the decoded parameters or NULL if an error 1041 * occurred. 1042 */ 1043 EC_KEY *d2i_ECParameters(EC_KEY **key, const unsigned char **in, long len); 1044 1045 /** Encodes ec parameter and stores the result in a buffer. 1046 * \param key the EC_KEY object with ec parameters to encode 1047 * \param out the buffer for the result (if NULL the function returns number 1048 * of bytes needed). 1049 * \return 1 on success and 0 if an error occurred. 1050 */ 1051 int i2d_ECParameters(EC_KEY *key, unsigned char **out); 1052 1053 /********************************************************************/ 1054 /* de- and encoding functions for EC public key */ 1055 /* (octet string, not DER -- hence 'o2i' and 'i2o') */ 1056 /********************************************************************/ 1057 1058 /** Decodes a ec public key from a octet string. 1059 * \param key a pointer to a EC_KEY object which should be used 1060 * \param in memory buffer with the encoded public key 1061 * \param len length of the encoded public key 1062 * \return EC_KEY object with decoded public key or NULL if an error 1063 * occurred. 1064 */ 1065 EC_KEY *o2i_ECPublicKey(EC_KEY **key, const unsigned char **in, long len); 1066 1067 /** Encodes a ec public key in an octet string. 1068 * \param key the EC_KEY object with the public key 1069 * \param out the buffer for the result (if NULL the function returns number 1070 * of bytes needed). 1071 * \return 1 on success and 0 if an error occurred 1072 */ 1073 int i2o_ECPublicKey(const EC_KEY *key, unsigned char **out); 1074 1075 /** Prints out the ec parameters on human readable form. 1076 * \param bp BIO object to which the information is printed 1077 * \param key EC_KEY object 1078 * \return 1 on success and 0 if an error occurred 1079 */ 1080 int ECParameters_print(BIO *bp, const EC_KEY *key); 1081 1082 /** Prints out the contents of a EC_KEY object 1083 * \param bp BIO object to which the information is printed 1084 * \param key EC_KEY object 1085 * \param off line offset 1086 * \return 1 on success and 0 if an error occurred 1087 */ 1088 int EC_KEY_print(BIO *bp, const EC_KEY *key, int off); 1089 1090 # ifndef OPENSSL_NO_STDIO 1091 /** Prints out the ec parameters on human readable form. 1092 * \param fp file descriptor to which the information is printed 1093 * \param key EC_KEY object 1094 * \return 1 on success and 0 if an error occurred 1095 */ 1096 int ECParameters_print_fp(FILE *fp, const EC_KEY *key); 1097 1098 /** Prints out the contents of a EC_KEY object 1099 * \param fp file descriptor to which the information is printed 1100 * \param key EC_KEY object 1101 * \param off line offset 1102 * \return 1 on success and 0 if an error occurred 1103 */ 1104 int EC_KEY_print_fp(FILE *fp, const EC_KEY *key, int off); 1105 1106 # endif 1107 1108 const EC_KEY_METHOD *EC_KEY_OpenSSL(void); 1109 const EC_KEY_METHOD *EC_KEY_get_default_method(void); 1110 void EC_KEY_set_default_method(const EC_KEY_METHOD *meth); 1111 const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key); 1112 int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth); 1113 EC_KEY *EC_KEY_new_method(ENGINE *engine); 1114 1115 /** The old name for ecdh_KDF_X9_63 1116 * The ECDH KDF specification has been mistakingly attributed to ANSI X9.62, 1117 * it is actually specified in ANSI X9.63. 1118 * This identifier is retained for backwards compatibility 1119 */ 1120 int ECDH_KDF_X9_62(unsigned char *out, size_t outlen, 1121 const unsigned char *Z, size_t Zlen, 1122 const unsigned char *sinfo, size_t sinfolen, 1123 const EVP_MD *md); 1124 1125 int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key, 1126 const EC_KEY *ecdh, 1127 void *(*KDF) (const void *in, size_t inlen, 1128 void *out, size_t *outlen)); 1129 1130 typedef struct ECDSA_SIG_st ECDSA_SIG; 1131 1132 /** Allocates and initialize a ECDSA_SIG structure 1133 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred 1134 */ 1135 ECDSA_SIG *ECDSA_SIG_new(void); 1136 1137 /** frees a ECDSA_SIG structure 1138 * \param sig pointer to the ECDSA_SIG structure 1139 */ 1140 void ECDSA_SIG_free(ECDSA_SIG *sig); 1141 1142 /** DER encode content of ECDSA_SIG object (note: this function modifies *pp 1143 * (*pp += length of the DER encoded signature)). 1144 * \param sig pointer to the ECDSA_SIG object 1145 * \param pp pointer to a unsigned char pointer for the output or NULL 1146 * \return the length of the DER encoded ECDSA_SIG object or a negative value 1147 * on error 1148 */ 1149 int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp); 1150 1151 /** Decodes a DER encoded ECDSA signature (note: this function changes *pp 1152 * (*pp += len)). 1153 * \param sig pointer to ECDSA_SIG pointer (may be NULL) 1154 * \param pp memory buffer with the DER encoded signature 1155 * \param len length of the buffer 1156 * \return pointer to the decoded ECDSA_SIG structure (or NULL) 1157 */ 1158 ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **sig, const unsigned char **pp, long len); 1159 1160 /** Accessor for r and s fields of ECDSA_SIG 1161 * \param sig pointer to ECDSA_SIG structure 1162 * \param pr pointer to BIGNUM pointer for r (may be NULL) 1163 * \param ps pointer to BIGNUM pointer for s (may be NULL) 1164 */ 1165 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps); 1166 1167 /** Accessor for r field of ECDSA_SIG 1168 * \param sig pointer to ECDSA_SIG structure 1169 */ 1170 const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig); 1171 1172 /** Accessor for s field of ECDSA_SIG 1173 * \param sig pointer to ECDSA_SIG structure 1174 */ 1175 const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig); 1176 1177 /** Setter for r and s fields of ECDSA_SIG 1178 * \param sig pointer to ECDSA_SIG structure 1179 * \param r pointer to BIGNUM for r (may be NULL) 1180 * \param s pointer to BIGNUM for s (may be NULL) 1181 */ 1182 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s); 1183 1184 /** Computes the ECDSA signature of the given hash value using 1185 * the supplied private key and returns the created signature. 1186 * \param dgst pointer to the hash value 1187 * \param dgst_len length of the hash value 1188 * \param eckey EC_KEY object containing a private EC key 1189 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred 1190 */ 1191 ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst, int dgst_len, 1192 EC_KEY *eckey); 1193 1194 /** Computes ECDSA signature of a given hash value using the supplied 1195 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory). 1196 * \param dgst pointer to the hash value to sign 1197 * \param dgstlen length of the hash value 1198 * \param kinv BIGNUM with a pre-computed inverse k (optional) 1199 * \param rp BIGNUM with a pre-computed rp value (optional), 1200 * see ECDSA_sign_setup 1201 * \param eckey EC_KEY object containing a private EC key 1202 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred 1203 */ 1204 ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen, 1205 const BIGNUM *kinv, const BIGNUM *rp, 1206 EC_KEY *eckey); 1207 1208 /** Verifies that the supplied signature is a valid ECDSA 1209 * signature of the supplied hash value using the supplied public key. 1210 * \param dgst pointer to the hash value 1211 * \param dgst_len length of the hash value 1212 * \param sig ECDSA_SIG structure 1213 * \param eckey EC_KEY object containing a public EC key 1214 * \return 1 if the signature is valid, 0 if the signature is invalid 1215 * and -1 on error 1216 */ 1217 int ECDSA_do_verify(const unsigned char *dgst, int dgst_len, 1218 const ECDSA_SIG *sig, EC_KEY *eckey); 1219 1220 /** Precompute parts of the signing operation 1221 * \param eckey EC_KEY object containing a private EC key 1222 * \param ctx BN_CTX object (optional) 1223 * \param kinv BIGNUM pointer for the inverse of k 1224 * \param rp BIGNUM pointer for x coordinate of k * generator 1225 * \return 1 on success and 0 otherwise 1226 */ 1227 int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, BIGNUM **rp); 1228 1229 /** Computes ECDSA signature of a given hash value using the supplied 1230 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory). 1231 * \param type this parameter is ignored 1232 * \param dgst pointer to the hash value to sign 1233 * \param dgstlen length of the hash value 1234 * \param sig memory for the DER encoded created signature 1235 * \param siglen pointer to the length of the returned signature 1236 * \param eckey EC_KEY object containing a private EC key 1237 * \return 1 on success and 0 otherwise 1238 */ 1239 int ECDSA_sign(int type, const unsigned char *dgst, int dgstlen, 1240 unsigned char *sig, unsigned int *siglen, EC_KEY *eckey); 1241 1242 /** Computes ECDSA signature of a given hash value using the supplied 1243 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory). 1244 * \param type this parameter is ignored 1245 * \param dgst pointer to the hash value to sign 1246 * \param dgstlen length of the hash value 1247 * \param sig buffer to hold the DER encoded signature 1248 * \param siglen pointer to the length of the returned signature 1249 * \param kinv BIGNUM with a pre-computed inverse k (optional) 1250 * \param rp BIGNUM with a pre-computed rp value (optional), 1251 * see ECDSA_sign_setup 1252 * \param eckey EC_KEY object containing a private EC key 1253 * \return 1 on success and 0 otherwise 1254 */ 1255 int ECDSA_sign_ex(int type, const unsigned char *dgst, int dgstlen, 1256 unsigned char *sig, unsigned int *siglen, 1257 const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey); 1258 1259 /** Verifies that the given signature is valid ECDSA signature 1260 * of the supplied hash value using the specified public key. 1261 * \param type this parameter is ignored 1262 * \param dgst pointer to the hash value 1263 * \param dgstlen length of the hash value 1264 * \param sig pointer to the DER encoded signature 1265 * \param siglen length of the DER encoded signature 1266 * \param eckey EC_KEY object containing a public EC key 1267 * \return 1 if the signature is valid, 0 if the signature is invalid 1268 * and -1 on error 1269 */ 1270 int ECDSA_verify(int type, const unsigned char *dgst, int dgstlen, 1271 const unsigned char *sig, int siglen, EC_KEY *eckey); 1272 1273 /** Returns the maximum length of the DER encoded signature 1274 * \param eckey EC_KEY object 1275 * \return numbers of bytes required for the DER encoded signature 1276 */ 1277 int ECDSA_size(const EC_KEY *eckey); 1278 1279 /********************************************************************/ 1280 /* EC_KEY_METHOD constructors, destructors, writers and accessors */ 1281 /********************************************************************/ 1282 1283 EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth); 1284 void EC_KEY_METHOD_free(EC_KEY_METHOD *meth); 1285 void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth, 1286 int (*init)(EC_KEY *key), 1287 void (*finish)(EC_KEY *key), 1288 int (*copy)(EC_KEY *dest, const EC_KEY *src), 1289 int (*set_group)(EC_KEY *key, const EC_GROUP *grp), 1290 int (*set_private)(EC_KEY *key, 1291 const BIGNUM *priv_key), 1292 int (*set_public)(EC_KEY *key, 1293 const EC_POINT *pub_key)); 1294 1295 void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth, 1296 int (*keygen)(EC_KEY *key)); 1297 1298 void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth, 1299 int (*ckey)(unsigned char **psec, 1300 size_t *pseclen, 1301 const EC_POINT *pub_key, 1302 const EC_KEY *ecdh)); 1303 1304 void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth, 1305 int (*sign)(int type, const unsigned char *dgst, 1306 int dlen, unsigned char *sig, 1307 unsigned int *siglen, 1308 const BIGNUM *kinv, const BIGNUM *r, 1309 EC_KEY *eckey), 1310 int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, 1311 BIGNUM **kinvp, BIGNUM **rp), 1312 ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, 1313 int dgst_len, 1314 const BIGNUM *in_kinv, 1315 const BIGNUM *in_r, 1316 EC_KEY *eckey)); 1317 1318 void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth, 1319 int (*verify)(int type, const unsigned 1320 char *dgst, int dgst_len, 1321 const unsigned char *sigbuf, 1322 int sig_len, EC_KEY *eckey), 1323 int (*verify_sig)(const unsigned char *dgst, 1324 int dgst_len, 1325 const ECDSA_SIG *sig, 1326 EC_KEY *eckey)); 1327 1328 void EC_KEY_METHOD_get_init(const EC_KEY_METHOD *meth, 1329 int (**pinit)(EC_KEY *key), 1330 void (**pfinish)(EC_KEY *key), 1331 int (**pcopy)(EC_KEY *dest, const EC_KEY *src), 1332 int (**pset_group)(EC_KEY *key, 1333 const EC_GROUP *grp), 1334 int (**pset_private)(EC_KEY *key, 1335 const BIGNUM *priv_key), 1336 int (**pset_public)(EC_KEY *key, 1337 const EC_POINT *pub_key)); 1338 1339 void EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD *meth, 1340 int (**pkeygen)(EC_KEY *key)); 1341 1342 void EC_KEY_METHOD_get_compute_key(const EC_KEY_METHOD *meth, 1343 int (**pck)(unsigned char **psec, 1344 size_t *pseclen, 1345 const EC_POINT *pub_key, 1346 const EC_KEY *ecdh)); 1347 1348 void EC_KEY_METHOD_get_sign(const EC_KEY_METHOD *meth, 1349 int (**psign)(int type, const unsigned char *dgst, 1350 int dlen, unsigned char *sig, 1351 unsigned int *siglen, 1352 const BIGNUM *kinv, const BIGNUM *r, 1353 EC_KEY *eckey), 1354 int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, 1355 BIGNUM **kinvp, BIGNUM **rp), 1356 ECDSA_SIG *(**psign_sig)(const unsigned char *dgst, 1357 int dgst_len, 1358 const BIGNUM *in_kinv, 1359 const BIGNUM *in_r, 1360 EC_KEY *eckey)); 1361 1362 void EC_KEY_METHOD_get_verify(const EC_KEY_METHOD *meth, 1363 int (**pverify)(int type, const unsigned 1364 char *dgst, int dgst_len, 1365 const unsigned char *sigbuf, 1366 int sig_len, EC_KEY *eckey), 1367 int (**pverify_sig)(const unsigned char *dgst, 1368 int dgst_len, 1369 const ECDSA_SIG *sig, 1370 EC_KEY *eckey)); 1371 1372 # define ECParameters_dup(x) ASN1_dup_of(EC_KEY,i2d_ECParameters,d2i_ECParameters,x) 1373 1374 # ifndef __cplusplus 1375 # if defined(__SUNPRO_C) 1376 # if __SUNPRO_C >= 0x520 1377 # pragma error_messages (default,E_ARRAY_OF_INCOMPLETE_NONAME,E_ARRAY_OF_INCOMPLETE) 1378 # endif 1379 # endif 1380 # endif 1381 1382 # define EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid) \ 1383 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1384 EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN, \ 1385 EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, nid, NULL) 1386 1387 # define EVP_PKEY_CTX_set_ec_param_enc(ctx, flag) \ 1388 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1389 EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN, \ 1390 EVP_PKEY_CTRL_EC_PARAM_ENC, flag, NULL) 1391 1392 # define EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, flag) \ 1393 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1394 EVP_PKEY_OP_DERIVE, \ 1395 EVP_PKEY_CTRL_EC_ECDH_COFACTOR, flag, NULL) 1396 1397 # define EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx) \ 1398 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1399 EVP_PKEY_OP_DERIVE, \ 1400 EVP_PKEY_CTRL_EC_ECDH_COFACTOR, -2, NULL) 1401 1402 # define EVP_PKEY_CTX_set_ecdh_kdf_type(ctx, kdf) \ 1403 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1404 EVP_PKEY_OP_DERIVE, \ 1405 EVP_PKEY_CTRL_EC_KDF_TYPE, kdf, NULL) 1406 1407 # define EVP_PKEY_CTX_get_ecdh_kdf_type(ctx) \ 1408 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1409 EVP_PKEY_OP_DERIVE, \ 1410 EVP_PKEY_CTRL_EC_KDF_TYPE, -2, NULL) 1411 1412 # define EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md) \ 1413 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1414 EVP_PKEY_OP_DERIVE, \ 1415 EVP_PKEY_CTRL_EC_KDF_MD, 0, (void *)(md)) 1416 1417 # define EVP_PKEY_CTX_get_ecdh_kdf_md(ctx, pmd) \ 1418 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1419 EVP_PKEY_OP_DERIVE, \ 1420 EVP_PKEY_CTRL_GET_EC_KDF_MD, 0, (void *)(pmd)) 1421 1422 # define EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx, len) \ 1423 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1424 EVP_PKEY_OP_DERIVE, \ 1425 EVP_PKEY_CTRL_EC_KDF_OUTLEN, len, NULL) 1426 1427 # define EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx, plen) \ 1428 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1429 EVP_PKEY_OP_DERIVE, \ 1430 EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, 0, \ 1431 (void *)(plen)) 1432 1433 # define EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx, p, plen) \ 1434 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1435 EVP_PKEY_OP_DERIVE, \ 1436 EVP_PKEY_CTRL_EC_KDF_UKM, plen, (void *)(p)) 1437 1438 # define EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx, p) \ 1439 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1440 EVP_PKEY_OP_DERIVE, \ 1441 EVP_PKEY_CTRL_GET_EC_KDF_UKM, 0, (void *)(p)) 1442 1443 /* SM2 will skip the operation check so no need to pass operation here */ 1444 # define EVP_PKEY_CTX_set1_id(ctx, id, id_len) \ 1445 EVP_PKEY_CTX_ctrl(ctx, -1, -1, \ 1446 EVP_PKEY_CTRL_SET1_ID, (int)id_len, (void*)(id)) 1447 1448 # define EVP_PKEY_CTX_get1_id(ctx, id) \ 1449 EVP_PKEY_CTX_ctrl(ctx, -1, -1, \ 1450 EVP_PKEY_CTRL_GET1_ID, 0, (void*)(id)) 1451 1452 # define EVP_PKEY_CTX_get1_id_len(ctx, id_len) \ 1453 EVP_PKEY_CTX_ctrl(ctx, -1, -1, \ 1454 EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)(id_len)) 1455 1456 # define EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID (EVP_PKEY_ALG_CTRL + 1) 1457 # define EVP_PKEY_CTRL_EC_PARAM_ENC (EVP_PKEY_ALG_CTRL + 2) 1458 # define EVP_PKEY_CTRL_EC_ECDH_COFACTOR (EVP_PKEY_ALG_CTRL + 3) 1459 # define EVP_PKEY_CTRL_EC_KDF_TYPE (EVP_PKEY_ALG_CTRL + 4) 1460 # define EVP_PKEY_CTRL_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 5) 1461 # define EVP_PKEY_CTRL_GET_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 6) 1462 # define EVP_PKEY_CTRL_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 7) 1463 # define EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 8) 1464 # define EVP_PKEY_CTRL_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 9) 1465 # define EVP_PKEY_CTRL_GET_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 10) 1466 # define EVP_PKEY_CTRL_SET1_ID (EVP_PKEY_ALG_CTRL + 11) 1467 # define EVP_PKEY_CTRL_GET1_ID (EVP_PKEY_ALG_CTRL + 12) 1468 # define EVP_PKEY_CTRL_GET1_ID_LEN (EVP_PKEY_ALG_CTRL + 13) 1469 /* KDF types */ 1470 # define EVP_PKEY_ECDH_KDF_NONE 1 1471 # define EVP_PKEY_ECDH_KDF_X9_63 2 1472 /** The old name for EVP_PKEY_ECDH_KDF_X9_63 1473 * The ECDH KDF specification has been mistakingly attributed to ANSI X9.62, 1474 * it is actually specified in ANSI X9.63. 1475 * This identifier is retained for backwards compatibility 1476 */ 1477 # define EVP_PKEY_ECDH_KDF_X9_62 EVP_PKEY_ECDH_KDF_X9_63 1478 1479 1480 # ifdef __cplusplus 1481 } 1482 # endif 1483 # endif 1484 #endif 1485