Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
/test/.deps
/test/.libs
/test/.dirstamp
/test/dh_alloc.test
/test/dh_alloc.log
/test/dh_alloc.trs
/test/unit.test
/test/unit.log
/test/unit.trs
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ having to touch the OpenSSL code itself.
* DRBG
* RSA
* DH
* With wolfCrypt FIPS, use named or imported parameters for key generation
and agreement. Legacy `DH_generate_parameters_ex()` returns 0.
* ECC
* ECDSA
* ECDH
Expand Down
20 changes: 20 additions & 0 deletions openssl_patches/1.0.2h/tests/fips/dhtest_102h.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
diff --git a/crypto/dh/dhtest.c b/crypto/dh/dhtest.c
--- a/crypto/dh/dhtest.c
+++ b/crypto/dh/dhtest.c
@@ -140,5 +140,13 @@
BN_GENCB_set(&_cb, &cb, out);
- if (((a = DH_new()) == NULL) || !DH_generate_parameters_ex(a, 1024,
- DH_GENERATOR_5,
- &_cb))
+ if ((a = DH_new()) == NULL)
+ goto err;
+ if (a->engine != e ||
+ DH_generate_parameters_ex(a, 1024, DH_GENERATOR_5, &_cb) != 0 ||
+ a->p != NULL || a->q != NULL || a->g != NULL || a->engine != e)
+ goto err;
+ BIO_puts(out, "Legacy DH parameter generation rejected in FIPS mode\n");
+ ERR_clear_error();
+ DH_free(a);
+ a = DH_get_2048_256();
+ if (a == NULL || a->engine != e)
goto err;
32 changes: 32 additions & 0 deletions openssl_patches/1.1.1b/tests/fips/dhtest_111b.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
diff --git a/test/dhtest.c b/test/dhtest.c
--- a/test/dhtest.c
+++ b/test/dhtest.c
@@ -25,4 +25,6 @@
static int cb(int p, int n, BN_GENCB *arg);
-
-
+
+extern ENGINE *e;
+
+
static const unsigned char dhtest_1024_p[] =
@@ -193,5 +193,17 @@
BN_GENCB_set(_cb, &cb, NULL);
if (!TEST_ptr(a = DH_new())
- || !TEST_true(DH_generate_parameters_ex(a, 1024,
- DH_GENERATOR_5, _cb)))
+ || !TEST_ptr_eq(DH_get0_engine(a), e)
+ || !TEST_int_eq(DH_generate_parameters_ex(a, 1024,
+ DH_GENERATOR_5, _cb), 0)
+ || !TEST_ptr_null(DH_get0_p(a))
+ || !TEST_ptr_null(DH_get0_q(a))
+ || !TEST_ptr_null(DH_get0_g(a))
+ || !TEST_ptr_eq(DH_get0_engine(a), e))
+ goto err3;
+ TEST_info("Legacy DH parameter generation rejected in FIPS mode");
+ ERR_clear_error();
+ DH_free(a);
+ a = NULL;
+ if (!TEST_ptr(a = DH_get_2048_256())
+ || !TEST_ptr_eq(DH_get0_engine(a), e))
goto err3;
167 changes: 135 additions & 32 deletions src/we_dh.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@

#define DEFAULT_PRIME_LEN 1024

static int we_dh_prime_len_valid(int primeLen)
{
return (primeLen == 1024) || (primeLen == 2048) || (primeLen == 3072);
}

/**
* Data required to complete DH operations.
*/
Expand Down Expand Up @@ -304,6 +309,22 @@ static int we_dh_set_parameters(const DH *dh, we_Dh *engineDh)
engineDh->qLen = qBufLen;
}
}
else if (ret == 1) {
if (engineDh->q != NULL) {
OPENSSL_free(engineDh->q);
engineDh->q = NULL;
}
engineDh->qLen = 0;

/* Reinitialize the key because setting q to NULL does not clear an
* existing q on every supported wolfCrypt version. */
wc_FreeDhKey(&engineDh->key);
rc = wc_InitDhKey(&engineDh->key);
if (rc != 0) {
WOLFENGINE_ERROR_FUNC(WE_LOG_KE, "wc_InitDhKey", rc);
ret = 0;
}
}

