2610477d378393dcd114bf3071454d1bdbdcb920
[trex.git] /
1 /*
2    BLAKE2 reference source code package - reference C implementations
3
4    Written in 2012 by Samuel Neves <[email protected]>
5
6    To the extent possible under law, the author(s) have dedicated all copyright
7    and related and neighboring rights to this software to the public domain
8    worldwide. This software is distributed without any warranty.
9
10    You should have received a copy of the CC0 Public Domain Dedication along with
11    this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
12 */
13
14 #include <stdint.h>
15 #include <string.h>
16 #include <stdio.h>
17
18 #include "blake2.h"
19 #include "blake2-impl.h"
20
21 static const uint64_t blake2b_IV[8] =
22 {
23   0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
24   0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
25   0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
26   0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
27 };
28
29 static const uint8_t blake2b_sigma[12][16] =
30 {
31   {  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15 } ,
32   { 14, 10,  4,  8,  9, 15, 13,  6,  1, 12,  0,  2, 11,  7,  5,  3 } ,
33   { 11,  8, 12,  0,  5,  2, 15, 13, 10, 14,  3,  6,  7,  1,  9,  4 } ,
34   {  7,  9,  3,  1, 13, 12, 11, 14,  2,  6,  5, 10,  4,  0, 15,  8 } ,
35   {  9,  0,  5,  7,  2,  4, 10, 15, 14,  1, 11, 12,  6,  8,  3, 13 } ,
36   {  2, 12,  6, 10,  0, 11,  8,  3,  4, 13,  7,  5, 15, 14,  1,  9 } ,
37   { 12,  5,  1, 15, 14, 13,  4, 10,  0,  7,  6,  3,  9,  2,  8, 11 } ,
38   { 13, 11,  7, 14, 12,  1,  3,  9,  5,  0, 15,  4,  8,  6,  2, 10 } ,
39   {  6, 15, 14,  9, 11,  3,  0,  8, 12,  2, 13,  7,  1,  4, 10,  5 } ,
40   { 10,  2,  8,  4,  7,  6,  1,  5, 15, 11,  9, 14,  3, 12, 13 , 0 } ,
41   {  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15 } ,
42   { 14, 10,  4,  8,  9, 15, 13,  6,  1, 12,  0,  2, 11,  7,  5,  3 }
43 };
44
45
46 /* LCOV_EXCL_START */
47 static inline int blake2b_set_lastnode( blake2b_state *S )
48 {
49   S->f[1] = ~0ULL;
50   return 0;
51 }
52 /* LCOV_EXCL_STOP */
53 #if 0
54 static inline int blake2b_clear_lastnode( blake2b_state *S )
55 {
56   S->f[1] = 0ULL;
57   return 0;
58 }
59 #endif
60 /* Some helper functions, not necessarily useful */
61 static inline int blake2b_set_lastblock( blake2b_state *S )
62 {
63   if( S->last_node ) blake2b_set_lastnode( S );
64
65   S->f[0] = ~0ULL;
66   return 0;
67 }
68 #if 0
69 static inline int blake2b_clear_lastblock( blake2b_state *S )
70 {
71   if( S->last_node ) blake2b_clear_lastnode( S );
72
73   S->f[0] = 0ULL;
74   return 0;
75 }
76 #endif
77 static inline int blake2b_increment_counter( blake2b_state *S, const uint64_t inc )
78 {
79   S->t[0] += inc;
80   S->t[1] += ( S->t[0] < inc );
81   return 0;
82 }
83
84
85
86 // Parameter-related functions
87 #if 0
88 static inline int blake2b_param_set_digest_length( blake2b_param *P, const uint8_t digest_length )
89 {
90   P->digest_length = digest_length;
91   return 0;
92 }
93
94 static inline int blake2b_param_set_fanout( blake2b_param *P, const uint8_t fanout )
95 {
96   P->fanout = fanout;
97   return 0;
98 }
99
100 static inline int blake2b_param_set_max_depth( blake2b_param *P, const uint8_t depth )
101 {
102   P->depth = depth;
103   return 0;
104 }
105
106 static inline int blake2b_param_set_leaf_length( blake2b_param *P, const uint32_t leaf_length )
107 {
108   store32( &P->leaf_length, leaf_length );
109   return 0;
110 }
111
112 static inline int blake2b_param_set_node_offset( blake2b_param *P, const uint64_t node_offset )
113 {
114   store64( &P->node_offset, node_offset );
115   return 0;
116 }
117
118 static inline int blake2b_param_set_node_depth( blake2b_param *P, const uint8_t node_depth )
119 {
120   P->node_depth = node_depth;
121   return 0;
122 }
123
124 static inline int blake2b_param_set_inner_length( blake2b_param *P, const uint8_t inner_length )
125 {
126   P->inner_length = inner_length;
127   return 0;
128 }
129 #endif
130 static inline int blake2b_param_set_salt( blake2b_param *P, const uint8_t salt[BLAKE2B_SALTBYTES] )
131 {
132   memcpy( P->salt, salt, BLAKE2B_SALTBYTES );
133   return 0;
134 }
135
136 static inline int blake2b_param_set_personal( blake2b_param *P, const uint8_t personal[BLAKE2B_PERSONALBYTES] )
137 {
138   memcpy( P->personal, personal, BLAKE2B_PERSONALBYTES );
139   return 0;
140 }
141
142 static inline int blake2b_init0( blake2b_state *S )
143 {
144   int i;
145   memset( S, 0, sizeof( blake2b_state ) );
146
147   for( i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i];
148
149   return 0;
150 }
151
152 /* init xors IV with input parameter block */
153 int blake2b_init_param( blake2b_state *S, const blake2b_param *P )
154 {
155   size_t i;
156   const uint8_t *p;
157
158   blake2b_init0( S );
159   p = ( const uint8_t * )( P );
160
161   /* IV XOR ParamBlock */
162   for( i = 0; i < 8; ++i )
163     S->h[i] ^= load64( p + sizeof( S->h[i] ) * i );
164
165   return 0;
166 }
167
168
169
170 int blake2b_init( blake2b_state *S, const uint8_t outlen )
171 {
172   blake2b_param P[1];
173
174   if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
175
176   P->digest_length = outlen;
177   P->key_length    = 0;
178   P->fanout        = 1;
179   P->depth         = 1;
180   store32( &P->leaf_length, 0 );
181   store64( &P->node_offset, 0 );
182   P->node_depth    = 0;
183   P->inner_length  = 0;
184   memset( P->reserved, 0, sizeof( P->reserved ) );
185   memset( P->salt,     0, sizeof( P->salt ) );
186   memset( P->personal, 0, sizeof( P->personal ) );
187   return blake2b_init_param( S, P );
188 }
189
190 int blake2b_init_salt_personal( blake2b_state *S, const uint8_t outlen,
191                                 const void *salt, const void *personal )
192 {
193   blake2b_param P[1];
194
195   if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
196
197   P->digest_length = outlen;
198   P->key_length    = 0;
199   P->fanout        = 1;
200   P->depth         = 1;
201   store32( &P->leaf_length, 0 );
202   store64( &P->node_offset, 0 );
203   P->node_depth    = 0;
204   P->inner_length  = 0;
205   memset( P->reserved, 0, sizeof( P->reserved ) );
206   if (salt != NULL) {
207     blake2b_param_set_salt( P, (const uint8_t *) salt );
208   } else {
209     memset( P->salt, 0, sizeof( P->salt ) );
210   }
211   if (personal != NULL) {
212     blake2b_param_set_personal( P, (const uint8_t *) personal );
213   } else {
214     memset( P->personal, 0, sizeof( P->personal ) );
215   }
216   return blake2b_init_param( S, P );
217 }
218
219 int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen )
220 {
221   blake2b_param P[1];
222
223   if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
224
225   if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1;
226
227   P->digest_length = outlen;
228   P->key_length    = keylen;
229   P->fanout        = 1;
230   P->depth         = 1;
231   store32( &P->leaf_length, 0 );
232   store64( &P->node_offset, 0 );
233   P->node_depth    = 0;
234   P->inner_length  = 0;
235   memset( P->reserved, 0, sizeof( P->reserved ) );
236   memset( P->salt,     0, sizeof( P->salt ) );
237   memset( P->personal, 0, sizeof( P->personal ) );
238
239   if( blake2b_init_param( S, P ) < 0 ) return -1;
240
241   {
242     uint8_t block[BLAKE2B_BLOCKBYTES];
243     memset( block, 0, BLAKE2B_BLOCKBYTES );
244     memcpy( block, key, keylen );
245     blake2b_update( S, block, BLAKE2B_BLOCKBYTES );
246     secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */
247   }
248   return 0;
249 }
250
251 int blake2b_init_key_salt_personal( blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen,
252                                     const void *salt, const void *personal )
253 {
254   blake2b_param P[1];
255
256   if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
257
258   if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1;
259
260   P->digest_length = outlen;
261   P->key_length    = keylen;
262   P->fanout        = 1;
263   P->depth         = 1;
264   store32( &P->leaf_length, 0 );
265   store64( &P->node_offset, 0 );
266   P->node_depth    = 0;
267   P->inner_length  = 0;
268   memset( P->reserved, 0, sizeof( P->reserved ) );
269   if (salt != NULL) {
270     blake2b_param_set_salt( P, (const uint8_t *) salt );
271   } else {
272     memset( P->salt, 0, sizeof( P->salt ) );
273   }
274   if (personal != NULL) {
275     blake2b_param_set_personal( P, (const uint8_t *) personal );
276   } else {
277     memset( P->personal, 0, sizeof( P->personal ) );
278   }
279
280   if( blake2b_init_param( S, P ) < 0 ) return -1;
281
282   {
283     uint8_t block[BLAKE2B_BLOCKBYTES];
284     memset( block, 0, BLAKE2B_BLOCKBYTES );
285     memcpy( block, key, keylen );
286     blake2b_update( S, block, BLAKE2B_BLOCKBYTES );
287     secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */
288   }
289   return 0;
290 }
291
292 static int blake2b_compress( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] )
293 {
294   uint64_t m[16];
295   uint64_t v[16];
296   int      i;
297
298   for( i = 0; i < 16; ++i )
299     m[i] = load64( block + i * sizeof( m[i] ) );
300
301   for( i = 0; i < 8; ++i )
302     v[i] = S->h[i];
303
304   v[ 8] = blake2b_IV[0];
305   v[ 9] = blake2b_IV[1];
306   v[10] = blake2b_IV[2];
307   v[11] = blake2b_IV[3];
308   v[12] = S->t[0] ^ blake2b_IV[4];
309   v[13] = S->t[1] ^ blake2b_IV[5];
310   v[14] = S->f[0] ^ blake2b_IV[6];
311   v[15] = S->f[1] ^ blake2b_IV[7];
312 #define G(r,i,a,b,c,d) \
313   do { \
314     a = a + b + m[blake2b_sigma[r][2*i+0]]; \
315     d = rotr64(d ^ a, 32); \
316     c = c + d; \
317     b = rotr64(b ^ c, 24); \
318     a = a + b + m[blake2b_sigma[r][2*i+1]]; \
319     d = rotr64(d ^ a, 16); \
320     c = c + d; \
321     b = rotr64(b ^ c, 63); \
322   } while(0)
323 #define ROUND(r)  \
324   do { \
325     G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
326     G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
327     G(r,2,v[ 2],v[ 6],v[10],v[14]); \
328     G(r,3,v[ 3],v[ 7],v[11],v[15]); \
329     G(r,4,v[ 0],v[ 5],v[10],v[15]); \
330     G(r,5,v[ 1],v[ 6],v[11],v[12]); \
331     G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
332     G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
333   } while(0)
334   ROUND( 0 );
335   ROUND( 1 );
336   ROUND( 2 );
337   ROUND( 3 );
338   ROUND( 4 );
339   ROUND( 5 );
340   ROUND( 6 );
341   ROUND( 7 );
342   ROUND( 8 );
343   ROUND( 9 );
344   ROUND( 10 );
345   ROUND( 11 );
346
347   for( i = 0; i < 8; ++i )
348     S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
349
350 #undef G
351 #undef ROUND
352   return 0;
353 }
354
355 /* inlen now in bytes */
356 int blake2b_update( blake2b_state *S, const uint8_t *in, uint64_t inlen )
357 {
358   while( inlen > 0 )
359   {
360     size_t left = S->buflen;
361     size_t fill = 2 * BLAKE2B_BLOCKBYTES - left;
362
363     if( inlen > fill )
364     {
365       memcpy( S->buf + left, in, fill ); // Fill buffer
366       S->buflen += fill;
367       blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
368       blake2b_compress( S, S->buf ); // Compress
369       memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); // Shift buffer left
370       S->buflen -= BLAKE2B_BLOCKBYTES;
371       in += fill;
372       inlen -= fill;
373     }
374     else // inlen <= fill
375     {
376       memcpy( S->buf + left, in, inlen );
377       S->buflen += inlen; // Be lazy, do not compress
378       in += inlen;
379       inlen -= inlen;
380     }
381   }
382
383   return 0;
384 }
385
386 /* Is this correct? */
387 int blake2b_final( blake2b_state *S, uint8_t *out, uint8_t outlen )
388 {
389   uint8_t buffer[BLAKE2B_OUTBYTES];
390   int     i;
391
392   if( outlen > BLAKE2B_OUTBYTES ) {
393     return -1;
394   }
395   if( S->buflen > BLAKE2B_BLOCKBYTES )
396   {
397     blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
398     blake2b_compress( S, S->buf );
399     S->buflen -= BLAKE2B_BLOCKBYTES;
400     memmove( S->buf, S->buf + BLAKE2B_BLOCKBYTES, S->buflen );
401   }
402
403   blake2b_increment_counter( S, S->buflen );
404   blake2b_set_lastblock( S );
405   memset( S->buf + S->buflen, 0, 2 * BLAKE2B_BLOCKBYTES - S->buflen ); /* Padding */
406   blake2b_compress( S, S->buf );
407
408   for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
409     store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
410
411   memcpy( out, buffer, outlen );
412   return 0;
413 }
414
415 /* inlen, at least, should be uint64_t. Others can be size_t. */
416 int blake2b( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen )
417 {
418   blake2b_state S[1];
419
420   /* Verify parameters */
421   if ( NULL == in ) return -1;
422
423   if ( NULL == out ) return -1;
424
425   if( NULL == key ) keylen = 0;
426
427   if( keylen > 0 )
428   {
429     if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1;
430   }
431   else
432   {
433     if( blake2b_init( S, outlen ) < 0 ) return -1;
434   }
435
436   blake2b_update( S, ( const uint8_t * )in, inlen );
437   blake2b_final( S, out, outlen );
438   return 0;
439 }
440
441 int blake2b_salt_personal( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen,
442                            const void *salt, const void *personal )
443 {
444   blake2b_state S[1];
445
446   /* Verify parameters */
447   if ( NULL == in ) return -1;
448
449   if ( NULL == out ) return -1;
450
451   if( NULL == key ) keylen = 0;
452
453   if( keylen > 0 )
454   {
455     if( blake2b_init_key_salt_personal( S, outlen, key, keylen, salt, personal ) < 0 ) return -1;
456   }
457   else
458   {
459     if( blake2b_init_salt_personal( S, outlen, salt, personal ) < 0 ) return -1;
460   }
461
462   blake2b_update( S, ( const uint8_t * )in, inlen );
463   blake2b_final( S, out, outlen );
464   return 0;
465 }