1 /*
2 * Argon2 source code package
3 *
4 * Written by Daniel Dinu and Dmitry Khovratovich, 2015
5 *
6 * This work is licensed under a Creative Commons CC0 1.0 License/Waiver.
7 *
8 * You should have received a copy of the CC0 Public Domain Dedication along
9 * with
10 * this software. If not, see
11 * <http://creativecommons.org/publicdomain/zero/1.0/>.
12 */
13
14 #include <stdint.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include "argon2-core.h"
19 #include "argon2.h"
20 #include "blamka-round-ref.h"
21 #include "private/common.h"
22
23 static void
fill_block(const block * prev_block,const block * ref_block,block * next_block)24 fill_block(const block *prev_block, const block *ref_block, block *next_block)
25 {
26 block blockR, block_tmp;
27 unsigned i;
28
29 copy_block(&blockR, ref_block);
30 xor_block(&blockR, prev_block);
31 copy_block(&block_tmp, &blockR);
32 /* Now blockR = ref_block + prev_block and bloc_tmp = ref_block + prev_block
33 Apply Blake2 on columns of 64-bit words: (0,1,...,15), then
34 (16,17,..31)... finally (112,113,...127) */
35 for (i = 0; i < 8; ++i) {
36 BLAKE2_ROUND_NOMSG(
37 blockR.v[16 * i], blockR.v[16 * i + 1], blockR.v[16 * i + 2],
38 blockR.v[16 * i + 3], blockR.v[16 * i + 4], blockR.v[16 * i + 5],
39 blockR.v[16 * i + 6], blockR.v[16 * i + 7], blockR.v[16 * i + 8],
40 blockR.v[16 * i + 9], blockR.v[16 * i + 10], blockR.v[16 * i + 11],
41 blockR.v[16 * i + 12], blockR.v[16 * i + 13], blockR.v[16 * i + 14],
42 blockR.v[16 * i + 15]);
43 }
44
45 /* Apply Blake2 on rows of 64-bit words: (0,1,16,17,...112,113), then
46 (2,3,18,19,...,114,115).. finally (14,15,30,31,...,126,127) */
47 for (i = 0; i < 8; i++) {
48 BLAKE2_ROUND_NOMSG(
49 blockR.v[2 * i], blockR.v[2 * i + 1], blockR.v[2 * i + 16],
50 blockR.v[2 * i + 17], blockR.v[2 * i + 32], blockR.v[2 * i + 33],
51 blockR.v[2 * i + 48], blockR.v[2 * i + 49], blockR.v[2 * i + 64],
52 blockR.v[2 * i + 65], blockR.v[2 * i + 80], blockR.v[2 * i + 81],
53 blockR.v[2 * i + 96], blockR.v[2 * i + 97], blockR.v[2 * i + 112],
54 blockR.v[2 * i + 113]);
55 }
56
57 copy_block(next_block, &block_tmp);
58 xor_block(next_block, &blockR);
59 }
60
61 static void
fill_block_with_xor(const block * prev_block,const block * ref_block,block * next_block)62 fill_block_with_xor(const block *prev_block, const block *ref_block,
63 block *next_block)
64 {
65 block blockR, block_tmp;
66 unsigned i;
67
68 copy_block(&blockR, ref_block);
69 xor_block(&blockR, prev_block);
70 copy_block(&block_tmp, &blockR);
71 xor_block(&block_tmp,
72 next_block); /* Saving the next block contents for XOR over */
73 /* Now blockR = ref_block + prev_block and bloc_tmp = ref_block + prev_block
74 * + next_block */
75 /* Apply Blake2 on columns of 64-bit words: (0,1,...,15) , then
76 (16,17,..31)... finally (112,113,...127) */
77 for (i = 0; i < 8; ++i) {
78 BLAKE2_ROUND_NOMSG(
79 blockR.v[16 * i], blockR.v[16 * i + 1], blockR.v[16 * i + 2],
80 blockR.v[16 * i + 3], blockR.v[16 * i + 4], blockR.v[16 * i + 5],
81 blockR.v[16 * i + 6], blockR.v[16 * i + 7], blockR.v[16 * i + 8],
82 blockR.v[16 * i + 9], blockR.v[16 * i + 10], blockR.v[16 * i + 11],
83 blockR.v[16 * i + 12], blockR.v[16 * i + 13], blockR.v[16 * i + 14],
84 blockR.v[16 * i + 15]);
85 }
86
87 /* Apply Blake2 on rows of 64-bit words: (0,1,16,17,...112,113), then
88 (2,3,18,19,...,114,115).. finally (14,15,30,31,...,126,127) */
89 for (i = 0; i < 8; i++) {
90 BLAKE2_ROUND_NOMSG(
91 blockR.v[2 * i], blockR.v[2 * i + 1], blockR.v[2 * i + 16],
92 blockR.v[2 * i + 17], blockR.v[2 * i + 32], blockR.v[2 * i + 33],
93 blockR.v[2 * i + 48], blockR.v[2 * i + 49], blockR.v[2 * i + 64],
94 blockR.v[2 * i + 65], blockR.v[2 * i + 80], blockR.v[2 * i + 81],
95 blockR.v[2 * i + 96], blockR.v[2 * i + 97], blockR.v[2 * i + 112],
96 blockR.v[2 * i + 113]);
97 }
98
99 copy_block(next_block, &block_tmp);
100 xor_block(next_block, &blockR);
101 }
102
103 /*
104 * Generate pseudo-random values to reference blocks in the segment and puts
105 * them into the array
106 * @param instance Pointer to the current instance
107 * @param position Pointer to the current position
108 * @param pseudo_rands Pointer to the array of 64-bit values
109 * @pre pseudo_rands must point to @a instance->segment_length allocated values
110 */
111 static void
generate_addresses(const argon2_instance_t * instance,const argon2_position_t * position,uint64_t * pseudo_rands)112 generate_addresses(const argon2_instance_t *instance,
113 const argon2_position_t *position, uint64_t *pseudo_rands)
114 {
115 block zero_block, input_block, address_block, tmp_block;
116 uint32_t i;
117
118 init_block_value(&zero_block, 0);
119 init_block_value(&input_block, 0);
120
121 if (instance != NULL && position != NULL) {
122 input_block.v[0] = position->pass;
123 input_block.v[1] = position->lane;
124 input_block.v[2] = position->slice;
125 input_block.v[3] = instance->memory_blocks;
126 input_block.v[4] = instance->passes;
127 input_block.v[5] = instance->type;
128
129 for (i = 0; i < instance->segment_length; ++i) {
130 if (i % ARGON2_ADDRESSES_IN_BLOCK == 0) {
131 input_block.v[6]++;
132 init_block_value(&tmp_block, 0);
133 init_block_value(&address_block, 0);
134 fill_block_with_xor(&zero_block, &input_block, &tmp_block);
135 fill_block_with_xor(&zero_block, &tmp_block, &address_block);
136 }
137
138 pseudo_rands[i] = address_block.v[i % ARGON2_ADDRESSES_IN_BLOCK];
139 }
140 }
141 }
142
143 void
fill_segment_ref(const argon2_instance_t * instance,argon2_position_t position)144 fill_segment_ref(const argon2_instance_t *instance, argon2_position_t position)
145 {
146 block *ref_block = NULL, *curr_block = NULL;
147 /* Pseudo-random values that determine the reference block position */
148 uint64_t *pseudo_rands = NULL;
149 uint64_t pseudo_rand, ref_index, ref_lane;
150 uint32_t prev_offset, curr_offset;
151 uint32_t starting_index;
152 uint32_t i;
153 int data_independent_addressing = 1;
154
155 if (instance == NULL) {
156 return;
157 }
158
159 if (instance->type == Argon2_id &&
160 (position.pass != 0 || position.slice >= ARGON2_SYNC_POINTS / 2)) {
161 data_independent_addressing = 0;
162 }
163
164 pseudo_rands = instance->pseudo_rands;
165
166 if (data_independent_addressing) {
167 generate_addresses(instance, &position, pseudo_rands);
168 }
169
170 starting_index = 0;
171
172 if ((0 == position.pass) && (0 == position.slice)) {
173 starting_index = 2; /* we have already generated the first two blocks */
174 }
175
176 /* Offset of the current block */
177 curr_offset = position.lane * instance->lane_length +
178 position.slice * instance->segment_length + starting_index;
179
180 if (0 == curr_offset % instance->lane_length) {
181 /* Last block in this lane */
182 prev_offset = curr_offset + instance->lane_length - 1;
183 } else {
184 /* Previous block */
185 prev_offset = curr_offset - 1;
186 }
187
188 for (i = starting_index; i < instance->segment_length;
189 ++i, ++curr_offset, ++prev_offset) {
190 /*1.1 Rotating prev_offset if needed */
191 if (curr_offset % instance->lane_length == 1) {
192 prev_offset = curr_offset - 1;
193 }
194
195 /* 1.2 Computing the index of the reference block */
196 /* 1.2.1 Taking pseudo-random value from the previous block */
197 if (data_independent_addressing) {
198 #pragma warning(push)
199 #pragma warning(disable : 6385)
200 pseudo_rand = pseudo_rands[i];
201 #pragma warning(pop)
202 } else {
203 pseudo_rand = instance->region->memory[prev_offset].v[0];
204 }
205
206 /* 1.2.2 Computing the lane of the reference block */
207 ref_lane = ((pseudo_rand >> 32)) % instance->lanes;
208
209 if ((position.pass == 0) && (position.slice == 0)) {
210 /* Can not reference other lanes yet */
211 ref_lane = position.lane;
212 }
213
214 /* 1.2.3 Computing the number of possible reference block within the
215 * lane.
216 */
217 position.index = i;
218 ref_index = index_alpha(instance, &position, pseudo_rand & 0xFFFFFFFF,
219 ref_lane == position.lane);
220
221 /* 2 Creating a new block */
222 ref_block = instance->region->memory +
223 instance->lane_length * ref_lane + ref_index;
224 curr_block = instance->region->memory + curr_offset;
225 if (position.pass != 0) {
226 fill_block_with_xor(instance->region->memory + prev_offset,
227 ref_block, curr_block);
228 } else {
229 fill_block(instance->region->memory + prev_offset, ref_block,
230 curr_block);
231 }
232 }
233 }
234