Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tpruvot
GitHub Repository: tpruvot/cpuminer-multi
Path: blob/linux/lyra2/Lyra2.c
1201 views
1
/**
2
* Implementation of the Lyra2 Password Hashing Scheme (PHS).
3
*
4
* Author: The Lyra PHC team (http://www.lyra-kdf.net/) -- 2014.
5
*
6
* This software is hereby placed in the public domain.
7
*
8
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
9
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
11
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
12
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
13
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
14
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
15
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
16
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
17
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
18
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
*/
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <string.h>
23
#include <time.h>
24
25
#include "Lyra2.h"
26
#include "Sponge.h"
27
28
/**
29
* Executes Lyra2 based on the G function from Blake2b. This version supports salts and passwords
30
* whose combined length is smaller than the size of the memory matrix, (i.e., (nRows x nCols x b) bits,
31
* where "b" is the underlying sponge's bitrate). In this implementation, the "basil" is composed by all
32
* integer parameters (treated as type "unsigned int") in the order they are provided, plus the value
33
* of nCols, (i.e., basil = kLen || pwdlen || saltlen || timeCost || nRows || nCols).
34
*
35
* @param K The derived key to be output by the algorithm
36
* @param kLen Desired key length
37
* @param pwd User password
38
* @param pwdlen Password length
39
* @param salt Salt
40
* @param saltlen Salt length
41
* @param timeCost Parameter to determine the processing time (T)
42
* @param nRows Number or rows of the memory matrix (R)
43
* @param nCols Number of columns of the memory matrix (C)
44
*
45
* @return 0 if the key is generated correctly; -1 if there is an error (usually due to lack of memory for allocation)
46
*/
47
int LYRA2(void *K, int64_t kLen, const void *pwd, int32_t pwdlen, const void *salt, int32_t saltlen, int64_t timeCost, const int16_t nRows, const int16_t nCols)
48
{
49
//============================= Basic variables ============================//
50
int64_t row = 2; //index of row to be processed
51
int64_t prev = 1; //index of prev (last row ever computed/modified)
52
int64_t rowa = 0; //index of row* (a previous row, deterministically picked during Setup and randomly picked while Wandering)
53
int64_t tau; //Time Loop iterator
54
int64_t step = 1; //Visitation step (used during Setup and Wandering phases)
55
int64_t window = 2; //Visitation window (used to define which rows can be revisited during Setup)
56
int64_t gap = 1; //Modifier to the step, assuming the values 1 or -1
57
int64_t i; //auxiliary iteration counter
58
int64_t v64; // 64bit var for memcpy
59
//==========================================================================/
60
61
//========== Initializing the Memory Matrix and pointers to it =============//
62
//Tries to allocate enough space for the whole memory matrix
63
64
const int64_t ROW_LEN_INT64 = BLOCK_LEN_INT64 * nCols;
65
const int64_t ROW_LEN_BYTES = ROW_LEN_INT64 * 8;
66
// for Lyra2REv2, nCols = 4, v1 was using 8
67
const int64_t BLOCK_LEN = (nCols == 4) ? BLOCK_LEN_BLAKE2_SAFE_INT64 : BLOCK_LEN_BLAKE2_SAFE_BYTES;
68
69
i = (int64_t)ROW_LEN_BYTES * nRows;
70
uint64_t *wholeMatrix = malloc(i);
71
if (wholeMatrix == NULL) {
72
return -1;
73
}
74
memset(wholeMatrix, 0, i);
75
76
//Allocates pointers to each row of the matrix
77
uint64_t **memMatrix = malloc(sizeof(uint64_t*) * nRows);
78
if (memMatrix == NULL) {
79
return -1;
80
}
81
//Places the pointers in the correct positions
82
uint64_t *ptrWord = wholeMatrix;
83
for (i = 0; i < nRows; i++) {
84
memMatrix[i] = ptrWord;
85
ptrWord += ROW_LEN_INT64;
86
}
87
//==========================================================================/
88
89
//============= Getting the password + salt + basil padded with 10*1 ===============//
90
//OBS.:The memory matrix will temporarily hold the password: not for saving memory,
91
//but this ensures that the password copied locally will be overwritten as soon as possible
92
93
//First, we clean enough blocks for the password, salt, basil and padding
94
int64_t nBlocksInput = ((saltlen + pwdlen + 6 * sizeof(uint64_t)) / BLOCK_LEN_BLAKE2_SAFE_BYTES) + 1;
95
96
byte *ptrByte = (byte*) wholeMatrix;
97
98
//Prepends the password
99
memcpy(ptrByte, pwd, pwdlen);
100
ptrByte += pwdlen;
101
102
//Concatenates the salt
103
memcpy(ptrByte, salt, saltlen);
104
ptrByte += saltlen;
105
106
memset(ptrByte, 0, nBlocksInput * BLOCK_LEN_BLAKE2_SAFE_BYTES - (saltlen + pwdlen));
107
108
//Concatenates the basil: every integer passed as parameter, in the order they are provided by the interface
109
memcpy(ptrByte, &kLen, sizeof(int64_t));
110
ptrByte += sizeof(uint64_t);
111
v64 = pwdlen;
112
memcpy(ptrByte, &v64, sizeof(int64_t));
113
ptrByte += sizeof(uint64_t);
114
v64 = saltlen;
115
memcpy(ptrByte, &v64, sizeof(int64_t));
116
ptrByte += sizeof(uint64_t);
117
v64 = timeCost;
118
memcpy(ptrByte, &v64, sizeof(int64_t));
119
ptrByte += sizeof(uint64_t);
120
v64 = nRows;
121
memcpy(ptrByte, &v64, sizeof(int64_t));
122
ptrByte += sizeof(uint64_t);
123
v64 = nCols;
124
memcpy(ptrByte, &v64, sizeof(int64_t));
125
ptrByte += sizeof(uint64_t);
126
127
//Now comes the padding
128
*ptrByte = 0x80; //first byte of padding: right after the password
129
ptrByte = (byte*) wholeMatrix; //resets the pointer to the start of the memory matrix
130
ptrByte += nBlocksInput * BLOCK_LEN_BLAKE2_SAFE_BYTES - 1; //sets the pointer to the correct position: end of incomplete block
131
*ptrByte ^= 0x01; //last byte of padding: at the end of the last incomplete block
132
//==========================================================================/
133
134
//======================= Initializing the Sponge State ====================//
135
//Sponge state: 16 uint64_t, BLOCK_LEN_INT64 words of them for the bitrate (b) and the remainder for the capacity (c)
136
uint64_t state[16];
137
initState(state);
138
//==========================================================================/
139
140
//================================ Setup Phase =============================//
141
//Absorbing salt, password and basil: this is the only place in which the block length is hard-coded to 512 bits
142
ptrWord = wholeMatrix;
143
for (i = 0; i < nBlocksInput; i++) {
144
absorbBlockBlake2Safe(state, ptrWord); //absorbs each block of pad(pwd || salt || basil)
145
ptrWord += BLOCK_LEN; //goes to next block of pad(pwd || salt || basil)
146
}
147
148
//Initializes M[0] and M[1]
149
reducedSqueezeRow0(state, memMatrix[0], nCols); //The locally copied password is most likely overwritten here
150
151
reducedDuplexRow1(state, memMatrix[0], memMatrix[1], nCols);
152
153
do {
154
//M[row] = rand; //M[row*] = M[row*] XOR rotW(rand)
155
156
reducedDuplexRowSetup(state, memMatrix[prev], memMatrix[rowa], memMatrix[row], nCols);
157
158
//updates the value of row* (deterministically picked during Setup))
159
rowa = (rowa + step) & (window - 1);
160
//update prev: it now points to the last row ever computed
161
prev = row;
162
//updates row: goes to the next row to be computed
163
row++;
164
165
//Checks if all rows in the window where visited.
166
if (rowa == 0) {
167
step = window + gap; //changes the step: approximately doubles its value
168
window *= 2; //doubles the size of the re-visitation window
169
gap = -gap; //inverts the modifier to the step
170
}
171
172
} while (row < nRows);
173
//==========================================================================/
174
175
//============================ Wandering Phase =============================//
176
row = 0; //Resets the visitation to the first row of the memory matrix
177
for (tau = 1; tau <= timeCost; tau++) {
178
//Step is approximately half the number of all rows of the memory matrix for an odd tau; otherwise, it is -1
179
step = (tau % 2 == 0) ? -1 : nRows / 2 - 1;
180
do {
181
//Selects a pseudorandom index row*
182
//------------------------------------------------------------------------------------------
183
rowa = state[0] & (unsigned int)(nRows-1); //(USE THIS IF nRows IS A POWER OF 2)
184
//rowa = state[0] % nRows; //(USE THIS FOR THE "GENERIC" CASE)
185
//------------------------------------------------------------------------------------------
186
187
//Performs a reduced-round duplexing operation over M[row*] XOR M[prev], updating both M[row*] and M[row]
188
reducedDuplexRow(state, memMatrix[prev], memMatrix[rowa], memMatrix[row], nCols);
189
190
//update prev: it now points to the last row ever computed
191
prev = row;
192
193
//updates row: goes to the next row to be computed
194
//------------------------------------------------------------------------------------------
195
row = (row + step) & (unsigned int)(nRows-1); //(USE THIS IF nRows IS A POWER OF 2)
196
//row = (row + step) % nRows; //(USE THIS FOR THE "GENERIC" CASE)
197
//------------------------------------------------------------------------------------------
198
199
} while (row != 0);
200
}
201
202
//============================ Wrap-up Phase ===============================//
203
//Absorbs the last block of the memory matrix
204
absorbBlock(state, memMatrix[rowa]);
205
206
//Squeezes the key
207
squeeze(state, K, (unsigned int) kLen);
208
209
//========================= Freeing the memory =============================//
210
free(memMatrix);
211
free(wholeMatrix);
212
213
return 0;
214
}
215
216
int LYRA2_3(void *K, int64_t kLen, const void *pwd, int32_t pwdlen, const void *salt, int32_t saltlen, int64_t timeCost, const int16_t nRows, const int16_t nCols)
217
{
218
//============================= Basic variables ============================//
219
int64_t row = 2; //index of row to be processed
220
int64_t prev = 1; //index of prev (last row ever computed/modified)
221
int64_t rowa = 0; //index of row* (a previous row, deterministically picked during Setup and randomly picked while Wandering)
222
int64_t tau; //Time Loop iterator
223
int64_t step = 1; //Visitation step (used during Setup and Wandering phases)
224
int64_t window = 2; //Visitation window (used to define which rows can be revisited during Setup)
225
int64_t gap = 1; //Modifier to the step, assuming the values 1 or -1
226
int64_t i; //auxiliary iteration counter
227
int64_t v64; // 64bit var for memcpy
228
uint64_t instance = 0;
229
//==========================================================================/
230
231
//========== Initializing the Memory Matrix and pointers to it =============//
232
//Tries to allocate enough space for the whole memory matrix
233
234
const int64_t ROW_LEN_INT64 = BLOCK_LEN_INT64 * nCols;
235
const int64_t ROW_LEN_BYTES = ROW_LEN_INT64 * 8;
236
const int64_t BLOCK_LEN = BLOCK_LEN_BLAKE2_SAFE_INT64;
237
238
size_t sz = (size_t)ROW_LEN_BYTES * nRows;
239
uint64_t *wholeMatrix = malloc(sz);
240
if (wholeMatrix == NULL) {
241
return -1;
242
}
243
memset(wholeMatrix, 0, sz);
244
245
//Allocates pointers to each row of the matrix
246
uint64_t **memMatrix = malloc(sizeof(uint64_t*) * nRows);
247
if (memMatrix == NULL) {
248
return -1;
249
}
250
//Places the pointers in the correct positions
251
uint64_t *ptrWord = wholeMatrix;
252
for (i = 0; i < nRows; i++) {
253
memMatrix[i] = ptrWord;
254
ptrWord += ROW_LEN_INT64;
255
}
256
//==========================================================================/
257
258
//============= Getting the password + salt + basil padded with 10*1 ===============//
259
//OBS.:The memory matrix will temporarily hold the password: not for saving memory,
260
//but this ensures that the password copied locally will be overwritten as soon as possible
261
262
//First, we clean enough blocks for the password, salt, basil and padding
263
int64_t nBlocksInput = ((saltlen + pwdlen + 6 * sizeof(uint64_t)) / BLOCK_LEN_BLAKE2_SAFE_BYTES) + 1;
264
265
byte *ptrByte = (byte*) wholeMatrix;
266
267
//Prepends the password
268
memcpy(ptrByte, pwd, pwdlen);
269
ptrByte += pwdlen;
270
271
//Concatenates the salt
272
memcpy(ptrByte, salt, saltlen);
273
ptrByte += saltlen;
274
275
memset(ptrByte, 0, (size_t) (nBlocksInput * BLOCK_LEN_BLAKE2_SAFE_BYTES - (saltlen + pwdlen)));
276
277
//Concatenates the basil: every integer passed as parameter, in the order they are provided by the interface
278
memcpy(ptrByte, &kLen, sizeof(int64_t));
279
ptrByte += sizeof(uint64_t);
280
v64 = pwdlen;
281
memcpy(ptrByte, &v64, sizeof(int64_t));
282
ptrByte += sizeof(uint64_t);
283
v64 = saltlen;
284
memcpy(ptrByte, &v64, sizeof(int64_t));
285
ptrByte += sizeof(uint64_t);
286
v64 = timeCost;
287
memcpy(ptrByte, &v64, sizeof(int64_t));
288
ptrByte += sizeof(uint64_t);
289
v64 = nRows;
290
memcpy(ptrByte, &v64, sizeof(int64_t));
291
ptrByte += sizeof(uint64_t);
292
v64 = nCols;
293
memcpy(ptrByte, &v64, sizeof(int64_t));
294
ptrByte += sizeof(uint64_t);
295
296
//Now comes the padding
297
*ptrByte = 0x80; //first byte of padding: right after the password
298
ptrByte = (byte*) wholeMatrix; //resets the pointer to the start of the memory matrix
299
ptrByte += nBlocksInput * BLOCK_LEN_BLAKE2_SAFE_BYTES - 1; //sets the pointer to the correct position: end of incomplete block
300
*ptrByte ^= 0x01; //last byte of padding: at the end of the last incomplete block
301
//==========================================================================/
302
303
//======================= Initializing the Sponge State ====================//
304
//Sponge state: 16 uint64_t, BLOCK_LEN_INT64 words of them for the bitrate (b) and the remainder for the capacity (c)
305
uint64_t state[16];
306
initState(state);
307
//==========================================================================/
308
309
//================================ Setup Phase =============================//
310
//Absorbing salt, password and basil: this is the only place in which the block length is hard-coded to 512 bits
311
ptrWord = wholeMatrix;
312
for (i = 0; i < nBlocksInput; i++) {
313
absorbBlockBlake2Safe(state, ptrWord); //absorbs each block of pad(pwd || salt || basil)
314
ptrWord += BLOCK_LEN; //goes to next block of pad(pwd || salt || basil)
315
}
316
317
//Initializes M[0] and M[1]
318
reducedSqueezeRow0(state, memMatrix[0], nCols); //The locally copied password is most likely overwritten here
319
320
reducedDuplexRow1(state, memMatrix[0], memMatrix[1], nCols);
321
322
do {
323
//M[row] = rand; //M[row*] = M[row*] XOR rotW(rand)
324
325
reducedDuplexRowSetup(state, memMatrix[prev], memMatrix[rowa], memMatrix[row], nCols);
326
327
//updates the value of row* (deterministically picked during Setup))
328
rowa = (rowa + step) & (window - 1);
329
//update prev: it now points to the last row ever computed
330
prev = row;
331
//updates row: goes to the next row to be computed
332
row++;
333
334
//Checks if all rows in the window where visited.
335
if (rowa == 0) {
336
step = window + gap; //changes the step: approximately doubles its value
337
window *= 2; //doubles the size of the re-visitation window
338
gap = -gap; //inverts the modifier to the step
339
}
340
341
} while (row < nRows);
342
//==========================================================================/
343
344
//============================ Wandering Phase =============================//
345
row = 0; //Resets the visitation to the first row of the memory matrix
346
for (tau = 1; tau <= timeCost; tau++) {
347
//Step is approximately half the number of all rows of the memory matrix for an odd tau; otherwise, it is -1
348
step = ((tau & 1) == 0) ? -1 : (nRows >> 1) - 1;
349
do {
350
//Selects a pseudorandom index row*
351
//------------------------------------------------------------------------------------------
352
instance = state[instance & 0xF];
353
rowa = state[instance & 0xF] & (unsigned int)(nRows-1);
354
355
//rowa = state[0] & (unsigned int)(nRows-1); //(USE THIS IF nRows IS A POWER OF 2)
356
//rowa = state[0] % nRows; //(USE THIS FOR THE "GENERIC" CASE)
357
//------------------------------------------------------------------------------------------
358
359
//Performs a reduced-round duplexing operation over M[row*] XOR M[prev], updating both M[row*] and M[row]
360
reducedDuplexRow(state, memMatrix[prev], memMatrix[rowa], memMatrix[row], nCols);
361
362
//update prev: it now points to the last row ever computed
363
prev = row;
364
365
//updates row: goes to the next row to be computed
366
//------------------------------------------------------------------------------------------
367
row = (row + step) & (unsigned int)(nRows-1); //(USE THIS IF nRows IS A POWER OF 2)
368
//row = (row + step) % nRows; //(USE THIS FOR THE "GENERIC" CASE)
369
//------------------------------------------------------------------------------------------
370
371
} while (row != 0);
372
}
373
374
//============================ Wrap-up Phase ===============================//
375
//Absorbs the last block of the memory matrix
376
absorbBlock(state, memMatrix[rowa]);
377
378
//Squeezes the key
379
squeeze(state, K, (unsigned int) kLen);
380
381
//========================= Freeing the memory =============================//
382
free(memMatrix);
383
free(wholeMatrix);
384
385
return 0;
386
}
387
388