Debian Codegraph @

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

raw symbols SCIP
1 <?php
2 declare(strict_types=1);
3
4 if (class_exists('ParagonIE_Sodium_Core_HChaCha20', false)) {
5 return;
6 }
7
8 /**
9 * Class ParagonIE_Sodium_Core_HChaCha20
10 */
11 class ParagonIE_Sodium_Core_HChaCha20 extends ParagonIE_Sodium_Core_ChaCha20
12 {
13 /**
14 * @param string $in
15 * @param string $key
16 * @param string|null $c
17 * @return string
18 *
19 * @throws SodiumException
20 */
21 public static function hChaCha20(
22 string $in,
23 #[SensitiveParameter]
24 string $key,
25 ?string $c = null
26 ): string {
27 if (self::strlen($in) !== 16) {
28 throw new SodiumException('Argument 1 must be 16 bytes');
29 }
30 if (self::strlen($key) !== 32) {
31 throw new SodiumException('Argument 2 must be 32 bytes');
32 }
33 $ctx = array();
34
35 if ($c === null) {
36 $ctx[0] = 0x61707865;
37 $ctx[1] = 0x3320646e;
38 $ctx[2] = 0x79622d32;
39 $ctx[3] = 0x6b206574;
40 } else {
41 $ctx[0] = self::load_4(self::substr($c, 0, 4));
42 $ctx[1] = self::load_4(self::substr($c, 4, 4));
43 $ctx[2] = self::load_4(self::substr($c, 8, 4));
44 $ctx[3] = self::load_4(self::substr($c, 12, 4));
45 }
46 $ctx[4] = self::load_4(self::substr($key, 0, 4));
47 $ctx[5] = self::load_4(self::substr($key, 4, 4));
48 $ctx[6] = self::load_4(self::substr($key, 8, 4));
49 $ctx[7] = self::load_4(self::substr($key, 12, 4));
50 $ctx[8] = self::load_4(self::substr($key, 16, 4));
51 $ctx[9] = self::load_4(self::substr($key, 20, 4));
52 $ctx[10] = self::load_4(self::substr($key, 24, 4));
53 $ctx[11] = self::load_4(self::substr($key, 28, 4));
54 $ctx[12] = self::load_4(self::substr($in, 0, 4));
55 $ctx[13] = self::load_4(self::substr($in, 4, 4));
56 $ctx[14] = self::load_4(self::substr($in, 8, 4));
57 $ctx[15] = self::load_4(self::substr($in, 12, 4));
58 return self::hChaCha20Bytes($ctx);
59 }
60
61 /**
62 * @param array $ctx
63 * @return string
64 * @throws TypeError
65 */
66 protected static function hChaCha20Bytes(array $ctx): string
67 {
68 $x0 = (int) $ctx[0];
69 $x1 = (int) $ctx[1];
70 $x2 = (int) $ctx[2];
71 $x3 = (int) $ctx[3];
72 $x4 = (int) $ctx[4];
73 $x5 = (int) $ctx[5];
74 $x6 = (int) $ctx[6];
75 $x7 = (int) $ctx[7];
76 $x8 = (int) $ctx[8];
77 $x9 = (int) $ctx[9];
78 $x10 = (int) $ctx[10];
79 $x11 = (int) $ctx[11];
80 $x12 = (int) $ctx[12];
81 $x13 = (int) $ctx[13];
82 $x14 = (int) $ctx[14];
83 $x15 = (int) $ctx[15];
84
85 for ($i = 0; $i < 10; ++$i) {
86 [$x0, $x4, $x8, $x12] = self::quarterRound($x0, $x4, $x8, $x12);
87 [$x1, $x5, $x9, $x13] = self::quarterRound($x1, $x5, $x9, $x13);
88 [$x2, $x6, $x10, $x14] = self::quarterRound($x2, $x6, $x10, $x14);
89 [$x3, $x7, $x11, $x15] = self::quarterRound($x3, $x7, $x11, $x15);
90
91 [$x0, $x5, $x10, $x15] = self::quarterRound($x0, $x5, $x10, $x15);
92 [$x1, $x6, $x11, $x12] = self::quarterRound($x1, $x6, $x11, $x12);
93 [$x2, $x7, $x8, $x13] = self::quarterRound($x2, $x7, $x8, $x13);
94 [$x3, $x4, $x9, $x14] = self::quarterRound($x3, $x4, $x9, $x14);
95 }
96
97 return self::store32_le(($x0 & 0xffffffff)) .
98 self::store32_le(($x1 & 0xffffffff)) .
99 self::store32_le(($x2 & 0xffffffff)) .
100 self::store32_le(($x3 & 0xffffffff)) .
101 self::store32_le(($x12 & 0xffffffff)) .
102 self::store32_le(($x13 & 0xffffffff)) .
103 self::store32_le(($x14 & 0xffffffff)) .
104 self::store32_le(($x15 & 0xffffffff));
105 }
106 }