xref: /dragonfly/crypto/libressl/crypto/dh/dh_check.c (revision 6f5ec8b5)
1 /* $OpenBSD: dh_check.c,v 1.25 2022/07/13 18:38:20 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <stdio.h>
60 
61 #include <openssl/bn.h>
62 #include <openssl/dh.h>
63 #include <openssl/err.h>
64 
65 #include "bn_lcl.h"
66 #include "dh_local.h"
67 
68 #define DH_NUMBER_ITERATIONS_FOR_PRIME 64
69 
70 /*
71  * Check that p is odd and 1 < g < p - 1. The _ex version removes the need of
72  * inspecting flags and pushes errors on the stack instead.
73  */
74 
75 int
76 DH_check_params_ex(const DH *dh)
77 {
78 	int flags = 0;
79 
80 	if (!DH_check_params(dh, &flags))
81 		return 0;
82 
83 	if ((flags & DH_CHECK_P_NOT_PRIME) != 0)
84 		DHerror(DH_R_CHECK_P_NOT_PRIME);
85 	if ((flags & DH_NOT_SUITABLE_GENERATOR) != 0)
86 		DHerror(DH_R_NOT_SUITABLE_GENERATOR);
87 
88 	return flags == 0;
89 }
90 
91 int
92 DH_check_params(const DH *dh, int *flags)
93 {
94 	BIGNUM *max_g = NULL;
95 	int ok = 0;
96 
97 	*flags = 0;
98 
99 	if (!BN_is_odd(dh->p))
100 		*flags |= DH_CHECK_P_NOT_PRIME;
101 
102 	/*
103 	 * Check that 1 < dh->g < p - 1
104 	 */
105 
106 	if (BN_cmp(dh->g, BN_value_one()) <= 0)
107 		*flags |= DH_NOT_SUITABLE_GENERATOR;
108 	/* max_g = p - 1 */
109 	if ((max_g = BN_dup(dh->p)) == NULL)
110 		goto err;
111 	if (!BN_sub_word(max_g, 1))
112 		goto err;
113 	/* check that g < max_g */
114 	if (BN_cmp(dh->g, max_g) >= 0)
115 		*flags |= DH_NOT_SUITABLE_GENERATOR;
116 
117 	ok = 1;
118 
119  err:
120 	BN_free(max_g);
121 
122 	return ok;
123 }
124 
125 /*
126  * Check that p is a safe prime and that g is a suitable generator.
127  * The _ex version puts errors on the stack instead of returning flags.
128  */
129 
130 int
131 DH_check_ex(const DH *dh)
132 {
133 	int flags = 0;
134 
135 	if (!DH_check(dh, &flags))
136 		return 0;
137 
138 	if ((flags & DH_NOT_SUITABLE_GENERATOR) != 0)
139 		DHerror(DH_R_NOT_SUITABLE_GENERATOR);
140 	if ((flags & DH_CHECK_Q_NOT_PRIME) != 0)
141 		DHerror(DH_R_CHECK_Q_NOT_PRIME);
142 	if ((flags & DH_CHECK_INVALID_Q_VALUE) != 0)
143 		DHerror(DH_R_CHECK_INVALID_Q_VALUE);
144 	if ((flags & DH_CHECK_INVALID_J_VALUE) != 0)
145 		DHerror(DH_R_CHECK_INVALID_J_VALUE);
146 	if ((flags & DH_UNABLE_TO_CHECK_GENERATOR) != 0)
147 		DHerror(DH_R_UNABLE_TO_CHECK_GENERATOR);
148 	if ((flags & DH_CHECK_P_NOT_PRIME) != 0)
149 		DHerror(DH_R_CHECK_P_NOT_PRIME);
150 	if ((flags & DH_CHECK_P_NOT_SAFE_PRIME) != 0)
151 		DHerror(DH_R_CHECK_P_NOT_SAFE_PRIME);
152 
153 	return flags == 0;
154 }
155 
156 int
157 DH_check(const DH *dh, int *flags)
158 {
159 	BN_CTX *ctx = NULL;
160 	int is_prime;
161 	int ok = 0;
162 
163 	*flags = 0;
164 
165 	if (!DH_check_params(dh, flags))
166 		goto err;
167 
168 	ctx = BN_CTX_new();
169 	if (ctx == NULL)
170 		goto err;
171 	BN_CTX_start(ctx);
172 
173 	if (dh->q != NULL) {
174 		BIGNUM *quotient, *residue;
175 
176 		if ((quotient = BN_CTX_get(ctx)) == NULL)
177 			goto err;
178 		if ((residue = BN_CTX_get(ctx)) == NULL)
179 			goto err;
180 		if ((*flags & DH_NOT_SUITABLE_GENERATOR) == 0) {
181 			/* Check g^q == 1 mod p */
182 			if (!BN_mod_exp_ct(residue, dh->g, dh->q, dh->p, ctx))
183 				goto err;
184 			if (!BN_is_one(residue))
185 				*flags |= DH_NOT_SUITABLE_GENERATOR;
186 		}
187 		is_prime = BN_is_prime_ex(dh->q, DH_NUMBER_ITERATIONS_FOR_PRIME,
188 		    ctx, NULL);
189 		if (is_prime < 0)
190 			goto err;
191 		if (is_prime == 0)
192 			*flags |= DH_CHECK_Q_NOT_PRIME;
193 		/* Check p == 1 mod q, i.e., q divides p - 1 */
194 		if (!BN_div_ct(quotient, residue, dh->p, dh->q, ctx))
195 			goto err;
196 		if (!BN_is_one(residue))
197 			*flags |= DH_CHECK_INVALID_Q_VALUE;
198 		if (dh->j != NULL && BN_cmp(dh->j, quotient) != 0)
199 			*flags |= DH_CHECK_INVALID_J_VALUE;
200 	}
201 
202 	is_prime = BN_is_prime_ex(dh->p, DH_NUMBER_ITERATIONS_FOR_PRIME,
203 	    ctx, NULL);
204 	if (is_prime < 0)
205 		goto err;
206 	if (is_prime == 0)
207 		*flags |= DH_CHECK_P_NOT_PRIME;
208 	else if (dh->q == NULL) {
209 		BIGNUM *q;
210 
211 		if ((q = BN_CTX_get(ctx)) == NULL)
212 			goto err;
213 		if (!BN_rshift1(q, dh->p))
214 			goto err;
215 		is_prime = BN_is_prime_ex(q, DH_NUMBER_ITERATIONS_FOR_PRIME,
216 		    ctx, NULL);
217 		if (is_prime < 0)
218 			goto err;
219 		if (is_prime == 0)
220 			*flags |= DH_CHECK_P_NOT_SAFE_PRIME;
221 	}
222 
223 	ok = 1;
224 
225  err:
226 	BN_CTX_end(ctx);
227 	BN_CTX_free(ctx);
228 	return ok;
229 }
230 
231 int
232 DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key)
233 {
234 	int flags = 0;
235 
236 	if (!DH_check_pub_key(dh, pub_key, &flags))
237 		return 0;
238 
239 	if ((flags & DH_CHECK_PUBKEY_TOO_SMALL) != 0)
240 		DHerror(DH_R_CHECK_PUBKEY_TOO_SMALL);
241 	if ((flags & DH_CHECK_PUBKEY_TOO_LARGE) != 0)
242 		DHerror(DH_R_CHECK_PUBKEY_TOO_LARGE);
243 	if ((flags & DH_CHECK_PUBKEY_INVALID) != 0)
244 		DHerror(DH_R_CHECK_PUBKEY_INVALID);
245 
246 	return flags == 0;
247 }
248 
249 int
250 DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *flags)
251 {
252 	BN_CTX *ctx = NULL;
253 	BIGNUM *max_pub_key;
254 	int ok = 0;
255 
256 	*flags = 0;
257 
258 	if ((ctx = BN_CTX_new()) == NULL)
259 		goto err;
260 	BN_CTX_start(ctx);
261 	if ((max_pub_key = BN_CTX_get(ctx)) == NULL)
262 		goto err;
263 
264 	/*
265 	 * Check that 1 < pub_key < dh->p - 1
266 	 */
267 
268 	if (BN_cmp(pub_key, BN_value_one()) <= 0)
269 		*flags |= DH_CHECK_PUBKEY_TOO_SMALL;
270 
271 	/* max_pub_key = dh->p - 1 */
272 	if (!BN_sub(max_pub_key, dh->p, BN_value_one()))
273 		goto err;
274 
275 	if (BN_cmp(pub_key, max_pub_key) >= 0)
276 		*flags |= DH_CHECK_PUBKEY_TOO_LARGE;
277 
278 	/*
279 	 * If dh->q is set, check that pub_key^q == 1 mod p
280 	 */
281 
282 	if (dh->q != NULL) {
283 		BIGNUM *residue;
284 
285 		if ((residue = BN_CTX_get(ctx)) == NULL)
286 			goto err;
287 
288 		if (!BN_mod_exp_ct(residue, pub_key, dh->q, dh->p, ctx))
289 			goto err;
290 		if (!BN_is_one(residue))
291 			*flags = DH_CHECK_PUBKEY_INVALID;
292 	}
293 
294 	ok = 1;
295 
296  err:
297 	BN_CTX_end(ctx);
298 	BN_CTX_free(ctx);
299 
300 	return ok;
301 }
302