Add fix for siphash cache and tests

This commit is contained in:
Anthony Tatowicz 2023-01-05 16:56:07 -06:00
parent adb406e1ee
commit a08c853799
2 changed files with 28 additions and 1 deletions

View File

@ -268,6 +268,7 @@ int wc_SipHashUpdate(SipHash* sipHash, const unsigned char* in, word32 inSz)
if (sipHash->cacheCnt == SIPHASH_BLOCK_SIZE) {
/* Compress the block from the cache. */
SipHashCompress(sipHash, sipHash->cache);
sipHash->inCnt += SIPHASH_BLOCK_SIZE;
sipHash->cacheCnt = 0;
}
}
@ -329,7 +330,7 @@ int wc_SipHashFinal(SipHash* sipHash, unsigned char* out, unsigned char outSz)
}
if (ret == 0) {
/* Put int remaining cached message bytes. */
/* Put in remaining cached message bytes. */
XMEMSET(sipHash->cache + sipHash->cacheCnt, 0, 7 - sipHash->cacheCnt);
sipHash->cache[7] = (byte)(sipHash->inCnt + sipHash->cacheCnt);

View File

@ -34980,6 +34980,7 @@ WOLFSSL_TEST_SUBROUTINE int siphash_test(void)
int i;
#if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
unsigned char res[SIPHASH_MAC_SIZE_16];
unsigned char tmp[SIPHASH_MAC_SIZE_8];
SipHash siphash;
for (i = 0; i < 64; i++) {
@ -35071,6 +35072,31 @@ WOLFSSL_TEST_SUBROUTINE int siphash_test(void)
if (ret != BAD_FUNC_ARG)
return -13315;
/* Test cache with multiple non blocksize bytes */
ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
if (ret != 0)
return -13316;
ret = wc_SipHashUpdate(&siphash, siphash_msg, 5);
if (ret != 0)
return -13317;
ret = wc_SipHashUpdate(&siphash, siphash_msg + 5, 4);
if (ret != 0)
return -13318;
ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
if (ret != 0)
return -13319;
ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
if (ret != 0)
return -13320;
ret = wc_SipHashUpdate(&siphash, siphash_msg, 9);
if (ret != 0)
return -13321;
ret = wc_SipHashFinal(&siphash, tmp, SIPHASH_MAC_SIZE_8);
if (ret != 0)
return -13322;
if (XMEMCMP(res, tmp, SIPHASH_MAC_SIZE_8) != 0)
return -13323;
return 0;
}