diff options
author | Mattias Andrée <maandree@kth.se> | 2019-02-11 16:22:00 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2019-02-11 16:22:00 +0100 |
commit | 5ff4c5af715d098852d124de116d354ee10f4ea4 (patch) | |
tree | 5789ad5798f2dbf21d9406a2942e48b222f773ae /libkeccak_hmac_fast_digest.c | |
parent | Remove old file (diff) | |
download | libkeccak-5ff4c5af715d098852d124de116d354ee10f4ea4.tar.gz libkeccak-5ff4c5af715d098852d124de116d354ee10f4ea4.tar.bz2 libkeccak-5ff4c5af715d098852d124de116d354ee10f4ea4.tar.xz |
Split most .c files into one per function and flatten file hierarchy
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | libkeccak_hmac_fast_digest.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/libkeccak_hmac_fast_digest.c b/libkeccak_hmac_fast_digest.c new file mode 100644 index 0000000..d4bacb8 --- /dev/null +++ b/libkeccak_hmac_fast_digest.c @@ -0,0 +1,78 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Absorb the last part of the message and fetch the hash + * without wiping sensitive data when possible + * + * You may use `&state->sponge` for continued squeezing + * + * @param state The hashing state + * @param msg The rest of the message, may be `NULL`, may be modified + * @param msglen The length of the partial message + * @param bits The number of bits at the end of the message not covered by `msglen` + * @param suffix The suffix concatenate to the message, only '1':s and '0':s, and NUL-termination + * @param hashsum Output parameter for the hashsum, may be `NULL` + * @return Zero on success, -1 on error + */ +int +libkeccak_hmac_fast_digest(libkeccak_hmac_state_t *restrict state, const void *restrict msg_, size_t msglen, + size_t bits, const char *restrict suffix, void *restrict hashsum) +{ + const char *restrict msg = msg_; + size_t hashsize = (size_t)state->sponge.n >> 3; + char *tmp = malloc((size_t)((state->sponge.n + 7) >> 3) * sizeof(char)); + char leftover[2]; + size_t newlen; + + if (!tmp) + return -1; + + if (!(state->key_length & 7)) { + if (libkeccak_fast_digest(&state->sponge, msg, msglen, bits, suffix, tmp) < 0) + goto fail; + goto stage_2; + } + + if (libkeccak_hmac_fast_update(state, msg, msglen) < 0) + goto fail; + leftover[0] = state->leftover; + if (bits) { + leftover[0] |= (char)(msg[msglen] >> (state->key_length & 7)); + leftover[1] = (char)((unsigned char)msg[msglen] << (8 - (state->key_length & 7))); + } + newlen = (state->key_length & 7) + bits; + if (libkeccak_fast_digest(&state->sponge, leftover, newlen >> 3, newlen & 7, suffix, tmp) < 0) + goto fail; + +stage_2: + bits = state->sponge.n & 7; + state->key_ipad = state->key_opad; + if (libkeccak_hmac_fast_update(state, NULL, 0) < 0) + goto fail; + + if (!(state->key_length & 7)) { + if (libkeccak_fast_digest(&state->sponge, tmp, hashsize, bits, suffix, hashsum) < 0) + goto fail; + goto stage_3; + } + + if (libkeccak_hmac_fast_update(state, tmp, hashsize) < 0) + goto fail; + leftover[0] = state->leftover; + if (bits) { + leftover[0] |= (char)(tmp[hashsize] >> (state->key_length & 7)); + leftover[1] = (char)((unsigned char)tmp[hashsize] << (8 - (state->key_length & 7))); + } + newlen = (state->key_length & 7) + bits; + if (libkeccak_fast_digest(&state->sponge, leftover, newlen >> 3, newlen & 7, suffix, tmp) < 0) + goto fail; + +stage_3: + free(tmp); + return 0; +fail: + free(tmp); + return -1; +} |