Added SHA2-384/512 crypto callback support.

This commit is contained in:
David Garske
2021-06-15 16:07:23 -07:00
parent 27218e1d40
commit 9c24731e3c
5 changed files with 213 additions and 10 deletions

View File

@@ -628,6 +628,72 @@ int wc_CryptoCb_Sha256Hash(wc_Sha256* sha256, const byte* in,
}
#endif /* !NO_SHA256 */
#ifdef WOLFSSL_SHA384
int wc_CryptoCb_Sha384Hash(wc_Sha384* sha384, const byte* in,
word32 inSz, byte* digest)
{
int ret = CRYPTOCB_UNAVAILABLE;
CryptoCb* dev;
/* locate registered callback */
if (sha384) {
dev = wc_CryptoCb_FindDevice(sha384->devId);
}
else {
/* locate first callback and try using it */
dev = wc_CryptoCb_FindDeviceByIndex(0);
}
if (dev && dev->cb) {
wc_CryptoInfo cryptoInfo;
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
cryptoInfo.algo_type = WC_ALGO_TYPE_HASH;
cryptoInfo.hash.type = WC_HASH_TYPE_SHA384;
cryptoInfo.hash.sha384 = sha384;
cryptoInfo.hash.in = in;
cryptoInfo.hash.inSz = inSz;
cryptoInfo.hash.digest = digest;
ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
}
return wc_CryptoCb_TranslateErrorCode(ret);
}
#endif /* WOLFSSL_SHA384 */
#ifdef WOLFSSL_SHA512
int wc_CryptoCb_Sha512Hash(wc_Sha512* sha512, const byte* in,
word32 inSz, byte* digest)
{
int ret = CRYPTOCB_UNAVAILABLE;
CryptoCb* dev;
/* locate registered callback */
if (sha512) {
dev = wc_CryptoCb_FindDevice(sha512->devId);
}
else {
/* locate first callback and try using it */
dev = wc_CryptoCb_FindDeviceByIndex(0);
}
if (dev && dev->cb) {
wc_CryptoInfo cryptoInfo;
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
cryptoInfo.algo_type = WC_ALGO_TYPE_HASH;
cryptoInfo.hash.type = WC_HASH_TYPE_SHA512;
cryptoInfo.hash.sha512 = sha512;
cryptoInfo.hash.in = in;
cryptoInfo.hash.inSz = inSz;
cryptoInfo.hash.digest = digest;
ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
}
return wc_CryptoCb_TranslateErrorCode(ret);
}
#endif /* WOLFSSL_SHA512 */
#ifndef NO_HMAC
int wc_CryptoCb_Hmac(Hmac* hmac, int macType, const byte* in, word32 inSz,
byte* digest)

View File

