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