]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dh/dh_gen.c
x86_64: Add endbranch at function entries for Intel CET
[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
d02b48c6 15#include <stdio.h>
b39fc560 16#include "internal/cryptlib.h"
ec577822 17#include <openssl/bn.h>
f11f86f6 18#include "crypto/dh.h"
706457b7 19#include "dh_local.h"
71fa4513 20
f11f86f6 21#ifndef FIPS_MODE
0f113f3e
MC
22static int dh_builtin_genparams(DH *ret, int prime_len, int generator,
23 BN_GENCB *cb);
f11f86f6
SL
24#endif /* FIPS_MODE */
25
26/*
27 * TODO(3.0): keygen should be able to use this method to do a FIPS186-4 style
28 * paramgen.
29 */
30int dh_generate_ffc_parameters(OPENSSL_CTX *libctx, DH *dh, int bits,
31 int qbits, int gindex, BN_GENCB *cb)
32{
33 int ret, res;
34
35 if (qbits <= 0) {
36 const EVP_MD *evpmd = bits >= 2048 ? EVP_sha256() : EVP_sha1();
37
38 qbits = EVP_MD_size(evpmd) * 8;
39 }
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);
43 if (ret > 0)
44 dh->dirty_cnt++;
45 return ret;
46}
0e4aa0d2 47
0f113f3e
MC
48int DH_generate_parameters_ex(DH *ret, int prime_len, int generator,
49 BN_GENCB *cb)
50{
f11f86f6
SL
51#ifdef FIPS_MODE
52 /*
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.
58 */
59 DH *dh = NULL;
60 int ok = 0, nid;
61
62 if (generator != 2)
63 return 0;
64
65 switch (prime_len) {
66 case 2048:
67 nid = NID_ffdhe2048;
68 break;
69 case 3072:
70 nid = NID_ffdhe3072;
71 break;
72 case 4096:
73 nid = NID_ffdhe4096;
74 break;
75 case 6144:
76 nid = NID_ffdhe6144;
77 break;
78 case 8192:
79 nid = NID_ffdhe8192;
80 break;
81 /* unsupported prime_len */
82 default:
83 return 0;
84 }
85 dh = DH_new_by_nid(nid);
86 if (dh != NULL && ffc_params_copy(&ret->params, &dh->params)) {
87 ok = 1;
88 ret->dirty_cnt++;
89 }
90 DH_free(dh);
91 return ok;
92#else
0f113f3e
MC
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);
f11f86f6 96#endif /* FIPS_MODE */
0f113f3e 97}
0e4aa0d2 98
f11f86f6 99#ifndef FIPS_MODE
1d97c843
TH
100/*-
101 * We generate DH parameters as follows
a38c878c
BE
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
107 * g^q mod p == 1.
108 * Or in terms of the Legendre symbol: (g/p) == 1.
d02b48c6
RE
109 *
110 * Having said all that,
111 * there is another special case method for the generators 2, 3 and 5.
a38c878c
BE
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
d02b48c6 118 *
a38c878c
BE
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
82652aaf 125 */
0f113f3e
MC
126static int dh_builtin_genparams(DH *ret, int prime_len, int generator,
127 BN_GENCB *cb)
128{
129 BIGNUM *t1, *t2;
130 int g, ok = -1;
131 BN_CTX *ctx = NULL;
d02b48c6 132
6de1fe90
BE
133 if (prime_len > OPENSSL_DH_MAX_MODULUS_BITS) {
134 DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_MODULUS_TOO_LARGE);
135 return 0;
136 }
137
138 if (prime_len < DH_MIN_MODULUS_BITS) {
139 DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_MODULUS_TOO_SMALL);
140 return 0;
141 }
142
0f113f3e
MC
143 ctx = BN_CTX_new();
144 if (ctx == NULL)
145 goto err;
146 BN_CTX_start(ctx);
147 t1 = BN_CTX_get(ctx);
148 t2 = BN_CTX_get(ctx);
edea42c6 149 if (t2 == NULL)
0f113f3e 150 goto err;
e9224c71 151
0f113f3e 152 /* Make sure 'ret' has the necessary elements */
dc8de3e6 153 if (ret->params.p == NULL && ((ret->params.p = BN_new()) == NULL))
0f113f3e 154 goto err;
dc8de3e6 155 if (ret->params.g == NULL && ((ret->params.g = BN_new()) == NULL))
0f113f3e
MC
156 goto err;
157
158 if (generator <= 1) {
159 DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR);
160 goto err;
161 }
162 if (generator == DH_GENERATOR_2) {
163 if (!BN_set_word(t1, 24))
164 goto err;
a38c878c 165 if (!BN_set_word(t2, 23))
0f113f3e
MC
166 goto err;
167 g = 2;
dfb56425 168 } else if (generator == DH_GENERATOR_5) {
a38c878c 169 if (!BN_set_word(t1, 60))
0f113f3e 170 goto err;
a38c878c 171 if (!BN_set_word(t2, 59))
0f113f3e 172 goto err;
0f113f3e
MC
173 g = 5;
174 } else {
175 /*
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
179 */
a38c878c 180 if (!BN_set_word(t1, 12))
0f113f3e 181 goto err;
a38c878c 182 if (!BN_set_word(t2, 11))
0f113f3e
MC
183 goto err;
184 g = generator;
185 }
186
dc8de3e6 187 if (!BN_generate_prime_ex(ret->params.p, prime_len, 1, t1, t2, cb))
0f113f3e
MC
188 goto err;
189 if (!BN_GENCB_call(cb, 3, 0))
190 goto err;
dc8de3e6 191 if (!BN_set_word(ret->params.g, g))
0f113f3e 192 goto err;
8b84b075 193 ret->dirty_cnt++;
0f113f3e
MC
194 ok = 1;
195 err:
196 if (ok == -1) {
197 DHerr(DH_F_DH_BUILTIN_GENPARAMS, ERR_R_BN_LIB);
198 ok = 0;
199 }
d02b48c6 200
ce1415ed
SL
201 BN_CTX_end(ctx);
202 BN_CTX_free(ctx);
0f113f3e
MC
203 return ok;
204}
f11f86f6 205#endif /* FIPS_MODE */