argon2-fill-block-ssse3.c 7.8 KB

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