/src/libressl/crypto/ecdsa/ecs_ossl.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: ecs_ossl.c,v 1.25 2022/06/30 11:14:47 tb Exp $ */ |
2 | | /* |
3 | | * Written by Nils Larsch for the OpenSSL project |
4 | | */ |
5 | | /* ==================================================================== |
6 | | * Copyright (c) 1998-2004 The OpenSSL Project. All rights reserved. |
7 | | * |
8 | | * Redistribution and use in source and binary forms, with or without |
9 | | * modification, are permitted provided that the following conditions |
10 | | * are met: |
11 | | * |
12 | | * 1. Redistributions of source code must retain the above copyright |
13 | | * notice, this list of conditions and the following disclaimer. |
14 | | * |
15 | | * 2. Redistributions in binary form must reproduce the above copyright |
16 | | * notice, this list of conditions and the following disclaimer in |
17 | | * the documentation and/or other materials provided with the |
18 | | * distribution. |
19 | | * |
20 | | * 3. All advertising materials mentioning features or use of this |
21 | | * software must display the following acknowledgment: |
22 | | * "This product includes software developed by the OpenSSL Project |
23 | | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
24 | | * |
25 | | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
26 | | * endorse or promote products derived from this software without |
27 | | * prior written permission. For written permission, please contact |
28 | | * openssl-core@OpenSSL.org. |
29 | | * |
30 | | * 5. Products derived from this software may not be called "OpenSSL" |
31 | | * nor may "OpenSSL" appear in their names without prior written |
32 | | * permission of the OpenSSL Project. |
33 | | * |
34 | | * 6. Redistributions of any form whatsoever must retain the following |
35 | | * acknowledgment: |
36 | | * "This product includes software developed by the OpenSSL Project |
37 | | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
38 | | * |
39 | | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
40 | | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
41 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
42 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
43 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
44 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
45 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
46 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
47 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
48 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
49 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
50 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
51 | | * ==================================================================== |
52 | | * |
53 | | * This product includes cryptographic software written by Eric Young |
54 | | * (eay@cryptsoft.com). This product includes software written by Tim |
55 | | * Hudson (tjh@cryptsoft.com). |
56 | | * |
57 | | */ |
58 | | |
59 | | #include <string.h> |
60 | | |
61 | | #include <openssl/opensslconf.h> |
62 | | |
63 | | #include <openssl/bn.h> |
64 | | #include <openssl/err.h> |
65 | | #include <openssl/objects.h> |
66 | | |
67 | | #include "bn_lcl.h" |
68 | | #include "ecs_locl.h" |
69 | | |
70 | | static int ecdsa_prepare_digest(const unsigned char *dgst, int dgst_len, |
71 | | BIGNUM *order, BIGNUM *ret); |
72 | | static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len, |
73 | | const BIGNUM *, const BIGNUM *, EC_KEY *eckey); |
74 | | static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, |
75 | | BIGNUM **rp); |
76 | | static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len, |
77 | | const ECDSA_SIG *sig, EC_KEY *eckey); |
78 | | |
79 | | static ECDSA_METHOD openssl_ecdsa_meth = { |
80 | | .name = "OpenSSL ECDSA method", |
81 | | .ecdsa_do_sign = ecdsa_do_sign, |
82 | | .ecdsa_sign_setup = ecdsa_sign_setup, |
83 | | .ecdsa_do_verify = ecdsa_do_verify |
84 | | }; |
85 | | |
86 | | const ECDSA_METHOD * |
87 | | ECDSA_OpenSSL(void) |
88 | 3 | { |
89 | 3 | return &openssl_ecdsa_meth; |
90 | 3 | } |
91 | | |
92 | | static int |
93 | | ecdsa_prepare_digest(const unsigned char *dgst, int dgst_len, BIGNUM *order, |
94 | | BIGNUM *ret) |
95 | 1.39k | { |
96 | 1.39k | int dgst_bits, order_bits; |
97 | | |
98 | 1.39k | if (!BN_bin2bn(dgst, dgst_len, ret)) { |
99 | 0 | ECDSAerror(ERR_R_BN_LIB); |
100 | 0 | return 0; |
101 | 0 | } |
102 | | |
103 | | /* FIPS 186-3 6.4: Use order_bits leftmost bits if digest is too long */ |
104 | 1.39k | dgst_bits = 8 * dgst_len; |
105 | 1.39k | order_bits = BN_num_bits(order); |
106 | 1.39k | if (dgst_bits > order_bits) { |
107 | 147 | if (!BN_rshift(ret, ret, dgst_bits - order_bits)) { |
108 | 0 | ECDSAerror(ERR_R_BN_LIB); |
109 | 0 | return 0; |
110 | 0 | } |
111 | 147 | } |
112 | | |
113 | 1.39k | return 1; |
114 | 1.39k | } |
115 | | |
116 | | int |
117 | | ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen, unsigned char *sig, |
118 | | unsigned int *siglen, const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey) |
119 | 441 | { |
120 | 441 | ECDSA_SIG *s; |
121 | | |
122 | 441 | if ((s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey)) == NULL) { |
123 | 0 | *siglen = 0; |
124 | 0 | return 0; |
125 | 0 | } |
126 | 441 | *siglen = i2d_ECDSA_SIG(s, &sig); |
127 | 441 | ECDSA_SIG_free(s); |
128 | 441 | return 1; |
129 | 441 | } |
130 | | |
131 | | static int |
132 | | ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) |
133 | 833 | { |
134 | 833 | BN_CTX *ctx = ctx_in; |
135 | 833 | BIGNUM *k = NULL, *r = NULL, *order = NULL, *X = NULL; |
136 | 833 | EC_POINT *point = NULL; |
137 | 833 | const EC_GROUP *group; |
138 | 833 | int order_bits, ret = 0; |
139 | | |
140 | 833 | if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) { |
141 | 0 | ECDSAerror(ERR_R_PASSED_NULL_PARAMETER); |
142 | 0 | return 0; |
143 | 0 | } |
144 | | |
145 | 833 | if (ctx == NULL) { |
146 | 0 | if ((ctx = BN_CTX_new()) == NULL) { |
147 | 0 | ECDSAerror(ERR_R_MALLOC_FAILURE); |
148 | 0 | return 0; |
149 | 0 | } |
150 | 0 | } |
151 | | |
152 | 833 | if ((k = BN_new()) == NULL || (r = BN_new()) == NULL || |
153 | 833 | (order = BN_new()) == NULL || (X = BN_new()) == NULL) { |
154 | 0 | ECDSAerror(ERR_R_MALLOC_FAILURE); |
155 | 0 | goto err; |
156 | 0 | } |
157 | 833 | if ((point = EC_POINT_new(group)) == NULL) { |
158 | 0 | ECDSAerror(ERR_R_EC_LIB); |
159 | 0 | goto err; |
160 | 0 | } |
161 | 833 | if (!EC_GROUP_get_order(group, order, ctx)) { |
162 | 0 | ECDSAerror(ERR_R_EC_LIB); |
163 | 0 | goto err; |
164 | 0 | } |
165 | | |
166 | 833 | if (BN_cmp(order, BN_value_one()) <= 0) { |
167 | 0 | ECDSAerror(EC_R_INVALID_GROUP_ORDER); |
168 | 0 | goto err; |
169 | 0 | } |
170 | | |
171 | | /* Preallocate space. */ |
172 | 833 | order_bits = BN_num_bits(order); |
173 | 833 | if (!BN_set_bit(k, order_bits) || |
174 | 833 | !BN_set_bit(r, order_bits) || |
175 | 833 | !BN_set_bit(X, order_bits)) |
176 | 0 | goto err; |
177 | | |
178 | 833 | do { |
179 | 833 | do { |
180 | 833 | if (!BN_rand_range(k, order)) { |
181 | 0 | ECDSAerror( |
182 | 0 | ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED); |
183 | 0 | goto err; |
184 | 0 | } |
185 | 833 | } while (BN_is_zero(k)); |
186 | | |
187 | | /* |
188 | | * We do not want timing information to leak the length of k, |
189 | | * so we compute G * k using an equivalent scalar of fixed |
190 | | * bit-length. |
191 | | * |
192 | | * We unconditionally perform both of these additions to prevent |
193 | | * a small timing information leakage. We then choose the sum |
194 | | * that is one bit longer than the order. This guarantees the |
195 | | * code path used in the constant time implementations |
196 | | * elsewhere. |
197 | | * |
198 | | * TODO: revisit the BN_copy aiming for a memory access agnostic |
199 | | * conditional copy. |
200 | | */ |
201 | 833 | if (!BN_add(r, k, order) || |
202 | 833 | !BN_add(X, r, order) || |
203 | 833 | !BN_copy(k, BN_num_bits(r) > order_bits ? r : X)) |
204 | 0 | goto err; |
205 | | |
206 | 833 | BN_set_flags(k, BN_FLG_CONSTTIME); |
207 | | |
208 | | /* Compute r, the x-coordinate of G * k. */ |
209 | 833 | if (!EC_POINT_mul(group, point, k, NULL, NULL, ctx)) { |
210 | 0 | ECDSAerror(ERR_R_EC_LIB); |
211 | 0 | goto err; |
212 | 0 | } |
213 | 833 | if (!EC_POINT_get_affine_coordinates(group, point, X, NULL, |
214 | 833 | ctx)) { |
215 | 0 | ECDSAerror(ERR_R_EC_LIB); |
216 | 0 | goto err; |
217 | 0 | } |
218 | 833 | if (!BN_nnmod(r, X, order, ctx)) { |
219 | 0 | ECDSAerror(ERR_R_BN_LIB); |
220 | 0 | goto err; |
221 | 0 | } |
222 | 833 | } while (BN_is_zero(r)); |
223 | | |
224 | 833 | if (BN_mod_inverse_ct(k, k, order, ctx) == NULL) { |
225 | 12 | ECDSAerror(ERR_R_BN_LIB); |
226 | 12 | goto err; |
227 | 12 | } |
228 | 821 | BN_clear_free(*rp); |
229 | 821 | BN_clear_free(*kinvp); |
230 | 821 | *rp = r; |
231 | 821 | *kinvp = k; |
232 | 821 | ret = 1; |
233 | | |
234 | 833 | err: |
235 | 833 | if (ret == 0) { |
236 | 12 | BN_clear_free(k); |
237 | 12 | BN_clear_free(r); |
238 | 12 | } |
239 | 833 | if (ctx_in == NULL) |
240 | 0 | BN_CTX_free(ctx); |
241 | 833 | BN_free(order); |
242 | 833 | EC_POINT_free(point); |
243 | 833 | BN_clear_free(X); |
244 | 833 | return (ret); |
245 | 821 | } |
246 | | |
247 | | /* replace w/ ecdsa_sign_setup() when ECDSA_METHOD gets removed */ |
248 | | int |
249 | | ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) |
250 | 833 | { |
251 | 833 | ECDSA_DATA *ecdsa; |
252 | | |
253 | 833 | if ((ecdsa = ecdsa_check(eckey)) == NULL) |
254 | 0 | return 0; |
255 | 833 | return ecdsa->meth->ecdsa_sign_setup(eckey, ctx_in, kinvp, rp); |
256 | 833 | } |
257 | | |
258 | | static ECDSA_SIG * |
259 | | ecdsa_do_sign(const unsigned char *dgst, int dgst_len, |
260 | | const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *eckey) |
261 | 833 | { |
262 | 833 | BIGNUM *b = NULL, *binv = NULL, *bm = NULL, *bxr = NULL; |
263 | 833 | BIGNUM *kinv = NULL, *m = NULL, *order = NULL, *range = NULL, *s; |
264 | 833 | const BIGNUM *ckinv, *priv_key; |
265 | 833 | BN_CTX *ctx = NULL; |
266 | 833 | const EC_GROUP *group; |
267 | 833 | ECDSA_SIG *ret; |
268 | 833 | ECDSA_DATA *ecdsa; |
269 | 833 | int ok = 0; |
270 | | |
271 | 833 | ecdsa = ecdsa_check(eckey); |
272 | 833 | group = EC_KEY_get0_group(eckey); |
273 | 833 | priv_key = EC_KEY_get0_private_key(eckey); |
274 | | |
275 | 833 | if (group == NULL || priv_key == NULL || ecdsa == NULL) { |
276 | 0 | ECDSAerror(ERR_R_PASSED_NULL_PARAMETER); |
277 | 0 | return NULL; |
278 | 0 | } |
279 | | |
280 | 833 | if ((ret = ECDSA_SIG_new()) == NULL) { |
281 | 0 | ECDSAerror(ERR_R_MALLOC_FAILURE); |
282 | 0 | return NULL; |
283 | 0 | } |
284 | 833 | s = ret->s; |
285 | | |
286 | 833 | if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL || |
287 | 833 | (range = BN_new()) == NULL || (b = BN_new()) == NULL || |
288 | 833 | (binv = BN_new()) == NULL || (bm = BN_new()) == NULL || |
289 | 833 | (bxr = BN_new()) == NULL || (m = BN_new()) == NULL) { |
290 | 0 | ECDSAerror(ERR_R_MALLOC_FAILURE); |
291 | 0 | goto err; |
292 | 0 | } |
293 | | |
294 | 833 | if (!EC_GROUP_get_order(group, order, ctx)) { |
295 | 0 | ECDSAerror(ERR_R_EC_LIB); |
296 | 0 | goto err; |
297 | 0 | } |
298 | | |
299 | 833 | if (!ecdsa_prepare_digest(dgst, dgst_len, order, m)) |
300 | 0 | goto err; |
301 | | |
302 | 833 | do { |
303 | 833 | if (in_kinv == NULL || in_r == NULL) { |
304 | 833 | if (!ECDSA_sign_setup(eckey, ctx, &kinv, &ret->r)) { |
305 | 12 | ECDSAerror(ERR_R_ECDSA_LIB); |
306 | 12 | goto err; |
307 | 12 | } |
308 | 821 | ckinv = kinv; |
309 | 821 | } else { |
310 | 0 | ckinv = in_kinv; |
311 | 0 | if (BN_copy(ret->r, in_r) == NULL) { |
312 | 0 | ECDSAerror(ERR_R_MALLOC_FAILURE); |
313 | 0 | goto err; |
314 | 0 | } |
315 | 0 | } |
316 | | |
317 | | /* |
318 | | * Compute: |
319 | | * |
320 | | * s = inv(k)(m + xr) mod order |
321 | | * |
322 | | * In order to reduce the possibility of a side-channel attack, |
323 | | * the following is calculated using a blinding value: |
324 | | * |
325 | | * s = inv(b)(bm + bxr)inv(k) mod order |
326 | | * |
327 | | * where b is a random value in the range [1, order-1]. |
328 | | */ |
329 | | |
330 | | /* Generate b in range [1, order-1]. */ |
331 | 821 | if (!BN_sub(range, order, BN_value_one())) { |
332 | 0 | ECDSAerror(ERR_R_BN_LIB); |
333 | 0 | goto err; |
334 | 0 | } |
335 | 821 | if (!BN_rand_range(b, range)) { |
336 | 0 | ECDSAerror(ERR_R_BN_LIB); |
337 | 0 | goto err; |
338 | 0 | } |
339 | 821 | if (!BN_add(b, b, BN_value_one())) { |
340 | 0 | ECDSAerror(ERR_R_BN_LIB); |
341 | 0 | goto err; |
342 | 0 | } |
343 | | |
344 | 821 | if (BN_mod_inverse_ct(binv, b, order, ctx) == NULL) { |
345 | 1 | ECDSAerror(ERR_R_BN_LIB); |
346 | 1 | goto err; |
347 | 1 | } |
348 | | |
349 | 820 | if (!BN_mod_mul(bxr, b, priv_key, order, ctx)) { /* bx */ |
350 | 0 | ECDSAerror(ERR_R_BN_LIB); |
351 | 0 | goto err; |
352 | 0 | } |
353 | 820 | if (!BN_mod_mul(bxr, bxr, ret->r, order, ctx)) { /* bxr */ |
354 | 0 | ECDSAerror(ERR_R_BN_LIB); |
355 | 0 | goto err; |
356 | 0 | } |
357 | 820 | if (!BN_mod_mul(bm, b, m, order, ctx)) { /* bm */ |
358 | 0 | ECDSAerror(ERR_R_BN_LIB); |
359 | 0 | goto err; |
360 | 0 | } |
361 | 820 | if (!BN_mod_add(s, bm, bxr, order, ctx)) { /* s = bm + bxr */ |
362 | 0 | ECDSAerror(ERR_R_BN_LIB); |
363 | 0 | goto err; |
364 | 0 | } |
365 | 820 | if (!BN_mod_mul(s, s, ckinv, order, ctx)) { /* s = b(m + xr)k^-1 */ |
366 | 0 | ECDSAerror(ERR_R_BN_LIB); |
367 | 0 | goto err; |
368 | 0 | } |
369 | 820 | if (!BN_mod_mul(s, s, binv, order, ctx)) { /* s = (m + xr)k^-1 */ |
370 | 0 | ECDSAerror(ERR_R_BN_LIB); |
371 | 0 | goto err; |
372 | 0 | } |
373 | | |
374 | 820 | if (BN_is_zero(s)) { |
375 | | /* |
376 | | * If kinv and r have been supplied by the caller, |
377 | | * don't generate new kinv and r values |
378 | | */ |
379 | 0 | if (in_kinv != NULL && in_r != NULL) { |
380 | 0 | ECDSAerror(ECDSA_R_NEED_NEW_SETUP_VALUES); |
381 | 0 | goto err; |
382 | 0 | } |
383 | 0 | } else |
384 | | /* s != 0 => we have a valid signature */ |
385 | 820 | break; |
386 | 820 | } while (1); |
387 | | |
388 | 820 | ok = 1; |
389 | | |
390 | 833 | err: |
391 | 833 | if (ok == 0) { |
392 | 13 | ECDSA_SIG_free(ret); |
393 | 13 | ret = NULL; |
394 | 13 | } |
395 | 833 | BN_CTX_free(ctx); |
396 | 833 | BN_clear_free(b); |
397 | 833 | BN_clear_free(binv); |
398 | 833 | BN_clear_free(bm); |
399 | 833 | BN_clear_free(bxr); |
400 | 833 | BN_clear_free(kinv); |
401 | 833 | BN_clear_free(m); |
402 | 833 | BN_free(order); |
403 | 833 | BN_free(range); |
404 | 833 | return ret; |
405 | 820 | } |
406 | | |
407 | | /* replace w/ ecdsa_do_sign() when ECDSA_METHOD gets removed */ |
408 | | ECDSA_SIG * |
409 | | ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len, |
410 | | const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *eckey) |
411 | 833 | { |
412 | 833 | ECDSA_DATA *ecdsa; |
413 | | |
414 | 833 | if ((ecdsa = ecdsa_check(eckey)) == NULL) |
415 | 0 | return NULL; |
416 | 833 | return ecdsa->meth->ecdsa_do_sign(dgst, dgst_len, in_kinv, in_r, eckey); |
417 | 833 | } |
418 | | |
419 | | int |
420 | | ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len, |
421 | | const unsigned char *sigbuf, int sig_len, EC_KEY *eckey) |
422 | 165 | { |
423 | 165 | ECDSA_SIG *s; |
424 | 165 | unsigned char *der = NULL; |
425 | 165 | const unsigned char *p = sigbuf; |
426 | 165 | int derlen = -1; |
427 | 165 | int ret = -1; |
428 | | |
429 | 165 | if ((s = ECDSA_SIG_new()) == NULL) |
430 | 0 | return (ret); |
431 | 165 | if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) |
432 | 20 | goto err; |
433 | | /* Ensure signature uses DER and doesn't have trailing garbage */ |
434 | 145 | derlen = i2d_ECDSA_SIG(s, &der); |
435 | 145 | if (derlen != sig_len || memcmp(sigbuf, der, derlen)) |
436 | 1 | goto err; |
437 | 144 | ret = ECDSA_do_verify(dgst, dgst_len, s, eckey); |
438 | | |
439 | 165 | err: |
440 | 165 | freezero(der, derlen); |
441 | 165 | ECDSA_SIG_free(s); |
442 | 165 | return (ret); |
443 | 144 | } |
444 | | |
445 | | static int |
446 | | ecdsa_do_verify(const unsigned char *dgst, int dgst_len, const ECDSA_SIG *sig, |
447 | | EC_KEY *eckey) |
448 | 607 | { |
449 | 607 | BN_CTX *ctx; |
450 | 607 | BIGNUM *order, *u1, *u2, *m, *X; |
451 | 607 | EC_POINT *point = NULL; |
452 | 607 | const EC_GROUP *group; |
453 | 607 | const EC_POINT *pub_key; |
454 | 607 | int ret = -1; |
455 | | |
456 | 607 | if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL || |
457 | 607 | (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) { |
458 | 0 | ECDSAerror(ECDSA_R_MISSING_PARAMETERS); |
459 | 0 | return -1; |
460 | 0 | } |
461 | | |
462 | 607 | if ((ctx = BN_CTX_new()) == NULL) { |
463 | 0 | ECDSAerror(ERR_R_MALLOC_FAILURE); |
464 | 0 | return -1; |
465 | 0 | } |
466 | 607 | BN_CTX_start(ctx); |
467 | 607 | order = BN_CTX_get(ctx); |
468 | 607 | u1 = BN_CTX_get(ctx); |
469 | 607 | u2 = BN_CTX_get(ctx); |
470 | 607 | m = BN_CTX_get(ctx); |
471 | 607 | X = BN_CTX_get(ctx); |
472 | 607 | if (X == NULL) { |
473 | 0 | ECDSAerror(ERR_R_BN_LIB); |
474 | 0 | goto err; |
475 | 0 | } |
476 | | |
477 | 607 | if (!EC_GROUP_get_order(group, order, ctx)) { |
478 | 0 | ECDSAerror(ERR_R_EC_LIB); |
479 | 0 | goto err; |
480 | 0 | } |
481 | | |
482 | | /* Verify that r and s are in the range [1, order-1]. */ |
483 | 607 | if (BN_is_zero(sig->r) || BN_is_negative(sig->r) || |
484 | 607 | BN_ucmp(sig->r, order) >= 0 || |
485 | 607 | BN_is_zero(sig->s) || BN_is_negative(sig->s) || |
486 | 607 | BN_ucmp(sig->s, order) >= 0) { |
487 | 43 | ECDSAerror(ECDSA_R_BAD_SIGNATURE); |
488 | 43 | ret = 0; |
489 | 43 | goto err; |
490 | 43 | } |
491 | | |
492 | 564 | if (!ecdsa_prepare_digest(dgst, dgst_len, order, m)) |
493 | 0 | goto err; |
494 | | |
495 | 564 | if (BN_mod_inverse_ct(u2, sig->s, order, ctx) == NULL) { /* w = inv(s) */ |
496 | 1 | ECDSAerror(ERR_R_BN_LIB); |
497 | 1 | goto err; |
498 | 1 | } |
499 | 563 | if (!BN_mod_mul(u1, m, u2, order, ctx)) { /* u1 = mw */ |
500 | 0 | ECDSAerror(ERR_R_BN_LIB); |
501 | 0 | goto err; |
502 | 0 | } |
503 | 563 | if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) { /* u2 = rw */ |
504 | 0 | ECDSAerror(ERR_R_BN_LIB); |
505 | 0 | goto err; |
506 | 0 | } |
507 | | |
508 | | /* Compute the x-coordinate of G * u1 + pub_key * u2. */ |
509 | 563 | if ((point = EC_POINT_new(group)) == NULL) { |
510 | 0 | ECDSAerror(ERR_R_MALLOC_FAILURE); |
511 | 0 | goto err; |
512 | 0 | } |
513 | 563 | if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) { |
514 | 0 | ECDSAerror(ERR_R_EC_LIB); |
515 | 0 | goto err; |
516 | 0 | } |
517 | 563 | if (!EC_POINT_get_affine_coordinates(group, point, X, NULL, ctx)) { |
518 | 1 | ECDSAerror(ERR_R_EC_LIB); |
519 | 1 | goto err; |
520 | 1 | } |
521 | 562 | if (!BN_nnmod(u1, X, order, ctx)) { |
522 | 0 | ECDSAerror(ERR_R_BN_LIB); |
523 | 0 | goto err; |
524 | 0 | } |
525 | | |
526 | | /* If the signature is correct, the x-coordinate is equal to sig->r. */ |
527 | 562 | ret = (BN_ucmp(u1, sig->r) == 0); |
528 | | |
529 | 607 | err: |
530 | 607 | BN_CTX_end(ctx); |
531 | 607 | BN_CTX_free(ctx); |
532 | 607 | EC_POINT_free(point); |
533 | 607 | return ret; |
534 | 562 | } |
535 | | |
536 | | /* replace w/ ecdsa_do_verify() when ECDSA_METHOD gets removed */ |
537 | | int |
538 | | ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len, |
539 | | const ECDSA_SIG *sig, EC_KEY *eckey) |
540 | 607 | { |
541 | 607 | ECDSA_DATA *ecdsa; |
542 | | |
543 | 607 | if ((ecdsa = ecdsa_check(eckey)) == NULL) |
544 | 0 | return 0; |
545 | 607 | return ecdsa->meth->ecdsa_do_verify(dgst, dgst_len, sig, eckey); |
546 | 607 | } |