Skip to content

Commit

Permalink
Allow selection of P-521 in NistCurve
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Apr 28, 2024
1 parent fd1c2ed commit 80b81cd
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/Curves/NistCurve.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Mdanter\Ecc\Math\GmpMathInterface;
use Mdanter\Ecc\Optimized\P256;
use Mdanter\Ecc\Optimized\P384;
use Mdanter\Ecc\Optimized\P521;
use Mdanter\Ecc\Primitives\CurveParameters;
use Mdanter\Ecc\Primitives\GeneratorPoint;
use Mdanter\Ecc\Random\RandomNumberGeneratorInterface;
Expand Down Expand Up @@ -240,6 +241,7 @@ public function optimizedCurve384(): NamedCurveFp
* Returns an NIST P-384 generator.
*
* @param ?RandomNumberGeneratorInterface $randomGenerator
* @param bool $optimized
* @return GeneratorPoint
*/
public function generator384(?RandomNumberGeneratorInterface $randomGenerator = null, bool $optimized = false): GeneratorPoint
Expand Down Expand Up @@ -279,15 +281,41 @@ public function curve521(): NamedCurveFp
return new NamedCurveFp(self::NAME_P521, $parameters, $this->adapter);
}

/**
* Returns an NIST P-521 curve.
*
* @return NamedCurveFp
*/
public function optimizedCurve521(): NamedCurveFp
{
/** @var GMP $p */
$p = gmp_init('6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151', 10);
/** @var GMP $b */
$b = gmp_init('0x051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00', 16);

/** @var GMP $minusThree */
$minusThree = gmp_init(-3, 10);
$parameters = new CurveParameters(521, $p, $minusThree, $b);

return (new OptimizedCurveFp(self::NAME_P521, $parameters, $this->adapter))
->setOptimizedCurveOps(new P521());
}

/**
* Returns an NIST P-521 generator.
*
* @param ?RandomNumberGeneratorInterface $randomGenerator
* @param bool $optimized
* @return GeneratorPoint
*/
public function generator521(?RandomNumberGeneratorInterface $randomGenerator = null): GeneratorPoint
public function generator521(?RandomNumberGeneratorInterface $randomGenerator = null, bool $optimized = false): GeneratorPoint
{
$curve = $this->curve521();

if ($optimized) {
$curve = $this->optimizedCurve521();
} else {
$curve = $this->curve521();
}
/** @var GMP $order */
$order = gmp_init('6864797660130609714981900799081393217269435300143305409394463459185543183397655394245057746333217197532963996371363321113864768612440380340372808892707005449', 10);

Expand Down

0 comments on commit 80b81cd

Please sign in to comment.