blob: 15583be056b1c804e14a88c46dc09887a0a0c3d5 [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
David Benjamin0b8dc302016-12-17 14:27:16 -050075#include "../bn/internal.h"
Adam Langley683d7bd2015-04-13 11:04:14 -070076#include "../internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070077
Adam Langley2b2d66d2015-01-30 17:08:37 -080078
David Benjamine8f783a2015-10-30 16:53:00 -040079#define OPENSSL_DSA_MAX_MODULUS_BITS 10000
80
81/* Primality test according to FIPS PUB 186[-1], Appendix 2.1: 50 rounds of
82 * Rabin-Miller */
83#define DSS_prime_checks 50
Adam Langley95c29f32014-06-20 12:00:00 -070084
David Benjamin9f33fc62015-04-15 17:29:53 -040085static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
86
David Benjamine8f783a2015-10-30 16:53:00 -040087DSA *DSA_new(void) {
Brian Smith5ba06892016-02-07 09:36:04 -100088 DSA *dsa = OPENSSL_malloc(sizeof(DSA));
Adam Langley95c29f32014-06-20 12:00:00 -070089 if (dsa == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -040090 OPENSSL_PUT_ERROR(DSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -070091 return NULL;
92 }
93
David Benjamin17cf2cb2016-12-13 01:07:13 -050094 OPENSSL_memset(dsa, 0, sizeof(DSA));
Adam Langley95c29f32014-06-20 12:00:00 -070095
Adam Langley95c29f32014-06-20 12:00:00 -070096 dsa->references = 1;
97
David Benjamin99c752a2016-06-16 15:13:43 -040098 CRYPTO_MUTEX_init(&dsa->method_mont_lock);
David Benjamin8a589332015-12-04 23:14:35 -050099 CRYPTO_new_ex_data(&dsa->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700100
101 return dsa;
102}
103
104void DSA_free(DSA *dsa) {
105 if (dsa == NULL) {
106 return;
107 }
108
Adam Langley0da323a2015-05-15 12:49:30 -0700109 if (!CRYPTO_refcount_dec_and_test_zero(&dsa->references)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700110 return;
111 }
112
David Benjamin9f33fc62015-04-15 17:29:53 -0400113 CRYPTO_free_ex_data(&g_ex_data_class, dsa, &dsa->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700114
David Benjamin22ccc2d2015-04-22 13:50:28 -0400115 BN_clear_free(dsa->p);
116 BN_clear_free(dsa->q);
117 BN_clear_free(dsa->g);
118 BN_clear_free(dsa->pub_key);
119 BN_clear_free(dsa->priv_key);
120 BN_clear_free(dsa->kinv);
121 BN_clear_free(dsa->r);
David Benjamine8f783a2015-10-30 16:53:00 -0400122 BN_MONT_CTX_free(dsa->method_mont_p);
David Benjamin99c752a2016-06-16 15:13:43 -0400123 BN_MONT_CTX_free(dsa->method_mont_q);
124 CRYPTO_MUTEX_cleanup(&dsa->method_mont_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700125 OPENSSL_free(dsa);
126}
127
128int DSA_up_ref(DSA *dsa) {
Adam Langley0da323a2015-05-15 12:49:30 -0700129 CRYPTO_refcount_inc(&dsa->references);
Adam Langley95c29f32014-06-20 12:00:00 -0700130 return 1;
131}
132
David Benjamin5a915032016-08-09 11:04:24 -0400133void DSA_get0_key(const DSA *dsa, const BIGNUM **out_pub_key,
134 const BIGNUM **out_priv_key) {
135 if (out_pub_key != NULL) {
136 *out_pub_key = dsa->pub_key;
137 }
138 if (out_priv_key != NULL) {
139 *out_priv_key = dsa->priv_key;
140 }
141}
142
143void DSA_get0_pqg(const DSA *dsa, const BIGNUM **out_p, const BIGNUM **out_q,
144 const BIGNUM **out_g) {
145 if (out_p != NULL) {
146 *out_p = dsa->p;
147 }
148 if (out_q != NULL) {
149 *out_q = dsa->q;
150 }
151 if (out_g != NULL) {
152 *out_g = dsa->g;
153 }
154}
155
Adam Langley95c29f32014-06-20 12:00:00 -0700156int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, const uint8_t *seed_in,
157 size_t seed_len, int *out_counter,
158 unsigned long *out_h, BN_GENCB *cb) {
David Benjamine8f783a2015-10-30 16:53:00 -0400159 int ok = 0;
160 unsigned char seed[SHA256_DIGEST_LENGTH];
161 unsigned char md[SHA256_DIGEST_LENGTH];
162 unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH];
163 BIGNUM *r0, *W, *X, *c, *test;
164 BIGNUM *g = NULL, *q = NULL, *p = NULL;
165 BN_MONT_CTX *mont = NULL;
166 int k, n = 0, m = 0;
167 unsigned i;
168 int counter = 0;
169 int r = 0;
170 BN_CTX *ctx = NULL;
171 unsigned int h = 2;
172 unsigned qsize;
173 const EVP_MD *evpmd;
174
175 evpmd = (bits >= 2048) ? EVP_sha256() : EVP_sha1();
176 qsize = EVP_MD_size(evpmd);
177
178 if (bits < 512) {
179 bits = 512;
Adam Langley95c29f32014-06-20 12:00:00 -0700180 }
David Benjamine8f783a2015-10-30 16:53:00 -0400181
182 bits = (bits + 63) / 64 * 64;
183
184 if (seed_in != NULL) {
185 if (seed_len < (size_t)qsize) {
186 return 0;
187 }
188 if (seed_len > (size_t)qsize) {
189 /* Only consume as much seed as is expected. */
190 seed_len = qsize;
191 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500192 OPENSSL_memcpy(seed, seed_in, seed_len);
David Benjamine8f783a2015-10-30 16:53:00 -0400193 }
194
195 ctx = BN_CTX_new();
196 if (ctx == NULL) {
197 goto err;
198 }
199 BN_CTX_start(ctx);
200
201 mont = BN_MONT_CTX_new();
202 if (mont == NULL) {
203 goto err;
204 }
205
206 r0 = BN_CTX_get(ctx);
207 g = BN_CTX_get(ctx);
208 W = BN_CTX_get(ctx);
209 q = BN_CTX_get(ctx);
210 X = BN_CTX_get(ctx);
211 c = BN_CTX_get(ctx);
212 p = BN_CTX_get(ctx);
213 test = BN_CTX_get(ctx);
214
215 if (test == NULL || !BN_lshift(test, BN_value_one(), bits - 1)) {
216 goto err;
217 }
218
219 for (;;) {
220 /* Find q. */
221 for (;;) {
222 /* step 1 */
223 if (!BN_GENCB_call(cb, 0, m++)) {
224 goto err;
225 }
226
227 int use_random_seed = (seed_in == NULL);
228 if (use_random_seed) {
229 if (!RAND_bytes(seed, qsize)) {
230 goto err;
231 }
232 } else {
233 /* If we come back through, use random seed next time. */
234 seed_in = NULL;
235 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500236 OPENSSL_memcpy(buf, seed, qsize);
237 OPENSSL_memcpy(buf2, seed, qsize);
David Benjamine8f783a2015-10-30 16:53:00 -0400238 /* precompute "SEED + 1" for step 7: */
239 for (i = qsize - 1; i < qsize; i--) {
240 buf[i]++;
241 if (buf[i] != 0) {
242 break;
243 }
244 }
245
246 /* step 2 */
247 if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL) ||
248 !EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL)) {
249 goto err;
250 }
251 for (i = 0; i < qsize; i++) {
252 md[i] ^= buf2[i];
253 }
254
255 /* step 3 */
256 md[0] |= 0x80;
257 md[qsize - 1] |= 0x01;
258 if (!BN_bin2bn(md, qsize, q)) {
259 goto err;
260 }
261
262 /* step 4 */
263 r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, use_random_seed, cb);
264 if (r > 0) {
265 break;
266 }
267 if (r != 0) {
268 goto err;
269 }
270
271 /* do a callback call */
272 /* step 5 */
273 }
274
275 if (!BN_GENCB_call(cb, 2, 0) || !BN_GENCB_call(cb, 3, 0)) {
276 goto err;
277 }
278
279 /* step 6 */
280 counter = 0;
281 /* "offset = 2" */
282
283 n = (bits - 1) / 160;
284
285 for (;;) {
286 if ((counter != 0) && !BN_GENCB_call(cb, 0, counter)) {
287 goto err;
288 }
289
290 /* step 7 */
291 BN_zero(W);
292 /* now 'buf' contains "SEED + offset - 1" */
293 for (k = 0; k <= n; k++) {
294 /* obtain "SEED + offset + k" by incrementing: */
295 for (i = qsize - 1; i < qsize; i--) {
296 buf[i]++;
297 if (buf[i] != 0) {
298 break;
299 }
300 }
301
302 if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL)) {
303 goto err;
304 }
305
306 /* step 8 */
307 if (!BN_bin2bn(md, qsize, r0) ||
308 !BN_lshift(r0, r0, (qsize << 3) * k) ||
309 !BN_add(W, W, r0)) {
310 goto err;
311 }
312 }
313
314 /* more of step 8 */
315 if (!BN_mask_bits(W, bits - 1) ||
316 !BN_copy(X, W) ||
317 !BN_add(X, X, test)) {
318 goto err;
319 }
320
321 /* step 9 */
322 if (!BN_lshift1(r0, q) ||
323 !BN_mod(c, X, r0, ctx) ||
324 !BN_sub(r0, c, BN_value_one()) ||
325 !BN_sub(p, X, r0)) {
326 goto err;
327 }
328
329 /* step 10 */
330 if (BN_cmp(p, test) >= 0) {
331 /* step 11 */
332 r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
333 if (r > 0) {
334 goto end; /* found it */
335 }
336 if (r != 0) {
337 goto err;
338 }
339 }
340
341 /* step 13 */
342 counter++;
343 /* "offset = offset + n + 1" */
344
345 /* step 14 */
346 if (counter >= 4096) {
347 break;
348 }
349 }
350 }
351end:
352 if (!BN_GENCB_call(cb, 2, 1)) {
353 goto err;
354 }
355
356 /* We now need to generate g */
357 /* Set r0=(p-1)/q */
358 if (!BN_sub(test, p, BN_value_one()) ||
359 !BN_div(r0, NULL, test, q, ctx)) {
360 goto err;
361 }
362
363 if (!BN_set_word(test, h) ||
364 !BN_MONT_CTX_set(mont, p, ctx)) {
365 goto err;
366 }
367
368 for (;;) {
369 /* g=test^r0%p */
370 if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont)) {
371 goto err;
372 }
373 if (!BN_is_one(g)) {
374 break;
375 }
376 if (!BN_add(test, test, BN_value_one())) {
377 goto err;
378 }
379 h++;
380 }
381
382 if (!BN_GENCB_call(cb, 3, 1)) {
383 goto err;
384 }
385
386 ok = 1;
387
388err:
389 if (ok) {
390 BN_free(dsa->p);
391 BN_free(dsa->q);
392 BN_free(dsa->g);
393 dsa->p = BN_dup(p);
394 dsa->q = BN_dup(q);
395 dsa->g = BN_dup(g);
396 if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
397 ok = 0;
398 goto err;
399 }
400 if (out_counter != NULL) {
401 *out_counter = counter;
402 }
403 if (out_h != NULL) {
404 *out_h = h;
405 }
406 }
407
408 if (ctx) {
409 BN_CTX_end(ctx);
410 BN_CTX_free(ctx);
411 }
412
413 BN_MONT_CTX_free(mont);
414
415 return ok;
Adam Langley95c29f32014-06-20 12:00:00 -0700416}
417
David Benjaminfda22a72016-01-30 01:51:01 -0500418DSA *DSAparams_dup(const DSA *dsa) {
419 DSA *ret = DSA_new();
420 if (ret == NULL) {
421 return NULL;
422 }
423 ret->p = BN_dup(dsa->p);
424 ret->q = BN_dup(dsa->q);
425 ret->g = BN_dup(dsa->g);
426 if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
427 DSA_free(ret);
428 return NULL;
429 }
430 return ret;
431}
432
Adam Langley95c29f32014-06-20 12:00:00 -0700433int DSA_generate_key(DSA *dsa) {
David Benjamine8f783a2015-10-30 16:53:00 -0400434 int ok = 0;
435 BN_CTX *ctx = NULL;
436 BIGNUM *pub_key = NULL, *priv_key = NULL;
437 BIGNUM prk;
438
439 ctx = BN_CTX_new();
440 if (ctx == NULL) {
441 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700442 }
David Benjamine8f783a2015-10-30 16:53:00 -0400443
444 priv_key = dsa->priv_key;
445 if (priv_key == NULL) {
446 priv_key = BN_new();
447 if (priv_key == NULL) {
448 goto err;
449 }
450 }
451
Brian Smith4edca0b2016-07-25 10:36:58 -1000452 if (!BN_rand_range_ex(priv_key, 1, dsa->q)) {
453 goto err;
454 }
David Benjamine8f783a2015-10-30 16:53:00 -0400455
456 pub_key = dsa->pub_key;
457 if (pub_key == NULL) {
458 pub_key = BN_new();
459 if (pub_key == NULL) {
460 goto err;
461 }
462 }
463
464 BN_init(&prk);
465 BN_with_flags(&prk, priv_key, BN_FLG_CONSTTIME);
466
David Benjaminaaa39e92016-06-16 15:18:38 -0400467 if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p, &dsa->method_mont_lock,
468 dsa->p, ctx) ||
469 !BN_mod_exp_mont_consttime(pub_key, dsa->g, &prk, dsa->p, ctx,
470 dsa->method_mont_p)) {
David Benjamine8f783a2015-10-30 16:53:00 -0400471 goto err;
472 }
473
474 dsa->priv_key = priv_key;
475 dsa->pub_key = pub_key;
476 ok = 1;
477
478err:
479 if (dsa->pub_key == NULL) {
480 BN_free(pub_key);
481 }
482 if (dsa->priv_key == NULL) {
483 BN_free(priv_key);
484 }
485 BN_CTX_free(ctx);
486
487 return ok;
Adam Langley95c29f32014-06-20 12:00:00 -0700488}
489
490DSA_SIG *DSA_SIG_new(void) {
491 DSA_SIG *sig;
492 sig = OPENSSL_malloc(sizeof(DSA_SIG));
493 if (!sig) {
494 return NULL;
495 }
496 sig->r = NULL;
497 sig->s = NULL;
498 return sig;
499}
500
501void DSA_SIG_free(DSA_SIG *sig) {
502 if (!sig) {
503 return;
504 }
505
David Benjamin22ccc2d2015-04-22 13:50:28 -0400506 BN_free(sig->r);
507 BN_free(sig->s);
Adam Langley95c29f32014-06-20 12:00:00 -0700508 OPENSSL_free(sig);
509}
510
511DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, DSA *dsa) {
David Benjamine8f783a2015-10-30 16:53:00 -0400512 BIGNUM *kinv = NULL, *r = NULL, *s = NULL;
513 BIGNUM m;
514 BIGNUM xr;
515 BN_CTX *ctx = NULL;
516 int reason = ERR_R_BN_LIB;
517 DSA_SIG *ret = NULL;
518 int noredo = 0;
519
520 BN_init(&m);
521 BN_init(&xr);
522
523 if (!dsa->p || !dsa->q || !dsa->g) {
524 reason = DSA_R_MISSING_PARAMETERS;
525 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700526 }
David Benjamine8f783a2015-10-30 16:53:00 -0400527
528 s = BN_new();
529 if (s == NULL) {
530 goto err;
531 }
532 ctx = BN_CTX_new();
533 if (ctx == NULL) {
534 goto err;
535 }
536
537redo:
538 if (dsa->kinv == NULL || dsa->r == NULL) {
539 if (!DSA_sign_setup(dsa, ctx, &kinv, &r)) {
540 goto err;
541 }
542 } else {
543 kinv = dsa->kinv;
544 dsa->kinv = NULL;
545 r = dsa->r;
546 dsa->r = NULL;
547 noredo = 1;
548 }
549
550 if (digest_len > BN_num_bytes(dsa->q)) {
551 /* if the digest length is greater than the size of q use the
552 * BN_num_bits(dsa->q) leftmost bits of the digest, see
553 * fips 186-3, 4.2 */
554 digest_len = BN_num_bytes(dsa->q);
555 }
556
557 if (BN_bin2bn(digest, digest_len, &m) == NULL) {
558 goto err;
559 }
560
561 /* Compute s = inv(k) (m + xr) mod q */
562 if (!BN_mod_mul(&xr, dsa->priv_key, r, dsa->q, ctx)) {
563 goto err; /* s = xr */
564 }
565 if (!BN_add(s, &xr, &m)) {
566 goto err; /* s = m + xr */
567 }
568 if (BN_cmp(s, dsa->q) > 0) {
569 if (!BN_sub(s, s, dsa->q)) {
570 goto err;
571 }
572 }
573 if (!BN_mod_mul(s, s, kinv, dsa->q, ctx)) {
574 goto err;
575 }
576
David Benjamine8f783a2015-10-30 16:53:00 -0400577 /* Redo if r or s is zero as required by FIPS 186-3: this is
578 * very unlikely. */
579 if (BN_is_zero(r) || BN_is_zero(s)) {
580 if (noredo) {
581 reason = DSA_R_NEED_NEW_SETUP_VALUES;
582 goto err;
583 }
584 goto redo;
585 }
David Benjamin29361702015-12-11 15:29:17 -0500586 ret = DSA_SIG_new();
587 if (ret == NULL) {
588 goto err;
589 }
David Benjamine8f783a2015-10-30 16:53:00 -0400590 ret->r = r;
591 ret->s = s;
592
593err:
David Benjamin29361702015-12-11 15:29:17 -0500594 if (ret == NULL) {
David Benjamine8f783a2015-10-30 16:53:00 -0400595 OPENSSL_PUT_ERROR(DSA, reason);
596 BN_free(r);
597 BN_free(s);
598 }
599 BN_CTX_free(ctx);
600 BN_clear_free(&m);
601 BN_clear_free(&xr);
602 BN_clear_free(kinv);
603
604 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700605}
606
607int DSA_do_verify(const uint8_t *digest, size_t digest_len, DSA_SIG *sig,
608 const DSA *dsa) {
David Benjamin95e18c52015-01-10 23:37:17 -0500609 int valid;
610 if (!DSA_do_check_signature(&valid, digest, digest_len, sig, dsa)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700611 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700612 }
David Benjamin95e18c52015-01-10 23:37:17 -0500613 return valid;
Adam Langley95c29f32014-06-20 12:00:00 -0700614}
615
616int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
617 size_t digest_len, DSA_SIG *sig, const DSA *dsa) {
David Benjamine8f783a2015-10-30 16:53:00 -0400618 BN_CTX *ctx;
619 BIGNUM u1, u2, t1;
David Benjamine8f783a2015-10-30 16:53:00 -0400620 int ret = 0;
621 unsigned i;
622
623 *out_valid = 0;
624
625 if (!dsa->p || !dsa->q || !dsa->g) {
626 OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
627 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700628 }
629
David Benjamine8f783a2015-10-30 16:53:00 -0400630 i = BN_num_bits(dsa->q);
631 /* fips 186-3 allows only different sizes for q */
632 if (i != 160 && i != 224 && i != 256) {
633 OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_Q_VALUE);
634 return 0;
635 }
636
637 if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
638 OPENSSL_PUT_ERROR(DSA, DSA_R_MODULUS_TOO_LARGE);
639 return 0;
640 }
641
642 BN_init(&u1);
643 BN_init(&u2);
644 BN_init(&t1);
645
646 ctx = BN_CTX_new();
647 if (ctx == NULL) {
648 goto err;
649 }
650
651 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
652 BN_ucmp(sig->r, dsa->q) >= 0) {
653 ret = 1;
654 goto err;
655 }
656 if (BN_is_zero(sig->s) || BN_is_negative(sig->s) ||
657 BN_ucmp(sig->s, dsa->q) >= 0) {
658 ret = 1;
659 goto err;
660 }
661
662 /* Calculate W = inv(S) mod Q
663 * save W in u2 */
664 if (BN_mod_inverse(&u2, sig->s, dsa->q, ctx) == NULL) {
665 goto err;
666 }
667
668 /* save M in u1 */
669 if (digest_len > (i >> 3)) {
670 /* if the digest length is greater than the size of q use the
671 * BN_num_bits(dsa->q) leftmost bits of the digest, see
672 * fips 186-3, 4.2 */
673 digest_len = (i >> 3);
674 }
675
676 if (BN_bin2bn(digest, digest_len, &u1) == NULL) {
677 goto err;
678 }
679
680 /* u1 = M * w mod q */
681 if (!BN_mod_mul(&u1, &u1, &u2, dsa->q, ctx)) {
682 goto err;
683 }
684
685 /* u2 = r * w mod q */
686 if (!BN_mod_mul(&u2, sig->r, &u2, dsa->q, ctx)) {
687 goto err;
688 }
689
Brian Smithd0357302016-03-25 10:11:04 -1000690 if (!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
David Benjamin99c752a2016-06-16 15:13:43 -0400691 (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p,
Brian Smithd0357302016-03-25 10:11:04 -1000692 ctx)) {
David Benjamine8f783a2015-10-30 16:53:00 -0400693 goto err;
694 }
695
696 if (!BN_mod_exp2_mont(&t1, dsa->g, &u1, dsa->pub_key, &u2, dsa->p, ctx,
Brian Smithd0357302016-03-25 10:11:04 -1000697 dsa->method_mont_p)) {
David Benjamine8f783a2015-10-30 16:53:00 -0400698 goto err;
699 }
700
701 /* BN_copy(&u1,&t1); */
702 /* let u1 = u1 mod q */
703 if (!BN_mod(&u1, &t1, dsa->q, ctx)) {
704 goto err;
705 }
706
707 /* V is now in u1. If the signature is correct, it will be
708 * equal to R. */
709 *out_valid = BN_ucmp(&u1, sig->r) == 0;
710 ret = 1;
711
712err:
713 if (ret != 1) {
714 OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
715 }
716 BN_CTX_free(ctx);
717 BN_free(&u1);
718 BN_free(&u2);
719 BN_free(&t1);
720
721 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700722}
723
724int DSA_sign(int type, const uint8_t *digest, size_t digest_len,
725 uint8_t *out_sig, unsigned int *out_siglen, DSA *dsa) {
726 DSA_SIG *s;
727
728 s = DSA_do_sign(digest, digest_len, dsa);
729 if (s == NULL) {
730 *out_siglen = 0;
731 return 0;
732 }
733
734 *out_siglen = i2d_DSA_SIG(s, &out_sig);
735 DSA_SIG_free(s);
736 return 1;
737}
738
739int DSA_verify(int type, const uint8_t *digest, size_t digest_len,
740 const uint8_t *sig, size_t sig_len, const DSA *dsa) {
David Benjamin95e18c52015-01-10 23:37:17 -0500741 int valid;
742 if (!DSA_check_signature(&valid, digest, digest_len, sig, sig_len, dsa)) {
743 return -1;
744 }
745 return valid;
746}
747
748int DSA_check_signature(int *out_valid, const uint8_t *digest,
749 size_t digest_len, const uint8_t *sig, size_t sig_len,
750 const DSA *dsa) {
Adam Langley95c29f32014-06-20 12:00:00 -0700751 DSA_SIG *s = NULL;
David Benjamin95e18c52015-01-10 23:37:17 -0500752 int ret = 0;
Adam Langleyca9a5382015-01-08 12:26:55 -0800753 uint8_t *der = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700754
755 s = DSA_SIG_new();
756 if (s == NULL) {
757 goto err;
758 }
759
Adam Langleyca9a5382015-01-08 12:26:55 -0800760 const uint8_t *sigp = sig;
761 if (d2i_DSA_SIG(&s, &sigp, sig_len) == NULL || sigp != sig + sig_len) {
762 goto err;
763 }
764
765 /* Ensure that the signature uses DER and doesn't have trailing garbage. */
766 int der_len = i2d_DSA_SIG(s, &der);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500767 if (der_len < 0 || (size_t)der_len != sig_len ||
768 OPENSSL_memcmp(sig, der, sig_len)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700769 goto err;
770 }
771
David Benjamin95e18c52015-01-10 23:37:17 -0500772 ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700773
774err:
David Benjamin22ccc2d2015-04-22 13:50:28 -0400775 OPENSSL_free(der);
776 DSA_SIG_free(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700777 return ret;
778}
779
David Benjamindf98a7a2016-01-02 14:35:47 -0800780/* der_len_len returns the number of bytes needed to represent a length of |len|
781 * in DER. */
782static size_t der_len_len(size_t len) {
783 if (len < 0x80) {
784 return 1;
785 }
786 size_t ret = 1;
787 while (len > 0) {
788 ret++;
789 len >>= 8;
790 }
791 return ret;
792}
793
Adam Langley95c29f32014-06-20 12:00:00 -0700794int DSA_size(const DSA *dsa) {
David Benjamindf98a7a2016-01-02 14:35:47 -0800795 size_t order_len = BN_num_bytes(dsa->q);
796 /* Compute the maximum length of an |order_len| byte integer. Defensively
797 * assume that the leading 0x00 is included. */
798 size_t integer_len = 1 /* tag */ + der_len_len(order_len + 1) + 1 + order_len;
799 if (integer_len < order_len) {
800 return 0;
801 }
802 /* A DSA signature is two INTEGERs. */
803 size_t value_len = 2 * integer_len;
804 if (value_len < integer_len) {
805 return 0;
806 }
807 /* Add the header. */
808 size_t ret = 1 /* tag */ + der_len_len(value_len) + value_len;
809 if (ret < value_len) {
810 return 0;
811 }
Adam Langley95c29f32014-06-20 12:00:00 -0700812 return ret;
813}
814
David Benjamine8f783a2015-10-30 16:53:00 -0400815int DSA_sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **out_kinv,
Adam Langley95c29f32014-06-20 12:00:00 -0700816 BIGNUM **out_r) {
David Benjamine8f783a2015-10-30 16:53:00 -0400817 BN_CTX *ctx;
David Benjamin0b8dc302016-12-17 14:27:16 -0500818 BIGNUM k, kq, *kinv = NULL, *r = NULL;
David Benjamine8f783a2015-10-30 16:53:00 -0400819 int ret = 0;
820
821 if (!dsa->p || !dsa->q || !dsa->g) {
822 OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
823 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700824 }
825
David Benjamine8f783a2015-10-30 16:53:00 -0400826 BN_init(&k);
827 BN_init(&kq);
828
829 ctx = ctx_in;
830 if (ctx == NULL) {
831 ctx = BN_CTX_new();
832 if (ctx == NULL) {
833 goto err;
834 }
835 }
836
837 r = BN_new();
838 if (r == NULL) {
839 goto err;
840 }
841
842 /* Get random k */
Brian Smith4edca0b2016-07-25 10:36:58 -1000843 if (!BN_rand_range_ex(&k, 1, dsa->q)) {
844 goto err;
845 }
David Benjamine8f783a2015-10-30 16:53:00 -0400846
847 BN_set_flags(&k, BN_FLG_CONSTTIME);
848
Brian Smithd0357302016-03-25 10:11:04 -1000849 if (!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
David Benjamin99c752a2016-06-16 15:13:43 -0400850 (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p,
851 ctx) ||
852 !BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_q,
853 (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->q,
Brian Smithd0357302016-03-25 10:11:04 -1000854 ctx)) {
David Benjamine8f783a2015-10-30 16:53:00 -0400855 goto err;
856 }
857
858 /* Compute r = (g^k mod p) mod q */
859 if (!BN_copy(&kq, &k)) {
860 goto err;
861 }
862
863 /* We do not want timing information to leak the length of k,
864 * so we compute g^k using an equivalent exponent of fixed length.
865 *
866 * (This is a kludge that we need because the BN_mod_exp_mont()
867 * does not let us specify the desired timing behaviour.) */
868
869 if (!BN_add(&kq, &kq, dsa->q)) {
870 goto err;
871 }
872 if (BN_num_bits(&kq) <= BN_num_bits(dsa->q) && !BN_add(&kq, &kq, dsa->q)) {
873 goto err;
874 }
875
David Benjamin26b7c352016-06-07 14:38:01 -0400876 BN_set_flags(&kq, BN_FLG_CONSTTIME);
David Benjaminaaa39e92016-06-16 15:18:38 -0400877 if (!BN_mod_exp_mont_consttime(r, dsa->g, &kq, dsa->p, ctx,
878 dsa->method_mont_p)) {
David Benjamine8f783a2015-10-30 16:53:00 -0400879 goto err;
880 }
881 if (!BN_mod(r, r, dsa->q, ctx)) {
882 goto err;
883 }
884
David Benjamin99c752a2016-06-16 15:13:43 -0400885 /* Compute part of 's = inv(k) (m + xr) mod q' using Fermat's Little
886 * Theorem. */
887 kinv = BN_new();
888 if (kinv == NULL ||
David Benjamin0b8dc302016-12-17 14:27:16 -0500889 !bn_mod_inverse_prime(kinv, &k, dsa->q, ctx, dsa->method_mont_q)) {
David Benjamine8f783a2015-10-30 16:53:00 -0400890 goto err;
891 }
892
893 BN_clear_free(*out_kinv);
894 *out_kinv = kinv;
895 kinv = NULL;
896 BN_clear_free(*out_r);
897 *out_r = r;
898 ret = 1;
899
900err:
901 if (!ret) {
902 OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
903 if (r != NULL) {
904 BN_clear_free(r);
905 }
906 }
907
908 if (ctx_in == NULL) {
909 BN_CTX_free(ctx);
910 }
911 BN_clear_free(&k);
912 BN_clear_free(&kq);
David Benjamin0b8dc302016-12-17 14:27:16 -0500913 BN_clear_free(kinv);
David Benjamine8f783a2015-10-30 16:53:00 -0400914 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700915}
916
David Benjamin8a589332015-12-04 23:14:35 -0500917int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
Adam Langley95c29f32014-06-20 12:00:00 -0700918 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -0400919 int index;
David Benjamin8a589332015-12-04 23:14:35 -0500920 if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, dup_func,
921 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -0400922 return -1;
923 }
924 return index;
Adam Langley95c29f32014-06-20 12:00:00 -0700925}
926
927int DSA_set_ex_data(DSA *d, int idx, void *arg) {
928 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
929}
930
931void *DSA_get_ex_data(const DSA *d, int idx) {
932 return CRYPTO_get_ex_data(&d->ex_data, idx);
933}
Adam Langleybed8ce72014-09-05 17:04:51 -0700934
935DH *DSA_dup_DH(const DSA *r) {
936 DH *ret = NULL;
937
938 if (r == NULL) {
939 goto err;
940 }
941 ret = DH_new();
942 if (ret == NULL) {
943 goto err;
944 }
945 if (r->q != NULL) {
946 ret->priv_length = BN_num_bits(r->q);
947 if ((ret->q = BN_dup(r->q)) == NULL) {
948 goto err;
949 }
950 }
951 if ((r->p != NULL && (ret->p = BN_dup(r->p)) == NULL) ||
952 (r->g != NULL && (ret->g = BN_dup(r->g)) == NULL) ||
953 (r->pub_key != NULL && (ret->pub_key = BN_dup(r->pub_key)) == NULL) ||
954 (r->priv_key != NULL && (ret->priv_key = BN_dup(r->priv_key)) == NULL)) {
955 goto err;
956 }
957
958 return ret;
959
960err:
David Benjamin22ccc2d2015-04-22 13:50:28 -0400961 DH_free(ret);
Adam Langleybed8ce72014-09-05 17:04:51 -0700962 return NULL;
963}