diff options
Diffstat (limited to 'lib/crypto/blake2s.c')
| -rw-r--r-- | lib/crypto/blake2s.c | 37 | 
1 files changed, 30 insertions, 7 deletions
diff --git a/lib/crypto/blake2s.c b/lib/crypto/blake2s.c index c71c09621c09..98e688c6d891 100644 --- a/lib/crypto/blake2s.c +++ b/lib/crypto/blake2s.c @@ -16,16 +16,44 @@  #include <linux/init.h>  #include <linux/bug.h> +static inline void blake2s_set_lastblock(struct blake2s_state *state) +{ +	state->f[0] = -1; +} +  void blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen)  { -	__blake2s_update(state, in, inlen, false); +	const size_t fill = BLAKE2S_BLOCK_SIZE - state->buflen; + +	if (unlikely(!inlen)) +		return; +	if (inlen > fill) { +		memcpy(state->buf + state->buflen, in, fill); +		blake2s_compress(state, state->buf, 1, BLAKE2S_BLOCK_SIZE); +		state->buflen = 0; +		in += fill; +		inlen -= fill; +	} +	if (inlen > BLAKE2S_BLOCK_SIZE) { +		const size_t nblocks = DIV_ROUND_UP(inlen, BLAKE2S_BLOCK_SIZE); +		blake2s_compress(state, in, nblocks - 1, BLAKE2S_BLOCK_SIZE); +		in += BLAKE2S_BLOCK_SIZE * (nblocks - 1); +		inlen -= BLAKE2S_BLOCK_SIZE * (nblocks - 1); +	} +	memcpy(state->buf + state->buflen, in, inlen); +	state->buflen += inlen;  }  EXPORT_SYMBOL(blake2s_update);  void blake2s_final(struct blake2s_state *state, u8 *out)  {  	WARN_ON(IS_ENABLED(DEBUG) && !out); -	__blake2s_final(state, out, false); +	blake2s_set_lastblock(state); +	memset(state->buf + state->buflen, 0, +	       BLAKE2S_BLOCK_SIZE - 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, state->outlen);  	memzero_explicit(state, sizeof(*state));  }  EXPORT_SYMBOL(blake2s_final); @@ -38,12 +66,7 @@ static int __init blake2s_mod_init(void)  	return 0;  } -static void __exit blake2s_mod_exit(void) -{ -} -  module_init(blake2s_mod_init); -module_exit(blake2s_mod_exit);  MODULE_LICENSE("GPL v2");  MODULE_DESCRIPTION("BLAKE2s hash function");  MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>");  | 
