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 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 * ECDSA 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>
22 #include <openssl/engine.h>
23 #include <openssl/self_test.h>
24 #include "prov/providercommon.h"
25 #include "crypto/bn.h"
27 static int ecdsa_keygen_pairwise_test(EC_KEY
*eckey
, OSSL_CALLBACK
*cb
,
31 EC_KEY
*EC_KEY_new(void)
33 return ec_key_new_method_int(NULL
, NULL
, NULL
);
37 EC_KEY
*EC_KEY_new_ex(OSSL_LIB_CTX
*ctx
, const char *propq
)
39 return ec_key_new_method_int(ctx
, propq
, NULL
);
42 EC_KEY
*EC_KEY_new_by_curve_name_ex(OSSL_LIB_CTX
*ctx
, const char *propq
,
45 EC_KEY
*ret
= EC_KEY_new_ex(ctx
, propq
);
48 ret
->group
= EC_GROUP_new_by_curve_name_ex(ctx
, propq
, nid
);
49 if (ret
->group
== NULL
) {
53 if (ret
->meth
->set_group
!= NULL
54 && ret
->meth
->set_group(ret
, ret
->group
) == 0) {
62 EC_KEY
*EC_KEY_new_by_curve_name(int nid
)
64 return EC_KEY_new_by_curve_name_ex(NULL
, NULL
, nid
);
68 void EC_KEY_free(EC_KEY
*r
)
75 CRYPTO_DOWN_REF(&r
->references
, &i
, r
->lock
);
76 REF_PRINT_COUNT("EC_KEY", r
);
79 REF_ASSERT_ISNT(i
< 0);
81 if (r
->meth
!= NULL
&& r
->meth
->finish
!= NULL
)
84 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
85 ENGINE_finish(r
->engine
);
88 if (r
->group
&& r
->group
->meth
->keyfinish
)
89 r
->group
->meth
->keyfinish(r
);
92 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY
, r
, &r
->ex_data
);
94 CRYPTO_THREAD_lock_free(r
->lock
);
95 EC_GROUP_free(r
->group
);
96 EC_POINT_free(r
->pub_key
);
97 BN_clear_free(r
->priv_key
);
98 OPENSSL_free(r
->propq
);
100 OPENSSL_clear_free((void *)r
, sizeof(EC_KEY
));
103 EC_KEY
*EC_KEY_copy(EC_KEY
*dest
, const EC_KEY
*src
)
105 if (dest
== NULL
|| src
== NULL
) {
106 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
109 if (src
->meth
!= dest
->meth
) {
110 if (dest
->meth
->finish
!= NULL
)
111 dest
->meth
->finish(dest
);
112 if (dest
->group
&& dest
->group
->meth
->keyfinish
)
113 dest
->group
->meth
->keyfinish(dest
);
114 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
115 if (ENGINE_finish(dest
->engine
) == 0)
120 dest
->libctx
= src
->libctx
;
121 /* copy the parameters */
122 if (src
->group
!= NULL
) {
123 /* clear the old group */
124 EC_GROUP_free(dest
->group
);
125 dest
->group
= ec_group_new_ex(src
->libctx
, src
->propq
, src
->group
->meth
);
126 if (dest
->group
== NULL
)
128 if (!EC_GROUP_copy(dest
->group
, src
->group
))
131 /* copy the public key */
132 if (src
->pub_key
!= NULL
) {
133 EC_POINT_free(dest
->pub_key
);
134 dest
->pub_key
= EC_POINT_new(src
->group
);
135 if (dest
->pub_key
== NULL
)
137 if (!EC_POINT_copy(dest
->pub_key
, src
->pub_key
))
140 /* copy the private key */
141 if (src
->priv_key
!= NULL
) {
142 if (dest
->priv_key
== NULL
) {
143 dest
->priv_key
= BN_new();
144 if (dest
->priv_key
== NULL
)
147 if (!BN_copy(dest
->priv_key
, src
->priv_key
))
149 if (src
->group
->meth
->keycopy
150 && src
->group
->meth
->keycopy(dest
, src
) == 0)
157 dest
->enc_flag
= src
->enc_flag
;
158 dest
->conv_form
= src
->conv_form
;
159 dest
->version
= src
->version
;
160 dest
->flags
= src
->flags
;
162 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY
,
163 &dest
->ex_data
, &src
->ex_data
))
167 if (src
->meth
!= dest
->meth
) {
168 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
169 if (src
->engine
!= NULL
&& ENGINE_init(src
->engine
) == 0)
171 dest
->engine
= src
->engine
;
173 dest
->meth
= src
->meth
;
176 if (src
->meth
->copy
!= NULL
&& src
->meth
->copy(dest
, src
) == 0)
184 EC_KEY
*EC_KEY_dup(const EC_KEY
*ec_key
)
186 EC_KEY
*ret
= ec_key_new_method_int(ec_key
->libctx
, ec_key
->propq
,
192 if (EC_KEY_copy(ret
, ec_key
) == NULL
) {
199 int EC_KEY_up_ref(EC_KEY
*r
)
203 if (CRYPTO_UP_REF(&r
->references
, &i
, r
->lock
) <= 0)
206 REF_PRINT_COUNT("EC_KEY", r
);
207 REF_ASSERT_ISNT(i
< 2);
208 return ((i
> 1) ? 1 : 0);
211 ENGINE
*EC_KEY_get0_engine(const EC_KEY
*eckey
)
213 return eckey
->engine
;
216 int EC_KEY_generate_key(EC_KEY
*eckey
)
218 if (eckey
== NULL
|| eckey
->group
== NULL
) {
219 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
222 if (eckey
->meth
->keygen
!= NULL
) {
225 ret
= eckey
->meth
->keygen(eckey
);
231 ERR_raise(ERR_LIB_EC
, EC_R_OPERATION_NOT_SUPPORTED
);
235 int ossl_ec_key_gen(EC_KEY
*eckey
)
239 ret
= eckey
->group
->meth
->keygen(eckey
);
247 * ECC Key generation.
248 * See SP800-56AR3 5.6.1.2.2 "Key Pair Generation by Testing Candidates"
251 * libctx A context containing an optional self test callback.
252 * eckey An EC key object that contains domain params. The generated keypair
253 * is stored in this object.
254 * pairwise_test Set to non zero to perform a pairwise test. If the test
255 * fails then the keypair is not generated,
256 * Returns 1 if the keypair was generated or 0 otherwise.
258 static int ec_generate_key(EC_KEY
*eckey
, int pairwise_test
)
261 BIGNUM
*priv_key
= NULL
;
262 const BIGNUM
*tmp
= NULL
;
263 BIGNUM
*order
= NULL
;
264 EC_POINT
*pub_key
= NULL
;
265 const EC_GROUP
*group
= eckey
->group
;
266 BN_CTX
*ctx
= BN_CTX_secure_new_ex(eckey
->libctx
);
267 int sm2
= EC_KEY_get_flags(eckey
) & EC_FLAG_SM2_RANGE
? 1 : 0;
272 if (eckey
->priv_key
== NULL
) {
273 priv_key
= BN_secure_new();
274 if (priv_key
== NULL
)
277 priv_key
= eckey
->priv_key
;
280 * Steps (1-2): Check domain parameters and security strength.
281 * These steps must be done by the user. This would need to be
282 * stated in the security policy.
285 tmp
= EC_GROUP_get0_order(group
);
290 * Steps (3-7): priv_key = DRBG_RAND(order_n_bits) (range [1, n-1]).
291 * Although this is slightly different from the standard, it is effectively
292 * equivalent as it gives an unbiased result ranging from 1..n-1. It is also
293 * faster as the standard needs to retry more often. Also doing
294 * 1 + rand[0..n-2] would effect the way that tests feed dummy entropy into
295 * rand so the simpler backward compatible method has been used here.
298 /* range of SM2 private key is [1, n-1) */
301 if (order
== NULL
|| !BN_sub(order
, tmp
, BN_value_one()))
310 if (!BN_priv_rand_range_ex(priv_key
, order
, ctx
))
312 while (BN_is_zero(priv_key
)) ;
314 if (eckey
->pub_key
== NULL
) {
315 pub_key
= EC_POINT_new(group
);
319 pub_key
= eckey
->pub_key
;
321 /* Step (8) : pub_key = priv_key * G (where G is a point on the curve) */
322 if (!EC_POINT_mul(group
, pub_key
, priv_key
, NULL
, NULL
, ctx
))
325 eckey
->priv_key
= priv_key
;
326 eckey
->pub_key
= pub_key
;
334 #endif /* FIPS_MODULE */
338 OSSL_CALLBACK
*cb
= NULL
;
341 OSSL_SELF_TEST_get_callback(eckey
->libctx
, &cb
, &cbarg
);
342 ok
= ecdsa_keygen_pairwise_test(eckey
, cb
, cbarg
);
345 /* Step (9): If there is an error return an invalid keypair. */
347 ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT
);
348 BN_clear(eckey
->priv_key
);
349 if (eckey
->pub_key
!= NULL
)
350 EC_POINT_set_to_infinity(group
, eckey
->pub_key
);
353 EC_POINT_free(pub_key
);
354 BN_clear_free(priv_key
);
360 int ec_key_simple_generate_key(EC_KEY
*eckey
)
362 return ec_generate_key(eckey
, 0);
365 int ec_key_simple_generate_public_key(EC_KEY
*eckey
)
368 BN_CTX
*ctx
= BN_CTX_new_ex(eckey
->libctx
);
374 * See SP800-56AR3 5.6.1.2.2: Step (8)
375 * pub_key = priv_key * G (where G is a point on the curve)
377 ret
= EC_POINT_mul(eckey
->group
, eckey
->pub_key
, eckey
->priv_key
, NULL
,
387 int EC_KEY_check_key(const EC_KEY
*eckey
)
389 if (eckey
== NULL
|| eckey
->group
== NULL
|| eckey
->pub_key
== NULL
) {
390 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
394 if (eckey
->group
->meth
->keycheck
== NULL
) {
395 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
399 return eckey
->group
->meth
->keycheck(eckey
);
403 * Check the range of the EC public key.
404 * See SP800-56A R3 Section 5.6.2.3.3 (Part 2)
406 * - If q = odd prime p: Verify that xQ and yQ are integers in the
407 * interval[0, p - 1], OR
408 * - If q = 2m: Verify that xQ and yQ are bit strings of length m bits.
409 * Returns 1 if the public key has a valid range, otherwise it returns 0.
411 static int ec_key_public_range_check(BN_CTX
*ctx
, const EC_KEY
*key
)
422 if (!EC_POINT_get_affine_coordinates(key
->group
, key
->pub_key
, x
, y
, ctx
))
425 if (EC_GROUP_get_field_type(key
->group
) == NID_X9_62_prime_field
) {
426 if (BN_is_negative(x
)
427 || BN_cmp(x
, key
->group
->field
) >= 0
429 || BN_cmp(y
, key
->group
->field
) >= 0) {
433 int m
= EC_GROUP_get_degree(key
->group
);
434 if (BN_num_bits(x
) > m
|| BN_num_bits(y
) > m
) {
445 * ECC Key validation as specified in SP800-56A R3.
446 * Section 5.6.2.3.3 ECC Full Public-Key Validation.
448 int ec_key_public_check(const EC_KEY
*eckey
, BN_CTX
*ctx
)
451 EC_POINT
*point
= NULL
;
452 const BIGNUM
*order
= NULL
;
454 if (eckey
== NULL
|| eckey
->group
== NULL
|| eckey
->pub_key
== NULL
) {
455 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
459 /* 5.6.2.3.3 (Step 1): Q != infinity */
460 if (EC_POINT_is_at_infinity(eckey
->group
, eckey
->pub_key
)) {
461 ERR_raise(ERR_LIB_EC
, EC_R_POINT_AT_INFINITY
);
465 point
= EC_POINT_new(eckey
->group
);
469 /* 5.6.2.3.3 (Step 2) Test if the public key is in range */
470 if (!ec_key_public_range_check(ctx
, eckey
)) {
471 ERR_raise(ERR_LIB_EC
, EC_R_COORDINATES_OUT_OF_RANGE
);
475 /* 5.6.2.3.3 (Step 3) is the pub_key on the elliptic curve */
476 if (EC_POINT_is_on_curve(eckey
->group
, eckey
->pub_key
, ctx
) <= 0) {
477 ERR_raise(ERR_LIB_EC
, EC_R_POINT_IS_NOT_ON_CURVE
);
481 order
= eckey
->group
->order
;
482 if (BN_is_zero(order
)) {
483 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_GROUP_ORDER
);
486 /* 5.6.2.3.3 (Step 4) : pub_key * order is the point at infinity. */
487 if (!EC_POINT_mul(eckey
->group
, point
, NULL
, eckey
->pub_key
, order
, ctx
)) {
488 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
491 if (!EC_POINT_is_at_infinity(eckey
->group
, point
)) {
492 ERR_raise(ERR_LIB_EC
, EC_R_WRONG_ORDER
);
497 EC_POINT_free(point
);
502 * ECC Key validation as specified in SP800-56A R3.
503 * Section 5.6.2.1.2 Owner Assurance of Private-Key Validity
504 * The private key is in the range [1, order-1]
506 int ec_key_private_check(const EC_KEY
*eckey
)
508 if (eckey
== NULL
|| eckey
->group
== NULL
|| eckey
->priv_key
== NULL
) {
509 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
512 if (BN_cmp(eckey
->priv_key
, BN_value_one()) < 0
513 || BN_cmp(eckey
->priv_key
, eckey
->group
->order
) >= 0) {
514 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_PRIVATE_KEY
);
521 * ECC Key validation as specified in SP800-56A R3.
522 * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency (b)
523 * Check if generator * priv_key = pub_key
525 int ec_key_pairwise_check(const EC_KEY
*eckey
, BN_CTX
*ctx
)
528 EC_POINT
*point
= NULL
;
531 || eckey
->group
== NULL
532 || eckey
->pub_key
== NULL
533 || eckey
->priv_key
== NULL
) {
534 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
538 point
= EC_POINT_new(eckey
->group
);
543 if (!EC_POINT_mul(eckey
->group
, point
, eckey
->priv_key
, NULL
, NULL
, ctx
)) {
544 ERR_raise(ERR_LIB_EC
, ERR_R_EC_LIB
);
547 if (EC_POINT_cmp(eckey
->group
, point
, eckey
->pub_key
, ctx
) != 0) {
548 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_PRIVATE_KEY
);
553 EC_POINT_free(point
);
559 * ECC Key validation as specified in SP800-56A R3.
560 * Section 5.6.2.3.3 ECC Full Public-Key Validation
561 * Section 5.6.2.1.2 Owner Assurance of Private-Key Validity
562 * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
564 * Before calling this method in fips mode, there should be an assurance that
565 * an approved elliptic-curve group is used.
566 * Returns 1 if the key is valid, otherwise it returns 0.
568 int ec_key_simple_check_key(const EC_KEY
*eckey
)
574 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
577 if ((ctx
= BN_CTX_new_ex(eckey
->libctx
)) == NULL
)
580 if (!ec_key_public_check(eckey
, ctx
))
583 if (eckey
->priv_key
!= NULL
) {
584 if (!ec_key_private_check(eckey
)
585 || !ec_key_pairwise_check(eckey
, ctx
))
594 int EC_KEY_set_public_key_affine_coordinates(EC_KEY
*key
, BIGNUM
*x
,
599 EC_POINT
*point
= NULL
;
602 if (key
== NULL
|| key
->group
== NULL
|| x
== NULL
|| y
== NULL
) {
603 ERR_raise(ERR_LIB_EC
, ERR_R_PASSED_NULL_PARAMETER
);
606 ctx
= BN_CTX_new_ex(key
->libctx
);
611 point
= EC_POINT_new(key
->group
);
616 tx
= BN_CTX_get(ctx
);
617 ty
= BN_CTX_get(ctx
);
621 if (!EC_POINT_set_affine_coordinates(key
->group
, point
, x
, y
, ctx
))
623 if (!EC_POINT_get_affine_coordinates(key
->group
, point
, tx
, ty
, ctx
))
627 * Check if retrieved coordinates match originals. The range check is done
628 * inside EC_KEY_check_key().
630 if (BN_cmp(x
, tx
) || BN_cmp(y
, ty
)) {
631 ERR_raise(ERR_LIB_EC
, EC_R_COORDINATES_OUT_OF_RANGE
);
635 /* EC_KEY_set_public_key updates dirty_cnt */
636 if (!EC_KEY_set_public_key(key
, point
))
639 if (EC_KEY_check_key(key
) == 0)
647 EC_POINT_free(point
);
652 OSSL_LIB_CTX
*ec_key_get_libctx(const EC_KEY
*key
)
657 const char *ec_key_get0_propq(const EC_KEY
*key
)
662 const EC_GROUP
*EC_KEY_get0_group(const EC_KEY
*key
)
667 int EC_KEY_set_group(EC_KEY
*key
, const EC_GROUP
*group
)
669 if (key
->meth
->set_group
!= NULL
&& key
->meth
->set_group(key
, group
) == 0)
671 EC_GROUP_free(key
->group
);
672 key
->group
= EC_GROUP_dup(group
);
674 return (key
->group
== NULL
) ? 0 : 1;
677 const BIGNUM
*EC_KEY_get0_private_key(const EC_KEY
*key
)
679 return key
->priv_key
;
682 int EC_KEY_set_private_key(EC_KEY
*key
, const BIGNUM
*priv_key
)
685 const BIGNUM
*order
= NULL
;
686 BIGNUM
*tmp_key
= NULL
;
688 if (key
->group
== NULL
|| key
->group
->meth
== NULL
)
692 * Not only should key->group be set, but it should also be in a valid
693 * fully initialized state.
695 * Specifically, to operate in constant time, we need that the group order
696 * is set, as we use its length as the fixed public size of any scalar used
697 * as an EC private key.
699 order
= EC_GROUP_get0_order(key
->group
);
700 if (order
== NULL
|| BN_is_zero(order
))
701 return 0; /* This should never happen */
703 if (key
->group
->meth
->set_private
!= NULL
704 && key
->group
->meth
->set_private(key
, priv_key
) == 0)
706 if (key
->meth
->set_private
!= NULL
707 && key
->meth
->set_private(key
, priv_key
) == 0)
711 * We should never leak the bit length of the secret scalar in the key,
712 * so we always set the `BN_FLG_CONSTTIME` flag on the internal `BIGNUM`
713 * holding the secret scalar.
715 * This is important also because `BN_dup()` (and `BN_copy()`) do not
716 * propagate the `BN_FLG_CONSTTIME` flag from the source `BIGNUM`, and
717 * this brings an extra risk of inadvertently losing the flag, even when
718 * the caller specifically set it.
720 * The propagation has been turned on and off a few times in the past
721 * years because in some conditions has shown unintended consequences in
722 * some code paths, so at the moment we can't fix this in the BN layer.
724 * In `EC_KEY_set_private_key()` we can work around the propagation by
725 * manually setting the flag after `BN_dup()` as we know for sure that
726 * inside the EC module the `BN_FLG_CONSTTIME` is always treated
727 * correctly and should not generate unintended consequences.
729 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
730 * to preallocate the BIGNUM internal buffer to a fixed public size big
731 * enough that operations performed during the processing never trigger
732 * a realloc which would leak the size of the scalar through memory
738 * The order of the large prime subgroup of the curve is our choice for
739 * a fixed public size, as that is generally the upper bound for
740 * generating a private key in EC cryptosystems and should fit all valid
743 * For preallocating the BIGNUM storage we look at the number of "words"
744 * required for the internal representation of the order, and we
745 * preallocate 2 extra "words" in case any of the subsequent processing
746 * might temporarily overflow the order length.
748 tmp_key
= BN_dup(priv_key
);
752 BN_set_flags(tmp_key
, BN_FLG_CONSTTIME
);
754 fixed_top
= bn_get_top(order
) + 2;
755 if (bn_wexpand(tmp_key
, fixed_top
) == NULL
) {
756 BN_clear_free(tmp_key
);
760 BN_clear_free(key
->priv_key
);
761 key
->priv_key
= tmp_key
;
767 const EC_POINT
*EC_KEY_get0_public_key(const EC_KEY
*key
)
772 int EC_KEY_set_public_key(EC_KEY
*key
, const EC_POINT
*pub_key
)
774 if (key
->meth
->set_public
!= NULL
775 && key
->meth
->set_public(key
, pub_key
) == 0)
777 EC_POINT_free(key
->pub_key
);
778 key
->pub_key
= EC_POINT_dup(pub_key
, key
->group
);
780 return (key
->pub_key
== NULL
) ? 0 : 1;
783 unsigned int EC_KEY_get_enc_flags(const EC_KEY
*key
)
785 return key
->enc_flag
;
788 void EC_KEY_set_enc_flags(EC_KEY
*key
, unsigned int flags
)
790 key
->enc_flag
= flags
;
793 point_conversion_form_t
EC_KEY_get_conv_form(const EC_KEY
*key
)
795 return key
->conv_form
;
798 void EC_KEY_set_conv_form(EC_KEY
*key
, point_conversion_form_t cform
)
800 key
->conv_form
= cform
;
801 if (key
->group
!= NULL
)
802 EC_GROUP_set_point_conversion_form(key
->group
, cform
);
805 void EC_KEY_set_asn1_flag(EC_KEY
*key
, int flag
)
807 if (key
->group
!= NULL
)
808 EC_GROUP_set_asn1_flag(key
->group
, flag
);
811 #ifndef OPENSSL_NO_DEPRECATED_3_0
812 int EC_KEY_precompute_mult(EC_KEY
*key
, BN_CTX
*ctx
)
814 if (key
->group
== NULL
)
816 return EC_GROUP_precompute_mult(key
->group
, ctx
);
820 int EC_KEY_get_flags(const EC_KEY
*key
)
825 void EC_KEY_set_flags(EC_KEY
*key
, int flags
)
831 void EC_KEY_clear_flags(EC_KEY
*key
, int flags
)
833 key
->flags
&= ~flags
;
837 int EC_KEY_decoded_from_explicit_params(const EC_KEY
*key
)
839 if (key
== NULL
|| key
->group
== NULL
)
841 return key
->group
->decoded_from_explicit_params
;
844 size_t EC_KEY_key2buf(const EC_KEY
*key
, point_conversion_form_t form
,
845 unsigned char **pbuf
, BN_CTX
*ctx
)
847 if (key
== NULL
|| key
->pub_key
== NULL
|| key
->group
== NULL
)
849 return EC_POINT_point2buf(key
->group
, key
->pub_key
, form
, pbuf
, ctx
);
852 int EC_KEY_oct2key(EC_KEY
*key
, const unsigned char *buf
, size_t len
,
855 if (key
== NULL
|| key
->group
== NULL
)
857 if (key
->pub_key
== NULL
)
858 key
->pub_key
= EC_POINT_new(key
->group
);
859 if (key
->pub_key
== NULL
)
861 if (EC_POINT_oct2point(key
->group
, key
->pub_key
, buf
, len
, ctx
) == 0)
865 * Save the point conversion form.
866 * For non-custom curves the first octet of the buffer (excluding
867 * the last significant bit) contains the point conversion form.
868 * EC_POINT_oct2point() has already performed sanity checking of
869 * the buffer so we know it is valid.
871 if ((key
->group
->meth
->flags
& EC_FLAGS_CUSTOM_CURVE
) == 0)
872 key
->conv_form
= (point_conversion_form_t
)(buf
[0] & ~0x01);
876 size_t EC_KEY_priv2oct(const EC_KEY
*eckey
,
877 unsigned char *buf
, size_t len
)
879 if (eckey
->group
== NULL
|| eckey
->group
->meth
== NULL
)
881 if (eckey
->group
->meth
->priv2oct
== NULL
) {
882 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
886 return eckey
->group
->meth
->priv2oct(eckey
, buf
, len
);
889 size_t ec_key_simple_priv2oct(const EC_KEY
*eckey
,
890 unsigned char *buf
, size_t len
)
894 buf_len
= (EC_GROUP_order_bits(eckey
->group
) + 7) / 8;
895 if (eckey
->priv_key
== NULL
)
899 else if (len
< buf_len
)
902 /* Octetstring may need leading zeros if BN is to short */
904 if (BN_bn2binpad(eckey
->priv_key
, buf
, buf_len
) == -1) {
905 ERR_raise(ERR_LIB_EC
, EC_R_BUFFER_TOO_SMALL
);
912 int EC_KEY_oct2priv(EC_KEY
*eckey
, const unsigned char *buf
, size_t len
)
916 if (eckey
->group
== NULL
|| eckey
->group
->meth
== NULL
)
918 if (eckey
->group
->meth
->oct2priv
== NULL
) {
919 ERR_raise(ERR_LIB_EC
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
922 ret
= eckey
->group
->meth
->oct2priv(eckey
, buf
, len
);
928 int ec_key_simple_oct2priv(EC_KEY
*eckey
, const unsigned char *buf
, size_t len
)
930 if (eckey
->priv_key
== NULL
)
931 eckey
->priv_key
= BN_secure_new();
932 if (eckey
->priv_key
== NULL
) {
933 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
936 eckey
->priv_key
= BN_bin2bn(buf
, len
, eckey
->priv_key
);
937 if (eckey
->priv_key
== NULL
) {
938 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
945 size_t EC_KEY_priv2buf(const EC_KEY
*eckey
, unsigned char **pbuf
)
950 len
= EC_KEY_priv2oct(eckey
, NULL
, 0);
953 if ((buf
= OPENSSL_malloc(len
)) == NULL
) {
954 ERR_raise(ERR_LIB_EC
, ERR_R_MALLOC_FAILURE
);
957 len
= EC_KEY_priv2oct(eckey
, buf
, len
);
966 int EC_KEY_can_sign(const EC_KEY
*eckey
)
968 if (eckey
->group
== NULL
|| eckey
->group
->meth
== NULL
969 || (eckey
->group
->meth
->flags
& EC_FLAGS_NO_SIGN
))
975 * FIPS 140-2 IG 9.9 AS09.33
976 * Perform a sign/verify operation.
978 * NOTE: When generating keys for key-agreement schemes - FIPS 140-2 IG 9.9
979 * states that no additional pairwise tests are required (apart from the tests
980 * specified in SP800-56A) when generating keys. Hence pairwise ECDH tests are
983 static int ecdsa_keygen_pairwise_test(EC_KEY
*eckey
, OSSL_CALLBACK
*cb
,
987 unsigned char dgst
[16] = {0};
988 int dgst_len
= (int)sizeof(dgst
);
989 ECDSA_SIG
*sig
= NULL
;
990 OSSL_SELF_TEST
*st
= NULL
;
992 st
= OSSL_SELF_TEST_new(cb
, cbarg
);
996 OSSL_SELF_TEST_onbegin(st
, OSSL_SELF_TEST_TYPE_PCT
,
997 OSSL_SELF_TEST_DESC_PCT_ECDSA
);
999 sig
= ECDSA_do_sign(dgst
, dgst_len
, eckey
);
1003 OSSL_SELF_TEST_oncorrupt_byte(st
, dgst
);
1005 if (ECDSA_do_verify(dgst
, dgst_len
, sig
, eckey
) != 1)
1010 OSSL_SELF_TEST_onend(st
, ret
);
1011 OSSL_SELF_TEST_free(st
);
1012 ECDSA_SIG_free(sig
);