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