From 5ff4c5af715d098852d124de116d354ee10f4ea4 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 11 Feb 2019 16:22:00 +0100 Subject: Split most .c files into one per function and flatten file hierarchy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libkeccak_hmac_fast_update.c | 51 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 libkeccak_hmac_fast_update.c (limited to 'libkeccak_hmac_fast_update.c') diff --git a/libkeccak_hmac_fast_update.c b/libkeccak_hmac_fast_update.c new file mode 100644 index 0000000..d4a3fbe --- /dev/null +++ b/libkeccak_hmac_fast_update.c @@ -0,0 +1,51 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Absorb more, or the first part, of the message + * without wiping sensitive data when possible + * + * @param state The hashing state + * @param msg The partial message + * @param msglen The length of the partial message, in bytes + * @return Zero on success, -1 on error + */ +int +libkeccak_hmac_fast_update(libkeccak_hmac_state_t *restrict state, const void *restrict msg_, size_t msglen) +{ + const char *restrict msg = msg_; + char *old; + size_t i; + int n, cn; + + if (state->key_ipad) { + if (libkeccak_fast_update(&state->sponge, state->key_ipad, state->key_length >> 3) < 0) + return -1; + if (state->key_length & 7) + state->leftover = state->key_ipad[state->key_length >> 3]; + state->key_ipad = NULL; + } + + if (!msg || !msglen) + return 0; + + if (!(state->key_length & 7)) + return libkeccak_fast_update(&state->sponge, msg, msglen); + + if (msglen != state->buffer_size) { + state->buffer = realloc(old = state->buffer, msglen); + if (!state->buffer) + return state->buffer = old, -1; + state->buffer_size = msglen; + } + + n = (int)(state->key_length & 7); + cn = 8 - n; + for (i = 1; i < msglen; i++) + state->buffer[i] = (char)((msg[i - 1] >> cn) | (msg[i] << n)); + state->buffer[0] = (char)((state->leftover & ((1 << n) - 1)) | (msg[0] << n)); + state->leftover = (char)((unsigned char)msg[msglen - 1] >> cn); + + return libkeccak_fast_update(&state->sponge, state->buffer, msglen); +} -- cgit v1.2.3-70-g09d2