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

Side by Side Diff: Modules/_blake2/impl/blake2s.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/impl/blake2-impl.h ('k') | Modules/_blake2/impl/blake2s-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
26 #include <emmintrin.h>
27 #if defined(HAVE_SSSE3)
28 #include <tmmintrin.h>
29 #endif
30 #if defined(HAVE_SSE41)
31 #include <smmintrin.h>
32 #endif
33 #if defined(HAVE_AVX)
34 #include <immintrin.h>
35 #endif
36 #if defined(HAVE_XOP)
37 #include <x86intrin.h>
38 #endif
39
40 #include "blake2s-round.h"
41
42 static const uint32_t blake2s_IV[8] =
43 {
44 0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL,
45 0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL
46 };
47
48 static const uint8_t blake2s_sigma[10][16] =
49 {
50 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
51 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
52 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
53 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
54 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
55 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
56 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
57 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
58 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
59 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
60 };
61
62
63 /* Some helper functions, not necessarily useful */
64 BLAKE2_LOCAL_INLINE(int) blake2s_set_lastnode( blake2s_state *S )
65 {
66 S->f[1] = -1;
67 return 0;
68 }
69
70 BLAKE2_LOCAL_INLINE(int) blake2s_clear_lastnode( blake2s_state *S )
71 {
72 S->f[1] = 0;
73 return 0;
74 }
75
76 BLAKE2_LOCAL_INLINE(int) blake2s_is_lastblock( const blake2s_state *S )
77 {
78 return S->f[0] != 0;
79 }
80
81 BLAKE2_LOCAL_INLINE(int) blake2s_set_lastblock( blake2s_state *S )
82 {
83 if( S->last_node ) blake2s_set_lastnode( S );
84
85 S->f[0] = -1;
86 return 0;
87 }
88
89 BLAKE2_LOCAL_INLINE(int) blake2s_clear_lastblock( blake2s_state *S )
90 {
91 if( S->last_node ) blake2s_clear_lastnode( S );
92
93 S->f[0] = 0;
94 return 0;
95 }
96
97 BLAKE2_LOCAL_INLINE(int) blake2s_increment_counter( blake2s_state *S, const uint 32_t inc )
98 {
99 uint64_t t = ( ( uint64_t )S->t[1] << 32 ) | S->t[0];
100 t += inc;
101 S->t[0] = ( uint32_t )( t >> 0 );
102 S->t[1] = ( uint32_t )( t >> 32 );
103 return 0;
104 }
105
106
107 /* Parameter-related functions */
108 BLAKE2_LOCAL_INLINE(int) blake2s_param_set_digest_length( blake2s_param *P, cons t uint8_t digest_length )
109 {
110 P->digest_length = digest_length;
111 return 0;
112 }
113
114 BLAKE2_LOCAL_INLINE(int) blake2s_param_set_fanout( blake2s_param *P, const uint8 _t fanout )
115 {
116 P->fanout = fanout;
117 return 0;
118 }
119
120 BLAKE2_LOCAL_INLINE(int) blake2s_param_set_max_depth( blake2s_param *P, const ui nt8_t depth )
121 {
122 P->depth = depth;
123 return 0;
124 }
125
126 BLAKE2_LOCAL_INLINE(int) blake2s_param_set_leaf_length( blake2s_param *P, const uint32_t leaf_length )
127 {
128 P->leaf_length = leaf_length;
129 return 0;
130 }
131
132 BLAKE2_LOCAL_INLINE(int) blake2s_param_set_node_offset( blake2s_param *P, const uint64_t node_offset )
133 {
134 store48( P->node_offset, node_offset );
135 return 0;
136 }
137
138 BLAKE2_LOCAL_INLINE(int) blake2s_param_set_node_depth( blake2s_param *P, const u int8_t node_depth )
139 {
140 P->node_depth = node_depth;
141 return 0;
142 }
143
144 BLAKE2_LOCAL_INLINE(int) blake2s_param_set_inner_length( blake2s_param *P, const uint8_t inner_length )
145 {
146 P->inner_length = inner_length;
147 return 0;
148 }
149
150 BLAKE2_LOCAL_INLINE(int) blake2s_param_set_salt( blake2s_param *P, const uint8_t salt[BLAKE2S_SALTBYTES] )
151 {
152 memcpy( P->salt, salt, BLAKE2S_SALTBYTES );
153 return 0;
154 }
155
156 BLAKE2_LOCAL_INLINE(int) blake2s_param_set_personal( blake2s_param *P, const uin t8_t personal[BLAKE2S_PERSONALBYTES] )
157 {
158 memcpy( P->personal, personal, BLAKE2S_PERSONALBYTES );
159 return 0;
160 }
161
162 BLAKE2_LOCAL_INLINE(int) blake2s_init0( blake2s_state *S )
163 {
164 memset( S, 0, sizeof( blake2s_state ) );
165
166 for( int i = 0; i < 8; ++i ) S->h[i] = blake2s_IV[i];
167
168 return 0;
169 }
170
171 /* init2 xors IV with input parameter block */
172 int blake2s_init_param( blake2s_state *S, const blake2s_param *P )
173 {
174 /*blake2s_init0( S ); */
175 const uint8_t * v = ( const uint8_t * )( blake2s_IV );
176 const uint8_t * p = ( const uint8_t * )( P );
177 uint8_t * h = ( uint8_t * )( S->h );
178 /* IV XOR ParamBlock */
179 memset( S, 0, sizeof( blake2s_state ) );
180
181 for( int i = 0; i < BLAKE2S_OUTBYTES; ++i ) h[i] = v[i] ^ p[i];
182
183 return 0;
184 }
185
186
187 /* Some sort of default parameter block initialization, for sequential blake2s * /
188 int blake2s_init( blake2s_state *S, const uint8_t outlen )
189 {
190 const blake2s_param P =
191 {
192 outlen,
193 0,
194 1,
195 1,
196 0,
197 {0},
198 0,
199 0,
200 {0},
201 {0}
202 };
203 /* Move interval verification here? */
204 if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1;
205 return blake2s_init_param( S, &P );
206 }
207
208
209 int blake2s_init_key( blake2s_state *S, const uint8_t outlen, const void *key, c onst uint8_t keylen )
210 {
211 const blake2s_param P =
212 {
213 outlen,
214 keylen,
215 1,
216 1,
217 0,
218 {0},
219 0,
220 0,
221 {0},
222 {0}
223 };
224
225 /* Move interval verification here? */
226 if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1;
227
228 if ( ( !key ) || ( !keylen ) || keylen > BLAKE2S_KEYBYTES ) return -1;
229
230 if( blake2s_init_param( S, &P ) < 0 )
231 return -1;
232
233 {
234 uint8_t block[BLAKE2S_BLOCKBYTES];
235 memset( block, 0, BLAKE2S_BLOCKBYTES );
236 memcpy( block, key, keylen );
237 blake2s_update( S, block, BLAKE2S_BLOCKBYTES );
238 secure_zero_memory( block, BLAKE2S_BLOCKBYTES ); /* Burn the key from stack */
239 }
240 return 0;
241 }
242
243
244 BLAKE2_LOCAL_INLINE(int) blake2s_compress( blake2s_state *S, const uint8_t block [BLAKE2S_BLOCKBYTES] )
245 {
246 __m128i row1, row2, row3, row4;
247 __m128i buf1, buf2, buf3, buf4;
248 #if defined(HAVE_SSE41)
249 __m128i t0, t1;
250 #if !defined(HAVE_XOP)
251 __m128i t2;
252 #endif
253 #endif
254 __m128i ff0, ff1;
255 #if defined(HAVE_SSSE3) && !defined(HAVE_XOP)
256 const __m128i r8 = _mm_set_epi8( 12, 15, 14, 13, 8, 11, 10, 9, 4, 7, 6, 5, 0, 3, 2, 1 );
257 const __m128i r16 = _mm_set_epi8( 13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0, 3, 2 );
258 #endif
259 #if defined(HAVE_SSE41)
260 const __m128i m0 = LOADU( block + 00 );
261 const __m128i m1 = LOADU( block + 16 );
262 const __m128i m2 = LOADU( block + 32 );
263 const __m128i m3 = LOADU( block + 48 );
264 #else
265 const uint32_t m0 = ( ( uint32_t * )block )[ 0];
266 const uint32_t m1 = ( ( uint32_t * )block )[ 1];
267 const uint32_t m2 = ( ( uint32_t * )block )[ 2];
268 const uint32_t m3 = ( ( uint32_t * )block )[ 3];
269 const uint32_t m4 = ( ( uint32_t * )block )[ 4];
270 const uint32_t m5 = ( ( uint32_t * )block )[ 5];
271 const uint32_t m6 = ( ( uint32_t * )block )[ 6];
272 const uint32_t m7 = ( ( uint32_t * )block )[ 7];
273 const uint32_t m8 = ( ( uint32_t * )block )[ 8];
274 const uint32_t m9 = ( ( uint32_t * )block )[ 9];
275 const uint32_t m10 = ( ( uint32_t * )block )[10];
276 const uint32_t m11 = ( ( uint32_t * )block )[11];
277 const uint32_t m12 = ( ( uint32_t * )block )[12];
278 const uint32_t m13 = ( ( uint32_t * )block )[13];
279 const uint32_t m14 = ( ( uint32_t * )block )[14];
280 const uint32_t m15 = ( ( uint32_t * )block )[15];
281 #endif
282 row1 = ff0 = LOADU( &S->h[0] );
283 row2 = ff1 = LOADU( &S->h[4] );
284 row3 = _mm_setr_epi32( 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A );
285 row4 = _mm_xor_si128( _mm_setr_epi32( 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5B E0CD19 ), LOADU( &S->t[0] ) );
286 ROUND( 0 );
287 ROUND( 1 );
288 ROUND( 2 );
289 ROUND( 3 );
290 ROUND( 4 );
291 ROUND( 5 );
292 ROUND( 6 );
293 ROUND( 7 );
294 ROUND( 8 );
295 ROUND( 9 );
296 STOREU( &S->h[0], _mm_xor_si128( ff0, _mm_xor_si128( row1, row3 ) ) );
297 STOREU( &S->h[4], _mm_xor_si128( ff1, _mm_xor_si128( row2, row4 ) ) );
298 return 0;
299 }
300
301 /* inlen now in bytes */
302 int blake2s_update( blake2s_state *S, const uint8_t *in, uint64_t inlen )
303 {
304 while( inlen > 0 )
305 {
306 size_t left = S->buflen;
307 size_t fill = 2 * BLAKE2S_BLOCKBYTES - left;
308
309 if( inlen > fill )
310 {
311 memcpy( S->buf + left, in, fill ); /* Fill buffer */
312 S->buflen += fill;
313 blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
314 blake2s_compress( S, S->buf ); /* Compress */
315 memcpy( S->buf, S->buf + BLAKE2S_BLOCKBYTES, BLAKE2S_BLOCKBYTES ); /* Shif t buffer left */
316 S->buflen -= BLAKE2S_BLOCKBYTES;
317 in += fill;
318 inlen -= fill;
319 }
320 else /* inlen <= fill */
321 {
322 memcpy( S->buf + left, in, inlen );
323 S->buflen += inlen; /* Be lazy, do not compress */
324 in += inlen;
325 inlen -= inlen;
326 }
327 }
328
329 return 0;
330 }
331
332 /* Is this correct? */
333 int blake2s_final( blake2s_state *S, uint8_t *out, uint8_t outlen )
334 {
335 uint8_t buffer[BLAKE2S_OUTBYTES] = {0};
336
337 if( outlen > BLAKE2S_OUTBYTES )
338 return -1;
339
340 if( blake2s_is_lastblock( S ) )
341 return -1;
342
343 if( S->buflen > BLAKE2S_BLOCKBYTES )
344 {
345 blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
346 blake2s_compress( S, S->buf );
347 S->buflen -= BLAKE2S_BLOCKBYTES;
348 memcpy( S->buf, S->buf + BLAKE2S_BLOCKBYTES, S->buflen );
349 }
350
351 blake2s_increment_counter( S, ( uint32_t )S->buflen );
352 blake2s_set_lastblock( S );
353 memset( S->buf + S->buflen, 0, 2 * BLAKE2S_BLOCKBYTES - S->buflen ); /* Paddin g */
354 blake2s_compress( S, S->buf );
355
356 for( int i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
357 store32( buffer + sizeof( S->h[i] ) * i, S->h[i] );
358
359 memcpy( out, buffer, outlen );
360 return 0;
361 }
362
363 /* inlen, at least, should be uint64_t. Others can be size_t. */
364 int blake2s( uint8_t *out, const void *in, const void *key, const uint8_t outlen , const uint64_t inlen, uint8_t keylen )
365 {
366 blake2s_state S[1];
367
368 /* Verify parameters */
369 if ( NULL == in && inlen > 0 ) return -1;
370
371 if ( NULL == out ) return -1;
372
373 if ( NULL == key && keylen > 0) return -1;
374
375 if( !outlen || outlen > BLAKE2S_OUTBYTES ) return -1;
376
377 if( keylen > BLAKE2S_KEYBYTES ) return -1;
378
379 if( keylen > 0 )
380 {
381 if( blake2s_init_key( S, outlen, key, keylen ) < 0 ) return -1;
382 }
383 else
384 {
385 if( blake2s_init( S, outlen ) < 0 ) return -1;
386 }
387
388 blake2s_update( S, ( const uint8_t * )in, inlen );
389 blake2s_final( S, out, outlen );
390 return 0;
391 }
392
393 #if defined(SUPERCOP)
394 int crypto_hash( unsigned char *out, unsigned char *in, unsigned long long inlen )
395 {
396 return blake2s( out, in, NULL, BLAKE2S_OUTBYTES, inlen, 0 );
397 }
398 #endif
399
400 #if defined(BLAKE2S_SELFTEST)
401 #include <string.h>
402 #include "blake2-kat.h"
403 int main( int argc, char **argv )
404 {
405 uint8_t key[BLAKE2S_KEYBYTES];
406 uint8_t buf[KAT_LENGTH];
407
408 for( size_t i = 0; i < BLAKE2S_KEYBYTES; ++i )
409 key[i] = ( uint8_t )i;
410
411 for( size_t i = 0; i < KAT_LENGTH; ++i )
412 buf[i] = ( uint8_t )i;
413
414 for( size_t i = 0; i < KAT_LENGTH; ++i )
415 {
416 uint8_t hash[BLAKE2S_OUTBYTES];
417
418 if( blake2s( hash, buf, key, BLAKE2S_OUTBYTES, i, BLAKE2S_KEYBYTES ) < 0 ||
419 0 != memcmp( hash, blake2s_keyed_kat[i], BLAKE2S_OUTBYTES ) )
420 {
421 puts( "error" );
422 return -1;
423 }
424 }
425
426 puts( "ok" );
427 return 0;
428 }
429 #endif
430
431
OLDNEW
« no previous file with comments | « Modules/_blake2/impl/blake2-impl.h ('k') | Modules/_blake2/impl/blake2s-load-sse2.h » ('j') | no next file with comments »

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