diff options
author | Samuel Neves <sneves@dei.uc.pt> | 2017-11-22 21:10:59 +0000 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2017-11-22 22:55:50 +0100 |
commit | ee4722ce71b58c51dd357248d185d4dd4d9ba067 (patch) | |
tree | 74d11c722673b21d4196bcc27245204a935bb28e /src/crypto/blake2s.c | |
parent | 4edb9051d9da9236df766a30516eacd03730592d (diff) |
blake2s: hmac space optimization
Signed-off-by: Samuel Neves <sneves@dei.uc.pt>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'src/crypto/blake2s.c')
-rw-r--r-- | src/crypto/blake2s.c | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/src/crypto/blake2s.c b/src/crypto/blake2s.c index 3e13277..d0a121a 100644 --- a/src/crypto/blake2s.c +++ b/src/crypto/blake2s.c @@ -257,39 +257,35 @@ void __blake2s_final(struct blake2s_state *state) void blake2s_hmac(u8 *out, const u8 *in, const u8 *key, const size_t outlen, const size_t inlen, const size_t keylen) { struct blake2s_state state; - u8 o_key[BLAKE2S_BLOCKBYTES] __aligned(__alignof__(u32)) = { 0 }; - u8 i_key[BLAKE2S_BLOCKBYTES] __aligned(__alignof__(u32)) = { 0 }; + u8 x_key[BLAKE2S_BLOCKBYTES] __aligned(__alignof__(u32)) = { 0 }; u8 i_hash[BLAKE2S_OUTBYTES] __aligned(__alignof__(u32)); int i; if (keylen > BLAKE2S_BLOCKBYTES) { blake2s_init(&state, BLAKE2S_OUTBYTES); blake2s_update(&state, key, keylen); - blake2s_final(&state, o_key, BLAKE2S_OUTBYTES); - memcpy(i_key, o_key, BLAKE2S_OUTBYTES); - } else { - memcpy(o_key, key, keylen); - memcpy(i_key, key, keylen); - } + blake2s_final(&state, x_key, BLAKE2S_OUTBYTES); + } else + memcpy(x_key, key, keylen); - for (i = 0; i < BLAKE2S_BLOCKBYTES; ++i) { - o_key[i] ^= 0x5c; - i_key[i] ^= 0x36; - } + for (i = 0; i < BLAKE2S_BLOCKBYTES; ++i) + x_key[i] ^= 0x36; blake2s_init(&state, BLAKE2S_OUTBYTES); - blake2s_update(&state, i_key, BLAKE2S_BLOCKBYTES); + blake2s_update(&state, x_key, BLAKE2S_BLOCKBYTES); blake2s_update(&state, in, inlen); blake2s_final(&state, i_hash, BLAKE2S_OUTBYTES); + for (i = 0; i < BLAKE2S_BLOCKBYTES; ++i) + x_key[i] ^= 0x5c ^ 0x36; + blake2s_init(&state, BLAKE2S_OUTBYTES); - blake2s_update(&state, o_key, BLAKE2S_BLOCKBYTES); + blake2s_update(&state, x_key, BLAKE2S_BLOCKBYTES); blake2s_update(&state, i_hash, BLAKE2S_OUTBYTES); blake2s_final(&state, i_hash, BLAKE2S_OUTBYTES); memcpy(out, i_hash, outlen); - memzero_explicit(o_key, BLAKE2S_BLOCKBYTES); - memzero_explicit(i_key, BLAKE2S_BLOCKBYTES); + memzero_explicit(x_key, BLAKE2S_BLOCKBYTES); memzero_explicit(i_hash, BLAKE2S_OUTBYTES); } |