Debian Codegraph @

lib/3rdParty/composer/paragonie/sodium_compat/src/Core/BLAKE2b.php

raw symbols SCIP
1 <?php
2 declare(strict_types=1);
3
4 if (class_exists('ParagonIE_Sodium_Core_BLAKE2b', false)) {
5 return;
6 }
7
8 /**
9 * Class ParagonIE_Sodium_Core_BLAKE2b
10 *
11 * Based on the work of Devi Mandiri in devi/salt.
12 */
13 abstract class ParagonIE_Sodium_Core_BLAKE2b extends ParagonIE_Sodium_Core_Util
14 {
15 /**
16 * @var SplFixedArray
17 */
18 protected static SplFixedArray $iv;
19
20 /**
21 * @var array<int, array<int, int>>
22 */
23 protected const SIGMA = [
24 [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
25 [ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3],
26 [ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4],
27 [ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8],
28 [ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13],
29 [ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9],
30 [ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11],
31 [ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10],
32 [ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5],
33 [ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0],
34 [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
35 [ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3]
36 ];
37
38 const KEYBYTES = 64;
39
40 /**
41 * Turn two 32-bit integers into a fixed array representing a 64-bit integer.
42 *
43 * @internal You should not use this directly from another application
44 *
45 * @param int $high
46 * @param int $low
47 * @return SplFixedArray
48 */
49 public static function new64(int $high, int $low): SplFixedArray
50 {
51 $i64 = new SplFixedArray(2);
52 $i64[0] = $high & 0xffffffff;
53 $i64[1] = $low & 0xffffffff;
54 return $i64;
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 SplFixedArray
65 */
66 protected static function to64(int $num): SplFixedArray
67 {
68 list($hi, $lo) = self::numericTo64BitInteger($num);
69 return self::new64($hi, $lo);
70 }
71
72 /**
73 * Adds two 64-bit integers together, returning their sum as a SplFixedArray
74 * containing two 32-bit integers (representing a 64-bit integer).
75 *
76 * @internal You should not use this directly from another application
77 *
78 * @param SplFixedArray $x
79 * @param SplFixedArray $y
80 * @return SplFixedArray
81 */
82 public static function add64(SplFixedArray $x, SplFixedArray $y): SplFixedArray
83 {
84 $l = ($x[1] + $y[1]) & 0xffffffff;
85 return self::new64(
86 (int) ($x[0] + $y[0] + (
87 ($l < $x[1]) ? 1 : 0
88 )),
89 $l
90 );
91 }
92
93 /**
94 * @internal You should not use this directly from another application
95 *
96 * @param SplFixedArray $x
97 * @param SplFixedArray $y
98 * @param SplFixedArray $z
99 * @return SplFixedArray
100 * @throws SodiumException
101 */
102 public static function add364(
103 SplFixedArray $x,
104 SplFixedArray $y,
105 SplFixedArray $z
106 ): SplFixedArray {
107 return self::add64($x, self::add64($y, $z));
108 }
109
110 /**
111 * @internal You should not use this directly from another application
112 *
113 * @param SplFixedArray $x
114 * @param SplFixedArray $y
115 * @return SplFixedArray
116 * @throws SodiumException
117 * @throws TypeError
118 */
119 public static function xor64(SplFixedArray $x, SplFixedArray $y): SplFixedArray
120 {
121 if (!is_numeric($x[0])) {
122 throw new SodiumException('x[0] is not an integer');
123 }
124 if (!is_numeric($x[1])) {
125 throw new SodiumException('x[1] is not an integer');
126 }
127 if (!is_numeric($y[0])) {
128 throw new SodiumException('y[0] is not an integer');
129 }
130 if (!is_numeric($y[1])) {
131 throw new SodiumException('y[1] is not an integer');
132 }
133 return self::new64(
134 (int) (($x[0] ^ $y[0]) & 0xffffffff),
135 (int) (($x[1] ^ $y[1]) & 0xffffffff)
136 );
137 }
138
139 /**
140 * @internal You should not use this directly from another application
141 *
142 * @param SplFixedArray $x
143 * @param int $c
144 * @return SplFixedArray
145 *
146 * @throws SodiumException
147 */
148 public static function rotr64(SplFixedArray $x, int $c): SplFixedArray
149 {
150 if ($c >= 64) {
151 $c %= 64;
152 }
153 if ($c >= 32) {
154 /** @var int $tmp */
155 $tmp = $x[0];
156 $x[0] = $x[1];
157 $x[1] = $tmp;
158 $c -= 32;
159 }
160 if ($c === 0) {
161 return $x;
162 }
163
164 $l0 = 0;
165 /** @var int $c */
166 $c = 64 - $c;
167
168 if ($c < 32) {
169 $h0 = ((int) ($x[0]) << $c) | (
170 (
171 (int) ($x[1]) & ((1 << $c) - 1)
172 <<
173 (32 - $c)
174 ) >> (32 - $c)
175 );
176 $l0 = (int) ($x[1]) << $c;
177 } else {
178 $h0 = (int) ($x[1]) << ($c - 32);
179 }
180
181 $h1 = 0;
182 $c1 = 64 - $c;
183
184 if ($c1 < 32) {
185 $h1 = (int) ($x[0]) >> $c1;
186 $l1 = ((int) ($x[1]) >> $c1) | ((int) ($x[0]) & ((1 << $c1) - 1)) << (32 - $c1);
187 } else {
188 $l1 = (int) ($x[0]) >> ($c1 - 32);
189 }
190
191 return self::new64($h0 | $h1, $l0 | $l1);
192 }
193
194 /**
195 * @internal You should not use this directly from another application
196 *
197 * @param SplFixedArray $x
198 * @return int
199 */
200 protected static function flatten64(SplFixedArray $x): int
201 {
202 return (int) ($x[0] * 4294967296 + $x[1]);
203 }
204
205 /**
206 * @internal You should not use this directly from another application
207 *
208 * @param SplFixedArray $x
209 * @param int $i
210 * @return SplFixedArray
211 */
212 protected static function load64(SplFixedArray $x, int $i): SplFixedArray
213 {
214 $l = (int) ($x[$i])
215 | ((int) ($x[$i+1]) << 8)
216 | ((int) ($x[$i+2]) << 16)
217 | ((int) ($x[$i+3]) << 24);
218 $h = (int) ($x[$i+4])
219 | ((int) ($x[$i+5]) << 8)
220 | ((int) ($x[$i+6]) << 16)
221 | ((int) ($x[$i+7]) << 24);
222 return self::new64($h, $l);
223 }
224
225 /**
226 * @internal You should not use this directly from another application
227 *
228 * @param SplFixedArray $x
229 * @param int $i
230 * @param SplFixedArray $u
231 * @return void
232 */
233 protected static function store64(SplFixedArray $x, int $i, SplFixedArray $u): void
234 {
235 $maxLength = $x->getSize() - 1;
236 for ($j = 0; $j < 8; ++$j) {
237 /*
238 [0, 1, 2, 3, 4, 5, 6, 7]
239 ... becomes ...
240 [0, 0, 0, 0, 1, 1, 1, 1]
241 */
242 $uIdx = ((7 - $j) & 4) >> 2;
243 $x[$i] = ((int) ($u[$uIdx]) & 0xff);
244 if (++$i > $maxLength) {
245 return;
246 }
247 $u[$uIdx] >>= 8;
248 }
249 }
250
251 /**
252 * This just sets the $iv static variable.
253 *
254 * @internal You should not use this directly from another application
255 *
256 * @return void
257 *
258 * @throws SodiumException
259 */
260 public static function pseudoConstructor(): void
261 {
262 static $called = false;
263 if ($called) {
264 return;
265 }
266 self::$iv = new SplFixedArray(8);
267 self::$iv[0] = self::new64(0x6a09e667, 0xf3bcc908);
268 self::$iv[1] = self::new64(0xbb67ae85, 0x84caa73b);
269 self::$iv[2] = self::new64(0x3c6ef372, 0xfe94f82b);
270 self::$iv[3] = self::new64(0xa54ff53a, 0x5f1d36f1);
271 self::$iv[4] = self::new64(0x510e527f, 0xade682d1);
272 self::$iv[5] = self::new64(0x9b05688c, 0x2b3e6c1f);
273 self::$iv[6] = self::new64(0x1f83d9ab, 0xfb41bd6b);
274 self::$iv[7] = self::new64(0x5be0cd19, 0x137e2179);
275
276 $called = true;
277 }
278
279 /**
280 * Returns a fresh BLAKE2 context.
281 *
282 * @internal You should not use this directly from another application
283 *
284 * @return SplFixedArray
285 *
286 * @throws SodiumException
287 */
288 protected static function context(): SplFixedArray
289 {
290 $ctx = new SplFixedArray(6);
291 $ctx[0] = new SplFixedArray(8); // h
292 $ctx[1] = new SplFixedArray(2); // t
293 $ctx[2] = new SplFixedArray(2); // f
294 $ctx[3] = new SplFixedArray(256); // buf
295 $ctx[4] = 0; // buflen
296 $ctx[5] = 0; // last_node (uint8_t)
297
298 for ($i = 8; $i--;) {
299 $ctx[0][$i] = self::$iv[$i];
300 }
301 for ($i = 256; $i--;) {
302 $ctx[3][$i] = 0;
303 }
304
305 $zero = self::new64(0, 0);
306 $ctx[1][0] = $zero;
307 $ctx[1][1] = $zero;
308 $ctx[2][0] = $zero;
309 $ctx[2][1] = $zero;
310
311 return $ctx;
312 }
313
314 /**
315 * @internal You should not use this directly from another application
316 *
317 * @param SplFixedArray $ctx
318 * @param SplFixedArray $buf
319 * @return void
320 * @throws SodiumException
321 * @throws TypeError
322 */
323 protected static function compress(SplFixedArray $ctx, SplFixedArray $buf): void
324 {
325 $m = new SplFixedArray(16);
326 $v = new SplFixedArray(16);
327
328 for ($i = 16; $i--;) {
329 $m[$i] = self::load64($buf, $i << 3);
330 }
331
332 for ($i = 8; $i--;) {
333 $v[$i] = $ctx[0][$i];
334 }
335
336 $v[ 8] = self::$iv[0];
337 $v[ 9] = self::$iv[1];
338 $v[10] = self::$iv[2];
339 $v[11] = self::$iv[3];
340
341 $v[12] = self::xor64($ctx[1][0], self::$iv[4]);
342 $v[13] = self::xor64($ctx[1][1], self::$iv[5]);
343 $v[14] = self::xor64($ctx[2][0], self::$iv[6]);
344 $v[15] = self::xor64($ctx[2][1], self::$iv[7]);
345
346 for ($r = 0; $r < 12; ++$r) {
347 $v = self::G($r, 0, 0, 4, 8, 12, $v, $m);
348 $v = self::G($r, 1, 1, 5, 9, 13, $v, $m);
349 $v = self::G($r, 2, 2, 6, 10, 14, $v, $m);
350 $v = self::G($r, 3, 3, 7, 11, 15, $v, $m);
351 $v = self::G($r, 4, 0, 5, 10, 15, $v, $m);
352 $v = self::G($r, 5, 1, 6, 11, 12, $v, $m);
353 $v = self::G($r, 6, 2, 7, 8, 13, $v, $m);
354 $v = self::G($r, 7, 3, 4, 9, 14, $v, $m);
355 }
356
357 for ($i = 8; $i--;) {
358 $ctx[0][$i] = self::xor64(
359 $ctx[0][$i], self::xor64($v[$i], $v[$i+8])
360 );
361 }
362 }
363
364 /**
365 * @internal You should not use this directly from another application
366 *
367 * @param int $r
368 * @param int $i
369 * @param int $a
370 * @param int $b
371 * @param int $c
372 * @param int $d
373 * @param SplFixedArray $v
374 * @param SplFixedArray $m
375 * @return SplFixedArray
376 * @throws SodiumException
377 * @throws TypeError
378 */
379 public static function G(
380 int $r,
381 int $i,
382 int $a,
383 int $b,
384 int $c,
385 int $d,
386 SplFixedArray $v,
387 SplFixedArray $m
388 ): SplFixedArray {
389 $v[$a] = self::add364($v[$a], $v[$b], $m[self::SIGMA[$r][$i << 1]]);
390 $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32);
391 $v[$c] = self::add64($v[$c], $v[$d]);
392 $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 24);
393 $v[$a] = self::add364($v[$a], $v[$b], $m[self::SIGMA[$r][($i << 1) + 1]]);
394 $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 16);
395 $v[$c] = self::add64($v[$c], $v[$d]);
396 $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 63);
397 return $v;
398 }
399
400 /**
401 * @internal You should not use this directly from another application
402 *
403 * @param SplFixedArray $ctx
404 * @param int $inc
405 * @return void
406 *
407 * @throws SodiumException
408 */
409 public static function increment_counter(SplFixedArray $ctx, int $inc): void
410 {
411 if ($inc < 0) {
412 throw new SodiumException('Increasing by a negative number makes no sense.');
413 }
414 $t = self::to64($inc);
415 # S->t is $ctx[1] in our implementation
416
417 # S->t[0] = ( uint64_t )( t >> 0 );
418 $ctx[1][0] = self::add64($ctx[1][0], $t);
419
420 # S->t[1] += ( S->t[0] < inc );
421 if (self::flatten64($ctx[1][0]) < $inc) {
422 $ctx[1][1] = self::add64($ctx[1][1], self::to64(1));
423 }
424 }
425
426 /**
427 * @internal You should not use this directly from another application
428 *
429 * @param SplFixedArray $ctx
430 * @param SplFixedArray $p
431 * @param int $plen
432 * @return void
433 *
434 * @throws SodiumException
435 * @throws TypeError
436 */
437 public static function update(SplFixedArray $ctx, SplFixedArray $p, int $plen): void
438 {
439 self::pseudoConstructor();
440
441 $offset = 0;
442 while ($plen > 0) {
443 $left = $ctx[4];
444 $fill = 256 - $left;
445
446 if ($plen > $fill) {
447 # memcpy( S->buf + left, in, fill ); /* Fill buffer */
448 for ($i = $fill; $i--;) {
449 $ctx[3][$i + $left] = $p[(int) ($i + $offset)];
450 }
451
452 # S->buflen += fill;
453 $ctx[4] += $fill;
454
455 # blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
456 self::increment_counter($ctx, 128);
457
458 # blake2b_compress( S, S->buf ); /* Compress */
459 self::compress($ctx, $ctx[3]);
460
461 # memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */
462 for ($i = 128; $i--;) {
463 $ctx[3][$i] = $ctx[3][$i + 128];
464 }
465
466 # S->buflen -= BLAKE2B_BLOCKBYTES;
467 $ctx[4] -= 128;
468
469 # in += fill;
470 $offset += $fill;
471
472 # inlen -= fill;
473 $plen -= $fill;
474 } else {
475 for ($i = $plen; $i--;) {
476 $ctx[3][$i + $left] = $p[(int) ($i + $offset)];
477 }
478 $ctx[4] += $plen;
479 $offset += $plen;
480 $plen -= $plen;
481 }
482 }
483 }
484
485 /**
486 * @internal You should not use this directly from another application
487 *
488 * @param SplFixedArray $ctx
489 * @param SplFixedArray $out
490 * @return SplFixedArray
491 *
492 * @throws SodiumException
493 * @throws TypeError
494 */
495 public static function finish(SplFixedArray $ctx, SplFixedArray $out): SplFixedArray
496 {
497 self::pseudoConstructor();
498 if ($ctx[4] > 128) {
499 self::increment_counter($ctx, 128);
500 self::compress($ctx, $ctx[3]);
501 $ctx[4] -= 128;
502 if ($ctx[4] > 128) {
503 throw new SodiumException('Failed to assert that buflen <= 128 bytes');
504 }
505 for ($i = $ctx[4]; $i--;) {
506 $ctx[3][$i] = $ctx[3][$i + 128];
507 }
508 }
509
510 self::increment_counter($ctx, $ctx[4]);
511 $ctx[2][0] = self::new64(0xffffffff, 0xffffffff);
512
513 for ($i = 256 - $ctx[4]; $i--;) {
514 $ctx[3][$i+$ctx[4]] = 0;
515 }
516
517 self::compress($ctx, $ctx[3]);
518
519 $i = (int) (($out->getSize() - 1) / 8);
520 for (; $i >= 0; --$i) {
521 self::store64($out, $i << 3, $ctx[0][$i]);
522 }
523 return $out;
524 }
525
526 /**
527 * @internal You should not use this directly from another application
528 *
529 * @param SplFixedArray|null $key
530 * @param int $outlen
531 * @param SplFixedArray|null $salt
532 * @param SplFixedArray|null $personal
533 * @return SplFixedArray
534 *
535 * @throws SodiumException
536 * @throws TypeError
537 */
538 public static function init(
539 ?SplFixedArray $key = null,
540 int $outlen = 64,
541 ?SplFixedArray $salt = null,
542 ?SplFixedArray $personal = null
543 ): SplFixedArray {
544 self::pseudoConstructor();
545 $klen = 0;
546
547 if ($key !== null) {
548 if (count($key) > 64) {
549 throw new SodiumException('Invalid key size');
550 }
551 $klen = count($key);
552 }
553
554 if ($outlen > 64) {
555 throw new SodiumException('Invalid output size');
556 }
557
558 $ctx = self::context();
559
560 $p = new SplFixedArray(64);
561 // Zero our param buffer...
562 for ($i = 64; --$i;) {
563 $p[$i] = 0;
564 }
565
566 $p[0] = $outlen; // digest_length
567 $p[1] = $klen; // key_length
568 $p[2] = 1; // fanout
569 $p[3] = 1; // depth
570
571 if ($salt instanceof SplFixedArray) {
572 // salt: [32] through [47]
573 for ($i = 0; $i < 16; ++$i) {
574 $p[32 + $i] = (int) $salt[$i];
575 }
576 }
577 if ($personal instanceof SplFixedArray) {
578 // personal: [48] through [63]
579 for ($i = 0; $i < 16; ++$i) {
580 $p[48 + $i] = (int) $personal[$i];
581 }
582 }
583
584 $ctx[0][0] = self::xor64(
585 $ctx[0][0],
586 self::load64($p, 0)
587 );
588 if ($salt instanceof SplFixedArray || $personal instanceof SplFixedArray) {
589 // We need to do what blake2b_init_param() does:
590 for ($i = 1; $i < 8; ++$i) {
591 $ctx[0][$i] = self::xor64(
592 $ctx[0][$i],
593 self::load64($p, $i << 3)
594 );
595 }
596 }
597
598 if ($klen > 0 && $key instanceof SplFixedArray) {
599 $block = new SplFixedArray(128);
600 for ($i = 128; $i--;) {
601 $block[$i] = 0;
602 }
603 for ($i = $klen; $i--;) {
604 $block[$i] = $key[$i];
605 }
606 self::update($ctx, $block, 128);
607 $ctx[4] = 128;
608 }
609
610 return $ctx;
611 }
612
613 /**
614 * Convert a string into an SplFixedArray of integers
615 *
616 * @internal You should not use this directly from another application
617 *
618 * @param string $str
619 * @return SplFixedArray
620 */
621 public static function stringToSplFixedArray(string $str = ''): SplFixedArray
622 {
623 $values = unpack('C*', $str);
624 return SplFixedArray::fromArray(array_values($values));
625 }
626
627 /**
628 * Convert an SplFixedArray of integers into a string
629 *
630 * @internal You should not use this directly from another application
631 *
632 * @param SplFixedArray $a
633 * @return string
634 * @throws TypeError
635 */
636 public static function SplFixedArrayToString(SplFixedArray $a): string
637 {
638 /**
639 * @var array<int, int|string> $arr
640 */
641 $arr = $a->toArray();
642 $c = $a->count();
643 array_unshift($arr, str_repeat('C', $c));
644 return (string) (call_user_func_array('pack', $arr));
645 }
646
647 /**
648 * @internal You should not use this directly from another application
649 *
650 * @param SplFixedArray $ctx
651 * @return string
652 * @throws TypeError
653 */
654 public static function contextToString(SplFixedArray $ctx): string
655 {
656 $str = '';
657 /** @var array<int, array<int, int>> $ctxA */
658 $ctxA = $ctx[0]->toArray();
659
660 # uint64_t h[8];
661 for ($i = 0; $i < 8; ++$i) {
662 $str .= self::store32_le($ctxA[$i][1]);
663 $str .= self::store32_le($ctxA[$i][0]);
664 }
665
666 # uint64_t t[2];
667 # uint64_t f[2];
668 for ($i = 1; $i < 3; ++$i) {
669 $ctxA = $ctx[$i]->toArray();
670 $str .= self::store32_le($ctxA[0][1]);
671 $str .= self::store32_le($ctxA[0][0]);
672 $str .= self::store32_le($ctxA[1][1]);
673 $str .= self::store32_le($ctxA[1][0]);
674 }
675
676 # uint8_t buf[2 * 128];
677 $str .= self::SplFixedArrayToString($ctx[3]);
678
679 /** @var int $ctx4 */
680 $ctx4 = (int) $ctx[4];
681
682 # size_t buflen;
683 $str .= implode('', array(
684 self::intToChr($ctx4 & 0xff),
685 self::intToChr(($ctx4 >> 8) & 0xff),
686 self::intToChr(($ctx4 >> 16) & 0xff),
687 self::intToChr(($ctx4 >> 24) & 0xff),
688 self::intToChr(($ctx4 >> 32) & 0xff),
689 self::intToChr(($ctx4 >> 40) & 0xff),
690 self::intToChr(($ctx4 >> 48) & 0xff),
691 self::intToChr(($ctx4 >> 56) & 0xff)
692 ));
693 # uint8_t last_node;
694 return $str . self::intToChr($ctx[5]) . str_repeat("\x00", 23);
695 }
696
697 /**
698 * Creates an SplFixedArray containing other SplFixedArray elements, from
699 * a string (compatible with \Sodium\crypto_generichash_{init, update, final})
700 *
701 * @internal You should not use this directly from another application
702 *
703 * @param string $string
704 * @return SplFixedArray
705 * @throws SodiumException
706 * @throws TypeError
707 */
708 public static function stringToContext(string $string): SplFixedArray
709 {
710 $ctx = self::context();
711
712 # uint64_t h[8];
713 for ($i = 0; $i < 8; ++$i) {
714 $ctx[0][$i] = SplFixedArray::fromArray(
715 array(
716 self::load_4(
717 self::substr($string, (($i << 3) + 4), 4)
718 ),
719 self::load_4(
720 self::substr($string, (($i << 3)), 4)
721 )
722 )
723 );
724 }
725
726 # uint64_t t[2];
727 # uint64_t f[2];
728 for ($i = 1; $i < 3; ++$i) {
729 $ctx[$i][1] = SplFixedArray::fromArray(
730 array(
731 self::load_4(self::substr($string, 76 + (($i - 1) << 4), 4)),
732 self::load_4(self::substr($string, 72 + (($i - 1) << 4), 4))
733 )
734 );
735 $ctx[$i][0] = SplFixedArray::fromArray(
736 array(
737 self::load_4(self::substr($string, 68 + (($i - 1) << 4), 4)),
738 self::load_4(self::substr($string, 64 + (($i - 1) << 4), 4))
739 )
740 );
741 }
742
743 # uint8_t buf[2 * 128];
744 $ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256));
745
746 # uint8_t buf[2 * 128];
747 $int = 0;
748 for ($i = 0; $i < 8; ++$i) {
749 $int |= self::chrToInt($string[352 + $i]) << ($i << 3);
750 }
751 $ctx[4] = $int;
752
753 return $ctx;
754 }
755 }