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 |
||
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 |
||
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) |
||
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 |
||
134 | |||
135 | /** |
||
136 | * @param string $kek The Key Encryption Key |
||
137 | * @param string $key The key to unwrap |
||
138 | * @param bool $padding_enabled If false, the AIV check must be RFC3394 compliant, else it must be RFC5649 or RFC3394 compliant |
||
139 | * |
||
140 | * @return string The key unwrapped |
||
141 | */ |
||
142 | public static function unwrap(string $kek, string $key, bool $padding_enabled = false): string |
||
175 | |||
176 | /** |
||
177 | * @return int |
||
178 | */ |
||
179 | abstract protected static function getExpectedKEKSize(): int; |
||
180 | |||
181 | /** |
||
182 | * @param int $bits |
||
183 | * @param int $value |
||
184 | * |
||
185 | * @return string |
||
186 | */ |
||
187 | private static function toXBits(int $bits, int $value): string |
||
191 | |||
192 | /** |
||
193 | * @param string $value |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | private static function getMSB(string $value): string |
||
201 | |||
202 | /** |
||
203 | * @param string $value |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | private static function getLSB(string $value): string |
||
211 | |||
212 | /** |
||
213 | * {@inheritdoc} |
||
214 | */ |
||
215 | private static function encrypt(string $kek, string $data): string |
||
219 | |||
220 | /** |
||
221 | * {@inheritdoc} |
||
222 | */ |
||
223 | private static function decrypt(string $kek, string $data): string |
||
227 | |||
228 | /** |
||
229 | * @param string $kek The Key Encryption Key |
||
230 | */ |
||
231 | private static function checkKEKSize(string $kek) |
||
237 | |||
238 | /** |
||
239 | * @param string $kek |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | private static function getMethod(string $kek): string |
||
247 | } |
||
248 |