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"
|
2016-04-07 00:49:48 +08:00
|
|
|
#include "dh_locl.h"
|
2014-10-31 07:58:19 +08:00
|
|
|
#include "internal/bn_int.h"
|
1998-12-21 18:52:47 +08:00
|
|
|
|
1999-08-24 07:11:32 +08:00
|
|
|
static int generate_key(DH *dh);
|
2000-11-07 22:30:37 +08:00
|
|
|
static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh);
|
|
|
|
static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
|
2015-01-22 11:40:55 +08:00
|
|
|
const BIGNUM *a, const BIGNUM *p,
|
|
|
|
const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
|
1999-08-24 07:11:32 +08:00
|
|
|
static int dh_init(DH *dh);
|
|
|
|
static int dh_finish(DH *dh);
|
|
|
|
|
1999-04-20 05:31:43 +08:00
|
|
|
int DH_generate_key(DH *dh)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
return dh->meth->generate_key(dh);
|
|
|
|
}
|
1999-08-24 07:11:32 +08:00
|
|
|
|
2000-11-07 22:30:37 +08:00
|
|
|
int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
return dh->meth->compute_key(key, pub_key, dh);
|
|
|
|
}
|
1999-08-24 07:11:32 +08:00
|
|
|
|
2011-03-09 03:07:26 +08:00
|
|
|
int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int rv, pad;
|
|
|
|
rv = dh->meth->compute_key(key, pub_key, dh);
|
|
|
|
if (rv <= 0)
|
|
|
|
return rv;
|
|
|
|
pad = BN_num_bytes(dh->p) - rv;
|
|
|
|
if (pad > 0) {
|
|
|
|
memmove(key + pad, key, rv);
|
|
|
|
memset(key, 0, pad);
|
|
|
|
}
|
|
|
|
return rv + pad;
|
|
|
|
}
|
2011-03-09 03:07:26 +08:00
|
|
|
|
1999-08-24 07:11:32 +08:00
|
|
|
static DH_METHOD dh_ossl = {
|
2015-01-22 11:40:55 +08:00
|
|
|
"OpenSSL DH Method",
|
|
|
|
generate_key,
|
|
|
|
compute_key,
|
|
|
|
dh_bn_mod_exp,
|
|
|
|
dh_init,
|
|
|
|
dh_finish,
|
|
|
|
DH_FLAG_FIPS_METHOD,
|
|
|
|
NULL,
|
|
|
|
NULL
|
1999-08-24 07:11:32 +08:00
|
|
|
};
|
|
|
|
|
2017-04-08 00:07:42 +08:00
|
|
|
static const DH_METHOD *default_DH_method = &dh_ossl;
|
|
|
|
|
2000-11-07 22:30:37 +08:00
|
|
|
const DH_METHOD *DH_OpenSSL(void)
|
1999-08-24 07:11:32 +08:00
|
|
|
{
|
2015-01-22 11:40:55 +08:00
|
|
|
return &dh_ossl;
|
1999-08-24 07:11:32 +08:00
|
|
|
}
|
|
|
|
|
2017-04-08 00:07:42 +08:00
|
|
|
void DH_set_default_method(const DH_METHOD *meth)
|
|
|
|
{
|
|
|
|
default_DH_method = meth;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DH_METHOD *DH_get_default_method(void)
|
|
|
|
{
|
|
|
|
return default_DH_method;
|
|
|
|
}
|
|
|
|
|
1999-08-24 07:11:32 +08:00
|
|
|
static int generate_key(DH *dh)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int ok = 0;
|
|
|
|
int generate_new_key = 0;
|
|
|
|
unsigned l;
|
2018-06-12 01:38:54 +08:00
|
|
|
BN_CTX *ctx = NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_MONT_CTX *mont = NULL;
|
|
|
|
BIGNUM *pub_key = NULL, *priv_key = NULL;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2018-06-12 01:38:54 +08:00
|
|
|
if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
|
|
|
|
DHerr(DH_F_GENERATE_KEY, DH_R_MODULUS_TOO_LARGE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-23 04:50:19 +08:00
|
|
|
if (BN_num_bits(dh->p) < DH_MIN_MODULUS_BITS) {
|
|
|
|
DHerr(DH_F_GENERATE_KEY, DH_R_MODULUS_TOO_SMALL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL)
|
|
|
|
goto err;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (dh->priv_key == NULL) {
|
2015-04-25 04:39:40 +08:00
|
|
|
priv_key = BN_secure_new();
|
2015-01-22 11:40:55 +08:00
|
|
|
if (priv_key == NULL)
|
|
|
|
goto err;
|
|
|
|
generate_new_key = 1;
|
|
|
|
} else
|
|
|
|
priv_key = dh->priv_key;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (dh->pub_key == NULL) {
|
|
|
|
pub_key = BN_new();
|
|
|
|
if (pub_key == NULL)
|
|
|
|
goto err;
|
|
|
|
} else
|
|
|
|
pub_key = dh->pub_key;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (dh->flags & DH_FLAG_CACHE_MONT_P) {
|
|
|
|
mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
|
2016-03-04 23:43:46 +08:00
|
|
|
dh->lock, dh->p, ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!mont)
|
|
|
|
goto err;
|
|
|
|
}
|
2005-04-27 07:58:54 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (generate_new_key) {
|
|
|
|
if (dh->q) {
|
|
|
|
do {
|
2017-08-03 02:00:52 +08:00
|
|
|
if (!BN_priv_rand_range(priv_key, dh->q))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
while (BN_is_zero(priv_key) || BN_is_one(priv_key));
|
|
|
|
} else {
|
|
|
|
/* secret exponent length */
|
|
|
|
l = dh->length ? dh->length : BN_num_bits(dh->p) - 1;
|
2017-08-03 02:00:52 +08:00
|
|
|
if (!BN_priv_rand(priv_key, l, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2019-07-10 21:52:36 +08:00
|
|
|
/*
|
|
|
|
* We handle just one known case where g is a quadratic non-residue:
|
|
|
|
* for g = 2: p % 8 == 3
|
|
|
|
*/
|
|
|
|
if (BN_is_word(dh->g, DH_GENERATOR_2) && !BN_is_bit_set(dh->p, 2)) {
|
|
|
|
/* clear bit 0, since it won't be a secret anyway */
|
|
|
|
if (!BN_clear_bit(priv_key, 0))
|
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
}
|
1998-12-21 19:00:56 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2016-05-26 17:55:11 +08:00
|
|
|
BIGNUM *prk = BN_new();
|
2005-05-16 09:43:31 +08:00
|
|
|
|
2016-05-26 17:55:11 +08:00
|
|
|
if (prk == NULL)
|
|
|
|
goto err;
|
|
|
|
BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
|
2005-05-16 09:43:31 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont)) {
|
2019-07-10 21:52:36 +08:00
|
|
|
BN_clear_free(prk);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2016-05-26 17:55:11 +08:00
|
|
|
/* We MUST free prk before any further use of priv_key */
|
2019-07-10 21:52:36 +08:00
|
|
|
BN_clear_free(prk);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2005-05-16 09:43:31 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
dh->pub_key = pub_key;
|
|
|
|
dh->priv_key = priv_key;
|
2019-07-07 16:56:46 +08:00
|
|
|
dh->dirty_cnt++;
|
2015-01-22 11:40:55 +08:00
|
|
|
ok = 1;
|
|
|
|
err:
|
|
|
|
if (ok != 1)
|
|
|
|
DHerr(DH_F_GENERATE_KEY, ERR_R_BN_LIB);
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-05-01 09:37:06 +08:00
|
|
|
if (pub_key != dh->pub_key)
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_free(pub_key);
|
2015-05-01 09:37:06 +08:00
|
|
|
if (priv_key != dh->priv_key)
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_free(priv_key);
|
|
|
|
BN_CTX_free(ctx);
|
2017-10-17 22:04:09 +08:00
|
|
|
return ok;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2000-11-07 22:30:37 +08:00
|
|
|
static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
BN_CTX *ctx = NULL;
|
|
|
|
BN_MONT_CTX *mont = NULL;
|
|
|
|
BIGNUM *tmp;
|
|
|
|
int ret = -1;
|
|
|
|
int check_result;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
|
|
|
|
DHerr(DH_F_COMPUTE_KEY, DH_R_MODULUS_TOO_LARGE);
|
|
|
|
goto err;
|
|
|
|
}
|
2006-09-28 21:45:34 +08:00
|
|
|
|
2019-07-23 04:50:19 +08:00
|
|
|
if (BN_num_bits(dh->p) < DH_MIN_MODULUS_BITS) {
|
|
|
|
DHerr(DH_F_COMPUTE_KEY, DH_R_MODULUS_TOO_SMALL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL)
|
|
|
|
goto err;
|
|
|
|
BN_CTX_start(ctx);
|
|
|
|
tmp = BN_CTX_get(ctx);
|
2016-12-22 20:51:27 +08:00
|
|
|
if (tmp == NULL)
|
|
|
|
goto err;
|
2005-04-27 07:58:54 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (dh->priv_key == NULL) {
|
|
|
|
DHerr(DH_F_COMPUTE_KEY, DH_R_NO_PRIVATE_VALUE);
|
|
|
|
goto err;
|
|
|
|
}
|
1998-12-21 19:00:56 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (dh->flags & DH_FLAG_CACHE_MONT_P) {
|
|
|
|
mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
|
2016-03-04 23:43:46 +08:00
|
|
|
dh->lock, dh->p, ctx);
|
2016-05-26 17:55:11 +08:00
|
|
|
BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!mont)
|
|
|
|
goto err;
|
|
|
|
}
|
2005-08-22 00:00:17 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) {
|
|
|
|
DHerr(DH_F_COMPUTE_KEY, DH_R_INVALID_PUBKEY);
|
|
|
|
goto err;
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!dh->
|
|
|
|
meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->p, ctx, mont)) {
|
|
|
|
DHerr(DH_F_COMPUTE_KEY, ERR_R_BN_LIB);
|
|
|
|
goto err;
|
|
|
|
}
|
1999-08-24 07:11:32 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ret = BN_bn2bin(tmp, key);
|
|
|
|
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 ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2000-06-08 05:29:25 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
|
|
|
|
const BIGNUM *a, const BIGNUM *p,
|
|
|
|
const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
|
|
|
|
{
|
2016-05-26 17:55:11 +08:00
|
|
|
return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1999-08-24 07:11:32 +08:00
|
|
|
|
|
|
|
static int dh_init(DH *dh)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
dh->flags |= DH_FLAG_CACHE_MONT_P;
|
2017-10-09 19:05:58 +08:00
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1999-08-24 07:11:32 +08:00
|
|
|
|
|
|
|
static int dh_finish(DH *dh)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-05-01 09:37:06 +08:00
|
|
|
BN_MONT_CTX_free(dh->method_mont_p);
|
2017-10-09 19:05:58 +08:00
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2019-01-25 23:34:49 +08:00
|
|
|
|
|
|
|
int dh_buf2key(DH *dh, const unsigned char *buf, size_t len)
|
|
|
|
{
|
|
|
|
int err_reason = DH_R_BN_ERROR;
|
|
|
|
BIGNUM *pubkey = NULL;
|
|
|
|
const BIGNUM *p;
|
|
|
|
size_t p_size;
|
|
|
|
|
|
|
|
if ((pubkey = BN_bin2bn(buf, len, NULL)) == NULL)
|
|
|
|
goto err;
|
|
|
|
DH_get0_pqg(dh, &p, NULL, NULL);
|
|
|
|
if (p == NULL || (p_size = BN_num_bytes(p)) == 0) {
|
|
|
|
err_reason = DH_R_NO_PARAMETERS_SET;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* As per Section 4.2.8.1 of RFC 8446 fail if DHE's
|
|
|
|
* public key is of size not equal to size of p
|
|
|
|
*/
|
|
|
|
if (BN_is_zero(pubkey) || p_size != len) {
|
|
|
|
err_reason = DH_R_INVALID_PUBKEY;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (DH_set0_key(dh, pubkey, NULL) != 1)
|
|
|
|
goto err;
|
|
|
|
return 1;
|
|
|
|
err:
|
|
|
|
DHerr(DH_F_DH_BUF2KEY, err_reason);
|
|
|
|
BN_free(pubkey);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t dh_key2buf(const DH *dh, unsigned char **pbuf_out)
|
|
|
|
{
|
|
|
|
const BIGNUM *pubkey;
|
|
|
|
unsigned char *pbuf;
|
|
|
|
const BIGNUM *p;
|
|
|
|
int p_size;
|
|
|
|
|
|
|
|
DH_get0_pqg(dh, &p, NULL, NULL);
|
|
|
|
DH_get0_key(dh, &pubkey, NULL);
|
|
|
|
if (p == NULL || pubkey == NULL
|
|
|
|
|| (p_size = BN_num_bytes(p)) == 0
|
|
|
|
|| BN_num_bytes(pubkey) == 0) {
|
|
|
|
DHerr(DH_F_DH_KEY2BUF, DH_R_INVALID_PUBKEY);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((pbuf = OPENSSL_malloc(p_size)) == NULL) {
|
|
|
|
DHerr(DH_F_DH_KEY2BUF, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* As per Section 4.2.8.1 of RFC 8446 left pad public
|
|
|
|
* key with zeros to the size of p
|
|
|
|
*/
|
|
|
|
if (BN_bn2binpad(pubkey, pbuf, p_size) < 0) {
|
|
|
|
OPENSSL_free(pbuf);
|
|
|
|
DHerr(DH_F_DH_KEY2BUF, DH_R_BN_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*pbuf_out = pbuf;
|
|
|
|
return p_size;
|
|
|
|
}
|