diff --git a/phpseclib/Crypt/RSA/PublicKey.php b/phpseclib/Crypt/RSA/PublicKey.php index 4adf3b89..a0b47e28 100644 --- a/phpseclib/Crypt/RSA/PublicKey.php +++ b/phpseclib/Crypt/RSA/PublicKey.php @@ -212,7 +212,7 @@ class PublicKey extends RSA implements Common\PublicKey // if $m is larger than two million terrabytes and you're using sha1, PKCS#1 suggests a "Label too long" error // be output. - $emLen = ($emBits + 1) >> 3; // ie. ceil($emBits / 8); + $emLen = ($emBits + 7) >> 3; // ie. ceil($emBits / 8); $sLen = $this->sLen !== null ? $this->sLen : $this->hLen; $mHash = $this->hash->hash($m); @@ -263,11 +263,11 @@ class PublicKey extends RSA implements Common\PublicKey // RSA verification - $modBits = 8 * $this->k; + $modBits = strlen($this->modulus->toBits()); $s2 = $this->os2ip($s); $m2 = $this->rsavp1($s2); - $em = $this->i2osp($m2, $modBits >> 3); + $em = $this->i2osp($m2, $this->k); if ($em === false) { return false; } diff --git a/tests/Unit/Crypt/RSA/ModeTest.php b/tests/Unit/Crypt/RSA/ModeTest.php index e4bc304a..f2cb4e5a 100644 --- a/tests/Unit/Crypt/RSA/ModeTest.php +++ b/tests/Unit/Crypt/RSA/ModeTest.php @@ -132,4 +132,27 @@ U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ $rsa = $rsa->getPublicKey(); $this->assertTrue($rsa->verify($plaintext, $sig)); } + + /** + * @group github1423 + */ + public function testPSSSigsWithNonPowerOf2Key() + { + $pub = <<withHash('sha256') + ->withSaltLength(32) + ->withMGFHash('sha256'); + + $sig = base64_decode(strtr('Ad022bD-UCmWpBNMtsYJjG0FVxML-FFlN4IKrByP8rwjVzV_D-YqSjc_oW6LrooV7jbtEF5803YLn8lllyzDnw00', '-_', '+/')); + + $payload = 'eyJraWQiOiJ0RkMyVUloRnBUTV9FYTNxY09kX01xUVQxY0JCbTlrRkxTRGZlSmhzUkc4IiwiYWxnI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