2 * Copyright 2002-2023 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the Apache License 2.0 (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
12 * EC_KEY low level APIs are deprecated for public use, but still ok for
15 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
20 #include "internal/refcount.h"
21 #include <openssl/err.h>
23 # include <openssl/engine.h>
25 #include <openssl/self_test.h>
26 #include "prov/providercommon.h"
28 #include "crypto/bn.h"
30 static int ecdsa_keygen_pairwise_test(EC_KEY
*eckey
, OSSL_CALLBACK
*cb
,
34 EC_KEY
*EC_KEY_new(void)
36 return ossl_ec_key_new_method_int(NULL
, NULL
, NULL
);
40 EC_KEY
*EC_KEY_new_ex(OSSL_LIB_CTX
*ctx
, const char *propq
)
42 return ossl_ec_key_new_method_int(ctx
, propq
, NULL
);
45 EC_KEY
*EC_KEY_new_by_curve_name_ex(OSSL_LIB_CTX
*ctx
, const char *propq
,
48 EC_KEY
*ret
= EC_KEY_new_ex(ctx
, propq
);
51 ret
->group
= EC_GROUP_new_by_curve_name_ex(ctx
, propq
, nid
);
52 if (ret
->group
== NULL
) {
56 if (ret
->meth
->set_group
!= NULL
57 && ret
->meth
->set_group(ret
, ret
->group
) == 0) {
65 EC_KEY
*EC_KEY_new_by_curve_name(int nid
)
67 return EC_KEY_new_by_curve_name_ex(NULL
, NULL
, nid
);
71 void EC_KEY_free(EC_KEY
*r
)
78 CRYPTO_DOWN_REF(&r
->references
, &i
);
79 REF_PRINT_COUNT("EC_KEY", r
);
82 REF_ASSERT_ISNT(i
< 0);
84 if (r
->meth
!= NULL
&& r
->meth
->finish
!= NULL
)
87 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
88 ENGINE_finish(r
->engine
);
91 if (r
->group
&& r
->group
->meth
->keyfinish
)
92 r
->group
->meth
->keyfinish(r
);
95 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY
, r
, &r
->ex_data
);
97 CRYPTO_FREE_REF(&r
->references
);
98 EC_GROUP_free(r
->group
);
99 EC_POINT_free(r
->pub_key
);
100 BN_clear_free(r
->priv_key
);
101 OPENSSL_free(r
->propq
);
103 OPENSSL_clear_free((void *)r
, sizeof(EC_KEY
));
106 EC_KEY
*EC_KEY_copy(EC_KEY
*dest
, const EC_KEY
*src
)
108 if (dest
== NULL
|| src
== NULL
) {
109 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
112 if (src
->meth
!= dest
->meth
) {
113 if (dest
->meth
->finish
!= NULL
)
114 dest
->meth
->finish(dest
);
115 if (dest
->group
&& dest
->group
->meth
->keyfinish
)
116 dest
->group
->meth
->keyfinish(dest
);
117 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
118 if (ENGINE_finish(dest
->engine
) == 0)
123 dest
->libctx
= src
->libctx
;
124 /* copy the parameters */
125 if (src
->group
!= NULL
) {
126 /* clear the old group */
127 EC_GROUP_free(dest
->group
);
128 dest
->group
= ossl_ec_group_new_ex(src
->libctx
, src
->propq
,
130 if (dest
->group
== NULL
)
132 if (!EC_GROUP_copy(dest
->group
, src
->group
))
135 /* copy the public key */
136 if (src
->pub_key
!= NULL
) {
137 EC_POINT_free(dest
->pub_key
);
138 dest
->pub_key
= EC_POINT_new(src
->group
);
139 if (dest
->pub_key
== NULL
)
141 if (!EC_POINT_copy(dest
->pub_key
, src
->pub_key
))
144 /* copy the private key */
145 if (src
->priv_key
!= NULL
) {
146 if (dest
->priv_key
== NULL
) {
147 dest
->priv_key
= BN_new();
148 if (dest
->priv_key
== NULL
)
151 if (!BN_copy(dest
->priv_key
, src
->priv_key
))
153 if (src
->group
->meth
->keycopy
154 && src
->group
->meth
->keycopy(dest
, src
) == 0)
161 dest
->enc_flag
= src
->enc_flag
;
162 dest
->conv_form
= src
->conv_form
;
163 dest
->version
= src
->version
;
164 dest
->flags
= src
->flags
;
166 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY
,
167 &dest
->ex_data
, &src
->ex_data
))
171 if (src
->meth
!= dest
->meth
) {
172 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
173 if (src
->engine
!= NULL
&& ENGINE_init(src
->engine
) == 0)
175 dest
->engine
= src
->engine
;
177 dest
->meth
= src
->meth
;
180 if (src
->meth
->copy
!= NULL
&& src
->meth
->copy(dest
, src
) == 0)
188 EC_KEY
*EC_KEY_dup(const EC_KEY
*ec_key
)
190 return ossl_ec_key_dup(ec_key
, OSSL_KEYMGMT_SELECT_ALL
);
193 int EC_KEY_up_ref(EC_KEY
*r
)
197 if (CRYPTO_UP_REF(&r
->references
, &i
) <= 0)
200 REF_PRINT_COUNT("EC_KEY", r
);
201 REF_ASSERT_ISNT(i
< 2);
202 return ((i
> 1) ? 1 : 0);
205 ENGINE
*EC_KEY_get0_engine(const EC_KEY
*eckey
)
207 return eckey
->engine
;
210 int EC_KEY_generate_key(EC_KEY
*eckey
)
212 if (eckey
== NULL
|| eckey
->group
== NULL
) {
213 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
216 if (eckey
->meth
->keygen
!= NULL
) {
219 ret
= eckey
->meth
->keygen(eckey
);
225 ERR_raise(ERR_LIB_EC
, EC_R_OPERATION_NOT_SUPPORTED
);
229 int ossl_ec_key_gen(EC_KEY
*eckey
)
233 ret
= eckey
->group
->meth
->keygen(eckey
);
241 * Refer: FIPS 140-3 IG 10.3.A Additional Comment 1
242 * Perform a KAT by duplicating the public key generation.
244 * NOTE: This issue requires a background understanding, provided in a separate
245 * document; the current IG 10.3.A AC1 is insufficient regarding the PCT for
246 * the key agreement scenario.
248 * Currently IG 10.3.A requires PCT in the mode of use prior to use of the
249 * key pair, citing the PCT defined in the associated standard. For key
250 * agreement, the only PCT defined in SP 800-56A is that of Section 5.6.2.4:
251 * the comparison of the original public key to a newly calculated public key.
253 static int ecdsa_keygen_knownanswer_test(EC_KEY
*eckey
, BN_CTX
*ctx
,
254 OSSL_CALLBACK
*cb
, void *cbarg
)
257 OSSL_SELF_TEST
*st
= NULL
;
258 unsigned char bytes
[512] = {0};
259 EC_POINT
*pub_key2
= EC_POINT_new(eckey
->group
);
261 if (pub_key2
== NULL
)
264 st
= OSSL_SELF_TEST_new(cb
, cbarg
);
268 OSSL_SELF_TEST_onbegin(st
, OSSL_SELF_TEST_TYPE_PCT_KAT
,
269 OSSL_SELF_TEST_DESC_PCT_ECDSA
);
271 /* pub_key = priv_key * G (where G is a point on the curve) */
272 if (!EC_POINT_mul(eckey
->group
, pub_key2
, eckey
->priv_key
, NULL
, NULL
, ctx
))
275 if (BN_num_bytes(pub_key2
->X
) > (int)sizeof(bytes
))
277 len
= BN_bn2bin(pub_key2
->X
, bytes
);
278 if (OSSL_SELF_TEST_oncorrupt_byte(st
, bytes
)
279 && BN_bin2bn(bytes
, len
, pub_key2
->X
) == NULL
)
281 ret
= !EC_POINT_cmp(eckey
->group
, eckey
->pub_key
, pub_key2
, ctx
);
284 OSSL_SELF_TEST_onend(st
, ret
);
285 OSSL_SELF_TEST_free(st
);
286 EC_POINT_free(pub_key2
);
291 * ECC Key generation.
292 * See SP800-56AR3 5.6.1.2.2 "Key Pair Generation by Testing Candidates"
295 * libctx A context containing an optional self test callback.
296 * eckey An EC key object that contains domain params. The generated keypair
297 * is stored in this object.
298 * pairwise_test Set to non zero to perform a pairwise test. If the test
299 * fails then the keypair is not generated,
300 * Returns 1 if the keypair was generated or 0 otherwise.
302 static int ec_generate_key(EC_KEY
*eckey
, int pairwise_test
)
305 BIGNUM
*priv_key
= NULL
;
306 const BIGNUM
*tmp
= NULL
;
307 BIGNUM
*order
= NULL
;
308 EC_POINT
*pub_key
= NULL
;
309 const EC_GROUP
*group
= eckey
->group
;
310 BN_CTX
*ctx
= BN_CTX_secure_new_ex(eckey
->libctx
);
311 int sm2
= EC_KEY_get_flags(eckey
) & EC_FLAG_SM2_RANGE
? 1 : 0;
316 if (eckey
->priv_key
== NULL
) {
317 priv_key
= BN_secure_new();
318 if (priv_key
== NULL
)
321 priv_key
= eckey
->priv_key
;
324 * Steps (1-2): Check domain parameters and security strength.
325 * These steps must be done by the user. This would need to be
326 * stated in the security policy.
329 tmp
= EC_GROUP_get0_order(group
);
334 * Steps (3-7): priv_key = DRBG_RAND(order_n_bits) (range [1, n-1]).
335 * Although this is slightly different from the standard, it is effectively
336 * equivalent as it gives an unbiased result ranging from 1..n-1. It is also
337 * faster as the standard needs to retry more often. Also doing
338 * 1 + rand[0..n-2] would effect the way that tests feed dummy entropy into
339 * rand so the simpler backward compatible method has been used here.
342 /* range of SM2 private key is [1, n-1) */
345 if (order
== NULL
|| !BN_sub(order
, tmp
, BN_value_one()))
354 if (!BN_priv_rand_range_ex(priv_key
, order
, 0, ctx
))
356 while (BN_is_zero(priv_key
)) ;
358 if (eckey
->pub_key
== NULL
) {
359 pub_key
= EC_POINT_new(group
);
363 pub_key
= eckey
->pub_key
;
365 /* Step (8) : pub_key = priv_key * G (where G is a point on the curve) */
366 if (!EC_POINT_mul(group
, pub_key
, priv_key
, NULL
, NULL
, ctx
))
369 eckey
->priv_key
= priv_key
;
370 eckey
->pub_key
= pub_key
;
378 #endif /* FIPS_MODULE */
382 OSSL_CALLBACK
*cb
= NULL
;
385 OSSL_SELF_TEST_get_callback(eckey
->libctx
, &cb
, &cbarg
);
386 ok
= ecdsa_keygen_pairwise_test(eckey
, cb
, cbarg
)
387 && ecdsa_keygen_knownanswer_test(eckey
, ctx
, cb
, cbarg
);
390 /* Step (9): If there is an error return an invalid keypair. */
392 ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT
);
393 BN_clear(eckey
->priv_key
);
394 if (eckey
->pub_key
!= NULL
)
395 EC_POINT_set_to_infinity(group
, eckey
->pub_key
);
398 EC_POINT_free(pub_key
);
399 BN_clear_free(priv_key
);
407 * This is similar to ec_generate_key(), except it uses an ikm to
408 * derive the private key.
410 int ossl_ec_generate_key_dhkem(EC_KEY
*eckey
,
411 const unsigned char *ikm
, size_t ikmlen
)
415 if (eckey
->priv_key
== NULL
) {
416 eckey
->priv_key
= BN_secure_new();
417 if (eckey
->priv_key
== NULL
)
420 if (ossl_ec_dhkem_derive_private(eckey
, eckey
->priv_key
, ikm
, ikmlen
) <= 0)
422 if (eckey
->pub_key
== NULL
) {
423 eckey
->pub_key
= EC_POINT_new(eckey
->group
);
424 if (eckey
->pub_key
== NULL
)
427 if (!ossl_ec_key_simple_generate_public_key(eckey
))
433 BN_clear_free(eckey
->priv_key
);
434 eckey
->priv_key
= NULL
;
435 if (eckey
->pub_key
!= NULL
)
436 EC_POINT_set_to_infinity(eckey
->group
, eckey
->pub_key
);
442 int ossl_ec_key_simple_generate_key(EC_KEY
*eckey
)
444 return ec_generate_key(eckey
, 0);
447 int ossl_ec_key_simple_generate_public_key(EC_KEY
*eckey
)
450 BN_CTX
*ctx
= BN_CTX_new_ex(eckey
->libctx
);
456 * See SP800-56AR3 5.6.1.2.2: Step (8)
457 * pub_key = priv_key * G (where G is a point on the curve)
459 ret
= EC_POINT_mul(eckey
->group
, eckey
->pub_key
, eckey
->priv_key
, NULL
,
469 int EC_KEY_check_key(const EC_KEY
*eckey
)
471 if (eckey
== NULL
|| eckey
->group
== NULL
|| eckey
->pub_key
== NULL
) {
472 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
476 if (eckey
->group
->meth
->keycheck
== NULL
) {
477 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
481 return eckey
->group
->meth
->keycheck(eckey
);
485 * Check the range of the EC public key.
486 * See SP800-56A R3 Section 5.6.2.3.3 (Part 2)
488 * - If q = odd prime p: Verify that xQ and yQ are integers in the
489 * interval[0, p - 1], OR
490 * - If q = 2m: Verify that xQ and yQ are bit strings of length m bits.
491 * Returns 1 if the public key has a valid range, otherwise it returns 0.
493 static int ec_key_public_range_check(BN_CTX
*ctx
, const EC_KEY
*key
)
504 if (!EC_POINT_get_affine_coordinates(key
->group
, key
->pub_key
, x
, y
, ctx
))
507 if (EC_GROUP_get_field_type(key
->group
) == NID_X9_62_prime_field
) {
508 if (BN_is_negative(x
)
509 || BN_cmp(x
, key
->group
->field
) >= 0
511 || BN_cmp(y
, key
->group
->field
) >= 0) {
515 int m
= EC_GROUP_get_degree(key
->group
);
516 if (BN_num_bits(x
) > m
|| BN_num_bits(y
) > m
) {
527 * ECC Partial Public-Key Validation as specified in SP800-56A R3
528 * Section 5.6.2.3.4 ECC Partial Public-Key Validation Routine.
530 int ossl_ec_key_public_check_quick(const EC_KEY
*eckey
, BN_CTX
*ctx
)
532 if (eckey
== NULL
|| eckey
->group
== NULL
|| eckey
->pub_key
== NULL
) {
533 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
537 /* 5.6.2.3.3 (Step 1): Q != infinity */
538 if (EC_POINT_is_at_infinity(eckey
->group
, eckey
->pub_key
)) {
539 ERR_raise(ERR_LIB_EC
, EC_R_POINT_AT_INFINITY
);
543 /* 5.6.2.3.3 (Step 2) Test if the public key is in range */
544 if (!ec_key_public_range_check(ctx
, eckey
)) {
545 ERR_raise(ERR_LIB_EC
, EC_R_COORDINATES_OUT_OF_RANGE
);
549 /* 5.6.2.3.3 (Step 3) is the pub_key on the elliptic curve */
550 if (EC_POINT_is_on_curve(eckey
->group
, eckey
->pub_key
, ctx
) <= 0) {
551 ERR_raise(ERR_LIB_EC
, EC_R_POINT_IS_NOT_ON_CURVE
);
558 * ECC Key validation as specified in SP800-56A R3.
559 * Section 5.6.2.3.3 ECC Full Public-Key Validation Routine.
561 int ossl_ec_key_public_check(const EC_KEY
*eckey
, BN_CTX
*ctx
)
564 EC_POINT
*point
= NULL
;
565 const BIGNUM
*order
= NULL
;
567 if (!ossl_ec_key_public_check_quick(eckey
, ctx
))
570 point
= EC_POINT_new(eckey
->group
);
574 order
= eckey
->group
->order
;
575 if (BN_is_zero(order
)) {
576 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GROUP_ORDER
);
579 /* 5.6.2.3.3 (Step 4) : pub_key * order is the point at infinity. */
580 if (!EC_POINT_mul(eckey
->group
, point
, NULL
, eckey
->pub_key
, order
, ctx
)) {
581 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
584 if (!EC_POINT_is_at_infinity(eckey
->group
, point
)) {
585 ERR_raise(ERR_LIB_EC
, EC_R_WRONG_ORDER
);
590 EC_POINT_free(point
);
595 * ECC Key validation as specified in SP800-56A R3.
596 * Section 5.6.2.1.2 Owner Assurance of Private-Key Validity
597 * The private key is in the range [1, order-1]
599 int ossl_ec_key_private_check(const EC_KEY
*eckey
)
601 if (eckey
== NULL
|| eckey
->group
== NULL
|| eckey
->priv_key
== NULL
) {
602 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
605 if (BN_cmp(eckey
->priv_key
, BN_value_one()) < 0
606 || BN_cmp(eckey
->priv_key
, eckey
->group
->order
) >= 0) {
607 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_PRIVATE_KEY
);
614 * ECC Key validation as specified in SP800-56A R3.
615 * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency (b)
616 * Check if generator * priv_key = pub_key
618 int ossl_ec_key_pairwise_check(const EC_KEY
*eckey
, BN_CTX
*ctx
)
621 EC_POINT
*point
= NULL
;
624 || eckey
->group
== NULL
625 || eckey
->pub_key
== NULL
626 || eckey
->priv_key
== NULL
) {
627 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
631 point
= EC_POINT_new(eckey
->group
);
636 if (!EC_POINT_mul(eckey
->group
, point
, eckey
->priv_key
, NULL
, NULL
, ctx
)) {
637 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
640 if (EC_POINT_cmp(eckey
->group
, point
, eckey
->pub_key
, ctx
) != 0) {
641 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_PRIVATE_KEY
);
646 EC_POINT_free(point
);
652 * ECC Key validation as specified in SP800-56A R3.
653 * Section 5.6.2.3.3 ECC Full Public-Key Validation
654 * Section 5.6.2.1.2 Owner Assurance of Private-Key Validity
655 * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
657 * Before calling this method in fips mode, there should be an assurance that
658 * an approved elliptic-curve group is used.
659 * Returns 1 if the key is valid, otherwise it returns 0.
661 int ossl_ec_key_simple_check_key(const EC_KEY
*eckey
)
667 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
670 if ((ctx
= BN_CTX_new_ex(eckey
->libctx
)) == NULL
)
673 if (!ossl_ec_key_public_check(eckey
, ctx
))
676 if (eckey
->priv_key
!= NULL
) {
677 if (!ossl_ec_key_private_check(eckey
)
678 || !ossl_ec_key_pairwise_check(eckey
, ctx
))
687 int EC_KEY_set_public_key_affine_coordinates(EC_KEY
*key
, BIGNUM
*x
,
692 EC_POINT
*point
= NULL
;
695 if (key
== NULL
|| key
->group
== NULL
|| x
== NULL
|| y
== NULL
) {
696 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
699 ctx
= BN_CTX_new_ex(key
->libctx
);
704 point
= EC_POINT_new(key
->group
);
709 tx
= BN_CTX_get(ctx
);
710 ty
= BN_CTX_get(ctx
);
714 if (!EC_POINT_set_affine_coordinates(key
->group
, point
, x
, y
, ctx
))
716 if (!EC_POINT_get_affine_coordinates(key
->group
, point
, tx
, ty
, ctx
))
720 * Check if retrieved coordinates match originals. The range check is done
721 * inside EC_KEY_check_key().
723 if (BN_cmp(x
, tx
) || BN_cmp(y
, ty
)) {
724 ERR_raise(ERR_LIB_EC
, EC_R_COORDINATES_OUT_OF_RANGE
);
728 /* EC_KEY_set_public_key updates dirty_cnt */
729 if (!EC_KEY_set_public_key(key
, point
))
732 if (EC_KEY_check_key(key
) == 0)
740 EC_POINT_free(point
);
745 OSSL_LIB_CTX
*ossl_ec_key_get_libctx(const EC_KEY
*key
)
750 const char *ossl_ec_key_get0_propq(const EC_KEY
*key
)
755 void ossl_ec_key_set0_libctx(EC_KEY
*key
, OSSL_LIB_CTX
*libctx
)
757 key
->libctx
= libctx
;
758 /* Do we need to propagate this to the group? */
761 const EC_GROUP
*EC_KEY_get0_group(const EC_KEY
*key
)
766 int EC_KEY_set_group(EC_KEY
*key
, const EC_GROUP
*group
)
768 if (key
->meth
->set_group
!= NULL
&& key
->meth
->set_group(key
, group
) == 0)
770 EC_GROUP_free(key
->group
);
771 key
->group
= EC_GROUP_dup(group
);
772 if (key
->group
!= NULL
&& EC_GROUP_get_curve_name(key
->group
) == NID_sm2
)
773 EC_KEY_set_flags(key
, EC_FLAG_SM2_RANGE
);
776 return (key
->group
== NULL
) ? 0 : 1;
779 const BIGNUM
*EC_KEY_get0_private_key(const EC_KEY
*key
)
781 return key
->priv_key
;
784 int EC_KEY_set_private_key(EC_KEY
*key
, const BIGNUM
*priv_key
)
787 const BIGNUM
*order
= NULL
;
788 BIGNUM
*tmp_key
= NULL
;
790 if (key
->group
== NULL
|| key
->group
->meth
== NULL
)
794 * Not only should key->group be set, but it should also be in a valid
795 * fully initialized state.
797 * Specifically, to operate in constant time, we need that the group order
798 * is set, as we use its length as the fixed public size of any scalar used
799 * as an EC private key.
801 order
= EC_GROUP_get0_order(key
->group
);
802 if (order
== NULL
|| BN_is_zero(order
))
803 return 0; /* This should never happen */
805 if (key
->group
->meth
->set_private
!= NULL
806 && key
->group
->meth
->set_private(key
, priv_key
) == 0)
808 if (key
->meth
->set_private
!= NULL
809 && key
->meth
->set_private(key
, priv_key
) == 0)
813 * Return `0` to comply with legacy behavior for this function, see
814 * https://github.com/openssl/openssl/issues/18744#issuecomment-1195175696
816 if (priv_key
== NULL
) {
817 BN_clear_free(key
->priv_key
);
818 key
->priv_key
= NULL
;
819 return 0; /* intentional for legacy compatibility */
823 * We should never leak the bit length of the secret scalar in the key,
824 * so we always set the `BN_FLG_CONSTTIME` flag on the internal `BIGNUM`
825 * holding the secret scalar.
827 * This is important also because `BN_dup()` (and `BN_copy()`) do not
828 * propagate the `BN_FLG_CONSTTIME` flag from the source `BIGNUM`, and
829 * this brings an extra risk of inadvertently losing the flag, even when
830 * the caller specifically set it.
832 * The propagation has been turned on and off a few times in the past
833 * years because in some conditions has shown unintended consequences in
834 * some code paths, so at the moment we can't fix this in the BN layer.
836 * In `EC_KEY_set_private_key()` we can work around the propagation by
837 * manually setting the flag after `BN_dup()` as we know for sure that
838 * inside the EC module the `BN_FLG_CONSTTIME` is always treated
839 * correctly and should not generate unintended consequences.
841 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
842 * to preallocate the BIGNUM internal buffer to a fixed public size big
843 * enough that operations performed during the processing never trigger
844 * a realloc which would leak the size of the scalar through memory
850 * The order of the large prime subgroup of the curve is our choice for
851 * a fixed public size, as that is generally the upper bound for
852 * generating a private key in EC cryptosystems and should fit all valid
855 * For preallocating the BIGNUM storage we look at the number of "words"
856 * required for the internal representation of the order, and we
857 * preallocate 2 extra "words" in case any of the subsequent processing
858 * might temporarily overflow the order length.
860 tmp_key
= BN_dup(priv_key
);
864 BN_set_flags(tmp_key
, BN_FLG_CONSTTIME
);
866 fixed_top
= bn_get_top(order
) + 2;
867 if (bn_wexpand(tmp_key
, fixed_top
) == NULL
) {
868 BN_clear_free(tmp_key
);
872 BN_clear_free(key
->priv_key
);
873 key
->priv_key
= tmp_key
;
879 const EC_POINT
*EC_KEY_get0_public_key(const EC_KEY
*key
)
884 int EC_KEY_set_public_key(EC_KEY
*key
, const EC_POINT
*pub_key
)
886 if (key
->meth
->set_public
!= NULL
887 && key
->meth
->set_public(key
, pub_key
) == 0)
889 EC_POINT_free(key
->pub_key
);
890 key
->pub_key
= EC_POINT_dup(pub_key
, key
->group
);
892 return (key
->pub_key
== NULL
) ? 0 : 1;
895 unsigned int EC_KEY_get_enc_flags(const EC_KEY
*key
)
897 return key
->enc_flag
;
900 void EC_KEY_set_enc_flags(EC_KEY
*key
, unsigned int flags
)
902 key
->enc_flag
= flags
;
905 point_conversion_form_t
EC_KEY_get_conv_form(const EC_KEY
*key
)
907 return key
->conv_form
;
910 void EC_KEY_set_conv_form(EC_KEY
*key
, point_conversion_form_t cform
)
912 key
->conv_form
= cform
;
913 if (key
->group
!= NULL
)
914 EC_GROUP_set_point_conversion_form(key
->group
, cform
);
917 void EC_KEY_set_asn1_flag(EC_KEY
*key
, int flag
)
919 if (key
->group
!= NULL
)
920 EC_GROUP_set_asn1_flag(key
->group
, flag
);
923 #ifndef OPENSSL_NO_DEPRECATED_3_0
924 int EC_KEY_precompute_mult(EC_KEY
*key
, BN_CTX
*ctx
)
926 if (key
->group
== NULL
)
928 return EC_GROUP_precompute_mult(key
->group
, ctx
);
932 int EC_KEY_get_flags(const EC_KEY
*key
)
937 void EC_KEY_set_flags(EC_KEY
*key
, int flags
)
943 void EC_KEY_clear_flags(EC_KEY
*key
, int flags
)
945 key
->flags
&= ~flags
;
949 int EC_KEY_decoded_from_explicit_params(const EC_KEY
*key
)
951 if (key
== NULL
|| key
->group
== NULL
)
953 return key
->group
->decoded_from_explicit_params
;
956 size_t EC_KEY_key2buf(const EC_KEY
*key
, point_conversion_form_t form
,
957 unsigned char **pbuf
, BN_CTX
*ctx
)
959 if (key
== NULL
|| key
->pub_key
== NULL
|| key
->group
== NULL
)
961 return EC_POINT_point2buf(key
->group
, key
->pub_key
, form
, pbuf
, ctx
);
964 int EC_KEY_oct2key(EC_KEY
*key
, const unsigned char *buf
, size_t len
,
967 if (key
== NULL
|| key
->group
== NULL
)
969 if (key
->pub_key
== NULL
)
970 key
->pub_key
= EC_POINT_new(key
->group
);
971 if (key
->pub_key
== NULL
)
973 if (EC_POINT_oct2point(key
->group
, key
->pub_key
, buf
, len
, ctx
) == 0)
977 * Save the point conversion form.
978 * For non-custom curves the first octet of the buffer (excluding
979 * the last significant bit) contains the point conversion form.
980 * EC_POINT_oct2point() has already performed sanity checking of
981 * the buffer so we know it is valid.
983 if ((key
->group
->meth
->flags
& EC_FLAGS_CUSTOM_CURVE
) == 0)
984 key
->conv_form
= (point_conversion_form_t
)(buf
[0] & ~0x01);
988 size_t EC_KEY_priv2oct(const EC_KEY
*eckey
,
989 unsigned char *buf
, size_t len
)
991 if (eckey
->group
== NULL
|| eckey
->group
->meth
== NULL
)
993 if (eckey
->group
->meth
->priv2oct
== NULL
) {
994 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
998 return eckey
->group
->meth
->priv2oct(eckey
, buf
, len
);
1001 size_t ossl_ec_key_simple_priv2oct(const EC_KEY
*eckey
,
1002 unsigned char *buf
, size_t len
)
1006 buf_len
= (EC_GROUP_order_bits(eckey
->group
) + 7) / 8;
1007 if (eckey
->priv_key
== NULL
)
1011 else if (len
< buf_len
)
1014 /* Octetstring may need leading zeros if BN is to short */
1016 if (BN_bn2binpad(eckey
->priv_key
, buf
, buf_len
) == -1) {
1017 ERR_raise(ERR_LIB_EC
, EC_R_BUFFER_TOO_SMALL
);
1024 int EC_KEY_oct2priv(EC_KEY
*eckey
, const unsigned char *buf
, size_t len
)
1028 if (eckey
->group
== NULL
|| eckey
->group
->meth
== NULL
)
1030 if (eckey
->group
->meth
->oct2priv
== NULL
) {
1031 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1034 ret
= eckey
->group
->meth
->oct2priv(eckey
, buf
, len
);
1040 int ossl_ec_key_simple_oct2priv(EC_KEY
*eckey
, const unsigned char *buf
,
1043 if (eckey
->priv_key
== NULL
)
1044 eckey
->priv_key
= BN_secure_new();
1045 if (eckey
->priv_key
== NULL
) {
1046 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
1049 if (BN_bin2bn(buf
, len
, eckey
->priv_key
) == NULL
) {
1050 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
1057 size_t EC_KEY_priv2buf(const EC_KEY
*eckey
, unsigned char **pbuf
)
1062 len
= EC_KEY_priv2oct(eckey
, NULL
, 0);
1065 if ((buf
= OPENSSL_malloc(len
)) == NULL
)
1067 len
= EC_KEY_priv2oct(eckey
, buf
, len
);
1076 int EC_KEY_can_sign(const EC_KEY
*eckey
)
1078 if (eckey
->group
== NULL
|| eckey
->group
->meth
== NULL
1079 || (eckey
->group
->meth
->flags
& EC_FLAGS_NO_SIGN
))
1085 * FIPS 140-2 IG 9.9 AS09.33
1086 * Perform a sign/verify operation.
1088 * NOTE: When generating keys for key-agreement schemes - FIPS 140-2 IG 9.9
1089 * states that no additional pairwise tests are required (apart from the tests
1090 * specified in SP800-56A) when generating keys. Hence pairwise ECDH tests are
1093 static int ecdsa_keygen_pairwise_test(EC_KEY
*eckey
, OSSL_CALLBACK
*cb
,
1097 unsigned char dgst
[16] = {0};
1098 int dgst_len
= (int)sizeof(dgst
);
1099 ECDSA_SIG
*sig
= NULL
;
1100 OSSL_SELF_TEST
*st
= NULL
;
1102 st
= OSSL_SELF_TEST_new(cb
, cbarg
);
1106 OSSL_SELF_TEST_onbegin(st
, OSSL_SELF_TEST_TYPE_PCT
,
1107 OSSL_SELF_TEST_DESC_PCT_ECDSA
);
1109 sig
= ECDSA_do_sign(dgst
, dgst_len
, eckey
);
1113 OSSL_SELF_TEST_oncorrupt_byte(st
, dgst
);
1115 if (ECDSA_do_verify(dgst
, dgst_len
, sig
, eckey
) != 1)
1120 OSSL_SELF_TEST_onend(st
, ret
);
1121 OSSL_SELF_TEST_free(st
);
1122 ECDSA_SIG_free(sig
);