aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--hmac_marshal.c41
-rw-r--r--hmac_unmarshal.c54
-rw-r--r--libsha2.h25
4 files changed, 122 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index 2df18be..dc7060e 100644
--- a/Makefile
+++ b/Makefile
@@ -20,6 +20,8 @@ OBJ =\
digest.o\
hmac_digest.o\
hmac_init.o\
+ hmac_marshal.o\
+ hmac_unmarshal.o\
hmac_update.o\
init.o\
marshal.o\
diff --git a/hmac_marshal.c b/hmac_marshal.c
new file mode 100644
index 0000000..2cb0816
--- /dev/null
+++ b/hmac_marshal.c
@@ -0,0 +1,41 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+/**
+ * Marshal an HMAC state into a buffer
+ *
+ * @param state The state to marshal
+ * @param buf Output buffer, `NULL` to only return the required size
+ * @return The number of bytes marshalled to `buf`
+ */
+size_t
+libsha2_hmac_marshal(const struct libsha2_hmac_state *restrict state, void *restrict buf_)
+{
+ char *restrict buf = buf_;
+ size_t off = 0;
+
+ if (buf)
+ *(int *)buf = 0; /* version */
+ off += sizeof(int);
+
+ off += libsha2_marshal(&state->sha2_state, buf ? &buf[off] : NULL);
+
+ if (buf)
+ *(size_t *)&buf[off] = state->outsize;
+ off += sizeof(size_t);
+
+ if (buf)
+ *(unsigned char *)&buf[off] = state->inited;
+ off += sizeof(unsigned char);
+
+ if (buf)
+ memcpy(&buf[off], state->ipad, state->sha2_state.chunk_size);
+ off += state->sha2_state.chunk_size;
+
+ if (buf)
+ memcpy(&buf[off], state->opad, state->sha2_state.chunk_size);
+ off += state->sha2_state.chunk_size;
+
+ return off;
+}
diff --git a/hmac_unmarshal.c b/hmac_unmarshal.c
new file mode 100644
index 0000000..cc62d5c
--- /dev/null
+++ b/hmac_unmarshal.c
@@ -0,0 +1,54 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+/**
+ * Unmarshal an HMAC state from a buffer
+ *
+ * @param state Output parameter for the unmarshalled state
+ * @param buf The buffer from which the state shall be unmarshalled
+ * @param bufsize The maximum number of bytes that can be unmarshalled
+ * @return The number of read bytes, 0 on failure
+ */
+size_t
+libsha2_hmac_unmarshal(struct libsha2_hmac_state *restrict state, const void *restrict buf_, size_t bufsize)
+{
+ const char *restrict buf = buf_;
+ size_t off = 0;
+ size_t r;
+
+ if (bufsize < sizeof(int)) {
+ errno = EINVAL;
+ return 0;
+ }
+
+ if (*(const int *)buf) { /* version */
+ errno = EINVAL;
+ return 0;
+ }
+ off += sizeof(int);
+
+ r = libsha2_unmarshal(&state->sha2_state, &buf[off], bufsize - off);
+ if (!r)
+ return 0;
+ off += r;
+
+ if (bufsize - off < sizeof(size_t) + sizeof(unsigned char) + 2 * state->sha2_state.chunk_size) {
+ errno = EINVAL;
+ return 0;
+ }
+
+ state->outsize = *(size_t *)&buf[off];
+ off += sizeof(size_t);
+
+ state->inited = *(unsigned char *)&buf[off];
+ off += sizeof(unsigned char);
+
+ memcpy(state->ipad, &buf[off], state->sha2_state.chunk_size);
+ off += state->sha2_state.chunk_size;
+
+ memcpy(state->opad, &buf[off], state->sha2_state.chunk_size);
+ off += state->sha2_state.chunk_size;
+
+ return off;
+}
diff --git a/libsha2.h b/libsha2.h
index d4d52bf..21e0b98 100644
--- a/libsha2.h
+++ b/libsha2.h
@@ -363,5 +363,30 @@ __attribute__((__leaf__, __nonnull__, __nothrow__))
#endif
int libsha2_hmac_digest(struct libsha2_hmac_state *restrict, const void *, size_t, void *);
+/**
+ * Marshal an HMAC state into a buffer
+ *
+ * @param state The state to marshal
+ * @param buf Output buffer, `NULL` to only return the required size
+ * @return The number of bytes marshalled to `buf`
+ */
+#if defined(__GNUC__)
+__attribute__((__leaf__, __nonnull__(1), __nothrow__))
+#endif
+size_t libsha2_hmac_marshal(const struct libsha2_hmac_state *restrict, void *restrict);
+
+/**
+ * Unmarshal an HMAC state from a buffer
+ *
+ * @param state Output parameter for the unmarshalled state
+ * @param buf The buffer from which the state shall be unmarshalled
+ * @param bufsize The maximum number of bytes that can be unmarshalled
+ * @return The number of read bytes, 0 on failure
+ */
+#if defined(__GNUC__)
+__attribute__((__leaf__, __nonnull__, __nothrow__))
+#endif
+size_t libsha2_hmac_unmarshal(struct libsha2_hmac_state *restrict, const void *restrict, size_t);
+
#endif