aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2019-02-10 17:10:20 +0100
committerMattias Andrée <maandree@kth.se>2019-02-10 17:11:30 +0100
commit32a5ae4e65844615cb3e32aaefcdb7abe4af54c9 (patch)
tree0ecfa02766944f2568b184d8d5a8ba0edc71425f
parentUse lowest bits rather than highest bits in complete byte, document this, and add tests (diff)
downloadlibsha2-32a5ae4e65844615cb3e32aaefcdb7abe4af54c9.tar.gz
libsha2-32a5ae4e65844615cb3e32aaefcdb7abe4af54c9.tar.bz2
libsha2-32a5ae4e65844615cb3e32aaefcdb7abe4af54c9.tar.xz
Add HMAC and use void * instead of char * for binary data
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--Makefile3
-rw-r--r--TODO1
-rw-r--r--behex_lower.c3
-rw-r--r--behex_upper.c3
-rw-r--r--digest.c31
-rw-r--r--hmac_digest.c37
-rw-r--r--hmac_init.c45
-rw-r--r--hmac_update.c26
-rw-r--r--libsha2.h112
-rw-r--r--libsha2.h.016
-rw-r--r--libsha2_behex_lower.32
-rw-r--r--libsha2_behex_upper.32
-rw-r--r--libsha2_digest.32
-rw-r--r--libsha2_marshal.32
-rw-r--r--libsha2_sum_fd.32
-rw-r--r--libsha2_unhex.32
-rw-r--r--libsha2_unmarshal.32
-rw-r--r--libsha2_update.32
-rw-r--r--marshal.c3
-rw-r--r--sum_fd.c2
-rw-r--r--test.c8
-rw-r--r--unhex.c15
-rw-r--r--unmarshal.c3
-rw-r--r--update.c3
24 files changed, 266 insertions, 61 deletions
diff --git a/Makefile b/Makefile
index 838e391..2df18be 100644
--- a/Makefile
+++ b/Makefile
@@ -18,6 +18,9 @@ OBJ =\
behex_lower.o\
behex_upper.o\
digest.o\
+ hmac_digest.o\
+ hmac_init.o\
+ hmac_update.o\
init.o\
marshal.o\
process.o\
diff --git a/TODO b/TODO
deleted file mode 100644
index 0087301..0000000
--- a/TODO
+++ /dev/null
@@ -1 +0,0 @@
-Add HMAC support
diff --git a/behex_lower.c b/behex_lower.c
index 476bb18..6fdf474 100644
--- a/behex_lower.c
+++ b/behex_lower.c
@@ -10,8 +10,9 @@
* @param n The size of `hashsum`
*/
void
-libsha2_behex_lower(char *restrict output, const char *restrict hashsum, size_t n)
+libsha2_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];
diff --git a/behex_upper.c b/behex_upper.c
index 29e5f91..cfcee75 100644
--- a/behex_upper.c
+++ b/behex_upper.c
@@ -10,8 +10,9 @@
* @param n The size of `hashsum`
*/
void
-libsha2_behex_upper(char *restrict output, const char *restrict hashsum, size_t n)
+libsha2_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];
diff --git a/digest.c b/digest.c
index 0b4f474..59bb680 100644
--- a/digest.c
+++ b/digest.c
@@ -11,8 +11,10 @@
* @param output The output buffer for the hash
*/
void
-libsha2_digest(struct libsha2_state *restrict state, const char *message, size_t msglen, char *output)
+libsha2_digest(struct libsha2_state *restrict state, const void *message_, size_t msglen, void *output_)
{
+ const char *message = message_;
+ unsigned char *output = output_;
size_t off, i, n;
if (msglen & ~(size_t)7) {
@@ -23,8 +25,7 @@ libsha2_digest(struct libsha2_state *restrict state, const char *message, size_t
off = (state->message_size / 8) % state->chunk_size;
if (msglen) {
- state->chunk[off] = (unsigned char)*message;
- state->chunk[off] <<= 8 - msglen;
+ state->chunk[off] = (unsigned char)(*message << (8 - (int)msglen));
state->chunk[off] |= (unsigned char)(1 << (7 - msglen));
state->chunk[off] &= (unsigned char)~((1 << (7 - msglen)) - 1);
state->message_size += msglen;
@@ -53,21 +54,21 @@ libsha2_digest(struct libsha2_state *restrict state, const char *message, size_t
n = libsha2_algorithm_output_size(state->algorithm);
if (state->algorithm <= LIBSHA2_256) {
for (i = 0, n /= 4; i < n; i++) {
- output[4 * i + 0] = (char)(state->h.b32[i] >> 24);
- output[4 * i + 1] = (char)(state->h.b32[i] >> 16);
- output[4 * i + 2] = (char)(state->h.b32[i] >> 8);
- output[4 * i + 3] = (char)(state->h.b32[i] >> 0);
+ output[4 * i + 0] = (unsigned char)(state->h.b32[i] >> 24);
+ output[4 * i + 1] = (unsigned char)(state->h.b32[i] >> 16);
+ output[4 * i + 2] = (unsigned char)(state->h.b32[i] >> 8);
+ output[4 * i + 3] = (unsigned char)(state->h.b32[i] >> 0);
}
} else {
for (i = 0, n = (n + 7) / 8; i < n; i++) {
- output[8 * i + 0] = (char)(state->h.b64[i] >> 56);
- output[8 * i + 1] = (char)(state->h.b64[i] >> 48);
- output[8 * i + 2] = (char)(state->h.b64[i] >> 40);
- output[8 * i + 3] = (char)(state->h.b64[i] >> 32);
- output[8 * i + 4] = (char)(state->h.b64[i] >> 24);
- output[8 * i + 5] = (char)(state->h.b64[i] >> 16);
- output[8 * i + 6] = (char)(state->h.b64[i] >> 8);
- output[8 * i + 7] = (char)(state->h.b64[i] >> 0);
+ output[8 * i + 0] = (unsigned char)(state->h.b64[i] >> 56);
+ output[8 * i + 1] = (unsigned char)(state->h.b64[i] >> 48);
+ output[8 * i + 2] = (unsigned char)(state->h.b64[i] >> 40);
+ output[8 * i + 3] = (unsigned char)(state->h.b64[i] >> 32);
+ output[8 * i + 4] = (unsigned char)(state->h.b64[i] >> 24);
+ output[8 * i + 5] = (unsigned char)(state->h.b64[i] >> 16);
+ output[8 * i + 6] = (unsigned char)(state->h.b64[i] >> 8);
+ output[8 * i + 7] = (unsigned char)(state->h.b64[i] >> 0);
}
}
}
diff --git a/hmac_digest.c b/hmac_digest.c
new file mode 100644
index 0000000..caee756
--- /dev/null
+++ b/hmac_digest.c
@@ -0,0 +1,37 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+/**
+ * Feed data into the HMAC algorithm and
+ * get the result
+ *
+ * The state of the algorithm will be reset and
+ * `libsha2_hmac_update` and `libsha2_hmac_update`
+ * can be called again
+ *
+ * @param state The state of the algorithm
+ * @param data Data to feed into the algorithm
+ * @param n The number of bytes to feed into the algorithm
+ * @param output The output buffer for the hash, it will be as
+ * large as for the underlaying hash algorithm
+ * @return Zero on success, -1 on error
+ */
+int
+libsha2_hmac_digest(struct libsha2_hmac_state *restrict state, const void *data, size_t n, void *output)
+{
+ if (!state->inited) {
+ if (libsha2_init(&state->sha2_state, state->sha2_state.algorithm))
+ return -1;
+ libsha2_update(&state->sha2_state, state->ipad, state->sha2_state.chunk_size * 8);
+ }
+
+ libsha2_digest(&state->sha2_state, data, n, output);
+ if (libsha2_init(&state->sha2_state, state->sha2_state.algorithm))
+ return -1;
+
+ libsha2_update(&state->sha2_state, state->opad, state->sha2_state.chunk_size * 8);
+ libsha2_digest(&state->sha2_state, output, state->outsize, output);
+ state->inited = 0;
+ return 0;
+}
diff --git a/hmac_init.c b/hmac_init.c
new file mode 100644
index 0000000..4aee2c2
--- /dev/null
+++ b/hmac_init.c
@@ -0,0 +1,45 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+/**
+ * Initialise an HMAC state
+ *
+ * @param state The state that should be initialised
+ * @param algorithm The hashing algorithm
+ * @param key The key
+ * @param key_length The length of key, in bits
+ * @return Zero on success, -1 on error
+ */
+int
+libsha2_hmac_init(struct libsha2_hmac_state *restrict state, enum libsha2_algorithm algorithm,
+ const void *restrict key_, size_t keylen)
+{
+ const unsigned char *restrict key = key_;
+ size_t i;
+
+ state->sha2_state.algorithm = algorithm;
+ state->outsize = libsha2_algorithm_output_size(algorithm) * 8;
+ state->inited = 0;
+
+ if (keylen <= state->sha2_state.chunk_size * 8) {
+ memset(state->ipad, 0x36, sizeof(state->ipad));
+ memset(state->opad, 0x5C, sizeof(state->opad));
+ for (i = 0, keylen /= 8; i < keylen; i++) {
+ state->ipad[i] ^= key[i];
+ state->opad[i] ^= key[i];
+ }
+ } else {
+ memset(state->ipad, 0, sizeof(state->ipad));
+ if (libsha2_init(&state->sha2_state, algorithm))
+ return -1;
+ libsha2_digest(&state->sha2_state, key, keylen, state->ipad);
+ memcpy(state->opad, state->ipad, sizeof(state->ipad));
+ for (i = 0; i < sizeof(state->ipad); i++) {
+ state->ipad[i] ^= 0x36;
+ state->opad[i] ^= 0x5C;
+ }
+ }
+
+ return 0;
+}
diff --git a/hmac_update.c b/hmac_update.c
new file mode 100644
index 0000000..7f4cef3
--- /dev/null
+++ b/hmac_update.c
@@ -0,0 +1,26 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+/**
+ * Feed data into the HMAC algorithm
+ *
+ * @param state The state of the algorithm
+ * @param data Data to feed into the algorithm
+ * @param n The number of bytes to feed into the
+ * algorithm, this must be a multiple of 8
+ * @return Zero on success, -1 on error
+ */
+int
+libsha2_hmac_update(struct libsha2_hmac_state *restrict state, const void *restrict data, size_t n)
+{
+ if (!state->inited) {
+ if (libsha2_init(&state->sha2_state, state->sha2_state.algorithm))
+ return -1;
+ libsha2_update(&state->sha2_state, state->ipad, state->sha2_state.chunk_size * 8);
+ state->inited = 1;
+ }
+
+ libsha2_update(&state->sha2_state, data, n);
+ return 0;
+}
diff --git a/libsha2.h b/libsha2.h
index 106e3dd..d4d52bf 100644
--- a/libsha2.h
+++ b/libsha2.h
@@ -141,10 +141,50 @@ struct libsha2_state {
/**
+ * Data structure that describes the state of a HMAC hashing process
+ *
+ * Data that could just as well be allocated (with `auto`) are
+ * allocated here so that is is easier to wipe the data without
+ * exposing two versions of each function: one to wipe data,
+ * and one not to wipe data to gain speed, now you can use use
+ * `explicit_bzero` (or `memset`) when you are done.
+ */
+struct libsha2_hmac_state {
+ /**
+ * State of the underlaying hash function
+ */
+ struct libsha2_state sha2_state;
+
+ /**
+ * The output size of the underlaying
+ * hash algorithm, in bits
+ */
+ size_t outsize;
+
+ /**
+ * Whether `.sha2_state` has been initialised
+ * and whether the `ipad` has been feed into
+ * the algorithm
+ */
+ unsigned char inited;
+
+ /**
+ * Inner pad XOR processed key
+ */
+ unsigned char ipad[128];
+
+ /**
+ * Outer pad XOR processed key
+ */
+ unsigned char opad[128];
+};
+
+
+/**
* Initialise a state
*
- * @parma state The state that should be initialised
- * @parma algorithm The hashing algorithm
+ * @param state The state that should be initialised
+ * @param algorithm The hashing algorithm
* @return Zero on success, -1 on error
*/
#if defined(__GNUC__)
@@ -155,7 +195,7 @@ int libsha2_init(struct libsha2_state *restrict, enum libsha2_algorithm);
/**
* Get the output size of the algorithm specified for a state
*
- * @parma state The state
+ * @param state The state
* @return The number of bytes in the output, zero on error
*/
#if defined(__GNUC__)
@@ -166,7 +206,7 @@ size_t libsha2_state_output_size(const struct libsha2_state *restrict);
/**
* Get the output size of an algorithm
*
- * @parma algorithm The hashing algorithm
+ * @param algorithm The hashing algorithm
* @return The number of bytes in the output, zero on error
*/
#if defined(__GNUC__)
@@ -184,7 +224,7 @@ size_t libsha2_algorithm_output_size(enum libsha2_algorithm);
#if defined(__GNUC__)
__attribute__((__nonnull__, __nothrow__))
#endif
-void libsha2_update(struct libsha2_state *restrict, const char *restrict, size_t);
+void libsha2_update(struct libsha2_state *restrict, const void *restrict, size_t);
/**
* Absorb the last part of the message and output a hash
@@ -197,7 +237,7 @@ void libsha2_update(struct libsha2_state *restrict, const char *restrict, size_t
#if defined(__GNUC__)
__attribute__((__nonnull__(1, 4), __nothrow__))
#endif
-void libsha2_digest(struct libsha2_state *restrict, const char *, size_t, char *);
+void libsha2_digest(struct libsha2_state *restrict, const void *, size_t, void *);
/**
* Calculate the checksum for a file,
@@ -211,7 +251,7 @@ void libsha2_digest(struct libsha2_state *restrict, const char *, size_t, char *
#if defined(__GNUC__)
__attribute__((__nonnull__, __leaf__))
#endif
-int libsha2_sum_fd(int, enum libsha2_algorithm, char *restrict);
+int libsha2_sum_fd(int, enum libsha2_algorithm, void *restrict);
/**
* Convert a binary hashsum to lower case hexadecimal representation
@@ -223,7 +263,7 @@ int libsha2_sum_fd(int, enum libsha2_algorithm, char *restrict);
#if defined(__GNUC__)
__attribute__((__leaf__, __nonnull__, __nothrow__))
#endif
-void libsha2_behex_lower(char *restrict, const char *restrict, size_t);
+void libsha2_behex_lower(char *restrict, const void *restrict, size_t);
/**
* Convert a binary hashsum to upper case hexadecimal representation
@@ -235,7 +275,7 @@ void libsha2_behex_lower(char *restrict, const char *restrict, size_t);
#if defined(__GNUC__)
__attribute__((__leaf__, __nonnull__, __nothrow__))
#endif
-void libsha2_behex_upper(char *restrict, const char *restrict, size_t);
+void libsha2_behex_upper(char *restrict, const void *restrict, size_t);
/**
* Convert a hexadecimal hashsum (both lower case, upper
@@ -248,7 +288,7 @@ void libsha2_behex_upper(char *restrict, const char *restrict, size_t);
#if defined(__GNUC__)
__attribute__((__leaf__, __nonnull__, __nothrow__))
#endif
-void libsha2_unhex(char *restrict, const char *restrict);
+void libsha2_unhex(void *restrict, const char *restrict);
/**
* Marshal a state into a buffer
@@ -260,7 +300,7 @@ void libsha2_unhex(char *restrict, const char *restrict);
#if defined(__GNUC__)
__attribute__((__leaf__, __nonnull__(1), __nothrow__))
#endif
-size_t libsha2_marshal(const struct libsha2_state *restrict, char *restrict);
+size_t libsha2_marshal(const struct libsha2_state *restrict, void *restrict);
/**
* Unmarshal a state from a buffer
@@ -273,7 +313,55 @@ size_t libsha2_marshal(const struct libsha2_state *restrict, char *restrict);
#if defined(__GNUC__)
__attribute__((__leaf__, __nonnull__, __nothrow__))
#endif
-size_t libsha2_unmarshal(struct libsha2_state *restrict, const char *restrict, size_t);
+size_t libsha2_unmarshal(struct libsha2_state *restrict, const void *restrict, size_t);
+
+/**
+ * Initialise an HMAC state
+ *
+ * @param state The state that should be initialised
+ * @param algorithm The hashing algorithm
+ * @param key The key
+ * @param key_length The length of key, in bits
+ * @return Zero on success, -1 on error
+ */
+#if defined(__GNUC__)
+__attribute__((__leaf__, __nonnull__, __nothrow__))
+#endif
+int libsha2_hmac_init(struct libsha2_hmac_state *restrict, enum libsha2_algorithm, const void *restrict, size_t);
+
+/**
+ * Feed data into the HMAC algorithm
+ *
+ * @param state The state of the algorithm
+ * @param data Data to feed into the algorithm
+ * @param n The number of bytes to feed into the
+ * algorithm, this must be a multiple of 8
+ * @return Zero on success, -1 on error
+ */
+#if defined(__GNUC__)
+__attribute__((__leaf__, __nonnull__, __nothrow__))
+#endif
+int libsha2_hmac_update(struct libsha2_hmac_state *restrict, const void *restrict, size_t);
+
+/**
+ * Feed data into the HMAC algorithm and
+ * get the result
+ *
+ * The state of the algorithm will be reset and
+ * `libsha2_hmac_update` and `libsha2_hmac_update`
+ * can be called again
+ *
+ * @param state The state of the algorithm
+ * @param data Data to feed into the algorithm
+ * @param n The number of bytes to feed into the algorithm
+ * @param output The output buffer for the hash, it will be as
+ * large as for the underlaying hash algorithm
+ * @return Zero on success, -1 on error
+ */
+#if defined(__GNUC__)
+__attribute__((__leaf__, __nonnull__, __nothrow__))
+#endif
+int libsha2_hmac_digest(struct libsha2_hmac_state *restrict, const void *, size_t, void *);
#endif
diff --git a/libsha2.h.0 b/libsha2.h.0
index a12c6ad..2ef80ac 100644
--- a/libsha2.h.0
+++ b/libsha2.h.0
@@ -21,14 +21,14 @@ struct libsha2_state {
int libsha2_init(struct libsha2_state *restrict \fIstate\fP, enum libsha2_algorithm \fIalgorithm\fP);
size_t libsha2_state_output_size(const struct libsha2_state *restrict \fIstate\fP);
size_t libsha2_algorithm_output_size(enum libsha2_algorithm \fIalgorithm\fP);
-void libsha2_update(struct libsha2_state *restrict \fIstate\fP, const char *restrict \fImessage\fP, size_t \fImsglen\fP);
-void libsha2_digest(struct libsha2_state *restrict \fIstate\fP, const char *restrict \fImessage\fP, size_t \fImsglen\fP, char *\fIoutput\fP);
-int libsha2_sum_fd(int \fIfd\fP, enum libsha2_algorithm \fIalgorithm\fP, char *restrict \fIhashsum\fP);
-void libsha2_behex_lower(char *restrict \fIoutput\fP, const char *restrict \fIhashsum\fP, size_t \fIn\fP);
-void libsha2_behex_upper(char *restrict \fIoutput\fP, const char *restrict \fIhashsum\fP, size_t \fIn\fP);
-void libsha2_unhex(char *restrict \fIoutput\fP, const char *restrict \fIhashsum\fP);
-size_t libsha2_marshal(const struct libsha2_state *restrict \fIstate\fP, char *restrict \fIbuf\fP);
-size_t libsha2_unmarshal(struct libsha2_state *restrict \fIstate\fP, const char *restrict \fIbuf\fP, size_t \fIbufsize\fP);
+void libsha2_update(struct libsha2_state *restrict \fIstate\fP, const void *restrict \fImessage\fP, size_t \fImsglen\fP);
+void libsha2_digest(struct libsha2_state *restrict \fIstate\fP, const void *restrict \fImessage\fP, size_t \fImsglen\fP, void *\fIoutput\fP);
+int libsha2_sum_fd(int \fIfd\fP, enum libsha2_algorithm \fIalgorithm\fP, void *restrict \fIhashsum\fP);
+void libsha2_behex_lower(char *restrict \fIoutput\fP, const void *restrict \fIhashsum\fP, size_t \fIn\fP);
+void libsha2_behex_upper(char *restrict \fIoutput\fP, const void *restrict \fIhashsum\fP, size_t \fIn\fP);
+void libsha2_unhex(void *restrict \fIoutput\fP, const char *restrict \fIhashsum\fP);
+size_t libsha2_marshal(const struct libsha2_state *restrict \fIstate\fP, void *restrict \fIbuf\fP);
+size_t libsha2_unmarshal(struct libsha2_state *restrict \fIstate\fP, const void *restrict \fIbuf\fP, size_t \fIbufsize\fP);
.fi
.PP
Link with
diff --git a/libsha2_behex_lower.3 b/libsha2_behex_lower.3
index 7882aa7..7d2a1f1 100644
--- a/libsha2_behex_lower.3
+++ b/libsha2_behex_lower.3
@@ -5,7 +5,7 @@ libsha2_behex_lower \- Convert binary to lower case hexadecimal
.nf
#include <libsha2.h>
-void libsha2_behex_lower(char *restrict \fIoutput\fP, const char *restrict \fIhashsum\fP, size_t \fIn\fP);
+void libsha2_behex_lower(char *restrict \fIoutput\fP, const void *restrict \fIhashsum\fP, size_t \fIn\fP);
.fi
.PP
Link with
diff --git a/libsha2_behex_upper.3 b/libsha2_behex_upper.3
index 6faaae2..3a0c2ba 100644
--- a/libsha2_behex_upper.3
+++ b/libsha2_behex_upper.3
@@ -5,7 +5,7 @@ libsha2_behex_upper \- Convert binary to upper case hexadecimal
.nf
#include <libsha2.h>
-void libsha2_behex_upper(char *restrict \fIoutput\fP, const char *restrict \fIhashsum\fP, size_t \fIn\fP);
+void libsha2_behex_upper(char *restrict \fIoutput\fP, const void *restrict \fIhashsum\fP, size_t \fIn\fP);
.fi
.PP
Link with
diff --git a/libsha2_digest.3 b/libsha2_digest.3
index dac48b4..37d1199 100644
--- a/libsha2_digest.3
+++ b/libsha2_digest.3
@@ -5,7 +5,7 @@ libsha2_digest \- Get the result of a SHA 2 hashing
.nf
#include <libsha2.h>
-void libsha2_digest(struct libsha2_state *restrict \fIstate\fP, const char *\fImessage\fP, size_t \fImsglen\fP, char *\fIoutput\fP);
+void libsha2_digest(struct libsha2_state *restrict \fIstate\fP, const void *\fImessage\fP, size_t \fImsglen\fP, void *\fIoutput\fP);
.fi
.PP
Link with
diff --git a/libsha2_marshal.3 b/libsha2_marshal.3
index 3742ca6..f6d1e4d 100644
--- a/libsha2_marshal.3
+++ b/libsha2_marshal.3
@@ -5,7 +5,7 @@ libsha2_marshal \- Marshal a SHA 2 hashing state
.nf
#include <libsha2.h>
-size_t libsha2_marshal(const struct libsha2_state *restrict \fIstate\fP, char *restrict \fIbuf\fP);
+size_t libsha2_marshal(const struct libsha2_state *restrict \fIstate\fP, void *restrict \fIbuf\fP);
.fi
.PP
Link with
diff --git a/libsha2_sum_fd.3 b/libsha2_sum_fd.3
index 82bff15..8f243c3 100644
--- a/libsha2_sum_fd.3
+++ b/libsha2_sum_fd.3
@@ -14,7 +14,7 @@ enum libsha2_algorithm {
LIBSHA2_512_256 /* SHA-512/256 */
};
-int libsha2_sum_fd(int \fIfd\fP, enum libsha2_algorithm \fIalgorithm\fP, char *restrict \fIhashsum\fP);
+int libsha2_sum_fd(int \fIfd\fP, enum libsha2_algorithm \fIalgorithm\fP, void *restrict \fIhashsum\fP);
.fi
.PP
Link with
diff --git a/libsha2_unhex.3 b/libsha2_unhex.3
index c1f7865..1c646bf 100644
--- a/libsha2_unhex.3
+++ b/libsha2_unhex.3
@@ -5,7 +5,7 @@ libsha2_unhex \- Covert hexadecimal to binary
.nf
#include <libsha2.h>
-void libsha2_unhex(char *restrict \fIoutput\fP, const char *restrict \fIhashsum\fP);
+void libsha2_unhex(void *restrict \fIoutput\fP, const char *restrict \fIhashsum\fP);
.fi
.PP
Link with
diff --git a/libsha2_unmarshal.3 b/libsha2_unmarshal.3
index 52fa2d4..3707cc8 100644
--- a/libsha2_unmarshal.3
+++ b/libsha2_unmarshal.3
@@ -5,7 +5,7 @@ libsha2_unmarshal \- Unmarshal a SHA 2 hashing state
.nf
#include <libsha2.h>
-size_t libsha2_unmarshal(struct libsha2_state *restrict \fIstate\fP, const char *restrict \fIbuf\fP, size_t \fIbufsize\fP);
+size_t libsha2_unmarshal(struct libsha2_state *restrict \fIstate\fP, const void *restrict \fIbuf\fP, size_t \fIbufsize\fP);
.fi
.PP
Link with
diff --git a/libsha2_update.3 b/libsha2_update.3
index fe82384..fd6124d 100644
--- a/libsha2_update.3
+++ b/libsha2_update.3
@@ -5,7 +5,7 @@ libsha2_update \- Feed data into a SHA 2 algorithm
.nf
#include <libsha2.h>
-void libsha2_update(struct libsha2_state *restrict \fIstate\fP, const char *restrict \fImessage\fP, size_t \fImsglen\fP);
+void libsha2_update(struct libsha2_state *restrict \fIstate\fP, const void *restrict \fImessage\fP, size_t \fImsglen\fP);
.fi
.PP
Link with
diff --git a/marshal.c b/marshal.c
index ed0cce5..ba00af2 100644
--- a/marshal.c
+++ b/marshal.c
@@ -10,8 +10,9 @@
* @return The number of bytes marshalled to `buf`
*/
size_t
-libsha2_marshal(const struct libsha2_state *restrict state, char *restrict buf)
+libsha2_marshal(const struct libsha2_state *restrict state, void *restrict buf_)
{
+ char *restrict buf = buf_;
size_t off = 0;
if (buf)
diff --git a/sum_fd.c b/sum_fd.c
index 888e423..f93d20c 100644
--- a/sum_fd.c
+++ b/sum_fd.c
@@ -12,7 +12,7 @@
* @return Zero on success, -1 on error
*/
int
-libsha2_sum_fd(int fd, enum libsha2_algorithm algorithm, char *restrict hashsum)
+libsha2_sum_fd(int fd, enum libsha2_algorithm algorithm, void *restrict hashsum)
{
struct libsha2_state state;
ssize_t r;
diff --git a/test.c b/test.c
index 6cd664e..87da91f 100644
--- a/test.c
+++ b/test.c
@@ -36,17 +36,17 @@
#define test_repeated_huge(CHR, N, ALGO, EXPECTED)\
do {\
- size_t n = N;\
+ size_t n__ = N;\
if (skip_huge)\
break;\
memset(buf, CHR, sizeof(buf));\
test(!libsha2_init(&s, ALGO));\
fprintf(stderr, "processing huge message: 0 %%\n");\
- for (; n > sizeof(buf); n -= sizeof(buf)) {\
+ for (; n__ > sizeof(buf); n__ -= sizeof(buf)) {\
libsha2_update(&s, buf, sizeof(buf) * 8);\
- fprintf(stderr, "\033[A\033[Kprocessing huge message: %zu %%\n", ((N) - n) * 100 / (N));\
+ fprintf(stderr, "\033[A\033[Kprocessing huge message: %zu %%\n", ((N) - n__) * 100 / (N));\
}\
- libsha2_update(&s, buf, n * 8);\
+ libsha2_update(&s, buf, n__ * 8);\
fprintf(stderr, "\033[A\033[K");\
fflush(stderr);\
libsha2_digest(&s, NULL, 0, buf);\
diff --git a/unhex.c b/unhex.c
index a84ac19..1cada1f 100644
--- a/unhex.c
+++ b/unhex.c
@@ -11,17 +11,18 @@
* @param hashsum The hashsum to convert
*/
void
-libsha2_unhex(char *restrict output, const char *restrict hashsum)
+libsha2_unhex(void *restrict output_, const char *restrict hashsum)
{
+ unsigned char *restrict output = output_;
size_t n = strlen(hashsum) / 2;
- char a, b;
+ unsigned char a, b;
while (n--) {
- a = hashsum[2 * n + 0];
- b = hashsum[2 * n + 1];
+ a = ((const unsigned char *)hashsum)[2 * n + 0];
+ b = ((const unsigned char *)hashsum)[2 * n + 1];
- a = (char)((a & 15) + (a > '9' ? 9 : 0));
- b = (char)((b & 15) + (b > '9' ? 9 : 0));
+ a = (unsigned char)((a & 15) + (a > '9' ? 9 : 0));
+ b = (unsigned char)((b & 15) + (b > '9' ? 9 : 0));
- output[n] = (char)((a << 4) | b);
+ output[n] = (unsigned char)((a << 4) | b);
}
}
diff --git a/unmarshal.c b/unmarshal.c
index 909bdc2..2876f31 100644
--- a/unmarshal.c
+++ b/unmarshal.c
@@ -11,8 +11,9 @@
* @return The number of read bytes, 0 on failure
*/
size_t
-libsha2_unmarshal(struct libsha2_state *restrict state, const char *restrict buf, size_t bufsize)
+libsha2_unmarshal(struct libsha2_state *restrict state, const void *restrict buf_, size_t bufsize)
{
+ const char *restrict buf = buf_;
size_t off = 0;
if (bufsize < sizeof(int) + sizeof(enum libsha2_algorithm) + sizeof(size_t)) {
diff --git a/update.c b/update.c
index 94e46f3..dc78248 100644
--- a/update.c
+++ b/update.c
@@ -10,8 +10,9 @@
* @param msglen The length of the message
*/
void
-libsha2_update(struct libsha2_state *restrict state, const char *restrict message, size_t msglen)
+libsha2_update(struct libsha2_state *restrict state, const void *restrict message_, size_t msglen)
{
+ const char *restrict message = message_;
size_t n, off;
off = (state->message_size / 8) % state->chunk_size;