2 * Copyright 1995-2021 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 * DH low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
18 #include <openssl/bn.h>
20 #include "crypto/dh.h"
23 * Check that p and g are suitable enough
28 int DH_check_params_ex(const DH *dh)
32 if (!DH_check_params(dh, &errflags))
35 if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
36 ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_PRIME);
37 if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
38 ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
39 if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
40 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
41 if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
42 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
48 int DH_check_params(const DH *dh, int *ret)
54 * SP800-56A R3 Section 5.5.2 Assurances of Domain Parameter Validity
55 * (1a) The domain parameters correspond to any approved safe prime group.
57 nid = DH_get_nid((DH *)dh);
62 * (2b) FFC domain params conform to FIPS-186-4 explicit domain param
65 return ossl_ffc_params_FIPS186_4_validate(dh->libctx, &dh->params,
66 FFC_PARAM_TYPE_DH, ret, NULL);
69 int DH_check_params(const DH *dh, int *ret)
76 ctx = BN_CTX_new_ex(dh->libctx);
80 tmp = BN_CTX_get(ctx);
84 if (!BN_is_odd(dh->params.p))
85 *ret |= DH_CHECK_P_NOT_PRIME;
86 if (BN_is_negative(dh->params.g)
87 || BN_is_zero(dh->params.g)
88 || BN_is_one(dh->params.g))
89 *ret |= DH_NOT_SUITABLE_GENERATOR;
90 if (BN_copy(tmp, dh->params.p) == NULL || !BN_sub_word(tmp, 1))
92 if (BN_cmp(dh->params.g, tmp) >= 0)
93 *ret |= DH_NOT_SUITABLE_GENERATOR;
94 if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS)
95 *ret |= DH_MODULUS_TOO_SMALL;
96 if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS)
97 *ret |= DH_MODULUS_TOO_LARGE;
105 #endif /* FIPS_MODULE */
108 * Check that p is a safe prime and
109 * g is a suitable generator.
111 int DH_check_ex(const DH *dh)
115 if (!DH_check(dh, &errflags))
118 if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
119 ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
120 if ((errflags & DH_CHECK_Q_NOT_PRIME) != 0)
121 ERR_raise(ERR_LIB_DH, DH_R_CHECK_Q_NOT_PRIME);
122 if ((errflags & DH_CHECK_INVALID_Q_VALUE) != 0)
123 ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_Q_VALUE);
124 if ((errflags & DH_CHECK_INVALID_J_VALUE) != 0)
125 ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_J_VALUE);
126 if ((errflags & DH_UNABLE_TO_CHECK_GENERATOR) != 0)
127 ERR_raise(ERR_LIB_DH, DH_R_UNABLE_TO_CHECK_GENERATOR);
128 if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
129 ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_PRIME);
130 if ((errflags & DH_CHECK_P_NOT_SAFE_PRIME) != 0)
131 ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_SAFE_PRIME);
132 if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
133 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
134 if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
135 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
137 return errflags == 0;
140 /* Note: according to documentation - this only checks the params */
141 int DH_check(const DH *dh, int *ret)
144 return DH_check_params(dh, ret);
148 BIGNUM *t1 = NULL, *t2 = NULL;
149 int nid = DH_get_nid((DH *)dh);
152 if (nid != NID_undef)
155 /* Don't do any checks at all with an excessively large modulus */
156 if (BN_num_bits(dh->params.p) > OPENSSL_DH_CHECK_MAX_MODULUS_BITS) {
157 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
158 *ret = DH_MODULUS_TOO_LARGE | DH_CHECK_P_NOT_PRIME;
162 if (!DH_check_params(dh, ret))
165 ctx = BN_CTX_new_ex(dh->libctx);
169 t1 = BN_CTX_get(ctx);
170 t2 = BN_CTX_get(ctx);
174 if (dh->params.q != NULL) {
175 if (BN_cmp(dh->params.g, BN_value_one()) <= 0)
176 *ret |= DH_NOT_SUITABLE_GENERATOR;
177 else if (BN_cmp(dh->params.g, dh->params.p) >= 0)
178 *ret |= DH_NOT_SUITABLE_GENERATOR;
180 /* Check g^q == 1 mod p */
181 if (!BN_mod_exp(t1, dh->params.g, dh->params.q, dh->params.p, ctx))
184 *ret |= DH_NOT_SUITABLE_GENERATOR;
186 r = BN_check_prime(dh->params.q, ctx, NULL);
190 *ret |= DH_CHECK_Q_NOT_PRIME;
191 /* Check p == 1 mod q i.e. q divides p - 1 */
192 if (!BN_div(t1, t2, dh->params.p, dh->params.q, ctx))
195 *ret |= DH_CHECK_INVALID_Q_VALUE;
196 if (dh->params.j != NULL
197 && BN_cmp(dh->params.j, t1))
198 *ret |= DH_CHECK_INVALID_J_VALUE;
201 r = BN_check_prime(dh->params.p, ctx, NULL);
205 *ret |= DH_CHECK_P_NOT_PRIME;
206 else if (dh->params.q == NULL) {
207 if (!BN_rshift1(t1, dh->params.p))
209 r = BN_check_prime(t1, ctx, NULL);
213 *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
220 #endif /* FIPS_MODULE */
223 int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key)
227 if (!DH_check_pub_key(dh, pub_key, &errflags))
230 if ((errflags & DH_CHECK_PUBKEY_TOO_SMALL) != 0)
231 ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_SMALL);
232 if ((errflags & DH_CHECK_PUBKEY_TOO_LARGE) != 0)
233 ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_LARGE);
234 if ((errflags & DH_CHECK_PUBKEY_INVALID) != 0)
235 ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_INVALID);
237 return errflags == 0;
241 * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Full public key validation.
243 int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
245 return ossl_ffc_validate_public_key(&dh->params, pub_key, ret);
249 * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Partial public key validation.
250 * To only be used with ephemeral FFC public keys generated using the approved
253 int ossl_dh_check_pub_key_partial(const DH *dh, const BIGNUM *pub_key, int *ret)
255 return ossl_ffc_validate_public_key_partial(&dh->params, pub_key, ret);
258 int ossl_dh_check_priv_key(const DH *dh, const BIGNUM *priv_key, int *ret)
261 BIGNUM *two_powN = NULL, *upper;
265 if (two_powN == NULL)
268 if (dh->params.q != NULL) {
269 upper = dh->params.q;
271 } else if (dh->params.p != NULL) {
273 * We do not have q so we just check the key is within some
274 * reasonable range, or the number of bits is equal to dh->length.
276 int length = dh->length;
279 length = BN_num_bits(dh->params.p) - 1;
280 if (BN_num_bits(priv_key) <= length
281 && BN_num_bits(priv_key) > 1)
283 } else if (BN_num_bits(priv_key) == length) {
292 /* Is it from an approved Safe prime group ?*/
293 if (DH_get_nid((DH *)dh) != NID_undef && dh->length != 0) {
294 if (!BN_lshift(two_powN, BN_value_one(), dh->length))
296 if (BN_cmp(two_powN, dh->params.q) < 0)
299 if (!ossl_ffc_validate_private_key(upper, priv_key, ret))
309 * FFC pairwise check from SP800-56A R3.
310 * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
312 int ossl_dh_check_pairwise(const DH *dh)
316 BIGNUM *pub_key = NULL;
318 if (dh->params.p == NULL
319 || dh->params.g == NULL
320 || dh->priv_key == NULL
321 || dh->pub_key == NULL)
324 ctx = BN_CTX_new_ex(dh->libctx);
331 /* recalculate the public key = (g ^ priv) mod p */
332 if (!ossl_dh_generate_public_key(ctx, dh, dh->priv_key, pub_key))
334 /* check it matches the existing pubic_key */
335 ret = BN_cmp(pub_key, dh->pub_key) == 0;