From d1acc40f9361cf5d1f0e92a0a2569b518b29b1cf Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 31 Aug 2024 07:41:27 +0200 Subject: Add support for extended hash + add support for output hash to custom buffer when supported MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libhashsum_init_keccak__.c | 49 +++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 14 deletions(-) (limited to 'libhashsum_init_keccak__.c') diff --git a/libhashsum_init_keccak__.c b/libhashsum_init_keccak__.c index 03dd466..ee146bc 100644 --- a/libhashsum_init_keccak__.c +++ b/libhashsum_init_keccak__.c @@ -3,6 +3,26 @@ #ifdef LIBHASHSUM_INCLUDE_LIBKECCAK_STATE +LIBHASHSUM_1_NONNULL_ +static void +stretch(struct libhashsum_hasher *this, int skip, void *buffer) +{ + if (skip) { + libkeccak_fast_squeeze(&this->state.keccak.s, 1); + return; + } + + if (buffer) + this->hash_output = NULL; + else if (this->hash_size > sizeof(this->state.keccak.sum.buf)) + this->hash_output = this->state.keccak.sum.dyn; + else + this->hash_output = this->state.keccak.sum.buf; + + libkeccak_squeeze(&this->state.keccak.s, buffer ? buffer : this->hash_output); +} + + LIBHASHSUM_1_NONNULL_ static size_t process(struct libhashsum_hasher *this, const void *data, size_t bytes) @@ -29,15 +49,15 @@ finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, u m = &m[r]; bytes -= r; - if (this->hash_size > sizeof(this->state.keccak.sum.buf)) - this->hash_output = this->state.keccak.sum.dyn; - else - this->hash_output = this->state.keccak.sum.buf; + if (!this->hash_output) { + if (this->hash_size > sizeof(this->state.keccak.sum.buf)) + this->hash_output = this->state.keccak.sum.dyn; + else + this->hash_output = this->state.keccak.sum.buf; + } libkeccak_digest(&this->state.keccak.s, m, bytes, extra_bits, this->state.keccak.suffix, this->hash_output); libkeccak_state_wipe_message(&this->state.keccak.s); - libkeccak_state_fast_destroy(&this->state.keccak.s); - memset(&this->state.keccak.s, 0, sizeof(this->state.keccak.s)); - this->state.keccak.s.M = NULL; + this->stretch = &stretch; return 0; } @@ -68,10 +88,12 @@ finalise(struct libhashsum_hasher *this, void *data, size_t bytes, unsigned extr need += this->input_block_size; } - if (this->hash_size > sizeof(this->state.keccak.sum.buf)) - this->hash_output = this->state.keccak.sum.dyn; - else - this->hash_output = this->state.keccak.sum.buf; + if (!this->hash_output) { + if (this->hash_size > sizeof(this->state.keccak.sum.buf)) + this->hash_output = this->state.keccak.sum.dyn; + else + this->hash_output = this->state.keccak.sum.buf; + } if (size < need) libkeccak_digest(&this->state.keccak.s, m, bytes, extra_bits, this->state.keccak.suffix, this->state.keccak.squeezes > 1U ? NULL : this->hash_output); @@ -90,9 +112,7 @@ finalise(struct libhashsum_hasher *this, void *data, size_t bytes, unsigned extr libkeccak_squeeze(&this->state.keccak.s, this->hash_output); libkeccak_state_wipe_message(&this->state.keccak.s); - libkeccak_state_fast_destroy(&this->state.keccak.s); - memset(&this->state.keccak.s, 0, sizeof(this->state.keccak.s)); - this->state.keccak.s.M = NULL; + this->stretch = &stretch; return 0; } @@ -142,6 +162,7 @@ libhashsum_init_keccak__(struct libhashsum_hasher *this, size_t hashbits, void * this->process = &process; this->finalise_const = &finalise_const; this->finalise = &finalise; + this->stretch = NULL; this->destroy = &destroy; return 0; } -- cgit v1.2.3-70-g09d2