aboutsummaryrefslogtreecommitdiffstats
path: root/libkeccak/files.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2017-10-14 01:01:14 +0200
committerMattias Andrée <maandree@kth.se>2017-10-14 01:01:14 +0200
commit3e1864aa14a33a3c917537a241f6a032cfcacf78 (patch)
tree25297b1363fa88c9f45b5102afa5e95d08e986c1 /libkeccak/files.c
parentChange style and license (diff)
downloadlibkeccak-3e1864aa14a33a3c917537a241f6a032cfcacf78.tar.gz
libkeccak-3e1864aa14a33a3c917537a241f6a032cfcacf78.tar.bz2
libkeccak-3e1864aa14a33a3c917537a241f6a032cfcacf78.tar.xz
General improvements
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'libkeccak/files.c')
-rw-r--r--libkeccak/files.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/libkeccak/files.c b/libkeccak/files.c
new file mode 100644
index 0000000..22d12f3
--- /dev/null
+++ b/libkeccak/files.c
@@ -0,0 +1,57 @@
+/* See LICENSE file for copyright and license details. */
+#include "files.h"
+
+#include <sys/stat.h>
+#include <alloca.h>
+#include <errno.h>
+#include <stddef.h>
+#include <unistd.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 (memory leak otherwise)
+ * @param spec Specifications for the hashing algorithm
+ * @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, libkeccak_state_t *restrict state,
+ const libkeccak_spec_t *restrict spec,
+ const char *restrict suffix, char *restrict hashsum)
+{
+ ssize_t got;
+ struct stat attr;
+ size_t blksize = 4096;
+ char *restrict chunk;
+
+ if (libkeccak_state_initialise(state, spec) < 0)
+ return -1;
+
+ if (fstat(fd, &attr) == 0)
+ if (attr.st_blksize > 0)
+ blksize = (size_t)(attr.st_blksize);
+
+ chunk = alloca(blksize);
+
+ for (;;) {
+ got = read(fd, chunk, blksize);
+ if (got < 0) {
+ if (errno == EINTR)
+ continue;
+ return -1;
+ }
+ if (got == 0)
+ break;
+ if (libkeccak_fast_update(state, chunk, (size_t)got) < 0)
+ return -1;
+ }
+
+ return libkeccak_fast_digest(state, NULL, 0, 0, suffix, hashsum);
+}