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
11 #include "internal/cryptlib.h"
14 #include "internal/refcount.h"
15 #include <openssl/err.h>
16 #include <openssl/engine.h>
18 EC_KEY *EC_KEY_new(void)
20 return EC_KEY_new_method(NULL);
23 EC_KEY *EC_KEY_new_by_curve_name(int nid)
25 EC_KEY *ret = EC_KEY_new();
28 ret->group = EC_GROUP_new_by_curve_name(nid);
29 if (ret->group == NULL) {
33 if (ret->meth->set_group != NULL
34 && ret->meth->set_group(ret, ret->group) == 0) {
41 void EC_KEY_free(EC_KEY *r)
48 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
49 REF_PRINT_COUNT("EC_KEY", r);
52 REF_ASSERT_ISNT(i < 0);
54 if (r->meth != NULL && r->meth->finish != NULL)
57 #ifndef OPENSSL_NO_ENGINE
58 ENGINE_finish(r->engine);
61 if (r->group && r->group->meth->keyfinish)
62 r->group->meth->keyfinish(r);
64 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
65 CRYPTO_THREAD_lock_free(r->lock);
66 EC_GROUP_free(r->group);
67 EC_POINT_free(r->pub_key);
68 BN_clear_free(r->priv_key);
70 OPENSSL_clear_free((void *)r, sizeof(EC_KEY));
73 EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
75 if (dest == NULL || src == NULL) {
76 ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
79 if (src->meth != dest->meth) {
80 if (dest->meth->finish != NULL)
81 dest->meth->finish(dest);
82 if (dest->group && dest->group->meth->keyfinish)
83 dest->group->meth->keyfinish(dest);
84 #ifndef OPENSSL_NO_ENGINE
85 if (ENGINE_finish(dest->engine) == 0)
90 /* copy the parameters */
91 if (src->group != NULL) {
92 const EC_METHOD *meth = EC_GROUP_method_of(src->group);
93 /* clear the old group */
94 EC_GROUP_free(dest->group);
95 dest->group = EC_GROUP_new(meth);
96 if (dest->group == NULL)
98 if (!EC_GROUP_copy(dest->group, src->group))
101 /* copy the public key */
102 if (src->pub_key != NULL) {
103 EC_POINT_free(dest->pub_key);
104 dest->pub_key = EC_POINT_new(src->group);
105 if (dest->pub_key == NULL)
107 if (!EC_POINT_copy(dest->pub_key, src->pub_key))
110 /* copy the private key */
111 if (src->priv_key != NULL) {
112 if (dest->priv_key == NULL) {
113 dest->priv_key = BN_new();
114 if (dest->priv_key == NULL)
117 if (!BN_copy(dest->priv_key, src->priv_key))
119 if (src->group->meth->keycopy
120 && src->group->meth->keycopy(dest, src) == 0)
127 dest->enc_flag = src->enc_flag;
128 dest->conv_form = src->conv_form;
129 dest->version = src->version;
130 dest->flags = src->flags;
131 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
132 &dest->ex_data, &src->ex_data))
135 if (src->meth != dest->meth) {
136 #ifndef OPENSSL_NO_ENGINE
137 if (src->engine != NULL && ENGINE_init(src->engine) == 0)
139 dest->engine = src->engine;
141 dest->meth = src->meth;
144 if (src->meth->copy != NULL && src->meth->copy(dest, src) == 0)
150 EC_KEY *EC_KEY_dup(const EC_KEY *ec_key)
152 EC_KEY *ret = EC_KEY_new_method(ec_key->engine);
157 if (EC_KEY_copy(ret, ec_key) == NULL) {
164 int EC_KEY_up_ref(EC_KEY *r)
168 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
171 REF_PRINT_COUNT("EC_KEY", r);
172 REF_ASSERT_ISNT(i < 2);
173 return ((i > 1) ? 1 : 0);
176 ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey)
178 return eckey->engine;
181 int EC_KEY_generate_key(EC_KEY *eckey)
183 if (eckey == NULL || eckey->group == NULL) {
184 ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
187 if (eckey->meth->keygen != NULL)
188 return eckey->meth->keygen(eckey);
189 ECerr(EC_F_EC_KEY_GENERATE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
193 int ossl_ec_key_gen(EC_KEY *eckey)
195 return eckey->group->meth->keygen(eckey);
199 * ECC Key generation.
200 * See SP800-56AR3 5.6.1.2.2 "Key Pair Generation by Testing Candidates"
203 * eckey An EC key object that contains domain params. The generated keypair
204 * is stored in this object.
205 * Returns 1 if the keypair was generated or 0 otherwise.
207 int ec_key_simple_generate_key(EC_KEY *eckey)
210 BIGNUM *priv_key = NULL;
211 const BIGNUM *order = NULL;
212 EC_POINT *pub_key = NULL;
213 const EC_GROUP *group = eckey->group;
215 if (eckey->priv_key == NULL) {
216 priv_key = BN_secure_new();
217 if (priv_key == NULL)
220 priv_key = eckey->priv_key;
223 * Steps (1-2): Check domain parameters and security strength.
224 * These steps must be done by the user. This would need to be
225 * stated in the security policy.
228 order = EC_GROUP_get0_order(group);
233 * Steps (3-7): priv_key = DRBG_RAND(order_n_bits) (range [1, n-1]).
234 * Although this is slightly different from the standard, it is effectively
235 * equivalent as it gives an unbiased result ranging from 1..n-1. It is also
236 * faster as the standard needs to retry more often. Also doing
237 * 1 + rand[0..n-2] would effect the way that tests feed dummy entropy into
238 * rand so the simpler backward compatible method has been used here.
241 if (!BN_priv_rand_range(priv_key, order))
243 while (BN_is_zero(priv_key)) ;
245 if (eckey->pub_key == NULL) {
246 pub_key = EC_POINT_new(group);
250 pub_key = eckey->pub_key;
252 /* Step (8) : pub_key = priv_key * G (where G is a point on the curve) */
253 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, NULL))
256 eckey->priv_key = priv_key;
257 eckey->pub_key = pub_key;
264 /* Step (9): If there is an error return an invalid keypair. */
266 BN_clear(eckey->priv_key);
267 if (eckey->pub_key != NULL)
268 EC_POINT_set_to_infinity(group, eckey->pub_key);
271 EC_POINT_free(pub_key);
272 BN_clear_free(priv_key);
276 int ec_key_simple_generate_public_key(EC_KEY *eckey)
279 * See SP800-56AR3 5.6.1.2.2: Step (8)
280 * pub_key = priv_key * G (where G is a point on the curve)
282 return EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
286 int EC_KEY_check_key(const EC_KEY *eckey)
288 if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
289 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
293 if (eckey->group->meth->keycheck == NULL) {
294 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
298 return eckey->group->meth->keycheck(eckey);
302 * Check the range of the EC public key.
303 * See SP800-56A R3 Section 5.6.2.3.3 (Part 2)
305 * - If q = odd prime p: Verify that xQ and yQ are integers in the
306 * interval[0, p - 1], OR
307 * - If q = 2m: Verify that xQ and yQ are bit strings of length m bits.
308 * Returns 1 if the public key has a valid range, otherwise it returns 0.
310 static int ec_key_public_range_check(BN_CTX *ctx, const EC_KEY *key)
321 if (!EC_POINT_get_affine_coordinates(key->group, key->pub_key, x, y, ctx))
324 if (EC_METHOD_get_field_type(key->group->meth) == NID_X9_62_prime_field) {
325 if (BN_is_negative(x)
326 || BN_cmp(x, key->group->field) >= 0
328 || BN_cmp(y, key->group->field) >= 0) {
332 int m = EC_GROUP_get_degree(key->group);
333 if (BN_num_bits(x) > m || BN_num_bits(y) > m) {
344 * ECC Key validation as specified in SP800-56A R3.
345 * Section 5.6.2.3.3 ECC Full Public-Key Validation
346 * Section 5.6.2.1.2 Owner Assurance of Private-Key Validity
347 * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
349 * Before calling this method in fips mode, there should be an assurance that
350 * an approved elliptic-curve group is used.
351 * Returns 1 if the key is valid, otherwise it returns 0.
353 int ec_key_simple_check_key(const EC_KEY *eckey)
357 const BIGNUM *order = NULL;
358 EC_POINT *point = NULL;
360 if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
361 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
365 /* 5.6.2.3.3 (Step 1): Q != infinity */
366 if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
367 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_AT_INFINITY);
371 if ((ctx = BN_CTX_new()) == NULL)
374 if ((point = EC_POINT_new(eckey->group)) == NULL)
377 /* 5.6.2.3.3 (Step 2) Test if the public key is in range */
378 if (!ec_key_public_range_check(ctx, eckey)) {
379 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_COORDINATES_OUT_OF_RANGE);
383 /* 5.6.2.3.3 (Step 3) is the pub_key on the elliptic curve */
384 if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
385 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
389 order = eckey->group->order;
390 if (BN_is_zero(order)) {
391 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
394 /* 5.6.2.3.3 (Step 4) : pub_key * order is the point at infinity. */
395 if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
396 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
399 if (!EC_POINT_is_at_infinity(eckey->group, point)) {
400 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
404 if (eckey->priv_key != NULL) {
406 * 5.6.2.1.2 Owner Assurance of Private-Key Validity
407 * The private key is in the range [1, order-1]
409 if (BN_cmp(eckey->priv_key, BN_value_one()) < 0
410 || BN_cmp(eckey->priv_key, order) >= 0) {
411 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
415 * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency (b)
416 * Check if generator * priv_key = pub_key
418 if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
420 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
423 if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
424 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
431 EC_POINT_free(point);
435 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
440 EC_POINT *point = NULL;
443 if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
444 ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
445 ERR_R_PASSED_NULL_PARAMETER);
453 point = EC_POINT_new(key->group);
458 tx = BN_CTX_get(ctx);
459 ty = BN_CTX_get(ctx);
463 if (!EC_POINT_set_affine_coordinates(key->group, point, x, y, ctx))
465 if (!EC_POINT_get_affine_coordinates(key->group, point, tx, ty, ctx))
469 * Check if retrieved coordinates match originals. The range check is done
470 * inside EC_KEY_check_key().
472 if (BN_cmp(x, tx) || BN_cmp(y, ty)) {
473 ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
474 EC_R_COORDINATES_OUT_OF_RANGE);
478 if (!EC_KEY_set_public_key(key, point))
481 if (EC_KEY_check_key(key) == 0)
489 EC_POINT_free(point);
494 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
499 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
501 if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)
503 EC_GROUP_free(key->group);
504 key->group = EC_GROUP_dup(group);
505 return (key->group == NULL) ? 0 : 1;
508 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
510 return key->priv_key;
513 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
515 if (key->group == NULL || key->group->meth == NULL)
517 if (key->group->meth->set_private != NULL
518 && key->group->meth->set_private(key, priv_key) == 0)
520 if (key->meth->set_private != NULL
521 && key->meth->set_private(key, priv_key) == 0)
523 BN_clear_free(key->priv_key);
524 key->priv_key = BN_dup(priv_key);
525 return (key->priv_key == NULL) ? 0 : 1;
528 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
533 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
535 if (key->meth->set_public != NULL
536 && key->meth->set_public(key, pub_key) == 0)
538 EC_POINT_free(key->pub_key);
539 key->pub_key = EC_POINT_dup(pub_key, key->group);
540 return (key->pub_key == NULL) ? 0 : 1;
543 unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
545 return key->enc_flag;
548 void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
550 key->enc_flag = flags;
553 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
555 return key->conv_form;
558 void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
560 key->conv_form = cform;
561 if (key->group != NULL)
562 EC_GROUP_set_point_conversion_form(key->group, cform);
565 void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
567 if (key->group != NULL)
568 EC_GROUP_set_asn1_flag(key->group, flag);
571 int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
573 if (key->group == NULL)
575 return EC_GROUP_precompute_mult(key->group, ctx);
578 int EC_KEY_get_flags(const EC_KEY *key)
583 void EC_KEY_set_flags(EC_KEY *key, int flags)
588 void EC_KEY_clear_flags(EC_KEY *key, int flags)
590 key->flags &= ~flags;
593 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
594 unsigned char **pbuf, BN_CTX *ctx)
596 if (key == NULL || key->pub_key == NULL || key->group == NULL)
598 return EC_POINT_point2buf(key->group, key->pub_key, form, pbuf, ctx);
601 int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
604 if (key == NULL || key->group == NULL)
606 if (key->pub_key == NULL)
607 key->pub_key = EC_POINT_new(key->group);
608 if (key->pub_key == NULL)
610 if (EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx) == 0)
613 * Save the point conversion form.
614 * For non-custom curves the first octet of the buffer (excluding
615 * the last significant bit) contains the point conversion form.
616 * EC_POINT_oct2point() has already performed sanity checking of
617 * the buffer so we know it is valid.
619 if ((key->group->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0)
620 key->conv_form = (point_conversion_form_t)(buf[0] & ~0x01);
624 size_t EC_KEY_priv2oct(const EC_KEY *eckey,
625 unsigned char *buf, size_t len)
627 if (eckey->group == NULL || eckey->group->meth == NULL)
629 if (eckey->group->meth->priv2oct == NULL) {
630 ECerr(EC_F_EC_KEY_PRIV2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
634 return eckey->group->meth->priv2oct(eckey, buf, len);
637 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
638 unsigned char *buf, size_t len)
642 buf_len = (EC_GROUP_order_bits(eckey->group) + 7) / 8;
643 if (eckey->priv_key == NULL)
647 else if (len < buf_len)
650 /* Octetstring may need leading zeros if BN is to short */
652 if (BN_bn2binpad(eckey->priv_key, buf, buf_len) == -1) {
653 ECerr(EC_F_EC_KEY_SIMPLE_PRIV2OCT, EC_R_BUFFER_TOO_SMALL);
660 int EC_KEY_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
662 if (eckey->group == NULL || eckey->group->meth == NULL)
664 if (eckey->group->meth->oct2priv == NULL) {
665 ECerr(EC_F_EC_KEY_OCT2PRIV, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
668 return eckey->group->meth->oct2priv(eckey, buf, len);
671 int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
673 if (eckey->priv_key == NULL)
674 eckey->priv_key = BN_secure_new();
675 if (eckey->priv_key == NULL) {
676 ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_MALLOC_FAILURE);
679 eckey->priv_key = BN_bin2bn(buf, len, eckey->priv_key);
680 if (eckey->priv_key == NULL) {
681 ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_BN_LIB);
687 size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf)
692 len = EC_KEY_priv2oct(eckey, NULL, 0);
695 if ((buf = OPENSSL_malloc(len)) == NULL) {
696 ECerr(EC_F_EC_KEY_PRIV2BUF, ERR_R_MALLOC_FAILURE);
699 len = EC_KEY_priv2oct(eckey, buf, len);
708 int EC_KEY_can_sign(const EC_KEY *eckey)
710 if (eckey->group == NULL || eckey->group->meth == NULL
711 || (eckey->group->meth->flags & EC_FLAGS_NO_SIGN))