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
43 BLAKE2S_IV7 = 0x5BE0CD19UL,43 BLAKE2S_IV7 = 0x5BE0CD19UL,
44};44};
45 45
46void blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen);46static inline void __blake2s_init(struct blake2s_state *state, size_t outlen,
47void blake2s_final(struct blake2s_state *state, u8 *out);47 const void *key, size_t keylen)
48
49static inline void blake2s_init_param(struct blake2s_state *state,
50 const u32 param)
51{48{
52 *state = (struct blake2s_state){{49 state->h[0] = BLAKE2S_IV0 ^ (0x01010000 | keylen << 8 | outlen);
53 BLAKE2S_IV0 ^ param,50 state->h[1] = BLAKE2S_IV1;
54 BLAKE2S_IV1,51 state->h[2] = BLAKE2S_IV2;
55 BLAKE2S_IV2,52 state->h[3] = BLAKE2S_IV3;
56 BLAKE2S_IV3,53 state->h[4] = BLAKE2S_IV4;
57 BLAKE2S_IV4,54 state->h[5] = BLAKE2S_IV5;
58 BLAKE2S_IV5,55 state->h[6] = BLAKE2S_IV6;
59 BLAKE2S_IV6,56 state->h[7] = BLAKE2S_IV7;
60 BLAKE2S_IV7,57 state->t[0] = 0;
61 }};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 }
62}68}
63 69
64static inline void blake2s_init(struct blake2s_state *state,70static inline void blake2s_init(struct blake2s_state *state,
65 const size_t outlen)71 const size_t outlen)
66{72{
67 blake2s_init_param(state, 0x01010000 | outlen);73 __blake2s_init(state, outlen, NULL, 0);
68 state->outlen = outlen;
69}74}
70 75
71static inline void blake2s_init_key(struct blake2s_state *state,76static 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
75 WARN_ON(IS_ENABLED(DEBUG) && (!outlen || outlen > BLAKE2S_HASH_SIZE ||80 WARN_ON(IS_ENABLED(DEBUG) && (!outlen || outlen > BLAKE2S_HASH_SIZE ||
76 !key || !keylen || keylen > BLAKE2S_KEY_SIZE));81 !key || !keylen || keylen > BLAKE2S_KEY_SIZE));
77 82
78 blake2s_init_param(state, 0x01010000 | keylen << 8 | outlen);83 __blake2s_init(state, outlen, key, keylen);
79 memcpy(state->buf, key, keylen);
80 state->buflen = BLAKE2S_BLOCK_SIZE;
81 state->outlen = outlen;
82}84}
83 85
86void blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen);
87void blake2s_final(struct blake2s_state *state, u8 *out);
88
84static inline void blake2s(u8 *out, const u8 *in, const u8 *key,89static inline void blake2s(u8 *out, const u8 *in, const u8 *key,
85 const size_t outlen, const size_t inlen,90 const size_t outlen, const size_t inlen,
86 const size_t keylen)91 const size_t keylen)
@@ -91,11 +96,7 @@ static inline void blake2s(u8 *out, const u8 *in, const u8 *key, · 4 unmodified lines
91 outlen > BLAKE2S_HASH_SIZE || keylen > BLAKE2S_KEY_SIZE ||96 outlen > BLAKE2S_HASH_SIZE || keylen > BLAKE2S_KEY_SIZE ||
92 (!key && keylen)));97 (!key && keylen)));
93 98
94 if (keylen)99 __blake2s_init(&state, outlen, key, keylen);
95 blake2s_init_key(&state, outlen, key, keylen);
96 else
97 blake2s_init(&state, outlen);
98
99 blake2s_update(&state, in, inlen);100 blake2s_update(&state, in, inlen);
100 blake2s_final(&state, out);101 blake2s_final(&state, out);
101}102}
include/crypto/internal/blake2s.h+1−4
@@ -93,10 +93,7 @@ static inline int crypto_blake2s_init(struct shash_desc *desc) · 92 unmodified lines
93 struct blake2s_state *state = shash_desc_ctx(desc);93 struct blake2s_state *state = shash_desc_ctx(desc);
94 unsigned int outlen = crypto_shash_digestsize(desc->tfm);94 unsigned int outlen = crypto_shash_digestsize(desc->tfm);
95 95
96 if (tctx->keylen)96 __blake2s_init(state, outlen, tctx->key, tctx->keylen);
97 blake2s_init_key(state, outlen, tctx->key, tctx->keylen);
98 else
99 blake2s_init(state, outlen);
100 return 0;97 return 0;
101}98}
102 99

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