From dc1f0d7822ea2b30a157e5fd7e65ecdc9546892d Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 19 Feb 2019 11:55:20 -0800 Subject: [PATCH] Fix for DH with QuickAssist to only use hardware for supported key sizes. Fix in random.c for seed devId when building async without crypto callbacks. --- wolfcrypt/src/dh.c | 38 ++++++++++++++++++++++++-------------- wolfcrypt/src/random.c | 4 +++- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/wolfcrypt/src/dh.c b/wolfcrypt/src/dh.c index 8ff761ca1..7b1f9be4e 100644 --- a/wolfcrypt/src/dh.c +++ b/wolfcrypt/src/dh.c @@ -1312,12 +1312,12 @@ static int wc_DhGenerateKeyPair_Async(DhKey* key, WC_RNG* rng, int ret; #if defined(HAVE_INTEL_QA) - word32 sz; + word32 pBits; - /* verify prime is at least 768-bits */ - /* QAT HW must have prime at least 768-bits */ - sz = mp_unsigned_bin_size(&key->p); - if (sz >= (768/8)) { + /* QAT DH sizes: 768, 1024, 1536, 2048, 3072 and 4096 bits */ + pBits = mp_unsigned_bin_size(&key->p) * 8; + if (pBits == 768 || pBits == 1024 || pBits == 1536 || + pBits == 2048 || pBits == 3072 || pBits == 4096) { mp_int x; ret = mp_init(&x); @@ -1918,15 +1918,23 @@ static int wc_DhAgree_Async(DhKey* key, byte* agree, word32* agreeSz, { int ret; -#ifdef HAVE_CAVIUM - /* TODO: Not implemented - use software for now */ - ret = wc_DhAgree_Sync(key, agree, agreeSz, priv, privSz, otherPub, pubSz); +#if defined(HAVE_INTEL_QA) + word32 pBits; + + /* QAT DH sizes: 768, 1024, 1536, 2048, 3072 and 4096 bits */ + pBits = mp_unsigned_bin_size(&key->p) * 8; + if (pBits == 768 || pBits == 1024 || pBits == 1536 || + pBits == 2048 || pBits == 3072 || pBits == 4096) { + ret = wc_mp_to_bigint(&key->p, &key->p.raw); + if (ret == MP_OKAY) + ret = IntelQaDhAgree(&key->asyncDev, &key->p.raw, + agree, agreeSz, priv, privSz, otherPub, pubSz); + return ret; + } + +#elif defined(HAVE_CAVIUM) + /* TODO: Not implemented - use software for now */ -#elif defined(HAVE_INTEL_QA) - ret = wc_mp_to_bigint(&key->p, &key->p.raw); - if (ret == MP_OKAY) - ret = IntelQaDhAgree(&key->asyncDev, &key->p.raw, - agree, agreeSz, priv, privSz, otherPub, pubSz); #else /* WOLFSSL_ASYNC_CRYPT_TEST */ if (wc_AsyncTestInit(&key->asyncDev, ASYNC_TEST_DH_AGREE)) { WC_ASYNC_TEST* testDev = &key->asyncDev.test; @@ -1939,9 +1947,11 @@ static int wc_DhAgree_Async(DhKey* key, byte* agree, word32* agreeSz, testDev->dhAgree.pubSz = pubSz; return WC_PENDING_E; } - ret = wc_DhAgree_Sync(key, agree, agreeSz, priv, privSz, otherPub, pubSz); #endif + /* otherwise use software DH */ + ret = wc_DhAgree_Sync(key, agree, agreeSz, priv, privSz, otherPub, pubSz); + return ret; } #endif /* WOLFSSL_ASYNC_CRYPT */ diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index a5fe6c838..5d809e58b 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -704,7 +704,9 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz, #endif #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB) rng->devId = devId; - rng->seed.devId = devId; + #if defined(WOLF_CRYPTO_CB) + rng->seed.devId = devId; + #endif #else (void)devId; #endif