diff options
-rw-r--r-- | src/libcryptobox/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/libcryptobox/siphash/ref.c | 9 | ||||
-rw-r--r-- | src/libcryptobox/siphash/siphash.c | 27 |
3 files changed, 30 insertions, 9 deletions
diff --git a/src/libcryptobox/CMakeLists.txt b/src/libcryptobox/CMakeLists.txt index 491d7f9b5..ecd729d6a 100644 --- a/src/libcryptobox/CMakeLists.txt +++ b/src/libcryptobox/CMakeLists.txt @@ -54,6 +54,9 @@ IF(HAVE_SSE2) SET(CHACHASRC ${CHACHASRC} ${CMAKE_CURRENT_SOURCE_DIR}/chacha20/sse2.S) SET(POLYSRC ${POLYSRC} ${CMAKE_CURRENT_SOURCE_DIR}/poly1305/sse2.S) ENDIF(HAVE_SSE2) +IF(HAVE_SSE41) + SET(SIPHASHSRC ${SIPHASHSRC} ${CMAKE_CURRENT_SOURCE_DIR}/siphash/sse41.S) +ENDIF(HAVE_SSE41) CONFIGURE_FILE(platform_config.h.in platform_config.h) INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}") diff --git a/src/libcryptobox/siphash/ref.c b/src/libcryptobox/siphash/ref.c index 2b20ae34d..1a09f2066 100644 --- a/src/libcryptobox/siphash/ref.c +++ b/src/libcryptobox/siphash/ref.c @@ -62,8 +62,8 @@ } while(0) -void -siphash_ref (uint8_t *out, const uint8_t *in, uint64_t inlen, const uint8_t *k) +uint64_t +siphash_ref (const unsigned char k[16], const unsigned char *in, const uint64_t inlen) { /* "somepseudorandomlygeneratedbytes" */ uint64_t v0 = 0x736f6d6570736575ULL; @@ -137,7 +137,7 @@ siphash_ref (uint8_t *out, const uint8_t *in, uint64_t inlen, const uint8_t *k) ; b = v0 ^ v1 ^ v2 ^ v3; - U64TO8_LE(out, b); + return b; #ifdef DOUBLE v1 ^= 0xdd; @@ -146,6 +146,7 @@ siphash_ref (uint8_t *out, const uint8_t *in, uint64_t inlen, const uint8_t *k) for( i=0; i<dROUNDS; ++i ) SIPROUND; b = v0 ^ v1 ^ v2 ^ v3; - U64TO8_LE( out+8, b ); + + return b; #endif } diff --git a/src/libcryptobox/siphash/siphash.c b/src/libcryptobox/siphash/siphash.c index f4c8a1854..f42456b7f 100644 --- a/src/libcryptobox/siphash/siphash.c +++ b/src/libcryptobox/siphash/siphash.c @@ -33,11 +33,11 @@ typedef struct siphash_impl_t unsigned long cpu_flags; const char *desc; - void (*siphash)(uint8_t *out, const uint8_t *in, uint64_t inlen, const uint8_t *k); + uint64_t (*siphash) (const unsigned char k[16], const unsigned char *in, const uint64_t inlen); } siphash_impl_t; #define SIPHASH_DECLARE(ext) \ - void siphash_##ext(uint8_t *out, const uint8_t *in, uint64_t inlen, const uint8_t *k); + uint64_t siphash_##ext(const unsigned char k[16], const unsigned char *in, const uint64_t inlen); #define SIPHASH_IMPL(cpuflags, desc, ext) \ {(cpuflags), desc, siphash_##ext} @@ -45,10 +45,17 @@ typedef struct siphash_impl_t SIPHASH_DECLARE(ref) #define SIPHASH_GENERIC SIPHASH_IMPL(0, "generic", ref) +#if defined(HAVE_SSE41) +SIPHASH_DECLARE(sse41) +#define SIPHASH_SSE41 SIPHASH_IMPL(CPUID_SSE41, "sse41", sse41) +#endif /* list implemenations from most optimized to least, with generic as the last entry */ static const siphash_impl_t siphash_list[] = { SIPHASH_GENERIC, +#if defined(SIPHASH_SSE41) + SIPHASH_SSE41, +#endif }; static const siphash_impl_t *siphash_opt = &siphash_list[0]; @@ -66,17 +73,22 @@ siphash_load(void) } } } + fprintf(stderr, "selected %s\n", siphash_opt->desc); } void siphash24 (unsigned char *out, const unsigned char *in, unsigned long long inlen, const unsigned char *k) { - siphash_opt->siphash (out, in, inlen, k); + uint64_t r; + + r = siphash_opt->siphash (k, in, inlen); + memcpy (out, &r, sizeof (r)); } size_t -siphash24_test (void) { +siphash24_test (bool generic) +{ static const unsigned char vectors[64][8] = { { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, }, { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, }, @@ -159,7 +171,12 @@ siphash24_test (void) { for (i = 0; i < sizeof in; ++i) { in[i] = i; - siphash24 (r.c, in, i, k); + if (generic) { + r.m = siphash_list[0].siphash (k, in, i); + } + else { + r.m = siphash_opt->siphash (k, in, i); + } if (memcmp (r.c, vectors[i], sizeof (r)) != 0) { return 0; } |