^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 BLAKE2b implementations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Keep this in sync with the corresponding BLAKE2s 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_BLAKE2B_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define _CRYPTO_INTERNAL_BLAKE2B_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <crypto/blake2b.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 blake2b_compress_generic(struct blake2b_state *state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) const u8 *block, size_t nblocks, u32 inc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static inline void blake2b_set_lastblock(struct blake2b_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) state->f[0] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) typedef void (*blake2b_compress_t)(struct blake2b_state *state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) const u8 *block, size_t nblocks, u32 inc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static inline void __blake2b_update(struct blake2b_state *state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) const u8 *in, size_t inlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) blake2b_compress_t compress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) const size_t fill = BLAKE2B_BLOCK_SIZE - state->buflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (unlikely(!inlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (inlen > fill) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) memcpy(state->buf + state->buflen, in, fill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) (*compress)(state, state->buf, 1, BLAKE2B_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) state->buflen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) in += fill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) inlen -= fill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (inlen > BLAKE2B_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) const size_t nblocks = DIV_ROUND_UP(inlen, BLAKE2B_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* Hash one less (full) block than strictly possible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) (*compress)(state, in, nblocks - 1, BLAKE2B_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) in += BLAKE2B_BLOCK_SIZE * (nblocks - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) inlen -= BLAKE2B_BLOCK_SIZE * (nblocks - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) memcpy(state->buf + state->buflen, in, inlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) state->buflen += inlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static inline void __blake2b_final(struct blake2b_state *state, u8 *out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) blake2b_compress_t compress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) blake2b_set_lastblock(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) memset(state->buf + state->buflen, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) BLAKE2B_BLOCK_SIZE - state->buflen); /* Padding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) (*compress)(state, state->buf, 1, state->buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) for (i = 0; i < ARRAY_SIZE(state->h); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) __cpu_to_le64s(&state->h[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) memcpy(out, state->h, state->outlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /* Helper functions for shash implementations of BLAKE2b */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct blake2b_tfm_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) u8 key[BLAKE2B_KEY_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) unsigned int keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static inline int crypto_blake2b_setkey(struct crypto_shash *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) const u8 *key, unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct blake2b_tfm_ctx *tctx = crypto_shash_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (keylen == 0 || keylen > BLAKE2B_KEY_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) memcpy(tctx->key, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) tctx->keylen = keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static inline int crypto_blake2b_init(struct shash_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) const struct blake2b_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct blake2b_state *state = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) unsigned int outlen = crypto_shash_digestsize(desc->tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) __blake2b_init(state, outlen, tctx->key, tctx->keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static inline int crypto_blake2b_update(struct shash_desc *desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) const u8 *in, unsigned int inlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) blake2b_compress_t compress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct blake2b_state *state = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) __blake2b_update(state, in, inlen, compress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static inline int crypto_blake2b_final(struct shash_desc *desc, u8 *out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) blake2b_compress_t compress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct blake2b_state *state = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) __blake2b_final(state, out, compress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) #endif /* _CRYPTO_INTERNAL_BLAKE2B_H */