Debian Codegraph @

lib/3rdParty/composer/paragonie/sodium_compat/src/Core/AEGIS256.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_AEGIS256 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
32 // ad_blocks = Split(ZeroPad(ad, 128), 128)
33 $ad_blocks = (self::strlen($ad) + 15) >> 4;
34 // for ai in ad_blocks:
35 // Absorb(ai)
36 for ($i = 0; $i < $ad_blocks; ++$i) {
37 $ai = self::substr($ad, $i << 4, 16);
38 if (self::strlen($ai) < 16) {
39 $ai = str_pad($ai, 16, "\0");
40 }
41 $state->absorb($ai);
42 }
43
44 $msg = '';
45 $cn = self::strlen($ct) & 15;
46 $ct_blocks = self::strlen($ct) >> 4;
47 // ct_blocks = Split(ZeroPad(ct, 128), 128)
48 // cn = Tail(ct, |ct| mod 128)
49 for ($i = 0; $i < $ct_blocks; ++$i) {
50 $msg .= $state->dec(self::substr($ct, $i << 4, 16));
51 }
52 // if cn is not empty:
53 // msg = msg || DecPartial(cn)
54 if ($cn) {
55 $start = $ct_blocks << 4;
56 $msg .= $state->decPartial(self::substr($ct, $start, $cn));
57 }
58 $expected_tag = $state->finalize(
59 self::strlen($ad) << 3,
60 self::strlen($msg) << 3
61 );
62 if (!self::hashEquals($expected_tag, $tag)) {
63 try {
64 // The RFC says to erase msg, so we shall try:
65 ParagonIE_Sodium_Compat::memzero($msg);
66 } catch (SodiumException) {
67 // Do nothing if we cannot memzero
68 }
69 throw new SodiumException('verification failed');
70 }
71 return $msg;
72 }
73
74 /**
75 * @param string $msg
76 * @param string $ad
77 * @param string $key
78 * @param string $nonce
79 * @return array
80 * @throws SodiumException
81 */
82 public static function encrypt(
83 #[SensitiveParameter]
84 string $msg,
85 string $ad,
86 #[SensitiveParameter]
87 string $key,
88 string $nonce
89 ): array {
90 $state = self::init($key, $nonce);
91 $ad_len = self::strlen($ad);
92 $msg_len = self::strlen($msg);
93 $ad_blocks = ($ad_len + 15) >> 4;
94 for ($i = 0; $i < $ad_blocks; ++$i) {
95 $ai = self::substr($ad, $i << 4, 16);
96 if (self::strlen($ai) < 16) {
97 $ai = str_pad($ai, 16, "\0");
98 }
99 $state->absorb($ai);
100 }
101
102 $ct = '';
103 $msg_blocks = ($msg_len + 15) >> 4;
104 for ($i = 0; $i < $msg_blocks; ++$i) {
105 $xi = self::substr($msg, $i << 4, 16);
106 if (self::strlen($xi) < 16) {
107 $xi = str_pad($xi, 16, "\0");
108 }
109 $ct .= $state->enc($xi);
110 }
111 $tag = $state->finalize(
112 $ad_len << 3,
113 $msg_len << 3
114 );
115 return array(
116 self::substr($ct, 0, $msg_len),
117 $tag
118 );
119
120 }
121
122 /**
123 * @param string $key
124 * @param string $nonce
125 * @return ParagonIE_Sodium_Core_AEGIS_State256
126 */
127 public static function init(
128 #[SensitiveParameter]
129 string $key,
130 string $nonce
131 ): ParagonIE_Sodium_Core_AEGIS_State256 {
132 return ParagonIE_Sodium_Core_AEGIS_State256::init($key, $nonce);
133 }
134 }