Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/dsa/dsa_gen.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
/*
11
 * Parameter generation follows the updated Appendix 2.2 for FIPS PUB 186,
12
 * also Appendix 2.2 of FIPS PUB 186-1 (i.e. use SHA as defined in FIPS PUB
13
 * 180-1)
14
 */
15
#define xxxHASH    EVP_sha1()
16
17
#include <openssl/opensslconf.h>
18
#include <stdio.h>
19
#include "internal/cryptlib.h"
20
#include <openssl/evp.h>
21
#include <openssl/bn.h>
22
#include <openssl/rand.h>
23
#include <openssl/sha.h>
24
#include "dsa_locl.h"
25
26
int DSA_generate_parameters_ex(DSA *ret, int bits,
27
                               const unsigned char *seed_in, int seed_len,
28
                               int *counter_ret, unsigned long *h_ret,
29
                               BN_GENCB *cb)
30
0
{
31
0
    if (ret->meth->dsa_paramgen)
32
0
        return ret->meth->dsa_paramgen(ret, bits, seed_in, seed_len,
33
0
                                       counter_ret, h_ret, cb);
34
0
    else {
35
0
        const EVP_MD *evpmd = bits >= 2048 ? EVP_sha256() : EVP_sha1();
36
0
        size_t qbits = EVP_MD_size(evpmd) * 8;
37
0
38
0
        return dsa_builtin_paramgen(ret, bits, qbits, evpmd,
39
0
                                    seed_in, seed_len, NULL, counter_ret,
40
0
                                    h_ret, cb);
41
0
    }
42
0
}
43
44
int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
45
                         const EVP_MD *evpmd, const unsigned char *seed_in,
46
                         size_t seed_len, unsigned char *seed_out,
47
                         int *counter_ret, unsigned long *h_ret, BN_GENCB *cb)
48
0
{
49
0
    int ok = 0;
50
0
    unsigned char seed[SHA256_DIGEST_LENGTH];
51
0
    unsigned char md[SHA256_DIGEST_LENGTH];
52
0
    unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH];
53
0
    BIGNUM *r0, *W, *X, *c, *test;
54
0
    BIGNUM *g = NULL, *q = NULL, *p = NULL;
55
0
    BN_MONT_CTX *mont = NULL;
56
0
    int i, k, n = 0, m = 0, qsize = qbits >> 3;
57
0
    int counter = 0;
58
0
    int r = 0;
59
0
    BN_CTX *ctx = NULL;
60
0
    unsigned int h = 2;
61
0
62
0
    if (qsize != SHA_DIGEST_LENGTH && qsize != SHA224_DIGEST_LENGTH &&
63
0
        qsize != SHA256_DIGEST_LENGTH)
64
0
        /* invalid q size */
65
0
        return 0;
66
0
67
0
    if (evpmd == NULL) {
68
0
        if (qsize == SHA_DIGEST_LENGTH)
69
0
            evpmd = EVP_sha1();
70
0
        else if (qsize == SHA224_DIGEST_LENGTH)
71
0
            evpmd = EVP_sha224();
72
0
        else
73
0
            evpmd = EVP_sha256();
74
0
    } else {
75
0
        qsize = EVP_MD_size(evpmd);
76
0
    }
77
0
78
0
    if (bits < 512)
79
0
        bits = 512;
80
0
81
0
    bits = (bits + 63) / 64 * 64;
82
0
83
0
    if (seed_in != NULL) {
84
0
        if (seed_len < (size_t)qsize) {
85
0
            DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN, DSA_R_SEED_LEN_SMALL);
86
0
            return 0;
87
0
        }
88
0
        if (seed_len > (size_t)qsize) {
89
0
            /* Only consume as much seed as is expected. */
90
0
            seed_len = qsize;
91
0
        }
92
0
        memcpy(seed, seed_in, seed_len);
93
0
    }
94
0
95
0
    if ((mont = BN_MONT_CTX_new()) == NULL)
96
0
        goto err;
97
0
98
0
    if ((ctx = BN_CTX_new()) == NULL)
99
0
        goto err;
100
0
101
0
    BN_CTX_start(ctx);
102
0
103
0
    r0 = BN_CTX_get(ctx);
104
0
    g = BN_CTX_get(ctx);
105
0
    W = BN_CTX_get(ctx);
106
0
    q = BN_CTX_get(ctx);
107
0
    X = BN_CTX_get(ctx);
108
0
    c = BN_CTX_get(ctx);
109
0
    p = BN_CTX_get(ctx);
