diff --git a/src/libsodium/crypto_stream/chacha20/stream_chacha20.c b/src/libsodium/crypto_stream/chacha20/stream_chacha20.c index 97da45f1..ebc3e5eb 100644 --- a/src/libsodium/crypto_stream/chacha20/stream_chacha20.c +++ b/src/libsodium/crypto_stream/chacha20/stream_chacha20.c @@ -2,6 +2,10 @@ #include "stream_chacha20.h" #include "runtime.h" #include "ref/stream_chacha20_ref.h" +#if (defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H)) || \ + (defined(_MSC_VER) && (defined(_M_X64) || defined(_M_AMD64))) +# include "vec/stream_chacha20_vec.h" +#endif static const crypto_stream_chacha20_implementation *implementation = &crypto_stream_chacha20_ref_implementation; @@ -68,3 +72,16 @@ crypto_stream_chacha20_ietf_xor(unsigned char *c, const unsigned char *m, { return implementation->stream_ietf_xor_ic(c, m, mlen, n, 0U, k); } + +int +_crypto_stream_chacha20_pick_best_implementation(void) +{ + implementation = &crypto_stream_chacha20_ref_implementation; +#if (defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H)) || \ + (defined(_MSC_VER) && (defined(_M_X64) || defined(_M_AMD64))) + if (sodium_runtime_has_ssse3()) { + implementation = &crypto_stream_chacha20_vec_implementation; + } +#endif + return 0; +} diff --git a/src/libsodium/include/sodium/crypto_stream_chacha20.h b/src/libsodium/include/sodium/crypto_stream_chacha20.h index 612c4a74..507e9b0c 100644 --- a/src/libsodium/include/sodium/crypto_stream_chacha20.h +++ b/src/libsodium/include/sodium/crypto_stream_chacha20.h @@ -65,6 +65,11 @@ int crypto_stream_chacha20_ietf_xor_ic(unsigned char *c, const unsigned char *m, unsigned long long mlen, const unsigned char *n, uint32_t ic, const unsigned char *k); + +/* ------------------------------------------------------------------------- */ + +int _crypto_stream_chacha20_pick_best_implementation(void); + #ifdef __cplusplus } #endif diff --git a/src/libsodium/sodium/core.c b/src/libsodium/sodium/core.c index aaca4733..66ef97ea 100644 --- a/src/libsodium/sodium/core.c +++ b/src/libsodium/sodium/core.c @@ -3,6 +3,7 @@ #include "crypto_generichash.h" #include "crypto_onetimeauth.h" #include "crypto_scalarmult.h" +#include "crypto_stream_chacha20.h" #include "randombytes.h" #include "runtime.h" #include "utils.h" @@ -21,6 +22,7 @@ sodium_init(void) _crypto_generichash_blake2b_pick_best_implementation(); _crypto_onetimeauth_poly1305_pick_best_implementation(); _crypto_scalarmult_curve25519_pick_best_implementation(); + _crypto_stream_chacha20_pick_best_implementation(); initialized = 1; return 0;