Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added crypto callback for SHA3. #7670

Merged
merged 2 commits into from
Jun 21, 2024
Merged
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
34 changes: 34 additions & 0 deletions wolfcrypt/src/cryptocb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,40 @@ int wc_CryptoCb_Sha512Hash(wc_Sha512* sha512, const byte* in,
}
#endif /* WOLFSSL_SHA512 */

#ifdef WOLFSSL_SHA3
int wc_CryptoCb_Sha3Hash(wc_Sha3* sha3, int type, const byte* in,
word32 inSz, byte* digest)
{
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
CryptoCb* dev;

/* locate registered callback */
if (sha3) {
dev = wc_CryptoCb_FindDevice(sha3->devId, WC_ALGO_TYPE_HASH);
}
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 = type;
cryptoInfo.hash.sha3 = sha3;
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_SHA3 */

#ifndef NO_HMAC
int wc_CryptoCb_Hmac(Hmac* hmac, int macType, const byte* in, word32 inSz,
byte* digest)
Expand Down
48 changes: 39 additions & 9 deletions wolfcrypt/src/sha3.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/hash.h>

#ifdef WOLF_CRYPTO_CB
#include <wolfssl/wolfcrypt/cryptocb.h>
#endif
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
Expand Down Expand Up @@ -802,7 +805,7 @@ static int Sha3Final(wc_Sha3* sha3, byte padChar, byte* hash, byte p, word32 l)
* devId Device identifier for asynchronous operation.
* returns 0 on success.
*/
static int wc_InitSha3(wc_Sha3* sha3, void* heap, int devId)
static int wc_InitSha3(wc_Sha3* sha3, int type, void* heap, int devId)
{
int ret = 0;

Expand All @@ -817,10 +820,15 @@ static int wc_InitSha3(wc_Sha3* sha3, void* heap, int devId)
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
ret = wolfAsync_DevCtxInit(&sha3->asyncDev,
WOLFSSL_ASYNC_MARKER_SHA3, sha3->heap, devId);
#else
(void)devId;
#elif defined(WOLF_CRYPTO_CB)
sha3->devId = devId;
sha3->type = type;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the other cases (async, sw), the p parameter passed into SHA3_Update and SHA3_Final controls which type (256, 384, 512) of SHA3 is being used. Consider letting the the processing functions determine the info->hash->type based on that and remove the type parameter/member here. Comments below.


#endif /* WOLFSSL_ASYNC_CRYPT */

(void)devId;
(void)type;

return ret;
}

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

#ifdef WOLF_CRYPTO_CB
#ifndef WOLF_CRYPTO_CB_FIND
if (sha3->devId != INVALID_DEVID)
#endif
{
ret = wc_CryptoCb_Sha3Hash(sha3, sha3->type, data, len, NULL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider:
int type = 0;
switch(p) {
case WC_SHA3_224_COUNT: type = WC_HASH_TYPE_SHA3_224; break;
case WC_SHA3_256_COUNT: type = WC_HASH_TYPE_SHA3_256; break;
case WC_SHA3_384_COUNT: type = WC_HASH_TYPE_SHA3_384; break;
case WC_SHA3_512_COUNT: type = WC_HASH_TYPE_SHA3_512; break;
default: return WC_ERROR_BAD_ARG;
}
ret = wc_CryptoCb_Sha3Hash(sha, type, NULL, 0, hash);

if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
return ret;
/* fall-through when unavailable */
}
#endif
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
if (sha3->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA3) {
#if defined(HAVE_INTEL_QA) && defined(QAT_V2)
Expand Down Expand Up @@ -880,6 +899,17 @@ static int wc_Sha3Final(wc_Sha3* sha3, byte* hash, byte p, byte len)
return BAD_FUNC_ARG;
}

#ifdef WOLF_CRYPTO_CB
billphipps marked this conversation as resolved.
Show resolved Hide resolved
#ifndef WOLF_CRYPTO_CB_FIND
if (sha3->devId != INVALID_DEVID)
#endif
{
ret = wc_CryptoCb_Sha3Hash(sha3, sha3->type, NULL, 0, hash);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider:
int type = 0;
switch(len) {
case WC_SHA3_224_DIGEST_SIZE: type = WC_HASH_TYPE_SHA3_224; break;
case WC_SHA3_256_DIGEST_SIZE: type = WC_HASH_TYPE_SHA3_256; break;
case WC_SHA3_384_DIGEST_SIZE: type = WC_HASH_TYPE_SHA3_384; break;
case WC_SHA3_512_DIGEST_SIZE: type = WC_HASH_TYPE_SHA3_512; break;
default: return WC_ERROR_BAD_ARG;
}
ret = wc_CryptoCb_Sha3Hash(sha, type, NULL, 0, hash);

if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
return ret;
/* fall-through when unavailable */
}
#endif
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
if (sha3->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA3) {
#if defined(HAVE_INTEL_QA) && defined(QAT_V2)
Expand Down Expand Up @@ -981,7 +1011,7 @@ static int wc_Sha3GetHash(wc_Sha3* sha3, byte* hash, byte p, byte len)
*/
int wc_InitSha3_224(wc_Sha3* sha3, void* heap, int devId)
{
return wc_InitSha3(sha3, heap, devId);
return wc_InitSha3(sha3, WC_HASH_TYPE_SHA3_224, heap, devId);
}

/* Update the SHA3-224 hash state with message data.
Expand Down Expand Up @@ -1053,7 +1083,7 @@ int wc_Sha3_224_Copy(wc_Sha3* src, wc_Sha3* dst)
*/
int wc_InitSha3_256(wc_Sha3* sha3, void* heap, int devId)
{
return wc_InitSha3(sha3, heap, devId);
return wc_InitSha3(sha3, WC_HASH_TYPE_SHA3_256, heap, devId);
}

/* Update the SHA3-256 hash state with message data.
Expand Down Expand Up @@ -1125,7 +1155,7 @@ int wc_Sha3_256_Copy(wc_Sha3* src, wc_Sha3* dst)
*/
int wc_InitSha3_384(wc_Sha3* sha3, void* heap, int devId)
{
return wc_InitSha3(sha3, heap, devId);
return wc_InitSha3(sha3, WC_HASH_TYPE_SHA3_384, heap, devId);
}

/* Update the SHA3-384 hash state with message data.
Expand Down Expand Up @@ -1197,7 +1227,7 @@ int wc_Sha3_384_Copy(wc_Sha3* src, wc_Sha3* dst)
*/
int wc_InitSha3_512(wc_Sha3* sha3, void* heap, int devId)
{
return wc_InitSha3(sha3, heap, devId);
return wc_InitSha3(sha3, WC_HASH_TYPE_SHA3_512, heap, devId);
}

/* Update the SHA3-512 hash state with message data.
Expand Down Expand Up @@ -1286,7 +1316,7 @@ int wc_Sha3_GetFlags(wc_Sha3* sha3, word32* flags)
*/
int wc_InitShake128(wc_Shake* shake, void* heap, int devId)
{
return wc_InitSha3(shake, heap, devId);
return wc_InitSha3(shake, WC_HASH_TYPE_SHAKE128, heap, devId);
}

/* Update the SHAKE128 hash state with message data.
Expand Down Expand Up @@ -1430,7 +1460,7 @@ int wc_Shake128_Copy(wc_Shake* src, wc_Shake* dst)
*/
int wc_InitShake256(wc_Shake* shake, void* heap, int devId)
{
return wc_InitSha3(shake, heap, devId);
return wc_InitSha3(shake, WC_HASH_TYPE_SHAKE256, heap, devId);
}

/* Update the SHAKE256 hash state with message data.
Expand Down
99 changes: 99 additions & 0 deletions wolfcrypt/test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -54801,6 +54801,97 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
#endif
}
else
#endif
#ifdef WOLFSSL_SHA3
if (info->hash.type == WC_HASH_TYPE_SHA3_224) {
if (info->hash.sha3 == NULL)
return NOT_COMPILED_IN;

/* set devId to invalid, so software is used */
info->hash.sha3->devId = INVALID_DEVID;

if (info->hash.in != NULL) {
ret = wc_Sha3_224_Update(
info->hash.sha3,
info->hash.in,
info->hash.inSz);
}
if (info->hash.digest != NULL) {
ret = wc_Sha3_224_Final(
info->hash.sha3,
info->hash.digest);
}

/* reset devId */
info->hash.sha3->devId = devIdArg;
}
else if (info->hash.type == WC_HASH_TYPE_SHA3_256) {
if (info->hash.sha3 == NULL)
return NOT_COMPILED_IN;

/* set devId to invalid, so software is used */
info->hash.sha3->devId = INVALID_DEVID;

if (info->hash.in != NULL) {
ret = wc_Sha3_256_Update(
info->hash.sha3,
info->hash.in,
info->hash.inSz);
}
if (info->hash.digest != NULL) {
ret = wc_Sha3_256_Final(
info->hash.sha3,
info->hash.digest);
}

/* reset devId */
info->hash.sha3->devId = devIdArg;
}
else if (info->hash.type == WC_HASH_TYPE_SHA3_384) {
if (info->hash.sha3 == NULL)
return NOT_COMPILED_IN;

/* set devId to invalid, so software is used */
info->hash.sha3->devId = INVALID_DEVID;

if (info->hash.in != NULL) {
ret = wc_Sha3_384_Update(
info->hash.sha3,
info->hash.in,
info->hash.inSz);
}
if (info->hash.digest != NULL) {
ret = wc_Sha3_384_Final(
info->hash.sha3,
info->hash.digest);
}

/* reset devId */
info->hash.sha3->devId = devIdArg;
}
else if (info->hash.type == WC_HASH_TYPE_SHA3_512) {
if (info->hash.sha3 == NULL)
return NOT_COMPILED_IN;

/* set devId to invalid, so software is used */
info->hash.sha3->devId = INVALID_DEVID;

if (info->hash.in != NULL) {
ret = wc_Sha3_512_Update(
info->hash.sha3,
info->hash.in,
info->hash.inSz);
}
if (info->hash.digest != NULL) {
ret = wc_Sha3_512_Final(
info->hash.sha3,
info->hash.digest);
}

/* reset devId */
info->hash.sha3->devId = devIdArg;
}
else
#endif
{
}
Expand Down Expand Up @@ -54998,6 +55089,10 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void)
#ifdef WOLFSSL_SHA512
if (ret == 0)
ret = sha512_test();
#ifdef WOLFSSL_SHA3
if (ret == 0)
ret = sha3_test();
#endif
#endif
#ifndef NO_HMAC
#ifndef NO_SHA
Expand All @@ -55008,6 +55103,10 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void)
if (ret == 0)
ret = hmac_sha256_test();
#endif
#ifdef WOLFSSL_SHA3
if (ret == 0)
ret = hmac_sha3_test();
#endif
#endif
#ifndef NO_PWDBASED
#if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
Expand Down
8 changes: 8 additions & 0 deletions wolfssl/wolfcrypt/cryptocb.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ typedef struct wc_CryptoInfo {
#endif
#ifdef WOLFSSL_SHA512
wc_Sha512* sha512;
#endif
#ifdef WOLFSSL_SHA3
wc_Sha3* sha3;
#endif
void* ctx;
#if HAVE_ANONYMOUS_INLINE_AGGREGATES
Expand Down Expand Up @@ -622,6 +625,11 @@ WOLFSSL_LOCAL int wc_CryptoCb_Sha512Hash(wc_Sha512* sha512, const byte* in,
word32 inSz, byte* digest);
#endif

#ifdef WOLFSSL_SHA3
WOLFSSL_LOCAL int wc_CryptoCb_Sha3Hash(wc_Sha3* sha3, int type, 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);
Expand Down
5 changes: 5 additions & 0 deletions wolfssl/wolfcrypt/sha3.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ struct wc_Sha3 {

void* heap;

#ifdef WOLF_CRYPTO_CB
int devId;
int type; /* enum wc_HashType */
#endif

#ifdef WC_C_DYNAMIC_FALLBACK
void (*sha3_block)(word64 *s);
void (*sha3_block_n)(word64 *s, const byte* data, word32 n,
Expand Down