110
0
    test = BN_CTX_get(ctx);
111
0
112
0
    if (test == NULL)
113
0
        goto err;
114
0
115
0
    if (!BN_lshift(test, BN_value_one(), bits - 1))
116
0
        goto err;
117
0
118
0
    for (;;) {
119
0
        for (;;) {              /* find q */
120
0
            int use_random_seed = (seed_in == NULL);
121
0
122
0
            /* step 1 */
123
0
            if (!BN_GENCB_call(cb, 0, m++))
124
0
                goto err;
125
0
126
0
            if (use_random_seed) {
127
0
                if (RAND_bytes(seed, qsize) <= 0)
128
0
                    goto err;
129
0
            } else {
130
0
                /* If we come back through, use random seed next time. */
131
0
                seed_in = NULL;
132
0
            }
133
0
            memcpy(buf, seed, qsize);
134
0
            memcpy(buf2, seed, qsize);
135
0
            /* precompute "SEED + 1" for step 7: */
136
0
            for (i = qsize - 1; i >= 0; i--) {
137
0
                buf[i]++;
138
0
                if (buf[i] != 0)
139
0
                    break;
140
0
            }
141
0
142
0
            /* step 2 */
143
0
            if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL))
144
0
                goto err;
145
0
            if (!EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL))
146
0
                goto err;
147
0
            for (i = 0; i < qsize; i++)
148
0
                md[i] ^= buf2[i];
149
0
150
0
            /* step 3 */
151
0
            md[0] |= 0x80;
152
0
            md[qsize - 1] |= 0x01;
153
0
            if (!BN_bin2bn(md, qsize, q))
154
0
                goto err;
155
0
156
0
            /* step 4 */
157
0
            r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,
158
0
                                        use_random_seed, cb);
159
0
            if (r > 0)
160
0
                break;
161
0
            if (r != 0)
162
0
                goto err;
163
0
164
0
            /* do a callback call */
165
0
            /* step 5 */
166
0
        }
167
0
168
0
        if (!BN_GENCB_call(cb, 2, 0))
169
0
            goto err;
170
0
        if (!BN_GENCB_call(cb, 3, 0))
171
0
            goto err;
172
0
173
0
        /* step 6 */
174
0
        counter = 0;
175
0
        /* "offset = 2" */
176
0
177
0
        n = (bits - 1) / 160;
178
0
179
0
        for (;;) {
180
0
            if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
181
0
                goto err;
182
0
183
0
            /* step 7 */
184
0
            BN_zero(W);
185
0
            /* now 'buf' contains "SEED + offset - 1" */
186
0
            for (k = 0; k <= n; k++) {
187
0
                /*
188
0
                 * obtain "SEED + offset + k" by incrementing:
189
0
                 */
190
0
                for (i = qsize - 1; i >= 0; i--) {
191
0
                    buf[i]++;
192
0
                    if (buf[i] != 0)
193
0
                        break;
194
0
                }
195
0
196
0
                if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL))
197
0
                    goto err;
198
0
199
0
                /* step 8 */
200
0
                if (!BN_bin2bn(md, qsize, r0))
201
0
                    goto err;
202
0
                if (!BN_lshift(r0, r0, (qsize << 3) * k))
203
0
                    goto err;
204
0
                if (!BN_add(W, W, r0))
205
0
                    goto err;
206
0
            }
207
0
208
0
            /* more of step 8 */
209
0
            if (!BN_mask_bits(W, bits - 1))
210
0
                goto err;
211
0
            if (!BN_copy(X, W))
212
0
                goto err;
213
0
            if (!BN_add(X, X, test))
214
0
                goto err;
215
0
216
0
            /* step 9 */
217
0
            if (!BN_lshift1(r0, q))
218
0
                goto err;
219
0
            if (!BN_mod(c, X, r0, ctx))
220
0
                goto err;
221
0
            if (!BN_sub(r0, c, BN_value_one()))
222
0
                goto err;
223
0
            if (!BN_sub(p, X, r0))
224
0
                goto err;
225
0
226
0
            /* step 10 */
227
0
            if (BN_cmp(p, test) >= 0) {
228
0
                /* step 11 */
229
0
                r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
230
0
                if (r > 0)
231
0
                    goto end;   /* found it */
232
0
                if (r != 0)
233
0
                    goto err;
234
0
            }
235
0
236
0
            /* step 13 */
237
0
            counter++;
238
0
            /* "offset = offset + n + 1" */
239
0
240
0
            /* step 14 */
241
0
            if (counter >= 4096)
242
0
                break;
