xref: /openbsd-src/lib/libcrypto/dh/dh_check.c (revision 1507347831fea71816a2a2738e14838208eb78f0)
1 /* $OpenBSD: dh_check.c,v 1.30 2024/11/29 15:59:57 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_local.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.
72  */
73 
74 static int
75 DH_check_params(const DH *dh, int *flags)
76 {
77 	BIGNUM *max_g = NULL;
78 	int ok = 0;
79 
80 	*flags = 0;
81 
82 	if (!BN_is_odd(dh->p))
83 		*flags |= DH_CHECK_P_NOT_PRIME;
84 
85 	/*
86 	 * Check that 1 < dh->g < p - 1
87 	 */
88 
89 	if (BN_cmp(dh->g, BN_value_one()) <= 0)
90 		*flags |= DH_NOT_SUITABLE_GENERATOR;
91 	/* max_g = p - 1 */
92 	if ((max_g = BN_dup(dh->p)) == NULL)
93 		goto err;
94 	if (!BN_sub_word(max_g, 1))
95 		goto err;
96 	/* check that g < max_g */
97 	if (BN_cmp(dh->g, max_g) >= 0)
98 		*flags |= DH_NOT_SUITABLE_GENERATOR;
99 
100 	ok = 1;
101 
102  err:
103 	BN_free(max_g);
104 
105 	return ok;
106 }
107 
108 /*
109  * Check that p is a safe prime and that g is a suitable generator.
110  */
111 
112 int
113 DH_check(const DH *dh, int *flags)
114 {
115 	BN_CTX *ctx = NULL;
116 	int is_prime;
117 	int ok = 0;
118 
119 	*flags = 0;
120 
121 	if (!DH_check_params(dh, flags))
122 		goto err;
123 
124 	ctx = BN_CTX_new();
125 	if (ctx == NULL)
126 		goto err;
127 	BN_CTX_start(ctx);
128 
129 	if (dh->q != NULL) {
130 		BIGNUM *residue;
131 
132 		if ((residue = BN_CTX_get(ctx)) == NULL)
133 			goto err;
134 		if ((*flags & DH_NOT_SUITABLE_GENERATOR) == 0) {
135 			/* Check g^q == 1 mod p */
136 			if (!BN_mod_exp_ct(residue, dh->g, dh->q, dh->p, ctx))
137 				goto err;
138 			if (!BN_is_one(residue))
139 				*flags |= DH_NOT_SUITABLE_GENERATOR;
140 		}
141 		is_prime = BN_is_prime_ex(dh->q, DH_NUMBER_ITERATIONS_FOR_PRIME,
142 		    ctx, NULL);
143 		if (is_prime < 0)
144 			goto err;
145 		if (is_prime == 0)
146 			*flags |= DH_CHECK_Q_NOT_PRIME;
147 		/* Check p == 1 mod q, i.e., q divides p - 1 */
148 		if (!BN_div_ct(NULL, residue, dh->p, dh->q, ctx))
149 			goto err;
150 		if (!BN_is_one(residue))
151 			*flags |= DH_CHECK_INVALID_Q_VALUE;
152 	}
153 
154 	is_prime = BN_is_prime_ex(dh->p, DH_NUMBER_ITERATIONS_FOR_PRIME,
155 	    ctx, NULL);
156 	if (is_prime < 0)
157 		goto err;
158 	if (is_prime == 0)
159 		*flags |= DH_CHECK_P_NOT_PRIME;
160 	else if (dh->q == NULL) {
161 		BIGNUM *q;
162 
163 		if ((q = BN_CTX_get(ctx)) == NULL)
164 			goto err;
165 		if (!BN_rshift1(q, dh->p))
166 			goto err;
167 		is_prime = BN_is_prime_ex(q, DH_NUMBER_ITERATIONS_FOR_PRIME,
168 		    ctx, NULL);
169 		if (is_prime < 0)
170 			goto err;
171 		if (is_prime == 0)
172 			*flags |= DH_CHECK_P_NOT_SAFE_PRIME;
173 	}
174 
175 	ok = 1;
176 
177  err:
178 	BN_CTX_end(ctx);
179 	BN_CTX_free(ctx);
180 	return ok;
181 }
182 LCRYPTO_ALIAS(DH_check);
183 
184 int
185 DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *flags)
186 {
187 	BN_CTX *ctx = NULL;
188 	BIGNUM *max_pub_key;
189 	int ok = 0;
190 
191 	*flags = 0;
192 
193 	if ((ctx = BN_CTX_new()) == NULL)
194 		goto err;
195 	BN_CTX_start(ctx);
196 	if ((max_pub_key = BN_CTX_get(ctx)) == NULL)
197 		goto err;
198 
199 	/*
200 	 * Check that 1 < pub_key < dh->p - 1
201 	 */
202 
203 	if (BN_cmp(pub_key, BN_value_one()) <= 0)
204 		*flags |= DH_CHECK_PUBKEY_TOO_SMALL;
205 
206 	/* max_pub_key = dh->p - 1 */
207 	if (!BN_sub(max_pub_key, dh->p, BN_value_one()))
208 		goto err;
209 
210 	if (BN_cmp(pub_key, max_pub_key) >= 0)
211 		*flags |= DH_CHECK_PUBKEY_TOO_LARGE;
212 
213 	/*
214 	 * If dh->q is set, check that pub_key^q == 1 mod p
215 	 */
216 
217 	if (dh->q != NULL) {
218 		BIGNUM *residue;
219 
220 		if ((residue = BN_CTX_get(ctx)) == NULL)
221 			goto err;
222 
223 		if (!BN_mod_exp_ct(residue, pub_key, dh->q, dh->p, ctx))
224 			goto err;
225 		if (!BN_is_one(residue))
226 			*flags |= DH_CHECK_PUBKEY_INVALID;
227 	}
228 
229 	ok = 1;
230 
231  err:
232 	BN_CTX_end(ctx);
233 	BN_CTX_free(ctx);
234 
235 	return ok;
236 }
237 LCRYPTO_ALIAS(DH_check_pub_key);
238