/src/openssl111/crypto/dh/dh_check.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 | | #include <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include <openssl/bn.h> |
13 | | #include "dh_local.h" |
14 | | |
15 | | # define DH_NUMBER_ITERATIONS_FOR_PRIME 64 |
16 | | |
17 | | /*- |
18 | | * Check that p and g are suitable enough |
19 | | * |
20 | | * p is odd |
21 | | * 1 < g < p - 1 |
22 | | */ |
23 | | int DH_check_params_ex(const DH *dh) |
24 | | { |
25 | | int errflags = 0; |
26 | | |
27 | | if (!DH_check_params(dh, &errflags)) |
28 | | return 0; |
29 | | |
30 | | if ((errflags & DH_CHECK_P_NOT_PRIME) != 0) |
31 | | DHerr(DH_F_DH_CHECK_PARAMS_EX, DH_R_CHECK_P_NOT_PRIME); |
32 | | if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0) |
33 | | DHerr(DH_F_DH_CHECK_PARAMS_EX, DH_R_NOT_SUITABLE_GENERATOR); |
34 | | |
35 | | return errflags == 0; |
36 | | } |
37 | | |
38 | | int DH_check_params(const DH *dh, int *ret) |
39 | 1.65k | { |
40 | 1.65k | int ok = 0; |
41 | 1.65k | BIGNUM *tmp = NULL; |
42 | 1.65k | BN_CTX *ctx = NULL; |
43 | | |
44 | 1.65k | *ret = 0; |
45 | 1.65k | ctx = BN_CTX_new(); |
46 | 1.65k | if (ctx == NULL) |
47 | 0 | goto err; |
48 | 1.65k | BN_CTX_start(ctx); |
49 | 1.65k | tmp = BN_CTX_get(ctx); |
50 | 1.65k | if (tmp == NULL) |
51 | 0 | goto err; |
52 | | |
53 | 1.65k | if (!BN_is_odd(dh->p)) |
54 | 100 | *ret |= DH_CHECK_P_NOT_PRIME; |
55 | 1.65k | if (BN_is_negative(dh->g) || BN_is_zero(dh->g) || BN_is_one(dh->g)) |
56 | 28 | *ret |= DH_NOT_SUITABLE_GENERATOR; |
57 | 1.65k | if (BN_copy(tmp, dh->p) == NULL || !BN_sub_word(tmp, 1)) |
58 | 0 | goto err; |
59 | 1.65k | if (BN_cmp(dh->g, tmp) >= 0) |
60 | 74 | *ret |= DH_NOT_SUITABLE_GENERATOR; |
61 | | |
62 | 1.65k | ok = 1; |
63 | 1.65k | err: |
64 | 1.65k | BN_CTX_end(ctx); |
65 | 1.65k | BN_CTX_free(ctx); |
66 | 1.65k | return ok; |
67 | 1.65k | } |
68 | | |
69 | | /*- |
70 | | * Check that p is a safe prime and |
71 | | * g is a suitable generator. |
72 | | */ |
73 | | int DH_check_ex(const DH *dh) |
74 | | { |
75 | | int errflags = 0; |
76 | | |
77 | | if (!DH_check(dh, &errflags)) |
78 | | return 0; |
79 | | |
80 | | if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0) |
81 | | DHerr(DH_F_DH_CHECK_EX, DH_R_NOT_SUITABLE_GENERATOR); |
82 | | if ((errflags & DH_CHECK_Q_NOT_PRIME) != 0) |
83 | | DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_Q_NOT_PRIME); |
84 | | if ((errflags & DH_CHECK_INVALID_Q_VALUE) != 0) |
85 | | DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_INVALID_Q_VALUE); |
86 | | if ((errflags & DH_CHECK_INVALID_J_VALUE) != 0) |
87 | | DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_INVALID_J_VALUE); |
88 | | if ((errflags & DH_UNABLE_TO_CHECK_GENERATOR) != 0) |
89 | | DHerr(DH_F_DH_CHECK_EX, DH_R_UNABLE_TO_CHECK_GENERATOR); |
90 | | if ((errflags & DH_CHECK_P_NOT_PRIME) != 0) |
91 | | DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_P_NOT_PRIME); |
92 | | if ((errflags & DH_CHECK_P_NOT_SAFE_PRIME) != 0) |
93 | | DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_P_NOT_SAFE_PRIME); |
94 | | |
95 | | return errflags == 0; |
96 | | } |
97 | | |
98 | | int DH_check(const DH *dh, int *ret) |
99 | | { |
100 | | int ok = 0, r, q_good = 0; |
101 | | BN_CTX *ctx = NULL; |
102 | | BIGNUM *t1 = NULL, *t2 = NULL; |
103 | | |
104 | | /* Don't do any checks at all with an excessively large modulus */ |
105 | | if (BN_num_bits(dh->p) > OPENSSL_DH_CHECK_MAX_MODULUS_BITS) { |
106 | | DHerr(DH_F_DH_CHECK, DH_R_MODULUS_TOO_LARGE); |
107 | | *ret = DH_CHECK_P_NOT_PRIME; |
108 | | return 0; |
109 | | } |
110 | | |
111 | | if (!DH_check_params(dh, ret)) |
112 | | return 0; |
113 | | |
114 | | ctx = BN_CTX_new(); |
115 | | if (ctx == NULL) |
116 | | goto err; |
117 | | BN_CTX_start(ctx); |
118 | | t1 = BN_CTX_get(ctx); |
119 | | t2 = BN_CTX_get(ctx); |
120 | | if (t2 == NULL) |
121 | | goto err; |
122 | | |
123 | | if (dh->q != NULL) { |
124 | | if (BN_ucmp(dh->p, dh->q) > 0) |
125 | | q_good = 1; |
126 | | else |
127 | | *ret |= DH_CHECK_INVALID_Q_VALUE; |
128 | | } |
129 | | |
130 | | if (q_good) { |
131 | | if (BN_cmp(dh->g, BN_value_one()) <= 0) |
132 | | *ret |= DH_NOT_SUITABLE_GENERATOR; |
133 | | else if (BN_cmp(dh->g, dh->p) >= 0) |
134 | | *ret |= DH_NOT_SUITABLE_GENERATOR; |
135 | | else { |
136 | | /* Check g^q == 1 mod p */ |
137 | | if (!BN_mod_exp(t1, dh->g, dh->q, dh->p, ctx)) |
138 | | goto err; |
139 | | if (!BN_is_one(t1)) |
140 | | *ret |= DH_NOT_SUITABLE_GENERATOR; |
141 | | } |
142 | | r = BN_is_prime_ex(dh->q, DH_NUMBER_ITERATIONS_FOR_PRIME, ctx, NULL); |
143 | | if (r < 0) |
144 | | goto err; |
145 | | if (!r) |
146 | | *ret |= DH_CHECK_Q_NOT_PRIME; |
147 | | /* Check p == 1 mod q i.e. q divides p - 1 */ |
148 | | if (!BN_div(t1, t2, dh->p, dh->q, ctx)) |
149 | | goto err; |
150 | | if (!BN_is_one(t2)) |
151 | | *ret |= DH_CHECK_INVALID_Q_VALUE; |
152 | | if (dh->j && BN_cmp(dh->j, t1)) |
153 | | *ret |= DH_CHECK_INVALID_J_VALUE; |
154 | | } |
155 | | |
156 | | r = BN_is_prime_ex(dh->p, DH_NUMBER_ITERATIONS_FOR_PRIME, ctx, NULL); |
157 | | if (r < 0) |
158 | | goto err; |
159 | | if (!r) |
160 | | *ret |= DH_CHECK_P_NOT_PRIME; |
161 | | else if (!dh->q) { |
162 | | if (!BN_rshift1(t1, dh->p)) |
163 | | goto err; |
164 | | r = BN_is_prime_ex(t1, DH_NUMBER_ITERATIONS_FOR_PRIME, ctx, NULL); |
165 | | if (r < 0) |
166 | | goto err; |
167 | | if (!r) |
168 | | *ret |= DH_CHECK_P_NOT_SAFE_PRIME; |
169 | | } |
170 | | ok = 1; |
171 | | err: |
172 | | BN_CTX_end(ctx); |
173 | | BN_CTX_free(ctx); |
174 | | return ok; |
175 | | } |
176 | | |
177 | | int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key) |
178 | 0 | { |
179 | 0 | int errflags = 0; |
180 | |
|
181 | 0 | if (!DH_check_pub_key(dh, pub_key, &errflags)) |
182 | 0 | return 0; |
183 | | |
184 | 0 | if ((errflags & DH_CHECK_PUBKEY_TOO_SMALL) != 0) |
185 | 0 | DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_TOO_SMALL); |
186 | 0 | if ((errflags & DH_CHECK_PUBKEY_TOO_LARGE) != 0) |
187 | 0 | DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_TOO_LARGE); |
188 | 0 | if ((errflags & DH_CHECK_PUBKEY_INVALID) != 0) |
189 | 0 | DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_INVALID); |
190 | |
|
191 | 0 | return errflags == 0; |
192 | 0 | } |
193 | | |
194 | | int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret) |
195 | 918 | { |
196 | 918 | int ok = 0; |
197 | 918 | BIGNUM *tmp = NULL; |
198 | 918 | BN_CTX *ctx = NULL; |
199 | | |
200 | 918 | *ret = 0; |
201 | 918 | ctx = BN_CTX_new(); |
202 | 918 | if (ctx == NULL) |
203 | 0 | goto err; |
204 | 918 | BN_CTX_start(ctx); |
205 | 918 | tmp = BN_CTX_get(ctx); |
206 | 918 | if (tmp == NULL || !BN_set_word(tmp, 1)) |
207 | 0 | goto err; |
208 | 918 | if (BN_cmp(pub_key, tmp) <= 0) |
209 | 2 | *ret |= DH_CHECK_PUBKEY_TOO_SMALL; |
210 | 918 | if (BN_copy(tmp, dh->p) == NULL || !BN_sub_word(tmp, 1)) |
211 | 0 | goto err; |
212 | 918 | if (BN_cmp(pub_key, tmp) >= 0) |
213 | 11 | *ret |= DH_CHECK_PUBKEY_TOO_LARGE; |
214 | | |
215 | 918 | if (dh->q != NULL) { |
216 | | /* Check pub_key^q == 1 mod p */ |
217 | 0 | if (!BN_mod_exp(tmp, pub_key, dh->q, dh->p, ctx)) |
218 | 0 | goto err; |
219 | 0 | if (!BN_is_one(tmp)) |
220 | 0 | *ret |= DH_CHECK_PUBKEY_INVALID; |
221 | 0 | } |
222 | | |
223 | 918 | ok = 1; |
224 | 918 | err: |
225 | 918 | BN_CTX_end(ctx); |
226 | 918 | BN_CTX_free(ctx); |
227 | 918 | return ok; |
228 | 918 | } |