source: UsbWattMeter/trunk/wolfssl-3.7.0/wolfcrypt/src/blake2b.c@ 164

Last change on this file since 164 was 164, checked in by coas-nagasima, 6 years ago

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 10.8 KB
Line 
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-2015 wolfSSL Inc.
16 *
17 * This file is part of wolfSSL. (formerly known as CyaSSL)
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 2 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-1301, USA
32 */
33
34
35#ifdef HAVE_CONFIG_H
36 #include <config.h>
37#endif
38
39#include <wolfssl/wolfcrypt/settings.h>
40
41#ifdef HAVE_BLAKE2
42
43#include <wolfssl/wolfcrypt/blake2.h>
44#include <wolfssl/wolfcrypt/blake2-impl.h>
45
46
47static const word64 blake2b_IV[8] =
48{
49 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
50 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
51 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
52 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
53};
54
55static 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
72static INLINE int blake2b_set_lastnode( blake2b_state *S )
73{
74 S->f[1] = ~0ULL;
75 return 0;
76}
77
78/* Some helper functions, not necessarily useful */
79static INLINE int blake2b_set_lastblock( blake2b_state *S )
80{
81 if( S->last_node ) blake2b_set_lastnode( S );
82
83 S->f[0] = ~0ULL;
84 return 0;
85}
86
87static INLINE int blake2b_increment_counter( blake2b_state *S, const word64
88 inc )
89{
90 S->t[0] += inc;
91 S->t[1] += ( S->t[0] < inc );
92 return 0;
93}
94
95static INLINE int blake2b_init0( blake2b_state *S )
96{
97 int i;
98 XMEMSET( S, 0, sizeof( blake2b_state ) );
99
100 for( i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i];
101
102 return 0;
103}
104
105/* init xors IV with input parameter block */
106int blake2b_init_param( blake2b_state *S, const blake2b_param *P )
107{
108 word32 i;
109 byte *p ;
110 blake2b_init0( S );
111 p = ( byte * )( P );
112
113 /* IV XOR ParamBlock */
114 for( i = 0; i < 8; ++i )
115 S->h[i] ^= load64( p + sizeof( S->h[i] ) * i );
116
117 return 0;
118}
119
120
121
122int blake2b_init( blake2b_state *S, const byte outlen )
123{
124 blake2b_param P[1];
125
126 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
127
128 P->digest_length = outlen;
129 P->key_length = 0;
130 P->fanout = 1;
131 P->depth = 1;
132 store32( &P->leaf_length, 0 );
133 store64( &P->node_offset, 0 );
134 P->node_depth = 0;
135 P->inner_length = 0;
136 XMEMSET( P->reserved, 0, sizeof( P->reserved ) );
137 XMEMSET( P->salt, 0, sizeof( P->salt ) );
138 XMEMSET( P->personal, 0, sizeof( P->personal ) );
139 return blake2b_init_param( S, P );
140}
141
142
143int blake2b_init_key( blake2b_state *S, const byte outlen, const void *key,
144 const byte keylen )
145{
146 blake2b_param P[1];
147
148 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
149
150 if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1;
151
152 P->digest_length = outlen;
153 P->key_length = keylen;
154 P->fanout = 1;
155 P->depth = 1;
156 store32( &P->leaf_length, 0 );
157 store64( &P->node_offset, 0 );
158 P->node_depth = 0;
159 P->inner_length = 0;
160 XMEMSET( P->reserved, 0, sizeof( P->reserved ) );
161 XMEMSET( P->salt, 0, sizeof( P->salt ) );
162 XMEMSET( P->personal, 0, sizeof( P->personal ) );
163
164 if( blake2b_init_param( S, P ) < 0 ) return -1;
165
166 {
167#ifdef WOLFSSL_SMALL_STACK
168 byte* block;
169
170 block = (byte*)XMALLOC(BLAKE2B_BLOCKBYTES, NULL, DYNAMIC_TYPE_TMP_BUFFER);
171
172 if ( block == NULL ) return -1;
173#else
174 byte block[BLAKE2B_BLOCKBYTES];
175#endif
176
177 XMEMSET( block, 0, BLAKE2B_BLOCKBYTES );
178 XMEMCPY( block, key, keylen );
179 blake2b_update( S, block, BLAKE2B_BLOCKBYTES );
180 secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from */
181 /* memory */
182
183#ifdef WOLFSSL_SMALL_STACK
184 XFREE(block, NULL, DYNAMIC_TYPE_TMP_BUFFER);
185#endif
186 }
187 return 0;
188}
189
190static int blake2b_compress( blake2b_state *S,
191 const byte block[BLAKE2B_BLOCKBYTES] )
192{
193 int i;
194
195#ifdef WOLFSSL_SMALL_STACK
196 word64* m;
197 word64* v;
198
199 m = (word64*)XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
200
201 if ( m == NULL ) return -1;
202
203 v = (word64*)XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
204
205 if ( v == NULL )
206 {
207 XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
208 return -1;
209 }
210#else
211 word64 m[16];
212 word64 v[16];
213#endif
214
215 for( i = 0; i < 16; ++i )
216 m[i] = load64( block + i * sizeof( m[i] ) );
217
218 for( i = 0; i < 8; ++i )
219 v[i] = S->h[i];
220
221 v[ 8] = blake2b_IV[0];
222 v[ 9] = blake2b_IV[1];
223 v[10] = blake2b_IV[2];
224 v[11] = blake2b_IV[3];
225 v[12] = S->t[0] ^ blake2b_IV[4];
226 v[13] = S->t[1] ^ blake2b_IV[5];
227 v[14] = S->f[0] ^ blake2b_IV[6];
228 v[15] = S->f[1] ^ blake2b_IV[7];
229#define G(r,i,a,b,c,d) \
230 do { \
231 a = a + b + m[blake2b_sigma[r][2*i+0]]; \
232 d = rotr64(d ^ a, 32); \
233 c = c + d; \
234 b = rotr64(b ^ c, 24); \
235 a = a + b + m[blake2b_sigma[r][2*i+1]]; \
236 d = rotr64(d ^ a, 16); \
237 c = c + d; \
238 b = rotr64(b ^ c, 63); \
239 } while(0)
240#define ROUND(r) \
241 do { \
242 G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
243 G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
244 G(r,2,v[ 2],v[ 6],v[10],v[14]); \
245 G(r,3,v[ 3],v[ 7],v[11],v[15]); \
246 G(r,4,v[ 0],v[ 5],v[10],v[15]); \
247 G(r,5,v[ 1],v[ 6],v[11],v[12]); \
248 G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
249 G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
250 } while(0)
251 ROUND( 0 );
252 ROUND( 1 );
253 ROUND( 2 );
254 ROUND( 3 );
255 ROUND( 4 );
256 ROUND( 5 );
257 ROUND( 6 );
258 ROUND( 7 );
259 ROUND( 8 );
260 ROUND( 9 );
261 ROUND( 10 );
262 ROUND( 11 );
263
264 for( i = 0; i < 8; ++i )
265 S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
266
267#undef G
268#undef ROUND
269
270#ifdef WOLFSSL_SMALL_STACK
271 XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
272 XFREE(v, NULL, DYNAMIC_TYPE_TMP_BUFFER);
273#endif
274
275 return 0;
276}
277
278/* inlen now in bytes */
279int blake2b_update( blake2b_state *S, const byte *in, word64 inlen )
280{
281 while( inlen > 0 )
282 {
283 word64 left = S->buflen;
284 word64 fill = 2 * BLAKE2B_BLOCKBYTES - left;
285
286 if( inlen > fill )
287 {
288 XMEMCPY( S->buf + left, in, (wolfssl_word)fill ); /* Fill buffer */
289 S->buflen += fill;
290 blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
291
292 if ( blake2b_compress( S, S->buf ) < 0 ) return -1; /* Compress */
293
294 XMEMCPY( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES );
295 /* Shift buffer left */
296 S->buflen -= BLAKE2B_BLOCKBYTES;
297 in += fill;
298 inlen -= fill;
299 }
300 else /* inlen <= fill */
301 {
302 XMEMCPY( S->buf + left, in, (wolfssl_word)inlen );
303 S->buflen += inlen; /* Be lazy, do not compress */
304 in += inlen;
305 inlen -= inlen;
306 }
307 }
308
309 return 0;
310}
311
312/* Is this correct? */
313int blake2b_final( blake2b_state *S, byte *out, byte outlen )
314{
315 byte buffer[BLAKE2B_OUTBYTES];
316 int i;
317
318 if( S->buflen > BLAKE2B_BLOCKBYTES )
319 {
320 blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
321
322 if ( blake2b_compress( S, S->buf ) < 0 ) return -1;
323
324 S->buflen -= BLAKE2B_BLOCKBYTES;
325 XMEMCPY( S->buf, S->buf + BLAKE2B_BLOCKBYTES, (wolfssl_word)S->buflen );
326 }
327
328 blake2b_increment_counter( S, S->buflen );
329 blake2b_set_lastblock( S );
330 XMEMSET( S->buf + S->buflen, 0, (wolfssl_word)(2 * BLAKE2B_BLOCKBYTES - S->buflen) );
331 /* Padding */
332 if ( blake2b_compress( S, S->buf ) < 0 ) return -1;
333
334 for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
335 store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
336
337 XMEMCPY( out, buffer, outlen );
338 return 0;
339}
340
341/* inlen, at least, should be word64. Others can be size_t. */
342int blake2b( byte *out, const void *in, const void *key, const byte outlen,
343 const word64 inlen, byte keylen )
344{
345 blake2b_state S[1];
346
347 /* Verify parameters */
348 if ( NULL == in ) return -1;
349
350 if ( NULL == out ) return -1;
351
352 if( NULL == key ) keylen = 0;
353
354 if( keylen > 0 )
355 {
356 if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1;
357 }
358 else
359 {
360 if( blake2b_init( S, outlen ) < 0 ) return -1;
361 }
362
363 if ( blake2b_update( S, ( byte * )in, inlen ) < 0) return -1;
364
365 return blake2b_final( S, out, outlen );
366}
367
368#if defined(BLAKE2B_SELFTEST)
369#include <string.h>
370#include "blake2-kat.h"
371int main( int argc, char **argv )
372{
373 byte key[BLAKE2B_KEYBYTES];
374 byte buf[KAT_LENGTH];
375
376 for( word32 i = 0; i < BLAKE2B_KEYBYTES; ++i )
377 key[i] = ( byte )i;
378
379 for( word32 i = 0; i < KAT_LENGTH; ++i )
380 buf[i] = ( byte )i;
381
382 for( word32 i = 0; i < KAT_LENGTH; ++i )
383 {
384 byte hash[BLAKE2B_OUTBYTES];
385 if ( blake2b( hash, buf, key, BLAKE2B_OUTBYTES, i, BLAKE2B_KEYBYTES ) < 0 )
386 {
387 puts( "error" );
388 return -1;
389 }
390
391 if( 0 != memcmp( hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES ) )
392 {
393 puts( "error" );
394 return -1;
395 }
396 }
397
398 puts( "ok" );
399 return 0;
400}
401#endif
402
403
404/* wolfCrypt API */
405
406/* Init Blake2b digest, track size incase final doesn't want to "remember" */
407int wc_InitBlake2b(Blake2b* b2b, word32 digestSz)
408{
409 b2b->digestSz = digestSz;
410
411 return blake2b_init(b2b->S, (byte)digestSz);
412}
413
414
415/* Blake2b Update */
416int wc_Blake2bUpdate(Blake2b* b2b, const byte* data, word32 sz)
417{
418 return blake2b_update(b2b->S, data, sz);
419}
420
421
422/* Blake2b Final, if pass in zero size we use init digestSz */
423int wc_Blake2bFinal(Blake2b* b2b, byte* final, word32 requestSz)
424{
425 word32 sz = requestSz ? requestSz : b2b->digestSz;
426
427 return blake2b_final(b2b->S, final, (byte)sz);
428}
429
430
431/* end CTaoCrypt API */
432
433#endif /* HAVE_BLAKE2 */
434
Note: See TracBrowser for help on using the repository browser.