Skip to content

Commit 9cbc3f9

Browse files
committed
cryptocb: sha512_family: try specific digest length hashtype first
If the cryptocb provider supports specific SHA512/224 and SHA512/256 hashtype, this commit allows to: 1. avoid a copy 2. do not touch the output buffer outside of the cryptocb handler 2 might be important for cryptocb provider that needs special handling of memory buffer (DMA, memory mapping).
1 parent 99c983d commit 9cbc3f9

4 files changed

Lines changed: 34 additions & 11 deletions

File tree

wolfcrypt/src/cryptocb.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,7 @@ int wc_CryptoCb_Sha384Hash(wc_Sha384* sha384, const byte* in,
17201720

17211721
#ifdef WOLFSSL_SHA512
17221722
int wc_CryptoCb_Sha512Hash(wc_Sha512* sha512, const byte* in,
1723-
word32 inSz, byte* digest)
1723+
word32 inSz, byte* digest, size_t digestSz)
17241724
{
17251725
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
17261726
CryptoCb* dev;
@@ -1738,16 +1738,43 @@ int wc_CryptoCb_Sha512Hash(wc_Sha512* sha512, const byte* in,
17381738
}
17391739

17401740
if (dev && dev->cb) {
1741+
byte localHash[WC_SHA512_DIGEST_SIZE];
17411742
wc_CryptoInfo cryptoInfo;
17421743
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
17431744
cryptoInfo.algo_type = WC_ALGO_TYPE_HASH;
1744-
cryptoInfo.hash.type = WC_HASH_TYPE_SHA512;
17451745
cryptoInfo.hash.sha512 = sha512;
17461746
cryptoInfo.hash.in = in;
17471747
cryptoInfo.hash.inSz = inSz;
17481748
cryptoInfo.hash.digest = digest;
17491749

1750+
/* try the specific family callbacks first */
1751+
#if !defined(WOLFSSL_NOSHA512_224)
1752+
if (digest != NULL && digestSz == WC_SHA512_224_DIGEST_SIZE) {
1753+
cryptoInfo.hash.type = WC_HASH_TYPE_SHA512_224;
1754+
ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1755+
ret = wc_CryptoCb_TranslateErrorCode(ret);
1756+
if (ret != CRYPTOCB_UNAVAILABLE)
1757+
return ret;
1758+
}
1759+
#endif
1760+
#if !defined(WOLFSSL_NOSHA512_256)
1761+
if (digest != NULL && digestSz == WC_SHA512_256_DIGEST_SIZE) {
1762+
cryptoInfo.hash.type = WC_HASH_TYPE_SHA512_256;
1763+
ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1764+
ret = wc_CryptoCb_TranslateErrorCode(ret);
1765+
if (ret != CRYPTOCB_UNAVAILABLE)
1766+
return ret;
1767+
}
1768+
#endif
1769+
cryptoInfo.hash.type = WC_HASH_TYPE_SHA512;
1770+
/* use local buffer if not full size */
1771+
if (digest != NULL && digestSz != WC_SHA512_DIGEST_SIZE)
1772+
cryptoInfo.hash.digest = localHash;
17501773
ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1774+
ret = wc_CryptoCb_TranslateErrorCode(ret);
1775+
if (ret == 0 && digest != NULL && digestSz != WC_SHA512_DIGEST_SIZE)
1776+
XMEMCPY(digest, localHash, digestSz);
1777+
return ret;
17511778
}
17521779

17531780
return wc_CryptoCb_TranslateErrorCode(ret);

wolfcrypt/src/port/arm/armv8-sha512.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ int wc_Sha512Update(wc_Sha512* sha512, const byte* data, word32 len)
543543
if (sha512->devId != INVALID_DEVID)
544544
#endif
545545
{
546-
int ret = wc_CryptoCb_Sha512Hash(sha512, data, len, NULL);
546+
int ret = wc_CryptoCb_Sha512Hash(sha512, data, len, NULL, 0);
547547
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
548548
return ret;
549549
/* fall-through when unavailable */
@@ -675,10 +675,8 @@ static int Sha512_Family_Final(wc_Sha512* sha512, byte* hash,
675675
if (sha512->devId != INVALID_DEVID)
676676
#endif
677677
{
678-
byte localHash[WC_SHA512_DIGEST_SIZE];
679-
ret = wc_CryptoCb_Sha512Hash(sha512, NULL, 0, localHash);
678+
ret = wc_CryptoCb_Sha512Hash(sha512, NULL, 0, hash, digestSz);
680679
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
681-
XMEMCPY(hash, localHash, digestSz);
682680
return ret;
683681
}
684682
/* fall-through when unavailable */

wolfcrypt/src/sha512.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ int wc_Sha512Update(wc_Sha512* sha512, const byte* data, word32 len)
11921192
if (sha512->devId != INVALID_DEVID)
11931193
#endif
11941194
{
1195-
int ret = wc_CryptoCb_Sha512Hash(sha512, data, len, NULL);
1195+
int ret = wc_CryptoCb_Sha512Hash(sha512, data, len, NULL, 0);
11961196
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
11971197
return ret;
11981198
/* fall-through when unavailable */
@@ -1429,10 +1429,8 @@ static int Sha512_Family_Final(wc_Sha512* sha512, byte* hash, size_t digestSz,
14291429
if (sha512->devId != INVALID_DEVID)
14301430
#endif
14311431
{
1432-
byte localHash[WC_SHA512_DIGEST_SIZE];
1433-
ret = wc_CryptoCb_Sha512Hash(sha512, NULL, 0, localHash);
1432+
ret = wc_CryptoCb_Sha512Hash(sha512, NULL, 0, hash, digestSz);
14341433
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
1435-
XMEMCPY(hash, localHash, digestSz);
14361434
return ret;
14371435
}
14381436
/* fall-through when unavailable */

wolfssl/wolfcrypt/cryptocb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ WOLFSSL_LOCAL int wc_CryptoCb_Sha384Hash(wc_Sha384* sha384, const byte* in,
676676
#endif
677677
#ifdef WOLFSSL_SHA512
678678
WOLFSSL_LOCAL int wc_CryptoCb_Sha512Hash(wc_Sha512* sha512, const byte* in,
679-
word32 inSz, byte* digest);
679+
word32 inSz, byte* digest, size_t digestSz);
680680
#endif
681681

682682
#ifdef WOLFSSL_SHA3

0 commit comments

Comments
 (0)