]> source.dussan.org Git - rspamd.git/commitdiff
Add platform detector based on cpuid.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 6 Feb 2015 15:04:41 +0000 (15:04 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 6 Feb 2015 15:04:41 +0000 (15:04 +0000)
src/libcryptobox/CMakeLists.txt
src/libcryptobox/cryptobox.c
src/libcryptobox/cryptobox.h
src/libcryptobox/platform_config.h.in

index cf87c2604569d4348d543a0825107b3d9efe41cd..a7d8e5a892734d7ec57f0692726c1b25cb8b6193 100644 (file)
@@ -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
index 056368e6817767d421a140830081ec84c63d436c..e96f4ec09328d6257e8a852e6e55308a092714e8 100644 (file)
  */
 
 #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 ();
+}
index 60b5d22523f336087be49d3aa13a8b8e01115e5e..565371b709911b8cdf6b4574f4cf574f2abfe5ee 100644 (file)
@@ -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);
 
 
index 46bdb741d41097e320a71ef69513e7c09a4f68f0..d109a7cd0d5ae05c6ba094f005b8bd7fff6ea720 100644 (file)
@@ -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