Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
*
Expand Down
24 changes: 3 additions & 21 deletions src/Crypto.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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."
Expand Down Expand Up @@ -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."
Expand Down
10 changes: 2 additions & 8 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down Expand Up @@ -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
Expand Down