wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Tue Aug 22 10:48:22 2017 +0000
Revision:
13:f67a6c6013ca
wolfSSL3.12.0 with TLS1.3

Who changed what in which revision?

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