Debian Codegraph @

lib/3rdParty/composer/spomky-labs/aes-key-wrap/src/AESKW.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace AESKW;
6
7 use function count;
8 use function hex2bin;
9 use InvalidArgumentException;
10 use function mb_str_split;
11 use function openssl_decrypt;
12 use function openssl_encrypt;
13 use const OPENSSL_RAW_DATA;
14 use const OPENSSL_ZERO_PADDING;
15 use const STR_PAD_LEFT;
16 use const STR_PAD_RIGHT;
17
18 trait AESKW
19 {
20 /**
21 * @param string $kek The Key Encryption Key
22 * @param string $key The key to wrap
23 * @param bool $padding_enabled If false, the key to wrap must be a sequence of one or more 64-bit blocks (RFC3394 compliant), else the key size must be at least one octet (RFC5649 compliant)
24 *
25 * @return string The wrapped key
26 */
27 public static function wrap(string $kek, string $key, bool $padding_enabled = false): string
28 {
29 $A = self::getInitialValue($key, $padding_enabled);
30 self::checkKeySize($key, $padding_enabled);
31 $P = mb_str_split($key, 8, '8bit');
32 $N = count($P);
33 $C = [];
34
35 if ($N === 1) {
36 $B = self::encrypt($kek, $A . $P[0]);
37 $C[0] = self::getMSB($B);
38 $C[1] = self::getLSB($B);
39 } elseif ($N > 1) {
40 $R = $P;
41 for ($j = 0; $j <= 5; ++$j) {
42 for ($i = 1; $i <= $N; ++$i) {
43 $B = self::encrypt($kek, $A . $R[$i - 1]);
44 $t = $i + $j * $N;
45 $A = self::toXBits(64, $t) ^ self::getMSB($B);
46 $R[$i - 1] = self::getLSB($B);
47 }
48 }
49 $C = array_merge([$A], $R);
50 }
51
52 return implode('', $C);
53 }
54
55 /**
56 * @param string $kek The Key Encryption Key
57 * @param string $key The key to unwrap
58 * @param bool $padding_enabled If false, the AIV check must be RFC3394 compliant, else it must be RFC5649 or RFC3394 compliant
59 *
60 * @return string The key unwrapped
61 */
62 public static function unwrap(string $kek, string $key, bool $padding_enabled = false): string
63 {
64 $P = mb_str_split($key, 8, '8bit');
65 $A = $P[0];
66 $N = count($P);
67
68 if ($N < 2) {
69 throw new InvalidArgumentException('Bad data');
70 }
71 if ($N === 2) {
72 $B = self::decrypt($kek, $P[0] . $P[1]);
73 $unwrapped = self::getLSB($B);
74 $A = self::getMSB($B);
75 } else {
76 $R = $P;
77 for ($j = 5; $j >= 0; --$j) {
78 for ($i = $N - 1; $i >= 1; --$i) {
79 $t = $i + $j * ($N - 1);
80 $B = self::decrypt($kek, (self::toXBits(64, $t) ^ $A) . $R[$i]);
81 $A = self::getMSB($B);
82 $R[$i] = self::getLSB($B);
83 }
84 }
85 unset($R[0]);
86
87 $unwrapped = implode('', $R);
88 }
89 if (self::checkInitialValue($unwrapped, $padding_enabled, $A) === false) {
90 throw new InvalidArgumentException('Integrity check failed!');
91 }
92
93 return $unwrapped;
94 }
95
96 /**
97 * The initial value used to wrap the key and check the integrity when unwrapped. The RFC3394 set this value to
98 * 0xA6A6A6A6A6A6A6A6 The RFC5649 set this value to 0xA65959A6XXXXXXXX (The part with XXXXXXXX is the MLI, depends
99 * on the padding).
100 *
101 * @param string $key The key
102 * @param bool $padding_enabled Enable padding (RFC5649)
103 *
104 * @see https://tools.ietf.org/html/rfc3394#section-2.2.3.1
105 */
106 private static function getInitialValue(string &$key, bool $padding_enabled): string
107 {
108 if ($padding_enabled === false) {
109 $bin = hex2bin('A6A6A6A6A6A6A6A6');
110 if ($bin === false) {
111 throw new InvalidArgumentException('Unable to convert the data');
112 }
113
114 return $bin;
115 }
116
117 $MLI = mb_strlen($key, '8bit');
118 $iv = hex2bin('A65959A6') . self::toXBits(32, $MLI);
119
120 $n = (int) ceil($MLI / 8);
121 $key = str_pad($key, 8 * $n, "\0", STR_PAD_RIGHT);
122
123 return $iv;
124 }
125
126 private static function checkInitialValue(string &$key, bool $padding_enabled, string $iv): bool
127 {
128 // RFC3394 compliant
129 if ($iv === hex2bin('A6A6A6A6A6A6A6A6')) {
130 return true;
131 }
132
133 // The RFC3394 is required but the previous check is not satisfied => invalid
134 if ($padding_enabled === false) {
135 return false;
136 }
137
138 // The high-order half of the AIV according to the RFC5649
139 if (hex2bin('A65959A6') !== self::getMSB($iv)) {
140 return false;
141 }
142
143 $n = mb_strlen($key, '8bit') / 8;
144 $MLI = (int) hexdec(bin2hex(ltrim(self::getLSB($iv), "\0")));
145
146 if (! (8 * ($n - 1) < $MLI && $MLI <= 8 * $n)) {
147 return false;
148 }
149
150 $b = 8 * $n - $MLI;
151 for ($i = 0; $i < $b; ++$i) {
152 if (mb_substr($key, $MLI + $i, 1, '8bit') !== "\0") {
153 return false;
154 }
155 }
156 $key = mb_substr($key, 0, $MLI, '8bit');
157
158 return true;
159 }
160
161 private static function checkKeySize(string $key, bool $padding_enabled): void
162 {
163 if ($key === '') {
164 throw new InvalidArgumentException('Bad key size');
165 }
166 if ($padding_enabled === false && mb_strlen($key, '8bit') % 8 !== 0) {
167 throw new InvalidArgumentException('Bad key size');
168 }
169 }
170
171 private static function toXBits(int $bits, int $value): string
172 {
173 $bin = hex2bin(str_pad(dechex($value), $bits / 4, '0', STR_PAD_LEFT));
174 if ($bin === false) {
175 throw new InvalidArgumentException('Unable to convert the data');
176 }
177
178 return $bin;
179 }
180
181 private static function getMSB(string $value): string
182 {
183 return mb_substr($value, 0, mb_strlen($value, '8bit') / 2, '8bit');
184 }
185
186 private static function getLSB(string $value): string
187 {
188 return mb_substr($value, mb_strlen($value, '8bit') / 2, null, '8bit');
189 }
190
191 private static function encrypt(string $kek, string $data): string
192 {
193 $result = openssl_encrypt($data, self::getMethod(), $kek, OPENSSL_ZERO_PADDING | OPENSSL_RAW_DATA);
194 if ($result === false) {
195 throw new InvalidArgumentException('Unable to encrypt the data');
196 }
197
198 return $result;
199 }
200
201 private static function decrypt(string $kek, string $data): string
202 {
203 $result = openssl_decrypt($data, self::getMethod(), $kek, OPENSSL_ZERO_PADDING | OPENSSL_RAW_DATA);
204 if ($result === false) {
205 throw new InvalidArgumentException('Unable to decrypt the data');
206 }
207
208 return $result;
209 }
210 }