Skip to content

Commit 0303a82

Browse files
Merge pull request #7670 from aidangarske/CryptocbSHA3
Added crypto callback for SHA3.
2 parents 63f666a + e8c3a7d commit 0303a82

5 files changed

Lines changed: 185 additions & 9 deletions

File tree

wolfcrypt/src/cryptocb.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,40 @@ int wc_CryptoCb_Sha512Hash(wc_Sha512* sha512, const byte* in,
16061606
}
16071607
#endif /* WOLFSSL_SHA512 */
16081608

1609+
#ifdef WOLFSSL_SHA3
1610+
int wc_CryptoCb_Sha3Hash(wc_Sha3* sha3, int type, const byte* in,
1611+
word32 inSz, byte* digest)
1612+
{
1613+
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1614+
CryptoCb* dev;
1615+
1616+
/* locate registered callback */
1617+
if (sha3) {
1618+
dev = wc_CryptoCb_FindDevice(sha3->devId, WC_ALGO_TYPE_HASH);
1619+
}
1620+
else
1621+
{
1622+
/* locate first callback and try using it */
1623+
dev = wc_CryptoCb_FindDeviceByIndex(0);
1624+
}
1625+
1626+
if (dev && dev->cb) {
1627+
wc_CryptoInfo cryptoInfo;
1628+
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1629+
cryptoInfo.algo_type = WC_ALGO_TYPE_HASH;
1630+
cryptoInfo.hash.type = type;
1631+
cryptoInfo.hash.sha3 = sha3;
1632+
cryptoInfo.hash.in = in;
1633+
cryptoInfo.hash.inSz = inSz;
1634+
cryptoInfo.hash.digest = digest;
1635+
1636+
ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1637+
}
1638+
1639+
return wc_CryptoCb_TranslateErrorCode(ret);
1640+
}
1641+
#endif /* WOLFSSL_SHA3 */
1642+
16091643
#ifndef NO_HMAC
16101644
int wc_CryptoCb_Hmac(Hmac* hmac, int macType, const byte* in, word32 inSz,
16111645
byte* digest)

wolfcrypt/src/sha3.c

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
#include <wolfssl/wolfcrypt/error-crypt.h>
4444
#include <wolfssl/wolfcrypt/hash.h>
4545

46+
#ifdef WOLF_CRYPTO_CB
47+
#include <wolfssl/wolfcrypt/cryptocb.h>
48+
#endif
4649
#ifdef NO_INLINE
4750
#include <wolfssl/wolfcrypt/misc.h>
4851
#else
@@ -802,7 +805,7 @@ static int Sha3Final(wc_Sha3* sha3, byte padChar, byte* hash, byte p, word32 l)
802805
* devId Device identifier for asynchronous operation.
803806
* returns 0 on success.
804807
*/
805-
static int wc_InitSha3(wc_Sha3* sha3, void* heap, int devId)
808+
static int wc_InitSha3(wc_Sha3* sha3, int type, void* heap, int devId)
806809
{
807810
int ret = 0;
808811

@@ -817,10 +820,15 @@ static int wc_InitSha3(wc_Sha3* sha3, void* heap, int devId)
817820
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
818821
ret = wolfAsync_DevCtxInit(&sha3->asyncDev,
819822
WOLFSSL_ASYNC_MARKER_SHA3, sha3->heap, devId);
820-
#else
821-
(void)devId;
823+
#elif defined(WOLF_CRYPTO_CB)
824+
sha3->devId = devId;
825+
sha3->type = type;
826+
822827
#endif /* WOLFSSL_ASYNC_CRYPT */
823828

829+
(void)devId;
830+
(void)type;
831+
824832
return ret;
825833
}
826834

@@ -845,6 +853,17 @@ static int wc_Sha3Update(wc_Sha3* sha3, const byte* data, word32 len, byte p)
845853
return 0;
846854
}
847855

856+
#ifdef WOLF_CRYPTO_CB
857+
#ifndef WOLF_CRYPTO_CB_FIND
858+
if (sha3->devId != INVALID_DEVID)
859+
#endif
860+
{
861+
ret = wc_CryptoCb_Sha3Hash(sha3, sha3->type, data, len, NULL);
862+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
863+
return ret;
864+
/* fall-through when unavailable */
865+
}
866+
#endif
848867
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
849868
if (sha3->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA3) {
850869
#if defined(HAVE_INTEL_QA) && defined(QAT_V2)
@@ -880,6 +899,17 @@ static int wc_Sha3Final(wc_Sha3* sha3, byte* hash, byte p, byte len)
880899
return BAD_FUNC_ARG;
881900
}
882901

