Set the impl of the Blake2b compression function once, at init time.

This commit is contained in:
Frank Denis 2015-11-01 11:34:32 +01:00
parent d62451ce9f
commit 384dd997a8
5 changed files with 47 additions and 46 deletions

View File

@ -19,15 +19,16 @@
#include "crypto_generichash_blake2b.h"
#define blake2b_init_param crypto_generichash_blake2b__init_param
#define blake2b_init crypto_generichash_blake2b__init
#define blake2b_init_salt_personal crypto_generichash_blake2b__init_salt_personal
#define blake2b_init_key crypto_generichash_blake2b__init_key
#define blake2b_init_key_salt_personal crypto_generichash_blake2b__init_key_salt_personal
#define blake2b_update crypto_generichash_blake2b__update
#define blake2b_final crypto_generichash_blake2b__final
#define blake2b crypto_generichash_blake2b__blake2b
#define blake2b_salt_personal crypto_generichash_blake2b__blake2b_salt_personal
#define blake2b_init_param crypto_generichash_blake2b__init_param
#define blake2b_init crypto_generichash_blake2b__init
#define blake2b_init_salt_personal crypto_generichash_blake2b__init_salt_personal
#define blake2b_init_key crypto_generichash_blake2b__init_key
#define blake2b_init_key_salt_personal crypto_generichash_blake2b__init_key_salt_personal
#define blake2b_update crypto_generichash_blake2b__update
#define blake2b_final crypto_generichash_blake2b__final
#define blake2b crypto_generichash_blake2b__blake2b
#define blake2b_salt_personal crypto_generichash_blake2b__blake2b_salt_personal
#define blake2b_pick_best_implementation crypto_generichash_blake2b__pick_best_implementation
#if defined(_MSC_VER)
#define ALIGN(x) __declspec(align(x))
@ -180,6 +181,7 @@ typedef crypto_generichash_blake2b_state blake2b_state;
}
typedef int ( *blake2b_compress_fn )( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] );
int blake2b_pick_best_implementation(void);
int blake2b_compress_ref( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] );
int blake2b_compress_ssse3( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] );
int blake2b_compress_sse41( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] );

View File

@ -1,5 +1,5 @@
/*
BLAKE2 reference source code package - reference C implementations
BLAKE2 reference source code package - C implementations
Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
@ -19,6 +19,8 @@
#include "blake2-impl.h"
#include "runtime.h"
static blake2b_compress_fn blake2b_compress = blake2b_compress_ref;
static const uint64_t blake2b_IV[8] =
{
0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
@ -279,23 +281,6 @@ int blake2b_init_key_salt_personal( blake2b_state *S, const uint8_t outlen, cons
/* inlen now in bytes */
int blake2b_update( blake2b_state *S, const uint8_t *in, uint64_t inlen )
{
blake2b_compress_fn blake2b_compress;
do {
#if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) && defined(HAVE_SMMINTRIN_H)
if (sodium_runtime_has_sse41()) {
blake2b_compress = blake2b_compress_sse41;
break;
}
#endif
#if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H)
if (sodium_runtime_has_ssse3()) {
blake2b_compress = blake2b_compress_ssse3;
break;
}
#endif
blake2b_compress = blake2b_compress_ref;
} while(0);
while( inlen > 0 )
{
size_t left = S->buflen;
@ -326,26 +311,9 @@ int blake2b_update( blake2b_state *S, const uint8_t *in, uint64_t inlen )
int blake2b_final( blake2b_state *S, uint8_t *out, uint8_t outlen )
{
blake2b_compress_fn blake2b_compress;
if( !outlen || outlen > BLAKE2B_OUTBYTES ) {
return -1;
}
do {
#if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) && defined(HAVE_SMMINTRIN_H)
if (sodium_runtime_has_sse41()) {
blake2b_compress = blake2b_compress_sse41;
break;
}
#endif
#if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H)
if (sodium_runtime_has_ssse3()) {
blake2b_compress = blake2b_compress_ssse3;
break;
}
#endif
blake2b_compress = blake2b_compress_ref;
} while(0);
if( S->buflen > BLAKE2B_BLOCKBYTES )
{
blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
@ -433,3 +401,23 @@ int blake2b_salt_personal( uint8_t *out, const void *in, const void *key, const
blake2b_final( S, out, outlen );
return 0;
}
int
blake2b_pick_best_implementation(void)
{
#if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) && defined(HAVE_SMMINTRIN_H)
if (sodium_runtime_has_sse41()) {
blake2b_compress = blake2b_compress_sse41;
return 0;
}
#endif
#if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H)
if (sodium_runtime_has_ssse3()) {
blake2b_compress = blake2b_compress_ssse3;
return 0;
}
#endif
blake2b_compress = blake2b_compress_ref;
return 0;
}

View File

@ -106,3 +106,9 @@ crypto_generichash_blake2b_final(crypto_generichash_blake2b_state *state,
assert(outlen <= UINT8_MAX);
return blake2b_final(state, (uint8_t *) out, (uint8_t) outlen);
}
int
_crypto_generichash_blake2b_pick_best_implementation(void)
{
return blake2b_pick_best_implementation();
}

View File

@ -104,6 +104,10 @@ int crypto_generichash_blake2b_final(crypto_generichash_blake2b_state *state,
unsigned char *out,
const size_t outlen);
/* ------------------------------------------------------------------------- */
int _crypto_generichash_blake2b_pick_best_implementation(void);
#ifdef __cplusplus
}
#endif

View File

@ -14,11 +14,12 @@ sodium_init(void)
return 1;
}
sodium_runtime_get_cpu_features();
randombytes_stir();
_sodium_alloc_init();
_crypto_generichash_blake2b_pick_best_implementation();
if (crypto_onetimeauth_pick_best_implementation() == NULL) {
return -1; /* LCOV_EXCL_LINE */
}
randombytes_stir();
_sodium_alloc_init();
initialized = 1;
return 0;