blob: 52363eee2b20e6763c6b473a7137793258a8daa2 [file] [log] [blame]
Jason A. Donenfeld66d7fb92019-11-08 13:22:28 +01001/* SPDX-License-Identifier: GPL-2.0 OR MIT */
Eric Biggers8c4a93a2020-12-23 00:09:54 -08002/*
3 * Helper functions for BLAKE2s implementations.
4 * Keep this in sync with the corresponding BLAKE2b header.
5 */
Jason A. Donenfeld66d7fb92019-11-08 13:22:28 +01006
Eric Biggers87868412020-12-23 00:09:57 -08007#ifndef _CRYPTO_INTERNAL_BLAKE2S_H
8#define _CRYPTO_INTERNAL_BLAKE2S_H
Jason A. Donenfeld66d7fb92019-11-08 13:22:28 +01009
10#include <crypto/blake2s.h>
Eric Biggers8c4a93a2020-12-23 00:09:54 -080011#include <crypto/internal/hash.h>
Eric Biggers057edc92020-12-23 00:09:53 -080012#include <linux/string.h>
Jason A. Donenfeld66d7fb92019-11-08 13:22:28 +010013
Jason A. Donenfeld6048fdc2021-12-22 14:56:58 +010014void blake2s_compress_generic(struct blake2s_state *state, const u8 *block,
Jason A. Donenfeld66d7fb92019-11-08 13:22:28 +010015 size_t nblocks, const u32 inc);
16
Jason A. Donenfeld6048fdc2021-12-22 14:56:58 +010017void blake2s_compress(struct blake2s_state *state, const u8 *block,
18 size_t nblocks, const u32 inc);
Jason A. Donenfeld66d7fb92019-11-08 13:22:28 +010019
Herbert Xuce0d5d62020-11-27 16:43:18 +110020bool blake2s_selftest(void);
21
Jason A. Donenfeld66d7fb92019-11-08 13:22:28 +010022static inline void blake2s_set_lastblock(struct blake2s_state *state)
23{
24 state->f[0] = -1;
25}
26
Eric Biggers8c4a93a2020-12-23 00:09:54 -080027/* Helper functions for BLAKE2s shared by the library and shash APIs */
28
Jason A. Donenfeldd2a02e32022-01-19 14:35:06 +010029static __always_inline void
30__blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen,
31 bool force_generic)
Eric Biggers057edc92020-12-23 00:09:53 -080032{
33 const size_t fill = BLAKE2S_BLOCK_SIZE - state->buflen;
34
35 if (unlikely(!inlen))
36 return;
37 if (inlen > fill) {
38 memcpy(state->buf + state->buflen, in, fill);
Jason A. Donenfeldd2a02e32022-01-19 14:35:06 +010039 if (force_generic)
40 blake2s_compress_generic(state, state->buf, 1,
41 BLAKE2S_BLOCK_SIZE);
42 else
43 blake2s_compress(state, state->buf, 1,
44 BLAKE2S_BLOCK_SIZE);
Eric Biggers057edc92020-12-23 00:09:53 -080045 state->buflen = 0;
46 in += fill;
47 inlen -= fill;
48 }
49 if (inlen > BLAKE2S_BLOCK_SIZE) {
50 const size_t nblocks = DIV_ROUND_UP(inlen, BLAKE2S_BLOCK_SIZE);
51 /* Hash one less (full) block than strictly possible */
Jason A. Donenfeldd2a02e32022-01-19 14:35:06 +010052 if (force_generic)
53 blake2s_compress_generic(state, in, nblocks - 1,
54 BLAKE2S_BLOCK_SIZE);
55 else
56 blake2s_compress(state, in, nblocks - 1,
57 BLAKE2S_BLOCK_SIZE);
Eric Biggers057edc92020-12-23 00:09:53 -080058 in += BLAKE2S_BLOCK_SIZE * (nblocks - 1);
59 inlen -= BLAKE2S_BLOCK_SIZE * (nblocks - 1);
60 }
61 memcpy(state->buf + state->buflen, in, inlen);
62 state->buflen += inlen;
63}
64
Jason A. Donenfeldd2a02e32022-01-19 14:35:06 +010065static __always_inline void
66__blake2s_final(struct blake2s_state *state, u8 *out, bool force_generic)
Eric Biggers057edc92020-12-23 00:09:53 -080067{
68 blake2s_set_lastblock(state);
69 memset(state->buf + state->buflen, 0,
70 BLAKE2S_BLOCK_SIZE - state->buflen); /* Padding */
Jason A. Donenfeldd2a02e32022-01-19 14:35:06 +010071 if (force_generic)
72 blake2s_compress_generic(state, state->buf, 1, state->buflen);
73 else
74 blake2s_compress(state, state->buf, 1, state->buflen);
Eric Biggers057edc92020-12-23 00:09:53 -080075 cpu_to_le32_array(state->h, ARRAY_SIZE(state->h));
76 memcpy(out, state->h, state->outlen);
77}
78
Eric Biggers8c4a93a2020-12-23 00:09:54 -080079/* Helper functions for shash implementations of BLAKE2s */
80
81struct blake2s_tfm_ctx {
82 u8 key[BLAKE2S_KEY_SIZE];
83 unsigned int keylen;
84};
85
86static inline int crypto_blake2s_setkey(struct crypto_shash *tfm,
87 const u8 *key, unsigned int keylen)
88{
89 struct blake2s_tfm_ctx *tctx = crypto_shash_ctx(tfm);
90
91 if (keylen == 0 || keylen > BLAKE2S_KEY_SIZE)
92 return -EINVAL;
93
94 memcpy(tctx->key, key, keylen);
95 tctx->keylen = keylen;
96
97 return 0;
98}
99
100static inline int crypto_blake2s_init(struct shash_desc *desc)
101{
102 const struct blake2s_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
103 struct blake2s_state *state = shash_desc_ctx(desc);
104 unsigned int outlen = crypto_shash_digestsize(desc->tfm);
105
Eric Biggers42ad8cf2020-12-23 00:09:55 -0800106 __blake2s_init(state, outlen, tctx->key, tctx->keylen);
Eric Biggers8c4a93a2020-12-23 00:09:54 -0800107 return 0;
108}
109
110static inline int crypto_blake2s_update(struct shash_desc *desc,
111 const u8 *in, unsigned int inlen,
Jason A. Donenfeldd2a02e32022-01-19 14:35:06 +0100112 bool force_generic)
Eric Biggers8c4a93a2020-12-23 00:09:54 -0800113{
114 struct blake2s_state *state = shash_desc_ctx(desc);
115
Jason A. Donenfeldd2a02e32022-01-19 14:35:06 +0100116 __blake2s_update(state, in, inlen, force_generic);
Eric Biggers8c4a93a2020-12-23 00:09:54 -0800117 return 0;
118}
119
120static inline int crypto_blake2s_final(struct shash_desc *desc, u8 *out,
Jason A. Donenfeldd2a02e32022-01-19 14:35:06 +0100121 bool force_generic)
Eric Biggers8c4a93a2020-12-23 00:09:54 -0800122{
123 struct blake2s_state *state = shash_desc_ctx(desc);
124
Jason A. Donenfeldd2a02e32022-01-19 14:35:06 +0100125 __blake2s_final(state, out, force_generic);
Eric Biggers8c4a93a2020-12-23 00:09:54 -0800126 return 0;
127}
128
Eric Biggers87868412020-12-23 00:09:57 -0800129#endif /* _CRYPTO_INTERNAL_BLAKE2S_H */