diff options
author | Mattias Andrée <maandree@kth.se> | 2019-02-10 20:21:19 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2019-02-10 20:21:19 +0100 |
commit | ed0296b9055713df0d910e4e7528ffe6fc539514 (patch) | |
tree | 8cbf8ecc9b6352257d6bc4946ff75cb8a4b484c0 /hmac_unmarshal.c | |
download | libsha1-ed0296b9055713df0d910e4e7528ffe6fc539514.tar.gz libsha1-ed0296b9055713df0d910e4e7528ffe6fc539514.tar.bz2 libsha1-ed0296b9055713df0d910e4e7528ffe6fc539514.tar.xz |
First commit
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'hmac_unmarshal.c')
-rw-r--r-- | hmac_unmarshal.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/hmac_unmarshal.c b/hmac_unmarshal.c new file mode 100644 index 0000000..8db73c8 --- /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 +libsha1_hmac_unmarshal(struct libsha1_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 = libsha1_unmarshal(&state->sha1_state, &buf[off], bufsize - off); + if (!r) + return 0; + off += r; + + if (bufsize - off < sizeof(size_t) + sizeof(unsigned char) + 2 * state->sha1_state.chunk_size) { + errno = EINVAL; + return 0; + } + + state->outsize = *(const size_t *)&buf[off]; + off += sizeof(size_t); + + state->inited = *(const unsigned char *)&buf[off]; + off += sizeof(unsigned char); + + memcpy(state->ipad, &buf[off], state->sha1_state.chunk_size); + off += state->sha1_state.chunk_size; + + memcpy(state->opad, &buf[off], state->sha1_state.chunk_size); + off += state->sha1_state.chunk_size; + + return off; +} |