1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
3 * Helper functions for BLAKE2s implementations.
4 * Keep this in sync with the corresponding BLAKE2b header.
7 #ifndef _CRYPTO_INTERNAL_BLAKE2S_H
8 #define _CRYPTO_INTERNAL_BLAKE2S_H
10 #include <crypto/blake2s.h>
11 #include <crypto/internal/hash.h>
12 #include <linux/string.h>
14 void blake2s_compress_generic(struct blake2s_state *state, const u8 *block,
15 size_t nblocks, const u32 inc);
17 void blake2s_compress(struct blake2s_state *state, const u8 *block,
18 size_t nblocks, const u32 inc);
20 bool blake2s_selftest(void);
22 static inline void blake2s_set_lastblock(struct blake2s_state *state)
27 /* Helper functions for BLAKE2s shared by the library and shash APIs */
29 static __always_inline void
30 __blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen,
33 const size_t fill = BLAKE2S_BLOCK_SIZE - state->buflen;
38 memcpy(state->buf + state->buflen, in, fill);
40 blake2s_compress_generic(state, state->buf, 1,
43 blake2s_compress(state, state->buf, 1,
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 */
53 blake2s_compress_generic(state, in, nblocks - 1,
56 blake2s_compress(state, in, nblocks - 1,
58 in += BLAKE2S_BLOCK_SIZE * (nblocks - 1);
59 inlen -= BLAKE2S_BLOCK_SIZE * (nblocks - 1);
61 memcpy(state->buf + state->buflen, in, inlen);
62 state->buflen += inlen;
65 static __always_inline void
66 __blake2s_final(struct blake2s_state *state, u8 *out, bool force_generic)
68 blake2s_set_lastblock(state);
69 memset(state->buf + state->buflen, 0,
70 BLAKE2S_BLOCK_SIZE - state->buflen); /* Padding */
72 blake2s_compress_generic(state, state->buf, 1, state->buflen);
74 blake2s_compress(state, state->buf, 1, state->buflen);
75 cpu_to_le32_array(state->h, ARRAY_SIZE(state->h));
76 memcpy(out, state->h, state->outlen);
79 /* Helper functions for shash implementations of BLAKE2s */
81 struct blake2s_tfm_ctx {
82 u8 key[BLAKE2S_KEY_SIZE];
86 static inline int crypto_blake2s_setkey(struct crypto_shash *tfm,
87 const u8 *key, unsigned int keylen)
89 struct blake2s_tfm_ctx *tctx = crypto_shash_ctx(tfm);
91 if (keylen == 0 || keylen > BLAKE2S_KEY_SIZE)
94 memcpy(tctx->key, key, keylen);
95 tctx->keylen = keylen;
100 static inline int crypto_blake2s_init(struct shash_desc *desc)
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);
106 __blake2s_init(state, outlen, tctx->key, tctx->keylen);
110 static inline int crypto_blake2s_update(struct shash_desc *desc,
111 const u8 *in, unsigned int inlen,
114 struct blake2s_state *state = shash_desc_ctx(desc);
116 __blake2s_update(state, in, inlen, force_generic);
120 static inline int crypto_blake2s_final(struct shash_desc *desc, u8 *out,
123 struct blake2s_state *state = shash_desc_ctx(desc);
125 __blake2s_final(state, out, force_generic);
129 #endif /* _CRYPTO_INTERNAL_BLAKE2S_H */