diff options
author | Mattias Andrée <maandree@kth.se> | 2019-02-10 17:10:20 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2019-02-10 17:11:30 +0100 |
commit | 32a5ae4e65844615cb3e32aaefcdb7abe4af54c9 (patch) | |
tree | 0ecfa02766944f2568b184d8d5a8ba0edc71425f /hmac_digest.c | |
parent | Use lowest bits rather than highest bits in complete byte, document this, and add tests (diff) | |
download | libsha2-32a5ae4e65844615cb3e32aaefcdb7abe4af54c9.tar.gz libsha2-32a5ae4e65844615cb3e32aaefcdb7abe4af54c9.tar.bz2 libsha2-32a5ae4e65844615cb3e32aaefcdb7abe4af54c9.tar.xz |
Add HMAC and use void * instead of char * for binary data
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | hmac_digest.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/hmac_digest.c b/hmac_digest.c new file mode 100644 index 0000000..caee756 --- /dev/null +++ b/hmac_digest.c @@ -0,0 +1,37 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Feed data into the HMAC algorithm and + * get the result + * + * The state of the algorithm will be reset and + * `libsha2_hmac_update` and `libsha2_hmac_update` + * can be called again + * + * @param state The state of the algorithm + * @param data Data to feed into the algorithm + * @param n The number of bytes to feed into the algorithm + * @param output The output buffer for the hash, it will be as + * large as for the underlaying hash algorithm + * @return Zero on success, -1 on error + */ +int +libsha2_hmac_digest(struct libsha2_hmac_state *restrict state, const void *data, size_t n, void *output) +{ + if (!state->inited) { + if (libsha2_init(&state->sha2_state, state->sha2_state.algorithm)) + return -1; + libsha2_update(&state->sha2_state, state->ipad, state->sha2_state.chunk_size * 8); + } + + libsha2_digest(&state->sha2_state, data, n, output); + if (libsha2_init(&state->sha2_state, state->sha2_state.algorithm)) + return -1; + + libsha2_update(&state->sha2_state, state->opad, state->sha2_state.chunk_size * 8); + libsha2_digest(&state->sha2_state, output, state->outsize, output); + state->inited = 0; + return 0; +} |