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

Delta Between Two Patch Sets: Modules/_blake2/impl/blake2s.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/blake2-impl.h ('k') | Modules/_blake2/impl/blake2s-load-sse2.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 - optimized C implementations 2 BLAKE2 reference source code package - optimized 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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } , 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 } , 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 } , 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 } , 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 } , 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 } , 59 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
60 }; 60 };
61 61
62 62
63 /* Some helper functions, not necessarily useful */ 63 /* Some helper functions, not necessarily useful */
64 Py_LOCAL_INLINE(int) blake2s_set_lastnode( blake2s_state *S ) 64 BLAKE2_LOCAL_INLINE(int) blake2s_set_lastnode( blake2s_state *S )
65 { 65 {
66 S->f[1] = -1; 66 S->f[1] = -1;
67 return 0; 67 return 0;
68 } 68 }
69 69
70 Py_LOCAL_INLINE(int) blake2s_clear_lastnode( blake2s_state *S ) 70 BLAKE2_LOCAL_INLINE(int) blake2s_clear_lastnode( blake2s_state *S )
71 { 71 {
72 S->f[1] = 0; 72 S->f[1] = 0;
73 return 0; 73 return 0;
74 } 74 }
75 75
76 Py_LOCAL_INLINE(int) blake2s_is_lastblock( const blake2s_state *S ) 76 BLAKE2_LOCAL_INLINE(int) blake2s_is_lastblock( const blake2s_state *S )
77 { 77 {
78 return S->f[0] != 0; 78 return S->f[0] != 0;
79 } 79 }
80 80
81 Py_LOCAL_INLINE(int) blake2s_set_lastblock( blake2s_state *S ) 81 BLAKE2_LOCAL_INLINE(int) blake2s_set_lastblock( blake2s_state *S )
82 { 82 {
83 if( S->last_node ) blake2s_set_lastnode( S ); 83 if( S->last_node ) blake2s_set_lastnode( S );
84 84
85 S->f[0] = -1; 85 S->f[0] = -1;
86 return 0; 86 return 0;
87 } 87 }
88 88
89 Py_LOCAL_INLINE(int) blake2s_clear_lastblock( blake2s_state *S ) 89 BLAKE2_LOCAL_INLINE(int) blake2s_clear_lastblock( blake2s_state *S )
90 { 90 {
91 if( S->last_node ) blake2s_clear_lastnode( S ); 91 if( S->last_node ) blake2s_clear_lastnode( S );
92 92
93 S->f[0] = 0; 93 S->f[0] = 0;
94 return 0; 94 return 0;
95 } 95 }
96 96
97 Py_LOCAL_INLINE(int) blake2s_increment_counter( blake2s_state *S, const uint32_t inc ) 97 BLAKE2_LOCAL_INLINE(int) blake2s_increment_counter( blake2s_state *S, const uint 32_t inc )
98 { 98 {
99 uint64_t t = ( ( uint64_t )S->t[1] << 32 ) | S->t[0]; 99 uint64_t t = ( ( uint64_t )S->t[1] << 32 ) | S->t[0];
100 t += inc; 100 t += inc;
101 S->t[0] = ( uint32_t )( t >> 0 ); 101 S->t[0] = ( uint32_t )( t >> 0 );
102 S->t[1] = ( uint32_t )( t >> 32 ); 102 S->t[1] = ( uint32_t )( t >> 32 );
103 return 0; 103 return 0;
104 } 104 }
105 105
106 106
107 /* Parameter-related functions */ 107 /* Parameter-related functions */
108 Py_LOCAL_INLINE(int) blake2s_param_set_digest_length( blake2s_param *P, const ui nt8_t digest_length ) 108 BLAKE2_LOCAL_INLINE(int) blake2s_param_set_digest_length( blake2s_param *P, cons t uint8_t digest_length )
109 { 109 {
110 P->digest_length = digest_length; 110 P->digest_length = digest_length;
111 return 0; 111 return 0;
112 } 112 }
113 113
114 Py_LOCAL_INLINE(int) blake2s_param_set_fanout( blake2s_param *P, const uint8_t f anout ) 114 BLAKE2_LOCAL_INLINE(int) blake2s_param_set_fanout( blake2s_param *P, const uint8 _t fanout )
115 { 115 {
116 P->fanout = fanout; 116 P->fanout = fanout;
117 return 0; 117 return 0;
118 } 118 }
119 119
120 Py_LOCAL_INLINE(int) blake2s_param_set_max_depth( blake2s_param *P, const uint8_ t depth ) 120 BLAKE2_LOCAL_INLINE(int) blake2s_param_set_max_depth( blake2s_param *P, const ui nt8_t depth )
121 { 121 {
122 P->depth = depth; 122 P->depth = depth;
123 return 0; 123 return 0;
124 } 124 }
125 125
126 Py_LOCAL_INLINE(int) blake2s_param_set_leaf_length( blake2s_param *P, const uint 32_t leaf_length ) 126 BLAKE2_LOCAL_INLINE(int) blake2s_param_set_leaf_length( blake2s_param *P, const uint32_t leaf_length )
127 { 127 {
128 P->leaf_length = leaf_length; 128 P->leaf_length = leaf_length;
129 return 0; 129 return 0;
130 } 130 }
131 131
132 Py_LOCAL_INLINE(int) blake2s_param_set_node_offset( blake2s_param *P, const uint 64_t node_offset ) 132 BLAKE2_LOCAL_INLINE(int) blake2s_param_set_node_offset( blake2s_param *P, const uint64_t node_offset )
133 { 133 {
134 store48( P->node_offset, node_offset ); 134 store48( P->node_offset, node_offset );
135 return 0; 135 return 0;
136 } 136 }
137 137
138 Py_LOCAL_INLINE(int) blake2s_param_set_node_depth( blake2s_param *P, const uint8 _t node_depth ) 138 BLAKE2_LOCAL_INLINE(int) blake2s_param_set_node_depth( blake2s_param *P, const u int8_t node_depth )
139 { 139 {
140 P->node_depth = node_depth; 140 P->node_depth = node_depth;
141 return 0; 141 return 0;
142 } 142 }
143 143
144 Py_LOCAL_INLINE(int) blake2s_param_set_inner_length( blake2s_param *P, const uin t8_t inner_length ) 144 BLAKE2_LOCAL_INLINE(int) blake2s_param_set_inner_length( blake2s_param *P, const uint8_t inner_length )
145 { 145 {
146 P->inner_length = inner_length; 146 P->inner_length = inner_length;
147 return 0; 147 return 0;
148 } 148 }
149 149
150 Py_LOCAL_INLINE(int) blake2s_param_set_salt( blake2s_param *P, const uint8_t sal t[BLAKE2S_SALTBYTES] ) 150 BLAKE2_LOCAL_INLINE(int) blake2s_param_set_salt( blake2s_param *P, const uint8_t salt[BLAKE2S_SALTBYTES] )
151 { 151 {
152 memcpy( P->salt, salt, BLAKE2S_SALTBYTES ); 152 memcpy( P->salt, salt, BLAKE2S_SALTBYTES );
153 return 0; 153 return 0;
154 } 154 }
155 155
156 Py_LOCAL_INLINE(int) blake2s_param_set_personal( blake2s_param *P, const uint8_t personal[BLAKE2S_PERSONALBYTES] ) 156 BLAKE2_LOCAL_INLINE(int) blake2s_param_set_personal( blake2s_param *P, const uin t8_t personal[BLAKE2S_PERSONALBYTES] )
157 { 157 {
158 memcpy( P->personal, personal, BLAKE2S_PERSONALBYTES ); 158 memcpy( P->personal, personal, BLAKE2S_PERSONALBYTES );
159 return 0; 159 return 0;
160 } 160 }
161 161
162 Py_LOCAL_INLINE(int) blake2s_init0( blake2s_state *S ) 162 BLAKE2_LOCAL_INLINE(int) blake2s_init0( blake2s_state *S )
163 { 163 {
164 memset( S, 0, sizeof( blake2s_state ) ); 164 memset( S, 0, sizeof( blake2s_state ) );
165 165
166 for( int i = 0; i < 8; ++i ) S->h[i] = blake2s_IV[i]; 166 for( int i = 0; i < 8; ++i ) S->h[i] = blake2s_IV[i];
167 167
168 return 0; 168 return 0;
169 } 169 }
170 170
171 /* init2 xors IV with input parameter block */ 171 /* init2 xors IV with input parameter block */
172 int blake2s_init_param( blake2s_state *S, const blake2s_param *P ) 172 int blake2s_init_param( blake2s_state *S, const blake2s_param *P )
(...skipping 22 matching lines...) Expand all
195 1, 195 1,
196 0, 196 0,
197 {0}, 197 {0},
198 0, 198 0,
199 0, 199 0,
200 {0}, 200 {0},
201 {0} 201 {0}
202 }; 202 };
203 /* Move interval verification here? */ 203 /* Move interval verification here? */
204 if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1; 204 if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1;
205
206 return blake2s_init_param( S, &P ); 205 return blake2s_init_param( S, &P );
207 } 206 }
208 207
209 208
210 int blake2s_init_key( blake2s_state *S, const uint8_t outlen, const void *key, c onst uint8_t keylen ) 209 int blake2s_init_key( blake2s_state *S, const uint8_t outlen, const void *key, c onst uint8_t keylen )
211 { 210 {
212 const blake2s_param P = 211 const blake2s_param P =
213 { 212 {
214 outlen, 213 outlen,
215 keylen, 214 keylen,
216 1, 215 1,
217 1, 216 1,
218 0, 217 0,
219 {0}, 218 {0},
220 0, 219 0,
221 0, 220 0,
222 {0}, 221 {0},
223 {0} 222 {0}
224 }; 223 };
224
225 /* Move interval verification here? */ 225 /* Move interval verification here? */
226 if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1; 226 if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1;
227 227
228 if ( ( !key ) || ( !keylen ) || keylen > BLAKE2S_KEYBYTES ) return -1; 228 if ( ( !key ) || ( !keylen ) || keylen > BLAKE2S_KEYBYTES ) return -1;
229 229
230 if( blake2s_init_param( S, &P ) < 0 ) 230 if( blake2s_init_param( S, &P ) < 0 )
231 return -1; 231 return -1;
232 232
233 { 233 {
234 uint8_t block[BLAKE2S_BLOCKBYTES]; 234 uint8_t block[BLAKE2S_BLOCKBYTES];
235 memset( block, 0, BLAKE2S_BLOCKBYTES ); 235 memset( block, 0, BLAKE2S_BLOCKBYTES );
236 memcpy( block, key, keylen ); 236 memcpy( block, key, keylen );
237 blake2s_update( S, block, BLAKE2S_BLOCKBYTES ); 237 blake2s_update( S, block, BLAKE2S_BLOCKBYTES );
238 secure_zero_memory( block, BLAKE2S_BLOCKBYTES ); /* Burn the key from stack */ 238 secure_zero_memory( block, BLAKE2S_BLOCKBYTES ); /* Burn the key from stack */
239 } 239 }
240 return 0; 240 return 0;
241 } 241 }
242 242
243 243
244 Py_LOCAL_INLINE(int) blake2s_compress( blake2s_state *S, const uint8_t block[BLA KE2S_BLOCKBYTES] ) 244 BLAKE2_LOCAL_INLINE(int) blake2s_compress( blake2s_state *S, const uint8_t block [BLAKE2S_BLOCKBYTES] )
245 { 245 {
246 __m128i row1, row2, row3, row4; 246 __m128i row1, row2, row3, row4;
247 __m128i buf1, buf2, buf3, buf4; 247 __m128i buf1, buf2, buf3, buf4;
248 #if defined(HAVE_SSE41) 248 #if defined(HAVE_SSE41)
249 __m128i t0, t1; 249 __m128i t0, t1;
250 #if !defined(HAVE_XOP) 250 #if !defined(HAVE_XOP)
251 __m128i t2; 251 __m128i t2;
252 #endif 252 #endif
253 #endif 253 #endif
254 __m128i ff0, ff1; 254 __m128i ff0, ff1;
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 return -1; 422 return -1;
423 } 423 }
424 } 424 }
425 425
426 puts( "ok" ); 426 puts( "ok" );
427 return 0; 427 return 0;
428 } 428 }
429 #endif 429 #endif
430 430
431 431
LEFTRIGHT

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