diff options
author | David Carlier <dcarlier@afilias.info> | 2019-05-23 04:32:44 (GMT) |
---|---|---|
committer | Inada Naoki <songofacandy@gmail.com> | 2019-05-23 04:32:44 (GMT) |
commit | 51aa35e9e17eef60d04add9619fe2a7eb938358c (patch) | |
tree | 87251bf2800e940f6872fa45d84342bb1dc455ec /Modules/_blake2/impl/blake2s.c | |
parent | 20e1e2582e5e69e43af88ff58699c8883d146acb (diff) | |
download | cpython-51aa35e9e17eef60d04add9619fe2a7eb938358c.zip cpython-51aa35e9e17eef60d04add9619fe2a7eb938358c.tar.gz cpython-51aa35e9e17eef60d04add9619fe2a7eb938358c.tar.bz2 |
bpo-33164: update blake2 implementation (GH-6286)
Diffstat (limited to 'Modules/_blake2/impl/blake2s.c')
-rw-r--r-- | Modules/_blake2/impl/blake2s.c | 215 |
1 files changed, 101 insertions, 114 deletions
diff --git a/Modules/_blake2/impl/blake2s.c b/Modules/_blake2/impl/blake2s.c index 69385dc..0c3636e 100644 --- a/Modules/_blake2/impl/blake2s.c +++ b/Modules/_blake2/impl/blake2s.c @@ -1,16 +1,14 @@ /* BLAKE2 reference source code package - optimized 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> @@ -22,18 +20,32 @@ #include "blake2-config.h" +#if defined(_MSC_VER) +#include <intrin.h> +#endif +#if defined(HAVE_SSE2) #include <emmintrin.h> +// MSVC only defines _mm_set_epi64x for x86_64... +#if defined(_MSC_VER) && !defined(_M_X64) +static inline __m128i _mm_set_epi64x( const uint64_t u1, const uint64_t u0 ) +{ + return _mm_set_epi32( u1 >> 32, u1, u0 >> 32, u0 ); +} +#endif +#endif + + #if defined(HAVE_SSSE3) #include <tmmintrin.h> #endif -#if defined(HAVE_SSE41) +#if defined(HAVE_SSE4_1) #include <smmintrin.h> #endif #if defined(HAVE_AVX) #include <immintrin.h> #endif -#if defined(HAVE_XOP) +#if defined(HAVE_XOP) && !defined(_MSC_VER) #include <x86intrin.h> #endif @@ -61,40 +73,35 @@ static const uint8_t blake2s_sigma[10][16] = /* Some helper functions, not necessarily useful */ -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; } -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 ) { uint64_t t = ( ( uint64_t )S->t[1] << 32 ) | S->t[0]; t += inc; @@ -104,91 +111,114 @@ BLAKE2_LOCAL_INLINE(int) blake2s_increment_counter( blake2s_state *S, const uint } -/* 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 ) { 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; } +#define blake2s_init BLAKE2_IMPL_NAME(blake2s_init) +#define blake2s_init_param BLAKE2_IMPL_NAME(blake2s_init_param) +#define blake2s_init_key BLAKE2_IMPL_NAME(blake2s_init_key) +#define blake2s_update BLAKE2_IMPL_NAME(blake2s_update) +#define blake2s_final BLAKE2_IMPL_NAME(blake2s_final) +#define blake2s BLAKE2_IMPL_NAME(blake2s) + +#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 ) { - /*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 ); - int i; + uint8_t *p, *h, *v; + //blake2s_init0( S ); + v = ( uint8_t * )( blake2s_IV ); + h = ( uint8_t * )( S->h ); + p = ( uint8_t * )( P ); /* IV XOR ParamBlock */ memset( S, 0, sizeof( blake2s_state ) ); - for( i = 0; i < BLAKE2S_OUTBYTES; ++i ) h[i] = v[i] ^ p[i]; + for( int i = 0; i < BLAKE2S_OUTBYTES; ++i ) h[i] = v[i] ^ p[i]; + S->outlen = P->digest_length; return 0; } /* Some sort of default parameter block initialization, for sequential blake2s */ -int blake2s_init( blake2s_state *S, const uint8_t outlen ) +int blake2s_init( blake2s_state *S, size_t outlen ) { + if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1; + const blake2s_param P = { outlen, @@ -202,14 +232,16 @@ 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 ) +int blake2s_init_key( blake2s_state *S, size_t outlen, const void *key, size_t keylen ) { + if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1; + + if ( ( !key ) || ( !keylen ) || keylen > BLAKE2S_KEYBYTES ) return -1; + const blake2s_param P = { outlen, @@ -224,11 +256,6 @@ 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; @@ -243,11 +270,11 @@ int blake2s_init_key( blake2s_state *S, const uint8_t outlen, const void *key, c } -BLAKE2_LOCAL_INLINE(int) blake2s_compress( blake2s_state *S, const uint8_t block[BLAKE2S_BLOCKBYTES] ) +static inline int blake2s_compress( blake2s_state *S, const uint8_t block[BLAKE2S_BLOCKBYTES] ) { __m128i row1, row2, row3, row4; __m128i buf1, buf2, buf3, buf4; -#if defined(HAVE_SSE41) +#if defined(HAVE_SSE4_1) __m128i t0, t1; #if !defined(HAVE_XOP) __m128i t2; @@ -258,7 +285,7 @@ BLAKE2_LOCAL_INLINE(int) blake2s_compress( blake2s_state *S, const uint8_t block const __m128i r8 = _mm_set_epi8( 12, 15, 14, 13, 8, 11, 10, 9, 4, 7, 6, 5, 0, 3, 2, 1 ); const __m128i r16 = _mm_set_epi8( 13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0, 3, 2 ); #endif -#if defined(HAVE_SSE41) +#if defined(HAVE_SSE4_1) const __m128i m0 = LOADU( block + 00 ); const __m128i m1 = LOADU( block + 16 ); const __m128i m2 = LOADU( block + 32 ); @@ -300,8 +327,8 @@ BLAKE2_LOCAL_INLINE(int) blake2s_compress( blake2s_state *S, const uint8_t block return 0; } -/* inlen now in bytes */ -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 ) { @@ -310,11 +337,11 @@ 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; @@ -322,7 +349,7 @@ int blake2s_update( blake2s_state *S, const uint8_t *in, uint64_t inlen ) 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; } @@ -331,24 +358,19 @@ int blake2s_update( blake2s_state *S, const uint8_t *in, uint64_t inlen ) return 0; } -/* Is this correct? */ -int blake2s_final( blake2s_state *S, uint8_t *out, uint8_t outlen ) -{ - uint8_t buffer[BLAKE2S_OUTBYTES] = {0}; - int i; - if( outlen > BLAKE2S_OUTBYTES ) - return -1; +int blake2s_final( blake2s_state *S, uint8_t *out, size_t outlen ) +{ + uint8_t buffer[BLAKE2S_OUTBYTES]; - if( blake2s_is_lastblock( S ) ) - return -1; + if(outlen != S->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 ); @@ -356,15 +378,14 @@ int blake2s_final( blake2s_state *S, uint8_t *out, uint8_t outlen ) memset( S->buf + S->buflen, 0, 2 * BLAKE2S_BLOCKBYTES - S->buflen ); /* Padding */ blake2s_compress( S, S->buf ); - for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */ + for( int 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; } -/* inlen, at least, should be uint64_t. Others can be size_t. */ -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]; @@ -388,48 +409,14 @@ 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]; - - if( blake2s( hash, buf, key, BLAKE2S_OUTBYTES, i, BLAKE2S_KEYBYTES ) < 0 || - 0 != memcmp( hash, blake2s_keyed_kat[i], BLAKE2S_OUTBYTES ) ) - { - puts( "error" ); - return -1; - } - } - - puts( "ok" ); - return 0; + return blake2s( out, in, NULL, BLAKE2S_OUTBYTES, (size_t)inlen, 0 ); } #endif - |