243
0
        }
244
0
    }
245
0
 end:
246
0
    if (!BN_GENCB_call(cb, 2, 1))
247
0
        goto err;
248
0
249
0
    /* We now need to generate g */
250
0
    /* Set r0=(p-1)/q */
251
0
    if (!BN_sub(test, p, BN_value_one()))
252
0
        goto err;
253
0
    if (!BN_div(r0, NULL, test, q, ctx))
254
0
        goto err;
255
0
256
0
    if (!BN_set_word(test, h))
257
0
        goto err;
258
0
    if (!BN_MONT_CTX_set(mont, p, ctx))
259
0
        goto err;
260
0
261
0
    for (;;) {
262
0
        /* g=test^r0%p */
263
0
        if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))
264
0
            goto err;
265
0
        if (!BN_is_one(g))
266
0
            break;
267
0
        if (!BN_add(test, test, BN_value_one()))
268
0
            goto err;
269
0
        h++;
270
0
    }
271
0
272
0
    if (!BN_GENCB_call(cb, 3, 1))
273
0
        goto err;
274
0
275
0
    ok = 1;
276
0
 err:
277
0
    if (ok) {
278
0
        BN_free(ret->p);
279
0
        BN_free(ret->q);
280
0
        BN_free(ret->g);
281
0
        ret->p = BN_dup(p);
282
0
        ret->q = BN_dup(q);
283
0
        ret->g = BN_dup(g);
284
0
        if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
285
0
            ok = 0;
286
0
            goto err;
287
0
        }
288
0
        if (counter_ret != NULL)
289
0
            *counter_ret = counter;
290
0
        if (h_ret != NULL)
291
0
            *h_ret = h;
292
0
        if (seed_out)
293
0
            memcpy(seed_out, seed, qsize);
294
0
    }
295
0
    if (ctx)
296
0
        BN_CTX_end(ctx);
297
0
    BN_CTX_free(ctx);
298
0
    BN_MONT_CTX_free(mont);
299
0
    return ok;
300
0
}
301
302
/*
303
 * This is a parameter generation algorithm for the DSA2 algorithm as
304
 * described in FIPS 186-3.
305
 */
306
307
int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
308
                          const EVP_MD *evpmd, const unsigned char *seed_in,
309
                          size_t seed_len, int idx, unsigned char *seed_out,
310
                          int *counter_ret, unsigned long *h_ret,
311
                          BN_GENCB *cb)
