]> git.codecow.com Git - Monocypher.git/commitdiff
Optimised key loading in Blake2b
authorLoup Vaillant <loup@loup-vaillant.fr>
Mon, 8 Jun 2020 15:46:16 +0000 (17:46 +0200)
committerLoup Vaillant <loup@loup-vaillant.fr>
Mon, 8 Jun 2020 21:38:06 +0000 (23:38 +0200)
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

index b26883d0145111259640b9cc88ebef1e95445d9d..c37482ec5a157a95c4ee8f4dc61132df20d16020 100644 (file)
@@ -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;
     }
 }