Debian Codegraph @

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

raw symbols SCIP
1 <?php
2 declare(strict_types=1);
3
4 if (class_exists('ParagonIE_Sodium_Core_SipHash', false)) {
5 return;
6 }
7
8 /**
9 * Class ParagonIE_SodiumCompat_Core_SipHash
10 *
11 * Only uses 32-bit arithmetic, while the original SipHash used 64-bit integers
12 */
13 class ParagonIE_Sodium_Core_SipHash extends ParagonIE_Sodium_Core_Util
14 {
15 /**
16 * @internal You should not use this directly from another application
17 *
18 * @param int[] $v
19 * @return int[]
20 *
21 */
22 public static function sipRound(array $v): array
23 {
24 # v0 += v1;
25 list($v[0], $v[1]) = self::add(
26 array($v[0], $v[1]),
27 array($v[2], $v[3])
28 );
29
30 # v1=ROTL(v1,13);
31 list($v[2], $v[3]) = self::rotl_64($v[2], $v[3], 13);
32
33 # v1 ^= v0;
34 $v[2] = $v[2] ^ $v[0];
35 $v[3] = $v[3] ^ $v[1];
36
37 # v0=ROTL(v0,32);
38 list($v[0], $v[1]) = self::rotl_64($v[0], $v[1], 32);
39
40 # v2 += v3;
41 list($v[4], $v[5]) = self::add(
42 array($v[4], $v[5]),
43 array($v[6], $v[7])
44 );
45
46 # v3=ROTL(v3,16);
47 list($v[6], $v[7]) = self::rotl_64($v[6], $v[7], 16);
48
49 # v3 ^= v2;
50 $v[6] = $v[6] ^ $v[4];
51 $v[7] = $v[7] ^ $v[5];
52
53 # v0 += v3;
54 list($v[0], $v[1]) = self::add(
55 array($v[0], $v[1]),
56 array($v[6], $v[7])
57 );
58
59 # v3=ROTL(v3,21);
60 list($v[6], $v[7]) = self::rotl_64($v[6], $v[7], 21);
61
62 # v3 ^= v0;
63 $v[6] = $v[6] ^ $v[0];
64 $v[7] = $v[7] ^ $v[1];
65
66 # v2 += v1;
67 list($v[4], $v[5]) = self::add(
68 array($v[4], $v[5]),
69 array($v[2], $v[3])
70 );
71
72 # v1=ROTL(v1,17);
73 list($v[2], $v[3]) = self::rotl_64($v[2], $v[3], 17);
74
75 # v1 ^= v2;;
76 $v[2] = $v[2] ^ $v[4];
77 $v[3] = $v[3] ^ $v[5];
78
79 # v2=ROTL(v2,32)
80 list($v[4], $v[5]) = self::rotl_64($v[4], $v[5], 32);
81
82 return $v;
83 }
84
85 /**
86 * Add two 32 bit integers representing a 64-bit integer.
87 *
88 * @internal You should not use this directly from another application
89 *
90 * @param int[] $a
91 * @param int[] $b
92 * @return array<int, mixed>
93 */
94 public static function add(array $a, array $b): array
95 {
96 /** @var int $x1 */
97 $x1 = $a[1] + $b[1];
98 /** @var int $c */
99 $c = $x1 >> 32; // Carry if ($a + $b) > 0xffffffff
100 /** @var int $x0 */
101 $x0 = $a[0] + $b[0] + $c;
102 return array(
103 $x0 & 0xffffffff,
104 $x1 & 0xffffffff
105 );
106 }
107
108 /**
109 * @internal You should not use this directly from another application
110 *
111 * @param int $int0
112 * @param int $int1
113 * @param int $c
114 * @return array<int, mixed>
115 */
116 public static function rotl_64(int $int0, int $int1, int $c): array
117 {
118 $int0 &= 0xffffffff;
119 $int1 &= 0xffffffff;
120 $c &= 63;
121 if ($c === 32) {
122 return array($int1, $int0);
123 }
124 if ($c > 31) {
125 $tmp = $int1;
126 $int1 = $int0;
127 $int0 = $tmp;
128 $c &= 31;
129 }
130 if ($c === 0) {
131 return array($int0, $int1);
132 }
133 return array(
134 0xffffffff & (
135 ($int0 << $c)
136 |
137 ($int1 >> (32 - $c))
138 ),
139 0xffffffff & (
140 ($int1 << $c)
141 |
142 ($int0 >> (32 - $c))
143 ),
144 );
145 }
146
147 /**
148 * Implements Siphash-2-4 using only 32-bit numbers.
149 *
150 * When we split an int into two, the higher bits go to the lower index.
151 * e.g. 0xDEADBEEFAB10C92D becomes [
152 * 0 => 0xDEADBEEF,
153 * 1 => 0xAB10C92D
154 * ].
155 *
156 * @internal You should not use this directly from another application
157 *
158 * @param string $in
159 * @param string $key
160 * @return string
161 * @throws SodiumException
162 * @throws TypeError
163 *
164 * @psalm-suppress PossiblyUndefinedArrayOffset
165 */
166 public static function sipHash24(
167 #[SensitiveParameter]
168 string $in,
169 #[SensitiveParameter]
170 string $key
171 ): string {
172 $inlen = self::strlen($in);
173
174 # /* "somepseudorandomlygeneratedbytes" */
175 # u64 v0 = 0x736f6d6570736575ULL;
176 # u64 v1 = 0x646f72616e646f6dULL;
177 # u64 v2 = 0x6c7967656e657261ULL;
178 # u64 v3 = 0x7465646279746573ULL;
179 $v = array(
180 0x736f6d65, // 0
181 0x70736575, // 1
182 0x646f7261, // 2
183 0x6e646f6d, // 3
184 0x6c796765, // 4
185 0x6e657261, // 5
186 0x74656462, // 6
187 0x79746573 // 7
188 );
189 // v0 => $v[0], $v[1]
190 // v1 => $v[2], $v[3]
191 // v2 => $v[4], $v[5]
192 // v3 => $v[6], $v[7]
193
194 # u64 k0 = LOAD64_LE( k );
195 # u64 k1 = LOAD64_LE( k + 8 );
196 $k = array(
197 self::load_4(self::substr($key, 4, 4)),
198 self::load_4(self::substr($key, 0, 4)),
199 self::load_4(self::substr($key, 12, 4)),
200 self::load_4(self::substr($key, 8, 4))
201 );
202 // k0 => $k[0], $k[1]
203 // k1 => $k[2], $k[3]
204
205 # b = ( ( u64 )inlen ) << 56;
206 $b = array(
207 $inlen << 24,
208 0
209 );
210 // See docblock for why the 0th index gets the higher bits.
211
212 # v3 ^= k1;
213 $v[6] ^= $k[2];
214 $v[7] ^= $k[3];
215 # v2 ^= k0;
216 $v[4] ^= $k[0];
217 $v[5] ^= $k[1];
218 # v1 ^= k1;
219 $v[2] ^= $k[2];
220 $v[3] ^= $k[3];
221 # v0 ^= k0;
222 $v[0] ^= $k[0];
223 $v[1] ^= $k[1];
224
225 $left = $inlen;
226 # for ( ; in != end; in += 8 )
227 while ($left >= 8) {
228 # m = LOAD64_LE( in );
229 $m = array(
230 self::load_4(self::substr($in, 4, 4)),
231 self::load_4(self::substr($in, 0, 4))
232 );
233
234 # v3 ^= m;
235 $v[6] ^= $m[0];
236 $v[7] ^= $m[1];
237
238 # SIPROUND;
239 # SIPROUND;
240 $v = self::sipRound($v);
241 $v = self::sipRound($v);
242
243 # v0 ^= m;
244 $v[0] ^= $m[0];
245 $v[1] ^= $m[1];
246
247 $in = self::substr($in, 8);
248 $left -= 8;
249 }
250
251 # switch( left )
252 # {
253 # case 7: b |= ( ( u64 )in[ 6] ) << 48;
254 # case 6: b |= ( ( u64 )in[ 5] ) << 40;
255 # case 5: b |= ( ( u64 )in[ 4] ) << 32;
256 # case 4: b |= ( ( u64 )in[ 3] ) << 24;
257 # case 3: b |= ( ( u64 )in[ 2] ) << 16;
258 # case 2: b |= ( ( u64 )in[ 1] ) << 8;
259 # case 1: b |= ( ( u64 )in[ 0] ); break;
260 # case 0: break;
261 # }
262 switch ($left) {
263 case 7:
264 $b[0] |= self::chrToInt($in[6]) << 16;
265 case 6:
266 $b[0] |= self::chrToInt($in[5]) << 8;
267 case 5:
268 $b[0] |= self::chrToInt($in[4]);
269 case 4:
270 $b[1] |= self::chrToInt($in[3]) << 24;
271 case 3:
272 $b[1] |= self::chrToInt($in[2]) << 16;
273 case 2:
274 $b[1] |= self::chrToInt($in[1]) << 8;
275 case 1:
276 $b[1] |= self::chrToInt($in[0]);
277 case 0:
278 break;
279 }
280 // See docblock for why the 0th index gets the higher bits.
281
282 # v3 ^= b;
283 $v[6] ^= $b[0];
284 $v[7] ^= $b[1];
285
286 # SIPROUND;
287 # SIPROUND;
288 $v = self::sipRound($v);
289 $v = self::sipRound($v);
290
291 # v0 ^= b;
292 $v[0] ^= $b[0];
293 $v[1] ^= $b[1];
294
295 // Flip the lower 8 bits of v2 which is ($v[4], $v[5]) in our implementation
296 # v2 ^= 0xff;
297 $v[5] ^= 0xff;
298
299 # SIPROUND;
300 # SIPROUND;
301 # SIPROUND;
302 # SIPROUND;
303 $v = self::sipRound(self::sipRound(self::sipRound(self::sipRound($v))));
304
305 # b = v0 ^ v1 ^ v2 ^ v3;
306 # STORE64_LE( out, b );
307 return self::store32_le($v[1] ^ $v[3] ^ $v[5] ^ $v[7]) .
308 self::store32_le($v[0] ^ $v[2] ^ $v[4] ^ $v[6]);
309 }
310 }