diff --git a/wolfcrypt/src/cryptocb.c b/wolfcrypt/src/cryptocb.c index e15706108..0cc8537e5 100644 --- a/wolfcrypt/src/cryptocb.c +++ b/wolfcrypt/src/cryptocb.c @@ -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) diff --git a/wolfcrypt/src/sha512.c b/wolfcrypt/src/sha512.c index 48744e661..3615f2309 100644 --- a/wolfcrypt/src/sha512.c +++ b/wolfcrypt/src/sha512.c @@ -45,6 +45,10 @@ #include #include +#ifdef WOLF_CRYPTO_CB + #include +#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) diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 515c33a35..fd6dd0701 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -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 diff --git a/wolfssl/wolfcrypt/cryptocb.h b/wolfssl/wolfcrypt/cryptocb.h index 15c7f876e..57dc5b8bb 100644 --- a/wolfssl/wolfcrypt/cryptocb.h +++ b/wolfssl/wolfcrypt/cryptocb.h @@ -62,7 +62,15 @@ #ifdef WOLFSSL_CMAC #include #endif - +#ifdef HAVE_ED25519 + #include +#endif +#ifdef HAVE_CURVE25519 + #include +#endif +#if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384) + #include +#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); diff --git a/wolfssl/wolfcrypt/sha512.h b/wolfssl/wolfcrypt/sha512.h index 620643b3d..81da52ebb 100644 --- a/wolfssl/wolfcrypt/sha512.h +++ b/wolfssl/wolfcrypt/sha512.h @@ -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