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