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

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

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