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