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 * [email protected].
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 * ([email protected]). This product includes software written by Tim
52 * Hudson ([email protected]).
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_zalloc(sizeof(EC_WRAPPED_SCALAR));
90 if (wrapped == NULL) {
91 return NULL;
92 }
93
94 wrapped->bignum.d = wrapped->scalar.words;
95 wrapped->bignum.width = group->order.N.width;
96 wrapped->bignum.dmax = group->order.N.width;
97 wrapped->bignum.flags = BN_FLG_STATIC_DATA;
98 return wrapped;
99 }
100
ec_wrapped_scalar_free(EC_WRAPPED_SCALAR * scalar)101 static void ec_wrapped_scalar_free(EC_WRAPPED_SCALAR *scalar) {
102 OPENSSL_free(scalar);
103 }
104
EC_KEY_new(void)105 EC_KEY *EC_KEY_new(void) { return EC_KEY_new_method(NULL); }
106
EC_KEY_new_method(const ENGINE * engine)107 EC_KEY *EC_KEY_new_method(const ENGINE *engine) {
108 EC_KEY *ret = OPENSSL_zalloc(sizeof(EC_KEY));
109 if (ret == NULL) {
110 return NULL;
111 }
112
113 if (engine) {
114 ret->ecdsa_meth = ENGINE_get_ECDSA_method(engine);
115 }
116 if (ret->ecdsa_meth) {
117 METHOD_ref(ret->ecdsa_meth);
118 }
119
120 ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
121 ret->references = 1;
122
123 CRYPTO_new_ex_data(&ret->ex_data);
124
125 if (ret->ecdsa_meth && ret->ecdsa_meth->init && !ret->ecdsa_meth->init(ret)) {
126 CRYPTO_free_ex_data(g_ec_ex_data_class_bss_get(), ret, &ret->ex_data);
127 if (ret->ecdsa_meth) {
128 METHOD_unref(ret->ecdsa_meth);
129 }
130 OPENSSL_free(ret);
131 return NULL;
132 }
133
134 return ret;
135 }
136
EC_KEY_new_by_curve_name(int nid)137 EC_KEY *EC_KEY_new_by_curve_name(int nid) {
138 EC_KEY *ret = EC_KEY_new();
139 if (ret == NULL) {
140 return NULL;
141 }
142 ret->group = EC_GROUP_new_by_curve_name(nid);
143 if (ret->group == NULL) {
144 EC_KEY_free(ret);
145 return NULL;
146 }
147 return ret;
148 }
149
EC_KEY_free(EC_KEY * r)150 void EC_KEY_free(EC_KEY *r) {
151 if (r == NULL) {
152 return;
153 }
154
155 if (!CRYPTO_refcount_dec_and_test_zero(&r->references)) {
156 return;
157 }
158
159 if (r->ecdsa_meth) {
160 if (r->ecdsa_meth->finish) {
161 r->ecdsa_meth->finish(r);
162 }
163 METHOD_unref(r->ecdsa_meth);
164 }
165
166 CRYPTO_free_ex_data(g_ec_ex_data_class_bss_get(), r, &r->ex_data);
167
168 EC_GROUP_free(r->group);
169 EC_POINT_free(r->pub_key);
170 ec_wrapped_scalar_free(r->priv_key);
171
172 OPENSSL_free(r);
173 }
174
EC_KEY_dup(const EC_KEY * src)175 EC_KEY *EC_KEY_dup(const EC_KEY *src) {
176 if (src == NULL) {
177 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
178 return NULL;
179 }
180
181 EC_KEY *ret = EC_KEY_new();
182 if (ret == NULL) {
183 return NULL;
184 }
185
186 if ((src->group != NULL &&
187 !EC_KEY_set_group(ret, src->group)) ||
188 (src->pub_key != NULL &&
189 !EC_KEY_set_public_key(ret, src->pub_key)) ||
190 (src->priv_key != NULL &&
191 !EC_KEY_set_private_key(ret, EC_KEY_get0_private_key(src)))) {
192 EC_KEY_free(ret);
193 return NULL;
194 }
195
196 ret->enc_flag = src->enc_flag;
197 ret->conv_form = src->conv_form;
198 return ret;
199 }
200
EC_KEY_up_ref(EC_KEY * r)201 int EC_KEY_up_ref(EC_KEY *r) {
202 CRYPTO_refcount_inc(&r->references);
203 return 1;
204 }
205
EC_KEY_is_opaque(const EC_KEY * key)206 int EC_KEY_is_opaque(const EC_KEY *key) {
207 return key->ecdsa_meth && (key->ecdsa_meth->flags & ECDSA_FLAG_OPAQUE);
208 }
209
EC_KEY_get0_group(const EC_KEY * key)210 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key) { return key->group; }
211
EC_KEY_set_group(EC_KEY * key,const EC_GROUP * group)212 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group) {
213 // If |key| already has a group, it is an error to switch to another one.
214 if (key->group != NULL) {
215 if (EC_GROUP_cmp(key->group, group, NULL) != 0) {
216 OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH);
217 return 0;
218 }
219 return 1;
220 }
221
222 assert(key->priv_key == NULL);
223 assert(key->pub_key == NULL);
224
225 EC_GROUP_free(key->group);
226 key->group = EC_GROUP_dup(group);
227 return key->group != NULL;
228 }
229
EC_KEY_get0_private_key(const EC_KEY * key)230 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key) {
231 return key->priv_key != NULL ? &key->priv_key->bignum : NULL;
232 }
233
EC_KEY_set_private_key(EC_KEY * key,const BIGNUM * priv_key)234 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key) {
235 if (key->group == NULL) {
236 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS);
237 return 0;
238 }
239
240 EC_WRAPPED_SCALAR *scalar = ec_wrapped_scalar_new(key->group);
241 if (scalar == NULL) {
242 return 0;
243 }
244 if (!ec_bignum_to_scalar(key->group, &scalar->scalar, priv_key) ||
245 // Zero is not a valid private key, so it is safe to leak the result of
246 // this comparison.
247 constant_time_declassify_int(
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_JACOBIAN 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 // Leaking this comparison only leaks whether |eckey|'s public key was
321 // correct.
322 if (!constant_time_declassify_int(ec_GFp_simple_points_equal(
323 eckey->group, &point, &eckey->pub_key->raw))) {
324 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_PRIVATE_KEY);
325 return 0;
326 }
327 }
328
329 return 1;
330 }
331
EC_KEY_check_fips(const EC_KEY * key)332 int EC_KEY_check_fips(const EC_KEY *key) {
333 int ret = 0;
334 FIPS_service_indicator_lock_state();
335
336 if (EC_KEY_is_opaque(key)) {
337 // Opaque keys can't be checked.
338 OPENSSL_PUT_ERROR(EC, EC_R_PUBLIC_KEY_VALIDATION_FAILED);
339 goto end;
340 }
341
342 if (!EC_KEY_check_key(key)) {
343 goto end;
344 }
345
346 if (key->priv_key) {
347 uint8_t data[16] = {0};
348 ECDSA_SIG *sig = ECDSA_do_sign(data, sizeof(data), key);
349 if (boringssl_fips_break_test("ECDSA_PWCT")) {
350 data[0] = ~data[0];
351 }
352 int ok = sig != NULL &&
353 ECDSA_do_verify(data, sizeof(data), sig, key);
354 ECDSA_SIG_free(sig);
355 if (!ok) {
356 OPENSSL_PUT_ERROR(EC, EC_R_PUBLIC_KEY_VALIDATION_FAILED);
357 goto end;
358 }
359 }
360
361 ret = 1;
362
363 end:
364 FIPS_service_indicator_unlock_state();
365 if (ret) {
366 EC_KEY_keygen_verify_service_indicator(key);
367 }
368
369 return ret;
370 }
371
EC_KEY_set_public_key_affine_coordinates(EC_KEY * key,const BIGNUM * x,const BIGNUM * y)372 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, const BIGNUM *x,
373 const BIGNUM *y) {
374 EC_POINT *point = NULL;
375 int ok = 0;
376
377 if (!key || !key->group || !x || !y) {
378 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
379 return 0;
380 }
381
382 point = EC_POINT_new(key->group);
383 if (point == NULL ||
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)) {
387 goto err;
388 }
389
390 ok = 1;
391
392 err:
393 EC_POINT_free(point);
394 return ok;
395 }
396
EC_KEY_oct2key(EC_KEY * key,const uint8_t * in,size_t len,BN_CTX * ctx)397 int EC_KEY_oct2key(EC_KEY *key, const uint8_t *in, size_t len, BN_CTX *ctx) {
398 if (key->group == NULL) {
399 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS);
400 return 0;
401 }
402
403 EC_POINT *point = EC_POINT_new(key->group);
404 int ok = point != NULL &&
405 EC_POINT_oct2point(key->group, point, in, len, ctx) &&
406 EC_KEY_set_public_key(key, point);
407 EC_POINT_free(point);
408 return ok;
409 }
410
EC_KEY_key2buf(const EC_KEY * key,point_conversion_form_t form,uint8_t ** out_buf,BN_CTX * ctx)411 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
412 uint8_t **out_buf, BN_CTX *ctx) {
413 if (key == NULL || key->pub_key == NULL || key->group == NULL) {
414 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS);
415 return 0;
416 }
417
418 return EC_POINT_point2buf(key->group, key->pub_key, form, out_buf, ctx);
419 }
420
EC_KEY_oct2priv(EC_KEY * key,const uint8_t * in,size_t len)421 int EC_KEY_oct2priv(EC_KEY *key, const uint8_t *in, size_t len) {
422 if (key->group == NULL) {
423 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS);
424 return 0;
425 }
426
427 if (len != BN_num_bytes(EC_GROUP_get0_order(key->group))) {
428 OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
429 return 0;
430 }
431
432 BIGNUM *priv_key = BN_bin2bn(in, len, NULL);
433 int ok = priv_key != NULL && //
434 EC_KEY_set_private_key(key, priv_key);
435 BN_free(priv_key);
436 return ok;
437 }
438
EC_KEY_priv2oct(const EC_KEY * key,uint8_t * out,size_t max_out)439 size_t EC_KEY_priv2oct(const EC_KEY *key, uint8_t *out, size_t max_out) {
440 if (key->group == NULL || key->priv_key == NULL) {
441 OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS);
442 return 0;
443 }
444
445 size_t len = BN_num_bytes(EC_GROUP_get0_order(key->group));
446 if (out == NULL) {
447 return len;
448 }
449
450 if (max_out < len) {
451 OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
452 return 0;
453 }
454
455 size_t bytes_written;
456 ec_scalar_to_bytes(key->group, out, &bytes_written, &key->priv_key->scalar);
457 assert(bytes_written == len);
458 return len;
459 }
460
EC_KEY_priv2buf(const EC_KEY * key,uint8_t ** out_buf)461 size_t EC_KEY_priv2buf(const EC_KEY *key, uint8_t **out_buf) {
462 *out_buf = NULL;
463 size_t len = EC_KEY_priv2oct(key, NULL, 0);
464 if (len == 0) {
465 return 0;
466 }
467
468 uint8_t *buf = OPENSSL_malloc(len);
469 if (buf == NULL) {
470 return 0;
471 }
472
473 len = EC_KEY_priv2oct(key, buf, len);
474 if (len == 0) {
475 OPENSSL_free(buf);
476 return 0;
477 }
478
479 *out_buf = buf;
480 return len;
481 }
482
EC_KEY_generate_key(EC_KEY * key)483 int EC_KEY_generate_key(EC_KEY *key) {
484 if (key == NULL || key->group == NULL) {
485 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
486 return 0;
487 }
488
489 // Check that the group order is FIPS compliant (FIPS 186-4 B.4.2).
490 if (EC_GROUP_order_bits(key->group) < 160) {
491 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_GROUP_ORDER);
492 return 0;
493 }
494
495 static const uint8_t kDefaultAdditionalData[32] = {0};
496 EC_WRAPPED_SCALAR *priv_key = ec_wrapped_scalar_new(key->group);
497 EC_POINT *pub_key = EC_POINT_new(key->group);
498 if (priv_key == NULL || pub_key == NULL ||
499 // Generate the private key by testing candidates (FIPS 186-4 B.4.2).
500 !ec_random_nonzero_scalar(key->group, &priv_key->scalar,
501 kDefaultAdditionalData) ||
502 !ec_point_mul_scalar_base(key->group, &pub_key->raw, &priv_key->scalar)) {
503 EC_POINT_free(pub_key);
504 ec_wrapped_scalar_free(priv_key);
505 return 0;
506 }
507
508 // The public key is derived from the private key, but it is public.
509 //
510 // TODO(crbug.com/boringssl/677): This isn't quite right. While |pub_key|
511 // represents a public point, it is still in Jacobian form and the exact
512 // Jacobian representation is secret. We need to make it affine first. See
513 // discussion in the bug.
514 CONSTTIME_DECLASSIFY(&pub_key->raw, sizeof(pub_key->raw));
515
516 ec_wrapped_scalar_free(key->priv_key);
517 key->priv_key = priv_key;
518 EC_POINT_free(key->pub_key);
519 key->pub_key = pub_key;
520 return 1;
521 }
522
EC_KEY_generate_key_fips(EC_KEY * eckey)523 int EC_KEY_generate_key_fips(EC_KEY *eckey) {
524 if (eckey == NULL || eckey->group == NULL) {
525 OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
526 return 0;
527 }
528
529 boringssl_ensure_ecc_self_test();
530
531 if (EC_KEY_generate_key(eckey) && EC_KEY_check_fips(eckey)) {
532 return 1;
533 }
534
535 EC_POINT_free(eckey->pub_key);
536 ec_wrapped_scalar_free(eckey->priv_key);
537 eckey->pub_key = NULL;
538 eckey->priv_key = NULL;
539 return 0;
540 }
541
EC_KEY_get_ex_new_index(long argl,void * argp,CRYPTO_EX_unused * unused,CRYPTO_EX_dup * dup_unused,CRYPTO_EX_free * free_func)542 int EC_KEY_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
543 CRYPTO_EX_dup *dup_unused,
544 CRYPTO_EX_free *free_func) {
545 return CRYPTO_get_ex_new_index_ex(g_ec_ex_data_class_bss_get(), argl, argp,
546 free_func);
547 }
548
EC_KEY_set_ex_data(EC_KEY * d,int idx,void * arg)549 int EC_KEY_set_ex_data(EC_KEY *d, int idx, void *arg) {
550 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
551 }
552
EC_KEY_get_ex_data(const EC_KEY * d,int idx)553 void *EC_KEY_get_ex_data(const EC_KEY *d, int idx) {
554 return CRYPTO_get_ex_data(&d->ex_data, idx);
555 }
556
EC_KEY_set_asn1_flag(EC_KEY * key,int flag)557 void EC_KEY_set_asn1_flag(EC_KEY *key, int flag) {}
558