^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) #ifndef _CRYPTO_BLAKE2B_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #define _CRYPTO_BLAKE2B_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) enum blake2b_lengths {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) BLAKE2B_BLOCK_SIZE = 128,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) BLAKE2B_HASH_SIZE = 64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) BLAKE2B_KEY_SIZE = 64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) BLAKE2B_160_HASH_SIZE = 20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) BLAKE2B_256_HASH_SIZE = 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) BLAKE2B_384_HASH_SIZE = 48,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) BLAKE2B_512_HASH_SIZE = 64,
^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) struct blake2b_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /* 'h', 't', and 'f' are used in assembly code, so keep them as-is. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) u64 h[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) u64 t[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) u64 f[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) u8 buf[BLAKE2B_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) unsigned int buflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) unsigned int outlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) enum blake2b_iv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) BLAKE2B_IV0 = 0x6A09E667F3BCC908ULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) BLAKE2B_IV1 = 0xBB67AE8584CAA73BULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) BLAKE2B_IV2 = 0x3C6EF372FE94F82BULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) BLAKE2B_IV3 = 0xA54FF53A5F1D36F1ULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) BLAKE2B_IV4 = 0x510E527FADE682D1ULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) BLAKE2B_IV5 = 0x9B05688C2B3E6C1FULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) BLAKE2B_IV6 = 0x1F83D9ABFB41BD6BULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) BLAKE2B_IV7 = 0x5BE0CD19137E2179ULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static inline void __blake2b_init(struct blake2b_state *state, size_t outlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) const void *key, size_t keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) state->h[0] = BLAKE2B_IV0 ^ (0x01010000 | keylen << 8 | outlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) state->h[1] = BLAKE2B_IV1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) state->h[2] = BLAKE2B_IV2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) state->h[3] = BLAKE2B_IV3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) state->h[4] = BLAKE2B_IV4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) state->h[5] = BLAKE2B_IV5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) state->h[6] = BLAKE2B_IV6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) state->h[7] = BLAKE2B_IV7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) state->t[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) state->t[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) state->f[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) state->f[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) state->buflen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) state->outlen = outlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (keylen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) memcpy(state->buf, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) memset(&state->buf[keylen], 0, BLAKE2B_BLOCK_SIZE - keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) state->buflen = BLAKE2B_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #endif /* _CRYPTO_BLAKE2B_H */