2 * Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
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
11 #include "internal/cryptlib.h"
14 #include "internal/refcount.h"
15 #include <openssl/err.h>
16 #include <openssl/engine.h>
17 #include "crypto/bn.h"
19 EC_KEY *EC_KEY_new(void)
21 return EC_KEY_new_method(NULL);
24 EC_KEY *EC_KEY_new_by_curve_name(int nid)
26 EC_KEY *ret = EC_KEY_new();
29 ret->group = EC_GROUP_new_by_curve_name(nid);
30 if (ret->group == NULL) {
34 if (ret->meth->set_group != NULL
35 && ret->meth->set_group(ret, ret->group) == 0) {
42 void EC_KEY_free(EC_KEY *r)
49 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
50 REF_PRINT_COUNT("EC_KEY", r);
53 REF_ASSERT_ISNT(i < 0);
55 if (r->meth != NULL && r->meth->finish != NULL)
58 #ifndef OPENSSL_NO_ENGINE
59 ENGINE_finish(r->engine);
62 if (r->group && r->group->meth->keyfinish)
63 r->group->meth->keyfinish(r);
65 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
66 CRYPTO_THREAD_lock_free(r->lock);
67 EC_GROUP_free(r->group);
68 EC_POINT_free(r->pub_key);
69 BN_clear_free(r->priv_key);
71 OPENSSL_clear_free((void *)r, sizeof(EC_KEY));
74 EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
76 if (dest == NULL || src == NULL) {
77 ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
80 if (src->meth != dest->meth) {
81 if (dest->meth->finish != NULL)
82 dest->meth->finish(dest);
83 if (dest->group && dest->group->meth->keyfinish)
84 dest->group->meth->keyfinish(dest);
85 #ifndef OPENSSL_NO_ENGINE
86 if (ENGINE_finish(dest->engine) == 0)
91 /* copy the parameters */
92 if (src->group != NULL) {
93 const EC_METHOD *meth = EC_GROUP_method_of(src->group);
94 /* clear the old group */
95 EC_GROUP_free(dest->group);
96 dest->group = EC_GROUP_new(meth);
97 if (dest->group == NULL)
99 if (!EC_GROUP_copy(dest->group, src->group))
102 /* copy the public key */
103 if (src->pub_key != NULL) {
104 EC_POINT_free(dest->pub_key);
105 dest->pub_key = EC_POINT_new(src->group);
106 if (dest->pub_key == NULL)
108 if (!EC_POINT_copy(dest->pub_key, src->pub_key))
111 /* copy the private key */
112 if (src->priv_key != NULL) {
113 if (dest->priv_key == NULL) {
114 dest->priv_key = BN_new();
115 if (dest->priv_key == NULL)
118 if (!BN_copy(dest->priv_key, src->priv_key))
120 if (src->group->meth->keycopy
121 && src->group->meth->keycopy(dest, src) == 0)
128 dest->enc_flag = src->enc_flag;
129 dest->conv_form = src->conv_form;
130 dest->version = src->version;
131 dest->flags = src->flags;
132 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
133 &dest->ex_data, &src->ex_data))
136 if (src->meth != dest->meth) {
137 #ifndef OPENSSL_NO_ENGINE
138 if (src->engine != NULL && ENGINE_init(src->engine) == 0)
140 dest->engine = src->engine;
142 dest->meth = src->meth;
145 if (src->meth->copy != NULL && src->meth->copy(dest, src) == 0)
151 EC_KEY *EC_KEY_dup(const EC_KEY *ec_key)
153 EC_KEY *ret = EC_KEY_new_method(ec_key->engine);
158 if (EC_KEY_copy(ret, ec_key) == NULL) {
165 int EC_KEY_up_ref(EC_KEY *r)
169 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
172 REF_PRINT_COUNT("EC_KEY", r);
173 REF_ASSERT_ISNT(i < 2);
174 return ((i > 1) ? 1 : 0);
177 ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey)
179 return eckey->engine;
182 int EC_KEY_generate_key(EC_KEY *eckey)
184 if (eckey == NULL || eckey->group == NULL) {
185 ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
188 if (eckey->meth->keygen != NULL)
189 return eckey->meth->keygen(eckey);
190 ECerr(EC_F_EC_KEY_GENERATE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
194 int ossl_ec_key_gen(EC_KEY *eckey)
196 return eckey->group->meth->keygen(eckey);
199 int ec_key_simple_generate_key(EC_KEY *eckey)
203 BIGNUM *priv_key = NULL;
204 const BIGNUM *order = NULL;
205 EC_POINT *pub_key = NULL;
207 if ((ctx = BN_CTX_new()) == NULL)
210 if (eckey->priv_key == NULL) {
212 if (priv_key == NULL)
215 priv_key = eckey->priv_key;
217 order = EC_GROUP_get0_order(eckey->group);
222 if (!BN_priv_rand_range(priv_key, order))
224 while (BN_is_zero(priv_key)) ;
226 if (eckey->pub_key == NULL) {
227 pub_key = EC_POINT_new(eckey->group);
231 pub_key = eckey->pub_key;
233 if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx))
236 eckey->priv_key = priv_key;
237 eckey->pub_key = pub_key;
242 if (eckey->pub_key == NULL)
243 EC_POINT_free(pub_key);
244 if (eckey->priv_key != priv_key)
250 int ec_key_simple_generate_public_key(EC_KEY *eckey)
252 return EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
256 int EC_KEY_check_key(const EC_KEY *eckey)
258 if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
259 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
263 if (eckey->group->meth->keycheck == NULL) {
264 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
268 return eckey->group->meth->keycheck(eckey);
271 int ec_key_simple_check_key(const EC_KEY *eckey)
275 const BIGNUM *order = NULL;
276 EC_POINT *point = NULL;
278 if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
279 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
283 if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
284 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_AT_INFINITY);
288 if ((ctx = BN_CTX_new()) == NULL)
290 if ((point = EC_POINT_new(eckey->group)) == NULL)
293 /* testing whether the pub_key is on the elliptic curve */
294 if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
295 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
298 /* testing whether pub_key * order is the point at infinity */
299 order = eckey->group->order;
300 if (BN_is_zero(order)) {
301 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
304 if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
305 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
308 if (!EC_POINT_is_at_infinity(eckey->group, point)) {
309 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
313 * in case the priv_key is present : check if generator * priv_key ==
316 if (eckey->priv_key != NULL) {
317 if (BN_cmp(eckey->priv_key, order) >= 0) {
318 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
321 if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
323 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
326 if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
327 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
334 EC_POINT_free(point);
338 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
343 EC_POINT *point = NULL;
346 if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
347 ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
348 ERR_R_PASSED_NULL_PARAMETER);
356 point = EC_POINT_new(key->group);
361 tx = BN_CTX_get(ctx);
362 ty = BN_CTX_get(ctx);
366 if (!EC_POINT_set_affine_coordinates(key->group, point, x, y, ctx))
368 if (!EC_POINT_get_affine_coordinates(key->group, point, tx, ty, ctx))
372 * Check if retrieved coordinates match originals and are less than field
373 * order: if not values are out of range.
375 if (BN_cmp(x, tx) || BN_cmp(y, ty)
376 || (BN_cmp(x, key->group->field) >= 0)
377 || (BN_cmp(y, key->group->field) >= 0)) {
378 ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
379 EC_R_COORDINATES_OUT_OF_RANGE);
383 if (!EC_KEY_set_public_key(key, point))
386 if (EC_KEY_check_key(key) == 0)
394 EC_POINT_free(point);
399 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
404 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
406 if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)
408 EC_GROUP_free(key->group);
409 key->group = EC_GROUP_dup(group);
410 return (key->group == NULL) ? 0 : 1;
413 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
415 return key->priv_key;
418 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
421 const BIGNUM *order = NULL;
422 BIGNUM *tmp_key = NULL;
424 if (key->group == NULL || key->group->meth == NULL)
428 * Not only should key->group be set, but it should also be in a valid
429 * fully initialized state.
431 * Specifically, to operate in constant time, we need that the group order
432 * is set, as we use its length as the fixed public size of any scalar used
433 * as an EC private key.
435 order = EC_GROUP_get0_order(key->group);
436 if (order == NULL || BN_is_zero(order))
437 return 0; /* This should never happen */
439 if (key->group->meth->set_private != NULL
440 && key->group->meth->set_private(key, priv_key) == 0)
442 if (key->meth->set_private != NULL
443 && key->meth->set_private(key, priv_key) == 0)
447 * We should never leak the bit length of the secret scalar in the key,
448 * so we always set the `BN_FLG_CONSTTIME` flag on the internal `BIGNUM`
449 * holding the secret scalar.
451 * This is important also because `BN_dup()` (and `BN_copy()`) do not
452 * propagate the `BN_FLG_CONSTTIME` flag from the source `BIGNUM`, and
453 * this brings an extra risk of inadvertently losing the flag, even when
454 * the caller specifically set it.
456 * The propagation has been turned on and off a few times in the past
457 * years because in some conditions has shown unintended consequences in
458 * some code paths, so at the moment we can't fix this in the BN layer.
460 * In `EC_KEY_set_private_key()` we can work around the propagation by
461 * manually setting the flag after `BN_dup()` as we know for sure that
462 * inside the EC module the `BN_FLG_CONSTTIME` is always treated
463 * correctly and should not generate unintended consequences.
465 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
466 * to preallocate the BIGNUM internal buffer to a fixed public size big
467 * enough that operations performed during the processing never trigger
468 * a realloc which would leak the size of the scalar through memory
474 * The order of the large prime subgroup of the curve is our choice for
475 * a fixed public size, as that is generally the upper bound for
476 * generating a private key in EC cryptosystems and should fit all valid
479 * For preallocating the BIGNUM storage we look at the number of "words"
480 * required for the internal representation of the order, and we
481 * preallocate 2 extra "words" in case any of the subsequent processing
482 * might temporarily overflow the order length.
484 tmp_key = BN_dup(priv_key);
488 BN_set_flags(tmp_key, BN_FLG_CONSTTIME);
490 fixed_top = bn_get_top(order) + 2;
491 if (bn_wexpand(tmp_key, fixed_top) == NULL) {
492 BN_clear_free(tmp_key);
496 BN_clear_free(key->priv_key);
497 key->priv_key = tmp_key;
502 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
507 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
509 if (key->meth->set_public != NULL
510 && key->meth->set_public(key, pub_key) == 0)
512 EC_POINT_free(key->pub_key);
513 key->pub_key = EC_POINT_dup(pub_key, key->group);
514 return (key->pub_key == NULL) ? 0 : 1;
517 unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
519 return key->enc_flag;
522 void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
524 key->enc_flag = flags;
527 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
529 return key->conv_form;
532 void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
534 key->conv_form = cform;
535 if (key->group != NULL)
536 EC_GROUP_set_point_conversion_form(key->group, cform);
539 void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
541 if (key->group != NULL)
542 EC_GROUP_set_asn1_flag(key->group, flag);
545 int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
547 if (key->group == NULL)
549 return EC_GROUP_precompute_mult(key->group, ctx);
552 int EC_KEY_get_flags(const EC_KEY *key)
557 void EC_KEY_set_flags(EC_KEY *key, int flags)
562 void EC_KEY_clear_flags(EC_KEY *key, int flags)
564 key->flags &= ~flags;
567 int EC_KEY_decoded_from_explicit_params(const EC_KEY *key)
569 if (key == NULL || key->group == NULL)
571 return key->group->decoded_from_explicit_params;
574 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
575 unsigned char **pbuf, BN_CTX *ctx)
577 if (key == NULL || key->pub_key == NULL || key->group == NULL)
579 return EC_POINT_point2buf(key->group, key->pub_key, form, pbuf, ctx);
582 int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
585 if (key == NULL || key->group == NULL)
587 if (key->pub_key == NULL)
588 key->pub_key = EC_POINT_new(key->group);
589 if (key->pub_key == NULL)
591 if (EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx) == 0)
594 * Save the point conversion form.
595 * For non-custom curves the first octet of the buffer (excluding
596 * the last significant bit) contains the point conversion form.
597 * EC_POINT_oct2point() has already performed sanity checking of
598 * the buffer so we know it is valid.
600 if ((key->group->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0)
601 key->conv_form = (point_conversion_form_t)(buf[0] & ~0x01);
605 size_t EC_KEY_priv2oct(const EC_KEY *eckey,
606 unsigned char *buf, size_t len)
608 if (eckey->group == NULL || eckey->group->meth == NULL)
610 if (eckey->group->meth->priv2oct == NULL) {
611 ECerr(EC_F_EC_KEY_PRIV2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
615 return eckey->group->meth->priv2oct(eckey, buf, len);
618 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
619 unsigned char *buf, size_t len)
623 buf_len = (EC_GROUP_order_bits(eckey->group) + 7) / 8;
624 if (eckey->priv_key == NULL)
628 else if (len < buf_len)
631 /* Octetstring may need leading zeros if BN is to short */
633 if (BN_bn2binpad(eckey->priv_key, buf, buf_len) == -1) {
634 ECerr(EC_F_EC_KEY_SIMPLE_PRIV2OCT, EC_R_BUFFER_TOO_SMALL);
641 int EC_KEY_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
643 if (eckey->group == NULL || eckey->group->meth == NULL)
645 if (eckey->group->meth->oct2priv == NULL) {
646 ECerr(EC_F_EC_KEY_OCT2PRIV, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
649 return eckey->group->meth->oct2priv(eckey, buf, len);
652 int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
654 if (eckey->priv_key == NULL)
655 eckey->priv_key = BN_secure_new();
656 if (eckey->priv_key == NULL) {
657 ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_MALLOC_FAILURE);
660 eckey->priv_key = BN_bin2bn(buf, len, eckey->priv_key);
661 if (eckey->priv_key == NULL) {
662 ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_BN_LIB);
668 size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf)
673 len = EC_KEY_priv2oct(eckey, NULL, 0);
676 if ((buf = OPENSSL_malloc(len)) == NULL) {
677 ECerr(EC_F_EC_KEY_PRIV2BUF, ERR_R_MALLOC_FAILURE);
680 len = EC_KEY_priv2oct(eckey, buf, len);
689 int EC_KEY_can_sign(const EC_KEY *eckey)
691 if (eckey->group == NULL || eckey->group->meth == NULL
692 || (eckey->group->meth->flags & EC_FLAGS_NO_SIGN))