312
0
{
313
0
    int ok = -1;
314
0
    unsigned char *seed = NULL, *seed_tmp = NULL;
315
0
    unsigned char md[EVP_MAX_MD_SIZE];
316
0
    int mdsize;
317
0
    BIGNUM *r0, *W, *X, *c, *test;
318
0
    BIGNUM *g = NULL, *q = NULL, *p = NULL;
319
0
    BN_MONT_CTX *mont = NULL;
320
0
    int i, k, n = 0, m = 0, qsize = N >> 3;
321
0
    int counter = 0;
322
0
    int r = 0;
323
0
    BN_CTX *ctx = NULL;
324
0
    EVP_MD_CTX *mctx = EVP_MD_CTX_new();
325
0
    unsigned int h = 2;
326
0
327
0
    if (mctx == NULL)
328
0
        goto err;
329
0
330
0
    if (evpmd == NULL) {
331
0
        if (N == 160)
332
0
            evpmd = EVP_sha1();
333
0
        else if (N == 224)
334
0
            evpmd = EVP_sha224();
335
0
        else
336
0
            evpmd = EVP_sha256();
337
0
    }
338
0
339
0
    mdsize = EVP_MD_size(evpmd);
340
0
    /* If unverifiable g generation only don't need seed */
341
0
    if (!ret->p || !ret->q || idx >= 0) {
342
0
        if (seed_len == 0)
343
0
            seed_len = mdsize;
344
0
345
0
        seed = OPENSSL_malloc(seed_len);
346
0
347
0
        if (seed_out)
348
0
            seed_tmp = seed_out;
349
0
        else
350
0
            seed_tmp = OPENSSL_malloc(seed_len);
351
0
352
0
        if (seed == NULL || seed_tmp == NULL)
353
0
            goto err;
354
0
355
0
        if (seed_in)
356
0
            memcpy(seed, seed_in, seed_len);
357
0
358
0
    }
359
0
360
0
    if ((ctx = BN_CTX_new()) == NULL)
361
0
        goto err;
362
0
363
0
    if ((mont = BN_MONT_CTX_new()) == NULL)
364
0
        goto err;
365
0
366
0
    BN_CTX_start(ctx);
367
0
    r0 = BN_CTX_get(ctx);
368
0
    g = BN_CTX_get(ctx);
369
0
    W = BN_CTX_get(ctx);
370
0
    X = BN_CTX_get(ctx);
371
0
    c = BN_CTX_get(ctx);
372
0
    test = BN_CTX_get(ctx);
373
0
    if (test == NULL)
374
0
        goto err;
375
0
376
0
    /* if p, q already supplied generate g only */
377
0
    if (ret->p && ret->q) {
378
0
        p = ret->p;
379
0
        q = ret->q;
380
0
        if (idx >= 0)
381
0
            memcpy(seed_tmp, seed, seed_len);
382
0
        goto g_only;
383
0
    } else {
384
0
        p = BN_CTX_get(ctx);
385
0
        q = BN_CTX_get(ctx);
386
0
        if (q == NULL)
387
0
            goto err;
388
0
    }
389
0
390
0
    if (!BN_lshift(test, BN_value_one(), L - 1))
391
0
        goto err;
392
0
    for (;;) {
393
0
        for (;;) {              /* find q */
394
0
            unsigned char *pmd;
395
0
            /* step 1 */
396
0
            if (!BN_GENCB_call(cb, 0, m++))
397
0
                goto err;
398
0
399
0
            if (!seed_in) {
400
0
                if (RAND_bytes(seed, seed_len) <= 0)
401
0
                    goto err;
402
0
            }
403
0
            /* step 2 */
404
0
            if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))
405
0
                goto err;
406
0
            /* Take least significant bits of md */
407
0
            if (mdsize > qsize)
408
0
                pmd = md + mdsize - qsize;
409
0
            else
410
0
                pmd = md;
411
0
412
0
            if (mdsize < qsize)
413
0
                memset(md + mdsize, 0, qsize - mdsize);
414
0
415
0
            /* step 3 */
416
0
            pmd[0] |= 0x80;
417
0
            pmd[qsize - 1] |= 0x01;
418
0
            if (!BN_bin2bn(pmd, qsize, q))
419
0
                goto err;
420
0
421
0
            /* step 4 */
422
0
            r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,
423
0
                                        seed_in ? 1 : 0, cb);
424
0
            if (r > 0)
425
0
                break;
426
0
            if (r != 0)
427
0
                goto err;
428
0
            /* Provided seed didn't produce a prime: error */
429
0
            if (seed_in) {
430
0
                ok = 0;
431
0
                DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_Q_NOT_PRIME);
432
0
                goto err;
433
0
            }
434
0
435
0
            /* do a callback call */
436
0
            /* step 5 */
437
0
        }
438
0
        /* Copy seed to seed_out before we mess with it */
439
0
        if (seed_out)
440
0
            memcpy(seed_out, seed, seed_len);
441
0
442
0
        if (!BN_GENCB_call(cb, 2, 0))
443
0
            goto err;
444
0
        if (!BN_GENCB_call(cb, 3, 0))
445
0
            goto err;
446
0
447
0
        /* step 6 */
448
0
        counter = 0;
449
0
        /* "offset = 1" */
450
0
451
0
        n = (L - 1) / (mdsize << 3);
452
0
453
0
        for (;;) {
454
0
            if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
455
0
                goto err;
456
0
457
0
            /* step 7 */
458
0
            BN_zero(W);
459
0
            /* now 'buf' contains "SEED + offset - 1" */
460
0
            for (k = 0; k <= n; k++) {
461
0
                /*
462
0
                 * obtain "SEED + offset + k" by incrementing:
463
0
                 */
464
0
                for (i = seed_len - 1; i >= 0; i--) {
465
0
                    seed[i]++;
466
0
                    if (seed[i] != 0)
467
0
                        break;
468
0
                }
469
0
470
0
                if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))
471
0
                    goto err;
472
0
473
0
                /* step 8 */
474
0
                if (!BN_bin2bn(md, mdsize, r0))
475
0
                    goto err;
476
0
                if (!BN_lshift(r0, r0, (mdsize << 3) * k))
477
0
                    goto err;
478
0
                if (!BN_add(W, W, r0))
479
0
                    goto err;
480
0
            }
481
0
482
0
            /* more of step 8 */
483
0
            if (!BN_mask_bits(W, L - 1))
484
0
                goto err;
485
0
            if (!BN_copy(X, W))
486
0
                goto err;
487
0
            if (!BN_add(X, X, test))
488
0
                goto err;
