2 BLAKE2 reference source code package - reference C implementations
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.
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/>.
19 #include "blake2-impl.h"
21 static const uint64_t blake2b_IV[8] =
23 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
24 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
25 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
26 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
29 static const uint8_t blake2b_sigma[12][16] =
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 }
47 static inline int blake2b_set_lastnode( blake2b_state *S )
54 static inline int blake2b_clear_lastnode( blake2b_state *S )
60 /* Some helper functions, not necessarily useful */
61 static inline int blake2b_set_lastblock( blake2b_state *S )
63 if( S->last_node ) blake2b_set_lastnode( S );
69 static inline int blake2b_clear_lastblock( blake2b_state *S )
71 if( S->last_node ) blake2b_clear_lastnode( S );
77 static inline int blake2b_increment_counter( blake2b_state *S, const uint64_t inc )
80 S->t[1] += ( S->t[0] < inc );
86 // Parameter-related functions
88 static inline int blake2b_param_set_digest_length( blake2b_param *P, const uint8_t digest_length )
90 P->digest_length = digest_length;
94 static inline int blake2b_param_set_fanout( blake2b_param *P, const uint8_t fanout )
100 static inline int blake2b_param_set_max_depth( blake2b_param *P, const uint8_t depth )
106 static inline int blake2b_param_set_leaf_length( blake2b_param *P, const uint32_t leaf_length )
108 store32( &P->leaf_length, leaf_length );
112 static inline int blake2b_param_set_node_offset( blake2b_param *P, const uint64_t node_offset )
114 store64( &P->node_offset, node_offset );
118 static inline int blake2b_param_set_node_depth( blake2b_param *P, const uint8_t node_depth )
120 P->node_depth = node_depth;
124 static inline int blake2b_param_set_inner_length( blake2b_param *P, const uint8_t inner_length )
126 P->inner_length = inner_length;
130 static inline int blake2b_param_set_salt( blake2b_param *P, const uint8_t salt[BLAKE2B_SALTBYTES] )
132 memcpy( P->salt, salt, BLAKE2B_SALTBYTES );
136 static inline int blake2b_param_set_personal( blake2b_param *P, const uint8_t personal[BLAKE2B_PERSONALBYTES] )
138 memcpy( P->personal, personal, BLAKE2B_PERSONALBYTES );
142 static inline int blake2b_init0( blake2b_state *S )
145 memset( S, 0, sizeof( blake2b_state ) );
147 for( i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i];
152 /* init xors IV with input parameter block */
153 int blake2b_init_param( blake2b_state *S, const blake2b_param *P )
159 p = ( const uint8_t * )( P );
161 /* IV XOR ParamBlock */
162 for( i = 0; i < 8; ++i )
163 S->h[i] ^= load64( p + sizeof( S->h[i] ) * i );
170 int blake2b_init( blake2b_state *S, const uint8_t outlen )
174 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
176 P->digest_length = outlen;
180 store32( &P->leaf_length, 0 );
181 store64( &P->node_offset, 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 );
190 int blake2b_init_salt_personal( blake2b_state *S, const uint8_t outlen,
191 const void *salt, const void *personal )
195 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
197 P->digest_length = outlen;
201 store32( &P->leaf_length, 0 );
202 store64( &P->node_offset, 0 );
205 memset( P->reserved, 0, sizeof( P->reserved ) );
207 blake2b_param_set_salt( P, (const uint8_t *) salt );
209 memset( P->salt, 0, sizeof( P->salt ) );
211 if (personal != NULL) {
212 blake2b_param_set_personal( P, (const uint8_t *) personal );
214 memset( P->personal, 0, sizeof( P->personal ) );
216 return blake2b_init_param( S, P );
219 int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen )
223 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
225 if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1;
227 P->digest_length = outlen;
228 P->key_length = keylen;
231 store32( &P->leaf_length, 0 );
232 store64( &P->node_offset, 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 ) );
239 if( blake2b_init_param( S, P ) < 0 ) return -1;
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 */
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 )
256 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
258 if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1;
260 P->digest_length = outlen;
261 P->key_length = keylen;
264 store32( &P->leaf_length, 0 );
265 store64( &P->node_offset, 0 );
268 memset( P->reserved, 0, sizeof( P->reserved ) );
270 blake2b_param_set_salt( P, (const uint8_t *) salt );
272 memset( P->salt, 0, sizeof( P->salt ) );
274 if (personal != NULL) {
275 blake2b_param_set_personal( P, (const uint8_t *) personal );
277 memset( P->personal, 0, sizeof( P->personal ) );
280 if( blake2b_init_param( S, P ) < 0 ) return -1;
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 */
292 static int blake2b_compress( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] )
298 for( i = 0; i < 16; ++i )
299 m[i] = load64( block + i * sizeof( m[i] ) );
301 for( i = 0; i < 8; ++i )
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) \
314 a = a + b + m[blake2b_sigma[r][2*i+0]]; \
315 d = rotr64(d ^ a, 32); \
317 b = rotr64(b ^ c, 24); \
318 a = a + b + m[blake2b_sigma[r][2*i+1]]; \
319 d = rotr64(d ^ a, 16); \
321 b = rotr64(b ^ c, 63); \
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]); \
347 for( i = 0; i < 8; ++i )
348 S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
355 /* inlen now in bytes */
356 int blake2b_update( blake2b_state *S, const uint8_t *in, uint64_t inlen )
360 size_t left = S->buflen;
361 size_t fill = 2 * BLAKE2B_BLOCKBYTES - left;
365 memcpy( S->buf + left, in, fill ); // Fill buffer
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;
374 else // inlen <= fill
376 memcpy( S->buf + left, in, inlen );
377 S->buflen += inlen; // Be lazy, do not compress
386 /* Is this correct? */
387 int blake2b_final( blake2b_state *S, uint8_t *out, uint8_t outlen )
389 uint8_t buffer[BLAKE2B_OUTBYTES];
392 if( outlen > BLAKE2B_OUTBYTES ) {
395 if( S->buflen > BLAKE2B_BLOCKBYTES )
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 );
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 );
408 for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
409 store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
411 memcpy( out, buffer, outlen );
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 )
420 /* Verify parameters */
421 if ( NULL == in ) return -1;
423 if ( NULL == out ) return -1;
425 if( NULL == key ) keylen = 0;
429 if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1;
433 if( blake2b_init( S, outlen ) < 0 ) return -1;
436 blake2b_update( S, ( const uint8_t * )in, inlen );
437 blake2b_final( S, out, outlen );
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 )
446 /* Verify parameters */
447 if ( NULL == in ) return -1;
449 if ( NULL == out ) return -1;
451 if( NULL == key ) keylen = 0;
455 if( blake2b_init_key_salt_personal( S, outlen, key, keylen, salt, personal ) < 0 ) return -1;
459 if( blake2b_init_salt_personal( S, outlen, salt, personal ) < 0 ) return -1;
462 blake2b_update( S, ( const uint8_t * )in, inlen );
463 blake2b_final( S, out, outlen );