Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(129493)

Side by Side Diff: Modules/_blake2/impl/blake2b-ref.c

Issue 26798: add BLAKE2 to hashlib
Patch Set: Created 3 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Modules/_blake2/impl/blake2b-load-sse41.h ('k') | Modules/_blake2/impl/blake2b-round.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 "blake2.h"
21 #include "blake2-impl.h"
22
23 static const uint64_t blake2b_IV[8] =
24 {
25 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
26 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
27 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
28 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
29 };
30
31 static const uint8_t blake2b_sigma[12][16] =
32 {
33 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
34 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
35 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
36 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
37 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
38 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
39 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
40 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
41 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
42 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
43 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
44 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
45 };
46
47
48 Py_LOCAL_INLINE(int) blake2b_set_lastnode( blake2b_state *S )
49 {
50 S->f[1] = -1;
51 return 0;
52 }
53
54 Py_LOCAL_INLINE(int) blake2b_clear_lastnode( blake2b_state *S )
55 {
56 S->f[1] = 0;
57 return 0;
58 }
59
60 /* Some helper functions, not necessarily useful */
61 Py_LOCAL_INLINE(int) blake2b_is_lastblock( const blake2b_state *S )
62 {
63 return S->f[0] != 0;
64 }
65
66 Py_LOCAL_INLINE(int) blake2b_set_lastblock( blake2b_state *S )
67 {
68 if( S->last_node ) blake2b_set_lastnode( S );
69
70 S->f[0] = -1;
71 return 0;
72 }
73
74 Py_LOCAL_INLINE(int) blake2b_clear_lastblock( blake2b_state *S )
75 {
76 if( S->last_node ) blake2b_clear_lastnode( S );
77
78 S->f[0] = 0;
79 return 0;
80 }
81
82 Py_LOCAL_INLINE(int) blake2b_increment_counter( blake2b_state *S, const uint64_t inc )
83 {
84 S->t[0] += inc;
85 S->t[1] += ( S->t[0] < inc );
86 return 0;
87 }
88
89
90
91 /* Parameter-related functions */
92 Py_LOCAL_INLINE(int) blake2b_param_set_digest_length( blake2b_param *P, const ui nt8_t digest_length )
93 {
94 P->digest_length = digest_length;
95 return 0;
96 }
97
98 Py_LOCAL_INLINE(int) blake2b_param_set_fanout( blake2b_param *P, const uint8_t f anout )
99 {
100 P->fanout = fanout;
101 return 0;
102 }
103
104 Py_LOCAL_INLINE(int) blake2b_param_set_max_depth( blake2b_param *P, const uint8_ t depth )
105 {
106 P->depth = depth;
107 return 0;
108 }
109
110 Py_LOCAL_INLINE(int) blake2b_param_set_leaf_length( blake2b_param *P, const uint 32_t leaf_length )
111 {
112 store32( &P->leaf_length, leaf_length );
113 return 0;
114 }
115
116 Py_LOCAL_INLINE(int) blake2b_param_set_node_offset( blake2b_param *P, const uint 64_t node_offset )
117 {
118 store64( &P->node_offset, node_offset );
119 return 0;
120 }
121
122 Py_LOCAL_INLINE(int) blake2b_param_set_node_depth( blake2b_param *P, const uint8 _t node_depth )
123 {
124 P->node_depth = node_depth;
125 return 0;
126 }
127
128 Py_LOCAL_INLINE(int) blake2b_param_set_inner_length( blake2b_param *P, const uin t8_t inner_length )
129 {
130 P->inner_length = inner_length;
131 return 0;
132 }
133
134 Py_LOCAL_INLINE(int) blake2b_param_set_salt( blake2b_param *P, const uint8_t sal t[BLAKE2B_SALTBYTES] )
135 {
136 memcpy( P->salt, salt, BLAKE2B_SALTBYTES );
137 return 0;
138 }
139
140 Py_LOCAL_INLINE(int) blake2b_param_set_personal( blake2b_param *P, const uint8_t personal[BLAKE2B_PERSONALBYTES] )
141 {
142 memcpy( P->personal, personal, BLAKE2B_PERSONALBYTES );
143 return 0;
144 }
145
146 Py_LOCAL_INLINE(int) blake2b_init0( blake2b_state *S )
147 {
148 memset( S, 0, sizeof( blake2b_state ) );
149
150 for( int i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i];
151
152 return 0;
153 }
154
155 /* init xors IV with input parameter block */
156 int blake2b_init_param( blake2b_state *S, const blake2b_param *P )
157 {
158 const uint8_t *p = ( const uint8_t * )( P );
159
160 blake2b_init0( S );
161
162 /* IV XOR ParamBlock */
163 for( size_t i = 0; i < 8; ++i )
164 S->h[i] ^= load64( p + sizeof( S->h[i] ) * i );
165
166 return 0;
167 }
168
169
170
171 int blake2b_init( blake2b_state *S, const uint8_t outlen )
172 {
173 blake2b_param P[1];
174
175 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
176
177 P->digest_length = outlen;
178 P->key_length = 0;
179 P->fanout = 1;
180 P->depth = 1;
181 store32( &P->leaf_length, 0 );
182 store64( &P->node_offset, 0 );
183 P->node_depth = 0;
184 P->inner_length = 0;
185 memset( P->reserved, 0, sizeof( P->reserved ) );
186 memset( P->salt, 0, sizeof( P->salt ) );
187 memset( P->personal, 0, sizeof( P->personal ) );
188 return blake2b_init_param( S, P );
189 }
190
191
192 int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen )
193 {
194 blake2b_param P[1];
195
196 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
197
198 if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1;
199
200 P->digest_length = outlen;
201 P->key_length = keylen;
202 P->fanout = 1;
203 P->depth = 1;
204 store32( &P->leaf_length, 0 );
205 store64( &P->node_offset, 0 );
206 P->node_depth = 0;
207 P->inner_length = 0;
208 memset( P->reserved, 0, sizeof( P->reserved ) );
209 memset( P->salt, 0, sizeof( P->salt ) );
210 memset( P->personal, 0, sizeof( P->personal ) );
211
212 if( blake2b_init_param( S, P ) < 0 ) return -1;
213
214 {
215 uint8_t block[BLAKE2B_BLOCKBYTES];
216 memset( block, 0, BLAKE2B_BLOCKBYTES );
217 memcpy( block, key, keylen );
218 blake2b_update( S, block, BLAKE2B_BLOCKBYTES );
219 secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */
220 }
221 return 0;
222 }
223
224 static int blake2b_compress( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCK BYTES] )
225 {
226 uint64_t m[16];
227 uint64_t v[16];
228 int i;
229
230 for( i = 0; i < 16; ++i )
231 m[i] = load64( block + i * sizeof( m[i] ) );
232
233 for( i = 0; i < 8; ++i )
234 v[i] = S->h[i];
235
236 v[ 8] = blake2b_IV[0];
237 v[ 9] = blake2b_IV[1];
238 v[10] = blake2b_IV[2];
239 v[11] = blake2b_IV[3];
240 v[12] = S->t[0] ^ blake2b_IV[4];
241 v[13] = S->t[1] ^ blake2b_IV[5];
242 v[14] = S->f[0] ^ blake2b_IV[6];
243 v[15] = S->f[1] ^ blake2b_IV[7];
244 #define G(r,i,a,b,c,d) \
245 do { \
246 a = a + b + m[blake2b_sigma[r][2*i+0]]; \
247 d = rotr64(d ^ a, 32); \
248 c = c + d; \
249 b = rotr64(b ^ c, 24); \
250 a = a + b + m[blake2b_sigma[r][2*i+1]]; \
251 d = rotr64(d ^ a, 16); \
252 c = c + d; \
253 b = rotr64(b ^ c, 63); \
254 } while(0)
255 #define ROUND(r) \
256 do { \
257 G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
258 G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
259 G(r,2,v[ 2],v[ 6],v[10],v[14]); \
260 G(r,3,v[ 3],v[ 7],v[11],v[15]); \
261 G(r,4,v[ 0],v[ 5],v[10],v[15]); \
262 G(r,5,v[ 1],v[ 6],v[11],v[12]); \
263 G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
264 G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
265 } while(0)
266 ROUND( 0 );
267 ROUND( 1 );
268 ROUND( 2 );
269 ROUND( 3 );
270 ROUND( 4 );
271 ROUND( 5 );
272 ROUND( 6 );
273 ROUND( 7 );
274 ROUND( 8 );
275 ROUND( 9 );
276 ROUND( 10 );
277 ROUND( 11 );
278
279 for( i = 0; i < 8; ++i )
280 S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
281
282 #undef G
283 #undef ROUND
284 return 0;
285 }
286
287 /* inlen now in bytes */
288 int blake2b_update( blake2b_state *S, const uint8_t *in, uint64_t inlen )
289 {
290 while( inlen > 0 )
291 {
292 size_t left = S->buflen;
293 size_t fill = 2 * BLAKE2B_BLOCKBYTES - left;
294
295 if( inlen > fill )
296 {
297 memcpy( S->buf + left, in, fill ); /* Fill buffer */
298 S->buflen += fill;
299 blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
300 blake2b_compress( S, S->buf ); /* Compress */
301 memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shif t buffer left */
302 S->buflen -= BLAKE2B_BLOCKBYTES;
303 in += fill;
304 inlen -= fill;
305 }
306 else /* inlen <= fill */
307 {
308 memcpy( S->buf + left, in, inlen );
309 S->buflen += inlen; /* Be lazy, do not compress */
310 in += inlen;
311 inlen -= inlen;
312 }
313 }
314
315 return 0;
316 }
317
318 /* Is this correct? */
319 int blake2b_final( blake2b_state *S, uint8_t *out, uint8_t outlen )
320 {
321 uint8_t buffer[BLAKE2B_OUTBYTES] = {0};
322
323 if( out == NULL || outlen == 0 || outlen > BLAKE2B_OUTBYTES )
324 return -1;
325
326 if( blake2b_is_lastblock( S ) )
327 return -1;
328
329 if( S->buflen > BLAKE2B_BLOCKBYTES )
330 {
331 blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
332 blake2b_compress( S, S->buf );
333 S->buflen -= BLAKE2B_BLOCKBYTES;
334 memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, S->buflen );
335 }
336
337 blake2b_increment_counter( S, S->buflen );
338 blake2b_set_lastblock( S );
339 memset( S->buf + S->buflen, 0, 2 * BLAKE2B_BLOCKBYTES - S->buflen ); /* Paddin g */
340 blake2b_compress( S, S->buf );
341
342 for( int i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
343 store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
344
345 memcpy( out, buffer, outlen );
346 return 0;
347 }
348
349 /* inlen, at least, should be uint64_t. Others can be size_t. */
350 int blake2b( uint8_t *out, const void *in, const void *key, const uint8_t outle n, const uint64_t inlen, uint8_t keylen )
351 {
352 blake2b_state S[1];
353
354 /* Verify parameters */
355 if ( NULL == in && inlen > 0 ) return -1;
356
357 if ( NULL == out ) return -1;
358
359 if( NULL == key && keylen > 0 ) return -1;
360
361 if( !outlen || outlen > BLAKE2B_OUTBYTES ) return -1;
362
363 if( keylen > BLAKE2B_KEYBYTES ) return -1;
364
365 if( keylen > 0 )
366 {
367 if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1;
368 }
369 else
370 {
371 if( blake2b_init( S, outlen ) < 0 ) return -1;
372 }
373
374 blake2b_update( S, ( const uint8_t * )in, inlen );
375 blake2b_final( S, out, outlen );
376 return 0;
377 }
378
379 #if defined(SUPERCOP)
380 int crypto_hash( unsigned char *out, unsigned char *in, unsigned long long inlen )
381 {
382 return blake2b( out, in, NULL, BLAKE2B_OUTBYTES, inlen, 0 );
383 }
384 #endif
385
386 #if defined(BLAKE2B_SELFTEST)
387 #include <string.h>
388 #include "blake2-kat.h"
389 int main( int argc, char **argv )
390 {
391 uint8_t key[BLAKE2B_KEYBYTES];
392 uint8_t buf[KAT_LENGTH];
393
394 for( size_t i = 0; i < BLAKE2B_KEYBYTES; ++i )
395 key[i] = ( uint8_t )i;
396
397 for( size_t i = 0; i < KAT_LENGTH; ++i )
398 buf[i] = ( uint8_t )i;
399
400 for( size_t i = 0; i < KAT_LENGTH; ++i )
401 {
402 uint8_t hash[BLAKE2B_OUTBYTES];
403 blake2b( hash, buf, key, BLAKE2B_OUTBYTES, i, BLAKE2B_KEYBYTES );
404
405 if( 0 != memcmp( hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES ) )
406 {
407 puts( "error" );
408 return -1;
409 }
410 }
411
412 puts( "ok" );
413 return 0;
414 }
415 #endif
416
OLDNEW
« no previous file with comments | « Modules/_blake2/impl/blake2b-load-sse41.h ('k') | Modules/_blake2/impl/blake2b-round.h » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+