From 9d4bd0942daca7b705d73de7341834444567b457 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 10 Feb 2019 17:20:40 +0100 Subject: Add libsha2_hmac_[un]marshal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- hmac_marshal.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 hmac_marshal.c (limited to 'hmac_marshal.c') 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; +} -- cgit v1.2.3-70-g09d2