xref: /openssl/crypto/dsa/dsa_gen.c (revision 1f99b53f)
1 /*
2  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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  * DSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15 
16 #include <openssl/opensslconf.h>
17 #include <stdio.h>
18 #include "internal/cryptlib.h"
19 #include <openssl/evp.h>
20 #include <openssl/bn.h>
21 #include <openssl/rand.h>
22 #include <openssl/sha.h>
23 #include "crypto/dsa.h"
24 #include "dsa_local.h"
25 
ossl_dsa_generate_ffc_parameters(DSA * dsa,int type,int pbits,int qbits,BN_GENCB * cb)26 int ossl_dsa_generate_ffc_parameters(DSA *dsa, int type, int pbits, int qbits,
27                                      BN_GENCB *cb)
28 {
29     int ret = 0, res;
30 
31 #ifndef FIPS_MODULE
32     if (type == DSA_PARAMGEN_TYPE_FIPS_186_2)
33         ret = ossl_ffc_params_FIPS186_2_generate(dsa->libctx, &dsa->params,
34                                                  FFC_PARAM_TYPE_DSA,
35                                                  pbits, qbits, &res, cb);
36     else
37 #endif
38         ret = ossl_ffc_params_FIPS186_4_generate(dsa->libctx, &dsa->params,
39                                                  FFC_PARAM_TYPE_DSA,
40                                                  pbits, qbits, &res, cb);
41     if (ret > 0)
42         dsa->dirty_cnt++;
43     return ret;
44 }
45 
46 #ifndef FIPS_MODULE
DSA_generate_parameters_ex(DSA * dsa,int bits,const unsigned char * seed_in,int seed_len,int * counter_ret,unsigned long * h_ret,BN_GENCB * cb)47 int DSA_generate_parameters_ex(DSA *dsa, int bits,
48                                const unsigned char *seed_in, int seed_len,
49                                int *counter_ret, unsigned long *h_ret,
50                                BN_GENCB *cb)
51 {
52     if (dsa->meth->dsa_paramgen)
53         return dsa->meth->dsa_paramgen(dsa, bits, seed_in, seed_len,
54                                        counter_ret, h_ret, cb);
55     if (seed_in != NULL
56         && !ossl_ffc_params_set_validate_params(&dsa->params, seed_in, seed_len,
57                                                 -1))
58         return 0;
59 
60     /* The old code used FIPS 186-2 DSA Parameter generation */
61     if (bits < 2048 && seed_len <= 20) {
62         if (!ossl_dsa_generate_ffc_parameters(dsa, DSA_PARAMGEN_TYPE_FIPS_186_2,
63                                               bits, 160, cb))
64             return 0;
65     } else {
66         if (!ossl_dsa_generate_ffc_parameters(dsa, DSA_PARAMGEN_TYPE_FIPS_186_4,
67                                               bits, 0, cb))
68             return 0;
69     }
70 
71     if (counter_ret != NULL)
72         *counter_ret = dsa->params.pcounter;
73     if (h_ret != NULL)
74         *h_ret = dsa->params.h;
75     return 1;
76 }
77 #endif
78