2016-05-18 03:38:09 +08:00
|
|
|
/*
|
2018-06-20 22:25:43 +08:00
|
|
|
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2016-05-18 03:38:09 +08:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
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)) {
|
2016-05-26 17:55:11 +08:00
|
|
|
BN_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 */
|
|
|
|
BN_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;
|
|
|
|
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
|
|
|
|
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:
|
|
|
|
if (ctx != NULL) {
|
|
|
|
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
|
|
|
}
|