if (ret == 1) {
/* Set p, g and q parameters into wolfSSL DH key. */
Expand Down Expand Up @@ -758,7 +779,7 @@ static int we_dh_convert_params(DhKey *wolfDh, DH *osslDh)
}
if (ret == 1) {
/* Allocate buffer for generator. */
g = (unsigned char*)OPENSSL_malloc(pLen);
g = (unsigned char*)OPENSSL_malloc(gLen == 0 ? 1 : gLen);
if (g == NULL) {
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_KE, "OPENSSL_malloc(g)", g);
ret = 0;
Expand All @@ -784,7 +805,7 @@ static int we_dh_convert_params(DhKey *wolfDh, DH *osslDh)
ret = 0;
}
}
if (ret == 1) {
if ((ret == 1) && (qLen > 0)) {
/* Allocate a big number and set value to be: Group prime. */
qBn = BN_bin2bn(q, qLen, NULL);
if (qBn == NULL) {
Expand Down Expand Up @@ -874,10 +895,10 @@ static int we_dh_paramgen_int(DH *dh, we_Dh *engineDh)
WOLFENGINE_ERROR_FUNC(WE_LOG_KE, "wc_DhGenerateParams", rc);
ret = 0;
}
}
#if defined(WE_DH_USE_GLOBAL_RNG) && !defined(WE_SINGLE_THREADED)
wc_UnLockMutex(we_rng_mutex);
wc_UnLockMutex(we_rng_mutex);
#endif
}

if (ret == 1) {
WOLFENGINE_MSG(WE_LOG_KE, "Converting DH params to OpenSSL DH");
Expand All @@ -895,35 +916,109 @@ static int we_dh_paramgen_int(DH *dh, we_Dh *engineDh)
*
* @param dh [in,out] OpenSSL DH object.
* @param primeLen [in] Length of prime.
* @param g [in] Generator to use. (ignored)
* @param cb [in] Callback to show generation progress. (ignored)
* @param g [in] Generator to use.
* @param cb [in] Callback to show generation progress.
* @returns 1 on success and 0 on failure.
*/
static int we_dh_generate_params(DH *dh, int primeLen, int g, BN_GENCB *cb)
{
int ret = 1;
we_Dh *engineDh;
int ret = 0;
#ifndef HAVE_FIPS
we_Dh *engineDh = NULL;
DH *params = NULL;
const BIGNUM *paramP = NULL;
const BIGNUM *paramQ = NULL;
const BIGNUM *paramG = NULL;
BIGNUM *p = NULL;
BIGNUM *q = NULL;
BIGNUM *generator = NULL;
#endif

WOLFENGINE_ENTER(WE_LOG_KE, "we_dh_generate_parameters");
WOLFENGINE_MSG_VERBOSE(WE_LOG_KE, "ARGS [dh = %p, primeLen = %d, g = %d, "
"cb = %p]", dh, primeLen, g, cb);

/* Cannot set g or callback to wolfSSL parameter generation. */
#ifdef HAVE_FIPS
(void)dh;
(void)primeLen;
(void)g;
(void)cb;

/* Retrieve internal DH object. */
engineDh = (we_Dh *)DH_get_ex_data(dh, WE_DH_EX_DATA_IDX);
if (engineDh == NULL) {
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_KE, "DH_get_ex_data", engineDh);
WOLFENGINE_ERROR_MSG(WE_LOG_KE, "Legacy DH parameter generation is not "
"supported in FIPS mode");
#else
ret = 1;
if (!we_dh_prime_len_valid(primeLen)) {
WOLFENGINE_ERROR_MSG(WE_LOG_KE, "Invalid DH prime bit length");
ret = 0;
}
if ((ret == 1) && (DH_get0_q(dh) != NULL)) {
WOLFENGINE_ERROR_MSG(WE_LOG_KE, "Cannot replace DH parameters with q");
ret = 0;
}

if (ret == 1) {
engineDh->primeLen = primeLen;
/* Generate the parameters with wolfSSL and copy into OpenSSL object. */
ret = we_dh_paramgen_int(dh, engineDh);
engineDh = (we_Dh *)DH_get_ex_data(dh, WE_DH_EX_DATA_IDX);
if (engineDh == NULL) {
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_KE, "DH_get_ex_data", engineDh);
ret = 0;
}
}

if (ret == 1) {
params = DH_new_method(NULL);
if (params == NULL) {
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_KE, "DH_new_method", params);
ret = 0;
}
}
if (ret == 1) {
ret = DH_set_method(params, DH_OpenSSL());
if (ret != 1) {
WOLFENGINE_ERROR_FUNC(WE_LOG_KE, "DH_set_method", ret);
}
}
if (ret == 1) {
ret = DH_generate_parameters_ex(params, primeLen, g, cb);
if (ret != 1) {
WOLFENGINE_ERROR_FUNC(WE_LOG_KE, "DH_generate_parameters_ex",
ret);
}
}
if (ret == 1) {
DH_get0_pqg(params, &paramP, &paramQ, &paramG);
p = BN_dup(paramP);
if (paramQ != NULL)
q = BN_dup(paramQ);
generator = BN_dup(paramG);
if ((p == NULL) || ((paramQ != NULL) && (q == NULL)) ||
(generator == NULL)) {
WOLFENGINE_ERROR_MSG(WE_LOG_KE, "Failed to duplicate DH params");
ret = 0;
}
}
if (ret == 1) {
ret = DH_set0_pqg(dh, p, q, generator);
if (ret != 1) {
WOLFENGINE_ERROR_FUNC(WE_LOG_KE, "DH_set0_pqg", ret);
}
else {
p = NULL;
q = NULL;
generator = NULL;
}
}
if (ret == 1) {
ret = we_dh_set_parameters(dh, engineDh);
if (ret != 1) {
WOLFENGINE_ERROR_FUNC(WE_LOG_KE, "we_dh_set_parameters", ret);
}
}

