diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-09-19 02:56:34 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-09-19 23:29:45 +0200 |
commit | cb8af686daad9b45451a91906441f9749ea3ed71 (patch) | |
tree | 2ab5c6d14458cee94cba94b87f07103b418bb480 | |
parent | 57fd00dbd4da0c9ba6da84aefb80f4f4920a051c (diff) |
blake2s: simplify final function
Suggested-by: Eric Biggers <ebiggers@kernel.org>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r-- | src/compat/compat.h | 10 | ||||
-rw-r--r-- | src/crypto/include/zinc/blake2s.h | 39 | ||||
-rw-r--r-- | src/crypto/zinc/blake2s/blake2s.c | 10 |
3 files changed, 19 insertions, 40 deletions
diff --git a/src/compat/compat.h b/src/compat/compat.h index 9e92236..c182234 100644 --- a/src/compat/compat.h +++ b/src/compat/compat.h @@ -688,6 +688,16 @@ static inline void *skb_put_data(struct sk_buff *skb, const void *data, unsigned #endif #endif +#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0) +static inline void cpu_to_le32_array(u32 *buf, unsigned int words) +{ + while (words--) { + __cpu_to_le32s(buf); + buf++; + } +} +#endif + /* https://lkml.kernel.org/r/20170624021727.17835-1-Jason@zx2c4.com */ #if IS_ENABLED(CONFIG_NF_CONNTRACK) #include <linux/ip.h> diff --git a/src/crypto/include/zinc/blake2s.h b/src/crypto/include/zinc/blake2s.h index 9512815..7a4382c 100644 --- a/src/crypto/include/zinc/blake2s.h +++ b/src/crypto/include/zinc/blake2s.h @@ -29,44 +29,7 @@ void blake2s_init(struct blake2s_state *state, const size_t outlen); void blake2s_init_key(struct blake2s_state *state, const size_t outlen, const void *key, const size_t keylen); void blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen); -void __blake2s_final(struct blake2s_state *state); -static inline void blake2s_final(struct blake2s_state *state, u8 *out, - const size_t outlen) -{ - int i; - -#ifdef DEBUG - BUG_ON(!out || !outlen || outlen > BLAKE2S_OUTBYTES); -#endif - __blake2s_final(state); - - if (__builtin_constant_p(outlen) && !(outlen % sizeof(u32))) { - if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) || - IS_ALIGNED((unsigned long)out, __alignof__(u32))) { - __le32 *outwords = (__le32 *)out; - - for (i = 0; i < outlen / sizeof(u32); ++i) - outwords[i] = cpu_to_le32(state->h[i]); - } else { - __le32 buffer[BLAKE2S_OUTBYTES]; - - for (i = 0; i < outlen / sizeof(u32); ++i) - buffer[i] = cpu_to_le32(state->h[i]); - memcpy(out, buffer, outlen); - memzero_explicit(buffer, sizeof(buffer)); - } - } else { - u8 buffer[BLAKE2S_OUTBYTES] __aligned(__alignof__(u32)); - __le32 *outwords = (__le32 *)buffer; - - for (i = 0; i < 8; ++i) - outwords[i] = cpu_to_le32(state->h[i]); - memcpy(out, buffer, outlen); - memzero_explicit(buffer, sizeof(buffer)); - } - - memzero_explicit(state, sizeof(*state)); -} +void blake2s_final(struct blake2s_state *state, u8 *out, const size_t outlen); static inline void blake2s(u8 *out, const u8 *in, const u8 *key, const size_t outlen, const size_t inlen, diff --git a/src/crypto/zinc/blake2s/blake2s.c b/src/crypto/zinc/blake2s/blake2s.c index 69b2b4e..29382da 100644 --- a/src/crypto/zinc/blake2s/blake2s.c +++ b/src/crypto/zinc/blake2s/blake2s.c @@ -230,14 +230,20 @@ void blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen) } EXPORT_SYMBOL(blake2s_update); -void __blake2s_final(struct blake2s_state *state) +void blake2s_final(struct blake2s_state *state, u8 *out, const size_t outlen) { +#ifdef DEBUG + BUG_ON(!out || !outlen || outlen > BLAKE2S_OUTBYTES); +#endif blake2s_set_lastblock(state); memset(state->buf + state->buflen, 0, BLAKE2S_BLOCKBYTES - state->buflen); /* Padding */ blake2s_compress(state, state->buf, 1, state->buflen); + cpu_to_le32_array(state->h, ARRAY_SIZE(state->h)); + memcpy(out, state->h, outlen); + memzero_explicit(state, sizeof(*state)); } -EXPORT_SYMBOL(__blake2s_final); +EXPORT_SYMBOL(blake2s_final); void blake2s_hmac(u8 *out, const u8 *in, const u8 *key, const size_t outlen, const size_t inlen, const size_t keylen) |