Fork of CyaSSL for my specific settings

Dependents:   CyaSSL_Example

Fork of CyaSSL by wolf SSL

Committer:
d0773d
Date:
Tue Mar 03 22:52:52 2015 +0000
Revision:
4:28ac50e1d49c
Parent:
0:1239e9b70ca2
CyaSSL example

Who changed what in which revision?

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