2016-05-18 03:38:09 +08:00
|
|
|
/*
|
2019-07-10 21:52:36 +08:00
|
|
|
* Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2018-12-06 20:36:05 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 03:38:09 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/bn.h>
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "dh_local.h"
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2016-12-31 04:57:28 +08:00
|
|
|
/*-
|
|
|
|
* Check that p and g are suitable enough
|
|
|
|
*
|
|
|
|
* p is odd
|
|
|
|
* 1 < g < p - 1
|
|
|
|
*/
|
2017-11-01 00:45:24 +08:00
|
|
|
int DH_check_params_ex(const DH *dh)
|
|
|
|
{
|
|
|
|
int errflags = 0;
|
|
|
|
|
2019-07-10 21:52:36 +08:00
|
|
|
if (!DH_check_params(dh, &errflags))
|
|
|
|
return 0;
|
2017-11-01 00:45:24 +08:00
|
|
|
|
|
|
|
if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
|
|
|
|
DHerr(DH_F_DH_CHECK_PARAMS_EX, DH_R_CHECK_P_NOT_PRIME);
|
|
|
|
if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
|
|
|
|
DHerr(DH_F_DH_CHECK_PARAMS_EX, DH_R_NOT_SUITABLE_GENERATOR);
|
2019-09-07 05:38:49 +08:00
|
|
|
if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
|
|
|
|
DHerr(DH_F_DH_CHECK_PARAMS_EX, DH_R_MODULUS_TOO_SMALL);
|
|
|
|
if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
|
|
|
|
DHerr(DH_F_DH_CHECK_PARAMS_EX, DH_R_MODULUS_TOO_LARGE);
|
2017-11-01 00:45:24 +08:00
|
|
|
|
|
|
|
return errflags == 0;
|
|
|
|
}
|
2016-12-31 04:57:28 +08:00
|
|
|
|
|
|
|
int DH_check_params(const DH *dh, int *ret)
|
|
|
|
{
|
|
|
|
int ok = 0;
|
|
|
|
BIGNUM *tmp = NULL;
|
|
|
|
BN_CTX *ctx = NULL;
|
|
|
|
|
|
|
|
*ret = 0;
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL)
|
|
|
|
goto err;
|
|
|
|
BN_CTX_start(ctx);
|
|
|
|
tmp = BN_CTX_get(ctx);
|
|
|
|
if (tmp == NULL)
|
|
|
|
goto err;
|
|
|
|
|
2020-01-24 12:09:33 +08:00
|
|
|
if (!BN_is_odd(dh->params.p))
|
2016-12-31 04:57:28 +08:00
|
|
|
*ret |= DH_CHECK_P_NOT_PRIME;
|
2020-01-24 12:09:33 +08:00
|
|
|
if (BN_is_negative(dh->params.g)
|
|
|
|
|| BN_is_zero(dh->params.g)
|
|
|
|
|| BN_is_one(dh->params.g))
|
2016-12-31 04:57:28 +08:00
|
|
|
*ret |= DH_NOT_SUITABLE_GENERATOR;
|
2020-01-24 12:09:33 +08:00
|
|
|
if (BN_copy(tmp, dh->params.p) == NULL || !BN_sub_word(tmp, 1))
|
2016-12-31 04:57:28 +08:00
|
|
|
goto err;
|
2020-01-24 12:09:33 +08:00
|
|
|
if (BN_cmp(dh->params.g, tmp) >= 0)
|
2016-12-31 04:57:28 +08:00
|
|
|
*ret |= DH_NOT_SUITABLE_GENERATOR;
|
2020-01-24 12:09:33 +08:00
|
|
|
if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS)
|
2019-09-07 05:38:49 +08:00
|
|
|
*ret |= DH_MODULUS_TOO_SMALL;
|
2020-01-24 12:09:33 +08:00
|
|
|
if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS)
|
2019-09-07 05:38:49 +08:00
|
|
|
*ret |= DH_MODULUS_TOO_LARGE;
|
2016-12-31 04:57:28 +08:00
|
|
|
|
|
|
|
ok = 1;
|
|
|
|
err:
|
2019-03-19 07:58:09 +08:00
|
|
|
BN_CTX_end(ctx);
|
|
|
|
BN_CTX_free(ctx);
|
2017-10-17 22:04:09 +08:00
|
|
|
return ok;
|
2016-12-31 04:57:28 +08:00
|
|
|
}
|
|
|
|
|
2014-12-28 10:48:40 +08:00
|
|
|
/*-
|
|
|
|
* Check that p is a safe prime and
|
2019-07-10 21:52:36 +08:00
|
|
|
* g is a suitable generator.
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
2017-11-01 00:45:24 +08:00
|
|
|
int DH_check_ex(const DH *dh)
|
|
|
|
{
|
|
|
|
int errflags = 0;
|
|
|
|
|
2019-07-10 21:52:36 +08:00
|
|
|
if (!DH_check(dh, &errflags))
|
|
|
|
return 0;
|
2017-11-01 00:45:24 +08:00
|
|
|
|
|
|
|
if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
|
|
|
|
DHerr(DH_F_DH_CHECK_EX, DH_R_NOT_SUITABLE_GENERATOR);
|
|
|
|
if ((errflags & DH_CHECK_Q_NOT_PRIME) != 0)
|
|
|
|
DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_Q_NOT_PRIME);
|
|
|
|
if ((errflags & DH_CHECK_INVALID_Q_VALUE) != 0)
|
|
|
|
DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_INVALID_Q_VALUE);
|
|
|
|
if ((errflags & DH_CHECK_INVALID_J_VALUE) != 0)
|
|
|
|
DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_INVALID_J_VALUE);
|
|
|
|
if ((errflags & DH_UNABLE_TO_CHECK_GENERATOR) != 0)
|
|
|
|
DHerr(DH_F_DH_CHECK_EX, DH_R_UNABLE_TO_CHECK_GENERATOR);
|
|
|
|
if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
|
|
|
|
DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_P_NOT_PRIME);
|
|
|
|
if ((errflags & DH_CHECK_P_NOT_SAFE_PRIME) != 0)
|
|
|
|
DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_P_NOT_SAFE_PRIME);
|
2019-09-07 05:38:49 +08:00
|
|
|
if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
|
|
|
|
DHerr(DH_F_DH_CHECK_EX, DH_R_MODULUS_TOO_SMALL);
|
|
|
|
if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
|
|
|
|
DHerr(DH_F_DH_CHECK_EX, DH_R_MODULUS_TOO_LARGE);
|
2017-11-01 00:45:24 +08:00
|
|
|
|
|
|
|
return errflags == 0;
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2000-11-07 22:30:37 +08:00
|
|
|
int DH_check(const DH *dh, int *ret)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2016-06-24 23:05:48 +08:00
|
|
|
int ok = 0, r;
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_CTX *ctx = NULL;
|
|
|
|
BIGNUM *t1 = NULL, *t2 = NULL;
|
|
|
|
|
2019-07-10 21:52:36 +08:00
|
|
|
if (!DH_check_params(dh, ret))
|
|
|
|
return 0;
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL)
|
|
|
|
goto err;
|
|
|
|
BN_CTX_start(ctx);
|
|
|
|
t1 = BN_CTX_get(ctx);
|
|
|
|
t2 = BN_CTX_get(ctx);
|
|
|
|
if (t2 == NULL)
|
|
|
|
goto err;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2020-01-24 12:09:33 +08:00
|
|
|
if (dh->params.q != NULL) {
|
|
|
|
if (BN_cmp(dh->params.g, BN_value_one()) <= 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
*ret |= DH_NOT_SUITABLE_GENERATOR;
|
2020-01-24 12:09:33 +08:00
|
|
|
else if (BN_cmp(dh->params.g, dh->params.p) >= 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
*ret |= DH_NOT_SUITABLE_GENERATOR;
|
|
|
|
else {
|
|
|
|
/* Check g^q == 1 mod p */
|
2020-01-24 12:09:33 +08:00
|
|
|
if (!BN_mod_exp(t1, dh->params.g, dh->params.q, dh->params.p, ctx))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
if (!BN_is_one(t1))
|
|
|
|
*ret |= DH_NOT_SUITABLE_GENERATOR;
|
|
|
|
}
|
2020-01-24 12:09:33 +08:00
|
|
|
r = BN_check_prime(dh->params.q, ctx, NULL);
|
2016-06-24 23:05:48 +08:00
|
|
|
if (r < 0)
|
|
|
|
goto err;
|
|
|
|
if (!r)
|
2015-01-22 11:40:55 +08:00
|
|
|
*ret |= DH_CHECK_Q_NOT_PRIME;
|
|
|
|
/* Check p == 1 mod q i.e. q divides p - 1 */
|
2020-01-24 12:09:33 +08:00
|
|
|
if (!BN_div(t1, t2, dh->params.p, dh->params.q, ctx))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
if (!BN_is_one(t2))
|
|
|
|
*ret |= DH_CHECK_INVALID_Q_VALUE;
|
2020-01-24 12:09:33 +08:00
|
|
|
if (dh->params.j != NULL
|
|
|
|
&& BN_cmp(dh->params.j, t1))
|
2015-01-22 11:40:55 +08:00
|
|
|
*ret |= DH_CHECK_INVALID_J_VALUE;
|
2019-07-10 21:52:36 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2020-01-24 12:09:33 +08:00
|
|
|
r = BN_check_prime(dh->params.p, ctx, NULL);
|
2016-06-24 23:05:48 +08:00
|
|
|
if (r < 0)
|
|
|
|
goto err;
|
|
|
|
if (!r)
|
2015-01-22 11:40:55 +08:00
|
|
|
*ret |= DH_CHECK_P_NOT_PRIME;
|
2020-01-24 12:09:33 +08:00
|
|
|
else if (dh->params.q == NULL) {
|
|
|
|
if (!BN_rshift1(t1, dh->params.p))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2019-10-06 23:21:16 +08:00
|
|
|
r = BN_check_prime(t1, ctx, NULL);
|
2016-06-24 23:05:48 +08:00
|
|
|
if (r < 0)
|
|
|
|
goto err;
|
2016-12-10 04:17:05 +08:00
|
|
|
if (!r)
|
2015-01-22 11:40:55 +08:00
|
|
|
*ret |= DH_CHECK_P_NOT_SAFE_PRIME;
|
|
|
|
}
|
|
|
|
ok = 1;
|
|
|
|
err:
|
2019-03-19 07:58:09 +08:00
|
|
|
BN_CTX_end(ctx);
|
|
|
|
BN_CTX_free(ctx);
|
2017-10-17 22:04:09 +08:00
|
|
|
return ok;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2005-08-22 00:00:17 +08:00
|
|
|
|
2017-11-01 00:45:24 +08:00
|
|
|
int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key)
|
|
|
|
{
|
|
|
|
int errflags = 0;
|
|
|
|
|
2019-09-07 06:58:31 +08:00
|
|
|
if (!DH_check_pub_key(dh, pub_key, &errflags))
|
|
|
|
return 0;
|
2017-11-01 00:45:24 +08:00
|
|
|
|
|
|
|
if ((errflags & DH_CHECK_PUBKEY_TOO_SMALL) != 0)
|
|
|
|
DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_TOO_SMALL);
|
|
|
|
if ((errflags & DH_CHECK_PUBKEY_TOO_LARGE) != 0)
|
|
|
|
DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_TOO_LARGE);
|
|
|
|
if ((errflags & DH_CHECK_PUBKEY_INVALID) != 0)
|
|
|
|
DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_INVALID);
|
|
|
|
|
|
|
|
return errflags == 0;
|
|
|
|
}
|
|
|
|
|
2005-08-22 00:00:17 +08:00
|
|
|
int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int ok = 0;
|
2016-01-18 19:31:58 +08:00
|
|
|
BIGNUM *tmp = NULL;
|
|
|
|
BN_CTX *ctx = NULL;
|
2005-08-22 00:00:17 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
*ret = 0;
|
2016-01-18 19:31:58 +08:00
|
|
|
ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2016-01-18 19:31:58 +08:00
|
|
|
BN_CTX_start(ctx);
|
|
|
|
tmp = BN_CTX_get(ctx);
|
2016-01-29 17:40:03 +08:00
|
|
|
if (tmp == NULL || !BN_set_word(tmp, 1))
|
2016-01-18 19:31:58 +08:00
|
|
|
goto err;
|
|
|
|
if (BN_cmp(pub_key, tmp) <= 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
*ret |= DH_CHECK_PUBKEY_TOO_SMALL;
|
2020-01-24 12:09:33 +08:00
|
|
|
if (BN_copy(tmp, dh->params.p) == NULL || !BN_sub_word(tmp, 1))
|
2016-01-29 17:40:03 +08:00
|
|
|
goto err;
|
2016-01-18 19:31:58 +08:00
|
|
|
if (BN_cmp(pub_key, tmp) >= 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
*ret |= DH_CHECK_PUBKEY_TOO_LARGE;
|
2005-08-22 00:00:17 +08:00
|
|
|
|
2020-01-24 12:09:33 +08:00
|
|
|
if (dh->params.q != NULL) {
|
2016-01-18 19:31:58 +08:00
|
|
|
/* Check pub_key^q == 1 mod p */
|
2020-01-24 12:09:33 +08:00
|
|
|
if (!BN_mod_exp(tmp, pub_key, dh->params.q, dh->params.p, ctx))
|
2016-01-18 19:31:58 +08:00
|
|
|
goto err;
|
|
|
|
if (!BN_is_one(tmp))
|
|
|
|
*ret |= DH_CHECK_PUBKEY_INVALID;
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ok = 1;
|
|
|
|
err:
|
2019-03-19 07:58:09 +08:00
|
|
|
BN_CTX_end(ctx);
|
|
|
|
BN_CTX_free(ctx);
|
2017-10-17 22:04:09 +08:00
|
|
|
return ok;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|