@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core_BLAKE2b', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -11,709 +11,709 @@ discard block |
||
11 | 11 | */ |
12 | 12 | abstract class ParagonIE_Sodium_Core32_BLAKE2b extends ParagonIE_Sodium_Core_Util |
13 | 13 | { |
14 | - /** |
|
15 | - * @var SplFixedArray |
|
16 | - */ |
|
17 | - public static $iv; |
|
18 | - |
|
19 | - /** |
|
20 | - * @var array<int, array<int, int>> |
|
21 | - */ |
|
22 | - public static $sigma = array( |
|
23 | - array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
24 | - array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3), |
|
25 | - array( 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4), |
|
26 | - array( 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8), |
|
27 | - array( 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13), |
|
28 | - array( 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9), |
|
29 | - array( 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11), |
|
30 | - array( 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10), |
|
31 | - array( 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5), |
|
32 | - array( 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0), |
|
33 | - array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
34 | - array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3) |
|
35 | - ); |
|
36 | - |
|
37 | - const BLOCKBYTES = 128; |
|
38 | - const OUTBYTES = 64; |
|
39 | - const KEYBYTES = 64; |
|
40 | - |
|
41 | - /** |
|
42 | - * Turn two 32-bit integers into a fixed array representing a 64-bit integer. |
|
43 | - * |
|
44 | - * @internal You should not use this directly from another application |
|
45 | - * |
|
46 | - * @param int $high |
|
47 | - * @param int $low |
|
48 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
49 | - * @throws SodiumException |
|
50 | - * @throws TypeError |
|
51 | - */ |
|
52 | - public static function new64($high, $low) |
|
53 | - { |
|
54 | - return ParagonIE_Sodium_Core32_Int64::fromInts($low, $high); |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * Convert an arbitrary number into an SplFixedArray of two 32-bit integers |
|
59 | - * that represents a 64-bit integer. |
|
60 | - * |
|
61 | - * @internal You should not use this directly from another application |
|
62 | - * |
|
63 | - * @param int $num |
|
64 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
65 | - * @throws SodiumException |
|
66 | - * @throws TypeError |
|
67 | - */ |
|
68 | - protected static function to64($num) |
|
69 | - { |
|
70 | - list($hi, $lo) = self::numericTo64BitInteger($num); |
|
71 | - return self::new64($hi, $lo); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Adds two 64-bit integers together, returning their sum as a SplFixedArray |
|
76 | - * containing two 32-bit integers (representing a 64-bit integer). |
|
77 | - * |
|
78 | - * @internal You should not use this directly from another application |
|
79 | - * |
|
80 | - * @param ParagonIE_Sodium_Core32_Int64 $x |
|
81 | - * @param ParagonIE_Sodium_Core32_Int64 $y |
|
82 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
83 | - */ |
|
84 | - protected static function add64($x, $y) |
|
85 | - { |
|
86 | - return $x->addInt64($y); |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @internal You should not use this directly from another application |
|
91 | - * |
|
92 | - * @param ParagonIE_Sodium_Core32_Int64 $x |
|
93 | - * @param ParagonIE_Sodium_Core32_Int64 $y |
|
94 | - * @param ParagonIE_Sodium_Core32_Int64 $z |
|
95 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
96 | - */ |
|
97 | - public static function add364($x, $y, $z) |
|
98 | - { |
|
99 | - return $x->addInt64($y)->addInt64($z); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @internal You should not use this directly from another application |
|
104 | - * |
|
105 | - * @param ParagonIE_Sodium_Core32_Int64 $x |
|
106 | - * @param ParagonIE_Sodium_Core32_Int64 $y |
|
107 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
108 | - * @throws TypeError |
|
109 | - */ |
|
110 | - public static function xor64(ParagonIE_Sodium_Core32_Int64 $x, ParagonIE_Sodium_Core32_Int64 $y) |
|
111 | - { |
|
112 | - return $x->xorInt64($y); |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * @internal You should not use this directly from another application |
|
117 | - * |
|
118 | - * @param ParagonIE_Sodium_Core32_Int64 $x |
|
119 | - * @param int $c |
|
120 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
121 | - * @throws SodiumException |
|
122 | - * @throws TypeError |
|
123 | - */ |
|
124 | - public static function rotr64(ParagonIE_Sodium_Core32_Int64 $x, $c) |
|
125 | - { |
|
126 | - return $x->rotateRight($c); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @internal You should not use this directly from another application |
|
131 | - * |
|
132 | - * @param SplFixedArray $x |
|
133 | - * @param int $i |
|
134 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
135 | - * @throws SodiumException |
|
136 | - * @throws TypeError |
|
137 | - */ |
|
138 | - public static function load64($x, $i) |
|
139 | - { |
|
140 | - /** @var int $l */ |
|
141 | - $l = (int) ($x[$i]) |
|
142 | - | ((int) ($x[$i+1]) << 8) |
|
143 | - | ((int) ($x[$i+2]) << 16) |
|
144 | - | ((int) ($x[$i+3]) << 24); |
|
145 | - /** @var int $h */ |
|
146 | - $h = (int) ($x[$i+4]) |
|
147 | - | ((int) ($x[$i+5]) << 8) |
|
148 | - | ((int) ($x[$i+6]) << 16) |
|
149 | - | ((int) ($x[$i+7]) << 24); |
|
150 | - return self::new64($h, $l); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * @internal You should not use this directly from another application |
|
155 | - * |
|
156 | - * @param SplFixedArray $x |
|
157 | - * @param int $i |
|
158 | - * @param ParagonIE_Sodium_Core32_Int64 $u |
|
159 | - * @return void |
|
160 | - * @throws TypeError |
|
161 | - * @psalm-suppress MixedArgument |
|
162 | - * @psalm-suppress MixedAssignment |
|
163 | - * @psalm-suppress MixedArrayAccess |
|
164 | - * @psalm-suppress MixedArrayAssignment |
|
165 | - * @psalm-suppress MixedArrayOffset |
|
166 | - */ |
|
167 | - public static function store64(SplFixedArray $x, $i, ParagonIE_Sodium_Core32_Int64 $u) |
|
168 | - { |
|
169 | - $v = clone $u; |
|
170 | - $maxLength = $x->getSize() - 1; |
|
171 | - for ($j = 0; $j < 8; ++$j) { |
|
172 | - $k = 3 - ($j >> 1); |
|
173 | - $x[$i] = $v->limbs[$k] & 0xff; |
|
174 | - if (++$i > $maxLength) { |
|
175 | - return; |
|
176 | - } |
|
177 | - $v->limbs[$k] >>= 8; |
|
178 | - } |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * This just sets the $iv static variable. |
|
183 | - * |
|
184 | - * @internal You should not use this directly from another application |
|
185 | - * |
|
186 | - * @return void |
|
187 | - * @throws SodiumException |
|
188 | - * @throws TypeError |
|
189 | - */ |
|
190 | - public static function pseudoConstructor() |
|
191 | - { |
|
192 | - static $called = false; |
|
193 | - if ($called) { |
|
194 | - return; |
|
195 | - } |
|
196 | - self::$iv = new SplFixedArray(8); |
|
197 | - self::$iv[0] = self::new64(0x6a09e667, 0xf3bcc908); |
|
198 | - self::$iv[1] = self::new64(0xbb67ae85, 0x84caa73b); |
|
199 | - self::$iv[2] = self::new64(0x3c6ef372, 0xfe94f82b); |
|
200 | - self::$iv[3] = self::new64(0xa54ff53a, 0x5f1d36f1); |
|
201 | - self::$iv[4] = self::new64(0x510e527f, 0xade682d1); |
|
202 | - self::$iv[5] = self::new64(0x9b05688c, 0x2b3e6c1f); |
|
203 | - self::$iv[6] = self::new64(0x1f83d9ab, 0xfb41bd6b); |
|
204 | - self::$iv[7] = self::new64(0x5be0cd19, 0x137e2179); |
|
205 | - |
|
206 | - $called = true; |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Returns a fresh BLAKE2 context. |
|
211 | - * |
|
212 | - * @internal You should not use this directly from another application |
|
213 | - * |
|
214 | - * @return SplFixedArray |
|
215 | - * @throws TypeError |
|
216 | - * @psalm-suppress MixedArgument |
|
217 | - * @psalm-suppress MixedAssignment |
|
218 | - * @psalm-suppress MixedArrayAccess |
|
219 | - * @psalm-suppress MixedArrayAssignment |
|
220 | - * @psalm-suppress MixedArrayOffset |
|
221 | - * @throws SodiumException |
|
222 | - * @throws TypeError |
|
223 | - */ |
|
224 | - protected static function context() |
|
225 | - { |
|
226 | - $ctx = new SplFixedArray(6); |
|
227 | - $ctx[0] = new SplFixedArray(8); // h |
|
228 | - $ctx[1] = new SplFixedArray(2); // t |
|
229 | - $ctx[2] = new SplFixedArray(2); // f |
|
230 | - $ctx[3] = new SplFixedArray(256); // buf |
|
231 | - $ctx[4] = 0; // buflen |
|
232 | - $ctx[5] = 0; // last_node (uint8_t) |
|
233 | - |
|
234 | - for ($i = 8; $i--;) { |
|
235 | - $ctx[0][$i] = self::$iv[$i]; |
|
236 | - } |
|
237 | - for ($i = 256; $i--;) { |
|
238 | - $ctx[3][$i] = 0; |
|
239 | - } |
|
240 | - |
|
241 | - $zero = self::new64(0, 0); |
|
242 | - $ctx[1][0] = $zero; |
|
243 | - $ctx[1][1] = $zero; |
|
244 | - $ctx[2][0] = $zero; |
|
245 | - $ctx[2][1] = $zero; |
|
246 | - |
|
247 | - return $ctx; |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * @internal You should not use this directly from another application |
|
252 | - * |
|
253 | - * @param SplFixedArray $ctx |
|
254 | - * @param SplFixedArray $buf |
|
255 | - * @return void |
|
256 | - * @throws SodiumException |
|
257 | - * @throws TypeError |
|
258 | - * @psalm-suppress MixedArgument |
|
259 | - * @psalm-suppress MixedArrayAccess |
|
260 | - * @psalm-suppress MixedArrayAssignment |
|
261 | - * @psalm-suppress MixedAssignment |
|
262 | - */ |
|
263 | - protected static function compress(SplFixedArray $ctx, SplFixedArray $buf) |
|
264 | - { |
|
265 | - $m = new SplFixedArray(16); |
|
266 | - $v = new SplFixedArray(16); |
|
267 | - |
|
268 | - for ($i = 16; $i--;) { |
|
269 | - $m[$i] = self::load64($buf, $i << 3); |
|
270 | - } |
|
271 | - |
|
272 | - for ($i = 8; $i--;) { |
|
273 | - $v[$i] = $ctx[0][$i]; |
|
274 | - } |
|
275 | - |
|
276 | - $v[ 8] = self::$iv[0]; |
|
277 | - $v[ 9] = self::$iv[1]; |
|
278 | - $v[10] = self::$iv[2]; |
|
279 | - $v[11] = self::$iv[3]; |
|
280 | - |
|
281 | - $v[12] = self::xor64($ctx[1][0], self::$iv[4]); |
|
282 | - $v[13] = self::xor64($ctx[1][1], self::$iv[5]); |
|
283 | - $v[14] = self::xor64($ctx[2][0], self::$iv[6]); |
|
284 | - $v[15] = self::xor64($ctx[2][1], self::$iv[7]); |
|
285 | - |
|
286 | - for ($r = 0; $r < 12; ++$r) { |
|
287 | - $v = self::G($r, 0, 0, 4, 8, 12, $v, $m); |
|
288 | - $v = self::G($r, 1, 1, 5, 9, 13, $v, $m); |
|
289 | - $v = self::G($r, 2, 2, 6, 10, 14, $v, $m); |
|
290 | - $v = self::G($r, 3, 3, 7, 11, 15, $v, $m); |
|
291 | - $v = self::G($r, 4, 0, 5, 10, 15, $v, $m); |
|
292 | - $v = self::G($r, 5, 1, 6, 11, 12, $v, $m); |
|
293 | - $v = self::G($r, 6, 2, 7, 8, 13, $v, $m); |
|
294 | - $v = self::G($r, 7, 3, 4, 9, 14, $v, $m); |
|
295 | - } |
|
296 | - |
|
297 | - for ($i = 8; $i--;) { |
|
298 | - $ctx[0][$i] = self::xor64( |
|
299 | - $ctx[0][$i], self::xor64($v[$i], $v[$i+8]) |
|
300 | - ); |
|
301 | - } |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * @internal You should not use this directly from another application |
|
306 | - * |
|
307 | - * @param int $r |
|
308 | - * @param int $i |
|
309 | - * @param int $a |
|
310 | - * @param int $b |
|
311 | - * @param int $c |
|
312 | - * @param int $d |
|
313 | - * @param SplFixedArray $v |
|
314 | - * @param SplFixedArray $m |
|
315 | - * @return SplFixedArray |
|
316 | - * @throws SodiumException |
|
317 | - * @throws TypeError |
|
318 | - * @psalm-suppress MixedArgument |
|
319 | - * @psalm-suppress MixedArrayOffset |
|
320 | - */ |
|
321 | - public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m) |
|
322 | - { |
|
323 | - $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]); |
|
324 | - $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32); |
|
325 | - $v[$c] = self::add64($v[$c], $v[$d]); |
|
326 | - $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 24); |
|
327 | - $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][($i << 1) + 1]]); |
|
328 | - $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 16); |
|
329 | - $v[$c] = self::add64($v[$c], $v[$d]); |
|
330 | - $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 63); |
|
331 | - return $v; |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * @internal You should not use this directly from another application |
|
336 | - * |
|
337 | - * @param SplFixedArray $ctx |
|
338 | - * @param int $inc |
|
339 | - * @return void |
|
340 | - * @throws SodiumException |
|
341 | - * @throws TypeError |
|
342 | - * @psalm-suppress MixedArgument |
|
343 | - * @psalm-suppress MixedArrayAccess |
|
344 | - * @psalm-suppress MixedArrayAssignment |
|
345 | - */ |
|
346 | - public static function increment_counter($ctx, $inc) |
|
347 | - { |
|
348 | - if ($inc < 0) { |
|
349 | - throw new SodiumException('Increasing by a negative number makes no sense.'); |
|
350 | - } |
|
351 | - $t = self::to64($inc); |
|
352 | - # S->t is $ctx[1] in our implementation |
|
353 | - |
|
354 | - # S->t[0] = ( uint64_t )( t >> 0 ); |
|
355 | - $ctx[1][0] = self::add64($ctx[1][0], $t); |
|
356 | - |
|
357 | - # S->t[1] += ( S->t[0] < inc ); |
|
358 | - if (!($ctx[1][0] instanceof ParagonIE_Sodium_Core32_Int64)) { |
|
359 | - throw new TypeError('Not an int64'); |
|
360 | - } |
|
361 | - /** @var ParagonIE_Sodium_Core32_Int64 $c*/ |
|
362 | - $c = $ctx[1][0]; |
|
363 | - if ($c->isLessThanInt($inc)) { |
|
364 | - $ctx[1][1] = self::add64($ctx[1][1], self::to64(1)); |
|
365 | - } |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * @internal You should not use this directly from another application |
|
370 | - * |
|
371 | - * @param SplFixedArray $ctx |
|
372 | - * @param SplFixedArray $p |
|
373 | - * @param int $plen |
|
374 | - * @return void |
|
375 | - * @throws SodiumException |
|
376 | - * @throws TypeError |
|
377 | - * @psalm-suppress MixedArgument |
|
378 | - * @psalm-suppress MixedAssignment |
|
379 | - * @psalm-suppress MixedArrayAccess |
|
380 | - * @psalm-suppress MixedArrayAssignment |
|
381 | - * @psalm-suppress MixedArrayOffset |
|
382 | - * @psalm-suppress MixedMethodCall |
|
383 | - * @psalm-suppress MixedOperand |
|
384 | - */ |
|
385 | - public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen) |
|
386 | - { |
|
387 | - self::pseudoConstructor(); |
|
388 | - |
|
389 | - $offset = 0; |
|
390 | - while ($plen > 0) { |
|
391 | - $left = $ctx[4]; |
|
392 | - $fill = 256 - $left; |
|
393 | - |
|
394 | - if ($plen > $fill) { |
|
395 | - # memcpy( S->buf + left, in, fill ); /* Fill buffer */ |
|
396 | - for ($i = $fill; $i--;) { |
|
397 | - $ctx[3][$i + $left] = $p[$i + $offset]; |
|
398 | - } |
|
399 | - |
|
400 | - # S->buflen += fill; |
|
401 | - $ctx[4] += $fill; |
|
402 | - |
|
403 | - # blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); |
|
404 | - self::increment_counter($ctx, 128); |
|
405 | - |
|
406 | - # blake2b_compress( S, S->buf ); /* Compress */ |
|
407 | - self::compress($ctx, $ctx[3]); |
|
408 | - |
|
409 | - # memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */ |
|
410 | - for ($i = 128; $i--;) { |
|
411 | - $ctx[3][$i] = $ctx[3][$i + 128]; |
|
412 | - } |
|
413 | - |
|
414 | - # S->buflen -= BLAKE2B_BLOCKBYTES; |
|
415 | - $ctx[4] -= 128; |
|
416 | - |
|
417 | - # in += fill; |
|
418 | - $offset += $fill; |
|
419 | - |
|
420 | - # inlen -= fill; |
|
421 | - $plen -= $fill; |
|
422 | - } else { |
|
423 | - for ($i = $plen; $i--;) { |
|
424 | - $ctx[3][$i + $left] = $p[$i + $offset]; |
|
425 | - } |
|
426 | - $ctx[4] += $plen; |
|
427 | - $offset += $plen; |
|
428 | - $plen -= $plen; |
|
429 | - } |
|
430 | - } |
|
431 | - } |
|
432 | - |
|
433 | - /** |
|
434 | - * @internal You should not use this directly from another application |
|
435 | - * |
|
436 | - * @param SplFixedArray $ctx |
|
437 | - * @param SplFixedArray $out |
|
438 | - * @return SplFixedArray |
|
439 | - * @throws SodiumException |
|
440 | - * @throws TypeError |
|
441 | - * @psalm-suppress MixedArgument |
|
442 | - * @psalm-suppress MixedAssignment |
|
443 | - * @psalm-suppress MixedArrayAccess |
|
444 | - * @psalm-suppress MixedArrayAssignment |
|
445 | - * @psalm-suppress MixedArrayOffset |
|
446 | - * @psalm-suppress MixedMethodCall |
|
447 | - * @psalm-suppress MixedOperand |
|
448 | - */ |
|
449 | - public static function finish(SplFixedArray $ctx, SplFixedArray $out) |
|
450 | - { |
|
451 | - self::pseudoConstructor(); |
|
452 | - if ($ctx[4] > 128) { |
|
453 | - self::increment_counter($ctx, 128); |
|
454 | - self::compress($ctx, $ctx[3]); |
|
455 | - $ctx[4] -= 128; |
|
456 | - if ($ctx[4] > 128) { |
|
457 | - throw new SodiumException('Failed to assert that buflen <= 128 bytes'); |
|
458 | - } |
|
459 | - for ($i = $ctx[4]; $i--;) { |
|
460 | - $ctx[3][$i] = $ctx[3][$i + 128]; |
|
461 | - } |
|
462 | - } |
|
463 | - |
|
464 | - self::increment_counter($ctx, $ctx[4]); |
|
465 | - $ctx[2][0] = self::new64(0xffffffff, 0xffffffff); |
|
466 | - |
|
467 | - for ($i = 256 - $ctx[4]; $i--;) { |
|
468 | - /** @var int $i */ |
|
469 | - $ctx[3][$i + $ctx[4]] = 0; |
|
470 | - } |
|
471 | - |
|
472 | - self::compress($ctx, $ctx[3]); |
|
473 | - |
|
474 | - $i = (int) (($out->getSize() - 1) / 8); |
|
475 | - for (; $i >= 0; --$i) { |
|
476 | - self::store64($out, $i << 3, $ctx[0][$i]); |
|
477 | - } |
|
478 | - return $out; |
|
479 | - } |
|
480 | - |
|
481 | - /** |
|
482 | - * @internal You should not use this directly from another application |
|
483 | - * |
|
484 | - * @param SplFixedArray|null $key |
|
485 | - * @param int $outlen |
|
486 | - * @param SplFixedArray|null $salt |
|
487 | - * @param SplFixedArray|null $personal |
|
488 | - * @return SplFixedArray |
|
489 | - * @throws SodiumException |
|
490 | - * @throws TypeError |
|
491 | - * @psalm-suppress MixedArgument |
|
492 | - * @psalm-suppress MixedAssignment |
|
493 | - * @psalm-suppress MixedArrayAccess |
|
494 | - * @psalm-suppress MixedArrayAssignment |
|
495 | - * @psalm-suppress MixedMethodCall |
|
496 | - */ |
|
497 | - public static function init( |
|
498 | - $key = null, |
|
499 | - $outlen = 64, |
|
500 | - $salt = null, |
|
501 | - $personal = null |
|
502 | - ) { |
|
503 | - self::pseudoConstructor(); |
|
504 | - $klen = 0; |
|
505 | - |
|
506 | - if ($key !== null) { |
|
507 | - if (count($key) > 64) { |
|
508 | - throw new SodiumException('Invalid key size'); |
|
509 | - } |
|
510 | - $klen = count($key); |
|
511 | - } |
|
512 | - |
|
513 | - if ($outlen > 64) { |
|
514 | - throw new SodiumException('Invalid output size'); |
|
515 | - } |
|
516 | - |
|
517 | - $ctx = self::context(); |
|
518 | - |
|
519 | - $p = new SplFixedArray(64); |
|
520 | - // Zero our param buffer... |
|
521 | - for ($i = 64; --$i;) { |
|
522 | - $p[$i] = 0; |
|
523 | - } |
|
524 | - |
|
525 | - $p[0] = $outlen; // digest_length |
|
526 | - $p[1] = $klen; // key_length |
|
527 | - $p[2] = 1; // fanout |
|
528 | - $p[3] = 1; // depth |
|
529 | - |
|
530 | - if ($salt instanceof SplFixedArray) { |
|
531 | - // salt: [32] through [47] |
|
532 | - for ($i = 0; $i < 16; ++$i) { |
|
533 | - $p[32 + $i] = (int) $salt[$i]; |
|
534 | - } |
|
535 | - } |
|
536 | - if ($personal instanceof SplFixedArray) { |
|
537 | - // personal: [48] through [63] |
|
538 | - for ($i = 0; $i < 16; ++$i) { |
|
539 | - $p[48 + $i] = (int) $personal[$i]; |
|
540 | - } |
|
541 | - } |
|
542 | - |
|
543 | - $ctx[0][0] = self::xor64( |
|
544 | - $ctx[0][0], |
|
545 | - self::load64($p, 0) |
|
546 | - ); |
|
547 | - |
|
548 | - if ($salt instanceof SplFixedArray || $personal instanceof SplFixedArray) { |
|
549 | - // We need to do what blake2b_init_param() does: |
|
550 | - for ($i = 1; $i < 8; ++$i) { |
|
551 | - $ctx[0][$i] = self::xor64( |
|
552 | - $ctx[0][$i], |
|
553 | - self::load64($p, $i << 3) |
|
554 | - ); |
|
555 | - } |
|
556 | - } |
|
557 | - |
|
558 | - if ($klen > 0 && $key instanceof SplFixedArray) { |
|
559 | - $block = new SplFixedArray(128); |
|
560 | - for ($i = 128; $i--;) { |
|
561 | - $block[$i] = 0; |
|
562 | - } |
|
563 | - for ($i = $klen; $i--;) { |
|
564 | - $block[$i] = $key[$i]; |
|
565 | - } |
|
566 | - self::update($ctx, $block, 128); |
|
567 | - $ctx[4] = 128; |
|
568 | - } |
|
569 | - |
|
570 | - return $ctx; |
|
571 | - } |
|
572 | - |
|
573 | - /** |
|
574 | - * Convert a string into an SplFixedArray of integers |
|
575 | - * |
|
576 | - * @internal You should not use this directly from another application |
|
577 | - * |
|
578 | - * @param string $str |
|
579 | - * @return SplFixedArray |
|
580 | - * @psalm-suppress MixedArgumentTypeCoercion |
|
581 | - */ |
|
582 | - public static function stringToSplFixedArray($str = '') |
|
583 | - { |
|
584 | - $values = unpack('C*', $str); |
|
585 | - return SplFixedArray::fromArray(array_values($values)); |
|
586 | - } |
|
587 | - |
|
588 | - /** |
|
589 | - * Convert an SplFixedArray of integers into a string |
|
590 | - * |
|
591 | - * @internal You should not use this directly from another application |
|
592 | - * |
|
593 | - * @param SplFixedArray $a |
|
594 | - * @return string |
|
595 | - */ |
|
596 | - public static function SplFixedArrayToString(SplFixedArray $a) |
|
597 | - { |
|
598 | - /** |
|
599 | - * @var array<int, string|int> |
|
600 | - */ |
|
601 | - $arr = $a->toArray(); |
|
602 | - $c = $a->count(); |
|
603 | - array_unshift($arr, str_repeat('C', $c)); |
|
604 | - return (string) (call_user_func_array('pack', $arr)); |
|
605 | - } |
|
606 | - |
|
607 | - /** |
|
608 | - * @internal You should not use this directly from another application |
|
609 | - * |
|
610 | - * @param SplFixedArray $ctx |
|
611 | - * @return string |
|
612 | - * @throws TypeError |
|
613 | - * @psalm-suppress MixedArgument |
|
614 | - * @psalm-suppress MixedArrayAccess |
|
615 | - * @psalm-suppress MixedArrayAssignment |
|
616 | - * @psalm-suppress MixedMethodCall |
|
617 | - */ |
|
618 | - public static function contextToString(SplFixedArray $ctx) |
|
619 | - { |
|
620 | - $str = ''; |
|
621 | - /** @var array<int, ParagonIE_Sodium_Core32_Int64> $ctxA */ |
|
622 | - $ctxA = $ctx[0]->toArray(); |
|
623 | - |
|
624 | - # uint64_t h[8]; |
|
625 | - for ($i = 0; $i < 8; ++$i) { |
|
626 | - if (!($ctxA[$i] instanceof ParagonIE_Sodium_Core32_Int64)) { |
|
627 | - throw new TypeError('Not an instance of Int64'); |
|
628 | - } |
|
629 | - /** @var ParagonIE_Sodium_Core32_Int64 $ctxAi */ |
|
630 | - $ctxAi = $ctxA[$i]; |
|
631 | - $str .= $ctxAi->toReverseString(); |
|
632 | - } |
|
633 | - |
|
634 | - # uint64_t t[2]; |
|
635 | - # uint64_t f[2]; |
|
636 | - for ($i = 1; $i < 3; ++$i) { |
|
637 | - /** @var array<int, ParagonIE_Sodium_Core32_Int64> $ctxA */ |
|
638 | - $ctxA = $ctx[$i]->toArray(); |
|
639 | - /** @var ParagonIE_Sodium_Core32_Int64 $ctxA1 */ |
|
640 | - $ctxA1 = $ctxA[0]; |
|
641 | - /** @var ParagonIE_Sodium_Core32_Int64 $ctxA2 */ |
|
642 | - $ctxA2 = $ctxA[1]; |
|
643 | - |
|
644 | - $str .= $ctxA1->toReverseString(); |
|
645 | - $str .= $ctxA2->toReverseString(); |
|
646 | - } |
|
647 | - |
|
648 | - # uint8_t buf[2 * 128]; |
|
649 | - $str .= self::SplFixedArrayToString($ctx[3]); |
|
650 | - |
|
651 | - /** @var int $ctx4 */ |
|
652 | - $ctx4 = $ctx[4]; |
|
653 | - |
|
654 | - # size_t buflen; |
|
655 | - $str .= implode('', array( |
|
656 | - self::intToChr($ctx4 & 0xff), |
|
657 | - self::intToChr(($ctx4 >> 8) & 0xff), |
|
658 | - self::intToChr(($ctx4 >> 16) & 0xff), |
|
659 | - self::intToChr(($ctx4 >> 24) & 0xff), |
|
660 | - "\x00\x00\x00\x00" |
|
661 | - /* |
|
14 | + /** |
|
15 | + * @var SplFixedArray |
|
16 | + */ |
|
17 | + public static $iv; |
|
18 | + |
|
19 | + /** |
|
20 | + * @var array<int, array<int, int>> |
|
21 | + */ |
|
22 | + public static $sigma = array( |
|
23 | + array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
24 | + array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3), |
|
25 | + array( 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4), |
|
26 | + array( 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8), |
|
27 | + array( 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13), |
|
28 | + array( 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9), |
|
29 | + array( 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11), |
|
30 | + array( 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10), |
|
31 | + array( 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5), |
|
32 | + array( 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0), |
|
33 | + array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), |
|
34 | + array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3) |
|
35 | + ); |
|
36 | + |
|
37 | + const BLOCKBYTES = 128; |
|
38 | + const OUTBYTES = 64; |
|
39 | + const KEYBYTES = 64; |
|
40 | + |
|
41 | + /** |
|
42 | + * Turn two 32-bit integers into a fixed array representing a 64-bit integer. |
|
43 | + * |
|
44 | + * @internal You should not use this directly from another application |
|
45 | + * |
|
46 | + * @param int $high |
|
47 | + * @param int $low |
|
48 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
49 | + * @throws SodiumException |
|
50 | + * @throws TypeError |
|
51 | + */ |
|
52 | + public static function new64($high, $low) |
|
53 | + { |
|
54 | + return ParagonIE_Sodium_Core32_Int64::fromInts($low, $high); |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * Convert an arbitrary number into an SplFixedArray of two 32-bit integers |
|
59 | + * that represents a 64-bit integer. |
|
60 | + * |
|
61 | + * @internal You should not use this directly from another application |
|
62 | + * |
|
63 | + * @param int $num |
|
64 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
65 | + * @throws SodiumException |
|
66 | + * @throws TypeError |
|
67 | + */ |
|
68 | + protected static function to64($num) |
|
69 | + { |
|
70 | + list($hi, $lo) = self::numericTo64BitInteger($num); |
|
71 | + return self::new64($hi, $lo); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Adds two 64-bit integers together, returning their sum as a SplFixedArray |
|
76 | + * containing two 32-bit integers (representing a 64-bit integer). |
|
77 | + * |
|
78 | + * @internal You should not use this directly from another application |
|
79 | + * |
|
80 | + * @param ParagonIE_Sodium_Core32_Int64 $x |
|
81 | + * @param ParagonIE_Sodium_Core32_Int64 $y |
|
82 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
83 | + */ |
|
84 | + protected static function add64($x, $y) |
|
85 | + { |
|
86 | + return $x->addInt64($y); |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @internal You should not use this directly from another application |
|
91 | + * |
|
92 | + * @param ParagonIE_Sodium_Core32_Int64 $x |
|
93 | + * @param ParagonIE_Sodium_Core32_Int64 $y |
|
94 | + * @param ParagonIE_Sodium_Core32_Int64 $z |
|
95 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
96 | + */ |
|
97 | + public static function add364($x, $y, $z) |
|
98 | + { |
|
99 | + return $x->addInt64($y)->addInt64($z); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @internal You should not use this directly from another application |
|
104 | + * |
|
105 | + * @param ParagonIE_Sodium_Core32_Int64 $x |
|
106 | + * @param ParagonIE_Sodium_Core32_Int64 $y |
|
107 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
108 | + * @throws TypeError |
|
109 | + */ |
|
110 | + public static function xor64(ParagonIE_Sodium_Core32_Int64 $x, ParagonIE_Sodium_Core32_Int64 $y) |
|
111 | + { |
|
112 | + return $x->xorInt64($y); |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * @internal You should not use this directly from another application |
|
117 | + * |
|
118 | + * @param ParagonIE_Sodium_Core32_Int64 $x |
|
119 | + * @param int $c |
|
120 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
121 | + * @throws SodiumException |
|
122 | + * @throws TypeError |
|
123 | + */ |
|
124 | + public static function rotr64(ParagonIE_Sodium_Core32_Int64 $x, $c) |
|
125 | + { |
|
126 | + return $x->rotateRight($c); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @internal You should not use this directly from another application |
|
131 | + * |
|
132 | + * @param SplFixedArray $x |
|
133 | + * @param int $i |
|
134 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
135 | + * @throws SodiumException |
|
136 | + * @throws TypeError |
|
137 | + */ |
|
138 | + public static function load64($x, $i) |
|
139 | + { |
|
140 | + /** @var int $l */ |
|
141 | + $l = (int) ($x[$i]) |
|
142 | + | ((int) ($x[$i+1]) << 8) |
|
143 | + | ((int) ($x[$i+2]) << 16) |
|
144 | + | ((int) ($x[$i+3]) << 24); |
|
145 | + /** @var int $h */ |
|
146 | + $h = (int) ($x[$i+4]) |
|
147 | + | ((int) ($x[$i+5]) << 8) |
|
148 | + | ((int) ($x[$i+6]) << 16) |
|
149 | + | ((int) ($x[$i+7]) << 24); |
|
150 | + return self::new64($h, $l); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * @internal You should not use this directly from another application |
|
155 | + * |
|
156 | + * @param SplFixedArray $x |
|
157 | + * @param int $i |
|
158 | + * @param ParagonIE_Sodium_Core32_Int64 $u |
|
159 | + * @return void |
|
160 | + * @throws TypeError |
|
161 | + * @psalm-suppress MixedArgument |
|
162 | + * @psalm-suppress MixedAssignment |
|
163 | + * @psalm-suppress MixedArrayAccess |
|
164 | + * @psalm-suppress MixedArrayAssignment |
|
165 | + * @psalm-suppress MixedArrayOffset |
|
166 | + */ |
|
167 | + public static function store64(SplFixedArray $x, $i, ParagonIE_Sodium_Core32_Int64 $u) |
|
168 | + { |
|
169 | + $v = clone $u; |
|
170 | + $maxLength = $x->getSize() - 1; |
|
171 | + for ($j = 0; $j < 8; ++$j) { |
|
172 | + $k = 3 - ($j >> 1); |
|
173 | + $x[$i] = $v->limbs[$k] & 0xff; |
|
174 | + if (++$i > $maxLength) { |
|
175 | + return; |
|
176 | + } |
|
177 | + $v->limbs[$k] >>= 8; |
|
178 | + } |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * This just sets the $iv static variable. |
|
183 | + * |
|
184 | + * @internal You should not use this directly from another application |
|
185 | + * |
|
186 | + * @return void |
|
187 | + * @throws SodiumException |
|
188 | + * @throws TypeError |
|
189 | + */ |
|
190 | + public static function pseudoConstructor() |
|
191 | + { |
|
192 | + static $called = false; |
|
193 | + if ($called) { |
|
194 | + return; |
|
195 | + } |
|
196 | + self::$iv = new SplFixedArray(8); |
|
197 | + self::$iv[0] = self::new64(0x6a09e667, 0xf3bcc908); |
|
198 | + self::$iv[1] = self::new64(0xbb67ae85, 0x84caa73b); |
|
199 | + self::$iv[2] = self::new64(0x3c6ef372, 0xfe94f82b); |
|
200 | + self::$iv[3] = self::new64(0xa54ff53a, 0x5f1d36f1); |
|
201 | + self::$iv[4] = self::new64(0x510e527f, 0xade682d1); |
|
202 | + self::$iv[5] = self::new64(0x9b05688c, 0x2b3e6c1f); |
|
203 | + self::$iv[6] = self::new64(0x1f83d9ab, 0xfb41bd6b); |
|
204 | + self::$iv[7] = self::new64(0x5be0cd19, 0x137e2179); |
|
205 | + |
|
206 | + $called = true; |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Returns a fresh BLAKE2 context. |
|
211 | + * |
|
212 | + * @internal You should not use this directly from another application |
|
213 | + * |
|
214 | + * @return SplFixedArray |
|
215 | + * @throws TypeError |
|
216 | + * @psalm-suppress MixedArgument |
|
217 | + * @psalm-suppress MixedAssignment |
|
218 | + * @psalm-suppress MixedArrayAccess |
|
219 | + * @psalm-suppress MixedArrayAssignment |
|
220 | + * @psalm-suppress MixedArrayOffset |
|
221 | + * @throws SodiumException |
|
222 | + * @throws TypeError |
|
223 | + */ |
|
224 | + protected static function context() |
|
225 | + { |
|
226 | + $ctx = new SplFixedArray(6); |
|
227 | + $ctx[0] = new SplFixedArray(8); // h |
|
228 | + $ctx[1] = new SplFixedArray(2); // t |
|
229 | + $ctx[2] = new SplFixedArray(2); // f |
|
230 | + $ctx[3] = new SplFixedArray(256); // buf |
|
231 | + $ctx[4] = 0; // buflen |
|
232 | + $ctx[5] = 0; // last_node (uint8_t) |
|
233 | + |
|
234 | + for ($i = 8; $i--;) { |
|
235 | + $ctx[0][$i] = self::$iv[$i]; |
|
236 | + } |
|
237 | + for ($i = 256; $i--;) { |
|
238 | + $ctx[3][$i] = 0; |
|
239 | + } |
|
240 | + |
|
241 | + $zero = self::new64(0, 0); |
|
242 | + $ctx[1][0] = $zero; |
|
243 | + $ctx[1][1] = $zero; |
|
244 | + $ctx[2][0] = $zero; |
|
245 | + $ctx[2][1] = $zero; |
|
246 | + |
|
247 | + return $ctx; |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * @internal You should not use this directly from another application |
|
252 | + * |
|
253 | + * @param SplFixedArray $ctx |
|
254 | + * @param SplFixedArray $buf |
|
255 | + * @return void |
|
256 | + * @throws SodiumException |
|
257 | + * @throws TypeError |
|
258 | + * @psalm-suppress MixedArgument |
|
259 | + * @psalm-suppress MixedArrayAccess |
|
260 | + * @psalm-suppress MixedArrayAssignment |
|
261 | + * @psalm-suppress MixedAssignment |
|
262 | + */ |
|
263 | + protected static function compress(SplFixedArray $ctx, SplFixedArray $buf) |
|
264 | + { |
|
265 | + $m = new SplFixedArray(16); |
|
266 | + $v = new SplFixedArray(16); |
|
267 | + |
|
268 | + for ($i = 16; $i--;) { |
|
269 | + $m[$i] = self::load64($buf, $i << 3); |
|
270 | + } |
|
271 | + |
|
272 | + for ($i = 8; $i--;) { |
|
273 | + $v[$i] = $ctx[0][$i]; |
|
274 | + } |
|
275 | + |
|
276 | + $v[ 8] = self::$iv[0]; |
|
277 | + $v[ 9] = self::$iv[1]; |
|
278 | + $v[10] = self::$iv[2]; |
|
279 | + $v[11] = self::$iv[3]; |
|
280 | + |
|
281 | + $v[12] = self::xor64($ctx[1][0], self::$iv[4]); |
|
282 | + $v[13] = self::xor64($ctx[1][1], self::$iv[5]); |
|
283 | + $v[14] = self::xor64($ctx[2][0], self::$iv[6]); |
|
284 | + $v[15] = self::xor64($ctx[2][1], self::$iv[7]); |
|
285 | + |
|
286 | + for ($r = 0; $r < 12; ++$r) { |
|
287 | + $v = self::G($r, 0, 0, 4, 8, 12, $v, $m); |
|
288 | + $v = self::G($r, 1, 1, 5, 9, 13, $v, $m); |
|
289 | + $v = self::G($r, 2, 2, 6, 10, 14, $v, $m); |
|
290 | + $v = self::G($r, 3, 3, 7, 11, 15, $v, $m); |
|
291 | + $v = self::G($r, 4, 0, 5, 10, 15, $v, $m); |
|
292 | + $v = self::G($r, 5, 1, 6, 11, 12, $v, $m); |
|
293 | + $v = self::G($r, 6, 2, 7, 8, 13, $v, $m); |
|
294 | + $v = self::G($r, 7, 3, 4, 9, 14, $v, $m); |
|
295 | + } |
|
296 | + |
|
297 | + for ($i = 8; $i--;) { |
|
298 | + $ctx[0][$i] = self::xor64( |
|
299 | + $ctx[0][$i], self::xor64($v[$i], $v[$i+8]) |
|
300 | + ); |
|
301 | + } |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * @internal You should not use this directly from another application |
|
306 | + * |
|
307 | + * @param int $r |
|
308 | + * @param int $i |
|
309 | + * @param int $a |
|
310 | + * @param int $b |
|
311 | + * @param int $c |
|
312 | + * @param int $d |
|
313 | + * @param SplFixedArray $v |
|
314 | + * @param SplFixedArray $m |
|
315 | + * @return SplFixedArray |
|
316 | + * @throws SodiumException |
|
317 | + * @throws TypeError |
|
318 | + * @psalm-suppress MixedArgument |
|
319 | + * @psalm-suppress MixedArrayOffset |
|
320 | + */ |
|
321 | + public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m) |
|
322 | + { |
|
323 | + $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]); |
|
324 | + $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32); |
|
325 | + $v[$c] = self::add64($v[$c], $v[$d]); |
|
326 | + $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 24); |
|
327 | + $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][($i << 1) + 1]]); |
|
328 | + $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 16); |
|
329 | + $v[$c] = self::add64($v[$c], $v[$d]); |
|
330 | + $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 63); |
|
331 | + return $v; |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * @internal You should not use this directly from another application |
|
336 | + * |
|
337 | + * @param SplFixedArray $ctx |
|
338 | + * @param int $inc |
|
339 | + * @return void |
|
340 | + * @throws SodiumException |
|
341 | + * @throws TypeError |
|
342 | + * @psalm-suppress MixedArgument |
|
343 | + * @psalm-suppress MixedArrayAccess |
|
344 | + * @psalm-suppress MixedArrayAssignment |
|
345 | + */ |
|
346 | + public static function increment_counter($ctx, $inc) |
|
347 | + { |
|
348 | + if ($inc < 0) { |
|
349 | + throw new SodiumException('Increasing by a negative number makes no sense.'); |
|
350 | + } |
|
351 | + $t = self::to64($inc); |
|
352 | + # S->t is $ctx[1] in our implementation |
|
353 | + |
|
354 | + # S->t[0] = ( uint64_t )( t >> 0 ); |
|
355 | + $ctx[1][0] = self::add64($ctx[1][0], $t); |
|
356 | + |
|
357 | + # S->t[1] += ( S->t[0] < inc ); |
|
358 | + if (!($ctx[1][0] instanceof ParagonIE_Sodium_Core32_Int64)) { |
|
359 | + throw new TypeError('Not an int64'); |
|
360 | + } |
|
361 | + /** @var ParagonIE_Sodium_Core32_Int64 $c*/ |
|
362 | + $c = $ctx[1][0]; |
|
363 | + if ($c->isLessThanInt($inc)) { |
|
364 | + $ctx[1][1] = self::add64($ctx[1][1], self::to64(1)); |
|
365 | + } |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * @internal You should not use this directly from another application |
|
370 | + * |
|
371 | + * @param SplFixedArray $ctx |
|
372 | + * @param SplFixedArray $p |
|
373 | + * @param int $plen |
|
374 | + * @return void |
|
375 | + * @throws SodiumException |
|
376 | + * @throws TypeError |
|
377 | + * @psalm-suppress MixedArgument |
|
378 | + * @psalm-suppress MixedAssignment |
|
379 | + * @psalm-suppress MixedArrayAccess |
|
380 | + * @psalm-suppress MixedArrayAssignment |
|
381 | + * @psalm-suppress MixedArrayOffset |
|
382 | + * @psalm-suppress MixedMethodCall |
|
383 | + * @psalm-suppress MixedOperand |
|
384 | + */ |
|
385 | + public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen) |
|
386 | + { |
|
387 | + self::pseudoConstructor(); |
|
388 | + |
|
389 | + $offset = 0; |
|
390 | + while ($plen > 0) { |
|
391 | + $left = $ctx[4]; |
|
392 | + $fill = 256 - $left; |
|
393 | + |
|
394 | + if ($plen > $fill) { |
|
395 | + # memcpy( S->buf + left, in, fill ); /* Fill buffer */ |
|
396 | + for ($i = $fill; $i--;) { |
|
397 | + $ctx[3][$i + $left] = $p[$i + $offset]; |
|
398 | + } |
|
399 | + |
|
400 | + # S->buflen += fill; |
|
401 | + $ctx[4] += $fill; |
|
402 | + |
|
403 | + # blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); |
|
404 | + self::increment_counter($ctx, 128); |
|
405 | + |
|
406 | + # blake2b_compress( S, S->buf ); /* Compress */ |
|
407 | + self::compress($ctx, $ctx[3]); |
|
408 | + |
|
409 | + # memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */ |
|
410 | + for ($i = 128; $i--;) { |
|
411 | + $ctx[3][$i] = $ctx[3][$i + 128]; |
|
412 | + } |
|
413 | + |
|
414 | + # S->buflen -= BLAKE2B_BLOCKBYTES; |
|
415 | + $ctx[4] -= 128; |
|
416 | + |
|
417 | + # in += fill; |
|
418 | + $offset += $fill; |
|
419 | + |
|
420 | + # inlen -= fill; |
|
421 | + $plen -= $fill; |
|
422 | + } else { |
|
423 | + for ($i = $plen; $i--;) { |
|
424 | + $ctx[3][$i + $left] = $p[$i + $offset]; |
|
425 | + } |
|
426 | + $ctx[4] += $plen; |
|
427 | + $offset += $plen; |
|
428 | + $plen -= $plen; |
|
429 | + } |
|
430 | + } |
|
431 | + } |
|
432 | + |
|
433 | + /** |
|
434 | + * @internal You should not use this directly from another application |
|
435 | + * |
|
436 | + * @param SplFixedArray $ctx |
|
437 | + * @param SplFixedArray $out |
|
438 | + * @return SplFixedArray |
|
439 | + * @throws SodiumException |
|
440 | + * @throws TypeError |
|
441 | + * @psalm-suppress MixedArgument |
|
442 | + * @psalm-suppress MixedAssignment |
|
443 | + * @psalm-suppress MixedArrayAccess |
|
444 | + * @psalm-suppress MixedArrayAssignment |
|
445 | + * @psalm-suppress MixedArrayOffset |
|
446 | + * @psalm-suppress MixedMethodCall |
|
447 | + * @psalm-suppress MixedOperand |
|
448 | + */ |
|
449 | + public static function finish(SplFixedArray $ctx, SplFixedArray $out) |
|
450 | + { |
|
451 | + self::pseudoConstructor(); |
|
452 | + if ($ctx[4] > 128) { |
|
453 | + self::increment_counter($ctx, 128); |
|
454 | + self::compress($ctx, $ctx[3]); |
|
455 | + $ctx[4] -= 128; |
|
456 | + if ($ctx[4] > 128) { |
|
457 | + throw new SodiumException('Failed to assert that buflen <= 128 bytes'); |
|
458 | + } |
|
459 | + for ($i = $ctx[4]; $i--;) { |
|
460 | + $ctx[3][$i] = $ctx[3][$i + 128]; |
|
461 | + } |
|
462 | + } |
|
463 | + |
|
464 | + self::increment_counter($ctx, $ctx[4]); |
|
465 | + $ctx[2][0] = self::new64(0xffffffff, 0xffffffff); |
|
466 | + |
|
467 | + for ($i = 256 - $ctx[4]; $i--;) { |
|
468 | + /** @var int $i */ |
|
469 | + $ctx[3][$i + $ctx[4]] = 0; |
|
470 | + } |
|
471 | + |
|
472 | + self::compress($ctx, $ctx[3]); |
|
473 | + |
|
474 | + $i = (int) (($out->getSize() - 1) / 8); |
|
475 | + for (; $i >= 0; --$i) { |
|
476 | + self::store64($out, $i << 3, $ctx[0][$i]); |
|
477 | + } |
|
478 | + return $out; |
|
479 | + } |
|
480 | + |
|
481 | + /** |
|
482 | + * @internal You should not use this directly from another application |
|
483 | + * |
|
484 | + * @param SplFixedArray|null $key |
|
485 | + * @param int $outlen |
|
486 | + * @param SplFixedArray|null $salt |
|
487 | + * @param SplFixedArray|null $personal |
|
488 | + * @return SplFixedArray |
|
489 | + * @throws SodiumException |
|
490 | + * @throws TypeError |
|
491 | + * @psalm-suppress MixedArgument |
|
492 | + * @psalm-suppress MixedAssignment |
|
493 | + * @psalm-suppress MixedArrayAccess |
|
494 | + * @psalm-suppress MixedArrayAssignment |
|
495 | + * @psalm-suppress MixedMethodCall |
|
496 | + */ |
|
497 | + public static function init( |
|
498 | + $key = null, |
|
499 | + $outlen = 64, |
|
500 | + $salt = null, |
|
501 | + $personal = null |
|
502 | + ) { |
|
503 | + self::pseudoConstructor(); |
|
504 | + $klen = 0; |
|
505 | + |
|
506 | + if ($key !== null) { |
|
507 | + if (count($key) > 64) { |
|
508 | + throw new SodiumException('Invalid key size'); |
|
509 | + } |
|
510 | + $klen = count($key); |
|
511 | + } |
|
512 | + |
|
513 | + if ($outlen > 64) { |
|
514 | + throw new SodiumException('Invalid output size'); |
|
515 | + } |
|
516 | + |
|
517 | + $ctx = self::context(); |
|
518 | + |
|
519 | + $p = new SplFixedArray(64); |
|
520 | + // Zero our param buffer... |
|
521 | + for ($i = 64; --$i;) { |
|
522 | + $p[$i] = 0; |
|
523 | + } |
|
524 | + |
|
525 | + $p[0] = $outlen; // digest_length |
|
526 | + $p[1] = $klen; // key_length |
|
527 | + $p[2] = 1; // fanout |
|
528 | + $p[3] = 1; // depth |
|
529 | + |
|
530 | + if ($salt instanceof SplFixedArray) { |
|
531 | + // salt: [32] through [47] |
|
532 | + for ($i = 0; $i < 16; ++$i) { |
|
533 | + $p[32 + $i] = (int) $salt[$i]; |
|
534 | + } |
|
535 | + } |
|
536 | + if ($personal instanceof SplFixedArray) { |
|
537 | + // personal: [48] through [63] |
|
538 | + for ($i = 0; $i < 16; ++$i) { |
|
539 | + $p[48 + $i] = (int) $personal[$i]; |
|
540 | + } |
|
541 | + } |
|
542 | + |
|
543 | + $ctx[0][0] = self::xor64( |
|
544 | + $ctx[0][0], |
|
545 | + self::load64($p, 0) |
|
546 | + ); |
|
547 | + |
|
548 | + if ($salt instanceof SplFixedArray || $personal instanceof SplFixedArray) { |
|
549 | + // We need to do what blake2b_init_param() does: |
|
550 | + for ($i = 1; $i < 8; ++$i) { |
|
551 | + $ctx[0][$i] = self::xor64( |
|
552 | + $ctx[0][$i], |
|
553 | + self::load64($p, $i << 3) |
|
554 | + ); |
|
555 | + } |
|
556 | + } |
|
557 | + |
|
558 | + if ($klen > 0 && $key instanceof SplFixedArray) { |
|
559 | + $block = new SplFixedArray(128); |
|
560 | + for ($i = 128; $i--;) { |
|
561 | + $block[$i] = 0; |
|
562 | + } |
|
563 | + for ($i = $klen; $i--;) { |
|
564 | + $block[$i] = $key[$i]; |
|
565 | + } |
|
566 | + self::update($ctx, $block, 128); |
|
567 | + $ctx[4] = 128; |
|
568 | + } |
|
569 | + |
|
570 | + return $ctx; |
|
571 | + } |
|
572 | + |
|
573 | + /** |
|
574 | + * Convert a string into an SplFixedArray of integers |
|
575 | + * |
|
576 | + * @internal You should not use this directly from another application |
|
577 | + * |
|
578 | + * @param string $str |
|
579 | + * @return SplFixedArray |
|
580 | + * @psalm-suppress MixedArgumentTypeCoercion |
|
581 | + */ |
|
582 | + public static function stringToSplFixedArray($str = '') |
|
583 | + { |
|
584 | + $values = unpack('C*', $str); |
|
585 | + return SplFixedArray::fromArray(array_values($values)); |
|
586 | + } |
|
587 | + |
|
588 | + /** |
|
589 | + * Convert an SplFixedArray of integers into a string |
|
590 | + * |
|
591 | + * @internal You should not use this directly from another application |
|
592 | + * |
|
593 | + * @param SplFixedArray $a |
|
594 | + * @return string |
|
595 | + */ |
|
596 | + public static function SplFixedArrayToString(SplFixedArray $a) |
|
597 | + { |
|
598 | + /** |
|
599 | + * @var array<int, string|int> |
|
600 | + */ |
|
601 | + $arr = $a->toArray(); |
|
602 | + $c = $a->count(); |
|
603 | + array_unshift($arr, str_repeat('C', $c)); |
|
604 | + return (string) (call_user_func_array('pack', $arr)); |
|
605 | + } |
|
606 | + |
|
607 | + /** |
|
608 | + * @internal You should not use this directly from another application |
|
609 | + * |
|
610 | + * @param SplFixedArray $ctx |
|
611 | + * @return string |
|
612 | + * @throws TypeError |
|
613 | + * @psalm-suppress MixedArgument |
|
614 | + * @psalm-suppress MixedArrayAccess |
|
615 | + * @psalm-suppress MixedArrayAssignment |
|
616 | + * @psalm-suppress MixedMethodCall |
|
617 | + */ |
|
618 | + public static function contextToString(SplFixedArray $ctx) |
|
619 | + { |
|
620 | + $str = ''; |
|
621 | + /** @var array<int, ParagonIE_Sodium_Core32_Int64> $ctxA */ |
|
622 | + $ctxA = $ctx[0]->toArray(); |
|
623 | + |
|
624 | + # uint64_t h[8]; |
|
625 | + for ($i = 0; $i < 8; ++$i) { |
|
626 | + if (!($ctxA[$i] instanceof ParagonIE_Sodium_Core32_Int64)) { |
|
627 | + throw new TypeError('Not an instance of Int64'); |
|
628 | + } |
|
629 | + /** @var ParagonIE_Sodium_Core32_Int64 $ctxAi */ |
|
630 | + $ctxAi = $ctxA[$i]; |
|
631 | + $str .= $ctxAi->toReverseString(); |
|
632 | + } |
|
633 | + |
|
634 | + # uint64_t t[2]; |
|
635 | + # uint64_t f[2]; |
|
636 | + for ($i = 1; $i < 3; ++$i) { |
|
637 | + /** @var array<int, ParagonIE_Sodium_Core32_Int64> $ctxA */ |
|
638 | + $ctxA = $ctx[$i]->toArray(); |
|
639 | + /** @var ParagonIE_Sodium_Core32_Int64 $ctxA1 */ |
|
640 | + $ctxA1 = $ctxA[0]; |
|
641 | + /** @var ParagonIE_Sodium_Core32_Int64 $ctxA2 */ |
|
642 | + $ctxA2 = $ctxA[1]; |
|
643 | + |
|
644 | + $str .= $ctxA1->toReverseString(); |
|
645 | + $str .= $ctxA2->toReverseString(); |
|
646 | + } |
|
647 | + |
|
648 | + # uint8_t buf[2 * 128]; |
|
649 | + $str .= self::SplFixedArrayToString($ctx[3]); |
|
650 | + |
|
651 | + /** @var int $ctx4 */ |
|
652 | + $ctx4 = $ctx[4]; |
|
653 | + |
|
654 | + # size_t buflen; |
|
655 | + $str .= implode('', array( |
|
656 | + self::intToChr($ctx4 & 0xff), |
|
657 | + self::intToChr(($ctx4 >> 8) & 0xff), |
|
658 | + self::intToChr(($ctx4 >> 16) & 0xff), |
|
659 | + self::intToChr(($ctx4 >> 24) & 0xff), |
|
660 | + "\x00\x00\x00\x00" |
|
661 | + /* |
|
662 | 662 | self::intToChr(($ctx4 >> 32) & 0xff), |
663 | 663 | self::intToChr(($ctx4 >> 40) & 0xff), |
664 | 664 | self::intToChr(($ctx4 >> 48) & 0xff), |
665 | 665 | self::intToChr(($ctx4 >> 56) & 0xff) |
666 | 666 | */ |
667 | - )); |
|
668 | - # uint8_t last_node; |
|
669 | - return $str . self::intToChr($ctx[5]) . str_repeat("\x00", 23); |
|
670 | - } |
|
671 | - |
|
672 | - /** |
|
673 | - * Creates an SplFixedArray containing other SplFixedArray elements, from |
|
674 | - * a string (compatible with \Sodium\crypto_generichash_{init, update, final}) |
|
675 | - * |
|
676 | - * @internal You should not use this directly from another application |
|
677 | - * |
|
678 | - * @param string $string |
|
679 | - * @return SplFixedArray |
|
680 | - * @throws SodiumException |
|
681 | - * @throws TypeError |
|
682 | - * @psalm-suppress MixedArrayAccess |
|
683 | - * @psalm-suppress MixedArrayAssignment |
|
684 | - */ |
|
685 | - public static function stringToContext($string) |
|
686 | - { |
|
687 | - $ctx = self::context(); |
|
688 | - |
|
689 | - # uint64_t h[8]; |
|
690 | - for ($i = 0; $i < 8; ++$i) { |
|
691 | - $ctx[0][$i] = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
692 | - self::substr($string, (($i << 3) + 0), 8) |
|
693 | - ); |
|
694 | - } |
|
695 | - |
|
696 | - # uint64_t t[2]; |
|
697 | - # uint64_t f[2]; |
|
698 | - for ($i = 1; $i < 3; ++$i) { |
|
699 | - $ctx[$i][1] = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
700 | - self::substr($string, 72 + (($i - 1) << 4), 8) |
|
701 | - ); |
|
702 | - $ctx[$i][0] = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
703 | - self::substr($string, 64 + (($i - 1) << 4), 8) |
|
704 | - ); |
|
705 | - } |
|
706 | - |
|
707 | - # uint8_t buf[2 * 128]; |
|
708 | - $ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256)); |
|
709 | - |
|
710 | - # uint8_t buf[2 * 128]; |
|
711 | - $int = 0; |
|
712 | - for ($i = 0; $i < 8; ++$i) { |
|
713 | - $int |= self::chrToInt($string[352 + $i]) << ($i << 3); |
|
714 | - } |
|
715 | - $ctx[4] = $int; |
|
716 | - |
|
717 | - return $ctx; |
|
718 | - } |
|
667 | + )); |
|
668 | + # uint8_t last_node; |
|
669 | + return $str . self::intToChr($ctx[5]) . str_repeat("\x00", 23); |
|
670 | + } |
|
671 | + |
|
672 | + /** |
|
673 | + * Creates an SplFixedArray containing other SplFixedArray elements, from |
|
674 | + * a string (compatible with \Sodium\crypto_generichash_{init, update, final}) |
|
675 | + * |
|
676 | + * @internal You should not use this directly from another application |
|
677 | + * |
|
678 | + * @param string $string |
|
679 | + * @return SplFixedArray |
|
680 | + * @throws SodiumException |
|
681 | + * @throws TypeError |
|
682 | + * @psalm-suppress MixedArrayAccess |
|
683 | + * @psalm-suppress MixedArrayAssignment |
|
684 | + */ |
|
685 | + public static function stringToContext($string) |
|
686 | + { |
|
687 | + $ctx = self::context(); |
|
688 | + |
|
689 | + # uint64_t h[8]; |
|
690 | + for ($i = 0; $i < 8; ++$i) { |
|
691 | + $ctx[0][$i] = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
692 | + self::substr($string, (($i << 3) + 0), 8) |
|
693 | + ); |
|
694 | + } |
|
695 | + |
|
696 | + # uint64_t t[2]; |
|
697 | + # uint64_t f[2]; |
|
698 | + for ($i = 1; $i < 3; ++$i) { |
|
699 | + $ctx[$i][1] = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
700 | + self::substr($string, 72 + (($i - 1) << 4), 8) |
|
701 | + ); |
|
702 | + $ctx[$i][0] = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
703 | + self::substr($string, 64 + (($i - 1) << 4), 8) |
|
704 | + ); |
|
705 | + } |
|
706 | + |
|
707 | + # uint8_t buf[2 * 128]; |
|
708 | + $ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256)); |
|
709 | + |
|
710 | + # uint8_t buf[2 * 128]; |
|
711 | + $int = 0; |
|
712 | + for ($i = 0; $i < 8; ++$i) { |
|
713 | + $int |= self::chrToInt($string[352 + $i]) << ($i << 3); |
|
714 | + } |
|
715 | + $ctx[4] = $int; |
|
716 | + |
|
717 | + return $ctx; |
|
718 | + } |
|
719 | 719 | } |
@@ -1,7 +1,7 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_Util', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_SipHash', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -11,228 +11,228 @@ discard block |
||
11 | 11 | */ |
12 | 12 | class ParagonIE_Sodium_Core32_SipHash extends ParagonIE_Sodium_Core32_Util |
13 | 13 | { |
14 | - /** |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param array<int, ParagonIE_Sodium_Core32_Int64> $v |
|
18 | - * @return array<int, ParagonIE_Sodium_Core32_Int64> |
|
19 | - */ |
|
20 | - public static function sipRound(array $v) |
|
21 | - { |
|
22 | - # v0 += v1; |
|
23 | - $v[0] = $v[0]->addInt64($v[1]); |
|
24 | - |
|
25 | - # v1 = ROTL(v1, 13); |
|
26 | - $v[1] = $v[1]->rotateLeft(13); |
|
27 | - |
|
28 | - # v1 ^= v0; |
|
29 | - $v[1] = $v[1]->xorInt64($v[0]); |
|
30 | - |
|
31 | - # v0=ROTL(v0,32); |
|
32 | - $v[0] = $v[0]->rotateLeft(32); |
|
33 | - |
|
34 | - # v2 += v3; |
|
35 | - $v[2] = $v[2]->addInt64($v[3]); |
|
36 | - |
|
37 | - # v3=ROTL(v3,16); |
|
38 | - $v[3] = $v[3]->rotateLeft(16); |
|
39 | - |
|
40 | - # v3 ^= v2; |
|
41 | - $v[3] = $v[3]->xorInt64($v[2]); |
|
42 | - |
|
43 | - # v0 += v3; |
|
44 | - $v[0] = $v[0]->addInt64($v[3]); |
|
45 | - |
|
46 | - # v3=ROTL(v3,21); |
|
47 | - $v[3] = $v[3]->rotateLeft(21); |
|
48 | - |
|
49 | - # v3 ^= v0; |
|
50 | - $v[3] = $v[3]->xorInt64($v[0]); |
|
51 | - |
|
52 | - # v2 += v1; |
|
53 | - $v[2] = $v[2]->addInt64($v[1]); |
|
54 | - |
|
55 | - # v1=ROTL(v1,17); |
|
56 | - $v[1] = $v[1]->rotateLeft(17); |
|
57 | - |
|
58 | - # v1 ^= v2; |
|
59 | - $v[1] = $v[1]->xorInt64($v[2]); |
|
60 | - |
|
61 | - # v2=ROTL(v2,32) |
|
62 | - $v[2] = $v[2]->rotateLeft(32); |
|
63 | - |
|
64 | - return $v; |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * @internal You should not use this directly from another application |
|
69 | - * |
|
70 | - * @param string $in |
|
71 | - * @param string $key |
|
72 | - * @return string |
|
73 | - * @throws SodiumException |
|
74 | - * @throws TypeError |
|
75 | - */ |
|
76 | - public static function sipHash24($in, $key) |
|
77 | - { |
|
78 | - $inlen = self::strlen($in); |
|
79 | - |
|
80 | - # /* "somepseudorandomlygeneratedbytes" */ |
|
81 | - # u64 v0 = 0x736f6d6570736575ULL; |
|
82 | - # u64 v1 = 0x646f72616e646f6dULL; |
|
83 | - # u64 v2 = 0x6c7967656e657261ULL; |
|
84 | - # u64 v3 = 0x7465646279746573ULL; |
|
85 | - $v = array( |
|
86 | - new ParagonIE_Sodium_Core32_Int64( |
|
87 | - array(0x736f, 0x6d65, 0x7073, 0x6575) |
|
88 | - ), |
|
89 | - new ParagonIE_Sodium_Core32_Int64( |
|
90 | - array(0x646f, 0x7261, 0x6e64, 0x6f6d) |
|
91 | - ), |
|
92 | - new ParagonIE_Sodium_Core32_Int64( |
|
93 | - array(0x6c79, 0x6765, 0x6e65, 0x7261) |
|
94 | - ), |
|
95 | - new ParagonIE_Sodium_Core32_Int64( |
|
96 | - array(0x7465, 0x6462, 0x7974, 0x6573) |
|
97 | - ) |
|
98 | - ); |
|
99 | - |
|
100 | - # u64 k0 = LOAD64_LE( k ); |
|
101 | - # u64 k1 = LOAD64_LE( k + 8 ); |
|
102 | - $k = array( |
|
103 | - ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
104 | - self::substr($key, 0, 8) |
|
105 | - ), |
|
106 | - ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
107 | - self::substr($key, 8, 8) |
|
108 | - ) |
|
109 | - ); |
|
110 | - |
|
111 | - # b = ( ( u64 )inlen ) << 56; |
|
112 | - $b = new ParagonIE_Sodium_Core32_Int64( |
|
113 | - array(($inlen << 8) & 0xffff, 0, 0, 0) |
|
114 | - ); |
|
115 | - |
|
116 | - # v3 ^= k1; |
|
117 | - $v[3] = $v[3]->xorInt64($k[1]); |
|
118 | - # v2 ^= k0; |
|
119 | - $v[2] = $v[2]->xorInt64($k[0]); |
|
120 | - # v1 ^= k1; |
|
121 | - $v[1] = $v[1]->xorInt64($k[1]); |
|
122 | - # v0 ^= k0; |
|
123 | - $v[0] = $v[0]->xorInt64($k[0]); |
|
124 | - |
|
125 | - $left = $inlen; |
|
126 | - # for ( ; in != end; in += 8 ) |
|
127 | - while ($left >= 8) { |
|
128 | - # m = LOAD64_LE( in ); |
|
129 | - $m = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
130 | - self::substr($in, 0, 8) |
|
131 | - ); |
|
132 | - |
|
133 | - # v3 ^= m; |
|
134 | - $v[3] = $v[3]->xorInt64($m); |
|
135 | - |
|
136 | - # SIPROUND; |
|
137 | - # SIPROUND; |
|
138 | - $v = self::sipRound($v); |
|
139 | - $v = self::sipRound($v); |
|
140 | - |
|
141 | - # v0 ^= m; |
|
142 | - $v[0] = $v[0]->xorInt64($m); |
|
143 | - |
|
144 | - $in = self::substr($in, 8); |
|
145 | - $left -= 8; |
|
146 | - } |
|
147 | - |
|
148 | - # switch( left ) |
|
149 | - # { |
|
150 | - # case 7: b |= ( ( u64 )in[ 6] ) << 48; |
|
151 | - # case 6: b |= ( ( u64 )in[ 5] ) << 40; |
|
152 | - # case 5: b |= ( ( u64 )in[ 4] ) << 32; |
|
153 | - # case 4: b |= ( ( u64 )in[ 3] ) << 24; |
|
154 | - # case 3: b |= ( ( u64 )in[ 2] ) << 16; |
|
155 | - # case 2: b |= ( ( u64 )in[ 1] ) << 8; |
|
156 | - # case 1: b |= ( ( u64 )in[ 0] ); break; |
|
157 | - # case 0: break; |
|
158 | - # } |
|
159 | - switch ($left) { |
|
160 | - case 7: |
|
161 | - $b = $b->orInt64( |
|
162 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
163 | - 0, self::chrToInt($in[6]) << 16 |
|
164 | - ) |
|
165 | - ); |
|
166 | - case 6: |
|
167 | - $b = $b->orInt64( |
|
168 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
169 | - 0, self::chrToInt($in[5]) << 8 |
|
170 | - ) |
|
171 | - ); |
|
172 | - case 5: |
|
173 | - $b = $b->orInt64( |
|
174 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
175 | - 0, self::chrToInt($in[4]) |
|
176 | - ) |
|
177 | - ); |
|
178 | - case 4: |
|
179 | - $b = $b->orInt64( |
|
180 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
181 | - self::chrToInt($in[3]) << 24, 0 |
|
182 | - ) |
|
183 | - ); |
|
184 | - case 3: |
|
185 | - $b = $b->orInt64( |
|
186 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
187 | - self::chrToInt($in[2]) << 16, 0 |
|
188 | - ) |
|
189 | - ); |
|
190 | - case 2: |
|
191 | - $b = $b->orInt64( |
|
192 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
193 | - self::chrToInt($in[1]) << 8, 0 |
|
194 | - ) |
|
195 | - ); |
|
196 | - case 1: |
|
197 | - $b = $b->orInt64( |
|
198 | - ParagonIE_Sodium_Core32_Int64::fromInts( |
|
199 | - self::chrToInt($in[0]), 0 |
|
200 | - ) |
|
201 | - ); |
|
202 | - case 0: |
|
203 | - break; |
|
204 | - } |
|
205 | - |
|
206 | - # v3 ^= b; |
|
207 | - $v[3] = $v[3]->xorInt64($b); |
|
208 | - |
|
209 | - # SIPROUND; |
|
210 | - # SIPROUND; |
|
211 | - $v = self::sipRound($v); |
|
212 | - $v = self::sipRound($v); |
|
213 | - |
|
214 | - # v0 ^= b; |
|
215 | - $v[0] = $v[0]->xorInt64($b); |
|
216 | - |
|
217 | - // Flip the lower 8 bits of v2 which is ($v[4], $v[5]) in our implementation |
|
218 | - # v2 ^= 0xff; |
|
219 | - $v[2]->limbs[3] ^= 0xff; |
|
220 | - |
|
221 | - # SIPROUND; |
|
222 | - # SIPROUND; |
|
223 | - # SIPROUND; |
|
224 | - # SIPROUND; |
|
225 | - $v = self::sipRound($v); |
|
226 | - $v = self::sipRound($v); |
|
227 | - $v = self::sipRound($v); |
|
228 | - $v = self::sipRound($v); |
|
229 | - |
|
230 | - # b = v0 ^ v1 ^ v2 ^ v3; |
|
231 | - # STORE64_LE( out, b ); |
|
232 | - return $v[0] |
|
233 | - ->xorInt64($v[1]) |
|
234 | - ->xorInt64($v[2]) |
|
235 | - ->xorInt64($v[3]) |
|
236 | - ->toReverseString(); |
|
237 | - } |
|
14 | + /** |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param array<int, ParagonIE_Sodium_Core32_Int64> $v |
|
18 | + * @return array<int, ParagonIE_Sodium_Core32_Int64> |
|
19 | + */ |
|
20 | + public static function sipRound(array $v) |
|
21 | + { |
|
22 | + # v0 += v1; |
|
23 | + $v[0] = $v[0]->addInt64($v[1]); |
|
24 | + |
|
25 | + # v1 = ROTL(v1, 13); |
|
26 | + $v[1] = $v[1]->rotateLeft(13); |
|
27 | + |
|
28 | + # v1 ^= v0; |
|
29 | + $v[1] = $v[1]->xorInt64($v[0]); |
|
30 | + |
|
31 | + # v0=ROTL(v0,32); |
|
32 | + $v[0] = $v[0]->rotateLeft(32); |
|
33 | + |
|
34 | + # v2 += v3; |
|
35 | + $v[2] = $v[2]->addInt64($v[3]); |
|
36 | + |
|
37 | + # v3=ROTL(v3,16); |
|
38 | + $v[3] = $v[3]->rotateLeft(16); |
|
39 | + |
|
40 | + # v3 ^= v2; |
|
41 | + $v[3] = $v[3]->xorInt64($v[2]); |
|
42 | + |
|
43 | + # v0 += v3; |
|
44 | + $v[0] = $v[0]->addInt64($v[3]); |
|
45 | + |
|
46 | + # v3=ROTL(v3,21); |
|
47 | + $v[3] = $v[3]->rotateLeft(21); |
|
48 | + |
|
49 | + # v3 ^= v0; |
|
50 | + $v[3] = $v[3]->xorInt64($v[0]); |
|
51 | + |
|
52 | + # v2 += v1; |
|
53 | + $v[2] = $v[2]->addInt64($v[1]); |
|
54 | + |
|
55 | + # v1=ROTL(v1,17); |
|
56 | + $v[1] = $v[1]->rotateLeft(17); |
|
57 | + |
|
58 | + # v1 ^= v2; |
|
59 | + $v[1] = $v[1]->xorInt64($v[2]); |
|
60 | + |
|
61 | + # v2=ROTL(v2,32) |
|
62 | + $v[2] = $v[2]->rotateLeft(32); |
|
63 | + |
|
64 | + return $v; |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * @internal You should not use this directly from another application |
|
69 | + * |
|
70 | + * @param string $in |
|
71 | + * @param string $key |
|
72 | + * @return string |
|
73 | + * @throws SodiumException |
|
74 | + * @throws TypeError |
|
75 | + */ |
|
76 | + public static function sipHash24($in, $key) |
|
77 | + { |
|
78 | + $inlen = self::strlen($in); |
|
79 | + |
|
80 | + # /* "somepseudorandomlygeneratedbytes" */ |
|
81 | + # u64 v0 = 0x736f6d6570736575ULL; |
|
82 | + # u64 v1 = 0x646f72616e646f6dULL; |
|
83 | + # u64 v2 = 0x6c7967656e657261ULL; |
|
84 | + # u64 v3 = 0x7465646279746573ULL; |
|
85 | + $v = array( |
|
86 | + new ParagonIE_Sodium_Core32_Int64( |
|
87 | + array(0x736f, 0x6d65, 0x7073, 0x6575) |
|
88 | + ), |
|
89 | + new ParagonIE_Sodium_Core32_Int64( |
|
90 | + array(0x646f, 0x7261, 0x6e64, 0x6f6d) |
|
91 | + ), |
|
92 | + new ParagonIE_Sodium_Core32_Int64( |
|
93 | + array(0x6c79, 0x6765, 0x6e65, 0x7261) |
|
94 | + ), |
|
95 | + new ParagonIE_Sodium_Core32_Int64( |
|
96 | + array(0x7465, 0x6462, 0x7974, 0x6573) |
|
97 | + ) |
|
98 | + ); |
|
99 | + |
|
100 | + # u64 k0 = LOAD64_LE( k ); |
|
101 | + # u64 k1 = LOAD64_LE( k + 8 ); |
|
102 | + $k = array( |
|
103 | + ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
104 | + self::substr($key, 0, 8) |
|
105 | + ), |
|
106 | + ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
107 | + self::substr($key, 8, 8) |
|
108 | + ) |
|
109 | + ); |
|
110 | + |
|
111 | + # b = ( ( u64 )inlen ) << 56; |
|
112 | + $b = new ParagonIE_Sodium_Core32_Int64( |
|
113 | + array(($inlen << 8) & 0xffff, 0, 0, 0) |
|
114 | + ); |
|
115 | + |
|
116 | + # v3 ^= k1; |
|
117 | + $v[3] = $v[3]->xorInt64($k[1]); |
|
118 | + # v2 ^= k0; |
|
119 | + $v[2] = $v[2]->xorInt64($k[0]); |
|
120 | + # v1 ^= k1; |
|
121 | + $v[1] = $v[1]->xorInt64($k[1]); |
|
122 | + # v0 ^= k0; |
|
123 | + $v[0] = $v[0]->xorInt64($k[0]); |
|
124 | + |
|
125 | + $left = $inlen; |
|
126 | + # for ( ; in != end; in += 8 ) |
|
127 | + while ($left >= 8) { |
|
128 | + # m = LOAD64_LE( in ); |
|
129 | + $m = ParagonIE_Sodium_Core32_Int64::fromReverseString( |
|
130 | + self::substr($in, 0, 8) |
|
131 | + ); |
|
132 | + |
|
133 | + # v3 ^= m; |
|
134 | + $v[3] = $v[3]->xorInt64($m); |
|
135 | + |
|
136 | + # SIPROUND; |
|
137 | + # SIPROUND; |
|
138 | + $v = self::sipRound($v); |
|
139 | + $v = self::sipRound($v); |
|
140 | + |
|
141 | + # v0 ^= m; |
|
142 | + $v[0] = $v[0]->xorInt64($m); |
|
143 | + |
|
144 | + $in = self::substr($in, 8); |
|
145 | + $left -= 8; |
|
146 | + } |
|
147 | + |
|
148 | + # switch( left ) |
|
149 | + # { |
|
150 | + # case 7: b |= ( ( u64 )in[ 6] ) << 48; |
|
151 | + # case 6: b |= ( ( u64 )in[ 5] ) << 40; |
|
152 | + # case 5: b |= ( ( u64 )in[ 4] ) << 32; |
|
153 | + # case 4: b |= ( ( u64 )in[ 3] ) << 24; |
|
154 | + # case 3: b |= ( ( u64 )in[ 2] ) << 16; |
|
155 | + # case 2: b |= ( ( u64 )in[ 1] ) << 8; |
|
156 | + # case 1: b |= ( ( u64 )in[ 0] ); break; |
|
157 | + # case 0: break; |
|
158 | + # } |
|
159 | + switch ($left) { |
|
160 | + case 7: |
|
161 | + $b = $b->orInt64( |
|
162 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
163 | + 0, self::chrToInt($in[6]) << 16 |
|
164 | + ) |
|
165 | + ); |
|
166 | + case 6: |
|
167 | + $b = $b->orInt64( |
|
168 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
169 | + 0, self::chrToInt($in[5]) << 8 |
|
170 | + ) |
|
171 | + ); |
|
172 | + case 5: |
|
173 | + $b = $b->orInt64( |
|
174 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
175 | + 0, self::chrToInt($in[4]) |
|
176 | + ) |
|
177 | + ); |
|
178 | + case 4: |
|
179 | + $b = $b->orInt64( |
|
180 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
181 | + self::chrToInt($in[3]) << 24, 0 |
|
182 | + ) |
|
183 | + ); |
|
184 | + case 3: |
|
185 | + $b = $b->orInt64( |
|
186 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
187 | + self::chrToInt($in[2]) << 16, 0 |
|
188 | + ) |
|
189 | + ); |
|
190 | + case 2: |
|
191 | + $b = $b->orInt64( |
|
192 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
193 | + self::chrToInt($in[1]) << 8, 0 |
|
194 | + ) |
|
195 | + ); |
|
196 | + case 1: |
|
197 | + $b = $b->orInt64( |
|
198 | + ParagonIE_Sodium_Core32_Int64::fromInts( |
|
199 | + self::chrToInt($in[0]), 0 |
|
200 | + ) |
|
201 | + ); |
|
202 | + case 0: |
|
203 | + break; |
|
204 | + } |
|
205 | + |
|
206 | + # v3 ^= b; |
|
207 | + $v[3] = $v[3]->xorInt64($b); |
|
208 | + |
|
209 | + # SIPROUND; |
|
210 | + # SIPROUND; |
|
211 | + $v = self::sipRound($v); |
|
212 | + $v = self::sipRound($v); |
|
213 | + |
|
214 | + # v0 ^= b; |
|
215 | + $v[0] = $v[0]->xorInt64($b); |
|
216 | + |
|
217 | + // Flip the lower 8 bits of v2 which is ($v[4], $v[5]) in our implementation |
|
218 | + # v2 ^= 0xff; |
|
219 | + $v[2]->limbs[3] ^= 0xff; |
|
220 | + |
|
221 | + # SIPROUND; |
|
222 | + # SIPROUND; |
|
223 | + # SIPROUND; |
|
224 | + # SIPROUND; |
|
225 | + $v = self::sipRound($v); |
|
226 | + $v = self::sipRound($v); |
|
227 | + $v = self::sipRound($v); |
|
228 | + $v = self::sipRound($v); |
|
229 | + |
|
230 | + # b = v0 ^ v1 ^ v2 ^ v3; |
|
231 | + # STORE64_LE( out, b ); |
|
232 | + return $v[0] |
|
233 | + ->xorInt64($v[1]) |
|
234 | + ->xorInt64($v[2]) |
|
235 | + ->xorInt64($v[3]) |
|
236 | + ->toReverseString(); |
|
237 | + } |
|
238 | 238 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_X25519', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,337 +9,337 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core32_X25519 extends ParagonIE_Sodium_Core32_Curve25519 |
11 | 11 | { |
12 | - /** |
|
13 | - * Alters the objects passed to this method in place. |
|
14 | - * |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f |
|
18 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $g |
|
19 | - * @param int $b |
|
20 | - * @return void |
|
21 | - * @throws SodiumException |
|
22 | - * @throws TypeError |
|
23 | - * @psalm-suppress MixedMethodCall |
|
24 | - */ |
|
25 | - public static function fe_cswap( |
|
26 | - ParagonIE_Sodium_Core32_Curve25519_Fe $f, |
|
27 | - ParagonIE_Sodium_Core32_Curve25519_Fe $g, |
|
28 | - $b = 0 |
|
29 | - ) { |
|
30 | - $f0 = (int) $f[0]->toInt(); |
|
31 | - $f1 = (int) $f[1]->toInt(); |
|
32 | - $f2 = (int) $f[2]->toInt(); |
|
33 | - $f3 = (int) $f[3]->toInt(); |
|
34 | - $f4 = (int) $f[4]->toInt(); |
|
35 | - $f5 = (int) $f[5]->toInt(); |
|
36 | - $f6 = (int) $f[6]->toInt(); |
|
37 | - $f7 = (int) $f[7]->toInt(); |
|
38 | - $f8 = (int) $f[8]->toInt(); |
|
39 | - $f9 = (int) $f[9]->toInt(); |
|
40 | - $g0 = (int) $g[0]->toInt(); |
|
41 | - $g1 = (int) $g[1]->toInt(); |
|
42 | - $g2 = (int) $g[2]->toInt(); |
|
43 | - $g3 = (int) $g[3]->toInt(); |
|
44 | - $g4 = (int) $g[4]->toInt(); |
|
45 | - $g5 = (int) $g[5]->toInt(); |
|
46 | - $g6 = (int) $g[6]->toInt(); |
|
47 | - $g7 = (int) $g[7]->toInt(); |
|
48 | - $g8 = (int) $g[8]->toInt(); |
|
49 | - $g9 = (int) $g[9]->toInt(); |
|
50 | - $b = -$b; |
|
51 | - /** @var int $x0 */ |
|
52 | - $x0 = ($f0 ^ $g0) & $b; |
|
53 | - /** @var int $x1 */ |
|
54 | - $x1 = ($f1 ^ $g1) & $b; |
|
55 | - /** @var int $x2 */ |
|
56 | - $x2 = ($f2 ^ $g2) & $b; |
|
57 | - /** @var int $x3 */ |
|
58 | - $x3 = ($f3 ^ $g3) & $b; |
|
59 | - /** @var int $x4 */ |
|
60 | - $x4 = ($f4 ^ $g4) & $b; |
|
61 | - /** @var int $x5 */ |
|
62 | - $x5 = ($f5 ^ $g5) & $b; |
|
63 | - /** @var int $x6 */ |
|
64 | - $x6 = ($f6 ^ $g6) & $b; |
|
65 | - /** @var int $x7 */ |
|
66 | - $x7 = ($f7 ^ $g7) & $b; |
|
67 | - /** @var int $x8 */ |
|
68 | - $x8 = ($f8 ^ $g8) & $b; |
|
69 | - /** @var int $x9 */ |
|
70 | - $x9 = ($f9 ^ $g9) & $b; |
|
71 | - $f[0] = ParagonIE_Sodium_Core32_Int32::fromInt($f0 ^ $x0); |
|
72 | - $f[1] = ParagonIE_Sodium_Core32_Int32::fromInt($f1 ^ $x1); |
|
73 | - $f[2] = ParagonIE_Sodium_Core32_Int32::fromInt($f2 ^ $x2); |
|
74 | - $f[3] = ParagonIE_Sodium_Core32_Int32::fromInt($f3 ^ $x3); |
|
75 | - $f[4] = ParagonIE_Sodium_Core32_Int32::fromInt($f4 ^ $x4); |
|
76 | - $f[5] = ParagonIE_Sodium_Core32_Int32::fromInt($f5 ^ $x5); |
|
77 | - $f[6] = ParagonIE_Sodium_Core32_Int32::fromInt($f6 ^ $x6); |
|
78 | - $f[7] = ParagonIE_Sodium_Core32_Int32::fromInt($f7 ^ $x7); |
|
79 | - $f[8] = ParagonIE_Sodium_Core32_Int32::fromInt($f8 ^ $x8); |
|
80 | - $f[9] = ParagonIE_Sodium_Core32_Int32::fromInt($f9 ^ $x9); |
|
81 | - $g[0] = ParagonIE_Sodium_Core32_Int32::fromInt($g0 ^ $x0); |
|
82 | - $g[1] = ParagonIE_Sodium_Core32_Int32::fromInt($g1 ^ $x1); |
|
83 | - $g[2] = ParagonIE_Sodium_Core32_Int32::fromInt($g2 ^ $x2); |
|
84 | - $g[3] = ParagonIE_Sodium_Core32_Int32::fromInt($g3 ^ $x3); |
|
85 | - $g[4] = ParagonIE_Sodium_Core32_Int32::fromInt($g4 ^ $x4); |
|
86 | - $g[5] = ParagonIE_Sodium_Core32_Int32::fromInt($g5 ^ $x5); |
|
87 | - $g[6] = ParagonIE_Sodium_Core32_Int32::fromInt($g6 ^ $x6); |
|
88 | - $g[7] = ParagonIE_Sodium_Core32_Int32::fromInt($g7 ^ $x7); |
|
89 | - $g[8] = ParagonIE_Sodium_Core32_Int32::fromInt($g8 ^ $x8); |
|
90 | - $g[9] = ParagonIE_Sodium_Core32_Int32::fromInt($g9 ^ $x9); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @internal You should not use this directly from another application |
|
95 | - * |
|
96 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f |
|
97 | - * @return ParagonIE_Sodium_Core32_Curve25519_Fe |
|
98 | - * @throws SodiumException |
|
99 | - * @throws TypeError |
|
100 | - * @psalm-suppress MixedAssignment |
|
101 | - * @psalm-suppress MixedMethodCall |
|
102 | - */ |
|
103 | - public static function fe_mul121666(ParagonIE_Sodium_Core32_Curve25519_Fe $f) |
|
104 | - { |
|
105 | - /** @var array<int, ParagonIE_Sodium_Core32_Int64> $h */ |
|
106 | - $h = array(); |
|
107 | - for ($i = 0; $i < 10; ++$i) { |
|
108 | - $h[$i] = $f[$i]->toInt64()->mulInt(121666, 17); |
|
109 | - } |
|
110 | - |
|
111 | - $carry9 = $h[9]->addInt(1 << 24)->shiftRight(25); |
|
112 | - $h[0] = $h[0]->addInt64($carry9->mulInt(19, 5)); |
|
113 | - $h[9] = $h[9]->subInt64($carry9->shiftLeft(25)); |
|
114 | - |
|
115 | - $carry1 = $h[1]->addInt(1 << 24)->shiftRight(25); |
|
116 | - $h[2] = $h[2]->addInt64($carry1); |
|
117 | - $h[1] = $h[1]->subInt64($carry1->shiftLeft(25)); |
|
118 | - |
|
119 | - $carry3 = $h[3]->addInt(1 << 24)->shiftRight(25); |
|
120 | - $h[4] = $h[4]->addInt64($carry3); |
|
121 | - $h[3] = $h[3]->subInt64($carry3->shiftLeft(25)); |
|
122 | - |
|
123 | - $carry5 = $h[5]->addInt(1 << 24)->shiftRight(25); |
|
124 | - $h[6] = $h[6]->addInt64($carry5); |
|
125 | - $h[5] = $h[5]->subInt64($carry5->shiftLeft(25)); |
|
126 | - |
|
127 | - $carry7 = $h[7]->addInt(1 << 24)->shiftRight(25); |
|
128 | - $h[8] = $h[8]->addInt64($carry7); |
|
129 | - $h[7] = $h[7]->subInt64($carry7->shiftLeft(25)); |
|
130 | - |
|
131 | - $carry0 = $h[0]->addInt(1 << 25)->shiftRight(26); |
|
132 | - $h[1] = $h[1]->addInt64($carry0); |
|
133 | - $h[0] = $h[0]->subInt64($carry0->shiftLeft(26)); |
|
134 | - |
|
135 | - $carry2 = $h[2]->addInt(1 << 25)->shiftRight(26); |
|
136 | - $h[3] = $h[3]->addInt64($carry2); |
|
137 | - $h[2] = $h[2]->subInt64($carry2->shiftLeft(26)); |
|
138 | - |
|
139 | - $carry4 = $h[4]->addInt(1 << 25)->shiftRight(26); |
|
140 | - $h[5] = $h[5]->addInt64($carry4); |
|
141 | - $h[4] = $h[4]->subInt64($carry4->shiftLeft(26)); |
|
142 | - |
|
143 | - $carry6 = $h[6]->addInt(1 << 25)->shiftRight(26); |
|
144 | - $h[7] = $h[7]->addInt64($carry6); |
|
145 | - $h[6] = $h[6]->subInt64($carry6->shiftLeft(26)); |
|
146 | - |
|
147 | - $carry8 = $h[8]->addInt(1 << 25)->shiftRight(26); |
|
148 | - $h[9] = $h[9]->addInt64($carry8); |
|
149 | - $h[8] = $h[8]->subInt64($carry8->shiftLeft(26)); |
|
150 | - |
|
151 | - for ($i = 0; $i < 10; ++$i) { |
|
152 | - $h[$i] = $h[$i]->toInt32(); |
|
153 | - } |
|
154 | - /** @var array<int, ParagonIE_Sodium_Core32_Int32> $h2 */ |
|
155 | - $h2 = $h; |
|
156 | - return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray($h2); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * @internal You should not use this directly from another application |
|
161 | - * |
|
162 | - * Inline comments preceded by # are from libsodium's ref10 code. |
|
163 | - * |
|
164 | - * @param string $n |
|
165 | - * @param string $p |
|
166 | - * @return string |
|
167 | - * @throws SodiumException |
|
168 | - * @throws TypeError |
|
169 | - */ |
|
170 | - public static function crypto_scalarmult_curve25519_ref10($n, $p) |
|
171 | - { |
|
172 | - # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
173 | - $e = '' . $n; |
|
174 | - # e[0] &= 248; |
|
175 | - $e[0] = self::intToChr( |
|
176 | - self::chrToInt($e[0]) & 248 |
|
177 | - ); |
|
178 | - # e[31] &= 127; |
|
179 | - # e[31] |= 64; |
|
180 | - $e[31] = self::intToChr( |
|
181 | - (self::chrToInt($e[31]) & 127) | 64 |
|
182 | - ); |
|
183 | - # fe_frombytes(x1,p); |
|
184 | - $x1 = self::fe_frombytes($p); |
|
185 | - # fe_1(x2); |
|
186 | - $x2 = self::fe_1(); |
|
187 | - # fe_0(z2); |
|
188 | - $z2 = self::fe_0(); |
|
189 | - # fe_copy(x3,x1); |
|
190 | - $x3 = self::fe_copy($x1); |
|
191 | - # fe_1(z3); |
|
192 | - $z3 = self::fe_1(); |
|
193 | - |
|
194 | - # swap = 0; |
|
195 | - /** @var int $swap */ |
|
196 | - $swap = 0; |
|
197 | - |
|
198 | - # for (pos = 254;pos >= 0;--pos) { |
|
199 | - for ($pos = 254; $pos >= 0; --$pos) { |
|
200 | - # b = e[pos / 8] >> (pos & 7); |
|
201 | - /** @var int $b */ |
|
202 | - $b = self::chrToInt( |
|
203 | - $e[(int) floor($pos / 8)] |
|
204 | - ) >> ($pos & 7); |
|
205 | - # b &= 1; |
|
206 | - $b &= 1; |
|
207 | - |
|
208 | - # swap ^= b; |
|
209 | - $swap ^= $b; |
|
210 | - |
|
211 | - # fe_cswap(x2,x3,swap); |
|
212 | - self::fe_cswap($x2, $x3, $swap); |
|
213 | - |
|
214 | - # fe_cswap(z2,z3,swap); |
|
215 | - self::fe_cswap($z2, $z3, $swap); |
|
216 | - |
|
217 | - # swap = b; |
|
218 | - /** @var int $swap */ |
|
219 | - $swap = $b; |
|
220 | - |
|
221 | - # fe_sub(tmp0,x3,z3); |
|
222 | - $tmp0 = self::fe_sub($x3, $z3); |
|
223 | - |
|
224 | - # fe_sub(tmp1,x2,z2); |
|
225 | - $tmp1 = self::fe_sub($x2, $z2); |
|
226 | - |
|
227 | - # fe_add(x2,x2,z2); |
|
228 | - $x2 = self::fe_add($x2, $z2); |
|
229 | - |
|
230 | - # fe_add(z2,x3,z3); |
|
231 | - $z2 = self::fe_add($x3, $z3); |
|
232 | - |
|
233 | - # fe_mul(z3,tmp0,x2); |
|
234 | - $z3 = self::fe_mul($tmp0, $x2); |
|
235 | - |
|
236 | - # fe_mul(z2,z2,tmp1); |
|
237 | - $z2 = self::fe_mul($z2, $tmp1); |
|
238 | - |
|
239 | - # fe_sq(tmp0,tmp1); |
|
240 | - $tmp0 = self::fe_sq($tmp1); |
|
241 | - |
|
242 | - # fe_sq(tmp1,x2); |
|
243 | - $tmp1 = self::fe_sq($x2); |
|
244 | - |
|
245 | - # fe_add(x3,z3,z2); |
|
246 | - $x3 = self::fe_add($z3, $z2); |
|
247 | - |
|
248 | - # fe_sub(z2,z3,z2); |
|
249 | - $z2 = self::fe_sub($z3, $z2); |
|
250 | - |
|
251 | - # fe_mul(x2,tmp1,tmp0); |
|
252 | - $x2 = self::fe_mul($tmp1, $tmp0); |
|
253 | - |
|
254 | - # fe_sub(tmp1,tmp1,tmp0); |
|
255 | - $tmp1 = self::fe_sub($tmp1, $tmp0); |
|
256 | - |
|
257 | - # fe_sq(z2,z2); |
|
258 | - $z2 = self::fe_sq($z2); |
|
259 | - |
|
260 | - # fe_mul121666(z3,tmp1); |
|
261 | - $z3 = self::fe_mul121666($tmp1); |
|
262 | - |
|
263 | - # fe_sq(x3,x3); |
|
264 | - $x3 = self::fe_sq($x3); |
|
265 | - |
|
266 | - # fe_add(tmp0,tmp0,z3); |
|
267 | - $tmp0 = self::fe_add($tmp0, $z3); |
|
268 | - |
|
269 | - # fe_mul(z3,x1,z2); |
|
270 | - $z3 = self::fe_mul($x1, $z2); |
|
271 | - |
|
272 | - # fe_mul(z2,tmp1,tmp0); |
|
273 | - $z2 = self::fe_mul($tmp1, $tmp0); |
|
274 | - } |
|
275 | - |
|
276 | - # fe_cswap(x2,x3,swap); |
|
277 | - self::fe_cswap($x2, $x3, $swap); |
|
278 | - |
|
279 | - # fe_cswap(z2,z3,swap); |
|
280 | - self::fe_cswap($z2, $z3, $swap); |
|
281 | - |
|
282 | - # fe_invert(z2,z2); |
|
283 | - $z2 = self::fe_invert($z2); |
|
284 | - |
|
285 | - # fe_mul(x2,x2,z2); |
|
286 | - $x2 = self::fe_mul($x2, $z2); |
|
287 | - # fe_tobytes(q,x2); |
|
288 | - return (string) self::fe_tobytes($x2); |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * @internal You should not use this directly from another application |
|
293 | - * |
|
294 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY |
|
295 | - * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ |
|
296 | - * @return ParagonIE_Sodium_Core32_Curve25519_Fe |
|
297 | - * @throws SodiumException |
|
298 | - * @throws TypeError |
|
299 | - */ |
|
300 | - public static function edwards_to_montgomery( |
|
301 | - ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY, |
|
302 | - ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ |
|
303 | - ) { |
|
304 | - $tempX = self::fe_add($edwardsZ, $edwardsY); |
|
305 | - $tempZ = self::fe_sub($edwardsZ, $edwardsY); |
|
306 | - $tempZ = self::fe_invert($tempZ); |
|
307 | - return self::fe_mul($tempX, $tempZ); |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * @internal You should not use this directly from another application |
|
312 | - * |
|
313 | - * @param string $n |
|
314 | - * @return string |
|
315 | - * @throws SodiumException |
|
316 | - * @throws TypeError |
|
317 | - */ |
|
318 | - public static function crypto_scalarmult_curve25519_ref10_base($n) |
|
319 | - { |
|
320 | - # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
321 | - $e = '' . $n; |
|
322 | - |
|
323 | - # e[0] &= 248; |
|
324 | - $e[0] = self::intToChr( |
|
325 | - self::chrToInt($e[0]) & 248 |
|
326 | - ); |
|
327 | - |
|
328 | - # e[31] &= 127; |
|
329 | - # e[31] |= 64; |
|
330 | - $e[31] = self::intToChr( |
|
331 | - (self::chrToInt($e[31]) & 127) | 64 |
|
332 | - ); |
|
333 | - |
|
334 | - $A = self::ge_scalarmult_base($e); |
|
335 | - if ( |
|
336 | - !($A->Y instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
337 | - || |
|
338 | - !($A->Z instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
339 | - ) { |
|
340 | - throw new TypeError('Null points encountered'); |
|
341 | - } |
|
342 | - $pk = self::edwards_to_montgomery($A->Y, $A->Z); |
|
343 | - return self::fe_tobytes($pk); |
|
344 | - } |
|
12 | + /** |
|
13 | + * Alters the objects passed to this method in place. |
|
14 | + * |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f |
|
18 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $g |
|
19 | + * @param int $b |
|
20 | + * @return void |
|
21 | + * @throws SodiumException |
|
22 | + * @throws TypeError |
|
23 | + * @psalm-suppress MixedMethodCall |
|
24 | + */ |
|
25 | + public static function fe_cswap( |
|
26 | + ParagonIE_Sodium_Core32_Curve25519_Fe $f, |
|
27 | + ParagonIE_Sodium_Core32_Curve25519_Fe $g, |
|
28 | + $b = 0 |
|
29 | + ) { |
|
30 | + $f0 = (int) $f[0]->toInt(); |
|
31 | + $f1 = (int) $f[1]->toInt(); |
|
32 | + $f2 = (int) $f[2]->toInt(); |
|
33 | + $f3 = (int) $f[3]->toInt(); |
|
34 | + $f4 = (int) $f[4]->toInt(); |
|
35 | + $f5 = (int) $f[5]->toInt(); |
|
36 | + $f6 = (int) $f[6]->toInt(); |
|
37 | + $f7 = (int) $f[7]->toInt(); |
|
38 | + $f8 = (int) $f[8]->toInt(); |
|
39 | + $f9 = (int) $f[9]->toInt(); |
|
40 | + $g0 = (int) $g[0]->toInt(); |
|
41 | + $g1 = (int) $g[1]->toInt(); |
|
42 | + $g2 = (int) $g[2]->toInt(); |
|
43 | + $g3 = (int) $g[3]->toInt(); |
|
44 | + $g4 = (int) $g[4]->toInt(); |
|
45 | + $g5 = (int) $g[5]->toInt(); |
|
46 | + $g6 = (int) $g[6]->toInt(); |
|
47 | + $g7 = (int) $g[7]->toInt(); |
|
48 | + $g8 = (int) $g[8]->toInt(); |
|
49 | + $g9 = (int) $g[9]->toInt(); |
|
50 | + $b = -$b; |
|
51 | + /** @var int $x0 */ |
|
52 | + $x0 = ($f0 ^ $g0) & $b; |
|
53 | + /** @var int $x1 */ |
|
54 | + $x1 = ($f1 ^ $g1) & $b; |
|
55 | + /** @var int $x2 */ |
|
56 | + $x2 = ($f2 ^ $g2) & $b; |
|
57 | + /** @var int $x3 */ |
|
58 | + $x3 = ($f3 ^ $g3) & $b; |
|
59 | + /** @var int $x4 */ |
|
60 | + $x4 = ($f4 ^ $g4) & $b; |
|
61 | + /** @var int $x5 */ |
|
62 | + $x5 = ($f5 ^ $g5) & $b; |
|
63 | + /** @var int $x6 */ |
|
64 | + $x6 = ($f6 ^ $g6) & $b; |
|
65 | + /** @var int $x7 */ |
|
66 | + $x7 = ($f7 ^ $g7) & $b; |
|
67 | + /** @var int $x8 */ |
|
68 | + $x8 = ($f8 ^ $g8) & $b; |
|
69 | + /** @var int $x9 */ |
|
70 | + $x9 = ($f9 ^ $g9) & $b; |
|
71 | + $f[0] = ParagonIE_Sodium_Core32_Int32::fromInt($f0 ^ $x0); |
|
72 | + $f[1] = ParagonIE_Sodium_Core32_Int32::fromInt($f1 ^ $x1); |
|
73 | + $f[2] = ParagonIE_Sodium_Core32_Int32::fromInt($f2 ^ $x2); |
|
74 | + $f[3] = ParagonIE_Sodium_Core32_Int32::fromInt($f3 ^ $x3); |
|
75 | + $f[4] = ParagonIE_Sodium_Core32_Int32::fromInt($f4 ^ $x4); |
|
76 | + $f[5] = ParagonIE_Sodium_Core32_Int32::fromInt($f5 ^ $x5); |
|
77 | + $f[6] = ParagonIE_Sodium_Core32_Int32::fromInt($f6 ^ $x6); |
|
78 | + $f[7] = ParagonIE_Sodium_Core32_Int32::fromInt($f7 ^ $x7); |
|
79 | + $f[8] = ParagonIE_Sodium_Core32_Int32::fromInt($f8 ^ $x8); |
|
80 | + $f[9] = ParagonIE_Sodium_Core32_Int32::fromInt($f9 ^ $x9); |
|
81 | + $g[0] = ParagonIE_Sodium_Core32_Int32::fromInt($g0 ^ $x0); |
|
82 | + $g[1] = ParagonIE_Sodium_Core32_Int32::fromInt($g1 ^ $x1); |
|
83 | + $g[2] = ParagonIE_Sodium_Core32_Int32::fromInt($g2 ^ $x2); |
|
84 | + $g[3] = ParagonIE_Sodium_Core32_Int32::fromInt($g3 ^ $x3); |
|
85 | + $g[4] = ParagonIE_Sodium_Core32_Int32::fromInt($g4 ^ $x4); |
|
86 | + $g[5] = ParagonIE_Sodium_Core32_Int32::fromInt($g5 ^ $x5); |
|
87 | + $g[6] = ParagonIE_Sodium_Core32_Int32::fromInt($g6 ^ $x6); |
|
88 | + $g[7] = ParagonIE_Sodium_Core32_Int32::fromInt($g7 ^ $x7); |
|
89 | + $g[8] = ParagonIE_Sodium_Core32_Int32::fromInt($g8 ^ $x8); |
|
90 | + $g[9] = ParagonIE_Sodium_Core32_Int32::fromInt($g9 ^ $x9); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @internal You should not use this directly from another application |
|
95 | + * |
|
96 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $f |
|
97 | + * @return ParagonIE_Sodium_Core32_Curve25519_Fe |
|
98 | + * @throws SodiumException |
|
99 | + * @throws TypeError |
|
100 | + * @psalm-suppress MixedAssignment |
|
101 | + * @psalm-suppress MixedMethodCall |
|
102 | + */ |
|
103 | + public static function fe_mul121666(ParagonIE_Sodium_Core32_Curve25519_Fe $f) |
|
104 | + { |
|
105 | + /** @var array<int, ParagonIE_Sodium_Core32_Int64> $h */ |
|
106 | + $h = array(); |
|
107 | + for ($i = 0; $i < 10; ++$i) { |
|
108 | + $h[$i] = $f[$i]->toInt64()->mulInt(121666, 17); |
|
109 | + } |
|
110 | + |
|
111 | + $carry9 = $h[9]->addInt(1 << 24)->shiftRight(25); |
|
112 | + $h[0] = $h[0]->addInt64($carry9->mulInt(19, 5)); |
|
113 | + $h[9] = $h[9]->subInt64($carry9->shiftLeft(25)); |
|
114 | + |
|
115 | + $carry1 = $h[1]->addInt(1 << 24)->shiftRight(25); |
|
116 | + $h[2] = $h[2]->addInt64($carry1); |
|
117 | + $h[1] = $h[1]->subInt64($carry1->shiftLeft(25)); |
|
118 | + |
|
119 | + $carry3 = $h[3]->addInt(1 << 24)->shiftRight(25); |
|
120 | + $h[4] = $h[4]->addInt64($carry3); |
|
121 | + $h[3] = $h[3]->subInt64($carry3->shiftLeft(25)); |
|
122 | + |
|
123 | + $carry5 = $h[5]->addInt(1 << 24)->shiftRight(25); |
|
124 | + $h[6] = $h[6]->addInt64($carry5); |
|
125 | + $h[5] = $h[5]->subInt64($carry5->shiftLeft(25)); |
|
126 | + |
|
127 | + $carry7 = $h[7]->addInt(1 << 24)->shiftRight(25); |
|
128 | + $h[8] = $h[8]->addInt64($carry7); |
|
129 | + $h[7] = $h[7]->subInt64($carry7->shiftLeft(25)); |
|
130 | + |
|
131 | + $carry0 = $h[0]->addInt(1 << 25)->shiftRight(26); |
|
132 | + $h[1] = $h[1]->addInt64($carry0); |
|
133 | + $h[0] = $h[0]->subInt64($carry0->shiftLeft(26)); |
|
134 | + |
|
135 | + $carry2 = $h[2]->addInt(1 << 25)->shiftRight(26); |
|
136 | + $h[3] = $h[3]->addInt64($carry2); |
|
137 | + $h[2] = $h[2]->subInt64($carry2->shiftLeft(26)); |
|
138 | + |
|
139 | + $carry4 = $h[4]->addInt(1 << 25)->shiftRight(26); |
|
140 | + $h[5] = $h[5]->addInt64($carry4); |
|
141 | + $h[4] = $h[4]->subInt64($carry4->shiftLeft(26)); |
|
142 | + |
|
143 | + $carry6 = $h[6]->addInt(1 << 25)->shiftRight(26); |
|
144 | + $h[7] = $h[7]->addInt64($carry6); |
|
145 | + $h[6] = $h[6]->subInt64($carry6->shiftLeft(26)); |
|
146 | + |
|
147 | + $carry8 = $h[8]->addInt(1 << 25)->shiftRight(26); |
|
148 | + $h[9] = $h[9]->addInt64($carry8); |
|
149 | + $h[8] = $h[8]->subInt64($carry8->shiftLeft(26)); |
|
150 | + |
|
151 | + for ($i = 0; $i < 10; ++$i) { |
|
152 | + $h[$i] = $h[$i]->toInt32(); |
|
153 | + } |
|
154 | + /** @var array<int, ParagonIE_Sodium_Core32_Int32> $h2 */ |
|
155 | + $h2 = $h; |
|
156 | + return ParagonIE_Sodium_Core32_Curve25519_Fe::fromArray($h2); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * @internal You should not use this directly from another application |
|
161 | + * |
|
162 | + * Inline comments preceded by # are from libsodium's ref10 code. |
|
163 | + * |
|
164 | + * @param string $n |
|
165 | + * @param string $p |
|
166 | + * @return string |
|
167 | + * @throws SodiumException |
|
168 | + * @throws TypeError |
|
169 | + */ |
|
170 | + public static function crypto_scalarmult_curve25519_ref10($n, $p) |
|
171 | + { |
|
172 | + # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
173 | + $e = '' . $n; |
|
174 | + # e[0] &= 248; |
|
175 | + $e[0] = self::intToChr( |
|
176 | + self::chrToInt($e[0]) & 248 |
|
177 | + ); |
|
178 | + # e[31] &= 127; |
|
179 | + # e[31] |= 64; |
|
180 | + $e[31] = self::intToChr( |
|
181 | + (self::chrToInt($e[31]) & 127) | 64 |
|
182 | + ); |
|
183 | + # fe_frombytes(x1,p); |
|
184 | + $x1 = self::fe_frombytes($p); |
|
185 | + # fe_1(x2); |
|
186 | + $x2 = self::fe_1(); |
|
187 | + # fe_0(z2); |
|
188 | + $z2 = self::fe_0(); |
|
189 | + # fe_copy(x3,x1); |
|
190 | + $x3 = self::fe_copy($x1); |
|
191 | + # fe_1(z3); |
|
192 | + $z3 = self::fe_1(); |
|
193 | + |
|
194 | + # swap = 0; |
|
195 | + /** @var int $swap */ |
|
196 | + $swap = 0; |
|
197 | + |
|
198 | + # for (pos = 254;pos >= 0;--pos) { |
|
199 | + for ($pos = 254; $pos >= 0; --$pos) { |
|
200 | + # b = e[pos / 8] >> (pos & 7); |
|
201 | + /** @var int $b */ |
|
202 | + $b = self::chrToInt( |
|
203 | + $e[(int) floor($pos / 8)] |
|
204 | + ) >> ($pos & 7); |
|
205 | + # b &= 1; |
|
206 | + $b &= 1; |
|
207 | + |
|
208 | + # swap ^= b; |
|
209 | + $swap ^= $b; |
|
210 | + |
|
211 | + # fe_cswap(x2,x3,swap); |
|
212 | + self::fe_cswap($x2, $x3, $swap); |
|
213 | + |
|
214 | + # fe_cswap(z2,z3,swap); |
|
215 | + self::fe_cswap($z2, $z3, $swap); |
|
216 | + |
|
217 | + # swap = b; |
|
218 | + /** @var int $swap */ |
|
219 | + $swap = $b; |
|
220 | + |
|
221 | + # fe_sub(tmp0,x3,z3); |
|
222 | + $tmp0 = self::fe_sub($x3, $z3); |
|
223 | + |
|
224 | + # fe_sub(tmp1,x2,z2); |
|
225 | + $tmp1 = self::fe_sub($x2, $z2); |
|
226 | + |
|
227 | + # fe_add(x2,x2,z2); |
|
228 | + $x2 = self::fe_add($x2, $z2); |
|
229 | + |
|
230 | + # fe_add(z2,x3,z3); |
|
231 | + $z2 = self::fe_add($x3, $z3); |
|
232 | + |
|
233 | + # fe_mul(z3,tmp0,x2); |
|
234 | + $z3 = self::fe_mul($tmp0, $x2); |
|
235 | + |
|
236 | + # fe_mul(z2,z2,tmp1); |
|
237 | + $z2 = self::fe_mul($z2, $tmp1); |
|
238 | + |
|
239 | + # fe_sq(tmp0,tmp1); |
|
240 | + $tmp0 = self::fe_sq($tmp1); |
|
241 | + |
|
242 | + # fe_sq(tmp1,x2); |
|
243 | + $tmp1 = self::fe_sq($x2); |
|
244 | + |
|
245 | + # fe_add(x3,z3,z2); |
|
246 | + $x3 = self::fe_add($z3, $z2); |
|
247 | + |
|
248 | + # fe_sub(z2,z3,z2); |
|
249 | + $z2 = self::fe_sub($z3, $z2); |
|
250 | + |
|
251 | + # fe_mul(x2,tmp1,tmp0); |
|
252 | + $x2 = self::fe_mul($tmp1, $tmp0); |
|
253 | + |
|
254 | + # fe_sub(tmp1,tmp1,tmp0); |
|
255 | + $tmp1 = self::fe_sub($tmp1, $tmp0); |
|
256 | + |
|
257 | + # fe_sq(z2,z2); |
|
258 | + $z2 = self::fe_sq($z2); |
|
259 | + |
|
260 | + # fe_mul121666(z3,tmp1); |
|
261 | + $z3 = self::fe_mul121666($tmp1); |
|
262 | + |
|
263 | + # fe_sq(x3,x3); |
|
264 | + $x3 = self::fe_sq($x3); |
|
265 | + |
|
266 | + # fe_add(tmp0,tmp0,z3); |
|
267 | + $tmp0 = self::fe_add($tmp0, $z3); |
|
268 | + |
|
269 | + # fe_mul(z3,x1,z2); |
|
270 | + $z3 = self::fe_mul($x1, $z2); |
|
271 | + |
|
272 | + # fe_mul(z2,tmp1,tmp0); |
|
273 | + $z2 = self::fe_mul($tmp1, $tmp0); |
|
274 | + } |
|
275 | + |
|
276 | + # fe_cswap(x2,x3,swap); |
|
277 | + self::fe_cswap($x2, $x3, $swap); |
|
278 | + |
|
279 | + # fe_cswap(z2,z3,swap); |
|
280 | + self::fe_cswap($z2, $z3, $swap); |
|
281 | + |
|
282 | + # fe_invert(z2,z2); |
|
283 | + $z2 = self::fe_invert($z2); |
|
284 | + |
|
285 | + # fe_mul(x2,x2,z2); |
|
286 | + $x2 = self::fe_mul($x2, $z2); |
|
287 | + # fe_tobytes(q,x2); |
|
288 | + return (string) self::fe_tobytes($x2); |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * @internal You should not use this directly from another application |
|
293 | + * |
|
294 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY |
|
295 | + * @param ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ |
|
296 | + * @return ParagonIE_Sodium_Core32_Curve25519_Fe |
|
297 | + * @throws SodiumException |
|
298 | + * @throws TypeError |
|
299 | + */ |
|
300 | + public static function edwards_to_montgomery( |
|
301 | + ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsY, |
|
302 | + ParagonIE_Sodium_Core32_Curve25519_Fe $edwardsZ |
|
303 | + ) { |
|
304 | + $tempX = self::fe_add($edwardsZ, $edwardsY); |
|
305 | + $tempZ = self::fe_sub($edwardsZ, $edwardsY); |
|
306 | + $tempZ = self::fe_invert($tempZ); |
|
307 | + return self::fe_mul($tempX, $tempZ); |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * @internal You should not use this directly from another application |
|
312 | + * |
|
313 | + * @param string $n |
|
314 | + * @return string |
|
315 | + * @throws SodiumException |
|
316 | + * @throws TypeError |
|
317 | + */ |
|
318 | + public static function crypto_scalarmult_curve25519_ref10_base($n) |
|
319 | + { |
|
320 | + # for (i = 0;i < 32;++i) e[i] = n[i]; |
|
321 | + $e = '' . $n; |
|
322 | + |
|
323 | + # e[0] &= 248; |
|
324 | + $e[0] = self::intToChr( |
|
325 | + self::chrToInt($e[0]) & 248 |
|
326 | + ); |
|
327 | + |
|
328 | + # e[31] &= 127; |
|
329 | + # e[31] |= 64; |
|
330 | + $e[31] = self::intToChr( |
|
331 | + (self::chrToInt($e[31]) & 127) | 64 |
|
332 | + ); |
|
333 | + |
|
334 | + $A = self::ge_scalarmult_base($e); |
|
335 | + if ( |
|
336 | + !($A->Y instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
337 | + || |
|
338 | + !($A->Z instanceof ParagonIE_Sodium_Core32_Curve25519_Fe) |
|
339 | + ) { |
|
340 | + throw new TypeError('Null points encountered'); |
|
341 | + } |
|
342 | + $pk = self::edwards_to_montgomery($A->Y, $A->Z); |
|
343 | + return self::fe_tobytes($pk); |
|
344 | + } |
|
345 | 345 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_Poly1305', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,55 +9,55 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core32_Poly1305 extends ParagonIE_Sodium_Core32_Util |
11 | 11 | { |
12 | - const BLOCK_SIZE = 16; |
|
12 | + const BLOCK_SIZE = 16; |
|
13 | 13 | |
14 | - /** |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param string $m |
|
18 | - * @param string $key |
|
19 | - * @return string |
|
20 | - * @throws SodiumException |
|
21 | - * @throws TypeError |
|
22 | - */ |
|
23 | - public static function onetimeauth($m, $key) |
|
24 | - { |
|
25 | - if (self::strlen($key) < 32) { |
|
26 | - throw new InvalidArgumentException( |
|
27 | - 'Key must be 32 bytes long.' |
|
28 | - ); |
|
29 | - } |
|
30 | - $state = new ParagonIE_Sodium_Core32_Poly1305_State( |
|
31 | - self::substr($key, 0, 32) |
|
32 | - ); |
|
33 | - return $state |
|
34 | - ->update($m) |
|
35 | - ->finish(); |
|
36 | - } |
|
14 | + /** |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param string $m |
|
18 | + * @param string $key |
|
19 | + * @return string |
|
20 | + * @throws SodiumException |
|
21 | + * @throws TypeError |
|
22 | + */ |
|
23 | + public static function onetimeauth($m, $key) |
|
24 | + { |
|
25 | + if (self::strlen($key) < 32) { |
|
26 | + throw new InvalidArgumentException( |
|
27 | + 'Key must be 32 bytes long.' |
|
28 | + ); |
|
29 | + } |
|
30 | + $state = new ParagonIE_Sodium_Core32_Poly1305_State( |
|
31 | + self::substr($key, 0, 32) |
|
32 | + ); |
|
33 | + return $state |
|
34 | + ->update($m) |
|
35 | + ->finish(); |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * @internal You should not use this directly from another application |
|
40 | - * |
|
41 | - * @param string $mac |
|
42 | - * @param string $m |
|
43 | - * @param string $key |
|
44 | - * @return bool |
|
45 | - * @throws SodiumException |
|
46 | - * @throws TypeError |
|
47 | - */ |
|
48 | - public static function onetimeauth_verify($mac, $m, $key) |
|
49 | - { |
|
50 | - if (self::strlen($key) < 32) { |
|
51 | - throw new InvalidArgumentException( |
|
52 | - 'Key must be 32 bytes long.' |
|
53 | - ); |
|
54 | - } |
|
55 | - $state = new ParagonIE_Sodium_Core32_Poly1305_State( |
|
56 | - self::substr($key, 0, 32) |
|
57 | - ); |
|
58 | - $calc = $state |
|
59 | - ->update($m) |
|
60 | - ->finish(); |
|
61 | - return self::verify_16($calc, $mac); |
|
62 | - } |
|
38 | + /** |
|
39 | + * @internal You should not use this directly from another application |
|
40 | + * |
|
41 | + * @param string $mac |
|
42 | + * @param string $m |
|
43 | + * @param string $key |
|
44 | + * @return bool |
|
45 | + * @throws SodiumException |
|
46 | + * @throws TypeError |
|
47 | + */ |
|
48 | + public static function onetimeauth_verify($mac, $m, $key) |
|
49 | + { |
|
50 | + if (self::strlen($key) < 32) { |
|
51 | + throw new InvalidArgumentException( |
|
52 | + 'Key must be 32 bytes long.' |
|
53 | + ); |
|
54 | + } |
|
55 | + $state = new ParagonIE_Sodium_Core32_Poly1305_State( |
|
56 | + self::substr($key, 0, 32) |
|
57 | + ); |
|
58 | + $calc = $state |
|
59 | + ->update($m) |
|
60 | + ->finish(); |
|
61 | + return self::verify_16($calc, $mac); |
|
62 | + } |
|
63 | 63 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_XSalsa20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,49 +9,49 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core32_XSalsa20 extends ParagonIE_Sodium_Core32_HSalsa20 |
11 | 11 | { |
12 | - /** |
|
13 | - * Expand a key and nonce into an xsalsa20 keystream. |
|
14 | - * |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param int $len |
|
18 | - * @param string $nonce |
|
19 | - * @param string $key |
|
20 | - * @return string |
|
21 | - * @throws SodiumException |
|
22 | - * @throws TypeError |
|
23 | - */ |
|
24 | - public static function xsalsa20($len, $nonce, $key) |
|
25 | - { |
|
26 | - $ret = self::salsa20( |
|
27 | - $len, |
|
28 | - self::substr($nonce, 16, 8), |
|
29 | - self::hsalsa20($nonce, $key) |
|
30 | - ); |
|
31 | - return $ret; |
|
32 | - } |
|
12 | + /** |
|
13 | + * Expand a key and nonce into an xsalsa20 keystream. |
|
14 | + * |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param int $len |
|
18 | + * @param string $nonce |
|
19 | + * @param string $key |
|
20 | + * @return string |
|
21 | + * @throws SodiumException |
|
22 | + * @throws TypeError |
|
23 | + */ |
|
24 | + public static function xsalsa20($len, $nonce, $key) |
|
25 | + { |
|
26 | + $ret = self::salsa20( |
|
27 | + $len, |
|
28 | + self::substr($nonce, 16, 8), |
|
29 | + self::hsalsa20($nonce, $key) |
|
30 | + ); |
|
31 | + return $ret; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Encrypt a string with XSalsa20. Doesn't provide integrity. |
|
36 | - * |
|
37 | - * @internal You should not use this directly from another application |
|
38 | - * |
|
39 | - * @param string $message |
|
40 | - * @param string $nonce |
|
41 | - * @param string $key |
|
42 | - * @return string |
|
43 | - * @throws SodiumException |
|
44 | - * @throws TypeError |
|
45 | - */ |
|
46 | - public static function xsalsa20_xor($message, $nonce, $key) |
|
47 | - { |
|
48 | - return self::xorStrings( |
|
49 | - $message, |
|
50 | - self::xsalsa20( |
|
51 | - self::strlen($message), |
|
52 | - $nonce, |
|
53 | - $key |
|
54 | - ) |
|
55 | - ); |
|
56 | - } |
|
34 | + /** |
|
35 | + * Encrypt a string with XSalsa20. Doesn't provide integrity. |
|
36 | + * |
|
37 | + * @internal You should not use this directly from another application |
|
38 | + * |
|
39 | + * @param string $message |
|
40 | + * @param string $nonce |
|
41 | + * @param string $key |
|
42 | + * @return string |
|
43 | + * @throws SodiumException |
|
44 | + * @throws TypeError |
|
45 | + */ |
|
46 | + public static function xsalsa20_xor($message, $nonce, $key) |
|
47 | + { |
|
48 | + return self::xorStrings( |
|
49 | + $message, |
|
50 | + self::xsalsa20( |
|
51 | + self::strlen($message), |
|
52 | + $nonce, |
|
53 | + $key |
|
54 | + ) |
|
55 | + ); |
|
56 | + } |
|
57 | 57 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_ChaCha20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,83 +9,83 @@ discard block |
||
9 | 9 | */ |
10 | 10 | class ParagonIE_Sodium_Core32_ChaCha20 extends ParagonIE_Sodium_Core32_Util |
11 | 11 | { |
12 | - /** |
|
13 | - * The ChaCha20 quarter round function. Works on four 32-bit integers. |
|
14 | - * |
|
15 | - * @internal You should not use this directly from another application |
|
16 | - * |
|
17 | - * @param ParagonIE_Sodium_Core32_Int32 $a |
|
18 | - * @param ParagonIE_Sodium_Core32_Int32 $b |
|
19 | - * @param ParagonIE_Sodium_Core32_Int32 $c |
|
20 | - * @param ParagonIE_Sodium_Core32_Int32 $d |
|
21 | - * @return array<int, ParagonIE_Sodium_Core32_Int32> |
|
22 | - * @throws SodiumException |
|
23 | - * @throws TypeError |
|
24 | - */ |
|
25 | - protected static function quarterRound( |
|
26 | - ParagonIE_Sodium_Core32_Int32 $a, |
|
27 | - ParagonIE_Sodium_Core32_Int32 $b, |
|
28 | - ParagonIE_Sodium_Core32_Int32 $c, |
|
29 | - ParagonIE_Sodium_Core32_Int32 $d |
|
30 | - ) { |
|
31 | - /** @var ParagonIE_Sodium_Core32_Int32 $a */ |
|
32 | - /** @var ParagonIE_Sodium_Core32_Int32 $b */ |
|
33 | - /** @var ParagonIE_Sodium_Core32_Int32 $c */ |
|
34 | - /** @var ParagonIE_Sodium_Core32_Int32 $d */ |
|
12 | + /** |
|
13 | + * The ChaCha20 quarter round function. Works on four 32-bit integers. |
|
14 | + * |
|
15 | + * @internal You should not use this directly from another application |
|
16 | + * |
|
17 | + * @param ParagonIE_Sodium_Core32_Int32 $a |
|
18 | + * @param ParagonIE_Sodium_Core32_Int32 $b |
|
19 | + * @param ParagonIE_Sodium_Core32_Int32 $c |
|
20 | + * @param ParagonIE_Sodium_Core32_Int32 $d |
|
21 | + * @return array<int, ParagonIE_Sodium_Core32_Int32> |
|
22 | + * @throws SodiumException |
|
23 | + * @throws TypeError |
|
24 | + */ |
|
25 | + protected static function quarterRound( |
|
26 | + ParagonIE_Sodium_Core32_Int32 $a, |
|
27 | + ParagonIE_Sodium_Core32_Int32 $b, |
|
28 | + ParagonIE_Sodium_Core32_Int32 $c, |
|
29 | + ParagonIE_Sodium_Core32_Int32 $d |
|
30 | + ) { |
|
31 | + /** @var ParagonIE_Sodium_Core32_Int32 $a */ |
|
32 | + /** @var ParagonIE_Sodium_Core32_Int32 $b */ |
|
33 | + /** @var ParagonIE_Sodium_Core32_Int32 $c */ |
|
34 | + /** @var ParagonIE_Sodium_Core32_Int32 $d */ |
|
35 | 35 | |
36 | - # a = PLUS(a,b); d = ROTATE(XOR(d,a),16); |
|
37 | - $a = $a->addInt32($b); |
|
38 | - $d = $d->xorInt32($a)->rotateLeft(16); |
|
36 | + # a = PLUS(a,b); d = ROTATE(XOR(d,a),16); |
|
37 | + $a = $a->addInt32($b); |
|
38 | + $d = $d->xorInt32($a)->rotateLeft(16); |
|
39 | 39 | |
40 | - # c = PLUS(c,d); b = ROTATE(XOR(b,c),12); |
|
41 | - $c = $c->addInt32($d); |
|
42 | - $b = $b->xorInt32($c)->rotateLeft(12); |
|
40 | + # c = PLUS(c,d); b = ROTATE(XOR(b,c),12); |
|
41 | + $c = $c->addInt32($d); |
|
42 | + $b = $b->xorInt32($c)->rotateLeft(12); |
|
43 | 43 | |
44 | - # a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); |
|
45 | - $a = $a->addInt32($b); |
|
46 | - $d = $d->xorInt32($a)->rotateLeft(8); |
|
44 | + # a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); |
|
45 | + $a = $a->addInt32($b); |
|
46 | + $d = $d->xorInt32($a)->rotateLeft(8); |
|
47 | 47 | |
48 | - # c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); |
|
49 | - $c = $c->addInt32($d); |
|
50 | - $b = $b->xorInt32($c)->rotateLeft(7); |
|
48 | + # c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); |
|
49 | + $c = $c->addInt32($d); |
|
50 | + $b = $b->xorInt32($c)->rotateLeft(7); |
|
51 | 51 | |
52 | - return array($a, $b, $c, $d); |
|
53 | - } |
|
52 | + return array($a, $b, $c, $d); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @internal You should not use this directly from another application |
|
57 | - * |
|
58 | - * @param ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx |
|
59 | - * @param string $message |
|
60 | - * |
|
61 | - * @return string |
|
62 | - * @throws SodiumException |
|
63 | - * @throws TypeError |
|
64 | - */ |
|
65 | - public static function encryptBytes( |
|
66 | - ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx, |
|
67 | - $message = '' |
|
68 | - ) { |
|
69 | - $bytes = self::strlen($message); |
|
55 | + /** |
|
56 | + * @internal You should not use this directly from another application |
|
57 | + * |
|
58 | + * @param ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx |
|
59 | + * @param string $message |
|
60 | + * |
|
61 | + * @return string |
|
62 | + * @throws SodiumException |
|
63 | + * @throws TypeError |
|
64 | + */ |
|
65 | + public static function encryptBytes( |
|
66 | + ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx, |
|
67 | + $message = '' |
|
68 | + ) { |
|
69 | + $bytes = self::strlen($message); |
|
70 | 70 | |
71 | - /** @var ParagonIE_Sodium_Core32_Int32 $x0 */ |
|
72 | - /** @var ParagonIE_Sodium_Core32_Int32 $x1 */ |
|
73 | - /** @var ParagonIE_Sodium_Core32_Int32 $x2 */ |
|
74 | - /** @var ParagonIE_Sodium_Core32_Int32 $x3 */ |
|
75 | - /** @var ParagonIE_Sodium_Core32_Int32 $x4 */ |
|
76 | - /** @var ParagonIE_Sodium_Core32_Int32 $x5 */ |
|
77 | - /** @var ParagonIE_Sodium_Core32_Int32 $x6 */ |
|
78 | - /** @var ParagonIE_Sodium_Core32_Int32 $x7 */ |
|
79 | - /** @var ParagonIE_Sodium_Core32_Int32 $x8 */ |
|
80 | - /** @var ParagonIE_Sodium_Core32_Int32 $x9 */ |
|
81 | - /** @var ParagonIE_Sodium_Core32_Int32 $x10 */ |
|
82 | - /** @var ParagonIE_Sodium_Core32_Int32 $x11 */ |
|
83 | - /** @var ParagonIE_Sodium_Core32_Int32 $x12 */ |
|
84 | - /** @var ParagonIE_Sodium_Core32_Int32 $x13 */ |
|
85 | - /** @var ParagonIE_Sodium_Core32_Int32 $x14 */ |
|
86 | - /** @var ParagonIE_Sodium_Core32_Int32 $x15 */ |
|
71 | + /** @var ParagonIE_Sodium_Core32_Int32 $x0 */ |
|
72 | + /** @var ParagonIE_Sodium_Core32_Int32 $x1 */ |
|
73 | + /** @var ParagonIE_Sodium_Core32_Int32 $x2 */ |
|
74 | + /** @var ParagonIE_Sodium_Core32_Int32 $x3 */ |
|
75 | + /** @var ParagonIE_Sodium_Core32_Int32 $x4 */ |
|
76 | + /** @var ParagonIE_Sodium_Core32_Int32 $x5 */ |
|
77 | + /** @var ParagonIE_Sodium_Core32_Int32 $x6 */ |
|
78 | + /** @var ParagonIE_Sodium_Core32_Int32 $x7 */ |
|
79 | + /** @var ParagonIE_Sodium_Core32_Int32 $x8 */ |
|
80 | + /** @var ParagonIE_Sodium_Core32_Int32 $x9 */ |
|
81 | + /** @var ParagonIE_Sodium_Core32_Int32 $x10 */ |
|
82 | + /** @var ParagonIE_Sodium_Core32_Int32 $x11 */ |
|
83 | + /** @var ParagonIE_Sodium_Core32_Int32 $x12 */ |
|
84 | + /** @var ParagonIE_Sodium_Core32_Int32 $x13 */ |
|
85 | + /** @var ParagonIE_Sodium_Core32_Int32 $x14 */ |
|
86 | + /** @var ParagonIE_Sodium_Core32_Int32 $x15 */ |
|
87 | 87 | |
88 | - /* |
|
88 | + /* |
|
89 | 89 | j0 = ctx->input[0]; |
90 | 90 | j1 = ctx->input[1]; |
91 | 91 | j2 = ctx->input[2]; |
@@ -103,89 +103,89 @@ discard block |
||
103 | 103 | j14 = ctx->input[14]; |
104 | 104 | j15 = ctx->input[15]; |
105 | 105 | */ |
106 | - /** @var ParagonIE_Sodium_Core32_Int32 $j0 */ |
|
107 | - $j0 = $ctx[0]; |
|
108 | - /** @var ParagonIE_Sodium_Core32_Int32 $j1 */ |
|
109 | - $j1 = $ctx[1]; |
|
110 | - /** @var ParagonIE_Sodium_Core32_Int32 $j2 */ |
|
111 | - $j2 = $ctx[2]; |
|
112 | - /** @var ParagonIE_Sodium_Core32_Int32 $j3 */ |
|
113 | - $j3 = $ctx[3]; |
|
114 | - /** @var ParagonIE_Sodium_Core32_Int32 $j4 */ |
|
115 | - $j4 = $ctx[4]; |
|
116 | - /** @var ParagonIE_Sodium_Core32_Int32 $j5 */ |
|
117 | - $j5 = $ctx[5]; |
|
118 | - /** @var ParagonIE_Sodium_Core32_Int32 $j6 */ |
|
119 | - $j6 = $ctx[6]; |
|
120 | - /** @var ParagonIE_Sodium_Core32_Int32 $j7 */ |
|
121 | - $j7 = $ctx[7]; |
|
122 | - /** @var ParagonIE_Sodium_Core32_Int32 $j8 */ |
|
123 | - $j8 = $ctx[8]; |
|
124 | - /** @var ParagonIE_Sodium_Core32_Int32 $j9 */ |
|
125 | - $j9 = $ctx[9]; |
|
126 | - /** @var ParagonIE_Sodium_Core32_Int32 $j10 */ |
|
127 | - $j10 = $ctx[10]; |
|
128 | - /** @var ParagonIE_Sodium_Core32_Int32 $j11 */ |
|
129 | - $j11 = $ctx[11]; |
|
130 | - /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ |
|
131 | - $j12 = $ctx[12]; |
|
132 | - /** @var ParagonIE_Sodium_Core32_Int32 $j13 */ |
|
133 | - $j13 = $ctx[13]; |
|
134 | - /** @var ParagonIE_Sodium_Core32_Int32 $j14 */ |
|
135 | - $j14 = $ctx[14]; |
|
136 | - /** @var ParagonIE_Sodium_Core32_Int32 $j15 */ |
|
137 | - $j15 = $ctx[15]; |
|
106 | + /** @var ParagonIE_Sodium_Core32_Int32 $j0 */ |
|
107 | + $j0 = $ctx[0]; |
|
108 | + /** @var ParagonIE_Sodium_Core32_Int32 $j1 */ |
|
109 | + $j1 = $ctx[1]; |
|
110 | + /** @var ParagonIE_Sodium_Core32_Int32 $j2 */ |
|
111 | + $j2 = $ctx[2]; |
|
112 | + /** @var ParagonIE_Sodium_Core32_Int32 $j3 */ |
|
113 | + $j3 = $ctx[3]; |
|
114 | + /** @var ParagonIE_Sodium_Core32_Int32 $j4 */ |
|
115 | + $j4 = $ctx[4]; |
|
116 | + /** @var ParagonIE_Sodium_Core32_Int32 $j5 */ |
|
117 | + $j5 = $ctx[5]; |
|
118 | + /** @var ParagonIE_Sodium_Core32_Int32 $j6 */ |
|
119 | + $j6 = $ctx[6]; |
|
120 | + /** @var ParagonIE_Sodium_Core32_Int32 $j7 */ |
|
121 | + $j7 = $ctx[7]; |
|
122 | + /** @var ParagonIE_Sodium_Core32_Int32 $j8 */ |
|
123 | + $j8 = $ctx[8]; |
|
124 | + /** @var ParagonIE_Sodium_Core32_Int32 $j9 */ |
|
125 | + $j9 = $ctx[9]; |
|
126 | + /** @var ParagonIE_Sodium_Core32_Int32 $j10 */ |
|
127 | + $j10 = $ctx[10]; |
|
128 | + /** @var ParagonIE_Sodium_Core32_Int32 $j11 */ |
|
129 | + $j11 = $ctx[11]; |
|
130 | + /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ |
|
131 | + $j12 = $ctx[12]; |
|
132 | + /** @var ParagonIE_Sodium_Core32_Int32 $j13 */ |
|
133 | + $j13 = $ctx[13]; |
|
134 | + /** @var ParagonIE_Sodium_Core32_Int32 $j14 */ |
|
135 | + $j14 = $ctx[14]; |
|
136 | + /** @var ParagonIE_Sodium_Core32_Int32 $j15 */ |
|
137 | + $j15 = $ctx[15]; |
|
138 | 138 | |
139 | - $c = ''; |
|
140 | - for (;;) { |
|
141 | - if ($bytes < 64) { |
|
142 | - $message .= str_repeat("\x00", 64 - $bytes); |
|
143 | - } |
|
139 | + $c = ''; |
|
140 | + for (;;) { |
|
141 | + if ($bytes < 64) { |
|
142 | + $message .= str_repeat("\x00", 64 - $bytes); |
|
143 | + } |
|
144 | 144 | |
145 | - $x0 = clone $j0; |
|
146 | - $x1 = clone $j1; |
|
147 | - $x2 = clone $j2; |
|
148 | - $x3 = clone $j3; |
|
149 | - $x4 = clone $j4; |
|
150 | - $x5 = clone $j5; |
|
151 | - $x6 = clone $j6; |
|
152 | - $x7 = clone $j7; |
|
153 | - $x8 = clone $j8; |
|
154 | - $x9 = clone $j9; |
|
155 | - $x10 = clone $j10; |
|
156 | - $x11 = clone $j11; |
|
157 | - $x12 = clone $j12; |
|
158 | - $x13 = clone $j13; |
|
159 | - $x14 = clone $j14; |
|
160 | - $x15 = clone $j15; |
|
145 | + $x0 = clone $j0; |
|
146 | + $x1 = clone $j1; |
|
147 | + $x2 = clone $j2; |
|
148 | + $x3 = clone $j3; |
|
149 | + $x4 = clone $j4; |
|
150 | + $x5 = clone $j5; |
|
151 | + $x6 = clone $j6; |
|
152 | + $x7 = clone $j7; |
|
153 | + $x8 = clone $j8; |
|
154 | + $x9 = clone $j9; |
|
155 | + $x10 = clone $j10; |
|
156 | + $x11 = clone $j11; |
|
157 | + $x12 = clone $j12; |
|
158 | + $x13 = clone $j13; |
|
159 | + $x14 = clone $j14; |
|
160 | + $x15 = clone $j15; |
|
161 | 161 | |
162 | - # for (i = 20; i > 0; i -= 2) { |
|
163 | - for ($i = 20; $i > 0; $i -= 2) { |
|
164 | - # QUARTERROUND( x0, x4, x8, x12) |
|
165 | - list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
162 | + # for (i = 20; i > 0; i -= 2) { |
|
163 | + for ($i = 20; $i > 0; $i -= 2) { |
|
164 | + # QUARTERROUND( x0, x4, x8, x12) |
|
165 | + list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
166 | 166 | |
167 | - # QUARTERROUND( x1, x5, x9, x13) |
|
168 | - list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
167 | + # QUARTERROUND( x1, x5, x9, x13) |
|
168 | + list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
169 | 169 | |
170 | - # QUARTERROUND( x2, x6, x10, x14) |
|
171 | - list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
170 | + # QUARTERROUND( x2, x6, x10, x14) |
|
171 | + list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
172 | 172 | |
173 | - # QUARTERROUND( x3, x7, x11, x15) |
|
174 | - list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
173 | + # QUARTERROUND( x3, x7, x11, x15) |
|
174 | + list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
175 | 175 | |
176 | - # QUARTERROUND( x0, x5, x10, x15) |
|
177 | - list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
176 | + # QUARTERROUND( x0, x5, x10, x15) |
|
177 | + list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
178 | 178 | |
179 | - # QUARTERROUND( x1, x6, x11, x12) |
|
180 | - list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
179 | + # QUARTERROUND( x1, x6, x11, x12) |
|
180 | + list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
181 | 181 | |
182 | - # QUARTERROUND( x2, x7, x8, x13) |
|
183 | - list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
182 | + # QUARTERROUND( x2, x7, x8, x13) |
|
183 | + list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
184 | 184 | |
185 | - # QUARTERROUND( x3, x4, x9, x14) |
|
186 | - list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
187 | - } |
|
188 | - /* |
|
185 | + # QUARTERROUND( x3, x4, x9, x14) |
|
186 | + list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
187 | + } |
|
188 | + /* |
|
189 | 189 | x0 = PLUS(x0, j0); |
190 | 190 | x1 = PLUS(x1, j1); |
191 | 191 | x2 = PLUS(x2, j2); |
@@ -203,24 +203,24 @@ discard block |
||
203 | 203 | x14 = PLUS(x14, j14); |
204 | 204 | x15 = PLUS(x15, j15); |
205 | 205 | */ |
206 | - $x0 = $x0->addInt32($j0); |
|
207 | - $x1 = $x1->addInt32($j1); |
|
208 | - $x2 = $x2->addInt32($j2); |
|
209 | - $x3 = $x3->addInt32($j3); |
|
210 | - $x4 = $x4->addInt32($j4); |
|
211 | - $x5 = $x5->addInt32($j5); |
|
212 | - $x6 = $x6->addInt32($j6); |
|
213 | - $x7 = $x7->addInt32($j7); |
|
214 | - $x8 = $x8->addInt32($j8); |
|
215 | - $x9 = $x9->addInt32($j9); |
|
216 | - $x10 = $x10->addInt32($j10); |
|
217 | - $x11 = $x11->addInt32($j11); |
|
218 | - $x12 = $x12->addInt32($j12); |
|
219 | - $x13 = $x13->addInt32($j13); |
|
220 | - $x14 = $x14->addInt32($j14); |
|
221 | - $x15 = $x15->addInt32($j15); |
|
206 | + $x0 = $x0->addInt32($j0); |
|
207 | + $x1 = $x1->addInt32($j1); |
|
208 | + $x2 = $x2->addInt32($j2); |
|
209 | + $x3 = $x3->addInt32($j3); |
|
210 | + $x4 = $x4->addInt32($j4); |
|
211 | + $x5 = $x5->addInt32($j5); |
|
212 | + $x6 = $x6->addInt32($j6); |
|
213 | + $x7 = $x7->addInt32($j7); |
|
214 | + $x8 = $x8->addInt32($j8); |
|
215 | + $x9 = $x9->addInt32($j9); |
|
216 | + $x10 = $x10->addInt32($j10); |
|
217 | + $x11 = $x11->addInt32($j11); |
|
218 | + $x12 = $x12->addInt32($j12); |
|
219 | + $x13 = $x13->addInt32($j13); |
|
220 | + $x14 = $x14->addInt32($j14); |
|
221 | + $x15 = $x15->addInt32($j15); |
|
222 | 222 | |
223 | - /* |
|
223 | + /* |
|
224 | 224 | x0 = XOR(x0, LOAD32_LE(m + 0)); |
225 | 225 | x1 = XOR(x1, LOAD32_LE(m + 4)); |
226 | 226 | x2 = XOR(x2, LOAD32_LE(m + 8)); |
@@ -238,36 +238,36 @@ discard block |
||
238 | 238 | x14 = XOR(x14, LOAD32_LE(m + 56)); |
239 | 239 | x15 = XOR(x15, LOAD32_LE(m + 60)); |
240 | 240 | */ |
241 | - $x0 = $x0->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 0, 4))); |
|
242 | - $x1 = $x1->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 4, 4))); |
|
243 | - $x2 = $x2->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 8, 4))); |
|
244 | - $x3 = $x3->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 12, 4))); |
|
245 | - $x4 = $x4->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 16, 4))); |
|
246 | - $x5 = $x5->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 20, 4))); |
|
247 | - $x6 = $x6->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 24, 4))); |
|
248 | - $x7 = $x7->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 28, 4))); |
|
249 | - $x8 = $x8->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 32, 4))); |
|
250 | - $x9 = $x9->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 36, 4))); |
|
251 | - $x10 = $x10->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 40, 4))); |
|
252 | - $x11 = $x11->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 44, 4))); |
|
253 | - $x12 = $x12->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 48, 4))); |
|
254 | - $x13 = $x13->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 52, 4))); |
|
255 | - $x14 = $x14->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 56, 4))); |
|
256 | - $x15 = $x15->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 60, 4))); |
|
241 | + $x0 = $x0->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 0, 4))); |
|
242 | + $x1 = $x1->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 4, 4))); |
|
243 | + $x2 = $x2->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 8, 4))); |
|
244 | + $x3 = $x3->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 12, 4))); |
|
245 | + $x4 = $x4->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 16, 4))); |
|
246 | + $x5 = $x5->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 20, 4))); |
|
247 | + $x6 = $x6->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 24, 4))); |
|
248 | + $x7 = $x7->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 28, 4))); |
|
249 | + $x8 = $x8->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 32, 4))); |
|
250 | + $x9 = $x9->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 36, 4))); |
|
251 | + $x10 = $x10->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 40, 4))); |
|
252 | + $x11 = $x11->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 44, 4))); |
|
253 | + $x12 = $x12->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 48, 4))); |
|
254 | + $x13 = $x13->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 52, 4))); |
|
255 | + $x14 = $x14->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 56, 4))); |
|
256 | + $x15 = $x15->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 60, 4))); |
|
257 | 257 | |
258 | - /* |
|
258 | + /* |
|
259 | 259 | j12 = PLUSONE(j12); |
260 | 260 | if (!j12) { |
261 | 261 | j13 = PLUSONE(j13); |
262 | 262 | } |
263 | 263 | */ |
264 | - /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ |
|
265 | - $j12 = $j12->addInt(1); |
|
266 | - if ($j12->limbs[0] === 0 && $j12->limbs[1] === 0) { |
|
267 | - $j13 = $j13->addInt(1); |
|
268 | - } |
|
264 | + /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ |
|
265 | + $j12 = $j12->addInt(1); |
|
266 | + if ($j12->limbs[0] === 0 && $j12->limbs[1] === 0) { |
|
267 | + $j13 = $j13->addInt(1); |
|
268 | + } |
|
269 | 269 | |
270 | - /* |
|
270 | + /* |
|
271 | 271 | STORE32_LE(c + 0, x0); |
272 | 272 | STORE32_LE(c + 4, x1); |
273 | 273 | STORE32_LE(c + 8, x2); |
@@ -286,115 +286,115 @@ discard block |
||
286 | 286 | STORE32_LE(c + 60, x15); |
287 | 287 | */ |
288 | 288 | |
289 | - $block = $x0->toReverseString() . |
|
290 | - $x1->toReverseString() . |
|
291 | - $x2->toReverseString() . |
|
292 | - $x3->toReverseString() . |
|
293 | - $x4->toReverseString() . |
|
294 | - $x5->toReverseString() . |
|
295 | - $x6->toReverseString() . |
|
296 | - $x7->toReverseString() . |
|
297 | - $x8->toReverseString() . |
|
298 | - $x9->toReverseString() . |
|
299 | - $x10->toReverseString() . |
|
300 | - $x11->toReverseString() . |
|
301 | - $x12->toReverseString() . |
|
302 | - $x13->toReverseString() . |
|
303 | - $x14->toReverseString() . |
|
304 | - $x15->toReverseString(); |
|
289 | + $block = $x0->toReverseString() . |
|
290 | + $x1->toReverseString() . |
|
291 | + $x2->toReverseString() . |
|
292 | + $x3->toReverseString() . |
|
293 | + $x4->toReverseString() . |
|
294 | + $x5->toReverseString() . |
|
295 | + $x6->toReverseString() . |
|
296 | + $x7->toReverseString() . |
|
297 | + $x8->toReverseString() . |
|
298 | + $x9->toReverseString() . |
|
299 | + $x10->toReverseString() . |
|
300 | + $x11->toReverseString() . |
|
301 | + $x12->toReverseString() . |
|
302 | + $x13->toReverseString() . |
|
303 | + $x14->toReverseString() . |
|
304 | + $x15->toReverseString(); |
|
305 | 305 | |
306 | - /* Partial block */ |
|
307 | - if ($bytes < 64) { |
|
308 | - $c .= self::substr($block, 0, $bytes); |
|
309 | - break; |
|
310 | - } |
|
306 | + /* Partial block */ |
|
307 | + if ($bytes < 64) { |
|
308 | + $c .= self::substr($block, 0, $bytes); |
|
309 | + break; |
|
310 | + } |
|
311 | 311 | |
312 | - /* Full block */ |
|
313 | - $c .= $block; |
|
314 | - $bytes -= 64; |
|
315 | - if ($bytes <= 0) { |
|
316 | - break; |
|
317 | - } |
|
318 | - $message = self::substr($message, 64); |
|
319 | - } |
|
320 | - /* end for(;;) loop */ |
|
312 | + /* Full block */ |
|
313 | + $c .= $block; |
|
314 | + $bytes -= 64; |
|
315 | + if ($bytes <= 0) { |
|
316 | + break; |
|
317 | + } |
|
318 | + $message = self::substr($message, 64); |
|
319 | + } |
|
320 | + /* end for(;;) loop */ |
|
321 | 321 | |
322 | - $ctx[12] = $j12; |
|
323 | - $ctx[13] = $j13; |
|
324 | - return $c; |
|
325 | - } |
|
322 | + $ctx[12] = $j12; |
|
323 | + $ctx[13] = $j13; |
|
324 | + return $c; |
|
325 | + } |
|
326 | 326 | |
327 | - /** |
|
328 | - * @internal You should not use this directly from another application |
|
329 | - * |
|
330 | - * @param int $len |
|
331 | - * @param string $nonce |
|
332 | - * @param string $key |
|
333 | - * @return string |
|
334 | - * @throws SodiumException |
|
335 | - * @throws TypeError |
|
336 | - */ |
|
337 | - public static function stream($len = 64, $nonce = '', $key = '') |
|
338 | - { |
|
339 | - return self::encryptBytes( |
|
340 | - new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce), |
|
341 | - str_repeat("\x00", $len) |
|
342 | - ); |
|
343 | - } |
|
327 | + /** |
|
328 | + * @internal You should not use this directly from another application |
|
329 | + * |
|
330 | + * @param int $len |
|
331 | + * @param string $nonce |
|
332 | + * @param string $key |
|
333 | + * @return string |
|
334 | + * @throws SodiumException |
|
335 | + * @throws TypeError |
|
336 | + */ |
|
337 | + public static function stream($len = 64, $nonce = '', $key = '') |
|
338 | + { |
|
339 | + return self::encryptBytes( |
|
340 | + new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce), |
|
341 | + str_repeat("\x00", $len) |
|
342 | + ); |
|
343 | + } |
|
344 | 344 | |
345 | - /** |
|
346 | - * @internal You should not use this directly from another application |
|
347 | - * |
|
348 | - * @param int $len |
|
349 | - * @param string $nonce |
|
350 | - * @param string $key |
|
351 | - * @return string |
|
352 | - * @throws SodiumException |
|
353 | - * @throws TypeError |
|
354 | - */ |
|
355 | - public static function ietfStream($len, $nonce = '', $key = '') |
|
356 | - { |
|
357 | - return self::encryptBytes( |
|
358 | - new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce), |
|
359 | - str_repeat("\x00", $len) |
|
360 | - ); |
|
361 | - } |
|
345 | + /** |
|
346 | + * @internal You should not use this directly from another application |
|
347 | + * |
|
348 | + * @param int $len |
|
349 | + * @param string $nonce |
|
350 | + * @param string $key |
|
351 | + * @return string |
|
352 | + * @throws SodiumException |
|
353 | + * @throws TypeError |
|
354 | + */ |
|
355 | + public static function ietfStream($len, $nonce = '', $key = '') |
|
356 | + { |
|
357 | + return self::encryptBytes( |
|
358 | + new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce), |
|
359 | + str_repeat("\x00", $len) |
|
360 | + ); |
|
361 | + } |
|
362 | 362 | |
363 | - /** |
|
364 | - * @internal You should not use this directly from another application |
|
365 | - * |
|
366 | - * @param string $message |
|
367 | - * @param string $nonce |
|
368 | - * @param string $key |
|
369 | - * @param string $ic |
|
370 | - * @return string |
|
371 | - * @throws SodiumException |
|
372 | - * @throws TypeError |
|
373 | - */ |
|
374 | - public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
375 | - { |
|
376 | - return self::encryptBytes( |
|
377 | - new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce, $ic), |
|
378 | - $message |
|
379 | - ); |
|
380 | - } |
|
363 | + /** |
|
364 | + * @internal You should not use this directly from another application |
|
365 | + * |
|
366 | + * @param string $message |
|
367 | + * @param string $nonce |
|
368 | + * @param string $key |
|
369 | + * @param string $ic |
|
370 | + * @return string |
|
371 | + * @throws SodiumException |
|
372 | + * @throws TypeError |
|
373 | + */ |
|
374 | + public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
375 | + { |
|
376 | + return self::encryptBytes( |
|
377 | + new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce, $ic), |
|
378 | + $message |
|
379 | + ); |
|
380 | + } |
|
381 | 381 | |
382 | - /** |
|
383 | - * @internal You should not use this directly from another application |
|
384 | - * |
|
385 | - * @param string $message |
|
386 | - * @param string $nonce |
|
387 | - * @param string $key |
|
388 | - * @param string $ic |
|
389 | - * @return string |
|
390 | - * @throws SodiumException |
|
391 | - * @throws TypeError |
|
392 | - */ |
|
393 | - public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
394 | - { |
|
395 | - return self::encryptBytes( |
|
396 | - new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce, $ic), |
|
397 | - $message |
|
398 | - ); |
|
399 | - } |
|
382 | + /** |
|
383 | + * @internal You should not use this directly from another application |
|
384 | + * |
|
385 | + * @param string $message |
|
386 | + * @param string $nonce |
|
387 | + * @param string $key |
|
388 | + * @param string $ic |
|
389 | + * @return string |
|
390 | + * @throws SodiumException |
|
391 | + * @throws TypeError |
|
392 | + */ |
|
393 | + public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') |
|
394 | + { |
|
395 | + return self::encryptBytes( |
|
396 | + new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce, $ic), |
|
397 | + $message |
|
398 | + ); |
|
399 | + } |
|
400 | 400 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_HChaCha20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,119 +9,119 @@ discard block |
||
9 | 9 | */ |
10 | 10 | class ParagonIE_Sodium_Core32_HChaCha20 extends ParagonIE_Sodium_Core32_ChaCha20 |
11 | 11 | { |
12 | - /** |
|
13 | - * @param string $in |
|
14 | - * @param string $key |
|
15 | - * @param string|null $c |
|
16 | - * @return string |
|
17 | - * @throws SodiumException |
|
18 | - * @throws TypeError |
|
19 | - */ |
|
20 | - public static function hChaCha20($in = '', $key = '', $c = null) |
|
21 | - { |
|
22 | - $ctx = array(); |
|
12 | + /** |
|
13 | + * @param string $in |
|
14 | + * @param string $key |
|
15 | + * @param string|null $c |
|
16 | + * @return string |
|
17 | + * @throws SodiumException |
|
18 | + * @throws TypeError |
|
19 | + */ |
|
20 | + public static function hChaCha20($in = '', $key = '', $c = null) |
|
21 | + { |
|
22 | + $ctx = array(); |
|
23 | 23 | |
24 | - if ($c === null) { |
|
25 | - $ctx[0] = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); |
|
26 | - $ctx[1] = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); |
|
27 | - $ctx[2] = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); |
|
28 | - $ctx[3] = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); |
|
29 | - } else { |
|
30 | - $ctx[0] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4)); |
|
31 | - $ctx[1] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4)); |
|
32 | - $ctx[2] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4)); |
|
33 | - $ctx[3] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4)); |
|
34 | - } |
|
35 | - $ctx[4] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 0, 4)); |
|
36 | - $ctx[5] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 4, 4)); |
|
37 | - $ctx[6] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 8, 4)); |
|
38 | - $ctx[7] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 12, 4)); |
|
39 | - $ctx[8] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 16, 4)); |
|
40 | - $ctx[9] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 20, 4)); |
|
41 | - $ctx[10] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 24, 4)); |
|
42 | - $ctx[11] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 28, 4)); |
|
43 | - $ctx[12] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4)); |
|
44 | - $ctx[13] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4)); |
|
45 | - $ctx[14] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4)); |
|
46 | - $ctx[15] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4)); |
|
24 | + if ($c === null) { |
|
25 | + $ctx[0] = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); |
|
26 | + $ctx[1] = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); |
|
27 | + $ctx[2] = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); |
|
28 | + $ctx[3] = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); |
|
29 | + } else { |
|
30 | + $ctx[0] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4)); |
|
31 | + $ctx[1] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4)); |
|
32 | + $ctx[2] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4)); |
|
33 | + $ctx[3] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4)); |
|
34 | + } |
|
35 | + $ctx[4] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 0, 4)); |
|
36 | + $ctx[5] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 4, 4)); |
|
37 | + $ctx[6] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 8, 4)); |
|
38 | + $ctx[7] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 12, 4)); |
|
39 | + $ctx[8] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 16, 4)); |
|
40 | + $ctx[9] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 20, 4)); |
|
41 | + $ctx[10] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 24, 4)); |
|
42 | + $ctx[11] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($key, 28, 4)); |
|
43 | + $ctx[12] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4)); |
|
44 | + $ctx[13] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4)); |
|
45 | + $ctx[14] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4)); |
|
46 | + $ctx[15] = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4)); |
|
47 | 47 | |
48 | - return self::hChaCha20Bytes($ctx); |
|
49 | - } |
|
48 | + return self::hChaCha20Bytes($ctx); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @param array $ctx |
|
53 | - * @return string |
|
54 | - * @throws SodiumException |
|
55 | - * @throws TypeError |
|
56 | - */ |
|
57 | - protected static function hChaCha20Bytes(array $ctx) |
|
58 | - { |
|
59 | - /** @var ParagonIE_Sodium_Core32_Int32 $x0 */ |
|
60 | - $x0 = $ctx[0]; |
|
61 | - /** @var ParagonIE_Sodium_Core32_Int32 $x1 */ |
|
62 | - $x1 = $ctx[1]; |
|
63 | - /** @var ParagonIE_Sodium_Core32_Int32 $x2 */ |
|
64 | - $x2 = $ctx[2]; |
|
65 | - /** @var ParagonIE_Sodium_Core32_Int32 $x3 */ |
|
66 | - $x3 = $ctx[3]; |
|
67 | - /** @var ParagonIE_Sodium_Core32_Int32 $x4 */ |
|
68 | - $x4 = $ctx[4]; |
|
69 | - /** @var ParagonIE_Sodium_Core32_Int32 $x5 */ |
|
70 | - $x5 = $ctx[5]; |
|
71 | - /** @var ParagonIE_Sodium_Core32_Int32 $x6 */ |
|
72 | - $x6 = $ctx[6]; |
|
73 | - /** @var ParagonIE_Sodium_Core32_Int32 $x7 */ |
|
74 | - $x7 = $ctx[7]; |
|
75 | - /** @var ParagonIE_Sodium_Core32_Int32 $x8 */ |
|
76 | - $x8 = $ctx[8]; |
|
77 | - /** @var ParagonIE_Sodium_Core32_Int32 $x9 */ |
|
78 | - $x9 = $ctx[9]; |
|
79 | - /** @var ParagonIE_Sodium_Core32_Int32 $x10 */ |
|
80 | - $x10 = $ctx[10]; |
|
81 | - /** @var ParagonIE_Sodium_Core32_Int32 $x11 */ |
|
82 | - $x11 = $ctx[11]; |
|
83 | - /** @var ParagonIE_Sodium_Core32_Int32 $x12 */ |
|
84 | - $x12 = $ctx[12]; |
|
85 | - /** @var ParagonIE_Sodium_Core32_Int32 $x13 */ |
|
86 | - $x13 = $ctx[13]; |
|
87 | - /** @var ParagonIE_Sodium_Core32_Int32 $x14 */ |
|
88 | - $x14 = $ctx[14]; |
|
89 | - /** @var ParagonIE_Sodium_Core32_Int32 $x15 */ |
|
90 | - $x15 = $ctx[15]; |
|
51 | + /** |
|
52 | + * @param array $ctx |
|
53 | + * @return string |
|
54 | + * @throws SodiumException |
|
55 | + * @throws TypeError |
|
56 | + */ |
|
57 | + protected static function hChaCha20Bytes(array $ctx) |
|
58 | + { |
|
59 | + /** @var ParagonIE_Sodium_Core32_Int32 $x0 */ |
|
60 | + $x0 = $ctx[0]; |
|
61 | + /** @var ParagonIE_Sodium_Core32_Int32 $x1 */ |
|
62 | + $x1 = $ctx[1]; |
|
63 | + /** @var ParagonIE_Sodium_Core32_Int32 $x2 */ |
|
64 | + $x2 = $ctx[2]; |
|
65 | + /** @var ParagonIE_Sodium_Core32_Int32 $x3 */ |
|
66 | + $x3 = $ctx[3]; |
|
67 | + /** @var ParagonIE_Sodium_Core32_Int32 $x4 */ |
|
68 | + $x4 = $ctx[4]; |
|
69 | + /** @var ParagonIE_Sodium_Core32_Int32 $x5 */ |
|
70 | + $x5 = $ctx[5]; |
|
71 | + /** @var ParagonIE_Sodium_Core32_Int32 $x6 */ |
|
72 | + $x6 = $ctx[6]; |
|
73 | + /** @var ParagonIE_Sodium_Core32_Int32 $x7 */ |
|
74 | + $x7 = $ctx[7]; |
|
75 | + /** @var ParagonIE_Sodium_Core32_Int32 $x8 */ |
|
76 | + $x8 = $ctx[8]; |
|
77 | + /** @var ParagonIE_Sodium_Core32_Int32 $x9 */ |
|
78 | + $x9 = $ctx[9]; |
|
79 | + /** @var ParagonIE_Sodium_Core32_Int32 $x10 */ |
|
80 | + $x10 = $ctx[10]; |
|
81 | + /** @var ParagonIE_Sodium_Core32_Int32 $x11 */ |
|
82 | + $x11 = $ctx[11]; |
|
83 | + /** @var ParagonIE_Sodium_Core32_Int32 $x12 */ |
|
84 | + $x12 = $ctx[12]; |
|
85 | + /** @var ParagonIE_Sodium_Core32_Int32 $x13 */ |
|
86 | + $x13 = $ctx[13]; |
|
87 | + /** @var ParagonIE_Sodium_Core32_Int32 $x14 */ |
|
88 | + $x14 = $ctx[14]; |
|
89 | + /** @var ParagonIE_Sodium_Core32_Int32 $x15 */ |
|
90 | + $x15 = $ctx[15]; |
|
91 | 91 | |
92 | - for ($i = 0; $i < 10; ++$i) { |
|
93 | - # QUARTERROUND( x0, x4, x8, x12) |
|
94 | - list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
92 | + for ($i = 0; $i < 10; ++$i) { |
|
93 | + # QUARTERROUND( x0, x4, x8, x12) |
|
94 | + list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); |
|
95 | 95 | |
96 | - # QUARTERROUND( x1, x5, x9, x13) |
|
97 | - list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
96 | + # QUARTERROUND( x1, x5, x9, x13) |
|
97 | + list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); |
|
98 | 98 | |
99 | - # QUARTERROUND( x2, x6, x10, x14) |
|
100 | - list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
99 | + # QUARTERROUND( x2, x6, x10, x14) |
|
100 | + list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); |
|
101 | 101 | |
102 | - # QUARTERROUND( x3, x7, x11, x15) |
|
103 | - list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
102 | + # QUARTERROUND( x3, x7, x11, x15) |
|
103 | + list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); |
|
104 | 104 | |
105 | - # QUARTERROUND( x0, x5, x10, x15) |
|
106 | - list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
105 | + # QUARTERROUND( x0, x5, x10, x15) |
|
106 | + list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); |
|
107 | 107 | |
108 | - # QUARTERROUND( x1, x6, x11, x12) |
|
109 | - list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
108 | + # QUARTERROUND( x1, x6, x11, x12) |
|
109 | + list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); |
|
110 | 110 | |
111 | - # QUARTERROUND( x2, x7, x8, x13) |
|
112 | - list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
111 | + # QUARTERROUND( x2, x7, x8, x13) |
|
112 | + list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); |
|
113 | 113 | |
114 | - # QUARTERROUND( x3, x4, x9, x14) |
|
115 | - list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
116 | - } |
|
114 | + # QUARTERROUND( x3, x4, x9, x14) |
|
115 | + list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); |
|
116 | + } |
|
117 | 117 | |
118 | - return $x0->toReverseString() . |
|
119 | - $x1->toReverseString() . |
|
120 | - $x2->toReverseString() . |
|
121 | - $x3->toReverseString() . |
|
122 | - $x12->toReverseString() . |
|
123 | - $x13->toReverseString() . |
|
124 | - $x14->toReverseString() . |
|
125 | - $x15->toReverseString(); |
|
126 | - } |
|
118 | + return $x0->toReverseString() . |
|
119 | + $x1->toReverseString() . |
|
120 | + $x2->toReverseString() . |
|
121 | + $x3->toReverseString() . |
|
122 | + $x12->toReverseString() . |
|
123 | + $x13->toReverseString() . |
|
124 | + $x14->toReverseString() . |
|
125 | + $x15->toReverseString(); |
|
126 | + } |
|
127 | 127 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (class_exists('ParagonIE_Sodium_Core32_HSalsa20', false)) { |
4 | - return; |
|
4 | + return; |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -9,133 +9,133 @@ discard block |
||
9 | 9 | */ |
10 | 10 | abstract class ParagonIE_Sodium_Core32_HSalsa20 extends ParagonIE_Sodium_Core32_Salsa20 |
11 | 11 | { |
12 | - /** |
|
13 | - * Calculate an hsalsa20 hash of a single block |
|
14 | - * |
|
15 | - * HSalsa20 doesn't have a counter and will never be used for more than |
|
16 | - * one block (used to derive a subkey for xsalsa20). |
|
17 | - * |
|
18 | - * @internal You should not use this directly from another application |
|
19 | - * |
|
20 | - * @param string $in |
|
21 | - * @param string $k |
|
22 | - * @param string|null $c |
|
23 | - * @return string |
|
24 | - * @throws SodiumException |
|
25 | - * @throws TypeError |
|
26 | - */ |
|
27 | - public static function hsalsa20($in, $k, $c = null) |
|
28 | - { |
|
29 | - /** |
|
30 | - * @var ParagonIE_Sodium_Core32_Int32 $x0 |
|
31 | - * @var ParagonIE_Sodium_Core32_Int32 $x1 |
|
32 | - * @var ParagonIE_Sodium_Core32_Int32 $x2 |
|
33 | - * @var ParagonIE_Sodium_Core32_Int32 $x3 |
|
34 | - * @var ParagonIE_Sodium_Core32_Int32 $x4 |
|
35 | - * @var ParagonIE_Sodium_Core32_Int32 $x5 |
|
36 | - * @var ParagonIE_Sodium_Core32_Int32 $x6 |
|
37 | - * @var ParagonIE_Sodium_Core32_Int32 $x7 |
|
38 | - * @var ParagonIE_Sodium_Core32_Int32 $x8 |
|
39 | - * @var ParagonIE_Sodium_Core32_Int32 $x9 |
|
40 | - * @var ParagonIE_Sodium_Core32_Int32 $x10 |
|
41 | - * @var ParagonIE_Sodium_Core32_Int32 $x11 |
|
42 | - * @var ParagonIE_Sodium_Core32_Int32 $x12 |
|
43 | - * @var ParagonIE_Sodium_Core32_Int32 $x13 |
|
44 | - * @var ParagonIE_Sodium_Core32_Int32 $x14 |
|
45 | - * @var ParagonIE_Sodium_Core32_Int32 $x15 |
|
46 | - * @var ParagonIE_Sodium_Core32_Int32 $j0 |
|
47 | - * @var ParagonIE_Sodium_Core32_Int32 $j1 |
|
48 | - * @var ParagonIE_Sodium_Core32_Int32 $j2 |
|
49 | - * @var ParagonIE_Sodium_Core32_Int32 $j3 |
|
50 | - * @var ParagonIE_Sodium_Core32_Int32 $j4 |
|
51 | - * @var ParagonIE_Sodium_Core32_Int32 $j5 |
|
52 | - * @var ParagonIE_Sodium_Core32_Int32 $j6 |
|
53 | - * @var ParagonIE_Sodium_Core32_Int32 $j7 |
|
54 | - * @var ParagonIE_Sodium_Core32_Int32 $j8 |
|
55 | - * @var ParagonIE_Sodium_Core32_Int32 $j9 |
|
56 | - * @var ParagonIE_Sodium_Core32_Int32 $j10 |
|
57 | - * @var ParagonIE_Sodium_Core32_Int32 $j11 |
|
58 | - * @var ParagonIE_Sodium_Core32_Int32 $j12 |
|
59 | - * @var ParagonIE_Sodium_Core32_Int32 $j13 |
|
60 | - * @var ParagonIE_Sodium_Core32_Int32 $j14 |
|
61 | - * @var ParagonIE_Sodium_Core32_Int32 $j15 |
|
62 | - */ |
|
63 | - if (self::strlen($k) < 32) { |
|
64 | - throw new RangeException('Key must be 32 bytes long'); |
|
65 | - } |
|
66 | - if ($c === null) { |
|
67 | - $x0 = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); |
|
68 | - $x5 = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); |
|
69 | - $x10 = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); |
|
70 | - $x15 = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); |
|
71 | - } else { |
|
72 | - $x0 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4)); |
|
73 | - $x5 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4)); |
|
74 | - $x10 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4)); |
|
75 | - $x15 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4)); |
|
76 | - } |
|
77 | - $x1 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 0, 4)); |
|
78 | - $x2 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 4, 4)); |
|
79 | - $x3 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 8, 4)); |
|
80 | - $x4 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 12, 4)); |
|
81 | - $x6 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4)); |
|
82 | - $x7 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4)); |
|
83 | - $x8 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4)); |
|
84 | - $x9 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4)); |
|
85 | - $x11 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 16, 4)); |
|
86 | - $x12 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 20, 4)); |
|
87 | - $x13 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 24, 4)); |
|
88 | - $x14 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 28, 4)); |
|
12 | + /** |
|
13 | + * Calculate an hsalsa20 hash of a single block |
|
14 | + * |
|
15 | + * HSalsa20 doesn't have a counter and will never be used for more than |
|
16 | + * one block (used to derive a subkey for xsalsa20). |
|
17 | + * |
|
18 | + * @internal You should not use this directly from another application |
|
19 | + * |
|
20 | + * @param string $in |
|
21 | + * @param string $k |
|
22 | + * @param string|null $c |
|
23 | + * @return string |
|
24 | + * @throws SodiumException |
|
25 | + * @throws TypeError |
|
26 | + */ |
|
27 | + public static function hsalsa20($in, $k, $c = null) |
|
28 | + { |
|
29 | + /** |
|
30 | + * @var ParagonIE_Sodium_Core32_Int32 $x0 |
|
31 | + * @var ParagonIE_Sodium_Core32_Int32 $x1 |
|
32 | + * @var ParagonIE_Sodium_Core32_Int32 $x2 |
|
33 | + * @var ParagonIE_Sodium_Core32_Int32 $x3 |
|
34 | + * @var ParagonIE_Sodium_Core32_Int32 $x4 |
|
35 | + * @var ParagonIE_Sodium_Core32_Int32 $x5 |
|
36 | + * @var ParagonIE_Sodium_Core32_Int32 $x6 |
|
37 | + * @var ParagonIE_Sodium_Core32_Int32 $x7 |
|
38 | + * @var ParagonIE_Sodium_Core32_Int32 $x8 |
|
39 | + * @var ParagonIE_Sodium_Core32_Int32 $x9 |
|
40 | + * @var ParagonIE_Sodium_Core32_Int32 $x10 |
|
41 | + * @var ParagonIE_Sodium_Core32_Int32 $x11 |
|
42 | + * @var ParagonIE_Sodium_Core32_Int32 $x12 |
|
43 | + * @var ParagonIE_Sodium_Core32_Int32 $x13 |
|
44 | + * @var ParagonIE_Sodium_Core32_Int32 $x14 |
|
45 | + * @var ParagonIE_Sodium_Core32_Int32 $x15 |
|
46 | + * @var ParagonIE_Sodium_Core32_Int32 $j0 |
|
47 | + * @var ParagonIE_Sodium_Core32_Int32 $j1 |
|
48 | + * @var ParagonIE_Sodium_Core32_Int32 $j2 |
|
49 | + * @var ParagonIE_Sodium_Core32_Int32 $j3 |
|
50 | + * @var ParagonIE_Sodium_Core32_Int32 $j4 |
|
51 | + * @var ParagonIE_Sodium_Core32_Int32 $j5 |
|
52 | + * @var ParagonIE_Sodium_Core32_Int32 $j6 |
|
53 | + * @var ParagonIE_Sodium_Core32_Int32 $j7 |
|
54 | + * @var ParagonIE_Sodium_Core32_Int32 $j8 |
|
55 | + * @var ParagonIE_Sodium_Core32_Int32 $j9 |
|
56 | + * @var ParagonIE_Sodium_Core32_Int32 $j10 |
|
57 | + * @var ParagonIE_Sodium_Core32_Int32 $j11 |
|
58 | + * @var ParagonIE_Sodium_Core32_Int32 $j12 |
|
59 | + * @var ParagonIE_Sodium_Core32_Int32 $j13 |
|
60 | + * @var ParagonIE_Sodium_Core32_Int32 $j14 |
|
61 | + * @var ParagonIE_Sodium_Core32_Int32 $j15 |
|
62 | + */ |
|
63 | + if (self::strlen($k) < 32) { |
|
64 | + throw new RangeException('Key must be 32 bytes long'); |
|
65 | + } |
|
66 | + if ($c === null) { |
|
67 | + $x0 = new ParagonIE_Sodium_Core32_Int32(array(0x6170, 0x7865)); |
|
68 | + $x5 = new ParagonIE_Sodium_Core32_Int32(array(0x3320, 0x646e)); |
|
69 | + $x10 = new ParagonIE_Sodium_Core32_Int32(array(0x7962, 0x2d32)); |
|
70 | + $x15 = new ParagonIE_Sodium_Core32_Int32(array(0x6b20, 0x6574)); |
|
71 | + } else { |
|
72 | + $x0 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 0, 4)); |
|
73 | + $x5 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 4, 4)); |
|
74 | + $x10 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 8, 4)); |
|
75 | + $x15 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($c, 12, 4)); |
|
76 | + } |
|
77 | + $x1 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 0, 4)); |
|
78 | + $x2 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 4, 4)); |
|
79 | + $x3 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 8, 4)); |
|
80 | + $x4 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 12, 4)); |
|
81 | + $x6 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 0, 4)); |
|
82 | + $x7 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 4, 4)); |
|
83 | + $x8 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 8, 4)); |
|
84 | + $x9 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($in, 12, 4)); |
|
85 | + $x11 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 16, 4)); |
|
86 | + $x12 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 20, 4)); |
|
87 | + $x13 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 24, 4)); |
|
88 | + $x14 = ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($k, 28, 4)); |
|
89 | 89 | |
90 | - for ($i = self::ROUNDS; $i > 0; $i -= 2) { |
|
91 | - $x4 = $x4->xorInt32($x0->addInt32($x12)->rotateLeft(7)); |
|
92 | - $x8 = $x8->xorInt32($x4->addInt32($x0)->rotateLeft(9)); |
|
93 | - $x12 = $x12->xorInt32($x8->addInt32($x4)->rotateLeft(13)); |
|
94 | - $x0 = $x0->xorInt32($x12->addInt32($x8)->rotateLeft(18)); |
|
90 | + for ($i = self::ROUNDS; $i > 0; $i -= 2) { |
|
91 | + $x4 = $x4->xorInt32($x0->addInt32($x12)->rotateLeft(7)); |
|
92 | + $x8 = $x8->xorInt32($x4->addInt32($x0)->rotateLeft(9)); |
|
93 | + $x12 = $x12->xorInt32($x8->addInt32($x4)->rotateLeft(13)); |
|
94 | + $x0 = $x0->xorInt32($x12->addInt32($x8)->rotateLeft(18)); |
|
95 | 95 | |
96 | - $x9 = $x9->xorInt32($x5->addInt32($x1)->rotateLeft(7)); |
|
97 | - $x13 = $x13->xorInt32($x9->addInt32($x5)->rotateLeft(9)); |
|
98 | - $x1 = $x1->xorInt32($x13->addInt32($x9)->rotateLeft(13)); |
|
99 | - $x5 = $x5->xorInt32($x1->addInt32($x13)->rotateLeft(18)); |
|
96 | + $x9 = $x9->xorInt32($x5->addInt32($x1)->rotateLeft(7)); |
|
97 | + $x13 = $x13->xorInt32($x9->addInt32($x5)->rotateLeft(9)); |
|
98 | + $x1 = $x1->xorInt32($x13->addInt32($x9)->rotateLeft(13)); |
|
99 | + $x5 = $x5->xorInt32($x1->addInt32($x13)->rotateLeft(18)); |
|
100 | 100 | |
101 | - $x14 = $x14->xorInt32($x10->addInt32($x6)->rotateLeft(7)); |
|
102 | - $x2 = $x2->xorInt32($x14->addInt32($x10)->rotateLeft(9)); |
|
103 | - $x6 = $x6->xorInt32($x2->addInt32($x14)->rotateLeft(13)); |
|
104 | - $x10 = $x10->xorInt32($x6->addInt32($x2)->rotateLeft(18)); |
|
101 | + $x14 = $x14->xorInt32($x10->addInt32($x6)->rotateLeft(7)); |
|
102 | + $x2 = $x2->xorInt32($x14->addInt32($x10)->rotateLeft(9)); |
|
103 | + $x6 = $x6->xorInt32($x2->addInt32($x14)->rotateLeft(13)); |
|
104 | + $x10 = $x10->xorInt32($x6->addInt32($x2)->rotateLeft(18)); |
|
105 | 105 | |
106 | - $x3 = $x3->xorInt32($x15->addInt32($x11)->rotateLeft(7)); |
|
107 | - $x7 = $x7->xorInt32($x3->addInt32($x15)->rotateLeft(9)); |
|
108 | - $x11 = $x11->xorInt32($x7->addInt32($x3)->rotateLeft(13)); |
|
109 | - $x15 = $x15->xorInt32($x11->addInt32($x7)->rotateLeft(18)); |
|
106 | + $x3 = $x3->xorInt32($x15->addInt32($x11)->rotateLeft(7)); |
|
107 | + $x7 = $x7->xorInt32($x3->addInt32($x15)->rotateLeft(9)); |
|
108 | + $x11 = $x11->xorInt32($x7->addInt32($x3)->rotateLeft(13)); |
|
109 | + $x15 = $x15->xorInt32($x11->addInt32($x7)->rotateLeft(18)); |
|
110 | 110 | |
111 | - $x1 = $x1->xorInt32($x0->addInt32($x3)->rotateLeft(7)); |
|
112 | - $x2 = $x2->xorInt32($x1->addInt32($x0)->rotateLeft(9)); |
|
113 | - $x3 = $x3->xorInt32($x2->addInt32($x1)->rotateLeft(13)); |
|
114 | - $x0 = $x0->xorInt32($x3->addInt32($x2)->rotateLeft(18)); |
|
111 | + $x1 = $x1->xorInt32($x0->addInt32($x3)->rotateLeft(7)); |
|
112 | + $x2 = $x2->xorInt32($x1->addInt32($x0)->rotateLeft(9)); |
|
113 | + $x3 = $x3->xorInt32($x2->addInt32($x1)->rotateLeft(13)); |
|
114 | + $x0 = $x0->xorInt32($x3->addInt32($x2)->rotateLeft(18)); |
|
115 | 115 | |
116 | - $x6 = $x6->xorInt32($x5->addInt32($x4)->rotateLeft(7)); |
|
117 | - $x7 = $x7->xorInt32($x6->addInt32($x5)->rotateLeft(9)); |
|
118 | - $x4 = $x4->xorInt32($x7->addInt32($x6)->rotateLeft(13)); |
|
119 | - $x5 = $x5->xorInt32($x4->addInt32($x7)->rotateLeft(18)); |
|
116 | + $x6 = $x6->xorInt32($x5->addInt32($x4)->rotateLeft(7)); |
|
117 | + $x7 = $x7->xorInt32($x6->addInt32($x5)->rotateLeft(9)); |
|
118 | + $x4 = $x4->xorInt32($x7->addInt32($x6)->rotateLeft(13)); |
|
119 | + $x5 = $x5->xorInt32($x4->addInt32($x7)->rotateLeft(18)); |
|
120 | 120 | |
121 | - $x11 = $x11->xorInt32($x10->addInt32($x9)->rotateLeft(7)); |
|
122 | - $x8 = $x8->xorInt32($x11->addInt32($x10)->rotateLeft(9)); |
|
123 | - $x9 = $x9->xorInt32($x8->addInt32($x11)->rotateLeft(13)); |
|
124 | - $x10 = $x10->xorInt32($x9->addInt32($x8)->rotateLeft(18)); |
|
121 | + $x11 = $x11->xorInt32($x10->addInt32($x9)->rotateLeft(7)); |
|
122 | + $x8 = $x8->xorInt32($x11->addInt32($x10)->rotateLeft(9)); |
|
123 | + $x9 = $x9->xorInt32($x8->addInt32($x11)->rotateLeft(13)); |
|
124 | + $x10 = $x10->xorInt32($x9->addInt32($x8)->rotateLeft(18)); |
|
125 | 125 | |
126 | - $x12 = $x12->xorInt32($x15->addInt32($x14)->rotateLeft(7)); |
|
127 | - $x13 = $x13->xorInt32($x12->addInt32($x15)->rotateLeft(9)); |
|
128 | - $x14 = $x14->xorInt32($x13->addInt32($x12)->rotateLeft(13)); |
|
129 | - $x15 = $x15->xorInt32($x14->addInt32($x13)->rotateLeft(18)); |
|
130 | - } |
|
126 | + $x12 = $x12->xorInt32($x15->addInt32($x14)->rotateLeft(7)); |
|
127 | + $x13 = $x13->xorInt32($x12->addInt32($x15)->rotateLeft(9)); |
|
128 | + $x14 = $x14->xorInt32($x13->addInt32($x12)->rotateLeft(13)); |
|
129 | + $x15 = $x15->xorInt32($x14->addInt32($x13)->rotateLeft(18)); |
|
130 | + } |
|
131 | 131 | |
132 | - return $x0->toReverseString() . |
|
133 | - $x5->toReverseString() . |
|
134 | - $x10->toReverseString() . |
|
135 | - $x15->toReverseString() . |
|
136 | - $x6->toReverseString() . |
|
137 | - $x7->toReverseString() . |
|
138 | - $x8->toReverseString() . |
|
139 | - $x9->toReverseString(); |
|
140 | - } |
|
132 | + return $x0->toReverseString() . |
|
133 | + $x5->toReverseString() . |
|
134 | + $x10->toReverseString() . |
|
135 | + $x15->toReverseString() . |
|
136 | + $x6->toReverseString() . |
|
137 | + $x7->toReverseString() . |
|
138 | + $x8->toReverseString() . |
|
139 | + $x9->toReverseString(); |
|
140 | + } |
|
141 | 141 | } |