1 /*
2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
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
16 #include <stdio.h>
17 #include "internal/cryptlib.h"
18 #include <openssl/bn.h>
19 #include "dh_local.h"
20 #include "crypto/dh.h"
21
22 /*-
23 * Check that p and g are suitable enough
24 *
25 * p is odd
26 * 1 < g < p - 1
27 */
DH_check_params_ex(const DH * dh)28 int DH_check_params_ex(const DH *dh)
29 {
30 int errflags = 0;
31
32 if (!DH_check_params(dh, &errflags))
33 return 0;
34
35 if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
36 ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_PRIME);
37 if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
38 ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
39 if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
40 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
41 if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
42 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
43
44 return errflags == 0;
45 }
46
47 #ifdef FIPS_MODULE
DH_check_params(const DH * dh,int * ret)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 */
65 return ossl_ffc_params_FIPS186_4_validate(dh->libctx, &dh->params,
66 FFC_PARAM_TYPE_DH, ret, NULL);
67 }
68 #else
DH_check_params(const DH * dh,int * ret)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_ex(dh->libctx);
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
84 if (!BN_is_odd(dh->params.p))
85 *ret |= DH_CHECK_P_NOT_PRIME;
86 if (BN_is_negative(dh->params.g)
87 || BN_is_zero(dh->params.g)
88 || BN_is_one(dh->params.g))
89 *ret |= DH_NOT_SUITABLE_GENERATOR;
90 if (BN_copy(tmp, dh->params.p) == NULL || !BN_sub_word(tmp, 1))
91 goto err;
92 if (BN_cmp(dh->params.g, tmp) >= 0)
93 *ret |= DH_NOT_SUITABLE_GENERATOR;
94 if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS)
95 *ret |= DH_MODULUS_TOO_SMALL;
96 if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS)
97 *ret |= DH_MODULUS_TOO_LARGE;
98
99 ok = 1;
100 err:
101 BN_CTX_end(ctx);
102 BN_CTX_free(ctx);
103 return ok;
104 }
105 #endif /* FIPS_MODULE */
106
107 /*-
108 * Check that p is a safe prime and
109 * g is a suitable generator.
110 */
DH_check_ex(const DH * dh)111 int DH_check_ex(const DH *dh)
112 {
113 int errflags = 0;
114
115 if (!DH_check(dh, &errflags))
116 return 0;
117
118 if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
119 ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
120 if ((errflags & DH_CHECK_Q_NOT_PRIME) != 0)
121 ERR_raise(ERR_LIB_DH, DH_R_CHECK_Q_NOT_PRIME);
122 if ((errflags & DH_CHECK_INVALID_Q_VALUE) != 0)
123 ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_Q_VALUE);
124 if ((errflags & DH_CHECK_INVALID_J_VALUE) != 0)
125 ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_J_VALUE);
126 if ((errflags & DH_UNABLE_TO_CHECK_GENERATOR) != 0)
127 ERR_raise(ERR_LIB_DH, DH_R_UNABLE_TO_CHECK_GENERATOR);
128 if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
129 ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_PRIME);
130 if ((errflags & DH_CHECK_P_NOT_SAFE_PRIME) != 0)
131 ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_SAFE_PRIME);
132 if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
133 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
134 if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
135 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
136
137 return errflags == 0;
138 }
139
140 /* Note: according to documentation - this only checks the params */
DH_check(const DH * dh,int * ret)141 int DH_check(const DH *dh, int *ret)
142 {
143 #ifdef FIPS_MODULE
144 return DH_check_params(dh, ret);
145 #else
146 int ok = 0, r, q_good = 0;
147 BN_CTX *ctx = NULL;
148 BIGNUM *t1 = NULL, *t2 = NULL;
149 int nid = DH_get_nid((DH *)dh);
150
151 *ret = 0;
152 if (nid != NID_undef)
153 return 1;
154
155 /* Don't do any checks at all with an excessively large modulus */
156 if (BN_num_bits(dh->params.p) > OPENSSL_DH_CHECK_MAX_MODULUS_BITS) {
157 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
158 return 0;
159 }
160
161 if (!DH_check_params(dh, ret))
162 return 0;
163
164 ctx = BN_CTX_new_ex(dh->libctx);
165 if (ctx == NULL)
166 goto err;
167 BN_CTX_start(ctx);
168 t1 = BN_CTX_get(ctx);
169 t2 = BN_CTX_get(ctx);
170 if (t2 == NULL)
171 goto err;
172
173 if (dh->params.q != NULL) {
174 if (BN_ucmp(dh->params.p, dh->params.q) > 0)
175 q_good = 1;
176 else
177 *ret |= DH_CHECK_INVALID_Q_VALUE;
178 }
179
180 if (q_good) {
181 if (BN_cmp(dh->params.g, BN_value_one()) <= 0)
182 *ret |= DH_NOT_SUITABLE_GENERATOR;
183 else if (BN_cmp(dh->params.g, dh->params.p) >= 0)
184 *ret |= DH_NOT_SUITABLE_GENERATOR;
185 else {
186 /* Check g^q == 1 mod p */
187 if (!BN_mod_exp(t1, dh->params.g, dh->params.q, dh->params.p, ctx))
188 goto err;
189 if (!BN_is_one(t1))
190 *ret |= DH_NOT_SUITABLE_GENERATOR;
191 }
192 r = BN_check_prime(dh->params.q, ctx, NULL);
193 if (r < 0)
194 goto err;
195 if (!r)
196 *ret |= DH_CHECK_Q_NOT_PRIME;
197 /* Check p == 1 mod q i.e. q divides p - 1 */
198 if (!BN_div(t1, t2, dh->params.p, dh->params.q, ctx))
199 goto err;
200 if (!BN_is_one(t2))
201 *ret |= DH_CHECK_INVALID_Q_VALUE;
202 if (dh->params.j != NULL
203 && BN_cmp(dh->params.j, t1))
204 *ret |= DH_CHECK_INVALID_J_VALUE;
205 }
206
207 r = BN_check_prime(dh->params.p, ctx, NULL);
208 if (r < 0)
209 goto err;
210 if (!r)
211 *ret |= DH_CHECK_P_NOT_PRIME;
212 else if (dh->params.q == NULL) {
213 if (!BN_rshift1(t1, dh->params.p))
214 goto err;
215 r = BN_check_prime(t1, ctx, NULL);
216 if (r < 0)
217 goto err;
218 if (!r)
219 *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
220 }
221 ok = 1;
222 err:
223 BN_CTX_end(ctx);
224 BN_CTX_free(ctx);
225 return ok;
226 #endif /* FIPS_MODULE */
227 }
228
DH_check_pub_key_ex(const DH * dh,const BIGNUM * pub_key)229 int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key)
230 {
231 int errflags = 0;
232
233 if (!DH_check_pub_key(dh, pub_key, &errflags))
234 return 0;
235
236 if ((errflags & DH_CHECK_PUBKEY_TOO_SMALL) != 0)
237 ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_SMALL);
238 if ((errflags & DH_CHECK_PUBKEY_TOO_LARGE) != 0)
239 ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_LARGE);
240 if ((errflags & DH_CHECK_PUBKEY_INVALID) != 0)
241 ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_INVALID);
242
243 return errflags == 0;
244 }
245
246 /*
247 * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Full public key validation.
248 */
DH_check_pub_key(const DH * dh,const BIGNUM * pub_key,int * ret)249 int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
250 {
251 /* Don't do any checks at all with an excessively large modulus */
252 if (BN_num_bits(dh->params.p) > OPENSSL_DH_CHECK_MAX_MODULUS_BITS) {
253 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
254 *ret = DH_MODULUS_TOO_LARGE | DH_CHECK_PUBKEY_INVALID;
255 return 0;
256 }
257
258 if (dh->params.q != NULL && BN_ucmp(dh->params.p, dh->params.q) < 0) {
259 *ret |= DH_CHECK_INVALID_Q_VALUE | DH_CHECK_PUBKEY_INVALID;
260 return 1;
261 }
262
263 return ossl_ffc_validate_public_key(&dh->params, pub_key, ret);
264 }
265
266 /*
267 * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Partial public key validation.
268 * To only be used with ephemeral FFC public keys generated using the approved
269 * safe-prime groups.
270 */
ossl_dh_check_pub_key_partial(const DH * dh,const BIGNUM * pub_key,int * ret)271 int ossl_dh_check_pub_key_partial(const DH *dh, const BIGNUM *pub_key, int *ret)
272 {
273 return ossl_ffc_validate_public_key_partial(&dh->params, pub_key, ret);
274 }
275
ossl_dh_check_priv_key(const DH * dh,const BIGNUM * priv_key,int * ret)276 int ossl_dh_check_priv_key(const DH *dh, const BIGNUM *priv_key, int *ret)
277 {
278 int ok = 0;
279 BIGNUM *two_powN = NULL, *upper;
280
281 *ret = 0;
282 two_powN = BN_new();
283 if (two_powN == NULL)
284 return 0;
285
286 if (dh->params.q != NULL) {
287 upper = dh->params.q;
288 #ifndef FIPS_MODULE
289 } else if (dh->params.p != NULL) {
290 /*
291 * We do not have q so we just check the key is within some
292 * reasonable range, or the number of bits is equal to dh->length.
293 */
294 int length = dh->length;
295
296 if (length == 0) {
297 length = BN_num_bits(dh->params.p) - 1;
298 if (BN_num_bits(priv_key) <= length
299 && BN_num_bits(priv_key) > 1)
300 ok = 1;
301 } else if (BN_num_bits(priv_key) == length) {
302 ok = 1;
303 }
304 goto end;
305 #endif
306 } else {
307 goto end;
308 }
309
310 /* Is it from an approved Safe prime group ?*/
311 if (DH_get_nid((DH *)dh) != NID_undef && dh->length != 0) {
312 if (!BN_lshift(two_powN, BN_value_one(), dh->length))
313 goto end;
314 if (BN_cmp(two_powN, dh->params.q) < 0)
315 upper = two_powN;
316 }
317 if (!ossl_ffc_validate_private_key(upper, priv_key, ret))
318 goto end;
319
320 ok = 1;
321 end:
322 BN_free(two_powN);
323 return ok;
324 }
325
326 /*
327 * FFC pairwise check from SP800-56A R3.
328 * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
329 */
ossl_dh_check_pairwise(const DH * dh)330 int ossl_dh_check_pairwise(const DH *dh)
331 {
332 int ret = 0;
333 BN_CTX *ctx = NULL;
334 BIGNUM *pub_key = NULL;
335
336 if (dh->params.p == NULL
337 || dh->params.g == NULL
338 || dh->priv_key == NULL
339 || dh->pub_key == NULL)
340 return 0;
341
342 ctx = BN_CTX_new_ex(dh->libctx);
343 if (ctx == NULL)
344 goto err;
345 pub_key = BN_new();
346 if (pub_key == NULL)
347 goto err;
348
349 /* recalculate the public key = (g ^ priv) mod p */
350 if (!ossl_dh_generate_public_key(ctx, dh, dh->priv_key, pub_key))
351 goto err;
352 /* check it matches the existing pubic_key */
353 ret = BN_cmp(pub_key, dh->pub_key) == 0;
354 err:
355 BN_free(pub_key);
356 BN_CTX_free(ctx);
357 return ret;
358 }
359