argon2-fill-block-avx512f.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Argon2 source code package
  3. *
  4. * Written by Daniel Dinu and Dmitry Khovratovich, 2015
  5. *
  6. * This work is licensed under a Creative Commons CC0 1.0 License/Waiver.
  7. *
  8. * You should have received a copy of the CC0 Public Domain Dedication along
  9. * with
  10. * this software. If not, see
  11. * <http://creativecommons.org/publicdomain/zero/1.0/>.
  12. */
  13. #include <stdint.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "argon2-core.h"
  17. #include "argon2.h"
  18. #include "private/common.h"
  19. #include "private/sse2_64_32.h"
  20. #if defined(HAVE_AVX512FINTRIN_H) && defined(HAVE_AVX2INTRIN_H) && \
  21. defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) && defined(HAVE_SMMINTRIN_H)
  22. # ifdef __GNUC__
  23. # pragma GCC target("sse2")
  24. # pragma GCC target("ssse3")
  25. # pragma GCC target("sse4.1")
  26. # pragma GCC target("avx2")
  27. # pragma GCC target("avx512f")
  28. # endif
  29. # ifdef _MSC_VER
  30. # include <intrin.h> /* for _mm_set_epi64x */
  31. # endif
  32. #include <emmintrin.h>
  33. #include <immintrin.h>
  34. #include <smmintrin.h>
  35. #include <tmmintrin.h>
  36. # include "blamka-round-avx512f.h"
  37. static void
  38. fill_block(__m512i *state, const uint8_t *ref_block, uint8_t *next_block)
  39. {
  40. __m512i block_XY[ARGON2_512BIT_WORDS_IN_BLOCK];
  41. uint32_t i;
  42. for (i = 0; i < ARGON2_512BIT_WORDS_IN_BLOCK; i++) {
  43. block_XY[i] = state[i] = _mm512_xor_si512(
  44. state[i], _mm512_loadu_si512((__m512i const *) (&ref_block[64 * i])));
  45. }
  46. for (i = 0; i < 2; ++i) {
  47. BLAKE2_ROUND_1(
  48. state[8 * i + 0], state[8 * i + 1], state[8 * i + 2], state[8 * i + 3],
  49. state[8 * i + 4], state[8 * i + 5], state[8 * i + 6], state[8 * i + 7]);
  50. }
  51. for (i = 0; i < 2; ++i) {
  52. BLAKE2_ROUND_2(
  53. state[2 * 0 + i], state[2 * 1 + i], state[2 * 2 + i], state[2 * 3 + i],
  54. state[2 * 4 + i], state[2 * 5 + i], state[2 * 6 + i], state[2 * 7 + i]);
  55. }
  56. for (i = 0; i < ARGON2_512BIT_WORDS_IN_BLOCK; i++) {
  57. state[i] = _mm512_xor_si512(state[i], block_XY[i]);
  58. _mm512_storeu_si512((__m512i *) (&next_block[64 * i]), state[i]);
  59. }
  60. }
  61. static void
  62. fill_block_with_xor(__m512i *state, const uint8_t *ref_block,
  63. uint8_t *next_block)
  64. {
  65. __m512i block_XY[ARGON2_512BIT_WORDS_IN_BLOCK];
  66. uint32_t i;
  67. for (i = 0; i < ARGON2_512BIT_WORDS_IN_BLOCK; i++) {
  68. state[i] = _mm512_xor_si512(
  69. state[i], _mm512_loadu_si512((__m512i const *) (&ref_block[64 * i])));
  70. block_XY[i] = _mm512_xor_si512(
  71. state[i], _mm512_loadu_si512((__m512i const *) (&next_block[64 * i])));
  72. }
  73. for (i = 0; i < 2; ++i) {
  74. BLAKE2_ROUND_1(
  75. state[8 * i + 0], state[8 * i + 1], state[8 * i + 2], state[8 * i + 3],
  76. state[8 * i + 4], state[8 * i + 5], state[8 * i + 6], state[8 * i + 7]);
  77. }
  78. for (i = 0; i < 2; ++i) {
  79. BLAKE2_ROUND_2(
  80. state[2 * 0 + i], state[2 * 1 + i], state[2 * 2 + i], state[2 * 3 + i],
  81. state[2 * 4 + i], state[2 * 5 + i], state[2 * 6 + i], state[2 * 7 + i]);
  82. }
  83. for (i = 0; i < ARGON2_512BIT_WORDS_IN_BLOCK; i++) {
  84. state[i] = _mm512_xor_si512(state[i], block_XY[i]);
  85. _mm512_storeu_si512((__m512i *) (&next_block[64 * i]), state[i]);
  86. }
  87. }
  88. static void
  89. generate_addresses(const argon2_instance_t *instance,
  90. const argon2_position_t *position, uint64_t *pseudo_rands)
  91. {
  92. block address_block, input_block, tmp_block;
  93. uint32_t i;
  94. init_block_value(&address_block, 0);
  95. init_block_value(&input_block, 0);
  96. if (instance != NULL && position != NULL) {
  97. input_block.v[0] = position->pass;
  98. input_block.v[1] = position->lane;
  99. input_block.v[2] = position->slice;
  100. input_block.v[3] = instance->memory_blocks;
  101. input_block.v[4] = instance->passes;
  102. input_block.v[5] = instance->type;
  103. for (i = 0; i < instance->segment_length; ++i) {
  104. if (i % ARGON2_ADDRESSES_IN_BLOCK == 0) {
  105. /* Temporary zero-initialized blocks */
  106. __m512i zero_block[ARGON2_512BIT_WORDS_IN_BLOCK];
  107. __m512i zero2_block[ARGON2_512BIT_WORDS_IN_BLOCK];
  108. memset(zero_block, 0, sizeof(zero_block));
  109. memset(zero2_block, 0, sizeof(zero2_block));
  110. init_block_value(&address_block, 0);
  111. init_block_value(&tmp_block, 0);
  112. /* Increasing index counter */
  113. input_block.v[6]++;
  114. /* First iteration of G */
  115. fill_block_with_xor(zero_block, (uint8_t *) &input_block.v,
  116. (uint8_t *) &tmp_block.v);
  117. /* Second iteration of G */
  118. fill_block_with_xor(zero2_block, (uint8_t *) &tmp_block.v,
  119. (uint8_t *) &address_block.v);
  120. }
  121. pseudo_rands[i] = address_block.v[i % ARGON2_ADDRESSES_IN_BLOCK];
  122. }
  123. }
  124. }
  125. void
  126. fill_segment_avx512f(const argon2_instance_t *instance,
  127. argon2_position_t position)
  128. {
  129. block *ref_block = NULL, *curr_block = NULL;
  130. uint64_t pseudo_rand, ref_index, ref_lane;
  131. uint32_t prev_offset, curr_offset;
  132. uint32_t starting_index, i;
  133. __m512i state[ARGON2_512BIT_WORDS_IN_BLOCK];
  134. int data_independent_addressing = 1;
  135. /* Pseudo-random values that determine the reference block position */
  136. uint64_t *pseudo_rands = NULL;
  137. if (instance == NULL) {
  138. return;
  139. }
  140. if (instance->type == Argon2_id &&
  141. (position.pass != 0 || position.slice >= ARGON2_SYNC_POINTS / 2)) {
  142. data_independent_addressing = 0;
  143. }
  144. pseudo_rands = instance->pseudo_rands;
  145. if (data_independent_addressing) {
  146. generate_addresses(instance, &position, pseudo_rands);
  147. }
  148. starting_index = 0;
  149. if ((0 == position.pass) && (0 == position.slice)) {
  150. starting_index = 2; /* we have already generated the first two blocks */
  151. }
  152. /* Offset of the current block */
  153. curr_offset = position.lane * instance->lane_length +
  154. position.slice * instance->segment_length + starting_index;
  155. if (0 == curr_offset % instance->lane_length) {
  156. /* Last block in this lane */
  157. prev_offset = curr_offset + instance->lane_length - 1;
  158. } else {
  159. /* Previous block */
  160. prev_offset = curr_offset - 1;
  161. }
  162. memcpy(state, ((instance->region->memory + prev_offset)->v),
  163. ARGON2_BLOCK_SIZE);
  164. for (i = starting_index; i < instance->segment_length;
  165. ++i, ++curr_offset, ++prev_offset) {
  166. /*1.1 Rotating prev_offset if needed */
  167. if (curr_offset % instance->lane_length == 1) {
  168. prev_offset = curr_offset - 1;
  169. }
  170. /* 1.2 Computing the index of the reference block */
  171. /* 1.2.1 Taking pseudo-random value from the previous block */
  172. if (data_independent_addressing) {
  173. #pragma warning(push)
  174. #pragma warning(disable : 6385)
  175. pseudo_rand = pseudo_rands[i];
  176. #pragma warning(pop)
  177. } else {
  178. pseudo_rand = instance->region->memory[prev_offset].v[0];
  179. }
  180. /* 1.2.2 Computing the lane of the reference block */
  181. ref_lane = ((pseudo_rand >> 32)) % instance->lanes;
  182. if ((position.pass == 0) && (position.slice == 0)) {
  183. /* Can not reference other lanes yet */
  184. ref_lane = position.lane;
  185. }
  186. /* 1.2.3 Computing the number of possible reference block within the
  187. * lane.
  188. */
  189. position.index = i;
  190. ref_index = index_alpha(instance, &position, pseudo_rand & 0xFFFFFFFF,
  191. ref_lane == position.lane);
  192. /* 2 Creating a new block */
  193. ref_block = instance->region->memory +
  194. instance->lane_length * ref_lane + ref_index;
  195. curr_block = instance->region->memory + curr_offset;
  196. if (position.pass != 0) {
  197. fill_block_with_xor(state, (uint8_t *) ref_block->v,
  198. (uint8_t *) curr_block->v);
  199. } else {
  200. fill_block(state, (uint8_t *) ref_block->v,
  201. (uint8_t *) curr_block->v);
  202. }
  203. }
  204. }
  205. #endif