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>
15 # define DH_NUMBER_ITERATIONS_FOR_PRIME 64
18 * Check that p and g are suitable enough
23 int DH_check_params_ex(const DH *dh)
27 if (!DH_check_params(dh, &errflags))
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);
38 int DH_check_params(const DH *dh, int *ret)
49 tmp = BN_CTX_get(ctx);
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))
59 if (BN_cmp(dh->g, tmp) >= 0)
60 *ret |= DH_NOT_SUITABLE_GENERATOR;
70 * Check that p is a safe prime and
71 * g is a suitable generator.
73 int DH_check_ex(const DH *dh)
77 if (!DH_check(dh, &errflags))
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);
98 int DH_check(const DH *dh, int *ret)
102 BIGNUM *t1 = NULL, *t2 = NULL;
104 if (!DH_check_params(dh, ret))
111 t1 = BN_CTX_get(ctx);
112 t2 = BN_CTX_get(ctx);
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;
122 /* Check g^q == 1 mod p */
123 if (!BN_mod_exp(t1, dh->g, dh->q, dh->p, ctx))
126 *ret |= DH_NOT_SUITABLE_GENERATOR;
128 r = BN_is_prime_ex(dh->q, DH_NUMBER_ITERATIONS_FOR_PRIME, ctx, NULL);
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))
137 *ret |= DH_CHECK_INVALID_Q_VALUE;
138 if (dh->j && BN_cmp(dh->j, t1))
139 *ret |= DH_CHECK_INVALID_J_VALUE;
142 r = BN_is_prime_ex(dh->p, DH_NUMBER_ITERATIONS_FOR_PRIME, ctx, NULL);
146 *ret |= DH_CHECK_P_NOT_PRIME;
148 if (!BN_rshift1(t1, dh->p))
150 r = BN_is_prime_ex(t1, DH_NUMBER_ITERATIONS_FOR_PRIME, ctx, NULL);
154 *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
163 int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key)
167 (void)DH_check(dh, &errflags);
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);
176 return errflags == 0;
179 int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
190 tmp = BN_CTX_get(ctx);
191 if (tmp == NULL || !BN_set_word(tmp, 1))
193 if (BN_cmp(pub_key, tmp) <= 0)
194 *ret |= DH_CHECK_PUBKEY_TOO_SMALL;
195 if (BN_copy(tmp, dh->p) == NULL || !BN_sub_word(tmp, 1))
197 if (BN_cmp(pub_key, tmp) >= 0)
198 *ret |= DH_CHECK_PUBKEY_TOO_LARGE;
201 /* Check pub_key^q == 1 mod p */
202 if (!BN_mod_exp(tmp, pub_key, dh->q, dh->p, ctx))
205 *ret |= DH_CHECK_PUBKEY_INVALID;