argon2_avx2.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #include <stdint.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include "argon2.h"
  34. void randomx_argon2_fill_segment_avx2(const argon2_instance_t* instance,
  35. argon2_position_t position);
  36. randomx_argon2_impl* randomx_argon2_impl_avx2() {
  37. #if defined(__AVX2__)
  38. return &randomx_argon2_fill_segment_avx2;
  39. #endif
  40. return NULL;
  41. }
  42. #if defined(__AVX2__)
  43. #include "argon2_core.h"
  44. #include "blake2/blamka-round-avx2.h"
  45. #include "blake2/blake2-impl.h"
  46. #include "blake2/blake2.h"
  47. static void fill_block(__m256i* state, const block* ref_block,
  48. block* next_block, int with_xor) {
  49. __m256i block_XY[ARGON2_HWORDS_IN_BLOCK];
  50. unsigned int i;
  51. if (with_xor) {
  52. for (i = 0; i < ARGON2_HWORDS_IN_BLOCK; i++) {
  53. state[i] = _mm256_xor_si256(
  54. state[i], _mm256_loadu_si256((const __m256i*)ref_block->v + i));
  55. block_XY[i] = _mm256_xor_si256(
  56. state[i], _mm256_loadu_si256((const __m256i*)next_block->v + i));
  57. }
  58. }
  59. else {
  60. for (i = 0; i < ARGON2_HWORDS_IN_BLOCK; i++) {
  61. block_XY[i] = state[i] = _mm256_xor_si256(
  62. state[i], _mm256_loadu_si256((const __m256i*)ref_block->v + i));
  63. }
  64. }
  65. for (i = 0; i < 4; ++i) {
  66. BLAKE2_ROUND_1(state[8 * i + 0], state[8 * i + 4], state[8 * i + 1], state[8 * i + 5],
  67. state[8 * i + 2], state[8 * i + 6], state[8 * i + 3], state[8 * i + 7]);
  68. }
  69. for (i = 0; i < 4; ++i) {
  70. BLAKE2_ROUND_2(state[0 + i], state[4 + i], state[8 + i], state[12 + i],
  71. state[16 + i], state[20 + i], state[24 + i], state[28 + i]);
  72. }
  73. for (i = 0; i < ARGON2_HWORDS_IN_BLOCK; i++) {
  74. state[i] = _mm256_xor_si256(state[i], block_XY[i]);
  75. _mm256_storeu_si256((__m256i*)next_block->v + i, state[i]);
  76. }
  77. }
  78. void randomx_argon2_fill_segment_avx2(const argon2_instance_t* instance,
  79. argon2_position_t position) {
  80. block* ref_block = NULL, * curr_block = NULL;
  81. block address_block, input_block;
  82. uint64_t pseudo_rand, ref_index, ref_lane;
  83. uint32_t prev_offset, curr_offset;
  84. uint32_t starting_index, i;
  85. __m256i state[ARGON2_HWORDS_IN_BLOCK];
  86. if (instance == NULL) {
  87. return;
  88. }
  89. starting_index = 0;
  90. if ((0 == position.pass) && (0 == position.slice)) {
  91. starting_index = 2; /* we have already generated the first two blocks */
  92. }
  93. /* Offset of the current block */
  94. curr_offset = position.lane * instance->lane_length +
  95. position.slice * instance->segment_length + starting_index;
  96. if (0 == curr_offset % instance->lane_length) {
  97. /* Last block in this lane */
  98. prev_offset = curr_offset + instance->lane_length - 1;
  99. }
  100. else {
  101. /* Previous block */
  102. prev_offset = curr_offset - 1;
  103. }
  104. memcpy(state, ((instance->memory + prev_offset)->v), ARGON2_BLOCK_SIZE);
  105. for (i = starting_index; i < instance->segment_length;
  106. ++i, ++curr_offset, ++prev_offset) {
  107. /*1.1 Rotating prev_offset if needed */
  108. if (curr_offset % instance->lane_length == 1) {
  109. prev_offset = curr_offset - 1;
  110. }
  111. /* 1.2 Computing the index of the reference block */
  112. /* 1.2.1 Taking pseudo-random value from the previous block */
  113. pseudo_rand = instance->memory[prev_offset].v[0];
  114. /* 1.2.2 Computing the lane of the reference block */
  115. ref_lane = ((pseudo_rand >> 32)) % instance->lanes;
  116. if ((position.pass == 0) && (position.slice == 0)) {
  117. /* Can not reference other lanes yet */
  118. ref_lane = position.lane;
  119. }
  120. /* 1.2.3 Computing the number of possible reference block within the
  121. * lane.
  122. */
  123. position.index = i;
  124. ref_index = randomx_argon2_index_alpha(instance, &position, pseudo_rand & 0xFFFFFFFF,
  125. ref_lane == position.lane);
  126. /* 2 Creating a new block */
  127. ref_block =
  128. instance->memory + instance->lane_length * ref_lane + ref_index;
  129. curr_block = instance->memory + curr_offset;
  130. if (ARGON2_VERSION_10 == instance->version) {
  131. /* version 1.2.1 and earlier: overwrite, not XOR */
  132. fill_block(state, ref_block, curr_block, 0);
  133. }
  134. else {
  135. if (0 == position.pass) {
  136. fill_block(state, ref_block, curr_block, 0);
  137. }
  138. else {
  139. fill_block(state, ref_block, curr_block, 1);
  140. }
  141. }
  142. }
  143. }
  144. #endif