Debian Codegraph @

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

raw symbols SCIP
1 <?php
2 declare(strict_types=1);
3
4 if (!defined('SODIUM_COMPAT_AEGIS_C0')) {
5 define('SODIUM_COMPAT_AEGIS_C0', "\x00\x01\x01\x02\x03\x05\x08\x0d\x15\x22\x37\x59\x90\xe9\x79\x62");
6 }
7 if (!defined('SODIUM_COMPAT_AEGIS_C1')) {
8 define('SODIUM_COMPAT_AEGIS_C1', "\xdb\x3d\x18\x55\x6d\xc2\x2f\xf1\x20\x11\x31\x42\x73\xb5\x28\xdd");
9 }
10
11 class ParagonIE_Sodium_Core_AEGIS128L extends ParagonIE_Sodium_Core_AES
12 {
13 /**
14 * @param string $ct
15 * @param string $tag
16 * @param string $ad
17 * @param string $key
18 * @param string $nonce
19 * @return string
20 * @throws SodiumException
21 */
22 public static function decrypt(
23 string $ct,
24 string $tag,
25 string $ad,
26 #[SensitiveParameter]
27 string $key,
28 string $nonce
29 ): string {
30 $state = self::init($key, $nonce);
31 $ad_blocks = (self::strlen($ad) + 31) >> 5;
32 for ($i = 0; $i < $ad_blocks; ++$i) {
33 $ai = self::substr($ad, $i << 5, 32);
34 if (self::strlen($ai) < 32) {
35 $ai = str_pad($ai, 32, "\0");
36 }
37 $state->absorb($ai);
38 }
39
40 $msg = '';
41 $cn = self::strlen($ct) & 31;
42 $ct_blocks = self::strlen($ct) >> 5;
43 for ($i = 0; $i < $ct_blocks; ++$i) {
44 $msg .= $state->dec(self::substr($ct, $i << 5, 32));
45 }
46 if ($cn) {
47 $start = $ct_blocks << 5;
48 $msg .= $state->decPartial(self::substr($ct, $start, $cn));
49 }
50 $expected_tag = $state->finalize(
51 self::strlen($ad) << 3,
52 self::strlen($msg) << 3
53 );
54 if (!self::hashEquals($expected_tag, $tag)) {
55 try {
56 // The RFC says to erase msg, so we shall try:
57 ParagonIE_Sodium_Compat::memzero($msg);
58 } catch (SodiumException) {
59 // Do nothing if we cannot memzero
60 }
61 throw new SodiumException('verification failed');
62 }
63 return $msg;
64 }
65
66 /**
67 * @param string $msg
68 * @param string $ad
69 * @param string $key
70 * @param string $nonce
71 * @return array
72 *
73 * @throws SodiumException
74 */
75 public static function encrypt(
76 #[SensitiveParameter]
77 string $msg,
78 string $ad,
79 #[SensitiveParameter]
80 string $key,
81 string $nonce
82 ): array {
83 $state = self::init($key, $nonce);
84 // ad_blocks = Split(ZeroPad(ad, 256), 256)
85 // for ai in ad_blocks:
86 // Absorb(ai)
87 $ad_len = self::strlen($ad);
88 $msg_len = self::strlen($msg);
89 $ad_blocks = ($ad_len + 31) >> 5;
90 for ($i = 0; $i < $ad_blocks; ++$i) {
91 $ai = self::substr($ad, $i << 5, 32);
92 if (self::strlen($ai) < 32) {
93 $ai = str_pad($ai, 32, "\0");
94 }
95 $state->absorb($ai);
96 }
97
98 // msg_blocks = Split(ZeroPad(msg, 256), 256)
99 // for xi in msg_blocks:
100 // ct = ct || Enc(xi)
101 $ct = '';
102 $msg_blocks = ($msg_len + 31) >> 5;
103 for ($i = 0; $i < $msg_blocks; ++$i) {
104 $xi = self::substr($msg, $i << 5, 32);
105 if (self::strlen($xi) < 32) {
106 $xi = str_pad($xi, 32, "\0");
107 }
108 $ct .= $state->enc($xi);
109 }
110 // tag = Finalize(|ad|, |msg|)
111 // ct = Truncate(ct, |msg|)
112 $tag = $state->finalize(
113 $ad_len << 3,
114 $msg_len << 3
115 );
116 // return ct and tag
117 return array(
118 self::substr($ct, 0, $msg_len),
119 $tag
120 );
121 }
122
123 /**
124 * @param string $key
125 * @param string $nonce
126 * @return ParagonIE_Sodium_Core_AEGIS_State128L
127 */
128 public static function init(string $key, string $nonce): ParagonIE_Sodium_Core_AEGIS_State128L
129 {
130 return ParagonIE_Sodium_Core_AEGIS_State128L::init($key, $nonce);
131 }
132 }