diff options
author | Christian Heimes <christian@python.org> | 2016-04-22 13:07:57 +0300 |
---|---|---|
committer | Christian Heimes <christian@python.org> | 2016-04-22 13:07:57 +0300 |
commit | 81a1bcf245f8d4ba1d627327b9069559b72d00b1 (patch) | |
tree | f3bc1c75214caab09bbc34b5a30007e981eef773 /sse/blake2s.c | |
parent | 02bf34f3d49c205812c34dfce9123a7c74509605 (diff) |
Make BLAKE2 code C89 and MSVC friendly
This patch makes the BLAKE2 reference implementation compatible with C89
compilers and MSVC.
1) Use C comments (/* */) instead of C++ comments.
2) No declarations after statements. All variables are declared on the
top of a block.
3) Optional inline with BLAKE2_LOCAL_INLINE() macro.
Signed-off-by: Christian Heimes <christian@python.org>
Diffstat (limited to 'sse/blake2s.c')
-rw-r--r-- | sse/blake2s.c | 63 |
1 files changed, 31 insertions, 32 deletions
diff --git a/sse/blake2s.c b/sse/blake2s.c index 59d6cca..030a85e 100644 --- a/sse/blake2s.c +++ b/sse/blake2s.c @@ -61,24 +61,24 @@ static const uint8_t blake2s_sigma[10][16] = /* Some helper functions, not necessarily useful */ -static inline int blake2s_set_lastnode( blake2s_state *S ) +BLAKE2_LOCAL_INLINE(int) blake2s_set_lastnode( blake2s_state *S ) { S->f[1] = -1; return 0; } -static inline int blake2s_clear_lastnode( blake2s_state *S ) +BLAKE2_LOCAL_INLINE(int) blake2s_clear_lastnode( blake2s_state *S ) { S->f[1] = 0; return 0; } -static inline int blake2s_is_lastblock( const blake2s_state *S ) +BLAKE2_LOCAL_INLINE(int) blake2s_is_lastblock( const blake2s_state *S ) { return S->f[0] != 0; } -static inline int blake2s_set_lastblock( blake2s_state *S ) +BLAKE2_LOCAL_INLINE(int) blake2s_set_lastblock( blake2s_state *S ) { if( S->last_node ) blake2s_set_lastnode( S ); @@ -86,7 +86,7 @@ static inline int blake2s_set_lastblock( blake2s_state *S ) return 0; } -static inline int blake2s_clear_lastblock( blake2s_state *S ) +BLAKE2_LOCAL_INLINE(int) blake2s_clear_lastblock( blake2s_state *S ) { if( S->last_node ) blake2s_clear_lastnode( S ); @@ -94,7 +94,7 @@ static inline int blake2s_clear_lastblock( blake2s_state *S ) return 0; } -static inline int blake2s_increment_counter( blake2s_state *S, const uint32_t inc ) +BLAKE2_LOCAL_INLINE(int) blake2s_increment_counter( blake2s_state *S, const uint32_t inc ) { uint64_t t = ( ( uint64_t )S->t[1] << 32 ) | S->t[0]; t += inc; @@ -104,62 +104,62 @@ static inline int blake2s_increment_counter( blake2s_state *S, const uint32_t in } -// Parameter-related functions -static inline int blake2s_param_set_digest_length( blake2s_param *P, const uint8_t digest_length ) +/* Parameter-related functions */ +BLAKE2_LOCAL_INLINE(int) blake2s_param_set_digest_length( blake2s_param *P, const uint8_t digest_length ) { P->digest_length = digest_length; return 0; } -static inline int blake2s_param_set_fanout( blake2s_param *P, const uint8_t fanout ) +BLAKE2_LOCAL_INLINE(int) blake2s_param_set_fanout( blake2s_param *P, const uint8_t fanout ) { P->fanout = fanout; return 0; } -static inline int blake2s_param_set_max_depth( blake2s_param *P, const uint8_t depth ) +BLAKE2_LOCAL_INLINE(int) blake2s_param_set_max_depth( blake2s_param *P, const uint8_t depth ) { P->depth = depth; return 0; } -static inline int blake2s_param_set_leaf_length( blake2s_param *P, const uint32_t leaf_length ) +BLAKE2_LOCAL_INLINE(int) blake2s_param_set_leaf_length( blake2s_param *P, const uint32_t leaf_length ) { P->leaf_length = leaf_length; return 0; } -static inline int blake2s_param_set_node_offset( blake2s_param *P, const uint64_t node_offset ) +BLAKE2_LOCAL_INLINE(int) blake2s_param_set_node_offset( blake2s_param *P, const uint64_t node_offset ) { store48( P->node_offset, node_offset ); return 0; } -static inline int blake2s_param_set_node_depth( blake2s_param *P, const uint8_t node_depth ) +BLAKE2_LOCAL_INLINE(int) blake2s_param_set_node_depth( blake2s_param *P, const uint8_t node_depth ) { P->node_depth = node_depth; return 0; } -static inline int blake2s_param_set_inner_length( blake2s_param *P, const uint8_t inner_length ) +BLAKE2_LOCAL_INLINE(int) blake2s_param_set_inner_length( blake2s_param *P, const uint8_t inner_length ) { P->inner_length = inner_length; return 0; } -static inline int blake2s_param_set_salt( blake2s_param *P, const uint8_t salt[BLAKE2S_SALTBYTES] ) +BLAKE2_LOCAL_INLINE(int) blake2s_param_set_salt( blake2s_param *P, const uint8_t salt[BLAKE2S_SALTBYTES] ) { memcpy( P->salt, salt, BLAKE2S_SALTBYTES ); return 0; } -static inline int blake2s_param_set_personal( blake2s_param *P, const uint8_t personal[BLAKE2S_PERSONALBYTES] ) +BLAKE2_LOCAL_INLINE(int) blake2s_param_set_personal( blake2s_param *P, const uint8_t personal[BLAKE2S_PERSONALBYTES] ) { memcpy( P->personal, personal, BLAKE2S_PERSONALBYTES ); return 0; } -static inline int blake2s_init0( blake2s_state *S ) +BLAKE2_LOCAL_INLINE(int) blake2s_init0( blake2s_state *S ) { memset( S, 0, sizeof( blake2s_state ) ); @@ -171,7 +171,7 @@ static inline int blake2s_init0( blake2s_state *S ) /* init2 xors IV with input parameter block */ int blake2s_init_param( blake2s_state *S, const blake2s_param *P ) { - //blake2s_init0( S ); + /*blake2s_init0( S ); */ const uint8_t * v = ( const uint8_t * )( blake2s_IV ); const uint8_t * p = ( const uint8_t * )( P ); uint8_t * h = ( uint8_t * )( S->h ); @@ -187,9 +187,6 @@ int blake2s_init_param( blake2s_state *S, const blake2s_param *P ) /* Some sort of default parameter block initialization, for sequential blake2s */ int blake2s_init( blake2s_state *S, const uint8_t outlen ) { - /* Move interval verification here? */ - if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1; - const blake2s_param P = { outlen, @@ -203,17 +200,14 @@ int blake2s_init( blake2s_state *S, const uint8_t outlen ) {0}, {0} }; + /* Move interval verification here? */ + if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1; return blake2s_init_param( S, &P ); } int blake2s_init_key( blake2s_state *S, const uint8_t outlen, const void *key, const uint8_t keylen ) { - /* Move interval verification here? */ - if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1; - - if ( ( !key ) || ( !keylen ) || keylen > BLAKE2S_KEYBYTES ) return -1; - const blake2s_param P = { outlen, @@ -228,6 +222,11 @@ int blake2s_init_key( blake2s_state *S, const uint8_t outlen, const void *key, c {0} }; + /* Move interval verification here? */ + if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1; + + if ( ( !key ) || ( !keylen ) || keylen > BLAKE2S_KEYBYTES ) return -1; + if( blake2s_init_param( S, &P ) < 0 ) return -1; @@ -242,7 +241,7 @@ int blake2s_init_key( blake2s_state *S, const uint8_t outlen, const void *key, c } -static inline int blake2s_compress( blake2s_state *S, const uint8_t block[BLAKE2S_BLOCKBYTES] ) +BLAKE2_LOCAL_INLINE(int) blake2s_compress( blake2s_state *S, const uint8_t block[BLAKE2S_BLOCKBYTES] ) { __m128i row1, row2, row3, row4; __m128i buf1, buf2, buf3, buf4; @@ -309,19 +308,19 @@ int blake2s_update( blake2s_state *S, const uint8_t *in, uint64_t inlen ) 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, inlen ); - S->buflen += inlen; // Be lazy, do not compress + S->buflen += inlen; /* Be lazy, do not compress */ in += inlen; inlen -= inlen; } |