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 * DH low level APIs are deprecated for public use, but still ok for
19 #include "internal/deprecated.h"
22 #include "internal/cryptlib.h"
23 #include <openssl/bn.h>
24 #include "crypto/dh.h"
28 static int dh_builtin_genparams(DH
*ret
, int prime_len
, int generator
,
30 #endif /* FIPS_MODE */
33 * TODO(3.0): keygen should be able to use this method to do a FIPS186-4 style
36 int dh_generate_ffc_parameters(DH
*dh
, int bits
,
37 int qbits
, int gindex
, BN_GENCB
*cb
)
42 const EVP_MD
*evpmd
= bits
>= 2048 ? EVP_sha256() : EVP_sha1();
44 qbits
= EVP_MD_size(evpmd
) * 8;
46 dh
->params
.gindex
= gindex
;
47 ret
= ffc_params_FIPS186_4_generate(dh
->libctx
, &dh
->params
,
49 bits
, qbits
, NULL
, &res
, cb
);
55 int DH_generate_parameters_ex(DH
*ret
, int prime_len
, int generator
,
60 * Just choose an approved safe prime group.
61 * The alternative to this is to generate FIPS186-4 domain parameters i.e.
62 * return dh_generate_ffc_parameters(ret, prime_len, -1, -1, cb);
63 * As the FIPS186-4 generated params are for backwards compatability,
64 * the safe prime group should be used as the default.
88 /* unsupported prime_len */
92 dh
= DH_new_by_nid(nid
);
93 if (dh
!= NULL
&& ffc_params_copy(&ret
->params
, &dh
->params
)) {
100 if (ret
->meth
->generate_params
)
101 return ret
->meth
->generate_params(ret
, prime_len
, generator
, cb
);
102 return dh_builtin_genparams(ret
, prime_len
, generator
, cb
);
103 #endif /* FIPS_MODE */
108 * We generate DH parameters as follows
109 * find a prime p which is prime_len bits long,
110 * where q=(p-1)/2 is also prime.
111 * In the following we assume that g is not 0, 1 or p-1, since it
112 * would generate only trivial subgroups.
113 * For this case, g is a generator of the order-q subgroup if
115 * Or in terms of the Legendre symbol: (g/p) == 1.
117 * Having said all that,
118 * there is another special case method for the generators 2, 3 and 5.
119 * Using the quadratic reciprocity law it is possible to solve
120 * (g/p) == 1 for the special values 2, 3, 5:
121 * (2/p) == 1 if p mod 8 == 1 or 7.
122 * (3/p) == 1 if p mod 12 == 1 or 11.
123 * (5/p) == 1 if p mod 5 == 1 or 4.
124 * See for instance: https://en.wikipedia.org/wiki/Legendre_symbol
126 * Since all safe primes > 7 must satisfy p mod 12 == 11
127 * and all safe primes > 11 must satisfy p mod 5 != 1
128 * we can further improve the condition for g = 2, 3 and 5:
129 * for 2, p mod 24 == 23
130 * for 3, p mod 12 == 11
131 * for 5, p mod 60 == 59
133 static int dh_builtin_genparams(DH
*ret
, int prime_len
, int generator
,
140 if (prime_len
> OPENSSL_DH_MAX_MODULUS_BITS
) {
141 DHerr(DH_F_DH_BUILTIN_GENPARAMS
, DH_R_MODULUS_TOO_LARGE
);
145 if (prime_len
< DH_MIN_MODULUS_BITS
) {
146 DHerr(DH_F_DH_BUILTIN_GENPARAMS
, DH_R_MODULUS_TOO_SMALL
);
154 t1
= BN_CTX_get(ctx
);
155 t2
= BN_CTX_get(ctx
);
159 /* Make sure 'ret' has the necessary elements */
160 if (ret
->params
.p
== NULL
&& ((ret
->params
.p
= BN_new()) == NULL
))
162 if (ret
->params
.g
== NULL
&& ((ret
->params
.g
= BN_new()) == NULL
))
165 if (generator
<= 1) {
166 DHerr(DH_F_DH_BUILTIN_GENPARAMS
, DH_R_BAD_GENERATOR
);
169 if (generator
== DH_GENERATOR_2
) {
170 if (!BN_set_word(t1
, 24))
172 if (!BN_set_word(t2
, 23))
175 } else if (generator
== DH_GENERATOR_5
) {
176 if (!BN_set_word(t1
, 60))
178 if (!BN_set_word(t2
, 59))
183 * in the general case, don't worry if 'generator' is a generator or
184 * not: since we are using safe primes, it will generate either an
185 * order-q or an order-2q group, which both is OK
187 if (!BN_set_word(t1
, 12))
189 if (!BN_set_word(t2
, 11))
194 if (!BN_generate_prime_ex(ret
->params
.p
, prime_len
, 1, t1
, t2
, cb
))
196 if (!BN_GENCB_call(cb
, 3, 0))
198 if (!BN_set_word(ret
->params
.g
, g
))
204 DHerr(DH_F_DH_BUILTIN_GENPARAMS
, ERR_R_BN_LIB
);
212 #endif /* FIPS_MODE */