2 BLAKE2 reference source code package - reference C implementations
4 Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
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 }
46 static inline int blake2b_set_lastnode( blake2b_state *S )
52 static inline int blake2b_clear_lastnode( blake2b_state *S )
58 /* Some helper functions, not necessarily useful */
59 static inline int blake2b_set_lastblock( blake2b_state *S )
61 if( S->last_node ) blake2b_set_lastnode( S );
67 static inline int blake2b_clear_lastblock( blake2b_state *S )
69 if( S->last_node ) blake2b_clear_lastnode( S );
75 static inline int blake2b_increment_counter( blake2b_state *S, const uint64_t inc )
78 S->t[1] += ( S->t[0] < inc );
84 // Parameter-related functions
85 static inline int blake2b_param_set_digest_length( blake2b_param *P, const uint8_t digest_length )
87 P->digest_length = digest_length;
91 static inline int blake2b_param_set_fanout( blake2b_param *P, const uint8_t fanout )
97 static inline int blake2b_param_set_max_depth( blake2b_param *P, const uint8_t depth )
103 static inline int blake2b_param_set_leaf_length( blake2b_param *P, const uint32_t leaf_length )
105 store32( &P->leaf_length, leaf_length );
109 static inline int blake2b_param_set_node_offset( blake2b_param *P, const uint64_t node_offset )
111 store64( &P->node_offset, node_offset );
115 static inline int blake2b_param_set_node_depth( blake2b_param *P, const uint8_t node_depth )
117 P->node_depth = node_depth;
121 static inline int blake2b_param_set_inner_length( blake2b_param *P, const uint8_t inner_length )
123 P->inner_length = inner_length;
127 static inline int blake2b_param_set_salt( blake2b_param *P, const uint8_t salt[BLAKE2B_SALTBYTES] )
129 memcpy( P->salt, salt, BLAKE2B_SALTBYTES );
133 static inline int blake2b_param_set_personal( blake2b_param *P, const uint8_t personal[BLAKE2B_PERSONALBYTES] )
135 memcpy( P->personal, personal, BLAKE2B_PERSONALBYTES );
139 static inline int blake2b_init0( blake2b_state *S )
141 memset( S, 0, sizeof( blake2b_state ) );
143 for( int i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i];
148 #define blake2b_init BLAKE2_IMPL_NAME(blake2b_init)
149 #define blake2b_init_param BLAKE2_IMPL_NAME(blake2b_init_param)
150 #define blake2b_init_key BLAKE2_IMPL_NAME(blake2b_init_key)
151 #define blake2b_update BLAKE2_IMPL_NAME(blake2b_update)
152 #define blake2b_final BLAKE2_IMPL_NAME(blake2b_final)
153 #define blake2b BLAKE2_IMPL_NAME(blake2b)
155 #if defined(__cplusplus)
158 int blake2b_init( blake2b_state *S, size_t outlen );
159 int blake2b_init_param( blake2b_state *S, const blake2b_param *P );
160 int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen );
161 int blake2b_update( blake2b_state *S, const uint8_t *in, size_t inlen );
162 int blake2b_final( blake2b_state *S, uint8_t *out, size_t outlen );
163 int blake2b( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
164 #if defined(__cplusplus)
168 /* init xors IV with input parameter block */
169 int blake2b_init_param( blake2b_state *S, const blake2b_param *P )
172 uint8_t *p = ( uint8_t * )( P );
174 /* IV XOR ParamBlock */
175 for( size_t i = 0; i < 8; ++i )
176 S->h[i] ^= load64( p + sizeof( S->h[i] ) * i );
178 S->outlen = P->digest_length;
184 int blake2b_init( blake2b_state *S, size_t outlen )
188 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
190 P->digest_length = ( uint8_t ) outlen;
194 store32( &P->leaf_length, 0 );
195 store64( &P->node_offset, 0 );
198 memset( P->reserved, 0, sizeof( P->reserved ) );
199 memset( P->salt, 0, sizeof( P->salt ) );
200 memset( P->personal, 0, sizeof( P->personal ) );
201 return blake2b_init_param( S, P );
205 int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen )
209 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
211 if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1;
213 P->digest_length = ( uint8_t ) outlen;
214 P->key_length = ( uint8_t ) keylen;
217 store32( &P->leaf_length, 0 );
218 store64( &P->node_offset, 0 );
221 memset( P->reserved, 0, sizeof( P->reserved ) );
222 memset( P->salt, 0, sizeof( P->salt ) );
223 memset( P->personal, 0, sizeof( P->personal ) );
225 if( blake2b_init_param( S, P ) < 0 ) return -1;
228 uint8_t block[BLAKE2B_BLOCKBYTES];
229 memset( block, 0, BLAKE2B_BLOCKBYTES );
230 memcpy( block, key, keylen );
231 blake2b_update( S, block, BLAKE2B_BLOCKBYTES );
232 secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */
237 static int blake2b_compress( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] )
243 for( i = 0; i < 16; ++i )
244 m[i] = load64( block + i * sizeof( m[i] ) );
246 for( i = 0; i < 8; ++i )
249 v[ 8] = blake2b_IV[0];
250 v[ 9] = blake2b_IV[1];
251 v[10] = blake2b_IV[2];
252 v[11] = blake2b_IV[3];
253 v[12] = S->t[0] ^ blake2b_IV[4];
254 v[13] = S->t[1] ^ blake2b_IV[5];
255 v[14] = S->f[0] ^ blake2b_IV[6];
256 v[15] = S->f[1] ^ blake2b_IV[7];
257 #define G(r,i,a,b,c,d) \
259 a = a + b + m[blake2b_sigma[r][2*i+0]]; \
260 d = rotr64(d ^ a, 32); \
262 b = rotr64(b ^ c, 24); \
263 a = a + b + m[blake2b_sigma[r][2*i+1]]; \
264 d = rotr64(d ^ a, 16); \
266 b = rotr64(b ^ c, 63); \
270 G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
271 G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
272 G(r,2,v[ 2],v[ 6],v[10],v[14]); \
273 G(r,3,v[ 3],v[ 7],v[11],v[15]); \
274 G(r,4,v[ 0],v[ 5],v[10],v[15]); \
275 G(r,5,v[ 1],v[ 6],v[11],v[12]); \
276 G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
277 G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
292 for( i = 0; i < 8; ++i )
293 S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
301 int blake2b_update( blake2b_state *S, const uint8_t *in, size_t inlen )
305 uint32_t left = S->buflen;
306 uint32_t fill = 2 * BLAKE2B_BLOCKBYTES - left;
310 memcpy( S->buf + left, in, fill ); // Fill buffer
312 blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
313 blake2b_compress( S, S->buf ); // Compress
314 memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); // Shift buffer left
315 S->buflen -= BLAKE2B_BLOCKBYTES;
319 else // inlen <= fill
321 memcpy( S->buf + left, in, inlen );
322 S->buflen += ( uint32_t ) inlen; // Be lazy, do not compress
331 int blake2b_final( blake2b_state *S, uint8_t *out, size_t outlen )
333 uint8_t buffer[BLAKE2B_OUTBYTES];
336 if(S->outlen != outlen) return -1;
338 if( S->buflen > BLAKE2B_BLOCKBYTES )
340 blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
341 blake2b_compress( S, S->buf );
342 S->buflen -= BLAKE2B_BLOCKBYTES;
343 memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, S->buflen );
346 blake2b_increment_counter( S, S->buflen );
347 blake2b_set_lastblock( S );
348 memset( S->buf + S->buflen, 0, 2 * BLAKE2B_BLOCKBYTES - S->buflen ); /* Padding */
349 blake2b_compress( S, S->buf );
351 for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
352 store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
354 memcpy( out, buffer, outlen );
358 int blake2b( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen )
362 /* Verify parameters */
363 if ( NULL == in && inlen > 0 ) return -1;
365 if ( NULL == out ) return -1;
367 if( NULL == key && keylen > 0 ) return -1;
369 if( !outlen || outlen > BLAKE2B_OUTBYTES ) return -1;
371 if( keylen > BLAKE2B_KEYBYTES ) return -1;
375 if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1;
379 if( blake2b_init( S, outlen ) < 0 ) return -1;
382 if( blake2b_update( S, ( uint8_t * )in, inlen ) < 0 ) return -1;
383 return blake2b_final( S, out, outlen );