]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libarchive/libarchive/archive_blake2s_ref.c
libarchive: import changes from upstream
[FreeBSD/FreeBSD.git] / contrib / libarchive / libarchive / archive_blake2s_ref.c
1 /*
2    BLAKE2 reference source code package - reference C implementations
3
4    Copyright 2012, Samuel Neves <sneves@dei.uc.pt>.  You may use this under the
5    terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
6    your option.  The terms of these licenses can be found at:
7
8    - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
9    - OpenSSL license   : https://www.openssl.org/source/license.html
10    - Apache 2.0        : http://www.apache.org/licenses/LICENSE-2.0
11
12    More information about the BLAKE2 hash function can be found at
13    https://blake2.net.
14 */
15
16 #include <stdint.h>
17 #include <string.h>
18 #include <stdio.h>
19
20 #include "archive_platform.h"
21 #include "archive_blake2.h"
22 #include "archive_blake2_impl.h"
23
24 static const uint32_t blake2s_IV[8] =
25 {
26   0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL,
27   0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL
28 };
29
30 static const uint8_t blake2s_sigma[10][16] =
31 {
32   {  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15 } ,
33   { 14, 10,  4,  8,  9, 15, 13,  6,  1, 12,  0,  2, 11,  7,  5,  3 } ,
34   { 11,  8, 12,  0,  5,  2, 15, 13, 10, 14,  3,  6,  7,  1,  9,  4 } ,
35   {  7,  9,  3,  1, 13, 12, 11, 14,  2,  6,  5, 10,  4,  0, 15,  8 } ,
36   {  9,  0,  5,  7,  2,  4, 10, 15, 14,  1, 11, 12,  6,  8,  3, 13 } ,
37   {  2, 12,  6, 10,  0, 11,  8,  3,  4, 13,  7,  5, 15, 14,  1,  9 } ,
38   { 12,  5,  1, 15, 14, 13,  4, 10,  0,  7,  6,  3,  9,  2,  8, 11 } ,
39   { 13, 11,  7, 14, 12,  1,  3,  9,  5,  0, 15,  4,  8,  6,  2, 10 } ,
40   {  6, 15, 14,  9, 11,  3,  0,  8, 12,  2, 13,  7,  1,  4, 10,  5 } ,
41   { 10,  2,  8,  4,  7,  6,  1,  5, 15, 11,  9, 14,  3, 12, 13 , 0 } ,
42 };
43
44 static void blake2s_set_lastnode( blake2s_state *S )
45 {
46   S->f[1] = (uint32_t)-1;
47 }
48
49 /* Some helper functions, not necessarily useful */
50 static int blake2s_is_lastblock( const blake2s_state *S )
51 {
52   return S->f[0] != 0;
53 }
54
55 static void blake2s_set_lastblock( blake2s_state *S )
56 {
57   if( S->last_node ) blake2s_set_lastnode( S );
58
59   S->f[0] = (uint32_t)-1;
60 }
61
62 static void blake2s_increment_counter( blake2s_state *S, const uint32_t inc )
63 {
64   S->t[0] += inc;
65   S->t[1] += ( S->t[0] < inc );
66 }
67
68 static void blake2s_init0( blake2s_state *S )
69 {
70   size_t i;
71   memset( S, 0, sizeof( blake2s_state ) );
72
73   for( i = 0; i < 8; ++i ) S->h[i] = blake2s_IV[i];
74 }
75
76 /* init2 xors IV with input parameter block */
77 int blake2s_init_param( blake2s_state *S, const blake2s_param *P )
78 {
79   const unsigned char *p = ( const unsigned char * )( P );
80   size_t i;
81
82   blake2s_init0( S );
83
84   /* IV XOR ParamBlock */
85   for( i = 0; i < 8; ++i )
86     S->h[i] ^= load32( &p[i * 4] );
87
88   S->outlen = P->digest_length;
89   return 0;
90 }
91
92
93 /* Sequential blake2s initialization */
94 int blake2s_init( blake2s_state *S, size_t outlen )
95 {
96   blake2s_param P[1];
97
98   /* Move interval verification here? */
99   if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1;
100
101   P->digest_length = (uint8_t)outlen;
102   P->key_length    = 0;
103   P->fanout        = 1;
104   P->depth         = 1;
105   store32( &P->leaf_length, 0 );
106   store32( &P->node_offset, 0 );
107   store16( &P->xof_length, 0 );
108   P->node_depth    = 0;
109   P->inner_length  = 0;
110   /* memset(P->reserved, 0, sizeof(P->reserved) ); */
111   memset( P->salt,     0, sizeof( P->salt ) );
112   memset( P->personal, 0, sizeof( P->personal ) );
113   return blake2s_init_param( S, P );
114 }
115
116 int blake2s_init_key( blake2s_state *S, size_t outlen, const void *key, size_t keylen )
117 {
118   blake2s_param P[1];
119
120   if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1;
121
122   if ( !key || !keylen || keylen > BLAKE2S_KEYBYTES ) return -1;
123
124   P->digest_length = (uint8_t)outlen;
125   P->key_length    = (uint8_t)keylen;
126   P->fanout        = 1;
127   P->depth         = 1;
128   store32( &P->leaf_length, 0 );
129   store32( &P->node_offset, 0 );
130   store16( &P->xof_length, 0 );
131   P->node_depth    = 0;
132   P->inner_length  = 0;
133   /* memset(P->reserved, 0, sizeof(P->reserved) ); */
134   memset( P->salt,     0, sizeof( P->salt ) );
135   memset( P->personal, 0, sizeof( P->personal ) );
136
137   if( blake2s_init_param( S, P ) < 0 ) return -1;
138
139   {
140     uint8_t block[BLAKE2S_BLOCKBYTES];
141     memset( block, 0, BLAKE2S_BLOCKBYTES );
142     memcpy( block, key, keylen );
143     blake2s_update( S, block, BLAKE2S_BLOCKBYTES );
144     secure_zero_memory( block, BLAKE2S_BLOCKBYTES ); /* Burn the key from stack */
145   }
146   return 0;
147 }
148
149 #define G(r,i,a,b,c,d)                      \
150   do {                                      \
151     a = a + b + m[blake2s_sigma[r][2*i+0]]; \
152     d = rotr32(d ^ a, 16);                  \
153     c = c + d;                              \
154     b = rotr32(b ^ c, 12);                  \
155     a = a + b + m[blake2s_sigma[r][2*i+1]]; \
156     d = rotr32(d ^ a, 8);                   \
157     c = c + d;                              \
158     b = rotr32(b ^ c, 7);                   \
159   } while(0)
160
161 #define ROUND(r)                    \
162   do {                              \
163     G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
164     G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
165     G(r,2,v[ 2],v[ 6],v[10],v[14]); \
166     G(r,3,v[ 3],v[ 7],v[11],v[15]); \
167     G(r,4,v[ 0],v[ 5],v[10],v[15]); \
168     G(r,5,v[ 1],v[ 6],v[11],v[12]); \
169     G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
170     G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
171   } while(0)
172
173 static void blake2s_compress( blake2s_state *S, const uint8_t in[BLAKE2S_BLOCKBYTES] )
174 {
175   uint32_t m[16];
176   uint32_t v[16];
177   size_t i;
178
179   for( i = 0; i < 16; ++i ) {
180     m[i] = load32( in + i * sizeof( m[i] ) );
181   }
182
183   for( i = 0; i < 8; ++i ) {
184     v[i] = S->h[i];
185   }
186
187   v[ 8] = blake2s_IV[0];
188   v[ 9] = blake2s_IV[1];
189   v[10] = blake2s_IV[2];
190   v[11] = blake2s_IV[3];
191   v[12] = S->t[0] ^ blake2s_IV[4];
192   v[13] = S->t[1] ^ blake2s_IV[5];
193   v[14] = S->f[0] ^ blake2s_IV[6];
194   v[15] = S->f[1] ^ blake2s_IV[7];
195
196   ROUND( 0 );
197   ROUND( 1 );
198   ROUND( 2 );
199   ROUND( 3 );
200   ROUND( 4 );
201   ROUND( 5 );
202   ROUND( 6 );
203   ROUND( 7 );
204   ROUND( 8 );
205   ROUND( 9 );
206
207   for( i = 0; i < 8; ++i ) {
208     S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
209   }
210 }
211
212 #undef G
213 #undef ROUND
214
215 int blake2s_update( blake2s_state *S, const void *pin, size_t inlen )
216 {
217   const unsigned char * in = (const unsigned char *)pin;
218   if( inlen > 0 )
219   {
220     size_t left = S->buflen;
221     size_t fill = BLAKE2S_BLOCKBYTES - left;
222     if( inlen > fill )
223     {
224       S->buflen = 0;
225       memcpy( S->buf + left, in, fill ); /* Fill buffer */
226       blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
227       blake2s_compress( S, S->buf ); /* Compress */
228       in += fill; inlen -= fill;
229       while(inlen > BLAKE2S_BLOCKBYTES) {
230         blake2s_increment_counter(S, BLAKE2S_BLOCKBYTES);
231         blake2s_compress( S, in );
232         in += BLAKE2S_BLOCKBYTES;
233         inlen -= BLAKE2S_BLOCKBYTES;
234       }
235     }
236     memcpy( S->buf + S->buflen, in, inlen );
237     S->buflen += inlen;
238   }
239   return 0;
240 }
241
242 int blake2s_final( blake2s_state *S, void *out, size_t outlen )
243 {
244   uint8_t buffer[BLAKE2S_OUTBYTES] = {0};
245   size_t i;
246
247   if( out == NULL || outlen < S->outlen )
248     return -1;
249
250   if( blake2s_is_lastblock( S ) )
251     return -1;
252
253   blake2s_increment_counter( S, ( uint32_t )S->buflen );
254   blake2s_set_lastblock( S );
255   memset( S->buf + S->buflen, 0, BLAKE2S_BLOCKBYTES - S->buflen ); /* Padding */
256   blake2s_compress( S, S->buf );
257
258   for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
259     store32( buffer + sizeof( S->h[i] ) * i, S->h[i] );
260
261   memcpy( out, buffer, outlen );
262   secure_zero_memory(buffer, sizeof(buffer));
263   return 0;
264 }
265
266 int blake2s( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen )
267 {
268   blake2s_state S[1];
269
270   /* Verify parameters */
271   if ( NULL == in && inlen > 0 ) return -1;
272
273   if ( NULL == out ) return -1;
274
275   if ( NULL == key && keylen > 0) return -1;
276
277   if( !outlen || outlen > BLAKE2S_OUTBYTES ) return -1;
278
279   if( keylen > BLAKE2S_KEYBYTES ) return -1;
280
281   if( keylen > 0 )
282   {
283     if( blake2s_init_key( S, outlen, key, keylen ) < 0 ) return -1;
284   }
285   else
286   {
287     if( blake2s_init( S, outlen ) < 0 ) return -1;
288   }
289
290   blake2s_update( S, ( const uint8_t * )in, inlen );
291   blake2s_final( S, out, outlen );
292   return 0;
293 }
294
295 #if defined(SUPERCOP)
296 int crypto_hash( unsigned char *out, unsigned char *in, unsigned long long inlen )
297 {
298   return blake2s( out, BLAKE2S_OUTBYTES, in, inlen, NULL, 0 );
299 }
300 #endif
301
302 #if defined(BLAKE2S_SELFTEST)
303 #include <string.h>
304 #include "blake2-kat.h"
305 int main( void )
306 {
307   uint8_t key[BLAKE2S_KEYBYTES];
308   uint8_t buf[BLAKE2_KAT_LENGTH];
309   size_t i, step;
310
311   for( i = 0; i < BLAKE2S_KEYBYTES; ++i )
312     key[i] = ( uint8_t )i;
313
314   for( i = 0; i < BLAKE2_KAT_LENGTH; ++i )
315     buf[i] = ( uint8_t )i;
316
317   /* Test simple API */
318   for( i = 0; i < BLAKE2_KAT_LENGTH; ++i )
319   {
320     uint8_t hash[BLAKE2S_OUTBYTES];
321     blake2s( hash, BLAKE2S_OUTBYTES, buf, i, key, BLAKE2S_KEYBYTES );
322
323     if( 0 != memcmp( hash, blake2s_keyed_kat[i], BLAKE2S_OUTBYTES ) )
324     {
325       goto fail;
326     }
327   }
328
329   /* Test streaming API */
330   for(step = 1; step < BLAKE2S_BLOCKBYTES; ++step) {
331     for (i = 0; i < BLAKE2_KAT_LENGTH; ++i) {
332       uint8_t hash[BLAKE2S_OUTBYTES];
333       blake2s_state S;
334       uint8_t * p = buf;
335       size_t mlen = i;
336       int err = 0;
337
338       if( (err = blake2s_init_key(&S, BLAKE2S_OUTBYTES, key, BLAKE2S_KEYBYTES)) < 0 ) {
339         goto fail;
340       }
341
342       while (mlen >= step) {
343         if ( (err = blake2s_update(&S, p, step)) < 0 ) {
344           goto fail;
345         }
346         mlen -= step;
347         p += step;
348       }
349       if ( (err = blake2s_update(&S, p, mlen)) < 0) {
350         goto fail;
351       }
352       if ( (err = blake2s_final(&S, hash, BLAKE2S_OUTBYTES)) < 0) {
353         goto fail;
354       }
355
356       if (0 != memcmp(hash, blake2s_keyed_kat[i], BLAKE2S_OUTBYTES)) {
357         goto fail;
358       }
359     }
360   }
361
362   puts( "ok" );
363   return 0;
364 fail:
365   puts("error");
366   return -1;
367 }
368 #endif