aboutsummaryrefslogtreecommitdiffstats
path: root/libkeccak_hmac_set_key.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2019-02-11 16:22:00 +0100
committerMattias Andrée <maandree@kth.se>2019-02-11 16:22:00 +0100
commit5ff4c5af715d098852d124de116d354ee10f4ea4 (patch)
tree5789ad5798f2dbf21d9406a2942e48b222f773ae /libkeccak_hmac_set_key.c
parentRemove old file (diff)
downloadlibkeccak-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 'libkeccak_hmac_set_key.c')
-rw-r--r--libkeccak_hmac_set_key.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/libkeccak_hmac_set_key.c b/libkeccak_hmac_set_key.c
new file mode 100644
index 0000000..4f450f8
--- /dev/null
+++ b/libkeccak_hmac_set_key.c
@@ -0,0 +1,46 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+/**
+ * Change the HMAC-hashing key on the state
+ *
+ * @param state The state that should be reset
+ * @param key The new key
+ * @param key_length The length of key, in bits
+ * @return Zero on success, -1 on error
+ */
+int
+libkeccak_hmac_set_key(libkeccak_hmac_state_t *restrict state, const void *restrict key, size_t key_length)
+{
+ size_t i, size, new_key_length, key_bytes;
+ char *old;
+
+ size = (size_t)(state->sponge.r) > key_length ? (size_t)(state->sponge.r) : key_length;
+ new_key_length = size;
+ size = (size + 7) >> 3;
+ key_bytes = (key_length + 7) >> 3;
+
+ if (size != key_bytes) {
+ state->key_opad = realloc(old = state->key_opad, 2 * size);
+ if (!state->key_opad)
+ return state->key_opad = old, -1;
+ state->key_ipad = state->key_opad + size / sizeof(char);
+ }
+
+ memcpy(state->key_opad, key, key_bytes);
+ if (key_length & 7)
+ state->key_opad[(key_bytes >> 3) - 1] &= (char)((1 << (key_length & 7)) - 1);
+
+ if ((size_t)(state->sponge.r) > key_length)
+ __builtin_memset(state->key_opad + key_bytes / sizeof(char), 0, size - key_bytes);
+
+ for (i = 0; i < size; i++) {
+ state->key_ipad[i] = state->key_opad[i] ^ HMAC_INNER_PAD;
+ state->key_opad[i] ^= HMAC_OUTER_PAD;
+ }
+
+ state->key_length = new_key_length;
+
+ return 0;
+}