@@ -45,6 +45,10 @@
#include <wolfssl/wolfcrypt/cpuid.h>
#include <wolfssl/wolfcrypt/hash.h>
#ifdef WOLF_CRYPTO_CB
#include <wolfssl/wolfcrypt/cryptocb.h>
#endif
/* deprecated USE_SLOW_SHA2 (replaced with USE_SLOW_SHA512) */
#if defined(USE_SLOW_SHA2) && !defined(USE_SLOW_SHA512)
#define USE_SLOW_SHA512
@@ -429,6 +433,10 @@ int wc_InitSha512_ex(wc_Sha512* sha512, void* heap, int devId)
#ifdef WOLFSSL_SMALL_STACK_CACHE
sha512->W = NULL;
#endif
#ifdef WOLF_CRYPTO_CB
sha512->devId = devId;
sha512->devCtx = NULL;
#endif
ret = InitSha512(sha512);
if (ret != 0)
@@ -734,6 +742,14 @@ int wc_Sha512Update(wc_Sha512* sha512, const byte* data, word32 len)
return BAD_FUNC_ARG;
}
#ifdef WOLF_CRYPTO_CB
if (sha512->devId != INVALID_DEVID) {
int ret = wc_CryptoCb_Sha512Hash(sha512, data, len, NULL);
if (ret != CRYPTOCB_UNAVAILABLE)
return ret;
/* fall-through when unavailable */
}
#endif
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
if (sha512->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA512) {
#if defined(HAVE_INTEL_QA)
@@ -877,7 +893,14 @@ int wc_Sha512Final(wc_Sha512* sha512, byte* hash)
if (sha512 == NULL || hash == NULL) {
return BAD_FUNC_ARG;
}
#ifdef WOLF_CRYPTO_CB
if (sha512->devId != INVALID_DEVID) {
ret = wc_CryptoCb_Sha512Hash(sha512, NULL, 0, hash);
if (ret != CRYPTOCB_UNAVAILABLE)
return ret;
/* fall-through when unavailable */
}
#endif
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
if (sha512->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA512) {
#if defined(HAVE_INTEL_QA)
@@ -1032,6 +1055,14 @@ int wc_Sha384Update(wc_Sha384* sha384, const byte* data, word32 len)
return BAD_FUNC_ARG;
}
#ifdef WOLF_CRYPTO_CB
if (sha384->devId != INVALID_DEVID) {
int ret = wc_CryptoCb_Sha384Hash(sha384, data, len, NULL);
if (ret != CRYPTOCB_UNAVAILABLE)
return ret;
/* fall-through when unavailable */
}
#endif
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
if (sha384->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA384) {
#if defined(HAVE_INTEL_QA)
@@ -1073,6 +1104,14 @@ int wc_Sha384Final(wc_Sha384* sha384, byte* hash)
return BAD_FUNC_ARG;
}
#ifdef WOLF_CRYPTO_CB
if (sha384->devId != INVALID_DEVID) {
ret = wc_CryptoCb_Sha384Hash(sha384, NULL, 0, hash);
if (ret != CRYPTOCB_UNAVAILABLE)
return ret;
/* fall-through when unavailable */
}
#endif
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
if (sha384->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA384) {
#if defined(HAVE_INTEL_QA)
@@ -1103,6 +1142,10 @@ int wc_InitSha384_ex(wc_Sha384* sha384, void* heap, int devId)
#ifdef WOLFSSL_SMALL_STACK_CACHE
sha384->W = NULL;
#endif
#ifdef WOLF_CRYPTO_CB
sha384->devId = devId;
sha384->devCtx = NULL;
#endif
ret = InitSha384(sha384);
if (ret != 0)

View File

@@ -36960,7 +36960,8 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
#endif /* !NO_DES3 */
#endif /* !NO_AES || !NO_DES3 */
}
#if !defined(NO_SHA) || !defined(NO_SHA256)
#if !defined(NO_SHA) || !defined(NO_SHA256) || \
defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
else if (info->algo_type == WC_ALGO_TYPE_HASH) {
#if !defined(NO_SHA)
if (info->hash.type == WC_HASH_TYPE_SHA) {
@@ -37011,6 +37012,56 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
info->hash.sha256->devId = devIdArg;
}
else
#endif
#ifdef WOLFSSL_SHA384
if (info->hash.type == WC_HASH_TYPE_SHA384) {
if (info->hash.sha384 == NULL)
return NOT_COMPILED_IN;
/* set devId to invalid, so software is used */
info->hash.sha384->devId = INVALID_DEVID;
if (info->hash.in != NULL) {
ret = wc_Sha384Update(
info->hash.sha384,
info->hash.in,
info->hash.inSz);
}
if (info->hash.digest != NULL) {
ret = wc_Sha384Final(
info->hash.sha384,
info->hash.digest);
}
/* reset devId */
info->hash.sha384->devId = devIdArg;
}
else
#endif
#ifdef WOLFSSL_SHA512
if (info->hash.type == WC_HASH_TYPE_SHA512) {
if (info->hash.sha512 == NULL)
return NOT_COMPILED_IN;
/* set devId to invalid, so software is used */
info->hash.sha512->devId = INVALID_DEVID;
if (info->hash.in != NULL) {
ret = wc_Sha512Update(
info->hash.sha512,
info->hash.in,
info->hash.inSz);
}
if (info->hash.digest != NULL) {
ret = wc_Sha512Final(
info->hash.sha512,
info->hash.digest);
}
/* reset devId */
info->hash.sha512->devId = devIdArg;
}
else
#endif
{
}
@@ -37085,15 +37136,21 @@ WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void)
if (ret == 0)
ret = des3_test();
#endif /* !NO_DES3 */
#if !defined(NO_SHA) || !defined(NO_SHA256)
#ifndef NO_SHA
#ifndef NO_SHA
if (ret == 0)
ret = sha_test();
#endif
#ifndef NO_SHA256
#endif
#ifndef NO_SHA256
if (ret == 0)
ret = sha256_test();
#endif
#endif
#ifdef WOLFSSL_SHA384
if (ret == 0)
ret = sha384_test();
#endif
#ifdef WOLFSSL_SHA512
if (ret == 0)
ret = sha512_test();
#endif
#ifndef NO_HMAC
#ifndef NO_SHA

View File

@@ -62,7 +62,15 @@
#ifdef WOLFSSL_CMAC
#include <wolfssl/wolfcrypt/cmac.h>
#endif
#ifdef HAVE_ED25519
#include <wolfssl/wolfcrypt/ed25519.h>
#endif
#ifdef HAVE_CURVE25519
#include <wolfssl/wolfcrypt/curve25519.h>
#endif
#if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384)
#include <wolfssl/wolfcrypt/sha512.h>
#endif
/* Crypto Information Structure for callbacks */
typedef struct wc_CryptoInfo {
@@ -130,6 +138,16 @@ typedef struct wc_CryptoInfo {
word32 pubKeySz;
} ecc_check;
#endif
#ifdef HAVE_CURVE25519
struct {
curve25519_key* key;
} curve25519;
#endif
#ifdef HAVE_ED25519
struct {
ed25519_key* key;
} ed25519;
#endif
};
} pk;
#endif /* !NO_RSA || HAVE_ECC */
@@ -183,7 +201,8 @@ typedef struct wc_CryptoInfo {
};
} cipher;
#endif /* !NO_AES || !NO_DES3 */
#if !defined(NO_SHA) || !defined(NO_SHA256)
#if !defined(NO_SHA) || !defined(NO_SHA256) || \
defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384)
struct {
int type; /* enum wc_HashType */
const byte* in;
@@ -196,6 +215,12 @@ typedef struct wc_CryptoInfo {
#ifndef NO_SHA256
wc_Sha256* sha256;
#endif
#ifdef WOLFSSL_SHA384
wc_Sha384* sha384;
#endif
#ifdef WOLFSSL_SHA512
wc_Sha512* sha512;
#endif
};
} hash;
#endif /* !NO_SHA || !NO_SHA256 */
@@ -313,6 +338,15 @@ WOLFSSL_LOCAL int wc_CryptoCb_ShaHash(wc_Sha* sha, const byte* in,
WOLFSSL_LOCAL int wc_CryptoCb_Sha256Hash(wc_Sha256* sha256, const byte* in,
word32 inSz, byte* digest);
#endif /* !NO_SHA256 */
#ifdef WOLFSSL_SHA384
WOLFSSL_LOCAL int wc_CryptoCb_Sha384Hash(wc_Sha384* sha384, const byte* in,
word32 inSz, byte* digest);
#endif
#ifdef WOLFSSL_SHA512
WOLFSSL_LOCAL int wc_CryptoCb_Sha512Hash(wc_Sha512* sha512, const byte* in,
word32 inSz, byte* digest);
#endif
#ifndef NO_HMAC
WOLFSSL_LOCAL int wc_CryptoCb_Hmac(Hmac* hmac, int macType, const byte* in,
word32 inSz, byte* digest);

View File

@@ -152,7 +152,10 @@ struct wc_Sha512 {
#if defined(WOLFSSL_SILABS_SE_ACCEL)
wc_silabs_sha_t silabsCtx;
#endif
#ifdef WOLF_CRYPTO_CB
int devId;
void* devCtx; /* generic crypto callback context */
#endif
#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
word32 flags; /* enum wc_HashFlags in hash.h */
#endif