155714Skris/* crypto/dsa/dsa_gen.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8296465Sdelphij *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15296465Sdelphij *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22296465Sdelphij *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37296465Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40296465Sdelphij *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52296465Sdelphij *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#undef GENUINE_DSA
6055714Skris
6155714Skris#ifdef GENUINE_DSA
62296465Sdelphij/*
63296465Sdelphij * Parameter generation follows the original release of FIPS PUB 186,
64296465Sdelphij * Appendix 2.2 (i.e. use SHA as defined in FIPS PUB 180)
65296465Sdelphij */
66296465Sdelphij# define HASH    EVP_sha()
6755714Skris#else
68296465Sdelphij/*
69296465Sdelphij * Parameter generation follows the updated Appendix 2.2 for FIPS PUB 186,
70296465Sdelphij * also Appendix 2.2 of FIPS PUB 186-1 (i.e. use SHA as defined in FIPS PUB
71296465Sdelphij * 180-1)
72296465Sdelphij */
73296465Sdelphij# define HASH    EVP_sha1()
74296465Sdelphij#endif
7555714Skris
76160814Ssimon#include <openssl/opensslconf.h> /* To see if OPENSSL_NO_SHA is defined */
77160814Ssimon
78109998Smarkm#ifndef OPENSSL_NO_SHA
7959191Skris
80296465Sdelphij# include <stdio.h>
81296465Sdelphij# include <time.h>
82296465Sdelphij# include "cryptlib.h"
83296465Sdelphij# include <openssl/evp.h>
84296465Sdelphij# include <openssl/bn.h>
85296465Sdelphij# include <openssl/dsa.h>
86296465Sdelphij# include <openssl/rand.h>
87296465Sdelphij# include <openssl/sha.h>
8855714Skris
89296465Sdelphij# ifndef OPENSSL_FIPS
90194206Ssimon
91160814Ssimonstatic int dsa_builtin_paramgen(DSA *ret, int bits,
92296465Sdelphij                                unsigned char *seed_in, int seed_len,
93296465Sdelphij                                int *counter_ret, unsigned long *h_ret,
94296465Sdelphij                                BN_GENCB *cb);
95160814Ssimon
96160814Ssimonint DSA_generate_parameters_ex(DSA *ret, int bits,
97296465Sdelphij                               unsigned char *seed_in, int seed_len,
98296465Sdelphij                               int *counter_ret, unsigned long *h_ret,
99296465Sdelphij                               BN_GENCB *cb)
100296465Sdelphij{
101296465Sdelphij    if (ret->meth->dsa_paramgen)
102296465Sdelphij        return ret->meth->dsa_paramgen(ret, bits, seed_in, seed_len,
103296465Sdelphij                                       counter_ret, h_ret, cb);
104296465Sdelphij    return dsa_builtin_paramgen(ret, bits, seed_in, seed_len,
105296465Sdelphij                                counter_ret, h_ret, cb);
106296465Sdelphij}
107160814Ssimon
108160814Ssimonstatic int dsa_builtin_paramgen(DSA *ret, int bits,
109296465Sdelphij                                unsigned char *seed_in, int seed_len,
110296465Sdelphij                                int *counter_ret, unsigned long *h_ret,
111296465Sdelphij                                BN_GENCB *cb)
112296465Sdelphij{
113296465Sdelphij    int ok = 0;
114296465Sdelphij    unsigned char seed[SHA_DIGEST_LENGTH];
115296465Sdelphij    unsigned char md[SHA_DIGEST_LENGTH];
116296465Sdelphij    unsigned char buf[SHA_DIGEST_LENGTH], buf2[SHA_DIGEST_LENGTH];
117296465Sdelphij    BIGNUM *r0, *W, *X, *c, *test;
118296465Sdelphij    BIGNUM *g = NULL, *q = NULL, *p = NULL;
119296465Sdelphij    BN_MONT_CTX *mont = NULL;
120296465Sdelphij    int k, n = 0, i, m = 0;
121296465Sdelphij    int counter = 0;
122296465Sdelphij    int r = 0;
123296465Sdelphij    BN_CTX *ctx = NULL;
124296465Sdelphij    unsigned int h = 2;
12555714Skris
126296465Sdelphij    if (bits < 512)
127296465Sdelphij        bits = 512;
128296465Sdelphij    bits = (bits + 63) / 64 * 64;
12955714Skris
130296465Sdelphij    /*
131296465Sdelphij     * NB: seed_len == 0 is special case: copy generated seed to seed_in if
132296465Sdelphij     * it is not NULL.
133296465Sdelphij     */
134296465Sdelphij    if (seed_len && (seed_len < 20))
135296465Sdelphij        seed_in = NULL;         /* seed buffer too small -- ignore */
136296465Sdelphij    if (seed_len > 20)
137296465Sdelphij        seed_len = 20;          /* App. 2.2 of FIPS PUB 186 allows larger
138296465Sdelphij                                 * SEED, but our internal buffers are
139296465Sdelphij                                 * restricted to 160 bits */
140296465Sdelphij    if ((seed_in != NULL) && (seed_len == 20)) {
141296465Sdelphij        memcpy(seed, seed_in, seed_len);
142296465Sdelphij        /* set seed_in to NULL to avoid it being copied back */
143296465Sdelphij        seed_in = NULL;
144296465Sdelphij    }
14555714Skris
146296465Sdelphij    if ((ctx = BN_CTX_new()) == NULL)
147296465Sdelphij        goto err;
14855714Skris
149296465Sdelphij    if ((mont = BN_MONT_CTX_new()) == NULL)
150296465Sdelphij        goto err;
15155714Skris
152296465Sdelphij    BN_CTX_start(ctx);
153296465Sdelphij    r0 = BN_CTX_get(ctx);
154296465Sdelphij    g = BN_CTX_get(ctx);
155296465Sdelphij    W = BN_CTX_get(ctx);
156296465Sdelphij    q = BN_CTX_get(ctx);
157296465Sdelphij    X = BN_CTX_get(ctx);
158296465Sdelphij    c = BN_CTX_get(ctx);
159296465Sdelphij    p = BN_CTX_get(ctx);
160296465Sdelphij    test = BN_CTX_get(ctx);
16155714Skris
162296465Sdelphij    if (!BN_lshift(test, BN_value_one(), bits - 1))
163296465Sdelphij        goto err;
16455714Skris
165296465Sdelphij    for (;;) {
166296465Sdelphij        for (;;) {              /* find q */
167296465Sdelphij            int seed_is_random;
16859191Skris
169296465Sdelphij            /* step 1 */
170296465Sdelphij            if (!BN_GENCB_call(cb, 0, m++))
171296465Sdelphij                goto err;
17255714Skris
173296465Sdelphij            if (!seed_len) {
174296465Sdelphij                RAND_pseudo_bytes(seed, SHA_DIGEST_LENGTH);
175296465Sdelphij                seed_is_random = 1;
176296465Sdelphij            } else {
177296465Sdelphij                seed_is_random = 0;
178296465Sdelphij                seed_len = 0;   /* use random seed if 'seed_in' turns out to
179296465Sdelphij                                 * be bad */
180296465Sdelphij            }
181296465Sdelphij            memcpy(buf, seed, SHA_DIGEST_LENGTH);
182296465Sdelphij            memcpy(buf2, seed, SHA_DIGEST_LENGTH);
183296465Sdelphij            /* precompute "SEED + 1" for step 7: */
184296465Sdelphij            for (i = SHA_DIGEST_LENGTH - 1; i >= 0; i--) {
185296465Sdelphij                buf[i]++;
186296465Sdelphij                if (buf[i] != 0)
187296465Sdelphij                    break;
188296465Sdelphij            }
18955714Skris
190296465Sdelphij            /* step 2 */
191296465Sdelphij            EVP_Digest(seed, SHA_DIGEST_LENGTH, md, NULL, HASH, NULL);
192296465Sdelphij            EVP_Digest(buf, SHA_DIGEST_LENGTH, buf2, NULL, HASH, NULL);
193296465Sdelphij            for (i = 0; i < SHA_DIGEST_LENGTH; i++)
194296465Sdelphij                md[i] ^= buf2[i];
19555714Skris
196296465Sdelphij            /* step 3 */
197296465Sdelphij            md[0] |= 0x80;
198296465Sdelphij            md[SHA_DIGEST_LENGTH - 1] |= 0x01;
199296465Sdelphij            if (!BN_bin2bn(md, SHA_DIGEST_LENGTH, q))
200296465Sdelphij                goto err;
20155714Skris
202296465Sdelphij            /* step 4 */
203296465Sdelphij            r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,
204296465Sdelphij                                        seed_is_random, cb);
205296465Sdelphij            if (r > 0)
206296465Sdelphij                break;
207296465Sdelphij            if (r != 0)
208296465Sdelphij                goto err;
20959191Skris
210296465Sdelphij            /* do a callback call */
211296465Sdelphij            /* step 5 */
212296465Sdelphij        }
21355714Skris
214296465Sdelphij        if (!BN_GENCB_call(cb, 2, 0))
215296465Sdelphij            goto err;
216296465Sdelphij        if (!BN_GENCB_call(cb, 3, 0))
217296465Sdelphij            goto err;
21855714Skris
219296465Sdelphij        /* step 6 */
220296465Sdelphij        counter = 0;
221296465Sdelphij        /* "offset = 2" */
22255714Skris
223296465Sdelphij        n = (bits - 1) / 160;
22455714Skris
225296465Sdelphij        for (;;) {
226296465Sdelphij            if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
227296465Sdelphij                goto err;
22859191Skris
229296465Sdelphij            /* step 7 */
230296465Sdelphij            BN_zero(W);
231296465Sdelphij            /* now 'buf' contains "SEED + offset - 1" */
232296465Sdelphij            for (k = 0; k <= n; k++) {
233296465Sdelphij                /*
234296465Sdelphij                 * obtain "SEED + offset + k" by incrementing:
235296465Sdelphij                 */
236296465Sdelphij                for (i = SHA_DIGEST_LENGTH - 1; i >= 0; i--) {
237296465Sdelphij                    buf[i]++;
238296465Sdelphij                    if (buf[i] != 0)
239296465Sdelphij                        break;
240296465Sdelphij                }
24155714Skris
242296465Sdelphij                EVP_Digest(buf, SHA_DIGEST_LENGTH, md, NULL, HASH, NULL);
24355714Skris
244296465Sdelphij                /* step 8 */
245296465Sdelphij                if (!BN_bin2bn(md, SHA_DIGEST_LENGTH, r0))
246296465Sdelphij                    goto err;
247296465Sdelphij                if (!BN_lshift(r0, r0, 160 * k))
248296465Sdelphij                    goto err;
249296465Sdelphij                if (!BN_add(W, W, r0))
250296465Sdelphij                    goto err;
251296465Sdelphij            }
25255714Skris
253296465Sdelphij            /* more of step 8 */
254296465Sdelphij            if (!BN_mask_bits(W, bits - 1))
255296465Sdelphij                goto err;
256296465Sdelphij            if (!BN_copy(X, W))
257296465Sdelphij                goto err;
258296465Sdelphij            if (!BN_add(X, X, test))
259296465Sdelphij                goto err;
26055714Skris
261296465Sdelphij            /* step 9 */
262296465Sdelphij            if (!BN_lshift1(r0, q))
263296465Sdelphij                goto err;
264296465Sdelphij            if (!BN_mod(c, X, r0, ctx))
265296465Sdelphij                goto err;
266296465Sdelphij            if (!BN_sub(r0, c, BN_value_one()))
267296465Sdelphij                goto err;
268296465Sdelphij            if (!BN_sub(p, X, r0))
269296465Sdelphij                goto err;
27055714Skris
271296465Sdelphij            /* step 10 */
272296465Sdelphij            if (BN_cmp(p, test) >= 0) {
273296465Sdelphij                /* step 11 */
274296465Sdelphij                r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
275296465Sdelphij                if (r > 0)
276296465Sdelphij                    goto end;   /* found it */
277296465Sdelphij                if (r != 0)
278296465Sdelphij                    goto err;
279296465Sdelphij            }
28055714Skris
281296465Sdelphij            /* step 13 */
282296465Sdelphij            counter++;
283296465Sdelphij            /* "offset = offset + n + 1" */
28455714Skris
285296465Sdelphij            /* step 14 */
286296465Sdelphij            if (counter >= 4096)
287296465Sdelphij                break;
288296465Sdelphij        }
289296465Sdelphij    }
290296465Sdelphij end:
291296465Sdelphij    if (!BN_GENCB_call(cb, 2, 1))
292296465Sdelphij        goto err;
29355714Skris
294296465Sdelphij    /* We now need to generate g */
295296465Sdelphij    /* Set r0=(p-1)/q */
296296465Sdelphij    if (!BN_sub(test, p, BN_value_one()))
297296465Sdelphij        goto err;
298296465Sdelphij    if (!BN_div(r0, NULL, test, q, ctx))
299296465Sdelphij        goto err;
30055714Skris
301296465Sdelphij    if (!BN_set_word(test, h))
302296465Sdelphij        goto err;
303296465Sdelphij    if (!BN_MONT_CTX_set(mont, p, ctx))
304296465Sdelphij        goto err;
30555714Skris
306296465Sdelphij    for (;;) {
307296465Sdelphij        /* g=test^r0%p */
308296465Sdelphij        if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))
309296465Sdelphij            goto err;
310296465Sdelphij        if (!BN_is_one(g))
311296465Sdelphij            break;
312296465Sdelphij        if (!BN_add(test, test, BN_value_one()))
313296465Sdelphij            goto err;
314296465Sdelphij        h++;
315296465Sdelphij    }
31655714Skris
317296465Sdelphij    if (!BN_GENCB_call(cb, 3, 1))
318296465Sdelphij        goto err;
31955714Skris
320296465Sdelphij    ok = 1;
321296465Sdelphij err:
322296465Sdelphij    if (ok) {
323296465Sdelphij        if (ret->p)
324296465Sdelphij            BN_free(ret->p);
325296465Sdelphij        if (ret->q)
326296465Sdelphij            BN_free(ret->q);
327296465Sdelphij        if (ret->g)
328296465Sdelphij            BN_free(ret->g);
329296465Sdelphij        ret->p = BN_dup(p);
330296465Sdelphij        ret->q = BN_dup(q);
331296465Sdelphij        ret->g = BN_dup(g);
332296465Sdelphij        if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
333296465Sdelphij            ok = 0;
334296465Sdelphij            goto err;
335296465Sdelphij        }
336296465Sdelphij        if (seed_in != NULL)
337296465Sdelphij            memcpy(seed_in, seed, 20);
338296465Sdelphij        if (counter_ret != NULL)
339296465Sdelphij            *counter_ret = counter;
340296465Sdelphij        if (h_ret != NULL)
341296465Sdelphij            *h_ret = h;
342296465Sdelphij    }
343296465Sdelphij    if (ctx) {
344296465Sdelphij        BN_CTX_end(ctx);
345296465Sdelphij        BN_CTX_free(ctx);
346296465Sdelphij    }
347296465Sdelphij    if (mont != NULL)
348296465Sdelphij        BN_MONT_CTX_free(mont);
349296465Sdelphij    return ok;
350296465Sdelphij}
351296465Sdelphij# endif
352160814Ssimon#endif
353