]>
Commit | Line | Data |
---|---|---|
c6700d27 GT |
1 | /* |
2 | * Written by Nils Larsch for the OpenSSL project | |
3 | */ | |
4d94ae00 | 4 | /* ==================================================================== |
c6700d27 | 5 | * Copyright (c) 1998-2004 The OpenSSL Project. All rights reserved. |
4d94ae00 BM |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * | |
11 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 12 | * notice, this list of conditions and the following disclaimer. |
4d94ae00 BM |
13 | * |
14 | * 2. Redistributions in binary form must reproduce the above copyright | |
15 | * notice, this list of conditions and the following disclaimer in | |
16 | * the documentation and/or other materials provided with the | |
17 | * distribution. | |
18 | * | |
19 | * 3. All advertising materials mentioning features or use of this | |
20 | * software must display the following acknowledgment: | |
21 | * "This product includes software developed by the OpenSSL Project | |
22 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
23 | * | |
24 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
25 | * endorse or promote products derived from this software without | |
26 | * prior written permission. For written permission, please contact | |
27 | * openssl-core@OpenSSL.org. | |
28 | * | |
29 | * 5. Products derived from this software may not be called "OpenSSL" | |
30 | * nor may "OpenSSL" appear in their names without prior written | |
31 | * permission of the OpenSSL Project. | |
32 | * | |
33 | * 6. Redistributions of any form whatsoever must retain the following | |
34 | * acknowledgment: | |
35 | * "This product includes software developed by the OpenSSL Project | |
36 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
37 | * | |
38 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
39 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
40 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
41 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
42 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
43 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
44 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
45 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
46 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
47 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
48 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
49 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
50 | * ==================================================================== | |
51 | * | |
52 | * This product includes cryptographic software written by Eric Young | |
53 | * (eay@cryptsoft.com). This product includes software written by Tim | |
54 | * Hudson (tjh@cryptsoft.com). | |
55 | * | |
56 | */ | |
0bee0e62 | 57 | |
a200a817 | 58 | #include <string.h> |
0bee0e62 | 59 | #include <openssl/err.h> |
14a7cfb3 | 60 | #include <openssl/obj_mac.h> |
0f814687 | 61 | #include <openssl/bn.h> |
8a99cb29 | 62 | #include <openssl/rand.h> |
6a47db45 DSH |
63 | #include <openssl/ec.h> |
64 | #include "ec_lcl.h" | |
190c615d | 65 | |
a200a817 DSH |
66 | int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen, |
67 | unsigned char *sig, unsigned int *siglen, | |
68 | const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey) | |
69 | { | |
70 | ECDSA_SIG *s; | |
71 | RAND_seed(dgst, dlen); | |
72 | s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey); | |
73 | if (s == NULL) { | |
74 | *siglen = 0; | |
75 | return 0; | |
76 | } | |
77 | *siglen = i2d_ECDSA_SIG(s, &sig); | |
78 | ECDSA_SIG_free(s); | |
79 | return 1; | |
80 | } | |
81 | ||
8d6a75dc | 82 | static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, |
0f113f3e MC |
83 | BIGNUM **kinvp, BIGNUM **rp, |
84 | const unsigned char *dgst, int dlen) | |
4d94ae00 | 85 | { |
0f113f3e MC |
86 | BN_CTX *ctx = NULL; |
87 | BIGNUM *k = NULL, *r = NULL, *order = NULL, *X = NULL; | |
88 | EC_POINT *tmp_point = NULL; | |
89 | const EC_GROUP *group; | |
90 | int ret = 0; | |
9dd84053 | 91 | |
0f113f3e | 92 | if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) { |
6a47db45 | 93 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER); |
0f113f3e MC |
94 | return 0; |
95 | } | |
a74333f9 | 96 | |
0f113f3e MC |
97 | if (ctx_in == NULL) { |
98 | if ((ctx = BN_CTX_new()) == NULL) { | |
6a47db45 | 99 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
100 | return 0; |
101 | } | |
102 | } else | |
103 | ctx = ctx_in; | |
4d94ae00 | 104 | |
0f113f3e MC |
105 | k = BN_new(); /* this value is later returned in *kinvp */ |
106 | r = BN_new(); /* this value is later returned in *rp */ | |
107 | order = BN_new(); | |
108 | X = BN_new(); | |
90945fa3 | 109 | if (k == NULL || r == NULL || order == NULL || X == NULL) { |
6a47db45 | 110 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
111 | goto err; |
112 | } | |
113 | if ((tmp_point = EC_POINT_new(group)) == NULL) { | |
6a47db45 | 114 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB); |
0f113f3e MC |
115 | goto err; |
116 | } | |
117 | if (!EC_GROUP_get_order(group, order, ctx)) { | |
6a47db45 | 118 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB); |
0f113f3e MC |
119 | goto err; |
120 | } | |
cac4fb58 | 121 | |
0f113f3e MC |
122 | do { |
123 | /* get random k */ | |
124 | do | |
0f113f3e MC |
125 | if (dgst != NULL) { |
126 | if (!BN_generate_dsa_nonce | |
127 | (k, order, EC_KEY_get0_private_key(eckey), dgst, dlen, | |
128 | ctx)) { | |
6a47db45 DSH |
129 | ECerr(EC_F_ECDSA_SIGN_SETUP, |
130 | EC_R_RANDOM_NUMBER_GENERATION_FAILED); | |
0f113f3e MC |
131 | goto err; |
132 | } | |
474e469b | 133 | } else { |
0f113f3e | 134 | if (!BN_rand_range(k, order)) { |
6a47db45 DSH |
135 | ECerr(EC_F_ECDSA_SIGN_SETUP, |
136 | EC_R_RANDOM_NUMBER_GENERATION_FAILED); | |
0f113f3e MC |
137 | goto err; |
138 | } | |
139 | } | |
140 | while (BN_is_zero(k)); | |
4d94ae00 | 141 | |
0f113f3e MC |
142 | /* |
143 | * We do not want timing information to leak the length of k, so we | |
144 | * compute G*k using an equivalent scalar of fixed bit-length. | |
145 | */ | |
992bdde6 | 146 | |
0f113f3e MC |
147 | if (!BN_add(k, k, order)) |
148 | goto err; | |
149 | if (BN_num_bits(k) <= BN_num_bits(order)) | |
150 | if (!BN_add(k, k, order)) | |
151 | goto err; | |
992bdde6 | 152 | |
0f113f3e MC |
153 | /* compute r the x-coordinate of generator * k */ |
154 | if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) { | |
6a47db45 | 155 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB); |
0f113f3e MC |
156 | goto err; |
157 | } | |
158 | if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == | |
159 | NID_X9_62_prime_field) { | |
160 | if (!EC_POINT_get_affine_coordinates_GFp | |
161 | (group, tmp_point, X, NULL, ctx)) { | |
6a47db45 | 162 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB); |
0f113f3e MC |
163 | goto err; |
164 | } | |
165 | } | |
b3310161 | 166 | #ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
167 | else { /* NID_X9_62_characteristic_two_field */ |
168 | ||
169 | if (!EC_POINT_get_affine_coordinates_GF2m(group, | |
170 | tmp_point, X, NULL, | |
171 | ctx)) { | |
6a47db45 | 172 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB); |
0f113f3e MC |
173 | goto err; |
174 | } | |
175 | } | |
b3310161 | 176 | #endif |
0f113f3e | 177 | if (!BN_nnmod(r, X, order, ctx)) { |
6a47db45 | 178 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); |
0f113f3e MC |
179 | goto err; |
180 | } | |
181 | } | |
182 | while (BN_is_zero(r)); | |
4d94ae00 | 183 | |
0f113f3e MC |
184 | /* compute the inverse of k */ |
185 | if (EC_GROUP_get_mont_data(group) != NULL) { | |
186 | /* | |
187 | * We want inverse in constant time, therefore we utilize the fact | |
188 | * order must be prime and use Fermats Little Theorem instead. | |
189 | */ | |
190 | if (!BN_set_word(X, 2)) { | |
6a47db45 | 191 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); |
0f113f3e MC |
192 | goto err; |
193 | } | |
194 | if (!BN_mod_sub(X, order, X, order, ctx)) { | |
6a47db45 | 195 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); |
0f113f3e MC |
196 | goto err; |
197 | } | |
198 | BN_set_flags(X, BN_FLG_CONSTTIME); | |
199 | if (!BN_mod_exp_mont_consttime | |
200 | (k, k, X, order, ctx, EC_GROUP_get_mont_data(group))) { | |
6a47db45 | 201 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); |
0f113f3e MC |
202 | goto err; |
203 | } | |
204 | } else { | |
205 | if (!BN_mod_inverse(k, k, order, ctx)) { | |
6a47db45 | 206 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); |
0f113f3e MC |
207 | goto err; |
208 | } | |
209 | } | |
f54be179 | 210 | |
0f113f3e | 211 | /* clear old values if necessary */ |
23a1d5e9 RS |
212 | BN_clear_free(*rp); |
213 | BN_clear_free(*kinvp); | |
0f113f3e MC |
214 | /* save the pre-computed values */ |
215 | *rp = r; | |
216 | *kinvp = k; | |
217 | ret = 1; | |
218 | err: | |
219 | if (!ret) { | |
23a1d5e9 RS |
220 | BN_clear_free(k); |
221 | BN_clear_free(r); | |
0f113f3e | 222 | } |
23a1d5e9 | 223 | if (ctx != ctx_in) |
0f113f3e | 224 | BN_CTX_free(ctx); |
23a1d5e9 | 225 | BN_free(order); |
8fdc3734 | 226 | EC_POINT_free(tmp_point); |
23a1d5e9 | 227 | BN_clear_free(X); |
0f113f3e | 228 | return (ret); |
4d94ae00 BM |
229 | } |
230 | ||
6a47db45 DSH |
231 | int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, |
232 | BIGNUM **rp) | |
233 | { | |
234 | return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0); | |
235 | } | |
236 | ||
237 | ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len, | |
238 | const BIGNUM *in_kinv, const BIGNUM *in_r, | |
239 | EC_KEY *eckey) | |
4d94ae00 | 240 | { |
0f113f3e MC |
241 | int ok = 0, i; |
242 | BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *order = NULL; | |
243 | const BIGNUM *ckinv; | |
244 | BN_CTX *ctx = NULL; | |
245 | const EC_GROUP *group; | |
246 | ECDSA_SIG *ret; | |
0f113f3e MC |
247 | const BIGNUM *priv_key; |
248 | ||
0f113f3e MC |
249 | group = EC_KEY_get0_group(eckey); |
250 | priv_key = EC_KEY_get0_private_key(eckey); | |
14a7cfb3 | 251 | |
6a47db45 DSH |
252 | if (group == NULL || priv_key == NULL) { |
253 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER); | |
0f113f3e MC |
254 | return NULL; |
255 | } | |
4d94ae00 | 256 | |
0f113f3e | 257 | ret = ECDSA_SIG_new(); |
90945fa3 | 258 | if (ret == NULL) { |
6a47db45 | 259 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
260 | return NULL; |
261 | } | |
262 | s = ret->s; | |
c6700d27 | 263 | |
0f113f3e MC |
264 | if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL || |
265 | (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) { | |
6a47db45 | 266 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
267 | goto err; |
268 | } | |
4d94ae00 | 269 | |
0f113f3e | 270 | if (!EC_GROUP_get_order(group, order, ctx)) { |
6a47db45 | 271 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_EC_LIB); |
0f113f3e MC |
272 | goto err; |
273 | } | |
274 | i = BN_num_bits(order); | |
275 | /* | |
276 | * Need to truncate digest if it is too long: first truncate whole bytes. | |
277 | */ | |
278 | if (8 * dgst_len > i) | |
279 | dgst_len = (i + 7) / 8; | |
280 | if (!BN_bin2bn(dgst, dgst_len, m)) { | |
6a47db45 | 281 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
282 | goto err; |
283 | } | |
284 | /* If still too long truncate remaining bits with a shift */ | |
285 | if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) { | |
6a47db45 | 286 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
287 | goto err; |
288 | } | |
289 | do { | |
290 | if (in_kinv == NULL || in_r == NULL) { | |
291 | if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) { | |
6a47db45 | 292 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB); |
0f113f3e MC |
293 | goto err; |
294 | } | |
295 | ckinv = kinv; | |
296 | } else { | |
297 | ckinv = in_kinv; | |
298 | if (BN_copy(ret->r, in_r) == NULL) { | |
6a47db45 | 299 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
300 | goto err; |
301 | } | |
302 | } | |
4d94ae00 | 303 | |
0f113f3e | 304 | if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) { |
6a47db45 | 305 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
306 | goto err; |
307 | } | |
308 | if (!BN_mod_add_quick(s, tmp, m, order)) { | |
6a47db45 | 309 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
310 | goto err; |
311 | } | |
312 | if (!BN_mod_mul(s, s, ckinv, order, ctx)) { | |
6a47db45 | 313 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
314 | goto err; |
315 | } | |
316 | if (BN_is_zero(s)) { | |
317 | /* | |
318 | * if kinv and r have been supplied by the caller don't to | |
319 | * generate new kinv and r values | |
320 | */ | |
321 | if (in_kinv != NULL && in_r != NULL) { | |
6a47db45 | 322 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES); |
0f113f3e MC |
323 | goto err; |
324 | } | |
325 | } else | |
326 | /* s != 0 => we have a valid signature */ | |
327 | break; | |
328 | } | |
329 | while (1); | |
4d94ae00 | 330 | |
0f113f3e MC |
331 | ok = 1; |
332 | err: | |
333 | if (!ok) { | |
334 | ECDSA_SIG_free(ret); | |
335 | ret = NULL; | |
336 | } | |
23a1d5e9 RS |
337 | BN_CTX_free(ctx); |
338 | BN_clear_free(m); | |
339 | BN_clear_free(tmp); | |
340 | BN_free(order); | |
341 | BN_clear_free(kinv); | |
0f113f3e | 342 | return ret; |
4d94ae00 BM |
343 | } |
344 | ||
a200a817 DSH |
345 | /*- |
346 | * returns | |
347 | * 1: correct signature | |
348 | * 0: incorrect signature | |
349 | * -1: error | |
350 | */ | |
351 | int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len, | |
352 | const unsigned char *sigbuf, int sig_len, EC_KEY *eckey) | |
353 | { | |
354 | ECDSA_SIG *s; | |
355 | const unsigned char *p = sigbuf; | |
356 | unsigned char *der = NULL; | |
357 | int derlen = -1; | |
358 | int ret = -1; | |
359 | ||
360 | s = ECDSA_SIG_new(); | |
361 | if (s == NULL) | |
362 | return (ret); | |
363 | if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) | |
364 | goto err; | |
365 | /* Ensure signature uses DER and doesn't have trailing garbage */ | |
366 | derlen = i2d_ECDSA_SIG(s, &der); | |
91e7bcc2 | 367 | if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0) |
a200a817 DSH |
368 | goto err; |
369 | ret = ECDSA_do_verify(dgst, dgst_len, s, eckey); | |
370 | err: | |
371 | OPENSSL_clear_free(der, derlen); | |
372 | ECDSA_SIG_free(s); | |
373 | return (ret); | |
374 | } | |
375 | ||
6a47db45 DSH |
376 | int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len, |
377 | const ECDSA_SIG *sig, EC_KEY *eckey) | |
4d94ae00 | 378 | { |
0f113f3e MC |
379 | int ret = -1, i; |
380 | BN_CTX *ctx; | |
381 | BIGNUM *order, *u1, *u2, *m, *X; | |
382 | EC_POINT *point = NULL; | |
383 | const EC_GROUP *group; | |
384 | const EC_POINT *pub_key; | |
9dd84053 | 385 | |
0f113f3e MC |
386 | /* check input values */ |
387 | if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL || | |
388 | (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) { | |
6a47db45 | 389 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS); |
0f113f3e MC |
390 | return -1; |
391 | } | |
4d94ae00 | 392 | |
0f113f3e | 393 | ctx = BN_CTX_new(); |
90945fa3 | 394 | if (ctx == NULL) { |
6a47db45 | 395 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
396 | return -1; |
397 | } | |
398 | BN_CTX_start(ctx); | |
399 | order = BN_CTX_get(ctx); | |
400 | u1 = BN_CTX_get(ctx); | |
401 | u2 = BN_CTX_get(ctx); | |
402 | m = BN_CTX_get(ctx); | |
403 | X = BN_CTX_get(ctx); | |
91e7bcc2 | 404 | if (X == NULL) { |
6a47db45 | 405 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
406 | goto err; |
407 | } | |
c6700d27 | 408 | |
0f113f3e | 409 | if (!EC_GROUP_get_order(group, order, ctx)) { |
6a47db45 | 410 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB); |
0f113f3e MC |
411 | goto err; |
412 | } | |
4d94ae00 | 413 | |
0f113f3e MC |
414 | if (BN_is_zero(sig->r) || BN_is_negative(sig->r) || |
415 | BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) || | |
416 | BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) { | |
6a47db45 | 417 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE); |
0f113f3e MC |
418 | ret = 0; /* signature is invalid */ |
419 | goto err; | |
420 | } | |
421 | /* calculate tmp1 = inv(S) mod order */ | |
422 | if (!BN_mod_inverse(u2, sig->s, order, ctx)) { | |
6a47db45 | 423 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
424 | goto err; |
425 | } | |
426 | /* digest -> m */ | |
427 | i = BN_num_bits(order); | |
428 | /* | |
429 | * Need to truncate digest if it is too long: first truncate whole bytes. | |
430 | */ | |
431 | if (8 * dgst_len > i) | |
432 | dgst_len = (i + 7) / 8; | |
433 | if (!BN_bin2bn(dgst, dgst_len, m)) { | |
6a47db45 | 434 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
435 | goto err; |
436 | } | |
437 | /* If still too long truncate remaining bits with a shift */ | |
438 | if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) { | |
6a47db45 | 439 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
440 | goto err; |
441 | } | |
442 | /* u1 = m * tmp mod order */ | |
443 | if (!BN_mod_mul(u1, m, u2, order, ctx)) { | |
6a47db45 | 444 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
445 | goto err; |
446 | } | |
447 | /* u2 = r * w mod q */ | |
448 | if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) { | |
6a47db45 | 449 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
450 | goto err; |
451 | } | |
452 | ||
453 | if ((point = EC_POINT_new(group)) == NULL) { | |
6a47db45 | 454 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
455 | goto err; |
456 | } | |
457 | if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) { | |
6a47db45 | 458 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB); |
0f113f3e MC |
459 | goto err; |
460 | } | |
461 | if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == | |
462 | NID_X9_62_prime_field) { | |
463 | if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) { | |
6a47db45 | 464 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB); |
0f113f3e MC |
465 | goto err; |
466 | } | |
467 | } | |
b3310161 | 468 | #ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
469 | else { /* NID_X9_62_characteristic_two_field */ |
470 | ||
471 | if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) { | |
6a47db45 | 472 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB); |
0f113f3e MC |
473 | goto err; |
474 | } | |
475 | } | |
476 | #endif | |
477 | if (!BN_nnmod(u1, X, order, ctx)) { | |
6a47db45 | 478 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
479 | goto err; |
480 | } | |
481 | /* if the signature is correct u1 is equal to sig->r */ | |
482 | ret = (BN_ucmp(u1, sig->r) == 0); | |
483 | err: | |
484 | BN_CTX_end(ctx); | |
485 | BN_CTX_free(ctx); | |
8fdc3734 | 486 | EC_POINT_free(point); |
0f113f3e | 487 | return ret; |
4d94ae00 | 488 | } |