first commit
This commit is contained in:
@@ -0,0 +1,260 @@
|
||||
<?php
|
||||
/**
|
||||
* Akeeba WebPush
|
||||
*
|
||||
* An abstraction layer for easier implementation of WebPush in Joomla components.
|
||||
*
|
||||
* @copyright (c) 2022 Akeeba Ltd
|
||||
* @license GNU GPL v3 or later; see LICENSE.txt
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Akeeba\WebPush\ECC;
|
||||
|
||||
use Brick\Math\BigInteger as BrickBigInteger;
|
||||
use InvalidArgumentException;
|
||||
use function chr;
|
||||
|
||||
/**
|
||||
* This class is copied verbatim from the JWT Framework by Spomky Labs.
|
||||
*
|
||||
* You can find the original code at https://github.com/web-token/jwt-framework
|
||||
*
|
||||
* The original file has the following copyright notice:
|
||||
*
|
||||
* =====================================================================================================================
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2020 Spomky-Labs
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE-SPOMKY.txt file for details.
|
||||
*
|
||||
* =====================================================================================================================
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class BigInteger
|
||||
{
|
||||
/**
|
||||
* Holds the BigInteger's value.
|
||||
*
|
||||
* @var BrickBigInteger
|
||||
*/
|
||||
private $value;
|
||||
|
||||
private function __construct(BrickBigInteger $value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BigInteger
|
||||
*/
|
||||
public static function createFromBinaryString(string $value): self
|
||||
{
|
||||
$res = unpack('H*', $value);
|
||||
if (false === $res) {
|
||||
throw new InvalidArgumentException('Unable to convert the value');
|
||||
}
|
||||
$data = current($res);
|
||||
|
||||
return new self(BrickBigInteger::fromBase($data, 16));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BigInteger
|
||||
*/
|
||||
public static function createFromDecimal(int $value): self
|
||||
{
|
||||
return new self(BrickBigInteger::of($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BigInteger
|
||||
*/
|
||||
public static function createFromBigInteger(BrickBigInteger $value): self
|
||||
{
|
||||
return new self($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a BigInteger to a binary string.
|
||||
*/
|
||||
public function toBytes(): string
|
||||
{
|
||||
if ($this->value->isEqualTo(BrickBigInteger::zero())) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$temp = $this->value->toBase(16);
|
||||
$temp = 0 !== (mb_strlen($temp, '8bit') & 1) ? '0'.$temp : $temp;
|
||||
$temp = hex2bin($temp);
|
||||
if (false === $temp) {
|
||||
throw new InvalidArgumentException('Unable to convert the value into bytes');
|
||||
}
|
||||
|
||||
return ltrim($temp, chr(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two BigIntegers.
|
||||
*
|
||||
* @param BigInteger $y
|
||||
*
|
||||
* @return BigInteger
|
||||
*/
|
||||
public function add(self $y): self
|
||||
{
|
||||
$value = $this->value->plus($y->value);
|
||||
|
||||
return new self($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts two BigIntegers.
|
||||
*
|
||||
* @param BigInteger $y
|
||||
*
|
||||
* @return BigInteger
|
||||
*/
|
||||
public function subtract(self $y): self
|
||||
{
|
||||
$value = $this->value->minus($y->value);
|
||||
|
||||
return new self($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies two BigIntegers.
|
||||
*
|
||||
* @param BigInteger $x
|
||||
*
|
||||
* @return BigInteger
|
||||
*/
|
||||
public function multiply(self $x): self
|
||||
{
|
||||
$value = $this->value->multipliedBy($x->value);
|
||||
|
||||
return new self($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Divides two BigIntegers.
|
||||
*
|
||||
* @param BigInteger $x
|
||||
*
|
||||
* @return BigInteger
|
||||
*/
|
||||
public function divide(self $x): self
|
||||
{
|
||||
$value = $this->value->dividedBy($x->value);
|
||||
|
||||
return new self($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs modular exponentiation.
|
||||
*
|
||||
* @param BigInteger $e
|
||||
* @param BigInteger $n
|
||||
*
|
||||
* @return BigInteger
|
||||
*/
|
||||
public function modPow(self $e, self $n): self
|
||||
{
|
||||
$value = $this->value->modPow($e->value, $n->value);
|
||||
|
||||
return new self($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs modular exponentiation.
|
||||
*
|
||||
* @param BigInteger $d
|
||||
*
|
||||
* @return BigInteger
|
||||
*/
|
||||
public function mod(self $d): self
|
||||
{
|
||||
$value = $this->value->mod($d->value);
|
||||
|
||||
return new self($value);
|
||||
}
|
||||
|
||||
public function modInverse(BigInteger $m): BigInteger
|
||||
{
|
||||
return new self($this->value->modInverse($m->value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two numbers.
|
||||
*
|
||||
* @param BigInteger $y
|
||||
*/
|
||||
public function compare(self $y): int
|
||||
{
|
||||
return $this->value->compareTo($y->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BigInteger $y
|
||||
*/
|
||||
public function equals(self $y): bool
|
||||
{
|
||||
return $this->value->isEqualTo($y->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BigInteger $y
|
||||
*
|
||||
* @return BigInteger
|
||||
*/
|
||||
public static function random(self $y): self
|
||||
{
|
||||
return new self(BrickBigInteger::randomRange(0, $y->value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BigInteger $y
|
||||
*
|
||||
* @return BigInteger
|
||||
*/
|
||||
public function gcd(self $y): self
|
||||
{
|
||||
return new self($this->value->gcd($y->value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BigInteger $y
|
||||
*/
|
||||
public function lowerThan(self $y): bool
|
||||
{
|
||||
return $this->value->isLessThan($y->value);
|
||||
}
|
||||
|
||||
public function isEven(): bool
|
||||
{
|
||||
return $this->value->isEven();
|
||||
}
|
||||
|
||||
public function get(): BrickBigInteger
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
353
administrator/components/com_akeebabackup/webpush/ECC/Curve.php
Normal file
353
administrator/components/com_akeebabackup/webpush/ECC/Curve.php
Normal file
@@ -0,0 +1,353 @@
|
||||
<?php
|
||||
/**
|
||||
* Akeeba WebPush
|
||||
*
|
||||
* An abstraction layer for easier implementation of WebPush in Joomla components.
|
||||
*
|
||||
* @copyright (c) 2022 Akeeba Ltd
|
||||
* @license GNU GPL v3 or later; see LICENSE.txt
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Akeeba\WebPush\ECC;
|
||||
|
||||
use Brick\Math\BigInteger;
|
||||
use RuntimeException;
|
||||
use function is_null;
|
||||
|
||||
/**
|
||||
* This class is copied verbatim from the JWT Framework by Spomky Labs.
|
||||
*
|
||||
* You can find the original code at https://github.com/web-token/jwt-framework
|
||||
*
|
||||
* The original file has the following copyright notice:
|
||||
*
|
||||
* =====================================================================================================================
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2020 Spomky-Labs
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE-SPOMKY.txt file for details.
|
||||
*
|
||||
* =====================================================================================================================
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class Curve
|
||||
{
|
||||
/**
|
||||
* Elliptic curve over the field of integers modulo a prime.
|
||||
*
|
||||
* @var BigInteger
|
||||
*/
|
||||
private $a;
|
||||
|
||||
/**
|
||||
* @var BigInteger
|
||||
*/
|
||||
private $b;
|
||||
|
||||
/**
|
||||
* @var BigInteger
|
||||
*/
|
||||
private $prime;
|
||||
|
||||
/**
|
||||
* Binary length of keys associated with these curve parameters.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $size;
|
||||
|
||||
/**
|
||||
* @var Point
|
||||
*/
|
||||
private $generator;
|
||||
|
||||
public function __construct(int $size, BigInteger $prime, BigInteger $a, BigInteger $b, Point $generator)
|
||||
{
|
||||
$this->size = $size;
|
||||
$this->prime = $prime;
|
||||
$this->a = $a;
|
||||
$this->b = $b;
|
||||
$this->generator = $generator;
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return 'curve('.Math::toString($this->getA()).', '.Math::toString($this->getB()).', '.Math::toString($this->getPrime()).')';
|
||||
}
|
||||
|
||||
public function getA(): BigInteger
|
||||
{
|
||||
return $this->a;
|
||||
}
|
||||
|
||||
public function getB(): BigInteger
|
||||
{
|
||||
return $this->b;
|
||||
}
|
||||
|
||||
public function getPrime(): BigInteger
|
||||
{
|
||||
return $this->prime;
|
||||
}
|
||||
|
||||
public function getSize(): int
|
||||
{
|
||||
return $this->size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RuntimeException if the curve does not contain the point
|
||||
*/
|
||||
public function getPoint(BigInteger $x, BigInteger $y, ?BigInteger $order = null): Point
|
||||
{
|
||||
if (!$this->contains($x, $y)) {
|
||||
throw new RuntimeException('Curve '.$this->__toString().' does not contain point ('.Math::toString($x).', '.Math::toString($y).')');
|
||||
}
|
||||
$point = Point::create($x, $y, $order);
|
||||
if (!is_null($order)) {
|
||||
$mul = $this->mul($point, $order);
|
||||
if (!$mul->isInfinity()) {
|
||||
throw new RuntimeException('SELF * ORDER MUST EQUAL INFINITY.');
|
||||
}
|
||||
}
|
||||
|
||||
return $point;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RuntimeException if the coordinates are out of range
|
||||
*/
|
||||
public function getPublicKeyFrom(BigInteger $x, BigInteger $y): PublicKey
|
||||
{
|
||||
$zero = BigInteger::zero();
|
||||
if ($x->compareTo($zero) < 0 || $y->compareTo($zero) < 0 || $this->generator->getOrder()->compareTo($x) <= 0 || $this->generator->getOrder()->compareTo($y) <= 0) {
|
||||
throw new RuntimeException('Generator point has x and y out of range.');
|
||||
}
|
||||
$point = $this->getPoint($x, $y);
|
||||
|
||||
return new PublicKey($point);
|
||||
}
|
||||
|
||||
public function contains(BigInteger $x, BigInteger $y): bool
|
||||
{
|
||||
return Math::equals(
|
||||
ModularArithmetic::sub(
|
||||
$y->power(2),
|
||||
Math::add(
|
||||
Math::add(
|
||||
$x->power(3),
|
||||
$this->getA()->multipliedBy($x)
|
||||
),
|
||||
$this->getB()
|
||||
),
|
||||
$this->getPrime()
|
||||
),
|
||||
BigInteger::zero()
|
||||
);
|
||||
}
|
||||
|
||||
public function add(Point $one, Point $two): Point
|
||||
{
|
||||
if ($two->isInfinity()) {
|
||||
return clone $one;
|
||||
}
|
||||
|
||||
if ($one->isInfinity()) {
|
||||
return clone $two;
|
||||
}
|
||||
|
||||
if ($two->getX()->isEqualTo($one->getX())) {
|
||||
if ($two->getY()->isEqualTo($one->getY())) {
|
||||
return $this->getDouble($one);
|
||||
}
|
||||
|
||||
return Point::infinity();
|
||||
}
|
||||
|
||||
$slope = ModularArithmetic::div(
|
||||
$two->getY()->minus($one->getY()),
|
||||
$two->getX()->minus($one->getX()),
|
||||
$this->getPrime()
|
||||
);
|
||||
|
||||
$xR = ModularArithmetic::sub(
|
||||
$slope->power(2)->minus($one->getX()),
|
||||
$two->getX(),
|
||||
$this->getPrime()
|
||||
);
|
||||
|
||||
$yR = ModularArithmetic::sub(
|
||||
$slope->multipliedBy($one->getX()->minus($xR)),
|
||||
$one->getY(),
|
||||
$this->getPrime()
|
||||
);
|
||||
|
||||
return $this->getPoint($xR, $yR, $one->getOrder());
|
||||
}
|
||||
|
||||
public function mul(Point $one, BigInteger $n): Point
|
||||
{
|
||||
if ($one->isInfinity()) {
|
||||
return Point::infinity();
|
||||
}
|
||||
|
||||
/** @var BigInteger $zero */
|
||||
$zero = BigInteger::zero();
|
||||
if ($one->getOrder()->compareTo($zero) > 0) {
|
||||
$n = $n->mod($one->getOrder());
|
||||
}
|
||||
|
||||
if ($n->isEqualTo($zero)) {
|
||||
return Point::infinity();
|
||||
}
|
||||
|
||||
/** @var Point[] $r */
|
||||
$r = [
|
||||
Point::infinity(),
|
||||
clone $one,
|
||||
];
|
||||
|
||||
$k = $this->getSize();
|
||||
$n1 = str_pad(Math::baseConvert(Math::toString($n), 10, 2), $k, '0', STR_PAD_LEFT);
|
||||
|
||||
for ($i = 0; $i < $k; ++$i) {
|
||||
$j = $n1[$i];
|
||||
Point::cswap($r[0], $r[1], $j ^ 1);
|
||||
$r[0] = $this->add($r[0], $r[1]);
|
||||
$r[1] = $this->getDouble($r[1]);
|
||||
Point::cswap($r[0], $r[1], $j ^ 1);
|
||||
}
|
||||
|
||||
$this->validate($r[0]);
|
||||
|
||||
return $r[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Curve $other
|
||||
*/
|
||||
public function cmp(self $other): int
|
||||
{
|
||||
$equal = $this->getA()->isEqualTo($other->getA())
|
||||
&& $this->getB()->isEqualTo($other->getB())
|
||||
&& $this->getPrime()->isEqualTo($other->getPrime());
|
||||
|
||||
return $equal ? 0 : 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Curve $other
|
||||
*/
|
||||
public function equals(self $other): bool
|
||||
{
|
||||
return 0 === $this->cmp($other);
|
||||
}
|
||||
|
||||
public function getDouble(Point $point): Point
|
||||
{
|
||||
if ($point->isInfinity()) {
|
||||
return Point::infinity();
|
||||
}
|
||||
|
||||
$a = $this->getA();
|
||||
$threeX2 = BigInteger::of(3)->multipliedBy($point->getX()->power(2));
|
||||
|
||||
$tangent = ModularArithmetic::div(
|
||||
$threeX2->plus($a),
|
||||
BigInteger::of(2)->multipliedBy($point->getY()),
|
||||
$this->getPrime()
|
||||
);
|
||||
|
||||
$x3 = ModularArithmetic::sub(
|
||||
$tangent->power(2),
|
||||
BigInteger::of(2)->multipliedBy($point->getX()),
|
||||
$this->getPrime()
|
||||
);
|
||||
|
||||
$y3 = ModularArithmetic::sub(
|
||||
$tangent->multipliedBy($point->getX()->minus($x3)),
|
||||
$point->getY(),
|
||||
$this->getPrime()
|
||||
);
|
||||
|
||||
return $this->getPoint($x3, $y3, $point->getOrder());
|
||||
}
|
||||
|
||||
public function createPrivateKey(): PrivateKey
|
||||
{
|
||||
return PrivateKey::create($this->generate());
|
||||
}
|
||||
|
||||
public function createPublicKey(PrivateKey $privateKey): PublicKey
|
||||
{
|
||||
$point = $this->mul($this->generator, $privateKey->getSecret());
|
||||
|
||||
return new PublicKey($point);
|
||||
}
|
||||
|
||||
public function getGenerator(): Point
|
||||
{
|
||||
return $this->generator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RuntimeException if the point is invalid
|
||||
*/
|
||||
private function validate(Point $point): void
|
||||
{
|
||||
if (!$point->isInfinity() && !$this->contains($point->getX(), $point->getY())) {
|
||||
throw new RuntimeException('Invalid point');
|
||||
}
|
||||
}
|
||||
|
||||
private function generate(): BigInteger
|
||||
{
|
||||
$max = $this->generator->getOrder();
|
||||
$numBits = $this->bnNumBits($max);
|
||||
$numBytes = (int) ceil($numBits / 8);
|
||||
// Generate an integer of size >= $numBits
|
||||
$bytes = BigInteger::randomBits($numBytes);
|
||||
$mask = BigInteger::of(2)->power($numBits)->minus(1);
|
||||
|
||||
return $bytes->and($mask);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of bits used to store this number. Non-significant upper bits are not counted.
|
||||
*
|
||||
* @see https://www.openssl.org/docs/crypto/BN_num_bytes.html
|
||||
*/
|
||||
private function bnNumBits(BigInteger $x): int
|
||||
{
|
||||
$zero = BigInteger::of(0);
|
||||
if ($x->isEqualTo($zero)) {
|
||||
return 0;
|
||||
}
|
||||
$log2 = 0;
|
||||
while (!$x->isEqualTo($zero)) {
|
||||
$x = $x->shiftedRight(1);
|
||||
++$log2;
|
||||
}
|
||||
|
||||
return $log2;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
/**
|
||||
* Akeeba WebPush
|
||||
*
|
||||
* An abstraction layer for easier implementation of WebPush in Joomla components.
|
||||
*
|
||||
* @copyright (c) 2022 Akeeba Ltd
|
||||
* @license GNU GPL v3 or later; see LICENSE.txt
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Akeeba\WebPush\ECC;
|
||||
|
||||
use Akeeba\WebPush\ECC\BigInteger as CoreBigInteger;
|
||||
use Brick\Math\BigInteger;
|
||||
|
||||
/**
|
||||
* This class is copied verbatim from the JWT Framework by Spomky Labs.
|
||||
*
|
||||
* You can find the original code at https://github.com/web-token/jwt-framework
|
||||
*
|
||||
* The original file has the following copyright notice:
|
||||
*
|
||||
* =====================================================================================================================
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2020 Spomky-Labs
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE-SPOMKY.txt file for details.
|
||||
*
|
||||
* =====================================================================================================================
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class Math
|
||||
{
|
||||
public static function equals(BigInteger $first, BigInteger $other): bool
|
||||
{
|
||||
return $first->isEqualTo($other);
|
||||
}
|
||||
|
||||
public static function add(BigInteger $augend, BigInteger $addend): BigInteger
|
||||
{
|
||||
return $augend->plus($addend);
|
||||
}
|
||||
|
||||
public static function toString(BigInteger $value): string
|
||||
{
|
||||
return $value->toBase(10);
|
||||
}
|
||||
|
||||
public static function inverseMod(BigInteger $a, BigInteger $m): BigInteger
|
||||
{
|
||||
return CoreBigInteger::createFromBigInteger($a)->modInverse(CoreBigInteger::createFromBigInteger($m))->get();
|
||||
}
|
||||
|
||||
public static function baseConvert(string $number, int $from, int $to): string
|
||||
{
|
||||
return BigInteger::fromBase($number, $from)->toBase($to);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
/**
|
||||
* Akeeba WebPush
|
||||
*
|
||||
* An abstraction layer for easier implementation of WebPush in Joomla components.
|
||||
*
|
||||
* @copyright (c) 2022 Akeeba Ltd
|
||||
* @license GNU GPL v3 or later; see LICENSE.txt
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Akeeba\WebPush\ECC;
|
||||
|
||||
use Brick\Math\BigInteger;
|
||||
|
||||
/**
|
||||
* This class is copied verbatim from the JWT Framework by Spomky Labs.
|
||||
*
|
||||
* You can find the original code at https://github.com/web-token/jwt-framework
|
||||
*
|
||||
* The original file has the following copyright notice:
|
||||
*
|
||||
* =====================================================================================================================
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2020 Spomky-Labs
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE-SPOMKY.txt file for details.
|
||||
*
|
||||
* =====================================================================================================================
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class ModularArithmetic
|
||||
{
|
||||
public static function sub(BigInteger $minuend, BigInteger $subtrahend, BigInteger $modulus): BigInteger
|
||||
{
|
||||
return $minuend->minus($subtrahend)->mod($modulus);
|
||||
}
|
||||
|
||||
public static function mul(BigInteger $multiplier, BigInteger $muliplicand, BigInteger $modulus): BigInteger
|
||||
{
|
||||
return $multiplier->multipliedBy($muliplicand)->mod($modulus);
|
||||
}
|
||||
|
||||
public static function div(BigInteger $dividend, BigInteger $divisor, BigInteger $modulus): BigInteger
|
||||
{
|
||||
return self::mul($dividend, Math::inverseMod($divisor, $modulus), $modulus);
|
||||
}
|
||||
}
|
||||
165
administrator/components/com_akeebabackup/webpush/ECC/Point.php
Normal file
165
administrator/components/com_akeebabackup/webpush/ECC/Point.php
Normal file
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
/**
|
||||
* Akeeba WebPush
|
||||
*
|
||||
* An abstraction layer for easier implementation of WebPush in Joomla components.
|
||||
*
|
||||
* @copyright (c) 2022 Akeeba Ltd
|
||||
* @license GNU GPL v3 or later; see LICENSE.txt
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Akeeba\WebPush\ECC;
|
||||
|
||||
use Brick\Math\BigInteger;
|
||||
|
||||
/**
|
||||
* This class is copied verbatim from the JWT Framework by Spomky Labs.
|
||||
*
|
||||
* You can find the original code at https://github.com/web-token/jwt-framework
|
||||
*
|
||||
* The original file has the following copyright notice:
|
||||
*
|
||||
* =====================================================================================================================
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2020 Spomky-Labs
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE-SPOMKY.txt file for details.
|
||||
*
|
||||
* =====================================================================================================================
|
||||
*
|
||||
* *********************************************************************
|
||||
* Copyright (C) 2012 Matyas Danter.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
||||
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
* ***********************************************************************
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class Point
|
||||
{
|
||||
/**
|
||||
* @var BigInteger
|
||||
*/
|
||||
private $x;
|
||||
|
||||
/**
|
||||
* @var BigInteger
|
||||
*/
|
||||
private $y;
|
||||
|
||||
/**
|
||||
* @var BigInteger
|
||||
*/
|
||||
private $order;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $infinity = false;
|
||||
|
||||
private function __construct(BigInteger $x, BigInteger $y, BigInteger $order, bool $infinity = false)
|
||||
{
|
||||
$this->x = $x;
|
||||
$this->y = $y;
|
||||
$this->order = $order;
|
||||
$this->infinity = $infinity;
|
||||
}
|
||||
|
||||
public static function create(BigInteger $x, BigInteger $y, ?BigInteger $order = null): self
|
||||
{
|
||||
return new self($x, $y, $order ?? BigInteger::zero());
|
||||
}
|
||||
|
||||
public static function infinity(): self
|
||||
{
|
||||
$zero = BigInteger::zero();
|
||||
|
||||
return new self($zero, $zero, $zero, true);
|
||||
}
|
||||
|
||||
public function isInfinity(): bool
|
||||
{
|
||||
return $this->infinity;
|
||||
}
|
||||
|
||||
public function getOrder(): BigInteger
|
||||
{
|
||||
return $this->order;
|
||||
}
|
||||
|
||||
public function getX(): BigInteger
|
||||
{
|
||||
return $this->x;
|
||||
}
|
||||
|
||||
public function getY(): BigInteger
|
||||
{
|
||||
return $this->y;
|
||||
}
|
||||
|
||||
public static function cswap(self $a, self $b, int $cond): void
|
||||
{
|
||||
self::cswapBigInteger($a->x, $b->x, $cond);
|
||||
self::cswapBigInteger($a->y, $b->y, $cond);
|
||||
self::cswapBigInteger($a->order, $b->order, $cond);
|
||||
self::cswapBoolean($a->infinity, $b->infinity, $cond);
|
||||
}
|
||||
|
||||
private static function cswapBoolean(bool &$a, bool &$b, int $cond): void
|
||||
{
|
||||
$sa = BigInteger::of((int) $a);
|
||||
$sb = BigInteger::of((int) $b);
|
||||
|
||||
self::cswapBigInteger($sa, $sb, $cond);
|
||||
|
||||
$a = (bool) $sa->toBase(10);
|
||||
$b = (bool) $sb->toBase(10);
|
||||
}
|
||||
|
||||
private static function cswapBigInteger(BigInteger &$sa, BigInteger &$sb, int $cond): void
|
||||
{
|
||||
$size = max(mb_strlen($sa->toBase(2), '8bit'), mb_strlen($sb->toBase(2), '8bit'));
|
||||
$mask = (string) (1 - $cond);
|
||||
$mask = str_pad('', $size, $mask, STR_PAD_LEFT);
|
||||
$mask = BigInteger::fromBase($mask, 2);
|
||||
$taA = $sa->and($mask);
|
||||
$taB = $sb->and($mask);
|
||||
$sa = $sa->xor($sb)->xor($taB);
|
||||
$sb = $sa->xor($sb)->xor($taA);
|
||||
$sa = $sa->xor($sb)->xor($taB);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
/**
|
||||
* Akeeba WebPush
|
||||
*
|
||||
* An abstraction layer for easier implementation of WebPush in Joomla components.
|
||||
*
|
||||
* @copyright (c) 2022 Akeeba Ltd
|
||||
* @license GNU GPL v3 or later; see LICENSE.txt
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Akeeba\WebPush\ECC;
|
||||
|
||||
use Brick\Math\BigInteger;
|
||||
|
||||
/**
|
||||
* This class is copied verbatim from the JWT Framework by Spomky Labs.
|
||||
*
|
||||
* You can find the original code at https://github.com/web-token/jwt-framework
|
||||
*
|
||||
* The original file has the following copyright notice:
|
||||
*
|
||||
* =====================================================================================================================
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2020 Spomky-Labs
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE-SPOMKY.txt file for details.
|
||||
*
|
||||
* =====================================================================================================================
|
||||
*
|
||||
* *********************************************************************
|
||||
* Copyright (C) 2012 Matyas Danter.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
||||
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
* ***********************************************************************
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class PrivateKey
|
||||
{
|
||||
/**
|
||||
* @var BigInteger
|
||||
*/
|
||||
private $secret;
|
||||
|
||||
private function __construct(BigInteger $secret)
|
||||
{
|
||||
$this->secret = $secret;
|
||||
}
|
||||
|
||||
public static function create(BigInteger $secret): self
|
||||
{
|
||||
return new self($secret);
|
||||
}
|
||||
|
||||
public function getSecret(): BigInteger
|
||||
{
|
||||
return $this->secret;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
/**
|
||||
* Akeeba WebPush
|
||||
*
|
||||
* An abstraction layer for easier implementation of WebPush in Joomla components.
|
||||
*
|
||||
* @copyright (c) 2022 Akeeba Ltd
|
||||
* @license GNU GPL v3 or later; see LICENSE.txt
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Akeeba\WebPush\ECC;
|
||||
|
||||
/**
|
||||
* This class is copied verbatim from the JWT Framework by Spomky Labs.
|
||||
*
|
||||
* You can find the original code at https://github.com/web-token/jwt-framework
|
||||
*
|
||||
* The original file has the following copyright notice:
|
||||
*
|
||||
* =====================================================================================================================
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014-2020 Spomky-Labs
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE-SPOMKY.txt file for details.
|
||||
*
|
||||
* =====================================================================================================================
|
||||
*
|
||||
* *********************************************************************
|
||||
* Copyright (C) 2012 Matyas Danter.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
||||
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
* ***********************************************************************
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class PublicKey
|
||||
{
|
||||
/**
|
||||
* @var Point
|
||||
*/
|
||||
private $point;
|
||||
|
||||
public function __construct(Point $point)
|
||||
{
|
||||
$this->point = $point;
|
||||
}
|
||||
|
||||
public function getPoint(): Point
|
||||
{
|
||||
return $this->point;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user