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_unmarshal.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 hmac_unmarshal.c (limited to 'hmac_unmarshal.c') 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; +} -- cgit v1.2.3-70-g09d2