aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/libkeccak.h1
-rw-r--r--src/libkeccak/files.c68
-rw-r--r--src/libkeccak/files.h124
3 files changed, 193 insertions, 0 deletions
diff --git a/src/libkeccak.h b/src/libkeccak.h
index cd401c5..fa564c4 100644
--- a/src/libkeccak.h
+++ b/src/libkeccak.h
@@ -25,6 +25,7 @@
#include "libkeccak/state.h"
#include "libkeccak/digest.h"
#include "libkeccak/hex.h"
+#include "libkeccak/files.h"
#undef
diff --git a/src/libkeccak/files.c b/src/libkeccak/files.c
new file mode 100644
index 0000000..5374620
--- /dev/null
+++ b/src/libkeccak/files.c
@@ -0,0 +1,68 @@
+/**
+ * libkeccak – Keccak-family hashing library
+ *
+ * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "files.h"
+
+
+#include <stddef.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <alloca.h>
+
+
+/**
+ * Calculate a Keccak-family hashsum of a file
+ *
+ * @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 / 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* chunk;
+
+ if (libkeccak_state_initialise(state, spec) < 0)
+ return -1;
+
+ if (fstat(fd, &attr) == 0)
+ if (attr.st_blksize > 0)
+ blksize = attr.st_blksize;
+
+ chunk = alloca(blksize);
+
+ for (;;)
+ {
+ got = read(fd, chunk, blksize);
+ if (got < 0) return -1;
+ if (got == 0) break;
+ if (libkeccak_update(state, chunk, (size_t)got) < 0)
+ return -1;
+ }
+
+ return libkeccak_digest(state, NULL, 0, 0, suffix, hashsum);
+}
+
diff --git a/src/libkeccak/files.h b/src/libkeccak/files.h
new file mode 100644
index 0000000..8283c0e
--- /dev/null
+++ b/src/libkeccak/files.h
@@ -0,0 +1,124 @@
+/**
+ * libkeccak – Keccak-family hashing library
+ *
+ * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef LIBKECCAK_FILES_H
+#define LIBKECCAK_FILES_H 1
+
+
+#include "../libkeccak.h"
+
+
+/**
+ * Calculate a Keccak-family hashsum of a file
+ *
+ * @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 / 8) * sizeof(char)`, may be `NULL`
+ * @return Zero on success, -1 on error
+ */
+__attribute__((nonnull(2, 3)))
+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);
+
+
+/**
+ * Calculate the Keccak hashsum of a file
+ *
+ * @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 hashsum Output array for the hashsum, have an allocation size of
+ * at least `(spec->output / 8) * sizeof(char)`, may be `NULL`
+ * @return Zero on success, -1 on error
+ */
+static inline __attribute__((nonnull(2, 3), artificial, gnu_inline))
+int libkeccak_keccaksum_fd(int fd, libkeccak_state_t* restrict state,
+ const libkeccak_spec_t* restrict spec, char* restrict hashsum)
+{
+ return libkeccak_generalised_sum_fd(fd, spec, NULL, hashsum);
+}
+
+
+/**
+ * Calculate the SHA3 hashsum of a file
+ *
+ * @param fd The file descriptor of the file to hash
+ * @param state The hashing state, should not be initialised (memory leak otherwise)
+ * @param output The output size parameter for the hashing algorithm
+ * @param hashsum Output array for the hashsum, have an allocation size of
+ * at least `(output / 8) * sizeof(char)`, may be `NULL`
+ * @return Zero on success, -1 on error
+ */
+static inline __attribute__((nonnull(2), artificial, gnu_inline))
+int libkeccak_sha3sum_fd(int fd, libkeccak_state_t* restrict state,
+ long output, char* restrict hashsum)
+{
+ libkeccak_spec_t spec;
+ libkeccak_spec_sha3(&spec, output);
+ return libkeccak_generalised_sum_fd(fd, &spec, LIBKECCAK_SHA3_SUFFIX, hashsum);
+}
+
+
+/**
+ * Calculate the RawSHAKE hashsum of a file
+ *
+ * @param fd The file descriptor of the file to hash
+ * @param state The hashing state, should not be initialised (memory leak otherwise)
+ * @param semicapacity The semicapacity parameter for the hashing algorithm
+ * @param output The output size parameter for the hashing algorithm
+ * @param hashsum Output array for the hashsum, have an allocation size of
+ * at least `(output / 8) * sizeof(char)`, may be `NULL`
+ * @return Zero on success, -1 on error
+ */
+static inline __attribute__((nonnull(2), artificial, gnu_inline))
+int libkeccak_rawshakesum_fd(int fd, libkeccak_state_t* restrict state,
+ long semicapacity, long output, char* restrict hashsum)
+{
+ libkeccak_spec_t spec;
+ libkeccak_spec_rawshake(&spec, semicapacity, output);
+ return libkeccak_generalised_sum_fd(fd, &spec, LIBKECCAK_RAWSHAKE_SUFFIX, hashsum);
+}
+
+
+/**
+ * Calculate the SHAKE hashsum of a file
+ *
+ * @param fd The file descriptor of the file to hash
+ * @param state The hashing state, should not be initialised (memory leak otherwise)
+ * @param semicapacity The semicapacity parameter for the hashing algorithm
+ * @param output The output size parameter for the hashing algorithm
+ * @param hashsum Output array for the hashsum, have an allocation size of
+ * at least `(output / 8) * sizeof(char)`, may be `NULL`
+ * @return Zero on success, -1 on error
+ */
+static inline __attribute__((nonnull(2), artificial, gnu_inline))
+int libkeccak_shakesum_fd(int fd, libkeccak_state_t* restrict state,
+ long semicapacity, long output, char* restrict hashsum)
+{
+ libkeccak_spec_t spec;
+ libkeccak_spec_shake(&spec, semicapacity, output);
+ return libkeccak_generalised_sum_fd(fd, &spec, LIBKECCAK_SHAKE_SUFFIX, hashsum);
+}
+
+
+#undef
+