Debian Codegraph @

cipher/blake2.c

raw
1 /* blake2.c - BLAKE2b and BLAKE2s hash functions (RFC 7693)
2 * Copyright (C) 2017 Jussi Kivilinna <[email protected]>
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /* The code is based on public-domain/CC0 BLAKE2 reference implementation
21 * by Samual Neves, at https://github.com/BLAKE2/BLAKE2/tree/master/ref
22 * Copyright 2012, Samuel Neves <[email protected]>
23 */
24
25 #include <config.h>
26 #include <string.h>
27 #include "g10lib.h"
28 #include "bithelp.h"
29 #include "bufhelp.h"
30 #include "cipher.h"
31 #include "hwf-common.h"
32 #include "hash-common.h"
33
34 /* USE_AVX indicates whether to compile with Intel AVX code. */
35 #undef USE_AVX
36 #if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_AVX) && \
37 (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
38 defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
39 # define USE_AVX 1
40 #endif
41
42 /* USE_AVX2 indicates whether to compile with Intel AVX2 code. */
43 #undef USE_AVX2
44 #if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_AVX2) && \
45 (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
46 defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
47 # define USE_AVX2 1
48 #endif
49
50 /* USE_AVX512 indicates whether to compile with Intel AVX512 code. */
51 #undef USE_AVX512
52 #if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_AVX512) && \
53 (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
54 defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
55 # define USE_AVX512 1
56 #endif
57
58 /* AMD64 assembly implementations use SystemV ABI, ABI conversion and additional
59 * stack to store XMM6-XMM15 needed on Win64. */
60 #undef ASM_FUNC_ABI
61 #undef ASM_EXTRA_STACK
62 #if (defined(USE_AVX) || defined(USE_AVX2) || defined(USE_AVX512)) \
63 && defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS)
64 # define ASM_FUNC_ABI __attribute__((sysv_abi))
65 # define ASM_EXTRA_STACK (10 * 16)
66 #else
67 # define ASM_FUNC_ABI
68 # define ASM_EXTRA_STACK 0
69 #endif
70
71 #define BLAKE2B_BLOCKBYTES 128
72 #define BLAKE2B_OUTBYTES 64
73 #define BLAKE2B_KEYBYTES 64
74
75 #define BLAKE2S_BLOCKBYTES 64
76 #define BLAKE2S_OUTBYTES 32
77 #define BLAKE2S_KEYBYTES 32
78
79 typedef struct
80 {
81 u64 h[8];
82 u64 t[2];
83 u64 f[2];
84 } BLAKE2B_STATE;
85
86 struct blake2b_param_s
87 {
88 byte digest_length;
89 byte key_length;
90 byte fanout;
91 byte depth;
92 byte leaf_length[4];
93 byte node_offset[4];
94 byte xof_length[4];
95 byte node_depth;
96 byte inner_length;
97 byte reserved[14];
98 byte salt[16];
99 byte personal[16];
100 };
101
102 typedef struct BLAKE2B_CONTEXT_S
103 {
104 BLAKE2B_STATE state;
105 byte buf[BLAKE2B_BLOCKBYTES];
106 size_t buflen;
107 size_t outlen;
108 #ifdef USE_AVX2
109 unsigned int use_avx2:1;
110 #endif
111 #ifdef USE_AVX512
112 unsigned int use_avx512:1;
113 #endif
114 } BLAKE2B_CONTEXT;
115
116 typedef struct
117 {
118 u32 h[8];
119 u32 t[2];
120 u32 f[2];
121 } BLAKE2S_STATE;
122
123 struct blake2s_param_s
124 {
125 byte digest_length;
126 byte key_length;
127 byte fanout;
128 byte depth;
129 byte leaf_length[4];
130 byte node_offset[4];
131 byte xof_length[2];
132 byte node_depth;
133 byte inner_length;
134 /* byte reserved[0]; */
135 byte salt[8];
136 byte personal[8];
137 };
138
139 typedef struct BLAKE2S_CONTEXT_S
140 {
141 BLAKE2S_STATE state;
142 byte buf[BLAKE2S_BLOCKBYTES];
143 size_t buflen;
144 size_t outlen;
145 #ifdef USE_AVX
146 unsigned int use_avx:1;
147 #endif
148 #ifdef USE_AVX512
149 unsigned int use_avx512:1;
150 #endif
151 } BLAKE2S_CONTEXT;
152
153 typedef unsigned int (*blake2_transform_t)(void *S, const void *inblk,
154 size_t nblks);
155
156
157 static const u64 blake2b_IV[8] =
158 {
159 U64_C(0x6a09e667f3bcc908), U64_C(0xbb67ae8584caa73b),
160 U64_C(0x3c6ef372fe94f82b), U64_C(0xa54ff53a5f1d36f1),
161 U64_C(0x510e527fade682d1), U64_C(0x9b05688c2b3e6c1f),
162 U64_C(0x1f83d9abfb41bd6b), U64_C(0x5be0cd19137e2179)
163 };
164
165 static const u32 blake2s_IV[8] =
166 {
167 0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL,
168 0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL
169 };
170
171 static byte zero_block[BLAKE2B_BLOCKBYTES] = { 0, };
172
173
174 static void blake2_write(void *S, const void *inbuf, size_t inlen,
175 byte *tmpbuf, size_t *tmpbuflen, size_t blkbytes,
176 blake2_transform_t transform_fn)
177 {
178 const byte* in = inbuf;
179 unsigned int burn = 0;
180
181 if (inlen > 0)
182 {
183 size_t left = *tmpbuflen;
184 size_t fill = blkbytes - left;
185 size_t nblks;
186
187 if (inlen > fill)
188 {
189 if (fill > 0)
190 buf_cpy (tmpbuf + left, in, fill); /* Fill buffer */
191 left = 0;
192
193 burn = transform_fn (S, tmpbuf, 1); /* Increment counter + Compress */
194
195 in += fill;
196 inlen -= fill;
197
198 nblks = inlen / blkbytes - !(inlen % blkbytes);
199 if (nblks)
200 {
201 burn = transform_fn(S, in, nblks);
202 in += blkbytes * nblks;
203 inlen -= blkbytes * nblks;
204 }
205 }
206
207 gcry_assert (inlen > 0);
208
209 buf_cpy (tmpbuf + left, in, inlen);
210 *tmpbuflen = left + inlen;
211 }
212
213 if (burn)
214 _gcry_burn_stack (burn);
215
216 return;
217 }
218
219
220 static inline void blake2b_set_lastblock(BLAKE2B_STATE *S)
221 {
222 S->f[0] = U64_C(0xffffffffffffffff);
223 }
224
225 static inline int blake2b_is_lastblock(const BLAKE2B_STATE *S)
226 {
227 return S->f[0] != 0;
228 }
229
230 static inline void blake2b_increment_counter(BLAKE2B_STATE *S, const int inc)
231 {
232 S->t[0] += (u64)inc;
233 S->t[1] += (S->t[0] < (u64)inc) - (inc < 0);
234 }
235
236 static inline u64 rotr64(u64 x, u64 n)
237 {
238 return ((x >> (n & 63)) | (x << ((64 - n) & 63)));
239 }
240
241 static unsigned int blake2b_transform_generic(BLAKE2B_STATE *S,
242 const void *inblks,
243 size_t nblks)
244 {
245 static const byte blake2b_sigma[12][16] =
246 {
247 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
248 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
249 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
250 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
251 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
252 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
253 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
254 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
255 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
256 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 },
257 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
258 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
259 };
260 const byte* in = inblks;
261 u64 m[16];
262 u64 v[16];
263
264 while (nblks--)
265 {
266 /* Increment counter */
267 blake2b_increment_counter (S, BLAKE2B_BLOCKBYTES);
268
269 /* Compress */
270 m[0] = buf_get_le64 (in + 0 * sizeof(m[0]));
271 m[1] = buf_get_le64 (in + 1 * sizeof(m[0]));
272 m[2] = buf_get_le64 (in + 2 * sizeof(m[0]));
273 m[3] = buf_get_le64 (in + 3 * sizeof(m[0]));
274 m[4] = buf_get_le64 (in + 4 * sizeof(m[0]));
275 m[5] = buf_get_le64 (in + 5 * sizeof(m[0]));
276 m[6] = buf_get_le64 (in + 6 * sizeof(m[0]));
277 m[7] = buf_get_le64 (in + 7 * sizeof(m[0]));
278 m[8] = buf_get_le64 (in + 8 * sizeof(m[0]));
279 m[9] = buf_get_le64 (in + 9 * sizeof(m[0]));
280 m[10] = buf_get_le64 (in + 10 * sizeof(m[0]));
281 m[11] = buf_get_le64 (in + 11 * sizeof(m[0]));
282 m[12] = buf_get_le64 (in + 12 * sizeof(m[0]));
283 m[13] = buf_get_le64 (in + 13 * sizeof(m[0]));
284 m[14] = buf_get_le64 (in + 14 * sizeof(m[0]));
285 m[15] = buf_get_le64 (in + 15 * sizeof(m[0]));
286
287 v[ 0] = S->h[0];
288 v[ 1] = S->h[1];
289 v[ 2] = S->h[2];
290 v[ 3] = S->h[3];
291 v[ 4] = S->h[4];
292 v[ 5] = S->h[5];
293 v[ 6] = S->h[6];
294 v[ 7] = S->h[7];
295 v[ 8] = blake2b_IV[0];
296 v[ 9] = blake2b_IV[1];
297 v[10] = blake2b_IV[2];
298 v[11] = blake2b_IV[3];
299 v[12] = blake2b_IV[4] ^ S->t[0];
300 v[13] = blake2b_IV[5] ^ S->t[1];
301 v[14] = blake2b_IV[6] ^ S->f[0];
302 v[15] = blake2b_IV[7] ^ S->f[1];
303
304 #define G(r,i,a,b,c,d) \
305 do { \
306 a = a + b + m[blake2b_sigma[r][2*i+0]]; \
307 d = rotr64(d ^ a, 32); \
308 c = c + d; \
309 b = rotr64(b ^ c, 24); \
310 a = a + b + m[blake2b_sigma[r][2*i+1]]; \
311 d = rotr64(d ^ a, 16); \
312 c = c + d; \
313 b = rotr64(b ^ c, 63); \
314 } while(0)
315
316 #define ROUND(r) \
317 do { \
318 G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
319 G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
320 G(r,2,v[ 2],v[ 6],v[10],v[14]); \
321 G(r,3,v[ 3],v[ 7],v[11],v[15]); \
322 G(r,4,v[ 0],v[ 5],v[10],v[15]); \
323 G(r,5,v[ 1],v[ 6],v[11],v[12]); \
324 G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
325 G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
326 } while(0)
327
328 ROUND(0);
329 ROUND(1);
330 ROUND(2);
331 ROUND(3);
332 ROUND(4);
333 ROUND(5);
334 ROUND(6);
335 ROUND(7);
336 ROUND(8);
337 ROUND(9);
338 ROUND(10);
339 ROUND(11);
340
341 #undef G
342 #undef ROUND
343
344 S->h[0] = S->h[0] ^ v[0] ^ v[0 + 8];
345 S->h[1] = S->h[1] ^ v[1] ^ v[1 + 8];
346 S->h[2] = S->h[2] ^ v[2] ^ v[2 + 8];
347 S->h[3] = S->h[3] ^ v[3] ^ v[3 + 8];
348 S->h[4] = S->h[4] ^ v[4] ^ v[4 + 8];
349 S->h[5] = S->h[5] ^ v[5] ^ v[5 + 8];
350 S->h[6] = S->h[6] ^ v[6] ^ v[6 + 8];
351 S->h[7] = S->h[7] ^ v[7] ^ v[7 + 8];
352
353 in += BLAKE2B_BLOCKBYTES;
354 }
355
356 return sizeof(void *) * 4 + sizeof(u64) * 16 * 2;
357 }
358
359 #ifdef USE_AVX2
360 unsigned int _gcry_blake2b_transform_amd64_avx2(BLAKE2B_STATE *S,
361 const void *inblks,
362 size_t nblks) ASM_FUNC_ABI;
363 #endif
364
365 #ifdef USE_AVX512
366 unsigned int _gcry_blake2b_transform_amd64_avx512(BLAKE2B_STATE *S,
367 const void *inblks,
368 size_t nblks) ASM_FUNC_ABI;
369 #endif
370
371 static unsigned int blake2b_transform(void *ctx, const void *inblks,
372 size_t nblks)
373 {
374 BLAKE2B_CONTEXT *c = ctx;
375 unsigned int nburn;
376
377 if (0)
378 {}
379 #ifdef USE_AVX512
380 else if (c->use_avx512)
381 nburn = _gcry_blake2b_transform_amd64_avx512(&c->state, inblks, nblks);
382 #endif
383 #ifdef USE_AVX2
384 else if (c->use_avx2)
385 nburn = _gcry_blake2b_transform_amd64_avx2(&c->state, inblks, nblks);
386 #endif
387 else
388 nburn = blake2b_transform_generic(&c->state, inblks, nblks);
389
390 if (nburn)
391 nburn += ASM_EXTRA_STACK;
392
393 return nburn;
394 }
395
396 static void blake2b_final(void *ctx)
397 {
398 BLAKE2B_CONTEXT *c = ctx;
399 BLAKE2B_STATE *S = &c->state;
400 unsigned int burn;
401 size_t i;
402
403 gcry_assert (sizeof(c->buf) >= c->outlen);
404 if (blake2b_is_lastblock(S))
405 return;
406
407 if (c->buflen < BLAKE2B_BLOCKBYTES)
408 memset (c->buf + c->buflen, 0, BLAKE2B_BLOCKBYTES - c->buflen); /* Padding */
409 blake2b_set_lastblock (S);
410 blake2b_increment_counter (S, (int)c->buflen - BLAKE2B_BLOCKBYTES);
411 burn = blake2b_transform (ctx, c->buf, 1);
412
413 /* Output full hash to buffer */
414 for (i = 0; i < 8; ++i)
415 buf_put_le64 (c->buf + sizeof(S->h[i]) * i, S->h[i]);
416
417 /* Zero out extra buffer bytes. */
418 if (c->outlen < sizeof(c->buf))
419 memset (c->buf + c->outlen, 0, sizeof(c->buf) - c->outlen);
420
421 if (burn)
422 _gcry_burn_stack (burn);
423 }
424
425 static byte *blake2b_read(void *ctx)
426 {
427 BLAKE2B_CONTEXT *c = ctx;
428 return c->buf;
429 }
430
431 static void blake2b_write(void *ctx, const void *inbuf, size_t inlen)
432 {
433 BLAKE2B_CONTEXT *c = ctx;
434 BLAKE2B_STATE *S = &c->state;
435 blake2_write(S, inbuf, inlen, c->buf, &c->buflen, BLAKE2B_BLOCKBYTES,
436 blake2b_transform);
437 }
438
439 static inline void blake2b_init_param(BLAKE2B_STATE *S,
440 const struct blake2b_param_s *P)
441 {
442 const byte *p = (const byte *)P;
443 size_t i;
444
445 /* init xors IV with input parameter block */
446
447 /* IV XOR ParamBlock */
448 for (i = 0; i < 8; ++i)
449 S->h[i] = blake2b_IV[i] ^ buf_get_le64(p + sizeof(S->h[i]) * i);
450 }
451
452 static inline gcry_err_code_t blake2b_init(BLAKE2B_CONTEXT *ctx,
453 const byte *key, size_t keylen)
454 {
455 struct blake2b_param_s P[1] = { { 0, } };
456 BLAKE2B_STATE *S = &ctx->state;
457
458 if (!ctx->outlen || ctx->outlen > BLAKE2B_OUTBYTES)
459 return GPG_ERR_INV_ARG;
460 if (sizeof(P[0]) != sizeof(u64) * 8)
461 return GPG_ERR_INTERNAL;
462 if (keylen && (!key || keylen > BLAKE2B_KEYBYTES))
463 return GPG_ERR_INV_KEYLEN;
464
465 P->digest_length = ctx->outlen;
466 P->key_length = keylen;
467 P->fanout = 1;
468 P->depth = 1;
469
470 blake2b_init_param (S, P);
471 wipememory (P, sizeof(P));
472
473 if (key)
474 {
475 blake2b_write (ctx, key, keylen);
476 blake2b_write (ctx, zero_block, BLAKE2B_BLOCKBYTES - keylen);
477 }
478
479 return 0;
480 }
481
482 static gcry_err_code_t blake2b_init_ctx(void *ctx, unsigned int flags,
483 const byte *key, size_t keylen,
484 unsigned int dbits)
485 {
486 BLAKE2B_CONTEXT *c = ctx;
487 unsigned int features = _gcry_get_hw_features ();
488
489 (void)features;
490 (void)flags;
491
492 memset (c, 0, sizeof (*c));
493
494 #ifdef USE_AVX2
495 c->use_avx2 = !!(features & HWF_INTEL_AVX2)
496 && !(_gcry_hwf_x86_cpu_details()->prefer_gpr_over_scalar_int_vector);
497 #endif
498 #ifdef USE_AVX512
499 c->use_avx512 = !!(features & HWF_INTEL_AVX512)
500 && !(_gcry_hwf_x86_cpu_details()->prefer_gpr_over_scalar_int_vector);
501 #endif
502
503 c->outlen = dbits / 8;
504 c->buflen = 0;
505 return blake2b_init(c, key, keylen);
506 }
507
508 /* Variable-length Hash Function H'. */
509 gcry_err_code_t
510 blake2b_vl_hash (const void *in, size_t inlen, size_t outputlen, void *output)
511 {
512 gcry_err_code_t ec;
513 BLAKE2B_CONTEXT ctx;
514 unsigned char buf[4];
515
516 ec = blake2b_init_ctx (&ctx, 0, NULL, 0,
517 (outputlen < 64 ? outputlen: 64)*8);
518 if (ec)
519 return ec;
520
521 buf_put_le32 (buf, outputlen);
522 blake2b_write (&ctx, buf, 4);
523 blake2b_write (&ctx, in, inlen);
524 blake2b_final (&ctx);
525
526 if (outputlen <= 64)
527 memcpy (output, ctx.buf, outputlen);
528 else
529 {
530 int r = (outputlen-1)/32 - 1;
531 unsigned int remained = outputlen - 32*r;
532 int i;
533 unsigned char d[64];
534
535 i = 0;
536 while (1)
537 {
538 memcpy (d, ctx.buf, 64);
539 memcpy ((unsigned char *)output+i*32, d, 32);
540
541 if (++i >= r)
542 break;
543
544 ec = blake2b_init_ctx (&ctx, 0, NULL, 0, 64*8);
545 if (ec)
546 return ec;
547
548 blake2b_write (&ctx, d, 64);
549 blake2b_final (&ctx);
550 }
551
552 ec = blake2b_init_ctx (&ctx, 0, NULL, 0, remained*8);
553 if (ec)
554 return ec;
555
556 blake2b_write (&ctx, d, 64);
557 blake2b_final (&ctx);
558
559 memcpy ((unsigned char *)output+r*32, ctx.buf, remained);
560 }
561
562 wipememory (buf, sizeof (buf));
563 wipememory (&ctx, sizeof (ctx));
564 return 0;
565 }
566
567 static inline void blake2s_set_lastblock(BLAKE2S_STATE *S)
568 {
569 S->f[0] = 0xFFFFFFFFUL;
570 }
571
572 static inline int blake2s_is_lastblock(BLAKE2S_STATE *S)
573 {
574 return S->f[0] != 0;
575 }
576
577 static inline void blake2s_increment_counter(BLAKE2S_STATE *S, const int inc)
578 {
579 S->t[0] += (u32)inc;
580 S->t[1] += (S->t[0] < (u32)inc) - (inc < 0);
581 }
582
583 static unsigned int blake2s_transform_generic(BLAKE2S_STATE *S,
584 const void *inblks,
585 size_t nblks)
586 {
587 static const byte blake2s_sigma[10][16] =
588 {
589 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
590 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
591 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
592 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
593 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
594 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
595 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
596 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
597 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
598 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 },
599 };
600 unsigned int burn = 0;
601 const byte* in = inblks;
602 u32 m[16];
603 u32 v[16];
604
605 while (nblks--)
606 {
607 /* Increment counter */
608 blake2s_increment_counter (S, BLAKE2S_BLOCKBYTES);
609
610 /* Compress */
611 m[0] = buf_get_le32 (in + 0 * sizeof(m[0]));
612 m[1] = buf_get_le32 (in + 1 * sizeof(m[0]));
613 m[2] = buf_get_le32 (in + 2 * sizeof(m[0]));
614 m[3] = buf_get_le32 (in + 3 * sizeof(m[0]));
615 m[4] = buf_get_le32 (in + 4 * sizeof(m[0]));
616 m[5] = buf_get_le32 (in + 5 * sizeof(m[0]));
617 m[6] = buf_get_le32 (in + 6 * sizeof(m[0]));
618 m[7] = buf_get_le32 (in + 7 * sizeof(m[0]));
619 m[8] = buf_get_le32 (in + 8 * sizeof(m[0]));
620 m[9] = buf_get_le32 (in + 9 * sizeof(m[0]));
621 m[10] = buf_get_le32 (in + 10 * sizeof(m[0]));
622 m[11] = buf_get_le32 (in + 11 * sizeof(m[0]));
623 m[12] = buf_get_le32 (in + 12 * sizeof(m[0]));
624 m[13] = buf_get_le32 (in + 13 * sizeof(m[0]));
625 m[14] = buf_get_le32 (in + 14 * sizeof(m[0]));
626 m[15] = buf_get_le32 (in + 15 * sizeof(m[0]));
627
628 v[ 0] = S->h[0];
629 v[ 1] = S->h[1];
630 v[ 2] = S->h[2];
631 v[ 3] = S->h[3];
632 v[ 4] = S->h[4];
633 v[ 5] = S->h[5];
634 v[ 6] = S->h[6];
635 v[ 7] = S->h[7];
636 v[ 8] = blake2s_IV[0];
637 v[ 9] = blake2s_IV[1];
638 v[10] = blake2s_IV[2];
639 v[11] = blake2s_IV[3];
640 v[12] = S->t[0] ^ blake2s_IV[4];
641 v[13] = S->t[1] ^ blake2s_IV[5];
642 v[14] = S->f[0] ^ blake2s_IV[6];
643 v[15] = S->f[1] ^ blake2s_IV[7];
644
645 #define G(r,i,a,b,c,d) \
646 do { \
647 a = a + b + m[blake2s_sigma[r][2*i+0]]; \
648 d = ror(d ^ a, 16); \
649 c = c + d; \
650 b = ror(b ^ c, 12); \
651 a = a + b + m[blake2s_sigma[r][2*i+1]]; \
652 d = ror(d ^ a, 8); \
653 c = c + d; \
654 b = ror(b ^ c, 7); \
655 } while(0)
656
657 #define ROUND(r) \
658 do { \
659 G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
660 G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
661 G(r,2,v[ 2],v[ 6],v[10],v[14]); \
662 G(r,3,v[ 3],v[ 7],v[11],v[15]); \
663 G(r,4,v[ 0],v[ 5],v[10],v[15]); \
664 G(r,5,v[ 1],v[ 6],v[11],v[12]); \
665 G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
666 G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
667 } while(0)
668
669 ROUND(0);
670 ROUND(1);
671 ROUND(2);
672 ROUND(3);
673 ROUND(4);
674 ROUND(5);
675 ROUND(6);
676 ROUND(7);
677 ROUND(8);
678 ROUND(9);
679
680 #undef G
681 #undef ROUND
682
683 S->h[0] = S->h[0] ^ v[0] ^ v[0 + 8];
684 S->h[1] = S->h[1] ^ v[1] ^ v[1 + 8];
685 S->h[2] = S->h[2] ^ v[2] ^ v[2 + 8];
686 S->h[3] = S->h[3] ^ v[3] ^ v[3 + 8];
687 S->h[4] = S->h[4] ^ v[4] ^ v[4 + 8];
688 S->h[5] = S->h[5] ^ v[5] ^ v[5 + 8];
689 S->h[6] = S->h[6] ^ v[6] ^ v[6 + 8];
690 S->h[7] = S->h[7] ^ v[7] ^ v[7 + 8];
691
692 in += BLAKE2S_BLOCKBYTES;
693 }
694
695 return burn;
696 }
697
698 #ifdef USE_AVX
699 unsigned int _gcry_blake2s_transform_amd64_avx(BLAKE2S_STATE *S,
700 const void *inblks,
701 size_t nblks) ASM_FUNC_ABI;
702 #endif
703
704 #ifdef USE_AVX512
705 unsigned int _gcry_blake2s_transform_amd64_avx512(BLAKE2S_STATE *S,
706 const void *inblks,
707 size_t nblks) ASM_FUNC_ABI;
708 #endif
709
710 static unsigned int blake2s_transform(void *ctx, const void *inblks,
711 size_t nblks)
712 {
713 BLAKE2S_CONTEXT *c = ctx;
714 unsigned int nburn;
715
716 if (0)
717 { }
718 #ifdef USE_AVX512
719 else if (c->use_avx512)
720 nburn = _gcry_blake2s_transform_amd64_avx512(&c->state, inblks, nblks);
721 #endif
722 #ifdef USE_AVX
723 else if (c->use_avx)
724 nburn = _gcry_blake2s_transform_amd64_avx(&c->state, inblks, nblks);
725 #endif
726 else
727 nburn = blake2s_transform_generic(&c->state, inblks, nblks);
728
729 if (nburn)
730 nburn += ASM_EXTRA_STACK;
731
732 return nburn;
733 }
734
735 static void blake2s_final(void *ctx)
736 {
737 BLAKE2S_CONTEXT *c = ctx;
738 BLAKE2S_STATE *S = &c->state;
739 unsigned int burn;
740 size_t i;
741
742 gcry_assert (sizeof(c->buf) >= c->outlen);
743 if (blake2s_is_lastblock(S))
744 return;
745
746 if (c->buflen < BLAKE2S_BLOCKBYTES)
747 memset (c->buf + c->buflen, 0, BLAKE2S_BLOCKBYTES - c->buflen); /* Padding */
748 blake2s_set_lastblock (S);
749 blake2s_increment_counter (S, (int)c->buflen - BLAKE2S_BLOCKBYTES);
750 burn = blake2s_transform (ctx, c->buf, 1);
751
752 /* Output full hash to buffer */
753 for (i = 0; i < 8; ++i)
754 buf_put_le32 (c->buf + sizeof(S->h[i]) * i, S->h[i]);
755
756 /* Zero out extra buffer bytes. */
757 if (c->outlen < sizeof(c->buf))
758 memset (c->buf + c->outlen, 0, sizeof(c->buf) - c->outlen);
759
760 if (burn)
761 _gcry_burn_stack (burn);
762 }
763
764 static byte *blake2s_read(void *ctx)
765 {
766 BLAKE2S_CONTEXT *c = ctx;
767 return c->buf;
768 }
769
770 static void blake2s_write(void *ctx, const void *inbuf, size_t inlen)
771 {
772 BLAKE2S_CONTEXT *c = ctx;
773 BLAKE2S_STATE *S = &c->state;
774 blake2_write(S, inbuf, inlen, c->buf, &c->buflen, BLAKE2S_BLOCKBYTES,
775 blake2s_transform);
776 }
777
778 static inline void blake2s_init_param(BLAKE2S_STATE *S,
779 const struct blake2s_param_s *P)
780 {
781 const byte *p = (const byte *)P;
782 size_t i;
783
784 /* init2 xors IV with input parameter block */
785
786 /* IV XOR ParamBlock */
787 for (i = 0; i < 8; ++i)
788 S->h[i] ^= blake2s_IV[i] ^ buf_get_le32(&p[i * 4]);
789 }
790
791 static inline gcry_err_code_t blake2s_init(BLAKE2S_CONTEXT *ctx,
792 const byte *key, size_t keylen)
793 {
794 struct blake2s_param_s P[1] = { { 0, } };
795 BLAKE2S_STATE *S = &ctx->state;
796
797 if (!ctx->outlen || ctx->outlen > BLAKE2S_OUTBYTES)
798 return GPG_ERR_INV_ARG;
799 if (sizeof(P[0]) != sizeof(u32) * 8)
800 return GPG_ERR_INTERNAL;
801 if (keylen && (!key || keylen > BLAKE2S_KEYBYTES))
802 return GPG_ERR_INV_KEYLEN;
803
804 P->digest_length = ctx->outlen;
805 P->key_length = keylen;
806 P->fanout = 1;
807 P->depth = 1;
808
809 blake2s_init_param (S, P);
810 wipememory (P, sizeof(P));
811
812 if (key)
813 {
814 blake2s_write (ctx, key, keylen);
815 blake2s_write (ctx, zero_block, BLAKE2S_BLOCKBYTES - keylen);
816 }
817
818 return 0;
819 }
820
821 static gcry_err_code_t blake2s_init_ctx(void *ctx, unsigned int flags,
822 const byte *key, size_t keylen,
823 unsigned int dbits)
824 {
825 BLAKE2S_CONTEXT *c = ctx;
826 unsigned int features = _gcry_get_hw_features ();
827
828 (void)features;
829 (void)flags;
830
831 memset (c, 0, sizeof (*c));
832
833 #ifdef USE_AVX
834 c->use_avx = !!(features & HWF_INTEL_AVX)
835 && !(_gcry_hwf_x86_cpu_details()->prefer_gpr_over_scalar_int_vector);
836 #endif
837 #ifdef USE_AVX512
838 c->use_avx512 = !!(features & HWF_INTEL_AVX512)
839 && !(_gcry_hwf_x86_cpu_details()->prefer_gpr_over_scalar_int_vector);
840 #endif
841
842 c->outlen = dbits / 8;
843 c->buflen = 0;
844 return blake2s_init(c, key, keylen);
845 }
846
847 /* Selftests from "RFC 7693, Appendix E. BLAKE2b and BLAKE2s Self-Test
848 * Module C Source". */
849 static void selftest_seq(byte *out, size_t len, u32 seed)
850 {
851 size_t i;
852 u32 t, a, b;
853
854 a = 0xDEAD4BAD * seed;
855 b = 1;
856
857 for (i = 0; i < len; i++)
858 {
859 t = a + b;
860 a = b;
861 b = t;
862 out[i] = (t >> 24) & 0xFF;
863 }
864 }
865
866 static gpg_err_code_t
867 selftests_blake2b (int algo, int extended, selftest_report_func_t report)
868 {
869 static const byte blake2b_res[32] =
870 {
871 0xC2, 0x3A, 0x78, 0x00, 0xD9, 0x81, 0x23, 0xBD,
872 0x10, 0xF5, 0x06, 0xC6, 0x1E, 0x29, 0xDA, 0x56,
873 0x03, 0xD7, 0x63, 0xB8, 0xBB, 0xAD, 0x2E, 0x73,
874 0x7F, 0x5E, 0x76, 0x5A, 0x7B, 0xCC, 0xD4, 0x75
875 };
876 static const size_t b2b_md_len[4] = { 20, 32, 48, 64 };
877 static const size_t b2b_in_len[6] = { 0, 3, 128, 129, 255, 1024 };
878 size_t i, j, outlen, inlen;
879 byte in[1024], key[64];
880 BLAKE2B_CONTEXT ctx;
881 BLAKE2B_CONTEXT ctx2;
882 const char *what;
883 const char *errtxt;
884
885 (void)extended;
886
887 what = "rfc7693 BLAKE2b selftest";
888
889 /* 256-bit hash for testing */
890 if (blake2b_init_ctx(&ctx, 0, NULL, 0, 32 * 8))
891 {
892 errtxt = "init failed";
893 goto failed;
894 }
895
896 for (i = 0; i < 4; i++)
897 {
898 outlen = b2b_md_len[i];
899 for (j = 0; j < 6; j++)
900 {
901 inlen = b2b_in_len[j];
902
903 selftest_seq(in, inlen, inlen); /* unkeyed hash */
904 blake2b_init_ctx(&ctx2, 0, NULL, 0, outlen * 8);
905 blake2b_write(&ctx2, in, inlen);
906 blake2b_final(&ctx2);
907 blake2b_write(&ctx, ctx2.buf, outlen); /* hash the hash */
908
909 selftest_seq(key, outlen, outlen); /* keyed hash */
910 blake2b_init_ctx(&ctx2, 0, key, outlen, outlen * 8);
911 blake2b_write(&ctx2, in, inlen);
912 blake2b_final(&ctx2);
913 blake2b_write(&ctx, ctx2.buf, outlen); /* hash the hash */
914 }
915 }
916
917 /* compute and compare the hash of hashes */
918 blake2b_final(&ctx);
919 for (i = 0; i < 32; i++)
920 {
921 if (ctx.buf[i] != blake2b_res[i])
922 {
923 errtxt = "digest mismatch";
924 goto failed;
925 }
926 }
927
928 return 0;
929
930 failed:
931 if (report)
932 report ("digest", algo, what, errtxt);
933 return GPG_ERR_SELFTEST_FAILED;
934 }
935
936 static gpg_err_code_t
937 selftests_blake2s (int algo, int extended, selftest_report_func_t report)
938 {
939 static const byte blake2s_res[32] =
940 {
941 0x6A, 0x41, 0x1F, 0x08, 0xCE, 0x25, 0xAD, 0xCD,
942 0xFB, 0x02, 0xAB, 0xA6, 0x41, 0x45, 0x1C, 0xEC,
943 0x53, 0xC5, 0x98, 0xB2, 0x4F, 0x4F, 0xC7, 0x87,
944 0xFB, 0xDC, 0x88, 0x79, 0x7F, 0x4C, 0x1D, 0xFE
945 };
946 static const size_t b2s_md_len[4] = { 16, 20, 28, 32 };
947 static const size_t b2s_in_len[6] = { 0, 3, 64, 65, 255, 1024 };
948 size_t i, j, outlen, inlen;
949 byte in[1024], key[32];
950 BLAKE2S_CONTEXT ctx;
951 BLAKE2S_CONTEXT ctx2;
952 const char *what;
953 const char *errtxt;
954
955 (void)extended;
956
957 what = "rfc7693 BLAKE2s selftest";
958
959 /* 256-bit hash for testing */
960 if (blake2s_init_ctx(&ctx, 0, NULL, 0, 32 * 8))
961 {
962 errtxt = "init failed";
963 goto failed;
964 }
965
966 for (i = 0; i < 4; i++)
967 {
968 outlen = b2s_md_len[i];
969 for (j = 0; j < 6; j++)
970 {
971 inlen = b2s_in_len[j];
972
973 selftest_seq(in, inlen, inlen); /* unkeyed hash */
974 blake2s_init_ctx(&ctx2, 0, NULL, 0, outlen * 8);
975 blake2s_write(&ctx2, in, inlen);
976 blake2s_final(&ctx2);
977 blake2s_write(&ctx, ctx2.buf, outlen); /* hash the hash */
978
979 selftest_seq(key, outlen, outlen); /* keyed hash */
980 blake2s_init_ctx(&ctx2, 0, key, outlen, outlen * 8);
981 blake2s_write(&ctx2, in, inlen);
982 blake2s_final(&ctx2);
983 blake2s_write(&ctx, ctx2.buf, outlen); /* hash the hash */
984 }
985 }
986
987 /* compute and compare the hash of hashes */
988 blake2s_final(&ctx);
989 for (i = 0; i < 32; i++)
990 {
991 if (ctx.buf[i] != blake2s_res[i])
992 {
993 errtxt = "digest mismatch";
994 goto failed;
995 }
996 }
997
998 return 0;
999
1000 failed:
1001 if (report)
1002 report ("digest", algo, what, errtxt);
1003 return GPG_ERR_SELFTEST_FAILED;
1004 }
1005
1006
1007 gcry_err_code_t _gcry_blake2_init_with_key(void *ctx, unsigned int flags,
1008 const unsigned char *key,
1009 size_t keylen, int algo)
1010 {
1011 gcry_err_code_t rc;
1012 switch (algo)
1013 {
1014 case GCRY_MD_BLAKE2B_512:
1015 rc = blake2b_init_ctx (ctx, flags, key, keylen, 512);
1016 break;
1017 case GCRY_MD_BLAKE2B_384:
1018 rc = blake2b_init_ctx (ctx, flags, key, keylen, 384);
1019 break;
1020 case GCRY_MD_BLAKE2B_256:
1021 rc = blake2b_init_ctx (ctx, flags, key, keylen, 256);
1022 break;
1023 case GCRY_MD_BLAKE2B_160:
1024 rc = blake2b_init_ctx (ctx, flags, key, keylen, 160);
1025 break;
1026 case GCRY_MD_BLAKE2S_256:
1027 rc = blake2s_init_ctx (ctx, flags, key, keylen, 256);
1028 break;
1029 case GCRY_MD_BLAKE2S_224:
1030 rc = blake2s_init_ctx (ctx, flags, key, keylen, 224);
1031 break;
1032 case GCRY_MD_BLAKE2S_160:
1033 rc = blake2s_init_ctx (ctx, flags, key, keylen, 160);
1034 break;
1035 case GCRY_MD_BLAKE2S_128:
1036 rc = blake2s_init_ctx (ctx, flags, key, keylen, 128);
1037 break;
1038 default:
1039 rc = GPG_ERR_DIGEST_ALGO;
1040 break;
1041 }
1042
1043 return rc;
1044 }
1045
1046
1047 #define DEFINE_BLAKE2_VARIANT(bs, BS, dbits, oid_branch) \
1048 static void blake2##bs##_##dbits##_init(void *ctx, unsigned int flags) \
1049 { \
1050 int err = blake2##bs##_init_ctx (ctx, flags, NULL, 0, dbits); \
1051 gcry_assert (err == 0); \
1052 } \
1053 static void \
1054 _gcry_blake2##bs##_##dbits##_hash_buffers(void *outbuf, size_t nbytes, \
1055 const gcry_buffer_t *iov, int iovcnt) \
1056 { \
1057 BLAKE2##BS##_CONTEXT hd; \
1058 (void)nbytes; \
1059 blake2##bs##_##dbits##_init (&hd, 0); \
1060 for (;iovcnt > 0; iov++, iovcnt--) \
1061 blake2##bs##_write (&hd, (const char*)iov[0].data + iov[0].off, \
1062 iov[0].len); \
1063 blake2##bs##_final (&hd); \
1064 memcpy (outbuf, blake2##bs##_read (&hd), dbits / 8); \
1065 } \
1066 static const byte blake2##bs##_##dbits##_asn[] = { 0x30 }; \
1067 static const gcry_md_oid_spec_t oid_spec_blake2##bs##_##dbits[] = \
1068 { \
1069 { " 1.3.6.1.4.1.1722.12.2." oid_branch }, \
1070 { NULL } \
1071 }; \
1072 const gcry_md_spec_t _gcry_digest_spec_blake2##bs##_##dbits = \
1073 { \
1074 GCRY_MD_BLAKE2##BS##_##dbits, {0, 0}, \
1075 "BLAKE2" #BS "_" #dbits, blake2##bs##_##dbits##_asn, \
1076 DIM (blake2##bs##_##dbits##_asn), oid_spec_blake2##bs##_##dbits, \
1077 dbits / 8, blake2##bs##_##dbits##_init, blake2##bs##_write, \
1078 blake2##bs##_final, blake2##bs##_read, NULL, \
1079 _gcry_blake2##bs##_##dbits##_hash_buffers, \
1080 sizeof (BLAKE2##BS##_CONTEXT), selftests_blake2##bs \
1081 };
1082
1083 DEFINE_BLAKE2_VARIANT(b, B, 512, "1.16")
1084 DEFINE_BLAKE2_VARIANT(b, B, 384, "1.12")
1085 DEFINE_BLAKE2_VARIANT(b, B, 256, "1.8")
1086 DEFINE_BLAKE2_VARIANT(b, B, 160, "1.5")
1087
1088 DEFINE_BLAKE2_VARIANT(s, S, 256, "2.8")
1089 DEFINE_BLAKE2_VARIANT(s, S, 224, "2.7")
1090 DEFINE_BLAKE2_VARIANT(s, S, 160, "2.5")
1091 DEFINE_BLAKE2_VARIANT(s, S, 128, "2.4")