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/>.
15 * Copyright (C) 2006-2014 wolfSSL Inc.
17 * This file is part of CyaSSL.
19 * CyaSSL is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * CyaSSL is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
39 #include <cyassl/ctaocrypt/settings.h>
43 #include <cyassl/ctaocrypt/blake2.h>
44 #include <cyassl/ctaocrypt/blake2-impl.h>
47 static const word64 blake2b_IV[8] =
49 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
50 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
51 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
52 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
55 static const byte blake2b_sigma[12][16] =
57 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
58 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
59 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
60 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
61 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
62 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
63 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
64 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
65 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
66 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
67 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
68 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
72 static INLINE int blake2b_set_lastnode( blake2b_state *S )
78 /* Some helper functions, not necessarily useful */
79 static INLINE int blake2b_set_lastblock( blake2b_state *S )
81 if( S->last_node ) blake2b_set_lastnode( S );
87 static INLINE int blake2b_increment_counter( blake2b_state *S, const word64
91 S->t[1] += ( S->t[0] < inc );
95 static INLINE int blake2b_init0( blake2b_state *S )
98 XMEMSET( S, 0, sizeof( blake2b_state ) );
100 for( i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i];
105 /* init xors IV with input parameter block */
106 int blake2b_init_param( blake2b_state *S, const blake2b_param *P )
110 byte *p = ( byte * )( P );
112 /* IV XOR ParamBlock */
113 for( i = 0; i < 8; ++i )
114 S->h[i] ^= load64( p + sizeof( S->h[i] ) * i );
121 int blake2b_init( blake2b_state *S, const byte outlen )
125 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
127 P->digest_length = outlen;
131 store32( &P->leaf_length, 0 );
132 store64( &P->node_offset, 0 );
135 XMEMSET( P->reserved, 0, sizeof( P->reserved ) );
136 XMEMSET( P->salt, 0, sizeof( P->salt ) );
137 XMEMSET( P->personal, 0, sizeof( P->personal ) );
138 return blake2b_init_param( S, P );
142 int blake2b_init_key( blake2b_state *S, const byte outlen, const void *key,
147 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
149 if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1;
151 P->digest_length = outlen;
152 P->key_length = keylen;
155 store32( &P->leaf_length, 0 );
156 store64( &P->node_offset, 0 );
159 XMEMSET( P->reserved, 0, sizeof( P->reserved ) );
160 XMEMSET( P->salt, 0, sizeof( P->salt ) );
161 XMEMSET( P->personal, 0, sizeof( P->personal ) );
163 if( blake2b_init_param( S, P ) < 0 ) return -1;
166 #ifdef CYASSL_SMALL_STACK
169 block = (byte*)XMALLOC(BLAKE2B_BLOCKBYTES, NULL, DYNAMIC_TYPE_TMP_BUFFER);
171 if ( block == NULL ) return -1;
173 byte block[BLAKE2B_BLOCKBYTES];
176 XMEMSET( block, 0, BLAKE2B_BLOCKBYTES );
177 XMEMCPY( block, key, keylen );
178 blake2b_update( S, block, BLAKE2B_BLOCKBYTES );
179 secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from */
182 #ifdef CYASSL_SMALL_STACK
183 XFREE(block, NULL, DYNAMIC_TYPE_TMP_BUFFER);
189 static int blake2b_compress( blake2b_state *S,
190 const byte block[BLAKE2B_BLOCKBYTES] )
194 #ifdef CYASSL_SMALL_STACK
198 m = (word64*)XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
200 if ( m == NULL ) return -1;
202 v = (word64*)XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
206 XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
214 for( i = 0; i < 16; ++i )
215 m[i] = load64( block + i * sizeof( m[i] ) );
217 for( i = 0; i < 8; ++i )
220 v[ 8] = blake2b_IV[0];
221 v[ 9] = blake2b_IV[1];
222 v[10] = blake2b_IV[2];
223 v[11] = blake2b_IV[3];
224 v[12] = S->t[0] ^ blake2b_IV[4];
225 v[13] = S->t[1] ^ blake2b_IV[5];
226 v[14] = S->f[0] ^ blake2b_IV[6];
227 v[15] = S->f[1] ^ blake2b_IV[7];
228 #define G(r,i,a,b,c,d) \
230 a = a + b + m[blake2b_sigma[r][2*i+0]]; \
231 d = rotr64(d ^ a, 32); \
233 b = rotr64(b ^ c, 24); \
234 a = a + b + m[blake2b_sigma[r][2*i+1]]; \
235 d = rotr64(d ^ a, 16); \
237 b = rotr64(b ^ c, 63); \
241 G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
242 G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
243 G(r,2,v[ 2],v[ 6],v[10],v[14]); \
244 G(r,3,v[ 3],v[ 7],v[11],v[15]); \
245 G(r,4,v[ 0],v[ 5],v[10],v[15]); \
246 G(r,5,v[ 1],v[ 6],v[11],v[12]); \
247 G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
248 G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
263 for( i = 0; i < 8; ++i )
264 S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
269 #ifdef CYASSL_SMALL_STACK
270 XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
271 XFREE(v, NULL, DYNAMIC_TYPE_TMP_BUFFER);
277 /* inlen now in bytes */
278 int blake2b_update( blake2b_state *S, const byte *in, word64 inlen )
282 word64 left = S->buflen;
283 word64 fill = 2 * BLAKE2B_BLOCKBYTES - left;
287 XMEMCPY( S->buf + left, in, (word)fill ); /* Fill buffer */
289 blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
291 if ( blake2b_compress( S, S->buf ) < 0 ) return -1; /* Compress */
293 XMEMCPY( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES );
294 /* Shift buffer left */
295 S->buflen -= BLAKE2B_BLOCKBYTES;
299 else /* inlen <= fill */
301 XMEMCPY( S->buf + left, in, (word)inlen );
302 S->buflen += inlen; /* Be lazy, do not compress */
311 /* Is this correct? */
312 int blake2b_final( blake2b_state *S, byte *out, byte outlen )
314 byte buffer[BLAKE2B_OUTBYTES];
317 if( S->buflen > BLAKE2B_BLOCKBYTES )
319 blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
321 if ( blake2b_compress( S, S->buf ) < 0 ) return -1;
323 S->buflen -= BLAKE2B_BLOCKBYTES;
324 XMEMCPY( S->buf, S->buf + BLAKE2B_BLOCKBYTES, (word)S->buflen );
327 blake2b_increment_counter( S, S->buflen );
328 blake2b_set_lastblock( S );
329 XMEMSET( S->buf + S->buflen, 0, (word)(2 * BLAKE2B_BLOCKBYTES - S->buflen) );
331 if ( blake2b_compress( S, S->buf ) < 0 ) return -1;
333 for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
334 store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
336 XMEMCPY( out, buffer, outlen );
340 /* inlen, at least, should be word64. Others can be size_t. */
341 int blake2b( byte *out, const void *in, const void *key, const byte outlen,
342 const word64 inlen, byte keylen )
346 /* Verify parameters */
347 if ( NULL == in ) return -1;
349 if ( NULL == out ) return -1;
351 if( NULL == key ) keylen = 0;
355 if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1;
359 if( blake2b_init( S, outlen ) < 0 ) return -1;
362 if ( blake2b_update( S, ( byte * )in, inlen ) < 0) return -1;
364 return blake2b_final( S, out, outlen );
367 #if defined(BLAKE2B_SELFTEST)
369 #include "blake2-kat.h"
370 int main( int argc, char **argv )
372 byte key[BLAKE2B_KEYBYTES];
373 byte buf[KAT_LENGTH];
375 for( word32 i = 0; i < BLAKE2B_KEYBYTES; ++i )
378 for( word32 i = 0; i < KAT_LENGTH; ++i )
381 for( word32 i = 0; i < KAT_LENGTH; ++i )
383 byte hash[BLAKE2B_OUTBYTES];
384 if ( blake2b( hash, buf, key, BLAKE2B_OUTBYTES, i, BLAKE2B_KEYBYTES ) < 0 )
390 if( 0 != memcmp( hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES ) )
405 /* Init Blake2b digest, track size incase final doesn't want to "remember" */
406 int InitBlake2b(Blake2b* b2b, word32 digestSz)
408 b2b->digestSz = digestSz;
410 return blake2b_init(b2b->S, (byte)digestSz);
415 int Blake2bUpdate(Blake2b* b2b, const byte* data, word32 sz)
417 return blake2b_update(b2b->S, data, sz);
421 /* Blake2b Final, if pass in zero size we use init digestSz */
422 int Blake2bFinal(Blake2b* b2b, byte* final, word32 requestSz)
424 word32 sz = requestSz ? requestSz : b2b->digestSz;
426 return blake2b_final(b2b->S, final, (byte)sz);
430 /* end CTaoCrypt API */
432 #endif /* HAVE_BLAKE2 */