blamka-round-ssse3.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Copyright (c) 2018-2019, tevador <tevador@gmail.com>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of the copyright holder nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  18. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  22. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /* Original code from Argon2 reference source code package used under CC0 Licence
  26. * https://github.com/P-H-C/phc-winner-argon2
  27. * Copyright 2015
  28. * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  29. */
  30. #ifndef BLAKE_ROUND_MKA_OPT_H
  31. #define BLAKE_ROUND_MKA_OPT_H
  32. #include "blake2-impl.h"
  33. #ifdef __GNUC__
  34. #include <x86intrin.h>
  35. #else
  36. #include <intrin.h>
  37. #endif
  38. #ifdef _mm_roti_epi64 //clang defines it using the XOP instruction set
  39. #undef _mm_roti_epi64
  40. #endif
  41. #define r16 \
  42. (_mm_setr_epi8(2, 3, 4, 5, 6, 7, 0, 1, 10, 11, 12, 13, 14, 15, 8, 9))
  43. #define r24 \
  44. (_mm_setr_epi8(3, 4, 5, 6, 7, 0, 1, 2, 11, 12, 13, 14, 15, 8, 9, 10))
  45. #define _mm_roti_epi64(x, c) \
  46. (-(c) == 32) \
  47. ? _mm_shuffle_epi32((x), _MM_SHUFFLE(2, 3, 0, 1)) \
  48. : (-(c) == 24) \
  49. ? _mm_shuffle_epi8((x), r24) \
  50. : (-(c) == 16) \
  51. ? _mm_shuffle_epi8((x), r16) \
  52. : (-(c) == 63) \
  53. ? _mm_xor_si128(_mm_srli_epi64((x), -(c)), \
  54. _mm_add_epi64((x), (x))) \
  55. : _mm_xor_si128(_mm_srli_epi64((x), -(c)), \
  56. _mm_slli_epi64((x), 64 - (-(c))))
  57. static FORCE_INLINE __m128i fBlaMka(__m128i x, __m128i y) {
  58. const __m128i z = _mm_mul_epu32(x, y);
  59. return _mm_add_epi64(_mm_add_epi64(x, y), _mm_add_epi64(z, z));
  60. }
  61. #define G1(A0, B0, C0, D0, A1, B1, C1, D1) \
  62. do { \
  63. A0 = fBlaMka(A0, B0); \
  64. A1 = fBlaMka(A1, B1); \
  65. \
  66. D0 = _mm_xor_si128(D0, A0); \
  67. D1 = _mm_xor_si128(D1, A1); \
  68. \
  69. D0 = _mm_roti_epi64(D0, -32); \
  70. D1 = _mm_roti_epi64(D1, -32); \
  71. \
  72. C0 = fBlaMka(C0, D0); \
  73. C1 = fBlaMka(C1, D1); \
  74. \
  75. B0 = _mm_xor_si128(B0, C0); \
  76. B1 = _mm_xor_si128(B1, C1); \
  77. \
  78. B0 = _mm_roti_epi64(B0, -24); \
  79. B1 = _mm_roti_epi64(B1, -24); \
  80. } while ((void)0, 0)
  81. #define G2(A0, B0, C0, D0, A1, B1, C1, D1) \
  82. do { \
  83. A0 = fBlaMka(A0, B0); \
  84. A1 = fBlaMka(A1, B1); \
  85. \
  86. D0 = _mm_xor_si128(D0, A0); \
  87. D1 = _mm_xor_si128(D1, A1); \
  88. \
  89. D0 = _mm_roti_epi64(D0, -16); \
  90. D1 = _mm_roti_epi64(D1, -16); \
  91. \
  92. C0 = fBlaMka(C0, D0); \
  93. C1 = fBlaMka(C1, D1); \
  94. \
  95. B0 = _mm_xor_si128(B0, C0); \
  96. B1 = _mm_xor_si128(B1, C1); \
  97. \
  98. B0 = _mm_roti_epi64(B0, -63); \
  99. B1 = _mm_roti_epi64(B1, -63); \
  100. } while ((void)0, 0)
  101. #define DIAGONALIZE(A0, B0, C0, D0, A1, B1, C1, D1) \
  102. do { \
  103. __m128i t0 = _mm_alignr_epi8(B1, B0, 8); \
  104. __m128i t1 = _mm_alignr_epi8(B0, B1, 8); \
  105. B0 = t0; \
  106. B1 = t1; \
  107. \
  108. t0 = C0; \
  109. C0 = C1; \
  110. C1 = t0; \
  111. \
  112. t0 = _mm_alignr_epi8(D1, D0, 8); \
  113. t1 = _mm_alignr_epi8(D0, D1, 8); \
  114. D0 = t1; \
  115. D1 = t0; \
  116. } while ((void)0, 0)
  117. #define UNDIAGONALIZE(A0, B0, C0, D0, A1, B1, C1, D1) \
  118. do { \
  119. __m128i t0 = _mm_alignr_epi8(B0, B1, 8); \
  120. __m128i t1 = _mm_alignr_epi8(B1, B0, 8); \
  121. B0 = t0; \
  122. B1 = t1; \
  123. \
  124. t0 = C0; \
  125. C0 = C1; \
  126. C1 = t0; \
  127. \
  128. t0 = _mm_alignr_epi8(D0, D1, 8); \
  129. t1 = _mm_alignr_epi8(D1, D0, 8); \
  130. D0 = t1; \
  131. D1 = t0; \
  132. } while ((void)0, 0)
  133. #define BLAKE2_ROUND(A0, A1, B0, B1, C0, C1, D0, D1) \
  134. do { \
  135. G1(A0, B0, C0, D0, A1, B1, C1, D1); \
  136. G2(A0, B0, C0, D0, A1, B1, C1, D1); \
  137. \
  138. DIAGONALIZE(A0, B0, C0, D0, A1, B1, C1, D1); \
  139. \
  140. G1(A0, B0, C0, D0, A1, B1, C1, D1); \
  141. G2(A0, B0, C0, D0, A1, B1, C1, D1); \
  142. \
  143. UNDIAGONALIZE(A0, B0, C0, D0, A1, B1, C1, D1); \
  144. } while ((void)0, 0)
  145. #endif /* BLAKE_ROUND_MKA_OPT_H */