2 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/bn.h>
16 * Check that p and g are suitable enough
21 int DH_check_params_ex(const DH *dh)
25 if (!DH_check_params(dh, &errflags))
28 if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
29 DHerr(DH_F_DH_CHECK_PARAMS_EX, DH_R_CHECK_P_NOT_PRIME);
30 if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
31 DHerr(DH_F_DH_CHECK_PARAMS_EX, DH_R_NOT_SUITABLE_GENERATOR);
32 if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
33 DHerr(DH_F_DH_CHECK_PARAMS_EX, DH_R_MODULUS_TOO_SMALL);
34 if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
35 DHerr(DH_F_DH_CHECK_PARAMS_EX, DH_R_MODULUS_TOO_LARGE);
40 int DH_check_params(const DH *dh, int *ret)
51 tmp = BN_CTX_get(ctx);
55 if (!BN_is_odd(dh->params.p))
56 *ret |= DH_CHECK_P_NOT_PRIME;
57 if (BN_is_negative(dh->params.g)
58 || BN_is_zero(dh->params.g)
59 || BN_is_one(dh->params.g))
60 *ret |= DH_NOT_SUITABLE_GENERATOR;
61 if (BN_copy(tmp, dh->params.p) == NULL || !BN_sub_word(tmp, 1))
63 if (BN_cmp(dh->params.g, tmp) >= 0)
64 *ret |= DH_NOT_SUITABLE_GENERATOR;
65 if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS)
66 *ret |= DH_MODULUS_TOO_SMALL;
67 if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS)
68 *ret |= DH_MODULUS_TOO_LARGE;
78 * Check that p is a safe prime and
79 * g is a suitable generator.
81 int DH_check_ex(const DH *dh)
85 if (!DH_check(dh, &errflags))
88 if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
89 DHerr(DH_F_DH_CHECK_EX, DH_R_NOT_SUITABLE_GENERATOR);
90 if ((errflags & DH_CHECK_Q_NOT_PRIME) != 0)
91 DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_Q_NOT_PRIME);
92 if ((errflags & DH_CHECK_INVALID_Q_VALUE) != 0)
93 DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_INVALID_Q_VALUE);
94 if ((errflags & DH_CHECK_INVALID_J_VALUE) != 0)
95 DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_INVALID_J_VALUE);
96 if ((errflags & DH_UNABLE_TO_CHECK_GENERATOR) != 0)
97 DHerr(DH_F_DH_CHECK_EX, DH_R_UNABLE_TO_CHECK_GENERATOR);
98 if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
99 DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_P_NOT_PRIME);
100 if ((errflags & DH_CHECK_P_NOT_SAFE_PRIME) != 0)
101 DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_P_NOT_SAFE_PRIME);
102 if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
103 DHerr(DH_F_DH_CHECK_EX, DH_R_MODULUS_TOO_SMALL);
104 if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
105 DHerr(DH_F_DH_CHECK_EX, DH_R_MODULUS_TOO_LARGE);
107 return errflags == 0;
110 int DH_check(const DH *dh, int *ret)
114 BIGNUM *t1 = NULL, *t2 = NULL;
116 if (!DH_check_params(dh, ret))
123 t1 = BN_CTX_get(ctx);
124 t2 = BN_CTX_get(ctx);
128 if (dh->params.q != NULL) {
129 if (BN_cmp(dh->params.g, BN_value_one()) <= 0)
130 *ret |= DH_NOT_SUITABLE_GENERATOR;
131 else if (BN_cmp(dh->params.g, dh->params.p) >= 0)
132 *ret |= DH_NOT_SUITABLE_GENERATOR;
134 /* Check g^q == 1 mod p */
135 if (!BN_mod_exp(t1, dh->params.g, dh->params.q, dh->params.p, ctx))
138 *ret |= DH_NOT_SUITABLE_GENERATOR;
140 r = BN_check_prime(dh->params.q, ctx, NULL);
144 *ret |= DH_CHECK_Q_NOT_PRIME;
145 /* Check p == 1 mod q i.e. q divides p - 1 */
146 if (!BN_div(t1, t2, dh->params.p, dh->params.q, ctx))
149 *ret |= DH_CHECK_INVALID_Q_VALUE;
150 if (dh->params.j != NULL
151 && BN_cmp(dh->params.j, t1))
152 *ret |= DH_CHECK_INVALID_J_VALUE;
155 r = BN_check_prime(dh->params.p, ctx, NULL);
159 *ret |= DH_CHECK_P_NOT_PRIME;
160 else if (dh->params.q == NULL) {
161 if (!BN_rshift1(t1, dh->params.p))
163 r = BN_check_prime(t1, ctx, NULL);
167 *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
176 int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key)
180 if (!DH_check_pub_key(dh, pub_key, &errflags))
183 if ((errflags & DH_CHECK_PUBKEY_TOO_SMALL) != 0)
184 DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_TOO_SMALL);
185 if ((errflags & DH_CHECK_PUBKEY_TOO_LARGE) != 0)
186 DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_TOO_LARGE);
187 if ((errflags & DH_CHECK_PUBKEY_INVALID) != 0)
188 DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_INVALID);
190 return errflags == 0;
193 int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
204 tmp = BN_CTX_get(ctx);
205 if (tmp == NULL || !BN_set_word(tmp, 1))
207 if (BN_cmp(pub_key, tmp) <= 0)
208 *ret |= DH_CHECK_PUBKEY_TOO_SMALL;
209 if (BN_copy(tmp, dh->params.p) == NULL || !BN_sub_word(tmp, 1))
211 if (BN_cmp(pub_key, tmp) >= 0)
212 *ret |= DH_CHECK_PUBKEY_TOO_LARGE;
214 if (dh->params.q != NULL) {
215 /* Check pub_key^q == 1 mod p */
216 if (!BN_mod_exp(tmp, pub_key, dh->params.q, dh->params.p, ctx))
219 *ret |= DH_CHECK_PUBKEY_INVALID;