1 /* Originally written by Bodo Moeller for the OpenSSL project.
2 * ====================================================================
3 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
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
15 * distribution.
16 *
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/)"
21 *
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.
26 *
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.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 *
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 * ====================================================================
49 *
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).
53 *
54 */
55 /* ====================================================================
56 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
57 *
58 * Portions of the attached software ("Contribution") are developed by
59 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
60 *
61 * The Contribution is licensed pursuant to the OpenSSL open source
62 * license provided above.
63 *
64 * The elliptic curve binary polynomial software is originally written by
65 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
66 * Laboratories. */
67
68 #include <openssl/ec_key.h>
69
70 #include <string.h>
71
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>
79
80 #include "internal.h"
81 #include "../delocate.h"
82 #include "../service_indicator/internal.h"
83 #include "../../internal.h"
84
85
DEFINE_STATIC_EX_DATA_CLASS(g_ec_ex_data_class)86 DEFINE_STATIC_EX_DATA_CLASS(g_ec_ex_data_class)
87
88 static EC_WRAPPED_SCALAR *ec_wrapped_scalar_new(const EC_GROUP *group) {
89 EC_WRAPPED_SCALAR *wrapped = OPENSSL_malloc(sizeof(EC_WRAPPED_SCALAR));
90 if (wrapped == NULL) {
91 return NULL;
92 }
93
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;
99 return wrapped;
100 }
101
ec_wrapped_scalar_free(EC_WRAPPED_SCALAR * scalar)102 static void ec_wrapped_scalar_free(EC_WRAPPED_SCALAR *scalar) {
103 OPENSSL_free(scalar);
104 }
105
EC_KEY_new(void)106 EC_KEY *EC_KEY_new(void) { return EC_KEY_new_method(NULL); }
107
EC_KEY_new_method(const ENGINE * engine)108 EC_KEY *EC_KEY_new_method(const ENGINE *engine) {
109 EC_KEY *ret = OPENSSL_malloc(sizeof(EC_KEY));
110 if (ret == NULL) {
111 return NULL;
112 }
113
114 OPENSSL_memset(ret, 0, sizeof(EC_KEY));
115
116 if (engine) {
117 ret->ecdsa_meth = ENGINE_get_ECDSA_method(engine);
118 }
119 if (ret->ecdsa_meth) {
120 METHOD_ref(ret->ecdsa_meth);
121 }
122
123 ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
124 ret->references = 1;
125
126 CRYPTO_new_ex_data(&ret->ex_data);
127
128 if (ret->ecdsa_meth && ret->ecdsa_meth->init && !ret->ecdsa_meth->init(ret)) {
129 CRYPTO_free_ex_data(g_ec_ex_data_class_bss_get(), ret, &ret->ex_data);
130 if (ret->ecdsa_meth) {
131 METHOD_unref(ret->ecdsa_meth);
132 }
133 OPENSSL_free(ret);
134 return NULL;
135 }
136
137 return ret;
138 }
139
EC_KEY_new_by_curve_name(int nid)140 EC_KEY *EC_KEY_new_by_curve_name(int nid) {
141 EC_KEY *ret = EC_KEY_new();
142 if (ret == NULL) {
143 return NULL;
144 }
145 ret->group = EC_GROUP_new_by_curve_name(nid);
146 if (ret->group == NULL) {
147 EC_KEY_free(ret);
148 return NULL;
149 }
150 return ret;
151 }
152
EC_KEY_free(EC_KEY * r)153 void EC_KEY_free(EC_KEY *r) {
154 if (r == NULL) {
155 return;
156 }
157
158 if (!CRYPTO_refcount_dec_and_test_zero(&r->references)) {
159 return;
160 }
161
162 if (r->ecdsa_meth) {
163 if (r->ecdsa_meth->finish) {
164 r->ecdsa_meth->finish(r);
165 }
166 METHOD_unref(r->ecdsa_meth);
167 }
168
169 EC_GROUP_free(r->group);
170 EC_POINT_free(r->pub_key);
171 ec_wrapped_scalar_free(r->priv_key);
172
173 CRYPTO_free_ex_data(g_ec_ex_data_class_bss_get(), r, &r->ex_data);
174
175 OPENSSL_free(r);
176 }
177
EC_KEY_dup(const EC_KEY * src)178 EC_KEY *EC_KEY_dup(const EC_KEY *src) {
179 if (src == NULL) {
180 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
181 return NULL;
182 }
183
184 EC_KEY *ret = EC_KEY_new();
185 if (ret == NULL) {
186 return NULL;
187 }
188
189 if ((src->group != NULL &&
190 !EC_KEY_set_group(ret, src->group)) ||
191 (src->pub_key != NULL &&
192 !EC_KEY_set_public_key(ret, src->pub_key)) ||
193 (src->priv_key != NULL &&
194 !EC_KEY_set_private_key(ret, EC_KEY_get0_private_key(src)))) {
195 EC_KEY_free(ret);
196 return NULL;
197 }
198
199 ret->enc_flag = src->enc_flag;
200 ret->conv_form = src->conv_form;
201 return ret;
202 }
203
EC_KEY_up_ref(EC_KEY * r)204 int EC_KEY_up_ref(EC_KEY *r) {
205 CRYPTO_refcount_inc(&r->references);
206 return 1;
207 }
208
EC_KEY_is_opaque(const EC_KEY * key)209 int EC_KEY_is_opaque(const EC_KEY *key) {
210 return key->ecdsa_meth && (key->ecdsa_meth->flags & ECDSA_FLAG_OPAQUE);
211 }
212
EC_KEY_get0_group(const EC_KEY * key)213 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key) { return key->group; }
214
EC_KEY_set_group(EC_KEY * key,const EC_GROUP * group)215 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group) {
216 // If |key| already has a group, it is an error to switch to another one.
217 if (key->group != NULL) {
218 if (EC_GROUP_cmp(key->group, group, NULL) != 0) {
219 OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH);
220 return 0;
221 }
222 return 1;
223 }
224
225 assert(key->priv_key == NULL);
226 assert(key->pub_key == NULL);
227
228 EC_GROUP_free(key->group);
229 key->group = EC_GROUP_dup(group);
230 return key->group != NULL;
231 }
232
EC_KEY_get0_private_key(const EC_KEY * key)233 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key) {
234 return key->priv_key != NULL ? &key->priv_key->bignum : NULL;
235 }
236
EC_KEY_set_private_key(EC_KEY * key,const BIGNUM * priv_key)237 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key) {
238 if (key->group == NULL) {
239 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS);
240 return 0;
241 }
242
243 EC_WRAPPED_SCALAR *scalar = ec_wrapped_scalar_new(key->group);
244 if (scalar == NULL) {
245 return 0;
246 }
247 if (!ec_bignum_to_scalar(key->group, &scalar->scalar, priv_key) ||
248 ec_scalar_is_zero(key->group, &scalar->scalar)) {
249 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_PRIVATE_KEY);
250 ec_wrapped_scalar_free(scalar);
251 return 0;
252 }
253 ec_wrapped_scalar_free(key->priv_key);
254 key->priv_key = scalar;
255 return 1;
256 }
257
EC_KEY_get0_public_key(const EC_KEY * key)258 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key) {
259 return key->pub_key;
260 }
261
EC_KEY_set_public_key(EC_KEY * key,const EC_POINT * pub_key)262 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key) {
263 if (key->group == NULL) {
264 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS);
265 return 0;
266 }
267
268 if (pub_key != NULL && EC_GROUP_cmp(key->group, pub_key->group, NULL) != 0) {
269 OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH);
270 return 0;
271 }
272
273 EC_POINT_free(key->pub_key);
274 key->pub_key = EC_POINT_dup(pub_key, key->group);
275 return (key->pub_key == NULL) ? 0 : 1;
276 }
277
EC_KEY_get_enc_flags(const EC_KEY * key)278 unsigned int EC_KEY_get_enc_flags(const EC_KEY *key) { return key->enc_flag; }
279
EC_KEY_set_enc_flags(EC_KEY * key,unsigned int flags)280 void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags) {
281 key->enc_flag = flags;
282 }
283
EC_KEY_get_conv_form(const EC_KEY * key)284 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key) {
285 return key->conv_form;
286 }
287
EC_KEY_set_conv_form(EC_KEY * key,point_conversion_form_t cform)288 void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform) {
289 key->conv_form = cform;
290 }
291
EC_KEY_check_key(const EC_KEY * eckey)292 int EC_KEY_check_key(const EC_KEY *eckey) {
293 if (!eckey || !eckey->group || !eckey->pub_key) {
294 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
295 return 0;
296 }
297
298 if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
299 OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
300 return 0;
301 }
302
303 // Test whether the public key is on the elliptic curve.
304 if (!EC_POINT_is_on_curve(eckey->group, eckey->pub_key, NULL)) {
305 OPENSSL_PUT_ERROR(EC, EC_R_POINT_IS_NOT_ON_CURVE);
306 return 0;
307 }
308
309 // Check the public and private keys match.
310 //
311 // NOTE: this is a FIPS pair-wise consistency check for the ECDH case. See SP
312 // 800-56Ar3, page 36.
313 if (eckey->priv_key != NULL) {
314 EC_RAW_POINT point;
315 if (!ec_point_mul_scalar_base(eckey->group, &point,
316 &eckey->priv_key->scalar)) {
317 OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
318 return 0;
319 }
320 if (!ec_GFp_simple_points_equal(eckey->group, &point,
321 &eckey->pub_key->raw)) {
322 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_PRIVATE_KEY);
323 return 0;
324 }
325 }
326
327 return 1;
328 }
329
EC_KEY_check_fips(const EC_KEY * key)330 int EC_KEY_check_fips(const EC_KEY *key) {
331 int ret = 0;
332 FIPS_service_indicator_lock_state();
333
334 if (EC_KEY_is_opaque(key)) {
335 // Opaque keys can't be checked.
336 OPENSSL_PUT_ERROR(EC, EC_R_PUBLIC_KEY_VALIDATION_FAILED);
337 goto end;
338 }
339
340 if (!EC_KEY_check_key(key)) {
341 goto end;
342 }
343
344 if (key->priv_key) {
345 uint8_t data[16] = {0};
346 ECDSA_SIG *sig = ECDSA_do_sign(data, sizeof(data), key);
347 if (boringssl_fips_break_test("ECDSA_PWCT")) {
348 data[0] = ~data[0];
349 }
350 int ok = sig != NULL &&
351 ECDSA_do_verify(data, sizeof(data), sig, key);
352 ECDSA_SIG_free(sig);
353 if (!ok) {
354 OPENSSL_PUT_ERROR(EC, EC_R_PUBLIC_KEY_VALIDATION_FAILED);
355 goto end;
356 }
357 }
358
359 ret = 1;
360
361 end:
362 FIPS_service_indicator_unlock_state();
363 if (ret) {
364 EC_KEY_keygen_verify_service_indicator(key);
365 }
366
367 return ret;
368 }
369
EC_KEY_set_public_key_affine_coordinates(EC_KEY * key,const BIGNUM * x,const BIGNUM * y)370 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, const BIGNUM *x,
371 const BIGNUM *y) {
372 EC_POINT *point = NULL;
373 int ok = 0;
374
375 if (!key || !key->group || !x || !y) {
376 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
377 return 0;
378 }
379
380 point = EC_POINT_new(key->group);
381 if (point == NULL ||
382 !EC_POINT_set_affine_coordinates_GFp(key->group, point, x, y, NULL) ||
383 !EC_KEY_set_public_key(key, point) ||
384 !EC_KEY_check_key(key)) {
385 goto err;
386 }
387
388 ok = 1;
389
390 err:
391 EC_POINT_free(point);
392 return ok;
393 }
394
EC_KEY_oct2key(EC_KEY * key,const uint8_t * in,size_t len,BN_CTX * ctx)395 int EC_KEY_oct2key(EC_KEY *key, const uint8_t *in, size_t len, BN_CTX *ctx) {
396 if (key->group == NULL) {
397 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS);
398 return 0;
399 }
400
401 EC_POINT *point = EC_POINT_new(key->group);
402 int ok = point != NULL &&
403 EC_POINT_oct2point(key->group, point, in, len, ctx) &&
404 EC_KEY_set_public_key(key, point);
405 EC_POINT_free(point);
406 return ok;
407 }
408
EC_KEY_key2buf(const EC_KEY * key,point_conversion_form_t form,uint8_t ** out_buf,BN_CTX * ctx)409 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
410 uint8_t **out_buf, BN_CTX *ctx) {
411 if (key == NULL || key->pub_key == NULL || key->group == NULL) {
412 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS);
413 return 0;
414 }
415
416 return EC_POINT_point2buf(key->group, key->pub_key, form, out_buf, ctx);
417 }
418
EC_KEY_oct2priv(EC_KEY * key,const uint8_t * in,size_t len)419 int EC_KEY_oct2priv(EC_KEY *key, const uint8_t *in, size_t len) {
420 if (key->group == NULL) {
421 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS);
422 return 0;
423 }
424
425 if (len != BN_num_bytes(EC_GROUP_get0_order(key->group))) {
426 OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
427 return 0;
428 }
429
430 BIGNUM *priv_key = BN_bin2bn(in, len, NULL);
431 int ok = priv_key != NULL && //
432 EC_KEY_set_private_key(key, priv_key);
433 BN_free(priv_key);
434 return ok;
435 }
436
EC_KEY_priv2oct(const EC_KEY * key,uint8_t * out,size_t max_out)437 size_t EC_KEY_priv2oct(const EC_KEY *key, uint8_t *out, size_t max_out) {
438 if (key->group == NULL || key->priv_key == NULL) {
439 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS);
440 return 0;
441 }
442
443 size_t len = BN_num_bytes(EC_GROUP_get0_order(key->group));
444 if (out == NULL) {
445 return len;
446 }
447
448 if (max_out < len) {
449 OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
450 return 0;
451 }
452
453 size_t bytes_written;
454 ec_scalar_to_bytes(key->group, out, &bytes_written, &key->priv_key->scalar);
455 assert(bytes_written == len);
456 return len;
457 }
458
EC_KEY_priv2buf(const EC_KEY * key,uint8_t ** out_buf)459 size_t EC_KEY_priv2buf(const EC_KEY *key, uint8_t **out_buf) {
460 *out_buf = NULL;
461 size_t len = EC_KEY_priv2oct(key, NULL, 0);
462 if (len == 0) {
463 return 0;
464 }
465
466 uint8_t *buf = OPENSSL_malloc(len);
467 if (buf == NULL) {
468 return 0;
469 }
470
471 len = EC_KEY_priv2oct(key, buf, len);
472 if (len == 0) {
473 OPENSSL_free(buf);
474 return 0;
475 }
476
477 *out_buf = buf;
478 return len;
479 }
480
EC_KEY_generate_key(EC_KEY * key)481 int EC_KEY_generate_key(EC_KEY *key) {
482 if (key == NULL || key->group == NULL) {
483 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
484 return 0;
485 }
486
487 // Check that the group order is FIPS compliant (FIPS 186-4 B.4.2).
488 if (BN_num_bits(EC_GROUP_get0_order(key->group)) < 160) {
489 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_GROUP_ORDER);
490 return 0;
491 }
492
493 static const uint8_t kDefaultAdditionalData[32] = {0};
494 EC_WRAPPED_SCALAR *priv_key = ec_wrapped_scalar_new(key->group);
495 EC_POINT *pub_key = EC_POINT_new(key->group);
496 if (priv_key == NULL || pub_key == NULL ||
497 // Generate the private key by testing candidates (FIPS 186-4 B.4.2).
498 !ec_random_nonzero_scalar(key->group, &priv_key->scalar,
499 kDefaultAdditionalData) ||
500 !ec_point_mul_scalar_base(key->group, &pub_key->raw, &priv_key->scalar)) {
501 EC_POINT_free(pub_key);
502 ec_wrapped_scalar_free(priv_key);
503 return 0;
504 }
505
506 ec_wrapped_scalar_free(key->priv_key);
507 key->priv_key = priv_key;
508 EC_POINT_free(key->pub_key);
509 key->pub_key = pub_key;
510 return 1;
511 }
512
EC_KEY_generate_key_fips(EC_KEY * eckey)513 int EC_KEY_generate_key_fips(EC_KEY *eckey) {
514 boringssl_ensure_ecc_self_test();
515
516 if (EC_KEY_generate_key(eckey) && EC_KEY_check_fips(eckey)) {
517 return 1;
518 }
519
520 EC_POINT_free(eckey->pub_key);
521 ec_wrapped_scalar_free(eckey->priv_key);
522 eckey->pub_key = NULL;
523 eckey->priv_key = NULL;
524 return 0;
525 }
526
EC_KEY_get_ex_new_index(long argl,void * argp,CRYPTO_EX_unused * unused,CRYPTO_EX_dup * dup_unused,CRYPTO_EX_free * free_func)527 int EC_KEY_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
528 CRYPTO_EX_dup *dup_unused,
529 CRYPTO_EX_free *free_func) {
530 int index;
531 if (!CRYPTO_get_ex_new_index(g_ec_ex_data_class_bss_get(), &index, argl, argp,
532 free_func)) {
533 return -1;
534 }
535 return index;
536 }
537
EC_KEY_set_ex_data(EC_KEY * d,int idx,void * arg)538 int EC_KEY_set_ex_data(EC_KEY *d, int idx, void *arg) {
539 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
540 }
541
EC_KEY_get_ex_data(const EC_KEY * d,int idx)542 void *EC_KEY_get_ex_data(const EC_KEY *d, int idx) {
543 return CRYPTO_get_ex_data(&d->ex_data, idx);
544 }
545
EC_KEY_set_asn1_flag(EC_KEY * key,int flag)546 void EC_KEY_set_asn1_flag(EC_KEY *key, int flag) {}
547