blob: 5e1f81abc41a72fe71868471f8c876f4bdd478b9 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 *
57 * The DSS routines are based on patches supplied by
58 * Steven Schoch <schoch@sheba.arc.nasa.gov>. */
59
60#include <openssl/dsa.h>
61
Adam Langley2b2d66d2015-01-30 17:08:37 -080062#include <string.h>
63
David Benjamine8f783a2015-10-30 16:53:00 -040064#include <openssl/bn.h>
Adam Langleybed8ce72014-09-05 17:04:51 -070065#include <openssl/dh.h>
David Benjamine8f783a2015-10-30 16:53:00 -040066#include <openssl/digest.h>
Adam Langley95c29f32014-06-20 12:00:00 -070067#include <openssl/engine.h>
68#include <openssl/err.h>
69#include <openssl/ex_data.h>
70#include <openssl/mem.h>
David Benjamine8f783a2015-10-30 16:53:00 -040071#include <openssl/rand.h>
72#include <openssl/sha.h>
Brian Smith054e6822015-03-27 21:12:01 -100073#include <openssl/thread.h>
Adam Langley95c29f32014-06-20 12:00:00 -070074
Adam Langley683d7bd2015-04-13 11:04:14 -070075#include "../internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070076
Adam Langley2b2d66d2015-01-30 17:08:37 -080077
David Benjamine8f783a2015-10-30 16:53:00 -040078#define OPENSSL_DSA_MAX_MODULUS_BITS 10000
79
80/* Primality test according to FIPS PUB 186[-1], Appendix 2.1: 50 rounds of
81 * Rabin-Miller */
82#define DSS_prime_checks 50
Adam Langley95c29f32014-06-20 12:00:00 -070083
David Benjamin9f33fc62015-04-15 17:29:53 -040084static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
85
David Benjamine8f783a2015-10-30 16:53:00 -040086DSA *DSA_new(void) {
Brian Smith5ba06892016-02-07 09:36:04 -100087 DSA *dsa = OPENSSL_malloc(sizeof(DSA));
Adam Langley95c29f32014-06-20 12:00:00 -070088 if (dsa == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -040089 OPENSSL_PUT_ERROR(DSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -070090 return NULL;
91 }
92
93 memset(dsa, 0, sizeof(DSA));
94
Adam Langley95c29f32014-06-20 12:00:00 -070095 dsa->references = 1;
96
David Benjamin99c752a2016-06-16 15:13:43 -040097 CRYPTO_MUTEX_init(&dsa->method_mont_lock);
David Benjamin8a589332015-12-04 23:14:35 -050098 CRYPTO_new_ex_data(&dsa->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -070099
100 return dsa;
101}
102
103void DSA_free(DSA *dsa) {
104 if (dsa == NULL) {
105 return;
106 }
107
Adam Langley0da323a2015-05-15 12:49:30 -0700108 if (!CRYPTO_refcount_dec_and_test_zero(&dsa->references)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700109 return;
110 }
111
David Benjamin9f33fc62015-04-15 17:29:53 -0400112 CRYPTO_free_ex_data(&g_ex_data_class, dsa, &dsa->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700113
David Benjamin22ccc2d2015-04-22 13:50:28 -0400114 BN_clear_free(dsa->p);
115 BN_clear_free(dsa->q);
116 BN_clear_free(dsa->g);
117 BN_clear_free(dsa->pub_key);
118 BN_clear_free(dsa->priv_key);
119 BN_clear_free(dsa->kinv);
120 BN_clear_free(dsa->r);
David Benjamine8f783a2015-10-30 16:53:00 -0400121 BN_MONT_CTX_free(dsa->method_mont_p);
David Benjamin99c752a2016-06-16 15:13:43 -0400122 BN_MONT_CTX_free(dsa->method_mont_q);
123 CRYPTO_MUTEX_cleanup(&dsa->method_mont_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700124 OPENSSL_free(dsa);
125}
126
127int DSA_up_ref(DSA *dsa) {
Adam Langley0da323a2015-05-15 12:49:30 -0700128 CRYPTO_refcount_inc(&dsa->references);
Adam Langley95c29f32014-06-20 12:00:00 -0700129 return 1;
130}
131
132int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, const uint8_t *seed_in,
133 size_t seed_len, int *out_counter,
134 unsigned long *out_h, BN_GENCB *cb) {
David Benjamine8f783a2015-10-30 16:53:00 -0400135 int ok = 0;
136 unsigned char seed[SHA256_DIGEST_LENGTH];
137 unsigned char md[SHA256_DIGEST_LENGTH];
138 unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH];
139 BIGNUM *r0, *W, *X, *c, *test;
140 BIGNUM *g = NULL, *q = NULL, *p = NULL;
141 BN_MONT_CTX *mont = NULL;
142 int k, n = 0, m = 0;
143 unsigned i;
144 int counter = 0;
145 int r = 0;
146 BN_CTX *ctx = NULL;
147 unsigned int h = 2;
148 unsigned qsize;
149 const EVP_MD *evpmd;
150
151 evpmd = (bits >= 2048) ? EVP_sha256() : EVP_sha1();
152 qsize = EVP_MD_size(evpmd);
153
154 if (bits < 512) {
155 bits = 512;
Adam Langley95c29f32014-06-20 12:00:00 -0700156 }
David Benjamine8f783a2015-10-30 16:53:00 -0400157
158 bits = (bits + 63) / 64 * 64;
159
160 if (seed_in != NULL) {
161 if (seed_len < (size_t)qsize) {
162 return 0;
163 }
164 if (seed_len > (size_t)qsize) {
165 /* Only consume as much seed as is expected. */
166 seed_len = qsize;
167 }
168 memcpy(seed, seed_in, seed_len);
169 }
170
171 ctx = BN_CTX_new();
172 if (ctx == NULL) {
173 goto err;
174 }
175 BN_CTX_start(ctx);
176
177 mont = BN_MONT_CTX_new();
178 if (mont == NULL) {
179 goto err;
180 }
181
182 r0 = BN_CTX_get(ctx);
183 g = BN_CTX_get(ctx);
184 W = BN_CTX_get(ctx);
185 q = BN_CTX_get(ctx);
186 X = BN_CTX_get(ctx);
187 c = BN_CTX_get(ctx);
188 p = BN_CTX_get(ctx);
189 test = BN_CTX_get(ctx);
190
191 if (test == NULL || !BN_lshift(test, BN_value_one(), bits - 1)) {
192 goto err;
193 }
194
195 for (;;) {
196 /* Find q. */
197 for (;;) {
198 /* step 1 */
199 if (!BN_GENCB_call(cb, 0, m++)) {
200 goto err;
201 }
202
203 int use_random_seed = (seed_in == NULL);
204 if (use_random_seed) {
205 if (!RAND_bytes(seed, qsize)) {
206 goto err;
207 }
208 } else {
209 /* If we come back through, use random seed next time. */
210 seed_in = NULL;
211 }
212 memcpy(buf, seed, qsize);
213 memcpy(buf2, seed, qsize);
214 /* precompute "SEED + 1" for step 7: */
215 for (i = qsize - 1; i < qsize; i--) {
216 buf[i]++;
217 if (buf[i] != 0) {
218 break;
219 }
220 }
221
222 /* step 2 */
223 if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL) ||
224 !EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL)) {
225 goto err;
226 }
227 for (i = 0; i < qsize; i++) {
228 md[i] ^= buf2[i];
229 }
230
231 /* step 3 */
232 md[0] |= 0x80;
233 md[qsize - 1] |= 0x01;
234 if (!BN_bin2bn(md, qsize, q)) {
235 goto err;
236 }
237
238 /* step 4 */
239 r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, use_random_seed, cb);
240 if (r > 0) {
241 break;
242 }
243 if (r != 0) {
244 goto err;
245 }
246
247 /* do a callback call */
248 /* step 5 */
249 }
250
251 if (!BN_GENCB_call(cb, 2, 0) || !BN_GENCB_call(cb, 3, 0)) {
252 goto err;
253 }
254
255 /* step 6 */
256 counter = 0;
257 /* "offset = 2" */
258
259 n = (bits - 1) / 160;
260
261 for (;;) {
262 if ((counter != 0) && !BN_GENCB_call(cb, 0, counter)) {
263 goto err;
264 }
265
266 /* step 7 */
267 BN_zero(W);
268 /* now 'buf' contains "SEED + offset - 1" */
269 for (k = 0; k <= n; k++) {
270 /* obtain "SEED + offset + k" by incrementing: */
271 for (i = qsize - 1; i < qsize; i--) {
272 buf[i]++;
273 if (buf[i] != 0) {
274 break;
275 }
276 }
277
278 if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL)) {
279 goto err;
280 }
281
282 /* step 8 */
283 if (!BN_bin2bn(md, qsize, r0) ||
284 !BN_lshift(r0, r0, (qsize << 3) * k) ||
285 !BN_add(W, W, r0)) {
286 goto err;
287 }
288 }
289
290 /* more of step 8 */
291 if (!BN_mask_bits(W, bits - 1) ||
292 !BN_copy(X, W) ||
293 !BN_add(X, X, test)) {
294 goto err;
295 }
296
297 /* step 9 */
298 if (!BN_lshift1(r0, q) ||
299 !BN_mod(c, X, r0, ctx) ||
300 !BN_sub(r0, c, BN_value_one()) ||
301 !BN_sub(p, X, r0)) {
302 goto err;
303 }
304
305 /* step 10 */
306 if (BN_cmp(p, test) >= 0) {
307 /* step 11 */
308 r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
309 if (r > 0) {
310 goto end; /* found it */
311 }
312 if (r != 0) {
313 goto err;
314 }
315 }
316
317 /* step 13 */
318 counter++;
319 /* "offset = offset + n + 1" */
320
321 /* step 14 */
322 if (counter >= 4096) {
323 break;
324 }
325 }
326 }
327end:
328 if (!BN_GENCB_call(cb, 2, 1)) {
329 goto err;
330 }
331
332 /* We now need to generate g */
333 /* Set r0=(p-1)/q */
334 if (!BN_sub(test, p, BN_value_one()) ||
335 !BN_div(r0, NULL, test, q, ctx)) {
336 goto err;
337 }
338
339 if (!BN_set_word(test, h) ||
340 !BN_MONT_CTX_set(mont, p, ctx)) {
341 goto err;
342 }
343
344 for (;;) {
345 /* g=test^r0%p */
346 if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont)) {
347 goto err;
348 }
349 if (!BN_is_one(g)) {
350 break;
351 }
352 if (!BN_add(test, test, BN_value_one())) {
353 goto err;
354 }
355 h++;
356 }
357
358 if (!BN_GENCB_call(cb, 3, 1)) {
359 goto err;
360 }
361
362 ok = 1;
363
364err:
365 if (ok) {
366 BN_free(dsa->p);
367 BN_free(dsa->q);
368 BN_free(dsa->g);
369 dsa->p = BN_dup(p);
370 dsa->q = BN_dup(q);
371 dsa->g = BN_dup(g);
372 if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
373 ok = 0;
374 goto err;
375 }
376 if (out_counter != NULL) {
377 *out_counter = counter;
378 }
379 if (out_h != NULL) {
380 *out_h = h;
381 }
382 }
383
384 if (ctx) {
385 BN_CTX_end(ctx);
386 BN_CTX_free(ctx);
387 }
388
389 BN_MONT_CTX_free(mont);
390
391 return ok;
Adam Langley95c29f32014-06-20 12:00:00 -0700392}
393
David Benjaminfda22a72016-01-30 01:51:01 -0500394DSA *DSAparams_dup(const DSA *dsa) {
395 DSA *ret = DSA_new();
396 if (ret == NULL) {
397 return NULL;
398 }
399 ret->p = BN_dup(dsa->p);
400 ret->q = BN_dup(dsa->q);
401 ret->g = BN_dup(dsa->g);
402 if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
403 DSA_free(ret);
404 return NULL;
405 }
406 return ret;
407}
408
Adam Langley95c29f32014-06-20 12:00:00 -0700409int DSA_generate_key(DSA *dsa) {
David Benjamine8f783a2015-10-30 16:53:00 -0400410 int ok = 0;
411 BN_CTX *ctx = NULL;
412 BIGNUM *pub_key = NULL, *priv_key = NULL;
413 BIGNUM prk;
414
415 ctx = BN_CTX_new();
416 if (ctx == NULL) {
417 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700418 }
David Benjamine8f783a2015-10-30 16:53:00 -0400419
420 priv_key = dsa->priv_key;
421 if (priv_key == NULL) {
422 priv_key = BN_new();
423 if (priv_key == NULL) {
424 goto err;
425 }
426 }
427
428 do {
429 if (!BN_rand_range(priv_key, dsa->q)) {
430 goto err;
431 }
432 } while (BN_is_zero(priv_key));
433
434 pub_key = dsa->pub_key;
435 if (pub_key == NULL) {
436 pub_key = BN_new();
437 if (pub_key == NULL) {
438 goto err;
439 }
440 }
441
442 BN_init(&prk);
443 BN_with_flags(&prk, priv_key, BN_FLG_CONSTTIME);
444
David Benjaminaaa39e92016-06-16 15:18:38 -0400445 if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p, &dsa->method_mont_lock,
446 dsa->p, ctx) ||
447 !BN_mod_exp_mont_consttime(pub_key, dsa->g, &prk, dsa->p, ctx,
448 dsa->method_mont_p)) {
David Benjamine8f783a2015-10-30 16:53:00 -0400449 goto err;
450 }
451
452 dsa->priv_key = priv_key;
453 dsa->pub_key = pub_key;
454 ok = 1;
455
456err:
457 if (dsa->pub_key == NULL) {
458 BN_free(pub_key);
459 }
460 if (dsa->priv_key == NULL) {
461 BN_free(priv_key);
462 }
463 BN_CTX_free(ctx);
464
465 return ok;
Adam Langley95c29f32014-06-20 12:00:00 -0700466}
467
468DSA_SIG *DSA_SIG_new(void) {
469 DSA_SIG *sig;
470 sig = OPENSSL_malloc(sizeof(DSA_SIG));
471 if (!sig) {
472 return NULL;
473 }
474 sig->r = NULL;
475 sig->s = NULL;
476 return sig;
477}
478
479void DSA_SIG_free(DSA_SIG *sig) {
480 if (!sig) {
481 return;
482 }
483
David Benjamin22ccc2d2015-04-22 13:50:28 -0400484 BN_free(sig->r);
485 BN_free(sig->s);
Adam Langley95c29f32014-06-20 12:00:00 -0700486 OPENSSL_free(sig);
487}
488
489DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, DSA *dsa) {
David Benjamine8f783a2015-10-30 16:53:00 -0400490 BIGNUM *kinv = NULL, *r = NULL, *s = NULL;
491 BIGNUM m;
492 BIGNUM xr;
493 BN_CTX *ctx = NULL;
494 int reason = ERR_R_BN_LIB;
495 DSA_SIG *ret = NULL;
496 int noredo = 0;
497
498 BN_init(&m);
499 BN_init(&xr);
500
501 if (!dsa->p || !dsa->q || !dsa->g) {
502 reason = DSA_R_MISSING_PARAMETERS;
503 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700504 }
David Benjamine8f783a2015-10-30 16:53:00 -0400505
506 s = BN_new();
507 if (s == NULL) {
508 goto err;
509 }
510 ctx = BN_CTX_new();
511 if (ctx == NULL) {
512 goto err;
513 }
514
515redo:
516 if (dsa->kinv == NULL || dsa->r == NULL) {
517 if (!DSA_sign_setup(dsa, ctx, &kinv, &r)) {
518 goto err;
519 }
520 } else {
521 kinv = dsa->kinv;
522 dsa->kinv = NULL;
523 r = dsa->r;
524 dsa->r = NULL;
525 noredo = 1;
526 }
527
528 if (digest_len > BN_num_bytes(dsa->q)) {
529 /* if the digest length is greater than the size of q use the
530 * BN_num_bits(dsa->q) leftmost bits of the digest, see
531 * fips 186-3, 4.2 */
532 digest_len = BN_num_bytes(dsa->q);
533 }
534
535 if (BN_bin2bn(digest, digest_len, &m) == NULL) {
536 goto err;
537 }
538
539 /* Compute s = inv(k) (m + xr) mod q */
540 if (!BN_mod_mul(&xr, dsa->priv_key, r, dsa->q, ctx)) {
541 goto err; /* s = xr */
542 }
543 if (!BN_add(s, &xr, &m)) {
544 goto err; /* s = m + xr */
545 }
546 if (BN_cmp(s, dsa->q) > 0) {
547 if (!BN_sub(s, s, dsa->q)) {
548 goto err;
549 }
550 }
551 if (!BN_mod_mul(s, s, kinv, dsa->q, ctx)) {
552 goto err;
553 }
554
David Benjamine8f783a2015-10-30 16:53:00 -0400555 /* Redo if r or s is zero as required by FIPS 186-3: this is
556 * very unlikely. */
557 if (BN_is_zero(r) || BN_is_zero(s)) {
558 if (noredo) {
559 reason = DSA_R_NEED_NEW_SETUP_VALUES;
560 goto err;
561 }
562 goto redo;
563 }
David Benjamin29361702015-12-11 15:29:17 -0500564 ret = DSA_SIG_new();
565 if (ret == NULL) {
566 goto err;
567 }
David Benjamine8f783a2015-10-30 16:53:00 -0400568 ret->r = r;
569 ret->s = s;
570
571err:
David Benjamin29361702015-12-11 15:29:17 -0500572 if (ret == NULL) {
David Benjamine8f783a2015-10-30 16:53:00 -0400573 OPENSSL_PUT_ERROR(DSA, reason);
574 BN_free(r);
575 BN_free(s);
576 }
577 BN_CTX_free(ctx);
578 BN_clear_free(&m);
579 BN_clear_free(&xr);
580 BN_clear_free(kinv);
581
582 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700583}
584
585int DSA_do_verify(const uint8_t *digest, size_t digest_len, DSA_SIG *sig,
586 const DSA *dsa) {
David Benjamin95e18c52015-01-10 23:37:17 -0500587 int valid;
588 if (!DSA_do_check_signature(&valid, digest, digest_len, sig, dsa)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700589 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700590 }
David Benjamin95e18c52015-01-10 23:37:17 -0500591 return valid;
Adam Langley95c29f32014-06-20 12:00:00 -0700592}
593
594int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
595 size_t digest_len, DSA_SIG *sig, const DSA *dsa) {
David Benjamine8f783a2015-10-30 16:53:00 -0400596 BN_CTX *ctx;
597 BIGNUM u1, u2, t1;
David Benjamine8f783a2015-10-30 16:53:00 -0400598 int ret = 0;
599 unsigned i;
600
601 *out_valid = 0;
602
603 if (!dsa->p || !dsa->q || !dsa->g) {
604 OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
605 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700606 }
607
David Benjamine8f783a2015-10-30 16:53:00 -0400608 i = BN_num_bits(dsa->q);
609 /* fips 186-3 allows only different sizes for q */
610 if (i != 160 && i != 224 && i != 256) {
611 OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_Q_VALUE);
612 return 0;
613 }
614
615 if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
616 OPENSSL_PUT_ERROR(DSA, DSA_R_MODULUS_TOO_LARGE);
617 return 0;
618 }
619
620 BN_init(&u1);
621 BN_init(&u2);
622 BN_init(&t1);
623
624 ctx = BN_CTX_new();
625 if (ctx == NULL) {
626 goto err;
627 }
628
629 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
630 BN_ucmp(sig->r, dsa->q) >= 0) {
631 ret = 1;
632 goto err;
633 }
634 if (BN_is_zero(sig->s) || BN_is_negative(sig->s) ||
635 BN_ucmp(sig->s, dsa->q) >= 0) {
636 ret = 1;
637 goto err;
638 }
639
640 /* Calculate W = inv(S) mod Q
641 * save W in u2 */
642 if (BN_mod_inverse(&u2, sig->s, dsa->q, ctx) == NULL) {
643 goto err;
644 }
645
646 /* save M in u1 */
647 if (digest_len > (i >> 3)) {
648 /* if the digest length is greater than the size of q use the
649 * BN_num_bits(dsa->q) leftmost bits of the digest, see
650 * fips 186-3, 4.2 */
651 digest_len = (i >> 3);
652 }
653
654 if (BN_bin2bn(digest, digest_len, &u1) == NULL) {
655 goto err;
656 }
657
658 /* u1 = M * w mod q */
659 if (!BN_mod_mul(&u1, &u1, &u2, dsa->q, ctx)) {
660 goto err;
661 }
662
663 /* u2 = r * w mod q */
664 if (!BN_mod_mul(&u2, sig->r, &u2, dsa->q, ctx)) {
665 goto err;
666 }
667
Brian Smithd0357302016-03-25 10:11:04 -1000668 if (!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
David Benjamin99c752a2016-06-16 15:13:43 -0400669 (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p,
Brian Smithd0357302016-03-25 10:11:04 -1000670 ctx)) {
David Benjamine8f783a2015-10-30 16:53:00 -0400671 goto err;
672 }
673
674 if (!BN_mod_exp2_mont(&t1, dsa->g, &u1, dsa->pub_key, &u2, dsa->p, ctx,
Brian Smithd0357302016-03-25 10:11:04 -1000675 dsa->method_mont_p)) {
David Benjamine8f783a2015-10-30 16:53:00 -0400676 goto err;
677 }
678
679 /* BN_copy(&u1,&t1); */
680 /* let u1 = u1 mod q */
681 if (!BN_mod(&u1, &t1, dsa->q, ctx)) {
682 goto err;
683 }
684
685 /* V is now in u1. If the signature is correct, it will be
686 * equal to R. */
687 *out_valid = BN_ucmp(&u1, sig->r) == 0;
688 ret = 1;
689
690err:
691 if (ret != 1) {
692 OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
693 }
694 BN_CTX_free(ctx);
695 BN_free(&u1);
696 BN_free(&u2);
697 BN_free(&t1);
698
699 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700700}
701
702int DSA_sign(int type, const uint8_t *digest, size_t digest_len,
703 uint8_t *out_sig, unsigned int *out_siglen, DSA *dsa) {
704 DSA_SIG *s;
705
706 s = DSA_do_sign(digest, digest_len, dsa);
707 if (s == NULL) {
708 *out_siglen = 0;
709 return 0;
710 }
711
712 *out_siglen = i2d_DSA_SIG(s, &out_sig);
713 DSA_SIG_free(s);
714 return 1;
715}
716
717int DSA_verify(int type, const uint8_t *digest, size_t digest_len,
718 const uint8_t *sig, size_t sig_len, const DSA *dsa) {
David Benjamin95e18c52015-01-10 23:37:17 -0500719 int valid;
720 if (!DSA_check_signature(&valid, digest, digest_len, sig, sig_len, dsa)) {
721 return -1;
722 }
723 return valid;
724}
725
726int DSA_check_signature(int *out_valid, const uint8_t *digest,
727 size_t digest_len, const uint8_t *sig, size_t sig_len,
728 const DSA *dsa) {
Adam Langley95c29f32014-06-20 12:00:00 -0700729 DSA_SIG *s = NULL;
David Benjamin95e18c52015-01-10 23:37:17 -0500730 int ret = 0;
Adam Langleyca9a5382015-01-08 12:26:55 -0800731 uint8_t *der = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700732
733 s = DSA_SIG_new();
734 if (s == NULL) {
735 goto err;
736 }
737
Adam Langleyca9a5382015-01-08 12:26:55 -0800738 const uint8_t *sigp = sig;
739 if (d2i_DSA_SIG(&s, &sigp, sig_len) == NULL || sigp != sig + sig_len) {
740 goto err;
741 }
742
743 /* Ensure that the signature uses DER and doesn't have trailing garbage. */
744 int der_len = i2d_DSA_SIG(s, &der);
745 if (der_len < 0 || (size_t)der_len != sig_len || memcmp(sig, der, sig_len)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700746 goto err;
747 }
748
David Benjamin95e18c52015-01-10 23:37:17 -0500749 ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700750
751err:
David Benjamin22ccc2d2015-04-22 13:50:28 -0400752 OPENSSL_free(der);
753 DSA_SIG_free(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700754 return ret;
755}
756
David Benjamindf98a7a2016-01-02 14:35:47 -0800757/* der_len_len returns the number of bytes needed to represent a length of |len|
758 * in DER. */
759static size_t der_len_len(size_t len) {
760 if (len < 0x80) {
761 return 1;
762 }
763 size_t ret = 1;
764 while (len > 0) {
765 ret++;
766 len >>= 8;
767 }
768 return ret;
769}
770
Adam Langley95c29f32014-06-20 12:00:00 -0700771int DSA_size(const DSA *dsa) {
David Benjamindf98a7a2016-01-02 14:35:47 -0800772 size_t order_len = BN_num_bytes(dsa->q);
773 /* Compute the maximum length of an |order_len| byte integer. Defensively
774 * assume that the leading 0x00 is included. */
775 size_t integer_len = 1 /* tag */ + der_len_len(order_len + 1) + 1 + order_len;
776 if (integer_len < order_len) {
777 return 0;
778 }
779 /* A DSA signature is two INTEGERs. */
780 size_t value_len = 2 * integer_len;
781 if (value_len < integer_len) {
782 return 0;
783 }
784 /* Add the header. */
785 size_t ret = 1 /* tag */ + der_len_len(value_len) + value_len;
786 if (ret < value_len) {
787 return 0;
788 }
Adam Langley95c29f32014-06-20 12:00:00 -0700789 return ret;
790}
791
David Benjamine8f783a2015-10-30 16:53:00 -0400792int DSA_sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **out_kinv,
Adam Langley95c29f32014-06-20 12:00:00 -0700793 BIGNUM **out_r) {
David Benjamine8f783a2015-10-30 16:53:00 -0400794 BN_CTX *ctx;
David Benjaminaaa39e92016-06-16 15:18:38 -0400795 BIGNUM k, kq, qm2, *kinv = NULL, *r = NULL;
David Benjamine8f783a2015-10-30 16:53:00 -0400796 int ret = 0;
797
798 if (!dsa->p || !dsa->q || !dsa->g) {
799 OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
800 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700801 }
802
David Benjamine8f783a2015-10-30 16:53:00 -0400803 BN_init(&k);
804 BN_init(&kq);
David Benjamin99c752a2016-06-16 15:13:43 -0400805 BN_init(&qm2);
David Benjamine8f783a2015-10-30 16:53:00 -0400806
807 ctx = ctx_in;
808 if (ctx == NULL) {
809 ctx = BN_CTX_new();
810 if (ctx == NULL) {
811 goto err;
812 }
813 }
814
815 r = BN_new();
816 if (r == NULL) {
817 goto err;
818 }
819
820 /* Get random k */
821 do {
822 if (!BN_rand_range(&k, dsa->q)) {
823 goto err;
824 }
825 } while (BN_is_zero(&k));
826
827 BN_set_flags(&k, BN_FLG_CONSTTIME);
828
Brian Smithd0357302016-03-25 10:11:04 -1000829 if (!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
David Benjamin99c752a2016-06-16 15:13:43 -0400830 (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p,
831 ctx) ||
832 !BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_q,
833 (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->q,
Brian Smithd0357302016-03-25 10:11:04 -1000834 ctx)) {
David Benjamine8f783a2015-10-30 16:53:00 -0400835 goto err;
836 }
837
838 /* Compute r = (g^k mod p) mod q */
839 if (!BN_copy(&kq, &k)) {
840 goto err;
841 }
842
843 /* We do not want timing information to leak the length of k,
844 * so we compute g^k using an equivalent exponent of fixed length.
845 *
846 * (This is a kludge that we need because the BN_mod_exp_mont()
847 * does not let us specify the desired timing behaviour.) */
848
849 if (!BN_add(&kq, &kq, dsa->q)) {
850 goto err;
851 }
852 if (BN_num_bits(&kq) <= BN_num_bits(dsa->q) && !BN_add(&kq, &kq, dsa->q)) {
853 goto err;
854 }
855
David Benjamin26b7c352016-06-07 14:38:01 -0400856 BN_set_flags(&kq, BN_FLG_CONSTTIME);
David Benjaminaaa39e92016-06-16 15:18:38 -0400857 if (!BN_mod_exp_mont_consttime(r, dsa->g, &kq, dsa->p, ctx,
858 dsa->method_mont_p)) {
David Benjamine8f783a2015-10-30 16:53:00 -0400859 goto err;
860 }
861 if (!BN_mod(r, r, dsa->q, ctx)) {
862 goto err;
863 }
864
David Benjamin99c752a2016-06-16 15:13:43 -0400865 /* Compute part of 's = inv(k) (m + xr) mod q' using Fermat's Little
866 * Theorem. */
867 kinv = BN_new();
868 if (kinv == NULL ||
869 !BN_set_word(&qm2, 2) ||
870 !BN_sub(&qm2, dsa->q, &qm2) ||
871 !BN_mod_exp_mont(kinv, &k, &qm2, dsa->q, ctx, dsa->method_mont_q)) {
David Benjamine8f783a2015-10-30 16:53:00 -0400872 goto err;
873 }
874
875 BN_clear_free(*out_kinv);
876 *out_kinv = kinv;
877 kinv = NULL;
878 BN_clear_free(*out_r);
879 *out_r = r;
880 ret = 1;
881
882err:
883 if (!ret) {
884 OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
885 if (r != NULL) {
886 BN_clear_free(r);
887 }
888 }
889
890 if (ctx_in == NULL) {
891 BN_CTX_free(ctx);
892 }
893 BN_clear_free(&k);
894 BN_clear_free(&kq);
David Benjamin99c752a2016-06-16 15:13:43 -0400895 BN_free(&qm2);
David Benjamine8f783a2015-10-30 16:53:00 -0400896 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700897}
898
David Benjamin8a589332015-12-04 23:14:35 -0500899int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
Adam Langley95c29f32014-06-20 12:00:00 -0700900 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -0400901 int index;
David Benjamin8a589332015-12-04 23:14:35 -0500902 if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, dup_func,
903 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -0400904 return -1;
905 }
906 return index;
Adam Langley95c29f32014-06-20 12:00:00 -0700907}
908
909int DSA_set_ex_data(DSA *d, int idx, void *arg) {
910 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
911}
912
913void *DSA_get_ex_data(const DSA *d, int idx) {
914 return CRYPTO_get_ex_data(&d->ex_data, idx);
915}
Adam Langleybed8ce72014-09-05 17:04:51 -0700916
917DH *DSA_dup_DH(const DSA *r) {
918 DH *ret = NULL;
919
920 if (r == NULL) {
921 goto err;
922 }
923 ret = DH_new();
924 if (ret == NULL) {
925 goto err;
926 }
927 if (r->q != NULL) {
928 ret->priv_length = BN_num_bits(r->q);
929 if ((ret->q = BN_dup(r->q)) == NULL) {
930 goto err;
931 }
932 }
933 if ((r->p != NULL && (ret->p = BN_dup(r->p)) == NULL) ||
934 (r->g != NULL && (ret->g = BN_dup(r->g)) == NULL) ||
935 (r->pub_key != NULL && (ret->pub_key = BN_dup(r->pub_key)) == NULL) ||
936 (r->priv_key != NULL && (ret->priv_key = BN_dup(r->priv_key)) == NULL)) {
937 goto err;
938 }
939
940 return ret;
941
942err:
David Benjamin22ccc2d2015-04-22 13:50:28 -0400943 DH_free(ret);
Adam Langleybed8ce72014-09-05 17:04:51 -0700944 return NULL;
945}