847c874ad22dd1f05c3d18c233126149a49dbe73
[openssl.git] / crypto / dsa / dsa_gen.c
1 /* crypto/dsa/dsa_gen.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #undef GENUINE_DSA
60
61 #ifdef GENUINE_DSA
62 /*
63  * Parameter generation follows the original release of FIPS PUB 186,
64  * Appendix 2.2 (i.e. use SHA as defined in FIPS PUB 180)
65  */
66 # define HASH    EVP_sha()
67 #else
68 /*
69  * Parameter generation follows the updated Appendix 2.2 for FIPS PUB 186,
70  * also Appendix 2.2 of FIPS PUB 186-1 (i.e. use SHA as defined in FIPS PUB
71  * 180-1)
72  */
73 # define HASH    EVP_sha1()
74 #endif
75
76 #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_SHA is defined */
77
78 #ifndef OPENSSL_NO_SHA
79
80 # include <stdio.h>
81 # include "cryptlib.h"
82 # include <openssl/evp.h>
83 # include <openssl/bn.h>
84 # include <openssl/rand.h>
85 # include <openssl/sha.h>
86 # include "dsa_locl.h"
87
88 # ifdef OPENSSL_FIPS
89 /* Workaround bug in prototype */
90 #  define fips_dsa_builtin_paramgen2 fips_dsa_paramgen_bad
91 #  include <openssl/fips.h>
92 # endif
93
94 int DSA_generate_parameters_ex(DSA *ret, int bits,
95                                const unsigned char *seed_in, int seed_len,
96                                int *counter_ret, unsigned long *h_ret,
97                                BN_GENCB *cb)
98 {
99 # ifdef OPENSSL_FIPS
100     if (FIPS_mode() && !(ret->meth->flags & DSA_FLAG_FIPS_METHOD)
101         && !(ret->flags & DSA_FLAG_NON_FIPS_ALLOW)) {
102         DSAerr(DSA_F_DSA_GENERATE_PARAMETERS_EX, DSA_R_NON_FIPS_DSA_METHOD);
103         return 0;
104     }
105 # endif
106     if (ret->meth->dsa_paramgen)
107         return ret->meth->dsa_paramgen(ret, bits, seed_in, seed_len,
108                                        counter_ret, h_ret, cb);
109 # ifdef OPENSSL_FIPS
110     else if (FIPS_mode()) {
111         return FIPS_dsa_generate_parameters_ex(ret, bits,
112                                                seed_in, seed_len,
113                                                counter_ret, h_ret, cb);
114     }
115 # endif
116     else {
117         const EVP_MD *evpmd;
118         size_t qbits = bits >= 2048 ? 256 : 160;
119
120         if (bits >= 2048) {
121             qbits = 256;
122             evpmd = EVP_sha256();
123         } else {
124             qbits = 160;
125             evpmd = EVP_sha1();
126         }
127
128         return dsa_builtin_paramgen(ret, bits, qbits, evpmd,
129                                     seed_in, seed_len, NULL, counter_ret,
130                                     h_ret, cb);
131     }
132 }
133
134 int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
135                          const EVP_MD *evpmd, const unsigned char *seed_in,
136                          size_t seed_len, unsigned char *seed_out,
137                          int *counter_ret, unsigned long *h_ret, BN_GENCB *cb)
138 {
139     int ok = 0;
140     unsigned char seed[SHA256_DIGEST_LENGTH];
141     unsigned char md[SHA256_DIGEST_LENGTH];
142     unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH];
143     BIGNUM *r0, *W, *X, *c, *test;
144     BIGNUM *g = NULL, *q = NULL, *p = NULL;
145     BN_MONT_CTX *mont = NULL;
146     int i, k, n = 0, m = 0, qsize = qbits >> 3;
147     int counter = 0;
148     int r = 0;
149     BN_CTX *ctx = NULL;
150     unsigned int h = 2;
151
152     if (qsize != SHA_DIGEST_LENGTH && qsize != SHA224_DIGEST_LENGTH &&
153         qsize != SHA256_DIGEST_LENGTH)
154         /* invalid q size */
155         return 0;
156
157     if (evpmd == NULL)
158         /* use SHA1 as default */
159         evpmd = EVP_sha1();
160
161     if (bits < 512)
162         bits = 512;
163
164     bits = (bits + 63) / 64 * 64;
165
166     if (seed_in != NULL) {
167         if (seed_len < (size_t)qsize)
168             return 0;
169         if (seed_len > (size_t)qsize) {
170             /* Don't overflow seed local variable. */
171             seed_len = qsize;
172         }
173         memcpy(seed, seed_in, seed_len);
174     }
175
176     if ((ctx = BN_CTX_new()) == NULL)
177         goto err;
178
179     if ((mont = BN_MONT_CTX_new()) == NULL)
180         goto err;
181
182     BN_CTX_start(ctx);
183     r0 = BN_CTX_get(ctx);
184     g = BN_CTX_get(ctx);
185     W = BN_CTX_get(ctx);
186     q = BN_CTX_get(ctx);
187     X = BN_CTX_get(ctx);
188     c = BN_CTX_get(ctx);
189     p = BN_CTX_get(ctx);
190     test = BN_CTX_get(ctx);
191
192     if (!BN_lshift(test, BN_value_one(), bits - 1))
193         goto err;
194
195     for (;;) {
196         for (;;) {              /* find q */
197             int seed_is_random = seed_in == NULL;
198
199             /* step 1 */
200             if (!BN_GENCB_call(cb, 0, m++))
201                 goto err;
202
203             if (seed_is_random) {
204                 if (RAND_bytes(seed, qsize) <= 0)
205                     goto err;
206             } else {
207                 /* If we come back through, use random seed next time. */
208                 seed_in = NULL;
209             }
210             memcpy(buf, seed, qsize);
211             memcpy(buf2, seed, qsize);
212             /* precompute "SEED + 1" for step 7: */
213             for (i = qsize - 1; i >= 0; i--) {
214                 buf[i]++;
215                 if (buf[i] != 0)
216                     break;
217             }
218
219             /* step 2 */
220             if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL))
221                 goto err;
222             if (!EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL))
223                 goto err;
224             for (i = 0; i < qsize; i++)
225                 md[i] ^= buf2[i];
226
227             /* step 3 */
228             md[0] |= 0x80;
229             md[qsize - 1] |= 0x01;
230             if (!BN_bin2bn(md, qsize, q))
231                 goto err;
232
233             /* step 4 */
234             r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,
235                                         seed_is_random, cb);
236             if (r > 0)
237                 break;
238             if (r != 0)
239                 goto err;
240
241             /* do a callback call */
242             /* step 5 */
243         }
244
245         if (!BN_GENCB_call(cb, 2, 0))
246             goto err;
247         if (!BN_GENCB_call(cb, 3, 0))
248             goto err;
249
250         /* step 6 */
251         counter = 0;
252         /* "offset = 2" */
253
254         n = (bits - 1) / 160;
255
256         for (;;) {
257             if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
258                 goto err;
259
260             /* step 7 */
261             BN_zero(W);
262             /* now 'buf' contains "SEED + offset - 1" */
263             for (k = 0; k <= n; k++) {
264                 /*
265                  * obtain "SEED + offset + k" by incrementing:
266                  */
267                 for (i = qsize - 1; i >= 0; i--) {
268                     buf[i]++;
269                     if (buf[i] != 0)
270                         break;
271                 }
272
273                 if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL))
274                     goto err;
275
276                 /* step 8 */
277                 if (!BN_bin2bn(md, qsize, r0))
278                     goto err;
279                 if (!BN_lshift(r0, r0, (qsize << 3) * k))
280                     goto err;
281                 if (!BN_add(W, W, r0))
282                     goto err;
283             }
284
285             /* more of step 8 */
286             if (!BN_mask_bits(W, bits - 1))
287                 goto err;
288             if (!BN_copy(X, W))
289                 goto err;
290             if (!BN_add(X, X, test))
291                 goto err;
292
293             /* step 9 */
294             if (!BN_lshift1(r0, q))
295                 goto err;
296             if (!BN_mod(c, X, r0, ctx))
297                 goto err;
298             if (!BN_sub(r0, c, BN_value_one()))
299                 goto err;
300             if (!BN_sub(p, X, r0))
301                 goto err;
302
303             /* step 10 */
304             if (BN_cmp(p, test) >= 0) {
305                 /* step 11 */
306                 r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
307                 if (r > 0)
308                     goto end;   /* found it */
309                 if (r != 0)
310                     goto err;
311             }
312
313             /* step 13 */
314             counter++;
315             /* "offset = offset + n + 1" */
316
317             /* step 14 */
318             if (counter >= 4096)
319                 break;
320         }
321     }
322  end:
323     if (!BN_GENCB_call(cb, 2, 1))
324         goto err;
325
326     /* We now need to generate g */
327     /* Set r0=(p-1)/q */
328     if (!BN_sub(test, p, BN_value_one()))
329         goto err;
330     if (!BN_div(r0, NULL, test, q, ctx))
331         goto err;
332
333     if (!BN_set_word(test, h))
334         goto err;
335     if (!BN_MONT_CTX_set(mont, p, ctx))
336         goto err;
337
338     for (;;) {
339         /* g=test^r0%p */
340         if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))
341             goto err;
342         if (!BN_is_one(g))
343             break;
344         if (!BN_add(test, test, BN_value_one()))
345             goto err;
346         h++;
347     }
348
349     if (!BN_GENCB_call(cb, 3, 1))
350         goto err;
351
352     ok = 1;
353  err:
354     if (ok) {
355         if (ret->p)
356             BN_free(ret->p);
357         if (ret->q)
358             BN_free(ret->q);
359         if (ret->g)
360             BN_free(ret->g);
361         ret->p = BN_dup(p);
362         ret->q = BN_dup(q);
363         ret->g = BN_dup(g);
364         if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
365             ok = 0;
366             goto err;
367         }
368         if (counter_ret != NULL)
369             *counter_ret = counter;
370         if (h_ret != NULL)
371             *h_ret = h;
372         if (seed_out)
373             memcpy(seed_out, seed, qsize);
374     }
375     if (ctx) {
376         BN_CTX_end(ctx);
377         BN_CTX_free(ctx);
378     }
379     if (mont != NULL)
380         BN_MONT_CTX_free(mont);
381     return ok;
382 }
383
384 # ifdef OPENSSL_FIPS
385 #  undef fips_dsa_builtin_paramgen2
386 extern int fips_dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
387                                       const EVP_MD *evpmd,
388                                       const unsigned char *seed_in,
389                                       size_t seed_len, int idx,
390                                       unsigned char *seed_out,
391                                       int *counter_ret, unsigned long *h_ret,
392                                       BN_GENCB *cb);
393 # endif
394
395 /*
396  * This is a parameter generation algorithm for the DSA2 algorithm as
397  * described in FIPS 186-3.
398  */
399
400 int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
401                           const EVP_MD *evpmd, const unsigned char *seed_in,
402                           size_t seed_len, int idx, unsigned char *seed_out,
403                           int *counter_ret, unsigned long *h_ret,
404                           BN_GENCB *cb)
405 {
406     int ok = -1;
407     unsigned char *seed = NULL, *seed_tmp = NULL;
408     unsigned char md[EVP_MAX_MD_SIZE];
409     int mdsize;
410     BIGNUM *r0, *W, *X, *c, *test;
411     BIGNUM *g = NULL, *q = NULL, *p = NULL;
412     BN_MONT_CTX *mont = NULL;
413     int i, k, n = 0, m = 0, qsize = N >> 3;
414     int counter = 0;
415     int r = 0;
416     BN_CTX *ctx = NULL;
417     EVP_MD_CTX mctx;
418     unsigned int h = 2;
419
420 # ifdef OPENSSL_FIPS
421
422     if (FIPS_mode())
423         return fips_dsa_builtin_paramgen2(ret, L, N, evpmd,
424                                           seed_in, seed_len, idx,
425                                           seed_out, counter_ret, h_ret, cb);
426 # endif
427
428     EVP_MD_CTX_init(&mctx);
429
430     if (evpmd == NULL) {
431         if (N == 160)
432             evpmd = EVP_sha1();
433         else if (N == 224)
434             evpmd = EVP_sha224();
435         else
436             evpmd = EVP_sha256();
437     }
438
439     mdsize = EVP_MD_size(evpmd);
440     /* If unverificable g generation only don't need seed */
441     if (!ret->p || !ret->q || idx >= 0) {
442         if (seed_len == 0)
443             seed_len = mdsize;
444
445         seed = OPENSSL_malloc(seed_len);
446
447         if (seed_out)
448             seed_tmp = seed_out;
449         else
450             seed_tmp = OPENSSL_malloc(seed_len);
451
452         if (!seed || !seed_tmp)
453             goto err;
454
455         if (seed_in)
456             memcpy(seed, seed_in, seed_len);
457
458     }
459
460     if ((ctx = BN_CTX_new()) == NULL)
461         goto err;
462
463     if ((mont = BN_MONT_CTX_new()) == NULL)
464         goto err;
465
466     BN_CTX_start(ctx);
467     r0 = BN_CTX_get(ctx);
468     g = BN_CTX_get(ctx);
469     W = BN_CTX_get(ctx);
470     X = BN_CTX_get(ctx);
471     c = BN_CTX_get(ctx);
472     test = BN_CTX_get(ctx);
473
474     /* if p, q already supplied generate g only */
475     if (ret->p && ret->q) {
476         p = ret->p;
477         q = ret->q;
478         if (idx >= 0)
479             memcpy(seed_tmp, seed, seed_len);
480         goto g_only;
481     } else {
482         p = BN_CTX_get(ctx);
483         q = BN_CTX_get(ctx);
484     }
485
486     if (!BN_lshift(test, BN_value_one(), L - 1))
487         goto err;
488     for (;;) {
489         for (;;) {              /* find q */
490             unsigned char *pmd;
491             /* step 1 */
492             if (!BN_GENCB_call(cb, 0, m++))
493                 goto err;
494
495             if (!seed_in) {
496                 if (RAND_pseudo_bytes(seed, seed_len) < 0)
497                     goto err;
498             }
499             /* step 2 */
500             if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))
501                 goto err;
502             /* Take least significant bits of md */
503             if (mdsize > qsize)
504                 pmd = md + mdsize - qsize;
505             else
506                 pmd = md;
507
508             if (mdsize < qsize)
509                 memset(md + mdsize, 0, qsize - mdsize);
510
511             /* step 3 */
512             pmd[0] |= 0x80;
513             pmd[qsize - 1] |= 0x01;
514             if (!BN_bin2bn(pmd, qsize, q))
515                 goto err;
516
517             /* step 4 */
518             r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,
519                                         seed_in ? 1 : 0, cb);
520             if (r > 0)
521                 break;
522             if (r != 0)
523                 goto err;
524             /* Provided seed didn't produce a prime: error */
525             if (seed_in) {
526                 ok = 0;
527                 DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_Q_NOT_PRIME);
528                 goto err;
529             }
530
531             /* do a callback call */
532             /* step 5 */
533         }
534         /* Copy seed to seed_out before we mess with it */
535         if (seed_out)
536             memcpy(seed_out, seed, seed_len);
537
538         if (!BN_GENCB_call(cb, 2, 0))
539             goto err;
540         if (!BN_GENCB_call(cb, 3, 0))
541             goto err;
542
543         /* step 6 */
544         counter = 0;
545         /* "offset = 1" */
546
547         n = (L - 1) / (mdsize << 3);
548
549         for (;;) {
550             if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
551                 goto err;
552
553             /* step 7 */
554             BN_zero(W);
555             /* now 'buf' contains "SEED + offset - 1" */
556             for (k = 0; k <= n; k++) {
557                 /*
558                  * obtain "SEED + offset + k" by incrementing:
559                  */
560                 for (i = seed_len - 1; i >= 0; i--) {
561                     seed[i]++;
562                     if (seed[i] != 0)
563                         break;
564                 }
565
566                 if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))
567                     goto err;
568
569                 /* step 8 */
570                 if (!BN_bin2bn(md, mdsize, r0))
571                     goto err;
572                 if (!BN_lshift(r0, r0, (mdsize << 3) * k))
573                     goto err;
574                 if (!BN_add(W, W, r0))
575                     goto err;
576             }
577
578             /* more of step 8 */
579             if (!BN_mask_bits(W, L - 1))
580                 goto err;
581             if (!BN_copy(X, W))
582                 goto err;
583             if (!BN_add(X, X, test))
584                 goto err;
585
586             /* step 9 */
587             if (!BN_lshift1(r0, q))
588                 goto err;
589             if (!BN_mod(c, X, r0, ctx))
590                 goto err;
591             if (!BN_sub(r0, c, BN_value_one()))
592                 goto err;
593             if (!BN_sub(p, X, r0))
594                 goto err;
595
596             /* step 10 */
597             if (BN_cmp(p, test) >= 0) {
598                 /* step 11 */
599                 r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
600                 if (r > 0)
601                     goto end;   /* found it */
602                 if (r != 0)
603                     goto err;
604             }
605
606             /* step 13 */
607             counter++;
608             /* "offset = offset + n + 1" */
609
610             /* step 14 */
611             if (counter >= (int)(4 * L))
612                 break;
613         }
614         if (seed_in) {
615             ok = 0;
616             DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS);
617             goto err;
618         }
619     }
620  end:
621     if (!BN_GENCB_call(cb, 2, 1))
622         goto err;
623
624  g_only:
625
626     /* We now need to generate g */
627     /* Set r0=(p-1)/q */
628     if (!BN_sub(test, p, BN_value_one()))
629         goto err;
630     if (!BN_div(r0, NULL, test, q, ctx))
631         goto err;
632
633     if (idx < 0) {
634         if (!BN_set_word(test, h))
635             goto err;
636     } else
637         h = 1;
638     if (!BN_MONT_CTX_set(mont, p, ctx))
639         goto err;
640
641     for (;;) {
642         static const unsigned char ggen[4] = { 0x67, 0x67, 0x65, 0x6e };
643         if (idx >= 0) {
644             md[0] = idx & 0xff;
645             md[1] = (h >> 8) & 0xff;
646             md[2] = h & 0xff;
647             if (!EVP_DigestInit_ex(&mctx, evpmd, NULL))
648                 goto err;
649             if (!EVP_DigestUpdate(&mctx, seed_tmp, seed_len))
650                 goto err;
651             if (!EVP_DigestUpdate(&mctx, ggen, sizeof(ggen)))
652                 goto err;
653             if (!EVP_DigestUpdate(&mctx, md, 3))
654                 goto err;
655             if (!EVP_DigestFinal_ex(&mctx, md, NULL))
656                 goto err;
657             if (!BN_bin2bn(md, mdsize, test))
658                 goto err;
659         }
660         /* g=test^r0%p */
661         if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))
662             goto err;
663         if (!BN_is_one(g))
664             break;
665         if (idx < 0 && !BN_add(test, test, BN_value_one()))
666             goto err;
667         h++;
668         if (idx >= 0 && h > 0xffff)
669             goto err;
670     }
671
672     if (!BN_GENCB_call(cb, 3, 1))
673         goto err;
674
675     ok = 1;
676  err:
677     if (ok == 1) {
678         if (p != ret->p) {
679             if (ret->p)
680                 BN_free(ret->p);
681             ret->p = BN_dup(p);
682         }
683         if (q != ret->q) {
684             if (ret->q)
685                 BN_free(ret->q);
686             ret->q = BN_dup(q);
687         }
688         if (ret->g)
689             BN_free(ret->g);
690         ret->g = BN_dup(g);
691         if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
692             ok = -1;
693             goto err;
694         }
695         if (counter_ret != NULL)
696             *counter_ret = counter;
697         if (h_ret != NULL)
698             *h_ret = h;
699     }
700     if (seed)
701         OPENSSL_free(seed);
702     if (seed_out != seed_tmp)
703         OPENSSL_free(seed_tmp);
704     if (ctx) {
705         BN_CTX_end(ctx);
706         BN_CTX_free(ctx);
707     }
708     if (mont != NULL)
709         BN_MONT_CTX_free(mont);
710     EVP_MD_CTX_cleanup(&mctx);
711     return ok;
712 }
713
714 int dsa_paramgen_check_g(DSA *dsa)
715 {
716     BN_CTX *ctx;
717     BIGNUM *tmp;
718     BN_MONT_CTX *mont = NULL;
719     int rv = -1;
720     ctx = BN_CTX_new();
721     if (!ctx)
722         return -1;
723     BN_CTX_start(ctx);
724     if (BN_cmp(dsa->g, BN_value_one()) <= 0)
725         return 0;
726     if (BN_cmp(dsa->g, dsa->p) >= 0)
727         return 0;
728     tmp = BN_CTX_get(ctx);
729     if (!tmp)
730         goto err;
731     if ((mont = BN_MONT_CTX_new()) == NULL)
732         goto err;
733     if (!BN_MONT_CTX_set(mont, dsa->p, ctx))
734         goto err;
735     /* Work out g^q mod p */
736     if (!BN_mod_exp_mont(tmp, dsa->g, dsa->q, dsa->p, ctx, mont))
737         goto err;
738     if (!BN_cmp(tmp, BN_value_one()))
739         rv = 1;
740     else
741         rv = 0;
742  err:
743     BN_CTX_end(ctx);
744     if (mont)
745         BN_MONT_CTX_free(mont);
746     BN_CTX_free(ctx);
747     return rv;
748
749 }
750 #endif