ref.c 7.1 KB

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