1 | n/a | /* |
---|
2 | n/a | BLAKE2 reference source code package - optimized C implementations |
---|
3 | n/a | |
---|
4 | n/a | Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the |
---|
5 | n/a | terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at |
---|
6 | n/a | your option. The terms of these licenses can be found at: |
---|
7 | n/a | |
---|
8 | n/a | - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0 |
---|
9 | n/a | - OpenSSL license : https://www.openssl.org/source/license.html |
---|
10 | n/a | - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | n/a | |
---|
12 | n/a | More information about the BLAKE2 hash function can be found at |
---|
13 | n/a | https://blake2.net. |
---|
14 | n/a | */ |
---|
15 | n/a | |
---|
16 | n/a | #include <stdint.h> |
---|
17 | n/a | #include <string.h> |
---|
18 | n/a | #include <stdio.h> |
---|
19 | n/a | |
---|
20 | n/a | #include "blake2.h" |
---|
21 | n/a | #include "blake2-impl.h" |
---|
22 | n/a | |
---|
23 | n/a | #include "blake2-config.h" |
---|
24 | n/a | |
---|
25 | n/a | #ifdef _MSC_VER |
---|
26 | n/a | #include <intrin.h> /* for _mm_set_epi64x */ |
---|
27 | n/a | #endif |
---|
28 | n/a | #include <emmintrin.h> |
---|
29 | n/a | #if defined(HAVE_SSSE3) |
---|
30 | n/a | #include <tmmintrin.h> |
---|
31 | n/a | #endif |
---|
32 | n/a | #if defined(HAVE_SSE41) |
---|
33 | n/a | #include <smmintrin.h> |
---|
34 | n/a | #endif |
---|
35 | n/a | #if defined(HAVE_AVX) |
---|
36 | n/a | #include <immintrin.h> |
---|
37 | n/a | #endif |
---|
38 | n/a | #if defined(HAVE_XOP) |
---|
39 | n/a | #include <x86intrin.h> |
---|
40 | n/a | #endif |
---|
41 | n/a | |
---|
42 | n/a | #include "blake2b-round.h" |
---|
43 | n/a | |
---|
44 | n/a | static const uint64_t blake2b_IV[8] = |
---|
45 | n/a | { |
---|
46 | n/a | 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, |
---|
47 | n/a | 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, |
---|
48 | n/a | 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, |
---|
49 | n/a | 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL |
---|
50 | n/a | }; |
---|
51 | n/a | |
---|
52 | n/a | static const uint8_t blake2b_sigma[12][16] = |
---|
53 | n/a | { |
---|
54 | n/a | { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } , |
---|
55 | n/a | { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } , |
---|
56 | n/a | { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } , |
---|
57 | n/a | { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } , |
---|
58 | n/a | { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } , |
---|
59 | n/a | { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } , |
---|
60 | n/a | { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } , |
---|
61 | n/a | { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } , |
---|
62 | n/a | { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } , |
---|
63 | n/a | { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } , |
---|
64 | n/a | { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } , |
---|
65 | n/a | { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } |
---|
66 | n/a | }; |
---|
67 | n/a | |
---|
68 | n/a | |
---|
69 | n/a | /* Some helper functions, not necessarily useful */ |
---|
70 | n/a | BLAKE2_LOCAL_INLINE(int) blake2b_set_lastnode( blake2b_state *S ) |
---|
71 | n/a | { |
---|
72 | n/a | S->f[1] = -1; |
---|
73 | n/a | return 0; |
---|
74 | n/a | } |
---|
75 | n/a | |
---|
76 | n/a | BLAKE2_LOCAL_INLINE(int) blake2b_clear_lastnode( blake2b_state *S ) |
---|
77 | n/a | { |
---|
78 | n/a | S->f[1] = 0; |
---|
79 | n/a | return 0; |
---|
80 | n/a | } |
---|
81 | n/a | |
---|
82 | n/a | BLAKE2_LOCAL_INLINE(int) blake2b_is_lastblock( const blake2b_state *S ) |
---|
83 | n/a | { |
---|
84 | n/a | return S->f[0] != 0; |
---|
85 | n/a | } |
---|
86 | n/a | |
---|
87 | n/a | BLAKE2_LOCAL_INLINE(int) blake2b_set_lastblock( blake2b_state *S ) |
---|
88 | n/a | { |
---|
89 | n/a | if( S->last_node ) blake2b_set_lastnode( S ); |
---|
90 | n/a | |
---|
91 | n/a | S->f[0] = -1; |
---|
92 | n/a | return 0; |
---|
93 | n/a | } |
---|
94 | n/a | |
---|
95 | n/a | BLAKE2_LOCAL_INLINE(int) blake2b_clear_lastblock( blake2b_state *S ) |
---|
96 | n/a | { |
---|
97 | n/a | if( S->last_node ) blake2b_clear_lastnode( S ); |
---|
98 | n/a | |
---|
99 | n/a | S->f[0] = 0; |
---|
100 | n/a | return 0; |
---|
101 | n/a | } |
---|
102 | n/a | |
---|
103 | n/a | |
---|
104 | n/a | BLAKE2_LOCAL_INLINE(int) blake2b_increment_counter( blake2b_state *S, const uint64_t inc ) |
---|
105 | n/a | { |
---|
106 | n/a | #if __x86_64__ |
---|
107 | n/a | /* ADD/ADC chain */ |
---|
108 | n/a | __uint128_t t = ( ( __uint128_t )S->t[1] << 64 ) | S->t[0]; |
---|
109 | n/a | t += inc; |
---|
110 | n/a | S->t[0] = ( uint64_t )( t >> 0 ); |
---|
111 | n/a | S->t[1] = ( uint64_t )( t >> 64 ); |
---|
112 | n/a | #else |
---|
113 | n/a | S->t[0] += inc; |
---|
114 | n/a | S->t[1] += ( S->t[0] < inc ); |
---|
115 | n/a | #endif |
---|
116 | n/a | return 0; |
---|
117 | n/a | } |
---|
118 | n/a | |
---|
119 | n/a | |
---|
120 | n/a | /* Parameter-related functions */ |
---|
121 | n/a | BLAKE2_LOCAL_INLINE(int) blake2b_param_set_digest_length( blake2b_param *P, const uint8_t digest_length ) |
---|
122 | n/a | { |
---|
123 | n/a | P->digest_length = digest_length; |
---|
124 | n/a | return 0; |
---|
125 | n/a | } |
---|
126 | n/a | |
---|
127 | n/a | BLAKE2_LOCAL_INLINE(int) blake2b_param_set_fanout( blake2b_param *P, const uint8_t fanout ) |
---|
128 | n/a | { |
---|
129 | n/a | P->fanout = fanout; |
---|
130 | n/a | return 0; |
---|
131 | n/a | } |
---|
132 | n/a | |
---|
133 | n/a | BLAKE2_LOCAL_INLINE(int) blake2b_param_set_max_depth( blake2b_param *P, const uint8_t depth ) |
---|
134 | n/a | { |
---|
135 | n/a | P->depth = depth; |
---|
136 | n/a | return 0; |
---|
137 | n/a | } |
---|
138 | n/a | |
---|
139 | n/a | BLAKE2_LOCAL_INLINE(int) blake2b_param_set_leaf_length( blake2b_param *P, const uint32_t leaf_length ) |
---|
140 | n/a | { |
---|
141 | n/a | P->leaf_length = leaf_length; |
---|
142 | n/a | return 0; |
---|
143 | n/a | } |
---|
144 | n/a | |
---|
145 | n/a | BLAKE2_LOCAL_INLINE(int) blake2b_param_set_node_offset( blake2b_param *P, const uint64_t node_offset ) |
---|
146 | n/a | { |
---|
147 | n/a | P->node_offset = node_offset; |
---|
148 | n/a | return 0; |
---|
149 | n/a | } |
---|
150 | n/a | |
---|
151 | n/a | BLAKE2_LOCAL_INLINE(int) blake2b_param_set_node_depth( blake2b_param *P, const uint8_t node_depth ) |
---|
152 | n/a | { |
---|
153 | n/a | P->node_depth = node_depth; |
---|
154 | n/a | return 0; |
---|
155 | n/a | } |
---|
156 | n/a | |
---|
157 | n/a | BLAKE2_LOCAL_INLINE(int) blake2b_param_set_inner_length( blake2b_param *P, const uint8_t inner_length ) |
---|
158 | n/a | { |
---|
159 | n/a | P->inner_length = inner_length; |
---|
160 | n/a | return 0; |
---|
161 | n/a | } |
---|
162 | n/a | |
---|
163 | n/a | BLAKE2_LOCAL_INLINE(int) blake2b_param_set_salt( blake2b_param *P, const uint8_t salt[BLAKE2B_SALTBYTES] ) |
---|
164 | n/a | { |
---|
165 | n/a | memcpy( P->salt, salt, BLAKE2B_SALTBYTES ); |
---|
166 | n/a | return 0; |
---|
167 | n/a | } |
---|
168 | n/a | |
---|
169 | n/a | BLAKE2_LOCAL_INLINE(int) blake2b_param_set_personal( blake2b_param *P, const uint8_t personal[BLAKE2B_PERSONALBYTES] ) |
---|
170 | n/a | { |
---|
171 | n/a | memcpy( P->personal, personal, BLAKE2B_PERSONALBYTES ); |
---|
172 | n/a | return 0; |
---|
173 | n/a | } |
---|
174 | n/a | |
---|
175 | n/a | BLAKE2_LOCAL_INLINE(int) blake2b_init0( blake2b_state *S ) |
---|
176 | n/a | { |
---|
177 | n/a | int i; |
---|
178 | n/a | memset( S, 0, sizeof( blake2b_state ) ); |
---|
179 | n/a | |
---|
180 | n/a | for( i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i]; |
---|
181 | n/a | |
---|
182 | n/a | return 0; |
---|
183 | n/a | } |
---|
184 | n/a | |
---|
185 | n/a | /* init xors IV with input parameter block */ |
---|
186 | n/a | int blake2b_init_param( blake2b_state *S, const blake2b_param *P ) |
---|
187 | n/a | { |
---|
188 | n/a | /*blake2b_init0( S ); */ |
---|
189 | n/a | const uint8_t * v = ( const uint8_t * )( blake2b_IV ); |
---|
190 | n/a | const uint8_t * p = ( const uint8_t * )( P ); |
---|
191 | n/a | uint8_t * h = ( uint8_t * )( S->h ); |
---|
192 | n/a | int i; |
---|
193 | n/a | /* IV XOR ParamBlock */ |
---|
194 | n/a | memset( S, 0, sizeof( blake2b_state ) ); |
---|
195 | n/a | |
---|
196 | n/a | for( i = 0; i < BLAKE2B_OUTBYTES; ++i ) h[i] = v[i] ^ p[i]; |
---|
197 | n/a | |
---|
198 | n/a | return 0; |
---|
199 | n/a | } |
---|
200 | n/a | |
---|
201 | n/a | |
---|
202 | n/a | /* Some sort of default parameter block initialization, for sequential blake2b */ |
---|
203 | n/a | int blake2b_init( blake2b_state *S, const uint8_t outlen ) |
---|
204 | n/a | { |
---|
205 | n/a | const blake2b_param P = |
---|
206 | n/a | { |
---|
207 | n/a | outlen, |
---|
208 | n/a | 0, |
---|
209 | n/a | 1, |
---|
210 | n/a | 1, |
---|
211 | n/a | 0, |
---|
212 | n/a | 0, |
---|
213 | n/a | 0, |
---|
214 | n/a | 0, |
---|
215 | n/a | {0}, |
---|
216 | n/a | {0}, |
---|
217 | n/a | {0} |
---|
218 | n/a | }; |
---|
219 | n/a | |
---|
220 | n/a | if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1; |
---|
221 | n/a | |
---|
222 | n/a | return blake2b_init_param( S, &P ); |
---|
223 | n/a | } |
---|
224 | n/a | |
---|
225 | n/a | int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen ) |
---|
226 | n/a | { |
---|
227 | n/a | const blake2b_param P = |
---|
228 | n/a | { |
---|
229 | n/a | outlen, |
---|
230 | n/a | keylen, |
---|
231 | n/a | 1, |
---|
232 | n/a | 1, |
---|
233 | n/a | 0, |
---|
234 | n/a | 0, |
---|
235 | n/a | 0, |
---|
236 | n/a | 0, |
---|
237 | n/a | {0}, |
---|
238 | n/a | {0}, |
---|
239 | n/a | {0} |
---|
240 | n/a | }; |
---|
241 | n/a | |
---|
242 | n/a | if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1; |
---|
243 | n/a | |
---|
244 | n/a | if ( ( !keylen ) || keylen > BLAKE2B_KEYBYTES ) return -1; |
---|
245 | n/a | |
---|
246 | n/a | if( blake2b_init_param( S, &P ) < 0 ) |
---|
247 | n/a | return 0; |
---|
248 | n/a | |
---|
249 | n/a | { |
---|
250 | n/a | uint8_t block[BLAKE2B_BLOCKBYTES]; |
---|
251 | n/a | memset( block, 0, BLAKE2B_BLOCKBYTES ); |
---|
252 | n/a | memcpy( block, key, keylen ); |
---|
253 | n/a | blake2b_update( S, block, BLAKE2B_BLOCKBYTES ); |
---|
254 | n/a | secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */ |
---|
255 | n/a | } |
---|
256 | n/a | return 0; |
---|
257 | n/a | } |
---|
258 | n/a | |
---|
259 | n/a | BLAKE2_LOCAL_INLINE(int) blake2b_compress( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] ) |
---|
260 | n/a | { |
---|
261 | n/a | __m128i row1l, row1h; |
---|
262 | n/a | __m128i row2l, row2h; |
---|
263 | n/a | __m128i row3l, row3h; |
---|
264 | n/a | __m128i row4l, row4h; |
---|
265 | n/a | __m128i b0, b1; |
---|
266 | n/a | __m128i t0, t1; |
---|
267 | n/a | #if defined(HAVE_SSSE3) && !defined(HAVE_XOP) |
---|
268 | n/a | const __m128i r16 = _mm_setr_epi8( 2, 3, 4, 5, 6, 7, 0, 1, 10, 11, 12, 13, 14, 15, 8, 9 ); |
---|
269 | n/a | const __m128i r24 = _mm_setr_epi8( 3, 4, 5, 6, 7, 0, 1, 2, 11, 12, 13, 14, 15, 8, 9, 10 ); |
---|
270 | n/a | #endif |
---|
271 | n/a | #if defined(HAVE_SSE41) |
---|
272 | n/a | const __m128i m0 = LOADU( block + 00 ); |
---|
273 | n/a | const __m128i m1 = LOADU( block + 16 ); |
---|
274 | n/a | const __m128i m2 = LOADU( block + 32 ); |
---|
275 | n/a | const __m128i m3 = LOADU( block + 48 ); |
---|
276 | n/a | const __m128i m4 = LOADU( block + 64 ); |
---|
277 | n/a | const __m128i m5 = LOADU( block + 80 ); |
---|
278 | n/a | const __m128i m6 = LOADU( block + 96 ); |
---|
279 | n/a | const __m128i m7 = LOADU( block + 112 ); |
---|
280 | n/a | #else |
---|
281 | n/a | const uint64_t m0 = ( ( uint64_t * )block )[ 0]; |
---|
282 | n/a | const uint64_t m1 = ( ( uint64_t * )block )[ 1]; |
---|
283 | n/a | const uint64_t m2 = ( ( uint64_t * )block )[ 2]; |
---|
284 | n/a | const uint64_t m3 = ( ( uint64_t * )block )[ 3]; |
---|
285 | n/a | const uint64_t m4 = ( ( uint64_t * )block )[ 4]; |
---|
286 | n/a | const uint64_t m5 = ( ( uint64_t * )block )[ 5]; |
---|
287 | n/a | const uint64_t m6 = ( ( uint64_t * )block )[ 6]; |
---|
288 | n/a | const uint64_t m7 = ( ( uint64_t * )block )[ 7]; |
---|
289 | n/a | const uint64_t m8 = ( ( uint64_t * )block )[ 8]; |
---|
290 | n/a | const uint64_t m9 = ( ( uint64_t * )block )[ 9]; |
---|
291 | n/a | const uint64_t m10 = ( ( uint64_t * )block )[10]; |
---|
292 | n/a | const uint64_t m11 = ( ( uint64_t * )block )[11]; |
---|
293 | n/a | const uint64_t m12 = ( ( uint64_t * )block )[12]; |
---|
294 | n/a | const uint64_t m13 = ( ( uint64_t * )block )[13]; |
---|
295 | n/a | const uint64_t m14 = ( ( uint64_t * )block )[14]; |
---|
296 | n/a | const uint64_t m15 = ( ( uint64_t * )block )[15]; |
---|
297 | n/a | #endif |
---|
298 | n/a | row1l = LOADU( &S->h[0] ); |
---|
299 | n/a | row1h = LOADU( &S->h[2] ); |
---|
300 | n/a | row2l = LOADU( &S->h[4] ); |
---|
301 | n/a | row2h = LOADU( &S->h[6] ); |
---|
302 | n/a | row3l = LOADU( &blake2b_IV[0] ); |
---|
303 | n/a | row3h = LOADU( &blake2b_IV[2] ); |
---|
304 | n/a | row4l = _mm_xor_si128( LOADU( &blake2b_IV[4] ), LOADU( &S->t[0] ) ); |
---|
305 | n/a | row4h = _mm_xor_si128( LOADU( &blake2b_IV[6] ), LOADU( &S->f[0] ) ); |
---|
306 | n/a | ROUND( 0 ); |
---|
307 | n/a | ROUND( 1 ); |
---|
308 | n/a | ROUND( 2 ); |
---|
309 | n/a | ROUND( 3 ); |
---|
310 | n/a | ROUND( 4 ); |
---|
311 | n/a | ROUND( 5 ); |
---|
312 | n/a | ROUND( 6 ); |
---|
313 | n/a | ROUND( 7 ); |
---|
314 | n/a | ROUND( 8 ); |
---|
315 | n/a | ROUND( 9 ); |
---|
316 | n/a | ROUND( 10 ); |
---|
317 | n/a | ROUND( 11 ); |
---|
318 | n/a | row1l = _mm_xor_si128( row3l, row1l ); |
---|
319 | n/a | row1h = _mm_xor_si128( row3h, row1h ); |
---|
320 | n/a | STOREU( &S->h[0], _mm_xor_si128( LOADU( &S->h[0] ), row1l ) ); |
---|
321 | n/a | STOREU( &S->h[2], _mm_xor_si128( LOADU( &S->h[2] ), row1h ) ); |
---|
322 | n/a | row2l = _mm_xor_si128( row4l, row2l ); |
---|
323 | n/a | row2h = _mm_xor_si128( row4h, row2h ); |
---|
324 | n/a | STOREU( &S->h[4], _mm_xor_si128( LOADU( &S->h[4] ), row2l ) ); |
---|
325 | n/a | STOREU( &S->h[6], _mm_xor_si128( LOADU( &S->h[6] ), row2h ) ); |
---|
326 | n/a | return 0; |
---|
327 | n/a | } |
---|
328 | n/a | |
---|
329 | n/a | |
---|
330 | n/a | int blake2b_update( blake2b_state *S, const uint8_t *in, uint64_t inlen ) |
---|
331 | n/a | { |
---|
332 | n/a | while( inlen > 0 ) |
---|
333 | n/a | { |
---|
334 | n/a | size_t left = S->buflen; |
---|
335 | n/a | size_t fill = 2 * BLAKE2B_BLOCKBYTES - left; |
---|
336 | n/a | |
---|
337 | n/a | if( inlen > fill ) |
---|
338 | n/a | { |
---|
339 | n/a | memcpy( S->buf + left, in, fill ); /* Fill buffer */ |
---|
340 | n/a | S->buflen += fill; |
---|
341 | n/a | blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); |
---|
342 | n/a | blake2b_compress( S, S->buf ); /* Compress */ |
---|
343 | n/a | memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */ |
---|
344 | n/a | S->buflen -= BLAKE2B_BLOCKBYTES; |
---|
345 | n/a | in += fill; |
---|
346 | n/a | inlen -= fill; |
---|
347 | n/a | } |
---|
348 | n/a | else /* inlen <= fill */ |
---|
349 | n/a | { |
---|
350 | n/a | memcpy( S->buf + left, in, inlen ); |
---|
351 | n/a | S->buflen += inlen; /* Be lazy, do not compress */ |
---|
352 | n/a | in += inlen; |
---|
353 | n/a | inlen -= inlen; |
---|
354 | n/a | } |
---|
355 | n/a | } |
---|
356 | n/a | |
---|
357 | n/a | return 0; |
---|
358 | n/a | } |
---|
359 | n/a | |
---|
360 | n/a | |
---|
361 | n/a | int blake2b_final( blake2b_state *S, uint8_t *out, uint8_t outlen ) |
---|
362 | n/a | { |
---|
363 | n/a | if( outlen > BLAKE2B_OUTBYTES ) |
---|
364 | n/a | return -1; |
---|
365 | n/a | |
---|
366 | n/a | if( blake2b_is_lastblock( S ) ) |
---|
367 | n/a | return -1; |
---|
368 | n/a | |
---|
369 | n/a | if( S->buflen > BLAKE2B_BLOCKBYTES ) |
---|
370 | n/a | { |
---|
371 | n/a | blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); |
---|
372 | n/a | blake2b_compress( S, S->buf ); |
---|
373 | n/a | S->buflen -= BLAKE2B_BLOCKBYTES; |
---|
374 | n/a | memmove( S->buf, S->buf + BLAKE2B_BLOCKBYTES, S->buflen ); |
---|
375 | n/a | } |
---|
376 | n/a | |
---|
377 | n/a | blake2b_increment_counter( S, S->buflen ); |
---|
378 | n/a | blake2b_set_lastblock( S ); |
---|
379 | n/a | memset( S->buf + S->buflen, 0, 2 * BLAKE2B_BLOCKBYTES - S->buflen ); /* Padding */ |
---|
380 | n/a | blake2b_compress( S, S->buf ); |
---|
381 | n/a | memcpy( out, &S->h[0], outlen ); |
---|
382 | n/a | return 0; |
---|
383 | n/a | } |
---|
384 | n/a | |
---|
385 | n/a | |
---|
386 | n/a | int blake2b( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen ) |
---|
387 | n/a | { |
---|
388 | n/a | blake2b_state S[1]; |
---|
389 | n/a | |
---|
390 | n/a | /* Verify parameters */ |
---|
391 | n/a | if ( NULL == in && inlen > 0 ) return -1; |
---|
392 | n/a | |
---|
393 | n/a | if ( NULL == out ) return -1; |
---|
394 | n/a | |
---|
395 | n/a | if( NULL == key && keylen > 0 ) return -1; |
---|
396 | n/a | |
---|
397 | n/a | if( !outlen || outlen > BLAKE2B_OUTBYTES ) return -1; |
---|
398 | n/a | |
---|
399 | n/a | if( keylen > BLAKE2B_KEYBYTES ) return -1; |
---|
400 | n/a | |
---|
401 | n/a | if( keylen ) |
---|
402 | n/a | { |
---|
403 | n/a | if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1; |
---|
404 | n/a | } |
---|
405 | n/a | else |
---|
406 | n/a | { |
---|
407 | n/a | if( blake2b_init( S, outlen ) < 0 ) return -1; |
---|
408 | n/a | } |
---|
409 | n/a | |
---|
410 | n/a | blake2b_update( S, ( const uint8_t * )in, inlen ); |
---|
411 | n/a | blake2b_final( S, out, outlen ); |
---|
412 | n/a | return 0; |
---|
413 | n/a | } |
---|
414 | n/a | |
---|
415 | n/a | #if defined(SUPERCOP) |
---|
416 | n/a | int crypto_hash( unsigned char *out, unsigned char *in, unsigned long long inlen ) |
---|
417 | n/a | { |
---|
418 | n/a | return blake2b( out, in, NULL, BLAKE2B_OUTBYTES, inlen, 0 ); |
---|
419 | n/a | } |
---|
420 | n/a | #endif |
---|
421 | n/a | |
---|
422 | n/a | #if defined(BLAKE2B_SELFTEST) |
---|
423 | n/a | #include <string.h> |
---|
424 | n/a | #include "blake2-kat.h" |
---|
425 | n/a | int main( int argc, char **argv ) |
---|
426 | n/a | { |
---|
427 | n/a | uint8_t key[BLAKE2B_KEYBYTES]; |
---|
428 | n/a | uint8_t buf[KAT_LENGTH]; |
---|
429 | n/a | size_t i; |
---|
430 | n/a | |
---|
431 | n/a | for( i = 0; i < BLAKE2B_KEYBYTES; ++i ) |
---|
432 | n/a | key[i] = ( uint8_t )i; |
---|
433 | n/a | |
---|
434 | n/a | for( i = 0; i < KAT_LENGTH; ++i ) |
---|
435 | n/a | buf[i] = ( uint8_t )i; |
---|
436 | n/a | |
---|
437 | n/a | for( i = 0; i < KAT_LENGTH; ++i ) |
---|
438 | n/a | { |
---|
439 | n/a | uint8_t hash[BLAKE2B_OUTBYTES]; |
---|
440 | n/a | blake2b( hash, buf, key, BLAKE2B_OUTBYTES, i, BLAKE2B_KEYBYTES ); |
---|
441 | n/a | |
---|
442 | n/a | if( 0 != memcmp( hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES ) ) |
---|
443 | n/a | { |
---|
444 | n/a | puts( "error" ); |
---|
445 | n/a | return -1; |
---|
446 | n/a | } |
---|
447 | n/a | } |
---|
448 | n/a | |
---|
449 | n/a | puts( "ok" ); |
---|
450 | n/a | return 0; |
---|
451 | n/a | } |
---|
452 | n/a | #endif |
---|
453 | n/a | |
---|