stream_salsa20.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "crypto_stream_salsa20.h"
  2. #include "private/common.h"
  3. #include "private/implementations.h"
  4. #include "randombytes.h"
  5. #include "runtime.h"
  6. #include "stream_salsa20.h"
  7. #ifdef HAVE_AMD64_ASM
  8. # include "xmm6/salsa20_xmm6.h"
  9. #else
  10. # include "ref/salsa20_ref.h"
  11. #endif
  12. #if !defined(HAVE_AMD64_ASM) && defined(HAVE_EMMINTRIN_H)
  13. # include "xmm6int/salsa20_xmm6int-sse2.h"
  14. #endif
  15. #if defined(HAVE_AVX2INTRIN_H) && defined(HAVE_EMMINTRIN_H) && \
  16. defined(HAVE_TMMINTRIN_H) && defined(HAVE_SMMINTRIN_H)
  17. # include "xmm6int/salsa20_xmm6int-avx2.h"
  18. #endif
  19. #if HAVE_AMD64_ASM
  20. static const crypto_stream_salsa20_implementation *implementation =
  21. &crypto_stream_salsa20_xmm6_implementation;
  22. #else
  23. static const crypto_stream_salsa20_implementation *implementation =
  24. &crypto_stream_salsa20_ref_implementation;
  25. #endif
  26. size_t
  27. crypto_stream_salsa20_keybytes(void)
  28. {
  29. return crypto_stream_salsa20_KEYBYTES;
  30. }
  31. size_t
  32. crypto_stream_salsa20_noncebytes(void)
  33. {
  34. return crypto_stream_salsa20_NONCEBYTES;
  35. }
  36. size_t
  37. crypto_stream_salsa20_messagebytes_max(void)
  38. {
  39. return crypto_stream_salsa20_MESSAGEBYTES_MAX;
  40. }
  41. int
  42. crypto_stream_salsa20(unsigned char *c, unsigned long long clen,
  43. const unsigned char *n, const unsigned char *k)
  44. {
  45. return implementation->stream(c, clen, n, k);
  46. }
  47. int
  48. crypto_stream_salsa20_xor_ic(unsigned char *c, const unsigned char *m,
  49. unsigned long long mlen,
  50. const unsigned char *n, uint64_t ic,
  51. const unsigned char *k)
  52. {
  53. return implementation->stream_xor_ic(c, m, mlen, n, ic, k);
  54. }
  55. int
  56. crypto_stream_salsa20_xor(unsigned char *c, const unsigned char *m,
  57. unsigned long long mlen, const unsigned char *n,
  58. const unsigned char *k)
  59. {
  60. return implementation->stream_xor_ic(c, m, mlen, n, 0U, k);
  61. }
  62. void
  63. crypto_stream_salsa20_keygen(unsigned char k[crypto_stream_salsa20_KEYBYTES])
  64. {
  65. randombytes_buf(k, crypto_stream_salsa20_KEYBYTES);
  66. }
  67. int
  68. _crypto_stream_salsa20_pick_best_implementation(void)
  69. {
  70. #ifdef HAVE_AMD64_ASM
  71. implementation = &crypto_stream_salsa20_xmm6_implementation;
  72. #else
  73. implementation = &crypto_stream_salsa20_ref_implementation;
  74. #endif
  75. #if defined(HAVE_AVX2INTRIN_H) && defined(HAVE_EMMINTRIN_H) && \
  76. defined(HAVE_TMMINTRIN_H) && defined(HAVE_SMMINTRIN_H)
  77. if (sodium_runtime_has_avx2()) {
  78. implementation = &crypto_stream_salsa20_xmm6int_avx2_implementation;
  79. return 0;
  80. }
  81. #endif
  82. #if !defined(HAVE_AMD64_ASM) && defined(HAVE_EMMINTRIN_H)
  83. if (sodium_runtime_has_sse2()) {
  84. implementation = &crypto_stream_salsa20_xmm6int_sse2_implementation;
  85. return 0;
  86. }
  87. #endif
  88. return 0; /* LCOV_EXCL_LINE */
  89. }