diff --git a/wolfcrypt/src/siphash.c b/wolfcrypt/src/siphash.c index 45e137cc7..30947ab21 100644 --- a/wolfcrypt/src/siphash.c +++ b/wolfcrypt/src/siphash.c @@ -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); diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 5f67ae98d..08185e391 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -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; }