1 /* Originally written by Bodo Moeller for the OpenSSL project.
2 * ====================================================================
3 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
55 /* ====================================================================
56 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
58 * Portions of the attached software ("Contribution") are developed by
59 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
61 * The Contribution is licensed pursuant to the OpenSSL open source
62 * license provided above.
64 * The elliptic curve binary polynomial software is originally written by
65 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
68 #include <openssl/ec_key.h>
72 #include <openssl/ec.h>
73 #include <openssl/ecdsa.h>
74 #include <openssl/engine.h>
75 #include <openssl/err.h>
76 #include <openssl/ex_data.h>
77 #include <openssl/mem.h>
78 #include <openssl/thread.h>
81 #include "../delocate.h"
82 #include "../../internal.h"
85 DEFINE_STATIC_EX_DATA_CLASS(g_ec_ex_data_class);
87 static EC_WRAPPED_SCALAR *ec_wrapped_scalar_new(const EC_GROUP *group) {
88 EC_WRAPPED_SCALAR *wrapped = OPENSSL_malloc(sizeof(EC_WRAPPED_SCALAR));
89 if (wrapped == NULL) {
90 OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
94 OPENSSL_memset(wrapped, 0, sizeof(EC_WRAPPED_SCALAR));
95 wrapped->bignum.d = wrapped->scalar.words;
96 wrapped->bignum.width = group->order.width;
97 wrapped->bignum.dmax = group->order.width;
98 wrapped->bignum.flags = BN_FLG_STATIC_DATA;
102 static void ec_wrapped_scalar_free(EC_WRAPPED_SCALAR *scalar) {
103 OPENSSL_free(scalar);
106 EC_KEY *EC_KEY_new(void) { return EC_KEY_new_method(NULL); }
108 EC_KEY *EC_KEY_new_method(const ENGINE *engine) {
109 EC_KEY *ret = OPENSSL_malloc(sizeof(EC_KEY));
111 OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
115 OPENSSL_memset(ret, 0, sizeof(EC_KEY));
118 ret->ecdsa_meth = ENGINE_get_ECDSA_method(engine);
120 if (ret->ecdsa_meth) {
121 METHOD_ref(ret->ecdsa_meth);
124 ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
127 CRYPTO_new_ex_data(&ret->ex_data);
129 if (ret->ecdsa_meth && ret->ecdsa_meth->init && !ret->ecdsa_meth->init(ret)) {
130 CRYPTO_free_ex_data(g_ec_ex_data_class_bss_get(), ret, &ret->ex_data);
131 if (ret->ecdsa_meth) {
132 METHOD_unref(ret->ecdsa_meth);
141 EC_KEY *EC_KEY_new_by_curve_name(int nid) {
142 EC_KEY *ret = EC_KEY_new();
144 OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
147 ret->group = EC_GROUP_new_by_curve_name(nid);
148 if (ret->group == NULL) {
155 void EC_KEY_free(EC_KEY *r) {
160 if (!CRYPTO_refcount_dec_and_test_zero(&r->references)) {
165 if (r->ecdsa_meth->finish) {
166 r->ecdsa_meth->finish(r);
168 METHOD_unref(r->ecdsa_meth);
171 EC_GROUP_free(r->group);
172 EC_POINT_free(r->pub_key);
173 ec_wrapped_scalar_free(r->priv_key);
176 CRYPTO_free_ex_data(g_ec_ex_data_class_bss_get(), r, &r->ex_data);
181 EC_KEY *EC_KEY_dup(const EC_KEY *src) {
183 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
187 EC_KEY *ret = EC_KEY_new();
192 if ((src->group != NULL &&
193 !EC_KEY_set_group(ret, src->group)) ||
194 (src->pub_key != NULL &&
195 !EC_KEY_set_public_key(ret, src->pub_key)) ||
196 (src->priv_key != NULL &&
197 !EC_KEY_set_private_key(ret, EC_KEY_get0_private_key(src)))) {
202 ret->enc_flag = src->enc_flag;
203 ret->conv_form = src->conv_form;
207 int EC_KEY_up_ref(EC_KEY *r) {
208 CRYPTO_refcount_inc(&r->references);
212 int EC_KEY_is_opaque(const EC_KEY *key) {
213 return key->ecdsa_meth && (key->ecdsa_meth->flags & ECDSA_FLAG_OPAQUE);
216 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key) { return key->group; }
218 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group) {
219 // If |key| already has a group, it is an error to switch to another one.
220 if (key->group != NULL) {
221 if (EC_GROUP_cmp(key->group, group, NULL) != 0) {
222 OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH);
228 assert(key->priv_key == NULL);
229 assert(key->pub_key == NULL);
231 EC_GROUP_free(key->group);
232 key->group = EC_GROUP_dup(group);
233 return key->group != NULL;
236 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key) {
237 return key->priv_key != NULL ? &key->priv_key->bignum : NULL;
240 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key) {
241 if (key->group == NULL) {
242 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS);
246 EC_WRAPPED_SCALAR *scalar = ec_wrapped_scalar_new(key->group);
247 if (scalar == NULL) {
250 if (!ec_bignum_to_scalar(key->group, &scalar->scalar, priv_key)) {
251 OPENSSL_PUT_ERROR(EC, EC_R_WRONG_ORDER);
252 ec_wrapped_scalar_free(scalar);
255 ec_wrapped_scalar_free(key->priv_key);
256 key->priv_key = scalar;
260 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key) {
264 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key) {
265 if (key->group == NULL) {
266 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS);
270 if (EC_GROUP_cmp(key->group, pub_key->group, NULL) != 0) {
271 OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH);
275 EC_POINT_free(key->pub_key);
276 key->pub_key = EC_POINT_dup(pub_key, key->group);
277 return (key->pub_key == NULL) ? 0 : 1;
280 unsigned int EC_KEY_get_enc_flags(const EC_KEY *key) { return key->enc_flag; }
282 void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags) {
283 key->enc_flag = flags;
286 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key) {
287 return key->conv_form;
290 void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform) {
291 key->conv_form = cform;
294 int EC_KEY_check_key(const EC_KEY *eckey) {
297 EC_POINT *point = NULL;
299 if (!eckey || !eckey->group || !eckey->pub_key) {
300 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
304 if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
305 OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
315 // testing whether the pub_key is on the elliptic curve
316 if (!EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx)) {
317 OPENSSL_PUT_ERROR(EC, EC_R_POINT_IS_NOT_ON_CURVE);
320 // in case the priv_key is present :
321 // check if generator * priv_key == pub_key
322 if (eckey->priv_key != NULL) {
323 point = EC_POINT_new(eckey->group);
325 !ec_point_mul_scalar(eckey->group, point, &eckey->priv_key->scalar,
327 OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
330 if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
331 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_PRIVATE_KEY);
339 EC_POINT_free(point);
343 int EC_KEY_check_fips(const EC_KEY *key) {
344 if (EC_KEY_is_opaque(key)) {
345 // Opaque keys can't be checked.
346 OPENSSL_PUT_ERROR(EC, EC_R_PUBLIC_KEY_VALIDATION_FAILED);
350 if (!EC_KEY_check_key(key)) {
355 uint8_t data[16] = {0};
356 ECDSA_SIG *sig = ECDSA_do_sign(data, sizeof(data), key);
357 #if defined(BORINGSSL_FIPS_BREAK_ECDSA_PWCT)
360 int ok = sig != NULL &&
361 ECDSA_do_verify(data, sizeof(data), sig, key);
364 OPENSSL_PUT_ERROR(EC, EC_R_PUBLIC_KEY_VALIDATION_FAILED);
372 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
374 EC_POINT *point = NULL;
377 if (!key || !key->group || !x || !y) {
378 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
382 point = EC_POINT_new(key->group);
384 !EC_POINT_set_affine_coordinates_GFp(key->group, point, x, y, NULL) ||
385 !EC_KEY_set_public_key(key, point) ||
386 !EC_KEY_check_key(key)) {
393 EC_POINT_free(point);
397 int EC_KEY_generate_key(EC_KEY *key) {
398 if (key == NULL || key->group == NULL) {
399 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
403 // Check that the group order is FIPS compliant (FIPS 186-4 B.4.2).
404 if (BN_num_bits(EC_GROUP_get0_order(key->group)) < 160) {
405 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_GROUP_ORDER);
409 static const uint8_t kDefaultAdditionalData[32] = {0};
410 EC_WRAPPED_SCALAR *priv_key = ec_wrapped_scalar_new(key->group);
411 EC_POINT *pub_key = EC_POINT_new(key->group);
412 if (priv_key == NULL || pub_key == NULL ||
413 // Generate the private key by testing candidates (FIPS 186-4 B.4.2).
414 !ec_random_nonzero_scalar(key->group, &priv_key->scalar,
415 kDefaultAdditionalData) ||
416 !ec_point_mul_scalar(key->group, pub_key, &priv_key->scalar, NULL, NULL,
418 EC_POINT_free(pub_key);
419 ec_wrapped_scalar_free(priv_key);
423 ec_wrapped_scalar_free(key->priv_key);
424 key->priv_key = priv_key;
425 EC_POINT_free(key->pub_key);
426 key->pub_key = pub_key;
430 int EC_KEY_generate_key_fips(EC_KEY *eckey) {
431 return EC_KEY_generate_key(eckey) && EC_KEY_check_fips(eckey);
434 int EC_KEY_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
435 CRYPTO_EX_dup *dup_unused,
436 CRYPTO_EX_free *free_func) {
438 if (!CRYPTO_get_ex_new_index(g_ec_ex_data_class_bss_get(), &index, argl, argp,
445 int EC_KEY_set_ex_data(EC_KEY *d, int idx, void *arg) {
446 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
449 void *EC_KEY_get_ex_data(const EC_KEY *d, int idx) {
450 return CRYPTO_get_ex_data(&d->ex_data, idx);
453 void EC_KEY_set_asn1_flag(EC_KEY *key, int flag) {}