diff options
-rw-r--r-- | include/ouroboros/sha3.h | 6 | ||||
-rw-r--r-- | src/lib/sha3.c | 4 | ||||
-rw-r--r-- | src/lib/tests/sha3_test.c | 37 |
3 files changed, 43 insertions, 4 deletions
diff --git a/include/ouroboros/sha3.h b/include/ouroboros/sha3.h index 17888870..2357b4aa 100644 --- a/include/ouroboros/sha3.h +++ b/include/ouroboros/sha3.h @@ -44,8 +44,8 @@ * or FITNESS FOR A PARTICULAR PURPOSE. Use this program at your own risk! */ -#ifndef OUROBOROS_SHA3_H -#define OUROBOROS_SHA3_H +#ifndef OUROBOROS_LIB_SHA3_H +#define OUROBOROS_LIB_SHA3_H #include <unistd.h> #include <stdint.h> @@ -83,4 +83,4 @@ void rhash_sha3_update(struct sha3_ctx * ctx, void rhash_sha3_final(struct sha3_ctx * ctx, uint8_t * res); -#endif /* OUROBOROS_SHA3_H */ +#endif /* OUROBOROS_LIB_SHA3_H */ diff --git a/src/lib/sha3.c b/src/lib/sha3.c index f80cc4bd..2c8a4614 100644 --- a/src/lib/sha3.c +++ b/src/lib/sha3.c @@ -268,7 +268,9 @@ void rhash_sha3_update(struct sha3_ctx * ctx, size_t block_size = (size_t) ctx->block_size; uint8_t * msg = (uint8_t *) pmsg; - if (ctx->rest & SHA3_FINALIZED) return; + if (ctx->rest & SHA3_FINALIZED) + return; + ctx->rest = (unsigned) ((ctx->rest + size) % block_size); /* fill partial block */ diff --git a/src/lib/tests/sha3_test.c b/src/lib/tests/sha3_test.c index 8f1bce05..0d0e8d0a 100644 --- a/src/lib/tests/sha3_test.c +++ b/src/lib/tests/sha3_test.c @@ -149,6 +149,12 @@ int sha3_test(int argc, "ijklmnopjklmnopqklmnopqrlmnopqrs" "mnopqrstnopqrstu"; + char * str4_inp2 = + " abcdefghbcdefghicdefghijdefghijk" + "efghijklfghijklmghijklmnhijklmno" + "ijklmnopjklmnopqklmnopqrlmnopqrs" + "mnopqrstnopqrstu"; + char * str4_224 = "543e6868e1666c1a643630df77367ae5" "a62a85070a51c14cbf665cbc"; @@ -292,5 +298,36 @@ int sha3_test(int argc, if (check_hash(str4_512, res, SHA3_512_HASH_LEN)) return -1; + /* unaligned 4th input string. */ + printf("test: %s.\n\n", str4_inp2 + 1); + + rhash_sha3_224_init(&ctx); + rhash_sha3_update(&ctx, str4_inp2 + 1, strlen(str4_inp2 + 1)); + rhash_sha3_final(&ctx, res); + + if (check_hash(str4_224, res, SHA3_224_HASH_LEN)) + return -1; + + rhash_sha3_256_init(&ctx); + rhash_sha3_update(&ctx, str4_inp2 + 1, strlen(str4_inp2 + 1)); + rhash_sha3_final(&ctx, res); + + if (check_hash(str4_256, res, SHA3_256_HASH_LEN)) + return -1; + + rhash_sha3_384_init(&ctx); + rhash_sha3_update(&ctx, str4_inp2 + 1, strlen(str4_inp2 + 1)); + rhash_sha3_final(&ctx, res); + + if (check_hash(str4_384, res, SHA3_384_HASH_LEN)) + return -1; + + rhash_sha3_512_init(&ctx); + rhash_sha3_update(&ctx, str4_inp2 + 1, strlen(str4_inp2 + 1)); + rhash_sha3_final(&ctx, res); + + if (check_hash(str4_512, res, SHA3_512_HASH_LEN)) + return -1; + return 0; } |