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

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

Issue 26798: add BLAKE2 to hashlib
Left Patch Set: Created 3 years, 3 months ago
Right Patch Set: Created 2 years, 11 months 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/clinic/blake2s_impl.c.h ('k') | Modules/_blake2/impl/blake2b-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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } , 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 } , 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 } , 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 } , 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 } , 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 } 65 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
66 }; 66 };
67 67
68 68
69 /* Some helper functions, not necessarily useful */ 69 /* Some helper functions, not necessarily useful */
70 Py_LOCAL_INLINE(int) blake2b_set_lastnode( blake2b_state *S ) 70 BLAKE2_LOCAL_INLINE(int) blake2b_set_lastnode( blake2b_state *S )
71 { 71 {
72 S->f[1] = -1; 72 S->f[1] = -1;
73 return 0; 73 return 0;
74 } 74 }
75 75
76 Py_LOCAL_INLINE(int) blake2b_clear_lastnode( blake2b_state *S ) 76 BLAKE2_LOCAL_INLINE(int) blake2b_clear_lastnode( blake2b_state *S )
77 { 77 {
78 S->f[1] = 0; 78 S->f[1] = 0;
79 return 0; 79 return 0;
80 } 80 }
81 81
82 Py_LOCAL_INLINE(int) blake2b_is_lastblock( const blake2b_state *S ) 82 BLAKE2_LOCAL_INLINE(int) blake2b_is_lastblock( const blake2b_state *S )
83 { 83 {
84 return S->f[0] != 0; 84 return S->f[0] != 0;
85 } 85 }
86 86
87 Py_LOCAL_INLINE(int) blake2b_set_lastblock( blake2b_state *S ) 87 BLAKE2_LOCAL_INLINE(int) blake2b_set_lastblock( blake2b_state *S )
88 { 88 {
89 if( S->last_node ) blake2b_set_lastnode( S ); 89 if( S->last_node ) blake2b_set_lastnode( S );
90 90
91 S->f[0] = -1; 91 S->f[0] = -1;
92 return 0; 92 return 0;
93 } 93 }
94 94
95 Py_LOCAL_INLINE(int) blake2b_clear_lastblock( blake2b_state *S ) 95 BLAKE2_LOCAL_INLINE(int) blake2b_clear_lastblock( blake2b_state *S )
96 { 96 {
97 if( S->last_node ) blake2b_clear_lastnode( S ); 97 if( S->last_node ) blake2b_clear_lastnode( S );
98 98
99 S->f[0] = 0; 99 S->f[0] = 0;
100 return 0; 100 return 0;
101 } 101 }
102 102
103 103
104 Py_LOCAL_INLINE(int) blake2b_increment_counter( blake2b_state *S, const uint64_t inc ) 104 BLAKE2_LOCAL_INLINE(int) blake2b_increment_counter( blake2b_state *S, const uint 64_t inc )
105 { 105 {
106 #if __x86_64__ 106 #if __x86_64__
107 /* ADD/ADC chain */ 107 /* ADD/ADC chain */
108 __uint128_t t = ( ( __uint128_t )S->t[1] << 64 ) | S->t[0]; 108 __uint128_t t = ( ( __uint128_t )S->t[1] << 64 ) | S->t[0];
109 t += inc; 109 t += inc;
110 S->t[0] = ( uint64_t )( t >> 0 ); 110 S->t[0] = ( uint64_t )( t >> 0 );
111 S->t[1] = ( uint64_t )( t >> 64 ); 111 S->t[1] = ( uint64_t )( t >> 64 );
112 #else 112 #else
113 S->t[0] += inc; 113 S->t[0] += inc;
114 S->t[1] += ( S->t[0] < inc ); 114 S->t[1] += ( S->t[0] < inc );
115 #endif 115 #endif
116 return 0; 116 return 0;
117 } 117 }
118 118
119 119
120 /* Parameter-related functions */ 120 /* Parameter-related functions */
121 Py_LOCAL_INLINE(int) blake2b_param_set_digest_length( blake2b_param *P, const ui nt8_t digest_length ) 121 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_digest_length( blake2b_param *P, cons t uint8_t digest_length )
122 { 122 {
123 P->digest_length = digest_length; 123 P->digest_length = digest_length;
124 return 0; 124 return 0;
125 } 125 }
126 126
127 Py_LOCAL_INLINE(int) blake2b_param_set_fanout( blake2b_param *P, const uint8_t f anout ) 127 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_fanout( blake2b_param *P, const uint8 _t fanout )
128 { 128 {
129 P->fanout = fanout; 129 P->fanout = fanout;
130 return 0; 130 return 0;
131 } 131 }
132 132
133 Py_LOCAL_INLINE(int) blake2b_param_set_max_depth( blake2b_param *P, const uint8_ t depth ) 133 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_max_depth( blake2b_param *P, const ui nt8_t depth )
134 { 134 {
135 P->depth = depth; 135 P->depth = depth;
136 return 0; 136 return 0;
137 } 137 }
138 138
139 Py_LOCAL_INLINE(int) blake2b_param_set_leaf_length( blake2b_param *P, const uint 32_t leaf_length ) 139 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_leaf_length( blake2b_param *P, const uint32_t leaf_length )
140 { 140 {
141 P->leaf_length = leaf_length; 141 P->leaf_length = leaf_length;
142 return 0; 142 return 0;
143 } 143 }
144 144
145 Py_LOCAL_INLINE(int) blake2b_param_set_node_offset( blake2b_param *P, const uint 64_t node_offset ) 145 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_node_offset( blake2b_param *P, const uint64_t node_offset )
146 { 146 {
147 P->node_offset = node_offset; 147 P->node_offset = node_offset;
148 return 0; 148 return 0;
149 } 149 }
150 150
151 Py_LOCAL_INLINE(int) blake2b_param_set_node_depth( blake2b_param *P, const uint8 _t node_depth ) 151 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_node_depth( blake2b_param *P, const u int8_t node_depth )
152 { 152 {
153 P->node_depth = node_depth; 153 P->node_depth = node_depth;
154 return 0; 154 return 0;
155 } 155 }
156 156
157 Py_LOCAL_INLINE(int) blake2b_param_set_inner_length( blake2b_param *P, const uin t8_t inner_length ) 157 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_inner_length( blake2b_param *P, const uint8_t inner_length )
158 { 158 {
159 P->inner_length = inner_length; 159 P->inner_length = inner_length;
160 return 0; 160 return 0;
161 } 161 }
162 162
163 Py_LOCAL_INLINE(int) blake2b_param_set_salt( blake2b_param *P, const uint8_t sal t[BLAKE2B_SALTBYTES] ) 163 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_salt( blake2b_param *P, const uint8_t salt[BLAKE2B_SALTBYTES] )
164 { 164 {
165 memcpy( P->salt, salt, BLAKE2B_SALTBYTES ); 165 memcpy( P->salt, salt, BLAKE2B_SALTBYTES );
166 return 0; 166 return 0;
167 } 167 }
168 168
169 Py_LOCAL_INLINE(int) blake2b_param_set_personal( blake2b_param *P, const uint8_t personal[BLAKE2B_PERSONALBYTES] ) 169 BLAKE2_LOCAL_INLINE(int) blake2b_param_set_personal( blake2b_param *P, const uin t8_t personal[BLAKE2B_PERSONALBYTES] )
170 { 170 {
171 memcpy( P->personal, personal, BLAKE2B_PERSONALBYTES ); 171 memcpy( P->personal, personal, BLAKE2B_PERSONALBYTES );
172 return 0; 172 return 0;
173 } 173 }
174 174
175 Py_LOCAL_INLINE(int) blake2b_init0( blake2b_state *S ) 175 BLAKE2_LOCAL_INLINE(int) blake2b_init0( blake2b_state *S )
176 { 176 {
177 memset( S, 0, sizeof( blake2b_state ) ); 177 memset( S, 0, sizeof( blake2b_state ) );
178 178
179 for( int i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i]; 179 for( int i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i];
180 180
181 return 0; 181 return 0;
182 } 182 }
183 183
184 /* init xors IV with input parameter block */ 184 /* init xors IV with input parameter block */
185 int blake2b_init_param( blake2b_state *S, const blake2b_param *P ) 185 int blake2b_init_param( blake2b_state *S, const blake2b_param *P )
(...skipping 21 matching lines...) Expand all
207 1, 207 1,
208 1, 208 1,
209 0, 209 0,
210 0, 210 0,
211 0, 211 0,
212 0, 212 0,
213 {0}, 213 {0},
214 {0}, 214 {0},
215 {0} 215 {0}
216 }; 216 };
217
217 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1; 218 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
218 219
219 return blake2b_init_param( S, &P ); 220 return blake2b_init_param( S, &P );
220 } 221 }
221 222
222 int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, c onst uint8_t keylen ) 223 int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, c onst uint8_t keylen )
223 { 224 {
224 const blake2b_param P = 225 const blake2b_param P =
225 { 226 {
226 outlen, 227 outlen,
(...skipping 19 matching lines...) Expand all
246 { 247 {
247 uint8_t block[BLAKE2B_BLOCKBYTES]; 248 uint8_t block[BLAKE2B_BLOCKBYTES];
248 memset( block, 0, BLAKE2B_BLOCKBYTES ); 249 memset( block, 0, BLAKE2B_BLOCKBYTES );
249 memcpy( block, key, keylen ); 250 memcpy( block, key, keylen );
250 blake2b_update( S, block, BLAKE2B_BLOCKBYTES ); 251 blake2b_update( S, block, BLAKE2B_BLOCKBYTES );
251 secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */ 252 secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */
252 } 253 }
253 return 0; 254 return 0;
254 } 255 }
255 256
256 Py_LOCAL_INLINE(int) blake2b_compress( blake2b_state *S, const uint8_t block[BLA KE2B_BLOCKBYTES] ) 257 BLAKE2_LOCAL_INLINE(int) blake2b_compress( blake2b_state *S, const uint8_t block [BLAKE2B_BLOCKBYTES] )
257 { 258 {
258 __m128i row1l, row1h; 259 __m128i row1l, row1h;
259 __m128i row2l, row2h; 260 __m128i row2l, row2h;
260 __m128i row3l, row3h; 261 __m128i row3l, row3h;
261 __m128i row4l, row4h; 262 __m128i row4l, row4h;
262 __m128i b0, b1; 263 __m128i b0, b1;
263 __m128i t0, t1; 264 __m128i t0, t1;
264 #if defined(HAVE_SSSE3) && !defined(HAVE_XOP) 265 #if defined(HAVE_SSSE3) && !defined(HAVE_XOP)
265 const __m128i r16 = _mm_setr_epi8( 2, 3, 4, 5, 6, 7, 0, 1, 10, 11, 12, 13, 14, 15, 8, 9 ); 266 const __m128i r16 = _mm_setr_epi8( 2, 3, 4, 5, 6, 7, 0, 1, 10, 11, 12, 13, 14, 15, 8, 9 );
266 const __m128i r24 = _mm_setr_epi8( 3, 4, 5, 6, 7, 0, 1, 2, 11, 12, 13, 14, 15, 8, 9, 10 ); 267 const __m128i r24 = _mm_setr_epi8( 3, 4, 5, 6, 7, 0, 1, 2, 11, 12, 13, 14, 15, 8, 9, 10 );
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 puts( "error" ); 441 puts( "error" );
441 return -1; 442 return -1;
442 } 443 }
443 } 444 }
444 445
445 puts( "ok" ); 446 puts( "ok" );
446 return 0; 447 return 0;
447 } 448 }
448 #endif 449 #endif
449 450
LEFTRIGHT

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