Skip to content

Commit 2a90a73

Browse files
authored
Merge pull request #384 from bigbrett/sha3
add SHA3 support
2 parents ea4a23e + 4c28887 commit 2a90a73

12 files changed

Lines changed: 3150 additions & 36 deletions

src/wh_client_crypto.c

Lines changed: 876 additions & 0 deletions
Large diffs are not rendered by default.

src/wh_client_cryptocb.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,53 @@ int wh_Client_CryptoCbStd(int devId, wc_CryptoInfo* info, void* inCtx)
628628
ret = wh_Client_Sha512(ctx, sha, in, inLen, out);
629629
} break;
630630
#endif /* WOLFSSL_SHA512 && WOLFSSL_SHA512_HASHTYPE */
631+
#if defined(WOLFSSL_SHA3)
632+
case WC_HASH_TYPE_SHA3_224:
633+
case WC_HASH_TYPE_SHA3_256:
634+
case WC_HASH_TYPE_SHA3_384:
635+
case WC_HASH_TYPE_SHA3_512: {
636+
wc_Sha3* sha = info->hash.sha3;
637+
#ifdef WOLFSSL_HASH_FLAGS
638+
/* Keccak-mode SHA3 (legacy 0x01-padding variant) is a software-
639+
* only mode; fall through to wolfCrypt's software path. */
640+
if (sha != NULL &&
641+
(sha->flags & WC_HASH_SHA3_KECCAK256) != 0u) {
642+
ret = CRYPTOCB_UNAVAILABLE;
643+
break;
644+
}
645+
#endif
646+
switch (info->hash.type) {
647+
#ifndef WOLFSSL_NOSHA3_224
648+
case WC_HASH_TYPE_SHA3_224:
649+
ret = wh_Client_Sha3_224(ctx, sha, info->hash.in,
650+
info->hash.inSz,
651+
info->hash.digest);
652+
break;
653+
#endif
654+
#ifndef WOLFSSL_NOSHA3_256
655+
case WC_HASH_TYPE_SHA3_256:
656+
ret = wh_Client_Sha3_256(ctx, sha, info->hash.in,
657+
info->hash.inSz,
658+
info->hash.digest);
659+
break;
660+
#endif
661+
#ifndef WOLFSSL_NOSHA3_384
662+
case WC_HASH_TYPE_SHA3_384:
663+
ret = wh_Client_Sha3_384(ctx, sha, info->hash.in,
664+
info->hash.inSz,
665+
info->hash.digest);
666+
break;
667+
#endif
668+
#ifndef WOLFSSL_NOSHA3_512
669+
case WC_HASH_TYPE_SHA3_512:
670+
ret = wh_Client_Sha3_512(ctx, sha, info->hash.in,
671+
info->hash.inSz,
672+
info->hash.digest);
673+
break;
674+
#endif
675+
}
676+
} break;
677+
#endif /* WOLFSSL_SHA3 */
631678
default:
632679
ret = CRYPTOCB_UNAVAILABLE;
633680
break;
@@ -1396,6 +1443,51 @@ int wh_Client_CryptoCbDma(int devId, wc_CryptoInfo* info, void* inCtx)
13961443
ret = wh_Client_Sha512Dma(ctx, sha, in, inLen, out);
13971444
} break;
13981445
#endif /* WOLFSSL_SHA512 && defined(WOLFSSL_SHA512_HASHTYPE) */
1446+
#if defined(WOLFSSL_SHA3)
1447+
case WC_HASH_TYPE_SHA3_224:
1448+
case WC_HASH_TYPE_SHA3_256:
1449+
case WC_HASH_TYPE_SHA3_384:
1450+
case WC_HASH_TYPE_SHA3_512: {
1451+
wc_Sha3* sha = info->hash.sha3;
1452+
#ifdef WOLFSSL_HASH_FLAGS
1453+
if (sha != NULL &&
1454+
(sha->flags & WC_HASH_SHA3_KECCAK256) != 0u) {
1455+
ret = CRYPTOCB_UNAVAILABLE;
1456+
break;
1457+
}
1458+
#endif
1459+
switch (info->hash.type) {
1460+
#ifndef WOLFSSL_NOSHA3_224
1461+
case WC_HASH_TYPE_SHA3_224:
1462+
ret = wh_Client_Sha3_224Dma(ctx, sha, info->hash.in,
1463+
info->hash.inSz,
1464+
info->hash.digest);
1465+
break;
1466+
#endif
1467+
#ifndef WOLFSSL_NOSHA3_256
1468+
case WC_HASH_TYPE_SHA3_256:
1469+
ret = wh_Client_Sha3_256Dma(ctx, sha, info->hash.in,
1470+
info->hash.inSz,
1471+
info->hash.digest);
1472+
break;
1473+
#endif
1474+
#ifndef WOLFSSL_NOSHA3_384
1475+
case WC_HASH_TYPE_SHA3_384:
1476+
ret = wh_Client_Sha3_384Dma(ctx, sha, info->hash.in,
1477+
info->hash.inSz,
1478+
info->hash.digest);
1479+
break;
1480+
#endif
1481+
#ifndef WOLFSSL_NOSHA3_512
1482+
case WC_HASH_TYPE_SHA3_512:
1483+
ret = wh_Client_Sha3_512Dma(ctx, sha, info->hash.in,
1484+
info->hash.inSz,
1485+
info->hash.digest);
1486+
break;
1487+
#endif
1488+
}
1489+
} break;
1490+
#endif /* WOLFSSL_SHA3 */
13991491
default:
14001492
ret = CRYPTOCB_UNAVAILABLE;
14011493
break;

