forked from zendframework/zendframework
/
Scrypt.php
340 lines (323 loc) · 11.8 KB
/
Scrypt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Crypt\Key\Derivation;
/**
* Scrypt key derivation function
*
* @see http://www.tarsnap.com/scrypt.html
* @see https://tools.ietf.org/html/draft-josefsson-scrypt-kdf-01
*/
abstract class Scrypt
{
/**
* Execute the scrypt algorithm
*
* @param string $password
* @param string $salt
* @param int $n CPU cost
* @param int $r Memory cost
* @param int $p parallelization cost
* @param int $length size of the output key
* @return string
*/
public static function calc($password, $salt, $n, $r, $p, $length)
{
if ($n == 0 || ($n & ($n - 1)) != 0) {
throw new Exception\InvalidArgumentException("N must be > 0 and a power of 2");
}
if ($n > PHP_INT_MAX / 128 / $r) {
throw new Exception\InvalidArgumentException("Parameter n is too large");
}
if ($r > PHP_INT_MAX / 128 / $p) {
throw new Exception\InvalidArgumentException("Parameter r is too large");
}
if (extension_loaded('Scrypt')) {
if ($length < 16) {
throw new Exception\InvalidArgumentException("Key length is too low, must be greater or equal to 16");
}
return self::hex2bin(scrypt($password, $salt, $n, $r, $p, $length));
}
$b = Pbkdf2::calc('sha256', $password, $salt, 1, $p * 128 * $r);
$s = '';
for ($i = 0; $i < $p; $i++) {
$s .= self::scryptROMix(substr($b, $i * 128 * $r, 128 * $r), $n, $r);
}
return Pbkdf2::calc('sha256', $password, $s, 1, $length);
}
/**
* scryptROMix
*
* @param string $b
* @param int $n
* @param int $r
* @return string
* @see https://tools.ietf.org/html/draft-josefsson-scrypt-kdf-01#section-4
*/
protected static function scryptROMix($b, $n, $r)
{
$x = $b;
$v = array();
for ($i = 0; $i < $n; $i++) {
$v[$i] = $x;
$x = self::scryptBlockMix($x, $r);
}
for ($i = 0; $i < $n; $i++) {
$j = self::integerify($x) % $n;
$t = $x ^ $v[$j];
$x = self::scryptBlockMix($t, $r);
}
return $x;
}
/**
* scryptBlockMix
*
* @param string $b
* @param int $r
* @return string
* @see https://tools.ietf.org/html/draft-josefsson-scrypt-kdf-01#section-3
*/
protected static function scryptBlockMix($b, $r)
{
$x = substr($b, -64);
$even = '';
$odd = '';
$len = 2 * $r;
for ($i = 0; $i < $len; $i++) {
if (PHP_INT_SIZE === 4) {
$x = self::salsa208Core32($x ^ substr($b, 64 * $i, 64));
} else {
$x = self::salsa208Core64($x ^ substr($b, 64 * $i, 64));
}
if ($i % 2 == 0) {
$even .= $x;
} else {
$odd .= $x;
}
}
return $even . $odd;
}
/**
* Salsa 20/8 core (32 bit version)
*
* @param string $b
* @return string
* @see https://tools.ietf.org/html/draft-josefsson-scrypt-kdf-01#section-2
* @see http://cr.yp.to/salsa20.html
*/
protected static function salsa208Core32($b)
{
$b32 = array();
for ($i = 0; $i < 16; $i++) {
list(, $b32[$i]) = unpack("V", substr($b, $i * 4, 4));
}
$x = $b32;
for ($i = 0; $i < 8; $i += 2) {
$a = ($x[ 0] + $x[12]);
$x[ 4] ^= ($a << 7) | ($a >> 25) & 0x7f;
$a = ($x[ 4] + $x[ 0]);
$x[ 8] ^= ($a << 9) | ($a >> 23) & 0x1ff;
$a = ($x[ 8] + $x[ 4]);
$x[12] ^= ($a << 13) | ($a >> 19) & 0x1fff;
$a = ($x[12] + $x[ 8]);
$x[ 0] ^= ($a << 18) | ($a >> 14) & 0x3ffff;
$a = ($x[ 5] + $x[ 1]);
$x[ 9] ^= ($a << 7) | ($a >> 25) & 0x7f;
$a = ($x[ 9] + $x[ 5]);
$x[13] ^= ($a << 9) | ($a >> 23) & 0x1ff;
$a = ($x[13] + $x[ 9]);
$x[ 1] ^= ($a << 13) | ($a >> 19) & 0x1fff;
$a = ($x[ 1] + $x[13]);
$x[ 5] ^= ($a << 18) | ($a >> 14) & 0x3ffff;
$a = ($x[10] + $x[ 6]);
$x[14] ^= ($a << 7) | ($a >> 25) & 0x7f;
$a = ($x[14] + $x[10]);
$x[ 2] ^= ($a << 9) | ($a >> 23) & 0x1ff;
$a = ($x[ 2] + $x[14]);
$x[ 6] ^= ($a << 13) | ($a >> 19) & 0x1fff;
$a = ($x[ 6] + $x[ 2]);
$x[10] ^= ($a << 18) | ($a >> 14) & 0x3ffff;
$a = ($x[15] + $x[11]);
$x[ 3] ^= ($a << 7) | ($a >> 25) & 0x7f;
$a = ($x[ 3] + $x[15]);
$x[ 7] ^= ($a << 9) | ($a >> 23) & 0x1ff;
$a = ($x[ 7] + $x[ 3]);
$x[11] ^= ($a << 13) | ($a >> 19) & 0x1fff;
$a = ($x[11] + $x[ 7]);
$x[15] ^= ($a << 18) | ($a >> 14) & 0x3ffff;
$a = ($x[ 0] + $x[ 3]);
$x[ 1] ^= ($a << 7) | ($a >> 25) & 0x7f;
$a = ($x[ 1] + $x[ 0]);
$x[ 2] ^= ($a << 9) | ($a >> 23) & 0x1ff;
$a = ($x[ 2] + $x[ 1]);
$x[ 3] ^= ($a << 13) | ($a >> 19) & 0x1fff;
$a = ($x[ 3] + $x[ 2]);
$x[ 0] ^= ($a << 18) | ($a >> 14) & 0x3ffff;
$a = ($x[ 5] + $x[ 4]);
$x[ 6] ^= ($a << 7) | ($a >> 25) & 0x7f;
$a = ($x[ 6] + $x[ 5]);
$x[ 7] ^= ($a << 9) | ($a >> 23) & 0x1ff;
$a = ($x[ 7] + $x[ 6]);
$x[ 4] ^= ($a << 13) | ($a >> 19) & 0x1fff;
$a = ($x[ 4] + $x[ 7]);
$x[ 5] ^= ($a << 18) | ($a >> 14) & 0x3ffff;
$a = ($x[10] + $x[ 9]);
$x[11] ^= ($a << 7) | ($a >> 25) & 0x7f;
$a = ($x[11] + $x[10]);
$x[ 8] ^= ($a << 9) | ($a >> 23) & 0x1ff;
$a = ($x[ 8] + $x[11]);
$x[ 9] ^= ($a << 13) | ($a >> 19) & 0x1fff;
$a = ($x[ 9] + $x[ 8]);
$x[10] ^= ($a << 18) | ($a >> 14) & 0x3ffff;
$a = ($x[15] + $x[14]);
$x[12] ^= ($a << 7) | ($a >> 25) & 0x7f;
$a = ($x[12] + $x[15]);
$x[13] ^= ($a << 9) | ($a >> 23) & 0x1ff;
$a = ($x[13] + $x[12]);
$x[14] ^= ($a << 13) | ($a >> 19) & 0x1fff;
$a = ($x[14] + $x[13]);
$x[15] ^= ($a << 18) | ($a >> 14) & 0x3ffff;
}
for ($i = 0; $i < 16; $i++) {
$b32[$i] = $b32[$i] + $x[$i];
}
$result = '';
for ($i = 0; $i < 16; $i++) {
$result .= pack("V", $b32[$i]);
}
return $result;
}
/**
* Salsa 20/8 core (64 bit version)
*
* @param string $b
* @return string
* @see https://tools.ietf.org/html/draft-josefsson-scrypt-kdf-01#section-2
* @see http://cr.yp.to/salsa20.html
*/
protected static function salsa208Core64($b)
{
$b32 = array();
for ($i = 0; $i < 16; $i++) {
list(, $b32[$i]) = unpack("V", substr($b, $i * 4, 4));
}
$x = $b32;
for ($i = 0; $i < 8; $i += 2) {
$a = ($x[ 0] + $x[12]) & 0xffffffff;
$x[ 4] ^= ($a << 7) | ($a >> 25);
$a = ($x[ 4] + $x[ 0]) & 0xffffffff;
$x[ 8] ^= ($a << 9) | ($a >> 23);
$a = ($x[ 8] + $x[ 4]) & 0xffffffff;
$x[12] ^= ($a << 13) | ($a >> 19);
$a = ($x[12] + $x[ 8]) & 0xffffffff;
$x[ 0] ^= ($a << 18) | ($a >> 14);
$a = ($x[ 5] + $x[ 1]) & 0xffffffff;
$x[ 9] ^= ($a << 7) | ($a >> 25);
$a = ($x[ 9] + $x[ 5]) & 0xffffffff;
$x[13] ^= ($a << 9) | ($a >> 23);
$a = ($x[13] + $x[ 9]) & 0xffffffff;
$x[ 1] ^= ($a << 13) | ($a >> 19);
$a = ($x[ 1] + $x[13]) & 0xffffffff;
$x[ 5] ^= ($a << 18) | ($a >> 14);
$a = ($x[10] + $x[ 6]) & 0xffffffff;
$x[14] ^= ($a << 7) | ($a >> 25);
$a = ($x[14] + $x[10]) & 0xffffffff;
$x[ 2] ^= ($a << 9) | ($a >> 23);
$a = ($x[ 2] + $x[14]) & 0xffffffff;
$x[ 6] ^= ($a << 13) | ($a >> 19);
$a = ($x[ 6] + $x[ 2]) & 0xffffffff;
$x[10] ^= ($a << 18) | ($a >> 14);
$a = ($x[15] + $x[11]) & 0xffffffff;
$x[ 3] ^= ($a << 7) | ($a >> 25);
$a = ($x[ 3] + $x[15]) & 0xffffffff;
$x[ 7] ^= ($a << 9) | ($a >> 23);
$a = ($x[ 7] + $x[ 3]) & 0xffffffff;
$x[11] ^= ($a << 13) | ($a >> 19);
$a = ($x[11] + $x[ 7]) & 0xffffffff;
$x[15] ^= ($a << 18) | ($a >> 14);
$a = ($x[ 0] + $x[ 3]) & 0xffffffff;
$x[ 1] ^= ($a << 7) | ($a >> 25);
$a = ($x[ 1] + $x[ 0]) & 0xffffffff;
$x[ 2] ^= ($a << 9) | ($a >> 23);
$a = ($x[ 2] + $x[ 1]) & 0xffffffff;
$x[ 3] ^= ($a << 13) | ($a >> 19);
$a = ($x[ 3] + $x[ 2]) & 0xffffffff;
$x[ 0] ^= ($a << 18) | ($a >> 14);
$a = ($x[ 5] + $x[ 4]) & 0xffffffff;
$x[ 6] ^= ($a << 7) | ($a >> 25);
$a = ($x[ 6] + $x[ 5]) & 0xffffffff;
$x[ 7] ^= ($a << 9) | ($a >> 23);
$a = ($x[ 7] + $x[ 6]) & 0xffffffff;
$x[ 4] ^= ($a << 13) | ($a >> 19);
$a = ($x[ 4] + $x[ 7]) & 0xffffffff;
$x[ 5] ^= ($a << 18) | ($a >> 14);
$a = ($x[10] + $x[ 9]) & 0xffffffff;
$x[11] ^= ($a << 7) | ($a >> 25);
$a = ($x[11] + $x[10]) & 0xffffffff;
$x[ 8] ^= ($a << 9) | ($a >> 23);
$a = ($x[ 8] + $x[11]) & 0xffffffff;
$x[ 9] ^= ($a << 13) | ($a >> 19);
$a = ($x[ 9] + $x[ 8]) & 0xffffffff;
$x[10] ^= ($a << 18) | ($a >> 14);
$a = ($x[15] + $x[14]) & 0xffffffff;
$x[12] ^= ($a << 7) | ($a >> 25);
$a = ($x[12] + $x[15]) & 0xffffffff;
$x[13] ^= ($a << 9) | ($a >> 23);
$a = ($x[13] + $x[12]) & 0xffffffff;
$x[14] ^= ($a << 13) | ($a >> 19);
$a = ($x[14] + $x[13]) & 0xffffffff;
$x[15] ^= ($a << 18) | ($a >> 14);
}
for ($i = 0; $i < 16; $i++) {
$b32[$i] = ($b32[$i] + $x[$i]) & 0xffffffff;
}
$result = '';
for ($i = 0; $i < 16; $i++) {
$result .= pack("V", $b32[$i]);
}
return $result;
}
/**
* Integerify
*
* Integerify (B[0] ... B[2 * r - 1]) is defined as the result
* of interpreting B[2 * r - 1] as a little-endian integer.
* Each block B is a string of 64 bytes.
*
* @param string $b
* @return int
* @see https://tools.ietf.org/html/draft-josefsson-scrypt-kdf-01#section-4
*/
protected static function integerify($b)
{
$v = 'v';
if (PHP_INT_SIZE === 8) {
$v = 'V';
}
list(, $n) = unpack($v, substr($b, -64));
return $n;
}
/**
* Convert hex string in a binary string
*
* @param string $hex
* @return string
*/
protected static function hex2bin($hex)
{
if (PHP_VERSION_ID >= 50400) {
return hex2bin($hex);
}
$len = strlen($hex);
$result = '';
for ($i = 0; $i < $len; $i+=2) {
$result .= chr(hexdec($hex[$i] . $hex[$i+1]));
}
return $result;
}
}