argon2_ref.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. Copyright (c) 2018 tevador
  3. This file is part of RandomX.
  4. RandomX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. RandomX is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with RandomX. If not, see<http://www.gnu.org/licenses/>.
  14. */
  15. /* Original code from Argon2 reference source code package used under CC0 Licence
  16. * https://github.com/P-H-C/phc-winner-argon2
  17. * Copyright 2015
  18. * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  19. */
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include "argon2.h"
  24. #include "argon2_core.h"
  25. #include "blake2/blamka-round-ref.h"
  26. #include "blake2/blake2-impl.h"
  27. #include "blake2/blake2.h"
  28. /*
  29. * Function fills a new memory block and optionally XORs the old block over the new one.
  30. * @next_block must be initialized.
  31. * @param prev_block Pointer to the previous block
  32. * @param ref_block Pointer to the reference block
  33. * @param next_block Pointer to the block to be constructed
  34. * @param with_xor Whether to XOR into the new block (1) or just overwrite (0)
  35. * @pre all block pointers must be valid
  36. */
  37. static void fill_block(const block *prev_block, const block *ref_block,
  38. block *next_block, int with_xor) {
  39. block blockR, block_tmp;
  40. unsigned i;
  41. copy_block(&blockR, ref_block);
  42. xor_block(&blockR, prev_block);
  43. copy_block(&block_tmp, &blockR);
  44. /* Now blockR = ref_block + prev_block and block_tmp = ref_block + prev_block */
  45. if (with_xor) {
  46. /* Saving the next block contents for XOR over: */
  47. xor_block(&block_tmp, next_block);
  48. /* Now blockR = ref_block + prev_block and
  49. block_tmp = ref_block + prev_block + next_block */
  50. }
  51. /* Apply Blake2 on columns of 64-bit words: (0,1,...,15) , then
  52. (16,17,..31)... finally (112,113,...127) */
  53. for (i = 0; i < 8; ++i) {
  54. BLAKE2_ROUND_NOMSG(
  55. blockR.v[16 * i], blockR.v[16 * i + 1], blockR.v[16 * i + 2],
  56. blockR.v[16 * i + 3], blockR.v[16 * i + 4], blockR.v[16 * i + 5],
  57. blockR.v[16 * i + 6], blockR.v[16 * i + 7], blockR.v[16 * i + 8],
  58. blockR.v[16 * i + 9], blockR.v[16 * i + 10], blockR.v[16 * i + 11],
  59. blockR.v[16 * i + 12], blockR.v[16 * i + 13], blockR.v[16 * i + 14],
  60. blockR.v[16 * i + 15]);
  61. }
  62. /* Apply Blake2 on rows of 64-bit words: (0,1,16,17,...112,113), then
  63. (2,3,18,19,...,114,115).. finally (14,15,30,31,...,126,127) */
  64. for (i = 0; i < 8; i++) {
  65. BLAKE2_ROUND_NOMSG(
  66. blockR.v[2 * i], blockR.v[2 * i + 1], blockR.v[2 * i + 16],
  67. blockR.v[2 * i + 17], blockR.v[2 * i + 32], blockR.v[2 * i + 33],
  68. blockR.v[2 * i + 48], blockR.v[2 * i + 49], blockR.v[2 * i + 64],
  69. blockR.v[2 * i + 65], blockR.v[2 * i + 80], blockR.v[2 * i + 81],
  70. blockR.v[2 * i + 96], blockR.v[2 * i + 97], blockR.v[2 * i + 112],
  71. blockR.v[2 * i + 113]);
  72. }
  73. copy_block(next_block, &block_tmp);
  74. xor_block(next_block, &blockR);
  75. }
  76. static void next_addresses(block *address_block, block *input_block,
  77. const block *zero_block) {
  78. input_block->v[6]++;
  79. fill_block(zero_block, input_block, address_block, 0);
  80. fill_block(zero_block, address_block, address_block, 0);
  81. }
  82. void fill_segment(const argon2_instance_t *instance,
  83. argon2_position_t position) {
  84. block *ref_block = NULL, *curr_block = NULL;
  85. block address_block, input_block, zero_block;
  86. uint64_t pseudo_rand, ref_index, ref_lane;
  87. uint32_t prev_offset, curr_offset;
  88. uint32_t starting_index;
  89. uint32_t i;
  90. int data_independent_addressing;
  91. if (instance == NULL) {
  92. return;
  93. }
  94. data_independent_addressing =
  95. (instance->type == Argon2_i) ||
  96. (instance->type == Argon2_id && (position.pass == 0) &&
  97. (position.slice < ARGON2_SYNC_POINTS / 2));
  98. if (data_independent_addressing) {
  99. init_block_value(&zero_block, 0);
  100. init_block_value(&input_block, 0);
  101. input_block.v[0] = position.pass;
  102. input_block.v[1] = position.lane;
  103. input_block.v[2] = position.slice;
  104. input_block.v[3] = instance->memory_blocks;
  105. input_block.v[4] = instance->passes;
  106. input_block.v[5] = instance->type;
  107. }
  108. starting_index = 0;
  109. if ((0 == position.pass) && (0 == position.slice)) {
  110. starting_index = 2; /* we have already generated the first two blocks */
  111. /* Don't forget to generate the first block of addresses: */
  112. if (data_independent_addressing) {
  113. next_addresses(&address_block, &input_block, &zero_block);
  114. }
  115. }
  116. /* Offset of the current block */
  117. curr_offset = position.lane * instance->lane_length +
  118. position.slice * instance->segment_length + starting_index;
  119. if (0 == curr_offset % instance->lane_length) {
  120. /* Last block in this lane */
  121. prev_offset = curr_offset + instance->lane_length - 1;
  122. }
  123. else {
  124. /* Previous block */
  125. prev_offset = curr_offset - 1;
  126. }
  127. for (i = starting_index; i < instance->segment_length;
  128. ++i, ++curr_offset, ++prev_offset) {
  129. /*1.1 Rotating prev_offset if needed */
  130. if (curr_offset % instance->lane_length == 1) {
  131. prev_offset = curr_offset - 1;
  132. }
  133. /* 1.2 Computing the index of the reference block */
  134. /* 1.2.1 Taking pseudo-random value from the previous block */
  135. if (data_independent_addressing) {
  136. if (i % ARGON2_ADDRESSES_IN_BLOCK == 0) {
  137. next_addresses(&address_block, &input_block, &zero_block);
  138. }
  139. pseudo_rand = address_block.v[i % ARGON2_ADDRESSES_IN_BLOCK];
  140. }
  141. else {
  142. pseudo_rand = instance->memory[prev_offset].v[0];
  143. }
  144. /* 1.2.2 Computing the lane of the reference block */
  145. ref_lane = ((pseudo_rand >> 32)) % instance->lanes;
  146. if ((position.pass == 0) && (position.slice == 0)) {
  147. /* Can not reference other lanes yet */
  148. ref_lane = position.lane;
  149. }
  150. /* 1.2.3 Computing the number of possible reference block within the
  151. * lane.
  152. */
  153. position.index = i;
  154. ref_index = index_alpha(instance, &position, pseudo_rand & 0xFFFFFFFF,
  155. ref_lane == position.lane);
  156. /* 2 Creating a new block */
  157. ref_block =
  158. instance->memory + instance->lane_length * ref_lane + ref_index;
  159. curr_block = instance->memory + curr_offset;
  160. if (ARGON2_VERSION_10 == instance->version) {
  161. /* version 1.2.1 and earlier: overwrite, not XOR */
  162. fill_block(instance->memory + prev_offset, ref_block, curr_block, 0);
  163. }
  164. else {
  165. if (0 == position.pass) {
  166. fill_block(instance->memory + prev_offset, ref_block,
  167. curr_block, 0);
  168. }
  169. else {
  170. fill_block(instance->memory + prev_offset, ref_block,
  171. curr_block, 1);
  172. }
  173. }
  174. }
  175. }