1 | /*
|
---|
2 | * Copyright 2002-2024 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License 2.0 (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
|
---|
8 | */
|
---|
9 |
|
---|
10 | /*
|
---|
11 | * ECDSA low level APIs are deprecated for public use, but still ok for
|
---|
12 | * internal use.
|
---|
13 | */
|
---|
14 | #include "internal/deprecated.h"
|
---|
15 |
|
---|
16 | #include <string.h>
|
---|
17 | #include <openssl/err.h>
|
---|
18 | #include <openssl/obj_mac.h>
|
---|
19 | #include <openssl/rand.h>
|
---|
20 | #include "crypto/bn.h"
|
---|
21 | #include "ec_local.h"
|
---|
22 | #include "internal/deterministic_nonce.h"
|
---|
23 |
|
---|
24 | #define MIN_ECDSA_SIGN_ORDERBITS 64
|
---|
25 | /*
|
---|
26 | * It is highly unlikely that a retry will happen,
|
---|
27 | * Multiple retries would indicate that something is wrong
|
---|
28 | * with the group parameters (which would normally only happen
|
---|
29 | * with a bad custom group).
|
---|
30 | */
|
---|
31 | #define MAX_ECDSA_SIGN_RETRIES 8
|
---|
32 |
|
---|
33 | static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
|
---|
34 | BIGNUM **kinvp, BIGNUM **rp,
|
---|
35 | const unsigned char *dgst, int dlen,
|
---|
36 | unsigned int nonce_type, const char *digestname,
|
---|
37 | OSSL_LIB_CTX *libctx, const char *propq);
|
---|
38 |
|
---|
39 | int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
|
---|
40 | BIGNUM **rp)
|
---|
41 | {
|
---|
42 | if (eckey->group->meth->ecdsa_sign_setup == NULL) {
|
---|
43 | ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
|
---|
44 | return 0;
|
---|
45 | }
|
---|
46 |
|
---|
47 | return eckey->group->meth->ecdsa_sign_setup(eckey, ctx_in, kinvp, rp);
|
---|
48 | }
|
---|
49 |
|
---|
50 | ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
|
---|
51 | const BIGNUM *in_kinv, const BIGNUM *in_r,
|
---|
52 | EC_KEY *eckey)
|
---|
53 | {
|
---|
54 | if (eckey->group->meth->ecdsa_sign_sig == NULL) {
|
---|
55 | ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
|
---|
56 | return NULL;
|
---|
57 | }
|
---|
58 |
|
---|
59 | return eckey->group->meth->ecdsa_sign_sig(dgst, dgst_len,
|
---|
60 | in_kinv, in_r, eckey);
|
---|
61 | }
|
---|
62 |
|
---|
63 | int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
|
---|
64 | const ECDSA_SIG *sig, EC_KEY *eckey)
|
---|
65 | {
|
---|
66 | if (eckey->group->meth->ecdsa_verify_sig == NULL) {
|
---|
67 | ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
|
---|
68 | return 0;
|
---|
69 | }
|
---|
70 |
|
---|
71 | return eckey->group->meth->ecdsa_verify_sig(dgst, dgst_len, sig, eckey);
|
---|
72 | }
|
---|
73 |
|
---|
74 | int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
|
---|
75 | unsigned char *sig, unsigned int *siglen,
|
---|
76 | const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey)
|
---|
77 | {
|
---|
78 | ECDSA_SIG *s;
|
---|
79 |
|
---|
80 | if (sig == NULL && (kinv == NULL || r == NULL)) {
|
---|
81 | *siglen = ECDSA_size(eckey);
|
---|
82 | return 1;
|
---|
83 | }
|
---|
84 |
|
---|
85 | s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
|
---|
86 | if (s == NULL) {
|
---|
87 | *siglen = 0;
|
---|
88 | return 0;
|
---|
89 | }
|
---|
90 | *siglen = i2d_ECDSA_SIG(s, sig != NULL ? &sig : NULL);
|
---|
91 | ECDSA_SIG_free(s);
|
---|
92 | return 1;
|
---|
93 | }
|
---|
94 |
|
---|
95 | int ossl_ecdsa_deterministic_sign(const unsigned char *dgst, int dlen,
|
---|
96 | unsigned char *sig, unsigned int *siglen,
|
---|
97 | EC_KEY *eckey, unsigned int nonce_type,
|
---|
98 | const char *digestname,
|
---|
99 | OSSL_LIB_CTX *libctx, const char *propq)
|
---|
100 | {
|
---|
101 | ECDSA_SIG *s;
|
---|
102 | BIGNUM *kinv = NULL, *r = NULL;
|
---|
103 | int ret = 0;
|
---|
104 |
|
---|
105 | if (sig == NULL) {
|
---|
106 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
|
---|
107 | return 0;
|
---|
108 | }
|
---|
109 |
|
---|
110 | *siglen = 0;
|
---|
111 | if (!ecdsa_sign_setup(eckey, NULL, &kinv, &r, dgst, dlen,
|
---|
112 | nonce_type, digestname, libctx, propq))
|
---|
113 | return 0;
|
---|
114 |
|
---|
115 | s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
|
---|
116 | if (s == NULL)
|
---|
117 | goto end;
|
---|
118 |
|
---|
119 | *siglen = i2d_ECDSA_SIG(s, &sig);
|
---|
120 | ECDSA_SIG_free(s);
|
---|
121 | ret = 1;
|
---|
122 | end:
|
---|
123 | BN_clear_free(kinv);
|
---|
124 | BN_clear_free(r);
|
---|
125 | return ret;
|
---|
126 | }
|
---|
127 |
|
---|
128 | static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
|
---|
129 | BIGNUM **kinvp, BIGNUM **rp,
|
---|
130 | const unsigned char *dgst, int dlen,
|
---|
131 | unsigned int nonce_type, const char *digestname,
|
---|
132 | OSSL_LIB_CTX *libctx, const char *propq)
|
---|
133 | {
|
---|
134 | BN_CTX *ctx = NULL;
|
---|
135 | BIGNUM *k = NULL, *r = NULL, *X = NULL;
|
---|
136 | const BIGNUM *order;
|
---|
137 | EC_POINT *tmp_point = NULL;
|
---|
138 | const EC_GROUP *group;
|
---|
139 | int ret = 0;
|
---|
140 | int order_bits;
|
---|
141 | const BIGNUM *priv_key;
|
---|
142 |
|
---|
143 | if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
|
---|
144 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
|
---|
145 | return 0;
|
---|
146 | }
|
---|
147 | if ((priv_key = EC_KEY_get0_private_key(eckey)) == NULL) {
|
---|
148 | ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
|
---|
149 | return 0;
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (!EC_KEY_can_sign(eckey)) {
|
---|
153 | ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
|
---|
154 | return 0;
|
---|
155 | }
|
---|
156 |
|
---|
157 | if ((ctx = ctx_in) == NULL) {
|
---|
158 | if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL) {
|
---|
159 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
160 | return 0;
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | k = BN_secure_new(); /* this value is later returned in *kinvp */
|
---|
165 | r = BN_new(); /* this value is later returned in *rp */
|
---|
166 | X = BN_new();
|
---|
167 | if (k == NULL || r == NULL || X == NULL) {
|
---|
168 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
169 | goto err;
|
---|
170 | }
|
---|
171 | if ((tmp_point = EC_POINT_new(group)) == NULL) {
|
---|
172 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
|
---|
173 | goto err;
|
---|
174 | }
|
---|
175 |
|
---|
176 | if ((order = EC_GROUP_get0_order(group)) == NULL) {
|
---|
177 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
|
---|
178 | goto err;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /* Preallocate space */
|
---|
182 | order_bits = BN_num_bits(order);
|
---|
183 | /* Check the number of bits here so that an infinite loop is not possible */
|
---|
184 | if (order_bits < MIN_ECDSA_SIGN_ORDERBITS
|
---|
185 | || !BN_set_bit(k, order_bits)
|
---|
186 | || !BN_set_bit(r, order_bits)
|
---|
187 | || !BN_set_bit(X, order_bits))
|
---|
188 | goto err;
|
---|
189 |
|
---|
190 | do {
|
---|
191 | /* get random or deterministic value of k */
|
---|
192 | do {
|
---|
193 | int res = 0;
|
---|
194 |
|
---|
195 | if (dgst != NULL) {
|
---|
196 | if (nonce_type == 1) {
|
---|
197 | #ifndef FIPS_MODULE
|
---|
198 | res = ossl_gen_deterministic_nonce_rfc6979(k, order,
|
---|
199 | priv_key,
|
---|
200 | dgst, dlen,
|
---|
201 | digestname,
|
---|
202 | libctx, propq);
|
---|
203 | #endif
|
---|
204 | } else {
|
---|
205 | res = ossl_bn_gen_dsa_nonce_fixed_top(k, order, priv_key,
|
---|
206 | dgst, dlen, ctx);
|
---|
207 | }
|
---|
208 | } else {
|
---|
209 | res = ossl_bn_priv_rand_range_fixed_top(k, order, 0, ctx);
|
---|
210 | }
|
---|
211 | if (!res) {
|
---|
212 | ERR_raise(ERR_LIB_EC, EC_R_RANDOM_NUMBER_GENERATION_FAILED);
|
---|
213 | goto err;
|
---|
214 | }
|
---|
215 | } while (ossl_bn_is_word_fixed_top(k, 0));
|
---|
216 |
|
---|
217 | /* compute r the x-coordinate of generator * k */
|
---|
218 | if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
|
---|
219 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
|
---|
220 | goto err;
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (!EC_POINT_get_affine_coordinates(group, tmp_point, X, NULL, ctx)) {
|
---|
224 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
|
---|
225 | goto err;
|
---|
226 | }
|
---|
227 |
|
---|
228 | if (!BN_nnmod(r, X, order, ctx)) {
|
---|
229 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
230 | goto err;
|
---|
231 | }
|
---|
232 | } while (BN_is_zero(r));
|
---|
233 |
|
---|
234 | /* compute the inverse of k */
|
---|
235 | if (!ossl_ec_group_do_inverse_ord(group, k, k, ctx)) {
|
---|
236 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
237 | goto err;
|
---|
238 | }
|
---|
239 |
|
---|
240 | /* clear old values if necessary */
|
---|
241 | BN_clear_free(*rp);
|
---|
242 | BN_clear_free(*kinvp);
|
---|
243 | /* save the pre-computed values */
|
---|
244 | *rp = r;
|
---|
245 | *kinvp = k;
|
---|
246 | ret = 1;
|
---|
247 | err:
|
---|
248 | if (!ret) {
|
---|
249 | BN_clear_free(k);
|
---|
250 | BN_clear_free(r);
|
---|
251 | }
|
---|
252 | if (ctx != ctx_in)
|
---|
253 | BN_CTX_free(ctx);
|
---|
254 | EC_POINT_free(tmp_point);
|
---|
255 | BN_clear_free(X);
|
---|
256 | return ret;
|
---|
257 | }
|
---|
258 |
|
---|
259 | int ossl_ecdsa_simple_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
|
---|
260 | BIGNUM **rp)
|
---|
261 | {
|
---|
262 | return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0,
|
---|
263 | 0, NULL, NULL, NULL);
|
---|
264 | }
|
---|
265 |
|
---|
266 | ECDSA_SIG *ossl_ecdsa_simple_sign_sig(const unsigned char *dgst, int dgst_len,
|
---|
267 | const BIGNUM *in_kinv, const BIGNUM *in_r,
|
---|
268 | EC_KEY *eckey)
|
---|
269 | {
|
---|
270 | int ok = 0, i;
|
---|
271 | int retries = 0;
|
---|
272 | BIGNUM *kinv = NULL, *s, *m = NULL;
|
---|
273 | const BIGNUM *order, *ckinv;
|
---|
274 | BN_CTX *ctx = NULL;
|
---|
275 | const EC_GROUP *group;
|
---|
276 | ECDSA_SIG *ret;
|
---|
277 | const BIGNUM *priv_key;
|
---|
278 |
|
---|
279 | group = EC_KEY_get0_group(eckey);
|
---|
280 | priv_key = EC_KEY_get0_private_key(eckey);
|
---|
281 |
|
---|
282 | if (group == NULL) {
|
---|
283 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
|
---|
284 | return NULL;
|
---|
285 | }
|
---|
286 | if (priv_key == NULL) {
|
---|
287 | ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
|
---|
288 | return NULL;
|
---|
289 | }
|
---|
290 |
|
---|
291 | if (!EC_KEY_can_sign(eckey)) {
|
---|
292 | ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
|
---|
293 | return NULL;
|
---|
294 | }
|
---|
295 |
|
---|
296 | ret = ECDSA_SIG_new();
|
---|
297 | if (ret == NULL) {
|
---|
298 | ERR_raise(ERR_LIB_EC, ERR_R_ECDSA_LIB);
|
---|
299 | return NULL;
|
---|
300 | }
|
---|
301 | ret->r = BN_new();
|
---|
302 | ret->s = BN_new();
|
---|
303 | if (ret->r == NULL || ret->s == NULL) {
|
---|
304 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
305 | goto err;
|
---|
306 | }
|
---|
307 | s = ret->s;
|
---|
308 |
|
---|
309 | if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL
|
---|
310 | || (m = BN_new()) == NULL) {
|
---|
311 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
312 | goto err;
|
---|
313 | }
|
---|
314 |
|
---|
315 | if ((order = EC_GROUP_get0_order(group)) == NULL) {
|
---|
316 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
|
---|
317 | goto err;
|
---|
318 | }
|
---|
319 |
|
---|
320 | i = BN_num_bits(order);
|
---|
321 | /*
|
---|
322 | * Need to truncate digest if it is too long: first truncate whole bytes.
|
---|
323 | */
|
---|
324 | if (8 * dgst_len > i)
|
---|
325 | dgst_len = (i + 7) / 8;
|
---|
326 | if (!BN_bin2bn(dgst, dgst_len, m)) {
|
---|
327 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
328 | goto err;
|
---|
329 | }
|
---|
330 | /* If still too long, truncate remaining bits with a shift */
|
---|
331 | if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
|
---|
332 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
333 | goto err;
|
---|
334 | }
|
---|
335 | do {
|
---|
336 | if (in_kinv == NULL || in_r == NULL) {
|
---|
337 | if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len,
|
---|
338 | 0, NULL, NULL, NULL)) {
|
---|
339 | ERR_raise(ERR_LIB_EC, ERR_R_ECDSA_LIB);
|
---|
340 | goto err;
|
---|
341 | }
|
---|
342 | ckinv = kinv;
|
---|
343 | } else {
|
---|
344 | ckinv = in_kinv;
|
---|
345 | if (BN_copy(ret->r, in_r) == NULL) {
|
---|
346 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
347 | goto err;
|
---|
348 | }
|
---|
349 | }
|
---|
350 |
|
---|
351 | /*
|
---|
352 | * With only one multiplicant being in Montgomery domain
|
---|
353 | * multiplication yields real result without post-conversion.
|
---|
354 | * Also note that all operations but last are performed with
|
---|
355 | * zero-padded vectors. Last operation, BN_mod_mul_montgomery
|
---|
356 | * below, returns user-visible value with removed zero padding.
|
---|
357 | */
|
---|
358 | if (!bn_to_mont_fixed_top(s, ret->r, group->mont_data, ctx)
|
---|
359 | || !bn_mul_mont_fixed_top(s, s, priv_key, group->mont_data, ctx)) {
|
---|
360 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
361 | goto err;
|
---|
362 | }
|
---|
363 | if (!bn_mod_add_fixed_top(s, s, m, order)) {
|
---|
364 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
365 | goto err;
|
---|
366 | }
|
---|
367 | /*
|
---|
368 | * |s| can still be larger than modulus, because |m| can be. In
|
---|
369 | * such case we count on Montgomery reduction to tie it up.
|
---|
370 | */
|
---|
371 | if (!bn_to_mont_fixed_top(s, s, group->mont_data, ctx)
|
---|
372 | || !BN_mod_mul_montgomery(s, s, ckinv, group->mont_data, ctx)) {
|
---|
373 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
374 | goto err;
|
---|
375 | }
|
---|
376 |
|
---|
377 | if (BN_is_zero(s)) {
|
---|
378 | /*
|
---|
379 | * if kinv and r have been supplied by the caller, don't
|
---|
380 | * generate new kinv and r values
|
---|
381 | */
|
---|
382 | if (in_kinv != NULL && in_r != NULL) {
|
---|
383 | ERR_raise(ERR_LIB_EC, EC_R_NEED_NEW_SETUP_VALUES);
|
---|
384 | goto err;
|
---|
385 | }
|
---|
386 | /* Avoid infinite loops cause by invalid group parameters */
|
---|
387 | if (retries++ > MAX_ECDSA_SIGN_RETRIES) {
|
---|
388 | ERR_raise(ERR_LIB_EC, EC_R_TOO_MANY_RETRIES);
|
---|
389 | goto err;
|
---|
390 | }
|
---|
391 | } else {
|
---|
392 | /* s != 0 => we have a valid signature */
|
---|
393 | break;
|
---|
394 | }
|
---|
395 | } while (1);
|
---|
396 |
|
---|
397 | ok = 1;
|
---|
398 | err:
|
---|
399 | if (!ok) {
|
---|
400 | ECDSA_SIG_free(ret);
|
---|
401 | ret = NULL;
|
---|
402 | }
|
---|
403 | BN_CTX_free(ctx);
|
---|
404 | BN_clear_free(m);
|
---|
405 | BN_clear_free(kinv);
|
---|
406 | return ret;
|
---|
407 | }
|
---|
408 |
|
---|
409 | /*-
|
---|
410 | * returns
|
---|
411 | * 1: correct signature
|
---|
412 | * 0: incorrect signature
|
---|
413 | * -1: error
|
---|
414 | */
|
---|
415 | int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
|
---|
416 | const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
|
---|
417 | {
|
---|
418 | ECDSA_SIG *s;
|
---|
419 | const unsigned char *p = sigbuf;
|
---|
420 | unsigned char *der = NULL;
|
---|
421 | int derlen = -1;
|
---|
422 | int ret = -1;
|
---|
423 |
|
---|
424 | s = ECDSA_SIG_new();
|
---|
425 | if (s == NULL)
|
---|
426 | return ret;
|
---|
427 | if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
|
---|
428 | goto err;
|
---|
429 | /* Ensure signature uses DER and doesn't have trailing garbage */
|
---|
430 | derlen = i2d_ECDSA_SIG(s, &der);
|
---|
431 | if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
|
---|
432 | goto err;
|
---|
433 | ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
|
---|
434 | err:
|
---|
435 | OPENSSL_free(der);
|
---|
436 | ECDSA_SIG_free(s);
|
---|
437 | return ret;
|
---|
438 | }
|
---|
439 |
|
---|
440 | int ossl_ecdsa_simple_verify_sig(const unsigned char *dgst, int dgst_len,
|
---|
441 | const ECDSA_SIG *sig, EC_KEY *eckey)
|
---|
442 | {
|
---|
443 | int ret = -1, i;
|
---|
444 | BN_CTX *ctx;
|
---|
445 | const BIGNUM *order;
|
---|
446 | BIGNUM *u1, *u2, *m, *X;
|
---|
447 | EC_POINT *point = NULL;
|
---|
448 | const EC_GROUP *group;
|
---|
449 | const EC_POINT *pub_key;
|
---|
450 |
|
---|
451 | /* check input values */
|
---|
452 | if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
|
---|
453 | (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
|
---|
454 | ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
|
---|
455 | return -1;
|
---|
456 | }
|
---|
457 |
|
---|
458 | if (!EC_KEY_can_sign(eckey)) {
|
---|
459 | ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
|
---|
460 | return -1;
|
---|
461 | }
|
---|
462 |
|
---|
463 | ctx = BN_CTX_new_ex(eckey->libctx);
|
---|
464 | if (ctx == NULL) {
|
---|
465 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
466 | return -1;
|
---|
467 | }
|
---|
468 | BN_CTX_start(ctx);
|
---|
469 | u1 = BN_CTX_get(ctx);
|
---|
470 | u2 = BN_CTX_get(ctx);
|
---|
471 | m = BN_CTX_get(ctx);
|
---|
472 | X = BN_CTX_get(ctx);
|
---|
473 | if (X == NULL) {
|
---|
474 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
475 | goto err;
|
---|
476 | }
|
---|
477 |
|
---|
478 | order = EC_GROUP_get0_order(group);
|
---|
479 | if (order == NULL) {
|
---|
480 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
|
---|
481 | goto err;
|
---|
482 | }
|
---|
483 |
|
---|
484 | if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
|
---|
485 | BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
|
---|
486 | BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
|
---|
487 | ERR_raise(ERR_LIB_EC, EC_R_BAD_SIGNATURE);
|
---|
488 | ret = 0; /* signature is invalid */
|
---|
489 | goto err;
|
---|
490 | }
|
---|
491 | /* calculate tmp1 = inv(S) mod order */
|
---|
492 | if (!ossl_ec_group_do_inverse_ord(group, u2, sig->s, ctx)) {
|
---|
493 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
494 | goto err;
|
---|
495 | }
|
---|
496 | /* digest -> m */
|
---|
497 | i = BN_num_bits(order);
|
---|
498 | /*
|
---|
499 | * Need to truncate digest if it is too long: first truncate whole bytes.
|
---|
500 | */
|
---|
501 | if (8 * dgst_len > i)
|
---|
502 | dgst_len = (i + 7) / 8;
|
---|
503 | if (!BN_bin2bn(dgst, dgst_len, m)) {
|
---|
504 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
505 | goto err;
|
---|
506 | }
|
---|
507 | /* If still too long truncate remaining bits with a shift */
|
---|
508 | if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
|
---|
509 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
510 | goto err;
|
---|
511 | }
|
---|
512 | /* u1 = m * tmp mod order */
|
---|
513 | if (!BN_mod_mul(u1, m, u2, order, ctx)) {
|
---|
514 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
515 | goto err;
|
---|
516 | }
|
---|
517 | /* u2 = r * w mod q */
|
---|
518 | if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
|
---|
519 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
520 | goto err;
|
---|
521 | }
|
---|
522 |
|
---|
523 | if ((point = EC_POINT_new(group)) == NULL) {
|
---|
524 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
|
---|
525 | goto err;
|
---|
526 | }
|
---|
527 | if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
|
---|
528 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
|
---|
529 | goto err;
|
---|
530 | }
|
---|
531 |
|
---|
532 | if (!EC_POINT_get_affine_coordinates(group, point, X, NULL, ctx)) {
|
---|
533 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
|
---|
534 | goto err;
|
---|
535 | }
|
---|
536 |
|
---|
537 | if (!BN_nnmod(u1, X, order, ctx)) {
|
---|
538 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
|
---|
539 | goto err;
|
---|
540 | }
|
---|
541 | /* if the signature is correct u1 is equal to sig->r */
|
---|
542 | ret = (BN_ucmp(u1, sig->r) == 0);
|
---|
543 | err:
|
---|
544 | BN_CTX_end(ctx);
|
---|
545 | BN_CTX_free(ctx);
|
---|
546 | EC_POINT_free(point);
|
---|
547 | return ret;
|
---|
548 | }
|
---|