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 "crypto/bn.h"
26 static int ecdsa_keygen_pairwise_test(EC_KEY *eckey, OSSL_CALLBACK *cb,
30 EC_KEY *EC_KEY_new(void)
32 return ec_key_new_method_int(NULL, NULL, NULL);
36 EC_KEY *EC_KEY_new_with_libctx(OPENSSL_CTX *ctx, const char *propq)
38 return ec_key_new_method_int(ctx, propq, NULL);
41 EC_KEY *EC_KEY_new_by_curve_name_with_libctx(OPENSSL_CTX *ctx,
42 const char *propq, int nid)
44 EC_KEY *ret = EC_KEY_new_with_libctx(ctx, propq);
47 ret->group = EC_GROUP_new_by_curve_name_with_libctx(ctx, propq, nid);
48 if (ret->group == NULL) {
52 if (ret->meth->set_group != NULL
53 && ret->meth->set_group(ret, ret->group) == 0) {
61 EC_KEY *EC_KEY_new_by_curve_name(int nid)
63 return EC_KEY_new_by_curve_name_with_libctx(NULL, NULL, nid);
67 void EC_KEY_free(EC_KEY *r)
74 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
75 REF_PRINT_COUNT("EC_KEY", r);
78 REF_ASSERT_ISNT(i < 0);
80 if (r->meth != NULL && r->meth->finish != NULL)
83 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
84 ENGINE_finish(r->engine);
87 if (r->group && r->group->meth->keyfinish)
88 r->group->meth->keyfinish(r);
91 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
93 CRYPTO_THREAD_lock_free(r->lock);
94 EC_GROUP_free(r->group);
95 EC_POINT_free(r->pub_key);
96 BN_clear_free(r->priv_key);
97 OPENSSL_free(r->propq);
99 OPENSSL_clear_free((void *)r, sizeof(EC_KEY));
102 EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
104 if (dest == NULL || src == NULL) {
105 ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
108 if (src->meth != dest->meth) {
109 if (dest->meth->finish != NULL)
110 dest->meth->finish(dest);
111 if (dest->group && dest->group->meth->keyfinish)
112 dest->group->meth->keyfinish(dest);
113 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
114 if (ENGINE_finish(dest->engine) == 0)
119 dest->libctx = src->libctx;
120 /* copy the parameters */
121 if (src->group != NULL) {
122 /* clear the old group */
123 EC_GROUP_free(dest->group);
124 dest->group = ec_group_new_with_libctx(src->libctx, src->propq,
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 ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
222 if (eckey->meth->keygen != NULL) {
225 ret = eckey->meth->keygen(eckey);
231 ECerr(EC_F_EC_KEY_GENERATE_KEY, 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 int ec_generate_key(OPENSSL_CTX *libctx, EC_KEY *eckey, int pairwise_test)
261 BIGNUM *priv_key = NULL;
262 const BIGNUM *order = NULL;
263 EC_POINT *pub_key = NULL;
264 const EC_GROUP *group = eckey->group;
265 BN_CTX *ctx = BN_CTX_secure_new_ex(eckey->libctx);
270 if (eckey->priv_key == NULL) {
271 priv_key = BN_secure_new();
272 if (priv_key == NULL)
275 priv_key = eckey->priv_key;
278 * Steps (1-2): Check domain parameters and security strength.
279 * These steps must be done by the user. This would need to be
280 * stated in the security policy.
283 order = EC_GROUP_get0_order(group);
288 * Steps (3-7): priv_key = DRBG_RAND(order_n_bits) (range [1, n-1]).
289 * Although this is slightly different from the standard, it is effectively
290 * equivalent as it gives an unbiased result ranging from 1..n-1. It is also
291 * faster as the standard needs to retry more often. Also doing
292 * 1 + rand[0..n-2] would effect the way that tests feed dummy entropy into
293 * rand so the simpler backward compatible method has been used here.
296 if (!BN_priv_rand_range_ex(priv_key, order, ctx))
298 while (BN_is_zero(priv_key)) ;
300 if (eckey->pub_key == NULL) {
301 pub_key = EC_POINT_new(group);
305 pub_key = eckey->pub_key;
307 /* Step (8) : pub_key = priv_key * G (where G is a point on the curve) */
308 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx))
311 eckey->priv_key = priv_key;
312 eckey->pub_key = pub_key;
320 #endif /* FIPS_MODULE */
324 OSSL_CALLBACK *cb = NULL;
327 OSSL_SELF_TEST_get_callback(libctx, &cb, &cbarg);
328 ok = ecdsa_keygen_pairwise_test(eckey, cb, cbarg);
331 /* Step (9): If there is an error return an invalid keypair. */
333 BN_clear(eckey->priv_key);
334 if (eckey->pub_key != NULL)
335 EC_POINT_set_to_infinity(group, eckey->pub_key);
338 EC_POINT_free(pub_key);
339 BN_clear_free(priv_key);
344 int ec_key_simple_generate_key(EC_KEY *eckey)
346 return ec_generate_key(NULL, eckey, 0);
349 int ec_key_simple_generate_public_key(EC_KEY *eckey)
354 * See SP800-56AR3 5.6.1.2.2: Step (8)
355 * pub_key = priv_key * G (where G is a point on the curve)
357 ret = EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
366 int EC_KEY_check_key(const EC_KEY *eckey)
368 if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
369 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
373 if (eckey->group->meth->keycheck == NULL) {
374 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
378 return eckey->group->meth->keycheck(eckey);
382 * Check the range of the EC public key.
383 * See SP800-56A R3 Section 5.6.2.3.3 (Part 2)
385 * - If q = odd prime p: Verify that xQ and yQ are integers in the
386 * interval[0, p - 1], OR
387 * - If q = 2m: Verify that xQ and yQ are bit strings of length m bits.
388 * Returns 1 if the public key has a valid range, otherwise it returns 0.
390 static int ec_key_public_range_check(BN_CTX *ctx, const EC_KEY *key)
401 if (!EC_POINT_get_affine_coordinates(key->group, key->pub_key, x, y, ctx))
404 if (EC_GROUP_get_field_type(key->group) == NID_X9_62_prime_field) {
405 if (BN_is_negative(x)
406 || BN_cmp(x, key->group->field) >= 0
408 || BN_cmp(y, key->group->field) >= 0) {
412 int m = EC_GROUP_get_degree(key->group);
413 if (BN_num_bits(x) > m || BN_num_bits(y) > m) {
424 * ECC Key validation as specified in SP800-56A R3.
425 * Section 5.6.2.3.3 ECC Full Public-Key Validation.
427 int ec_key_public_check(const EC_KEY *eckey, BN_CTX *ctx)
430 EC_POINT *point = NULL;
431 const BIGNUM *order = NULL;
433 if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
434 ECerr(0, ERR_R_PASSED_NULL_PARAMETER);
438 /* 5.6.2.3.3 (Step 1): Q != infinity */
439 if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
440 ECerr(0, EC_R_POINT_AT_INFINITY);
444 point = EC_POINT_new(eckey->group);
448 /* 5.6.2.3.3 (Step 2) Test if the public key is in range */
449 if (!ec_key_public_range_check(ctx, eckey)) {
450 ECerr(0, EC_R_COORDINATES_OUT_OF_RANGE);
454 /* 5.6.2.3.3 (Step 3) is the pub_key on the elliptic curve */
455 if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
456 ECerr(0, EC_R_POINT_IS_NOT_ON_CURVE);
460 order = eckey->group->order;
461 if (BN_is_zero(order)) {
462 ECerr(0, EC_R_INVALID_GROUP_ORDER);
465 /* 5.6.2.3.3 (Step 4) : pub_key * order is the point at infinity. */
466 if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
467 ECerr(0, ERR_R_EC_LIB);
470 if (!EC_POINT_is_at_infinity(eckey->group, point)) {
471 ECerr(0, EC_R_WRONG_ORDER);
476 EC_POINT_free(point);
481 * ECC Key validation as specified in SP800-56A R3.
482 * Section 5.6.2.1.2 Owner Assurance of Private-Key Validity
483 * The private key is in the range [1, order-1]
485 int ec_key_private_check(const EC_KEY *eckey)
487 if (eckey == NULL || eckey->group == NULL || eckey->priv_key == NULL) {
488 ECerr(0, ERR_R_PASSED_NULL_PARAMETER);
491 if (BN_cmp(eckey->priv_key, BN_value_one()) < 0
492 || BN_cmp(eckey->priv_key, eckey->group->order) >= 0) {
493 ECerr(0, EC_R_INVALID_PRIVATE_KEY);
500 * ECC Key validation as specified in SP800-56A R3.
501 * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency (b)
502 * Check if generator * priv_key = pub_key
504 int ec_key_pairwise_check(const EC_KEY *eckey, BN_CTX *ctx)
507 EC_POINT *point = NULL;
510 || eckey->group == NULL
511 || eckey->pub_key == NULL
512 || eckey->priv_key == NULL) {
513 ECerr(0, ERR_R_PASSED_NULL_PARAMETER);
517 point = EC_POINT_new(eckey->group);
522 if (!EC_POINT_mul(eckey->group, point, eckey->priv_key, NULL, NULL, ctx)) {
523 ECerr(0, ERR_R_EC_LIB);
526 if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
527 ECerr(0, EC_R_INVALID_PRIVATE_KEY);
532 EC_POINT_free(point);
538 * ECC Key validation as specified in SP800-56A R3.
539 * Section 5.6.2.3.3 ECC Full Public-Key Validation
540 * Section 5.6.2.1.2 Owner Assurance of Private-Key Validity
541 * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
543 * Before calling this method in fips mode, there should be an assurance that
544 * an approved elliptic-curve group is used.
545 * Returns 1 if the key is valid, otherwise it returns 0.
547 int ec_key_simple_check_key(const EC_KEY *eckey)
553 ECerr(0, ERR_R_PASSED_NULL_PARAMETER);
556 if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL)
559 if (!ec_key_public_check(eckey, ctx))
562 if (eckey->priv_key != NULL) {
563 if (!ec_key_private_check(eckey)
564 || !ec_key_pairwise_check(eckey, ctx))
573 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
578 EC_POINT *point = NULL;
581 if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
582 ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
583 ERR_R_PASSED_NULL_PARAMETER);
586 ctx = BN_CTX_new_ex(key->libctx);
591 point = EC_POINT_new(key->group);
596 tx = BN_CTX_get(ctx);
597 ty = BN_CTX_get(ctx);
601 if (!EC_POINT_set_affine_coordinates(key->group, point, x, y, ctx))
603 if (!EC_POINT_get_affine_coordinates(key->group, point, tx, ty, ctx))
607 * Check if retrieved coordinates match originals. The range check is done
608 * inside EC_KEY_check_key().
610 if (BN_cmp(x, tx) || BN_cmp(y, ty)) {
611 ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
612 EC_R_COORDINATES_OUT_OF_RANGE);
616 /* EC_KEY_set_public_key updates dirty_cnt */
617 if (!EC_KEY_set_public_key(key, point))
620 if (EC_KEY_check_key(key) == 0)
628 EC_POINT_free(point);
633 OPENSSL_CTX *ec_key_get_libctx(const EC_KEY *key)
638 const char *ec_key_get0_propq(const EC_KEY *key)
643 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
648 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
650 if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)
652 EC_GROUP_free(key->group);
653 key->group = EC_GROUP_dup(group);
655 return (key->group == NULL) ? 0 : 1;
658 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
660 return key->priv_key;
663 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
666 const BIGNUM *order = NULL;
667 BIGNUM *tmp_key = NULL;
669 if (key->group == NULL || key->group->meth == NULL)
673 * Not only should key->group be set, but it should also be in a valid
674 * fully initialized state.
676 * Specifically, to operate in constant time, we need that the group order
677 * is set, as we use its length as the fixed public size of any scalar used
678 * as an EC private key.
680 order = EC_GROUP_get0_order(key->group);
681 if (order == NULL || BN_is_zero(order))
682 return 0; /* This should never happen */
684 if (key->group->meth->set_private != NULL
685 && key->group->meth->set_private(key, priv_key) == 0)
687 if (key->meth->set_private != NULL
688 && key->meth->set_private(key, priv_key) == 0)
692 * We should never leak the bit length of the secret scalar in the key,
693 * so we always set the `BN_FLG_CONSTTIME` flag on the internal `BIGNUM`
694 * holding the secret scalar.
696 * This is important also because `BN_dup()` (and `BN_copy()`) do not
697 * propagate the `BN_FLG_CONSTTIME` flag from the source `BIGNUM`, and
698 * this brings an extra risk of inadvertently losing the flag, even when
699 * the caller specifically set it.
701 * The propagation has been turned on and off a few times in the past
702 * years because in some conditions has shown unintended consequences in
703 * some code paths, so at the moment we can't fix this in the BN layer.
705 * In `EC_KEY_set_private_key()` we can work around the propagation by
706 * manually setting the flag after `BN_dup()` as we know for sure that
707 * inside the EC module the `BN_FLG_CONSTTIME` is always treated
708 * correctly and should not generate unintended consequences.
710 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
711 * to preallocate the BIGNUM internal buffer to a fixed public size big
712 * enough that operations performed during the processing never trigger
713 * a realloc which would leak the size of the scalar through memory
719 * The order of the large prime subgroup of the curve is our choice for
720 * a fixed public size, as that is generally the upper bound for
721 * generating a private key in EC cryptosystems and should fit all valid
724 * For preallocating the BIGNUM storage we look at the number of "words"
725 * required for the internal representation of the order, and we
726 * preallocate 2 extra "words" in case any of the subsequent processing
727 * might temporarily overflow the order length.
729 tmp_key = BN_dup(priv_key);
733 BN_set_flags(tmp_key, BN_FLG_CONSTTIME);
735 fixed_top = bn_get_top(order) + 2;
736 if (bn_wexpand(tmp_key, fixed_top) == NULL) {
737 BN_clear_free(tmp_key);
741 BN_clear_free(key->priv_key);
742 key->priv_key = tmp_key;
748 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
753 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
755 if (key->meth->set_public != NULL
756 && key->meth->set_public(key, pub_key) == 0)
758 EC_POINT_free(key->pub_key);
759 key->pub_key = EC_POINT_dup(pub_key, key->group);
761 return (key->pub_key == NULL) ? 0 : 1;
764 unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
766 return key->enc_flag;
769 void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
771 key->enc_flag = flags;
774 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
776 return key->conv_form;
779 void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
781 key->conv_form = cform;
782 if (key->group != NULL)
783 EC_GROUP_set_point_conversion_form(key->group, cform);
786 void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
788 if (key->group != NULL)
789 EC_GROUP_set_asn1_flag(key->group, flag);
792 #ifndef OPENSSL_NO_DEPRECATED_3_0
793 int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
795 if (key->group == NULL)
797 return EC_GROUP_precompute_mult(key->group, ctx);
801 int EC_KEY_get_flags(const EC_KEY *key)
806 void EC_KEY_set_flags(EC_KEY *key, int flags)
812 void EC_KEY_clear_flags(EC_KEY *key, int flags)
814 key->flags &= ~flags;
818 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
819 unsigned char **pbuf, BN_CTX *ctx)
821 if (key == NULL || key->pub_key == NULL || key->group == NULL)
823 return EC_POINT_point2buf(key->group, key->pub_key, form, pbuf, ctx);
826 int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
829 if (key == NULL || key->group == NULL)
831 if (key->pub_key == NULL)
832 key->pub_key = EC_POINT_new(key->group);
833 if (key->pub_key == NULL)
835 if (EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx) == 0)
839 * Save the point conversion form.
840 * For non-custom curves the first octet of the buffer (excluding
841 * the last significant bit) contains the point conversion form.
842 * EC_POINT_oct2point() has already performed sanity checking of
843 * the buffer so we know it is valid.
845 if ((key->group->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0)
846 key->conv_form = (point_conversion_form_t)(buf[0] & ~0x01);
850 size_t EC_KEY_priv2oct(const EC_KEY *eckey,
851 unsigned char *buf, size_t len)
853 if (eckey->group == NULL || eckey->group->meth == NULL)
855 if (eckey->group->meth->priv2oct == NULL) {
856 ECerr(EC_F_EC_KEY_PRIV2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
860 return eckey->group->meth->priv2oct(eckey, buf, len);
863 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
864 unsigned char *buf, size_t len)
868 buf_len = (EC_GROUP_order_bits(eckey->group) + 7) / 8;
869 if (eckey->priv_key == NULL)
873 else if (len < buf_len)
876 /* Octetstring may need leading zeros if BN is to short */
878 if (BN_bn2binpad(eckey->priv_key, buf, buf_len) == -1) {
879 ECerr(EC_F_EC_KEY_SIMPLE_PRIV2OCT, EC_R_BUFFER_TOO_SMALL);
886 int EC_KEY_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
890 if (eckey->group == NULL || eckey->group->meth == NULL)
892 if (eckey->group->meth->oct2priv == NULL) {
893 ECerr(EC_F_EC_KEY_OCT2PRIV, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
896 ret = eckey->group->meth->oct2priv(eckey, buf, len);
902 int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
904 if (eckey->priv_key == NULL)
905 eckey->priv_key = BN_secure_new();
906 if (eckey->priv_key == NULL) {
907 ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_MALLOC_FAILURE);
910 eckey->priv_key = BN_bin2bn(buf, len, eckey->priv_key);
911 if (eckey->priv_key == NULL) {
912 ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_BN_LIB);
919 size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf)
924 len = EC_KEY_priv2oct(eckey, NULL, 0);
927 if ((buf = OPENSSL_malloc(len)) == NULL) {
928 ECerr(EC_F_EC_KEY_PRIV2BUF, ERR_R_MALLOC_FAILURE);
931 len = EC_KEY_priv2oct(eckey, buf, len);
940 int EC_KEY_can_sign(const EC_KEY *eckey)
942 if (eckey->group == NULL || eckey->group->meth == NULL
943 || (eckey->group->meth->flags & EC_FLAGS_NO_SIGN))
949 * FIPS 140-2 IG 9.9 AS09.33
950 * Perform a sign/verify operation.
952 * NOTE: When generating keys for key-agreement schemes - FIPS 140-2 IG 9.9
953 * states that no additional pairwise tests are required (apart from the tests
954 * specified in SP800-56A) when generating keys. Hence pairwise ECDH tests are
957 static int ecdsa_keygen_pairwise_test(EC_KEY *eckey, OSSL_CALLBACK *cb,
961 unsigned char dgst[16] = {0};
962 int dgst_len = (int)sizeof(dgst);
963 ECDSA_SIG *sig = NULL;
964 OSSL_SELF_TEST *st = NULL;
966 st = OSSL_SELF_TEST_new(cb, cbarg);
970 OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT,
971 OSSL_SELF_TEST_DESC_PCT_ECDSA);
973 sig = ECDSA_do_sign(dgst, dgst_len, eckey);
977 OSSL_SELF_TEST_oncorrupt_byte(st, dgst);
979 if (ECDSA_do_verify(dgst, dgst_len, sig, eckey) != 1)
984 OSSL_SELF_TEST_onend(st, ret);
985 OSSL_SELF_TEST_free(st);