489
0
490
0
            /* step 9 */
491
0
            if (!BN_lshift1(r0, q))
492
0
                goto err;
493
0
            if (!BN_mod(c, X, r0, ctx))
494
0
                goto err;
495
0
            if (!BN_sub(r0, c, BN_value_one()))
496
0
                goto err;
497
0
            if (!BN_sub(p, X, r0))
498
0
                goto err;
499
0
500
0
            /* step 10 */
501
0
            if (BN_cmp(p, test) >= 0) {
502
0
                /* step 11 */
503
0
                r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
504
0
                if (r > 0)
505
0
                    goto end;   /* found it */
506
0
                if (r != 0)
507
0
                    goto err;
508
0
            }
509
0
510
0
            /* step 13 */
511
0
            counter++;
512
0
            /* "offset = offset + n + 1" */
513
0
514
0
            /* step 14 */
515
0
            if (counter >= (int)(4 * L))
516
0
                break;
517
0
        }
518
0
        if (seed_in) {
519
0
            ok = 0;
520
0
            DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS);
521
0
            goto err;
522
0
        }
523
0
    }
524
0
 end:
525
0
    if (!BN_GENCB_call(cb, 2, 1))
526
0
        goto err;
527
0
528
0
 g_only:
529
0
530
0
    /* We now need to generate g */
531
0
    /* Set r0=(p-1)/q */
532
0
    if (!BN_sub(test, p, BN_value_one()))
533
0
        goto err;
534
0
    if (!BN_div(r0, NULL, test, q, ctx))
535
0
        goto err;
536
0
537
0
    if (idx < 0) {
538
0
        if (!BN_set_word(test, h))
539
0
            goto err;
540
0
    } else
541
0
        h = 1;
542
0
    if (!BN_MONT_CTX_set(mont, p, ctx))
543
0
        goto err;
544
0
545
0
    for (;;) {
546
0
        static const unsigned char ggen[4] = { 0x67, 0x67, 0x65, 0x6e };
547
0
        if (idx >= 0) {
548
0
            md[0] = idx & 0xff;
549
0
            md[1] = (h >> 8) & 0xff;
550
0
            md[2] = h & 0xff;
551
0
            if (!EVP_DigestInit_ex(mctx, evpmd, NULL))
552
0
                goto err;
553
0
            if (!EVP_DigestUpdate(mctx, seed_tmp, seed_len))
554
0
                goto err;
555
0
            if (!EVP_DigestUpdate(mctx, ggen, sizeof(ggen)))
556
0
                goto err;
557
0
            if (!EVP_DigestUpdate(mctx, md, 3))
558
0
                goto err;
559
0
            if (!EVP_DigestFinal_ex(mctx, md, NULL))
560
0
                goto err;
561
0
            if (!BN_bin2bn(md, mdsize, test))
562
0
                goto err;
563
0
        }
564
0
        /* g=test^r0%p */
565
0
        if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))
566
0
            goto err;
567
0
        if (!BN_is_one(g))
568
0
            break;
569
0
        if (idx < 0 && !BN_add(test, test, BN_value_one()))
570
0
            goto err;
571
0
        h++;
572
0
        if (idx >= 0 && h > 0xffff)
573
0
            goto err;
574
0
    }
575
0
576
0
    if (!BN_GENCB_call(cb, 3, 1))
577
0
        goto err;
578
0
579
0
    ok = 1;
580
0
 err:
581
0
    if (ok == 1) {
582
0
        if (p != ret->p) {
583
0
            BN_free(ret->p);
584
0
            ret->p = BN_dup(p);
585
0
        }
586
0
        if (q != ret->q) {
587
0
            BN_free(ret->q);
588
0
            ret->q = BN_dup(q);
589
0
        }
590
0
        BN_free(ret->g);
591
0
        ret->g = BN_dup(g);
592
0
        if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
593
0
            ok = -1;
594
0
            goto err;
595
0
        }
596
0
        if (counter_ret != NULL)
597
0
            *counter_ret = counter;
598
0
        if (h_ret != NULL)
599
0
            *h_ret = h;
600
0
    }
601
0
    OPENSSL_free(seed);
602
0
    if (seed_out != seed_tmp)
603
0
        OPENSSL_free(seed_tmp);
604
0
    if (ctx)
605
0
        BN_CTX_end(ctx);
606
0
    BN_CTX_free(ctx);
607
0
    BN_MONT_CTX_free(mont);
608
0
    EVP_MD_CTX_free(mctx);
609
0
    return ok;
610
0
}