diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-10-12 13:57:28 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-10-12 13:57:28 +0100 |
commit | 49f9e007f17ed552b1aad8d608c01f374463c1b2 (patch) | |
tree | 9d73a26d236a0f1cb1a7a96dff628465e24109ec /src/libcryptobox | |
parent | db1f808884e11639306010ec629abb2c9c370393 (diff) | |
download | rspamd-49f9e007f17ed552b1aad8d608c01f374463c1b2.tar.gz rspamd-49f9e007f17ed552b1aad8d608c01f374463c1b2.zip |
Rework curve25519 library for modular design.
Diffstat (limited to 'src/libcryptobox')
-rw-r--r-- | src/libcryptobox/CMakeLists.txt | 9 | ||||
-rw-r--r-- | src/libcryptobox/cryptobox.c | 1 | ||||
-rw-r--r-- | src/libcryptobox/curve25519/curve25519-donna-c64.c | 4 | ||||
-rw-r--r-- | src/libcryptobox/curve25519/curve25519-donna.c | 3 | ||||
-rw-r--r-- | src/libcryptobox/curve25519/curve25519.c | 97 | ||||
-rw-r--r-- | src/libcryptobox/curve25519/curve25519.h | 1 |
6 files changed, 107 insertions, 8 deletions
diff --git a/src/libcryptobox/CMakeLists.txt b/src/libcryptobox/CMakeLists.txt index 0b591bfc1..5a172532a 100644 --- a/src/libcryptobox/CMakeLists.txt +++ b/src/libcryptobox/CMakeLists.txt @@ -39,17 +39,20 @@ IF(${ARCH} STREQUAL "x86_64") TEST1 xorl ") ASM_OP(HAVE_DOLLARMACRO "dollar macro convention") - - SET(CURVESRC ${CMAKE_CURRENT_SOURCE_DIR}/curve25519/curve25519-donna-c64.c) + SET(POLYSRC ${POLYSRC} ${CMAKE_CURRENT_SOURCE_DIR}/poly1305/ref-64.c) ELSEIF(${ARCH} STREQUAL "i386") SET(POLYSRC ${POLYSRC} ${CMAKE_CURRENT_SOURCE_DIR}/poly1305/ref-32.c) - SET(CURVESRC ${CMAKE_CURRENT_SOURCE_DIR}/curve25519/curve25519-donna.c) + ELSE() SET(CURVESRC ${CMAKE_CURRENT_SOURCE_DIR}/curve25519/ref.c) SET(POLYSRC ${POLYSRC} ${CMAKE_CURRENT_SOURCE_DIR}/poly1305/ref-32.c) ENDIF() +SET(CURVESRC ${CMAKE_CURRENT_SOURCE_DIR}/curve25519/curve25519-donna-c64.c + ${CMAKE_CURRENT_SOURCE_DIR}/curve25519/curve25519-donna.c + ${CMAKE_CURRENT_SOURCE_DIR}/curve25519/curve25519.c) + IF(HAVE_AVX2) SET(CHACHASRC ${CHACHASRC} ${CMAKE_CURRENT_SOURCE_DIR}/chacha20/avx2.S) SET(POLYSRC ${POLYSRC} ${CMAKE_CURRENT_SOURCE_DIR}/poly1305/avx2.S) diff --git a/src/libcryptobox/cryptobox.c b/src/libcryptobox/cryptobox.c index e4a17f7d4..5e344d46c 100644 --- a/src/libcryptobox/cryptobox.c +++ b/src/libcryptobox/cryptobox.c @@ -138,6 +138,7 @@ rspamd_cryptobox_init (void) chacha_load (); poly1305_load (); siphash_load (); + curve25519_load (); } void diff --git a/src/libcryptobox/curve25519/curve25519-donna-c64.c b/src/libcryptobox/curve25519/curve25519-donna-c64.c index e6a5045ad..d98c385c2 100644 --- a/src/libcryptobox/curve25519/curve25519-donna-c64.c +++ b/src/libcryptobox/curve25519/curve25519-donna-c64.c @@ -477,9 +477,7 @@ static void crecip (felem out, const felem z) /* 2^255 - 21 */fmul (out, t0, a); } -int curve25519 (u8 *, const u8 *, const u8 *); - -int curve25519 (u8 *mypublic, const u8 *secret, const u8 *basepoint) +int scalarmult_donna64 (u8 *mypublic, const u8 *secret, const u8 *basepoint) { limb bp[5], x[5], z[5], zmone[5]; int i; diff --git a/src/libcryptobox/curve25519/curve25519-donna.c b/src/libcryptobox/curve25519/curve25519-donna.c index e6a50b87f..b3b59d860 100644 --- a/src/libcryptobox/curve25519/curve25519-donna.c +++ b/src/libcryptobox/curve25519/curve25519-donna.c @@ -891,10 +891,9 @@ static void crecip (limb *out, const limb *z) /* 2^255 - 21 */fmul (out, t1, z11); } -int curve25519 (u8 *mypublic, const u8 *secret, const u8 *basepoint) +int scalarmult_donna32 (u8 *mypublic, const u8 *secret, const u8 *basepoint) { limb bp[10], x[10], z[11], zmone[10]; - int i; fexpand (bp, basepoint); cmult (x, z, secret, bp); diff --git a/src/libcryptobox/curve25519/curve25519.c b/src/libcryptobox/curve25519/curve25519.c new file mode 100644 index 000000000..1870d4987 --- /dev/null +++ b/src/libcryptobox/curve25519/curve25519.c @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2015, Vsevolod Stakhov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "cryptobox.h" +#include "curve25519.h" +#include "platform_config.h" + +extern unsigned long cpu_config; + +typedef struct curve25519_impl_s { + unsigned long cpu_flags; + const char *desc; + + void (*scalarmult) (guint8 *mypublic, + const guint8 *secret, + const guint8 *basepoint); +} curve25519_impl_t; + +#define CURVE25519_DECLARE(ext) \ + void scalarmult_##ext(guint8 *mypublic, const guint8 *secret, const guint8 *basepoint) + +#define CURVE25519_IMPL(cpuflags, desc, ext) \ + {(cpuflags), desc, scalarmult_##ext} + +#if defined(__LP64__) + +#if defined(HAVE_AVX) +CURVE25519_DECLARE(avx); +#define CURVE25519_AVX CURVE25519_IMPL(CPUID_AVX, "avx", avx) +#endif + +#endif + +#if !defined(__LP64__) +CURVE25519_DECLARE(donna32); +#define CURVE25519_GENERIC CURVE25519_IMPL(0, "donna32", donna32) +#else +CURVE25519_DECLARE(donna64); +#define CURVE25519_GENERIC CURVE25519_IMPL(0, "donna64", donna64) +#endif + +static const curve25519_impl_t curve25519_list[] = { + CURVE25519_GENERIC, +#if defined(CURVE25519_AVX) + CURVE25519_AVX, +#endif +}; + +static const curve25519_impl_t *curve25519_opt = &curve25519_list[0]; + +void +curve25519_load (void) +{ + guint i; + + if (cpu_config != 0) { + for (i = 0; i < G_N_ELEMENTS(curve25519_list); i++) { + if (curve25519_list[i].cpu_flags & cpu_config) { + curve25519_opt = &curve25519_list[i]; + break; + } + } + } +} + +int +curve25519 (guchar *mypublic, + const guchar *secret, + const guchar *basepoint) +{ + curve25519_opt->scalarmult (mypublic, secret, basepoint); + + return 0; +} + diff --git a/src/libcryptobox/curve25519/curve25519.h b/src/libcryptobox/curve25519/curve25519.h index 2d87e34cb..17fed4e53 100644 --- a/src/libcryptobox/curve25519/curve25519.h +++ b/src/libcryptobox/curve25519/curve25519.h @@ -6,5 +6,6 @@ static const guchar curve25519_basepoint[32] = {9}; int curve25519 (guchar *mypublic, const guchar *secret, const guchar *basepoint); +void curve25519_load (void); #endif |