diff options
author | Mattias Andrée <maandree@kth.se> | 2024-08-31 07:41:27 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2024-08-31 07:41:27 +0200 |
commit | d1acc40f9361cf5d1f0e92a0a2569b518b29b1cf (patch) | |
tree | a0538a97e06b8783f07eeed5cc3d686e3a8a686f /libhashsum_init_keccak__.c | |
parent | Add more tests + m fixes (diff) | |
download | libhashsum-d1acc40f9361cf5d1f0e92a0a2569b518b29b1cf.tar.gz libhashsum-d1acc40f9361cf5d1f0e92a0a2569b518b29b1cf.tar.bz2 libhashsum-d1acc40f9361cf5d1f0e92a0a2569b518b29b1cf.tar.xz |
Add support for extended hash + add support for output hash to custom buffer when supported
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | libhashsum_init_keccak__.c | 49 |
1 files changed, 35 insertions, 14 deletions
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 @@ -4,6 +4,26 @@ 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; } |