902+
#ifdef WOLF_CRYPTO_CB
903+
#ifndef WOLF_CRYPTO_CB_FIND
904+
if (sha3->devId != INVALID_DEVID)
905+
#endif
906+
{
907+
ret = wc_CryptoCb_Sha3Hash(sha3, sha3->type, NULL, 0, hash);
908+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
909+
return ret;
910+
/* fall-through when unavailable */
911+
}
912+
#endif
883913
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
884914
if (sha3->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA3) {
885915
#if defined(HAVE_INTEL_QA) && defined(QAT_V2)
@@ -981,7 +1011,7 @@ static int wc_Sha3GetHash(wc_Sha3* sha3, byte* hash, byte p, byte len)
9811011
*/
9821012
int wc_InitSha3_224(wc_Sha3* sha3, void* heap, int devId)
9831013
{
984-
return wc_InitSha3(sha3, heap, devId);
1014+
return wc_InitSha3(sha3, WC_HASH_TYPE_SHA3_224, heap, devId);
9851015
}
9861016

9871017
/* Update the SHA3-224 hash state with message data.
@@ -1053,7 +1083,7 @@ int wc_Sha3_224_Copy(wc_Sha3* src, wc_Sha3* dst)
10531083
*/
10541084
int wc_InitSha3_256(wc_Sha3* sha3, void* heap, int devId)
10551085
{
1056-
return wc_InitSha3(sha3, heap, devId);
1086+
return wc_InitSha3(sha3, WC_HASH_TYPE_SHA3_256, heap, devId);
10571087
}
10581088

10591089
/* Update the SHA3-256 hash state with message data.
@@ -1125,7 +1155,7 @@ int wc_Sha3_256_Copy(wc_Sha3* src, wc_Sha3* dst)
11251155
*/
11261156
int wc_InitSha3_384(wc_Sha3* sha3, void* heap, int devId)
11271157
{
1128-
return wc_InitSha3(sha3, heap, devId);
1158+
return wc_InitSha3(sha3, WC_HASH_TYPE_SHA3_384, heap, devId);
11291159
}
11301160

11311161
/* Update the SHA3-384 hash state with message data.
@@ -1197,7 +1227,7 @@ int wc_Sha3_384_Copy(wc_Sha3* src, wc_Sha3* dst)
11971227
*/
11981228
int wc_InitSha3_512(wc_Sha3* sha3, void* heap, int devId)
11991229
{
1200-
return wc_InitSha3(sha3, heap, devId);
1230+
return wc_InitSha3(sha3, WC_HASH_TYPE_SHA3_512, heap, devId);
12011231
}
12021232

12031233
/* Update the SHA3-512 hash state with message data.
@@ -1286,7 +1316,7 @@ int wc_Sha3_GetFlags(wc_Sha3* sha3, word32* flags)
12861316
*/
12871317
int wc_InitShake128(wc_Shake* shake, void* heap, int devId)
12881318
{
1289-
return wc_InitSha3(shake, heap, devId);
1319+
return wc_InitSha3(shake, WC_HASH_TYPE_SHAKE128, heap, devId);
12901320
}
12911321

12921322
/* Update the SHAKE128 hash state with message data.
@@ -1430,7 +1460,7 @@ int wc_Shake128_Copy(wc_Shake* src, wc_Shake* dst)
14301460
*/
14311461
int wc_InitShake256(wc_Shake* shake, void* heap, int devId)
14321462
{
1433-
return wc_InitSha3(shake, heap, devId);
1463+
return wc_InitSha3(shake, WC_HASH_TYPE_SHAKE256, heap, devId);
14341464
}
14351465

14361466
/* Update the SHAKE256 hash state with message data.

wolfcrypt/test/test.c

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54801,6 +54801,97 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
5480154801
#endif
5480254802
}
5480354803
else
54804+
#endif
54805+
#ifdef WOLFSSL_SHA3
54806+
if (info->hash.type == WC_HASH_TYPE_SHA3_224) {
54807+
if (info->hash.sha3 == NULL)
54808+
return NOT_COMPILED_IN;
54809+
54810+
/* set devId to invalid, so software is used */
54811+
info->hash.sha3->devId = INVALID_DEVID;
54812+
54813+
if (info->hash.in != NULL) {
54814+
ret = wc_Sha3_224_Update(
54815+
info->hash.sha3,
54816+
info->hash.in,
54817+
info->hash.inSz);
54818+
}
54819+
if (info->hash.digest != NULL) {
54820+
ret = wc_Sha3_224_Final(
54821+
info->hash.sha3,
54822+
info->hash.digest);
54823+
}
54824+
54825+
/* reset devId */
54826+
info->hash.sha3->devId = devIdArg;
54827+
}
54828+
else if (info->hash.type == WC_HASH_TYPE_SHA3_256) {
54829+
if (info->hash.sha3 == NULL)
54830+
return NOT_COMPILED_IN;
54831+
54832+
/* set devId to invalid, so software is used */
54833+
info->hash.sha3->devId = INVALID_DEVID;
54834+
54835+
if (info->hash.in != NULL) {
54836+
ret = wc_Sha3_256_Update(
54837+
info->hash.sha3,
54838+
info->hash.in,
54839+
info->hash.inSz);
54840+
}
54841+
if (info->hash.digest != NULL) {
54842+
ret = wc_Sha3_256_Final(
54843+
info->hash.sha3,
54844+
info->hash.digest);
54845+
}
54846+
54847+
/* reset devId */
54848+
info->hash.sha3->devId = devIdArg;
54849+
}
54850+
else if (info->hash.type == WC_HASH_TYPE_SHA3_384) {
54851+
if (info->hash.sha3 == NULL)
54852+
return NOT_COMPILED_IN;
54853+
54854+
/* set devId to invalid, so software is used */
54855+
info->hash.sha3->devId = INVALID_DEVID;
54856+
54857+
if (info->hash.in != NULL) {
54858+
ret = wc_Sha3_384_Update(
54859+
info->hash.sha3,
54860+
info->hash.in,
54861+
info->hash.inSz);
54862+
}
54863+
if (info->hash.digest != NULL) {
54864+
ret = wc_Sha3_384_Final(
54865+
info->hash.sha3,
54866+
info->hash.digest);
54867+
}
54868+
54869+
/* reset devId */
54870+
info->hash.sha3->devId = devIdArg;
54871+
}
54872+
else if (info->hash.type == WC_HASH_TYPE_SHA3_512) {
54873+
if (info->hash.sha3 == NULL)
54874+
return NOT_COMPILED_IN;
54875+
54876+
/* set devId to invalid, so software is used */
54877+
info->hash.sha3->devId = INVALID_DEVID;
54878+
54879+
if (info->hash.in != NULL) {
54880+
ret = wc_Sha3_512_Update(
54881+
info->hash.sha3,
54882+
info->hash.in,
54883+
info->hash.inSz);
54884+
}
54885+
if (info->hash.digest != NULL) {
54886+
ret = wc_Sha3_512_Final(
54887+
info->hash.sha3,
54888+
info->hash.digest);
54889+
}
54890+
54891+
/* reset devId */
54892+
info->hash.sha3->devId = devIdArg;
54893+
}
54894+
else
5480454895
#endif
5480554896
{
5480654897
}
@@ -54998,6 +55089,10 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void)
5499855089
#ifdef WOLFSSL_SHA512
5499955090
if (ret == 0)
5500055091
ret = sha512_test();
55092+
#ifdef WOLFSSL_SHA3
55093+
if (ret == 0)
55094+
ret = sha3_test();
55095+
#endif
5500155096
#endif
5500255097
#ifndef NO_HMAC
5500355098
#ifndef NO_SHA
@@ -55008,6 +55103,10 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void)
5500855103
if (ret == 0)
5500955104
ret = hmac_sha256_test();
5501055105
#endif
55106+
#ifdef WOLFSSL_SHA3
55107+
if (ret == 0)
55108+
ret = hmac_sha3_test();
55109+
#endif
5501155110
#endif
5501255111
#ifndef NO_PWDBASED
5501355112
#if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)

