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
18 #include <stdint.h>
19 #include <string.h>
20 #include <stdlib.h>
21
22 #include "argon2.h"
23 #include "ref.h"
24
25 #include "blamka-round-ref.h"
26 #include "blake2-impl.h"
27 #include "blake2.h"
28
29
fill_block(const block * prev_block,const block * ref_block,block * next_block,int with_xor)30 static void fill_block(const block *prev_block, const block *ref_block,
31 block *next_block, int with_xor) {
32 block blockR, block_tmp;
33 unsigned i;
34
35 copy_block(&blockR, ref_block);
36 xor_block(&blockR, prev_block);
37 copy_block(&block_tmp, &blockR);
38 /* Now blockR = ref_block + prev_block and block_tmp = ref_block + prev_block */
39 if (with_xor) {
40 /* Saving the next block contents for XOR over: */
41 xor_block(&block_tmp, next_block);
42 /* Now blockR = ref_block + prev_block and
43 block_tmp = ref_block + prev_block + next_block */
44 }
45
46 /* Apply Blake2 on columns of 64-bit words: (0,1,...,15) , then
47 (16,17,..31)... finally (112,113,...127) */
48 for (i = 0; i < 8; ++i) {
49 BLAKE2_ROUND_NOMSG(
50 blockR.v[16 * i], blockR.v[16 * i + 1], blockR.v[16 * i + 2],
51 blockR.v[16 * i + 3], blockR.v[16 * i + 4], blockR.v[16 * i + 5],
52 blockR.v[16 * i + 6], blockR.v[16 * i + 7], blockR.v[16 * i + 8],
53 blockR.v[16 * i + 9], blockR.v[16 * i + 10], blockR.v[16 * i + 11],
54 blockR.v[16 * i + 12], blockR.v[16 * i + 13], blockR.v[16 * i + 14],
55 blockR.v[16 * i + 15]);
56 }
57
58 /* Apply Blake2 on rows of 64-bit words: (0,1,16,17,...112,113), then
59 (2,3,18,19,...,114,115).. finally (14,15,30,31,...,126,127) */
60 for (i = 0; i < 8; i++) {
61 BLAKE2_ROUND_NOMSG(
62 blockR.v[2 * i], blockR.v[2 * i + 1], blockR.v[2 * i + 16],
63 blockR.v[2 * i + 17], blockR.v[2 * i + 32], blockR.v[2 * i + 33],
64 blockR.v[2 * i + 48], blockR.v[2 * i + 49], blockR.v[2 * i + 64],
65 blockR.v[2 * i + 65], blockR.v[2 * i + 80], blockR.v[2 * i + 81],
66 blockR.v[2 * i + 96], blockR.v[2 * i + 97], blockR.v[2 * i + 112],
67 blockR.v[2 * i + 113]);
68 }
69
70 copy_block(next_block, &block_tmp);
71 xor_block(next_block, &blockR);
72 }
73
next_addresses(block * address_block,block * input_block,const block * zero_block)74 static void next_addresses(block *address_block, block *input_block,
75 const block *zero_block) {
76 input_block->v[6]++;
77 fill_block(zero_block, input_block, address_block, 0);
78 fill_block(zero_block, address_block, address_block, 0);
79 }
80
fill_segment(const argon2_instance_t * instance,argon2_position_t position)81 static void fill_segment(const argon2_instance_t *instance,
82 argon2_position_t position) {
83 block *ref_block = NULL, *curr_block = NULL;
84 block address_block, input_block, zero_block;
85 uint64_t pseudo_rand, ref_index, ref_lane;
86 uint32_t prev_offset, curr_offset;
87 uint32_t starting_index;
88 uint32_t i;
89 int data_independent_addressing;
90
91 if (instance == NULL) {
92 return;
93 }
94
95 data_independent_addressing =
96 (instance->type == Argon2_i) ||
97 (instance->type == Argon2_id && (position.pass == 0) &&
98 (position.slice < ARGON2_SYNC_POINTS / 2));
99
100 if (data_independent_addressing) {
101 init_block_value(&zero_block, 0);
102 init_block_value(&input_block, 0);
103
104 input_block.v[0] = position.pass;
105 input_block.v[1] = position.lane;
106 input_block.v[2] = position.slice;
107 input_block.v[3] = instance->memory_blocks;
108 input_block.v[4] = instance->passes;
109 input_block.v[5] = instance->type;
110 }
111
112 starting_index = 0;
113
114 if ((0 == position.pass) && (0 == position.slice)) {
115 starting_index = 2; /* we have already generated the first two blocks */
116
117 /* Don't forget to generate the first block of addresses: */
118 if (data_independent_addressing) {
119 next_addresses(&address_block, &input_block, &zero_block);
120 }
121 }
122
123 /* Offset of the current block */
124 curr_offset = position.lane * instance->lane_length +
125 position.slice * instance->segment_length + starting_index;
126
127 if (0 == curr_offset % instance->lane_length) {
128 /* Last block in this lane */
129 prev_offset = curr_offset + instance->lane_length - 1;
130 } else {
131 /* Previous block */
132 prev_offset = curr_offset - 1;
133 }
134
135 for (i = starting_index; i < instance->segment_length;
136 ++i, ++curr_offset, ++prev_offset) {
137 /*1.1 Rotating prev_offset if needed */
138 if (curr_offset % instance->lane_length == 1) {
139 prev_offset = curr_offset - 1;
140 }
141
142 /* 1.2 Computing the index of the reference block */
143 /* 1.2.1 Taking pseudo-random value from the previous block */
144 if (data_independent_addressing) {
145 if (i % ARGON2_ADDRESSES_IN_BLOCK == 0) {
146 next_addresses(&address_block, &input_block, &zero_block);
147 }
148 pseudo_rand = address_block.v[i % ARGON2_ADDRESSES_IN_BLOCK];
149 } else {
150 pseudo_rand = instance->memory[prev_offset].v[0];
151 }
152
153 /* 1.2.2 Computing the lane of the reference block */
154 ref_lane = ((pseudo_rand >> 32)) % instance->lanes;
155
156 if ((position.pass == 0) && (position.slice == 0)) {
157 /* Can not reference other lanes yet */
158 ref_lane = position.lane;
159 }
160
161 /* 1.2.3 Computing the number of possible reference block within the
162 * lane.
163 */
164 position.index = i;
165 ref_index = index_alpha(instance, &position, pseudo_rand & 0xFFFFFFFF,
166 ref_lane == position.lane);
167
168 /* 2 Creating a new block */
169 ref_block =
170 instance->memory + instance->lane_length * ref_lane + ref_index;
171 curr_block = instance->memory + curr_offset;
172 if (ARGON2_VERSION_10 == instance->version) {
173 /* version 1.2.1 and earlier: overwrite, not XOR */
174 fill_block(instance->memory + prev_offset, ref_block, curr_block, 0);
175 } else {
176 if(0 == position.pass) {
177 fill_block(instance->memory + prev_offset, ref_block,
178 curr_block, 0);
179 } else {
180 fill_block(instance->memory + prev_offset, ref_block,
181 curr_block, 1);
182 }
183 }
184 }
185 }
186