libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_generichash / blake2 / ref / blake2b-ref.c
blobc36ea6357306e8a3e3f66bd1f3540542b6aae589
1 /*
2 BLAKE2 reference source code package - reference C implementations
4 Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
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.
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/>.
14 #include <stdint.h>
15 #include <string.h>
16 #include <stdio.h>
18 #include "blake2.h"
19 #include "blake2-impl.h"
21 static const uint64_t blake2b_IV[8] =
23 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
24 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
25 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
26 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
29 static const uint8_t blake2b_sigma[12][16] =
31 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
32 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
33 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
34 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
35 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
36 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
37 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
38 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
39 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
40 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
41 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
42 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
46 static inline int blake2b_set_lastnode( blake2b_state *S )
48 S->f[1] = ~0ULL;
49 return 0;
52 static inline int blake2b_clear_lastnode( blake2b_state *S )
54 S->f[1] = 0ULL;
55 return 0;
58 /* Some helper functions, not necessarily useful */
59 static inline int blake2b_set_lastblock( blake2b_state *S )
61 if( S->last_node ) blake2b_set_lastnode( S );
63 S->f[0] = ~0ULL;
64 return 0;
67 static inline int blake2b_clear_lastblock( blake2b_state *S )
69 if( S->last_node ) blake2b_clear_lastnode( S );
71 S->f[0] = 0ULL;
72 return 0;
75 static inline int blake2b_increment_counter( blake2b_state *S, const uint64_t inc )
77 S->t[0] += inc;
78 S->t[1] += ( S->t[0] < inc );
79 return 0;
84 // Parameter-related functions
85 static inline int blake2b_param_set_digest_length( blake2b_param *P, const uint8_t digest_length )
87 P->digest_length = digest_length;
88 return 0;
91 static inline int blake2b_param_set_fanout( blake2b_param *P, const uint8_t fanout )
93 P->fanout = fanout;
94 return 0;
97 static inline int blake2b_param_set_max_depth( blake2b_param *P, const uint8_t depth )
99 P->depth = depth;
100 return 0;
103 static inline int blake2b_param_set_leaf_length( blake2b_param *P, const uint32_t leaf_length )
105 store32( &P->leaf_length, leaf_length );
106 return 0;
109 static inline int blake2b_param_set_node_offset( blake2b_param *P, const uint64_t node_offset )
111 store64( &P->node_offset, node_offset );
112 return 0;
115 static inline int blake2b_param_set_node_depth( blake2b_param *P, const uint8_t node_depth )
117 P->node_depth = node_depth;
118 return 0;
121 static inline int blake2b_param_set_inner_length( blake2b_param *P, const uint8_t inner_length )
123 P->inner_length = inner_length;
124 return 0;
127 static inline int blake2b_param_set_salt( blake2b_param *P, const uint8_t salt[BLAKE2B_SALTBYTES] )
129 memcpy( P->salt, salt, BLAKE2B_SALTBYTES );
130 return 0;
133 static inline int blake2b_param_set_personal( blake2b_param *P, const uint8_t personal[BLAKE2B_PERSONALBYTES] )
135 memcpy( P->personal, personal, BLAKE2B_PERSONALBYTES );
136 return 0;
139 static inline int blake2b_init0( blake2b_state *S )
141 memset( S, 0, sizeof( blake2b_state ) );
143 for( int i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i];
145 return 0;
148 /* init xors IV with input parameter block */
149 int blake2b_init_param( blake2b_state *S, const blake2b_param *P )
151 blake2b_init0( S );
152 uint8_t *p = ( uint8_t * )( P );
154 /* IV XOR ParamBlock */
155 for( size_t i = 0; i < 8; ++i )
156 S->h[i] ^= load64( p + sizeof( S->h[i] ) * i );
158 return 0;
163 int blake2b_init( blake2b_state *S, const uint8_t outlen )
165 blake2b_param P[1];
167 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
169 P->digest_length = outlen;
170 P->key_length = 0;
171 P->fanout = 1;
172 P->depth = 1;
173 store32( &P->leaf_length, 0 );
174 store64( &P->node_offset, 0 );
175 P->node_depth = 0;
176 P->inner_length = 0;
177 memset( P->reserved, 0, sizeof( P->reserved ) );
178 memset( P->salt, 0, sizeof( P->salt ) );
179 memset( P->personal, 0, sizeof( P->personal ) );
180 return blake2b_init_param( S, P );
184 int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen )
186 blake2b_param P[1];
188 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
190 if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1;
192 P->digest_length = outlen;
193 P->key_length = keylen;
194 P->fanout = 1;
195 P->depth = 1;
196 store32( &P->leaf_length, 0 );
197 store64( &P->node_offset, 0 );
198 P->node_depth = 0;
199 P->inner_length = 0;
200 memset( P->reserved, 0, sizeof( P->reserved ) );
201 memset( P->salt, 0, sizeof( P->salt ) );
202 memset( P->personal, 0, sizeof( P->personal ) );
204 if( blake2b_init_param( S, P ) < 0 ) return -1;
207 uint8_t block[BLAKE2B_BLOCKBYTES];
208 memset( block, 0, BLAKE2B_BLOCKBYTES );
209 memcpy( block, key, keylen );
210 blake2b_update( S, block, BLAKE2B_BLOCKBYTES );
211 secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */
213 return 0;
216 static int blake2b_compress( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] )
218 uint64_t m[16];
219 uint64_t v[16];
220 int i;
222 for( i = 0; i < 16; ++i )
223 m[i] = load64( block + i * sizeof( m[i] ) );
225 for( i = 0; i < 8; ++i )
226 v[i] = S->h[i];
228 v[ 8] = blake2b_IV[0];
229 v[ 9] = blake2b_IV[1];
230 v[10] = blake2b_IV[2];
231 v[11] = blake2b_IV[3];
232 v[12] = S->t[0] ^ blake2b_IV[4];
233 v[13] = S->t[1] ^ blake2b_IV[5];
234 v[14] = S->f[0] ^ blake2b_IV[6];
235 v[15] = S->f[1] ^ blake2b_IV[7];
236 #define G(r,i,a,b,c,d) \
237 do { \
238 a = a + b + m[blake2b_sigma[r][2*i+0]]; \
239 d = rotr64(d ^ a, 32); \
240 c = c + d; \
241 b = rotr64(b ^ c, 24); \
242 a = a + b + m[blake2b_sigma[r][2*i+1]]; \
243 d = rotr64(d ^ a, 16); \
244 c = c + d; \
245 b = rotr64(b ^ c, 63); \
246 } while(0)
247 #define ROUND(r) \
248 do { \
249 G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
250 G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
251 G(r,2,v[ 2],v[ 6],v[10],v[14]); \
252 G(r,3,v[ 3],v[ 7],v[11],v[15]); \
253 G(r,4,v[ 0],v[ 5],v[10],v[15]); \
254 G(r,5,v[ 1],v[ 6],v[11],v[12]); \
255 G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
256 G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
257 } while(0)
258 ROUND( 0 );
259 ROUND( 1 );
260 ROUND( 2 );
261 ROUND( 3 );
262 ROUND( 4 );
263 ROUND( 5 );
264 ROUND( 6 );
265 ROUND( 7 );
266 ROUND( 8 );
267 ROUND( 9 );
268 ROUND( 10 );
269 ROUND( 11 );
271 for( i = 0; i < 8; ++i )
272 S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
274 #undef G
275 #undef ROUND
276 return 0;
279 /* inlen now in bytes */
280 int blake2b_update( blake2b_state *S, const uint8_t *in, uint64_t inlen )
282 while( inlen > 0 )
284 size_t left = S->buflen;
285 size_t fill = 2 * BLAKE2B_BLOCKBYTES - left;
287 if( inlen > fill )
289 memcpy( S->buf + left, in, fill ); // Fill buffer
290 S->buflen += fill;
291 blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
292 blake2b_compress( S, S->buf ); // Compress
293 memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); // Shift buffer left
294 S->buflen -= BLAKE2B_BLOCKBYTES;
295 in += fill;
296 inlen -= fill;
298 else // inlen <= fill
300 memcpy( S->buf + left, in, inlen );
301 S->buflen += inlen; // Be lazy, do not compress
302 in += inlen;
303 inlen -= inlen;
307 return 0;
310 /* Is this correct? */
311 int blake2b_final( blake2b_state *S, uint8_t *out, uint8_t outlen )
313 uint8_t buffer[BLAKE2B_OUTBYTES];
315 if( S->buflen > BLAKE2B_BLOCKBYTES )
317 blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
318 blake2b_compress( S, S->buf );
319 S->buflen -= BLAKE2B_BLOCKBYTES;
320 memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, S->buflen );
323 blake2b_increment_counter( S, S->buflen );
324 blake2b_set_lastblock( S );
325 memset( S->buf + S->buflen, 0, 2 * BLAKE2B_BLOCKBYTES - S->buflen ); /* Padding */
326 blake2b_compress( S, S->buf );
328 for( int i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
329 store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
331 memcpy( out, buffer, outlen );
332 return 0;
335 /* inlen, at least, should be uint64_t. Others can be size_t. */
336 int blake2b( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen )
338 blake2b_state S[1];
340 /* Verify parameters */
341 if ( NULL == in ) return -1;
343 if ( NULL == out ) return -1;
345 if( NULL == key ) keylen = 0;
347 if( keylen > 0 )
349 if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1;
351 else
353 if( blake2b_init( S, outlen ) < 0 ) return -1;
356 blake2b_update( S, ( uint8_t * )in, inlen );
357 blake2b_final( S, out, outlen );
358 return 0;