/src/wolfssl-sp-math-all-8bit/wolfcrypt/src/blake2b.c
Line | Count | Source |
1 | | /* |
2 | | BLAKE2 reference source code package - reference C implementations |
3 | | |
4 | | Written in 2012 by Samuel Neves <sneves@dei.uc.pt> |
5 | | |
6 | | To the extent possible under law, the author(s) have dedicated all copyright |
7 | | and related and neighboring rights to this software to the public domain |
8 | | worldwide. This software is distributed without any warranty. |
9 | | |
10 | | You should have received a copy of the CC0 Public Domain Dedication along with |
11 | | this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. |
12 | | */ |
13 | | /* blake2b.c |
14 | | * |
15 | | * Copyright (C) 2006-2026 wolfSSL Inc. |
16 | | * |
17 | | * This file is part of wolfSSL. |
18 | | * |
19 | | * wolfSSL is free software; you can redistribute it and/or modify |
20 | | * it under the terms of the GNU General Public License as published by |
21 | | * the Free Software Foundation; either version 3 of the License, or |
22 | | * (at your option) any later version. |
23 | | * |
24 | | * wolfSSL is distributed in the hope that it will be useful, |
25 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
26 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
27 | | * GNU General Public License for more details. |
28 | | * |
29 | | * You should have received a copy of the GNU General Public License |
30 | | * along with this program; if not, write to the Free Software |
31 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
32 | | */ |
33 | | |
34 | | #include <wolfssl/wolfcrypt/libwolfssl_sources.h> |
35 | | |
36 | | #ifdef HAVE_BLAKE2B |
37 | | |
38 | | #include <wolfssl/wolfcrypt/blake2.h> |
39 | | #include <wolfssl/wolfcrypt/blake2-impl.h> |
40 | | #ifdef NO_INLINE |
41 | | #include <wolfssl/wolfcrypt/misc.h> |
42 | | #else |
43 | | #define WOLFSSL_MISC_INCLUDED |
44 | | #include <wolfcrypt/src/misc.c> |
45 | | #endif |
46 | | |
47 | | static const word64 blake2b_IV[8] = |
48 | | { |
49 | | W64LIT(0x6a09e667f3bcc908), W64LIT(0xbb67ae8584caa73b), |
50 | | W64LIT(0x3c6ef372fe94f82b), W64LIT(0xa54ff53a5f1d36f1), |
51 | | W64LIT(0x510e527fade682d1), W64LIT(0x9b05688c2b3e6c1f), |
52 | | W64LIT(0x1f83d9abfb41bd6b), W64LIT(0x5be0cd19137e2179) |
53 | | }; |
54 | | |
55 | | static const byte blake2b_sigma[12][16] = |
56 | | { |
57 | | { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } , |
58 | | { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } , |
59 | | { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } , |
60 | | { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } , |
61 | | { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } , |
62 | | { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } , |
63 | | { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } , |
64 | | { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } , |
65 | | { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } , |
66 | | { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } , |
67 | | { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } , |
68 | | { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } |
69 | | }; |
70 | | |
71 | | |
72 | | static WC_INLINE int blake2b_set_lastnode( blake2b_state *S ) |
73 | 0 | { |
74 | 0 | S->f[1] = ~W64LIT(0); |
75 | 0 | return 0; |
76 | 0 | } |
77 | | |
78 | | /* Some helper functions, not necessarily useful */ |
79 | | static WC_INLINE int blake2b_set_lastblock( blake2b_state *S ) |
80 | 0 | { |
81 | 0 | if( S->last_node ) blake2b_set_lastnode( S ); |
82 | |
|
83 | 0 | S->f[0] = ~W64LIT(0); |
84 | 0 | return 0; |
85 | 0 | } |
86 | | |
87 | | static WC_INLINE int blake2b_increment_counter( blake2b_state *S, const word64 |
88 | | inc ) |
89 | 0 | { |
90 | 0 | S->t[0] += inc; |
91 | 0 | S->t[1] += ( S->t[0] < inc ); |
92 | 0 | return 0; |
93 | 0 | } |
94 | | |
95 | | static WC_INLINE int blake2b_init0( blake2b_state *S ) |
96 | 0 | { |
97 | 0 | int i; |
98 | 0 | XMEMSET( S, 0, sizeof( blake2b_state ) ); |
99 | |
|
100 | 0 | for( i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i]; |
101 | |
|
102 | 0 | return 0; |
103 | 0 | } |
104 | | |
105 | | /* init xors IV with input parameter block */ |
106 | | int blake2b_init_param( blake2b_state *S, const blake2b_param *P ) |
107 | 0 | { |
108 | 0 | word32 i; |
109 | 0 | const byte *p ; |
110 | 0 | blake2b_init0( S ); |
111 | 0 | p = ( const byte * )( P ); |
112 | | |
113 | | /* IV XOR ParamBlock */ |
114 | 0 | for( i = 0; i < 8; ++i ) |
115 | 0 | S->h[i] ^= load64( p + sizeof( S->h[i] ) * i ); |
116 | |
|
117 | 0 | return 0; |
118 | 0 | } |
119 | | |
120 | | |
121 | | int blake2b_init( blake2b_state *S, const byte outlen ) |
122 | 0 | { |
123 | 0 | volatile blake2b_param P; |
124 | |
|
125 | 0 | if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return BAD_FUNC_ARG; |
126 | | |
127 | 0 | XMEMSET((void *)(wc_ptr_t)&P, 0, sizeof(P)); |
128 | 0 | WC_BARRIER(); |
129 | 0 | P.digest_length = outlen; |
130 | 0 | P.fanout = 1; |
131 | 0 | P.depth = 1; |
132 | |
|
133 | 0 | return blake2b_init_param(S, (const blake2b_param *)(wc_ptr_t)&P); |
134 | 0 | } |
135 | | |
136 | | int blake2b_init_key( blake2b_state *S, const byte outlen, const void *key, |
137 | | const byte keylen ) |
138 | 0 | { |
139 | 0 | int ret = 0; |
140 | 0 | volatile blake2b_param P; |
141 | |
|
142 | 0 | if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return BAD_FUNC_ARG; |
143 | | |
144 | 0 | if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return BAD_FUNC_ARG; |
145 | | |
146 | 0 | XMEMSET( (void *)(wc_ptr_t)&P, 0, sizeof( P ) ); |
147 | 0 | WC_BARRIER(); |
148 | 0 | P.digest_length = outlen; |
149 | 0 | P.key_length = keylen; |
150 | 0 | P.fanout = 1; |
151 | 0 | P.depth = 1; |
152 | |
|
153 | 0 | ret = blake2b_init_param(S, (const blake2b_param *)(wc_ptr_t)&P); |
154 | 0 | if ( ret < 0 ) return ret; |
155 | | |
156 | 0 | { |
157 | 0 | #ifdef WOLFSSL_SMALL_STACK |
158 | 0 | byte* block; |
159 | |
|
160 | 0 | block = (byte*)XMALLOC(BLAKE2B_BLOCKBYTES, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
161 | |
|
162 | 0 | if ( block == NULL ) return MEMORY_E; |
163 | | #else |
164 | | byte block[BLAKE2B_BLOCKBYTES]; |
165 | | #endif |
166 | | |
167 | 0 | XMEMSET( block, 0, BLAKE2B_BLOCKBYTES ); |
168 | 0 | XMEMCPY( block, key, keylen ); |
169 | 0 | ret = blake2b_update( S, block, BLAKE2B_BLOCKBYTES ); |
170 | 0 | secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from */ |
171 | | /* memory */ |
172 | |
|
173 | 0 | WC_FREE_VAR_EX(block, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
174 | 0 | } |
175 | 0 | return ret; |
176 | 0 | } |
177 | | |
178 | | static WC_INLINE int blake2b_compress( |
179 | | blake2b_state *S, |
180 | | const byte block[BLAKE2B_BLOCKBYTES], |
181 | | word64* m, |
182 | | word64* v) |
183 | 0 | { |
184 | 0 | word64 i; |
185 | |
|
186 | 0 | for( i = 0; i < 16; ++i ) |
187 | 0 | m[i] = load64( block + i * sizeof( m[i] ) ); |
188 | |
|
189 | 0 | for( i = 0; i < 8; ++i ) |
190 | 0 | v[i] = S->h[i]; |
191 | |
|
192 | 0 | v[ 8] = blake2b_IV[0]; |
193 | 0 | v[ 9] = blake2b_IV[1]; |
194 | 0 | v[10] = blake2b_IV[2]; |
195 | 0 | v[11] = blake2b_IV[3]; |
196 | 0 | v[12] = S->t[0] ^ blake2b_IV[4]; |
197 | 0 | v[13] = S->t[1] ^ blake2b_IV[5]; |
198 | 0 | v[14] = S->f[0] ^ blake2b_IV[6]; |
199 | 0 | v[15] = S->f[1] ^ blake2b_IV[7]; |
200 | 0 | #define G(r,i,a,b,c,d) \ |
201 | 0 | do { \ |
202 | 0 | (a) = (a) + (b) + m[blake2b_sigma[r][2*(i)+0]]; \ |
203 | 0 | (d) = rotr64((d) ^ (a), 32); \ |
204 | 0 | (c) = (c) + (d); \ |
205 | 0 | (b) = rotr64((b) ^ (c), 24); \ |
206 | 0 | (a) = (a) + (b) + m[blake2b_sigma[r][2*(i)+1]]; \ |
207 | 0 | (d) = rotr64((d) ^ (a), 16); \ |
208 | 0 | (c) = (c) + (d); \ |
209 | 0 | (b) = rotr64((b) ^ (c), 63); \ |
210 | 0 | } while(0) |
211 | 0 | #define ROUND(r) \ |
212 | 0 | do { \ |
213 | 0 | G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \ |
214 | 0 | G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \ |
215 | 0 | G(r,2,v[ 2],v[ 6],v[10],v[14]); \ |
216 | 0 | G(r,3,v[ 3],v[ 7],v[11],v[15]); \ |
217 | 0 | G(r,4,v[ 0],v[ 5],v[10],v[15]); \ |
218 | 0 | G(r,5,v[ 1],v[ 6],v[11],v[12]); \ |
219 | 0 | G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \ |
220 | 0 | G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \ |
221 | 0 | } while(0) |
222 | 0 | ROUND( 0 ); |
223 | 0 | ROUND( 1 ); |
224 | 0 | ROUND( 2 ); |
225 | 0 | ROUND( 3 ); |
226 | 0 | ROUND( 4 ); |
227 | 0 | ROUND( 5 ); |
228 | 0 | ROUND( 6 ); |
229 | 0 | ROUND( 7 ); |
230 | 0 | ROUND( 8 ); |
231 | 0 | ROUND( 9 ); |
232 | 0 | ROUND( 10 ); |
233 | 0 | ROUND( 11 ); |
234 | |
|
235 | 0 | for( i = 0; i < 8; ++i ) |
236 | 0 | S->h[i] = S->h[i] ^ v[i] ^ v[i + 8]; |
237 | |
|
238 | 0 | #undef G |
239 | 0 | #undef ROUND |
240 | |
|
241 | 0 | return 0; |
242 | 0 | } |
243 | | |
244 | | /* inlen now in bytes */ |
245 | | int blake2b_update( blake2b_state *S, const byte *in, word64 inlen ) |
246 | 0 | { |
247 | 0 | int ret = 0; |
248 | 0 | #ifdef WOLFSSL_SMALL_STACK |
249 | 0 | word64* m; |
250 | 0 | word64* v; |
251 | |
|
252 | 0 | m = (word64*)XMALLOC(sizeof(word64) * 32, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
253 | |
|
254 | 0 | if ( m == NULL ) return MEMORY_E; |
255 | | |
256 | 0 | v = &m[16]; |
257 | | #else |
258 | | word64 m[16]; |
259 | | word64 v[16]; |
260 | | #endif |
261 | |
|
262 | 0 | while( inlen > 0 ) |
263 | 0 | { |
264 | 0 | word64 left = S->buflen; |
265 | 0 | word64 fill = 2 * BLAKE2B_BLOCKBYTES - left; |
266 | |
|
267 | 0 | if( inlen > fill ) |
268 | 0 | { |
269 | 0 | XMEMCPY( S->buf + left, in, (wolfssl_word)fill ); /* Fill buffer */ |
270 | 0 | S->buflen += fill; |
271 | 0 | blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); |
272 | |
|
273 | 0 | { |
274 | 0 | ret = blake2b_compress( S, S->buf, m, v ); |
275 | 0 | if (ret < 0) break; |
276 | 0 | } |
277 | | |
278 | 0 | XMEMCPY( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); |
279 | | /* Shift buffer left */ |
280 | 0 | S->buflen -= BLAKE2B_BLOCKBYTES; |
281 | 0 | in += fill; |
282 | 0 | inlen -= fill; |
283 | 0 | } |
284 | 0 | else /* inlen <= fill */ |
285 | 0 | { |
286 | 0 | XMEMCPY( S->buf + left, in, (wolfssl_word)inlen ); |
287 | 0 | S->buflen += inlen; /* Be lazy, do not compress */ |
288 | 0 | inlen = 0; |
289 | 0 | } |
290 | 0 | } |
291 | |
|
292 | 0 | WC_FREE_VAR_EX(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
293 | |
|
294 | 0 | return ret; |
295 | 0 | } |
296 | | |
297 | | /* Is this correct? */ |
298 | | int blake2b_final( blake2b_state *S, byte *out, byte outlen ) |
299 | 0 | { |
300 | 0 | int ret = 0; |
301 | 0 | byte buffer[BLAKE2B_OUTBYTES]; |
302 | 0 | word64 i; |
303 | 0 | #ifdef WOLFSSL_SMALL_STACK |
304 | 0 | word64* m; |
305 | 0 | word64* v; |
306 | |
|
307 | 0 | m = (word64*)XMALLOC(sizeof(word64) * 32, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
308 | |
|
309 | 0 | if ( m == NULL ) return MEMORY_E; |
310 | | |
311 | 0 | v = &m[16]; |
312 | | #else |
313 | | word64 m[16]; |
314 | | word64 v[16]; |
315 | | #endif |
316 | |
|
317 | 0 | if( S->buflen > BLAKE2B_BLOCKBYTES ) |
318 | 0 | { |
319 | 0 | blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); |
320 | |
|
321 | 0 | { |
322 | 0 | ret = blake2b_compress( S, S->buf, m, v ); |
323 | 0 | if (ret < 0) goto out; |
324 | 0 | } |
325 | | |
326 | 0 | S->buflen -= BLAKE2B_BLOCKBYTES; |
327 | 0 | if ( S->buflen > BLAKE2B_BLOCKBYTES ) |
328 | 0 | return BAD_LENGTH_E; |
329 | 0 | XMEMMOVE( S->buf, S->buf + BLAKE2B_BLOCKBYTES, (wolfssl_word)S->buflen ); |
330 | 0 | } |
331 | | |
332 | 0 | blake2b_increment_counter( S, S->buflen ); |
333 | 0 | blake2b_set_lastblock( S ); |
334 | 0 | XMEMSET( S->buf + S->buflen, 0, (wolfssl_word)(2 * BLAKE2B_BLOCKBYTES - S->buflen) ); |
335 | | /* Padding */ |
336 | 0 | { |
337 | 0 | ret = blake2b_compress( S, S->buf, m, v ); |
338 | 0 | if (ret < 0) goto out; |
339 | 0 | } |
340 | | |
341 | 0 | for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */ |
342 | 0 | store64( buffer + sizeof( S->h[i] ) * i, S->h[i] ); |
343 | |
|
344 | 0 | XMEMCPY( out, buffer, outlen ); |
345 | |
|
346 | 0 | out: |
347 | |
|
348 | 0 | WC_FREE_VAR_EX(m, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
349 | |
|
350 | 0 | return ret; |
351 | 0 | } |
352 | | |
353 | | /* inlen, at least, should be word64. Others can be size_t. */ |
354 | | int blake2b( byte *out, const void *in, const void *key, const byte outlen, |
355 | | const word64 inlen, byte keylen ) |
356 | 0 | { |
357 | 0 | blake2b_state S[1]; |
358 | | |
359 | | /* Verify parameters */ |
360 | 0 | if ( NULL == in ) return BAD_FUNC_ARG; |
361 | | |
362 | 0 | if ( NULL == out ) return BAD_FUNC_ARG; |
363 | | |
364 | 0 | if( NULL == key ) keylen = 0; |
365 | |
|
366 | 0 | if( keylen > 0 ) |
367 | 0 | { |
368 | 0 | int ret = blake2b_init_key( S, outlen, key, keylen ); |
369 | 0 | if (ret < 0) return ret; |
370 | 0 | } |
371 | 0 | else |
372 | 0 | { |
373 | 0 | int ret = blake2b_init( S, outlen ); |
374 | 0 | if (ret < 0) return ret; |
375 | 0 | } |
376 | | |
377 | 0 | { |
378 | 0 | int ret = blake2b_update( S, ( const byte * )in, inlen ); |
379 | 0 | if (ret < 0) return ret; |
380 | 0 | } |
381 | | |
382 | 0 | return blake2b_final( S, out, outlen ); |
383 | 0 | } |
384 | | |
385 | | #if defined(BLAKE2B_SELFTEST) |
386 | | #include <string.h> |
387 | | #include "blake2-kat.h" |
388 | | int main( int argc, char **argv ) |
389 | | { |
390 | | byte key[BLAKE2B_KEYBYTES]; |
391 | | byte buf[KAT_LENGTH]; |
392 | | |
393 | | for( word32 i = 0; i < BLAKE2B_KEYBYTES; ++i ) |
394 | | key[i] = ( byte )i; |
395 | | |
396 | | for( word32 i = 0; i < KAT_LENGTH; ++i ) |
397 | | buf[i] = ( byte )i; |
398 | | |
399 | | for( word32 i = 0; i < KAT_LENGTH; ++i ) |
400 | | { |
401 | | byte hash[BLAKE2B_OUTBYTES]; |
402 | | if ( blake2b( hash, buf, key, BLAKE2B_OUTBYTES, i, BLAKE2B_KEYBYTES ) < 0 ) |
403 | | { |
404 | | puts( "error" ); |
405 | | return -1; |
406 | | } |
407 | | |
408 | | if( 0 != XMEMCMP( hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES ) ) |
409 | | { |
410 | | puts( "error" ); |
411 | | return -1; |
412 | | } |
413 | | } |
414 | | |
415 | | puts( "ok" ); |
416 | | return 0; |
417 | | } |
418 | | #endif |
419 | | |
420 | | |
421 | | /* wolfCrypt API */ |
422 | | |
423 | | /* Init Blake2b digest, track size in case final doesn't want to "remember" */ |
424 | | int wc_InitBlake2b(Blake2b* b2b, word32 digestSz) |
425 | 0 | { |
426 | 0 | if (b2b == NULL){ |
427 | 0 | return BAD_FUNC_ARG; |
428 | 0 | } |
429 | 0 | if (digestSz == 0 || digestSz > BLAKE2B_OUTBYTES) { |
430 | 0 | return BAD_FUNC_ARG; |
431 | 0 | } |
432 | 0 | b2b->digestSz = digestSz; |
433 | |
|
434 | 0 | return blake2b_init(b2b->S, (byte)digestSz); |
435 | 0 | } |
436 | | |
437 | | /* Init Blake2b digest with key, track size in case final doesn't want to "remember" */ |
438 | | int wc_InitBlake2b_WithKey(Blake2b* b2b, word32 digestSz, const byte *key, word32 keylen) |
439 | 0 | { |
440 | 0 | if (b2b == NULL){ |
441 | 0 | return BAD_FUNC_ARG; |
442 | 0 | } |
443 | 0 | if (digestSz == 0 || digestSz > BLAKE2B_OUTBYTES) { |
444 | 0 | return BAD_FUNC_ARG; |
445 | 0 | } |
446 | 0 | b2b->digestSz = digestSz; |
447 | |
|
448 | 0 | if (keylen >= 256) |
449 | 0 | return BAD_FUNC_ARG; |
450 | | |
451 | 0 | if (key) |
452 | 0 | return blake2b_init_key(b2b->S, (byte)digestSz, key, (byte)keylen); |
453 | 0 | else |
454 | 0 | return blake2b_init(b2b->S, (byte)digestSz); |
455 | 0 | } |
456 | | |
457 | | /* Blake2b Update */ |
458 | | int wc_Blake2bUpdate(Blake2b* b2b, const byte* data, word32 sz) |
459 | 0 | { |
460 | 0 | if (b2b == NULL){ |
461 | 0 | return BAD_FUNC_ARG; |
462 | 0 | } |
463 | 0 | if (data == NULL && sz != 0){ |
464 | 0 | return BAD_FUNC_ARG; |
465 | 0 | } |
466 | 0 | if (sz == 0){ |
467 | 0 | return 0; |
468 | 0 | } |
469 | | |
470 | 0 | return blake2b_update(b2b->S, data, sz); |
471 | 0 | } |
472 | | |
473 | | |
474 | | /* Blake2b Final, if pass in zero size we use init digestSz */ |
475 | | int wc_Blake2bFinal(Blake2b* b2b, byte* final, word32 requestSz) |
476 | 0 | { |
477 | 0 | word32 sz; |
478 | |
|
479 | 0 | if (b2b == NULL){ |
480 | 0 | return BAD_FUNC_ARG; |
481 | 0 | } |
482 | 0 | if (final == NULL){ |
483 | 0 | return BAD_FUNC_ARG; |
484 | 0 | } |
485 | | |
486 | 0 | sz = requestSz ? requestSz : b2b->digestSz; |
487 | 0 | if (sz == 0 || sz > BLAKE2B_OUTBYTES) { |
488 | 0 | return BAD_FUNC_ARG; |
489 | 0 | } |
490 | | |
491 | 0 | return blake2b_final(b2b->S, final, (byte)sz); |
492 | 0 | } |
493 | | |
494 | | |
495 | | int wc_Blake2bHmacInit(Blake2b* b2b, const byte* key, size_t key_len) |
496 | 0 | { |
497 | 0 | byte x_key[BLAKE2B_BLOCKBYTES]; |
498 | 0 | int i; |
499 | 0 | int ret = 0; |
500 | |
|
501 | 0 | if (key == NULL) |
502 | 0 | return BAD_FUNC_ARG; |
503 | | |
504 | 0 | XMEMSET(x_key, 0, sizeof(x_key)); |
505 | |
|
506 | 0 | if (key_len > BLAKE2B_BLOCKBYTES) { |
507 | 0 | ret = wc_InitBlake2b(b2b, BLAKE2B_OUTBYTES); |
508 | 0 | if (ret == 0) |
509 | 0 | ret = wc_Blake2bUpdate(b2b, key, (word32)key_len); |
510 | 0 | if (ret == 0) |
511 | 0 | ret = wc_Blake2bFinal(b2b, x_key, 0); |
512 | 0 | } else { |
513 | 0 | XMEMCPY(x_key, key, key_len); |
514 | 0 | } |
515 | |
|
516 | 0 | if (ret == 0) { |
517 | 0 | for (i = 0; i < BLAKE2B_BLOCKBYTES; ++i) |
518 | 0 | x_key[i] ^= 0x36U; |
519 | 0 | } |
520 | |
|
521 | 0 | if (ret == 0) |
522 | 0 | ret = wc_InitBlake2b(b2b, BLAKE2B_OUTBYTES); |
523 | 0 | if (ret == 0) |
524 | 0 | ret = wc_Blake2bUpdate(b2b, x_key, BLAKE2B_BLOCKBYTES); |
525 | |
|
526 | 0 | ForceZero(x_key, sizeof(x_key)); |
527 | |
|
528 | 0 | return ret; |
529 | 0 | } |
530 | | |
531 | | int wc_Blake2bHmacUpdate(Blake2b* b2b, const byte* in, size_t in_len) |
532 | 0 | { |
533 | 0 | if (in == NULL) |
534 | 0 | return BAD_FUNC_ARG; |
535 | | /* Sanity check in_len to prevent truncation when cast to word32. */ |
536 | 0 | if (in_len > WOLFSSL_MAX_32BIT) |
537 | 0 | return BAD_FUNC_ARG; |
538 | | |
539 | 0 | return wc_Blake2bUpdate(b2b, in, (word32)in_len); |
540 | 0 | } |
541 | | |
542 | | int wc_Blake2bHmacFinal(Blake2b* b2b, const byte* key, size_t key_len, |
543 | | byte* out, size_t out_len) |
544 | 0 | { |
545 | 0 | byte x_key[BLAKE2B_BLOCKBYTES]; |
546 | 0 | Blake2b keyHash; |
547 | 0 | int i; |
548 | 0 | int ret = 0; |
549 | |
|
550 | 0 | if (key == NULL) |
551 | 0 | return BAD_FUNC_ARG; |
552 | | |
553 | 0 | if (out_len != BLAKE2B_OUTBYTES) |
554 | 0 | return BUFFER_E; |
555 | | |
556 | 0 | XMEMSET(x_key, 0, sizeof(x_key)); |
557 | |
|
558 | 0 | if (key_len > BLAKE2B_BLOCKBYTES) { |
559 | 0 | ret = wc_InitBlake2b(&keyHash, BLAKE2B_OUTBYTES); |
560 | 0 | if (ret == 0) |
561 | 0 | ret = wc_Blake2bUpdate(&keyHash, key, (word32)key_len); |
562 | 0 | if (ret == 0) |
563 | 0 | ret = wc_Blake2bFinal(&keyHash, x_key, 0); |
564 | 0 | ForceZero(&keyHash, sizeof(keyHash)); |
565 | 0 | } else { |
566 | 0 | XMEMCPY(x_key, key, key_len); |
567 | 0 | } |
568 | |
|
569 | 0 | if (ret == 0) { |
570 | 0 | for (i = 0; i < BLAKE2B_BLOCKBYTES; ++i) |
571 | 0 | x_key[i] ^= 0x5CU; |
572 | 0 | } |
573 | |
|
574 | 0 | if (ret == 0) |
575 | 0 | ret = wc_Blake2bFinal(b2b, out, 0); |
576 | |
|
577 | 0 | if (ret == 0) |
578 | 0 | ret = wc_InitBlake2b(b2b, BLAKE2B_OUTBYTES); |
579 | 0 | if (ret == 0) |
580 | 0 | ret = wc_Blake2bUpdate(b2b, x_key, BLAKE2B_BLOCKBYTES); |
581 | 0 | if (ret == 0) |
582 | 0 | ret = wc_Blake2bUpdate(b2b, out, BLAKE2B_OUTBYTES); |
583 | 0 | if (ret == 0) |
584 | 0 | ret = wc_Blake2bFinal(b2b, out, 0); |
585 | |
|
586 | 0 | ForceZero(x_key, sizeof(x_key)); |
587 | |
|
588 | 0 | return ret; |
589 | 0 | } |
590 | | |
591 | | int wc_Blake2bHmac(const byte* in, size_t in_len, |
592 | | const byte* key, size_t key_len, |
593 | | byte* out, size_t out_len) |
594 | 0 | { |
595 | 0 | Blake2b state; |
596 | 0 | int ret; |
597 | |
|
598 | 0 | ret = wc_Blake2bHmacInit(&state, key, key_len); |
599 | 0 | if (ret == 0) |
600 | 0 | ret = wc_Blake2bHmacUpdate(&state, in, in_len); |
601 | 0 | if (ret == 0) |
602 | 0 | ret = wc_Blake2bHmacFinal(&state, key, key_len, out, out_len); |
603 | |
|
604 | 0 | return ret; |
605 | 0 | } |
606 | | |
607 | | /* end wolfCrypt API */ |
608 | | |
609 | | #endif /* HAVE_BLAKE2B */ |