2 * Copyright 2002-2019 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>
25 EC_KEY *EC_KEY_new(void)
27 return ec_key_new_method_int(NULL, NULL);
31 EC_KEY *EC_KEY_new_ex(OPENSSL_CTX *ctx)
33 return ec_key_new_method_int(ctx, NULL);
36 EC_KEY *EC_KEY_new_by_curve_name_ex(OPENSSL_CTX *ctx, int nid)
38 EC_KEY *ret = EC_KEY_new_ex(ctx);
41 ret->group = EC_GROUP_new_by_curve_name_ex(ctx, nid);
42 if (ret->group == NULL) {
46 if (ret->meth->set_group != NULL
47 && ret->meth->set_group(ret, ret->group) == 0) {
55 EC_KEY *EC_KEY_new_by_curve_name(int nid)
57 return EC_KEY_new_by_curve_name_ex(NULL, nid);
61 void EC_KEY_free(EC_KEY *r)
68 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
69 REF_PRINT_COUNT("EC_KEY", r);
72 REF_ASSERT_ISNT(i < 0);
74 if (r->meth != NULL && r->meth->finish != NULL)
77 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
78 ENGINE_finish(r->engine);
81 if (r->group && r->group->meth->keyfinish)
82 r->group->meth->keyfinish(r);
85 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
87 CRYPTO_THREAD_lock_free(r->lock);
88 EC_GROUP_free(r->group);
89 EC_POINT_free(r->pub_key);
90 BN_clear_free(r->priv_key);
92 OPENSSL_clear_free((void *)r, sizeof(EC_KEY));
95 EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
97 if (dest == NULL || src == NULL) {
98 ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
101 if (src->meth != dest->meth) {
102 if (dest->meth->finish != NULL)
103 dest->meth->finish(dest);
104 if (dest->group && dest->group->meth->keyfinish)
105 dest->group->meth->keyfinish(dest);
106 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
107 if (ENGINE_finish(dest->engine) == 0)
112 dest->libctx = src->libctx;
113 /* copy the parameters */
114 if (src->group != NULL) {
115 const EC_METHOD *meth = EC_GROUP_method_of(src->group);
116 /* clear the old group */
117 EC_GROUP_free(dest->group);
118 dest->group = EC_GROUP_new_ex(src->libctx, meth);
119 if (dest->group == NULL)
121 if (!EC_GROUP_copy(dest->group, src->group))
124 /* copy the public key */
125 if (src->pub_key != NULL) {
126 EC_POINT_free(dest->pub_key);
127 dest->pub_key = EC_POINT_new(src->group);
128 if (dest->pub_key == NULL)
130 if (!EC_POINT_copy(dest->pub_key, src->pub_key))
133 /* copy the private key */
134 if (src->priv_key != NULL) {
135 if (dest->priv_key == NULL) {
136 dest->priv_key = BN_new();
137 if (dest->priv_key == NULL)
140 if (!BN_copy(dest->priv_key, src->priv_key))
142 if (src->group->meth->keycopy
143 && src->group->meth->keycopy(dest, src) == 0)
150 dest->enc_flag = src->enc_flag;
151 dest->conv_form = src->conv_form;
152 dest->version = src->version;
153 dest->flags = src->flags;
155 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
156 &dest->ex_data, &src->ex_data))
160 if (src->meth != dest->meth) {
161 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
162 if (src->engine != NULL && ENGINE_init(src->engine) == 0)
164 dest->engine = src->engine;
166 dest->meth = src->meth;
169 if (src->meth->copy != NULL && src->meth->copy(dest, src) == 0)
175 EC_KEY *EC_KEY_dup(const EC_KEY *ec_key)
177 EC_KEY *ret = ec_key_new_method_int(ec_key->libctx, ec_key->engine);
182 if (EC_KEY_copy(ret, ec_key) == NULL) {
189 int EC_KEY_up_ref(EC_KEY *r)
193 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
196 REF_PRINT_COUNT("EC_KEY", r);
197 REF_ASSERT_ISNT(i < 2);
198 return ((i > 1) ? 1 : 0);
201 ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey)
203 return eckey->engine;
206 int EC_KEY_generate_key(EC_KEY *eckey)
208 if (eckey == NULL || eckey->group == NULL) {
209 ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
212 if (eckey->meth->keygen != NULL)
213 return eckey->meth->keygen(eckey);
214 ECerr(EC_F_EC_KEY_GENERATE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
218 int ossl_ec_key_gen(EC_KEY *eckey)
220 return eckey->group->meth->keygen(eckey);
224 * ECC Key generation.
225 * See SP800-56AR3 5.6.1.2.2 "Key Pair Generation by Testing Candidates"
228 * eckey An EC key object that contains domain params. The generated keypair
229 * is stored in this object.
230 * Returns 1 if the keypair was generated or 0 otherwise.
232 int ec_key_simple_generate_key(EC_KEY *eckey)
235 BIGNUM *priv_key = NULL;
236 const BIGNUM *order = NULL;
237 EC_POINT *pub_key = NULL;
238 const EC_GROUP *group = eckey->group;
239 BN_CTX *ctx = BN_CTX_secure_new_ex(eckey->libctx);
244 if (eckey->priv_key == NULL) {
245 priv_key = BN_secure_new();
246 if (priv_key == NULL)
249 priv_key = eckey->priv_key;
252 * Steps (1-2): Check domain parameters and security strength.
253 * These steps must be done by the user. This would need to be
254 * stated in the security policy.
257 order = EC_GROUP_get0_order(group);
262 * Steps (3-7): priv_key = DRBG_RAND(order_n_bits) (range [1, n-1]).
263 * Although this is slightly different from the standard, it is effectively
264 * equivalent as it gives an unbiased result ranging from 1..n-1. It is also
265 * faster as the standard needs to retry more often. Also doing
266 * 1 + rand[0..n-2] would effect the way that tests feed dummy entropy into
267 * rand so the simpler backward compatible method has been used here.
270 if (!BN_priv_rand_range_ex(priv_key, order, ctx))
272 while (BN_is_zero(priv_key)) ;
274 if (eckey->pub_key == NULL) {
275 pub_key = EC_POINT_new(group);
279 pub_key = eckey->pub_key;
281 /* Step (8) : pub_key = priv_key * G (where G is a point on the curve) */
282 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx))
285 eckey->priv_key = priv_key;
286 eckey->pub_key = pub_key;
293 /* Step (9): If there is an error return an invalid keypair. */
295 BN_clear(eckey->priv_key);
296 if (eckey->pub_key != NULL)
297 EC_POINT_set_to_infinity(group, eckey->pub_key);
300 EC_POINT_free(pub_key);
301 BN_clear_free(priv_key);
306 int ec_key_simple_generate_public_key(EC_KEY *eckey)
309 * See SP800-56AR3 5.6.1.2.2: Step (8)
310 * pub_key = priv_key * G (where G is a point on the curve)
312 return EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
316 int EC_KEY_check_key(const EC_KEY *eckey)
318 if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
319 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
323 if (eckey->group->meth->keycheck == NULL) {
324 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
328 return eckey->group->meth->keycheck(eckey);
332 * Check the range of the EC public key.
333 * See SP800-56A R3 Section 5.6.2.3.3 (Part 2)
335 * - If q = odd prime p: Verify that xQ and yQ are integers in the
336 * interval[0, p - 1], OR
337 * - If q = 2m: Verify that xQ and yQ are bit strings of length m bits.
338 * Returns 1 if the public key has a valid range, otherwise it returns 0.
340 static int ec_key_public_range_check(BN_CTX *ctx, const EC_KEY *key)
351 if (!EC_POINT_get_affine_coordinates(key->group, key->pub_key, x, y, ctx))
354 if (EC_METHOD_get_field_type(key->group->meth) == NID_X9_62_prime_field) {
355 if (BN_is_negative(x)
356 || BN_cmp(x, key->group->field) >= 0
358 || BN_cmp(y, key->group->field) >= 0) {
362 int m = EC_GROUP_get_degree(key->group);
363 if (BN_num_bits(x) > m || BN_num_bits(y) > m) {
374 * ECC Key validation as specified in SP800-56A R3.
375 * Section 5.6.2.3.3 ECC Full Public-Key Validation
376 * Section 5.6.2.1.2 Owner Assurance of Private-Key Validity
377 * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
379 * Before calling this method in fips mode, there should be an assurance that
380 * an approved elliptic-curve group is used.
381 * Returns 1 if the key is valid, otherwise it returns 0.
383 int ec_key_simple_check_key(const EC_KEY *eckey)
387 const BIGNUM *order = NULL;
388 EC_POINT *point = NULL;
390 if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
391 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
395 /* 5.6.2.3.3 (Step 1): Q != infinity */
396 if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
397 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_AT_INFINITY);
401 if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL)
404 if ((point = EC_POINT_new(eckey->group)) == NULL)
407 /* 5.6.2.3.3 (Step 2) Test if the public key is in range */
408 if (!ec_key_public_range_check(ctx, eckey)) {
409 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_COORDINATES_OUT_OF_RANGE);
413 /* 5.6.2.3.3 (Step 3) is the pub_key on the elliptic curve */
414 if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
415 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
419 order = eckey->group->order;
420 if (BN_is_zero(order)) {
421 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
424 /* 5.6.2.3.3 (Step 4) : pub_key * order is the point at infinity. */
425 if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
426 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
429 if (!EC_POINT_is_at_infinity(eckey->group, point)) {
430 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
434 if (eckey->priv_key != NULL) {
436 * 5.6.2.1.2 Owner Assurance of Private-Key Validity
437 * The private key is in the range [1, order-1]
439 if (BN_cmp(eckey->priv_key, BN_value_one()) < 0
440 || BN_cmp(eckey->priv_key, order) >= 0) {
441 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
445 * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency (b)
446 * Check if generator * priv_key = pub_key
448 if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
450 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
453 if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
454 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
461 EC_POINT_free(point);
465 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
470 EC_POINT *point = NULL;
473 if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
474 ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
475 ERR_R_PASSED_NULL_PARAMETER);
478 ctx = BN_CTX_new_ex(key->libctx);
483 point = EC_POINT_new(key->group);
488 tx = BN_CTX_get(ctx);
489 ty = BN_CTX_get(ctx);
493 if (!EC_POINT_set_affine_coordinates(key->group, point, x, y, ctx))
495 if (!EC_POINT_get_affine_coordinates(key->group, point, tx, ty, ctx))
499 * Check if retrieved coordinates match originals. The range check is done
500 * inside EC_KEY_check_key().
502 if (BN_cmp(x, tx) || BN_cmp(y, ty)) {
503 ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
504 EC_R_COORDINATES_OUT_OF_RANGE);
508 if (!EC_KEY_set_public_key(key, point))
511 if (EC_KEY_check_key(key) == 0)
519 EC_POINT_free(point);
524 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
529 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
531 if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)
533 EC_GROUP_free(key->group);
534 key->group = EC_GROUP_dup(group);
535 return (key->group == NULL) ? 0 : 1;
538 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
540 return key->priv_key;
543 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
545 if (key->group == NULL || key->group->meth == NULL)
547 if (key->group->meth->set_private != NULL
548 && key->group->meth->set_private(key, priv_key) == 0)
550 if (key->meth->set_private != NULL
551 && key->meth->set_private(key, priv_key) == 0)
553 BN_clear_free(key->priv_key);
554 key->priv_key = BN_dup(priv_key);
555 return (key->priv_key == NULL) ? 0 : 1;
558 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
563 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
565 if (key->meth->set_public != NULL
566 && key->meth->set_public(key, pub_key) == 0)
568 EC_POINT_free(key->pub_key);
569 key->pub_key = EC_POINT_dup(pub_key, key->group);
570 return (key->pub_key == NULL) ? 0 : 1;
573 unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
575 return key->enc_flag;
578 void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
580 key->enc_flag = flags;
583 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
585 return key->conv_form;
588 void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
590 key->conv_form = cform;
591 if (key->group != NULL)
592 EC_GROUP_set_point_conversion_form(key->group, cform);
595 void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
597 if (key->group != NULL)
598 EC_GROUP_set_asn1_flag(key->group, flag);
601 int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
603 if (key->group == NULL)
605 return EC_GROUP_precompute_mult(key->group, ctx);
608 int EC_KEY_get_flags(const EC_KEY *key)
613 void EC_KEY_set_flags(EC_KEY *key, int flags)
618 void EC_KEY_clear_flags(EC_KEY *key, int flags)
620 key->flags &= ~flags;
623 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
624 unsigned char **pbuf, BN_CTX *ctx)
626 if (key == NULL || key->pub_key == NULL || key->group == NULL)
628 return EC_POINT_point2buf(key->group, key->pub_key, form, pbuf, ctx);
631 int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
634 if (key == NULL || key->group == NULL)
636 if (key->pub_key == NULL)
637 key->pub_key = EC_POINT_new(key->group);
638 if (key->pub_key == NULL)
640 if (EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx) == 0)
643 * Save the point conversion form.
644 * For non-custom curves the first octet of the buffer (excluding
645 * the last significant bit) contains the point conversion form.
646 * EC_POINT_oct2point() has already performed sanity checking of
647 * the buffer so we know it is valid.
649 if ((key->group->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0)
650 key->conv_form = (point_conversion_form_t)(buf[0] & ~0x01);
654 size_t EC_KEY_priv2oct(const EC_KEY *eckey,
655 unsigned char *buf, size_t len)
657 if (eckey->group == NULL || eckey->group->meth == NULL)
659 if (eckey->group->meth->priv2oct == NULL) {
660 ECerr(EC_F_EC_KEY_PRIV2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
664 return eckey->group->meth->priv2oct(eckey, buf, len);
667 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
668 unsigned char *buf, size_t len)
672 buf_len = (EC_GROUP_order_bits(eckey->group) + 7) / 8;
673 if (eckey->priv_key == NULL)
677 else if (len < buf_len)
680 /* Octetstring may need leading zeros if BN is to short */
682 if (BN_bn2binpad(eckey->priv_key, buf, buf_len) == -1) {
683 ECerr(EC_F_EC_KEY_SIMPLE_PRIV2OCT, EC_R_BUFFER_TOO_SMALL);
690 int EC_KEY_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
692 if (eckey->group == NULL || eckey->group->meth == NULL)
694 if (eckey->group->meth->oct2priv == NULL) {
695 ECerr(EC_F_EC_KEY_OCT2PRIV, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
698 return eckey->group->meth->oct2priv(eckey, buf, len);
701 int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
703 if (eckey->priv_key == NULL)
704 eckey->priv_key = BN_secure_new();
705 if (eckey->priv_key == NULL) {
706 ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_MALLOC_FAILURE);
709 eckey->priv_key = BN_bin2bn(buf, len, eckey->priv_key);
710 if (eckey->priv_key == NULL) {
711 ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_BN_LIB);
717 size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf)
722 len = EC_KEY_priv2oct(eckey, NULL, 0);
725 if ((buf = OPENSSL_malloc(len)) == NULL) {
726 ECerr(EC_F_EC_KEY_PRIV2BUF, ERR_R_MALLOC_FAILURE);
729 len = EC_KEY_priv2oct(eckey, buf, len);
738 int EC_KEY_can_sign(const EC_KEY *eckey)
740 if (eckey->group == NULL || eckey->group->meth == NULL
741 || (eckey->group->meth->flags & EC_FLAGS_NO_SIGN))