aboutsummaryrefslogtreecommitdiffstats
path: root/libhashsum_init_blake512_hasher.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-08-24 20:13:01 +0200
committerMattias Andrée <maandree@kth.se>2024-08-24 20:13:01 +0200
commitfebb5279f7bf3c86ec872c1b2ed1e024f73e64c5 (patch)
treeea6918fc1dcb29e11ce9399b8300a124cc0342cf /libhashsum_init_blake512_hasher.c
parentAdd support for Keccak, SHA3, SHAKE, and RawSHAKE via libkeccak>=1.3 (this version introduced zerocopy) (diff)
downloadlibhashsum-febb5279f7bf3c86ec872c1b2ed1e024f73e64c5.tar.gz
libhashsum-febb5279f7bf3c86ec872c1b2ed1e024f73e64c5.tar.bz2
libhashsum-febb5279f7bf3c86ec872c1b2ed1e024f73e64c5.tar.xz
Add BLAKE via libblake>=1.1 (this version introduced libblake_init())
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--libhashsum_init_blake512_hasher.c98
1 files changed, 98 insertions, 0 deletions
diff --git a/libhashsum_init_blake512_hasher.c b/libhashsum_init_blake512_hasher.c
new file mode 100644
index 0000000..b1d2c13
--- /dev/null
+++ b/libhashsum_init_blake512_hasher.c
@@ -0,0 +1,98 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+#ifdef SUPPORT_BLAKE512
+
+
+LIBHASHSUM_1_NONNULL_
+static size_t
+process(struct libhashsum_hasher *this, const void *data, size_t bytes)
+{
+ return libblake_blake512_update(&this->state.blake512.s, data, bytes);
+}
+
+
+LIBHASHSUM_1_NONNULL_
+static int
+finalise_const(struct libhashsum_hasher *this, const void *data, size_t bytes, unsigned extra_bits)
+{
+ const uint8_t *m = data;
+ size_t r;
+
+ if (extra_bits > 7U) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ r = libblake_blake512_update(&this->state.blake512.s, data, bytes);
+ m = &m[r];
+ bytes -= r;
+
+ memcpy(this->state.blake512.buf, m, bytes + (size_t)(extra_bits > 0U));
+ if (extra_bits)
+ this->state.blake512.buf[bytes] = libhashsum_reverse_byte__(this->state.blake512.buf[bytes]);
+ libblake_blake512_digest(&this->state.blake512.s, this->state.blake512.buf, bytes,
+ extra_bits, NULL, this->state.blake512.buf);
+ memset(&this->state.blake512.s, 0, sizeof(this->state.blake512.s));
+ this->hash_output = this->state.blake512.buf;
+ return 0;
+}
+
+
+LIBHASHSUM_1_NONNULL_
+static int
+finalise(struct libhashsum_hasher *this, void *data, size_t bytes, unsigned extra_bits, size_t size)
+{
+ uint8_t *m = data;
+ size_t r;
+
+ if (extra_bits > 7U) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ r = libblake_blake512_update(&this->state.blake512.s, data, bytes);
+ m = &m[r];
+ bytes -= r;
+ size -= r;
+
+ if (size < libblake_blake512_digest_get_required_input_size(bytes, extra_bits, NULL)) {
+ memcpy(this->state.blake512.buf, m, bytes + (size_t)(extra_bits > 0U));
+ m = this->state.blake512.buf;
+ }
+ if (extra_bits)
+ m[bytes] = libhashsum_reverse_byte__(m[bytes]);
+ libblake_blake512_digest(&this->state.blake512.s, m, bytes, extra_bits, NULL, this->state.blake512.buf);
+ memset(&this->state.blake512.s, 0, sizeof(this->state.blake512.s));
+ this->hash_output = this->state.blake512.buf;
+ return 0;
+}
+
+
+int
+libhashsum_init_blake512_hasher(struct libhashsum_hasher *this)
+{
+ libblake_init();
+ this->algorithm = LIBHASHSUM_BLAKE512;
+ this->algorithm_string = "BLAKE512";
+ this->input_block_size = 128U;
+ this->hash_size = LIBHASHSUM_BLAKE512_HASH_SIZE;
+ this->hash_output = NULL;
+ this->supports_non_whole_bytes = 1;
+ this->process = &process;
+ this->finalise_const = &finalise_const;
+ this->finalise = &finalise;
+ this->destroy = NULL;
+ libblake_blake512_init2(&this->state.blake512.s, NULL);
+ return 0;
+}
+
+
+#else
+int
+libhashsum_init_blake512_hasher(struct libhashsum_hasher *this)
+{
+ (void) this;
+ errno = ENOSYS;
+ return -1;
+}
+#endif