argon2_ref.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #include "argon2_core.h"
  35. #include "blake2/blamka-round-ref.h"
  36. #include "blake2/blake2-impl.h"
  37. #include "blake2/blake2.h"
  38. static void copy_block(block* dst, const block* src) {
  39. memcpy(dst->v, src->v, sizeof(uint64_t) * ARGON2_QWORDS_IN_BLOCK);
  40. }
  41. static void xor_block(block* dst, const block* src) {
  42. int i;
  43. for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
  44. dst->v[i] ^= src->v[i];
  45. }
  46. }
  47. /*
  48. * Function fills a new memory block and optionally XORs the old block over the new one.
  49. * @next_block must be initialized.
  50. * @param prev_block Pointer to the previous block
  51. * @param ref_block Pointer to the reference block
  52. * @param next_block Pointer to the block to be constructed
  53. * @param with_xor Whether to XOR into the new block (1) or just overwrite (0)
  54. * @pre all block pointers must be valid
  55. */
  56. static void fill_block(const block *prev_block, const block *ref_block,
  57. block *next_block, int with_xor) {
  58. block blockR, block_tmp;
  59. unsigned i;
  60. copy_block(&blockR, ref_block);
  61. xor_block(&blockR, prev_block);
  62. copy_block(&block_tmp, &blockR);
  63. /* Now blockR = ref_block + prev_block and block_tmp = ref_block + prev_block */
  64. if (with_xor) {
  65. /* Saving the next block contents for XOR over: */
  66. xor_block(&block_tmp, next_block);
  67. /* Now blockR = ref_block + prev_block and
  68. block_tmp = ref_block + prev_block + next_block */
  69. }
  70. /* Apply Blake2 on columns of 64-bit words: (0,1,...,15) , then
  71. (16,17,..31)... finally (112,113,...127) */
  72. for (i = 0; i < 8; ++i) {
  73. BLAKE2_ROUND_NOMSG(
  74. blockR.v[16 * i], blockR.v[16 * i + 1], blockR.v[16 * i + 2],
  75. blockR.v[16 * i + 3], blockR.v[16 * i + 4], blockR.v[16 * i + 5],
  76. blockR.v[16 * i + 6], blockR.v[16 * i + 7], blockR.v[16 * i + 8],
  77. blockR.v[16 * i + 9], blockR.v[16 * i + 10], blockR.v[16 * i + 11],
  78. blockR.v[16 * i + 12], blockR.v[16 * i + 13], blockR.v[16 * i + 14],
  79. blockR.v[16 * i + 15]);
  80. }
  81. /* Apply Blake2 on rows of 64-bit words: (0,1,16,17,...112,113), then
  82. (2,3,18,19,...,114,115).. finally (14,15,30,31,...,126,127) */
  83. for (i = 0; i < 8; i++) {
  84. BLAKE2_ROUND_NOMSG(
  85. blockR.v[2 * i], blockR.v[2 * i + 1], blockR.v[2 * i + 16],
  86. blockR.v[2 * i + 17], blockR.v[2 * i + 32], blockR.v[2 * i + 33],
  87. blockR.v[2 * i + 48], blockR.v[2 * i + 49], blockR.v[2 * i + 64],
  88. blockR.v[2 * i + 65], blockR.v[2 * i + 80], blockR.v[2 * i + 81],
  89. blockR.v[2 * i + 96], blockR.v[2 * i + 97], blockR.v[2 * i + 112],
  90. blockR.v[2 * i + 113]);
  91. }
  92. copy_block(next_block, &block_tmp);
  93. xor_block(next_block, &blockR);
  94. }
  95. void randomx_argon2_fill_segment_ref(const argon2_instance_t *instance,
  96. argon2_position_t position) {
  97. block *ref_block = NULL, *curr_block = NULL;
  98. block address_block, input_block, zero_block;
  99. uint64_t pseudo_rand, ref_index, ref_lane;
  100. uint32_t prev_offset, curr_offset;
  101. uint32_t starting_index;
  102. uint32_t i;
  103. if (instance == NULL) {
  104. return;
  105. }
  106. starting_index = 0;
  107. if ((0 == position.pass) && (0 == position.slice)) {
  108. starting_index = 2; /* we have already generated the first two blocks */
  109. }
  110. /* Offset of the current block */
  111. curr_offset = position.lane * instance->lane_length +
  112. position.slice * instance->segment_length + starting_index;
  113. if (0 == curr_offset % instance->lane_length) {
  114. /* Last block in this lane */
  115. prev_offset = curr_offset + instance->lane_length - 1;
  116. }
  117. else {
  118. /* Previous block */
  119. prev_offset = curr_offset - 1;
  120. }
  121. for (i = starting_index; i < instance->segment_length;
  122. ++i, ++curr_offset, ++prev_offset) {
  123. /*1.1 Rotating prev_offset if needed */
  124. if (curr_offset % instance->lane_length == 1) {
  125. prev_offset = curr_offset - 1;
  126. }
  127. /* 1.2 Computing the index of the reference block */
  128. /* 1.2.1 Taking pseudo-random value from the previous block */
  129. pseudo_rand = instance->memory[prev_offset].v[0];
  130. /* 1.2.2 Computing the lane of the reference block */
  131. ref_lane = ((pseudo_rand >> 32)) % instance->lanes;
  132. if ((position.pass == 0) && (position.slice == 0)) {
  133. /* Can not reference other lanes yet */
  134. ref_lane = position.lane;
  135. }
  136. /* 1.2.3 Computing the number of possible reference block within the
  137. * lane.
  138. */
  139. position.index = i;
  140. ref_index = randomx_argon2_index_alpha(instance, &position, pseudo_rand & 0xFFFFFFFF,
  141. ref_lane == position.lane);
  142. /* 2 Creating a new block */
  143. ref_block =
  144. instance->memory + instance->lane_length * ref_lane + ref_index;
  145. curr_block = instance->memory + curr_offset;
  146. if (ARGON2_VERSION_10 == instance->version) {
  147. /* version 1.2.1 and earlier: overwrite, not XOR */
  148. fill_block(instance->memory + prev_offset, ref_block, curr_block, 0);
  149. }
  150. else {
  151. if (0 == position.pass) {
  152. fill_block(instance->memory + prev_offset, ref_block,
  153. curr_block, 0);
  154. }
  155. else {
  156. fill_block(instance->memory + prev_offset, ref_block,
  157. curr_block, 1);
  158. }
  159. }
  160. }
  161. }