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
*/
#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
}
#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 ();
+}
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
*/
* @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);