1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
3 * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
6 #ifndef _CRYPTO_BLAKE2S_H
7 #define _CRYPTO_BLAKE2S_H
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/string.h>
14 enum blake2s_lengths {
15 BLAKE2S_BLOCK_SIZE = 64,
16 BLAKE2S_HASH_SIZE = 32,
17 BLAKE2S_KEY_SIZE = 32,
19 BLAKE2S_128_HASH_SIZE = 16,
20 BLAKE2S_160_HASH_SIZE = 20,
21 BLAKE2S_224_HASH_SIZE = 28,
22 BLAKE2S_256_HASH_SIZE = 32,
25 struct blake2s_state {
29 u8 buf[BLAKE2S_BLOCK_SIZE];
35 BLAKE2S_IV0 = 0x6A09E667UL,
36 BLAKE2S_IV1 = 0xBB67AE85UL,
37 BLAKE2S_IV2 = 0x3C6EF372UL,
38 BLAKE2S_IV3 = 0xA54FF53AUL,
39 BLAKE2S_IV4 = 0x510E527FUL,
40 BLAKE2S_IV5 = 0x9B05688CUL,
41 BLAKE2S_IV6 = 0x1F83D9ABUL,
42 BLAKE2S_IV7 = 0x5BE0CD19UL,
45 void blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen);
46 void blake2s_final(struct blake2s_state *state, u8 *out);
48 static inline void blake2s_init_param(struct blake2s_state *state,
51 *state = (struct blake2s_state){{
63 static inline void blake2s_init(struct blake2s_state *state,
66 blake2s_init_param(state, 0x01010000 | outlen);
67 state->outlen = outlen;
70 static inline void blake2s_init_key(struct blake2s_state *state,
71 const size_t outlen, const void *key,
74 WARN_ON(IS_ENABLED(DEBUG) && (!outlen || outlen > BLAKE2S_HASH_SIZE ||
75 !key || !keylen || keylen > BLAKE2S_KEY_SIZE));
77 blake2s_init_param(state, 0x01010000 | keylen << 8 | outlen);
78 memcpy(state->buf, key, keylen);
79 state->buflen = BLAKE2S_BLOCK_SIZE;
80 state->outlen = outlen;
83 static inline void blake2s(u8 *out, const u8 *in, const u8 *key,
84 const size_t outlen, const size_t inlen,
87 struct blake2s_state state;
89 WARN_ON(IS_ENABLED(DEBUG) && ((!in && inlen > 0) || !out || !outlen ||
90 outlen > BLAKE2S_HASH_SIZE || keylen > BLAKE2S_KEY_SIZE ||
94 blake2s_init_key(&state, outlen, key, keylen);
96 blake2s_init(&state, outlen);
98 blake2s_update(&state, in, inlen);
99 blake2s_final(&state, out);
102 #endif /* _CRYPTO_BLAKE2S_H */