]>
Commit | Line | Data |
---|---|---|
aa6bb135 | 1 | /* |
33388b44 | 2 | * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 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 | ||
ada66e78 P |
10 | /* |
11 | * DH low level APIs are deprecated for public use, but still ok for | |
12 | * internal use. | |
13 | */ | |
14 | #include "internal/deprecated.h" | |
15 | ||
d02b48c6 | 16 | #include <stdio.h> |
b39fc560 | 17 | #include "internal/cryptlib.h" |
ec577822 | 18 | #include <openssl/bn.h> |
706457b7 | 19 | #include "dh_local.h" |
8083fd3a | 20 | #include "crypto/dh.h" |
d02b48c6 | 21 | |
26505153 RL |
22 | /*- |
23 | * Check that p and g are suitable enough | |
24 | * | |
25 | * p is odd | |
26 | * 1 < g < p - 1 | |
27 | */ | |
b0004708 PY |
28 | int DH_check_params_ex(const DH *dh) |
29 | { | |
30 | int errflags = 0; | |
31 | ||
a38c878c BE |
32 | if (!DH_check_params(dh, &errflags)) |
33 | return 0; | |
b0004708 PY |
34 | |
35 | if ((errflags & DH_CHECK_P_NOT_PRIME) != 0) | |
36 | DHerr(DH_F_DH_CHECK_PARAMS_EX, DH_R_CHECK_P_NOT_PRIME); | |
37 | if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0) | |
38 | DHerr(DH_F_DH_CHECK_PARAMS_EX, DH_R_NOT_SUITABLE_GENERATOR); | |
feeb7ecd BE |
39 | if ((errflags & DH_MODULUS_TOO_SMALL) != 0) |
40 | DHerr(DH_F_DH_CHECK_PARAMS_EX, DH_R_MODULUS_TOO_SMALL); | |
41 | if ((errflags & DH_MODULUS_TOO_LARGE) != 0) | |
42 | DHerr(DH_F_DH_CHECK_PARAMS_EX, DH_R_MODULUS_TOO_LARGE); | |
b0004708 PY |
43 | |
44 | return errflags == 0; | |
45 | } | |
26505153 | 46 | |
f844f9eb | 47 | #ifdef FIPS_MODULE |
8083fd3a SL |
48 | int DH_check_params(const DH *dh, int *ret) |
49 | { | |
50 | int nid; | |
51 | ||
52 | *ret = 0; | |
53 | /* | |
54 | * SP800-56A R3 Section 5.5.2 Assurances of Domain Parameter Validity | |
55 | * (1a) The domain parameters correspond to any approved safe prime group. | |
56 | */ | |
57 | nid = DH_get_nid((DH *)dh); | |
58 | if (nid != NID_undef) | |
59 | return 1; | |
60 | /* | |
61 | * OR | |
62 | * (2b) FFC domain params conform to FIPS-186-4 explicit domain param | |
63 | * validity tests. | |
64 | */ | |
5357c106 P |
65 | return ossl_ffc_params_FIPS186_4_validate(dh->libctx, &dh->params, |
66 | FFC_PARAM_TYPE_DH, ret, NULL); | |
8083fd3a SL |
67 | } |
68 | #else | |
26505153 RL |
69 | int DH_check_params(const DH *dh, int *ret) |
70 | { | |
71 | int ok = 0; | |
72 | BIGNUM *tmp = NULL; | |
73 | BN_CTX *ctx = NULL; | |
74 | ||
75 | *ret = 0; | |
76 | ctx = BN_CTX_new(); | |
77 | if (ctx == NULL) | |
78 | goto err; | |
79 | BN_CTX_start(ctx); | |
80 | tmp = BN_CTX_get(ctx); | |
81 | if (tmp == NULL) | |
82 | goto err; | |
83 | ||
dc8de3e6 | 84 | if (!BN_is_odd(dh->params.p)) |
26505153 | 85 | *ret |= DH_CHECK_P_NOT_PRIME; |
dc8de3e6 SL |
86 | if (BN_is_negative(dh->params.g) |
87 | || BN_is_zero(dh->params.g) | |
88 | || BN_is_one(dh->params.g)) | |
26505153 | 89 | *ret |= DH_NOT_SUITABLE_GENERATOR; |
dc8de3e6 | 90 | if (BN_copy(tmp, dh->params.p) == NULL || !BN_sub_word(tmp, 1)) |
26505153 | 91 | goto err; |
dc8de3e6 | 92 | if (BN_cmp(dh->params.g, tmp) >= 0) |
26505153 | 93 | *ret |= DH_NOT_SUITABLE_GENERATOR; |
dc8de3e6 | 94 | if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) |
feeb7ecd | 95 | *ret |= DH_MODULUS_TOO_SMALL; |
dc8de3e6 | 96 | if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) |
feeb7ecd | 97 | *ret |= DH_MODULUS_TOO_LARGE; |
26505153 RL |
98 | |
99 | ok = 1; | |
100 | err: | |
ce1415ed SL |
101 | BN_CTX_end(ctx); |
102 | BN_CTX_free(ctx); | |
26a7d938 | 103 | return ok; |
26505153 | 104 | } |
f844f9eb | 105 | #endif /* FIPS_MODULE */ |
26505153 | 106 | |
1d97c843 TH |
107 | /*- |
108 | * Check that p is a safe prime and | |
a38c878c | 109 | * g is a suitable generator. |
d02b48c6 | 110 | */ |
b0004708 PY |
111 | int DH_check_ex(const DH *dh) |
112 | { | |
113 | int errflags = 0; | |
114 | ||
a38c878c BE |
115 | if (!DH_check(dh, &errflags)) |
116 | return 0; | |
b0004708 PY |
117 | |
118 | if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0) | |
119 | DHerr(DH_F_DH_CHECK_EX, DH_R_NOT_SUITABLE_GENERATOR); | |
120 | if ((errflags & DH_CHECK_Q_NOT_PRIME) != 0) | |
121 | DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_Q_NOT_PRIME); | |
122 | if ((errflags & DH_CHECK_INVALID_Q_VALUE) != 0) | |
123 | DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_INVALID_Q_VALUE); | |
124 | if ((errflags & DH_CHECK_INVALID_J_VALUE) != 0) | |
125 | DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_INVALID_J_VALUE); | |
126 | if ((errflags & DH_UNABLE_TO_CHECK_GENERATOR) != 0) | |
127 | DHerr(DH_F_DH_CHECK_EX, DH_R_UNABLE_TO_CHECK_GENERATOR); | |
128 | if ((errflags & DH_CHECK_P_NOT_PRIME) != 0) | |
129 | DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_P_NOT_PRIME); | |
130 | if ((errflags & DH_CHECK_P_NOT_SAFE_PRIME) != 0) | |
131 | DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_P_NOT_SAFE_PRIME); | |
feeb7ecd BE |
132 | if ((errflags & DH_MODULUS_TOO_SMALL) != 0) |
133 | DHerr(DH_F_DH_CHECK_EX, DH_R_MODULUS_TOO_SMALL); | |
134 | if ((errflags & DH_MODULUS_TOO_LARGE) != 0) | |
135 | DHerr(DH_F_DH_CHECK_EX, DH_R_MODULUS_TOO_LARGE); | |
b0004708 PY |
136 | |
137 | return errflags == 0; | |
138 | } | |
d02b48c6 | 139 | |
8083fd3a | 140 | /* Note: according to documentation - this only checks the params */ |
f971ccb2 | 141 | int DH_check(const DH *dh, int *ret) |
0f113f3e | 142 | { |
f844f9eb | 143 | #ifdef FIPS_MODULE |
8083fd3a SL |
144 | return DH_check_params(dh, ret); |
145 | #else | |
748e8530 | 146 | int ok = 0, r; |
0f113f3e | 147 | BN_CTX *ctx = NULL; |
0f113f3e | 148 | BIGNUM *t1 = NULL, *t2 = NULL; |
8083fd3a SL |
149 | int nid = DH_get_nid((DH *)dh); |
150 | ||
151 | *ret = 0; | |
152 | if (nid != NID_undef) | |
153 | return 1; | |
0f113f3e | 154 | |
a38c878c BE |
155 | if (!DH_check_params(dh, ret)) |
156 | return 0; | |
157 | ||
0f113f3e MC |
158 | ctx = BN_CTX_new(); |
159 | if (ctx == NULL) | |
160 | goto err; | |
161 | BN_CTX_start(ctx); | |
162 | t1 = BN_CTX_get(ctx); | |
0f113f3e MC |
163 | t2 = BN_CTX_get(ctx); |
164 | if (t2 == NULL) | |
165 | goto err; | |
d02b48c6 | 166 | |
dc8de3e6 SL |
167 | if (dh->params.q != NULL) { |
168 | if (BN_cmp(dh->params.g, BN_value_one()) <= 0) | |
0f113f3e | 169 | *ret |= DH_NOT_SUITABLE_GENERATOR; |
dc8de3e6 | 170 | else if (BN_cmp(dh->params.g, dh->params.p) >= 0) |
0f113f3e MC |
171 | *ret |= DH_NOT_SUITABLE_GENERATOR; |
172 | else { | |
173 | /* Check g^q == 1 mod p */ | |
dc8de3e6 | 174 | if (!BN_mod_exp(t1, dh->params.g, dh->params.q, dh->params.p, ctx)) |
0f113f3e MC |
175 | goto err; |
176 | if (!BN_is_one(t1)) | |
177 | *ret |= DH_NOT_SUITABLE_GENERATOR; | |
178 | } | |
dc8de3e6 | 179 | r = BN_check_prime(dh->params.q, ctx, NULL); |
748e8530 DB |
180 | if (r < 0) |
181 | goto err; | |
182 | if (!r) | |
0f113f3e MC |
183 | *ret |= DH_CHECK_Q_NOT_PRIME; |
184 | /* Check p == 1 mod q i.e. q divides p - 1 */ | |
dc8de3e6 | 185 | if (!BN_div(t1, t2, dh->params.p, dh->params.q, ctx)) |
0f113f3e MC |
186 | goto err; |
187 | if (!BN_is_one(t2)) | |
188 | *ret |= DH_CHECK_INVALID_Q_VALUE; | |
dc8de3e6 SL |
189 | if (dh->params.j != NULL |
190 | && BN_cmp(dh->params.j, t1)) | |
0f113f3e | 191 | *ret |= DH_CHECK_INVALID_J_VALUE; |
a38c878c | 192 | } |
d02b48c6 | 193 | |
dc8de3e6 | 194 | r = BN_check_prime(dh->params.p, ctx, NULL); |
748e8530 DB |
195 | if (r < 0) |
196 | goto err; | |
197 | if (!r) | |
0f113f3e | 198 | *ret |= DH_CHECK_P_NOT_PRIME; |
dc8de3e6 SL |
199 | else if (dh->params.q == NULL) { |
200 | if (!BN_rshift1(t1, dh->params.p)) | |
0f113f3e | 201 | goto err; |
42619397 | 202 | r = BN_check_prime(t1, ctx, NULL); |
748e8530 DB |
203 | if (r < 0) |
204 | goto err; | |
e705fcf1 | 205 | if (!r) |
0f113f3e MC |
206 | *ret |= DH_CHECK_P_NOT_SAFE_PRIME; |
207 | } | |
208 | ok = 1; | |
209 | err: | |
ce1415ed SL |
210 | BN_CTX_end(ctx); |
211 | BN_CTX_free(ctx); | |
26a7d938 | 212 | return ok; |
f844f9eb | 213 | #endif /* FIPS_MODULE */ |
0f113f3e | 214 | } |
bf3d6c0c | 215 | |
b0004708 PY |
216 | int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key) |
217 | { | |
218 | int errflags = 0; | |
219 | ||
2b95e8ef BE |
220 | if (!DH_check_pub_key(dh, pub_key, &errflags)) |
221 | return 0; | |
b0004708 PY |
222 | |
223 | if ((errflags & DH_CHECK_PUBKEY_TOO_SMALL) != 0) | |
224 | DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_TOO_SMALL); | |
225 | if ((errflags & DH_CHECK_PUBKEY_TOO_LARGE) != 0) | |
226 | DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_TOO_LARGE); | |
227 | if ((errflags & DH_CHECK_PUBKEY_INVALID) != 0) | |
228 | DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_INVALID); | |
229 | ||
230 | return errflags == 0; | |
231 | } | |
232 | ||
8083fd3a SL |
233 | /* |
234 | * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Full public key validation. | |
235 | */ | |
bf3d6c0c | 236 | int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret) |
8083fd3a | 237 | { |
5357c106 | 238 | return ossl_ffc_validate_public_key(&dh->params, pub_key, ret); |
8083fd3a SL |
239 | } |
240 | ||
241 | /* | |
242 | * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Partial public key validation. | |
243 | * To only be used with ephemeral FFC public keys generated using the approved | |
244 | * safe-prime groups. | |
245 | */ | |
246 | int dh_check_pub_key_partial(const DH *dh, const BIGNUM *pub_key, int *ret) | |
247 | { | |
5357c106 | 248 | return ossl_ffc_validate_public_key_partial(&dh->params, pub_key, ret); |
8083fd3a SL |
249 | } |
250 | ||
251 | int dh_check_priv_key(const DH *dh, const BIGNUM *priv_key, int *ret) | |
0f113f3e MC |
252 | { |
253 | int ok = 0; | |
8083fd3a | 254 | BIGNUM *two_powN = NULL, *upper; |
bf3d6c0c | 255 | |
0f113f3e | 256 | *ret = 0; |
8083fd3a SL |
257 | two_powN = BN_new(); |
258 | if (two_powN == NULL) | |
259 | return 0; | |
260 | if (dh->params.q == NULL) | |
f5a12207 | 261 | goto err; |
8083fd3a | 262 | upper = dh->params.q; |
bf3d6c0c | 263 | |
8083fd3a SL |
264 | /* Is it from an approved Safe prime group ?*/ |
265 | if (DH_get_nid((DH *)dh) != NID_undef) { | |
266 | if (!BN_lshift(two_powN, BN_value_one(), dh->length)) | |
b128abc3 | 267 | goto err; |
8083fd3a SL |
268 | if (BN_cmp(two_powN, dh->params.q) < 0) |
269 | upper = two_powN; | |
b128abc3 | 270 | } |
5357c106 | 271 | if (!ossl_ffc_validate_private_key(upper, priv_key, ret)) |
8083fd3a | 272 | goto err; |
b128abc3 | 273 | |
0f113f3e | 274 | ok = 1; |
8083fd3a SL |
275 | err: |
276 | BN_free(two_powN); | |
26a7d938 | 277 | return ok; |
0f113f3e | 278 | } |
8083fd3a SL |
279 | |
280 | /* | |
281 | * FFC pairwise check from SP800-56A R3. | |
282 | * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency | |
283 | */ | |
d1fb6b48 | 284 | int dh_check_pairwise(const DH *dh) |
8083fd3a SL |
285 | { |
286 | int ret = 0; | |
287 | BN_CTX *ctx = NULL; | |
288 | BIGNUM *pub_key = NULL; | |
289 | ||
290 | if (dh->params.p == NULL | |
291 | || dh->params.g == NULL | |
292 | || dh->priv_key == NULL | |
293 | || dh->pub_key == NULL) | |
294 | return 0; | |
295 | ||
296 | ctx = BN_CTX_new_ex(dh->libctx); | |
297 | if (ctx == NULL) | |
298 | goto err; | |
299 | pub_key = BN_new(); | |
300 | if (pub_key == NULL) | |
301 | goto err; | |
302 | ||
303 | /* recalculate the public key = (g ^ priv) mod p */ | |
304 | if (!dh_generate_public_key(ctx, dh, dh->priv_key, pub_key)) | |
305 | goto err; | |
306 | /* check it matches the existing pubic_key */ | |
307 | ret = BN_cmp(pub_key, dh->pub_key) == 0; | |
308 | err: | |
309 | BN_free(pub_key); | |
310 | BN_CTX_free(ctx); | |
311 | return ret; | |
312 | } |