7b72867f71979143cd0796f4fb917cf7667e1be8
[openssl.git] / crypto / dsa / dsa_gen.c
1 /*
2  * Copyright 1995-2020 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
26 int dsa_generate_ffc_parameters(DSA *dsa, int type,
27                                 int pbits, int qbits,
28                                 EVP_MD *md, BN_GENCB *cb)
29 {
30     int ret = 0, res;
31
32     if (qbits <= 0) {
33         if (md != NULL)
34             qbits = EVP_MD_size(md) * 8;
35         else
36             qbits = (pbits >= 2048 ? SHA256_DIGEST_LENGTH :
37                                      SHA_DIGEST_LENGTH) * 8;
38     }
39 #ifndef FIPS_MODE
40     if (type == DSA_PARAMGEN_TYPE_FIPS_186_2)
41         ret = ffc_params_FIPS186_2_generate(dsa->libctx, &dsa->params,
42                                             FFC_PARAM_TYPE_DSA,
43                                             pbits, qbits, md, &res, cb);
44     else
45 #endif
46         ret = ffc_params_FIPS186_4_generate(dsa->libctx, &dsa->params,
47                                             FFC_PARAM_TYPE_DSA,
48                                             pbits, qbits, md, &res, cb);
49     if (ret > 0)
50         dsa->dirty_cnt++;
51     return ret;
52 }
53
54 #ifndef FIPS_MODE
55 int DSA_generate_parameters_ex(DSA *dsa, int bits,
56                                const unsigned char *seed_in, int seed_len,
57                                int *counter_ret, unsigned long *h_ret,
58                                BN_GENCB *cb)
59 {
60 #ifndef FIPS_MODE
61     if (dsa->meth->dsa_paramgen)
62         return dsa->meth->dsa_paramgen(dsa, bits, seed_in, seed_len,
63                                        counter_ret, h_ret, cb);
64 #endif
65     if (seed_in != NULL
66         && !ffc_params_set_validate_params(&dsa->params, seed_in, seed_len, -1))
67         return 0;
68
69 #ifndef FIPS_MODE
70     /* The old code used FIPS 186-2 DSA Parameter generation */
71     if (bits <= 1024 && seed_len == 20) {
72         if (!dsa_generate_ffc_parameters(dsa, DSA_PARAMGEN_TYPE_FIPS_186_2,
73                                          bits, 160, NULL, cb))
74             return 0;
75     } else
76 #endif
77     {
78         if (!dsa_generate_ffc_parameters(dsa, DSA_PARAMGEN_TYPE_FIPS_186_4,
79                                          bits, -1, NULL, cb))
80             return 0;
81     }
82
83     if (counter_ret != NULL)
84         *counter_ret = dsa->params.pcounter;
85     if (h_ret != NULL)
86         *h_ret = dsa->params.h;
87     return 1;
88 }
89 #endif