Debian Codegraph @

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

raw symbols SCIP
1 <?php
2 declare(strict_types=1);
3
4 if (class_exists('ParagonIE_Sodium_Core_Salsa20', false)) {
5 return;
6 }
7
8 /**
9 * Class ParagonIE_Sodium_Core_Salsa20
10 */
11 abstract class ParagonIE_Sodium_Core_Salsa20 extends ParagonIE_Sodium_Core_Util
12 {
13 const ROUNDS = 20;
14
15 /**
16 * Calculate an salsa20 hash of a single block
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 TypeError
25 */
26 public static function core_salsa20(
27 string $in,
28 #[SensitiveParameter]
29 string $k,
30 ?string $c = null
31 ): string {
32 if (self::strlen($k) < 32) {
33 throw new RangeException('Key must be 32 bytes long');
34 }
35 if ($c === null) {
36 $j0 = $x0 = 0x61707865;
37 $j5 = $x5 = 0x3320646e;
38 $j10 = $x10 = 0x79622d32;
39 $j15 = $x15 = 0x6b206574;
40 } else {
41 $j0 = $x0 = self::load_4(self::substr($c, 0, 4));
42 $j5 = $x5 = self::load_4(self::substr($c, 4, 4));
43 $j10 = $x10 = self::load_4(self::substr($c, 8, 4));
44 $j15 = $x15 = self::load_4(self::substr($c, 12, 4));
45 }
46 $j1 = $x1 = self::load_4(self::substr($k, 0, 4));
47 $j2 = $x2 = self::load_4(self::substr($k, 4, 4));
48 $j3 = $x3 = self::load_4(self::substr($k, 8, 4));
49 $j4 = $x4 = self::load_4(self::substr($k, 12, 4));
50 $j6 = $x6 = self::load_4(self::substr($in, 0, 4));
51 $j7 = $x7 = self::load_4(self::substr($in, 4, 4));
52 $j8 = $x8 = self::load_4(self::substr($in, 8, 4));
53 $j9 = $x9 = self::load_4(self::substr($in, 12, 4));
54 $j11 = $x11 = self::load_4(self::substr($k, 16, 4));
55 $j12 = $x12 = self::load_4(self::substr($k, 20, 4));
56 $j13 = $x13 = self::load_4(self::substr($k, 24, 4));
57 $j14 = $x14 = self::load_4(self::substr($k, 28, 4));
58
59 for ($i = self::ROUNDS; $i > 0; $i -= 2) {
60 $x4 ^= self::rotate($x0 + $x12, 7);
61 $x8 ^= self::rotate($x4 + $x0, 9);
62 $x12 ^= self::rotate($x8 + $x4, 13);
63 $x0 ^= self::rotate($x12 + $x8, 18);
64
65 $x9 ^= self::rotate($x5 + $x1, 7);
66 $x13 ^= self::rotate($x9 + $x5, 9);
67 $x1 ^= self::rotate($x13 + $x9, 13);
68 $x5 ^= self::rotate($x1 + $x13, 18);
69
70 $x14 ^= self::rotate($x10 + $x6, 7);
71 $x2 ^= self::rotate($x14 + $x10, 9);
72 $x6 ^= self::rotate($x2 + $x14, 13);
73 $x10 ^= self::rotate($x6 + $x2, 18);
74
75 $x3 ^= self::rotate($x15 + $x11, 7);
76 $x7 ^= self::rotate($x3 + $x15, 9);
77 $x11 ^= self::rotate($x7 + $x3, 13);
78 $x15 ^= self::rotate($x11 + $x7, 18);
79
80 $x1 ^= self::rotate($x0 + $x3, 7);
81 $x2 ^= self::rotate($x1 + $x0, 9);
82 $x3 ^= self::rotate($x2 + $x1, 13);
83 $x0 ^= self::rotate($x3 + $x2, 18);
84
85 $x6 ^= self::rotate($x5 + $x4, 7);
86 $x7 ^= self::rotate($x6 + $x5, 9);
87 $x4 ^= self::rotate($x7 + $x6, 13);
88 $x5 ^= self::rotate($x4 + $x7, 18);
89
90 $x11 ^= self::rotate($x10 + $x9, 7);
91 $x8 ^= self::rotate($x11 + $x10, 9);
92 $x9 ^= self::rotate($x8 + $x11, 13);
93 $x10 ^= self::rotate($x9 + $x8, 18);
94
95 $x12 ^= self::rotate($x15 + $x14, 7);
96 $x13 ^= self::rotate($x12 + $x15, 9);
97 $x14 ^= self::rotate($x13 + $x12, 13);
98 $x15 ^= self::rotate($x14 + $x13, 18);
99 }
100
101 $x0 += $j0;
102 $x1 += $j1;
103 $x2 += $j2;
104 $x3 += $j3;
105 $x4 += $j4;
106 $x5 += $j5;
107 $x6 += $j6;
108 $x7 += $j7;
109 $x8 += $j8;
110 $x9 += $j9;
111 $x10 += $j10;
112 $x11 += $j11;
113 $x12 += $j12;
114 $x13 += $j13;
115 $x14 += $j14;
116 $x15 += $j15;
117
118 return self::store32_le($x0) .
119 self::store32_le($x1) .
120 self::store32_le($x2) .
121 self::store32_le($x3) .
122 self::store32_le($x4) .
123 self::store32_le($x5) .
124 self::store32_le($x6) .
125 self::store32_le($x7) .
126 self::store32_le($x8) .
127 self::store32_le($x9) .
128 self::store32_le($x10) .
129 self::store32_le($x11) .
130 self::store32_le($x12) .
131 self::store32_le($x13) .
132 self::store32_le($x14) .
133 self::store32_le($x15);
134 }
135
136 /**
137 * @internal You should not use this directly from another application
138 *
139 * @param int $len
140 * @param string $nonce
141 * @param string $key
142 * @return string
143 * @throws SodiumException
144 * @throws TypeError
145 */
146 public static function salsa20(
147 int $len,
148 string $nonce,
149 #[SensitiveParameter]
150 string $key
151 ): string {
152 if (self::strlen($key) !== 32) {
153 throw new RangeException('Key must be 32 bytes long');
154 }
155 $kcopy = $key;
156 $in = self::substr($nonce, 0, 8) . str_repeat("\0", 8);
157 $c = '';
158 while ($len >= 64) {
159 $c .= self::core_salsa20($in, $kcopy);
160 $u = 1;
161 // Internal counter.
162 for ($i = 8; $i < 16; ++$i) {
163 $u += self::chrToInt($in[$i]);
164 $in[$i] = self::intToChr($u & 0xff);
165 $u >>= 8;
166 }
167 $len -= 64;
168 }
169 if ($len > 0) {
170 $c .= self::substr(
171 self::core_salsa20($in, $kcopy),
172 0,
173 $len
174 );
175 }
176 try {
177 ParagonIE_Sodium_Compat::memzero($kcopy);
178 } catch (SodiumException) {
179 $kcopy = null;
180 }
181 return $c;
182 }
183
184 /**
185 * @internal You should not use this directly from another application
186 *
187 * @param string $m
188 * @param string $n
189 * @param int $ic
190 * @param string $k
191 * @return string
192 * @throws SodiumException
193 * @throws TypeError
194 */
195 public static function salsa20_xor_ic(
196 #[SensitiveParameter]
197 string $m,
198 string $n,
199 int $ic,
200 #[SensitiveParameter]
201 string $k
202 ): string {
203 $mlen = self::strlen($m);
204 if ($mlen < 1) {
205 return '';
206 }
207 $kcopy = self::substr($k, 0, 32);
208 $in = self::substr($n, 0, 8);
209 // Initialize the counter
210 $in .= ParagonIE_Sodium_Core_Util::store64_le($ic);
211
212 $c = '';
213 while ($mlen >= 64) {
214 $block = self::core_salsa20($in, $kcopy);
215 $c .= self::xorStrings(
216 self::substr($m, 0, 64),
217 self::substr($block, 0, 64)
218 );
219 $u = 1;
220 for ($i = 8; $i < 16; ++$i) {
221 $u += self::chrToInt($in[$i]);
222 $in[$i] = self::intToChr($u & 0xff);
223 $u >>= 8;
224 }
225
226 $mlen -= 64;
227 $m = self::substr($m, 64);
228 }
229
230 if ($mlen) {
231 $block = self::core_salsa20($in, $kcopy);
232 $c .= self::xorStrings(
233 self::substr($m, 0, $mlen),
234 self::substr($block, 0, $mlen)
235 );
236 }
237 try {
238 ParagonIE_Sodium_Compat::memzero($block);
239 ParagonIE_Sodium_Compat::memzero($kcopy);
240 } catch (SodiumException) {
241 $block = null;
242 $kcopy = null;
243 }
244
245 return $c;
246 }
247
248 /**
249 * @internal You should not use this directly from another application
250 *
251 * @param string $message
252 * @param string $nonce
253 * @param string $key
254 * @return string
255 * @throws SodiumException
256 * @throws TypeError
257 */
258 public static function salsa20_xor(
259 #[SensitiveParameter]
260 string $message,
261 string $nonce,
262 #[SensitiveParameter]
263 string $key
264 ): string {
265 return self::xorStrings(
266 $message,
267 self::salsa20(
268 self::strlen($message),
269 $nonce,
270 $key
271 )
272 );
273 }
274
275 /**
276 * @internal You should not use this directly from another application
277 *
278 * @param int $u
279 * @param int $c
280 * @return int
281 */
282 public static function rotate(int $u, int $c): int
283 {
284 $u &= 0xffffffff;
285 $c %= 32;
286 return (0xffffffff & (
287 ($u << $c)
288 |
289 ($u >> (32 - $c))
290 )
291 );
292 }
293 }