summaryrefslogtreecommitdiffstats
path: root/Modules/_blake2/impl/blake2s-ref.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/_blake2/impl/blake2s-ref.c')
-rw-r--r--Modules/_blake2/impl/blake2s-ref.c195
1 files changed, 76 insertions, 119 deletions
diff --git a/Modules/_blake2/impl/blake2s-ref.c b/Modules/_blake2/impl/blake2s-ref.c
index 6636753..baf0b58 100644
--- a/Modules/_blake2/impl/blake2s-ref.c
+++ b/Modules/_blake2/impl/blake2s-ref.c
@@ -1,16 +1,14 @@
/*
BLAKE2 reference source code package - reference C implementations
-
- Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the
- terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
- your option. The terms of these licenses can be found at:
-
- - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
- - OpenSSL license : https://www.openssl.org/source/license.html
- - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
-
- More information about the BLAKE2 hash function can be found at
- https://blake2.net.
+
+ Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
+
+ To the extent possible under law, the author(s) have dedicated all copyright
+ and related and neighboring rights to this software to the public domain
+ worldwide. This software is distributed without any warranty.
+
+ You should have received a copy of the CC0 Public Domain Dedication along with
+ this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdint.h>
@@ -40,137 +38,143 @@ static const uint8_t blake2s_sigma[10][16] =
{ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
};
-BLAKE2_LOCAL_INLINE(int) blake2s_set_lastnode( blake2s_state *S )
+static inline int blake2s_set_lastnode( blake2s_state *S )
{
- S->f[1] = -1;
+ S->f[1] = ~0U;
return 0;
}
-BLAKE2_LOCAL_INLINE(int) blake2s_clear_lastnode( blake2s_state *S )
+static inline int blake2s_clear_lastnode( blake2s_state *S )
{
- S->f[1] = 0;
+ S->f[1] = 0U;
return 0;
}
/* Some helper functions, not necessarily useful */
-BLAKE2_LOCAL_INLINE(int) blake2s_is_lastblock( const blake2s_state *S )
-{
- return S->f[0] != 0;
-}
-
-BLAKE2_LOCAL_INLINE(int) blake2s_set_lastblock( blake2s_state *S )
+static inline int blake2s_set_lastblock( blake2s_state *S )
{
if( S->last_node ) blake2s_set_lastnode( S );
- S->f[0] = -1;
+ S->f[0] = ~0U;
return 0;
}
-BLAKE2_LOCAL_INLINE(int) blake2s_clear_lastblock( blake2s_state *S )
+static inline int blake2s_clear_lastblock( blake2s_state *S )
{
if( S->last_node ) blake2s_clear_lastnode( S );
- S->f[0] = 0;
+ S->f[0] = 0U;
return 0;
}
-BLAKE2_LOCAL_INLINE(int) blake2s_increment_counter( blake2s_state *S, const uint32_t inc )
+static inline int blake2s_increment_counter( blake2s_state *S, const uint32_t inc )
{
S->t[0] += inc;
S->t[1] += ( S->t[0] < inc );
return 0;
}
-/* Parameter-related functions */
-BLAKE2_LOCAL_INLINE(int) blake2s_param_set_digest_length( blake2s_param *P, const uint8_t digest_length )
+// Parameter-related functions
+static inline int blake2s_param_set_digest_length( blake2s_param *P, const uint8_t digest_length )
{
P->digest_length = digest_length;
return 0;
}
-BLAKE2_LOCAL_INLINE(int) blake2s_param_set_fanout( blake2s_param *P, const uint8_t fanout )
+static inline int blake2s_param_set_fanout( blake2s_param *P, const uint8_t fanout )
{
P->fanout = fanout;
return 0;
}
-BLAKE2_LOCAL_INLINE(int) blake2s_param_set_max_depth( blake2s_param *P, const uint8_t depth )
+static inline int blake2s_param_set_max_depth( blake2s_param *P, const uint8_t depth )
{
P->depth = depth;
return 0;
}
-BLAKE2_LOCAL_INLINE(int) blake2s_param_set_leaf_length( blake2s_param *P, const uint32_t leaf_length )
+static inline int blake2s_param_set_leaf_length( blake2s_param *P, const uint32_t leaf_length )
{
store32( &P->leaf_length, leaf_length );
return 0;
}
-BLAKE2_LOCAL_INLINE(int) blake2s_param_set_node_offset( blake2s_param *P, const uint64_t node_offset )
+static inline int blake2s_param_set_node_offset( blake2s_param *P, const uint64_t node_offset )
{
store48( P->node_offset, node_offset );
return 0;
}
-BLAKE2_LOCAL_INLINE(int) blake2s_param_set_node_depth( blake2s_param *P, const uint8_t node_depth )
+static inline int blake2s_param_set_node_depth( blake2s_param *P, const uint8_t node_depth )
{
P->node_depth = node_depth;
return 0;
}
-BLAKE2_LOCAL_INLINE(int) blake2s_param_set_inner_length( blake2s_param *P, const uint8_t inner_length )
+static inline int blake2s_param_set_inner_length( blake2s_param *P, const uint8_t inner_length )
{
P->inner_length = inner_length;
return 0;
}
-BLAKE2_LOCAL_INLINE(int) blake2s_param_set_salt( blake2s_param *P, const uint8_t salt[BLAKE2S_SALTBYTES] )
+static inline int blake2s_param_set_salt( blake2s_param *P, const uint8_t salt[BLAKE2S_SALTBYTES] )
{
memcpy( P->salt, salt, BLAKE2S_SALTBYTES );
return 0;
}
-BLAKE2_LOCAL_INLINE(int) blake2s_param_set_personal( blake2s_param *P, const uint8_t personal[BLAKE2S_PERSONALBYTES] )
+static inline int blake2s_param_set_personal( blake2s_param *P, const uint8_t personal[BLAKE2S_PERSONALBYTES] )
{
memcpy( P->personal, personal, BLAKE2S_PERSONALBYTES );
return 0;
}
-BLAKE2_LOCAL_INLINE(int) blake2s_init0( blake2s_state *S )
+static inline int blake2s_init0( blake2s_state *S )
{
- int i;
memset( S, 0, sizeof( blake2s_state ) );
- for( i = 0; i < 8; ++i ) S->h[i] = blake2s_IV[i];
+ for( int i = 0; i < 8; ++i ) S->h[i] = blake2s_IV[i];
return 0;
}
+#if defined(__cplusplus)
+extern "C" {
+#endif
+ int blake2s_init( blake2s_state *S, size_t outlen );
+ int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
+ int blake2s_init_key( blake2s_state *S, size_t outlen, const void *key, size_t keylen );
+ int blake2s_update( blake2s_state *S, const uint8_t *in, size_t inlen );
+ int blake2s_final( blake2s_state *S, uint8_t *out, size_t outlen );
+ int blake2s( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
+#if defined(__cplusplus)
+}
+#endif
+
/* init2 xors IV with input parameter block */
int blake2s_init_param( blake2s_state *S, const blake2s_param *P )
{
- const uint32_t *p = ( const uint32_t * )( P );
- size_t i;
-
blake2s_init0( S );
+ uint32_t *p = ( uint32_t * )( P );
/* IV XOR ParamBlock */
- for( i = 0; i < 8; ++i )
+ for( size_t i = 0; i < 8; ++i )
S->h[i] ^= load32( &p[i] );
+ S->outlen = P->digest_length;
return 0;
}
-/* Sequential blake2s initialization */
-int blake2s_init( blake2s_state *S, const uint8_t outlen )
+// Sequential blake2s initialization
+int blake2s_init( blake2s_state *S, size_t outlen )
{
blake2s_param P[1];
/* Move interval verification here? */
if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1;
- P->digest_length = outlen;
+ P->digest_length = ( uint8_t) outlen;
P->key_length = 0;
P->fanout = 1;
P->depth = 1;
@@ -178,13 +182,13 @@ int blake2s_init( blake2s_state *S, const uint8_t outlen )
store48( &P->node_offset, 0 );
P->node_depth = 0;
P->inner_length = 0;
- /* memset(P->reserved, 0, sizeof(P->reserved) ); */
+ // memset(P->reserved, 0, sizeof(P->reserved) );
memset( P->salt, 0, sizeof( P->salt ) );
memset( P->personal, 0, sizeof( P->personal ) );
return blake2s_init_param( S, P );
}
-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, size_t outlen, const void *key, size_t keylen )
{
blake2s_param P[1];
@@ -192,15 +196,15 @@ int blake2s_init_key( blake2s_state *S, const uint8_t outlen, const void *key, c
if ( !key || !keylen || keylen > BLAKE2S_KEYBYTES ) return -1;
- P->digest_length = outlen;
- P->key_length = keylen;
+ P->digest_length = ( uint8_t ) outlen;
+ P->key_length = ( uint8_t ) keylen;
P->fanout = 1;
P->depth = 1;
store32( &P->leaf_length, 0 );
store48( &P->node_offset, 0 );
P->node_depth = 0;
P->inner_length = 0;
- /* memset(P->reserved, 0, sizeof(P->reserved) ); */
+ // memset(P->reserved, 0, sizeof(P->reserved) );
memset( P->salt, 0, sizeof( P->salt ) );
memset( P->personal, 0, sizeof( P->personal ) );
@@ -220,12 +224,11 @@ static int blake2s_compress( blake2s_state *S, const uint8_t block[BLAKE2S_BLOCK
{
uint32_t m[16];
uint32_t v[16];
- size_t i;
- for( i = 0; i < 16; ++i )
+ for( size_t i = 0; i < 16; ++i )
m[i] = load32( block + i * sizeof( m[i] ) );
- for( i = 0; i < 8; ++i )
+ for( size_t i = 0; i < 8; ++i )
v[i] = S->h[i];
v[ 8] = blake2s_IV[0];
@@ -269,7 +272,7 @@ static int blake2s_compress( blake2s_state *S, const uint8_t block[BLAKE2S_BLOCK
ROUND( 8 );
ROUND( 9 );
- for( i = 0; i < 8; ++i )
+ for( size_t i = 0; i < 8; ++i )
S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
#undef G
@@ -278,28 +281,28 @@ static int blake2s_compress( blake2s_state *S, const uint8_t block[BLAKE2S_BLOCK
}
-int blake2s_update( blake2s_state *S, const uint8_t *in, uint64_t inlen )
+int blake2s_update( blake2s_state *S, const uint8_t *in, size_t inlen )
{
while( inlen > 0 )
{
- size_t left = S->buflen;
- size_t fill = 2 * BLAKE2S_BLOCKBYTES - left;
+ uint32_t left = S->buflen;
+ uint32_t fill = 2 * BLAKE2S_BLOCKBYTES - left;
if( inlen > fill )
{
- memcpy( S->buf + left, in, fill ); /* Fill buffer */
+ memcpy( S->buf + left, in, fill ); // Fill buffer
S->buflen += fill;
blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
- blake2s_compress( S, S->buf ); /* Compress */
- memcpy( S->buf, S->buf + BLAKE2S_BLOCKBYTES, BLAKE2S_BLOCKBYTES ); /* Shift buffer left */
+ blake2s_compress( S, S->buf ); // Compress
+ memcpy( S->buf, S->buf + BLAKE2S_BLOCKBYTES, BLAKE2S_BLOCKBYTES ); // Shift buffer left
S->buflen -= BLAKE2S_BLOCKBYTES;
in += fill;
inlen -= fill;
}
- else /* inlen <= fill */
+ else // inlen <= fill
{
- memcpy( S->buf + left, in, (size_t)inlen );
- S->buflen += (size_t)inlen; /* Be lazy, do not compress */
+ memcpy( S->buf + left, in, inlen );
+ S->buflen += ( uint32_t ) inlen; // Be lazy, do not compress
in += inlen;
inlen -= inlen;
}
@@ -308,24 +311,19 @@ int blake2s_update( blake2s_state *S, const uint8_t *in, uint64_t inlen )
return 0;
}
-int blake2s_final( blake2s_state *S, uint8_t *out, uint8_t outlen )
+int blake2s_final( blake2s_state *S, uint8_t *out, size_t outlen )
{
- uint8_t buffer[BLAKE2S_OUTBYTES] = {0};
- int i;
-
- if( out == NULL || outlen == 0 || outlen > BLAKE2S_OUTBYTES )
- return -1;
-
- if( blake2s_is_lastblock( S ) )
- return -1;
+ uint8_t buffer[BLAKE2S_OUTBYTES];
+ size_t i;
+ if(S->outlen != outlen) return -1;
if( S->buflen > BLAKE2S_BLOCKBYTES )
{
blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
blake2s_compress( S, S->buf );
S->buflen -= BLAKE2S_BLOCKBYTES;
- memmove( S->buf, S->buf + BLAKE2S_BLOCKBYTES, S->buflen );
+ memcpy( S->buf, S->buf + BLAKE2S_BLOCKBYTES, S->buflen );
}
blake2s_increment_counter( S, ( uint32_t )S->buflen );
@@ -335,12 +333,12 @@ int blake2s_final( blake2s_state *S, uint8_t *out, uint8_t outlen )
for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
store32( buffer + sizeof( S->h[i] ) * i, S->h[i] );
-
+
memcpy( out, buffer, outlen );
return 0;
}
-int blake2s( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen )
+int blake2s( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen )
{
blake2s_state S[1];
@@ -349,7 +347,7 @@ int blake2s( uint8_t *out, const void *in, const void *key, const uint8_t outlen
if ( NULL == out ) return -1;
- if ( NULL == key && keylen > 0) return -1;
+ if ( NULL == key && keylen > 0 ) return -1;
if( !outlen || outlen > BLAKE2S_OUTBYTES ) return -1;
@@ -364,48 +362,7 @@ int blake2s( uint8_t *out, const void *in, const void *key, const uint8_t outlen
if( blake2s_init( S, outlen ) < 0 ) return -1;
}
- blake2s_update( S, ( const uint8_t * )in, inlen );
- blake2s_final( S, out, outlen );
- return 0;
+ if( blake2s_update( S, ( uint8_t * )in, inlen ) < 0) return -1;
+ return blake2s_final( S, out, outlen );
}
-#if defined(SUPERCOP)
-int crypto_hash( unsigned char *out, unsigned char *in, unsigned long long inlen )
-{
- return blake2s( out, in, NULL, BLAKE2S_OUTBYTES, inlen, 0 );
-}
-#endif
-
-#if defined(BLAKE2S_SELFTEST)
-#include <string.h>
-#include "blake2-kat.h"
-int main( int argc, char **argv )
-{
- uint8_t key[BLAKE2S_KEYBYTES];
- uint8_t buf[KAT_LENGTH];
- size_t i;
-
- for( i = 0; i < BLAKE2S_KEYBYTES; ++i )
- key[i] = ( uint8_t )i;
-
- for( i = 0; i < KAT_LENGTH; ++i )
- buf[i] = ( uint8_t )i;
-
- for( i = 0; i < KAT_LENGTH; ++i )
- {
- uint8_t hash[BLAKE2S_OUTBYTES];
- blake2s( hash, buf, key, BLAKE2S_OUTBYTES, i, BLAKE2S_KEYBYTES );
-
- if( 0 != memcmp( hash, blake2s_keyed_kat[i], BLAKE2S_OUTBYTES ) )
- {
- puts( "error" );
- return -1;
- }
- }
-
- puts( "ok" );
- return 0;
-}
-#endif
-
-