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

Delta Between Two Patch Sets: Modules/_blake2/impl/blake2b-ref.c

Issue 26798: add BLAKE2 to hashlib
Left Patch Set: Created 3 years, 5 months ago
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Modules/_blake2/impl/blake2b-load-sse41.h ('k') | Modules/_blake2/impl/blake2b-round.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 BLAKE2 reference source code package - reference C implementations 2 BLAKE2 reference source code package - reference C implementations
3 3
4 Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the 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 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: 6 your option. The terms of these licenses can be found at:
7 7
8 - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0 8 - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
9 - OpenSSL license : https://www.openssl.org/source/license.html 9 - OpenSSL license : https://www.openssl.org/source/license.html
10 - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0 10 - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
(...skipping 27 matching lines...) Expand all
38 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } , 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 } , 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 } , 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 } , 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 } , 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 } , 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 } 44 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
45 }; 45 };
46 46
47 47
48 Py_LOCAL_INLINE(int) blake2b_set_lastnode( blake2b_state *S ) 48 BLAKE2_LOCAL_INLINE(int) blake2b_set_lastnode( blake2b_state *S )
49 { 49 {
50 S->f[1] = -1; 50 S->f[1] = -1;
51 return 0; 51 return 0;
52 } 52 }
53 53
54 Py_LOCAL_INLINE(int) blake2b_clear_lastnode( blake2b_state *S ) 54 BLAKE2_LOCAL_INLINE(int) blake2b_clear_lastnode( blake2b_state *S )
55 { 55 {
56 S->f[1] = 0; 56 S->f[1] = 0;
57 return 0; 57 return 0;
58 } 58 }
59 59
60 /* Some helper functions, not necessarily useful */ 60 /* Some helper functions, not necessarily useful */
61 Py_LOCAL_INLINE(int) blake2b_is_lastblock( const blake2b_state *S ) 61 BLAKE2_LOCAL_INLINE(int) blake2b_is_lastblock( const blake2b_state *S )
62 { 62 {
63 return S->f[0] != 0; 63 return S->f[0] != 0;
64 } 64 }
65 65
66 Py_LOCAL_INLINE(int) blake2b_set_lastblock( blake2b_state *S ) 66 BLAKE2_LOCAL_INLINE(int) blake2b_set_lastblock( blake2b_state *S )
67 { 67 {
68 if( S->last_node ) blake2b_set_lastnode( S ); 68 if( S->last_node ) blake2b_set_lastnode( S );
69 69
70 S->f[0] = -1; 70 S->f[0] = -1;
71 return 0; 71 return 0;
72 } 72 }
73 73
74 Py_LOCAL_INLINE(int) blake2b_clear_lastblock( blake2b_state *S ) 74 BLAKE2_LOCAL_INLINE(int) blake2b_clear_lastblock( blake2b_state *S )
75 { 75 {
76 if( S->last_node ) blake2b_clear_lastnode( S ); 76 if( S->last_node ) blake2b_clear_lastnode( S );
77 77
78 S->f[0] = 0; 78 S->f[0] = 0;
79 return 0; 79 return 0;
80 } 80 }
81 81
82 Py_LOCAL_INLINE(int) blake2b_increment_counter( blake2b_state *S, const uint64_t inc ) 82 BLAKE2_LOCAL_INLINE(int) blake2b_increment_counter( blake2b_state *S, const uint 64_t inc )
83 { 83 {
84 S->t[0] += inc; 84 S->t[0] += inc;
85 S->t[1] += ( S->t[0] < inc ); 85 S->t[1] += ( S->t[0] < inc );
86 return 0; 86 return 0;
87 } 87 }
88 88
89 89
90 90
91 /* Parameter-related functions */ 91 /* Parameter-related functions */
92 Py_LOCAL_INLINE(int) blake2b_param_set_digest_length( blake2b_param *P, const ui nt8_t digest_length ) 92 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_digest_length( blake2b_param *P, cons t uint8_t digest_length )
93 { 93 {
94 P->digest_length = digest_length; 94 P->digest_length = digest_length;
95 return 0; 95 return 0;
96 } 96 }
97 97
98 Py_LOCAL_INLINE(int) blake2b_param_set_fanout( blake2b_param *P, const uint8_t f anout ) 98 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_fanout( blake2b_param *P, const uint8 _t fanout )
99 { 99 {
100 P->fanout = fanout; 100 P->fanout = fanout;
101 return 0; 101 return 0;
102 } 102 }
103 103
104 Py_LOCAL_INLINE(int) blake2b_param_set_max_depth( blake2b_param *P, const uint8_ t depth ) 104 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_max_depth( blake2b_param *P, const ui nt8_t depth )
105 { 105 {
106 P->depth = depth; 106 P->depth = depth;
107 return 0; 107 return 0;
108 } 108 }
109 109
110 Py_LOCAL_INLINE(int) blake2b_param_set_leaf_length( blake2b_param *P, const uint 32_t leaf_length ) 110 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_leaf_length( blake2b_param *P, const uint32_t leaf_length )
111 { 111 {
112 store32( &P->leaf_length, leaf_length ); 112 store32( &P->leaf_length, leaf_length );
113 return 0; 113 return 0;
114 } 114 }
115 115
116 Py_LOCAL_INLINE(int) blake2b_param_set_node_offset( blake2b_param *P, const uint 64_t node_offset ) 116 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_node_offset( blake2b_param *P, const uint64_t node_offset )
117 { 117 {
118 store64( &P->node_offset, node_offset ); 118 store64( &P->node_offset, node_offset );
119 return 0; 119 return 0;
120 } 120 }
121 121
122 Py_LOCAL_INLINE(int) blake2b_param_set_node_depth( blake2b_param *P, const uint8 _t node_depth ) 122 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_node_depth( blake2b_param *P, const u int8_t node_depth )
123 { 123 {
124 P->node_depth = node_depth; 124 P->node_depth = node_depth;
125 return 0; 125 return 0;
126 } 126 }
127 127
128 Py_LOCAL_INLINE(int) blake2b_param_set_inner_length( blake2b_param *P, const uin t8_t inner_length ) 128 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_inner_length( blake2b_param *P, const uint8_t inner_length )
129 { 129 {
130 P->inner_length = inner_length; 130 P->inner_length = inner_length;
131 return 0; 131 return 0;
132 } 132 }
133 133
134 Py_LOCAL_INLINE(int) blake2b_param_set_salt( blake2b_param *P, const uint8_t sal t[BLAKE2B_SALTBYTES] ) 134 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_salt( blake2b_param *P, const uint8_t salt[BLAKE2B_SALTBYTES] )
135 { 135 {
136 memcpy( P->salt, salt, BLAKE2B_SALTBYTES ); 136 memcpy( P->salt, salt, BLAKE2B_SALTBYTES );
137 return 0; 137 return 0;
138 } 138 }
139 139
140 Py_LOCAL_INLINE(int) blake2b_param_set_personal( blake2b_param *P, const uint8_t personal[BLAKE2B_PERSONALBYTES] ) 140 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_personal( blake2b_param *P, const uin t8_t personal[BLAKE2B_PERSONALBYTES] )
141 { 141 {
142 memcpy( P->personal, personal, BLAKE2B_PERSONALBYTES ); 142 memcpy( P->personal, personal, BLAKE2B_PERSONALBYTES );
143 return 0; 143 return 0;
144 } 144 }
145 145
146 Py_LOCAL_INLINE(int) blake2b_init0( blake2b_state *S ) 146 BLAKE2_LOCAL_INLINE(int) blake2b_init0( blake2b_state *S )
147 { 147 {
148 memset( S, 0, sizeof( blake2b_state ) ); 148 memset( S, 0, sizeof( blake2b_state ) );
149 149
150 for( int i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i]; 150 for( int i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i];
151 151
152 return 0; 152 return 0;
153 } 153 }
154 154
155 /* init xors IV with input parameter block */ 155 /* init xors IV with input parameter block */
156 int blake2b_init_param( blake2b_state *S, const blake2b_param *P ) 156 int blake2b_init_param( blake2b_state *S, const blake2b_param *P )
157 { 157 {
158 const uint8_t *p = ( const uint8_t * )( P ); 158 const uint8_t *p = ( const uint8_t * )( P );
159 159
160 blake2b_init0( S ); 160 blake2b_init0( S );
161 161
162 /* IV XOR ParamBlock */ 162 /* IV XOR ParamBlock */
163 for( size_t i = 0; i < 8; ++i ) 163 for( size_t i = 0; i < 8; ++i )
164 S->h[i] ^= load64( p + sizeof( S->h[i] ) * i ); 164 S->h[i] ^= load64( p + sizeof( S->h[i] ) * i );
165 165
166 return 0; 166 return 0;
167 } 167 }
168 168
169 169
170 170
171 int blake2b_init( blake2b_state *S, const uint8_t outlen ) 171 int blake2b_init( blake2b_state *S, const uint8_t outlen )
172 { 172 {
173 blake2b_param P[1]; 173 blake2b_param P[1];
174 174
175 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1; 175 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
176 176
177 P->digest_length = outlen; 177 P->digest_length = outlen;
178 P->key_length = 0; 178 P->key_length = 0;
179 P->fanout = 1; 179 P->fanout = 1;
180 P->depth = 1; 180 P->depth = 1;
181 store32( &P->leaf_length, 0 ); 181 store32( &P->leaf_length, 0 );
182 store64( &P->node_offset, 0 ); 182 store64( &P->node_offset, 0 );
183 P->node_depth = 0; 183 P->node_depth = 0;
184 P->inner_length = 0; 184 P->inner_length = 0;
185 memset( P->reserved, 0, sizeof( P->reserved ) ); 185 memset( P->reserved, 0, sizeof( P->reserved ) );
186 memset( P->salt, 0, sizeof( P->salt ) ); 186 memset( P->salt, 0, sizeof( P->salt ) );
187 memset( P->personal, 0, sizeof( P->personal ) ); 187 memset( P->personal, 0, sizeof( P->personal ) );
188 return blake2b_init_param( S, P ); 188 return blake2b_init_param( S, P );
189 } 189 }
190 190
191 191
192 int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen ) 192 int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, c onst uint8_t keylen )
193 { 193 {
194 blake2b_param P[1]; 194 blake2b_param P[1];
195 195
196 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1; 196 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
197 197
198 if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1; 198 if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1;
199 199
200 P->digest_length = outlen; 200 P->digest_length = outlen;
201 P->key_length = keylen; 201 P->key_length = keylen;
202 P->fanout = 1; 202 P->fanout = 1;
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 278
279 for( i = 0; i < 8; ++i ) 279 for( i = 0; i < 8; ++i )
280 S->h[i] = S->h[i] ^ v[i] ^ v[i + 8]; 280 S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
281 281
282 #undef G 282 #undef G
283 #undef ROUND 283 #undef ROUND
284 return 0; 284 return 0;
285 } 285 }
286 286
287 /* inlen now in bytes */ 287 /* inlen now in bytes */
288 int blake2b_update( blake2b_state *S, const uint8_t *in, uint64_t inlen ) 288 int blake2b_update( blake2b_state *S, const uint8_t *in, uint64_t inlen )
289 { 289 {
290 while( inlen > 0 ) 290 while( inlen > 0 )
291 { 291 {
292 size_t left = S->buflen; 292 size_t left = S->buflen;
293 size_t fill = 2 * BLAKE2B_BLOCKBYTES - left; 293 size_t fill = 2 * BLAKE2B_BLOCKBYTES - left;
294 294
295 if( inlen > fill ) 295 if( inlen > fill )
296 { 296 {
297 memcpy( S->buf + left, in, fill ); /* Fill buffer */ 297 memcpy( S->buf + left, in, fill ); /* Fill buffer */
298 S->buflen += fill; 298 S->buflen += fill;
(...skipping 10 matching lines...) Expand all
309 S->buflen += inlen; /* Be lazy, do not compress */ 309 S->buflen += inlen; /* Be lazy, do not compress */
310 in += inlen; 310 in += inlen;
311 inlen -= inlen; 311 inlen -= inlen;
312 } 312 }
313 } 313 }
314 314
315 return 0; 315 return 0;
316 } 316 }
317 317
318 /* Is this correct? */ 318 /* Is this correct? */
319 int blake2b_final( blake2b_state *S, uint8_t *out, uint8_t outlen ) 319 int blake2b_final( blake2b_state *S, uint8_t *out, uint8_t outlen )
320 { 320 {
321 uint8_t buffer[BLAKE2B_OUTBYTES] = {0}; 321 uint8_t buffer[BLAKE2B_OUTBYTES] = {0};
322 322
323 if( out == NULL || outlen == 0 || outlen > BLAKE2B_OUTBYTES ) 323 if( out == NULL || outlen == 0 || outlen > BLAKE2B_OUTBYTES )
324 return -1; 324 return -1;
325 325
326 if( blake2b_is_lastblock( S ) ) 326 if( blake2b_is_lastblock( S ) )
327 return -1; 327 return -1;
328 328
329 if( S->buflen > BLAKE2B_BLOCKBYTES ) 329 if( S->buflen > BLAKE2B_BLOCKBYTES )
(...skipping 10 matching lines...) Expand all
340 blake2b_compress( S, S->buf ); 340 blake2b_compress( S, S->buf );
341 341
342 for( int i = 0; i < 8; ++i ) /* Output full hash to temp buffer */ 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] ); 343 store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
344 344
345 memcpy( out, buffer, outlen ); 345 memcpy( out, buffer, outlen );
346 return 0; 346 return 0;
347 } 347 }
348 348
349 /* inlen, at least, should be uint64_t. Others can be size_t. */ 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 ) 350 int blake2b( uint8_t *out, const void *in, const void *key, const uint8_t outlen , const uint64_t inlen, uint8_t keylen )
351 { 351 {
352 blake2b_state S[1]; 352 blake2b_state S[1];
353 353
354 /* Verify parameters */ 354 /* Verify parameters */
355 if ( NULL == in && inlen > 0 ) return -1; 355 if ( NULL == in && inlen > 0 ) return -1;
356 356
357 if ( NULL == out ) return -1; 357 if ( NULL == out ) return -1;
358 358
359 if( NULL == key && keylen > 0 ) return -1; 359 if( NULL == key && keylen > 0 ) return -1;
360 360
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 puts( "error" ); 407 puts( "error" );
408 return -1; 408 return -1;
409 } 409 }
410 } 410 }
411 411
412 puts( "ok" ); 412 puts( "ok" );
413 return 0; 413 return 0;
414 } 414 }
415 #endif 415 #endif
416 416
LEFTRIGHT

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