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) { |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Check KEK size. |
||
| 217 | * |
||
| 218 | * @param string $kek |
||
| 219 | * @throws \UnexpectedValueException |
||
| 220 | * @return self |
||
| 221 | */ |
||
| 222 | 38 | protected function _checkKEKSize($kek) { |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Apply Key Wrap to data blocks. |
||
| 232 | * |
||
| 233 | * Uses alternative version of the key wrap procedure described in the RFC. |
||
| 234 | * |
||
| 235 | * @link https://tools.ietf.org/html/rfc3394#section-2.2.1 |
||
| 236 | * @param string[] $P Plaintext, n 64-bit values <code>{P1, P2, ..., |
||
| 237 | * Pn}</code> |
||
| 238 | * @param string $kek Key encryption key |
||
| 239 | * @param string $iv Initial value |
||
| 240 | * @return string[] Ciphertext, (n+1) 64-bit values <code>{C0, C1, ..., |
||
| 241 | * Cn}</code> |
||
| 242 | */ |
||
| 243 | 17 | protected function _wrapBlocks(array $P, $kek, $iv) { |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Unwrap the padded ciphertext producing plaintext and integrity value. |
||
| 275 | * |
||
| 276 | * @param string[] $C Ciphertext, (n+1) 64-bit values <code>{C0, C1, ..., |
||
| 277 | * Cn}</code> |
||
| 278 | * @param string $kek Encryption key |
||
| 279 | * @return array Tuple of plaintext <code>P</code> and integrity value |
||
| 280 | * <code>A</code> |
||
| 281 | */ |
||
| 282 | 13 | protected function _unwrapPaddedBlocks(array $C, $kek) { |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Apply Key Unwrap to data blocks. |
||
| 300 | * |
||
| 301 | * Uses the index based version of key unwrap procedure |
||
| 302 | * described in the RFC. |
||
| 303 | * |
||
| 304 | * Does not compute step 3. |
||
| 305 | * |
||
| 306 | * @link https://tools.ietf.org/html/rfc3394#section-2.2.2 |
||
| 307 | * @param string[] $C Ciphertext, (n+1) 64-bit values <code>{C0, C1, ..., |
||
| 308 | * Cn}</code> |
||
| 309 | * @param string $kek Key encryption key |
||
| 310 | * @throws \UnexpectedValueException |
||
| 311 | * @return array Tuple of integrity value <code>A</code> and register |
||
| 312 | * <code>R</code> |
||
| 313 | */ |
||
| 314 | 15 | protected function _unwrapBlocks(array $C, $kek) { |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Apply AES(K, W) operation (encrypt) to 64 bit block. |
||
| 342 | * |
||
| 343 | * @param string $kek |
||
| 344 | * @param string $block |
||
| 345 | * @throws \RuntimeException If encrypt fails |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | 26 | View Code Duplication | protected function _encrypt($kek, $block) { |
| 357 | |||
| 358 | /** |
||
| 359 | * Apply AES-1(K, W) operation (decrypt) to 64 bit block. |
||
| 360 | * |
||
| 361 | * @param string $kek |
||
| 362 | * @param string $block |
||
| 363 | * @throws \RuntimeException If decrypt fails |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | 23 | View Code Duplication | protected function _decrypt($kek, $block) { |
| 375 | |||
| 376 | /** |
||
| 377 | * Get latest OpenSSL error message. |
||
| 378 | * |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | 2 | protected function _getLastOpenSSLError() { |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Take 64 most significant bits from value. |
||
| 391 | * |
||
| 392 | * @param string $val |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | 20 | protected function _msb64($val) { |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Take 64 least significant bits from value. |
||
| 401 | * |
||
| 402 | * @param string $val |
||
| 403 | * @return string |
||
| 404 | */ |
||
| 405 | 20 | protected function _lsb64($val) { |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Convert number to 64 bit unsigned integer octet string with |
||
| 411 | * most significant bit first. |
||
| 412 | * |
||
| 413 | * @param int $num |
||
| 414 | * @return string |
||
| 415 | */ |
||
| 416 | 20 | protected function _uint64($num) { |
|
| 423 | } |
||
| 424 |