our type changes

pull/1/head
toddouska 2013-02-28 17:51:35 -08:00
parent 3319ed9921
commit 98e766e770
3 changed files with 198 additions and 188 deletions

View File

@ -39,12 +39,8 @@
#include <cyassl/ctaocrypt/blake2.h> #include <cyassl/ctaocrypt/blake2.h>
#include <cyassl/ctaocrypt/blake2-impl.h> #include <cyassl/ctaocrypt/blake2-impl.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
static const word64 blake2b_IV[8] =
static const uint64_t blake2b_IV[8] =
{ {
0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
@ -52,7 +48,7 @@ static const uint64_t blake2b_IV[8] =
0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
}; };
static const uint8_t blake2b_sigma[12][16] = static const byte blake2b_sigma[12][16] =
{ {
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } , { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } , { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
@ -69,20 +65,20 @@ static const uint8_t blake2b_sigma[12][16] =
}; };
static inline int blake2b_set_lastnode( blake2b_state *S ) static INLINE int blake2b_set_lastnode( blake2b_state *S )
{ {
S->f[1] = ~0ULL; S->f[1] = ~0ULL;
return 0; return 0;
} }
static inline int blake2b_clear_lastnode( blake2b_state *S ) static INLINE int blake2b_clear_lastnode( blake2b_state *S )
{ {
S->f[1] = 0ULL; S->f[1] = 0ULL;
return 0; return 0;
} }
/* Some helper functions, not necessarily useful */ /* Some helper functions, not necessarily useful */
static inline int blake2b_set_lastblock( blake2b_state *S ) static INLINE int blake2b_set_lastblock( blake2b_state *S )
{ {
if( S->last_node ) blake2b_set_lastnode( S ); if( S->last_node ) blake2b_set_lastnode( S );
@ -90,7 +86,7 @@ static inline int blake2b_set_lastblock( blake2b_state *S )
return 0; return 0;
} }
static inline int blake2b_clear_lastblock( blake2b_state *S ) static INLINE int blake2b_clear_lastblock( blake2b_state *S )
{ {
if( S->last_node ) blake2b_clear_lastnode( S ); if( S->last_node ) blake2b_clear_lastnode( S );
@ -98,7 +94,8 @@ static inline int blake2b_clear_lastblock( blake2b_state *S )
return 0; return 0;
} }
static inline int blake2b_increment_counter( blake2b_state *S, const uint64_t inc ) static INLINE int blake2b_increment_counter( blake2b_state *S, const word64
inc )
{ {
S->t[0] += inc; S->t[0] += inc;
S->t[1] += ( S->t[0] < inc ); S->t[1] += ( S->t[0] < inc );
@ -108,64 +105,72 @@ static inline int blake2b_increment_counter( blake2b_state *S, const uint64_t in
// Parameter-related functions // Parameter-related functions
static inline int blake2b_param_set_digest_length( blake2b_param *P, const uint8_t digest_length ) static INLINE int blake2b_param_set_digest_length( blake2b_param *P,
const byte digest_length )
{ {
P->digest_length = digest_length; P->digest_length = digest_length;
return 0; return 0;
} }
static inline int blake2b_param_set_fanout( blake2b_param *P, const uint8_t fanout ) static INLINE int blake2b_param_set_fanout( blake2b_param *P, const byte fanout)
{ {
P->fanout = fanout; P->fanout = fanout;
return 0; return 0;
} }
static inline int blake2b_param_set_max_depth( blake2b_param *P, const uint8_t depth ) static INLINE int blake2b_param_set_max_depth( blake2b_param *P,
const byte depth )
{ {
P->depth = depth; P->depth = depth;
return 0; return 0;
} }
static inline int blake2b_param_set_leaf_length( blake2b_param *P, const uint32_t leaf_length ) static INLINE int blake2b_param_set_leaf_length( blake2b_param *P,
const word32 leaf_length )
{ {
store32( &P->leaf_length, leaf_length ); store32( &P->leaf_length, leaf_length );
return 0; return 0;
} }
static inline int blake2b_param_set_node_offset( blake2b_param *P, const uint64_t node_offset ) static INLINE int blake2b_param_set_node_offset( blake2b_param *P,
const word64 node_offset )
{ {
store64( &P->node_offset, node_offset ); store64( &P->node_offset, node_offset );
return 0; return 0;
} }
static inline int blake2b_param_set_node_depth( blake2b_param *P, const uint8_t node_depth ) static INLINE int blake2b_param_set_node_depth( blake2b_param *P,
const byte node_depth )
{ {
P->node_depth = node_depth; P->node_depth = node_depth;
return 0; return 0;
} }
static inline int blake2b_param_set_inner_length( blake2b_param *P, const uint8_t inner_length ) static INLINE int blake2b_param_set_inner_length( blake2b_param *P,
const byte inner_length )
{ {
P->inner_length = inner_length; P->inner_length = inner_length;
return 0; return 0;
} }
static inline int blake2b_param_set_salt( blake2b_param *P, const uint8_t salt[BLAKE2B_SALTBYTES] ) static INLINE int blake2b_param_set_salt( blake2b_param *P,
const byte salt[BLAKE2B_SALTBYTES] )
{ {
memcpy( P->salt, salt, BLAKE2B_SALTBYTES ); XMEMCPY( P->salt, salt, BLAKE2B_SALTBYTES );
return 0; return 0;
} }
static inline int blake2b_param_set_personal( blake2b_param *P, const uint8_t personal[BLAKE2B_PERSONALBYTES] ) static INLINE int blake2b_param_set_personal( blake2b_param *P,
const byte personal[BLAKE2B_PERSONALBYTES] )
{ {
memcpy( P->personal, personal, BLAKE2B_PERSONALBYTES ); XMEMCPY( P->personal, personal, BLAKE2B_PERSONALBYTES );
return 0; return 0;
} }
static inline int blake2b_init0( blake2b_state *S ) static INLINE int blake2b_init0( blake2b_state *S )
{ {
int i; int i;
memset( S, 0, sizeof( blake2b_state ) ); XMEMSET( S, 0, sizeof( blake2b_state ) );
for( i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i]; for( i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i];
@ -175,9 +180,9 @@ static inline int blake2b_init0( blake2b_state *S )
/* init xors IV with input parameter block */ /* init xors IV with input parameter block */
int blake2b_init_param( blake2b_state *S, const blake2b_param *P ) int blake2b_init_param( blake2b_state *S, const blake2b_param *P )
{ {
size_t i; word32 i;
blake2b_init0( S ); blake2b_init0( S );
uint8_t *p = ( uint8_t * )( P ); byte *p = ( byte * )( P );
/* IV XOR ParamBlock */ /* IV XOR ParamBlock */
for( i = 0; i < 8; ++i ) for( i = 0; i < 8; ++i )
@ -188,7 +193,7 @@ int blake2b_init_param( blake2b_state *S, const blake2b_param *P )
int blake2b_init( blake2b_state *S, const uint8_t outlen ) int blake2b_init( blake2b_state *S, const byte outlen )
{ {
blake2b_param P[1]; blake2b_param P[1];
@ -202,14 +207,15 @@ int blake2b_init( blake2b_state *S, const uint8_t outlen )
store64( &P->node_offset, 0 ); store64( &P->node_offset, 0 );
P->node_depth = 0; P->node_depth = 0;
P->inner_length = 0; P->inner_length = 0;
memset( P->reserved, 0, sizeof( P->reserved ) ); XMEMSET( P->reserved, 0, sizeof( P->reserved ) );
memset( P->salt, 0, sizeof( P->salt ) ); XMEMSET( P->salt, 0, sizeof( P->salt ) );
memset( P->personal, 0, sizeof( P->personal ) ); XMEMSET( P->personal, 0, sizeof( P->personal ) );
return blake2b_init_param( S, P ); return blake2b_init_param( S, P );
} }
int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen ) int blake2b_init_key( blake2b_state *S, const byte outlen, const void *key,
const byte keylen )
{ {
blake2b_param P[1]; blake2b_param P[1];
@ -225,26 +231,28 @@ int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, c
store64( &P->node_offset, 0 ); store64( &P->node_offset, 0 );
P->node_depth = 0; P->node_depth = 0;
P->inner_length = 0; P->inner_length = 0;
memset( P->reserved, 0, sizeof( P->reserved ) ); XMEMSET( P->reserved, 0, sizeof( P->reserved ) );
memset( P->salt, 0, sizeof( P->salt ) ); XMEMSET( P->salt, 0, sizeof( P->salt ) );
memset( P->personal, 0, sizeof( P->personal ) ); XMEMSET( P->personal, 0, sizeof( P->personal ) );
if( blake2b_init_param( S, P ) < 0 ) return -1; if( blake2b_init_param( S, P ) < 0 ) return -1;
{ {
uint8_t block[BLAKE2B_BLOCKBYTES]; byte block[BLAKE2B_BLOCKBYTES];
memset( block, 0, BLAKE2B_BLOCKBYTES ); XMEMSET( block, 0, BLAKE2B_BLOCKBYTES );
memcpy( block, key, keylen ); XMEMCPY( block, key, keylen );
blake2b_update( S, block, BLAKE2B_BLOCKBYTES ); blake2b_update( S, block, BLAKE2B_BLOCKBYTES );
secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */ secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from */
/*stack */
} }
return 0; return 0;
} }
static int blake2b_compress( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] ) static int blake2b_compress( blake2b_state *S,
const byte block[BLAKE2B_BLOCKBYTES] )
{ {
uint64_t m[16]; word64 m[16];
uint64_t v[16]; word64 v[16];
int i; int i;
for( i = 0; i < 16; ++i ) for( i = 0; i < 16; ++i )
@ -305,27 +313,28 @@ static int blake2b_compress( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCK
} }
/* inlen now in bytes */ /* inlen now in bytes */
int blake2b_update( blake2b_state *S, const uint8_t *in, uint64_t inlen ) int blake2b_update( blake2b_state *S, const byte *in, word64 inlen )
{ {
while( inlen > 0 ) while( inlen > 0 )
{ {
size_t left = S->buflen; word64 left = S->buflen;
size_t fill = 2 * BLAKE2B_BLOCKBYTES - left; word64 fill = 2 * BLAKE2B_BLOCKBYTES - left;
if( inlen > fill ) if( inlen > fill )
{ {
memcpy( S->buf + left, in, fill ); // Fill buffer XMEMCPY( S->buf + left, in, fill ); // Fill buffer
S->buflen += fill; S->buflen += fill;
blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
blake2b_compress( S, S->buf ); // Compress blake2b_compress( S, S->buf ); // Compress
memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); // Shift buffer left XMEMCPY( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES );
// Shift buffer left
S->buflen -= BLAKE2B_BLOCKBYTES; S->buflen -= BLAKE2B_BLOCKBYTES;
in += fill; in += fill;
inlen -= fill; inlen -= fill;
} }
else // inlen <= fill else // inlen <= fill
{ {
memcpy( S->buf + left, in, inlen ); XMEMCPY( S->buf + left, in, inlen );
S->buflen += inlen; // Be lazy, do not compress S->buflen += inlen; // Be lazy, do not compress
in += inlen; in += inlen;
inlen -= inlen; inlen -= inlen;
@ -336,9 +345,9 @@ int blake2b_update( blake2b_state *S, const uint8_t *in, uint64_t inlen )
} }
/* Is this correct? */ /* Is this correct? */
int blake2b_final( blake2b_state *S, uint8_t *out, uint8_t outlen ) int blake2b_final( blake2b_state *S, byte *out, byte outlen )
{ {
uint8_t buffer[BLAKE2B_OUTBYTES]; byte buffer[BLAKE2B_OUTBYTES];
int i; int i;
if( S->buflen > BLAKE2B_BLOCKBYTES ) if( S->buflen > BLAKE2B_BLOCKBYTES )
@ -346,23 +355,25 @@ int blake2b_final( blake2b_state *S, uint8_t *out, uint8_t outlen )
blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
blake2b_compress( S, S->buf ); blake2b_compress( S, S->buf );
S->buflen -= BLAKE2B_BLOCKBYTES; S->buflen -= BLAKE2B_BLOCKBYTES;
memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, S->buflen ); XMEMCPY( S->buf, S->buf + BLAKE2B_BLOCKBYTES, S->buflen );
} }
blake2b_increment_counter( S, S->buflen ); blake2b_increment_counter( S, S->buflen );
blake2b_set_lastblock( S ); blake2b_set_lastblock( S );
memset( S->buf + S->buflen, 0, 2 * BLAKE2B_BLOCKBYTES - S->buflen ); /* Padding */ XMEMSET( S->buf + S->buflen, 0, 2 * BLAKE2B_BLOCKBYTES - S->buflen );
/* Padding */
blake2b_compress( S, S->buf ); blake2b_compress( S, S->buf );
for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */ for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
store64( buffer + sizeof( S->h[i] ) * i, S->h[i] ); store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
memcpy( out, buffer, outlen ); XMEMCPY( out, buffer, outlen );
return 0; return 0;
} }
/* inlen, at least, should be uint64_t. Others can be size_t. */ /* inlen, at least, should be word64. Others can be size_t. */
int blake2b( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen ) int blake2b( byte *out, const void *in, const void *key, const byte outlen,
const word64 inlen, byte keylen )
{ {
blake2b_state S[1]; blake2b_state S[1];
@ -382,7 +393,7 @@ int blake2b( uint8_t *out, const void *in, const void *key, const uint8_t outlen
if( blake2b_init( S, outlen ) < 0 ) return -1; if( blake2b_init( S, outlen ) < 0 ) return -1;
} }
blake2b_update( S, ( uint8_t * )in, inlen ); blake2b_update( S, ( byte * )in, inlen );
blake2b_final( S, out, outlen ); blake2b_final( S, out, outlen );
return 0; return 0;
} }
@ -392,18 +403,18 @@ int blake2b( uint8_t *out, const void *in, const void *key, const uint8_t outlen
#include "blake2-kat.h" #include "blake2-kat.h"
int main( int argc, char **argv ) int main( int argc, char **argv )
{ {
uint8_t key[BLAKE2B_KEYBYTES]; byte key[BLAKE2B_KEYBYTES];
uint8_t buf[KAT_LENGTH]; byte buf[KAT_LENGTH];
for( size_t i = 0; i < BLAKE2B_KEYBYTES; ++i ) for( word32 i = 0; i < BLAKE2B_KEYBYTES; ++i )
key[i] = ( uint8_t )i; key[i] = ( byte )i;
for( size_t i = 0; i < KAT_LENGTH; ++i ) for( word32 i = 0; i < KAT_LENGTH; ++i )
buf[i] = ( uint8_t )i; buf[i] = ( byte )i;
for( size_t i = 0; i < KAT_LENGTH; ++i ) for( word32 i = 0; i < KAT_LENGTH; ++i )
{ {
uint8_t hash[BLAKE2B_OUTBYTES]; byte hash[BLAKE2B_OUTBYTES];
blake2b( hash, buf, key, BLAKE2B_OUTBYTES, i, BLAKE2B_KEYBYTES ); blake2b( hash, buf, key, BLAKE2B_OUTBYTES, i, BLAKE2B_KEYBYTES );
if( 0 != memcmp( hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES ) ) if( 0 != memcmp( hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES ) )

View File

@ -31,122 +31,121 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/ */
#pragma once
#ifndef CTAOCRYPT_BLAKE2_IMPL_H #ifndef CTAOCRYPT_BLAKE2_IMPL_H
#define CTAOCRYPT_BLAKE2_IMPL_H #define CTAOCRYPT_BLAKE2_IMPL_H
#include <cyassl/ctaocrypt/types.h> #include <cyassl/ctaocrypt/types.h>
#include <stdint.h>
static inline uint32_t load32( const void *src ) static inline word32 load32( const void *src )
{ {
#if defined(LITTLE_ENDIAN_ORDER) #if defined(LITTLE_ENDIAN_ORDER)
return *( uint32_t * )( src ); return *( word32 * )( src );
#else #else
const uint8_t *p = ( uint8_t * )src; const byte *p = ( byte * )src;
uint32_t w = *p++; word32 w = *p++;
w |= ( uint32_t )( *p++ ) << 8; w |= ( word32 )( *p++ ) << 8;
w |= ( uint32_t )( *p++ ) << 16; w |= ( word32 )( *p++ ) << 16;
w |= ( uint32_t )( *p++ ) << 24; w |= ( word32 )( *p++ ) << 24;
return w; return w;
#endif #endif
} }
static inline uint64_t load64( const void *src ) static inline word64 load64( const void *src )
{ {
#if defined(LITTLE_ENDIAN_ORDER) #if defined(LITTLE_ENDIAN_ORDER)
return *( uint64_t * )( src ); return *( word64 * )( src );
#else #else
const uint8_t *p = ( uint8_t * )src; const byte *p = ( byte * )src;
uint64_t w = *p++; word64 w = *p++;
w |= ( uint64_t )( *p++ ) << 8; w |= ( word64 )( *p++ ) << 8;
w |= ( uint64_t )( *p++ ) << 16; w |= ( word64 )( *p++ ) << 16;
w |= ( uint64_t )( *p++ ) << 24; w |= ( word64 )( *p++ ) << 24;
w |= ( uint64_t )( *p++ ) << 32; w |= ( word64 )( *p++ ) << 32;
w |= ( uint64_t )( *p++ ) << 40; w |= ( word64 )( *p++ ) << 40;
w |= ( uint64_t )( *p++ ) << 48; w |= ( word64 )( *p++ ) << 48;
w |= ( uint64_t )( *p++ ) << 56; w |= ( word64 )( *p++ ) << 56;
return w; return w;
#endif #endif
} }
static inline void store32( void *dst, uint32_t w ) static inline void store32( void *dst, word32 w )
{ {
#if defined(LITTLE_ENDIAN_ORDER) #if defined(LITTLE_ENDIAN_ORDER)
*( uint32_t * )( dst ) = w; *( word32 * )( dst ) = w;
#else #else
uint8_t *p = ( uint8_t * )dst; byte *p = ( byte * )dst;
*p++ = ( uint8_t )w; w >>= 8; *p++ = ( byte )w; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8; *p++ = ( byte )w; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8; *p++ = ( byte )w; w >>= 8;
*p++ = ( uint8_t )w; *p++ = ( byte )w;
#endif #endif
} }
static inline void store64( void *dst, uint64_t w ) static inline void store64( void *dst, word64 w )
{ {
#if defined(LITTLE_ENDIAN_ORDER) #if defined(LITTLE_ENDIAN_ORDER)
*( uint64_t * )( dst ) = w; *( word64 * )( dst ) = w;
#else #else
uint8_t *p = ( uint8_t * )dst; byte *p = ( byte * )dst;
*p++ = ( uint8_t )w; w >>= 8; *p++ = ( byte )w; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8; *p++ = ( byte )w; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8; *p++ = ( byte )w; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8; *p++ = ( byte )w; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8; *p++ = ( byte )w; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8; *p++ = ( byte )w; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8; *p++ = ( byte )w; w >>= 8;
*p++ = ( uint8_t )w; *p++ = ( byte )w;
#endif #endif
} }
static inline uint64_t load48( const void *src ) static inline word64 load48( const void *src )
{ {
const uint8_t *p = ( const uint8_t * )src; const byte *p = ( const byte * )src;
uint64_t w = *p++; word64 w = *p++;
w |= ( uint64_t )( *p++ ) << 8; w |= ( word64 )( *p++ ) << 8;
w |= ( uint64_t )( *p++ ) << 16; w |= ( word64 )( *p++ ) << 16;
w |= ( uint64_t )( *p++ ) << 24; w |= ( word64 )( *p++ ) << 24;
w |= ( uint64_t )( *p++ ) << 32; w |= ( word64 )( *p++ ) << 32;
w |= ( uint64_t )( *p++ ) << 40; w |= ( word64 )( *p++ ) << 40;
return w; return w;
} }
static inline void store48( void *dst, uint64_t w ) static inline void store48( void *dst, word64 w )
{ {
uint8_t *p = ( uint8_t * )dst; byte *p = ( byte * )dst;
*p++ = ( uint8_t )w; w >>= 8; *p++ = ( byte )w; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8; *p++ = ( byte )w; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8; *p++ = ( byte )w; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8; *p++ = ( byte )w; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8; *p++ = ( byte )w; w >>= 8;
*p++ = ( uint8_t )w; *p++ = ( byte )w;
} }
static inline uint32_t rotl32( const uint32_t w, const unsigned c ) static inline word32 rotl32( const word32 w, const unsigned c )
{ {
return ( w << c ) | ( w >> ( 32 - c ) ); return ( w << c ) | ( w >> ( 32 - c ) );
} }
static inline uint64_t rotl64( const uint64_t w, const unsigned c ) static inline word64 rotl64( const word64 w, const unsigned c )
{ {
return ( w << c ) | ( w >> ( 64 - c ) ); return ( w << c ) | ( w >> ( 64 - c ) );
} }
static inline uint32_t rotr32( const uint32_t w, const unsigned c ) static inline word32 rotr32( const word32 w, const unsigned c )
{ {
return ( w >> c ) | ( w << ( 32 - c ) ); return ( w >> c ) | ( w << ( 32 - c ) );
} }
static inline uint64_t rotr64( const uint64_t w, const unsigned c ) static inline word64 rotr64( const word64 w, const unsigned c )
{ {
return ( w >> c ) | ( w << ( 64 - c ) ); return ( w >> c ) | ( w << ( 64 - c ) );
} }
/* prevents compiler optimizing out memset() */ /* prevents compiler optimizing out memset() */
static inline void secure_zero_memory( void *v, size_t n ) static inline void secure_zero_memory( void *v, word64 n )
{ {
volatile uint8_t *p = ( volatile uint8_t * )v; volatile byte *p = ( volatile byte * )v;
while( n-- ) *p++ = 0; while( n-- ) *p++ = 0;
} }

View File

@ -36,18 +36,18 @@
#include <cyassl/ctaocrypt/types.h> #include <cyassl/ctaocrypt/types.h>
#include <stddef.h>
#include <stdint.h>
#if defined(_MSC_VER) #if defined(_MSC_VER)
#define ALIGN(x) __declspec(align(x)) #define ALIGN(x) __declspec(align(x))
#elif defined(__GNUC__)
#define ALIGN(x) __attribute__((aligned(x)))
#else #else
#define ALIGN(x) __attribute__((aligned(x))) #define ALIGN(x)
#endif #endif
#if defined(__cplusplus) #if defined(__cplusplus)
extern "C" { extern "C" {
#endif #endif
enum blake2s_constant enum blake2s_constant
@ -71,108 +71,108 @@ extern "C" {
#pragma pack(push, 1) #pragma pack(push, 1)
typedef struct __blake2s_param typedef struct __blake2s_param
{ {
uint8_t digest_length; // 1 byte digest_length; // 1
uint8_t key_length; // 2 byte key_length; // 2
uint8_t fanout; // 3 byte fanout; // 3
uint8_t depth; // 4 byte depth; // 4
uint32_t leaf_length; // 8 word32 leaf_length; // 8
uint8_t node_offset[6];// 14 byte node_offset[6];// 14
uint8_t node_depth; // 15 byte node_depth; // 15
uint8_t inner_length; // 16 byte inner_length; // 16
// uint8_t reserved[0]; // byte reserved[0];
uint8_t salt[BLAKE2B_SALTBYTES]; // 24 byte salt[BLAKE2B_SALTBYTES]; // 24
uint8_t personal[BLAKE2S_PERSONALBYTES]; // 32 byte personal[BLAKE2S_PERSONALBYTES]; // 32
} blake2s_param; } blake2s_param;
ALIGN( 64 ) typedef struct __blake2s_state ALIGN( 64 ) typedef struct __blake2s_state
{ {
uint32_t h[8]; word32 h[8];
uint32_t t[2]; word32 t[2];
uint32_t f[2]; word32 f[2];
uint8_t buf[2 * BLAKE2S_BLOCKBYTES]; byte buf[2 * BLAKE2S_BLOCKBYTES];
size_t buflen; word64 buflen;
uint8_t last_node; byte last_node;
} blake2s_state ; } blake2s_state ;
typedef struct __blake2b_param typedef struct __blake2b_param
{ {
uint8_t digest_length; // 1 byte digest_length; // 1
uint8_t key_length; // 2 byte key_length; // 2
uint8_t fanout; // 3 byte fanout; // 3
uint8_t depth; // 4 byte depth; // 4
uint32_t leaf_length; // 8 word32 leaf_length; // 8
uint64_t node_offset; // 16 word64 node_offset; // 16
uint8_t node_depth; // 17 byte node_depth; // 17
uint8_t inner_length; // 18 byte inner_length; // 18
uint8_t reserved[14]; // 32 byte reserved[14]; // 32
uint8_t salt[BLAKE2B_SALTBYTES]; // 48 byte salt[BLAKE2B_SALTBYTES]; // 48
uint8_t personal[BLAKE2B_PERSONALBYTES]; // 64 byte personal[BLAKE2B_PERSONALBYTES]; // 64
} blake2b_param; } blake2b_param;
ALIGN( 64 ) typedef struct __blake2b_state ALIGN( 64 ) typedef struct __blake2b_state
{ {
uint64_t h[8]; word64 h[8];
uint64_t t[2]; word64 t[2];
uint64_t f[2]; word64 f[2];
uint8_t buf[2 * BLAKE2B_BLOCKBYTES]; byte buf[2 * BLAKE2B_BLOCKBYTES];
size_t buflen; word64 buflen;
uint8_t last_node; byte last_node;
} blake2b_state; } blake2b_state;
typedef struct __blake2sp_state typedef struct __blake2sp_state
{ {
blake2s_state S[8][1]; blake2s_state S[8][1];
blake2s_state R[1]; blake2s_state R[1];
uint8_t buf[8 * BLAKE2S_BLOCKBYTES]; byte buf[8 * BLAKE2S_BLOCKBYTES];
size_t buflen; word64 buflen;
} blake2sp_state; } blake2sp_state;
typedef struct __blake2bp_state typedef struct __blake2bp_state
{ {
blake2b_state S[4][1]; blake2b_state S[4][1];
blake2b_state R[1]; blake2b_state R[1];
uint8_t buf[4 * BLAKE2B_BLOCKBYTES]; byte buf[4 * BLAKE2B_BLOCKBYTES];
size_t buflen; word64 buflen;
} blake2bp_state; } blake2bp_state;
#pragma pack(pop) #pragma pack(pop)
// Streaming API // Streaming API
int blake2s_init( blake2s_state *S, const uint8_t outlen ); int blake2s_init( blake2s_state *S, const byte outlen );
int blake2s_init_key( blake2s_state *S, const uint8_t outlen, const void *key, const uint8_t keylen ); int blake2s_init_key( blake2s_state *S, const byte outlen, const void *key, const byte keylen );
int blake2s_init_param( blake2s_state *S, const blake2s_param *P ); int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
int blake2s_update( blake2s_state *S, const uint8_t *in, uint64_t inlen ); int blake2s_update( blake2s_state *S, const byte *in, word64 inlen );
int blake2s_final( blake2s_state *S, uint8_t *out, uint8_t outlen ); int blake2s_final( blake2s_state *S, byte *out, byte outlen );
CYASSL_API int blake2b_init( blake2b_state *S, const uint8_t outlen ); CYASSL_API int blake2b_init( blake2b_state *S, const byte outlen );
int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen ); int blake2b_init_key( blake2b_state *S, const byte outlen, const void *key, const byte keylen );
int blake2b_init_param( blake2b_state *S, const blake2b_param *P ); int blake2b_init_param( blake2b_state *S, const blake2b_param *P );
CYASSL_API int blake2b_update( blake2b_state *S, const uint8_t *in, uint64_t inlen ); CYASSL_API int blake2b_update( blake2b_state *S, const byte *in, word64 inlen );
CYASSL_API int blake2b_final( blake2b_state *S, uint8_t *out, uint8_t outlen ); CYASSL_API int blake2b_final( blake2b_state *S, byte *out, byte outlen );
int blake2sp_init( blake2sp_state *S, const uint8_t outlen ); int blake2sp_init( blake2sp_state *S, const byte outlen );
int blake2sp_init_key( blake2sp_state *S, const uint8_t outlen, const void *key, const uint8_t keylen ); int blake2sp_init_key( blake2sp_state *S, const byte outlen, const void *key, const byte keylen );
int blake2sp_update( blake2sp_state *S, const uint8_t *in, uint64_t inlen ); int blake2sp_update( blake2sp_state *S, const byte *in, word64 inlen );
int blake2sp_final( blake2sp_state *S, uint8_t *out, uint8_t outlen ); int blake2sp_final( blake2sp_state *S, byte *out, byte outlen );
int blake2bp_init( blake2bp_state *S, const uint8_t outlen ); int blake2bp_init( blake2bp_state *S, const byte outlen );
int blake2bp_init_key( blake2bp_state *S, const uint8_t outlen, const void *key, const uint8_t keylen ); int blake2bp_init_key( blake2bp_state *S, const byte outlen, const void *key, const byte keylen );
int blake2bp_update( blake2bp_state *S, const uint8_t *in, uint64_t inlen ); int blake2bp_update( blake2bp_state *S, const byte *in, word64 inlen );
int blake2bp_final( blake2bp_state *S, uint8_t *out, uint8_t outlen ); int blake2bp_final( blake2bp_state *S, byte *out, byte outlen );
// Simple API // Simple API
int blake2s( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen ); int blake2s( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen );
int blake2b( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen ); int blake2b( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen );
int blake2sp( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen ); int blake2sp( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen );
int blake2bp( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen ); int blake2bp( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen );
static inline int blake2( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen ) static inline int blake2( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen )
{ {
return blake2b( out, in, key, outlen, inlen, keylen ); return blake2b( out, in, key, outlen, inlen, keylen );
} }
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif
#endif /* CTAOCRYPT_BLAKE2_H */ #endif /* CTAOCRYPT_BLAKE2_H */