1
0
mirror of https://github.com/danog/phpseclib.git synced 2024-12-13 01:27:40 +01:00

RSA: replace user_error's with Exceptions

This commit is contained in:
terrafrost 2015-07-28 19:41:32 -05:00
parent 081f2cb825
commit ebe9301efa
2 changed files with 66 additions and 50 deletions

View File

@ -2010,14 +2010,14 @@ class RSA
* @access private * @access private
* @param \phpseclib\Math\BigInteger $x * @param \phpseclib\Math\BigInteger $x
* @param Integer $xLen * @param Integer $xLen
* @throws \OutOfBoundsException if strlen($x) > $xLen
* @return String * @return String
*/ */
function _i2osp($x, $xLen) function _i2osp($x, $xLen)
{ {
$x = $x->toBytes(); $x = $x->toBytes();
if (strlen($x) > $xLen) { if (strlen($x) > $xLen) {
user_error('Integer too large'); throw new \OutOfBoundsException('Integer too large');
return false;
} }
return str_pad($x, $xLen, chr(0), STR_PAD_LEFT); return str_pad($x, $xLen, chr(0), STR_PAD_LEFT);
} }
@ -2172,13 +2172,13 @@ class RSA
* *
* @access private * @access private
* @param \phpseclib\Math\BigInteger $m * @param \phpseclib\Math\BigInteger $m
* @throws \OutOfRangeException if $m < 0 or $m > $this->modulus
* @return \phpseclib\Math\BigInteger * @return \phpseclib\Math\BigInteger
*/ */
function _rsaep($m) function _rsaep($m)
{ {
if ($m->compare($this->zero) < 0 || $m->compare($this->modulus) > 0) { if ($m->compare($this->zero) < 0 || $m->compare($this->modulus) > 0) {
user_error('Message representative out of range'); throw new \OutOfRangeException('Message representative out of range');
return false;
} }
return $this->_exponentiate($m); return $this->_exponentiate($m);
} }
@ -2190,13 +2190,13 @@ class RSA
* *
* @access private * @access private
* @param \phpseclib\Math\BigInteger $c * @param \phpseclib\Math\BigInteger $c
* @throws \OutOfRangeException if $c < 0 or $c > $this->modulus
* @return \phpseclib\Math\BigInteger * @return \phpseclib\Math\BigInteger
*/ */
function _rsadp($c) function _rsadp($c)
{ {
if ($c->compare($this->zero) < 0 || $c->compare($this->modulus) > 0) { if ($c->compare($this->zero) < 0 || $c->compare($this->modulus) > 0) {
user_error('Ciphertext representative out of range'); throw new \OutOfRangeException('Ciphertext representative out of range');
return false;
} }
return $this->_exponentiate($c); return $this->_exponentiate($c);
} }
@ -2208,13 +2208,13 @@ class RSA
* *
* @access private * @access private
* @param \phpseclib\Math\BigInteger $m * @param \phpseclib\Math\BigInteger $m
* @throws \OutOfRangeException if $m < 0 or $m > $this->modulus
* @return \phpseclib\Math\BigInteger * @return \phpseclib\Math\BigInteger
*/ */
function _rsasp1($m) function _rsasp1($m)
{ {
if ($m->compare($this->zero) < 0 || $m->compare($this->modulus) > 0) { if ($m->compare($this->zero) < 0 || $m->compare($this->modulus) > 0) {
user_error('Message representative out of range'); throw new \OutOfRangeException('Message representative out of range');
return false;
} }
return $this->_exponentiate($m); return $this->_exponentiate($m);
} }
@ -2226,13 +2226,13 @@ class RSA
* *
* @access private * @access private
* @param \phpseclib\Math\BigInteger $s * @param \phpseclib\Math\BigInteger $s
* @throws \OutOfRangeException if $s < 0 or $s > $this->modulus
* @return \phpseclib\Math\BigInteger * @return \phpseclib\Math\BigInteger
*/ */
function _rsavp1($s) function _rsavp1($s)
{ {
if ($s->compare($this->zero) < 0 || $s->compare($this->modulus) > 0) { if ($s->compare($this->zero) < 0 || $s->compare($this->modulus) > 0) {
user_error('Signature representative out of range'); throw new \OutOfRangeException('Signature representative out of range');
return false;
} }
return $this->_exponentiate($s); return $this->_exponentiate($s);
} }
@ -2270,6 +2270,7 @@ class RSA
* @access private * @access private
* @param String $m * @param String $m
* @param String $l * @param String $l
* @throws \OutOfBoundsException if strlen($m) > $this->k - 2 * $this->hLen - 2
* @return String * @return String
*/ */
function _rsaes_oaep_encrypt($m, $l = '') function _rsaes_oaep_encrypt($m, $l = '')
@ -2282,8 +2283,7 @@ class RSA
// be output. // be output.
if ($mLen > $this->k - 2 * $this->hLen - 2) { if ($mLen > $this->k - 2 * $this->hLen - 2) {
user_error('Message too long'); throw new \OutOfBoundsException('Message too long');
return false;
} }
// EME-OAEP encoding // EME-OAEP encoding
@ -2333,6 +2333,7 @@ class RSA
* @access private * @access private
* @param String $c * @param String $c
* @param String $l * @param String $l
* @throws \RuntimeException on decryption error
* @return String * @return String
*/ */
function _rsaes_oaep_decrypt($c, $l = '') function _rsaes_oaep_decrypt($c, $l = '')
@ -2343,8 +2344,7 @@ class RSA
// be output. // be output.
if (strlen($c) != $this->k || $this->k < 2 * $this->hLen + 2) { if (strlen($c) != $this->k || $this->k < 2 * $this->hLen + 2) {
user_error('Decryption error'); throw new \RuntimeException('Decryption error');
return false;
} }
// RSA decryption // RSA decryption
@ -2352,8 +2352,7 @@ class RSA
$c = $this->_os2ip($c); $c = $this->_os2ip($c);
$m = $this->_rsadp($c); $m = $this->_rsadp($c);
if ($m === false) { if ($m === false) {
user_error('Decryption error'); throw new \RuntimeException('Decryption error');
return false;
} }
$em = $this->_i2osp($m, $this->k); $em = $this->_i2osp($m, $this->k);
@ -2370,13 +2369,11 @@ class RSA
$lHash2 = substr($db, 0, $this->hLen); $lHash2 = substr($db, 0, $this->hLen);
$m = substr($db, $this->hLen); $m = substr($db, $this->hLen);
if ($lHash != $lHash2) { if ($lHash != $lHash2) {
user_error('Decryption error'); throw new \RuntimeException('Decryption error');
return false;
} }
$m = ltrim($m, chr(0)); $m = ltrim($m, chr(0));
if (ord($m[0]) != 1) { if (ord($m[0]) != 1) {
user_error('Decryption error'); throw new \RuntimeException('Decryption error');
return false;
} }
// Output the message M // Output the message M
@ -2407,6 +2404,7 @@ class RSA
* *
* @access private * @access private
* @param String $m * @param String $m
* @throws \OutOfBoundsException if strlen($m) > $this->k - 11
* @return String * @return String
*/ */
function _rsaes_pkcs1_v1_5_encrypt($m) function _rsaes_pkcs1_v1_5_encrypt($m)
@ -2416,8 +2414,7 @@ class RSA
// Length checking // Length checking
if ($mLen > $this->k - 11) { if ($mLen > $this->k - 11) {
user_error('Message too long'); throw new \OutOfBoundsException('Message too long');
return false;
} }
// EME-PKCS1-v1_5 encoding // EME-PKCS1-v1_5 encoding
@ -2466,6 +2463,7 @@ class RSA
* *
* @access private * @access private
* @param String $c * @param String $c
* @throws \RuntimeException on decryption error
* @return String * @return String
*/ */
function _rsaes_pkcs1_v1_5_decrypt($c) function _rsaes_pkcs1_v1_5_decrypt($c)
@ -2473,8 +2471,7 @@ class RSA
// Length checking // Length checking
if (strlen($c) != $this->k) { // or if k < 11 if (strlen($c) != $this->k) { // or if k < 11
user_error('Decryption error'); throw new \RuntimeException('Decryption error');
return false;
} }
// RSA decryption // RSA decryption
@ -2483,24 +2480,21 @@ class RSA
$m = $this->_rsadp($c); $m = $this->_rsadp($c);
if ($m === false) { if ($m === false) {
user_error('Decryption error'); throw new \RuntimeException('Decryption error');
return false;
} }
$em = $this->_i2osp($m, $this->k); $em = $this->_i2osp($m, $this->k);
// EME-PKCS1-v1_5 decoding // EME-PKCS1-v1_5 decoding
if (ord($em[0]) != 0 || ord($em[1]) > 2) { if (ord($em[0]) != 0 || ord($em[1]) > 2) {
user_error('Decryption error'); throw new \RuntimeException('Decryption error');
return false;
} }
$ps = substr($em, 2, strpos($em, chr(0), 2) - 2); $ps = substr($em, 2, strpos($em, chr(0), 2) - 2);
$m = substr($em, strlen($ps) + 3); $m = substr($em, strlen($ps) + 3);
if (strlen($ps) < 8) { if (strlen($ps) < 8) {
user_error('Decryption error'); throw new \RuntimeException('Decryption error');
return false;
} }
// Output M // Output M
@ -2515,6 +2509,7 @@ class RSA
* *
* @access private * @access private
* @param String $m * @param String $m
* @throws \RuntimeException on encoding error
* @param Integer $emBits * @param Integer $emBits
*/ */
function _emsa_pss_encode($m, $emBits) function _emsa_pss_encode($m, $emBits)
@ -2527,8 +2522,7 @@ class RSA
$mHash = $this->hash->hash($m); $mHash = $this->hash->hash($m);
if ($emLen < $this->hLen + $sLen + 2) { if ($emLen < $this->hLen + $sLen + 2) {
user_error('Encoding error'); throw new \RuntimeException('Encoding error');
return false;
} }
$salt = Random::string($sLen); $salt = Random::string($sLen);
@ -2625,6 +2619,7 @@ class RSA
* @access private * @access private
* @param String $m * @param String $m
* @param String $s * @param String $s
* @throws \RuntimeException on invalid signature
* @return String * @return String
*/ */
function _rsassa_pss_verify($m, $s) function _rsassa_pss_verify($m, $s)
@ -2632,8 +2627,7 @@ class RSA
// Length checking // Length checking
if (strlen($s) != $this->k) { if (strlen($s) != $this->k) {
user_error('Invalid signature'); throw new \RuntimeException('Invalid signature');
return false;
} }
// RSA verification // RSA verification
@ -2643,13 +2637,11 @@ class RSA
$s2 = $this->_os2ip($s); $s2 = $this->_os2ip($s);
$m2 = $this->_rsavp1($s2); $m2 = $this->_rsavp1($s2);
if ($m2 === false) { if ($m2 === false) {
user_error('Invalid signature'); throw new \RuntimeException('Invalid signature');
return false;
} }
$em = $this->_i2osp($m2, $modBits >> 3); $em = $this->_i2osp($m2, $modBits >> 3);
if ($em === false) { if ($em === false) {
user_error('Invalid signature'); throw new \RuntimeException('Invalid signature');
return false;
} }
// EMSA-PSS verification // EMSA-PSS verification
@ -2665,6 +2657,7 @@ class RSA
* @access private * @access private
* @param String $m * @param String $m
* @param Integer $emLen * @param Integer $emLen
* @throws \LengthException if the intended encoded message length is too short
* @return String * @return String
*/ */
function _emsa_pkcs1_v1_5_encode($m, $emLen) function _emsa_pkcs1_v1_5_encode($m, $emLen)
@ -2698,8 +2691,7 @@ class RSA
$tLen = strlen($t); $tLen = strlen($t);
if ($emLen < $tLen + 11) { if ($emLen < $tLen + 11) {
user_error('Intended encoded message length too short'); throw new \LengthException('Intended encoded message length too short');
return false;
} }
$ps = str_repeat(chr(0xFF), $emLen - $tLen - 3); $ps = str_repeat(chr(0xFF), $emLen - $tLen - 3);
@ -2716,6 +2708,7 @@ class RSA
* *
* @access private * @access private
* @param String $m * @param String $m
* @throws \LengthException if the RSA modulus is too short
* @return String * @return String
*/ */
function _rsassa_pkcs1_v1_5_sign($m) function _rsassa_pkcs1_v1_5_sign($m)
@ -2724,8 +2717,7 @@ class RSA
$em = $this->_emsa_pkcs1_v1_5_encode($m, $this->k); $em = $this->_emsa_pkcs1_v1_5_encode($m, $this->k);
if ($em === false) { if ($em === false) {
user_error('RSA modulus too short'); throw new \LengthException('RSA modulus too short');
return false;
} }
// RSA signature // RSA signature
@ -2746,6 +2738,8 @@ class RSA
* *
* @access private * @access private
* @param String $m * @param String $m
* @throws \RuntimeException if the signature is invalid
* @throws \LengthException if the RSA modulus is too short
* @return String * @return String
*/ */
function _rsassa_pkcs1_v1_5_verify($m, $s) function _rsassa_pkcs1_v1_5_verify($m, $s)
@ -2753,8 +2747,7 @@ class RSA
// Length checking // Length checking
if (strlen($s) != $this->k) { if (strlen($s) != $this->k) {
user_error('Invalid signature'); throw new \RuntimeSignature('Invalid signature');
return false;
} }
// RSA verification // RSA verification
@ -2762,21 +2755,18 @@ class RSA
$s = $this->_os2ip($s); $s = $this->_os2ip($s);
$m2 = $this->_rsavp1($s); $m2 = $this->_rsavp1($s);
if ($m2 === false) { if ($m2 === false) {
user_error('Invalid signature'); throw new \RuntimeSignature('Invalid signature');
return false;
} }
$em = $this->_i2osp($m2, $this->k); $em = $this->_i2osp($m2, $this->k);
if ($em === false) { if ($em === false) {
user_error('Invalid signature'); throw new \RuntimeSignature('Invalid signature');
return false;
} }
// EMSA-PKCS1-v1_5 encoding // EMSA-PKCS1-v1_5 encoding
$em2 = $this->_emsa_pkcs1_v1_5_encode($m, $this->k); $em2 = $this->_emsa_pkcs1_v1_5_encode($m, $this->k);
if ($em2 === false) { if ($em2 === false) {
user_error('RSA modulus too short'); throw new \LengthException('RSA modulus too short');
return false;
} }
// Compare // Compare

View File

@ -0,0 +1,26 @@
<?php
/**
* DecryptionException
*
* PHP version 5
*
* @category Exception
* @package DecryptionException
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2015 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
*/
namespace phpseclib\Exception;
/**
* KeyGenerationException
*
* @package DecryptionException
* @author Jim Wigginton <terrafrost@php.net>
*/
class DecryptionException extends \Exception
{
}