2016-05-17 19:38:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2016-05-17 19:38:09 +00: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 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2016-04-06 16:49:48 +00:00
|
|
|
#include "dh_locl.h"
|
2014-10-30 23:58:19 +00:00
|
|
|
#include "internal/bn_int.h"
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-08-23 23:11:32 +00:00
|
|
|
static int generate_key(DH *dh);
|
2000-11-07 14:30:37 +00: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 03:40:55 +00:00
|
|
|
const BIGNUM *a, const BIGNUM *p,
|
|
|
|
const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
|
1999-08-23 23:11:32 +00:00
|
|
|
static int dh_init(DH *dh);
|
|
|
|
static int dh_finish(DH *dh);
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
int DH_generate_key(DH *dh)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return dh->meth->generate_key(dh);
|
|
|
|
}
|
1999-08-23 23:11:32 +00:00
|
|
|
|
2000-11-07 14:30:37 +00:00
|
|
|
int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return dh->meth->compute_key(key, pub_key, dh);
|
|
|
|
}
|
1999-08-23 23:11:32 +00:00
|
|
|
|
2011-03-08 19:07:26 +00:00
|
|
|
int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
|
2015-01-22 03:40:55 +00: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-08 19:07:26 +00:00
|
|
|
|
1999-08-23 23:11:32 +00:00
|
|
|
static DH_METHOD dh_ossl = {
|
2015-01-22 03:40:55 +00:00
|
|
|
"OpenSSL DH Method",
|
|
|
|
generate_key,
|
|
|
|
compute_key,
|
|
|
|
dh_bn_mod_exp,
|
|
|
|
dh_init,
|
|
|
|
dh_finish,
|
|
|
|
DH_FLAG_FIPS_METHOD,
|
|
|
|
NULL,
|
|
|
|
NULL
|
1999-08-23 23:11:32 +00:00
|
|
|
};
|
|
|
|
|
2017-04-07 16:07:42 +00:00
|
|
|
static const DH_METHOD *default_DH_method = &dh_ossl;
|
|
|
|
|
2000-11-07 14:30:37 +00:00
|
|
|
const DH_METHOD *DH_OpenSSL(void)
|
1999-08-23 23:11:32 +00:00
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
return &dh_ossl;
|
1999-08-23 23:11:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 16:07:42 +00: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-23 23:11:32 +00:00
|
|
|
static int generate_key(DH *dh)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int ok = 0;
|
|
|
|
int generate_new_key = 0;
|
|
|
|
unsigned l;
|
|
|
|
BN_CTX *ctx;
|
|
|
|
BN_MONT_CTX *mont = NULL;
|
|
|
|
BIGNUM *pub_key = NULL, *priv_key = NULL;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL)
|
|
|
|
goto err;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (dh->priv_key == NULL) {
|
2015-04-24 20:39:40 +00:00
|
|
|
priv_key = BN_secure_new();
|
2015-01-22 03:40:55 +00:00
|
|
|
if (priv_key == NULL)
|
|
|
|
goto err;
|
|
|
|
generate_new_key = 1;
|
|
|
|
} else
|
|
|
|
priv_key = dh->priv_key;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00: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 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (dh->flags & DH_FLAG_CACHE_MONT_P) {
|
|
|
|
mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
|
2016-03-04 15:43:46 +00:00
|
|
|
dh->lock, dh->p, ctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!mont)
|
|
|
|
goto err;
|
|
|
|
}
|
2005-04-26 23:58:54 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (generate_new_key) {
|
|
|
|
if (dh->q) {
|
|
|
|
do {
|
2017-08-02 18:00:52 +00:00
|
|
|
if (!BN_priv_rand_range(priv_key, dh->q))
|
2015-01-22 03:40:55 +00: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-02 18:00:52 +00:00
|
|
|
if (!BN_priv_rand(priv_key, l, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
1998-12-21 11:00:56 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2016-05-26 09:55:11 +00:00
|
|
|
BIGNUM *prk = BN_new();
|
2005-05-16 01:43:31 +00:00
|
|
|
|
2016-05-26 09:55:11 +00:00
|
|
|
if (prk == NULL)
|
|
|
|
goto err;
|
|
|
|
BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
|
2005-05-16 01:43:31 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont)) {
|
2016-05-26 09:55:11 +00:00
|
|
|
BN_free(prk);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2016-05-26 09:55:11 +00:00
|
|
|
/* We MUST free prk before any further use of priv_key */
|
|
|
|
BN_free(prk);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2005-05-16 01:43:31 +00:00
|
|
|
|
2015-01-22 03:40:55 +00: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 10:52:47 +00:00
|
|
|
|
2015-05-01 01:37:06 +00:00
|
|
|
if (pub_key != dh->pub_key)
|
2015-01-22 03:40:55 +00:00
|
|
|
BN_free(pub_key);
|
2015-05-01 01:37:06 +00:00
|
|
|
if (priv_key != dh->priv_key)
|
2015-01-22 03:40:55 +00:00
|
|
|
BN_free(priv_key);
|
|
|
|
BN_CTX_free(ctx);
|
2017-10-17 14:04:09 +00:00
|
|
|
return ok;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2000-11-07 14:30:37 +00:00
|
|
|
static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
BN_CTX *ctx = NULL;
|
|
|
|
BN_MONT_CTX *mont = NULL;
|
|
|
|
BIGNUM *tmp;
|
|
|
|
int ret = -1;
|
|
|
|
int check_result;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00: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 13:45:34 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL)
|
|
|
|
goto err;
|
|
|
|
BN_CTX_start(ctx);
|
|
|
|
tmp = BN_CTX_get(ctx);
|
2016-12-22 12:51:27 +00:00
|
|
|
if (tmp == NULL)
|
|
|
|
goto err;
|
2005-04-26 23:58:54 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (dh->priv_key == NULL) {
|
|
|
|
DHerr(DH_F_COMPUTE_KEY, DH_R_NO_PRIVATE_VALUE);
|
|
|
|
goto err;
|
|
|
|
}
|
1998-12-21 11:00:56 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (dh->flags & DH_FLAG_CACHE_MONT_P) {
|
|
|
|
mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
|
2016-03-04 15:43:46 +00:00
|
|
|
dh->lock, dh->p, ctx);
|
2016-05-26 09:55:11 +00:00
|
|
|
BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!mont)
|
|
|
|
goto err;
|
|
|
|
}
|
2005-08-21 16:00:17 +00:00
|
|
|
|
2015-01-22 03:40:55 +00: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 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00: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-23 23:11:32 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
ret = BN_bn2bin(tmp, key);
|
|
|
|
err:
|
|
|
|
if (ctx != NULL) {
|
|
|
|
BN_CTX_end(ctx);
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
}
|
2017-10-17 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2000-06-07 21:29:25 +00:00
|
|
|
|
2015-01-22 03:40:55 +00: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 09:55:11 +00:00
|
|
|
return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1999-08-23 23:11:32 +00:00
|
|
|
|
|
|
|
static int dh_init(DH *dh)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
dh->flags |= DH_FLAG_CACHE_MONT_P;
|
2017-10-09 11:05:58 +00:00
|
|
|
return 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1999-08-23 23:11:32 +00:00
|
|
|
|
|
|
|
static int dh_finish(DH *dh)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2015-05-01 01:37:06 +00:00
|
|
|
BN_MONT_CTX_free(dh->method_mont_p);
|
2017-10-09 11:05:58 +00:00
|
|
|
return 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|