salsa20_xmm6int-sse2.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <stdint.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "crypto_stream_salsa20.h"
  5. #include "private/common.h"
  6. #include "private/sse2_64_32.h"
  7. #include "utils.h"
  8. #ifdef HAVE_EMMINTRIN_H
  9. # ifdef __GNUC__
  10. # pragma GCC target("sse2")
  11. # endif
  12. # include <emmintrin.h>
  13. # include "../stream_salsa20.h"
  14. # include "salsa20_xmm6int-sse2.h"
  15. # define ROUNDS 20
  16. typedef struct salsa_ctx {
  17. uint32_t input[16];
  18. } salsa_ctx;
  19. static const int TR[16] = {
  20. 0, 5, 10, 15, 12, 1, 6, 11, 8, 13, 2, 7, 4, 9, 14, 3
  21. };
  22. static void
  23. salsa_keysetup(salsa_ctx *ctx, const uint8_t *k)
  24. {
  25. ctx->input[TR[1]] = LOAD32_LE(k + 0);
  26. ctx->input[TR[2]] = LOAD32_LE(k + 4);
  27. ctx->input[TR[3]] = LOAD32_LE(k + 8);
  28. ctx->input[TR[4]] = LOAD32_LE(k + 12);
  29. ctx->input[TR[11]] = LOAD32_LE(k + 16);
  30. ctx->input[TR[12]] = LOAD32_LE(k + 20);
  31. ctx->input[TR[13]] = LOAD32_LE(k + 24);
  32. ctx->input[TR[14]] = LOAD32_LE(k + 28);
  33. ctx->input[TR[0]] = 0x61707865;
  34. ctx->input[TR[5]] = 0x3320646e;
  35. ctx->input[TR[10]] = 0x79622d32;
  36. ctx->input[TR[15]] = 0x6b206574;
  37. }
  38. static void
  39. salsa_ivsetup(salsa_ctx *ctx, const uint8_t *iv, const uint8_t *counter)
  40. {
  41. ctx->input[TR[6]] = LOAD32_LE(iv + 0);
  42. ctx->input[TR[7]] = LOAD32_LE(iv + 4);
  43. ctx->input[TR[8]] = counter == NULL ? 0 : LOAD32_LE(counter + 0);
  44. ctx->input[TR[9]] = counter == NULL ? 0 : LOAD32_LE(counter + 4);
  45. }
  46. static void
  47. salsa20_encrypt_bytes(salsa_ctx *ctx, const uint8_t *m, uint8_t *c,
  48. unsigned long long bytes)
  49. {
  50. uint32_t * const x = &ctx->input[0];
  51. if (!bytes) {
  52. return; /* LCOV_EXCL_LINE */
  53. }
  54. #include "u4.h"
  55. #include "u1.h"
  56. #include "u0.h"
  57. }
  58. static int
  59. stream_sse2(unsigned char *c, unsigned long long clen, const unsigned char *n,
  60. const unsigned char *k)
  61. {
  62. struct salsa_ctx ctx;
  63. if (!clen) {
  64. return 0;
  65. }
  66. COMPILER_ASSERT(crypto_stream_salsa20_KEYBYTES == 256 / 8);
  67. salsa_keysetup(&ctx, k);
  68. salsa_ivsetup(&ctx, n, NULL);
  69. memset(c, 0, clen);
  70. salsa20_encrypt_bytes(&ctx, c, c, clen);
  71. sodium_memzero(&ctx, sizeof ctx);
  72. return 0;
  73. }
  74. static int
  75. stream_sse2_xor_ic(unsigned char *c, const unsigned char *m,
  76. unsigned long long mlen, const unsigned char *n, uint64_t ic,
  77. const unsigned char *k)
  78. {
  79. struct salsa_ctx ctx;
  80. uint8_t ic_bytes[8];
  81. uint32_t ic_high;
  82. uint32_t ic_low;
  83. if (!mlen) {
  84. return 0;
  85. }
  86. ic_high = (uint32_t) (ic >> 32);
  87. ic_low = (uint32_t) (ic);
  88. STORE32_LE(&ic_bytes[0], ic_low);
  89. STORE32_LE(&ic_bytes[4], ic_high);
  90. salsa_keysetup(&ctx, k);
  91. salsa_ivsetup(&ctx, n, ic_bytes);
  92. salsa20_encrypt_bytes(&ctx, m, c, mlen);
  93. sodium_memzero(&ctx, sizeof ctx);
  94. return 0;
  95. }
  96. struct crypto_stream_salsa20_implementation
  97. crypto_stream_salsa20_xmm6int_sse2_implementation = {
  98. SODIUM_C99(.stream =) stream_sse2,
  99. SODIUM_C99(.stream_xor_ic =) stream_sse2_xor_ic
  100. };
  101. #endif