blob: 442c639c9ad94ae8ea35226a7269e564b54335d9 [file] [log] [blame]
David Sterba91d68932019-10-24 18:28:31 +02001// SPDX-License-Identifier: (GPL-2.0-only OR Apache-2.0)
2/*
3 * BLAKE2b reference source code package - reference C implementations
4 *
5 * Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the
6 * terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
7 * your option. The terms of these licenses can be found at:
8 *
9 * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
10 * - OpenSSL license : https://www.openssl.org/source/license.html
11 * - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * More information about the BLAKE2 hash function can be found at
14 * https://blake2.net.
15 *
16 * Note: the original sources have been modified for inclusion in linux kernel
17 * in terms of coding style, using generic helpers and simplifications of error
18 * handling.
19 */
20
21#include <asm/unaligned.h>
22#include <linux/module.h>
23#include <linux/string.h>
24#include <linux/kernel.h>
25#include <linux/bitops.h>
26#include <crypto/internal/hash.h>
27
28#define BLAKE2B_160_DIGEST_SIZE (160 / 8)
29#define BLAKE2B_256_DIGEST_SIZE (256 / 8)
30#define BLAKE2B_384_DIGEST_SIZE (384 / 8)
31#define BLAKE2B_512_DIGEST_SIZE (512 / 8)
32
33enum blake2b_constant {
34 BLAKE2B_BLOCKBYTES = 128,
David Sterba91d68932019-10-24 18:28:31 +020035 BLAKE2B_KEYBYTES = 64,
David Sterba91d68932019-10-24 18:28:31 +020036};
37
38struct blake2b_state {
39 u64 h[8];
40 u64 t[2];
41 u64 f[2];
42 u8 buf[BLAKE2B_BLOCKBYTES];
43 size_t buflen;
David Sterba91d68932019-10-24 18:28:31 +020044};
45
David Sterba91d68932019-10-24 18:28:31 +020046static const u64 blake2b_IV[8] = {
47 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
48 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
49 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
50 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
51};
52
53static const u8 blake2b_sigma[12][16] = {
54 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
55 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
56 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
57 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
58 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
59 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
60 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
61 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
62 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
63 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
64 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
65 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
66};
67
David Sterba91d68932019-10-24 18:28:31 +020068static void blake2b_set_lastblock(struct blake2b_state *S)
69{
David Sterba91d68932019-10-24 18:28:31 +020070 S->f[0] = (u64)-1;
71}
72
73static void blake2b_increment_counter(struct blake2b_state *S, const u64 inc)
74{
75 S->t[0] += inc;
76 S->t[1] += (S->t[0] < inc);
77}
78
David Sterba91d68932019-10-24 18:28:31 +020079#define G(r,i,a,b,c,d) \
80 do { \
81 a = a + b + m[blake2b_sigma[r][2*i+0]]; \
82 d = ror64(d ^ a, 32); \
83 c = c + d; \
84 b = ror64(b ^ c, 24); \
85 a = a + b + m[blake2b_sigma[r][2*i+1]]; \
86 d = ror64(d ^ a, 16); \
87 c = c + d; \
88 b = ror64(b ^ c, 63); \
89 } while (0)
90
91#define ROUND(r) \
92 do { \
93 G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
94 G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
95 G(r,2,v[ 2],v[ 6],v[10],v[14]); \
96 G(r,3,v[ 3],v[ 7],v[11],v[15]); \
97 G(r,4,v[ 0],v[ 5],v[10],v[15]); \
98 G(r,5,v[ 1],v[ 6],v[11],v[12]); \
99 G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
100 G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
101 } while (0)
102
103static void blake2b_compress(struct blake2b_state *S,
104 const u8 block[BLAKE2B_BLOCKBYTES])
105{
106 u64 m[16];
107 u64 v[16];
108 size_t i;
109
110 for (i = 0; i < 16; ++i)
111 m[i] = get_unaligned_le64(block + i * sizeof(m[i]));
112
113 for (i = 0; i < 8; ++i)
114 v[i] = S->h[i];
115
116 v[ 8] = blake2b_IV[0];
117 v[ 9] = blake2b_IV[1];
118 v[10] = blake2b_IV[2];
119 v[11] = blake2b_IV[3];
120 v[12] = blake2b_IV[4] ^ S->t[0];
121 v[13] = blake2b_IV[5] ^ S->t[1];
122 v[14] = blake2b_IV[6] ^ S->f[0];
123 v[15] = blake2b_IV[7] ^ S->f[1];
124
125 ROUND(0);
126 ROUND(1);
127 ROUND(2);
128 ROUND(3);
129 ROUND(4);
130 ROUND(5);
131 ROUND(6);
132 ROUND(7);
133 ROUND(8);
134 ROUND(9);
135 ROUND(10);
136 ROUND(11);
137
138 for (i = 0; i < 8; ++i)
139 S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
140}
141
142#undef G
143#undef ROUND
144
145static void blake2b_update(struct blake2b_state *S, const void *pin, size_t inlen)
146{
147 const u8 *in = (const u8 *)pin;
148
149 if (inlen > 0) {
150 size_t left = S->buflen;
151 size_t fill = BLAKE2B_BLOCKBYTES - left;
152
153 if (inlen > fill) {
154 S->buflen = 0;
155 /* Fill buffer */
156 memcpy(S->buf + left, in, fill);
157 blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES);
158 /* Compress */
159 blake2b_compress(S, S->buf);
160 in += fill;
161 inlen -= fill;
162 while (inlen > BLAKE2B_BLOCKBYTES) {
163 blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES);
164 blake2b_compress(S, in);
165 in += BLAKE2B_BLOCKBYTES;
166 inlen -= BLAKE2B_BLOCKBYTES;
167 }
168 }
169 memcpy(S->buf + S->buflen, in, inlen);
170 S->buflen += inlen;
171 }
172}
173
David Sterba91d68932019-10-24 18:28:31 +0200174struct digest_tfm_ctx {
175 u8 key[BLAKE2B_KEYBYTES];
176 unsigned int keylen;
177};
178
179static int digest_setkey(struct crypto_shash *tfm, const u8 *key,
180 unsigned int keylen)
181{
182 struct digest_tfm_ctx *mctx = crypto_shash_ctx(tfm);
183
184 if (keylen == 0 || keylen > BLAKE2B_KEYBYTES) {
185 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
186 return -EINVAL;
187 }
188
189 memcpy(mctx->key, key, keylen);
190 mctx->keylen = keylen;
191
192 return 0;
193}
194
David Sterbae3749692019-11-12 11:20:25 +0100195static int blake2b_init(struct shash_desc *desc)
David Sterba91d68932019-10-24 18:28:31 +0200196{
197 struct digest_tfm_ctx *mctx = crypto_shash_ctx(desc->tfm);
198 struct blake2b_state *state = shash_desc_ctx(desc);
199 const int digestsize = crypto_shash_digestsize(desc->tfm);
200
David Sterbae3749692019-11-12 11:20:25 +0100201 memset(state, 0, sizeof(*state));
202 memcpy(state->h, blake2b_IV, sizeof(state->h));
203
204 /* Parameter block is all zeros except index 0, no xor for 1..7 */
205 state->h[0] ^= 0x01010000 | mctx->keylen << 8 | digestsize;
206
207 if (mctx->keylen) {
David Sterbae87e4842019-11-12 11:20:26 +0100208 /*
209 * Prefill the buffer with the key, next call to _update or
210 * _final will process it
211 */
212 memcpy(state->buf, mctx->key, mctx->keylen);
213 state->buflen = BLAKE2B_BLOCKBYTES;
David Sterbae3749692019-11-12 11:20:25 +0100214 }
David Sterba91d68932019-10-24 18:28:31 +0200215 return 0;
216}
217
218static int digest_update(struct shash_desc *desc, const u8 *data,
219 unsigned int length)
220{
221 struct blake2b_state *state = shash_desc_ctx(desc);
222
223 blake2b_update(state, data, length);
224 return 0;
225}
226
David Sterba086db432019-11-12 11:20:24 +0100227static int blake2b_final(struct shash_desc *desc, u8 *out)
David Sterba91d68932019-10-24 18:28:31 +0200228{
229 struct blake2b_state *state = shash_desc_ctx(desc);
230 const int digestsize = crypto_shash_digestsize(desc->tfm);
David Sterba086db432019-11-12 11:20:24 +0100231 size_t i;
David Sterba91d68932019-10-24 18:28:31 +0200232
David Sterba086db432019-11-12 11:20:24 +0100233 blake2b_increment_counter(state, state->buflen);
234 blake2b_set_lastblock(state);
235 /* Padding */
236 memset(state->buf + state->buflen, 0, BLAKE2B_BLOCKBYTES - state->buflen);
237 blake2b_compress(state, state->buf);
238
239 /* Avoid temporary buffer and switch the internal output to LE order */
240 for (i = 0; i < ARRAY_SIZE(state->h); i++)
241 __cpu_to_le64s(&state->h[i]);
242
243 memcpy(out, state->h, digestsize);
David Sterba91d68932019-10-24 18:28:31 +0200244 return 0;
245}
246
247static struct shash_alg blake2b_algs[] = {
248 {
249 .base.cra_name = "blake2b-160",
250 .base.cra_driver_name = "blake2b-160-generic",
251 .base.cra_priority = 100,
252 .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
253 .base.cra_blocksize = BLAKE2B_BLOCKBYTES,
254 .base.cra_ctxsize = sizeof(struct digest_tfm_ctx),
255 .base.cra_module = THIS_MODULE,
256 .digestsize = BLAKE2B_160_DIGEST_SIZE,
257 .setkey = digest_setkey,
David Sterbae3749692019-11-12 11:20:25 +0100258 .init = blake2b_init,
David Sterba91d68932019-10-24 18:28:31 +0200259 .update = digest_update,
David Sterba086db432019-11-12 11:20:24 +0100260 .final = blake2b_final,
David Sterba91d68932019-10-24 18:28:31 +0200261 .descsize = sizeof(struct blake2b_state),
262 }, {
263 .base.cra_name = "blake2b-256",
264 .base.cra_driver_name = "blake2b-256-generic",
265 .base.cra_priority = 100,
266 .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
267 .base.cra_blocksize = BLAKE2B_BLOCKBYTES,
268 .base.cra_ctxsize = sizeof(struct digest_tfm_ctx),
269 .base.cra_module = THIS_MODULE,
270 .digestsize = BLAKE2B_256_DIGEST_SIZE,
271 .setkey = digest_setkey,
David Sterbae3749692019-11-12 11:20:25 +0100272 .init = blake2b_init,
David Sterba91d68932019-10-24 18:28:31 +0200273 .update = digest_update,
David Sterba086db432019-11-12 11:20:24 +0100274 .final = blake2b_final,
David Sterba91d68932019-10-24 18:28:31 +0200275 .descsize = sizeof(struct blake2b_state),
276 }, {
277 .base.cra_name = "blake2b-384",
278 .base.cra_driver_name = "blake2b-384-generic",
279 .base.cra_priority = 100,
280 .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
281 .base.cra_blocksize = BLAKE2B_BLOCKBYTES,
282 .base.cra_ctxsize = sizeof(struct digest_tfm_ctx),
283 .base.cra_module = THIS_MODULE,
284 .digestsize = BLAKE2B_384_DIGEST_SIZE,
285 .setkey = digest_setkey,
David Sterbae3749692019-11-12 11:20:25 +0100286 .init = blake2b_init,
David Sterba91d68932019-10-24 18:28:31 +0200287 .update = digest_update,
David Sterba086db432019-11-12 11:20:24 +0100288 .final = blake2b_final,
David Sterba91d68932019-10-24 18:28:31 +0200289 .descsize = sizeof(struct blake2b_state),
290 }, {
291 .base.cra_name = "blake2b-512",
292 .base.cra_driver_name = "blake2b-512-generic",
293 .base.cra_priority = 100,
294 .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
295 .base.cra_blocksize = BLAKE2B_BLOCKBYTES,
296 .base.cra_ctxsize = sizeof(struct digest_tfm_ctx),
297 .base.cra_module = THIS_MODULE,
298 .digestsize = BLAKE2B_512_DIGEST_SIZE,
299 .setkey = digest_setkey,
David Sterbae3749692019-11-12 11:20:25 +0100300 .init = blake2b_init,
David Sterba91d68932019-10-24 18:28:31 +0200301 .update = digest_update,
David Sterba086db432019-11-12 11:20:24 +0100302 .final = blake2b_final,
David Sterba91d68932019-10-24 18:28:31 +0200303 .descsize = sizeof(struct blake2b_state),
304 }
305};
306
307static int __init blake2b_mod_init(void)
308{
David Sterba91d68932019-10-24 18:28:31 +0200309 return crypto_register_shashes(blake2b_algs, ARRAY_SIZE(blake2b_algs));
310}
311
312static void __exit blake2b_mod_fini(void)
313{
314 crypto_unregister_shashes(blake2b_algs, ARRAY_SIZE(blake2b_algs));
315}
316
317subsys_initcall(blake2b_mod_init);
318module_exit(blake2b_mod_fini);
319
320MODULE_AUTHOR("David Sterba <kdave@kernel.org>");
321MODULE_DESCRIPTION("BLAKE2b generic implementation");
322MODULE_LICENSE("GPL");
323MODULE_ALIAS_CRYPTO("blake2b-160");
324MODULE_ALIAS_CRYPTO("blake2b-160-generic");
325MODULE_ALIAS_CRYPTO("blake2b-256");
326MODULE_ALIAS_CRYPTO("blake2b-256-generic");
327MODULE_ALIAS_CRYPTO("blake2b-384");
328MODULE_ALIAS_CRYPTO("blake2b-384-generic");
329MODULE_ALIAS_CRYPTO("blake2b-512");
330MODULE_ALIAS_CRYPTO("blake2b-512-generic");