From 2689c727e7a99841e1d3f13e3a5a5fe62853e803 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Wed, 1 May 2024 15:12:04 -0500 Subject: [PATCH] BigInteger: EvalBarrett / Barrett could sometimes slow to a crawl --- .../Engines/BCMath/Reductions/Barrett.php | 2 +- .../Engines/PHP/Reductions/Barrett.php | 2 +- .../Engines/PHP/Reductions/EvalBarrett.php | 2 +- tests/Unit/Crypt/RSA/LoadKeyTest.php | 21 +++++++++++++++++++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/phpseclib/Math/BigInteger/Engines/BCMath/Reductions/Barrett.php b/phpseclib/Math/BigInteger/Engines/BCMath/Reductions/Barrett.php index 0fb7eaeb..ec1d5caa 100644 --- a/phpseclib/Math/BigInteger/Engines/BCMath/Reductions/Barrett.php +++ b/phpseclib/Math/BigInteger/Engines/BCMath/Reductions/Barrett.php @@ -66,7 +66,7 @@ abstract class Barrett extends Base $m_length = strlen($m); - if (strlen($n) > 2 * $m_length) { + if (strlen($n) >= 2 * $m_length) { return bcmod($n, $m); } diff --git a/phpseclib/Math/BigInteger/Engines/PHP/Reductions/Barrett.php b/phpseclib/Math/BigInteger/Engines/PHP/Reductions/Barrett.php index 3518d76f..84937419 100644 --- a/phpseclib/Math/BigInteger/Engines/PHP/Reductions/Barrett.php +++ b/phpseclib/Math/BigInteger/Engines/PHP/Reductions/Barrett.php @@ -56,7 +56,7 @@ abstract class Barrett extends Base $m_length = count($m); // if (self::compareHelper($n, $static::square($m)) >= 0) { - if (count($n) > 2 * $m_length) { + if (count($n) >= 2 * $m_length) { $lhs = new $class(); $rhs = new $class(); $lhs->value = $n; diff --git a/phpseclib/Math/BigInteger/Engines/PHP/Reductions/EvalBarrett.php b/phpseclib/Math/BigInteger/Engines/PHP/Reductions/EvalBarrett.php index 2f943317..2cf69f2e 100644 --- a/phpseclib/Math/BigInteger/Engines/PHP/Reductions/EvalBarrett.php +++ b/phpseclib/Math/BigInteger/Engines/PHP/Reductions/EvalBarrett.php @@ -100,7 +100,7 @@ abstract class EvalBarrett extends Base $cutoff = count($m) + (count($m) >> 1); $code = ' - if (count($n) > ' . (2 * count($m)) . ') { + if (count($n) >= ' . (2 * count($m)) . ') { $lhs = new ' . $class . '(); $rhs = new ' . $class . '(); $lhs->value = $n; diff --git a/tests/Unit/Crypt/RSA/LoadKeyTest.php b/tests/Unit/Crypt/RSA/LoadKeyTest.php index 3ec7c9e6..59a1fc10 100644 --- a/tests/Unit/Crypt/RSA/LoadKeyTest.php +++ b/tests/Unit/Crypt/RSA/LoadKeyTest.php @@ -1421,4 +1421,25 @@ vtpiPBM= $this->pkcs8tester($key, $pass); } + + /** + * @group github1994 + */ + public function testCloseNumbers() + { + $rsa = PublicKeyLoader::load([ + // Modulus + 'n' => new BigInteger('5BDD6AFB1E1AFB50D1B2989F70B549B8D44AE3712B444F2C5D862C46C99526E998B79BF0B4F1461524E39D263F3130B9E08F3B17C2070785EFB0EDEC1E75C6C2B8185FA9596886D5DAF8B68E92FCF5F1B33E7CD772845555B086D2A2466B6398A04DFE1C727BB020g1ED2BF3F03D2826F89616D0846C18B1D87064616FAD394462', 16), + + // Exponent + 'e' => new BigInteger('6FE4F5D0AFCC16E8A5CC68955D4EF28255A546D06F34DD103540B9A7D202AEC96353072DB65D9C360E9030F413971142EE6A28974767CCF3ABFA4E7ADDAEAD81D3F8AE5FF1B8241CA9EF51C10941FFFA74482A636CBD909D29CF7A0346653D3C286EA1F392F4968AEF1489EC4B4BCEA4F248F3931B1C9BE2808DBD33B049731A', 16) + ]) + ->withPadding(RSA::SIGNATURE_PKCS1) + ->withHash('md5') + ->asPrivateKey(); + + $sig = bin2hex($rsa->sign('toto')); + $expected = '4370b3fd5dd318c0c3be8989574fbf4ededc805c6f225ada84f8d882d327b7b300f899878204ff99efdf03b17c26518b8941d602abd16dbdac637c5ae61814cb689da266fe07bc978d417fe6742f650bc35ee79dd2431912fc19e36012e61fcb7cdfd506ca3c5b80'; + $this->assertSame($expected, $sig); + } }