From: Vsevolod Stakhov Date: Fri, 6 Feb 2015 15:04:41 +0000 (+0000) Subject: Add platform detector based on cpuid. X-Git-Tag: 0.9.0~757 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ea46a2f8c7732b13be1e4cee7618c7293706aa55;p=rspamd.git Add platform detector based on cpuid. --- diff --git a/src/libcryptobox/CMakeLists.txt b/src/libcryptobox/CMakeLists.txt index cf87c2604..a7d8e5a89 100644 --- a/src/libcryptobox/CMakeLists.txt +++ b/src/libcryptobox/CMakeLists.txt @@ -3,7 +3,7 @@ INCLUDE(AsmOp.cmake) TARGET_ARCHITECTURE(ARCH) -SET(CHACHASRC chacha20/ref.c) +SET(CHACHASRC chacha20/chacha.c chacha20/ref.c) SET(POLYSRC poly1305/poly1305-donna.c) # For now we support only x86_64 architecture with optimizations diff --git a/src/libcryptobox/cryptobox.c b/src/libcryptobox/cryptobox.c index 056368e68..e96f4ec09 100644 --- a/src/libcryptobox/cryptobox.c +++ b/src/libcryptobox/cryptobox.c @@ -22,14 +22,12 @@ */ #include "cryptobox.h" +#include "platform_config.h" +#include "chacha20/chacha.h" +#include "poly1305/poly1305-donna.h" +#include "curve25519/curve25519.h" -struct rspamd_cryptobox_config { - gboolean has_sse3; - gboolean has_avx; - gboolean has_avx2; -}; - - +unsigned long cpu_config = 0; #ifdef HAVE_WEAK_SYMBOLS __attribute__((weak)) void @@ -60,3 +58,47 @@ rspamd_explicit_memzero(void * const pnt, const gsize len) } #endif } + +static void +rspamd_cryptobox_cpuid (gint cpu[4], gint info) +{ +#if defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__)) + __asm__ __volatile__ ( + "cpuid": + "=a" (cpu[0]), + "=b" (cpu[1]), + "=c" (cpu[2]), + "=d" (cpu[3]) : + "a" (info), "c" (0) + ); +#else + memset (cpu, 0, sizeof (cpu)); +#endif +} + + +void +rspamd_cryptobox_init (void) +{ + gint cpu[4], nid; + + rspamd_cryptobox_cpuid (cpu, 0); + nid = cpu[0]; + + if (nid > 1) { + if ((cpu[3] & ((gint)1 << 26))) { + cpu_config |= CPUID_SSE2; + } + if ((cpu[2] & ((gint)1 << 28))) { + cpu_config |= CPUID_AVX; + } + } + if (nid > 7) { + rspamd_cryptobox_cpuid (cpu, 7); + if ((cpu[1] & ((gint)1 << 5))) { + cpu_config |= CPUID_AVX2; + } + } + + chacha_load (); +} diff --git a/src/libcryptobox/cryptobox.h b/src/libcryptobox/cryptobox.h index 60b5d2252..565371b70 100644 --- a/src/libcryptobox/cryptobox.h +++ b/src/libcryptobox/cryptobox.h @@ -36,11 +36,6 @@ typedef guchar rspamd_sk_t[rspamd_cryptobox_SKBYTES]; typedef guchar rspamd_sig_t[rspamd_cryptobox_MACBYTES]; typedef guchar rspamd_nm_t[rspamd_cryptobox_NMBYTES]; -struct rspamd_encrypt_segment { - guchar *buf; - gsize len; -}; - /** * Init cryptobox library */ @@ -85,7 +80,7 @@ gboolean rspamd_cryptobox_decrypt_inplace (guchar *data, gsize len, * @param sk local secret key * @param sig output signature */ -void rspamd_cryptobox_encrypt_nm_inplace (struct rspamd_encrypt_segment *segments, +void rspamd_cryptobox_encrypt_nm_inplace (guchar *data, gsize len, gsize cnt, const rspamd_nm_t nm, rspamd_sig_t sig); diff --git a/src/libcryptobox/platform_config.h.in b/src/libcryptobox/platform_config.h.in index 46bdb741d..d109a7cd0 100644 --- a/src/libcryptobox/platform_config.h.in +++ b/src/libcryptobox/platform_config.h.in @@ -8,4 +8,8 @@ #cmakedefine HAVE_SLASHMACRO 1 #cmakedefine HAVE_DOLLARMACRO 1 +#define CPUID_AVX2 0x1 +#define CPUID_AVX 0x2 +#define CPUID_SSE2 0x4 + #endif \ No newline at end of file