src/wh_message_crypto.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,59 @@ int wh_MessageCrypto_TranslateSha2Response(
704704
return 0;
705705
}
706706

707+
/* SHA3 state translation - shared across all SHA3 variants and across the
708+
* non-DMA and DMA wire formats. */
709+
int wh_MessageCrypto_TranslateSha3State(uint16_t magic,
710+
const whMessageCrypto_Sha3State* src,
711+
whMessageCrypto_Sha3State* dest)
712+
{
713+
int k;
714+
715+
if ((src == NULL) || (dest == NULL)) {
716+
return WH_ERROR_BADARGS;
717+
}
718+
for (k = 0; k < 25; k++) {
719+
WH_T64(magic, dest, src, s[k]);
720+
}
721+
return 0;
722+
}
723+
724+
/* SHA3 Request translation. Trailing input bytes are raw and need no
725+
* translation. */
726+
int wh_MessageCrypto_TranslateSha3Request(
727+
uint16_t magic, const whMessageCrypto_Sha3Request* src,
728+
whMessageCrypto_Sha3Request* dest)
729+
{
730+
if ((src == NULL) || (dest == NULL)) {
731+
return WH_ERROR_BADARGS;
732+
}
733+
WH_T32(magic, dest, src, isLastBlock);
734+
WH_T32(magic, dest, src, inSz);
735+
return wh_MessageCrypto_TranslateSha3State(magic, &src->resumeState,
736+
&dest->resumeState);
737+
}
738+
739+
/* SHA3 Response translation */
740+
int wh_MessageCrypto_TranslateSha3Response(
741+
uint16_t magic, const whMessageCrypto_Sha3Response* src,
742+
whMessageCrypto_Sha3Response* dest)
743+
{
744+
int ret;
745+
746+
if ((src == NULL) || (dest == NULL)) {
747+
return WH_ERROR_BADARGS;
748+
}
749+
ret = wh_MessageCrypto_TranslateSha3State(magic, &src->resumeState,
750+
&dest->resumeState);
751+
if (ret != 0) {
752+
return ret;
753+
}
754+
if (src != dest) {
755+
memcpy(dest->hash, src->hash, sizeof(src->hash));
756+
}
757+
return 0;
758+
}
759+
707760

708761
/* CMAC-AES State translation */
709762
int wh_MessageCrypto_TranslateCmacAesState(
@@ -1037,6 +1090,48 @@ int wh_MessageCrypto_TranslateSha2DmaResponse(
10371090
&dest->dmaAddrStatus);
10381091
}
10391092

1093+
/* SHA3 DMA Request translation */
1094+
int wh_MessageCrypto_TranslateSha3DmaRequest(
1095+
uint16_t magic, const whMessageCrypto_Sha3DmaRequest* src,
1096+
whMessageCrypto_Sha3DmaRequest* dest)
1097+
{
1098+
int ret;
1099+
1100+
if ((src == NULL) || (dest == NULL)) {
1101+
return WH_ERROR_BADARGS;
1102+
}
1103+
ret = wh_MessageCrypto_TranslateDmaBuffer(magic, &src->input, &dest->input);
1104+
if (ret != 0) {
1105+
return ret;
1106+
}
1107+
WH_T32(magic, dest, src, isLastBlock);
1108+
WH_T32(magic, dest, src, inSz);
1109+
return wh_MessageCrypto_TranslateSha3State(magic, &src->resumeState,
1110+
&dest->resumeState);
1111+
}
1112+
1113+
/* SHA3 DMA Response translation */
1114+
int wh_MessageCrypto_TranslateSha3DmaResponse(
1115+
uint16_t magic, const whMessageCrypto_Sha3DmaResponse* src,
1116+
whMessageCrypto_Sha3DmaResponse* dest)
1117+
{
1118+
int ret;
1119+
1120+
if ((src == NULL) || (dest == NULL)) {
1121+
return WH_ERROR_BADARGS;
1122+
}
1123+
ret = wh_MessageCrypto_TranslateSha3State(magic, &src->resumeState,
1124+
&dest->resumeState);
1125+
if (ret != 0) {
1126+
return ret;
1127+
}
1128+
if (src != dest) {
1129+
memcpy(dest->hash, src->hash, sizeof(src->hash));
1130+
}
1131+
return wh_MessageCrypto_TranslateDmaAddrStatus(magic, &src->dmaAddrStatus,
1132+
&dest->dmaAddrStatus);
1133+
}
1134+
10401135
/* CMAC-AES DMA Request translation */
10411136
int wh_MessageCrypto_TranslateCmacAesDmaRequest(
10421137
uint16_t magic, const whMessageCrypto_CmacAesDmaRequest* src,

0 commit comments

Comments
 (0)