argon2-fill-block-avx2.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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_AVX2INTRIN_H) && defined(HAVE_EMMINTRIN_H) && \
  21. 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. # endif
  28. # ifdef _MSC_VER
  29. # include <intrin.h> /* for _mm_set_epi64x */
  30. # endif
  31. #include <emmintrin.h>
  32. #include <immintrin.h>
  33. #include <smmintrin.h>
  34. #include <tmmintrin.h>
  35. # include "blamka-round-avx2.h"
  36. static void
  37. fill_block(__m256i *state, const uint8_t *ref_block, uint8_t *next_block)
  38. {
  39. __m256i block_XY[ARGON2_HWORDS_IN_BLOCK];
  40. uint32_t i;
  41. for (i = 0; i < ARGON2_HWORDS_IN_BLOCK; i++) {
  42. block_XY[i] = state[i] = _mm256_xor_si256(
  43. state[i], _mm256_loadu_si256((__m256i const *) (&ref_block[32 * i])));
  44. }
  45. for (i = 0; i < 4; ++i) {
  46. BLAKE2_ROUND_1(state[8 * i + 0], state[8 * i + 4], state[8 * i + 1], state[8 * i + 5],
  47. state[8 * i + 2], state[8 * i + 6], state[8 * i + 3], state[8 * i + 7]);
  48. }
  49. for (i = 0; i < 4; ++i) {
  50. BLAKE2_ROUND_2(state[ 0 + i], state[ 4 + i], state[ 8 + i], state[12 + i],
  51. state[16 + i], state[20 + i], state[24 + i], state[28 + i]);
  52. }
  53. for (i = 0; i < ARGON2_HWORDS_IN_BLOCK; i++) {
  54. state[i] = _mm256_xor_si256(state[i], block_XY[i]);
  55. _mm256_storeu_si256((__m256i *) (&next_block[32 * i]), state[i]);
  56. }
  57. }
  58. static void
  59. fill_block_with_xor(__m256i *state, const uint8_t *ref_block,
  60. uint8_t *next_block)
  61. {
  62. __m256i block_XY[ARGON2_HWORDS_IN_BLOCK];
  63. uint32_t i;
  64. for (i = 0; i < ARGON2_HWORDS_IN_BLOCK; i++) {
  65. state[i] = _mm256_xor_si256(
  66. state[i], _mm256_loadu_si256((__m256i const *) (&ref_block[32 * i])));
  67. block_XY[i] = _mm256_xor_si256(
  68. state[i], _mm256_loadu_si256((__m256i const *) (&next_block[32 * i])));
  69. }
  70. for (i = 0; i < 4; ++i) {
  71. BLAKE2_ROUND_1(state[8 * i + 0], state[8 * i + 4], state[8 * i + 1], state[8 * i + 5],
  72. state[8 * i + 2], state[8 * i + 6], state[8 * i + 3], state[8 * i + 7]);
  73. }
  74. for (i = 0; i < 4; ++i) {
  75. BLAKE2_ROUND_2(state[ 0 + i], state[ 4 + i], state[ 8 + i], state[12 + i],
  76. state[16 + i], state[20 + i], state[24 + i], state[28 + i]);
  77. }
  78. for (i = 0; i < ARGON2_HWORDS_IN_BLOCK; i++) {
  79. state[i] = _mm256_xor_si256(state[i], block_XY[i]);
  80. _mm256_storeu_si256((__m256i *) (&next_block[32 * i]), state[i]);
  81. }
  82. }
  83. static void
  84. generate_addresses(const argon2_instance_t *instance,
  85. const argon2_position_t *position, uint64_t *pseudo_rands)
  86. {
  87. block address_block, input_block, tmp_block;
  88. uint32_t i;
  89. init_block_value(&address_block, 0);
  90. init_block_value(&input_block, 0);
  91. if (instance != NULL && position != NULL) {
  92. input_block.v[0] = position->pass;
  93. input_block.v[1] = position->lane;
  94. input_block.v[2] = position->slice;
  95. input_block.v[3] = instance->memory_blocks;
  96. input_block.v[4] = instance->passes;
  97. input_block.v[5] = instance->type;
  98. for (i = 0; i < instance->segment_length; ++i) {
  99. if (i % ARGON2_ADDRESSES_IN_BLOCK == 0) {
  100. /* Temporary zero-initialized blocks */
  101. __m256i zero_block[ARGON2_HWORDS_IN_BLOCK];
  102. __m256i zero2_block[ARGON2_HWORDS_IN_BLOCK];
  103. memset(zero_block, 0, sizeof(zero_block));
  104. memset(zero2_block, 0, sizeof(zero2_block));
  105. init_block_value(&address_block, 0);
  106. init_block_value(&tmp_block, 0);
  107. /* Increasing index counter */
  108. input_block.v[6]++;
  109. /* First iteration of G */
  110. fill_block_with_xor(zero_block, (uint8_t *) &input_block.v,
  111. (uint8_t *) &tmp_block.v);
  112. /* Second iteration of G */
  113. fill_block_with_xor(zero2_block, (uint8_t *) &tmp_block.v,
  114. (uint8_t *) &address_block.v);
  115. }
  116. pseudo_rands[i] = address_block.v[i % ARGON2_ADDRESSES_IN_BLOCK];
  117. }
  118. }
  119. }
  120. void
  121. fill_segment_avx2(const argon2_instance_t *instance,
  122. argon2_position_t position)
  123. {
  124. block *ref_block = NULL, *curr_block = NULL;
  125. uint64_t pseudo_rand, ref_index, ref_lane;
  126. uint32_t prev_offset, curr_offset;
  127. uint32_t starting_index, i;
  128. __m256i state[ARGON2_HWORDS_IN_BLOCK];
  129. int data_independent_addressing = 1;
  130. /* Pseudo-random values that determine the reference block position */
  131. uint64_t *pseudo_rands = NULL;
  132. if (instance == NULL) {
  133. return;
  134. }
  135. if (instance->type == Argon2_id &&
  136. (position.pass != 0 || position.slice >= ARGON2_SYNC_POINTS / 2)) {
  137. data_independent_addressing = 0;
  138. }
  139. pseudo_rands = instance->pseudo_rands;
  140. if (data_independent_addressing) {
  141. generate_addresses(instance, &position, pseudo_rands);
  142. }
  143. starting_index = 0;
  144. if ((0 == position.pass) && (0 == position.slice)) {
  145. starting_index = 2; /* we have already generated the first two blocks */
  146. }
  147. /* Offset of the current block */
  148. curr_offset = position.lane * instance->lane_length +
  149. position.slice * instance->segment_length + starting_index;
  150. if (0 == curr_offset % instance->lane_length) {
  151. /* Last block in this lane */
  152. prev_offset = curr_offset + instance->lane_length - 1;
  153. } else {
  154. /* Previous block */
  155. prev_offset = curr_offset - 1;
  156. }
  157. memcpy(state, ((instance->region->memory + prev_offset)->v),
  158. ARGON2_BLOCK_SIZE);
  159. for (i = starting_index; i < instance->segment_length;
  160. ++i, ++curr_offset, ++prev_offset) {
  161. /*1.1 Rotating prev_offset if needed */
  162. if (curr_offset % instance->lane_length == 1) {
  163. prev_offset = curr_offset - 1;
  164. }
  165. /* 1.2 Computing the index of the reference block */
  166. /* 1.2.1 Taking pseudo-random value from the previous block */
  167. if (data_independent_addressing) {
  168. #pragma warning(push)
  169. #pragma warning(disable : 6385)
  170. pseudo_rand = pseudo_rands[i];
  171. #pragma warning(pop)
  172. } else {
  173. pseudo_rand = instance->region->memory[prev_offset].v[0];
  174. }
  175. /* 1.2.2 Computing the lane of the reference block */
  176. ref_lane = ((pseudo_rand >> 32)) % instance->lanes;
  177. if ((position.pass == 0) && (position.slice == 0)) {
  178. /* Can not reference other lanes yet */
  179. ref_lane = position.lane;
  180. }
  181. /* 1.2.3 Computing the number of possible reference block within the
  182. * lane.
  183. */
  184. position.index = i;
  185. ref_index = index_alpha(instance, &position, pseudo_rand & 0xFFFFFFFF,
  186. ref_lane == position.lane);
  187. /* 2 Creating a new block */
  188. ref_block = instance->region->memory +
  189. instance->lane_length * ref_lane + ref_index;
  190. curr_block = instance->region->memory + curr_offset;
  191. if (position.pass != 0) {
  192. fill_block_with_xor(state, (uint8_t *) ref_block->v,
  193. (uint8_t *) curr_block->v);
  194. } else {
  195. fill_block(state, (uint8_t *) ref_block->v,
  196. (uint8_t *) curr_block->v);
  197. }
  198. }
  199. }
  200. #endif