Complex classes like Algorithm often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Algorithm, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | abstract class Algorithm implements AESKeyWrapAlgorithm |
||
13 | { |
||
14 | /** |
||
15 | * Default initial value. |
||
16 | * |
||
17 | * @link https://tools.ietf.org/html/rfc3394#section-2.2.3.1 |
||
18 | * @var string |
||
19 | */ |
||
20 | const DEFAULT_IV = "\xA6\xA6\xA6\xA6\xA6\xA6\xA6\xA6"; |
||
21 | |||
22 | /** |
||
23 | * High order bytes of the alternative initial value for padding. |
||
24 | * |
||
25 | * @link https://tools.ietf.org/html/rfc5649#section-3 |
||
26 | * @var string |
||
27 | */ |
||
28 | const AIV_HI = "\xA6\x59\x59\xA6"; |
||
29 | |||
30 | /** |
||
31 | * Initial value. |
||
32 | * |
||
33 | * @var string $_iv |
||
34 | */ |
||
35 | protected $_iv; |
||
36 | |||
37 | /** |
||
38 | * Get OpenSSL cipher method. |
||
39 | * |
||
40 | * @return string |
||
41 | */ |
||
42 | abstract protected function _cipherMethod(): string; |
||
43 | |||
44 | /** |
||
45 | * Get key encryption key size. |
||
46 | * |
||
47 | * @return int |
||
48 | */ |
||
49 | abstract protected function _keySize(): int; |
||
50 | |||
51 | /** |
||
52 | * Constructor. |
||
53 | * |
||
54 | * @param string $iv Initial value |
||
55 | */ |
||
56 | 48 | public function __construct(string $iv = self::DEFAULT_IV) |
|
63 | |||
64 | /** |
||
65 | * Wrap a key using given key encryption key. |
||
66 | * |
||
67 | * Key length must be at least 64 bits (8 octets) and a multiple |
||
68 | * of 64 bits (8 octets). |
||
69 | * Use <i>wrapPad</i> to wrap a key of arbitrary length. |
||
70 | * |
||
71 | * Key encryption key must have a size of underlying AES algorithm, |
||
72 | * ie. 128, 196 or 256 bits. |
||
73 | * |
||
74 | * @param string $key Key to wrap |
||
75 | * @param string $kek Key encryption key |
||
76 | * @throws \UnexpectedValueException If the key length is invalid |
||
77 | * @return string Ciphertext |
||
78 | */ |
||
79 | 13 | public function wrap(string $key, string $kek): string |
|
101 | |||
102 | /** |
||
103 | * Unwrap a key from a ciphertext using given key encryption key. |
||
104 | * |
||
105 | * @param string $ciphertext Ciphertext of the wrapped key |
||
106 | * @param string $kek Key encryption key |
||
107 | * @throws \UnexpectedValueException If the ciphertext is invalid |
||
108 | * @return string Unwrapped key |
||
109 | */ |
||
110 | 12 | public function unwrap(string $ciphertext, string $kek): string |
|
128 | |||
129 | /** |
||
130 | * Wrap a key of arbitrary length using given key encryption key. |
||
131 | * |
||
132 | * This variant of wrapping does not place any restriction on key size. |
||
133 | * |
||
134 | * Key encryption key has the same restrictions as with <i>wrap</i> method. |
||
135 | * |
||
136 | * @param string $key Key to wrap |
||
137 | * @param string $kek Key encryption key |
||
138 | * @throws \UnexpectedValueException If the key length is invalid |
||
139 | * @return string Ciphertext |
||
140 | */ |
||
141 | 19 | public function wrapPad(string $key, string $kek): string |
|
165 | |||
166 | /** |
||
167 | * Unwrap a key from a padded ciphertext using given key encryption key. |
||
168 | * |
||
169 | * This variant of unwrapping must be used if the key was wrapped using |
||
170 | * <i>wrapPad</i>. |
||
171 | * |
||
172 | * @param string $ciphertext Ciphertext of the wrapped and padded key |
||
173 | * @param string $kek Key encryption key |
||
174 | * @throws \UnexpectedValueException If the ciphertext is invalid |
||
175 | * @return string Unwrapped key |
||
176 | */ |
||
177 | 15 | public function unwrapPad(string $ciphertext, string $kek): string |
|
192 | |||
193 | /** |
||
194 | * Check KEK size. |
||
195 | * |
||
196 | * @param string $kek |
||
197 | * @throws \UnexpectedValueException |
||
198 | * @return self |
||
199 | */ |
||
200 | 38 | protected function _checkKEKSize(string $kek): self |
|
208 | |||
209 | /** |
||
210 | * Apply Key Wrap to data blocks. |
||
211 | * |
||
212 | * Uses alternative version of the key wrap procedure described in the RFC. |
||
213 | * |
||
214 | * @link https://tools.ietf.org/html/rfc3394#section-2.2.1 |
||
215 | * @param string[] $P Plaintext, n 64-bit values <code>{P1, P2, ..., |
||
216 | * Pn}</code> |
||
217 | * @param string $kek Key encryption key |
||
218 | * @param string $iv Initial value |
||
219 | * @return string[] Ciphertext, (n+1) 64-bit values <code>{C0, C1, ..., |
||
220 | * Cn}</code> |
||
221 | */ |
||
222 | 17 | protected function _wrapBlocks(array $P, string $kek, string $iv): array |
|
252 | |||
253 | /** |
||
254 | * Unwrap the padded ciphertext producing plaintext and integrity value. |
||
255 | * |
||
256 | * @param string $ciphertext Ciphertext |
||
257 | * @param string $kek Encryption key |
||
258 | * @return array Tuple of plaintext <code>{P1, P2, ..., Pn}</code> and |
||
259 | * integrity value <code>A</code> |
||
260 | */ |
||
261 | 13 | protected function _unwrapPaddedCiphertext(string $ciphertext, string $kek): array |
|
279 | |||
280 | /** |
||
281 | * Apply Key Unwrap to data blocks. |
||
282 | * |
||
283 | * Uses the index based version of key unwrap procedure |
||
284 | * described in the RFC. |
||
285 | * |
||
286 | * Does not compute step 3. |
||
287 | * |
||
288 | * @link https://tools.ietf.org/html/rfc3394#section-2.2.2 |
||
289 | * @param string[] $C Ciphertext, (n+1) 64-bit values <code>{C0, C1, ..., |
||
290 | * Cn}</code> |
||
291 | * @param string $kek Key encryption key |
||
292 | * @throws \UnexpectedValueException |
||
293 | * @return array Tuple of integrity value <code>A</code> and register |
||
294 | * <code>R</code> |
||
295 | */ |
||
296 | 15 | protected function _unwrapBlocks(array $C, string $kek): array |
|
322 | |||
323 | /** |
||
324 | * Pad a key with zeroes and compute alternative initial value. |
||
325 | * |
||
326 | * @param string $key Key |
||
327 | * @return array Tuple of padded key and AIV |
||
328 | */ |
||
329 | 17 | protected function _padKey(string $key): array |
|
341 | |||
342 | /** |
||
343 | * Check that the integrity check value of the padded key is correct. |
||
344 | * |
||
345 | * @param string $A |
||
346 | * @throws \UnexpectedValueException |
||
347 | */ |
||
348 | 13 | protected function _checkPaddedIntegrity(string $A) |
|
355 | |||
356 | /** |
||
357 | * Verify that the padding of the plaintext is valid. |
||
358 | * |
||
359 | * @param array $P Plaintext, n 64-bit values <code>{P1, P2, ..., |
||
360 | * Pn}</code> |
||
361 | * @param string $A Integrity check value |
||
362 | * @throws \UnexpectedValueException |
||
363 | * @return int Message length without padding |
||
364 | */ |
||
365 | 12 | protected function _verifyPadding(array $P, string $A): int |
|
387 | |||
388 | /** |
||
389 | * Apply AES(K, W) operation (encrypt) to 64 bit block. |
||
390 | * |
||
391 | * @param string $kek |
||
392 | * @param string $block |
||
393 | * @throws \RuntimeException If encrypt fails |
||
394 | * @return string |
||
395 | */ |
||
396 | 26 | protected function _encrypt(string $kek, string $block): string |
|
406 | |||
407 | /** |
||
408 | * Apply AES-1(K, W) operation (decrypt) to 64 bit block. |
||
409 | * |
||
410 | * @param string $kek |
||
411 | * @param string $block |
||
412 | * @throws \RuntimeException If decrypt fails |
||
413 | * @return string |
||
414 | */ |
||
415 | 23 | protected function _decrypt(string $kek, string $block): string |
|
425 | |||
426 | /** |
||
427 | * Get the latest OpenSSL error message. |
||
428 | * |
||
429 | * @return string |
||
430 | */ |
||
431 | 2 | protected function _getLastOpenSSLError(): string |
|
439 | |||
440 | /** |
||
441 | * Take 64 most significant bits from value. |
||
442 | * |
||
443 | * @param string $val |
||
444 | * @return string |
||
445 | */ |
||
446 | 20 | protected function _msb64(string $val): string |
|
450 | |||
451 | /** |
||
452 | * Take 64 least significant bits from value. |
||
453 | * |
||
454 | * @param string $val |
||
455 | * @return string |
||
456 | */ |
||
457 | 20 | protected function _lsb64(string $val): string |
|
461 | |||
462 | /** |
||
463 | * Convert number to 64 bit unsigned integer octet string with |
||
464 | * most significant bit first. |
||
465 | * |
||
466 | * @param int $num |
||
467 | * @return string |
||
468 | */ |
||
469 | 20 | protected function _uint64(int $num): string |
|
477 | } |
||
478 |