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