From e273f67fdfd1cfeb4b1db7d6d8160b9b0c90df98 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 29 Jan 2016 09:53:19 +0000 Subject: [PATCH] Output configuration of libcryptobox --- src/libcryptobox/blake2/blake2.c | 4 +- src/libcryptobox/blake2/blake2.h | 2 +- src/libcryptobox/chacha20/chacha.c | 4 +- src/libcryptobox/chacha20/chacha.h | 2 +- src/libcryptobox/cryptobox.c | 56 +++++++++++++++++++++--- src/libcryptobox/cryptobox.h | 11 ++++- src/libcryptobox/curve25519/curve25519.c | 5 ++- src/libcryptobox/curve25519/curve25519.h | 2 +- src/libcryptobox/poly1305/poly1305.c | 5 ++- src/libcryptobox/poly1305/poly1305.h | 2 +- src/libcryptobox/siphash/siphash.c | 4 +- src/libcryptobox/siphash/siphash.h | 2 +- 12 files changed, 81 insertions(+), 18 deletions(-) diff --git a/src/libcryptobox/blake2/blake2.c b/src/libcryptobox/blake2/blake2.c index addaf61f5..ecc465b35 100644 --- a/src/libcryptobox/blake2/blake2.c +++ b/src/libcryptobox/blake2/blake2.c @@ -278,7 +278,7 @@ blake2b_keyed (unsigned char *hash, blake2b_final (&S, hash); } -void +const char* blake2b_load (void) { guint i; @@ -291,4 +291,6 @@ blake2b_load (void) } } } + + return blake2b_opt->desc; } diff --git a/src/libcryptobox/blake2/blake2.h b/src/libcryptobox/blake2/blake2.h index 37f1dbb4b..903a6ed0e 100644 --- a/src/libcryptobox/blake2/blake2.h +++ b/src/libcryptobox/blake2/blake2.h @@ -65,7 +65,7 @@ void blake2b_keyed (unsigned char *hash, const unsigned char *key, size_t keylen); -void blake2b_load (void); +const char* blake2b_load (void); #if defined(__cplusplus) } diff --git a/src/libcryptobox/chacha20/chacha.c b/src/libcryptobox/chacha20/chacha.c index b349d9ff9..4bb2098a3 100644 --- a/src/libcryptobox/chacha20/chacha.c +++ b/src/libcryptobox/chacha20/chacha.c @@ -89,7 +89,7 @@ chacha_is_aligned (const void *p) return ((size_t) p & (sizeof(size_t) - 1)) == 0; } -void +const char * chacha_load (void) { guint i; @@ -102,6 +102,8 @@ chacha_load (void) } } } + + return chacha_impl->desc; } void chacha_init (chacha_state *S, const chacha_key *key, diff --git a/src/libcryptobox/chacha20/chacha.h b/src/libcryptobox/chacha20/chacha.h index cd2a63bd9..f69a63db9 100644 --- a/src/libcryptobox/chacha20/chacha.h +++ b/src/libcryptobox/chacha20/chacha.h @@ -74,6 +74,6 @@ void xchacha (const chacha_key *key, const chacha_iv24 *iv, const unsigned char *in, unsigned char *out, size_t inlen, size_t rounds); -void chacha_load (void); +const char* chacha_load (void); #endif /* CHACHA_H_ */ diff --git a/src/libcryptobox/cryptobox.c b/src/libcryptobox/cryptobox.c index 37b8704df..3739097fd 100644 --- a/src/libcryptobox/cryptobox.c +++ b/src/libcryptobox/cryptobox.c @@ -36,6 +36,7 @@ #include "blake2/blake2.h" #include "siphash/siphash.h" #include "ottery.h" +#include "printf.h" #ifdef HAVE_CPUID_H #include @@ -182,17 +183,22 @@ rspamd_cryptobox_test_instr (gint instr) return ok == 1; } -void +struct rspamd_cryptobox_library_ctx* rspamd_cryptobox_init (void) { gint cpu[4], nid; + gulong bit; + static struct rspamd_cryptobox_library_ctx *ctx; + GString *buf; if (cryptobox_loaded) { /* Ignore reload attempts */ - return; + return ctx; } cryptobox_loaded = TRUE; + ctx = g_malloc0 (sizeof (*ctx)); + rspamd_cryptobox_cpuid (cpu, 0); nid = cpu[0]; rspamd_cryptobox_cpuid (cpu, 1); @@ -238,12 +244,48 @@ rspamd_cryptobox_init (void) } } + buf = g_string_new (""); + + for (bit = 0x1; bit != 0; bit <<= 1) { + if (cpu_config & bit) { + switch (bit) { + case CPUID_SSE2: + rspamd_printf_gstring (buf, "sse2, "); + break; + case CPUID_SSE3: + rspamd_printf_gstring (buf, "sse3, "); + break; + case CPUID_SSSE3: + rspamd_printf_gstring (buf, "ssse3, "); + break; + case CPUID_SSE41: + rspamd_printf_gstring (buf, "sse4.1, "); + break; + case CPUID_AVX: + rspamd_printf_gstring (buf, "avx, "); + break; + case CPUID_AVX2: + rspamd_printf_gstring (buf, "avx2, "); + break; + } + } + } + + if (buf->len > 2) { + /* Trim last chars */ + g_string_erase (buf, buf->len - 2, 2); + } + + ctx->cpu_extensions = buf->str; + g_string_free (buf, FALSE); + + ctx->chacha20_impl = chacha_load (); + ctx->poly1305_impl = poly1305_load (); + ctx->siphash_impl = siphash_load (); + ctx->curve25519_impl = curve25519_load (); + ctx->blake2_impl = blake2b_load (); - chacha_load (); - poly1305_load (); - siphash_load (); - curve25519_load (); - blake2b_load (); + return ctx; } void diff --git a/src/libcryptobox/cryptobox.h b/src/libcryptobox/cryptobox.h index 7977bd0e6..5dd3b9d60 100644 --- a/src/libcryptobox/cryptobox.h +++ b/src/libcryptobox/cryptobox.h @@ -47,10 +47,19 @@ typedef guchar rspamd_nm_t[rspamd_cryptobox_MAX_NMBYTES]; typedef guchar rspamd_nonce_t[rspamd_cryptobox_MAX_NONCEBYTES]; typedef guchar rspamd_sipkey_t[rspamd_cryptobox_SIPKEYBYTES]; +struct rspamd_cryptobox_library_ctx { + gchar *cpu_extensions; + const gchar *curve25519_impl; + const gchar *chacha20_impl; + const gchar *poly1305_impl; + const gchar *siphash_impl; + const gchar *blake2_impl; +}; + /** * Init cryptobox library */ -void rspamd_cryptobox_init (void); +struct rspamd_cryptobox_library_ctx* rspamd_cryptobox_init (void); /** * Generate new keypair diff --git a/src/libcryptobox/curve25519/curve25519.c b/src/libcryptobox/curve25519/curve25519.c index 220a1da13..62a253069 100644 --- a/src/libcryptobox/curve25519/curve25519.c +++ b/src/libcryptobox/curve25519/curve25519.c @@ -119,7 +119,7 @@ curve25519_test_impl (const curve25519_impl_t *impl) return TRUE; } -void +const char* curve25519_load (void) { guint i; @@ -132,7 +132,10 @@ curve25519_load (void) } } } + g_assert (curve25519_test_impl (curve25519_opt)); + + return curve25519_opt->desc; } int diff --git a/src/libcryptobox/curve25519/curve25519.h b/src/libcryptobox/curve25519/curve25519.h index 8ea440b40..8b404f5c6 100644 --- a/src/libcryptobox/curve25519/curve25519.h +++ b/src/libcryptobox/curve25519/curve25519.h @@ -8,6 +8,6 @@ static const guchar curve25519_basepoint[32] = {9}; int curve25519 (guchar *mypublic, const guchar *secret, const guchar *basepoint); /* Call for optimized implementation of scalarmult if needed */ int curve25519_base (guchar *mypublic, const guchar *secret); -void curve25519_load (void); +const char* curve25519_load (void); #endif diff --git a/src/libcryptobox/poly1305/poly1305.c b/src/libcryptobox/poly1305/poly1305.c index c98b28017..9e1eed04e 100644 --- a/src/libcryptobox/poly1305/poly1305.c +++ b/src/libcryptobox/poly1305/poly1305.c @@ -101,7 +101,8 @@ static int poly1305_is_aligned(const void *p) return ((size_t) p & (sizeof(size_t) - 1)) == 0; } -void poly1305_load(void) +const char* +poly1305_load(void) { guint i; @@ -113,6 +114,8 @@ void poly1305_load(void) } } } + + return poly1305_opt->desc; } /* processes inlen bytes (full blocks only), handling input alignment */ diff --git a/src/libcryptobox/poly1305/poly1305.h b/src/libcryptobox/poly1305/poly1305.h index 8eae97c88..902a9c288 100644 --- a/src/libcryptobox/poly1305/poly1305.h +++ b/src/libcryptobox/poly1305/poly1305.h @@ -28,7 +28,7 @@ void poly1305_auth(unsigned char *mac, const unsigned char *in, size_t inlen, const poly1305_key *key); int poly1305_verify(const unsigned char mac1[16], const unsigned char mac2[16]); -void poly1305_load(void); +const char* poly1305_load(void); #if defined(__cplusplus) } diff --git a/src/libcryptobox/siphash/siphash.c b/src/libcryptobox/siphash/siphash.c index 32907292a..43effe409 100644 --- a/src/libcryptobox/siphash/siphash.c +++ b/src/libcryptobox/siphash/siphash.c @@ -61,7 +61,7 @@ static const siphash_impl_t siphash_list[] = { static const siphash_impl_t *siphash_opt = &siphash_list[0]; -void +const char * siphash_load(void) { guint i; @@ -74,6 +74,8 @@ siphash_load(void) } } } + + return siphash_opt->desc; } void siphash24 (unsigned char *out, const unsigned char *in, diff --git a/src/libcryptobox/siphash/siphash.h b/src/libcryptobox/siphash/siphash.h index 667f3919f..d4ec5af4a 100644 --- a/src/libcryptobox/siphash/siphash.h +++ b/src/libcryptobox/siphash/siphash.h @@ -29,7 +29,7 @@ extern "C" { #endif -void siphash_load (void); +const char* siphash_load (void); void siphash24 (unsigned char *out, const unsigned char *in, unsigned long long inlen, -- 2.39.5