]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dh/dh_gen.c
Deprecate the low level Diffie-Hellman functions.
[thirdparty/openssl.git] / crypto / dh / dh_gen.c
CommitLineData
aa6bb135 1/*
a38c878c 2 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
0f113f3e 3 *
e38873f5 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
aa6bb135
RS
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
d02b48c6
RE
8 */
9
0f113f3e
MC
10/*
11 * NB: These functions have been upgraded - the previous prototypes are in
12 * dh_depr.c as wrappers to these ones. - Geoff
e9224c71
GT
13 */
14
ada66e78
P
15/*
16 * DH low level APIs are deprecated for public use, but still ok for
17 * internal use.
18 */
19#include "internal/deprecated.h"
20
d02b48c6 21#include <stdio.h>
b39fc560 22#include "internal/cryptlib.h"
ec577822 23#include <openssl/bn.h>
f11f86f6 24#include "crypto/dh.h"
706457b7 25#include "dh_local.h"
71fa4513 26
f11f86f6 27#ifndef FIPS_MODE
0f113f3e
MC
28static int dh_builtin_genparams(DH *ret, int prime_len, int generator,
29 BN_GENCB *cb);
f11f86f6
SL
30#endif /* FIPS_MODE */
31
32/*
33 * TODO(3.0): keygen should be able to use this method to do a FIPS186-4 style
34 * paramgen.
35 */
8083fd3a 36int dh_generate_ffc_parameters(DH *dh, int bits,
f11f86f6
SL
37 int qbits, int gindex, BN_GENCB *cb)
38{
39 int ret, res;
40
41 if (qbits <= 0) {
42 const EVP_MD *evpmd = bits >= 2048 ? EVP_sha256() : EVP_sha1();
43
44 qbits = EVP_MD_size(evpmd) * 8;
45 }
46 dh->params.gindex = gindex;
8083fd3a
SL
47 ret = ffc_params_FIPS186_4_generate(dh->libctx, &dh->params,
48 FFC_PARAM_TYPE_DH,
f11f86f6
SL
49 bits, qbits, NULL, &res, cb);
50 if (ret > 0)
51 dh->dirty_cnt++;
52 return ret;
53}
0e4aa0d2 54
0f113f3e
MC
55int DH_generate_parameters_ex(DH *ret, int prime_len, int generator,
56 BN_GENCB *cb)
57{
f11f86f6
SL
58#ifdef FIPS_MODE
59 /*
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.
65 */
66 DH *dh = NULL;
67 int ok = 0, nid;
68
69 if (generator != 2)
70 return 0;
71
72 switch (prime_len) {
73 case 2048:
74 nid = NID_ffdhe2048;
75 break;
76 case 3072:
77 nid = NID_ffdhe3072;
78 break;
79 case 4096:
80 nid = NID_ffdhe4096;
81 break;
82 case 6144:
83 nid = NID_ffdhe6144;
84 break;
85 case 8192:
86 nid = NID_ffdhe8192;
87 break;
88 /* unsupported prime_len */
89 default:
90 return 0;
91 }
92 dh = DH_new_by_nid(nid);
93 if (dh != NULL && ffc_params_copy(&ret->params, &dh->params)) {
94 ok = 1;
95 ret->dirty_cnt++;
96 }
97 DH_free(dh);
98 return ok;
99#else
0f113f3e
MC
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);
f11f86f6 103#endif /* FIPS_MODE */
0f113f3e 104}
0e4aa0d2 105
f11f86f6 106#ifndef FIPS_MODE
1d97c843
TH
107/*-
108 * We generate DH parameters as follows
a38c878c
BE
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
114 * g^q mod p == 1.
115 * Or in terms of the Legendre symbol: (g/p) == 1.
d02b48c6
RE
116 *
117 * Having said all that,
118 * there is another special case method for the generators 2, 3 and 5.
a38c878c
BE
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
d02b48c6 125 *
a38c878c
BE
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
82652aaf 132 */
0f113f3e
MC
133static int dh_builtin_genparams(DH *ret, int prime_len, int generator,
134 BN_GENCB *cb)
135{
136 BIGNUM *t1, *t2;
137 int g, ok = -1;
138 BN_CTX *ctx = NULL;
d02b48c6 139
6de1fe90
BE
140 if (prime_len > OPENSSL_DH_MAX_MODULUS_BITS) {
141 DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_MODULUS_TOO_LARGE);
142 return 0;
143 }
144
145 if (prime_len < DH_MIN_MODULUS_BITS) {
146 DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_MODULUS_TOO_SMALL);
147 return 0;
148 }
149
0f113f3e
MC
150 ctx = BN_CTX_new();
151 if (ctx == NULL)
152 goto err;
153 BN_CTX_start(ctx);
154 t1 = BN_CTX_get(ctx);
155 t2 = BN_CTX_get(ctx);
edea42c6 156 if (t2 == NULL)
0f113f3e 157 goto err;
e9224c71 158
0f113f3e 159 /* Make sure 'ret' has the necessary elements */
dc8de3e6 160 if (ret->params.p == NULL && ((ret->params.p = BN_new()) == NULL))
0f113f3e 161 goto err;
dc8de3e6 162 if (ret->params.g == NULL && ((ret->params.g = BN_new()) == NULL))
0f113f3e
MC
163 goto err;
164
165 if (generator <= 1) {
166 DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR);
167 goto err;
168 }
169 if (generator == DH_GENERATOR_2) {
170 if (!BN_set_word(t1, 24))
171 goto err;
a38c878c 172 if (!BN_set_word(t2, 23))
0f113f3e
MC
173 goto err;
174 g = 2;
dfb56425 175 } else if (generator == DH_GENERATOR_5) {
a38c878c 176 if (!BN_set_word(t1, 60))
0f113f3e 177 goto err;
a38c878c 178 if (!BN_set_word(t2, 59))
0f113f3e 179 goto err;
0f113f3e
MC
180 g = 5;
181 } else {
182 /*
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
186 */
a38c878c 187 if (!BN_set_word(t1, 12))
0f113f3e 188 goto err;
a38c878c 189 if (!BN_set_word(t2, 11))
0f113f3e
MC
190 goto err;
191 g = generator;
192 }
193
dc8de3e6 194 if (!BN_generate_prime_ex(ret->params.p, prime_len, 1, t1, t2, cb))
0f113f3e
MC
195 goto err;
196 if (!BN_GENCB_call(cb, 3, 0))
197 goto err;
dc8de3e6 198 if (!BN_set_word(ret->params.g, g))
0f113f3e 199 goto err;
8b84b075 200 ret->dirty_cnt++;
0f113f3e
MC
201 ok = 1;
202 err:
203 if (ok == -1) {
204 DHerr(DH_F_DH_BUILTIN_GENPARAMS, ERR_R_BN_LIB);
205 ok = 0;
206 }
d02b48c6 207
ce1415ed
SL
208 BN_CTX_end(ctx);
209 BN_CTX_free(ctx);
0f113f3e
MC
210 return ok;
211}
f11f86f6 212#endif /* FIPS_MODE */