Debian Codegraph @

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

raw symbols SCIP
1 <?php
2 declare(strict_types=1);
3
4 if (class_exists('ParagonIE_Sodium_Core_AES', false)) {
5 return;
6 }
7
8 /**
9 * Bitsliced implementation of the AES block cipher.
10 *
11 * Based on the implementation provided by BearSSL.
12 *
13 * @internal This should only be used by sodium_compat
14 */
15 class ParagonIE_Sodium_Core_AES extends ParagonIE_Sodium_Core_Util
16 {
17 /**
18 * @var int[] AES round constants
19 */
20 private const Rcon = [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36];
21
22 /**
23 * Mutates the values of $q!
24 *
25 * @param ParagonIE_Sodium_Core_AES_Block $q
26 * @return void
27 */
28 public static function sbox(ParagonIE_Sodium_Core_AES_Block $q): void
29 {
30 /**
31 * @var int $x0
32 * @var int $x1
33 * @var int $x2
34 * @var int $x3
35 * @var int $x4
36 * @var int $x5
37 * @var int $x6
38 * @var int $x7
39 */
40 $x0 = $q[7] & self::U32_MAX;
41 $x1 = $q[6] & self::U32_MAX;
42 $x2 = $q[5] & self::U32_MAX;
43 $x3 = $q[4] & self::U32_MAX;
44 $x4 = $q[3] & self::U32_MAX;
45 $x5 = $q[2] & self::U32_MAX;
46 $x6 = $q[1] & self::U32_MAX;
47 $x7 = $q[0] & self::U32_MAX;
48
49 $y14 = $x3 ^ $x5;
50 $y13 = $x0 ^ $x6;
51 $y9 = $x0 ^ $x3;
52 $y8 = $x0 ^ $x5;
53 $t0 = $x1 ^ $x2;
54 $y1 = $t0 ^ $x7;
55 $y4 = $y1 ^ $x3;
56 $y12 = $y13 ^ $y14;
57 $y2 = $y1 ^ $x0;
58 $y5 = $y1 ^ $x6;
59 $y3 = $y5 ^ $y8;
60 $t1 = $x4 ^ $y12;
61 $y15 = $t1 ^ $x5;
62 $y20 = $t1 ^ $x1;
63 $y6 = $y15 ^ $x7;
64 $y10 = $y15 ^ $t0;
65 $y11 = $y20 ^ $y9;
66 $y7 = $x7 ^ $y11;
67 $y17 = $y10 ^ $y11;
68 $y19 = $y10 ^ $y8;
69 $y16 = $t0 ^ $y11;
70 $y21 = $y13 ^ $y16;
71 $y18 = $x0 ^ $y16;
72
73 /*
74 * Non-linear section.
75 */
76 $t2 = $y12 & $y15;
77 $t3 = $y3 & $y6;
78 $t4 = $t3 ^ $t2;
79 $t5 = $y4 & $x7;
80 $t6 = $t5 ^ $t2;
81 $t7 = $y13 & $y16;
82 $t8 = $y5 & $y1;
83 $t9 = $t8 ^ $t7;
84 $t10 = $y2 & $y7;
85 $t11 = $t10 ^ $t7;
86 $t12 = $y9 & $y11;
87 $t13 = $y14 & $y17;
88 $t14 = $t13 ^ $t12;
89 $t15 = $y8 & $y10;
90 $t16 = $t15 ^ $t12;
91 $t17 = $t4 ^ $t14;
92 $t18 = $t6 ^ $t16;
93 $t19 = $t9 ^ $t14;
94 $t20 = $t11 ^ $t16;
95 $t21 = $t17 ^ $y20;
96 $t22 = $t18 ^ $y19;
97 $t23 = $t19 ^ $y21;
98 $t24 = $t20 ^ $y18;
99
100 $t25 = $t21 ^ $t22;
101 $t26 = $t21 & $t23;
102 $t27 = $t24 ^ $t26;
103 $t28 = $t25 & $t27;
104 $t29 = $t28 ^ $t22;
105 $t30 = $t23 ^ $t24;
106 $t31 = $t22 ^ $t26;
107 $t32 = $t31 & $t30;
108 $t33 = $t32 ^ $t24;
109 $t34 = $t23 ^ $t33;
110 $t35 = $t27 ^ $t33;
111 $t36 = $t24 & $t35;
112 $t37 = $t36 ^ $t34;
113 $t38 = $t27 ^ $t36;
114 $t39 = $t29 & $t38;
115 $t40 = $t25 ^ $t39;
116
117 $t41 = $t40 ^ $t37;
118 $t42 = $t29 ^ $t33;
119 $t43 = $t29 ^ $t40;
120 $t44 = $t33 ^ $t37;
121 $t45 = $t42 ^ $t41;
122 $z0 = $t44 & $y15;
123 $z1 = $t37 & $y6;
124 $z2 = $t33 & $x7;
125 $z3 = $t43 & $y16;
126 $z4 = $t40 & $y1;
127 $z5 = $t29 & $y7;
128 $z6 = $t42 & $y11;
129 $z7 = $t45 & $y17;
130 $z8 = $t41 & $y10;
131 $z9 = $t44 & $y12;
132 $z10 = $t37 & $y3;
133 $z11 = $t33 & $y4;
134 $z12 = $t43 & $y13;
135 $z13 = $t40 & $y5;
136 $z14 = $t29 & $y2;
137 $z15 = $t42 & $y9;
138 $z16 = $t45 & $y14;
139 $z17 = $t41 & $y8;
140
141 /*
142 * Bottom linear transformation.
143 */
144 $t46 = $z15 ^ $z16;
145 $t47 = $z10 ^ $z11;
146 $t48 = $z5 ^ $z13;
147 $t49 = $z9 ^ $z10;
148 $t50 = $z2 ^ $z12;
149 $t51 = $z2 ^ $z5;
150 $t52 = $z7 ^ $z8;
151 $t53 = $z0 ^ $z3;
152 $t54 = $z6 ^ $z7;
153 $t55 = $z16 ^ $z17;
154 $t56 = $z12 ^ $t48;
155 $t57 = $t50 ^ $t53;
156 $t58 = $z4 ^ $t46;
157 $t59 = $z3 ^ $t54;
158 $t60 = $t46 ^ $t57;
159 $t61 = $z14 ^ $t57;
160 $t62 = $t52 ^ $t58;
161 $t63 = $t49 ^ $t58;
162 $t64 = $z4 ^ $t59;
163 $t65 = $t61 ^ $t62;
164 $t66 = $z1 ^ $t63;
165 $s0 = $t59 ^ $t63;
166 $s6 = $t56 ^ ~$t62;
167 $s7 = $t48 ^ ~$t60;
168 $t67 = $t64 ^ $t65;
169 $s3 = $t53 ^ $t66;
170 $s4 = $t51 ^ $t66;
171 $s5 = $t47 ^ $t65;
172 $s1 = $t64 ^ ~$s3;
173 $s2 = $t55 ^ ~$t67;
174
175 $q[7] = $s0 & self::U32_MAX;
176 $q[6] = $s1 & self::U32_MAX;
177 $q[5] = $s2 & self::U32_MAX;
178 $q[4] = $s3 & self::U32_MAX;
179 $q[3] = $s4 & self::U32_MAX;
180 $q[2] = $s5 & self::U32_MAX;
181 $q[1] = $s6 & self::U32_MAX;
182 $q[0] = $s7 & self::U32_MAX;
183 }
184
185 /**
186 * Mutates the values of $q!
187 *
188 * @param ParagonIE_Sodium_Core_AES_Block $q
189 * @return void
190 */
191 public static function invSbox(ParagonIE_Sodium_Core_AES_Block $q): void
192 {
193 self::processInversion($q);
194 self::sbox($q);
195 self::processInversion($q);
196 }
197
198 /**
199 * This is some boilerplate code needed to invert an S-box. Rather than repeat the code
200 * twice, I moved it to a protected method.
201 *
202 * Mutates $q
203 *
204 * @param ParagonIE_Sodium_Core_AES_Block $q
205 * @return void
206 */
207 protected static function processInversion(ParagonIE_Sodium_Core_AES_Block $q): void
208 {
209 $q0 = (~$q[0]) & self::U32_MAX;
210 $q1 = (~$q[1]) & self::U32_MAX;
211 $q2 = $q[2] & self::U32_MAX;
212 $q3 = $q[3] & self::U32_MAX;
213 $q4 = $q[4] & self::U32_MAX;
214 $q5 = (~$q[5]) & self::U32_MAX;
215 $q6 = (~$q[6]) & self::U32_MAX;
216 $q7 = $q[7] & self::U32_MAX;
217 $q[7] = ($q1 ^ $q4 ^ $q6) & self::U32_MAX;
218 $q[6] = ($q0 ^ $q3 ^ $q5) & self::U32_MAX;
219 $q[5] = ($q7 ^ $q2 ^ $q4) & self::U32_MAX;
220 $q[4] = ($q6 ^ $q1 ^ $q3) & self::U32_MAX;
221 $q[3] = ($q5 ^ $q0 ^ $q2) & self::U32_MAX;
222 $q[2] = ($q4 ^ $q7 ^ $q1) & self::U32_MAX;
223 $q[1] = ($q3 ^ $q6 ^ $q0) & self::U32_MAX;
224 $q[0] = ($q2 ^ $q5 ^ $q7) & self::U32_MAX;
225 }
226
227 /**
228 * @param int $x
229 * @return int
230 */
231 public static function subWord(int $x): int
232 {
233 $q = ParagonIE_Sodium_Core_AES_Block::fromArray(
234 array($x, $x, $x, $x, $x, $x, $x, $x)
235 );
236 $q->orthogonalize();
237 self::sbox($q);
238 $q->orthogonalize();
239 return $q[0] & self::U32_MAX;
240 }
241
242 /**
243 * Calculate the key schedule from a given random key
244 *
245 * @param string $key
246 * @return ParagonIE_Sodium_Core_AES_KeySchedule
247 * @throws SodiumException
248 */
249 public static function keySchedule(
250 #[SensitiveParameter]
251 string $key
252 ): ParagonIE_Sodium_Core_AES_KeySchedule {
253 $key_len = self::strlen($key);
254 $num_rounds = match ($key_len) {
255 16 => 10,
256 24 => 12,
257 32 => 14,
258 default => throw new SodiumException('Invalid key length: ' . $key_len),
259 };
260 $skey = array();
261 $comp_skey = array();
262 $nk = $key_len >> 2;
263 $nkf = ($num_rounds + 1) << 2;
264 $tmp = 0;
265
266 for ($i = 0; $i < $nk; ++$i) {
267 $tmp = self::load_4(self::substr($key, $i << 2, 4));
268 $skey[($i << 1)] = $tmp;
269 $skey[($i << 1) + 1] = $tmp;
270 }
271
272 for ($i = $nk, $j = 0, $k = 0; $i < $nkf; ++$i) {
273 if ($j === 0) {
274 $tmp = (($tmp & 0xff) << 24) | ($tmp >> 8);
275 /** @psalm-suppress InvalidArrayOffset */
276 $tmp = (self::subWord($tmp) ^ self::Rcon[$k]) & self::U32_MAX;
277 } elseif ($nk > 6 && $j === 4) {
278 $tmp = self::subWord($tmp);
279 }
280 $tmp ^= $skey[($i - $nk) << 1];
281 $skey[($i << 1)] = $tmp & self::U32_MAX;
282 $skey[($i << 1) + 1] = $tmp & self::U32_MAX;
283 if (++$j === $nk) {
284 /** @psalm-suppress LoopInvalidation */
285 $j = 0;
286 ++$k;
287 }
288 }
289 for ($i = 0; $i < $nkf; $i += 4) {
290 $q = ParagonIE_Sodium_Core_AES_Block::fromArray(
291 array_slice($skey, $i << 1, 8)
292 );
293 $q->orthogonalize();
294 // We have to overwrite $skey since we're not using C pointers like BearSSL did
295 for ($j = 0; $j < 8; ++$j) {
296 $skey[($i << 1) + $j] = $q[$j];
297 }
298 }
299 for ($i = 0, $j = 0; $i < $nkf; ++$i, $j += 2) {
300 $comp_skey[$i] = ($skey[$j] & 0x55555555)
301 | ($skey[$j + 1] & 0xAAAAAAAA);
302 }
303 return new ParagonIE_Sodium_Core_AES_KeySchedule($comp_skey, $num_rounds);
304 }
305
306 /**
307 * Mutates $q
308 *
309 * @param ParagonIE_Sodium_Core_AES_KeySchedule $skey
310 * @param ParagonIE_Sodium_Core_AES_Block $q
311 * @param int $offset
312 * @return void
313 */
314 public static function addRoundKey(
315 ParagonIE_Sodium_Core_AES_Block $q,
316 ParagonIE_Sodium_Core_AES_KeySchedule $skey,
317 int $offset = 0
318 ): void {
319 $block = $skey->getRoundKey($offset);
320 for ($j = 0; $j < 8; ++$j) {
321 $q[$j] = ($q[$j] ^ $block[$j]) & ParagonIE_Sodium_Core_Util::U32_MAX;
322 }
323 }
324
325 /**
326 * This mainly exists for testing, as we need the round key features for AEGIS.
327 *
328 * @param string $message
329 * @param string $key
330 * @return string
331 * @throws SodiumException
332 */
333 public static function decryptBlockECB(
334 string $message,
335 #[SensitiveParameter]
336 string $key
337 ): string {
338 if (self::strlen($message) !== 16) {
339 throw new SodiumException('decryptBlockECB() expects a 16 byte message');
340 }
341 $skey = self::keySchedule($key)->expand();
342 $q = ParagonIE_Sodium_Core_AES_Block::init();
343 $q[0] = self::load_4(self::substr($message, 0, 4));
344 $q[2] = self::load_4(self::substr($message, 4, 4));
345 $q[4] = self::load_4(self::substr($message, 8, 4));
346 $q[6] = self::load_4(self::substr($message, 12, 4));
347
348 $q->orthogonalize();
349 self::bitsliceDecryptBlock($skey, $q);
350 $q->orthogonalize();
351
352 return self::store32_le($q[0]) .
353 self::store32_le($q[2]) .
354 self::store32_le($q[4]) .
355 self::store32_le($q[6]);
356 }
357
358 /**
359 * This mainly exists for testing, as we need the round key features for AEGIS.
360 *
361 * @param string $message
362 * @param string $key
363 * @return string
364 * @throws SodiumException
365 */
366 public static function encryptBlockECB(
367 #[SensitiveParameter]
368 string $message,
369 #[SensitiveParameter]
370 string $key
371 ): string {
372 if (self::strlen($message) !== 16) {
373 throw new SodiumException('encryptBlockECB() expects a 16 byte message');
374 }
375 $comp_skey = self::keySchedule($key);
376 $skey = $comp_skey->expand();
377 $q = ParagonIE_Sodium_Core_AES_Block::init();
378 $q[0] = self::load_4(self::substr($message, 0, 4));
379 $q[2] = self::load_4(self::substr($message, 4, 4));
380 $q[4] = self::load_4(self::substr($message, 8, 4));
381 $q[6] = self::load_4(self::substr($message, 12, 4));
382
383 $q->orthogonalize();
384 self::bitsliceEncryptBlock($skey, $q);
385 $q->orthogonalize();
386
387 return self::store32_le($q[0]) .
388 self::store32_le($q[2]) .
389 self::store32_le($q[4]) .
390 self::store32_le($q[6]);
391 }
392
393 /**
394 * Mutates $q
395 *
396 * @param ParagonIE_Sodium_Core_AES_Expanded $skey
397 * @param ParagonIE_Sodium_Core_AES_Block $q
398 * @return void
399 */
400 public static function bitsliceEncryptBlock(
401 ParagonIE_Sodium_Core_AES_Expanded $skey,
402 ParagonIE_Sodium_Core_AES_Block $q
403 ): void {
404 self::addRoundKey($q, $skey);
405 for ($u = 1; $u < $skey->getNumRounds(); ++$u) {
406 self::sbox($q);
407 $q->shiftRows();
408 $q->mixColumns();
409 self::addRoundKey($q, $skey, ($u << 3));
410 }
411 self::sbox($q);
412 $q->shiftRows();
413 self::addRoundKey($q, $skey, ($skey->getNumRounds() << 3));
414 }
415
416 /**
417 * @param string $x
418 * @param string $y
419 * @return string
420 */
421 public static function aesRound(
422 #[SensitiveParameter]
423 string $x,
424 #[SensitiveParameter]
425 string $y
426 ): string {
427 $q = ParagonIE_Sodium_Core_AES_Block::init();
428 $q[0] = self::load_4(self::substr($x, 0, 4));
429 $q[2] = self::load_4(self::substr($x, 4, 4));
430 $q[4] = self::load_4(self::substr($x, 8, 4));
431 $q[6] = self::load_4(self::substr($x, 12, 4));
432
433 $rk = ParagonIE_Sodium_Core_AES_Block::init();
434 $rk[0] = $rk[1] = self::load_4(self::substr($y, 0, 4));
435 $rk[2] = $rk[3] = self::load_4(self::substr($y, 4, 4));
436 $rk[4] = $rk[5] = self::load_4(self::substr($y, 8, 4));
437 $rk[6] = $rk[7] = self::load_4(self::substr($y, 12, 4));
438
439 $q->orthogonalize();
440 self::sbox($q);
441 $q->shiftRows();
442 $q->mixColumns();
443 $q->orthogonalize();
444 // add round key without key schedule:
445 for ($i = 0; $i < 8; ++$i) {
446 $q[$i] ^= $rk[$i];
447 }
448 return self::store32_le($q[0]) .
449 self::store32_le($q[2]) .
450 self::store32_le($q[4]) .
451 self::store32_le($q[6]);
452 }
453
454 /**
455 * Process two AES blocks in one shot.
456 *
457 * @param string $b0 First AES block
458 * @param string $rk0 First round key
459 * @param string $b1 Second AES block
460 * @param string $rk1 Second round key
461 * @return string[]
462 */
463 public static function doubleRound(
464 #[SensitiveParameter]
465 string $b0,
466 #[SensitiveParameter]
467 string $rk0,
468 #[SensitiveParameter]
469 string $b1,
470 #[SensitiveParameter]
471 string $rk1
472 ): array {
473 $q = ParagonIE_Sodium_Core_AES_Block::init();
474 // First block
475 $q[0] = self::load_4(self::substr($b0, 0, 4));
476 $q[2] = self::load_4(self::substr($b0, 4, 4));
477 $q[4] = self::load_4(self::substr($b0, 8, 4));
478 $q[6] = self::load_4(self::substr($b0, 12, 4));
479 // Second block
480 $q[1] = self::load_4(self::substr($b1, 0, 4));
481 $q[3] = self::load_4(self::substr($b1, 4, 4));
482 $q[5] = self::load_4(self::substr($b1, 8, 4));
483 $q[7] = self::load_4(self::substr($b1, 12, 4));
484
485 $rk = ParagonIE_Sodium_Core_AES_Block::init();
486 // First round key
487 $rk[0] = self::load_4(self::substr($rk0, 0, 4));
488 $rk[2] = self::load_4(self::substr($rk0, 4, 4));
489 $rk[4] = self::load_4(self::substr($rk0, 8, 4));
490 $rk[6] = self::load_4(self::substr($rk0, 12, 4));
491 // Second round key
492 $rk[1] = self::load_4(self::substr($rk1, 0, 4));
493 $rk[3] = self::load_4(self::substr($rk1, 4, 4));
494 $rk[5] = self::load_4(self::substr($rk1, 8, 4));
495 $rk[7] = self::load_4(self::substr($rk1, 12, 4));
496
497 $q->orthogonalize();
498 self::sbox($q);
499 $q->shiftRows();
500 $q->mixColumns();
501 $q->orthogonalize();
502 // add round key without key schedule:
503 for ($i = 0; $i < 8; ++$i) {
504 $q[$i] ^= $rk[$i];
505 }
506 return array(
507 self::store32_le($q[0]) . self::store32_le($q[2]) . self::store32_le($q[4]) . self::store32_le($q[6]),
508 self::store32_le($q[1]) . self::store32_le($q[3]) . self::store32_le($q[5]) . self::store32_le($q[7]),
509 );
510 }
511
512 /**
513 * @param ParagonIE_Sodium_Core_AES_Expanded $skey
514 * @param ParagonIE_Sodium_Core_AES_Block $q
515 * @return void
516 */
517 public static function bitsliceDecryptBlock(
518 ParagonIE_Sodium_Core_AES_Expanded $skey,
519 ParagonIE_Sodium_Core_AES_Block $q
520 ): void {
521 self::addRoundKey($q, $skey, ($skey->getNumRounds() << 3));
522 for ($u = $skey->getNumRounds() - 1; $u > 0; --$u) {
523 $q->inverseShiftRows();
524 self::invSbox($q);
525 self::addRoundKey($q, $skey, ($u << 3));
526 $q->inverseMixColumns();
527 }
528 $q->inverseShiftRows();
529 self::invSbox($q);
530 self::addRoundKey($q, $skey, ($u << 3));
531 }
532 }