2 * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the OpenSSL license (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 <openssl/err.h>
15 #include <openssl/engine.h>
17 EC_KEY *EC_KEY_new(void)
19 return EC_KEY_new_method(NULL);
22 EC_KEY *EC_KEY_new_by_curve_name(int nid)
24 EC_KEY *ret = EC_KEY_new();
27 ret->group = EC_GROUP_new_by_curve_name(nid);
28 if (ret->group == NULL) {
32 if (ret->meth->set_group != NULL
33 && ret->meth->set_group(ret, ret->group) == 0) {
40 void EC_KEY_free(EC_KEY *r)
47 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
48 REF_PRINT_COUNT("EC_KEY", r);
51 REF_ASSERT_ISNT(i < 0);
53 if (r->meth->finish != NULL)
56 #ifndef OPENSSL_NO_ENGINE
57 ENGINE_finish(r->engine);
60 if (r->group && r->group->meth->keyfinish)
61 r->group->meth->keyfinish(r);
63 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
64 CRYPTO_THREAD_lock_free(r->lock);
65 EC_GROUP_free(r->group);
66 EC_POINT_free(r->pub_key);
67 BN_clear_free(r->priv_key);
69 OPENSSL_clear_free((void *)r, sizeof(EC_KEY));
72 EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
74 if (dest == NULL || src == NULL) {
75 ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
78 if (src->meth != dest->meth) {
79 if (dest->meth->finish != NULL)
80 dest->meth->finish(dest);
81 if (dest->group && dest->group->meth->keyfinish)
82 dest->group->meth->keyfinish(dest);
83 #ifndef OPENSSL_NO_ENGINE
84 if (ENGINE_finish(dest->engine) == 0)
89 /* copy the parameters */
90 if (src->group != NULL) {
91 const EC_METHOD *meth = EC_GROUP_method_of(src->group);
92 /* clear the old group */
93 EC_GROUP_free(dest->group);
94 dest->group = EC_GROUP_new(meth);
95 if (dest->group == NULL)
97 if (!EC_GROUP_copy(dest->group, src->group))
100 /* copy the public key */
101 if (src->pub_key != NULL) {
102 EC_POINT_free(dest->pub_key);
103 dest->pub_key = EC_POINT_new(src->group);
104 if (dest->pub_key == NULL)
106 if (!EC_POINT_copy(dest->pub_key, src->pub_key))
109 /* copy the private key */
110 if (src->priv_key != NULL) {
111 if (dest->priv_key == NULL) {
112 dest->priv_key = BN_new();
113 if (dest->priv_key == NULL)
116 if (!BN_copy(dest->priv_key, src->priv_key))
118 if (src->group->meth->keycopy
119 && src->group->meth->keycopy(dest, src) == 0)
126 dest->enc_flag = src->enc_flag;
127 dest->conv_form = src->conv_form;
128 dest->version = src->version;
129 dest->flags = src->flags;
130 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
131 &dest->ex_data, &src->ex_data))
134 if (src->meth != dest->meth) {
135 #ifndef OPENSSL_NO_ENGINE
136 if (src->engine != NULL && ENGINE_init(src->engine) == 0)
138 dest->engine = src->engine;
140 dest->meth = src->meth;
143 if (src->meth->copy != NULL && src->meth->copy(dest, src) == 0)
149 EC_KEY *EC_KEY_dup(const EC_KEY *ec_key)
151 EC_KEY *ret = EC_KEY_new_method(ec_key->engine);
156 if (EC_KEY_copy(ret, ec_key) == NULL) {
163 int EC_KEY_up_ref(EC_KEY *r)
167 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
170 REF_PRINT_COUNT("EC_KEY", r);
171 REF_ASSERT_ISNT(i < 2);
172 return ((i > 1) ? 1 : 0);
175 ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey)
177 return eckey->engine;
180 int EC_KEY_generate_key(EC_KEY *eckey)
182 if (eckey == NULL || eckey->group == NULL) {
183 ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
186 if (eckey->meth->keygen != NULL)
187 return eckey->meth->keygen(eckey);
188 ECerr(EC_F_EC_KEY_GENERATE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
192 int ossl_ec_key_gen(EC_KEY *eckey)
194 if (!ossl_assert(eckey->group->meth->keygen != NULL))
196 return eckey->group->meth->keygen(eckey);
199 int ec_key_simple_generate_key(EC_KEY *eckey)
203 BIGNUM *priv_key = NULL;
204 const BIGNUM *order = NULL;
205 EC_POINT *pub_key = NULL;
207 if ((ctx = BN_CTX_new()) == NULL)
210 if (eckey->priv_key == NULL) {
212 if (priv_key == NULL)
215 priv_key = eckey->priv_key;
217 order = EC_GROUP_get0_order(eckey->group);
222 if (!BN_priv_rand_range(priv_key, order))
224 while (BN_is_zero(priv_key)) ;
226 if (eckey->pub_key == NULL) {
227 pub_key = EC_POINT_new(eckey->group);
231 pub_key = eckey->pub_key;
233 if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx))
236 eckey->priv_key = priv_key;
237 eckey->pub_key = pub_key;
242 if (eckey->pub_key == NULL)
243 EC_POINT_free(pub_key);
244 if (eckey->priv_key != priv_key)
250 int ec_key_simple_generate_public_key(EC_KEY *eckey)
252 return EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
256 int EC_KEY_check_key(const EC_KEY *eckey)
258 if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
259 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
263 if (eckey->group->meth->keycheck == NULL) {
264 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
268 return eckey->group->meth->keycheck(eckey);
271 int ec_key_simple_check_key(const EC_KEY *eckey)
275 const BIGNUM *order = NULL;
276 EC_POINT *point = NULL;
278 if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
279 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
283 if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
284 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_AT_INFINITY);
288 if ((ctx = BN_CTX_new()) == NULL)
290 if ((point = EC_POINT_new(eckey->group)) == NULL)
293 /* testing whether the pub_key is on the elliptic curve */
294 if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
295 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
298 /* testing whether pub_key * order is the point at infinity */
299 order = eckey->group->order;
300 if (BN_is_zero(order)) {
301 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
304 if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
305 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
308 if (!EC_POINT_is_at_infinity(eckey->group, point)) {
309 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
313 * in case the priv_key is present : check if generator * priv_key ==
316 if (eckey->priv_key != NULL) {
317 if (BN_cmp(eckey->priv_key, order) >= 0) {
318 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
321 if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
323 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
326 if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
327 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
334 EC_POINT_free(point);
338 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
343 EC_POINT *point = NULL;
345 #ifndef OPENSSL_NO_EC2M
346 int tmp_nid, is_char_two = 0;
349 if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
350 ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
351 ERR_R_PASSED_NULL_PARAMETER);
359 point = EC_POINT_new(key->group);
364 tx = BN_CTX_get(ctx);
365 ty = BN_CTX_get(ctx);
369 #ifndef OPENSSL_NO_EC2M
370 tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(key->group));
372 if (tmp_nid == NID_X9_62_characteristic_two_field)
376 if (!EC_POINT_set_affine_coordinates_GF2m(key->group, point,
379 if (!EC_POINT_get_affine_coordinates_GF2m(key->group, point,
385 if (!EC_POINT_set_affine_coordinates_GFp(key->group, point,
388 if (!EC_POINT_get_affine_coordinates_GFp(key->group, point,
393 * Check if retrieved coordinates match originals and are less than field
394 * order: if not values are out of range.
396 if (BN_cmp(x, tx) || BN_cmp(y, ty)
397 || (BN_cmp(x, key->group->field) >= 0)
398 || (BN_cmp(y, key->group->field) >= 0)) {
399 ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
400 EC_R_COORDINATES_OUT_OF_RANGE);
404 if (!EC_KEY_set_public_key(key, point))
407 if (EC_KEY_check_key(key) == 0)
415 EC_POINT_free(point);
420 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
425 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
427 if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)
429 EC_GROUP_free(key->group);
430 key->group = EC_GROUP_dup(group);
431 return (key->group == NULL) ? 0 : 1;
434 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
436 return key->priv_key;
439 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
441 if (key->group == NULL || key->group->meth == NULL)
443 if (key->group->meth->set_private != NULL
444 && key->group->meth->set_private(key, priv_key) == 0)
446 if (key->meth->set_private != NULL
447 && key->meth->set_private(key, priv_key) == 0)
449 BN_clear_free(key->priv_key);
450 key->priv_key = BN_dup(priv_key);
451 return (key->priv_key == NULL) ? 0 : 1;
454 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
459 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
461 if (key->meth->set_public != NULL
462 && key->meth->set_public(key, pub_key) == 0)
464 EC_POINT_free(key->pub_key);
465 key->pub_key = EC_POINT_dup(pub_key, key->group);
466 return (key->pub_key == NULL) ? 0 : 1;
469 unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
471 return key->enc_flag;
474 void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
476 key->enc_flag = flags;
479 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
481 return key->conv_form;
484 void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
486 key->conv_form = cform;
487 if (key->group != NULL)
488 EC_GROUP_set_point_conversion_form(key->group, cform);
491 void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
493 if (key->group != NULL)
494 EC_GROUP_set_asn1_flag(key->group, flag);
497 int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
499 if (key->group == NULL)
501 return EC_GROUP_precompute_mult(key->group, ctx);
504 int EC_KEY_get_flags(const EC_KEY *key)
509 void EC_KEY_set_flags(EC_KEY *key, int flags)
514 void EC_KEY_clear_flags(EC_KEY *key, int flags)
516 key->flags &= ~flags;
519 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
520 unsigned char **pbuf, BN_CTX *ctx)
522 if (key == NULL || key->pub_key == NULL || key->group == NULL)
524 return EC_POINT_point2buf(key->group, key->pub_key, form, pbuf, ctx);
527 int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
530 if (key == NULL || key->group == NULL)
532 if (key->pub_key == NULL)
533 key->pub_key = EC_POINT_new(key->group);
534 if (key->pub_key == NULL)
536 if (EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx) == 0)
539 * Save the point conversion form.
540 * For non-custom curves the first octet of the buffer (excluding
541 * the last significant bit) contains the point conversion form.
542 * EC_POINT_oct2point() has already performed sanity checking of
543 * the buffer so we know it is valid.
545 if ((key->group->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0)
546 key->conv_form = (point_conversion_form_t)(buf[0] & ~0x01);
550 size_t EC_KEY_priv2oct(const EC_KEY *eckey,
551 unsigned char *buf, size_t len)
553 if (eckey->group == NULL || eckey->group->meth == NULL)
555 if (eckey->group->meth->priv2oct == NULL) {
556 ECerr(EC_F_EC_KEY_PRIV2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
560 return eckey->group->meth->priv2oct(eckey, buf, len);
563 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
564 unsigned char *buf, size_t len)
568 buf_len = (EC_GROUP_order_bits(eckey->group) + 7) / 8;
569 if (eckey->priv_key == NULL)
573 else if (len < buf_len)
576 /* Octetstring may need leading zeros if BN is to short */
578 if (BN_bn2binpad(eckey->priv_key, buf, buf_len) == -1) {
579 ECerr(EC_F_EC_KEY_SIMPLE_PRIV2OCT, EC_R_BUFFER_TOO_SMALL);
586 int EC_KEY_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
588 if (eckey->group == NULL || eckey->group->meth == NULL)
590 if (eckey->group->meth->oct2priv == NULL) {
591 ECerr(EC_F_EC_KEY_OCT2PRIV, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
594 return eckey->group->meth->oct2priv(eckey, buf, len);
597 int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
599 if (eckey->priv_key == NULL)
600 eckey->priv_key = BN_secure_new();
601 if (eckey->priv_key == NULL) {
602 ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_MALLOC_FAILURE);
605 eckey->priv_key = BN_bin2bn(buf, len, eckey->priv_key);
606 if (eckey->priv_key == NULL) {
607 ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_BN_LIB);
613 size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf)
617 len = EC_KEY_priv2oct(eckey, NULL, 0);
620 buf = OPENSSL_malloc(len);
623 len = EC_KEY_priv2oct(eckey, buf, len);
632 int EC_KEY_can_sign(const EC_KEY *eckey)
634 if (eckey->group == NULL || eckey->group->meth == NULL
635 || (eckey->group->meth->flags & EC_FLAGS_NO_SIGN))