From 32b3a978091f2c6b318d7cebaa192b08321ead7f Mon Sep 17 00:00:00 2001 From: Leon Date: Mon, 10 Jun 2019 20:40:49 +0200 Subject: [PATCH] add sha3_384 --- common/fips202.c | 43 +++++++++++++++++++++++++++++++++++++++++++ common/fips202.h | 12 ++++++++++++ 2 files changed, 55 insertions(+) diff --git a/common/fips202.c b/common/fips202.c index 0fb29fab..794e25a0 100644 --- a/common/fips202.c +++ b/common/fips202.c @@ -718,6 +718,49 @@ void sha3_256(uint8_t *output, const uint8_t *input, size_t inlen) { } } +void sha3_384_inc_init(sha3_384incctx *state) { + keccak_inc_init(state->ctx); +} + +void sha3_384_inc_absorb(sha3_384incctx *state, const uint8_t *input, size_t inlen) { + keccak_inc_absorb(state->ctx, SHA3_384_RATE, input, inlen); +} + +void sha3_384_inc_finalize(uint8_t *output, sha3_384incctx *state) { + uint8_t t[SHA3_384_RATE]; + keccak_inc_finalize(state->ctx, SHA3_384_RATE, 0x06); + + keccak_squeezeblocks(t, 1, state->ctx, SHA3_384_RATE); + + for (size_t i = 0; i < 48; i++) { + output[i] = t[i]; + } +} + +/************************************************* + * Name: sha3_384 + * + * Description: SHA3-256 with non-incremental API + * + * Arguments: - uint8_t *output: pointer to output + * - const uint8_t *input: pointer to input + * - size_t inlen: length of input in bytes + **************************************************/ +void sha3_384(uint8_t *output, const uint8_t *input, size_t inlen) { + uint64_t s[25]; + uint8_t t[SHA3_384_RATE]; + + /* Absorb input */ + keccak_absorb(s, SHA3_384_RATE, input, inlen, 0x06); + + /* Squeeze output */ + keccak_squeezeblocks(t, 1, s, SHA3_384_RATE); + + for (size_t i = 0; i < 48; i++) { + output[i] = t[i]; + } +} + void sha3_512_inc_init(sha3_512incctx *state) { keccak_inc_init(state->ctx); } diff --git a/common/fips202.h b/common/fips202.h index 622d736a..5d62b284 100644 --- a/common/fips202.h +++ b/common/fips202.h @@ -7,6 +7,7 @@ #define SHAKE128_RATE 168 #define SHAKE256_RATE 136 #define SHA3_256_RATE 136 +#define SHA3_384_RATE 104 #define SHA3_512_RATE 72 @@ -35,6 +36,11 @@ typedef struct { uint64_t ctx[26]; } sha3_256incctx; +// Context for incremental API +typedef struct { + uint64_t ctx[26]; +} sha3_384incctx; + // Context for incremental API typedef struct { uint64_t ctx[26]; @@ -69,6 +75,12 @@ void sha3_256_inc_finalize(uint8_t *output, sha3_256incctx *state); void sha3_256(uint8_t *output, const uint8_t *input, size_t inlen); +void sha3_384_inc_init(sha3_384incctx *state); +void sha3_384_inc_absorb(sha3_384incctx *state, const uint8_t *input, size_t inlen); +void sha3_384_inc_finalize(uint8_t *output, sha3_384incctx *state); + +void sha3_384(uint8_t *output, const uint8_t *input, size_t inlen); + void sha3_512_inc_init(sha3_512incctx *state); void sha3_512_inc_absorb(sha3_512incctx *state, const uint8_t *input, size_t inlen); void sha3_512_inc_finalize(uint8_t *output, sha3_512incctx *state);