Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 11 | abstract class Algorithm implements |
||
| 12 | 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(); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Get key encryption key size. |
||
| 46 | * |
||
| 47 | * @return int |
||
| 48 | */ |
||
| 49 | abstract protected function _keySize(); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructor |
||
| 53 | * |
||
| 54 | * @param string $iv Initial value |
||
| 55 | */ |
||
| 56 | 48 | public function __construct($iv = self::DEFAULT_IV) { |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Wrap a key using given key encryption key. |
||
| 65 | * |
||
| 66 | * Key length must be at least 64 bits (8 octets) and a multiple |
||
| 67 | * of 64 bits (8 octets). |
||
| 68 | * Use <i>wrapPad</i> to wrap a key of arbitrary length. |
||
| 69 | * |
||
| 70 | * Key encryption key must have a size of underlying AES algorithm, |
||
| 71 | * ie. 128, 196 or 256 bits. |
||
| 72 | * |
||
| 73 | * @param string $key Key to wrap |
||
| 74 | * @param string $kek Key encryption key |
||
| 75 | * @throws \UnexpectedValueException |
||
| 76 | * @return string Ciphertext |
||
| 77 | */ |
||
| 78 | 13 | public function wrap($key, $kek) { |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Unwrap a key from a ciphertext using given key encryption key. |
||
| 102 | * |
||
| 103 | * @param string $ciphertext Ciphertext of the wrapped key |
||
| 104 | * @param string $kek Key encryption key |
||
| 105 | * @throws \UnexpectedValueException |
||
| 106 | * @return string Unwrapped key |
||
| 107 | */ |
||
| 108 | 12 | public function unwrap($ciphertext, $kek) { |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Wrap a key of arbitrary length using given key encryption key. |
||
| 128 | * |
||
| 129 | * This variant of wrapping does not place any restriction on key size. |
||
| 130 | * |
||
| 131 | * Key encryption key has the same restrictions as with <i>wrap</i> method. |
||
| 132 | * |
||
| 133 | * @param string $key Key to wrap |
||
| 134 | * @param string $kek Key encryption key |
||
| 135 | * @return string Ciphertext |
||
| 136 | */ |
||
| 137 | 19 | public function wrapPad($key, $kek) { |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Unwrap a key from a padded ciphertext using given key encryption key. |
||
| 170 | * |
||
| 171 | * This variant of unwrapping must be used if the key was wrapped using |
||
| 172 | * <i>wrapPad</i>. |
||
| 173 | * |
||
| 174 | * @param string $ciphertext Ciphertext of the wrapped and padded key |
||
| 175 | * @param string $kek Key encryption key |
||
| 176 | * @throws \UnexpectedValueException |
||
| 177 | * @throws \RangeException |
||
| 178 | * @return string Unwrapped key |
||
| 179 | */ |
||
| 180 | 15 | public function unwrapPad($ciphertext, $kek) { |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Check KEK size. |
||
| 197 | * |
||
| 198 | * @param string $kek |
||
| 199 | * @throws \UnexpectedValueException |
||
| 200 | * @return self |
||
| 201 | */ |
||
| 202 | 38 | protected function _checkKEKSize($kek) { |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Apply Key Wrap to data blocks. |
||
| 212 | * |
||
| 213 | * Uses alternative version of the key wrap procedure described in the RFC. |
||
| 214 | * |
||
| 215 | * @link https://tools.ietf.org/html/rfc3394#section-2.2.1 |
||
| 216 | * @param string[] $P Plaintext, n 64-bit values <code>{P1, P2, ..., |
||
| 217 | * Pn}</code> |
||
| 218 | * @param string $kek Key encryption key |
||
| 219 | * @param string $iv Initial value |
||
| 220 | * @return string[] Ciphertext, (n+1) 64-bit values <code>{C0, C1, ..., |
||
| 221 | * Cn}</code> |
||
| 222 | */ |
||
| 223 | 17 | protected function _wrapBlocks(array $P, $kek, $iv) { |
|
| 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($ciphertext, $kek) { |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Apply Key Unwrap to data blocks. |
||
| 281 | * |
||
| 282 | * Uses the index based version of key unwrap procedure |
||
| 283 | * described in the RFC. |
||
| 284 | * |
||
| 285 | * Does not compute step 3. |
||
| 286 | * |
||
| 287 | * @link https://tools.ietf.org/html/rfc3394#section-2.2.2 |
||
| 288 | * @param string[] $C Ciphertext, (n+1) 64-bit values <code>{C0, C1, ..., |
||
| 289 | * Cn}</code> |
||
| 290 | * @param string $kek Key encryption key |
||
| 291 | * @throws \UnexpectedValueException |
||
| 292 | * @return array Tuple of integrity value <code>A</code> and register |
||
| 293 | * <code>R</code> |
||
| 294 | */ |
||
| 295 | 15 | protected function _unwrapBlocks(array $C, $kek) { |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Check that the integrity check value of the padded key is correct. |
||
| 323 | * |
||
| 324 | * @param string $A |
||
| 325 | * @throws \UnexpectedValueException |
||
| 326 | */ |
||
| 327 | 13 | protected function _checkPaddedIntegrity($A) { |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Verify that the padding of the plaintext is valid. |
||
| 336 | * |
||
| 337 | * @param array $P Plaintext, n 64-bit values <code>{P1, P2, ..., |
||
| 338 | * Pn}</code> |
||
| 339 | * @param string $A Integrity check value |
||
| 340 | * @throws \UnexpectedValueException |
||
| 341 | * @return int Message length without padding |
||
| 342 | */ |
||
| 343 | 12 | protected function _verifyPadding(array $P, $A) { |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Apply AES(K, W) operation (encrypt) to 64 bit block. |
||
| 367 | * |
||
| 368 | * @param string $kek |
||
| 369 | * @param string $block |
||
| 370 | * @throws \RuntimeException If encrypt fails |
||
| 371 | * @return string |
||
| 372 | */ |
||
| 373 | 26 | View Code Duplication | protected function _encrypt($kek, $block) { |
| 382 | |||
| 383 | /** |
||
| 384 | * Apply AES-1(K, W) operation (decrypt) to 64 bit block. |
||
| 385 | * |
||
| 386 | * @param string $kek |
||
| 387 | * @param string $block |
||
| 388 | * @throws \RuntimeException If decrypt fails |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | 23 | View Code Duplication | protected function _decrypt($kek, $block) { |
| 400 | |||
| 401 | /** |
||
| 402 | * Get latest OpenSSL error message. |
||
| 403 | * |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | 2 | protected function _getLastOpenSSLError() { |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Take 64 most significant bits from value. |
||
| 416 | * |
||
| 417 | * @param string $val |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | 20 | protected function _msb64($val) { |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Take 64 least significant bits from value. |
||
| 426 | * |
||
| 427 | * @param string $val |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | 20 | protected function _lsb64($val) { |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Convert number to 64 bit unsigned integer octet string with |
||
| 436 | * most significant bit first. |
||
| 437 | * |
||
| 438 | * @param int $num |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | 20 | protected function _uint64($num) { |
|
| 448 | } |
||
| 449 |