From 0f94c014ef4334f9d792f4af58b108652847e68a Mon Sep 17 00:00:00 2001 From: Loup Vaillant Date: Mon, 8 Jun 2020 17:46:16 +0200 Subject: [PATCH] Optimised key loading in Blake2b The idea is to avoid the slow loading code in the internal blake2b_update() function, and avoid the overhead of calling crypto_blake2b_update(). It's a micro-optimisation that in principle shouldn't matter that much, but it might help a bit if we repeatedly hash small messages with a key, as can happen in authenticated key exchanges like Monokex. --- src/monocypher.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/monocypher.c b/src/monocypher.c index b26883d..c37482e 100644 --- a/src/monocypher.c +++ b/src/monocypher.c @@ -614,8 +614,11 @@ void crypto_blake2b_general_init(crypto_blake2b_ctx *ctx, size_t hash_size, // if there is a key, the first block is that key (padded with zeroes) if (key_size > 0) { - crypto_blake2b_update(ctx, key , key_size); - crypto_blake2b_update(ctx, zero, 128 - key_size); + u8 key_block[128] = {0}; + COPY(key_block, key, key_size); + // same as calling crypto_blake2b_update(ctx, key_block , 128) + load64_le_buf(ctx->input, key_block, 16); + ctx->input_idx = 128; } } -- 2.47.3