]> source.dussan.org Git - rspamd.git/commitdiff
Add detection for RDRAND support
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 29 Jan 2016 10:08:04 +0000 (10:08 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 29 Jan 2016 10:08:04 +0000 (10:08 +0000)
src/libcryptobox/cryptobox.c
src/libcryptobox/cryptobox.h
src/libcryptobox/platform_config.h.in
src/libutil/util.c
src/rspamd.h

index 3739097fdf8695f2e63037054d49cc3b4545314a..451d98d053a79be325a94ddac1c399af68388bb9 100644 (file)
@@ -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 ();
index 5dd3b9d60a61d5e2bca24ef306b15759660af69a..4a3d1e1f78134a98e99590e493e807c140b915e2 100644 (file)
@@ -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;
 };
 
 /**
index 577e8528bcf87548ed99c151893914bb8e458716..0eba7cf50992b3b7dca43cee4190b49e512f9927 100644 (file)
 #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
index dc9e0320181d9e6f273b8282f58ada59bd63e20c..7f8272c0eff2c695ca65807d7dc91b4f7e54315f 100644 (file)
@@ -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
index 6126b17a765ab89a1e188563923f07c16a735866..304075c5319dde3ba842c1af4c45448e95a0a326 100644 (file)
@@ -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;
 };