History

crypto: blake2s - optimize blake2s initialization

If no key was provided, then don't waste time initializing the block buffer, as its initial contents won't be used. Also, make crypto_blake2s_init() and blake2s() call a single internal function __blake2s_init() which treats the key as optional, rather than conditionally calling blake2s_init() or blake2s_init_key(). This reduces the compiled code size, as previously both blake2s_init() and blake2s_init_key() were being inlined into these two callers, except when the key size passed to blake2s() was a compile-time constant. These optimizations aren't that significant for BLAKE2s. However, the equivalent optimizations will be more significant for BLAKE2b, as everything is twice as big in BLAKE2b. And it's good to keep things consistent rather than making optimizations for BLAKE2b but not BLAKE2s.

Eric Biggers committed 5 years ago

Commit 42ad8cf1 parent 8c4a93a2 files · +28 −30Browse files

Unified · Split

2 files
  1. include/crypto/blake2s.h +27 −26
  2. include/crypto/internal/blake2s.h +1 −4
include/crypto/blake2s.h+27−26
@@ -43,29 +43,34 @@ enum blake2s_iv { · 42 unmodified lines
4343 BLAKE2S_IV7 = 0x5BE0CD19UL,
4444 };
4545
46−void blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen);
47−void blake2s_final(struct blake2s_state *state, u8 *out);
48−
49−static inline void blake2s_init_param(struct blake2s_state *state,
50− const u32 param)
46+static inline void __blake2s_init(struct blake2s_state *state, size_t outlen,
47+ const void *key, size_t keylen)
5148 {
52− *state = (struct blake2s_state){{
53− BLAKE2S_IV0 ^ param,
54− BLAKE2S_IV1,
55− BLAKE2S_IV2,
56− BLAKE2S_IV3,
57− BLAKE2S_IV4,
58− BLAKE2S_IV5,
59− BLAKE2S_IV6,
60− BLAKE2S_IV7,
61− }};
49+ state->h[0] = BLAKE2S_IV0 ^ (0x01010000 | keylen << 8 | outlen);
50+ state->h[1] = BLAKE2S_IV1;
51+ state->h[2] = BLAKE2S_IV2;
52+ state->h[3] = BLAKE2S_IV3;
53+ state->h[4] = BLAKE2S_IV4;
54+ state->h[5] = BLAKE2S_IV5;
55+ state->h[6] = BLAKE2S_IV6;
56+ state->h[7] = BLAKE2S_IV7;
57+ state->t[0] = 0;
58+ state->t[1] = 0;
59+ state->f[0] = 0;
60+ state->f[1] = 0;
61+ state->buflen = 0;
62+ state->outlen = outlen;
63+ if (keylen) {
64+ memcpy(state->buf, key, keylen);
65+ memset(&state->buf[keylen], 0, BLAKE2S_BLOCK_SIZE - keylen);
66+ state->buflen = BLAKE2S_BLOCK_SIZE;
67+ }
6268 }
6369
6470 static inline void blake2s_init(struct blake2s_state *state,
6571 const size_t outlen)
6672 {
67− blake2s_init_param(state, 0x01010000 | outlen);
68− state->outlen = outlen;
73+ __blake2s_init(state, outlen, NULL, 0);
6974 }
7075
7176 static inline void blake2s_init_key(struct blake2s_state *state,
@@ -75,12 +80,12 @@ static inline void blake2s_init_key(struct blake2s_state *state, · 3 unmodified lines
7580 WARN_ON(IS_ENABLED(DEBUG) && (!outlen || outlen > BLAKE2S_HASH_SIZE ||
7681 !key || !keylen || keylen > BLAKE2S_KEY_SIZE));
7782
78− blake2s_init_param(state, 0x01010000 | keylen << 8 | outlen);
79− memcpy(state->buf, key, keylen);
80− state->buflen = BLAKE2S_BLOCK_SIZE;
81− state->outlen = outlen;
83+ __blake2s_init(state, outlen, key, keylen);
8284 }
8385
86+void blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen);
87+void blake2s_final(struct blake2s_state *state, u8 *out);
88+
8489 static inline void blake2s(u8 *out, const u8 *in, const u8 *key,
8590 const size_t outlen, const size_t inlen,
8691 const size_t keylen)
@@ -91,11 +96,7 @@ static inline void blake2s(u8 *out, const u8 *in, const u8 *key, · 4 unmodified lines
9196 outlen > BLAKE2S_HASH_SIZE || keylen > BLAKE2S_KEY_SIZE ||
9297 (!key && keylen)));
9398
94− if (keylen)
95− blake2s_init_key(&state, outlen, key, keylen);
96− else
97− blake2s_init(&state, outlen);
98−
99+ __blake2s_init(&state, outlen, key, keylen);
99100 blake2s_update(&state, in, inlen);
100101 blake2s_final(&state, out);
101102 }
include/crypto/internal/blake2s.h+1−4
@@ -93,10 +93,7 @@ static inline int crypto_blake2s_init(struct shash_desc *desc) · 92 unmodified lines
9393 struct blake2s_state *state = shash_desc_ctx(desc);
9494 unsigned int outlen = crypto_shash_digestsize(desc->tfm);
9595
96− if (tctx->keylen)
97− blake2s_init_key(state, outlen, tctx->key, tctx->keylen);
98− else
99− blake2s_init(state, outlen);
96+ __blake2s_init(state, outlen, tctx->key, tctx->keylen);
10097 return 0;
10198 }
10299

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t