wolfssl/wolfcrypt/cryptocb.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ typedef struct wc_CryptoInfo {
398398
#endif
399399
#ifdef WOLFSSL_SHA512
400400
wc_Sha512* sha512;
401+
#endif
402+
#ifdef WOLFSSL_SHA3
403+
wc_Sha3* sha3;
401404
#endif
402405
void* ctx;
403406
#if HAVE_ANONYMOUS_INLINE_AGGREGATES
@@ -622,6 +625,11 @@ WOLFSSL_LOCAL int wc_CryptoCb_Sha512Hash(wc_Sha512* sha512, const byte* in,
622625
word32 inSz, byte* digest);
623626
#endif
624627

628+
#ifdef WOLFSSL_SHA3
629+
WOLFSSL_LOCAL int wc_CryptoCb_Sha3Hash(wc_Sha3* sha3, int type, const byte* in,
630+
word32 inSz, byte* digest);
631+
#endif
632+
625633
#ifndef NO_HMAC
626634
WOLFSSL_LOCAL int wc_CryptoCb_Hmac(Hmac* hmac, int macType, const byte* in,
627635
word32 inSz, byte* digest);

wolfssl/wolfcrypt/sha3.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ struct wc_Sha3 {
124124

125125
void* heap;
126126

127+
#ifdef WOLF_CRYPTO_CB
128+
int devId;
129+
int type; /* enum wc_HashType */
130+
#endif
131+
127132
#ifdef WC_C_DYNAMIC_FALLBACK
128133
void (*sha3_block)(word64 *s);
129134
void (*sha3_block_n)(word64 *s, const byte* data, word32 n,

0 commit comments

Comments
 (0)