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

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

Issue 26798: add BLAKE2 to hashlib
Patch Set: Created 3 years, 1 month 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/clinic/blake2s_impl.c.h ('k') | Modules/_blake2/impl/blake2b-load-sse2.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 - optimized 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 #include "blake2-config.h"
24
25 #ifdef _MSC_VER
26 #include <intrin.h> /* for _mm_set_epi64x */
27 #endif
28 #include <emmintrin.h>
29 #if defined(HAVE_SSSE3)
30 #include <tmmintrin.h>
31 #endif
32 #if defined(HAVE_SSE41)
33 #include <smmintrin.h>
34 #endif
35 #if defined(HAVE_AVX)
36 #include <immintrin.h>
37 #endif
38 #if defined(HAVE_XOP)
39 #include <x86intrin.h>
40 #endif
41
42 #include "blake2b-round.h"
43
44 static const uint64_t blake2b_IV[8] =
45 {
46 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
47 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
48 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
49 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
50 };
51
52 static const uint8_t blake2b_sigma[12][16] =
53 {
54 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
55 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
56 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
57 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
58 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
59 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
60 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
61 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
62 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
63 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
64 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
65 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
66 };
67
68
69 /* Some helper functions, not necessarily useful */
70 BLAKE2_LOCAL_INLINE(int) blake2b_set_lastnode( blake2b_state *S )
71 {
72 S->f[1] = -1;
73 return 0;
74 }
75
76 BLAKE2_LOCAL_INLINE(int) blake2b_clear_lastnode( blake2b_state *S )
77 {
78 S->f[1] = 0;
79 return 0;
80 }
81
82 BLAKE2_LOCAL_INLINE(int) blake2b_is_lastblock( const blake2b_state *S )
83 {
84 return S->f[0] != 0;
85 }
86
87 BLAKE2_LOCAL_INLINE(int) blake2b_set_lastblock( blake2b_state *S )
88 {
89 if( S->last_node ) blake2b_set_lastnode( S );
90
91 S->f[0] = -1;
92 return 0;
93 }
94
95 BLAKE2_LOCAL_INLINE(int) blake2b_clear_lastblock( blake2b_state *S )
96 {
97 if( S->last_node ) blake2b_clear_lastnode( S );
98
99 S->f[0] = 0;
100 return 0;
101 }
102
103
104 BLAKE2_LOCAL_INLINE(int) blake2b_increment_counter( blake2b_state *S, const uint 64_t inc )
105 {
106 #if __x86_64__
107 /* ADD/ADC chain */
108 __uint128_t t = ( ( __uint128_t )S->t[1] << 64 ) | S->t[0];
109 t += inc;
110 S->t[0] = ( uint64_t )( t >> 0 );
111 S->t[1] = ( uint64_t )( t >> 64 );
112 #else
113 S->t[0] += inc;
114 S->t[1] += ( S->t[0] < inc );
115 #endif
116 return 0;
117 }
118
119
120 /* Parameter-related functions */
121 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_digest_length( blake2b_param *P, cons t uint8_t digest_length )
122 {
123 P->digest_length = digest_length;
124 return 0;
125 }
126
127 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_fanout( blake2b_param *P, const uint8 _t fanout )
128 {
129 P->fanout = fanout;
130 return 0;
131 }
132
133 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_max_depth( blake2b_param *P, const ui nt8_t depth )
134 {
135 P->depth = depth;
136 return 0;
137 }
138
139 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_leaf_length( blake2b_param *P, const uint32_t leaf_length )
140 {
141 P->leaf_length = leaf_length;
142 return 0;
143 }
144
145 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_node_offset( blake2b_param *P, const uint64_t node_offset )
146 {
147 P->node_offset = node_offset;
148 return 0;
149 }
150
151 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_node_depth( blake2b_param *P, const u int8_t node_depth )
152 {
153 P->node_depth = node_depth;
154 return 0;
155 }
156
157 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_inner_length( blake2b_param *P, const uint8_t inner_length )
158 {
159 P->inner_length = inner_length;
160 return 0;
161 }
162
163 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_salt( blake2b_param *P, const uint8_t salt[BLAKE2B_SALTBYTES] )
164 {
165 memcpy( P->salt, salt, BLAKE2B_SALTBYTES );
166 return 0;
167 }
168
169 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_personal( blake2b_param *P, const uin t8_t personal[BLAKE2B_PERSONALBYTES] )
170 {
171 memcpy( P->personal, personal, BLAKE2B_PERSONALBYTES );
172 return 0;
173 }
174
175 BLAKE2_LOCAL_INLINE(int) blake2b_init0( blake2b_state *S )
176 {
177 memset( S, 0, sizeof( blake2b_state ) );
178
179 for( int i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i];
180
181 return 0;
182 }
183
184 /* init xors IV with input parameter block */
185 int blake2b_init_param( blake2b_state *S, const blake2b_param *P )
186 {
187 /*blake2b_init0( S ); */
188 const uint8_t * v = ( const uint8_t * )( blake2b_IV );
189 const uint8_t * p = ( const uint8_t * )( P );
190 uint8_t * h = ( uint8_t * )( S->h );
191 /* IV XOR ParamBlock */
192 memset( S, 0, sizeof( blake2b_state ) );
193
194 for( int i = 0; i < BLAKE2B_OUTBYTES; ++i ) h[i] = v[i] ^ p[i];
195
196 return 0;
197 }
198
199
200 /* Some sort of default parameter block initialization, for sequential blake2b * /
201 int blake2b_init( blake2b_state *S, const uint8_t outlen )
202 {
203 const blake2b_param P =
204 {
205 outlen,
206 0,
207 1,
208 1,
209 0,
210 0,
211 0,
212 0,
213 {0},
214 {0},
215 {0}
216 };
217
218 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
219
220 return blake2b_init_param( S, &P );
221 }
222
223 int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, c onst uint8_t keylen )
224 {
225 const blake2b_param P =
226 {
227 outlen,
228 keylen,
229 1,
230 1,
231 0,
232 0,
233 0,
234 0,
235 {0},
236 {0},
237 {0}
238 };
239
240 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
241
242 if ( ( !keylen ) || keylen > BLAKE2B_KEYBYTES ) return -1;
243
244 if( blake2b_init_param( S, &P ) < 0 )
245 return 0;
246
247 {
248 uint8_t block[BLAKE2B_BLOCKBYTES];
249 memset( block, 0, BLAKE2B_BLOCKBYTES );
250 memcpy( block, key, keylen );
251 blake2b_update( S, block, BLAKE2B_BLOCKBYTES );
252 secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */
253 }
254 return 0;
255 }
256
257 BLAKE2_LOCAL_INLINE(int) blake2b_compress( blake2b_state *S, const uint8_t block [BLAKE2B_BLOCKBYTES] )
258 {
259 __m128i row1l, row1h;
260 __m128i row2l, row2h;
261 __m128i row3l, row3h;
262 __m128i row4l, row4h;
263 __m128i b0, b1;
264 __m128i t0, t1;
265 #if defined(HAVE_SSSE3) && !defined(HAVE_XOP)
266 const __m128i r16 = _mm_setr_epi8( 2, 3, 4, 5, 6, 7, 0, 1, 10, 11, 12, 13, 14, 15, 8, 9 );
267 const __m128i r24 = _mm_setr_epi8( 3, 4, 5, 6, 7, 0, 1, 2, 11, 12, 13, 14, 15, 8, 9, 10 );
268 #endif
269 #if defined(HAVE_SSE41)
270 const __m128i m0 = LOADU( block + 00 );
271 const __m128i m1 = LOADU( block + 16 );
272 const __m128i m2 = LOADU( block + 32 );
273 const __m128i m3 = LOADU( block + 48 );
274 const __m128i m4 = LOADU( block + 64 );
275 const __m128i m5 = LOADU( block + 80 );
276 const __m128i m6 = LOADU( block + 96 );
277 const __m128i m7 = LOADU( block + 112 );
278 #else
279 const uint64_t m0 = ( ( uint64_t * )block )[ 0];
280 const uint64_t m1 = ( ( uint64_t * )block )[ 1];
281 const uint64_t m2 = ( ( uint64_t * )block )[ 2];
282 const uint64_t m3 = ( ( uint64_t * )block )[ 3];
283 const uint64_t m4 = ( ( uint64_t * )block )[ 4];
284 const uint64_t m5 = ( ( uint64_t * )block )[ 5];
285 const uint64_t m6 = ( ( uint64_t * )block )[ 6];
286 const uint64_t m7 = ( ( uint64_t * )block )[ 7];
287 const uint64_t m8 = ( ( uint64_t * )block )[ 8];
288 const uint64_t m9 = ( ( uint64_t * )block )[ 9];
289 const uint64_t m10 = ( ( uint64_t * )block )[10];
290 const uint64_t m11 = ( ( uint64_t * )block )[11];
291 const uint64_t m12 = ( ( uint64_t * )block )[12];
292 const uint64_t m13 = ( ( uint64_t * )block )[13];
293 const uint64_t m14 = ( ( uint64_t * )block )[14];
294 const uint64_t m15 = ( ( uint64_t * )block )[15];
295 #endif
296 row1l = LOADU( &S->h[0] );
297 row1h = LOADU( &S->h[2] );
298 row2l = LOADU( &S->h[4] );
299 row2h = LOADU( &S->h[6] );
300 row3l = LOADU( &blake2b_IV[0] );
301 row3h = LOADU( &blake2b_IV[2] );
302 row4l = _mm_xor_si128( LOADU( &blake2b_IV[4] ), LOADU( &S->t[0] ) );
303 row4h = _mm_xor_si128( LOADU( &blake2b_IV[6] ), LOADU( &S->f[0] ) );
304 ROUND( 0 );
305 ROUND( 1 );
306 ROUND( 2 );
307 ROUND( 3 );
308 ROUND( 4 );
309 ROUND( 5 );
310 ROUND( 6 );
311 ROUND( 7 );
312 ROUND( 8 );
313 ROUND( 9 );
314 ROUND( 10 );
315 ROUND( 11 );
316 row1l = _mm_xor_si128( row3l, row1l );
317 row1h = _mm_xor_si128( row3h, row1h );
318 STOREU( &S->h[0], _mm_xor_si128( LOADU( &S->h[0] ), row1l ) );
319 STOREU( &S->h[2], _mm_xor_si128( LOADU( &S->h[2] ), row1h ) );
320 row2l = _mm_xor_si128( row4l, row2l );
321 row2h = _mm_xor_si128( row4h, row2h );
322 STOREU( &S->h[4], _mm_xor_si128( LOADU( &S->h[4] ), row2l ) );
323 STOREU( &S->h[6], _mm_xor_si128( LOADU( &S->h[6] ), row2h ) );
324 return 0;
325 }
326
327
328 int blake2b_update( blake2b_state *S, const uint8_t *in, uint64_t inlen )
329 {
330 while( inlen > 0 )
331 {
332 size_t left = S->buflen;
333 size_t fill = 2 * BLAKE2B_BLOCKBYTES - left;
334
335 if( inlen > fill )
336 {
337 memcpy( S->buf + left, in, fill ); /* Fill buffer */
338 S->buflen += fill;
339 blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
340 blake2b_compress( S, S->buf ); /* Compress */
341 memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shif t buffer left */
342 S->buflen -= BLAKE2B_BLOCKBYTES;
343 in += fill;
344 inlen -= fill;
345 }
346 else /* inlen <= fill */
347 {
348 memcpy( S->buf + left, in, inlen );
349 S->buflen += inlen; /* Be lazy, do not compress */
350 in += inlen;
351 inlen -= inlen;
352 }
353 }
354
355 return 0;
356 }
357
358
359 int blake2b_final( blake2b_state *S, uint8_t *out, uint8_t outlen )
360 {
361 if( outlen > BLAKE2B_OUTBYTES )
362 return -1;
363
364 if( blake2b_is_lastblock( S ) )
365 return -1;
366
367 if( S->buflen > BLAKE2B_BLOCKBYTES )
368 {
369 blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
370 blake2b_compress( S, S->buf );
371 S->buflen -= BLAKE2B_BLOCKBYTES;
372 memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, S->buflen );
373 }
374
375 blake2b_increment_counter( S, S->buflen );
376 blake2b_set_lastblock( S );
377 memset( S->buf + S->buflen, 0, 2 * BLAKE2B_BLOCKBYTES - S->buflen ); /* Paddin g */
378 blake2b_compress( S, S->buf );
379 memcpy( out, &S->h[0], outlen );
380 return 0;
381 }
382
383
384 int blake2b( uint8_t *out, const void *in, const void *key, const uint8_t outlen , const uint64_t inlen, uint8_t keylen )
385 {
386 blake2b_state S[1];
387
388 /* Verify parameters */
389 if ( NULL == in && inlen > 0 ) return -1;
390
391 if ( NULL == out ) return -1;
392
393 if( NULL == key && keylen > 0 ) return -1;
394
395 if( !outlen || outlen > BLAKE2B_OUTBYTES ) return -1;
396
397 if( keylen > BLAKE2B_KEYBYTES ) return -1;
398
399 if( keylen )
400 {
401 if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1;
402 }
403 else
404 {
405 if( blake2b_init( S, outlen ) < 0 ) return -1;
406 }
407
408 blake2b_update( S, ( const uint8_t * )in, inlen );
409 blake2b_final( S, out, outlen );
410 return 0;
411 }
412
413 #if defined(SUPERCOP)
414 int crypto_hash( unsigned char *out, unsigned char *in, unsigned long long inlen )
415 {
416 return blake2b( out, in, NULL, BLAKE2B_OUTBYTES, inlen, 0 );
417 }
418 #endif
419
420 #if defined(BLAKE2B_SELFTEST)
421 #include <string.h>
422 #include "blake2-kat.h"
423 int main( int argc, char **argv )
424 {
425 uint8_t key[BLAKE2B_KEYBYTES];
426 uint8_t buf[KAT_LENGTH];
427
428 for( size_t i = 0; i < BLAKE2B_KEYBYTES; ++i )
429 key[i] = ( uint8_t )i;
430
431 for( size_t i = 0; i < KAT_LENGTH; ++i )
432 buf[i] = ( uint8_t )i;
433
434 for( size_t i = 0; i < KAT_LENGTH; ++i )
435 {
436 uint8_t hash[BLAKE2B_OUTBYTES];
437 blake2b( hash, buf, key, BLAKE2B_OUTBYTES, i, BLAKE2B_KEYBYTES );
438
439 if( 0 != memcmp( hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES ) )
440 {
441 puts( "error" );
442 return -1;
443 }
444 }
445
446 puts( "ok" );
447 return 0;
448 }
449 #endif
450
OLDNEW
« no previous file with comments | « Modules/_blake2/clinic/blake2s_impl.c.h ('k') | Modules/_blake2/impl/blake2b-load-sse2.h » ('j') | no next file with comments »

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