From 058eba2d2171e38813d6ad722333ee58668d35b1 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Mon, 4 Sep 2023 15:40:05 -0400 Subject: sha3.[hc]: add sha3_{224,256,384,512}_{init,absorb,final}() and test_sha3_{224,256,384,512}_ctx() --- sha3.h | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 125 insertions(+), 2 deletions(-) (limited to 'sha3.h') diff --git a/sha3.h b/sha3.h index 9e2a015..003d5b5 100644 --- a/sha3.h +++ b/sha3.h @@ -11,13 +11,20 @@ extern "C" { #include -// sha3 state +// Internal sha3 state. typedef union { uint8_t u8[200]; uint64_t u64[25]; } sha3_state_t; -// Internal XOF state +// Iterative SHA-3 context (all members are private). +typedef struct { + size_t num_bytes; // number of bytes absorbed + sha3_state_t a; // internal state + _Bool finalized; // mode (absorbing or finalized) +} sha3_t; + +// Iterative XOF context (all members are private). typedef struct { size_t num_bytes; // number of bytes absorbed sha3_state_t a; // internal state @@ -35,6 +42,35 @@ typedef struct { */ void sha3_224(const uint8_t *m, size_t m_len, uint8_t dst[static 28]); +/** + * Initialize SHA3-224 hash context. + * + * @param[out] hash SHA3-224 hash context. + */ +void sha3_224_init(sha3_t *hash); + +/** + * Absorb input data in `src` of length `len` bytes into SHA3-224 hash + * context `hash`. Can be called iteratively to absorb input data in + * chunks. + * + * @param[in/out] hash SHA3-224 hash context. + * @param[in] src Input data. + * @param[in] len Input data length, in bytes. + * + * @return True if data was absorbed, and false otherwise (e.g., if context has already been finalized). + */ +_Bool sha3_224_absorb(sha3_t *hash, const uint8_t *src, const size_t len); + +/** + * Finalize SHA3-224 hash context and write 28 bytes of output to + * destination buffer `dst`. + * + * @param[in/out] hash SHA3-224 hash context. + * @param[out] dst Destination buffer. Must be at least 28 bytes in length. + */ +void sha3_224_final(sha3_t *hash, uint8_t dst[28]); + /** * Hash input message in input buffer `m` of length `m_len` bytes with * SHA3-256 (FIPS 202, section 6.1) and write 32 bytes of output to @@ -46,6 +82,35 @@ void sha3_224(const uint8_t *m, size_t m_len, uint8_t dst[static 28]); */ void sha3_256(const uint8_t *m, size_t m_len, uint8_t dst[static 32]); +/** + * Initialize SHA3-256 hash context. + * + * @param[out] hash SHA3-256 hash context. + */ +void sha3_256_init(sha3_t *hash); + +/** + * Absorb input data in `src` of length `len` bytes into SHA3-256 hash + * context `hash`. Can be called iteratively to absorb input data in + * chunks. + * + * @param[in/out] hash SHA3-256 hash context. + * @param[in] src Input data. + * @param[in] len Input data length, in bytes. + * + * @return True if data was absorbed, and false otherwise (e.g., if context has already been finalized). + */ +_Bool sha3_256_absorb(sha3_t *hash, const uint8_t *src, const size_t len); + +/** + * Finalize SHA3-256 hash context and write 28 bytes of output to + * destination buffer `dst`. + * + * @param[in/out] hash SHA3-256 hash context. + * @param[out] dst Destination buffer. Must be at least 32 bytes in length. + */ +void sha3_256_final(sha3_t *hash, uint8_t dst[32]); + /** * Hash input message in input buffer `m` of length `m_len` bytes with * SHA3-384 (FIPS 202, section 6.1) and write 48 bytes of output to @@ -57,6 +122,35 @@ void sha3_256(const uint8_t *m, size_t m_len, uint8_t dst[static 32]); */ void sha3_384(const uint8_t *m, size_t m_len, uint8_t dst[static 48]); +/** + * Initialize SHA3-384 hash context. + * + * @param[out] hash SHA3-384 hash context. + */ +void sha3_384_init(sha3_t *hash); + +/** + * Absorb input data in `src` of length `len` bytes into SHA3-384 hash + * context `hash`. Can be called iteratively to absorb input data in + * chunks. + * + * @param[in/out] hash SHA3-384 hash context. + * @param[in] src Input data. + * @param[in] len Input data length, in bytes. + * + * @return True if data was absorbed, and false otherwise (e.g., if context has already been finalized). + */ +_Bool sha3_384_absorb(sha3_t *hash, const uint8_t *src, const size_t len); + +/** + * Finalize SHA3-384 hash context and write 28 bytes of output to + * destination buffer `dst`. + * + * @param[in/out] hash SHA3-384 hash context. + * @param[out] dst Destination buffer. Must be at least 48 bytes in length. + */ +void sha3_384_final(sha3_t *hash, uint8_t dst[48]); + /** * Hash input message in input buffer `m` of length `m_len` bytes with * SHA3-512 (FIPS 202, section 6.1) and write 64 bytes of output to @@ -68,6 +162,35 @@ void sha3_384(const uint8_t *m, size_t m_len, uint8_t dst[static 48]); */ void sha3_512(const uint8_t *m, size_t m_len, uint8_t dst[static 64]); +/** + * Initialize SHA3-512 hash context. + * + * @param[out] hash SHA3-512 hash context. + */ +void sha3_512_init(sha3_t *hash); + +/** + * Absorb input data in `src` of length `len` bytes into SHA3-512 hash + * context `hash`. Can be called iteratively to absorb input data in + * chunks. + * + * @param[in/out] hash SHA3-512 hash context. + * @param[in] src Input data. + * @param[in] len Input data length, in bytes. + * + * @return True if data was absorbed, and false otherwise (e.g., if context has already been finalized). + */ +_Bool sha3_512_absorb(sha3_t *hash, const uint8_t *src, const size_t len); + +/** + * Finalize SHA3-512 hash context and write 28 bytes of output to + * destination buffer `dst`. + * + * @param[in/out] hash SHA3-512 hash context. + * @param[out] dst Destination buffer. Must be at least 64 bytes in length. + */ +void sha3_512_final(sha3_t *hash, uint8_t dst[64]); + /** * Hash input message in buffer `m` of length `m_len` bytes with * SHAKE128 (FIPS 202, section 6.2) and write 16 bytes of output to -- cgit v1.2.3