|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Robin NTLM |
| 4 | + * |
| 5 | + * @copyright 2015 Robin Powered, Inc. |
| 6 | + * @link https://robinpowered.com/ |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Robin\Ntlm\Crypt\Des; |
| 10 | + |
| 11 | +use InvalidArgumentException; |
| 12 | +use Robin\Ntlm\Crypt\CipherMode; |
| 13 | +use Robin\Ntlm\Crypt\Exception\CryptographicFailureException; |
| 14 | + |
| 15 | +/** |
| 16 | + * An engine used to encrypt data using the DES standard algorithm and |
| 17 | + * implemented using the PHP "openssl" extension. |
| 18 | + * |
| 19 | + * @link http://php.net/openssl |
| 20 | + */ |
| 21 | +class OpenSslDesEncrypter implements DesEncrypterInterface |
| 22 | +{ |
| 23 | + |
| 24 | + /** |
| 25 | + * Constants |
| 26 | + */ |
| 27 | + |
| 28 | + /** |
| 29 | + * The default OpenSSL encryption options. |
| 30 | + * |
| 31 | + * @type int |
| 32 | + */ |
| 33 | + const DEFAULT_OPENSSL_OPTIONS = OPENSSL_RAW_DATA; |
| 34 | + |
| 35 | + |
| 36 | + /** |
| 37 | + * Properties |
| 38 | + */ |
| 39 | + |
| 40 | + /** |
| 41 | + * A map of {@link CipherMode}s to the "openssl" extension equivalents. |
| 42 | + */ |
| 43 | + private static $cipher_mode_map = [ |
| 44 | + CipherMode::CBC => 'des-cbc', |
| 45 | + CipherMode::CFB => 'des-cfb', |
| 46 | + CipherMode::ECB => 'des-ecb', |
| 47 | + CipherMode::OFB => 'des-ofb', |
| 48 | + ]; |
| 49 | + |
| 50 | + /** |
| 51 | + * Whether or not to zero-byte pad the data before encrypting for some |
| 52 | + * cipher modes. |
| 53 | + * |
| 54 | + * @type bool |
| 55 | + */ |
| 56 | + private $zero_pad; |
| 57 | + |
| 58 | + |
| 59 | + /** |
| 60 | + * Methods |
| 61 | + */ |
| 62 | + |
| 63 | + /** |
| 64 | + * Constructor |
| 65 | + * |
| 66 | + * @param bool $zero_pad Whether or not to zero-byte pad the data before |
| 67 | + * encrypting for some cipher modes. |
| 68 | + */ |
| 69 | + public function __construct($zero_pad = true) |
| 70 | + { |
| 71 | + $this->zero_pad = $zero_pad; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * {@inheritDoc} |
| 76 | + */ |
| 77 | + public function encrypt($key, $data, $mode, $initialization_vector) |
| 78 | + { |
| 79 | + if (isset(self::$cipher_mode_map[$mode])) { |
| 80 | + $mode = self::$cipher_mode_map[$mode]; |
| 81 | + } else { |
| 82 | + throw new InvalidArgumentException('Unknown cipher mode "'. $mode .'"'); |
| 83 | + } |
| 84 | + |
| 85 | + $options = $this->getOpenSslEncryptionOptions(); |
| 86 | + |
| 87 | + $encrypted = openssl_encrypt($data, $mode, $key, $options, $initialization_vector); |
| 88 | + |
| 89 | + if (false === $encrypted) { |
| 90 | + throw CryptographicFailureException::forReasonCode( |
| 91 | + CryptographicFailureException::CODE_FOR_ENCRYPTION_FAILURE |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + return $encrypted; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Gets the OpenSSL encryption options. |
| 100 | + * |
| 101 | + * @return int The options to use in an OpenSSL encryption call. |
| 102 | + */ |
| 103 | + private function getOpenSslEncryptionOptions() |
| 104 | + { |
| 105 | + $options = self::DEFAULT_OPENSSL_OPTIONS; |
| 106 | + |
| 107 | + if ($this->zero_pad) { |
| 108 | + $options = $options | OPENSSL_ZERO_PADDING; |
| 109 | + } |
| 110 | + |
| 111 | + return $options; |
| 112 | + } |
| 113 | +} |
0 commit comments