1 /* $OpenBSD: ec_key.c,v 1.24 2019/01/19 01:12:48 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, tmp_nid, is_char_two = 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 tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(key->group)); 400 401 if (tmp_nid == NID_X9_62_characteristic_two_field) 402 is_char_two = 1; 403 404 if ((tx = BN_CTX_get(ctx)) == NULL) 405 goto err; 406 if ((ty = BN_CTX_get(ctx)) == NULL) 407 goto err; 408 409 #ifndef OPENSSL_NO_EC2M 410 if (is_char_two) { 411 if (!EC_POINT_set_affine_coordinates_GF2m(key->group, point, 412 x, y, ctx)) 413 goto err; 414 if (!EC_POINT_get_affine_coordinates_GF2m(key->group, point, 415 tx, ty, ctx)) 416 goto err; 417 } else 418 #endif 419 { 420 if (!EC_POINT_set_affine_coordinates_GFp(key->group, point, 421 x, y, ctx)) 422 goto err; 423 if (!EC_POINT_get_affine_coordinates_GFp(key->group, point, 424 tx, ty, ctx)) 425 goto err; 426 } 427 /* 428 * Check if retrieved coordinates match originals: if not values are 429 * out of range. 430 */ 431 if (BN_cmp(x, tx) || BN_cmp(y, ty)) { 432 ECerror(EC_R_COORDINATES_OUT_OF_RANGE); 433 goto err; 434 } 435 if (!EC_KEY_set_public_key(key, point)) 436 goto err; 437 438 if (EC_KEY_check_key(key) == 0) 439 goto err; 440 441 ok = 1; 442 443 err: 444 BN_CTX_free(ctx); 445 EC_POINT_free(point); 446 return ok; 447 448 } 449 450 const EC_GROUP * 451 EC_KEY_get0_group(const EC_KEY * key) 452 { 453 return key->group; 454 } 455 456 int 457 EC_KEY_set_group(EC_KEY * key, const EC_GROUP * group) 458 { 459 if (key->meth->set_group != NULL && 460 key->meth->set_group(key, group) == 0) 461 return 0; 462 EC_GROUP_free(key->group); 463 key->group = EC_GROUP_dup(group); 464 return (key->group == NULL) ? 0 : 1; 465 } 466 467 const BIGNUM * 468 EC_KEY_get0_private_key(const EC_KEY * key) 469 { 470 return key->priv_key; 471 } 472 473 int 474 EC_KEY_set_private_key(EC_KEY * key, const BIGNUM * priv_key) 475 { 476 if (key->meth->set_private != NULL && 477 key->meth->set_private(key, priv_key) == 0) 478 return 0; 479 BN_clear_free(key->priv_key); 480 key->priv_key = BN_dup(priv_key); 481 return (key->priv_key == NULL) ? 0 : 1; 482 } 483 484 const EC_POINT * 485 EC_KEY_get0_public_key(const EC_KEY * key) 486 { 487 return key->pub_key; 488 } 489 490 int 491 EC_KEY_set_public_key(EC_KEY * key, const EC_POINT * pub_key) 492 { 493 if (key->meth->set_public != NULL && 494 key->meth->set_public(key, pub_key) == 0) 495 return 0; 496 EC_POINT_free(key->pub_key); 497 key->pub_key = EC_POINT_dup(pub_key, key->group); 498 return (key->pub_key == NULL) ? 0 : 1; 499 } 500 501 unsigned int 502 EC_KEY_get_enc_flags(const EC_KEY * key) 503 { 504 return key->enc_flag; 505 } 506 507 void 508 EC_KEY_set_enc_flags(EC_KEY * key, unsigned int flags) 509 { 510 key->enc_flag = flags; 511 } 512 513 point_conversion_form_t 514 EC_KEY_get_conv_form(const EC_KEY * key) 515 { 516 return key->conv_form; 517 } 518 519 void 520 EC_KEY_set_conv_form(EC_KEY * key, point_conversion_form_t cform) 521 { 522 key->conv_form = cform; 523 if (key->group != NULL) 524 EC_GROUP_set_point_conversion_form(key->group, cform); 525 } 526 527 void * 528 EC_KEY_get_key_method_data(EC_KEY *key, 529 void *(*dup_func) (void *), 530 void (*free_func) (void *), 531 void (*clear_free_func) (void *)) 532 { 533 void *ret; 534 535 CRYPTO_r_lock(CRYPTO_LOCK_EC); 536 ret = EC_EX_DATA_get_data(key->method_data, dup_func, free_func, clear_free_func); 537 CRYPTO_r_unlock(CRYPTO_LOCK_EC); 538 539 return ret; 540 } 541 542 void * 543 EC_KEY_insert_key_method_data(EC_KEY * key, void *data, 544 void *(*dup_func) (void *), 545 void (*free_func) (void *), 546 void (*clear_free_func) (void *)) 547 { 548 EC_EXTRA_DATA *ex_data; 549 550 CRYPTO_w_lock(CRYPTO_LOCK_EC); 551 ex_data = EC_EX_DATA_get_data(key->method_data, dup_func, free_func, clear_free_func); 552 if (ex_data == NULL) 553 EC_EX_DATA_set_data(&key->method_data, data, dup_func, free_func, clear_free_func); 554 CRYPTO_w_unlock(CRYPTO_LOCK_EC); 555 556 return ex_data; 557 } 558 559 void 560 EC_KEY_set_asn1_flag(EC_KEY * key, int flag) 561 { 562 if (key->group != NULL) 563 EC_GROUP_set_asn1_flag(key->group, flag); 564 } 565 566 int 567 EC_KEY_precompute_mult(EC_KEY * key, BN_CTX * ctx) 568 { 569 if (key->group == NULL) 570 return 0; 571 return EC_GROUP_precompute_mult(key->group, ctx); 572 } 573 574 int 575 EC_KEY_get_flags(const EC_KEY * key) 576 { 577 return key->flags; 578 } 579 580 void 581 EC_KEY_set_flags(EC_KEY * key, int flags) 582 { 583 key->flags |= flags; 584 } 585 586 void 587 EC_KEY_clear_flags(EC_KEY * key, int flags) 588 { 589 key->flags &= ~flags; 590 } 591