diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-03-19 12:54:51 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-03-19 13:25:58 +0000 |
commit | ae95563c52ef1b47454cc4043cbf3ceab0d8a928 (patch) | |
tree | 36886f3bc8ccd22ebff0cd0a4e1e800466fac7c3 | |
parent | 91db59c5972b4184fd2a3b4e81899c1c204e081c (diff) | |
download | rspamd-ae95563c52ef1b47454cc4043cbf3ceab0d8a928.tar.gz rspamd-ae95563c52ef1b47454cc4043cbf3ceab0d8a928.zip |
[Minor] Add constant time memcmp function
-rw-r--r-- | src/libcryptobox/cryptobox.c | 54 | ||||
-rw-r--r-- | src/libcryptobox/cryptobox.h | 10 |
2 files changed, 58 insertions, 6 deletions
diff --git a/src/libcryptobox/cryptobox.c b/src/libcryptobox/cryptobox.c index 7fe1f5418..d99dd912d 100644 --- a/src/libcryptobox/cryptobox.c +++ b/src/libcryptobox/cryptobox.c @@ -65,18 +65,34 @@ static gboolean cryptobox_loaded = FALSE; static const guchar n0[16] = {0}; +#define CRYPTOBOX_ALIGNMENT 16 +#define cryptobox_align_ptr(p, a) \ + (void *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1)) + #ifdef HAVE_WEAK_SYMBOLS __attribute__((weak)) void -_dummy_symbol_to_prevent_lto(void * const pnt, const size_t len) +_dummy_symbol_to_prevent_lto_memzero(void * const pnt, const size_t len); +__attribute__((weak)) void +_dummy_symbol_to_prevent_lto_memzero(void * const pnt, const size_t len) { (void) pnt; (void) len; } -#endif -#define CRYPTOBOX_ALIGNMENT 32 /* Better for AVX */ -#define cryptobox_align_ptr(p, a) \ - (void *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1)) +__attribute__((weak)) void +_dummy_symbol_to_prevent_lto_memcmp(const unsigned char *b1, + const unsigned char *b2, + const size_t len); +__attribute__((weak)) void +_dummy_symbol_to_prevent_lto_memcmp(const unsigned char *b1, + const unsigned char *b2, + const size_t len) +{ + (void) b1; + (void) b2; + (void) len; +} +#endif void rspamd_explicit_memzero(void * const pnt, const gsize len) @@ -89,7 +105,7 @@ rspamd_explicit_memzero(void * const pnt, const gsize len) explicit_bzero (pnt, len); #elif defined(HAVE_WEAK_SYMBOLS) memset (pnt, 0, len); - _dummy_symbol_to_prevent_lto (pnt, len); + _dummy_symbol_to_prevent_lto_memzero (pnt, len); #else volatile unsigned char *pnt_ = (volatile unsigned char *) pnt; gsize i = (gsize) 0U; @@ -99,6 +115,32 @@ rspamd_explicit_memzero(void * const pnt, const gsize len) #endif } +gint +rspamd_cryptobox_memcmp (const void *const b1_, const void *const b2_, gsize len) +{ +#ifdef HAVE_WEAK_SYMBOLS + const unsigned char *b1 = (const unsigned char *) b1_; + const unsigned char *b2 = (const unsigned char *) b2_; +#else + const volatile unsigned char *volatile b1 = + (const volatile unsigned char *volatile) b1_; + const volatile unsigned char *volatile b2 = + (const volatile unsigned char *volatile) b2_; +#endif + gsize i; + volatile unsigned char d = 0U; + +#if HAVE_WEAK_SYMBOLS + _dummy_symbol_to_prevent_lto_memcmp (b1, b2, len); +#endif + + for (i = 0U; i < len; i++) { + d |= b1[i] ^ b2[i]; + } + + return (1 & ((d - 1) >> 8)) - 1; +} + static void rspamd_cryptobox_cpuid (gint cpu[4], gint info) { diff --git a/src/libcryptobox/cryptobox.h b/src/libcryptobox/cryptobox.h index 9c2665671..82438b788 100644 --- a/src/libcryptobox/cryptobox.h +++ b/src/libcryptobox/cryptobox.h @@ -227,6 +227,16 @@ bool rspamd_cryptobox_verify (const guchar *sig, void rspamd_explicit_memzero (void * const buf, gsize buflen); /** + * Constant time memcmp + * @param b1_ + * @param b2_ + * @param len + * @return + */ +gint +rspamd_cryptobox_memcmp (const void *const b1_, const void *const b2_, gsize len); + +/** * Calculates siphash-2-4 for a message * @param out (8 bytes output) * @param in |