]> pd.if.org Git - zpackage/blob - lib/blake2b.c
50ed77818f074ad7dac05b8f00de84327827fdbb
[zpackage] / lib / blake2b.c
1 /*
2  * public domain blake2 implementation adapted from the reference
3  * implementation by Samuel Neves
4  * More information about the BLAKE2 hash function can be found at
5  * https://blake2.net.
6  */
7
8 #include <stdint.h>
9 #include <string.h>
10 #include <stdio.h>
11
12 #include "blake2.h"
13
14 static uint64_t load64( const void *src )
15 {
16 #if defined(NATIVE_LITTLE_ENDIAN)
17   uint64_t w;
18   memcpy(&w, src, sizeof w);
19   return w;
20 #else
21   const uint8_t *p = ( const uint8_t * )src;
22   return (( uint64_t )( p[0] ) <<  0) |
23          (( uint64_t )( p[1] ) <<  8) |
24          (( uint64_t )( p[2] ) << 16) |
25          (( uint64_t )( p[3] ) << 24) |
26          (( uint64_t )( p[4] ) << 32) |
27          (( uint64_t )( p[5] ) << 40) |
28          (( uint64_t )( p[6] ) << 48) |
29          (( uint64_t )( p[7] ) << 56) ;
30 #endif
31 }
32
33 static void store32( void *dst, uint32_t w )
34 {
35 #if defined(NATIVE_LITTLE_ENDIAN)
36   memcpy(dst, &w, sizeof w);
37 #else
38   uint8_t *p = ( uint8_t * )dst;
39   p[0] = (uint8_t)(w >>  0);
40   p[1] = (uint8_t)(w >>  8);
41   p[2] = (uint8_t)(w >> 16);
42   p[3] = (uint8_t)(w >> 24);
43 #endif
44 }
45
46 static void store64( void *dst, uint64_t w )
47 {
48 #if defined(NATIVE_LITTLE_ENDIAN)
49   memcpy(dst, &w, sizeof w);
50 #else
51   uint8_t *p = ( uint8_t * )dst;
52   p[0] = (uint8_t)(w >>  0);
53   p[1] = (uint8_t)(w >>  8);
54   p[2] = (uint8_t)(w >> 16);
55   p[3] = (uint8_t)(w >> 24);
56   p[4] = (uint8_t)(w >> 32);
57   p[5] = (uint8_t)(w >> 40);
58   p[6] = (uint8_t)(w >> 48);
59   p[7] = (uint8_t)(w >> 56);
60 #endif
61 }
62
63 static uint64_t rotr64( const uint64_t w, const unsigned c )
64 {
65   return ( w >> c ) | ( w << ( 64 - c ) );
66 }
67
68 /* prevents compiler optimizing out memset() */
69 static void secure_zero_memory(void *v, size_t n)
70 {
71   static void *(*const volatile memset_v)(void *, int, size_t) = &memset;
72   memset_v(v, 0, n);
73 }
74
75 static const uint64_t blake2b_IV[8] =
76 {
77   0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
78   0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
79   0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
80   0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
81 };
82
83 static const uint8_t blake2b_sigma[12][16] =
84 {
85   {  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15 } ,
86   { 14, 10,  4,  8,  9, 15, 13,  6,  1, 12,  0,  2, 11,  7,  5,  3 } ,
87   { 11,  8, 12,  0,  5,  2, 15, 13, 10, 14,  3,  6,  7,  1,  9,  4 } ,
88   {  7,  9,  3,  1, 13, 12, 11, 14,  2,  6,  5, 10,  4,  0, 15,  8 } ,
89   {  9,  0,  5,  7,  2,  4, 10, 15, 14,  1, 11, 12,  6,  8,  3, 13 } ,
90   {  2, 12,  6, 10,  0, 11,  8,  3,  4, 13,  7,  5, 15, 14,  1,  9 } ,
91   { 12,  5,  1, 15, 14, 13,  4, 10,  0,  7,  6,  3,  9,  2,  8, 11 } ,
92   { 13, 11,  7, 14, 12,  1,  3,  9,  5,  0, 15,  4,  8,  6,  2, 10 } ,
93   {  6, 15, 14,  9, 11,  3,  0,  8, 12,  2, 13,  7,  1,  4, 10,  5 } ,
94   { 10,  2,  8,  4,  7,  6,  1,  5, 15, 11,  9, 14,  3, 12, 13 , 0 } ,
95   {  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15 } ,
96   { 14, 10,  4,  8,  9, 15, 13,  6,  1, 12,  0,  2, 11,  7,  5,  3 }
97 };
98
99
100 static void blake2b_set_lastnode( blake2b_state *S )
101 {
102   S->f[1] = (uint64_t)-1;
103 }
104
105 /* Some helper functions, not necessarily useful */
106 static int blake2b_is_lastblock( const blake2b_state *S ) {
107         return S->f[0] != 0;
108 }
109
110 static void blake2b_set_lastblock( blake2b_state *S )
111 {
112   if( S->last_node ) blake2b_set_lastnode( S );
113
114   S->f[0] = (uint64_t)-1;
115 }
116
117 static void blake2b_increment_counter( blake2b_state *S, const uint64_t inc )
118 {
119   S->t[0] += inc;
120   S->t[1] += ( S->t[0] < inc );
121 }
122
123 static void blake2b_init0( blake2b_state *S )
124 {
125   size_t i;
126   memset( S, 0, sizeof( blake2b_state ) );
127
128   for( i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i];
129 }
130
131 /* init xors IV with input parameter block */
132 int blake2b_init_param( blake2b_state *S, const blake2b_param *P )
133 {
134   const uint8_t *p = ( const uint8_t * )( P );
135   size_t i;
136
137   blake2b_init0( S );
138
139   /* IV XOR ParamBlock */
140   for( i = 0; i < 8; ++i )
141     S->h[i] ^= load64( p + sizeof( S->h[i] ) * i );
142
143   S->outlen = P->digest_length;
144   return 0;
145 }
146
147
148
149 int blake2b_init( blake2b_state *S, size_t outlen )
150 {
151   blake2b_param P[1];
152
153   if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
154
155   P->digest_length = (uint8_t)outlen;
156   P->key_length    = 0;
157   P->fanout        = 1;
158   P->depth         = 1;
159   store32( &P->leaf_length, 0 );
160   store32( &P->node_offset, 0 );
161   store32( &P->xof_length, 0 );
162   P->node_depth    = 0;
163   P->inner_length  = 0;
164   memset( P->reserved, 0, sizeof( P->reserved ) );
165   memset( P->salt,     0, sizeof( P->salt ) );
166   memset( P->personal, 0, sizeof( P->personal ) );
167   return blake2b_init_param( S, P );
168 }
169
170
171 int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen )
172 {
173   blake2b_param P[1];
174
175   if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
176
177   if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1;
178
179   P->digest_length = (uint8_t)outlen;
180   P->key_length    = (uint8_t)keylen;
181   P->fanout        = 1;
182   P->depth         = 1;
183   store32( &P->leaf_length, 0 );
184   store32( &P->node_offset, 0 );
185   store32( &P->xof_length, 0 );
186   P->node_depth    = 0;
187   P->inner_length  = 0;
188   memset( P->reserved, 0, sizeof( P->reserved ) );
189   memset( P->salt,     0, sizeof( P->salt ) );
190   memset( P->personal, 0, sizeof( P->personal ) );
191
192   if( blake2b_init_param( S, P ) < 0 ) return -1;
193
194   {
195     uint8_t block[BLAKE2B_BLOCKBYTES];
196     memset( block, 0, BLAKE2B_BLOCKBYTES );
197     memcpy( block, key, keylen );
198     blake2b_update( S, block, BLAKE2B_BLOCKBYTES );
199     secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */
200   }
201   return 0;
202 }
203
204 #define G(r,i,a,b,c,d)                      \
205   do {                                      \
206     a = a + b + m[blake2b_sigma[r][2*i+0]]; \
207     d = rotr64(d ^ a, 32);                  \
208     c = c + d;                              \
209     b = rotr64(b ^ c, 24);                  \
210     a = a + b + m[blake2b_sigma[r][2*i+1]]; \
211     d = rotr64(d ^ a, 16);                  \
212     c = c + d;                              \
213     b = rotr64(b ^ c, 63);                  \
214   } while(0)
215
216 #define ROUND(r)                    \
217   do {                              \
218     G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
219     G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
220     G(r,2,v[ 2],v[ 6],v[10],v[14]); \
221     G(r,3,v[ 3],v[ 7],v[11],v[15]); \
222     G(r,4,v[ 0],v[ 5],v[10],v[15]); \
223     G(r,5,v[ 1],v[ 6],v[11],v[12]); \
224     G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
225     G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
226   } while(0)
227
228 static void blake2b_compress( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] )
229 {
230   uint64_t m[16];
231   uint64_t v[16];
232   size_t i;
233
234   for( i = 0; i < 16; ++i ) {
235     m[i] = load64( block + i * sizeof( m[i] ) );
236   }
237
238   for( i = 0; i < 8; ++i ) {
239     v[i] = S->h[i];
240   }
241
242   v[ 8] = blake2b_IV[0];
243   v[ 9] = blake2b_IV[1];
244   v[10] = blake2b_IV[2];
245   v[11] = blake2b_IV[3];
246   v[12] = blake2b_IV[4] ^ S->t[0];
247   v[13] = blake2b_IV[5] ^ S->t[1];
248   v[14] = blake2b_IV[6] ^ S->f[0];
249   v[15] = blake2b_IV[7] ^ S->f[1];
250
251   ROUND( 0 );
252   ROUND( 1 );
253   ROUND( 2 );
254   ROUND( 3 );
255   ROUND( 4 );
256   ROUND( 5 );
257   ROUND( 6 );
258   ROUND( 7 );
259   ROUND( 8 );
260   ROUND( 9 );
261   ROUND( 10 );
262   ROUND( 11 );
263
264   for( i = 0; i < 8; ++i ) {
265     S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
266   }
267 }
268
269 #undef G
270 #undef ROUND
271
272 int blake2b_update( blake2b_state *S, const void *pin, size_t inlen )
273 {
274   const unsigned char * in = (const unsigned char *)pin;
275   if( inlen > 0 )
276   {
277     size_t left = S->buflen;
278     size_t fill = BLAKE2B_BLOCKBYTES - left;
279     if( inlen > fill )
280     {
281       S->buflen = 0;
282       memcpy( S->buf + left, in, fill ); /* Fill buffer */
283       blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
284       blake2b_compress( S, S->buf ); /* Compress */
285       in += fill; inlen -= fill;
286       while(inlen > BLAKE2B_BLOCKBYTES) {
287         blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES);
288         blake2b_compress( S, in );
289         in += BLAKE2B_BLOCKBYTES;
290         inlen -= BLAKE2B_BLOCKBYTES;
291       }
292     }
293     memcpy( S->buf + S->buflen, in, inlen );
294     S->buflen += inlen;
295   }
296   return 0;
297 }
298
299 int blake2b_final( blake2b_state *S, void *out, size_t outlen )
300 {
301   uint8_t buffer[BLAKE2B_OUTBYTES] = {0};
302   size_t i;
303
304   if( out == NULL || outlen < S->outlen )
305     return -1;
306
307   if( blake2b_is_lastblock( S ) )
308     return -1;
309
310   blake2b_increment_counter( S, S->buflen );
311   blake2b_set_lastblock( S );
312   memset( S->buf + S->buflen, 0, BLAKE2B_BLOCKBYTES - S->buflen ); /* Padding */
313   blake2b_compress( S, S->buf );
314
315   for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
316     store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
317
318   memcpy( out, buffer, S->outlen );
319   secure_zero_memory(buffer, sizeof(buffer));
320   return 0;
321 }
322
323 /* inlen, at least, should be uint64_t. Others can be size_t. */
324 int blake2b( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen )
325 {
326   blake2b_state S[1];
327
328   /* Verify parameters */
329   if ( NULL == in && inlen > 0 ) return -1;
330
331   if ( NULL == out ) return -1;
332
333   if( NULL == key && keylen > 0 ) return -1;
334
335   if( !outlen || outlen > BLAKE2B_OUTBYTES ) return -1;
336
337   if( keylen > BLAKE2B_KEYBYTES ) return -1;
338
339   if( keylen > 0 )
340   {
341     if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1;
342   }
343   else
344   {
345     if( blake2b_init( S, outlen ) < 0 ) return -1;
346   }
347
348   blake2b_update( S, ( const uint8_t * )in, inlen );
349   blake2b_final( S, out, outlen );
350   return 0;
351 }
352
353 int blake2( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ) {
354   return blake2b(out, outlen, in, inlen, key, keylen);
355 }