aboutsummaryrefslogtreecommitdiffstats
path: root/hmac_marshal.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2019-02-10 17:20:40 +0100
committerMattias Andrée <maandree@kth.se>2019-02-10 17:20:40 +0100
commit9d4bd0942daca7b705d73de7341834444567b457 (patch)
treeed217ffaf0babdd8fb9031ef0b042eb2817c82dc /hmac_marshal.c
parentAdd HMAC and use void * instead of char * for binary data (diff)
downloadlibsha2-9d4bd0942daca7b705d73de7341834444567b457.tar.gz
libsha2-9d4bd0942daca7b705d73de7341834444567b457.tar.bz2
libsha2-9d4bd0942daca7b705d73de7341834444567b457.tar.xz
Add libsha2_hmac_[un]marshal
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'hmac_marshal.c')
-rw-r--r--hmac_marshal.c41
1 files changed, 41 insertions, 0 deletions
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;
+}