1 /*
2 * Copyright 2002-2025 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4 *
5 * Licensed under the Apache License 2.0 (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
9 */
10
11 /*
12 * EC_KEY low level APIs are deprecated for public use, but still ok for
13 * internal use.
14 */
15 #include "internal/deprecated.h"
16
17 #include "internal/cryptlib.h"
18 #include <string.h>
19 #include "ec_local.h"
20 #include "internal/refcount.h"
21 #include <openssl/err.h>
22 #ifndef FIPS_MODULE
23 #include <openssl/engine.h>
24 #endif
25 #include <openssl/self_test.h>
26 #include "prov/providercommon.h"
27 #include "prov/ecx.h"
28 #include "crypto/bn.h"
29
30 static int ecdsa_keygen_pairwise_test(EC_KEY *eckey, OSSL_CALLBACK *cb,
31 void *cbarg);
32
33 #ifndef FIPS_MODULE
EC_KEY_new(void)34 EC_KEY *EC_KEY_new(void)
35 {
36 return ossl_ec_key_new_method_int(NULL, NULL, NULL);
37 }
38 #endif
39
EC_KEY_new_ex(OSSL_LIB_CTX * ctx,const char * propq)40 EC_KEY *EC_KEY_new_ex(OSSL_LIB_CTX *ctx, const char *propq)
41 {
42 return ossl_ec_key_new_method_int(ctx, propq, NULL);
43 }
44
EC_KEY_new_by_curve_name_ex(OSSL_LIB_CTX * ctx,const char * propq,int nid)45 EC_KEY *EC_KEY_new_by_curve_name_ex(OSSL_LIB_CTX *ctx, const char *propq,
46 int nid)
47 {
48 EC_KEY *ret = EC_KEY_new_ex(ctx, propq);
49 if (ret == NULL)
50 return NULL;
51 ret->group = EC_GROUP_new_by_curve_name_ex(ctx, propq, nid);
52 if (ret->group == NULL) {
53 EC_KEY_free(ret);
54 return NULL;
55 }
56 if (ret->meth->set_group != NULL
57 && ret->meth->set_group(ret, ret->group) == 0) {
58 EC_KEY_free(ret);
59 return NULL;
60 }
61 return ret;
62 }
63
64 #ifndef FIPS_MODULE
EC_KEY_new_by_curve_name(int nid)65 EC_KEY *EC_KEY_new_by_curve_name(int nid)
66 {
67 return EC_KEY_new_by_curve_name_ex(NULL, NULL, nid);
68 }
69 #endif
70
EC_KEY_free(EC_KEY * r)71 void EC_KEY_free(EC_KEY *r)
72 {
73 int i;
74
75 if (r == NULL)
76 return;
77
78 CRYPTO_DOWN_REF(&r->references, &i);
79 REF_PRINT_COUNT("EC_KEY", i, r);
80 if (i > 0)
81 return;
82 REF_ASSERT_ISNT(i < 0);
83
84 if (r->meth != NULL && r->meth->finish != NULL)
85 r->meth->finish(r);
86
87 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
88 ENGINE_finish(r->engine);
89 #endif
90
91 if (r->group && r->group->meth->keyfinish)
92 r->group->meth->keyfinish(r);
93
94 #ifndef FIPS_MODULE
95 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
96 #endif
97 CRYPTO_FREE_REF(&r->references);
98 EC_GROUP_free(r->group);
99 EC_POINT_free(r->pub_key);
100 BN_clear_free(r->priv_key);
101 OPENSSL_free(r->propq);
102
103 OPENSSL_clear_free((void *)r, sizeof(EC_KEY));
104 }
105
EC_KEY_copy(EC_KEY * dest,const EC_KEY * src)106 EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
107 {
108 if (dest == NULL || src == NULL) {
109 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
110 return NULL;
111 }
112 if (src->meth != dest->meth) {
113 if (dest->meth->finish != NULL)
114 dest->meth->finish(dest);
115 if (dest->group && dest->group->meth->keyfinish)
116 dest->group->meth->keyfinish(dest);
117 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
118 if (ENGINE_finish(dest->engine) == 0)
119 return 0;
120 dest->engine = NULL;
121 #endif
122 }
123 dest->libctx = src->libctx;
124 /* copy the parameters */
125 if (src->group != NULL) {
126 /* clear the old group */
127 EC_GROUP_free(dest->group);
128 dest->group = ossl_ec_group_new_ex(src->libctx, src->propq,
129 src->group->meth);
130 if (dest->group == NULL)
131 return NULL;
132 if (!EC_GROUP_copy(dest->group, src->group))
133 return NULL;
134
135 /* copy the public key */
136 if (src->pub_key != NULL) {
137 EC_POINT_free(dest->pub_key);
138 dest->pub_key = EC_POINT_new(src->group);
139 if (dest->pub_key == NULL)
140 return NULL;
141 if (!EC_POINT_copy(dest->pub_key, src->pub_key))
142 return NULL;
143 }
144 /* copy the private key */
145 if (src->priv_key != NULL) {
146 if (dest->priv_key == NULL) {
147 dest->priv_key = BN_new();
148 if (dest->priv_key == NULL)
149 return NULL;
150 }
151 if (!BN_copy(dest->priv_key, src->priv_key))
152 return NULL;
153 if (src->group->meth->keycopy
154 && src->group->meth->keycopy(dest, src) == 0)
155 return NULL;
156 }
157 }
158
159 /* copy the rest */
160 dest->enc_flag = src->enc_flag;
161 dest->conv_form = src->conv_form;
162 dest->version = src->version;
163 dest->flags = src->flags;
164 #ifndef FIPS_MODULE
165 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
166 &dest->ex_data, &src->ex_data))
167 return NULL;
168 #endif
169
170 if (src->meth != dest->meth) {
171 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
172 if (src->engine != NULL && ENGINE_init(src->engine) == 0)
173 return NULL;
174 dest->engine = src->engine;
175 #endif
176 dest->meth = src->meth;
177 }
178
179 if (src->meth->copy != NULL && src->meth->copy(dest, src) == 0)
180 return NULL;
181
182 dest->dirty_cnt++;
183
184 return dest;
185 }
186
EC_KEY_dup(const EC_KEY * ec_key)187 EC_KEY *EC_KEY_dup(const EC_KEY *ec_key)
188 {
189 return ossl_ec_key_dup(ec_key, OSSL_KEYMGMT_SELECT_ALL);
190 }
191
EC_KEY_up_ref(EC_KEY * r)192 int EC_KEY_up_ref(EC_KEY *r)
193 {
194 int i;
195
196 if (CRYPTO_UP_REF(&r->references, &i) <= 0)
197 return 0;
198
199 REF_PRINT_COUNT("EC_KEY", i, r);
200 REF_ASSERT_ISNT(i < 2);
201 return ((i > 1) ? 1 : 0);
202 }
203
EC_KEY_get0_engine(const EC_KEY * eckey)204 ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey)
205 {
206 return eckey->engine;
207 }
208
EC_KEY_generate_key(EC_KEY * eckey)209 int EC_KEY_generate_key(EC_KEY *eckey)
210 {
211 if (eckey == NULL || eckey->group == NULL) {
212 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
213 return 0;
214 }
215 if (eckey->meth->keygen != NULL) {
216 int ret;
217
218 ret = eckey->meth->keygen(eckey);
219 if (ret == 1)
220 eckey->dirty_cnt++;
221
222 return ret;
223 }
224 ERR_raise(ERR_LIB_EC, EC_R_OPERATION_NOT_SUPPORTED);
225 return 0;
226 }
227
ossl_ec_key_gen(EC_KEY * eckey)228 int ossl_ec_key_gen(EC_KEY *eckey)
229 {
230 int ret;
231
232 ret = eckey->group->meth->keygen(eckey);
233
234 if (ret == 1)
235 eckey->dirty_cnt++;
236 return ret;
237 }
238
239 /*
240 * Refer: FIPS 140-3 IG 10.3.A Additional Comment 1
241 * Perform a KAT by duplicating the public key generation.
242 *
243 * NOTE: This issue requires a background understanding, provided in a separate
244 * document; the current IG 10.3.A AC1 is insufficient regarding the PCT for
245 * the key agreement scenario.
246 *
247 * Currently IG 10.3.A requires PCT in the mode of use prior to use of the
248 * key pair, citing the PCT defined in the associated standard. For key
249 * agreement, the only PCT defined in SP 800-56A is that of Section 5.6.2.4:
250 * the comparison of the original public key to a newly calculated public key.
251 */
ecdsa_keygen_knownanswer_test(EC_KEY * eckey,BN_CTX * ctx,OSSL_CALLBACK * cb,void * cbarg)252 static int ecdsa_keygen_knownanswer_test(EC_KEY *eckey, BN_CTX *ctx,
253 OSSL_CALLBACK *cb, void *cbarg)
254 {
255 int len, ret = 0;
256 OSSL_SELF_TEST *st = NULL;
257 unsigned char bytes[512] = { 0 };
258 EC_POINT *pub_key2 = NULL;
259
260 st = OSSL_SELF_TEST_new(cb, cbarg);
261 if (st == NULL)
262 return 0;
263
264 OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT_KAT,
265 OSSL_SELF_TEST_DESC_PCT_ECDSA);
266
267 if ((pub_key2 = EC_POINT_new(eckey->group)) == NULL)
268 goto err;
269
270 /* pub_key = priv_key * G (where G is a point on the curve) */
271 if (!EC_POINT_mul(eckey->group, pub_key2, eckey->priv_key, NULL, NULL, ctx))
272 goto err;
273
274 if (BN_num_bytes(pub_key2->X) > (int)sizeof(bytes))
275 goto err;
276 len = BN_bn2bin(pub_key2->X, bytes);
277 if (OSSL_SELF_TEST_oncorrupt_byte(st, bytes)
278 && BN_bin2bn(bytes, len, pub_key2->X) == NULL)
279 goto err;
280 ret = !EC_POINT_cmp(eckey->group, eckey->pub_key, pub_key2, ctx);
281
282 err:
283 OSSL_SELF_TEST_onend(st, ret);
284 OSSL_SELF_TEST_free(st);
285 EC_POINT_free(pub_key2);
286 return ret;
287 }
288
289 /*
290 * ECC Key generation.
291 * See SP800-56AR3 5.6.1.2.2 "Key Pair Generation by Testing Candidates"
292 *
293 * Params:
294 * libctx A context containing an optional self test callback.
295 * eckey An EC key object that contains domain params. The generated keypair
296 * is stored in this object.
297 * pairwise_test Set to non zero to perform a pairwise test. If the test
298 * fails then the keypair is not generated,
299 * Returns 1 if the keypair was generated or 0 otherwise.
300 */
ec_generate_key(EC_KEY * eckey,int pairwise_test)301 static int ec_generate_key(EC_KEY *eckey, int pairwise_test)
302 {
303 int ok = 0;
304 BIGNUM *priv_key = NULL;
305 const BIGNUM *tmp = NULL;
306 BIGNUM *order = NULL;
307 EC_POINT *pub_key = NULL;
308 const EC_GROUP *group = eckey->group;
309 BN_CTX *ctx = BN_CTX_secure_new_ex(eckey->libctx);
310 int sm2 = EC_KEY_get_flags(eckey) & EC_FLAG_SM2_RANGE ? 1 : 0;
311
312 if (ctx == NULL)
313 goto err;
314
315 if (eckey->priv_key == NULL) {
316 priv_key = BN_secure_new();
317 if (priv_key == NULL)
318 goto err;
319 } else
320 priv_key = eckey->priv_key;
321
322 /*
323 * Steps (1-2): Check domain parameters and security strength.
324 * These steps must be done by the user. This would need to be
325 * stated in the security policy.
326 */
327
328 tmp = EC_GROUP_get0_order(group);
329 if (tmp == NULL)
330 goto err;
331
332 /*
333 * Steps (3-7): priv_key = DRBG_RAND(order_n_bits) (range [1, n-1]).
334 * Although this is slightly different from the standard, it is effectively
335 * equivalent as it gives an unbiased result ranging from 1..n-1. It is also
336 * faster as the standard needs to retry more often. Also doing
337 * 1 + rand[0..n-2] would effect the way that tests feed dummy entropy into
338 * rand so the simpler backward compatible method has been used here.
339 */
340
341 /* range of SM2 private key is [1, n-1) */
342 if (sm2) {
343 order = BN_new();
344 if (order == NULL || !BN_sub(order, tmp, BN_value_one()))
345 goto err;
346 } else {
347 order = BN_dup(tmp);
348 if (order == NULL)
349 goto err;
350 }
351
352 do
353 if (!BN_priv_rand_range_ex(priv_key, order, 0, ctx))
354 goto err;
355 while (BN_is_zero(priv_key));
356
357 if (eckey->pub_key == NULL) {
358 pub_key = EC_POINT_new(group);
359 if (pub_key == NULL)
360 goto err;
361 } else
362 pub_key = eckey->pub_key;
363
364 /* Step (8) : pub_key = priv_key * G (where G is a point on the curve) */
365 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx))
366 goto err;
367
368 eckey->priv_key = priv_key;
369 eckey->pub_key = pub_key;
370 priv_key = NULL;
371 pub_key = NULL;
372
373 eckey->dirty_cnt++;
374
375 #ifdef FIPS_MODULE
376 pairwise_test = 1;
377 #endif /* FIPS_MODULE */
378
379 ok = 1;
380 if (pairwise_test) {
381 OSSL_CALLBACK *cb = NULL;
382 void *cbarg = NULL;
383
384 OSSL_SELF_TEST_get_callback(eckey->libctx, &cb, &cbarg);
385 ok = ecdsa_keygen_pairwise_test(eckey, cb, cbarg)
386 && ecdsa_keygen_knownanswer_test(eckey, ctx, cb, cbarg);
387 }
388 err:
389 /* Step (9): If there is an error return an invalid keypair. */
390 if (!ok) {
391 ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT);
392 BN_clear(eckey->priv_key);
393 if (eckey->pub_key != NULL)
394 EC_POINT_set_to_infinity(group, eckey->pub_key);
395 }
396
397 EC_POINT_free(pub_key);
398 BN_clear_free(priv_key);
399 BN_CTX_free(ctx);
400 BN_free(order);
401 return ok;
402 }
403
404 #ifndef FIPS_MODULE
405 /*
406 * This is similar to ec_generate_key(), except it uses an ikm to
407 * derive the private key.
408 */
ossl_ec_generate_key_dhkem(EC_KEY * eckey,const unsigned char * ikm,size_t ikmlen)409 int ossl_ec_generate_key_dhkem(EC_KEY *eckey,
410 const unsigned char *ikm, size_t ikmlen)
411 {
412 int ok = 0;
413
414 if (eckey->priv_key == NULL) {
415 eckey->priv_key = BN_secure_new();
416 if (eckey->priv_key == NULL)
417 goto err;
418 }
419 if (ossl_ec_dhkem_derive_private(eckey, eckey->priv_key, ikm, ikmlen) <= 0)
420 goto err;
421 if (eckey->pub_key == NULL) {
422 eckey->pub_key = EC_POINT_new(eckey->group);
423 if (eckey->pub_key == NULL)
424 goto err;
425 }
426 if (!ossl_ec_key_simple_generate_public_key(eckey))
427 goto err;
428
429 ok = 1;
430 err:
431 if (!ok) {
432 BN_clear_free(eckey->priv_key);
433 eckey->priv_key = NULL;
434 if (eckey->pub_key != NULL)
435 EC_POINT_set_to_infinity(eckey->group, eckey->pub_key);
436 }
437 return ok;
438 }
439 #endif
440
ossl_ec_key_simple_generate_key(EC_KEY * eckey)441 int ossl_ec_key_simple_generate_key(EC_KEY *eckey)
442 {
443 return ec_generate_key(eckey, 0);
444 }
445
ossl_ec_key_simple_generate_public_key(EC_KEY * eckey)446 int ossl_ec_key_simple_generate_public_key(EC_KEY *eckey)
447 {
448 int ret;
449 BN_CTX *ctx = BN_CTX_new_ex(eckey->libctx);
450
451 if (ctx == NULL)
452 return 0;
453
454 /*
455 * See SP800-56AR3 5.6.1.2.2: Step (8)
456 * pub_key = priv_key * G (where G is a point on the curve)
457 */
458 ret = EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
459 NULL, ctx);
460
461 BN_CTX_free(ctx);
462 if (ret == 1)
463 eckey->dirty_cnt++;
464
465 return ret;
466 }
467
EC_KEY_check_key(const EC_KEY * eckey)468 int EC_KEY_check_key(const EC_KEY *eckey)
469 {
470 if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
471 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
472 return 0;
473 }
474
475 if (eckey->group->meth->keycheck == NULL) {
476 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
477 return 0;
478 }
479
480 return eckey->group->meth->keycheck(eckey);
481 }
482
483 /*
484 * Check the range of the EC public key.
485 * See SP800-56A R3 Section 5.6.2.3.3 (Part 2)
486 * i.e.
487 * - If q = odd prime p: Verify that xQ and yQ are integers in the
488 * interval[0, p - 1], OR
489 * - If q = 2m: Verify that xQ and yQ are bit strings of length m bits.
490 * Returns 1 if the public key has a valid range, otherwise it returns 0.
491 */
ec_key_public_range_check(BN_CTX * ctx,const EC_KEY * key)492 static int ec_key_public_range_check(BN_CTX *ctx, const EC_KEY *key)
493 {
494 int ret = 0;
495 BIGNUM *x, *y;
496
497 BN_CTX_start(ctx);
498 x = BN_CTX_get(ctx);
499 y = BN_CTX_get(ctx);
500 if (y == NULL)
501 goto err;
502
503 if (!EC_POINT_get_affine_coordinates(key->group, key->pub_key, x, y, ctx))
504 goto err;
505
506 if (EC_GROUP_get_field_type(key->group) == NID_X9_62_prime_field) {
507 if (BN_is_negative(x)
508 || BN_cmp(x, key->group->field) >= 0
509 || BN_is_negative(y)
510 || BN_cmp(y, key->group->field) >= 0) {
511 goto err;
512 }
513 } else {
514 int m = EC_GROUP_get_degree(key->group);
515 if (BN_num_bits(x) > m || BN_num_bits(y) > m) {
516 goto err;
517 }
518 }
519 ret = 1;
520 err:
521 BN_CTX_end(ctx);
522 return ret;
523 }
524
525 /*
526 * ECC Partial Public-Key Validation as specified in SP800-56A R3
527 * Section 5.6.2.3.4 ECC Partial Public-Key Validation Routine.
528 */
ossl_ec_key_public_check_quick(const EC_KEY * eckey,BN_CTX * ctx)529 int ossl_ec_key_public_check_quick(const EC_KEY *eckey, BN_CTX *ctx)
530 {
531 if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
532 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
533 return 0;
534 }
535
536 /* 5.6.2.3.3 (Step 1): Q != infinity */
537 if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
538 ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY);
539 return 0;
540 }
541
542 /* 5.6.2.3.3 (Step 2) Test if the public key is in range */
543 if (!ec_key_public_range_check(ctx, eckey)) {
544 ERR_raise(ERR_LIB_EC, EC_R_COORDINATES_OUT_OF_RANGE);
545 return 0;
546 }
547
548 /* 5.6.2.3.3 (Step 3) is the pub_key on the elliptic curve */
549 if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
550 ERR_raise(ERR_LIB_EC, EC_R_POINT_IS_NOT_ON_CURVE);
551 return 0;
552 }
553 return 1;
554 }
555
556 /*
557 * ECC Key validation as specified in SP800-56A R3.
558 * Section 5.6.2.3.3 ECC Full Public-Key Validation Routine.
559 */
ossl_ec_key_public_check(const EC_KEY * eckey,BN_CTX * ctx)560 int ossl_ec_key_public_check(const EC_KEY *eckey, BN_CTX *ctx)
561 {
562 int ret = 0;
563 EC_POINT *point = NULL;
564 const BIGNUM *order = NULL;
565 const BIGNUM *cofactor = EC_GROUP_get0_cofactor(eckey->group);
566
567 if (!ossl_ec_key_public_check_quick(eckey, ctx))
568 return 0;
569
570 if (cofactor != NULL && BN_is_one(cofactor)) {
571 /* Skip the unnecessary expensive computation for curves with cofactor of 1. */
572 return 1;
573 }
574
575 point = EC_POINT_new(eckey->group);
576 if (point == NULL)
577 return 0;
578
579 order = eckey->group->order;
580 if (BN_is_zero(order)) {
581 ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
582 goto err;
583 }
584 /* 5.6.2.3.3 (Step 4) : pub_key * order is the point at infinity. */
585 if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
586 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
587 goto err;
588 }
589 if (!EC_POINT_is_at_infinity(eckey->group, point)) {
590 ERR_raise(ERR_LIB_EC, EC_R_WRONG_ORDER);
591 goto err;
592 }
593 ret = 1;
594 err:
595 EC_POINT_free(point);
596 return ret;
597 }
598
599 /*
600 * ECC Key validation as specified in SP800-56A R3.
601 * Section 5.6.2.1.2 Owner Assurance of Private-Key Validity
602 * The private key is in the range [1, order-1]
603 */
ossl_ec_key_private_check(const EC_KEY * eckey)604 int ossl_ec_key_private_check(const EC_KEY *eckey)
605 {
606 if (eckey == NULL || eckey->group == NULL || eckey->priv_key == NULL) {
607 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
608 return 0;
609 }
610 if (BN_cmp(eckey->priv_key, BN_value_one()) < 0
611 || BN_cmp(eckey->priv_key, eckey->group->order) >= 0) {
612 ERR_raise(ERR_LIB_EC, EC_R_INVALID_PRIVATE_KEY);
613 return 0;
614 }
615 return 1;
616 }
617
618 /*
619 * ECC Key validation as specified in SP800-56A R3.
620 * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency (b)
621 * Check if generator * priv_key = pub_key
622 */
ossl_ec_key_pairwise_check(const EC_KEY * eckey,BN_CTX * ctx)623 int ossl_ec_key_pairwise_check(const EC_KEY *eckey, BN_CTX *ctx)
624 {
625 int ret = 0;
626 EC_POINT *point = NULL;
627
628 if (eckey == NULL
629 || eckey->group == NULL
630 || eckey->pub_key == NULL
631 || eckey->priv_key == NULL) {
632 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
633 return 0;
634 }
635
636 point = EC_POINT_new(eckey->group);
637 if (point == NULL)
638 goto err;
639
640 if (!EC_POINT_mul(eckey->group, point, eckey->priv_key, NULL, NULL, ctx)) {
641 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
642 goto err;
643 }
644 if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
645 ERR_raise(ERR_LIB_EC, EC_R_INVALID_PRIVATE_KEY);
646 goto err;
647 }
648 ret = 1;
649 err:
650 EC_POINT_free(point);
651 return ret;
652 }
653
654 /*
655 * ECC Key validation as specified in SP800-56A R3.
656 * Section 5.6.2.3.3 ECC Full Public-Key Validation
657 * Section 5.6.2.1.2 Owner Assurance of Private-Key Validity
658 * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
659 * NOTES:
660 * Before calling this method in fips mode, there should be an assurance that
661 * an approved elliptic-curve group is used.
662 * Returns 1 if the key is valid, otherwise it returns 0.
663 */
ossl_ec_key_simple_check_key(const EC_KEY * eckey)664 int ossl_ec_key_simple_check_key(const EC_KEY *eckey)
665 {
666 int ok = 0;
667 BN_CTX *ctx = NULL;
668
669 if (eckey == NULL) {
670 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
671 return 0;
672 }
673 if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL)
674 return 0;
675
676 if (!ossl_ec_key_public_check(eckey, ctx))
677 goto err;
678
679 if (eckey->priv_key != NULL) {
680 if (!ossl_ec_key_private_check(eckey)
681 || !ossl_ec_key_pairwise_check(eckey, ctx))
682 goto err;
683 }
684 ok = 1;
685 err:
686 BN_CTX_free(ctx);
687 return ok;
688 }
689
EC_KEY_set_public_key_affine_coordinates(EC_KEY * key,BIGNUM * x,BIGNUM * y)690 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
691 BIGNUM *y)
692 {
693 BN_CTX *ctx = NULL;
694 BIGNUM *tx, *ty;
695 EC_POINT *point = NULL;
696 int ok = 0;
697
698 if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
699 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
700 return 0;
701 }
702 ctx = BN_CTX_new_ex(key->libctx);
703 if (ctx == NULL)
704 return 0;
705
706 BN_CTX_start(ctx);
707 point = EC_POINT_new(key->group);
708
709 if (point == NULL)
710 goto err;
711
712 tx = BN_CTX_get(ctx);
713 ty = BN_CTX_get(ctx);
714 if (ty == NULL)
715 goto err;
716
717 if (!EC_POINT_set_affine_coordinates(key->group, point, x, y, ctx))
718 goto err;
719 if (!EC_POINT_get_affine_coordinates(key->group, point, tx, ty, ctx))
720 goto err;
721
722 /*
723 * Check if retrieved coordinates match originals. The range check is done
724 * inside EC_KEY_check_key().
725 */
726 if (BN_cmp(x, tx) || BN_cmp(y, ty)) {
727 ERR_raise(ERR_LIB_EC, EC_R_COORDINATES_OUT_OF_RANGE);
728 goto err;
729 }
730
731 /* EC_KEY_set_public_key updates dirty_cnt */
732 if (!EC_KEY_set_public_key(key, point))
733 goto err;
734
735 if (EC_KEY_check_key(key) == 0)
736 goto err;
737
738 ok = 1;
739
740 err:
741 BN_CTX_end(ctx);
742 BN_CTX_free(ctx);
743 EC_POINT_free(point);
744 return ok;
745 }
746
ossl_ec_key_get_libctx(const EC_KEY * key)747 OSSL_LIB_CTX *ossl_ec_key_get_libctx(const EC_KEY *key)
748 {
749 return key->libctx;
750 }
751
ossl_ec_key_get0_propq(const EC_KEY * key)752 const char *ossl_ec_key_get0_propq(const EC_KEY *key)
753 {
754 return key->propq;
755 }
756
ossl_ec_key_set0_libctx(EC_KEY * key,OSSL_LIB_CTX * libctx)757 void ossl_ec_key_set0_libctx(EC_KEY *key, OSSL_LIB_CTX *libctx)
758 {
759 key->libctx = libctx;
760 /* Do we need to propagate this to the group? */
761 }
762
EC_KEY_get0_group(const EC_KEY * key)763 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
764 {
765 return key->group;
766 }
767
EC_KEY_set_group(EC_KEY * key,const EC_GROUP * group)768 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
769 {
770 if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)
771 return 0;
772 EC_GROUP_free(key->group);
773 key->group = EC_GROUP_dup(group);
774 if (key->group != NULL && EC_GROUP_get_curve_name(key->group) == NID_sm2)
775 EC_KEY_set_flags(key, EC_FLAG_SM2_RANGE);
776
777 key->dirty_cnt++;
778 return (key->group == NULL) ? 0 : 1;
779 }
780
EC_KEY_get0_private_key(const EC_KEY * key)781 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
782 {
783 return key->priv_key;
784 }
785
EC_KEY_set_private_key(EC_KEY * key,const BIGNUM * priv_key)786 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
787 {
788 int fixed_top;
789 const BIGNUM *order = NULL;
790 BIGNUM *tmp_key = NULL;
791
792 if (key->group == NULL || key->group->meth == NULL)
793 return 0;
794
795 /*
796 * Not only should key->group be set, but it should also be in a valid
797 * fully initialized state.
798 *
799 * Specifically, to operate in constant time, we need that the group order
800 * is set, as we use its length as the fixed public size of any scalar used
801 * as an EC private key.
802 */
803 order = EC_GROUP_get0_order(key->group);
804 if (order == NULL || BN_is_zero(order))
805 return 0; /* This should never happen */
806
807 if (key->group->meth->set_private != NULL
808 && key->group->meth->set_private(key, priv_key) == 0)
809 return 0;
810 if (key->meth->set_private != NULL
811 && key->meth->set_private(key, priv_key) == 0)
812 return 0;
813
814 /*
815 * Return `0` to comply with legacy behavior for this function, see
816 * https://github.com/openssl/openssl/issues/18744#issuecomment-1195175696
817 */
818 if (priv_key == NULL) {
819 BN_clear_free(key->priv_key);
820 key->priv_key = NULL;
821 return 0; /* intentional for legacy compatibility */
822 }
823
824 /*
825 * We should never leak the bit length of the secret scalar in the key,
826 * so we always set the `BN_FLG_CONSTTIME` flag on the internal `BIGNUM`
827 * holding the secret scalar.
828 *
829 * This is important also because `BN_dup()` (and `BN_copy()`) do not
830 * propagate the `BN_FLG_CONSTTIME` flag from the source `BIGNUM`, and
831 * this brings an extra risk of inadvertently losing the flag, even when
832 * the caller specifically set it.
833 *
834 * The propagation has been turned on and off a few times in the past
835 * years because in some conditions has shown unintended consequences in
836 * some code paths, so at the moment we can't fix this in the BN layer.
837 *
838 * In `EC_KEY_set_private_key()` we can work around the propagation by
839 * manually setting the flag after `BN_dup()` as we know for sure that
840 * inside the EC module the `BN_FLG_CONSTTIME` is always treated
841 * correctly and should not generate unintended consequences.
842 *
843 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
844 * to preallocate the BIGNUM internal buffer to a fixed public size big
845 * enough that operations performed during the processing never trigger
846 * a realloc which would leak the size of the scalar through memory
847 * accesses.
848 *
849 * Fixed Length
850 * ------------
851 *
852 * The order of the large prime subgroup of the curve is our choice for
853 * a fixed public size, as that is generally the upper bound for
854 * generating a private key in EC cryptosystems and should fit all valid
855 * secret scalars.
856 *
857 * For preallocating the BIGNUM storage we look at the number of "words"
858 * required for the internal representation of the order, and we
859 * preallocate 2 extra "words" in case any of the subsequent processing
860 * might temporarily overflow the order length.
861 */
862 tmp_key = BN_dup(priv_key);
863 if (tmp_key == NULL)
864 return 0;
865
866 BN_set_flags(tmp_key, BN_FLG_CONSTTIME);
867
868 fixed_top = bn_get_top(order) + 2;
869 if (bn_wexpand(tmp_key, fixed_top) == NULL) {
870 BN_clear_free(tmp_key);
871 return 0;
872 }
873
874 BN_clear_free(key->priv_key);
875 key->priv_key = tmp_key;
876 key->dirty_cnt++;
877
878 return 1;
879 }
880
EC_KEY_get0_public_key(const EC_KEY * key)881 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
882 {
883 return key->pub_key;
884 }
885
EC_KEY_set_public_key(EC_KEY * key,const EC_POINT * pub_key)886 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
887 {
888 if (key->meth->set_public != NULL
889 && key->meth->set_public(key, pub_key) == 0)
890 return 0;
891 EC_POINT_free(key->pub_key);
892 key->pub_key = EC_POINT_dup(pub_key, key->group);
893 key->dirty_cnt++;
894 return (key->pub_key == NULL) ? 0 : 1;
895 }
896
EC_KEY_get_enc_flags(const EC_KEY * key)897 unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
898 {
899 return key->enc_flag;
900 }
901
EC_KEY_set_enc_flags(EC_KEY * key,unsigned int flags)902 void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
903 {
904 key->enc_flag = flags;
905 }
906
EC_KEY_get_conv_form(const EC_KEY * key)907 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
908 {
909 return key->conv_form;
910 }
911
EC_KEY_set_conv_form(EC_KEY * key,point_conversion_form_t cform)912 void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
913 {
914 key->conv_form = cform;
915 if (key->group != NULL)
916 EC_GROUP_set_point_conversion_form(key->group, cform);
917 }
918
EC_KEY_set_asn1_flag(EC_KEY * key,int flag)919 void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
920 {
921 if (key->group != NULL)
922 EC_GROUP_set_asn1_flag(key->group, flag);
923 }
924
925 #ifndef OPENSSL_NO_DEPRECATED_3_0
EC_KEY_precompute_mult(EC_KEY * key,BN_CTX * ctx)926 int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
927 {
928 if (key->group == NULL)
929 return 0;
930 return EC_GROUP_precompute_mult(key->group, ctx);
931 }
932 #endif
933
EC_KEY_get_flags(const EC_KEY * key)934 int EC_KEY_get_flags(const EC_KEY *key)
935 {
936 return key->flags;
937 }
938
EC_KEY_set_flags(EC_KEY * key,int flags)939 void EC_KEY_set_flags(EC_KEY *key, int flags)
940 {
941 key->flags |= flags;
942 key->dirty_cnt++;
943 }
944
EC_KEY_clear_flags(EC_KEY * key,int flags)945 void EC_KEY_clear_flags(EC_KEY *key, int flags)
946 {
947 key->flags &= ~flags;
948 key->dirty_cnt++;
949 }
950
EC_KEY_decoded_from_explicit_params(const EC_KEY * key)951 int EC_KEY_decoded_from_explicit_params(const EC_KEY *key)
952 {
953 if (key == NULL || key->group == NULL)
954 return -1;
955 return key->group->decoded_from_explicit_params;
956 }
957
EC_KEY_key2buf(const EC_KEY * key,point_conversion_form_t form,unsigned char ** pbuf,BN_CTX * ctx)958 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
959 unsigned char **pbuf, BN_CTX *ctx)
960 {
961 if (key == NULL || key->pub_key == NULL || key->group == NULL)
962 return 0;
963 return EC_POINT_point2buf(key->group, key->pub_key, form, pbuf, ctx);
964 }
965
EC_KEY_oct2key(EC_KEY * key,const unsigned char * buf,size_t len,BN_CTX * ctx)966 int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
967 BN_CTX *ctx)
968 {
969 if (key == NULL || key->group == NULL)
970 return 0;
971 if (key->pub_key == NULL)
972 key->pub_key = EC_POINT_new(key->group);
973 if (key->pub_key == NULL)
974 return 0;
975 if (EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx) == 0)
976 return 0;
977 key->dirty_cnt++;
978 /*
979 * Save the point conversion form.
980 * For non-custom curves the first octet of the buffer (excluding
981 * the last significant bit) contains the point conversion form.
982 * EC_POINT_oct2point() has already performed sanity checking of
983 * the buffer so we know it is valid.
984 */
985 if ((key->group->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0)
986 key->conv_form = (point_conversion_form_t)(buf[0] & ~0x01);
987 return 1;
988 }
989
EC_KEY_priv2oct(const EC_KEY * eckey,unsigned char * buf,size_t len)990 size_t EC_KEY_priv2oct(const EC_KEY *eckey,
991 unsigned char *buf, size_t len)
992 {
993 if (eckey->group == NULL || eckey->group->meth == NULL)
994 return 0;
995 if (eckey->group->meth->priv2oct == NULL) {
996 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
997 return 0;
998 }
999
1000 return eckey->group->meth->priv2oct(eckey, buf, len);
1001 }
1002
ossl_ec_key_simple_priv2oct(const EC_KEY * eckey,unsigned char * buf,size_t len)1003 size_t ossl_ec_key_simple_priv2oct(const EC_KEY *eckey,
1004 unsigned char *buf, size_t len)
1005 {
1006 size_t buf_len;
1007
1008 buf_len = (EC_GROUP_order_bits(eckey->group) + 7) / 8;
1009 if (eckey->priv_key == NULL)
1010 return 0;
1011 if (buf == NULL)
1012 return buf_len;
1013 else if (len < buf_len)
1014 return 0;
1015
1016 /* Octetstring may need leading zeros if BN is to short */
1017
1018 if (BN_bn2binpad(eckey->priv_key, buf, buf_len) == -1) {
1019 ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
1020 return 0;
1021 }
1022
1023 return buf_len;
1024 }
1025
EC_KEY_oct2priv(EC_KEY * eckey,const unsigned char * buf,size_t len)1026 int EC_KEY_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
1027 {
1028 int ret;
1029
1030 if (eckey->group == NULL || eckey->group->meth == NULL)
1031 return 0;
1032 if (eckey->group->meth->oct2priv == NULL) {
1033 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1034 return 0;
1035 }
1036 ret = eckey->group->meth->oct2priv(eckey, buf, len);
1037 if (ret == 1)
1038 eckey->dirty_cnt++;
1039 return ret;
1040 }
1041
ossl_ec_key_simple_oct2priv(EC_KEY * eckey,const unsigned char * buf,size_t len)1042 int ossl_ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf,
1043 size_t len)
1044 {
1045 if (eckey->priv_key == NULL)
1046 eckey->priv_key = BN_secure_new();
1047 if (eckey->priv_key == NULL) {
1048 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
1049 return 0;
1050 }
1051 if (BN_bin2bn(buf, len, eckey->priv_key) == NULL) {
1052 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
1053 return 0;
1054 }
1055 eckey->dirty_cnt++;
1056 return 1;
1057 }
1058
EC_KEY_priv2buf(const EC_KEY * eckey,unsigned char ** pbuf)1059 size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf)
1060 {
1061 size_t len;
1062 unsigned char *buf;
1063
1064 len = EC_KEY_priv2oct(eckey, NULL, 0);
1065 if (len == 0)
1066 return 0;
1067 if ((buf = OPENSSL_malloc(len)) == NULL)
1068 return 0;
1069 len = EC_KEY_priv2oct(eckey, buf, len);
1070 if (len == 0) {
1071 OPENSSL_free(buf);
1072 return 0;
1073 }
1074 *pbuf = buf;
1075 return len;
1076 }
1077
EC_KEY_can_sign(const EC_KEY * eckey)1078 int EC_KEY_can_sign(const EC_KEY *eckey)
1079 {
1080 if (eckey->group == NULL || eckey->group->meth == NULL
1081 || (eckey->group->meth->flags & EC_FLAGS_NO_SIGN))
1082 return 0;
1083 return 1;
1084 }
1085
1086 /*
1087 * FIPS 140-2 IG 9.9 AS09.33
1088 * Perform a sign/verify operation.
1089 *
1090 * NOTE: When generating keys for key-agreement schemes - FIPS 140-2 IG 9.9
1091 * states that no additional pairwise tests are required (apart from the tests
1092 * specified in SP800-56A) when generating keys. Hence pairwise ECDH tests are
1093 * omitted here.
1094 */
ecdsa_keygen_pairwise_test(EC_KEY * eckey,OSSL_CALLBACK * cb,void * cbarg)1095 static int ecdsa_keygen_pairwise_test(EC_KEY *eckey, OSSL_CALLBACK *cb,
1096 void *cbarg)
1097 {
1098 int ret = 0;
1099 unsigned char dgst[16] = { 0 };
1100 int dgst_len = (int)sizeof(dgst);
1101 ECDSA_SIG *sig = NULL;
1102 OSSL_SELF_TEST *st = NULL;
1103
1104 st = OSSL_SELF_TEST_new(cb, cbarg);
1105 if (st == NULL)
1106 return 0;
1107
1108 OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT,
1109 OSSL_SELF_TEST_DESC_PCT_ECDSA);
1110
1111 sig = ECDSA_do_sign(dgst, dgst_len, eckey);
1112 if (sig == NULL)
1113 goto err;
1114
1115 OSSL_SELF_TEST_oncorrupt_byte(st, dgst);
1116
1117 if (ECDSA_do_verify(dgst, dgst_len, sig, eckey) != 1)
1118 goto err;
1119
1120 ret = 1;
1121 err:
1122 OSSL_SELF_TEST_onend(st, ret);
1123 OSSL_SELF_TEST_free(st);
1124 ECDSA_SIG_free(sig);
1125 return ret;
1126 }
1127