1 /* $OpenBSD: ec_key.c,v 1.37 2023/08/03 18:53:56 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_local.h" 74 #include "ec_local.h" 75 76 EC_KEY * 77 EC_KEY_new(void) 78 { 79 return EC_KEY_new_method(NULL); 80 } 81 LCRYPTO_ALIAS(EC_KEY_new); 82 83 EC_KEY * 84 EC_KEY_new_by_curve_name(int nid) 85 { 86 EC_KEY *ret = EC_KEY_new(); 87 if (ret == NULL) 88 return NULL; 89 ret->group = EC_GROUP_new_by_curve_name(nid); 90 if (ret->group == NULL) { 91 EC_KEY_free(ret); 92 return NULL; 93 } 94 if (ret->meth->set_group != NULL && 95 ret->meth->set_group(ret, ret->group) == 0) { 96 EC_KEY_free(ret); 97 return NULL; 98 } 99 return ret; 100 } 101 LCRYPTO_ALIAS(EC_KEY_new_by_curve_name); 102 103 void 104 EC_KEY_free(EC_KEY *r) 105 { 106 int i; 107 108 if (r == NULL) 109 return; 110 111 i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_EC); 112 if (i > 0) 113 return; 114 115 if (r->meth != NULL && r->meth->finish != NULL) 116 r->meth->finish(r); 117 118 #ifndef OPENSSL_NO_ENGINE 119 ENGINE_finish(r->engine); 120 #endif 121 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data); 122 123 EC_GROUP_free(r->group); 124 EC_POINT_free(r->pub_key); 125 BN_free(r->priv_key); 126 127 freezero(r, sizeof(EC_KEY)); 128 } 129 LCRYPTO_ALIAS(EC_KEY_free); 130 131 EC_KEY * 132 EC_KEY_copy(EC_KEY *dest, const EC_KEY *src) 133 { 134 if (dest == NULL || src == NULL) { 135 ECerror(ERR_R_PASSED_NULL_PARAMETER); 136 return NULL; 137 } 138 if (src->meth != dest->meth) { 139 if (dest->meth != NULL && dest->meth->finish != NULL) 140 dest->meth->finish(dest); 141 #ifndef OPENSSL_NO_ENGINE 142 if (ENGINE_finish(dest->engine) == 0) 143 return 0; 144 dest->engine = NULL; 145 #endif 146 } 147 /* copy the parameters */ 148 if (src->group) { 149 const EC_METHOD *meth = EC_GROUP_method_of(src->group); 150 /* clear the old group */ 151 EC_GROUP_free(dest->group); 152 dest->group = EC_GROUP_new(meth); 153 if (dest->group == NULL) 154 return NULL; 155 if (!EC_GROUP_copy(dest->group, src->group)) 156 return NULL; 157 } 158 /* copy the public key */ 159 if (src->pub_key && src->group) { 160 EC_POINT_free(dest->pub_key); 161 dest->pub_key = EC_POINT_new(src->group); 162 if (dest->pub_key == NULL) 163 return NULL; 164 if (!EC_POINT_copy(dest->pub_key, src->pub_key)) 165 return NULL; 166 } 167 /* copy the private key */ 168 if (src->priv_key) { 169 if (dest->priv_key == NULL) { 170 dest->priv_key = BN_new(); 171 if (dest->priv_key == NULL) 172 return NULL; 173 } 174 if (!bn_copy(dest->priv_key, src->priv_key)) 175 return NULL; 176 } 177 178 /* copy the rest */ 179 dest->enc_flag = src->enc_flag; 180 dest->conv_form = src->conv_form; 181 dest->version = src->version; 182 dest->flags = src->flags; 183 184 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY, &dest->ex_data, 185 &((EC_KEY *)src)->ex_data)) /* XXX const */ 186 return NULL; 187 188 if (src->meth != dest->meth) { 189 #ifndef OPENSSL_NO_ENGINE 190 if (src->engine != NULL && ENGINE_init(src->engine) == 0) 191 return 0; 192 dest->engine = src->engine; 193 #endif 194 dest->meth = src->meth; 195 } 196 197 if (src->meth != NULL && src->meth->copy != NULL && 198 src->meth->copy(dest, src) == 0) 199 return 0; 200 201 return dest; 202 } 203 LCRYPTO_ALIAS(EC_KEY_copy); 204 205 EC_KEY * 206 EC_KEY_dup(const EC_KEY *ec_key) 207 { 208 EC_KEY *ret; 209 210 if ((ret = EC_KEY_new_method(ec_key->engine)) == NULL) 211 return NULL; 212 if (EC_KEY_copy(ret, ec_key) == NULL) { 213 EC_KEY_free(ret); 214 return NULL; 215 } 216 return ret; 217 } 218 LCRYPTO_ALIAS(EC_KEY_dup); 219 220 int 221 EC_KEY_up_ref(EC_KEY *r) 222 { 223 int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_EC); 224 return ((i > 1) ? 1 : 0); 225 } 226 LCRYPTO_ALIAS(EC_KEY_up_ref); 227 228 int 229 EC_KEY_set_ex_data(EC_KEY *r, int idx, void *arg) 230 { 231 return CRYPTO_set_ex_data(&r->ex_data, idx, arg); 232 } 233 LCRYPTO_ALIAS(EC_KEY_set_ex_data); 234 235 void * 236 EC_KEY_get_ex_data(const EC_KEY *r, int idx) 237 { 238 return CRYPTO_get_ex_data(&r->ex_data, idx); 239 } 240 LCRYPTO_ALIAS(EC_KEY_get_ex_data); 241 242 int 243 EC_KEY_generate_key(EC_KEY *eckey) 244 { 245 if (eckey->meth->keygen != NULL) 246 return eckey->meth->keygen(eckey); 247 ECerror(EC_R_NOT_IMPLEMENTED); 248 return 0; 249 } 250 LCRYPTO_ALIAS(EC_KEY_generate_key); 251 252 int 253 ec_key_gen(EC_KEY *eckey) 254 { 255 BIGNUM *priv_key = NULL; 256 EC_POINT *pub_key = NULL; 257 const BIGNUM *order; 258 int ret = 0; 259 260 if (eckey == NULL || eckey->group == NULL) { 261 ECerror(ERR_R_PASSED_NULL_PARAMETER); 262 goto err; 263 } 264 265 if ((priv_key = BN_new()) == NULL) 266 goto err; 267 if ((pub_key = EC_POINT_new(eckey->group)) == NULL) 268 goto err; 269 270 if ((order = EC_GROUP_get0_order(eckey->group)) == NULL) 271 goto err; 272 if (!bn_rand_interval(priv_key, 1, order)) 273 goto err; 274 if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, NULL)) 275 goto err; 276 277 BN_free(eckey->priv_key); 278 eckey->priv_key = priv_key; 279 priv_key = NULL; 280 281 EC_POINT_free(eckey->pub_key); 282 eckey->pub_key = pub_key; 283 pub_key = NULL; 284 285 ret = 1; 286 287 err: 288 EC_POINT_free(pub_key); 289 BN_free(priv_key); 290 291 return ret; 292 } 293 294 int 295 EC_KEY_check_key(const EC_KEY *eckey) 296 { 297 BN_CTX *ctx = NULL; 298 EC_POINT *point = NULL; 299 const BIGNUM *order; 300 int ret = 0; 301 302 if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) { 303 ECerror(ERR_R_PASSED_NULL_PARAMETER); 304 goto err; 305 } 306 307 if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key) > 0) { 308 ECerror(EC_R_POINT_AT_INFINITY); 309 goto err; 310 } 311 312 if ((ctx = BN_CTX_new()) == NULL) 313 goto err; 314 315 if ((point = EC_POINT_new(eckey->group)) == NULL) 316 goto err; 317 318 /* Ensure public key is on the elliptic curve. */ 319 if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) { 320 ECerror(EC_R_POINT_IS_NOT_ON_CURVE); 321 goto err; 322 } 323 324 /* Ensure public key multiplied by the order is the point at infinity. */ 325 if ((order = EC_GROUP_get0_order(eckey->group)) == NULL) { 326 ECerror(EC_R_INVALID_GROUP_ORDER); 327 goto err; 328 } 329 if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) { 330 ECerror(ERR_R_EC_LIB); 331 goto err; 332 } 333 if (EC_POINT_is_at_infinity(eckey->group, point) <= 0) { 334 ECerror(EC_R_WRONG_ORDER); 335 goto err; 336 } 337 338 /* 339 * If the private key is present, ensure that the private key multiplied 340 * by the generator matches the public key. 341 */ 342 if (eckey->priv_key != NULL) { 343 if (BN_cmp(eckey->priv_key, order) >= 0) { 344 ECerror(EC_R_WRONG_ORDER); 345 goto err; 346 } 347 if (!EC_POINT_mul(eckey->group, point, eckey->priv_key, NULL, 348 NULL, ctx)) { 349 ECerror(ERR_R_EC_LIB); 350 goto err; 351 } 352 if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, 353 ctx) != 0) { 354 ECerror(EC_R_INVALID_PRIVATE_KEY); 355 goto err; 356 } 357 } 358 359 ret = 1; 360 361 err: 362 BN_CTX_free(ctx); 363 EC_POINT_free(point); 364 365 return ret; 366 } 367 LCRYPTO_ALIAS(EC_KEY_check_key); 368 369 int 370 EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x, BIGNUM *y) 371 { 372 BN_CTX *ctx = NULL; 373 EC_POINT *point = NULL; 374 BIGNUM *tx, *ty; 375 int ret = 0; 376 377 if (key == NULL || key->group == NULL || x == NULL || y == NULL) { 378 ECerror(ERR_R_PASSED_NULL_PARAMETER); 379 goto err; 380 } 381 382 if ((ctx = BN_CTX_new()) == NULL) 383 goto err; 384 385 BN_CTX_start(ctx); 386 387 if ((tx = BN_CTX_get(ctx)) == NULL) 388 goto err; 389 if ((ty = BN_CTX_get(ctx)) == NULL) 390 goto err; 391 392 if ((point = EC_POINT_new(key->group)) == NULL) 393 goto err; 394 395 if (!EC_POINT_set_affine_coordinates(key->group, point, x, y, ctx)) 396 goto err; 397 if (!EC_POINT_get_affine_coordinates(key->group, point, tx, ty, ctx)) 398 goto err; 399 400 /* 401 * Check if retrieved coordinates match originals: if not values are 402 * out of range. 403 */ 404 if (BN_cmp(x, tx) != 0 || BN_cmp(y, ty) != 0) { 405 ECerror(EC_R_COORDINATES_OUT_OF_RANGE); 406 goto err; 407 } 408 if (!EC_KEY_set_public_key(key, point)) 409 goto err; 410 if (EC_KEY_check_key(key) == 0) 411 goto err; 412 413 ret = 1; 414 415 err: 416 BN_CTX_end(ctx); 417 BN_CTX_free(ctx); 418 EC_POINT_free(point); 419 420 return ret; 421 } 422 LCRYPTO_ALIAS(EC_KEY_set_public_key_affine_coordinates); 423 424 const EC_GROUP * 425 EC_KEY_get0_group(const EC_KEY *key) 426 { 427 return key->group; 428 } 429 LCRYPTO_ALIAS(EC_KEY_get0_group); 430 431 int 432 EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group) 433 { 434 if (key->meth->set_group != NULL && 435 key->meth->set_group(key, group) == 0) 436 return 0; 437 EC_GROUP_free(key->group); 438 key->group = EC_GROUP_dup(group); 439 return (key->group == NULL) ? 0 : 1; 440 } 441 LCRYPTO_ALIAS(EC_KEY_set_group); 442 443 const BIGNUM * 444 EC_KEY_get0_private_key(const EC_KEY *key) 445 { 446 return key->priv_key; 447 } 448 LCRYPTO_ALIAS(EC_KEY_get0_private_key); 449 450 int 451 EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key) 452 { 453 if (key->meth->set_private != NULL && 454 key->meth->set_private(key, priv_key) == 0) 455 return 0; 456 457 BN_free(key->priv_key); 458 if ((key->priv_key = BN_dup(priv_key)) == NULL) 459 return 0; 460 461 return 1; 462 } 463 LCRYPTO_ALIAS(EC_KEY_set_private_key); 464 465 const EC_POINT * 466 EC_KEY_get0_public_key(const EC_KEY *key) 467 { 468 return key->pub_key; 469 } 470 LCRYPTO_ALIAS(EC_KEY_get0_public_key); 471 472 int 473 EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key) 474 { 475 if (key->meth->set_public != NULL && 476 key->meth->set_public(key, pub_key) == 0) 477 return 0; 478 479 EC_POINT_free(key->pub_key); 480 if ((key->pub_key = EC_POINT_dup(pub_key, key->group)) == NULL) 481 return 0; 482 483 return 1; 484 } 485 LCRYPTO_ALIAS(EC_KEY_set_public_key); 486 487 unsigned int 488 EC_KEY_get_enc_flags(const EC_KEY *key) 489 { 490 return key->enc_flag; 491 } 492 LCRYPTO_ALIAS(EC_KEY_get_enc_flags); 493 494 void 495 EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags) 496 { 497 key->enc_flag = flags; 498 } 499 LCRYPTO_ALIAS(EC_KEY_set_enc_flags); 500 501 point_conversion_form_t 502 EC_KEY_get_conv_form(const EC_KEY *key) 503 { 504 return key->conv_form; 505 } 506 LCRYPTO_ALIAS(EC_KEY_get_conv_form); 507 508 void 509 EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform) 510 { 511 key->conv_form = cform; 512 if (key->group != NULL) 513 EC_GROUP_set_point_conversion_form(key->group, cform); 514 } 515 LCRYPTO_ALIAS(EC_KEY_set_conv_form); 516 517 void 518 EC_KEY_set_asn1_flag(EC_KEY *key, int flag) 519 { 520 if (key->group != NULL) 521 EC_GROUP_set_asn1_flag(key->group, flag); 522 } 523 LCRYPTO_ALIAS(EC_KEY_set_asn1_flag); 524 525 int 526 EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx) 527 { 528 if (key->group == NULL) 529 return 0; 530 return EC_GROUP_precompute_mult(key->group, ctx); 531 } 532 LCRYPTO_ALIAS(EC_KEY_precompute_mult); 533 534 int 535 EC_KEY_get_flags(const EC_KEY *key) 536 { 537 return key->flags; 538 } 539 LCRYPTO_ALIAS(EC_KEY_get_flags); 540 541 void 542 EC_KEY_set_flags(EC_KEY *key, int flags) 543 { 544 key->flags |= flags; 545 } 546 LCRYPTO_ALIAS(EC_KEY_set_flags); 547 548 void 549 EC_KEY_clear_flags(EC_KEY *key, int flags) 550 { 551 key->flags &= ~flags; 552 } 553 LCRYPTO_ALIAS(EC_KEY_clear_flags); 554