]>
Commit | Line | Data |
---|---|---|
c6700d27 | 1 | /* |
3c7d0945 | 2 | * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved. |
4d94ae00 | 3 | * |
aa6bb135 RS |
4 | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
4d94ae00 | 8 | */ |
0bee0e62 | 9 | |
a200a817 | 10 | #include <string.h> |
0bee0e62 | 11 | #include <openssl/err.h> |
14a7cfb3 | 12 | #include <openssl/obj_mac.h> |
0f814687 | 13 | #include <openssl/bn.h> |
8a99cb29 | 14 | #include <openssl/rand.h> |
6a47db45 DSH |
15 | #include <openssl/ec.h> |
16 | #include "ec_lcl.h" | |
190c615d | 17 | |
a200a817 DSH |
18 | int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen, |
19 | unsigned char *sig, unsigned int *siglen, | |
20 | const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey) | |
21 | { | |
22 | ECDSA_SIG *s; | |
75e2c877 | 23 | |
a200a817 DSH |
24 | s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey); |
25 | if (s == NULL) { | |
26 | *siglen = 0; | |
27 | return 0; | |
28 | } | |
29 | *siglen = i2d_ECDSA_SIG(s, &sig); | |
30 | ECDSA_SIG_free(s); | |
31 | return 1; | |
32 | } | |
33 | ||
8d6a75dc | 34 | static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, |
0f113f3e MC |
35 | BIGNUM **kinvp, BIGNUM **rp, |
36 | const unsigned char *dgst, int dlen) | |
4d94ae00 | 37 | { |
0f113f3e | 38 | BN_CTX *ctx = NULL; |
be2e334f DSH |
39 | BIGNUM *k = NULL, *r = NULL, *X = NULL; |
40 | const BIGNUM *order; | |
0f113f3e MC |
41 | EC_POINT *tmp_point = NULL; |
42 | const EC_GROUP *group; | |
43 | int ret = 0; | |
4a089bbd | 44 | int order_bits; |
9dd84053 | 45 | |
0f113f3e | 46 | if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) { |
6a47db45 | 47 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER); |
0f113f3e MC |
48 | return 0; |
49 | } | |
a74333f9 | 50 | |
4b0555ec DSH |
51 | if (!EC_KEY_can_sign(eckey)) { |
52 | ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING); | |
53 | return 0; | |
54 | } | |
55 | ||
0f113f3e MC |
56 | if (ctx_in == NULL) { |
57 | if ((ctx = BN_CTX_new()) == NULL) { | |
6a47db45 | 58 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
59 | return 0; |
60 | } | |
61 | } else | |
62 | ctx = ctx_in; | |
4d94ae00 | 63 | |
0f113f3e MC |
64 | k = BN_new(); /* this value is later returned in *kinvp */ |
65 | r = BN_new(); /* this value is later returned in *rp */ | |
0f113f3e | 66 | X = BN_new(); |
be2e334f | 67 | if (k == NULL || r == NULL || X == NULL) { |
6a47db45 | 68 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
69 | goto err; |
70 | } | |
71 | if ((tmp_point = EC_POINT_new(group)) == NULL) { | |
6a47db45 | 72 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB); |
0f113f3e MC |
73 | goto err; |
74 | } | |
be2e334f DSH |
75 | order = EC_GROUP_get0_order(group); |
76 | if (order == NULL) { | |
6a47db45 | 77 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB); |
0f113f3e MC |
78 | goto err; |
79 | } | |
cac4fb58 | 80 | |
4a089bbd P |
81 | /* Preallocate space */ |
82 | order_bits = BN_num_bits(order); | |
83 | if (!BN_set_bit(k, order_bits) | |
84 | || !BN_set_bit(r, order_bits) | |
85 | || !BN_set_bit(X, order_bits)) | |
86 | goto err; | |
87 | ||
0f113f3e MC |
88 | do { |
89 | /* get random k */ | |
90 | do | |
0f113f3e MC |
91 | if (dgst != NULL) { |
92 | if (!BN_generate_dsa_nonce | |
93 | (k, order, EC_KEY_get0_private_key(eckey), dgst, dlen, | |
94 | ctx)) { | |
6a47db45 DSH |
95 | ECerr(EC_F_ECDSA_SIGN_SETUP, |
96 | EC_R_RANDOM_NUMBER_GENERATION_FAILED); | |
0f113f3e MC |
97 | goto err; |
98 | } | |
474e469b | 99 | } else { |
ddc6a5c8 | 100 | if (!BN_priv_rand_range(k, order)) { |
6a47db45 DSH |
101 | ECerr(EC_F_ECDSA_SIGN_SETUP, |
102 | EC_R_RANDOM_NUMBER_GENERATION_FAILED); | |
0f113f3e MC |
103 | goto err; |
104 | } | |
105 | } | |
106 | while (BN_is_zero(k)); | |
4d94ae00 | 107 | |
0f113f3e MC |
108 | /* compute r the x-coordinate of generator * k */ |
109 | if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) { | |
6a47db45 | 110 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB); |
0f113f3e MC |
111 | goto err; |
112 | } | |
113 | if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == | |
114 | NID_X9_62_prime_field) { | |
115 | if (!EC_POINT_get_affine_coordinates_GFp | |
116 | (group, tmp_point, X, NULL, ctx)) { | |
6a47db45 | 117 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB); |
0f113f3e MC |
118 | goto err; |
119 | } | |
120 | } | |
b3310161 | 121 | #ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
122 | else { /* NID_X9_62_characteristic_two_field */ |
123 | ||
124 | if (!EC_POINT_get_affine_coordinates_GF2m(group, | |
125 | tmp_point, X, NULL, | |
126 | ctx)) { | |
6a47db45 | 127 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB); |
0f113f3e MC |
128 | goto err; |
129 | } | |
130 | } | |
b3310161 | 131 | #endif |
0f113f3e | 132 | if (!BN_nnmod(r, X, order, ctx)) { |
6a47db45 | 133 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); |
0f113f3e MC |
134 | goto err; |
135 | } | |
136 | } | |
137 | while (BN_is_zero(r)); | |
4d94ae00 | 138 | |
eb791696 AP |
139 | /* Check if optimized inverse is implemented */ |
140 | if (EC_GROUP_do_inverse_ord(group, k, k, ctx) == 0) { | |
141 | /* compute the inverse of k */ | |
142 | if (group->mont_data != NULL) { | |
143 | /* | |
144 | * We want inverse in constant time, therefore we utilize the fact | |
145 | * order must be prime and use Fermats Little Theorem instead. | |
146 | */ | |
147 | if (!BN_set_word(X, 2)) { | |
148 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); | |
149 | goto err; | |
150 | } | |
151 | if (!BN_mod_sub(X, order, X, order, ctx)) { | |
152 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); | |
153 | goto err; | |
154 | } | |
155 | BN_set_flags(X, BN_FLG_CONSTTIME); | |
156 | if (!BN_mod_exp_mont_consttime(k, k, X, order, ctx, | |
157 | group->mont_data)) { | |
158 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); | |
159 | goto err; | |
160 | } | |
161 | } else { | |
162 | if (!BN_mod_inverse(k, k, order, ctx)) { | |
163 | ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); | |
164 | goto err; | |
165 | } | |
0f113f3e MC |
166 | } |
167 | } | |
f54be179 | 168 | |
0f113f3e | 169 | /* clear old values if necessary */ |
23a1d5e9 RS |
170 | BN_clear_free(*rp); |
171 | BN_clear_free(*kinvp); | |
0f113f3e MC |
172 | /* save the pre-computed values */ |
173 | *rp = r; | |
174 | *kinvp = k; | |
175 | ret = 1; | |
176 | err: | |
177 | if (!ret) { | |
23a1d5e9 RS |
178 | BN_clear_free(k); |
179 | BN_clear_free(r); | |
0f113f3e | 180 | } |
23a1d5e9 | 181 | if (ctx != ctx_in) |
0f113f3e | 182 | BN_CTX_free(ctx); |
8fdc3734 | 183 | EC_POINT_free(tmp_point); |
23a1d5e9 | 184 | BN_clear_free(X); |
26a7d938 | 185 | return ret; |
4d94ae00 BM |
186 | } |
187 | ||
6a47db45 DSH |
188 | int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, |
189 | BIGNUM **rp) | |
190 | { | |
191 | return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0); | |
192 | } | |
193 | ||
194 | ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len, | |
195 | const BIGNUM *in_kinv, const BIGNUM *in_r, | |
196 | EC_KEY *eckey) | |
4d94ae00 | 197 | { |
0f113f3e | 198 | int ok = 0, i; |
a3e9d5aa MC |
199 | BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *blind = NULL; |
200 | BIGNUM *blindm = NULL; | |
be2e334f | 201 | const BIGNUM *order, *ckinv; |
0f113f3e MC |
202 | BN_CTX *ctx = NULL; |
203 | const EC_GROUP *group; | |
204 | ECDSA_SIG *ret; | |
0f113f3e MC |
205 | const BIGNUM *priv_key; |
206 | ||
0f113f3e MC |
207 | group = EC_KEY_get0_group(eckey); |
208 | priv_key = EC_KEY_get0_private_key(eckey); | |
14a7cfb3 | 209 | |
6a47db45 DSH |
210 | if (group == NULL || priv_key == NULL) { |
211 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER); | |
0f113f3e MC |
212 | return NULL; |
213 | } | |
4d94ae00 | 214 | |
4b0555ec DSH |
215 | if (!EC_KEY_can_sign(eckey)) { |
216 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING); | |
217 | return NULL; | |
218 | } | |
219 | ||
0f113f3e | 220 | ret = ECDSA_SIG_new(); |
90945fa3 | 221 | if (ret == NULL) { |
6a47db45 | 222 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
223 | return NULL; |
224 | } | |
8cc44d97 DSH |
225 | ret->r = BN_new(); |
226 | ret->s = BN_new(); | |
227 | if (ret->r == NULL || ret->s == NULL) { | |
228 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE); | |
229 | goto err; | |
230 | } | |
0f113f3e | 231 | s = ret->s; |
c6700d27 | 232 | |
a3e9d5aa MC |
233 | ctx = BN_CTX_secure_new(); |
234 | if (ctx == NULL) { | |
235 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE); | |
236 | goto err; | |
237 | } | |
238 | ||
239 | BN_CTX_start(ctx); | |
240 | tmp = BN_CTX_get(ctx); | |
241 | m = BN_CTX_get(ctx); | |
242 | blind = BN_CTX_get(ctx); | |
243 | blindm = BN_CTX_get(ctx); | |
244 | if (blindm == NULL) { | |
6a47db45 | 245 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
246 | goto err; |
247 | } | |
4d94ae00 | 248 | |
be2e334f DSH |
249 | order = EC_GROUP_get0_order(group); |
250 | if (order == NULL) { | |
6a47db45 | 251 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_EC_LIB); |
0f113f3e MC |
252 | goto err; |
253 | } | |
254 | i = BN_num_bits(order); | |
255 | /* | |
256 | * Need to truncate digest if it is too long: first truncate whole bytes. | |
257 | */ | |
258 | if (8 * dgst_len > i) | |
259 | dgst_len = (i + 7) / 8; | |
260 | if (!BN_bin2bn(dgst, dgst_len, m)) { | |
6a47db45 | 261 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
262 | goto err; |
263 | } | |
264 | /* If still too long truncate remaining bits with a shift */ | |
265 | if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) { | |
6a47db45 | 266 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
267 | goto err; |
268 | } | |
269 | do { | |
270 | if (in_kinv == NULL || in_r == NULL) { | |
271 | if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) { | |
6a47db45 | 272 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB); |
0f113f3e MC |
273 | goto err; |
274 | } | |
275 | ckinv = kinv; | |
276 | } else { | |
277 | ckinv = in_kinv; | |
278 | if (BN_copy(ret->r, in_r) == NULL) { | |
6a47db45 | 279 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
280 | goto err; |
281 | } | |
282 | } | |
4d94ae00 | 283 | |
a3e9d5aa MC |
284 | /* |
285 | * The normal signature calculation is: | |
286 | * | |
287 | * s := k^-1 * (m + r * priv_key) mod order | |
288 | * | |
289 | * We will blind this to protect against side channel attacks | |
290 | * | |
7f9822a4 | 291 | * s := blind^-1 * k^-1 * (blind * m + blind * r * priv_key) mod order |
a3e9d5aa MC |
292 | */ |
293 | ||
294 | /* Generate a blinding value */ | |
295 | do { | |
296 | if (!BN_priv_rand(blind, BN_num_bits(order) - 1, | |
297 | BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY)) | |
298 | goto err; | |
299 | } while (BN_is_zero(blind)); | |
300 | BN_set_flags(blind, BN_FLG_CONSTTIME); | |
301 | BN_set_flags(blindm, BN_FLG_CONSTTIME); | |
302 | BN_set_flags(tmp, BN_FLG_CONSTTIME); | |
303 | ||
304 | /* tmp := blind * priv_key * r mod order */ | |
305 | if (!BN_mod_mul(tmp, blind, priv_key, order, ctx)) { | |
306 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB); | |
307 | goto err; | |
308 | } | |
309 | if (!BN_mod_mul(tmp, tmp, ret->r, order, ctx)) { | |
310 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB); | |
311 | goto err; | |
312 | } | |
313 | ||
314 | /* blindm := blind * m mod order */ | |
315 | if (!BN_mod_mul(blindm, blind, m, order, ctx)) { | |
316 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB); | |
317 | goto err; | |
318 | } | |
319 | ||
320 | /* s : = (blind * priv_key * r) + (blind * m) mod order */ | |
321 | if (!BN_mod_add_quick(s, tmp, blindm, order)) { | |
6a47db45 | 322 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
323 | goto err; |
324 | } | |
a3e9d5aa | 325 | |
7f9822a4 MC |
326 | /* s := s * k^-1 mod order */ |
327 | if (!BN_mod_mul(s, s, ckinv, order, ctx)) { | |
a3e9d5aa MC |
328 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB); |
329 | goto err; | |
330 | } | |
7f9822a4 MC |
331 | |
332 | /* s:= s * blind^-1 mod order */ | |
333 | if (BN_mod_inverse(blind, blind, order, ctx) == NULL) { | |
6a47db45 | 334 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
335 | goto err; |
336 | } | |
7f9822a4 | 337 | if (!BN_mod_mul(s, s, blind, order, ctx)) { |
6a47db45 | 338 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
339 | goto err; |
340 | } | |
a3e9d5aa | 341 | |
0f113f3e MC |
342 | if (BN_is_zero(s)) { |
343 | /* | |
532b1183 | 344 | * if kinv and r have been supplied by the caller, don't |
0f113f3e MC |
345 | * generate new kinv and r values |
346 | */ | |
347 | if (in_kinv != NULL && in_r != NULL) { | |
6a47db45 | 348 | ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES); |
0f113f3e MC |
349 | goto err; |
350 | } | |
351 | } else | |
352 | /* s != 0 => we have a valid signature */ | |
353 | break; | |
354 | } | |
355 | while (1); | |
4d94ae00 | 356 | |
0f113f3e MC |
357 | ok = 1; |
358 | err: | |
359 | if (!ok) { | |
360 | ECDSA_SIG_free(ret); | |
361 | ret = NULL; | |
362 | } | |
a3e9d5aa | 363 | BN_CTX_end(ctx); |
23a1d5e9 | 364 | BN_CTX_free(ctx); |
23a1d5e9 | 365 | BN_clear_free(kinv); |
0f113f3e | 366 | return ret; |
4d94ae00 BM |
367 | } |
368 | ||
a200a817 DSH |
369 | /*- |
370 | * returns | |
371 | * 1: correct signature | |
372 | * 0: incorrect signature | |
373 | * -1: error | |
374 | */ | |
375 | int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len, | |
376 | const unsigned char *sigbuf, int sig_len, EC_KEY *eckey) | |
377 | { | |
378 | ECDSA_SIG *s; | |
379 | const unsigned char *p = sigbuf; | |
380 | unsigned char *der = NULL; | |
381 | int derlen = -1; | |
382 | int ret = -1; | |
383 | ||
384 | s = ECDSA_SIG_new(); | |
385 | if (s == NULL) | |
26a7d938 | 386 | return ret; |
a200a817 DSH |
387 | if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) |
388 | goto err; | |
389 | /* Ensure signature uses DER and doesn't have trailing garbage */ | |
390 | derlen = i2d_ECDSA_SIG(s, &der); | |
91e7bcc2 | 391 | if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0) |
a200a817 DSH |
392 | goto err; |
393 | ret = ECDSA_do_verify(dgst, dgst_len, s, eckey); | |
394 | err: | |
395 | OPENSSL_clear_free(der, derlen); | |
396 | ECDSA_SIG_free(s); | |
26a7d938 | 397 | return ret; |
a200a817 DSH |
398 | } |
399 | ||
6a47db45 DSH |
400 | int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len, |
401 | const ECDSA_SIG *sig, EC_KEY *eckey) | |
4d94ae00 | 402 | { |
0f113f3e MC |
403 | int ret = -1, i; |
404 | BN_CTX *ctx; | |
be2e334f DSH |
405 | const BIGNUM *order; |
406 | BIGNUM *u1, *u2, *m, *X; | |
0f113f3e MC |
407 | EC_POINT *point = NULL; |
408 | const EC_GROUP *group; | |
409 | const EC_POINT *pub_key; | |
9dd84053 | 410 | |
0f113f3e MC |
411 | /* check input values */ |
412 | if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL || | |
413 | (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) { | |
6a47db45 | 414 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS); |
0f113f3e MC |
415 | return -1; |
416 | } | |
4d94ae00 | 417 | |
4b0555ec DSH |
418 | if (!EC_KEY_can_sign(eckey)) { |
419 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING); | |
420 | return -1; | |
421 | } | |
422 | ||
0f113f3e | 423 | ctx = BN_CTX_new(); |
90945fa3 | 424 | if (ctx == NULL) { |
6a47db45 | 425 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
426 | return -1; |
427 | } | |
428 | BN_CTX_start(ctx); | |
0f113f3e MC |
429 | u1 = BN_CTX_get(ctx); |
430 | u2 = BN_CTX_get(ctx); | |
431 | m = BN_CTX_get(ctx); | |
432 | X = BN_CTX_get(ctx); | |
91e7bcc2 | 433 | if (X == NULL) { |
6a47db45 | 434 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
435 | goto err; |
436 | } | |
c6700d27 | 437 | |
be2e334f DSH |
438 | order = EC_GROUP_get0_order(group); |
439 | if (order == NULL) { | |
6a47db45 | 440 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB); |
0f113f3e MC |
441 | goto err; |
442 | } | |
4d94ae00 | 443 | |
0f113f3e MC |
444 | if (BN_is_zero(sig->r) || BN_is_negative(sig->r) || |
445 | BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) || | |
446 | BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) { | |
6a47db45 | 447 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE); |
0f113f3e MC |
448 | ret = 0; /* signature is invalid */ |
449 | goto err; | |
450 | } | |
451 | /* calculate tmp1 = inv(S) mod order */ | |
eb791696 AP |
452 | /* Check if optimized inverse is implemented */ |
453 | if (EC_GROUP_do_inverse_ord(group, u2, sig->s, ctx) == 0) { | |
454 | if (!BN_mod_inverse(u2, sig->s, order, ctx)) { | |
455 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); | |
456 | goto err; | |
457 | } | |
0f113f3e MC |
458 | } |
459 | /* digest -> m */ | |
460 | i = BN_num_bits(order); | |
461 | /* | |
462 | * Need to truncate digest if it is too long: first truncate whole bytes. | |
463 | */ | |
464 | if (8 * dgst_len > i) | |
465 | dgst_len = (i + 7) / 8; | |
466 | if (!BN_bin2bn(dgst, dgst_len, m)) { | |
6a47db45 | 467 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
468 | goto err; |
469 | } | |
470 | /* If still too long truncate remaining bits with a shift */ | |
471 | if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) { | |
6a47db45 | 472 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
473 | goto err; |
474 | } | |
475 | /* u1 = m * tmp mod order */ | |
476 | if (!BN_mod_mul(u1, m, u2, order, ctx)) { | |
6a47db45 | 477 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
478 | goto err; |
479 | } | |
480 | /* u2 = r * w mod q */ | |
481 | if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) { | |
6a47db45 | 482 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
483 | goto err; |
484 | } | |
485 | ||
486 | if ((point = EC_POINT_new(group)) == NULL) { | |
6a47db45 | 487 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
488 | goto err; |
489 | } | |
490 | if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) { | |
6a47db45 | 491 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB); |
0f113f3e MC |
492 | goto err; |
493 | } | |
494 | if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == | |
495 | NID_X9_62_prime_field) { | |
496 | if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) { | |
6a47db45 | 497 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB); |
0f113f3e MC |
498 | goto err; |
499 | } | |
500 | } | |
b3310161 | 501 | #ifndef OPENSSL_NO_EC2M |
0f113f3e MC |
502 | else { /* NID_X9_62_characteristic_two_field */ |
503 | ||
504 | if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) { | |
6a47db45 | 505 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB); |
0f113f3e MC |
506 | goto err; |
507 | } | |
508 | } | |
509 | #endif | |
510 | if (!BN_nnmod(u1, X, order, ctx)) { | |
6a47db45 | 511 | ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB); |
0f113f3e MC |
512 | goto err; |
513 | } | |
514 | /* if the signature is correct u1 is equal to sig->r */ | |
515 | ret = (BN_ucmp(u1, sig->r) == 0); | |
516 | err: | |
517 | BN_CTX_end(ctx); | |
518 | BN_CTX_free(ctx); | |
8fdc3734 | 519 | EC_POINT_free(point); |
0f113f3e | 520 | return ret; |
4d94ae00 | 521 | } |