1 /* $OpenBSD: ec_key.c,v 1.26 2021/04/20 17:23:37 tb Exp $ */ 2 /* 3 * Written by Nils Larsch for the OpenSSL project. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. All advertising materials mentioning features or use of this 21 * software must display the following acknowledgment: 22 * "This product includes software developed by the OpenSSL Project 23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 24 * 25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26 * endorse or promote products derived from this software without 27 * prior written permission. For written permission, please contact 28 * openssl-core@openssl.org. 29 * 30 * 5. Products derived from this software may not be called "OpenSSL" 31 * nor may "OpenSSL" appear in their names without prior written 32 * permission of the OpenSSL Project. 33 * 34 * 6. Redistributions of any form whatsoever must retain the following 35 * acknowledgment: 36 * "This product includes software developed by the OpenSSL Project 37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50 * OF THE POSSIBILITY OF SUCH DAMAGE. 51 * ==================================================================== 52 * 53 * This product includes cryptographic software written by Eric Young 54 * (eay@cryptsoft.com). This product includes software written by Tim 55 * Hudson (tjh@cryptsoft.com). 56 * 57 */ 58 /* ==================================================================== 59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 60 * Portions originally developed by SUN MICROSYSTEMS, INC., and 61 * contributed to the OpenSSL project. 62 */ 63 64 #include <string.h> 65 66 #include <openssl/opensslconf.h> 67 68 #ifndef OPENSSL_NO_ENGINE 69 #include <openssl/engine.h> 70 #endif 71 #include <openssl/err.h> 72 73 #include "bn_lcl.h" 74 #include "ec_lcl.h" 75 76 EC_KEY * 77 EC_KEY_new(void) 78 { 79 return EC_KEY_new_method(NULL); 80 } 81 82 EC_KEY * 83 EC_KEY_new_by_curve_name(int nid) 84 { 85 EC_KEY *ret = EC_KEY_new(); 86 if (ret == NULL) 87 return NULL; 88 ret->group = EC_GROUP_new_by_curve_name(nid); 89 if (ret->group == NULL) { 90 EC_KEY_free(ret); 91 return NULL; 92 } 93 if (ret->meth->set_group != NULL && 94 ret->meth->set_group(ret, ret->group) == 0) { 95 EC_KEY_free(ret); 96 return NULL; 97 } 98 return ret; 99 } 100 101 void 102 EC_KEY_free(EC_KEY * r) 103 { 104 int i; 105 106 if (r == NULL) 107 return; 108 109 i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_EC); 110 if (i > 0) 111 return; 112 113 if (r->meth != NULL && r->meth->finish != NULL) 114 r->meth->finish(r); 115 116 #ifndef OPENSSL_NO_ENGINE 117 ENGINE_finish(r->engine); 118 #endif 119 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data); 120 121 EC_GROUP_free(r->group); 122 EC_POINT_free(r->pub_key); 123 BN_clear_free(r->priv_key); 124 125 EC_EX_DATA_free_all_data(&r->method_data); 126 127 freezero(r, sizeof(EC_KEY)); 128 } 129 130 EC_KEY * 131 EC_KEY_copy(EC_KEY * dest, const EC_KEY * src) 132 { 133 EC_EXTRA_DATA *d; 134 135 if (dest == NULL || src == NULL) { 136 ECerror(ERR_R_PASSED_NULL_PARAMETER); 137 return NULL; 138 } 139 if (src->meth != dest->meth) { 140 if (dest->meth != NULL && dest->meth->finish != NULL) 141 dest->meth->finish(dest); 142 #ifndef OPENSSL_NO_ENGINE 143 if (ENGINE_finish(dest->engine) == 0) 144 return 0; 145 dest->engine = NULL; 146 #endif 147 } 148 /* copy the parameters */ 149 if (src->group) { 150 const EC_METHOD *meth = EC_GROUP_method_of(src->group); 151 /* clear the old group */ 152 EC_GROUP_free(dest->group); 153 dest->group = EC_GROUP_new(meth); 154 if (dest->group == NULL) 155 return NULL; 156 if (!EC_GROUP_copy(dest->group, src->group)) 157 return NULL; 158 } 159 /* copy the public key */ 160 if (src->pub_key && src->group) { 161 EC_POINT_free(dest->pub_key); 162 dest->pub_key = EC_POINT_new(src->group); 163 if (dest->pub_key == NULL) 164 return NULL; 165 if (!EC_POINT_copy(dest->pub_key, src->pub_key)) 166 return NULL; 167 } 168 /* copy the private key */ 169 if (src->priv_key) { 170 if (dest->priv_key == NULL) { 171 dest->priv_key = BN_new(); 172 if (dest->priv_key == NULL) 173 return NULL; 174 } 175 if (!BN_copy(dest->priv_key, src->priv_key)) 176 return NULL; 177 } 178 /* copy method/extra data */ 179 EC_EX_DATA_free_all_data(&dest->method_data); 180 181 for (d = src->method_data; d != NULL; d = d->next) { 182 void *t = d->dup_func(d->data); 183 184 if (t == NULL) 185 return 0; 186 if (!EC_EX_DATA_set_data(&dest->method_data, t, d->dup_func, 187 d->free_func, d->clear_free_func)) 188 return 0; 189 } 190 191 /* copy the rest */ 192 dest->enc_flag = src->enc_flag; 193 dest->conv_form = src->conv_form; 194 dest->version = src->version; 195 dest->flags = src->flags; 196 197 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY, &dest->ex_data, 198 &((EC_KEY *)src)->ex_data)) /* XXX const */ 199 return NULL; 200 201 if (src->meth != dest->meth) { 202 #ifndef OPENSSL_NO_ENGINE 203 if (src->engine != NULL && ENGINE_init(src->engine) == 0) 204 return 0; 205 dest->engine = src->engine; 206 #endif 207 dest->meth = src->meth; 208 } 209 210 if (src->meth != NULL && src->meth->copy != NULL && 211 src->meth->copy(dest, src) == 0) 212 return 0; 213 214 return dest; 215 } 216 217 EC_KEY * 218 EC_KEY_dup(const EC_KEY * ec_key) 219 { 220 EC_KEY *ret; 221 222 if ((ret = EC_KEY_new_method(ec_key->engine)) == NULL) 223 return NULL; 224 if (EC_KEY_copy(ret, ec_key) == NULL) { 225 EC_KEY_free(ret); 226 return NULL; 227 } 228 return ret; 229 } 230 231 int 232 EC_KEY_up_ref(EC_KEY * r) 233 { 234 int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_EC); 235 return ((i > 1) ? 1 : 0); 236 } 237 238 int 239 EC_KEY_set_ex_data(EC_KEY *r, int idx, void *arg) 240 { 241 return CRYPTO_set_ex_data(&r->ex_data, idx, arg); 242 } 243 244 void * 245 EC_KEY_get_ex_data(const EC_KEY *r, int idx) 246 { 247 return CRYPTO_get_ex_data(&r->ex_data, idx); 248 } 249 250 int 251 EC_KEY_generate_key(EC_KEY *eckey) 252 { 253 if (eckey->meth->keygen != NULL) 254 return eckey->meth->keygen(eckey); 255 ECerror(EC_R_NOT_IMPLEMENTED); 256 return 0; 257 } 258 259 int 260 ossl_ec_key_gen(EC_KEY *eckey) 261 { 262 int ok = 0; 263 BN_CTX *ctx = NULL; 264 BIGNUM *priv_key = NULL, *order = NULL; 265 EC_POINT *pub_key = NULL; 266 267 if (!eckey || !eckey->group) { 268 ECerror(ERR_R_PASSED_NULL_PARAMETER); 269 return 0; 270 } 271 272 if ((order = BN_new()) == NULL) 273 goto err; 274 if ((ctx = BN_CTX_new()) == NULL) 275 goto err; 276 277 if ((priv_key = eckey->priv_key) == NULL) { 278 if ((priv_key = BN_new()) == NULL) 279 goto err; 280 } 281 282 if (!EC_GROUP_get_order(eckey->group, order, ctx)) 283 goto err; 284 285 if (!bn_rand_interval(priv_key, BN_value_one(), order)) 286 goto err; 287 288 if ((pub_key = eckey->pub_key) == NULL) { 289 if ((pub_key = EC_POINT_new(eckey->group)) == NULL) 290 goto err; 291 } 292 293 if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx)) 294 goto err; 295 296 eckey->priv_key = priv_key; 297 eckey->pub_key = pub_key; 298 299 ok = 1; 300 301 err: 302 BN_free(order); 303 if (eckey->pub_key == NULL) 304 EC_POINT_free(pub_key); 305 if (eckey->priv_key == NULL) 306 BN_free(priv_key); 307 BN_CTX_free(ctx); 308 return (ok); 309 } 310 311 int 312 EC_KEY_check_key(const EC_KEY * eckey) 313 { 314 int ok = 0; 315 BN_CTX *ctx = NULL; 316 const BIGNUM *order = NULL; 317 EC_POINT *point = NULL; 318 319 if (!eckey || !eckey->group || !eckey->pub_key) { 320 ECerror(ERR_R_PASSED_NULL_PARAMETER); 321 return 0; 322 } 323 if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key) > 0) { 324 ECerror(EC_R_POINT_AT_INFINITY); 325 goto err; 326 } 327 if ((ctx = BN_CTX_new()) == NULL) 328 goto err; 329 if ((point = EC_POINT_new(eckey->group)) == NULL) 330 goto err; 331 332 /* testing whether the pub_key is on the elliptic curve */ 333 if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) { 334 ECerror(EC_R_POINT_IS_NOT_ON_CURVE); 335 goto err; 336 } 337 /* testing whether pub_key * order is the point at infinity */ 338 order = &eckey->group->order; 339 if (BN_is_zero(order)) { 340 ECerror(EC_R_INVALID_GROUP_ORDER); 341 goto err; 342 } 343 if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) { 344 ECerror(ERR_R_EC_LIB); 345 goto err; 346 } 347 if (EC_POINT_is_at_infinity(eckey->group, point) <= 0) { 348 ECerror(EC_R_WRONG_ORDER); 349 goto err; 350 } 351 /* 352 * in case the priv_key is present : check if generator * priv_key == 353 * pub_key 354 */ 355 if (eckey->priv_key) { 356 if (BN_cmp(eckey->priv_key, order) >= 0) { 357 ECerror(EC_R_WRONG_ORDER); 358 goto err; 359 } 360 if (!EC_POINT_mul(eckey->group, point, eckey->priv_key, 361 NULL, NULL, ctx)) { 362 ECerror(ERR_R_EC_LIB); 363 goto err; 364 } 365 if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, 366 ctx) != 0) { 367 ECerror(EC_R_INVALID_PRIVATE_KEY); 368 goto err; 369 } 370 } 371 ok = 1; 372 err: 373 BN_CTX_free(ctx); 374 EC_POINT_free(point); 375 return (ok); 376 } 377 378 int 379 EC_KEY_set_public_key_affine_coordinates(EC_KEY * key, BIGNUM * x, BIGNUM * y) 380 { 381 BN_CTX *ctx = NULL; 382 BIGNUM *tx, *ty; 383 EC_POINT *point = NULL; 384 int ok = 0; 385 386 if (!key || !key->group || !x || !y) { 387 ECerror(ERR_R_PASSED_NULL_PARAMETER); 388 return 0; 389 } 390 ctx = BN_CTX_new(); 391 if (!ctx) 392 goto err; 393 394 point = EC_POINT_new(key->group); 395 396 if (!point) 397 goto err; 398 399 if ((tx = BN_CTX_get(ctx)) == NULL) 400 goto err; 401 if ((ty = BN_CTX_get(ctx)) == NULL) 402 goto err; 403 404 if (!EC_POINT_set_affine_coordinates(key->group, point, x, y, ctx)) 405 goto err; 406 if (!EC_POINT_get_affine_coordinates(key->group, point, tx, ty, ctx)) 407 goto err; 408 /* 409 * Check if retrieved coordinates match originals: if not values are 410 * out of range. 411 */ 412 if (BN_cmp(x, tx) || BN_cmp(y, ty)) { 413 ECerror(EC_R_COORDINATES_OUT_OF_RANGE); 414 goto err; 415 } 416 if (!EC_KEY_set_public_key(key, point)) 417 goto err; 418 419 if (EC_KEY_check_key(key) == 0) 420 goto err; 421 422 ok = 1; 423 424 err: 425 BN_CTX_free(ctx); 426 EC_POINT_free(point); 427 return ok; 428 429 } 430 431 const EC_GROUP * 432 EC_KEY_get0_group(const EC_KEY * key) 433 { 434 return key->group; 435 } 436 437 int 438 EC_KEY_set_group(EC_KEY * key, const EC_GROUP * group) 439 { 440 if (key->meth->set_group != NULL && 441 key->meth->set_group(key, group) == 0) 442 return 0; 443 EC_GROUP_free(key->group); 444 key->group = EC_GROUP_dup(group); 445 return (key->group == NULL) ? 0 : 1; 446 } 447 448 const BIGNUM * 449 EC_KEY_get0_private_key(const EC_KEY * key) 450 { 451 return key->priv_key; 452 } 453 454 int 455 EC_KEY_set_private_key(EC_KEY * key, const BIGNUM * priv_key) 456 { 457 if (key->meth->set_private != NULL && 458 key->meth->set_private(key, priv_key) == 0) 459 return 0; 460 BN_clear_free(key->priv_key); 461 key->priv_key = BN_dup(priv_key); 462 return (key->priv_key == NULL) ? 0 : 1; 463 } 464 465 const EC_POINT * 466 EC_KEY_get0_public_key(const EC_KEY * key) 467 { 468 return key->pub_key; 469 } 470 471 int 472 EC_KEY_set_public_key(EC_KEY * key, const EC_POINT * pub_key) 473 { 474 if (key->meth->set_public != NULL && 475 key->meth->set_public(key, pub_key) == 0) 476 return 0; 477 EC_POINT_free(key->pub_key); 478 key->pub_key = EC_POINT_dup(pub_key, key->group); 479 return (key->pub_key == NULL) ? 0 : 1; 480 } 481 482 unsigned int 483 EC_KEY_get_enc_flags(const EC_KEY * key) 484 { 485 return key->enc_flag; 486 } 487 488 void 489 EC_KEY_set_enc_flags(EC_KEY * key, unsigned int flags) 490 { 491 key->enc_flag = flags; 492 } 493 494 point_conversion_form_t 495 EC_KEY_get_conv_form(const EC_KEY * key) 496 { 497 return key->conv_form; 498 } 499 500 void 501 EC_KEY_set_conv_form(EC_KEY * key, point_conversion_form_t cform) 502 { 503 key->conv_form = cform; 504 if (key->group != NULL) 505 EC_GROUP_set_point_conversion_form(key->group, cform); 506 } 507 508 void * 509 EC_KEY_get_key_method_data(EC_KEY *key, 510 void *(*dup_func) (void *), 511 void (*free_func) (void *), 512 void (*clear_free_func) (void *)) 513 { 514 void *ret; 515 516 CRYPTO_r_lock(CRYPTO_LOCK_EC); 517 ret = EC_EX_DATA_get_data(key->method_data, dup_func, free_func, clear_free_func); 518 CRYPTO_r_unlock(CRYPTO_LOCK_EC); 519 520 return ret; 521 } 522 523 void * 524 EC_KEY_insert_key_method_data(EC_KEY * key, void *data, 525 void *(*dup_func) (void *), 526 void (*free_func) (void *), 527 void (*clear_free_func) (void *)) 528 { 529 EC_EXTRA_DATA *ex_data; 530 531 CRYPTO_w_lock(CRYPTO_LOCK_EC); 532 ex_data = EC_EX_DATA_get_data(key->method_data, dup_func, free_func, clear_free_func); 533 if (ex_data == NULL) 534 EC_EX_DATA_set_data(&key->method_data, data, dup_func, free_func, clear_free_func); 535 CRYPTO_w_unlock(CRYPTO_LOCK_EC); 536 537 return ex_data; 538 } 539 540 void 541 EC_KEY_set_asn1_flag(EC_KEY * key, int flag) 542 { 543 if (key->group != NULL) 544 EC_GROUP_set_asn1_flag(key->group, flag); 545 } 546 547 int 548 EC_KEY_precompute_mult(EC_KEY * key, BN_CTX * ctx) 549 { 550 if (key->group == NULL) 551 return 0; 552 return EC_GROUP_precompute_mult(key->group, ctx); 553 } 554 555 int 556 EC_KEY_get_flags(const EC_KEY * key) 557 { 558 return key->flags; 559 } 560 561 void 562 EC_KEY_set_flags(EC_KEY * key, int flags) 563 { 564 key->flags |= flags; 565 } 566 567 void 568 EC_KEY_clear_flags(EC_KEY * key, int flags) 569 { 570 key->flags &= ~flags; 571 } 572