BN_free(generator);
BN_free(q);
BN_free(p);
DH_free(params);
#endif

WOLFENGINE_LEAVE(WE_LOG_KE, "we_dh_generate_parameters", ret);

Expand Down Expand Up @@ -1028,7 +1123,7 @@ static void we_dh_pkey_cleanup(EVP_PKEY_CTX *ctx)
* - EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR: intended to set the generator, "g",
* but doesn't actually do this, as wolfCrypt doesn't allow you to specify
* the generator used in DH key creation.
* - EVP_PKEY_CTRL_DH_PAD: pad out secret to input length.
* - EVP_PKEY_CTRL_DH_PAD: pad secret to the DH modulus size.
* - EVP_PKEY_CTRL_PEER_KEY: intended to set the peer key, but doesn't actually
* do this, as we can get the peer key directly from the ctx when needed.
*
Expand Down Expand Up @@ -1064,7 +1159,7 @@ static int we_dh_pkey_ctrl(EVP_PKEY_CTX *ctx, int type, int num, void *ptr)
WOLFENGINE_MSG(WE_LOG_KE,
"EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN");
/* These are the sizes allowed by wolfCrypt. */
if (num != 1024 && num != 2048 && num != 3072) {
if (!we_dh_prime_len_valid(num)) {
WOLFENGINE_ERROR_MSG(WE_LOG_KE,
"Invalid DH prime bit length.");
ret = 0;
Expand All @@ -1085,7 +1180,7 @@ static int we_dh_pkey_ctrl(EVP_PKEY_CTX *ctx, int type, int num, void *ptr)
ret = 0;
break;
case EVP_PKEY_CTRL_DH_PAD:
dh->pad = num;
dh->pad = num != 0;
break;
case EVP_PKEY_CTRL_PEER_KEY:
WOLFENGINE_MSG(WE_LOG_KE, "EVP_PKEY_CTRL_PEER_KEY");
Expand All @@ -1112,7 +1207,7 @@ static int we_dh_pkey_ctrl(EVP_PKEY_CTX *ctx, int type, int num, void *ptr)
* Extra operations for working with DH.
* Supported operations include:
* - "dh_param": set the named parameters.
* - "dh_pad": pad out secret to input length.
* - "dh_pad": pad secret to the DH modulus size.
* - "dh_paramgen_prime_len": set the length of the prime, "p."
*
* @param ctx [in] Public key context of operation.
Expand Down Expand Up @@ -1218,7 +1313,7 @@ static int we_dh_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
}
/* Set padding requirement for secret output. */
else if (XSTRCMP(type, "dh_pad") == 0) {
dh->pad = XATOI(value);
dh->pad = XATOI(value) != 0;
}
else if (XSTRCMP(type, "dh_paramgen_prime_len") == 0) {
ret = we_dh_pkey_ctrl(ctx, EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN,
Expand Down Expand Up @@ -1403,6 +1498,7 @@ static int we_dh_pkey_derive(EVP_PKEY_CTX *ctx, unsigned char *secret,
DH *peerDh = NULL;
const BIGNUM *peerPub = NULL;
size_t totalLen = *secretLen;
size_t dhLen = 0;

WOLFENGINE_ENTER(WE_LOG_KE, "we_dh_pkey_derive");
WOLFENGINE_MSG_VERBOSE(WE_LOG_KE, "ARGS [ctx = %p, secret = %p, "
Expand Down Expand Up @@ -1433,16 +1529,25 @@ static int we_dh_pkey_derive(EVP_PKEY_CTX *ctx, unsigned char *secret,
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_KE, "EVP_PKEY_get0_DH", ourDh);
ret = 0;
}
else {
dhLen = (size_t)DH_size(ourDh);
}
}

if ((ret == 1) && (secret == NULL)) {
/* Return the length of the secret only. */
*secretLen = DH_size(ourDh);
*secretLen = dhLen;
}
else if (ret == 1) {
if (engineDh->pad && (totalLen < dhLen)) {
WOLFENGINE_ERROR_MSG(WE_LOG_KE, "DH secret buffer too small");
ret = 0;
}

/* Get the peer's key. */
peerKey = EVP_PKEY_CTX_get0_peerkey(ctx);
if (peerKey == NULL) {
if (ret == 1)
peerKey = EVP_PKEY_CTX_get0_peerkey(ctx);
if ((ret == 1) && (peerKey == NULL)) {
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_KE,
"EVP_PKEY_CTX_get0_peerkey", peerKey);
ret = 0;
Expand Down Expand Up @@ -1480,13 +1585,11 @@ static int we_dh_pkey_derive(EVP_PKEY_CTX *ctx, unsigned char *secret,
}
}

/* When padding required and total buffer size is bigger than secret
* generated, ensure the buffer is zero padded at the front. */
if ((ret == 1) && engineDh->pad && (totalLen != *secretLen)) {
XMEMMOVE(secret + totalLen - *secretLen, secret, *secretLen);
XMEMSET(secret, 0, totalLen - *secretLen);
/* Return the original length. */
*secretLen = (int)totalLen;
/* Pad the generated secret to the DH modulus size. */
if ((ret == 1) && engineDh->pad && (dhLen > *secretLen)) {
XMEMMOVE(secret + dhLen - *secretLen, secret, *secretLen);
XMEMSET(secret, 0, dhLen - *secretLen);
*secretLen = dhLen;
}
}

Expand Down
17 changes: 17 additions & 0 deletions test-openssl-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ do_config() {
if [ "$VERBOSE" = "yes" ]; then
cat $TMP_FILE
fi

echo -n " dh_alloc.test ... "
LD_LIBRARY_PATH=$OPENSSL_DIR ./test/dh_alloc.test >$TMP_FILE 2>&1
DH_ALLOC_RESULT=$?
case $DH_ALLOC_RESULT in
0) echo "PASS" ;;
77) echo "SKIP" ;;
*)
cat $TMP_FILE
echo "DH allocation test failed for wolfSSL engine"
do_cleanup
exit 1
;;
esac
if [ "$VERBOSE" = "yes" ]; then
cat $TMP_FILE
fi
}

while [ $# -gt 0 ]
Expand Down
7 changes: 7 additions & 0 deletions test/include.am
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ test_unit_test_SOURCES = \
test/unit.c
test_unit_test_LDADD = libwolfengine.la
noinst_HEADERS += test/unit.h

check_PROGRAMS += test/dh_alloc.test
noinst_PROGRAMS += test/dh_alloc.test
DISTCLEANFILES += test/.libs/dh_alloc.test

test_dh_alloc_test_SOURCES = test/test_dh_alloc.c
test_dh_alloc_test_LDADD = libwolfengine.la
Loading