| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
/*- |
|
2
|
|
|
|
|
|
|
* Copyright 2009 Colin Percival |
|
3
|
|
|
|
|
|
|
* All rights reserved. |
|
4
|
|
|
|
|
|
|
* |
|
5
|
|
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without |
|
6
|
|
|
|
|
|
|
* modification, are permitted provided that the following conditions |
|
7
|
|
|
|
|
|
|
* are met: |
|
8
|
|
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright |
|
9
|
|
|
|
|
|
|
* notice, this list of conditions and the following disclaimer. |
|
10
|
|
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
|
11
|
|
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in the |
|
12
|
|
|
|
|
|
|
* documentation and/or other materials provided with the distribution. |
|
13
|
|
|
|
|
|
|
* |
|
14
|
|
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
|
15
|
|
|
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
16
|
|
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
17
|
|
|
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 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 |
|
20
|
|
|
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
21
|
|
|
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
22
|
|
|
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|
23
|
|
|
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
24
|
|
|
|
|
|
|
* SUCH DAMAGE. |
|
25
|
|
|
|
|
|
|
* |
|
26
|
|
|
|
|
|
|
* This file was originally written by Colin Percival as part of the Tarsnap |
|
27
|
|
|
|
|
|
|
* online backup system. |
|
28
|
|
|
|
|
|
|
*/ |
|
29
|
|
|
|
|
|
|
#include "scrypt_platform.h" |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
#include |
|
32
|
|
|
|
|
|
|
/*XXX #include */ |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
#include |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
#ifdef _MSC_VER |
|
37
|
|
|
|
|
|
|
#include "msinttypes.h" |
|
38
|
|
|
|
|
|
|
#elif defined(__sun) || defined(__sun__) |
|
39
|
|
|
|
|
|
|
#include |
|
40
|
|
|
|
|
|
|
#else |
|
41
|
|
|
|
|
|
|
#include |
|
42
|
|
|
|
|
|
|
#endif |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
#include |
|
45
|
|
|
|
|
|
|
#include |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
#include "sha256.c.inc" |
|
48
|
|
|
|
|
|
|
#include "sysendian.h" |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
#include "crypto_scrypt.h" |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
static void blkcpy(void *, void *, size_t); |
|
53
|
|
|
|
|
|
|
static void blkxor(void *, void *, size_t); |
|
54
|
|
|
|
|
|
|
static void salsa20_8(uint32_t[16]); |
|
55
|
|
|
|
|
|
|
static void blockmix_salsa8(uint32_t *, uint32_t *, uint32_t *, size_t); |
|
56
|
|
|
|
|
|
|
static uint64_t integerify(void *, size_t); |
|
57
|
|
|
|
|
|
|
static void smix(uint8_t *, size_t, uint64_t, uint32_t *, uint32_t *); |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
static void |
|
60
|
|
|
|
|
|
|
blkcpy(void * dest, void * src, size_t len) |
|
61
|
|
|
|
|
|
|
{ |
|
62
|
|
|
|
|
|
|
size_t * D = dest; |
|
63
|
|
|
|
|
|
|
size_t * S = src; |
|
64
|
106532
|
|
|
|
|
|
size_t L = len / sizeof(size_t); |
|
65
|
|
|
|
|
|
|
size_t i; |
|
66
|
|
|
|
|
|
|
|
|
67
|
154000920
|
100
|
|
|
|
|
for (i = 0; i < L; i++) |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
68
|
139729664
|
|
|
|
|
|
D[i] = S[i]; |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
static void |
|
72
|
213064
|
|
|
|
|
|
blkxor(void * dest, void * src, size_t len) |
|
73
|
|
|
|
|
|
|
{ |
|
74
|
|
|
|
|
|
|
size_t * D = dest; |
|
75
|
|
|
|
|
|
|
size_t * S = src; |
|
76
|
213064
|
|
|
|
|
|
size_t L = len / sizeof(size_t); |
|
77
|
|
|
|
|
|
|
size_t i; |
|
78
|
|
|
|
|
|
|
|
|
79
|
88821480
|
100
|
|
|
|
|
for (i = 0; i < L; i++) |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
80
|
81792384
|
|
|
|
|
|
D[i] ^= S[i]; |
|
81
|
213064
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
/** |
|
84
|
|
|
|
|
|
|
* salsa20_8(B): |
|
85
|
|
|
|
|
|
|
* Apply the salsa20/8 core to the provided block. |
|
86
|
|
|
|
|
|
|
*/ |
|
87
|
|
|
|
|
|
|
static void |
|
88
|
6816032
|
|
|
|
|
|
salsa20_8(uint32_t B[16]) |
|
89
|
|
|
|
|
|
|
{ |
|
90
|
|
|
|
|
|
|
uint32_t x[16]; |
|
91
|
|
|
|
|
|
|
size_t i; |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
blkcpy(x, B, 64); |
|
94
|
34080160
|
100
|
|
|
|
|
for (i = 0; i < 8; i += 2) { |
|
95
|
|
|
|
|
|
|
#define R(a,b) (((a) << (b)) | ((a) >> (32 - (b)))) |
|
96
|
|
|
|
|
|
|
/* Operate on columns. */ |
|
97
|
27264128
|
|
|
|
|
|
x[ 4] ^= R(x[ 0]+x[12], 7); x[ 8] ^= R(x[ 4]+x[ 0], 9); |
|
98
|
27264128
|
|
|
|
|
|
x[12] ^= R(x[ 8]+x[ 4],13); x[ 0] ^= R(x[12]+x[ 8],18); |
|
99
|
|
|
|
|
|
|
|
|
100
|
27264128
|
|
|
|
|
|
x[ 9] ^= R(x[ 5]+x[ 1], 7); x[13] ^= R(x[ 9]+x[ 5], 9); |
|
101
|
27264128
|
|
|
|
|
|
x[ 1] ^= R(x[13]+x[ 9],13); x[ 5] ^= R(x[ 1]+x[13],18); |
|
102
|
|
|
|
|
|
|
|
|
103
|
27264128
|
|
|
|
|
|
x[14] ^= R(x[10]+x[ 6], 7); x[ 2] ^= R(x[14]+x[10], 9); |
|
104
|
27264128
|
|
|
|
|
|
x[ 6] ^= R(x[ 2]+x[14],13); x[10] ^= R(x[ 6]+x[ 2],18); |
|
105
|
|
|
|
|
|
|
|
|
106
|
27264128
|
|
|
|
|
|
x[ 3] ^= R(x[15]+x[11], 7); x[ 7] ^= R(x[ 3]+x[15], 9); |
|
107
|
27264128
|
|
|
|
|
|
x[11] ^= R(x[ 7]+x[ 3],13); x[15] ^= R(x[11]+x[ 7],18); |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
/* Operate on rows. */ |
|
110
|
27264128
|
|
|
|
|
|
x[ 1] ^= R(x[ 0]+x[ 3], 7); x[ 2] ^= R(x[ 1]+x[ 0], 9); |
|
111
|
27264128
|
|
|
|
|
|
x[ 3] ^= R(x[ 2]+x[ 1],13); x[ 0] ^= R(x[ 3]+x[ 2],18); |
|
112
|
|
|
|
|
|
|
|
|
113
|
27264128
|
|
|
|
|
|
x[ 6] ^= R(x[ 5]+x[ 4], 7); x[ 7] ^= R(x[ 6]+x[ 5], 9); |
|
114
|
27264128
|
|
|
|
|
|
x[ 4] ^= R(x[ 7]+x[ 6],13); x[ 5] ^= R(x[ 4]+x[ 7],18); |
|
115
|
|
|
|
|
|
|
|
|
116
|
27264128
|
|
|
|
|
|
x[11] ^= R(x[10]+x[ 9], 7); x[ 8] ^= R(x[11]+x[10], 9); |
|
117
|
27264128
|
|
|
|
|
|
x[ 9] ^= R(x[ 8]+x[11],13); x[10] ^= R(x[ 9]+x[ 8],18); |
|
118
|
|
|
|
|
|
|
|
|
119
|
27264128
|
|
|
|
|
|
x[12] ^= R(x[15]+x[14], 7); x[13] ^= R(x[12]+x[15], 9); |
|
120
|
27264128
|
|
|
|
|
|
x[14] ^= R(x[13]+x[12],13); x[15] ^= R(x[14]+x[13],18); |
|
121
|
|
|
|
|
|
|
#undef R |
|
122
|
|
|
|
|
|
|
} |
|
123
|
115872544
|
100
|
|
|
|
|
for (i = 0; i < 16; i++) |
|
124
|
109056512
|
|
|
|
|
|
B[i] += x[i]; |
|
125
|
6816032
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
/** |
|
128
|
|
|
|
|
|
|
* blockmix_salsa8(Bin, Bout, X, r): |
|
129
|
|
|
|
|
|
|
* Compute Bout = BlockMix_{salsa20/8, r}(Bin). The input Bin must be 128r |
|
130
|
|
|
|
|
|
|
* bytes in length; the output Bout must also be the same size. The |
|
131
|
|
|
|
|
|
|
* temporary space X must be 64 bytes. |
|
132
|
|
|
|
|
|
|
*/ |
|
133
|
|
|
|
|
|
|
static void |
|
134
|
426128
|
|
|
|
|
|
blockmix_salsa8(uint32_t * Bin, uint32_t * Bout, uint32_t * X, size_t r) |
|
135
|
|
|
|
|
|
|
{ |
|
136
|
|
|
|
|
|
|
size_t i; |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
/* 1: X <-- B_{2r - 1} */ |
|
139
|
426128
|
|
|
|
|
|
blkcpy(X, &Bin[(2 * r - 1) * 16], 64); |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
/* 2: for i = 0 to 2r - 1 do */ |
|
142
|
3834144
|
100
|
|
|
|
|
for (i = 0; i < 2 * r; i += 2) { |
|
143
|
|
|
|
|
|
|
/* 3: X <-- H(X \xor B_i) */ |
|
144
|
3408016
|
|
|
|
|
|
blkxor(X, &Bin[i * 16], 64); |
|
145
|
3408016
|
|
|
|
|
|
salsa20_8(X); |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
/* 4: Y_i <-- X */ |
|
148
|
|
|
|
|
|
|
/* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */ |
|
149
|
3408016
|
|
|
|
|
|
blkcpy(&Bout[i * 8], X, 64); |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
/* 3: X <-- H(X \xor B_i) */ |
|
152
|
3408016
|
|
|
|
|
|
blkxor(X, &Bin[i * 16 + 16], 64); |
|
153
|
3408016
|
|
|
|
|
|
salsa20_8(X); |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
/* 4: Y_i <-- X */ |
|
156
|
|
|
|
|
|
|
/* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */ |
|
157
|
3408016
|
|
|
|
|
|
blkcpy(&Bout[i * 8 + r * 16], X, 64); |
|
158
|
|
|
|
|
|
|
} |
|
159
|
426128
|
|
|
|
|
|
} |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
/** |
|
162
|
|
|
|
|
|
|
* integerify(B, r): |
|
163
|
|
|
|
|
|
|
* Return the result of parsing B_{2r-1} as a little-endian integer. |
|
164
|
|
|
|
|
|
|
*/ |
|
165
|
|
|
|
|
|
|
static uint64_t |
|
166
|
|
|
|
|
|
|
integerify(void * B, size_t r) |
|
167
|
|
|
|
|
|
|
{ |
|
168
|
213064
|
|
|
|
|
|
uint32_t * X = (void *)((uintptr_t)(B) + (2 * r - 1) * 64); |
|
169
|
|
|
|
|
|
|
|
|
170
|
213064
|
|
|
|
|
|
return (((uint64_t)(X[1]) << 32) + X[0]); |
|
171
|
|
|
|
|
|
|
} |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
/** |
|
174
|
|
|
|
|
|
|
* smix(B, r, N, V, XY): |
|
175
|
|
|
|
|
|
|
* Compute B = SMix_r(B, N). The input B must be 128r bytes in length; |
|
176
|
|
|
|
|
|
|
* the temporary storage V must be 128rN bytes in length; the temporary |
|
177
|
|
|
|
|
|
|
* storage XY must be 256r + 64 bytes in length. The value N must be a |
|
178
|
|
|
|
|
|
|
* power of 2 greater than 1. The arrays B, V, and XY must be aligned to a |
|
179
|
|
|
|
|
|
|
* multiple of 64 bytes. |
|
180
|
|
|
|
|
|
|
*/ |
|
181
|
|
|
|
|
|
|
static void |
|
182
|
35
|
|
|
|
|
|
smix(uint8_t * B, size_t r, uint64_t N, uint32_t * V, uint32_t * XY) |
|
183
|
|
|
|
|
|
|
{ |
|
184
|
|
|
|
|
|
|
uint32_t * X = XY; |
|
185
|
35
|
|
|
|
|
|
uint32_t * Y = &XY[32 * r]; |
|
186
|
35
|
|
|
|
|
|
uint32_t * Z = &XY[64 * r]; |
|
187
|
|
|
|
|
|
|
uint64_t i; |
|
188
|
|
|
|
|
|
|
uint64_t j; |
|
189
|
|
|
|
|
|
|
size_t k; |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
/* 1: X <-- B */ |
|
192
|
7427
|
100
|
|
|
|
|
for (k = 0; k < 32 * r; k++) |
|
193
|
7392
|
|
|
|
|
|
X[k] = le32dec(&B[4 * k]); |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
/* 2: for i = 0 to N - 1 do */ |
|
196
|
106567
|
100
|
|
|
|
|
for (i = 0; i < N; i += 2) { |
|
197
|
|
|
|
|
|
|
/* 3: V_i <-- X */ |
|
198
|
106532
|
|
|
|
|
|
blkcpy(&V[i * (32 * r)], X, 128 * r); |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
/* 4: X <-- H(X) */ |
|
201
|
106532
|
|
|
|
|
|
blockmix_salsa8(X, Y, Z, r); |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
/* 3: V_i <-- X */ |
|
204
|
106532
|
|
|
|
|
|
blkcpy(&V[(i + 1) * (32 * r)], Y, 128 * r); |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
/* 4: X <-- H(X) */ |
|
207
|
106532
|
|
|
|
|
|
blockmix_salsa8(Y, X, Z, r); |
|
208
|
|
|
|
|
|
|
} |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
/* 6: for i = 0 to N - 1 do */ |
|
211
|
106567
|
100
|
|
|
|
|
for (i = 0; i < N; i += 2) { |
|
212
|
|
|
|
|
|
|
/* 7: j <-- Integerify(X) mod N */ |
|
213
|
106532
|
|
|
|
|
|
j = integerify(X, r) & (N - 1); |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
/* 8: X <-- H(X \xor V_j) */ |
|
216
|
106532
|
|
|
|
|
|
blkxor(X, &V[j * (32 * r)], 128 * r); |
|
217
|
106532
|
|
|
|
|
|
blockmix_salsa8(X, Y, Z, r); |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
/* 7: j <-- Integerify(X) mod N */ |
|
220
|
106532
|
|
|
|
|
|
j = integerify(Y, r) & (N - 1); |
|
221
|
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
/* 8: X <-- H(X \xor V_j) */ |
|
223
|
106532
|
|
|
|
|
|
blkxor(Y, &V[j * (32 * r)], 128 * r); |
|
224
|
106532
|
|
|
|
|
|
blockmix_salsa8(Y, X, Z, r); |
|
225
|
|
|
|
|
|
|
} |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
/* 10: B' <-- X */ |
|
228
|
7427
|
100
|
|
|
|
|
for (k = 0; k < 32 * r; k++) |
|
229
|
7392
|
|
|
|
|
|
le32enc(&B[4 * k], X[k]); |
|
230
|
35
|
|
|
|
|
|
} |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
/** |
|
233
|
|
|
|
|
|
|
* crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen): |
|
234
|
|
|
|
|
|
|
* Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r, |
|
235
|
|
|
|
|
|
|
* p, buflen) and write the result into buf. The parameters r, p, and buflen |
|
236
|
|
|
|
|
|
|
* must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N |
|
237
|
|
|
|
|
|
|
* must be a power of 2 greater than 1. |
|
238
|
|
|
|
|
|
|
* |
|
239
|
|
|
|
|
|
|
* Return 0 on success; or -1 on error. |
|
240
|
|
|
|
|
|
|
*/ |
|
241
|
|
|
|
|
|
|
int |
|
242
|
20
|
|
|
|
|
|
crypto_scrypt(const uint8_t * passwd, size_t passwdlen, |
|
243
|
|
|
|
|
|
|
const uint8_t * salt, size_t saltlen, uint64_t N, uint32_t r, uint32_t p, |
|
244
|
|
|
|
|
|
|
uint8_t * buf, size_t buflen) |
|
245
|
|
|
|
|
|
|
{ |
|
246
|
|
|
|
|
|
|
void * B0, * V0, * XY0; |
|
247
|
|
|
|
|
|
|
uint8_t * B; |
|
248
|
|
|
|
|
|
|
uint32_t * V; |
|
249
|
|
|
|
|
|
|
uint32_t * XY; |
|
250
|
|
|
|
|
|
|
uint32_t i; |
|
251
|
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
/* Sanity-check parameters. */ |
|
253
|
|
|
|
|
|
|
#if SIZE_MAX > UINT32_MAX |
|
254
|
20
|
50
|
|
|
|
|
if (buflen > (((uint64_t)(1) << 32) - 1) * 32) { |
|
255
|
0
|
|
|
|
|
|
errno = EFBIG; |
|
256
|
0
|
|
|
|
|
|
goto err0; |
|
257
|
|
|
|
|
|
|
} |
|
258
|
|
|
|
|
|
|
#endif |
|
259
|
20
|
50
|
|
|
|
|
if ((uint64_t)(r) * (uint64_t)(p) >= (1 << 30)) { |
|
260
|
0
|
|
|
|
|
|
errno = EFBIG; |
|
261
|
0
|
|
|
|
|
|
goto err0; |
|
262
|
|
|
|
|
|
|
} |
|
263
|
20
|
50
|
|
|
|
|
if (((N & (N - 1)) != 0) || (N == 0)) { |
|
|
|
50
|
|
|
|
|
|
|
264
|
0
|
|
|
|
|
|
errno = EINVAL; |
|
265
|
0
|
|
|
|
|
|
goto err0; |
|
266
|
|
|
|
|
|
|
} |
|
267
|
20
|
50
|
|
|
|
|
if ((r > SIZE_MAX / 128 / p) || |
|
268
|
|
|
|
|
|
|
#if SIZE_MAX / 256 <= UINT32_MAX |
|
269
|
|
|
|
|
|
|
(r > SIZE_MAX / 256) || |
|
270
|
|
|
|
|
|
|
#endif |
|
271
|
20
|
50
|
|
|
|
|
(N > SIZE_MAX / 128 / r)) { |
|
272
|
0
|
|
|
|
|
|
errno = ENOMEM; |
|
273
|
0
|
|
|
|
|
|
goto err0; |
|
274
|
|
|
|
|
|
|
} |
|
275
|
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
/* Allocate memory. */ |
|
277
|
|
|
|
|
|
|
#ifdef HAVE_POSIX_MEMALIGN |
|
278
|
|
|
|
|
|
|
if ((errno = posix_memalign(&B0, 64, 128 * r * p)) != 0) |
|
279
|
|
|
|
|
|
|
goto err0; |
|
280
|
|
|
|
|
|
|
B = (uint8_t *)(B0); |
|
281
|
|
|
|
|
|
|
if ((errno = posix_memalign(&XY0, 64, 256 * r + 64)) != 0) |
|
282
|
|
|
|
|
|
|
goto err1; |
|
283
|
|
|
|
|
|
|
XY = (uint32_t *)(XY0); |
|
284
|
|
|
|
|
|
|
#ifndef MAP_ANON |
|
285
|
|
|
|
|
|
|
if ((errno = posix_memalign(&V0, 64, 128 * r * N)) != 0) |
|
286
|
|
|
|
|
|
|
goto err2; |
|
287
|
|
|
|
|
|
|
V = (uint32_t *)(V0); |
|
288
|
|
|
|
|
|
|
#endif |
|
289
|
|
|
|
|
|
|
#else |
|
290
|
20
|
50
|
|
|
|
|
if ((B0 = malloc(128 * r * p + 63)) == NULL) |
|
291
|
0
|
|
|
|
|
|
goto err0; |
|
292
|
20
|
|
|
|
|
|
B = (uint8_t *)(((uintptr_t)(B0) + 63) & ~ (uintptr_t)(63)); |
|
293
|
20
|
50
|
|
|
|
|
if ((XY0 = malloc(256 * r + 64 + 63)) == NULL) |
|
294
|
0
|
|
|
|
|
|
goto err1; |
|
295
|
20
|
|
|
|
|
|
XY = (uint32_t *)(((uintptr_t)(XY0) + 63) & ~ (uintptr_t)(63)); |
|
296
|
|
|
|
|
|
|
#ifndef MAP_ANON |
|
297
|
20
|
50
|
|
|
|
|
if ((V0 = malloc(128 * r * N + 63)) == NULL) |
|
298
|
0
|
|
|
|
|
|
goto err2; |
|
299
|
20
|
|
|
|
|
|
V = (uint32_t *)(((uintptr_t)(V0) + 63) & ~ (uintptr_t)(63)); |
|
300
|
|
|
|
|
|
|
#endif |
|
301
|
|
|
|
|
|
|
#endif |
|
302
|
|
|
|
|
|
|
#ifdef MAP_ANON |
|
303
|
|
|
|
|
|
|
if ((V0 = mmap(NULL, 128 * r * N, PROT_READ | PROT_WRITE, |
|
304
|
|
|
|
|
|
|
#ifdef MAP_NOCORE |
|
305
|
|
|
|
|
|
|
MAP_ANON | MAP_PRIVATE | MAP_NOCORE, |
|
306
|
|
|
|
|
|
|
#else |
|
307
|
|
|
|
|
|
|
MAP_ANON | MAP_PRIVATE, |
|
308
|
|
|
|
|
|
|
#endif |
|
309
|
|
|
|
|
|
|
-1, 0)) == MAP_FAILED) |
|
310
|
|
|
|
|
|
|
goto err2; |
|
311
|
|
|
|
|
|
|
V = (uint32_t *)(V0); |
|
312
|
|
|
|
|
|
|
#endif |
|
313
|
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
/* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */ |
|
315
|
20
|
|
|
|
|
|
PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, B, p * 128 * r); |
|
316
|
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
/* 2: for i = 0 to p - 1 do */ |
|
318
|
55
|
100
|
|
|
|
|
for (i = 0; i < p; i++) { |
|
319
|
|
|
|
|
|
|
/* 3: B_i <-- MF(B_i, N) */ |
|
320
|
35
|
|
|
|
|
|
smix(&B[i * 128 * r], r, N, V, XY); |
|
321
|
|
|
|
|
|
|
} |
|
322
|
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
/* 5: DK <-- PBKDF2(P, B, 1, dkLen) */ |
|
324
|
20
|
|
|
|
|
|
PBKDF2_SHA256(passwd, passwdlen, B, p * 128 * r, 1, buf, buflen); |
|
325
|
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
/* Free memory. */ |
|
327
|
|
|
|
|
|
|
#ifdef MAP_ANON |
|
328
|
|
|
|
|
|
|
if (munmap(V0, 128 * r * N)) |
|
329
|
|
|
|
|
|
|
goto err2; |
|
330
|
|
|
|
|
|
|
#else |
|
331
|
20
|
|
|
|
|
|
free(V0); |
|
332
|
|
|
|
|
|
|
#endif |
|
333
|
20
|
|
|
|
|
|
free(XY0); |
|
334
|
20
|
|
|
|
|
|
free(B0); |
|
335
|
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
/* Success! */ |
|
337
|
20
|
|
|
|
|
|
return (0); |
|
338
|
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
err2: |
|
340
|
0
|
|
|
|
|
|
free(XY0); |
|
341
|
0
|
|
|
|
|
|
err1: |
|
342
|
0
|
|
|
|
|
|
free(B0); |
|
343
|
|
|
|
|
|
|
err0: |
|
344
|
|
|
|
|
|
|
/* Failure! */ |
|
345
|
|
|
|
|
|
|
return (-1); |
|
346
|
|
|
|
|
|
|
} |