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 14 #include <stdint.h> 15 #include <string.h> 16 #include <stdio.h> 17 18 #include "blake2.h" 19 #include "blake2-impl.h" 20 21 static const uint64_t blake2b_IV[8] = 22 { 23 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 24 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, 25 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, 26 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL 27 }; 28 29 static const uint8_t blake2b_sigma[12][16] = 30 { 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 } 43 }; 44 45 46 static inline int blake2b_set_lastnode( blake2b_state *S ) 47 { 48 S->f[1] = ~0ULL; 49 return 0; 50 } 51 52 static inline int blake2b_clear_lastnode( blake2b_state *S ) 53 { 54 S->f[1] = 0ULL; 55 return 0; 56 } 57 58 /* Some helper functions, not necessarily useful */ 59 static inline int blake2b_set_lastblock( blake2b_state *S ) 60 { 61 if( S->last_node ) blake2b_set_lastnode( S ); 62 63 S->f[0] = ~0ULL; 64 return 0; 65 } 66 67 static inline int blake2b_clear_lastblock( blake2b_state *S ) 68 { 69 if( S->last_node ) blake2b_clear_lastnode( S ); 70 71 S->f[0] = 0ULL; 72 return 0; 73 } 74 75 static inline int blake2b_increment_counter( blake2b_state *S, const uint64_t inc ) 76 { 77 S->t[0] += inc; 78 S->t[1] += ( S->t[0] < inc ); 79 return 0; 80 } 81 82 83 84 // Parameter-related functions 85 static inline int blake2b_param_set_digest_length( blake2b_param *P, const uint8_t digest_length ) 86 { 87 P->digest_length = digest_length; 88 return 0; 89 } 90 91 static inline int blake2b_param_set_fanout( blake2b_param *P, const uint8_t fanout ) 92 { 93 P->fanout = fanout; 94 return 0; 95 } 96 97 static inline int blake2b_param_set_max_depth( blake2b_param *P, const uint8_t depth ) 98 { 99 P->depth = depth; 100 return 0; 101 } 102 103 static inline int blake2b_param_set_leaf_length( blake2b_param *P, const uint32_t leaf_length ) 104 { 105 store32( &P->leaf_length, leaf_length ); 106 return 0; 107 } 108 109 static inline int blake2b_param_set_node_offset( blake2b_param *P, const uint64_t node_offset ) 110 { 111 store64( &P->node_offset, node_offset ); 112 return 0; 113 } 114 115 static inline int blake2b_param_set_node_depth( blake2b_param *P, const uint8_t node_depth ) 116 { 117 P->node_depth = node_depth; 118 return 0; 119 } 120 121 static inline int blake2b_param_set_inner_length( blake2b_param *P, const uint8_t inner_length ) 122 { 123 P->inner_length = inner_length; 124 return 0; 125 } 126 127 static inline int blake2b_param_set_salt( blake2b_param *P, const uint8_t salt[BLAKE2B_SALTBYTES] ) 128 { 129 memcpy( P->salt, salt, BLAKE2B_SALTBYTES ); 130 return 0; 131 } 132 133 static inline int blake2b_param_set_personal( blake2b_param *P, const uint8_t personal[BLAKE2B_PERSONALBYTES] ) 134 { 135 memcpy( P->personal, personal, BLAKE2B_PERSONALBYTES ); 136 return 0; 137 } 138 139 static inline int blake2b_init0( blake2b_state *S ) 140 { 141 memset( S, 0, sizeof( blake2b_state ) ); 142 143 for( int i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i]; 144 145 return 0; 146 } 147 148 #define blake2b_init BLAKE2_IMPL_NAME(blake2b_init) 149 #define blake2b_init_param BLAKE2_IMPL_NAME(blake2b_init_param) 150 #define blake2b_init_key BLAKE2_IMPL_NAME(blake2b_init_key) 151 #define blake2b_update BLAKE2_IMPL_NAME(blake2b_update) 152 #define blake2b_final BLAKE2_IMPL_NAME(blake2b_final) 153 #define blake2b BLAKE2_IMPL_NAME(blake2b) 154 155 #if defined(__cplusplus) 156 extern "C" { 157 #endif 158 int blake2b_init( blake2b_state *S, size_t outlen ); 159 int blake2b_init_param( blake2b_state *S, const blake2b_param *P ); 160 int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen ); 161 int blake2b_update( blake2b_state *S, const uint8_t *in, size_t inlen ); 162 int blake2b_final( blake2b_state *S, uint8_t *out, size_t outlen ); 163 int blake2b( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen ); 164 #if defined(__cplusplus) 165 } 166 #endif 167 168 /* init xors IV with input parameter block */ 169 int blake2b_init_param( blake2b_state *S, const blake2b_param *P ) 170 { 171 blake2b_init0( S ); 172 uint8_t *p = ( uint8_t * )( P ); 173 174 /* IV XOR ParamBlock */ 175 for( size_t i = 0; i < 8; ++i ) 176 S->h[i] ^= load64( p + sizeof( S->h[i] ) * i ); 177 178 S->outlen = P->digest_length; 179 return 0; 180 } 181 182 183 184 int blake2b_init( blake2b_state *S, size_t outlen ) 185 { 186 blake2b_param P[1]; 187 188 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1; 189 190 P->digest_length = ( uint8_t ) outlen; 191 P->key_length = 0; 192 P->fanout = 1; 193 P->depth = 1; 194 store32( &P->leaf_length, 0 ); 195 store64( &P->node_offset, 0 ); 196 P->node_depth = 0; 197 P->inner_length = 0; 198 memset( P->reserved, 0, sizeof( P->reserved ) ); 199 memset( P->salt, 0, sizeof( P->salt ) ); 200 memset( P->personal, 0, sizeof( P->personal ) ); 201 return blake2b_init_param( S, P ); 202 } 203 204 205 int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen ) 206 { 207 blake2b_param P[1]; 208 209 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1; 210 211 if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1; 212 213 P->digest_length = ( uint8_t ) outlen; 214 P->key_length = ( uint8_t ) keylen; 215 P->fanout = 1; 216 P->depth = 1; 217 store32( &P->leaf_length, 0 ); 218 store64( &P->node_offset, 0 ); 219 P->node_depth = 0; 220 P->inner_length = 0; 221 memset( P->reserved, 0, sizeof( P->reserved ) ); 222 memset( P->salt, 0, sizeof( P->salt ) ); 223 memset( P->personal, 0, sizeof( P->personal ) ); 224 225 if( blake2b_init_param( S, P ) < 0 ) return -1; 226 227 { 228 uint8_t block[BLAKE2B_BLOCKBYTES]; 229 memset( block, 0, BLAKE2B_BLOCKBYTES ); 230 memcpy( block, key, keylen ); 231 blake2b_update( S, block, BLAKE2B_BLOCKBYTES ); 232 secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */ 233 } 234 return 0; 235 } 236 237 static int blake2b_compress( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] ) 238 { 239 uint64_t m[16]; 240 uint64_t v[16]; 241 size_t i; 242 243 for( i = 0; i < 16; ++i ) 244 m[i] = load64( block + i * sizeof( m[i] ) ); 245 246 for( i = 0; i < 8; ++i ) 247 v[i] = S->h[i]; 248 249 v[ 8] = blake2b_IV[0]; 250 v[ 9] = blake2b_IV[1]; 251 v[10] = blake2b_IV[2]; 252 v[11] = blake2b_IV[3]; 253 v[12] = S->t[0] ^ blake2b_IV[4]; 254 v[13] = S->t[1] ^ blake2b_IV[5]; 255 v[14] = S->f[0] ^ blake2b_IV[6]; 256 v[15] = S->f[1] ^ blake2b_IV[7]; 257 #define G(r,i,a,b,c,d) \ 258 do { \ 259 a = a + b + m[blake2b_sigma[r][2*i+0]]; \ 260 d = rotr64(d ^ a, 32); \ 261 c = c + d; \ 262 b = rotr64(b ^ c, 24); \ 263 a = a + b + m[blake2b_sigma[r][2*i+1]]; \ 264 d = rotr64(d ^ a, 16); \ 265 c = c + d; \ 266 b = rotr64(b ^ c, 63); \ 267 } while(0) 268 #define ROUND(r) \ 269 do { \ 270 G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \ 271 G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \ 272 G(r,2,v[ 2],v[ 6],v[10],v[14]); \ 273 G(r,3,v[ 3],v[ 7],v[11],v[15]); \ 274 G(r,4,v[ 0],v[ 5],v[10],v[15]); \ 275 G(r,5,v[ 1],v[ 6],v[11],v[12]); \ 276 G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \ 277 G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \ 278 } while(0) 279 ROUND( 0 ); 280 ROUND( 1 ); 281 ROUND( 2 ); 282 ROUND( 3 ); 283 ROUND( 4 ); 284 ROUND( 5 ); 285 ROUND( 6 ); 286 ROUND( 7 ); 287 ROUND( 8 ); 288 ROUND( 9 ); 289 ROUND( 10 ); 290 ROUND( 11 ); 291 292 for( i = 0; i < 8; ++i ) 293 S->h[i] = S->h[i] ^ v[i] ^ v[i + 8]; 294 295 #undef G 296 #undef ROUND 297 return 0; 298 } 299 300 301 int blake2b_update( blake2b_state *S, const uint8_t *in, size_t inlen ) 302 { 303 while( inlen > 0 ) 304 { 305 uint32_t left = S->buflen; 306 uint32_t fill = 2 * BLAKE2B_BLOCKBYTES - left; 307 308 if( inlen > fill ) 309 { 310 memcpy( S->buf + left, in, fill ); // Fill buffer 311 S->buflen += fill; 312 blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); 313 blake2b_compress( S, S->buf ); // Compress 314 memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); // Shift buffer left 315 S->buflen -= BLAKE2B_BLOCKBYTES; 316 in += fill; 317 inlen -= fill; 318 } 319 else // inlen <= fill 320 { 321 memcpy( S->buf + left, in, inlen ); 322 S->buflen += ( uint32_t ) inlen; // Be lazy, do not compress 323 in += inlen; 324 inlen -= inlen; 325 } 326 } 327 328 return 0; 329 } 330 331 int blake2b_final( blake2b_state *S, uint8_t *out, size_t outlen ) 332 { 333 uint8_t buffer[BLAKE2B_OUTBYTES]; 334 size_t i; 335 336 if(S->outlen != outlen) return -1; 337 338 if( S->buflen > BLAKE2B_BLOCKBYTES ) 339 { 340 blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); 341 blake2b_compress( S, S->buf ); 342 S->buflen -= BLAKE2B_BLOCKBYTES; 343 memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, S->buflen ); 344 } 345 346 blake2b_increment_counter( S, S->buflen ); 347 blake2b_set_lastblock( S ); 348 memset( S->buf + S->buflen, 0, 2 * BLAKE2B_BLOCKBYTES - S->buflen ); /* Padding */ 349 blake2b_compress( S, S->buf ); 350 351 for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */ 352 store64( buffer + sizeof( S->h[i] ) * i, S->h[i] ); 353 354 memcpy( out, buffer, outlen ); 355 return 0; 356 } 357 358 int blake2b( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen ) 359 { 360 blake2b_state S[1]; 361 362 /* Verify parameters */ 363 if ( NULL == in && inlen > 0 ) return -1; 364 365 if ( NULL == out ) return -1; 366 367 if( NULL == key && keylen > 0 ) return -1; 368 369 if( !outlen || outlen > BLAKE2B_OUTBYTES ) return -1; 370 371 if( keylen > BLAKE2B_KEYBYTES ) return -1; 372 373 if( keylen > 0 ) 374 { 375 if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1; 376 } 377 else 378 { 379 if( blake2b_init( S, outlen ) < 0 ) return -1; 380 } 381 382 if( blake2b_update( S, ( uint8_t * )in, inlen ) < 0 ) return -1; 383 return blake2b_final( S, out, outlen ); 384 } 385 386 387