1 2 #include "crypto_scalarmult_curve25519.h" 3 #include "private/implementations.h" 4 #include "scalarmult_curve25519.h" 5 #include "runtime.h" 6 7 #ifdef HAVE_AVX_ASM 8 # include "sandy2x/curve25519_sandy2x.h" 9 #endif 10 #include "ref10/x25519_ref10.h" 11 static const crypto_scalarmult_curve25519_implementation *implementation = 12 &crypto_scalarmult_curve25519_ref10_implementation; 13 14 int crypto_scalarmult_curve25519(unsigned char * q,const unsigned char * n,const unsigned char * p)15crypto_scalarmult_curve25519(unsigned char *q, const unsigned char *n, 16 const unsigned char *p) 17 { 18 size_t i; 19 volatile unsigned char d = 0; 20 21 if (implementation->mult(q, n, p) != 0) { 22 return -1; /* LCOV_EXCL_LINE */ 23 } 24 for (i = 0; i < crypto_scalarmult_curve25519_BYTES; i++) { 25 d |= q[i]; 26 } 27 return -(1 & ((d - 1) >> 8)); 28 } 29 30 int crypto_scalarmult_curve25519_base(unsigned char * q,const unsigned char * n)31crypto_scalarmult_curve25519_base(unsigned char *q, const unsigned char *n) 32 { 33 return implementation->mult_base(q, n); 34 } 35 36 size_t crypto_scalarmult_curve25519_bytes(void)37crypto_scalarmult_curve25519_bytes(void) 38 { 39 return crypto_scalarmult_curve25519_BYTES; 40 } 41 42 size_t crypto_scalarmult_curve25519_scalarbytes(void)43crypto_scalarmult_curve25519_scalarbytes(void) 44 { 45 return crypto_scalarmult_curve25519_SCALARBYTES; 46 } 47 48 int _crypto_scalarmult_curve25519_pick_best_implementation(void)49_crypto_scalarmult_curve25519_pick_best_implementation(void) 50 { 51 implementation = &crypto_scalarmult_curve25519_ref10_implementation; 52 53 #ifdef HAVE_AVX_ASM 54 if (sodium_runtime_has_avx()) { 55 implementation = &crypto_scalarmult_curve25519_sandy2x_implementation; 56 } 57 #endif 58 return 0; 59 } 60