Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/dsa/dsa.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected])
2 // The DSS routines are based on patches supplied by Steven Schoch <[email protected]>.
3 // SPDX-License-Identifier: Apache-2.0
4
5 #include <openssl/dsa.h>
6
7 #include <string.h>
8
9 #include <openssl/bio.h>
10 #include <openssl/bn.h>
11 #include <openssl/dh.h>
12 #include <openssl/digest.h>
13 #include <openssl/engine.h>
14 #include <openssl/err.h>
15 #include <openssl/evp.h>
16 #include <openssl/ex_data.h>
17 #include <openssl/mem.h>
18 #include <openssl/rand.h>
19 #include <openssl/sha.h>
20 #include <openssl/thread.h>
21
22 #include "internal.h"
23 #include "../fipsmodule/bn/internal.h"
24 #include "../fipsmodule/dh/internal.h"
25 #include "../internal.h"
26
27
28 // Primality test according to FIPS PUB 186[-1], Appendix 2.1: 50 rounds of
29 // Miller-Rabin.
30 #define DSS_prime_checks 50
31
32 static int dsa_sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **out_kinv,
33 BIGNUM **out_r);
34
35 static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
36
37 DSA *DSA_new(void) {
38 DSA *dsa = OPENSSL_zalloc(sizeof(DSA));
39 if (dsa == NULL) {
40 return NULL;
41 }
42
43 dsa->references = 1;
44 CRYPTO_MUTEX_init(&dsa->method_mont_lock);
45 CRYPTO_new_ex_data(&dsa->ex_data);
46 return dsa;
47 }
48
49 void DSA_free(DSA *dsa) {
50 if (dsa == NULL) {
51 return;
52 }
53
54 if (!CRYPTO_refcount_dec_and_test_zero(&dsa->references)) {
55 return;
56 }
57
58 CRYPTO_free_ex_data(&g_ex_data_class, dsa, &dsa->ex_data);
59
60 BN_clear_free(dsa->p);
61 BN_clear_free(dsa->q);
62 BN_clear_free(dsa->g);
63 BN_clear_free(dsa->pub_key);
64 BN_clear_free(dsa->priv_key);
65 BN_MONT_CTX_free(dsa->method_mont_p);
66 BN_MONT_CTX_free(dsa->method_mont_q);
67 CRYPTO_MUTEX_cleanup(&dsa->method_mont_lock);
68 OPENSSL_free(dsa);
69 }
70
71 int DSA_print(BIO *bio, const DSA *dsa, int indent) {
72 EVP_PKEY *pkey = EVP_PKEY_new();
73 int ret = pkey != NULL &&
74 EVP_PKEY_set1_DSA(pkey, (DSA *)dsa) &&
75 EVP_PKEY_print_private(bio, pkey, indent, NULL);
76 EVP_PKEY_free(pkey);
77 return ret;
78 }
79
80
81 int DSA_print_fp(FILE *fp, const DSA *dsa, int indent) {
82 BIO *bio = BIO_new(BIO_s_file());
83 if (bio == NULL) {
84 OPENSSL_PUT_ERROR(RSA, ERR_R_BUF_LIB);
85 return 0;
86 }
87 BIO_set_fp(bio, fp, BIO_NOCLOSE);
88 int ret = DSA_print(bio, dsa, indent);
89 BIO_free(bio);
90 return ret;
91 }
92
93 int DSA_up_ref(DSA *dsa) {
94 CRYPTO_refcount_inc(&dsa->references);
95 return 1;
96 }
97
98 unsigned DSA_bits(const DSA *dsa) { return BN_num_bits(dsa->p); }
99
100 const BIGNUM *DSA_get0_pub_key(const DSA *dsa) { return dsa->pub_key; }
101
102 const BIGNUM *DSA_get0_priv_key(const DSA *dsa) { return dsa->priv_key; }
103
104 const BIGNUM *DSA_get0_p(const DSA *dsa) { return dsa->p; }
105
106 const BIGNUM *DSA_get0_q(const DSA *dsa) { return dsa->q; }
107
108 const BIGNUM *DSA_get0_g(const DSA *dsa) { return dsa->g; }
109
110 void DSA_get0_key(const DSA *dsa, const BIGNUM **out_pub_key,
111 const BIGNUM **out_priv_key) {
112 if (out_pub_key != NULL) {
113 *out_pub_key = dsa->pub_key;
114 }
115 if (out_priv_key != NULL) {
116 *out_priv_key = dsa->priv_key;
117 }
118 }
119
120 void DSA_get0_pqg(const DSA *dsa, const BIGNUM **out_p, const BIGNUM **out_q,
121 const BIGNUM **out_g) {
122 if (out_p != NULL) {
123 *out_p = dsa->p;
124 }
125 if (out_q != NULL) {
126 *out_q = dsa->q;
127 }
128 if (out_g != NULL) {
129 *out_g = dsa->g;
130 }
131 }
132
133 int DSA_set0_key(DSA *dsa, BIGNUM *pub_key, BIGNUM *priv_key) {
134 if (dsa->pub_key == NULL && pub_key == NULL) {
135 return 0;
136 }
137
138 if (pub_key != NULL) {
139 BN_free(dsa->pub_key);
140 dsa->pub_key = pub_key;
141 }
142 if (priv_key != NULL) {
143 BN_free(dsa->priv_key);
144 dsa->priv_key = priv_key;
145 }
146
147 return 1;
148 }
149
150 int DSA_set0_pqg(DSA *dsa, BIGNUM *p, BIGNUM *q, BIGNUM *g) {
151 if ((dsa->p == NULL && p == NULL) ||
152 (dsa->q == NULL && q == NULL) ||
153 (dsa->g == NULL && g == NULL)) {
154 return 0;
155 }
156
157 if (p != NULL) {
158 BN_free(dsa->p);
159 dsa->p = p;
160 }
161 if (q != NULL) {
162 BN_free(dsa->q);
163 dsa->q = q;
164 }
165 if (g != NULL) {
166 BN_free(dsa->g);
167 dsa->g = g;
168 }
169
170 BN_MONT_CTX_free(dsa->method_mont_p);
171 dsa->method_mont_p = NULL;
172 BN_MONT_CTX_free(dsa->method_mont_q);
173 dsa->method_mont_q = NULL;
174 return 1;
175 }
176
177 int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, const uint8_t *seed_in,
178 size_t seed_len, int *out_counter,
179 unsigned long *out_h, BN_GENCB *cb) {
180 const EVP_MD *evpmd = (bits >= 2048) ? EVP_sha256() : EVP_sha1();
181 return dsa_internal_paramgen(dsa, bits, evpmd, seed_in, seed_len, out_counter, out_h, cb);
182 }
183
184 int dsa_internal_paramgen(DSA *dsa, size_t bits, const EVP_MD *evpmd,
185 const unsigned char *seed_in, size_t seed_len,
186 int *out_counter, unsigned long *out_h,
187 BN_GENCB *cb)
188 {
189 if (bits > OPENSSL_DSA_MAX_MODULUS_BITS) {
190 OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS);
191 return 0;
192 }
193
194 int ok = 0;
195 unsigned char seed[SHA256_DIGEST_LENGTH];
196 unsigned char md[SHA256_DIGEST_LENGTH];
197 unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH];
198 BIGNUM *r0, *W, *X, *c, *test;
199 BIGNUM *g = NULL, *q = NULL, *p = NULL;
200 BN_MONT_CTX *mont = NULL;
201 int k, n = 0, m = 0;
202 int counter = 0;
203 int r = 0;
204 BN_CTX *ctx = NULL;
205 unsigned int h = 2;
206 const size_t qsize = EVP_MD_size(evpmd);
207
208 if (bits < 512) {
209 bits = 512;
210 }
211
212 bits = (bits + 63) / 64 * 64;
213
214 if (seed_in != NULL) {
215 if (seed_len < qsize) {
216 return 0;
217 }
218 if (seed_len > qsize) {
219 // Only consume as much seed as is expected.
220 seed_len = qsize;
221 }
222 OPENSSL_memcpy(seed, seed_in, seed_len);
223 }
224
225 ctx = BN_CTX_new();
226 if (ctx == NULL) {
227 goto err;
228 }
229 BN_CTX_start(ctx);
230
231 r0 = BN_CTX_get(ctx);
232 g = BN_CTX_get(ctx);
233 W = BN_CTX_get(ctx);
234 q = BN_CTX_get(ctx);
235 X = BN_CTX_get(ctx);
236 c = BN_CTX_get(ctx);
237 p = BN_CTX_get(ctx);
238 test = BN_CTX_get(ctx);
239
240 if (test == NULL || !BN_lshift(test, BN_value_one(), bits - 1)) {
241 goto err;
242 }
243
244 for (;;) {
245 // Find q.
246 for (;;) {
247 // step 1
248 if (!BN_GENCB_call(cb, BN_GENCB_GENERATED, m++)) {
249 goto err;
250 }
251
252 int use_random_seed = (seed_in == NULL);
253 if (use_random_seed) {
254 AWSLC_ABORT_IF_NOT_ONE(RAND_bytes(seed, qsize));
255 // DSA parameters are public.
256 CONSTTIME_DECLASSIFY(seed, qsize);
257 } else {
258 // If we come back through, use random seed next time.
259 seed_in = NULL;
260 }
261 OPENSSL_memcpy(buf, seed, qsize);
262 OPENSSL_memcpy(buf2, seed, qsize);
263 // precompute "SEED + 1" for step 7:
264 for (size_t i = qsize - 1; i < qsize; i--) {
265 buf[i]++;
266 if (buf[i] != 0) {
267 break;
268 }
269 }
270
271 // step 2
272 if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL) ||
273 !EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL)) {
274 goto err;
275 }
276 for (size_t i = 0; i < qsize; i++) {
277 md[i] ^= buf2[i];
278 }
279
280 // step 3
281 md[0] |= 0x80;
282 md[qsize - 1] |= 0x01;
283 if (!BN_bin2bn(md, qsize, q)) {
284 goto err;
285 }
286
287 // step 4
288 r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, use_random_seed, cb);
289 if (r > 0) {
290 break;
291 }
292 if (r != 0) {
293 goto err;
294 }
295
296 // do a callback call
297 // step 5
298 }
299
300 if (!BN_GENCB_call(cb, 2, 0) || !BN_GENCB_call(cb, 3, 0)) {
301 goto err;
302 }
303
304 // step 6
305 counter = 0;
306 // "offset = 2"
307
308 n = (bits - 1) / 160;
309
310 for (;;) {
311 if ((counter != 0) && !BN_GENCB_call(cb, BN_GENCB_GENERATED, counter)) {
312 goto err;
313 }
314
315 // step 7
316 BN_zero(W);
317 // now 'buf' contains "SEED + offset - 1"
318 for (k = 0; k <= n; k++) {
319 // obtain "SEED + offset + k" by incrementing:
320 for (size_t i = qsize - 1; i < qsize; i--) {
321 buf[i]++;
322 if (buf[i] != 0) {
323 break;
324 }
325 }
326
327 if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL)) {
328 goto err;
329 }
330
331 // step 8
332 if (!BN_bin2bn(md, qsize, r0) ||
333 !BN_lshift(r0, r0, (qsize << 3) * k) ||
334 !BN_add(W, W, r0)) {
335 goto err;
336 }
337 }
338
339 // more of step 8
340 if (!BN_mask_bits(W, bits - 1) ||
341 !BN_copy(X, W) ||
342 !BN_add(X, X, test)) {
343 goto err;
344 }
345
346 // step 9
347 if (!BN_lshift1(r0, q) ||
348 !BN_mod(c, X, r0, ctx) ||
349 !BN_sub(r0, c, BN_value_one()) ||
350 !BN_sub(p, X, r0)) {
351 goto err;
352 }
353
354 // step 10
355 if (BN_cmp(p, test) >= 0) {
356 // step 11
357 r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
358 if (r > 0) {
359 goto end; // found it
360 }
361 if (r != 0) {
362 goto err;
363 }
364 }
365
366 // step 13
367 counter++;
368 // "offset = offset + n + 1"
369
370 // step 14
371 if (counter >= 4096) {
372 break;
373 }
374 }
375 }
376 end:
377 if (!BN_GENCB_call(cb, 2, 1)) {
378 goto err;
379 }
380
381 // We now need to generate g
382 // Set r0=(p-1)/q
383 if (!BN_sub(test, p, BN_value_one()) ||
384 !BN_div(r0, NULL, test, q, ctx)) {
385 goto err;
386 }
387
388 mont = BN_MONT_CTX_new_for_modulus(p, ctx);
389 if (mont == NULL ||
390 !BN_set_word(test, h)) {
391 goto err;
392 }
393
394 for (;;) {
395 // g=test^r0%p
396 if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont)) {
397 goto err;
398 }
399 if (!BN_is_one(g)) {
400 break;
401 }
402 if (!BN_add(test, test, BN_value_one())) {
403 goto err;
404 }
405 h++;
406 }
407
408 if (!BN_GENCB_call(cb, 3, 1)) {
409 goto err;
410 }
411
412 ok = 1;
413
414 err:
415 if (ok) {
416 BN_free(dsa->p);
417 BN_free(dsa->q);
418 BN_free(dsa->g);
419 dsa->p = BN_dup(p);
420 dsa->q = BN_dup(q);
421 dsa->g = BN_dup(g);
422 if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
423 ok = 0;
424 goto err;
425 }
426 if (out_counter != NULL) {
427 *out_counter = counter;
428 }
429 if (out_h != NULL) {
430 *out_h = h;
431 }
432 }
433
434 if (ctx) {
435 BN_CTX_end(ctx);
436 BN_CTX_free(ctx);
437 }
438
439 BN_MONT_CTX_free(mont);
440 OPENSSL_cleanse(seed, SHA256_DIGEST_LENGTH);
441
442 return ok;
443 }
444
445 DSA *DSAparams_dup(const DSA *dsa) {
446 DSA *ret = DSA_new();
447 if (ret == NULL) {
448 return NULL;
449 }
450 ret->p = BN_dup(dsa->p);
451 ret->q = BN_dup(dsa->q);
452 ret->g = BN_dup(dsa->g);
453 if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
454 DSA_free(ret);
455 return NULL;
456 }
457 return ret;
458 }
459
460 int DSA_generate_key(DSA *dsa) {
461 if (!dsa_check_key(dsa)) {
462 return 0;
463 }
464
465 int ok = 0;
466 BIGNUM *pub_key = NULL, *priv_key = NULL;
467 BN_CTX *ctx = BN_CTX_new();
468 if (ctx == NULL) {
469 goto err;
470 }
471
472 priv_key = dsa->priv_key;
473 if (priv_key == NULL) {
474 priv_key = BN_new();
475 if (priv_key == NULL) {
476 goto err;
477 }
478 }
479
480 if (!BN_rand_range_ex(priv_key, 1, dsa->q)) {
481 goto err;
482 }
483
484 pub_key = dsa->pub_key;
485 if (pub_key == NULL) {
486 pub_key = BN_new();
487 if (pub_key == NULL) {
488 goto err;
489 }
490 }
491
492 if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p, &dsa->method_mont_lock,
493 dsa->p, ctx) ||
494 !BN_mod_exp_mont_consttime(pub_key, dsa->g, priv_key, dsa->p, ctx,
495 dsa->method_mont_p)) {
496 goto err;
497 }
498
499 // The public key is computed from the private key, but is public.
500 bn_declassify(pub_key);
501
502 dsa->priv_key = priv_key;
503 dsa->pub_key = pub_key;
504 ok = 1;
505
506 err:
507 if (dsa->pub_key == NULL) {
508 BN_free(pub_key);
509 }
510 if (dsa->priv_key == NULL) {
511 BN_free(priv_key);
512 }
513 BN_CTX_free(ctx);
514
515 return ok;
516 }
517
518 DSA_SIG *DSA_SIG_new(void) { return OPENSSL_zalloc(sizeof(DSA_SIG)); }
519
520 void DSA_SIG_free(DSA_SIG *sig) {
521 if (!sig) {
522 return;
523 }
524
525 BN_free(sig->r);
526 BN_free(sig->s);
527 OPENSSL_free(sig);
528 }
529
530 void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **out_r,
531 const BIGNUM **out_s) {
532 if (out_r != NULL) {
533 *out_r = sig->r;
534 }
535 if (out_s != NULL) {
536 *out_s = sig->s;
537 }
538 }
539
540 int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s) {
541 if (r == NULL || s == NULL) {
542 return 0;
543 }
544 BN_free(sig->r);
545 BN_free(sig->s);
546 sig->r = r;
547 sig->s = s;
548 return 1;
549 }
550
551 // mod_mul_consttime sets |r| to |a| * |b| modulo |mont->N|, treating |a| and
552 // |b| as secret. This function internally uses Montgomery reduction, but
553 // neither inputs nor outputs are in Montgomery form.
554 static int mod_mul_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
555 const BN_MONT_CTX *mont, BN_CTX *ctx) {
556 BN_CTX_start(ctx);
557 BIGNUM *tmp = BN_CTX_get(ctx);
558 // |BN_mod_mul_montgomery| removes a factor of R, so we cancel it with a
559 // single |BN_to_montgomery| which adds one factor of R.
560 int ok = tmp != NULL &&
561 BN_to_montgomery(tmp, a, mont, ctx) &&
562 BN_mod_mul_montgomery(r, tmp, b, mont, ctx);
563 BN_CTX_end(ctx);
564 return ok;
565 }
566
567 DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, const DSA *dsa) {
568 if (!dsa_check_key(dsa)) {
569 return NULL;
570 }
571
572 if (dsa->priv_key == NULL) {
573 OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
574 return NULL;
575 }
576
577 BIGNUM *kinv = NULL, *r = NULL, *s = NULL;
578 BIGNUM m;
579 BIGNUM xr;
580 BN_CTX *ctx = NULL;
581 DSA_SIG *ret = NULL;
582
583 BN_init(&m);
584 BN_init(&xr);
585 s = BN_new();
586 if (s == NULL) {
587 goto err;
588 }
589 ctx = BN_CTX_new();
590 if (ctx == NULL) {
591 goto err;
592 }
593
594 // Cap iterations so that invalid parameters do not infinite loop. This does
595 // not impact valid parameters because the probability of requiring even one
596 // retry is negligible, let alone 32. Unfortunately, DSA was mis-specified, so
597 // invalid parameters are reachable from most callers handling untrusted
598 // private keys. (The |dsa_check_key| call above is not sufficient. Checking
599 // whether arbitrary paremeters form a valid DSA group is expensive.)
600 static const int kMaxIterations = 32;
601 int iters = 0;
602 redo:
603 if (!dsa_sign_setup(dsa, ctx, &kinv, &r)) {
604 goto err;
605 }
606
607 if (digest_len > BN_num_bytes(dsa->q)) {
608 // If the digest length is greater than the size of |dsa->q| use the
609 // BN_num_bits(dsa->q) leftmost bits of the digest, see FIPS 186-3, 4.2.
610 // Note the above check that |dsa->q| is a multiple of 8 bits.
611 digest_len = BN_num_bytes(dsa->q);
612 }
613
614 if (BN_bin2bn(digest, digest_len, &m) == NULL) {
615 goto err;
616 }
617
618 // |m| is bounded by 2^(num_bits(q)), which is slightly looser than q. This
619 // violates |bn_mod_add_consttime| and |mod_mul_consttime|'s preconditions.
620 // (The underlying algorithms could accept looser bounds, but we reduce for
621 // simplicity.)
622 size_t q_width = bn_minimal_width(dsa->q);
623 if (!bn_resize_words(&m, q_width) ||
624 !bn_resize_words(&xr, q_width)) {
625 goto err;
626 }
627 bn_reduce_once_in_place(m.d, 0 /* no carry word */, dsa->q->d,
628 xr.d /* scratch space */, q_width);
629
630 // Compute s = inv(k) (m + xr) mod q. Note |dsa->method_mont_q| is
631 // initialized by |dsa_sign_setup|.
632 if (!mod_mul_consttime(&xr, dsa->priv_key, r, dsa->method_mont_q, ctx) ||
633 !bn_mod_add_consttime(s, &xr, &m, dsa->q, ctx) ||
634 !mod_mul_consttime(s, s, kinv, dsa->method_mont_q, ctx)) {
635 goto err;
636 }
637
638 // The signature is computed from the private key, but is public.
639 bn_declassify(r);
640 bn_declassify(s);
641
642 // Redo if r or s is zero as required by FIPS 186-3: this is
643 // very unlikely.
644 if (BN_is_zero(r) || BN_is_zero(s)) {
645 iters++;
646 if (iters > kMaxIterations) {
647 OPENSSL_PUT_ERROR(DSA, DSA_R_TOO_MANY_ITERATIONS);
648 goto err;
649 }
650 goto redo;
651 }
652
653 ret = DSA_SIG_new();
654 if (ret == NULL) {
655 goto err;
656 }
657 ret->r = r;
658 ret->s = s;
659
660 err:
661 if (ret == NULL) {
662 OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
663 BN_free(r);
664 BN_free(s);
665 }
666 BN_CTX_free(ctx);
667 BN_clear_free(&m);
668 BN_clear_free(&xr);
669 BN_clear_free(kinv);
670
671 return ret;
672 }
673
674 int DSA_do_verify(const uint8_t *digest, size_t digest_len, const DSA_SIG *sig,
675 const DSA *dsa) {
676 int valid;
677 if (!DSA_do_check_signature(&valid, digest, digest_len, sig, dsa)) {
678 return -1;
679 }
680 return valid;
681 }
682
683 int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
684 size_t digest_len, const DSA_SIG *sig,
685 const DSA *dsa) {
686 *out_valid = 0;
687 if (!dsa_check_key(dsa)) {
688 return 0;
689 }
690
691 if (dsa->pub_key == NULL) {
692 OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
693 return 0;
694 }
695
696 int ret = 0;
697 BIGNUM u1, u2, t1;
698 BN_init(&u1);
699 BN_init(&u2);
700 BN_init(&t1);
701 BN_CTX *ctx = BN_CTX_new();
702 if (ctx == NULL) {
703 goto err;
704 }
705
706 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
707 BN_ucmp(sig->r, dsa->q) >= 0) {
708 ret = 1;
709 goto err;
710 }
711 if (BN_is_zero(sig->s) || BN_is_negative(sig->s) ||
712 BN_ucmp(sig->s, dsa->q) >= 0) {
713 ret = 1;
714 goto err;
715 }
716
717 // Calculate W = inv(S) mod Q
718 // save W in u2
719 if (BN_mod_inverse(&u2, sig->s, dsa->q, ctx) == NULL) {
720 goto err;
721 }
722
723 // save M in u1
724 unsigned q_bits = BN_num_bits(dsa->q);
725 if (digest_len > (q_bits >> 3)) {
726 // if the digest length is greater than the size of q use the
727 // BN_num_bits(dsa->q) leftmost bits of the digest, see
728 // fips 186-3, 4.2
729 digest_len = (q_bits >> 3);
730 }
731
732 if (BN_bin2bn(digest, digest_len, &u1) == NULL) {
733 goto err;
734 }
735
736 // u1 = M * w mod q
737 if (!BN_mod_mul(&u1, &u1, &u2, dsa->q, ctx)) {
738 goto err;
739 }
740
741 // u2 = r * w mod q
742 if (!BN_mod_mul(&u2, sig->r, &u2, dsa->q, ctx)) {
743 goto err;
744 }
745
746 if (!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
747 (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p,
748 ctx)) {
749 goto err;
750 }
751
752 if (!BN_mod_exp2_mont(&t1, dsa->g, &u1, dsa->pub_key, &u2, dsa->p, ctx,
753 dsa->method_mont_p)) {
754 goto err;
755 }
756
757 // BN_copy(&u1,&t1);
758 // let u1 = u1 mod q
759 if (!BN_mod(&u1, &t1, dsa->q, ctx)) {
760 goto err;
761 }
762
763 // V is now in u1. If the signature is correct, it will be
764 // equal to R.
765 *out_valid = BN_ucmp(&u1, sig->r) == 0;
766 ret = 1;
767
768 err:
769 if (ret != 1) {
770 OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
771 }
772 BN_CTX_free(ctx);
773 BN_free(&u1);
774 BN_free(&u2);
775 BN_free(&t1);
776
777 return ret;
778 }
779
780 int DSA_sign(int type, const uint8_t *digest, size_t digest_len,
781 uint8_t *out_sig, unsigned int *out_siglen, const DSA *dsa) {
782 DSA_SIG *s;
783
784 s = DSA_do_sign(digest, digest_len, dsa);
785 if (s == NULL) {
786 *out_siglen = 0;
787 return 0;
788 }
789
790 *out_siglen = i2d_DSA_SIG(s, &out_sig);
791 DSA_SIG_free(s);
792 return 1;
793 }
794
795 int DSA_verify(int type, const uint8_t *digest, size_t digest_len,
796 const uint8_t *sig, size_t sig_len, const DSA *dsa) {
797 int valid;
798 if (!DSA_check_signature(&valid, digest, digest_len, sig, sig_len, dsa)) {
799 return -1;
800 }
801 return valid;
802 }
803
804 int DSA_check_signature(int *out_valid, const uint8_t *digest,
805 size_t digest_len, const uint8_t *sig, size_t sig_len,
806 const DSA *dsa) {
807 DSA_SIG *s = NULL;
808 int ret = 0;
809 uint8_t *der = NULL;
810
811 s = DSA_SIG_new();
812 if (s == NULL) {
813 goto err;
814 }
815
816 const uint8_t *sigp = sig;
817 if (d2i_DSA_SIG(&s, &sigp, sig_len) == NULL || sigp != sig + sig_len) {
818 goto err;
819 }
820
821 // Ensure that the signature uses DER and doesn't have trailing garbage.
822 int der_len = i2d_DSA_SIG(s, &der);
823 if (der_len < 0 || (size_t)der_len != sig_len ||
824 OPENSSL_memcmp(sig, der, sig_len)) {
825 goto err;
826 }
827
828 ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa);
829
830 err:
831 OPENSSL_free(der);
832 DSA_SIG_free(s);
833 return ret;
834 }
835
836 // der_len_len returns the number of bytes needed to represent a length of |len|
837 // in DER.
838 static size_t der_len_len(size_t len) {
839 if (len < 0x80) {
840 return 1;
841 }
842 size_t ret = 1;
843 while (len > 0) {
844 ret++;
845 len >>= 8;
846 }
847 return ret;
848 }
849
850 int DSA_size(const DSA *dsa) {
851 if (dsa->q == NULL) {
852 return 0;
853 }
854
855 size_t order_len = BN_num_bytes(dsa->q);
856 // Compute the maximum length of an |order_len| byte integer. Defensively
857 // assume that the leading 0x00 is included.
858 size_t integer_len = 1 /* tag */ + der_len_len(order_len + 1) + 1 + order_len;
859 if (integer_len < order_len) {
860 return 0;
861 }
862 // A DSA signature is two INTEGERs.
863 size_t value_len = 2 * integer_len;
864 if (value_len < integer_len) {
865 return 0;
866 }
867 // Add the header.
868 size_t ret = 1 /* tag */ + der_len_len(value_len) + value_len;
869 if (ret < value_len) {
870 return 0;
871 }
872 return ret;
873 }
874
875 static int dsa_sign_setup(const DSA *dsa, BN_CTX *ctx, BIGNUM **out_kinv,
876 BIGNUM **out_r) {
877 int ret = 0;
878 BIGNUM k;
879 BN_init(&k);
880 BIGNUM *r = BN_new();
881 BIGNUM *kinv = BN_new();
882 if (r == NULL || kinv == NULL ||
883 // Get random k
884 !BN_rand_range_ex(&k, 1, dsa->q) ||
885 !BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
886 (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p,
887 ctx) ||
888 !BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_q,
889 (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->q,
890 ctx) ||
891 // Compute r = (g^k mod p) mod q
892 !BN_mod_exp_mont_consttime(r, dsa->g, &k, dsa->p, ctx,
893 dsa->method_mont_p)) {
894 OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
895 goto err;
896 }
897 // Note |BN_mod| below is not constant-time and may leak information about
898 // |r|. |dsa->p| may be significantly larger than |dsa->q|, so this is not
899 // easily performed in constant-time with Montgomery reduction.
900 //
901 // However, |r| at this point is g^k (mod p). It is almost the value of |r|
902 // revealed in the signature anyway (g^k (mod p) (mod q)), going from it to
903 // |k| would require computing a discrete log.
904 bn_declassify(r);
905 if (!BN_mod(r, r, dsa->q, ctx) ||
906 // Compute part of 's = inv(k) (m + xr) mod q' using Fermat's Little
907 // Theorem.
908 !bn_mod_inverse_prime(kinv, &k, dsa->q, ctx, dsa->method_mont_q)) {
909 OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
910 goto err;
911 }
912
913 BN_clear_free(*out_kinv);
914 *out_kinv = kinv;
915 kinv = NULL;
916
917 BN_clear_free(*out_r);
918 *out_r = r;
919 r = NULL;
920
921 ret = 1;
922
923 err:
924 BN_clear_free(&k);
925 BN_clear_free(r);
926 BN_clear_free(kinv);
927 return ret;
928 }
929
930 int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
931 CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
932 int index;
933 if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
934 free_func)) {
935 return -1;
936 }
937 return index;
938 }
939
940 int DSA_set_ex_data(DSA *dsa, int idx, void *arg) {
941 return CRYPTO_set_ex_data(&dsa->ex_data, idx, arg);
942 }
943
944 void *DSA_get_ex_data(const DSA *dsa, int idx) {
945 return CRYPTO_get_ex_data(&dsa->ex_data, idx);
946 }
947
948 DH *DSA_dup_DH(const DSA *dsa) {
949 if (dsa == NULL) {
950 return NULL;
951 }
952
953 DH *ret = DH_new();
954 if (ret == NULL) {
955 goto err;
956 }
957 if (dsa->q != NULL) {
958 ret->priv_length = BN_num_bits(dsa->q);
959 if ((ret->q = BN_dup(dsa->q)) == NULL) {
960 goto err;
961 }
962 }
963 if ((dsa->p != NULL && (ret->p = BN_dup(dsa->p)) == NULL) ||
964 (dsa->g != NULL && (ret->g = BN_dup(dsa->g)) == NULL) ||
965 (dsa->pub_key != NULL && (ret->pub_key = BN_dup(dsa->pub_key)) == NULL) ||
966 (dsa->priv_key != NULL &&
967 (ret->priv_key = BN_dup(dsa->priv_key)) == NULL)) {
968 goto err;
969 }
970
971 return ret;
972
973 err:
974 DH_free(ret);
975 return NULL;
976 }