2 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
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
11 * NB: These functions have been upgraded - the previous prototypes are in
12 * dh_depr.c as wrappers to these ones. - Geoff
16 #include "internal/cryptlib.h"
17 #include <openssl/bn.h>
18 #include "crypto/dh.h"
22 static int dh_builtin_genparams(DH
*ret
, int prime_len
, int generator
,
24 #endif /* FIPS_MODE */
27 * TODO(3.0): keygen should be able to use this method to do a FIPS186-4 style
30 int dh_generate_ffc_parameters(OPENSSL_CTX
*libctx
, DH
*dh
, int bits
,
31 int qbits
, int gindex
, BN_GENCB
*cb
)
36 const EVP_MD
*evpmd
= bits
>= 2048 ? EVP_sha256() : EVP_sha1();
38 qbits
= EVP_MD_size(evpmd
) * 8;
40 dh
->params
.gindex
= gindex
;
41 ret
= ffc_params_FIPS186_4_generate(libctx
, &dh
->params
, FFC_PARAM_TYPE_DH
,
42 bits
, qbits
, NULL
, &res
, cb
);
48 int DH_generate_parameters_ex(DH
*ret
, int prime_len
, int generator
,
53 * Just choose an approved safe prime group.
54 * The alternative to this is to generate FIPS186-4 domain parameters i.e.
55 * return dh_generate_ffc_parameters(ret, prime_len, -1, -1, cb);
56 * As the FIPS186-4 generated params are for backwards compatability,
57 * the safe prime group should be used as the default.
81 /* unsupported prime_len */
85 dh
= DH_new_by_nid(nid
);
86 if (dh
!= NULL
&& ffc_params_copy(&ret
->params
, &dh
->params
)) {
93 if (ret
->meth
->generate_params
)
94 return ret
->meth
->generate_params(ret
, prime_len
, generator
, cb
);
95 return dh_builtin_genparams(ret
, prime_len
, generator
, cb
);
96 #endif /* FIPS_MODE */
101 * We generate DH parameters as follows
102 * find a prime p which is prime_len bits long,
103 * where q=(p-1)/2 is also prime.
104 * In the following we assume that g is not 0, 1 or p-1, since it
105 * would generate only trivial subgroups.
106 * For this case, g is a generator of the order-q subgroup if
108 * Or in terms of the Legendre symbol: (g/p) == 1.
110 * Having said all that,
111 * there is another special case method for the generators 2, 3 and 5.
112 * Using the quadratic reciprocity law it is possible to solve
113 * (g/p) == 1 for the special values 2, 3, 5:
114 * (2/p) == 1 if p mod 8 == 1 or 7.
115 * (3/p) == 1 if p mod 12 == 1 or 11.
116 * (5/p) == 1 if p mod 5 == 1 or 4.
117 * See for instance: https://en.wikipedia.org/wiki/Legendre_symbol
119 * Since all safe primes > 7 must satisfy p mod 12 == 11
120 * and all safe primes > 11 must satisfy p mod 5 != 1
121 * we can further improve the condition for g = 2, 3 and 5:
122 * for 2, p mod 24 == 23
123 * for 3, p mod 12 == 11
124 * for 5, p mod 60 == 59
126 static int dh_builtin_genparams(DH
*ret
, int prime_len
, int generator
,
133 if (prime_len
> OPENSSL_DH_MAX_MODULUS_BITS
) {
134 DHerr(DH_F_DH_BUILTIN_GENPARAMS
, DH_R_MODULUS_TOO_LARGE
);
138 if (prime_len
< DH_MIN_MODULUS_BITS
) {
139 DHerr(DH_F_DH_BUILTIN_GENPARAMS
, DH_R_MODULUS_TOO_SMALL
);
147 t1
= BN_CTX_get(ctx
);
148 t2
= BN_CTX_get(ctx
);
152 /* Make sure 'ret' has the necessary elements */
153 if (ret
->params
.p
== NULL
&& ((ret
->params
.p
= BN_new()) == NULL
))
155 if (ret
->params
.g
== NULL
&& ((ret
->params
.g
= BN_new()) == NULL
))
158 if (generator
<= 1) {
159 DHerr(DH_F_DH_BUILTIN_GENPARAMS
, DH_R_BAD_GENERATOR
);
162 if (generator
== DH_GENERATOR_2
) {
163 if (!BN_set_word(t1
, 24))
165 if (!BN_set_word(t2
, 23))
168 } else if (generator
== DH_GENERATOR_5
) {
169 if (!BN_set_word(t1
, 60))
171 if (!BN_set_word(t2
, 59))
176 * in the general case, don't worry if 'generator' is a generator or
177 * not: since we are using safe primes, it will generate either an
178 * order-q or an order-2q group, which both is OK
180 if (!BN_set_word(t1
, 12))
182 if (!BN_set_word(t2
, 11))
187 if (!BN_generate_prime_ex(ret
->params
.p
, prime_len
, 1, t1
, t2
, cb
))
189 if (!BN_GENCB_call(cb
, 3, 0))
191 if (!BN_set_word(ret
->params
.g
, g
))
197 DHerr(DH_F_DH_BUILTIN_GENPARAMS
, ERR_R_BN_LIB
);
205 #endif /* FIPS_MODE */