From e361a69509eb788d59cbd5960630275396d82e9b Mon Sep 17 00:00:00 2001 From: Paragon Initiative Enterprises Date: Sun, 28 Apr 2024 04:34:34 -0400 Subject: [PATCH] Reduce noise from Scrutinizer --- src/Crypto/EcDH/EcDHInterface.php | 4 +++- src/Curves/CurveFactory.php | 1 - src/Curves/OptimizedCurveFp.php | 3 +++ src/Exception/UnsupportedCurveException.php | 6 ++++++ src/Optimized/Common/BarretReductionTrait.php | 4 ++-- 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Crypto/EcDH/EcDHInterface.php b/src/Crypto/EcDH/EcDHInterface.php index 5f440596..ef45fca7 100644 --- a/src/Crypto/EcDH/EcDHInterface.php +++ b/src/Crypto/EcDH/EcDHInterface.php @@ -51,6 +51,7 @@ public function createMultiPartyKey(): PublicKeyInterface; * Sets the sender's key. * * @param PrivateKeyInterface $key + * @return self */ public function setSenderKey(PrivateKeyInterface $key); @@ -58,7 +59,8 @@ public function setSenderKey(PrivateKeyInterface $key); * Sets the recipient key. * * @param PublicKeyInterface $key - * @return void + * @return self + * @psalm-suppress PossiblyUnusedReturnValue */ public function setRecipientKey(PublicKeyInterface $key); } diff --git a/src/Curves/CurveFactory.php b/src/Curves/CurveFactory.php index 6bfe81df..98b94a41 100644 --- a/src/Curves/CurveFactory.php +++ b/src/Curves/CurveFactory.php @@ -3,7 +3,6 @@ namespace Mdanter\Ecc\Curves; -use Mdanter\Ecc\Exception\UnknownCurveException; use Mdanter\Ecc\Exception\UnsupportedCurveException; use Mdanter\Ecc\Math\GmpMathInterface; use Mdanter\Ecc\Math\MathAdapterFactory; diff --git a/src/Curves/OptimizedCurveFp.php b/src/Curves/OptimizedCurveFp.php index 86e59283..899a4538 100644 --- a/src/Curves/OptimizedCurveFp.php +++ b/src/Curves/OptimizedCurveFp.php @@ -20,6 +20,9 @@ public function setOptimizedCurveOps(OptimizedCurveOpsInterface $ops): Optimized public function getOptimizedCurveOps(): OptimizedCurveOpsInterface { + if (is_null($this->optimizedOps)) { + throw new \TypeError('Optimized ops was not set'); + } return $this->optimizedOps; } } diff --git a/src/Exception/UnsupportedCurveException.php b/src/Exception/UnsupportedCurveException.php index dca01c76..0fc3eea6 100644 --- a/src/Exception/UnsupportedCurveException.php +++ b/src/Exception/UnsupportedCurveException.php @@ -46,11 +46,17 @@ public function hasOid(): bool public function getCurveName(): string { + if (is_null($this->curveName)) { + return ''; + } return $this->curveName; } public function getOid(): string { + if (is_null($this->oid)) { + return ''; + } return $this->oid; } } diff --git a/src/Optimized/Common/BarretReductionTrait.php b/src/Optimized/Common/BarretReductionTrait.php index e6bc86b5..83166925 100644 --- a/src/Optimized/Common/BarretReductionTrait.php +++ b/src/Optimized/Common/BarretReductionTrait.php @@ -26,12 +26,12 @@ protected function barrettReduce(GMP $product): GMP $ctMath = new ConstantTimeMath(); $b = ((gmp_sign($r) >> 1)) & 1; - $rPrime = $r + $this->p; + $rPrime = gmp_add($r, $this->p); $r = $ctMath->select($b, $rPrime, $r); // Guaranteed to be reduced after 2 cycles for ($i = 0; $i < 2; ++$i) { - $rPrime = $r - $this->p; + $rPrime = gmp_sub($r, $this->p); // $b = 1 if $rPrime > 0, which means subtracting is necessary $b = (~(gmp_sign($rPrime) >> 1)) & 1; $r = $ctMath->select($b, $rPrime, $r);