^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 OR MIT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Helper functions for BLAKE2s implementations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Keep this in sync with the corresponding BLAKE2b header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #ifndef _CRYPTO_INTERNAL_BLAKE2S_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define _CRYPTO_INTERNAL_BLAKE2S_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <crypto/blake2s.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) void blake2s_compress_generic(struct blake2s_state *state,const u8 *block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) size_t nblocks, const u32 inc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) void blake2s_compress_arch(struct blake2s_state *state,const u8 *block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) size_t nblocks, const u32 inc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) bool blake2s_selftest(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static inline void blake2s_set_lastblock(struct blake2s_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) state->f[0] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) typedef void (*blake2s_compress_t)(struct blake2s_state *state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) const u8 *block, size_t nblocks, u32 inc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /* Helper functions for BLAKE2s shared by the library and shash APIs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static inline void __blake2s_update(struct blake2s_state *state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) const u8 *in, size_t inlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) blake2s_compress_t compress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) const size_t fill = BLAKE2S_BLOCK_SIZE - state->buflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (unlikely(!inlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (inlen > fill) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) memcpy(state->buf + state->buflen, in, fill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) (*compress)(state, state->buf, 1, BLAKE2S_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) state->buflen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) in += fill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) inlen -= fill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (inlen > BLAKE2S_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) const size_t nblocks = DIV_ROUND_UP(inlen, BLAKE2S_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /* Hash one less (full) block than strictly possible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) (*compress)(state, in, nblocks - 1, BLAKE2S_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) in += BLAKE2S_BLOCK_SIZE * (nblocks - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) inlen -= BLAKE2S_BLOCK_SIZE * (nblocks - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) memcpy(state->buf + state->buflen, in, inlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) state->buflen += inlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static inline void __blake2s_final(struct blake2s_state *state, u8 *out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) blake2s_compress_t compress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) blake2s_set_lastblock(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) memset(state->buf + state->buflen, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) BLAKE2S_BLOCK_SIZE - state->buflen); /* Padding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) (*compress)(state, state->buf, 1, state->buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) cpu_to_le32_array(state->h, ARRAY_SIZE(state->h));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) memcpy(out, state->h, state->outlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /* Helper functions for shash implementations of BLAKE2s */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct blake2s_tfm_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) u8 key[BLAKE2S_KEY_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) unsigned int keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static inline int crypto_blake2s_setkey(struct crypto_shash *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) const u8 *key, unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct blake2s_tfm_ctx *tctx = crypto_shash_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (keylen == 0 || keylen > BLAKE2S_KEY_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) memcpy(tctx->key, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) tctx->keylen = keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static inline int crypto_blake2s_init(struct shash_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) const struct blake2s_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct blake2s_state *state = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) unsigned int outlen = crypto_shash_digestsize(desc->tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) __blake2s_init(state, outlen, tctx->key, tctx->keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static inline int crypto_blake2s_update(struct shash_desc *desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) const u8 *in, unsigned int inlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) blake2s_compress_t compress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct blake2s_state *state = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) __blake2s_update(state, in, inlen, compress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static inline int crypto_blake2s_final(struct shash_desc *desc, u8 *out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) blake2s_compress_t compress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct blake2s_state *state = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) __blake2s_final(state, out, compress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) #endif /* _CRYPTO_INTERNAL_BLAKE2S_H */