aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-09-15 02:15:08 +0200
committerMattias Andrée <maandree@kth.se>2024-09-15 02:15:08 +0200
commitd4ce8327ff902b5ecd42d057063c03793e6d91c2 (patch)
tree7ec1db1573f12225d6f6c324865b1b49a5a9580d /util
parentm (diff)
downloadlibkeccak-d4ce8327ff902b5ecd42d057063c03793e6d91c2.tar.gz
libkeccak-d4ce8327ff902b5ecd42d057063c03793e6d91c2.tar.bz2
libkeccak-d4ce8327ff902b5ecd42d057063c03793e6d91c2.tar.xz
Organise files
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'util')
-rw-r--r--util/libkeccak_behex_lower.c22
-rw-r--r--util/libkeccak_behex_upper.c22
-rw-r--r--util/libkeccak_generalised_sum_fd.c112
-rw-r--r--util/libkeccak_keccaksum_fd.c5
-rw-r--r--util/libkeccak_rawshakesum_fd.c5
-rw-r--r--util/libkeccak_sha3sum_fd.c5
-rw-r--r--util/libkeccak_shakesum_fd.c5
-rw-r--r--util/libkeccak_unhex.c30
8 files changed, 206 insertions, 0 deletions
diff --git a/util/libkeccak_behex_lower.c b/util/libkeccak_behex_lower.c
new file mode 100644
index 0000000..07593e8
--- /dev/null
+++ b/util/libkeccak_behex_lower.c
@@ -0,0 +1,22 @@
+/* See LICENSE file for copyright and license details. */
+#include "../common.h"
+
+
+/**
+ * Convert a binary hashsum to lower case hexadecimal representation
+ *
+ * @param output Output array, should have an allocation size of at least `2 * n + 1`
+ * @param hashsum_ The hashsum to convert
+ * @param n The size of `hashsum`
+ */
+void
+libkeccak_behex_lower(char *restrict output, const void *restrict hashsum_, size_t n)
+{
+ const unsigned char *restrict hashsum = hashsum_;
+
+ output[2 * n] = '\0';
+ while (n--) {
+ output[2 * n + 0] = "0123456789abcdef"[(hashsum[n] >> 4) & 15];
+ output[2 * n + 1] = "0123456789abcdef"[(hashsum[n] >> 0) & 15];
+ }
+}
diff --git a/util/libkeccak_behex_upper.c b/util/libkeccak_behex_upper.c
new file mode 100644
index 0000000..5186a28
--- /dev/null
+++ b/util/libkeccak_behex_upper.c
@@ -0,0 +1,22 @@
+/* See LICENSE file for copyright and license details. */
+#include "../common.h"
+
+
+/**
+ * Convert a binary hashsum to upper case hexadecimal representation
+ *
+ * @param output Output array, should have an allocation size of at least `2 * n + 1`
+ * @param hashsum_ The hashsum to convert
+ * @param n The size of `hashsum`
+ */
+void
+libkeccak_behex_upper(char *restrict output, const void *restrict hashsum_, size_t n)
+{
+ const unsigned char *restrict hashsum = hashsum_;
+
+ output[2 * n] = '\0';
+ while (n--) {
+ output[2 * n + 0] = "0123456789ABCDEF"[(hashsum[n] >> 4) & 15];
+ output[2 * n + 1] = "0123456789ABCDEF"[(hashsum[n] >> 0) & 15];
+ }
+}
diff --git a/util/libkeccak_generalised_sum_fd.c b/util/libkeccak_generalised_sum_fd.c
new file mode 100644
index 0000000..bad35e9
--- /dev/null
+++ b/util/libkeccak_generalised_sum_fd.c
@@ -0,0 +1,112 @@
+/* See LICENSE file for copyright and license details. */
+#include "../common.h"
+#include <stdio.h>
+
+
+/**
+ * Calculate a Keccak-family hashsum of a file,
+ * the content of the file is assumed non-sensitive
+ *
+ * @param fd The file descriptor of the file to hash
+ * @param state The hashing state, should not be initialised unless
+ * `spec` is `NULL` (memory leak otherwise)
+ * @param spec Specifications for the hashing algorithm; or `NULL`
+ * if `spec` is already initialised
+ * @param suffix The data suffix, see `libkeccak_digest`
+ * @param hashsum Output array for the hashsum, have an allocation size of
+ * at least `((spec->output + 7) / 8) * sizeof(char)`, may be `NULL`
+ * @return Zero on success, -1 on error
+ */
+int
+libkeccak_generalised_sum_fd(int fd, struct libkeccak_state *restrict state, const struct libkeccak_spec *restrict spec,
+ const char *restrict suffix, void *restrict hashsum)
+{
+ ssize_t got;
+ size_t offset;
+#ifndef _WIN32
+ struct stat attr;
+#endif
+ size_t blksize = 4096;
+ unsigned char *restrict chunk;
+ size_t chunksize, extrasize, extrachunks;
+ size_t chunks, chunkmod;
+
+ if (spec && libkeccak_state_initialise(state, spec) < 0)
+ return -1;
+
+ chunksize = libkeccak_zerocopy_chunksize(state);
+ extrasize = ((suffix ? strlen(suffix) : 0) + 2 + 7) >> 3;
+ extrachunks = (extrasize + (chunksize - 1)) / chunksize;
+
+#ifndef _WIN32
+ if (fstat(fd, &attr) == 0)
+ if (attr.st_blksize > 0)
+ blksize = (size_t)attr.st_blksize;
+#endif
+
+ chunks = blksize / chunksize;
+ chunkmod = blksize % chunksize;
+ if (chunkmod) {
+ blksize -= chunkmod;
+ blksize += chunksize;
+ chunks += 1;
+ }
+ if (chunks < extrachunks + 1)
+ blksize = (extrachunks + 1) * chunksize;
+
+#if ALLOCA_LIMIT > 0
+ if (blksize > (size_t)ALLOCA_LIMIT) {
+ blksize = (size_t)ALLOCA_LIMIT;
+ blksize -= blksize % chunksize;
+ if (!blksize)
+ blksize = chunksize;
+ }
+# if defined(__clang__)
+ /* We are using a limit so it's just like declaring an array
+ * in a function, except we might use less of the stack. */
+# pragma clang diagnostic push
+# pragma clang diagnostic ignored "-Walloca"
+# endif
+ chunk = alloca(blksize);
+# if defined(__clang__)
+# pragma clang diagnostic pop
+# endif
+#else
+ chunk = malloc(blksize);
+ if (!chunk)
+ return -1;
+#endif
+
+ offset = 0;
+ for (;;) {
+ got = read(fd, &chunk[offset], blksize - offset);
+ if (got <= 0) {
+ if (!got)
+ break;
+ if (errno == EINTR)
+ continue;
+ goto fail;
+ }
+ offset += (size_t)got;
+ if (offset == blksize) {
+ libkeccak_zerocopy_update(state, chunk, blksize);
+ offset = 0;
+ }
+ }
+
+ if (extrasize > blksize - offset) {
+ chunkmod = offset % chunksize;
+ libkeccak_zerocopy_update(state, chunk, offset - chunkmod);
+ __builtin_memcpy(chunk, &chunk[offset - chunkmod], chunkmod * sizeof(char));
+ offset = chunkmod;
+ }
+
+ libkeccak_zerocopy_digest(state, chunk, offset, 0, suffix, hashsum);
+ return 0;
+
+fail:
+#if ALLOCA_LIMIT <= 0
+ free(chunk);
+#endif
+ return -1;
+}
diff --git a/util/libkeccak_keccaksum_fd.c b/util/libkeccak_keccaksum_fd.c
new file mode 100644
index 0000000..560e390
--- /dev/null
+++ b/util/libkeccak_keccaksum_fd.c
@@ -0,0 +1,5 @@
+/* See LICENSE file for copyright and license details. */
+#include "../common.h"
+
+
+extern inline int libkeccak_keccaksum_fd(int, struct libkeccak_state *restrict, const struct libkeccak_spec *restrict, void *restrict);
diff --git a/util/libkeccak_rawshakesum_fd.c b/util/libkeccak_rawshakesum_fd.c
new file mode 100644
index 0000000..c9c66ec
--- /dev/null
+++ b/util/libkeccak_rawshakesum_fd.c
@@ -0,0 +1,5 @@
+/* See LICENSE file for copyright and license details. */
+#include "../common.h"
+
+
+extern inline int libkeccak_rawshakesum_fd(int, struct libkeccak_state *restrict, long, long, void *restrict);
diff --git a/util/libkeccak_sha3sum_fd.c b/util/libkeccak_sha3sum_fd.c
new file mode 100644
index 0000000..43be4b7
--- /dev/null
+++ b/util/libkeccak_sha3sum_fd.c
@@ -0,0 +1,5 @@
+/* See LICENSE file for copyright and license details. */
+#include "../common.h"
+
+
+extern inline int libkeccak_sha3sum_fd(int, struct libkeccak_state *restrict, long, void *restrict);
diff --git a/util/libkeccak_shakesum_fd.c b/util/libkeccak_shakesum_fd.c
new file mode 100644
index 0000000..9bb4b80
--- /dev/null
+++ b/util/libkeccak_shakesum_fd.c
@@ -0,0 +1,5 @@
+/* See LICENSE file for copyright and license details. */
+#include "../common.h"
+
+
+extern inline int libkeccak_shakesum_fd(int, struct libkeccak_state *restrict, long, long, void *restrict);
diff --git a/util/libkeccak_unhex.c b/util/libkeccak_unhex.c
new file mode 100644
index 0000000..b32a3f3
--- /dev/null
+++ b/util/libkeccak_unhex.c
@@ -0,0 +1,30 @@
+/* See LICENSE file for copyright and license details. */
+#include "../common.h"
+
+
+/**
+ * Convert a hexadecimal hashsum (both lower case, upper
+ * case and mixed is supported) to binary representation
+ *
+ * @param output_ Output array, should have an allocation size of at least `strlen(hashsum) / 2`
+ * @param hashsum The hashsum to convert
+ */
+void
+libkeccak_unhex(void *restrict output_, const char *restrict hashsum)
+{
+ unsigned char *restrict output = output_;
+ size_t n = strlen(hashsum) / 2;
+ unsigned char a, b;
+
+ while (n--) {
+ a = (unsigned char)hashsum[2 * n + 0];
+ b = (unsigned char)hashsum[2 * n + 1];
+
+ a = (unsigned char)((a & 15) + (a > '9' ? 9 : 0));
+ b = (unsigned char)((b & 15) + (b > '9' ? 9 : 0));
+
+ a = (unsigned char)(a << 4);
+ a |= b;
+ output[n] = a;
+ }
+}