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