]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dh/dh_check.c
Uniform TEST_*() check usage in test/ectest.c
[thirdparty/openssl.git] / crypto / dh / dh_check.c
CommitLineData
aa6bb135 1/*
a38c878c 2 * Copyright 1995-2019 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
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
ec577822 12#include <openssl/bn.h>
0aeddcfa 13#include "dh_locl.h"
d02b48c6 14
2500c093
JM
15# define DH_NUMBER_ITERATIONS_FOR_PRIME 64
16
26505153
RL
17/*-
18 * Check that p and g are suitable enough
19 *
20 * p is odd
21 * 1 < g < p - 1
22 */
b0004708
PY
23int DH_check_params_ex(const DH *dh)
24{
25 int errflags = 0;
26
a38c878c
BE
27 if (!DH_check_params(dh, &errflags))
28 return 0;
b0004708
PY
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}
26505153
RL
37
38int DH_check_params(const DH *dh, int *ret)
39{
40 int ok = 0;
41 BIGNUM *tmp = NULL;
42 BN_CTX *ctx = NULL;
43
44 *ret = 0;
45 ctx = BN_CTX_new();
46 if (ctx == NULL)
47 goto err;
48 BN_CTX_start(ctx);
49 tmp = BN_CTX_get(ctx);
50 if (tmp == NULL)
51 goto err;
52
53 if (!BN_is_odd(dh->p))
54 *ret |= DH_CHECK_P_NOT_PRIME;
55 if (BN_is_negative(dh->g) || BN_is_zero(dh->g) || BN_is_one(dh->g))
56 *ret |= DH_NOT_SUITABLE_GENERATOR;
57 if (BN_copy(tmp, dh->p) == NULL || !BN_sub_word(tmp, 1))
58 goto err;
59 if (BN_cmp(dh->g, tmp) >= 0)
60 *ret |= DH_NOT_SUITABLE_GENERATOR;
61
62 ok = 1;
63 err:
ce1415ed
SL
64 BN_CTX_end(ctx);
65 BN_CTX_free(ctx);
26a7d938 66 return ok;
26505153
RL
67}
68
1d97c843
TH
69/*-
70 * Check that p is a safe prime and
a38c878c 71 * g is a suitable generator.
d02b48c6 72 */
b0004708
PY
73int DH_check_ex(const DH *dh)
74{
75 int errflags = 0;
76
a38c878c
BE
77 if (!DH_check(dh, &errflags))
78 return 0;
b0004708
PY
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}
d02b48c6 97
f971ccb2 98int DH_check(const DH *dh, int *ret)
0f113f3e 99{
748e8530 100 int ok = 0, r;
0f113f3e 101 BN_CTX *ctx = NULL;
0f113f3e
MC
102 BIGNUM *t1 = NULL, *t2 = NULL;
103
a38c878c
BE
104 if (!DH_check_params(dh, ret))
105 return 0;
106
0f113f3e
MC
107 ctx = BN_CTX_new();
108 if (ctx == NULL)
109 goto err;
110 BN_CTX_start(ctx);
111 t1 = BN_CTX_get(ctx);
0f113f3e
MC
112 t2 = BN_CTX_get(ctx);
113 if (t2 == NULL)
114 goto err;
d02b48c6 115
0f113f3e
MC
116 if (dh->q) {
117 if (BN_cmp(dh->g, BN_value_one()) <= 0)
118 *ret |= DH_NOT_SUITABLE_GENERATOR;
119 else if (BN_cmp(dh->g, dh->p) >= 0)
120 *ret |= DH_NOT_SUITABLE_GENERATOR;
121 else {
122 /* Check g^q == 1 mod p */
123 if (!BN_mod_exp(t1, dh->g, dh->q, dh->p, ctx))
124 goto err;
125 if (!BN_is_one(t1))
126 *ret |= DH_NOT_SUITABLE_GENERATOR;
127 }
2500c093 128 r = BN_is_prime_ex(dh->q, DH_NUMBER_ITERATIONS_FOR_PRIME, ctx, NULL);
748e8530
DB
129 if (r < 0)
130 goto err;
131 if (!r)
0f113f3e
MC
132 *ret |= DH_CHECK_Q_NOT_PRIME;
133 /* Check p == 1 mod q i.e. q divides p - 1 */
134 if (!BN_div(t1, t2, dh->p, dh->q, ctx))
135 goto err;
136 if (!BN_is_one(t2))
137 *ret |= DH_CHECK_INVALID_Q_VALUE;
138 if (dh->j && BN_cmp(dh->j, t1))
139 *ret |= DH_CHECK_INVALID_J_VALUE;
a38c878c 140 }
d02b48c6 141
2500c093 142 r = BN_is_prime_ex(dh->p, DH_NUMBER_ITERATIONS_FOR_PRIME, ctx, NULL);
748e8530
DB
143 if (r < 0)
144 goto err;
145 if (!r)
0f113f3e
MC
146 *ret |= DH_CHECK_P_NOT_PRIME;
147 else if (!dh->q) {
148 if (!BN_rshift1(t1, dh->p))
149 goto err;
2500c093 150 r = BN_is_prime_ex(t1, DH_NUMBER_ITERATIONS_FOR_PRIME, ctx, NULL);
748e8530
DB
151 if (r < 0)
152 goto err;
e705fcf1 153 if (!r)
0f113f3e
MC
154 *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
155 }
156 ok = 1;
157 err:
ce1415ed
SL
158 BN_CTX_end(ctx);
159 BN_CTX_free(ctx);
26a7d938 160 return ok;
0f113f3e 161}
bf3d6c0c 162
b0004708
PY
163int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key)
164{
165 int errflags = 0;
166
167 (void)DH_check(dh, &errflags);
168
169 if ((errflags & DH_CHECK_PUBKEY_TOO_SMALL) != 0)
170 DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_TOO_SMALL);
171 if ((errflags & DH_CHECK_PUBKEY_TOO_LARGE) != 0)
172 DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_TOO_LARGE);
173 if ((errflags & DH_CHECK_PUBKEY_INVALID) != 0)
174 DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_INVALID);
175
176 return errflags == 0;
177}
178
bf3d6c0c 179int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
0f113f3e
MC
180{
181 int ok = 0;
b128abc3
MC
182 BIGNUM *tmp = NULL;
183 BN_CTX *ctx = NULL;
bf3d6c0c 184
0f113f3e 185 *ret = 0;
b128abc3
MC
186 ctx = BN_CTX_new();
187 if (ctx == NULL)
0f113f3e 188 goto err;
b128abc3
MC
189 BN_CTX_start(ctx);
190 tmp = BN_CTX_get(ctx);
f5a12207 191 if (tmp == NULL || !BN_set_word(tmp, 1))
b128abc3 192 goto err;
b128abc3 193 if (BN_cmp(pub_key, tmp) <= 0)
0f113f3e 194 *ret |= DH_CHECK_PUBKEY_TOO_SMALL;
f5a12207
MC
195 if (BN_copy(tmp, dh->p) == NULL || !BN_sub_word(tmp, 1))
196 goto err;
b128abc3 197 if (BN_cmp(pub_key, tmp) >= 0)
0f113f3e 198 *ret |= DH_CHECK_PUBKEY_TOO_LARGE;
bf3d6c0c 199
b128abc3
MC
200 if (dh->q != NULL) {
201 /* Check pub_key^q == 1 mod p */
202 if (!BN_mod_exp(tmp, pub_key, dh->q, dh->p, ctx))
203 goto err;
204 if (!BN_is_one(tmp))
205 *ret |= DH_CHECK_PUBKEY_INVALID;
206 }
207
0f113f3e
MC
208 ok = 1;
209 err:
ce1415ed
SL
210 BN_CTX_end(ctx);
211 BN_CTX_free(ctx);
26a7d938 212 return ok;
0f113f3e 213}