wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

Committer:
wolfSSL
Date:
Tue May 02 08:44:47 2017 +0000
Revision:
7:481bce714567
wolfSSL3.10.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 7:481bce714567 1 /*
wolfSSL 7:481bce714567 2 BLAKE2 reference source code package - reference C implementations
wolfSSL 7:481bce714567 3
wolfSSL 7:481bce714567 4 Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
wolfSSL 7:481bce714567 5
wolfSSL 7:481bce714567 6 To the extent possible under law, the author(s) have dedicated all copyright
wolfSSL 7:481bce714567 7 and related and neighboring rights to this software to the public domain
wolfSSL 7:481bce714567 8 worldwide. This software is distributed without any warranty.
wolfSSL 7:481bce714567 9
wolfSSL 7:481bce714567 10 You should have received a copy of the CC0 Public Domain Dedication along with
wolfSSL 7:481bce714567 11 this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
wolfSSL 7:481bce714567 12 */
wolfSSL 7:481bce714567 13 /* blake2b.c
wolfSSL 7:481bce714567 14 *
wolfSSL 7:481bce714567 15 * Copyright (C) 2006-2016 wolfSSL Inc.
wolfSSL 7:481bce714567 16 *
wolfSSL 7:481bce714567 17 * This file is part of wolfSSL.
wolfSSL 7:481bce714567 18 *
wolfSSL 7:481bce714567 19 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 7:481bce714567 20 * it under the terms of the GNU General Public License as published by
wolfSSL 7:481bce714567 21 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 7:481bce714567 22 * (at your option) any later version.
wolfSSL 7:481bce714567 23 *
wolfSSL 7:481bce714567 24 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 7:481bce714567 25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 7:481bce714567 26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 7:481bce714567 27 * GNU General Public License for more details.
wolfSSL 7:481bce714567 28 *
wolfSSL 7:481bce714567 29 * You should have received a copy of the GNU General Public License
wolfSSL 7:481bce714567 30 * along with this program; if not, write to the Free Software
wolfSSL 7:481bce714567 31 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 7:481bce714567 32 */
wolfSSL 7:481bce714567 33
wolfSSL 7:481bce714567 34
wolfSSL 7:481bce714567 35
wolfSSL 7:481bce714567 36 #ifdef HAVE_CONFIG_H
wolfSSL 7:481bce714567 37 #include <config.h>
wolfSSL 7:481bce714567 38 #endif
wolfSSL 7:481bce714567 39
wolfSSL 7:481bce714567 40 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 7:481bce714567 41
wolfSSL 7:481bce714567 42 #ifdef HAVE_BLAKE2
wolfSSL 7:481bce714567 43
wolfSSL 7:481bce714567 44 #include <wolfssl/wolfcrypt/blake2.h>
wolfSSL 7:481bce714567 45 #include <wolfssl/wolfcrypt/blake2-impl.h>
wolfSSL 7:481bce714567 46
wolfSSL 7:481bce714567 47
wolfSSL 7:481bce714567 48 static const word64 blake2b_IV[8] =
wolfSSL 7:481bce714567 49 {
wolfSSL 7:481bce714567 50 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
wolfSSL 7:481bce714567 51 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
wolfSSL 7:481bce714567 52 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
wolfSSL 7:481bce714567 53 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
wolfSSL 7:481bce714567 54 };
wolfSSL 7:481bce714567 55
wolfSSL 7:481bce714567 56 static const byte blake2b_sigma[12][16] =
wolfSSL 7:481bce714567 57 {
wolfSSL 7:481bce714567 58 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
wolfSSL 7:481bce714567 59 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
wolfSSL 7:481bce714567 60 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
wolfSSL 7:481bce714567 61 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
wolfSSL 7:481bce714567 62 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
wolfSSL 7:481bce714567 63 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
wolfSSL 7:481bce714567 64 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
wolfSSL 7:481bce714567 65 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
wolfSSL 7:481bce714567 66 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
wolfSSL 7:481bce714567 67 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
wolfSSL 7:481bce714567 68 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
wolfSSL 7:481bce714567 69 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
wolfSSL 7:481bce714567 70 };
wolfSSL 7:481bce714567 71
wolfSSL 7:481bce714567 72
wolfSSL 7:481bce714567 73 static INLINE int blake2b_set_lastnode( blake2b_state *S )
wolfSSL 7:481bce714567 74 {
wolfSSL 7:481bce714567 75 S->f[1] = ~0ULL;
wolfSSL 7:481bce714567 76 return 0;
wolfSSL 7:481bce714567 77 }
wolfSSL 7:481bce714567 78
wolfSSL 7:481bce714567 79 /* Some helper functions, not necessarily useful */
wolfSSL 7:481bce714567 80 static INLINE int blake2b_set_lastblock( blake2b_state *S )
wolfSSL 7:481bce714567 81 {
wolfSSL 7:481bce714567 82 if( S->last_node ) blake2b_set_lastnode( S );
wolfSSL 7:481bce714567 83
wolfSSL 7:481bce714567 84 S->f[0] = ~0ULL;
wolfSSL 7:481bce714567 85 return 0;
wolfSSL 7:481bce714567 86 }
wolfSSL 7:481bce714567 87
wolfSSL 7:481bce714567 88 static INLINE int blake2b_increment_counter( blake2b_state *S, const word64
wolfSSL 7:481bce714567 89 inc )
wolfSSL 7:481bce714567 90 {
wolfSSL 7:481bce714567 91 S->t[0] += inc;
wolfSSL 7:481bce714567 92 S->t[1] += ( S->t[0] < inc );
wolfSSL 7:481bce714567 93 return 0;
wolfSSL 7:481bce714567 94 }
wolfSSL 7:481bce714567 95
wolfSSL 7:481bce714567 96 static INLINE int blake2b_init0( blake2b_state *S )
wolfSSL 7:481bce714567 97 {
wolfSSL 7:481bce714567 98 int i;
wolfSSL 7:481bce714567 99 XMEMSET( S, 0, sizeof( blake2b_state ) );
wolfSSL 7:481bce714567 100
wolfSSL 7:481bce714567 101 for( i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i];
wolfSSL 7:481bce714567 102
wolfSSL 7:481bce714567 103 return 0;
wolfSSL 7:481bce714567 104 }
wolfSSL 7:481bce714567 105
wolfSSL 7:481bce714567 106 /* init xors IV with input parameter block */
wolfSSL 7:481bce714567 107 int blake2b_init_param( blake2b_state *S, const blake2b_param *P )
wolfSSL 7:481bce714567 108 {
wolfSSL 7:481bce714567 109 word32 i;
wolfSSL 7:481bce714567 110 byte *p ;
wolfSSL 7:481bce714567 111 blake2b_init0( S );
wolfSSL 7:481bce714567 112 p = ( byte * )( P );
wolfSSL 7:481bce714567 113
wolfSSL 7:481bce714567 114 /* IV XOR ParamBlock */
wolfSSL 7:481bce714567 115 for( i = 0; i < 8; ++i )
wolfSSL 7:481bce714567 116 S->h[i] ^= load64( p + sizeof( S->h[i] ) * i );
wolfSSL 7:481bce714567 117
wolfSSL 7:481bce714567 118 return 0;
wolfSSL 7:481bce714567 119 }
wolfSSL 7:481bce714567 120
wolfSSL 7:481bce714567 121
wolfSSL 7:481bce714567 122
wolfSSL 7:481bce714567 123 int blake2b_init( blake2b_state *S, const byte outlen )
wolfSSL 7:481bce714567 124 {
wolfSSL 7:481bce714567 125 blake2b_param P[1];
wolfSSL 7:481bce714567 126
wolfSSL 7:481bce714567 127 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
wolfSSL 7:481bce714567 128
wolfSSL 7:481bce714567 129 P->digest_length = outlen;
wolfSSL 7:481bce714567 130 P->key_length = 0;
wolfSSL 7:481bce714567 131 P->fanout = 1;
wolfSSL 7:481bce714567 132 P->depth = 1;
wolfSSL 7:481bce714567 133 store32( &P->leaf_length, 0 );
wolfSSL 7:481bce714567 134 store64( &P->node_offset, 0 );
wolfSSL 7:481bce714567 135 P->node_depth = 0;
wolfSSL 7:481bce714567 136 P->inner_length = 0;
wolfSSL 7:481bce714567 137 XMEMSET( P->reserved, 0, sizeof( P->reserved ) );
wolfSSL 7:481bce714567 138 XMEMSET( P->salt, 0, sizeof( P->salt ) );
wolfSSL 7:481bce714567 139 XMEMSET( P->personal, 0, sizeof( P->personal ) );
wolfSSL 7:481bce714567 140 return blake2b_init_param( S, P );
wolfSSL 7:481bce714567 141 }
wolfSSL 7:481bce714567 142
wolfSSL 7:481bce714567 143
wolfSSL 7:481bce714567 144 int blake2b_init_key( blake2b_state *S, const byte outlen, const void *key,
wolfSSL 7:481bce714567 145 const byte keylen )
wolfSSL 7:481bce714567 146 {
wolfSSL 7:481bce714567 147 blake2b_param P[1];
wolfSSL 7:481bce714567 148
wolfSSL 7:481bce714567 149 if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
wolfSSL 7:481bce714567 150
wolfSSL 7:481bce714567 151 if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1;
wolfSSL 7:481bce714567 152
wolfSSL 7:481bce714567 153 P->digest_length = outlen;
wolfSSL 7:481bce714567 154 P->key_length = keylen;
wolfSSL 7:481bce714567 155 P->fanout = 1;
wolfSSL 7:481bce714567 156 P->depth = 1;
wolfSSL 7:481bce714567 157 store32( &P->leaf_length, 0 );
wolfSSL 7:481bce714567 158 store64( &P->node_offset, 0 );
wolfSSL 7:481bce714567 159 P->node_depth = 0;
wolfSSL 7:481bce714567 160 P->inner_length = 0;
wolfSSL 7:481bce714567 161 XMEMSET( P->reserved, 0, sizeof( P->reserved ) );
wolfSSL 7:481bce714567 162 XMEMSET( P->salt, 0, sizeof( P->salt ) );
wolfSSL 7:481bce714567 163 XMEMSET( P->personal, 0, sizeof( P->personal ) );
wolfSSL 7:481bce714567 164
wolfSSL 7:481bce714567 165 if( blake2b_init_param( S, P ) < 0 ) return -1;
wolfSSL 7:481bce714567 166
wolfSSL 7:481bce714567 167 {
wolfSSL 7:481bce714567 168 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 169 byte* block;
wolfSSL 7:481bce714567 170
wolfSSL 7:481bce714567 171 block = (byte*)XMALLOC(BLAKE2B_BLOCKBYTES, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 172
wolfSSL 7:481bce714567 173 if ( block == NULL ) return -1;
wolfSSL 7:481bce714567 174 #else
wolfSSL 7:481bce714567 175 byte block[BLAKE2B_BLOCKBYTES];
wolfSSL 7:481bce714567 176 #endif
wolfSSL 7:481bce714567 177
wolfSSL 7:481bce714567 178 XMEMSET( block, 0, BLAKE2B_BLOCKBYTES );
wolfSSL 7:481bce714567 179 XMEMCPY( block, key, keylen );
wolfSSL 7:481bce714567 180 blake2b_update( S, block, BLAKE2B_BLOCKBYTES );
wolfSSL 7:481bce714567 181 secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from */
wolfSSL 7:481bce714567 182 /* memory */
wolfSSL 7:481bce714567 183
wolfSSL 7:481bce714567 184 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 185 XFREE(block, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 186 #endif
wolfSSL 7:481bce714567 187 }
wolfSSL 7:481bce714567 188 return 0;
wolfSSL 7:481bce714567 189 }
wolfSSL 7:481bce714567 190
wolfSSL 7:481bce714567 191 static int blake2b_compress( blake2b_state *S,
wolfSSL 7:481bce714567 192 const byte block[BLAKE2B_BLOCKBYTES] )
wolfSSL 7:481bce714567 193 {
wolfSSL 7:481bce714567 194 int i;
wolfSSL 7:481bce714567 195
wolfSSL 7:481bce714567 196 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 197 word64* m;
wolfSSL 7:481bce714567 198 word64* v;
wolfSSL 7:481bce714567 199
wolfSSL 7:481bce714567 200 m = (word64*)XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 201
wolfSSL 7:481bce714567 202 if ( m == NULL ) return -1;
wolfSSL 7:481bce714567 203
wolfSSL 7:481bce714567 204 v = (word64*)XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 205
wolfSSL 7:481bce714567 206 if ( v == NULL )
wolfSSL 7:481bce714567 207 {
wolfSSL 7:481bce714567 208 XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 209 return -1;
wolfSSL 7:481bce714567 210 }
wolfSSL 7:481bce714567 211 #else
wolfSSL 7:481bce714567 212 word64 m[16];
wolfSSL 7:481bce714567 213 word64 v[16];
wolfSSL 7:481bce714567 214 #endif
wolfSSL 7:481bce714567 215
wolfSSL 7:481bce714567 216 for( i = 0; i < 16; ++i )
wolfSSL 7:481bce714567 217 m[i] = load64( block + i * sizeof( m[i] ) );
wolfSSL 7:481bce714567 218
wolfSSL 7:481bce714567 219 for( i = 0; i < 8; ++i )
wolfSSL 7:481bce714567 220 v[i] = S->h[i];
wolfSSL 7:481bce714567 221
wolfSSL 7:481bce714567 222 v[ 8] = blake2b_IV[0];
wolfSSL 7:481bce714567 223 v[ 9] = blake2b_IV[1];
wolfSSL 7:481bce714567 224 v[10] = blake2b_IV[2];
wolfSSL 7:481bce714567 225 v[11] = blake2b_IV[3];
wolfSSL 7:481bce714567 226 v[12] = S->t[0] ^ blake2b_IV[4];
wolfSSL 7:481bce714567 227 v[13] = S->t[1] ^ blake2b_IV[5];
wolfSSL 7:481bce714567 228 v[14] = S->f[0] ^ blake2b_IV[6];
wolfSSL 7:481bce714567 229 v[15] = S->f[1] ^ blake2b_IV[7];
wolfSSL 7:481bce714567 230 #define G(r,i,a,b,c,d) \
wolfSSL 7:481bce714567 231 do { \
wolfSSL 7:481bce714567 232 a = a + b + m[blake2b_sigma[r][2*i+0]]; \
wolfSSL 7:481bce714567 233 d = rotr64(d ^ a, 32); \
wolfSSL 7:481bce714567 234 c = c + d; \
wolfSSL 7:481bce714567 235 b = rotr64(b ^ c, 24); \
wolfSSL 7:481bce714567 236 a = a + b + m[blake2b_sigma[r][2*i+1]]; \
wolfSSL 7:481bce714567 237 d = rotr64(d ^ a, 16); \
wolfSSL 7:481bce714567 238 c = c + d; \
wolfSSL 7:481bce714567 239 b = rotr64(b ^ c, 63); \
wolfSSL 7:481bce714567 240 } while(0)
wolfSSL 7:481bce714567 241 #define ROUND(r) \
wolfSSL 7:481bce714567 242 do { \
wolfSSL 7:481bce714567 243 G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
wolfSSL 7:481bce714567 244 G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
wolfSSL 7:481bce714567 245 G(r,2,v[ 2],v[ 6],v[10],v[14]); \
wolfSSL 7:481bce714567 246 G(r,3,v[ 3],v[ 7],v[11],v[15]); \
wolfSSL 7:481bce714567 247 G(r,4,v[ 0],v[ 5],v[10],v[15]); \
wolfSSL 7:481bce714567 248 G(r,5,v[ 1],v[ 6],v[11],v[12]); \
wolfSSL 7:481bce714567 249 G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
wolfSSL 7:481bce714567 250 G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
wolfSSL 7:481bce714567 251 } while(0)
wolfSSL 7:481bce714567 252 ROUND( 0 );
wolfSSL 7:481bce714567 253 ROUND( 1 );
wolfSSL 7:481bce714567 254 ROUND( 2 );
wolfSSL 7:481bce714567 255 ROUND( 3 );
wolfSSL 7:481bce714567 256 ROUND( 4 );
wolfSSL 7:481bce714567 257 ROUND( 5 );
wolfSSL 7:481bce714567 258 ROUND( 6 );
wolfSSL 7:481bce714567 259 ROUND( 7 );
wolfSSL 7:481bce714567 260 ROUND( 8 );
wolfSSL 7:481bce714567 261 ROUND( 9 );
wolfSSL 7:481bce714567 262 ROUND( 10 );
wolfSSL 7:481bce714567 263 ROUND( 11 );
wolfSSL 7:481bce714567 264
wolfSSL 7:481bce714567 265 for( i = 0; i < 8; ++i )
wolfSSL 7:481bce714567 266 S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
wolfSSL 7:481bce714567 267
wolfSSL 7:481bce714567 268 #undef G
wolfSSL 7:481bce714567 269 #undef ROUND
wolfSSL 7:481bce714567 270
wolfSSL 7:481bce714567 271 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 7:481bce714567 272 XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 273 XFREE(v, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 274 #endif
wolfSSL 7:481bce714567 275
wolfSSL 7:481bce714567 276 return 0;
wolfSSL 7:481bce714567 277 }
wolfSSL 7:481bce714567 278
wolfSSL 7:481bce714567 279 /* inlen now in bytes */
wolfSSL 7:481bce714567 280 int blake2b_update( blake2b_state *S, const byte *in, word64 inlen )
wolfSSL 7:481bce714567 281 {
wolfSSL 7:481bce714567 282 while( inlen > 0 )
wolfSSL 7:481bce714567 283 {
wolfSSL 7:481bce714567 284 word64 left = S->buflen;
wolfSSL 7:481bce714567 285 word64 fill = 2 * BLAKE2B_BLOCKBYTES - left;
wolfSSL 7:481bce714567 286
wolfSSL 7:481bce714567 287 if( inlen > fill )
wolfSSL 7:481bce714567 288 {
wolfSSL 7:481bce714567 289 XMEMCPY( S->buf + left, in, (wolfssl_word)fill ); /* Fill buffer */
wolfSSL 7:481bce714567 290 S->buflen += fill;
wolfSSL 7:481bce714567 291 blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
wolfSSL 7:481bce714567 292
wolfSSL 7:481bce714567 293 if ( blake2b_compress( S, S->buf ) < 0 ) return -1; /* Compress */
wolfSSL 7:481bce714567 294
wolfSSL 7:481bce714567 295 XMEMCPY( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES );
wolfSSL 7:481bce714567 296 /* Shift buffer left */
wolfSSL 7:481bce714567 297 S->buflen -= BLAKE2B_BLOCKBYTES;
wolfSSL 7:481bce714567 298 in += fill;
wolfSSL 7:481bce714567 299 inlen -= fill;
wolfSSL 7:481bce714567 300 }
wolfSSL 7:481bce714567 301 else /* inlen <= fill */
wolfSSL 7:481bce714567 302 {
wolfSSL 7:481bce714567 303 XMEMCPY( S->buf + left, in, (wolfssl_word)inlen );
wolfSSL 7:481bce714567 304 S->buflen += inlen; /* Be lazy, do not compress */
wolfSSL 7:481bce714567 305 in += inlen;
wolfSSL 7:481bce714567 306 inlen -= inlen;
wolfSSL 7:481bce714567 307 }
wolfSSL 7:481bce714567 308 }
wolfSSL 7:481bce714567 309
wolfSSL 7:481bce714567 310 return 0;
wolfSSL 7:481bce714567 311 }
wolfSSL 7:481bce714567 312
wolfSSL 7:481bce714567 313 /* Is this correct? */
wolfSSL 7:481bce714567 314 int blake2b_final( blake2b_state *S, byte *out, byte outlen )
wolfSSL 7:481bce714567 315 {
wolfSSL 7:481bce714567 316 byte buffer[BLAKE2B_OUTBYTES];
wolfSSL 7:481bce714567 317 int i;
wolfSSL 7:481bce714567 318
wolfSSL 7:481bce714567 319 if( S->buflen > BLAKE2B_BLOCKBYTES )
wolfSSL 7:481bce714567 320 {
wolfSSL 7:481bce714567 321 blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
wolfSSL 7:481bce714567 322
wolfSSL 7:481bce714567 323 if ( blake2b_compress( S, S->buf ) < 0 ) return -1;
wolfSSL 7:481bce714567 324
wolfSSL 7:481bce714567 325 S->buflen -= BLAKE2B_BLOCKBYTES;
wolfSSL 7:481bce714567 326 XMEMCPY( S->buf, S->buf + BLAKE2B_BLOCKBYTES, (wolfssl_word)S->buflen );
wolfSSL 7:481bce714567 327 }
wolfSSL 7:481bce714567 328
wolfSSL 7:481bce714567 329 blake2b_increment_counter( S, S->buflen );
wolfSSL 7:481bce714567 330 blake2b_set_lastblock( S );
wolfSSL 7:481bce714567 331 XMEMSET( S->buf + S->buflen, 0, (wolfssl_word)(2 * BLAKE2B_BLOCKBYTES - S->buflen) );
wolfSSL 7:481bce714567 332 /* Padding */
wolfSSL 7:481bce714567 333 if ( blake2b_compress( S, S->buf ) < 0 ) return -1;
wolfSSL 7:481bce714567 334
wolfSSL 7:481bce714567 335 for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
wolfSSL 7:481bce714567 336 store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
wolfSSL 7:481bce714567 337
wolfSSL 7:481bce714567 338 XMEMCPY( out, buffer, outlen );
wolfSSL 7:481bce714567 339 return 0;
wolfSSL 7:481bce714567 340 }
wolfSSL 7:481bce714567 341
wolfSSL 7:481bce714567 342 /* inlen, at least, should be word64. Others can be size_t. */
wolfSSL 7:481bce714567 343 int blake2b( byte *out, const void *in, const void *key, const byte outlen,
wolfSSL 7:481bce714567 344 const word64 inlen, byte keylen )
wolfSSL 7:481bce714567 345 {
wolfSSL 7:481bce714567 346 blake2b_state S[1];
wolfSSL 7:481bce714567 347
wolfSSL 7:481bce714567 348 /* Verify parameters */
wolfSSL 7:481bce714567 349 if ( NULL == in ) return -1;
wolfSSL 7:481bce714567 350
wolfSSL 7:481bce714567 351 if ( NULL == out ) return -1;
wolfSSL 7:481bce714567 352
wolfSSL 7:481bce714567 353 if( NULL == key ) keylen = 0;
wolfSSL 7:481bce714567 354
wolfSSL 7:481bce714567 355 if( keylen > 0 )
wolfSSL 7:481bce714567 356 {
wolfSSL 7:481bce714567 357 if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1;
wolfSSL 7:481bce714567 358 }
wolfSSL 7:481bce714567 359 else
wolfSSL 7:481bce714567 360 {
wolfSSL 7:481bce714567 361 if( blake2b_init( S, outlen ) < 0 ) return -1;
wolfSSL 7:481bce714567 362 }
wolfSSL 7:481bce714567 363
wolfSSL 7:481bce714567 364 if ( blake2b_update( S, ( byte * )in, inlen ) < 0) return -1;
wolfSSL 7:481bce714567 365
wolfSSL 7:481bce714567 366 return blake2b_final( S, out, outlen );
wolfSSL 7:481bce714567 367 }
wolfSSL 7:481bce714567 368
wolfSSL 7:481bce714567 369 #if defined(BLAKE2B_SELFTEST)
wolfSSL 7:481bce714567 370 #include <string.h>
wolfSSL 7:481bce714567 371 #include "blake2-kat.h"
wolfSSL 7:481bce714567 372 int main( int argc, char **argv )
wolfSSL 7:481bce714567 373 {
wolfSSL 7:481bce714567 374 byte key[BLAKE2B_KEYBYTES];
wolfSSL 7:481bce714567 375 byte buf[KAT_LENGTH];
wolfSSL 7:481bce714567 376
wolfSSL 7:481bce714567 377 for( word32 i = 0; i < BLAKE2B_KEYBYTES; ++i )
wolfSSL 7:481bce714567 378 key[i] = ( byte )i;
wolfSSL 7:481bce714567 379
wolfSSL 7:481bce714567 380 for( word32 i = 0; i < KAT_LENGTH; ++i )
wolfSSL 7:481bce714567 381 buf[i] = ( byte )i;
wolfSSL 7:481bce714567 382
wolfSSL 7:481bce714567 383 for( word32 i = 0; i < KAT_LENGTH; ++i )
wolfSSL 7:481bce714567 384 {
wolfSSL 7:481bce714567 385 byte hash[BLAKE2B_OUTBYTES];
wolfSSL 7:481bce714567 386 if ( blake2b( hash, buf, key, BLAKE2B_OUTBYTES, i, BLAKE2B_KEYBYTES ) < 0 )
wolfSSL 7:481bce714567 387 {
wolfSSL 7:481bce714567 388 puts( "error" );
wolfSSL 7:481bce714567 389 return -1;
wolfSSL 7:481bce714567 390 }
wolfSSL 7:481bce714567 391
wolfSSL 7:481bce714567 392 if( 0 != XMEMCMP( hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES ) )
wolfSSL 7:481bce714567 393 {
wolfSSL 7:481bce714567 394 puts( "error" );
wolfSSL 7:481bce714567 395 return -1;
wolfSSL 7:481bce714567 396 }
wolfSSL 7:481bce714567 397 }
wolfSSL 7:481bce714567 398
wolfSSL 7:481bce714567 399 puts( "ok" );
wolfSSL 7:481bce714567 400 return 0;
wolfSSL 7:481bce714567 401 }
wolfSSL 7:481bce714567 402 #endif
wolfSSL 7:481bce714567 403
wolfSSL 7:481bce714567 404
wolfSSL 7:481bce714567 405 /* wolfCrypt API */
wolfSSL 7:481bce714567 406
wolfSSL 7:481bce714567 407 /* Init Blake2b digest, track size in case final doesn't want to "remember" */
wolfSSL 7:481bce714567 408 int wc_InitBlake2b(Blake2b* b2b, word32 digestSz)
wolfSSL 7:481bce714567 409 {
wolfSSL 7:481bce714567 410 b2b->digestSz = digestSz;
wolfSSL 7:481bce714567 411
wolfSSL 7:481bce714567 412 return blake2b_init(b2b->S, (byte)digestSz);
wolfSSL 7:481bce714567 413 }
wolfSSL 7:481bce714567 414
wolfSSL 7:481bce714567 415
wolfSSL 7:481bce714567 416 /* Blake2b Update */
wolfSSL 7:481bce714567 417 int wc_Blake2bUpdate(Blake2b* b2b, const byte* data, word32 sz)
wolfSSL 7:481bce714567 418 {
wolfSSL 7:481bce714567 419 return blake2b_update(b2b->S, data, sz);
wolfSSL 7:481bce714567 420 }
wolfSSL 7:481bce714567 421
wolfSSL 7:481bce714567 422
wolfSSL 7:481bce714567 423 /* Blake2b Final, if pass in zero size we use init digestSz */
wolfSSL 7:481bce714567 424 int wc_Blake2bFinal(Blake2b* b2b, byte* final, word32 requestSz)
wolfSSL 7:481bce714567 425 {
wolfSSL 7:481bce714567 426 word32 sz = requestSz ? requestSz : b2b->digestSz;
wolfSSL 7:481bce714567 427
wolfSSL 7:481bce714567 428 return blake2b_final(b2b->S, final, (byte)sz);
wolfSSL 7:481bce714567 429 }
wolfSSL 7:481bce714567 430
wolfSSL 7:481bce714567 431
wolfSSL 7:481bce714567 432 /* end CTaoCrypt API */
wolfSSL 7:481bce714567 433
wolfSSL 7:481bce714567 434 #endif /* HAVE_BLAKE2 */
wolfSSL 7:481bce714567 435
wolfSSL 7:481bce714567 436