diff --git a/src/Core.php b/src/Core.php index 0c2822f..17e6f5a 100644 --- a/src/Core.php +++ b/src/Core.php @@ -27,12 +27,7 @@ public static function incrementCounter($ctr, $inc, &$config) { static $ivsize = null; if ($ivsize === null) { - $ivsize = \openssl_cipher_iv_length($config->cipherMethod()); - if ($ivsize === false) { - throw new Ex\CannotPerformOperationException( - "Problem obtaining the correct nonce length." - ); - } + $ivsize = self::cipherIvLength($config->cipherMethod()); } if (self::ourStrlen($ctr) !== $ivsize) { @@ -73,6 +68,27 @@ public static function incrementCounter($ctr, $inc, &$config) return $ctr; } + /** + * Returns the cipher initialization vector (iv) length. + * + * @param string $method + * @return int + * @throws Ex\CannotPerformOperationException + */ + public static function cipherIvLength($method) + { + self::ensureFunctionExists('openssl_cipher_iv_length'); + $ivsize = \openssl_cipher_iv_length($method); + + if ($ivsize === false || $ivsize <= 0) { + throw new Ex\CannotPerformOperationException( + 'Could not get the IV length from OpenSSL' + ); + } + + return $ivsize; + } + /** * Returns a random binary string of length $octets bytes. * diff --git a/src/Crypto.php b/src/Crypto.php index 2ae39b3..207090f 100755 --- a/src/Crypto.php +++ b/src/Crypto.php @@ -105,13 +105,7 @@ public static function encrypt($plaintext, $key, $raw_binary = false) ); // Generate a random initialization vector. - Core::ensureFunctionExists("openssl_cipher_iv_length"); - $ivsize = \openssl_cipher_iv_length($config->cipherMethod()); - if ($ivsize === false || $ivsize <= 0) { - throw new Ex\CannotPerformOperationException( - "Could not get the IV length from OpenSSL" - ); - } + $ivsize = Core::cipherIvLength($config->cipherMethod()); $iv = Core::secureRandom($ivsize); $ciphertext = $salt . $iv . self::plainEncrypt($plaintext, $ekey, $iv, $config); @@ -210,13 +204,7 @@ public static function decrypt($ciphertext, $key, $raw_binary = false) $ekey = Core::HKDF($config->hashFunctionName(), $key, $config->keyByteSize(), $config->encryptionInfoString(), $salt, $config); // Extract the initialization vector from the ciphertext. - Core::EnsureFunctionExists("openssl_cipher_iv_length"); - $ivsize = \openssl_cipher_iv_length($config->cipherMethod()); - if ($ivsize === false || $ivsize <= 0) { - throw new Ex\CannotPerformOperationException( - "Could not get the IV length from OpenSSL" - ); - } + $ivsize = Core::cipherIvLength($config->cipherMethod()); if (Core::ourStrlen($ciphertext) <= $ivsize) { throw new Ex\InvalidCiphertextException( "Ciphertext is too short." @@ -302,13 +290,7 @@ public static function legacyDecrypt($ciphertext, $key) ); // Extract the initialization vector from the ciphertext. - Core::EnsureFunctionExists("openssl_cipher_iv_length"); - $ivsize = \openssl_cipher_iv_length($config->cipherMethod()); - if ($ivsize === false || $ivsize <= 0) { - throw new Ex\CannotPerformOperationException( - "Could not get the IV length from OpenSSL" - ); - } + $ivsize = Core::cipherIvLength($config->cipherMethod()); if (Core::ourStrlen($ciphertext) <= $ivsize) { throw new Ex\InvalidCiphertextException( "Ciphertext is too short." diff --git a/src/File.php b/src/File.php index 82ee859..5475422 100644 --- a/src/File.php +++ b/src/File.php @@ -273,13 +273,7 @@ public static function encryptResource($inputHandle, $outputHandle, Key $key) /** * Generate a random initialization vector. */ - Core::ensureFunctionExists("openssl_cipher_iv_length"); - $ivsize = \openssl_cipher_iv_length($config->cipherMethod()); - if ($ivsize === false || $ivsize <= 0) { - throw new Ex\CannotPerformOperationException( - 'Improper IV size' - ); - } + $ivsize = Core::cipherIvLength($config->cipherMethod()); $iv = Core::secureRandom($ivsize); /** @@ -459,7 +453,7 @@ public static function decryptResource($inputHandle, $outputHandle, Key $key) * * It should be the first N blocks of the file (N = 16) */ - $ivsize = \openssl_cipher_iv_length($config->cipherMethod()); + $ivsize = Core::cipherIvLength($config->cipherMethod()); $iv = self::readBytes($inputHandle, $ivsize); // How much do we increase the counter after each buffered encryption to prevent nonce reuse