From 3cb0248f9977ddd8c83c03a6a87df5f1736741ed Mon Sep 17 00:00:00 2001 From: terrafrost Date: Wed, 15 Jul 2015 12:50:57 -0500 Subject: [PATCH] Crypt/Base: add initial exception support To accomodate this decrypt() can no longer return false. If padding is enabled and the padding length is invalid (the only condition under which false was returned) an InvalidInputException will be thrown. Also, previously, when encrypting, if the padding was disabled and the plaintext wasn't a multiple of the block size a E_USER_NOTICE would be issued via user_error() and then padding would be enabled. Now it's not enabled - an exception is thrown suggesting that you might want to enable padding and that's that. --- phpseclib/Crypt/Base.php | 10 +++++----- phpseclib/Exception/InvalidInputException.php | 19 +++++++++++++++++++ .../Exception/KeyGenerationException.php | 19 +++++++++++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 phpseclib/Exception/InvalidInputException.php create mode 100644 phpseclib/Exception/KeyGenerationException.php diff --git a/phpseclib/Crypt/Base.php b/phpseclib/Crypt/Base.php index c14cbf3e..51d68026 100644 --- a/phpseclib/Crypt/Base.php +++ b/phpseclib/Crypt/Base.php @@ -37,6 +37,8 @@ namespace phpseclib\Crypt; use phpseclib\Crypt\Hash; +use phpseclib\Exception\InvalidInputException; +use phpseclib\Exception\KeyGenerationException; /** * Base Class for all \phpseclib\Crypt\* cipher classes @@ -579,8 +581,7 @@ abstract class Base $hashObj = new Hash(); $hashObj->setHash($hash); if ($dkLen > $hashObj->getLength()) { - user_error('Derived key too long'); - return false; + throw new KeyGenerationException('Derived key too long'); } $t = $password . $salt; for ($i = 0; $i < $count; ++$i) { @@ -1779,8 +1780,7 @@ abstract class Base if ($length % $this->block_size == 0) { return $text; } else { - user_error("The plaintext's length ($length) is not a multiple of the block size ({$this->block_size})"); - $this->padding = true; + throw new InvalidInputException("The plaintext's length ($length) is not a multiple of the block size ({$this->block_size}). Try enabling padding."); } } @@ -1809,7 +1809,7 @@ abstract class Base $length = ord($text[strlen($text) - 1]); if (!$length || $length > $this->block_size) { - return false; + throw new InvalidInputException("The ciphertext has an invalid padding length ($length) compared to the block size ({$this->block_size})"); } return substr($text, 0, -$length); diff --git a/phpseclib/Exception/InvalidInputException.php b/phpseclib/Exception/InvalidInputException.php new file mode 100644 index 00000000..511f62b6 --- /dev/null +++ b/phpseclib/Exception/InvalidInputException.php @@ -0,0 +1,19 @@ +