1 /* 2 * Copyright 2002-2019 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) ASN1_d2i_bio_of(EC_GROUP,NULL,d2i_ECPKParameters,bp,x) 797 # define i2d_ECPKParameters_bio(bp,x) ASN1_i2d_bio_of_const(EC_GROUP,i2d_ECPKParameters,bp,x) 798 # define d2i_ECPKParameters_fp(fp,x) (EC_GROUP *)ASN1_d2i_fp(NULL, \ 799 (char *(*)())d2i_ECPKParameters,(fp),(unsigned char **)(x)) 800 # define i2d_ECPKParameters_fp(fp,x) ASN1_i2d_fp(i2d_ECPKParameters,(fp), \ 801 (unsigned char *)(x)) 802 803 int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off); 804 # ifndef OPENSSL_NO_STDIO 805 int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off); 806 # endif 807 808 /********************************************************************/ 809 /* EC_KEY functions */ 810 /********************************************************************/ 811 812 /* some values for the encoding_flag */ 813 # define EC_PKEY_NO_PARAMETERS 0x001 814 # define EC_PKEY_NO_PUBKEY 0x002 815 816 /* some values for the flags field */ 817 # define EC_FLAG_NON_FIPS_ALLOW 0x1 818 # define EC_FLAG_FIPS_CHECKED 0x2 819 # define EC_FLAG_COFACTOR_ECDH 0x1000 820 821 /** Creates a new EC_KEY object. 822 * \return EC_KEY object or NULL if an error occurred. 823 */ 824 EC_KEY *EC_KEY_new(void); 825 826 int EC_KEY_get_flags(const EC_KEY *key); 827 828 void EC_KEY_set_flags(EC_KEY *key, int flags); 829 830 void EC_KEY_clear_flags(EC_KEY *key, int flags); 831 832 /** Creates a new EC_KEY object using a named curve as underlying 833 * EC_GROUP object. 834 * \param nid NID of the named curve. 835 * \return EC_KEY object or NULL if an error occurred. 836 */ 837 EC_KEY *EC_KEY_new_by_curve_name(int nid); 838 839 /** Frees a EC_KEY object. 840 * \param key EC_KEY object to be freed. 841 */ 842 void EC_KEY_free(EC_KEY *key); 843 844 /** Copies a EC_KEY object. 845 * \param dst destination EC_KEY object 846 * \param src src EC_KEY object 847 * \return dst or NULL if an error occurred. 848 */ 849 EC_KEY *EC_KEY_copy(EC_KEY *dst, const EC_KEY *src); 850 851 /** Creates a new EC_KEY object and copies the content from src to it. 852 * \param src the source EC_KEY object 853 * \return newly created EC_KEY object or NULL if an error occurred. 854 */ 855 EC_KEY *EC_KEY_dup(const EC_KEY *src); 856 857 /** Increases the internal reference count of a EC_KEY object. 858 * \param key EC_KEY object 859 * \return 1 on success and 0 if an error occurred. 860 */ 861 int EC_KEY_up_ref(EC_KEY *key); 862 863 /** Returns the ENGINE object of a EC_KEY object 864 * \param eckey EC_KEY object 865 * \return the ENGINE object (possibly NULL). 866 */ 867 ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey); 868 869 /** Returns the EC_GROUP object of a EC_KEY object 870 * \param key EC_KEY object 871 * \return the EC_GROUP object (possibly NULL). 872 */ 873 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key); 874 875 /** Sets the EC_GROUP of a EC_KEY object. 876 * \param key EC_KEY object 877 * \param group EC_GROUP to use in the EC_KEY object (note: the EC_KEY 878 * object will use an own copy of the EC_GROUP). 879 * \return 1 on success and 0 if an error occurred. 880 */ 881 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group); 882 883 /** Returns the private key of a EC_KEY object. 884 * \param key EC_KEY object 885 * \return a BIGNUM with the private key (possibly NULL). 886 */ 887 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key); 888 889 /** Sets the private key of a EC_KEY object. 890 * \param key EC_KEY object 891 * \param prv BIGNUM with the private key (note: the EC_KEY object 892 * will use an own copy of the BIGNUM). 893 * \return 1 on success and 0 if an error occurred. 894 */ 895 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *prv); 896 897 /** Returns the public key of a EC_KEY object. 898 * \param key the EC_KEY object 899 * \return a EC_POINT object with the public key (possibly NULL) 900 */ 901 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key); 902 903 /** Sets the public key of a EC_KEY object. 904 * \param key EC_KEY object 905 * \param pub EC_POINT object with the public key (note: the EC_KEY object 906 * will use an own copy of the EC_POINT object). 907 * \return 1 on success and 0 if an error occurred. 908 */ 909 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub); 910 911 unsigned EC_KEY_get_enc_flags(const EC_KEY *key); 912 void EC_KEY_set_enc_flags(EC_KEY *eckey, unsigned int flags); 913 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key); 914 void EC_KEY_set_conv_form(EC_KEY *eckey, point_conversion_form_t cform); 915 916 #define EC_KEY_get_ex_new_index(l, p, newf, dupf, freef) \ 917 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_EC_KEY, l, p, newf, dupf, freef) 918 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg); 919 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx); 920 921 /* wrapper functions for the underlying EC_GROUP object */ 922 void EC_KEY_set_asn1_flag(EC_KEY *eckey, int asn1_flag); 923 924 /** Creates a table of pre-computed multiples of the generator to 925 * accelerate further EC_KEY operations. 926 * \param key EC_KEY object 927 * \param ctx BN_CTX object (optional) 928 * \return 1 on success and 0 if an error occurred. 929 */ 930 int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx); 931 932 /** Creates a new ec private (and optional a new public) key. 933 * \param key EC_KEY object 934 * \return 1 on success and 0 if an error occurred. 935 */ 936 int EC_KEY_generate_key(EC_KEY *key); 937 938 /** Verifies that a private and/or public key is valid. 939 * \param key the EC_KEY object 940 * \return 1 on success and 0 otherwise. 941 */ 942 int EC_KEY_check_key(const EC_KEY *key); 943 944 /** Indicates if an EC_KEY can be used for signing. 945 * \param eckey the EC_KEY object 946 * \return 1 if can can sign and 0 otherwise. 947 */ 948 int EC_KEY_can_sign(const EC_KEY *eckey); 949 950 /** Sets a public key from affine coordinates performing 951 * necessary NIST PKV tests. 952 * \param key the EC_KEY object 953 * \param x public key x coordinate 954 * \param y public key y coordinate 955 * \return 1 on success and 0 otherwise. 956 */ 957 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x, 958 BIGNUM *y); 959 960 /** Encodes an EC_KEY public key to an allocated octet string 961 * \param key key to encode 962 * \param form point conversion form 963 * \param pbuf returns pointer to allocated buffer 964 * \param ctx BN_CTX object (optional) 965 * \return the length of the encoded octet string or 0 if an error occurred 966 */ 967 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form, 968 unsigned char **pbuf, BN_CTX *ctx); 969 970 /** Decodes a EC_KEY public key from a octet string 971 * \param key key to decode 972 * \param buf memory buffer with the encoded ec point 973 * \param len length of the encoded ec point 974 * \param ctx BN_CTX object (optional) 975 * \return 1 on success and 0 if an error occurred 976 */ 977 978 int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len, 979 BN_CTX *ctx); 980 981 /** Decodes an EC_KEY private key from an octet string 982 * \param key key to decode 983 * \param buf memory buffer with the encoded private key 984 * \param len length of the encoded key 985 * \return 1 on success and 0 if an error occurred 986 */ 987 988 int EC_KEY_oct2priv(EC_KEY *key, const unsigned char *buf, size_t len); 989 990 /** Encodes a EC_KEY private key to an octet string 991 * \param key key to encode 992 * \param buf memory buffer for the result. If NULL the function returns 993 * required buffer size. 994 * \param len length of the memory buffer 995 * \return the length of the encoded octet string or 0 if an error occurred 996 */ 997 998 size_t EC_KEY_priv2oct(const EC_KEY *key, unsigned char *buf, size_t len); 999 1000 /** Encodes an EC_KEY private key to an allocated octet string 1001 * \param eckey key to encode 1002 * \param pbuf returns pointer to allocated buffer 1003 * \return the length of the encoded octet string or 0 if an error occurred 1004 */ 1005 size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf); 1006 1007 /********************************************************************/ 1008 /* de- and encoding functions for SEC1 ECPrivateKey */ 1009 /********************************************************************/ 1010 1011 /** Decodes a private key from a memory buffer. 1012 * \param key a pointer to a EC_KEY object which should be used (or NULL) 1013 * \param in pointer to memory with the DER encoded private key 1014 * \param len length of the DER encoded private key 1015 * \return the decoded private key or NULL if an error occurred. 1016 */ 1017 EC_KEY *d2i_ECPrivateKey(EC_KEY **key, const unsigned char **in, long len); 1018 1019 /** Encodes a private key object and stores the result in a buffer. 1020 * \param key the EC_KEY object to encode 1021 * \param out the buffer for the result (if NULL the function returns number 1022 * of bytes needed). 1023 * \return 1 on success and 0 if an error occurred. 1024 */ 1025 int i2d_ECPrivateKey(EC_KEY *key, unsigned char **out); 1026 1027 /********************************************************************/ 1028 /* de- and encoding functions for EC parameters */ 1029 /********************************************************************/ 1030 1031 /** Decodes ec parameter from a memory buffer. 1032 * \param key a pointer to a EC_KEY object which should be used (or NULL) 1033 * \param in pointer to memory with the DER encoded ec parameters 1034 * \param len length of the DER encoded ec parameters 1035 * \return a EC_KEY object with the decoded parameters or NULL if an error 1036 * occurred. 1037 */ 1038 EC_KEY *d2i_ECParameters(EC_KEY **key, const unsigned char **in, long len); 1039 1040 /** Encodes ec parameter and stores the result in a buffer. 1041 * \param key the EC_KEY object with ec parameters to encode 1042 * \param out the buffer for the result (if NULL the function returns number 1043 * of bytes needed). 1044 * \return 1 on success and 0 if an error occurred. 1045 */ 1046 int i2d_ECParameters(EC_KEY *key, unsigned char **out); 1047 1048 /********************************************************************/ 1049 /* de- and encoding functions for EC public key */ 1050 /* (octet string, not DER -- hence 'o2i' and 'i2o') */ 1051 /********************************************************************/ 1052 1053 /** Decodes a ec public key from a octet string. 1054 * \param key a pointer to a EC_KEY object which should be used 1055 * \param in memory buffer with the encoded public key 1056 * \param len length of the encoded public key 1057 * \return EC_KEY object with decoded public key or NULL if an error 1058 * occurred. 1059 */ 1060 EC_KEY *o2i_ECPublicKey(EC_KEY **key, const unsigned char **in, long len); 1061 1062 /** Encodes a ec public key in an octet string. 1063 * \param key the EC_KEY object with the public key 1064 * \param out the buffer for the result (if NULL the function returns number 1065 * of bytes needed). 1066 * \return 1 on success and 0 if an error occurred 1067 */ 1068 int i2o_ECPublicKey(const EC_KEY *key, unsigned char **out); 1069 1070 /** Prints out the ec parameters on human readable form. 1071 * \param bp BIO object to which the information is printed 1072 * \param key EC_KEY object 1073 * \return 1 on success and 0 if an error occurred 1074 */ 1075 int ECParameters_print(BIO *bp, const EC_KEY *key); 1076 1077 /** Prints out the contents of a EC_KEY object 1078 * \param bp BIO object to which the information is printed 1079 * \param key EC_KEY object 1080 * \param off line offset 1081 * \return 1 on success and 0 if an error occurred 1082 */ 1083 int EC_KEY_print(BIO *bp, const EC_KEY *key, int off); 1084 1085 # ifndef OPENSSL_NO_STDIO 1086 /** Prints out the ec parameters on human readable form. 1087 * \param fp file descriptor to which the information is printed 1088 * \param key EC_KEY object 1089 * \return 1 on success and 0 if an error occurred 1090 */ 1091 int ECParameters_print_fp(FILE *fp, const EC_KEY *key); 1092 1093 /** Prints out the contents of a EC_KEY object 1094 * \param fp file descriptor to which the information is printed 1095 * \param key EC_KEY object 1096 * \param off line offset 1097 * \return 1 on success and 0 if an error occurred 1098 */ 1099 int EC_KEY_print_fp(FILE *fp, const EC_KEY *key, int off); 1100 1101 # endif 1102 1103 const EC_KEY_METHOD *EC_KEY_OpenSSL(void); 1104 const EC_KEY_METHOD *EC_KEY_get_default_method(void); 1105 void EC_KEY_set_default_method(const EC_KEY_METHOD *meth); 1106 const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key); 1107 int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth); 1108 EC_KEY *EC_KEY_new_method(ENGINE *engine); 1109 1110 /** The old name for ecdh_KDF_X9_63 1111 * The ECDH KDF specification has been mistakingly attributed to ANSI X9.62, 1112 * it is actually specified in ANSI X9.63. 1113 * This identifier is retained for backwards compatibility 1114 */ 1115 int ECDH_KDF_X9_62(unsigned char *out, size_t outlen, 1116 const unsigned char *Z, size_t Zlen, 1117 const unsigned char *sinfo, size_t sinfolen, 1118 const EVP_MD *md); 1119 1120 int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key, 1121 const EC_KEY *ecdh, 1122 void *(*KDF) (const void *in, size_t inlen, 1123 void *out, size_t *outlen)); 1124 1125 typedef struct ECDSA_SIG_st ECDSA_SIG; 1126 1127 /** Allocates and initialize a ECDSA_SIG structure 1128 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred 1129 */ 1130 ECDSA_SIG *ECDSA_SIG_new(void); 1131 1132 /** frees a ECDSA_SIG structure 1133 * \param sig pointer to the ECDSA_SIG structure 1134 */ 1135 void ECDSA_SIG_free(ECDSA_SIG *sig); 1136 1137 /** DER encode content of ECDSA_SIG object (note: this function modifies *pp 1138 * (*pp += length of the DER encoded signature)). 1139 * \param sig pointer to the ECDSA_SIG object 1140 * \param pp pointer to a unsigned char pointer for the output or NULL 1141 * \return the length of the DER encoded ECDSA_SIG object or a negative value 1142 * on error 1143 */ 1144 int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp); 1145 1146 /** Decodes a DER encoded ECDSA signature (note: this function changes *pp 1147 * (*pp += len)). 1148 * \param sig pointer to ECDSA_SIG pointer (may be NULL) 1149 * \param pp memory buffer with the DER encoded signature 1150 * \param len length of the buffer 1151 * \return pointer to the decoded ECDSA_SIG structure (or NULL) 1152 */ 1153 ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **sig, const unsigned char **pp, long len); 1154 1155 /** Accessor for r and s fields of ECDSA_SIG 1156 * \param sig pointer to ECDSA_SIG structure 1157 * \param pr pointer to BIGNUM pointer for r (may be NULL) 1158 * \param ps pointer to BIGNUM pointer for s (may be NULL) 1159 */ 1160 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps); 1161 1162 /** Accessor for r field of ECDSA_SIG 1163 * \param sig pointer to ECDSA_SIG structure 1164 */ 1165 const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig); 1166 1167 /** Accessor for s field of ECDSA_SIG 1168 * \param sig pointer to ECDSA_SIG structure 1169 */ 1170 const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig); 1171 1172 /** Setter for r and s fields of ECDSA_SIG 1173 * \param sig pointer to ECDSA_SIG structure 1174 * \param r pointer to BIGNUM for r (may be NULL) 1175 * \param s pointer to BIGNUM for s (may be NULL) 1176 */ 1177 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s); 1178 1179 /** Computes the ECDSA signature of the given hash value using 1180 * the supplied private key and returns the created signature. 1181 * \param dgst pointer to the hash value 1182 * \param dgst_len length of the hash value 1183 * \param eckey EC_KEY object containing a private EC key 1184 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred 1185 */ 1186 ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst, int dgst_len, 1187 EC_KEY *eckey); 1188 1189 /** Computes ECDSA signature of a given hash value using the supplied 1190 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory). 1191 * \param dgst pointer to the hash value to sign 1192 * \param dgstlen length of the hash value 1193 * \param kinv BIGNUM with a pre-computed inverse k (optional) 1194 * \param rp BIGNUM with a pre-computed rp value (optional), 1195 * see ECDSA_sign_setup 1196 * \param eckey EC_KEY object containing a private EC key 1197 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred 1198 */ 1199 ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen, 1200 const BIGNUM *kinv, const BIGNUM *rp, 1201 EC_KEY *eckey); 1202 1203 /** Verifies that the supplied signature is a valid ECDSA 1204 * signature of the supplied hash value using the supplied public key. 1205 * \param dgst pointer to the hash value 1206 * \param dgst_len length of the hash value 1207 * \param sig ECDSA_SIG structure 1208 * \param eckey EC_KEY object containing a public EC key 1209 * \return 1 if the signature is valid, 0 if the signature is invalid 1210 * and -1 on error 1211 */ 1212 int ECDSA_do_verify(const unsigned char *dgst, int dgst_len, 1213 const ECDSA_SIG *sig, EC_KEY *eckey); 1214 1215 /** Precompute parts of the signing operation 1216 * \param eckey EC_KEY object containing a private EC key 1217 * \param ctx BN_CTX object (optional) 1218 * \param kinv BIGNUM pointer for the inverse of k 1219 * \param rp BIGNUM pointer for x coordinate of k * generator 1220 * \return 1 on success and 0 otherwise 1221 */ 1222 int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, BIGNUM **rp); 1223 1224 /** Computes ECDSA signature of a given hash value using the supplied 1225 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory). 1226 * \param type this parameter is ignored 1227 * \param dgst pointer to the hash value to sign 1228 * \param dgstlen length of the hash value 1229 * \param sig memory for the DER encoded created signature 1230 * \param siglen pointer to the length of the returned signature 1231 * \param eckey EC_KEY object containing a private EC key 1232 * \return 1 on success and 0 otherwise 1233 */ 1234 int ECDSA_sign(int type, const unsigned char *dgst, int dgstlen, 1235 unsigned char *sig, unsigned int *siglen, EC_KEY *eckey); 1236 1237 /** Computes ECDSA signature of a given hash value using the supplied 1238 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory). 1239 * \param type this parameter is ignored 1240 * \param dgst pointer to the hash value to sign 1241 * \param dgstlen length of the hash value 1242 * \param sig buffer to hold the DER encoded signature 1243 * \param siglen pointer to the length of the returned signature 1244 * \param kinv BIGNUM with a pre-computed inverse k (optional) 1245 * \param rp BIGNUM with a pre-computed rp value (optional), 1246 * see ECDSA_sign_setup 1247 * \param eckey EC_KEY object containing a private EC key 1248 * \return 1 on success and 0 otherwise 1249 */ 1250 int ECDSA_sign_ex(int type, const unsigned char *dgst, int dgstlen, 1251 unsigned char *sig, unsigned int *siglen, 1252 const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey); 1253 1254 /** Verifies that the given signature is valid ECDSA signature 1255 * of the supplied hash value using the specified public key. 1256 * \param type this parameter is ignored 1257 * \param dgst pointer to the hash value 1258 * \param dgstlen length of the hash value 1259 * \param sig pointer to the DER encoded signature 1260 * \param siglen length of the DER encoded signature 1261 * \param eckey EC_KEY object containing a public EC key 1262 * \return 1 if the signature is valid, 0 if the signature is invalid 1263 * and -1 on error 1264 */ 1265 int ECDSA_verify(int type, const unsigned char *dgst, int dgstlen, 1266 const unsigned char *sig, int siglen, EC_KEY *eckey); 1267 1268 /** Returns the maximum length of the DER encoded signature 1269 * \param eckey EC_KEY object 1270 * \return numbers of bytes required for the DER encoded signature 1271 */ 1272 int ECDSA_size(const EC_KEY *eckey); 1273 1274 /********************************************************************/ 1275 /* EC_KEY_METHOD constructors, destructors, writers and accessors */ 1276 /********************************************************************/ 1277 1278 EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth); 1279 void EC_KEY_METHOD_free(EC_KEY_METHOD *meth); 1280 void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth, 1281 int (*init)(EC_KEY *key), 1282 void (*finish)(EC_KEY *key), 1283 int (*copy)(EC_KEY *dest, const EC_KEY *src), 1284 int (*set_group)(EC_KEY *key, const EC_GROUP *grp), 1285 int (*set_private)(EC_KEY *key, 1286 const BIGNUM *priv_key), 1287 int (*set_public)(EC_KEY *key, 1288 const EC_POINT *pub_key)); 1289 1290 void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth, 1291 int (*keygen)(EC_KEY *key)); 1292 1293 void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth, 1294 int (*ckey)(unsigned char **psec, 1295 size_t *pseclen, 1296 const EC_POINT *pub_key, 1297 const EC_KEY *ecdh)); 1298 1299 void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth, 1300 int (*sign)(int type, const unsigned char *dgst, 1301 int dlen, unsigned char *sig, 1302 unsigned int *siglen, 1303 const BIGNUM *kinv, const BIGNUM *r, 1304 EC_KEY *eckey), 1305 int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, 1306 BIGNUM **kinvp, BIGNUM **rp), 1307 ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, 1308 int dgst_len, 1309 const BIGNUM *in_kinv, 1310 const BIGNUM *in_r, 1311 EC_KEY *eckey)); 1312 1313 void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth, 1314 int (*verify)(int type, const unsigned 1315 char *dgst, int dgst_len, 1316 const unsigned char *sigbuf, 1317 int sig_len, EC_KEY *eckey), 1318 int (*verify_sig)(const unsigned char *dgst, 1319 int dgst_len, 1320 const ECDSA_SIG *sig, 1321 EC_KEY *eckey)); 1322 1323 void EC_KEY_METHOD_get_init(const EC_KEY_METHOD *meth, 1324 int (**pinit)(EC_KEY *key), 1325 void (**pfinish)(EC_KEY *key), 1326 int (**pcopy)(EC_KEY *dest, const EC_KEY *src), 1327 int (**pset_group)(EC_KEY *key, 1328 const EC_GROUP *grp), 1329 int (**pset_private)(EC_KEY *key, 1330 const BIGNUM *priv_key), 1331 int (**pset_public)(EC_KEY *key, 1332 const EC_POINT *pub_key)); 1333 1334 void EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD *meth, 1335 int (**pkeygen)(EC_KEY *key)); 1336 1337 void EC_KEY_METHOD_get_compute_key(const EC_KEY_METHOD *meth, 1338 int (**pck)(unsigned char **psec, 1339 size_t *pseclen, 1340 const EC_POINT *pub_key, 1341 const EC_KEY *ecdh)); 1342 1343 void EC_KEY_METHOD_get_sign(const EC_KEY_METHOD *meth, 1344 int (**psign)(int type, const unsigned char *dgst, 1345 int dlen, unsigned char *sig, 1346 unsigned int *siglen, 1347 const BIGNUM *kinv, const BIGNUM *r, 1348 EC_KEY *eckey), 1349 int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, 1350 BIGNUM **kinvp, BIGNUM **rp), 1351 ECDSA_SIG *(**psign_sig)(const unsigned char *dgst, 1352 int dgst_len, 1353 const BIGNUM *in_kinv, 1354 const BIGNUM *in_r, 1355 EC_KEY *eckey)); 1356 1357 void EC_KEY_METHOD_get_verify(const EC_KEY_METHOD *meth, 1358 int (**pverify)(int type, const unsigned 1359 char *dgst, int dgst_len, 1360 const unsigned char *sigbuf, 1361 int sig_len, EC_KEY *eckey), 1362 int (**pverify_sig)(const unsigned char *dgst, 1363 int dgst_len, 1364 const ECDSA_SIG *sig, 1365 EC_KEY *eckey)); 1366 1367 # define ECParameters_dup(x) ASN1_dup_of(EC_KEY,i2d_ECParameters,d2i_ECParameters,x) 1368 1369 # ifndef __cplusplus 1370 # if defined(__SUNPRO_C) 1371 # if __SUNPRO_C >= 0x520 1372 # pragma error_messages (default,E_ARRAY_OF_INCOMPLETE_NONAME,E_ARRAY_OF_INCOMPLETE) 1373 # endif 1374 # endif 1375 # endif 1376 1377 # define EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid) \ 1378 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1379 EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN, \ 1380 EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, nid, NULL) 1381 1382 # define EVP_PKEY_CTX_set_ec_param_enc(ctx, flag) \ 1383 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1384 EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN, \ 1385 EVP_PKEY_CTRL_EC_PARAM_ENC, flag, NULL) 1386 1387 # define EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, flag) \ 1388 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1389 EVP_PKEY_OP_DERIVE, \ 1390 EVP_PKEY_CTRL_EC_ECDH_COFACTOR, flag, NULL) 1391 1392 # define EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx) \ 1393 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1394 EVP_PKEY_OP_DERIVE, \ 1395 EVP_PKEY_CTRL_EC_ECDH_COFACTOR, -2, NULL) 1396 1397 # define EVP_PKEY_CTX_set_ecdh_kdf_type(ctx, kdf) \ 1398 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1399 EVP_PKEY_OP_DERIVE, \ 1400 EVP_PKEY_CTRL_EC_KDF_TYPE, kdf, NULL) 1401 1402 # define EVP_PKEY_CTX_get_ecdh_kdf_type(ctx) \ 1403 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1404 EVP_PKEY_OP_DERIVE, \ 1405 EVP_PKEY_CTRL_EC_KDF_TYPE, -2, NULL) 1406 1407 # define EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md) \ 1408 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1409 EVP_PKEY_OP_DERIVE, \ 1410 EVP_PKEY_CTRL_EC_KDF_MD, 0, (void *)(md)) 1411 1412 # define EVP_PKEY_CTX_get_ecdh_kdf_md(ctx, pmd) \ 1413 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1414 EVP_PKEY_OP_DERIVE, \ 1415 EVP_PKEY_CTRL_GET_EC_KDF_MD, 0, (void *)(pmd)) 1416 1417 # define EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx, len) \ 1418 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1419 EVP_PKEY_OP_DERIVE, \ 1420 EVP_PKEY_CTRL_EC_KDF_OUTLEN, len, NULL) 1421 1422 # define EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx, plen) \ 1423 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1424 EVP_PKEY_OP_DERIVE, \ 1425 EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, 0, \ 1426 (void *)(plen)) 1427 1428 # define EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx, p, plen) \ 1429 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1430 EVP_PKEY_OP_DERIVE, \ 1431 EVP_PKEY_CTRL_EC_KDF_UKM, plen, (void *)(p)) 1432 1433 # define EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx, p) \ 1434 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ 1435 EVP_PKEY_OP_DERIVE, \ 1436 EVP_PKEY_CTRL_GET_EC_KDF_UKM, 0, (void *)(p)) 1437 1438 /* SM2 will skip the operation check so no need to pass operation here */ 1439 # define EVP_PKEY_CTX_set1_id(ctx, id, id_len) \ 1440 EVP_PKEY_CTX_ctrl(ctx, -1, -1, \ 1441 EVP_PKEY_CTRL_SET1_ID, (int)id_len, (void*)(id)) 1442 1443 # define EVP_PKEY_CTX_get1_id(ctx, id) \ 1444 EVP_PKEY_CTX_ctrl(ctx, -1, -1, \ 1445 EVP_PKEY_CTRL_GET1_ID, 0, (void*)(id)) 1446 1447 # define EVP_PKEY_CTX_get1_id_len(ctx, id_len) \ 1448 EVP_PKEY_CTX_ctrl(ctx, -1, -1, \ 1449 EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)(id_len)) 1450 1451 # define EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID (EVP_PKEY_ALG_CTRL + 1) 1452 # define EVP_PKEY_CTRL_EC_PARAM_ENC (EVP_PKEY_ALG_CTRL + 2) 1453 # define EVP_PKEY_CTRL_EC_ECDH_COFACTOR (EVP_PKEY_ALG_CTRL + 3) 1454 # define EVP_PKEY_CTRL_EC_KDF_TYPE (EVP_PKEY_ALG_CTRL + 4) 1455 # define EVP_PKEY_CTRL_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 5) 1456 # define EVP_PKEY_CTRL_GET_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 6) 1457 # define EVP_PKEY_CTRL_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 7) 1458 # define EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 8) 1459 # define EVP_PKEY_CTRL_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 9) 1460 # define EVP_PKEY_CTRL_GET_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 10) 1461 # define EVP_PKEY_CTRL_SET1_ID (EVP_PKEY_ALG_CTRL + 11) 1462 # define EVP_PKEY_CTRL_GET1_ID (EVP_PKEY_ALG_CTRL + 12) 1463 # define EVP_PKEY_CTRL_GET1_ID_LEN (EVP_PKEY_ALG_CTRL + 13) 1464 /* KDF types */ 1465 # define EVP_PKEY_ECDH_KDF_NONE 1 1466 # define EVP_PKEY_ECDH_KDF_X9_63 2 1467 /** The old name for EVP_PKEY_ECDH_KDF_X9_63 1468 * The ECDH KDF specification has been mistakingly attributed to ANSI X9.62, 1469 * it is actually specified in ANSI X9.63. 1470 * This identifier is retained for backwards compatibility 1471 */ 1472 # define EVP_PKEY_ECDH_KDF_X9_62 EVP_PKEY_ECDH_KDF_X9_63 1473 1474 1475 # ifdef __cplusplus 1476 } 1477 # endif 1478 # endif 1479 #endif 1480