^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) * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #ifndef _CRYPTO_BLAKE2S_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #define _CRYPTO_BLAKE2S_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.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) enum blake2s_lengths {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) BLAKE2S_BLOCK_SIZE = 64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) BLAKE2S_HASH_SIZE = 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) BLAKE2S_KEY_SIZE = 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) BLAKE2S_128_HASH_SIZE = 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) BLAKE2S_160_HASH_SIZE = 20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) BLAKE2S_224_HASH_SIZE = 28,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) BLAKE2S_256_HASH_SIZE = 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct blake2s_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /* 'h', 't', and 'f' are used in assembly code, so keep them as-is. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) u32 h[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) u32 t[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) u32 f[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) u8 buf[BLAKE2S_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) unsigned int buflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned int outlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) enum blake2s_iv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) BLAKE2S_IV0 = 0x6A09E667UL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) BLAKE2S_IV1 = 0xBB67AE85UL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) BLAKE2S_IV2 = 0x3C6EF372UL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) BLAKE2S_IV3 = 0xA54FF53AUL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) BLAKE2S_IV4 = 0x510E527FUL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) BLAKE2S_IV5 = 0x9B05688CUL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) BLAKE2S_IV6 = 0x1F83D9ABUL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) BLAKE2S_IV7 = 0x5BE0CD19UL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static inline void __blake2s_init(struct blake2s_state *state, size_t outlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) const void *key, size_t keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) state->h[0] = BLAKE2S_IV0 ^ (0x01010000 | keylen << 8 | outlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) state->h[1] = BLAKE2S_IV1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) state->h[2] = BLAKE2S_IV2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) state->h[3] = BLAKE2S_IV3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) state->h[4] = BLAKE2S_IV4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) state->h[5] = BLAKE2S_IV5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) state->h[6] = BLAKE2S_IV6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) state->h[7] = BLAKE2S_IV7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) state->t[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) state->t[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) state->f[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) state->f[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) state->buflen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) state->outlen = outlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (keylen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) memcpy(state->buf, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) memset(&state->buf[keylen], 0, BLAKE2S_BLOCK_SIZE - keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) state->buflen = BLAKE2S_BLOCK_SIZE;
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static inline void blake2s_init(struct blake2s_state *state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) const size_t outlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) __blake2s_init(state, outlen, NULL, 0);
^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 void blake2s_init_key(struct blake2s_state *state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) const size_t outlen, const void *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) const size_t keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) WARN_ON(IS_ENABLED(DEBUG) && (!outlen || outlen > BLAKE2S_HASH_SIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) !key || !keylen || keylen > BLAKE2S_KEY_SIZE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) __blake2s_init(state, outlen, key, keylen);
^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) void blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) void blake2s_final(struct blake2s_state *state, u8 *out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static inline void blake2s(u8 *out, const u8 *in, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) const size_t outlen, const size_t inlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) const size_t keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct blake2s_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) WARN_ON(IS_ENABLED(DEBUG) && ((!in && inlen > 0) || !out || !outlen ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) outlen > BLAKE2S_HASH_SIZE || keylen > BLAKE2S_KEY_SIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) (!key && keylen)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) __blake2s_init(&state, outlen, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) blake2s_update(&state, in, inlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) blake2s_final(&state, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) void blake2s256_hmac(u8 *out, const u8 *in, const u8 *key, const size_t inlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) const size_t keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #endif /* _CRYPTO_BLAKE2S_H */