* https://www.openssl.org/source/license.html
*/
+/*
+ * DH low level APIs are deprecated for public use, but still ok for
+ * internal use.
+ */
+#include "internal/deprecated.h"
+
#include <stdio.h>
#include "internal/cryptlib.h"
#include "dh_local.h"
#include "crypto/bn.h"
#include "crypto/dh.h"
+#include "crypto/security_bits.h"
+
+#ifdef FIPS_MODULE
+# define MIN_STRENGTH 112
+#else
+# define MIN_STRENGTH 80
+#endif
static int generate_key(DH *dh);
static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
static int dh_init(DH *dh);
static int dh_finish(DH *dh);
-int dh_compute_key(OPENSSL_CTX *libctx, unsigned char *key,
- const BIGNUM *pub_key, DH *dh)
+static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
{
BN_CTX *ctx = NULL;
BN_MONT_CTX *mont = NULL;
BIGNUM *tmp;
int ret = -1;
-#ifndef FIPS_MODE
+#ifndef FIPS_MODULE
int check_result;
#endif
return 0;
}
- ctx = BN_CTX_new_ex(libctx);
+ ctx = BN_CTX_new_ex(dh->libctx);
if (ctx == NULL)
goto err;
BN_CTX_start(ctx);
goto err;
}
/* TODO(3.0) : Solve in a PR related to Key validation for DH */
-#ifndef FIPS_MODE
+#ifndef FIPS_MODULE
if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) {
DHerr(0, DH_R_INVALID_PUBKEY);
goto err;
return ret;
}
-static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
+int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
{
- return dh_compute_key(NULL, key, pub_key, dh);
+#ifdef FIPS_MODULE
+ return compute_key(key, pub_key, dh);
+#else
+ return dh->meth->compute_key(key, pub_key, dh);
+#endif
}
-int dh_compute_key_padded(OPENSSL_CTX *libctx, unsigned char *key,
- const BIGNUM *pub_key, DH *dh)
+int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
{
int rv, pad;
-#ifdef FIPS_MODE
- rv = dh_compute_key(libctx, key, pub_key, dh);
+#ifdef FIPS_MODULE
+ rv = compute_key(key, pub_key, dh);
#else
rv = dh->meth->compute_key(key, pub_key, dh);
#endif
return rv + pad;
}
-#ifndef FIPS_MODE
-int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
-{
- return dh->meth->compute_key(key, pub_key, dh);
-}
-
-int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
-{
- return dh_compute_key_padded(NULL, key, pub_key, dh);
-}
-#endif
-
static DH_METHOD dh_ossl = {
"OpenSSL DH Method",
generate_key,
static int dh_init(DH *dh)
{
dh->flags |= DH_FLAG_CACHE_MONT_P;
- ffc_params_init(&dh->params);
+ ossl_ffc_params_init(&dh->params);
dh->dirty_cnt++;
return 1;
}
return 1;
}
-#ifndef FIPS_MODE
+#ifndef FIPS_MODULE
void DH_set_default_method(const DH_METHOD *meth)
{
default_DH_method = meth;
}
+#endif /* FIPS_MODULE */
int DH_generate_key(DH *dh)
{
+#ifdef FIPS_MODULE
+ return generate_key(dh);
+#else
return dh->meth->generate_key(dh);
+#endif
+}
+
+int dh_generate_public_key(BN_CTX *ctx, const DH *dh, const BIGNUM *priv_key,
+ BIGNUM *pub_key)
+{
+ int ret = 0;
+ BIGNUM *prk = BN_new();
+ BN_MONT_CTX *mont = NULL;
+
+ if (prk == NULL)
+ return 0;
+
+ if (dh->flags & DH_FLAG_CACHE_MONT_P) {
+ /*
+ * We take the input DH as const, but we lie, because in some cases we
+ * want to get a hold of its Montgomery context.
+ *
+ * We cast to remove the const qualifier in this case, it should be
+ * fine...
+ */
+ BN_MONT_CTX **pmont = (BN_MONT_CTX **)&dh->method_mont_p;
+
+ mont = BN_MONT_CTX_set_locked(pmont, dh->lock, dh->params.p, ctx);
+ if (mont == NULL)
+ goto err;
+ }
+ BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
+
+ /* pub_key = g^priv_key mod p */
+ if (!dh->meth->bn_mod_exp(dh, pub_key, dh->params.g, prk, dh->params.p,
+ ctx, mont))
+ goto err;
+ ret = 1;
+err:
+ BN_clear_free(prk);
+ return ret;
}
-#endif /* FIPS_MODE */
-static int dh_generate_key(OPENSSL_CTX *libctx, DH *dh)
+static int generate_key(DH *dh)
{
int ok = 0;
int generate_new_key = 0;
-#ifndef FIPS_MODE
+#ifndef FIPS_MODULE
unsigned l;
#endif
BN_CTX *ctx = NULL;
- BN_MONT_CTX *mont = NULL;
BIGNUM *pub_key = NULL, *priv_key = NULL;
if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
return 0;
}
- ctx = BN_CTX_new_ex(libctx);
+ ctx = BN_CTX_new_ex(dh->libctx);
if (ctx == NULL)
goto err;
if (priv_key == NULL)
goto err;
generate_new_key = 1;
- } else
+ } else {
priv_key = dh->priv_key;
+ }
if (dh->pub_key == NULL) {
pub_key = BN_new();
if (pub_key == NULL)
goto err;
- } else
+ } else {
pub_key = dh->pub_key;
-
- if (dh->flags & DH_FLAG_CACHE_MONT_P) {
- mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
- dh->lock, dh->params.p, ctx);
- if (!mont)
- goto err;
}
-
if (generate_new_key) {
/* Is it an approved safe prime ?*/
if (DH_get_nid(dh) != NID_undef) {
- /*
- * The safe prime group code sets N = 2*s
- * (where s = max security strength supported).
- * N = dh->length (N = maximum bit length of private key)
- */
- if (dh->length == 0
- || dh->params.q == NULL
+ int max_strength =
+ ifc_ffc_compute_security_bits(BN_num_bits(dh->params.p));
+
+ if (dh->params.q == NULL
|| dh->length > BN_num_bits(dh->params.q))
goto err;
- if (!ffc_generate_private_key(ctx, &dh->params, dh->length,
- dh->length / 2, priv_key))
+ /* dh->length = maximum bit length of generated private key */
+ if (!ossl_ffc_generate_private_key(ctx, &dh->params, dh->length,
+ max_strength, priv_key))
goto err;
} else {
-#ifdef FIPS_MODE
+#ifdef FIPS_MODULE
if (dh->params.q == NULL)
goto err;
#else
} else
#endif
{
+ /* Do a partial check for invalid p, q, g */
+ if (!ossl_ffc_params_simple_validate(dh->libctx, &dh->params,
+ FFC_PARAM_TYPE_DH))
+ goto err;
/*
* For FFC FIPS 186-4 keygen
* security strength s = 112,
* Max Private key size N = len(q)
*/
- if (!ffc_generate_private_key(ctx, &dh->params,
- BN_num_bits(dh->params.q), 112,
- priv_key))
+ if (!ossl_ffc_generate_private_key(ctx, &dh->params,
+ BN_num_bits(dh->params.q),
+ MIN_STRENGTH,
+ priv_key))
goto err;
}
}
}
- {
- BIGNUM *prk = BN_new();
-
- if (prk == NULL)
- goto err;
- BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
-
- /* pub_key = g^priv_key mod p */
- if (!dh->meth->bn_mod_exp(dh, pub_key, dh->params.g, prk, dh->params.p,
- ctx, mont)) {
- BN_clear_free(prk);
- goto err;
- }
- /* We MUST free prk before any further use of priv_key */
- BN_clear_free(prk);
- }
+ if (!dh_generate_public_key(ctx, dh, priv_key, pub_key))
+ goto err;
dh->pub_key = pub_key;
dh->priv_key = priv_key;
return ok;
}
-static int generate_key(DH *dh)
-{
- return dh_generate_key(NULL, dh);
-}
-
int dh_buf2key(DH *dh, const unsigned char *buf, size_t len)
{
int err_reason = DH_R_BN_ERROR;
return 0;
}
-size_t dh_key2buf(const DH *dh, unsigned char **pbuf_out)
+size_t dh_key2buf(const DH *dh, unsigned char **pbuf_out, size_t size, int alloc)
{
const BIGNUM *pubkey;
- unsigned char *pbuf;
+ unsigned char *pbuf = NULL;
const BIGNUM *p;
int p_size;
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;
+ if (pbuf_out != NULL && (alloc || *pbuf_out != NULL)) {
+ if (!alloc) {
+ if (size >= (size_t)p_size)
+ pbuf = *pbuf_out;
+ } else {
+ pbuf = OPENSSL_malloc(p_size);
+ }
+
+ if (pbuf == 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) {
+ if (alloc)
+ OPENSSL_free(pbuf);
+ DHerr(DH_F_DH_KEY2BUF, DH_R_BN_ERROR);
+ return 0;
+ }
+ *pbuf_out = pbuf;
}
- *pbuf_out = pbuf;
return p_size;
}