From 609caa681b8d0cf9e58cb2824323d5fe04af6934 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 29 Jan 2016 10:08:04 +0000 Subject: [PATCH] Add detection for RDRAND support --- src/libcryptobox/cryptobox.c | 18 ++++++++++++++++++ src/libcryptobox/cryptobox.h | 9 +++++++++ src/libcryptobox/platform_config.h.in | 7 ------- src/libutil/util.c | 2 +- src/rspamd.h | 2 ++ 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/libcryptobox/cryptobox.c b/src/libcryptobox/cryptobox.c index 3739097fd..451d98d05 100644 --- a/src/libcryptobox/cryptobox.c +++ b/src/libcryptobox/cryptobox.c @@ -130,6 +130,7 @@ static gboolean rspamd_cryptobox_test_instr (gint instr) { void (*old_handler) (int); + guint32 rd; #if defined(__GNUC__) ok = 1; @@ -172,6 +173,14 @@ rspamd_cryptobox_test_instr (gint instr) __asm__ volatile ("vpaddq %ymm0, %ymm0, %ymm0");\ break; #endif + case CPUID_RDRAND: + /* Use byte code here for compatibility */ + __asm__ volatile (".byte 0x0f,0xc7,0xf0; setc %1" + : "=a" (rd), "=qm" (ok) + : + : "edx" + ); + break; default: break; } @@ -231,6 +240,11 @@ rspamd_cryptobox_init (void) cpu_config |= CPUID_SSE41; } } + if ((cpu[2] & ((gint)1 << 30))) { + if (rspamd_cryptobox_test_instr (CPUID_RDRAND)) { + cpu_config |= CPUID_RDRAND; + } + } if (nid > 7) { rspamd_cryptobox_cpuid (cpu, 7); @@ -267,6 +281,9 @@ rspamd_cryptobox_init (void) case CPUID_AVX2: rspamd_printf_gstring (buf, "avx2, "); break; + case CPUID_RDRAND: + rspamd_printf_gstring (buf, "rdrand, "); + break; } } } @@ -278,6 +295,7 @@ rspamd_cryptobox_init (void) ctx->cpu_extensions = buf->str; g_string_free (buf, FALSE); + ctx->cpu_config = cpu_config; ctx->chacha20_impl = chacha_load (); ctx->poly1305_impl = poly1305_load (); diff --git a/src/libcryptobox/cryptobox.h b/src/libcryptobox/cryptobox.h index 5dd3b9d60..4a3d1e1f7 100644 --- a/src/libcryptobox/cryptobox.h +++ b/src/libcryptobox/cryptobox.h @@ -40,6 +40,14 @@ struct rspamd_cryptobox_segment { #define rspamd_cryptobox_HASHKEYBYTES 64 #define rspamd_cryptobox_HASHSTATEBYTES 256 +#define CPUID_AVX2 0x1 +#define CPUID_AVX 0x2 +#define CPUID_SSE2 0x4 +#define CPUID_SSE3 0x8 +#define CPUID_SSSE3 0x10 +#define CPUID_SSE41 0x20 +#define CPUID_RDRAND 0x40 + typedef guchar rspamd_pk_t[rspamd_cryptobox_MAX_PKBYTES]; typedef guchar rspamd_sk_t[rspamd_cryptobox_MAX_SKBYTES]; typedef guchar rspamd_sig_t[rspamd_cryptobox_MAX_MACBYTES]; @@ -54,6 +62,7 @@ struct rspamd_cryptobox_library_ctx { const gchar *poly1305_impl; const gchar *siphash_impl; const gchar *blake2_impl; + unsigned long cpu_config; }; /** diff --git a/src/libcryptobox/platform_config.h.in b/src/libcryptobox/platform_config.h.in index 577e8528b..0eba7cf50 100644 --- a/src/libcryptobox/platform_config.h.in +++ b/src/libcryptobox/platform_config.h.in @@ -12,11 +12,4 @@ #cmakedefine HAVE_SLASHMACRO 1 #cmakedefine HAVE_DOLLARMACRO 1 -#define CPUID_AVX2 0x1 -#define CPUID_AVX 0x2 -#define CPUID_SSE2 0x4 -#define CPUID_SSE3 0x8 -#define CPUID_SSSE3 0x10 -#define CPUID_SSE41 0x20 - #endif \ No newline at end of file diff --git a/src/libutil/util.c b/src/libutil/util.c index dc9e03201..7f8272c0e 100644 --- a/src/libutil/util.c +++ b/src/libutil/util.c @@ -1940,7 +1940,7 @@ rspamd_init_libs (void) struct rspamd_external_libs_ctx *ctx; ctx = g_slice_alloc0 (sizeof (*ctx)); - rspamd_cryptobox_init (); + ctx->crypto_ctx = rspamd_cryptobox_init (); ottery_init (NULL); #ifdef HAVE_LOCALE_H diff --git a/src/rspamd.h b/src/rspamd.h index 6126b17a7..304075c53 100644 --- a/src/rspamd.h +++ b/src/rspamd.h @@ -127,6 +127,7 @@ struct rspamd_classifier_config; struct mime_part; struct rspamd_dns_resolver; struct rspamd_task; +struct rspamd_cryptobox_library_ctx; /** * Server statistics @@ -192,6 +193,7 @@ struct controller_session { struct rspamd_external_libs_ctx { magic_t libmagic; void **local_addrs; + struct rspamd_cryptobox_library_ctx *crypto_ctx; ref_entry_t ref; }; -- 2.39.5