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 return eckey->group->meth->keygen(eckey);
197 int ec_key_simple_generate_key(EC_KEY *eckey)
201 BIGNUM *priv_key = NULL;
202 const BIGNUM *order = NULL;
203 EC_POINT *pub_key = NULL;
205 if ((ctx = BN_CTX_new()) == NULL)
208 if (eckey->priv_key == NULL) {
210 if (priv_key == NULL)
213 priv_key = eckey->priv_key;
215 order = EC_GROUP_get0_order(eckey->group);
220 if (!BN_priv_rand_range(priv_key, order))
222 while (BN_is_zero(priv_key)) ;
224 if (eckey->pub_key == NULL) {
225 pub_key = EC_POINT_new(eckey->group);
229 pub_key = eckey->pub_key;
231 if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx))
234 eckey->priv_key = priv_key;
235 eckey->pub_key = pub_key;
240 if (eckey->pub_key == NULL)
241 EC_POINT_free(pub_key);
242 if (eckey->priv_key != priv_key)
248 int ec_key_simple_generate_public_key(EC_KEY *eckey)
250 return EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
254 int EC_KEY_check_key(const EC_KEY *eckey)
256 if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
257 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
261 if (eckey->group->meth->keycheck == NULL) {
262 ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
266 return eckey->group->meth->keycheck(eckey);
269 int ec_key_simple_check_key(const EC_KEY *eckey)
273 const BIGNUM *order = NULL;
274 EC_POINT *point = NULL;
276 if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
277 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
281 if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
282 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_AT_INFINITY);
286 if ((ctx = BN_CTX_new()) == NULL)
288 if ((point = EC_POINT_new(eckey->group)) == NULL)
291 /* testing whether the pub_key is on the elliptic curve */
292 if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
293 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
296 /* testing whether pub_key * order is the point at infinity */
297 order = eckey->group->order;
298 if (BN_is_zero(order)) {
299 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
302 if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
303 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
306 if (!EC_POINT_is_at_infinity(eckey->group, point)) {
307 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
311 * in case the priv_key is present : check if generator * priv_key ==
314 if (eckey->priv_key != NULL) {
315 if (BN_cmp(eckey->priv_key, order) >= 0) {
316 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
319 if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
321 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
324 if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
325 ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
332 EC_POINT_free(point);
336 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
341 EC_POINT *point = NULL;
343 #ifndef OPENSSL_NO_EC2M
344 int tmp_nid, is_char_two = 0;
347 if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
348 ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
349 ERR_R_PASSED_NULL_PARAMETER);
357 point = EC_POINT_new(key->group);
362 tx = BN_CTX_get(ctx);
363 ty = BN_CTX_get(ctx);
367 #ifndef OPENSSL_NO_EC2M
368 tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(key->group));
370 if (tmp_nid == NID_X9_62_characteristic_two_field)
374 if (!EC_POINT_set_affine_coordinates_GF2m(key->group, point,
377 if (!EC_POINT_get_affine_coordinates_GF2m(key->group, point,
383 if (!EC_POINT_set_affine_coordinates_GFp(key->group, point,
386 if (!EC_POINT_get_affine_coordinates_GFp(key->group, point,
391 * Check if retrieved coordinates match originals and are less than field
392 * order: if not values are out of range.
394 if (BN_cmp(x, tx) || BN_cmp(y, ty)
395 || (BN_cmp(x, key->group->field) >= 0)
396 || (BN_cmp(y, key->group->field) >= 0)) {
397 ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
398 EC_R_COORDINATES_OUT_OF_RANGE);
402 if (!EC_KEY_set_public_key(key, point))
405 if (EC_KEY_check_key(key) == 0)
413 EC_POINT_free(point);
418 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
423 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
425 if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)
427 EC_GROUP_free(key->group);
428 key->group = EC_GROUP_dup(group);
429 return (key->group == NULL) ? 0 : 1;
432 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
434 return key->priv_key;
437 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
439 if (key->group == NULL || key->group->meth == NULL)
441 if (key->group->meth->set_private != NULL
442 && key->group->meth->set_private(key, priv_key) == 0)
444 if (key->meth->set_private != NULL
445 && key->meth->set_private(key, priv_key) == 0)
447 BN_clear_free(key->priv_key);
448 key->priv_key = BN_dup(priv_key);
449 return (key->priv_key == NULL) ? 0 : 1;
452 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
457 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
459 if (key->meth->set_public != NULL
460 && key->meth->set_public(key, pub_key) == 0)
462 EC_POINT_free(key->pub_key);
463 key->pub_key = EC_POINT_dup(pub_key, key->group);
464 return (key->pub_key == NULL) ? 0 : 1;
467 unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
469 return key->enc_flag;
472 void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
474 key->enc_flag = flags;
477 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
479 return key->conv_form;
482 void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
484 key->conv_form = cform;
485 if (key->group != NULL)
486 EC_GROUP_set_point_conversion_form(key->group, cform);
489 void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
491 if (key->group != NULL)
492 EC_GROUP_set_asn1_flag(key->group, flag);
495 int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
497 if (key->group == NULL)
499 return EC_GROUP_precompute_mult(key->group, ctx);
502 int EC_KEY_get_flags(const EC_KEY *key)
507 void EC_KEY_set_flags(EC_KEY *key, int flags)
512 void EC_KEY_clear_flags(EC_KEY *key, int flags)
514 key->flags &= ~flags;
517 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
518 unsigned char **pbuf, BN_CTX *ctx)
520 if (key == NULL || key->pub_key == NULL || key->group == NULL)
522 return EC_POINT_point2buf(key->group, key->pub_key, form, pbuf, ctx);
525 int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
528 if (key == NULL || key->group == NULL)
530 if (key->pub_key == NULL)
531 key->pub_key = EC_POINT_new(key->group);
532 if (key->pub_key == NULL)
534 if (EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx) == 0)
537 * Save the point conversion form.
538 * For non-custom curves the first octet of the buffer (excluding
539 * the last significant bit) contains the point conversion form.
540 * EC_POINT_oct2point() has already performed sanity checking of
541 * the buffer so we know it is valid.
543 if ((key->group->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0)
544 key->conv_form = (point_conversion_form_t)(buf[0] & ~0x01);
548 size_t EC_KEY_priv2oct(const EC_KEY *eckey,
549 unsigned char *buf, size_t len)
551 if (eckey->group == NULL || eckey->group->meth == NULL)
553 if (eckey->group->meth->priv2oct == NULL) {
554 ECerr(EC_F_EC_KEY_PRIV2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
558 return eckey->group->meth->priv2oct(eckey, buf, len);
561 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
562 unsigned char *buf, size_t len)
566 buf_len = (EC_GROUP_order_bits(eckey->group) + 7) / 8;
567 if (eckey->priv_key == NULL)
571 else if (len < buf_len)
574 /* Octetstring may need leading zeros if BN is to short */
576 if (BN_bn2binpad(eckey->priv_key, buf, buf_len) == -1) {
577 ECerr(EC_F_EC_KEY_SIMPLE_PRIV2OCT, EC_R_BUFFER_TOO_SMALL);
584 int EC_KEY_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
586 if (eckey->group == NULL || eckey->group->meth == NULL)
588 if (eckey->group->meth->oct2priv == NULL) {
589 ECerr(EC_F_EC_KEY_OCT2PRIV, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
592 return eckey->group->meth->oct2priv(eckey, buf, len);
595 int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
597 if (eckey->priv_key == NULL)
598 eckey->priv_key = BN_secure_new();
599 if (eckey->priv_key == NULL) {
600 ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_MALLOC_FAILURE);
603 eckey->priv_key = BN_bin2bn(buf, len, eckey->priv_key);
604 if (eckey->priv_key == NULL) {
605 ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_BN_LIB);
611 size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf)
615 len = EC_KEY_priv2oct(eckey, NULL, 0);
618 buf = OPENSSL_malloc(len);
621 len = EC_KEY_priv2oct(eckey, buf, len);
630 int EC_KEY_can_sign(const EC_KEY *eckey)
632 if (eckey->group == NULL || eckey->group->meth == NULL
633 || (eckey->group->meth->flags & EC_FLAGS_NO_SIGN))