Xuyi Wang / wolfSSL

Dependents:   OS

Committer:
sPymbed
Date:
Tue Nov 19 14:32:16 2019 +0000
Revision:
16:048e5e270a58
Parent:
15:117db924cf7c
working ssl

Who changed what in which revision?

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