GNU Linux-libre 4.19.286-gnu1
[releases.git] / include / crypto / blake2s.h
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /*
3  * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4  */
5
6 #ifndef _CRYPTO_BLAKE2S_H
7 #define _CRYPTO_BLAKE2S_H
8
9 #include <linux/bug.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13
14 enum blake2s_lengths {
15         BLAKE2S_BLOCK_SIZE = 64,
16         BLAKE2S_HASH_SIZE = 32,
17         BLAKE2S_KEY_SIZE = 32,
18
19         BLAKE2S_128_HASH_SIZE = 16,
20         BLAKE2S_160_HASH_SIZE = 20,
21         BLAKE2S_224_HASH_SIZE = 28,
22         BLAKE2S_256_HASH_SIZE = 32,
23 };
24
25 struct blake2s_state {
26         u32 h[8];
27         u32 t[2];
28         u32 f[2];
29         u8 buf[BLAKE2S_BLOCK_SIZE];
30         unsigned int buflen;
31         unsigned int outlen;
32 };
33
34 enum blake2s_iv {
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,
43 };
44
45 void blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen);
46 void blake2s_final(struct blake2s_state *state, u8 *out);
47
48 static inline void blake2s_init_param(struct blake2s_state *state,
49                                       const u32 param)
50 {
51         *state = (struct blake2s_state){{
52                 BLAKE2S_IV0 ^ param,
53                 BLAKE2S_IV1,
54                 BLAKE2S_IV2,
55                 BLAKE2S_IV3,
56                 BLAKE2S_IV4,
57                 BLAKE2S_IV5,
58                 BLAKE2S_IV6,
59                 BLAKE2S_IV7,
60         }};
61 }
62
63 static inline void blake2s_init(struct blake2s_state *state,
64                                 const size_t outlen)
65 {
66         blake2s_init_param(state, 0x01010000 | outlen);
67         state->outlen = outlen;
68 }
69
70 static inline void blake2s_init_key(struct blake2s_state *state,
71                                     const size_t outlen, const void *key,
72                                     const size_t keylen)
73 {
74         WARN_ON(IS_ENABLED(DEBUG) && (!outlen || outlen > BLAKE2S_HASH_SIZE ||
75                 !key || !keylen || keylen > BLAKE2S_KEY_SIZE));
76
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;
81 }
82
83 static inline void blake2s(u8 *out, const u8 *in, const u8 *key,
84                            const size_t outlen, const size_t inlen,
85                            const size_t keylen)
86 {
87         struct blake2s_state state;
88
89         WARN_ON(IS_ENABLED(DEBUG) && ((!in && inlen > 0) || !out || !outlen ||
90                 outlen > BLAKE2S_HASH_SIZE || keylen > BLAKE2S_KEY_SIZE ||
91                 (!key && keylen)));
92
93         if (keylen)
94                 blake2s_init_key(&state, outlen, key, keylen);
95         else
96                 blake2s_init(&state, outlen);
97
98         blake2s_update(&state, in, inlen);
99         blake2s_final(&state, out);
100 }
101
102 #endif /* _CRYPTO_BLAKE2S_H */