diff --git a/autoload-phpunit.php b/autoload-phpunit.php index 44060056..a9f2f601 100644 --- a/autoload-phpunit.php +++ b/autoload-phpunit.php @@ -1,11 +1,5 @@ = 50300) { - require_once (dirname(__FILE__) . '/tests/phpunit-shim.php'); -} require_once (dirname(__FILE__) . '/autoload.php'); -if (PHP_VERSION_ID >= 50300) { - require_once(dirname(__FILE__) . '/lib/php84compat.php'); -} +require_once (dirname(__FILE__) . '/lib/php84compat.php'); diff --git a/composer.json b/composer.json index 20337ed1..1305fbc1 100644 --- a/composer.json +++ b/composer.json @@ -60,7 +60,6 @@ "test": "phpunit" }, "suggest": { - "ext-libsodium": "PHP < 7.0: Better performance, password hashing (Argon2i), secure memory management (memzero), and better security.", - "ext-sodium": "PHP >= 7.0: Better performance, password hashing (Argon2i), secure memory management (memzero), and better security." + "ext-sodium": "Better performance, password hashing (Argon2i), secure memory management (memzero), and better security." } } diff --git a/src/Core/AES/Block.php b/src/Core/AES/Block.php index ae39b82e..771a9c5a 100644 --- a/src/Core/AES/Block.php +++ b/src/Core/AES/Block.php @@ -86,7 +86,7 @@ public static function fromArray($array, $save_indexes = null) * @psalm-suppress MixedArrayOffset */ #[ReturnTypeWillChange] - public function offsetSet($offset, $value) + public function offsetSet($offset, $value): void { if (is_null($offset)) { $this->values[] = $value; @@ -120,7 +120,7 @@ public function offsetExists($offset) * @psalm-suppress MixedArrayOffset */ #[ReturnTypeWillChange] - public function offsetUnset($offset) + public function offsetUnset($offset): void { unset($this->values[$offset]); } diff --git a/src/Core/BLAKE2b.php b/src/Core/BLAKE2b.php index 0e569621..fbe348b3 100644 --- a/src/Core/BLAKE2b.php +++ b/src/Core/BLAKE2b.php @@ -165,9 +165,9 @@ public static function rotr64(SplFixedArray $x, int $c): SplFixedArray } $l0 = 0; + /** @var int $c */ $c = 64 - $c; - /** @var int $c */ if ($c < 32) { $h0 = ((int) ($x[0]) << $c) | ( ( @@ -211,8 +211,6 @@ protected static function flatten64(SplFixedArray $x): int * @param SplFixedArray $x * @param int $i * @return SplFixedArray - * - * @throws SodiumException */ protected static function load64(SplFixedArray $x, int $i): SplFixedArray { diff --git a/src/Core/ChaCha20.php b/src/Core/ChaCha20.php index 0b8113fe..a3adfba9 100644 --- a/src/Core/ChaCha20.php +++ b/src/Core/ChaCha20.php @@ -84,24 +84,6 @@ public static function encryptBytes( ): string { $bytes = self::strlen($message); - /* - j0 = ctx->input[0]; - j1 = ctx->input[1]; - j2 = ctx->input[2]; - j3 = ctx->input[3]; - j4 = ctx->input[4]; - j5 = ctx->input[5]; - j6 = ctx->input[6]; - j7 = ctx->input[7]; - j8 = ctx->input[8]; - j9 = ctx->input[9]; - j10 = ctx->input[10]; - j11 = ctx->input[11]; - j12 = ctx->input[12]; - j13 = ctx->input[13]; - j14 = ctx->input[14]; - j15 = ctx->input[15]; - */ $j0 = (int) $ctx[0]; $j1 = (int) $ctx[1]; $j2 = (int) $ctx[2]; @@ -144,48 +126,16 @@ public static function encryptBytes( # for (i = 20; i > 0; i -= 2) { for ($i = 20; $i > 0; $i -= 2) { - # QUARTERROUND( x0, x4, x8, x12) - list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); + [$x0, $x4, $x8, $x12] = self::quarterRound($x0, $x4, $x8, $x12); + [$x1, $x5, $x9, $x13] = self::quarterRound($x1, $x5, $x9, $x13); + [$x2, $x6, $x10, $x14] = self::quarterRound($x2, $x6, $x10, $x14); + [$x3, $x7, $x11, $x15] = self::quarterRound($x3, $x7, $x11, $x15); - # QUARTERROUND( x1, x5, x9, x13) - list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); - - # QUARTERROUND( x2, x6, x10, x14) - list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); - - # QUARTERROUND( x3, x7, x11, x15) - list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); - - # QUARTERROUND( x0, x5, x10, x15) - list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); - - # QUARTERROUND( x1, x6, x11, x12) - list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); - - # QUARTERROUND( x2, x7, x8, x13) - list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); - - # QUARTERROUND( x3, x4, x9, x14) - list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); + [$x0, $x5, $x10, $x15] = self::quarterRound($x0, $x5, $x10, $x15); + [$x1, $x6, $x11, $x12] = self::quarterRound($x1, $x6, $x11, $x12); + [$x2, $x7, $x8, $x13] = self::quarterRound($x2, $x7, $x8, $x13); + [$x3, $x4, $x9, $x14] = self::quarterRound($x3, $x4, $x9, $x14); } - /* - x0 = PLUS(x0, j0); - x1 = PLUS(x1, j1); - x2 = PLUS(x2, j2); - x3 = PLUS(x3, j3); - x4 = PLUS(x4, j4); - x5 = PLUS(x5, j5); - x6 = PLUS(x6, j6); - x7 = PLUS(x7, j7); - x8 = PLUS(x8, j8); - x9 = PLUS(x9, j9); - x10 = PLUS(x10, j10); - x11 = PLUS(x11, j11); - x12 = PLUS(x12, j12); - x13 = PLUS(x13, j13); - x14 = PLUS(x14, j14); - x15 = PLUS(x15, j15); - */ $x0 = ($x0 & 0xffffffff) + $j0; $x1 = ($x1 & 0xffffffff) + $j1; $x2 = ($x2 & 0xffffffff) + $j2; @@ -203,24 +153,6 @@ public static function encryptBytes( $x14 = ($x14 & 0xffffffff) + $j14; $x15 = ($x15 & 0xffffffff) + $j15; - /* - x0 = XOR(x0, LOAD32_LE(m + 0)); - x1 = XOR(x1, LOAD32_LE(m + 4)); - x2 = XOR(x2, LOAD32_LE(m + 8)); - x3 = XOR(x3, LOAD32_LE(m + 12)); - x4 = XOR(x4, LOAD32_LE(m + 16)); - x5 = XOR(x5, LOAD32_LE(m + 20)); - x6 = XOR(x6, LOAD32_LE(m + 24)); - x7 = XOR(x7, LOAD32_LE(m + 28)); - x8 = XOR(x8, LOAD32_LE(m + 32)); - x9 = XOR(x9, LOAD32_LE(m + 36)); - x10 = XOR(x10, LOAD32_LE(m + 40)); - x11 = XOR(x11, LOAD32_LE(m + 44)); - x12 = XOR(x12, LOAD32_LE(m + 48)); - x13 = XOR(x13, LOAD32_LE(m + 52)); - x14 = XOR(x14, LOAD32_LE(m + 56)); - x15 = XOR(x15, LOAD32_LE(m + 60)); - */ $x0 ^= self::load_4(self::substr($message, 0, 4)); $x1 ^= self::load_4(self::substr($message, 4, 4)); $x2 ^= self::load_4(self::substr($message, 8, 4)); @@ -238,35 +170,11 @@ public static function encryptBytes( $x14 ^= self::load_4(self::substr($message, 56, 4)); $x15 ^= self::load_4(self::substr($message, 60, 4)); - /* - j12 = PLUSONE(j12); - if (!j12) { - j13 = PLUSONE(j13); - } - */ ++$j12; if ($j12 & 0xf0000000) { throw new SodiumException('Overflow'); } - /* - STORE32_LE(c + 0, x0); - STORE32_LE(c + 4, x1); - STORE32_LE(c + 8, x2); - STORE32_LE(c + 12, x3); - STORE32_LE(c + 16, x4); - STORE32_LE(c + 20, x5); - STORE32_LE(c + 24, x6); - STORE32_LE(c + 28, x7); - STORE32_LE(c + 32, x8); - STORE32_LE(c + 36, x9); - STORE32_LE(c + 40, x10); - STORE32_LE(c + 44, x11); - STORE32_LE(c + 48, x12); - STORE32_LE(c + 52, x13); - STORE32_LE(c + 56, x14); - STORE32_LE(c + 60, x15); - */ $block = self::store32_le(($x0 & 0xffffffff)) . self::store32_le(($x1 & 0xffffffff)) . self::store32_le(($x2 & 0xffffffff)) . diff --git a/src/Core/ChaCha20/Ctx.php b/src/Core/ChaCha20/Ctx.php index a44b413b..4a4317c6 100644 --- a/src/Core/ChaCha20/Ctx.php +++ b/src/Core/ChaCha20/Ctx.php @@ -77,7 +77,7 @@ public function __construct( * @return void */ #[ReturnTypeWillChange] - public function offsetSet($offset, $value) + public function offsetSet($offset, $value): void { $this->container[$offset] = $value; } @@ -101,7 +101,7 @@ public function offsetExists($offset) * @return void */ #[ReturnTypeWillChange] - public function offsetUnset($offset) + public function offsetUnset($offset): void { unset($this->container[$offset]); } diff --git a/src/Core/Curve25519.php b/src/Core/Curve25519.php index 82c65ec5..84f398d5 100644 --- a/src/Core/Curve25519.php +++ b/src/Core/Curve25519.php @@ -26,7 +26,7 @@ abstract class ParagonIE_Sodium_Core_Curve25519 extends ParagonIE_Sodium_Core_Cu public static function fe_0(): ParagonIE_Sodium_Core_Curve25519_Fe { return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( - array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0) + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ); } @@ -40,7 +40,7 @@ public static function fe_0(): ParagonIE_Sodium_Core_Curve25519_Fe public static function fe_1(): ParagonIE_Sodium_Core_Curve25519_Fe { return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( - array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0) + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0] ); } @@ -90,19 +90,6 @@ public static function fe_cmov( return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray($h); } - /** - * Create a copy of a field element. - * - * @internal You should not use this directly from another application - * - * @param ParagonIE_Sodium_Core_Curve25519_Fe $f - * @return ParagonIE_Sodium_Core_Curve25519_Fe - */ - public static function fe_copy(ParagonIE_Sodium_Core_Curve25519_Fe $f): ParagonIE_Sodium_Core_Curve25519_Fe - { - return (clone $f); - } - /** * Give: 32-byte string. * Receive: A field element object to use for internal calculations. @@ -165,18 +152,7 @@ public static function fe_frombytes( $h8 -= $carry8 << 26; return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( - array( - $h0, - $h1, - $h2, - $h3, - $h4, - $h5, - $h6, - $h7, - $h8, - $h9 - ) + [$h0, $h1, $h2, $h3, $h4, $h5, $h6, $h7, $h8, $h9] ); } @@ -529,18 +505,7 @@ public static function fe_mul( return self::fe_normalize( ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( - array( - $h0, - $h1, - $h2, - $h3, - $h4, - $h5, - $h6, - $h7, - $h8, - $h9 - ) + [$h0, $h1, $h2, $h3, $h4, $h5, $h6, $h7, $h8, $h9] ) ); } @@ -574,8 +539,9 @@ public static function fe_neg(ParagonIE_Sodium_Core_Curve25519_Fe $f): ParagonIE * @param ParagonIE_Sodium_Core_Curve25519_Fe $f * @return ParagonIE_Sodium_Core_Curve25519_Fe */ - public static function fe_sq(ParagonIE_Sodium_Core_Curve25519_Fe $f): ParagonIE_Sodium_Core_Curve25519_Fe - { + public static function fe_sq( + ParagonIE_Sodium_Core_Curve25519_Fe $f + ): ParagonIE_Sodium_Core_Curve25519_Fe { $f = self::fe_normalize($f); $f0 = $f[0]; $f1 = $f[1]; @@ -712,18 +678,7 @@ public static function fe_sq(ParagonIE_Sodium_Core_Curve25519_Fe $f): ParagonIE_ return self::fe_normalize( ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( - array( - $h0, - $h1, - $h2, - $h3, - $h4, - $h5, - $h6, - $h7, - $h8, - $h9 - ) + [$h0, $h1, $h2, $h3, $h4, $h5, $h6, $h7, $h8, $h9] ) ); } @@ -889,8 +844,9 @@ public static function fe_sq2(ParagonIE_Sodium_Core_Curve25519_Fe $f): ParagonIE * @param ParagonIE_Sodium_Core_Curve25519_Fe $Z * @return ParagonIE_Sodium_Core_Curve25519_Fe */ - public static function fe_invert(ParagonIE_Sodium_Core_Curve25519_Fe $Z): ParagonIE_Sodium_Core_Curve25519_Fe - { + public static function fe_invert( + ParagonIE_Sodium_Core_Curve25519_Fe $Z + ): ParagonIE_Sodium_Core_Curve25519_Fe { $z = clone $Z; $t0 = self::fe_sq($z); $t1 = self::fe_sq($t0); @@ -952,14 +908,6 @@ public static function fe_invert(ParagonIE_Sodium_Core_Curve25519_Fe $Z): Parago public static function fe_pow22523(ParagonIE_Sodium_Core_Curve25519_Fe $z): ParagonIE_Sodium_Core_Curve25519_Fe { $z = self::fe_normalize($z); - # fe_sq(t0, z); - # fe_sq(t1, t0); - # fe_sq(t1, t1); - # fe_mul(t1, z, t1); - # fe_mul(t0, t0, t1); - # fe_sq(t0, t0); - # fe_mul(t0, t1, t0); - # fe_sq(t1, t0); $t0 = self::fe_sq($z); $t1 = self::fe_sq($t0); $t1 = self::fe_sq($t1); @@ -969,89 +917,52 @@ public static function fe_pow22523(ParagonIE_Sodium_Core_Curve25519_Fe $z): Para $t0 = self::fe_mul($t1, $t0); $t1 = self::fe_sq($t0); - # for (i = 1; i < 5; ++i) { - # fe_sq(t1, t1); - # } for ($i = 1; $i < 5; ++$i) { $t1 = self::fe_sq($t1); } - # fe_mul(t0, t1, t0); - # fe_sq(t1, t0); $t0 = self::fe_mul($t1, $t0); $t1 = self::fe_sq($t0); - # for (i = 1; i < 10; ++i) { - # fe_sq(t1, t1); - # } for ($i = 1; $i < 10; ++$i) { $t1 = self::fe_sq($t1); } - # fe_mul(t1, t1, t0); - # fe_sq(t2, t1); $t1 = self::fe_mul($t1, $t0); $t2 = self::fe_sq($t1); - # for (i = 1; i < 20; ++i) { - # fe_sq(t2, t2); - # } for ($i = 1; $i < 20; ++$i) { $t2 = self::fe_sq($t2); } - # fe_mul(t1, t2, t1); - # fe_sq(t1, t1); $t1 = self::fe_mul($t2, $t1); $t1 = self::fe_sq($t1); - # for (i = 1; i < 10; ++i) { - # fe_sq(t1, t1); - # } for ($i = 1; $i < 10; ++$i) { $t1 = self::fe_sq($t1); } - # fe_mul(t0, t1, t0); - # fe_sq(t1, t0); $t0 = self::fe_mul($t1, $t0); $t1 = self::fe_sq($t0); - # for (i = 1; i < 50; ++i) { - # fe_sq(t1, t1); - # } for ($i = 1; $i < 50; ++$i) { $t1 = self::fe_sq($t1); } - # fe_mul(t1, t1, t0); - # fe_sq(t2, t1); $t1 = self::fe_mul($t1, $t0); $t2 = self::fe_sq($t1); - # for (i = 1; i < 100; ++i) { - # fe_sq(t2, t2); - # } for ($i = 1; $i < 100; ++$i) { $t2 = self::fe_sq($t2); } - # fe_mul(t1, t2, t1); - # fe_sq(t1, t1); $t1 = self::fe_mul($t2, $t1); $t1 = self::fe_sq($t1); - # for (i = 1; i < 50; ++i) { - # fe_sq(t1, t1); - # } for ($i = 1; $i < 50; ++$i) { $t1 = self::fe_sq($t1); } - # fe_mul(t0, t1, t0); - # fe_sq(t0, t0); - # fe_sq(t0, t0); - # fe_mul(out, t0, z); $t0 = self::fe_mul($t1, $t0); $t0 = self::fe_sq($t0); $t0 = self::fe_sq($t0); @@ -1147,7 +1058,6 @@ public static function slide(string $a): array /** @var array $r */ $r = array(); - /** @var int $i */ for ($i = 0; $i < 256; ++$i) { $r[$i] = ( 1 & ( @@ -1199,57 +1109,31 @@ public static function ge_frombytes_negate_vartime( if (!$d) { $d = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::D); } - - # fe_frombytes(h->Y,s); - # fe_1(h->Z); $h = new ParagonIE_Sodium_Core_Curve25519_Ge_P3( self::fe_0(), self::fe_frombytes($s), self::fe_1() ); - # fe_sq(u,h->Y); - # fe_mul(v,u,d); - # fe_sub(u,u,h->Z); /* u = y^2-1 */ - # fe_add(v,v,h->Z); /* v = dy^2+1 */ $u = self::fe_sq($h->Y); - /** @var ParagonIE_Sodium_Core_Curve25519_Fe $d */ $v = self::fe_mul($u, $d); $u = self::fe_sub($u, $h->Z); /* u = y^2 - 1 */ $v = self::fe_add($v, $h->Z); /* v = dy^2 + 1 */ - # fe_sq(v3,v); - # fe_mul(v3,v3,v); /* v3 = v^3 */ - # fe_sq(h->X,v3); - # fe_mul(h->X,h->X,v); - # fe_mul(h->X,h->X,u); /* x = uv^7 */ $v3 = self::fe_sq($v); $v3 = self::fe_mul($v3, $v); /* v3 = v^3 */ $h->X = self::fe_sq($v3); $h->X = self::fe_mul($h->X, $v); $h->X = self::fe_mul($h->X, $u); /* x = uv^7 */ - # fe_pow22523(h->X,h->X); /* x = (uv^7)^((q-5)/8) */ - # fe_mul(h->X,h->X,v3); - # fe_mul(h->X,h->X,u); /* x = uv^3(uv^7)^((q-5)/8) */ $h->X = self::fe_pow22523($h->X); /* x = (uv^7)^((q-5)/8) */ $h->X = self::fe_mul($h->X, $v3); $h->X = self::fe_mul($h->X, $u); /* x = uv^3(uv^7)^((q-5)/8) */ - # fe_sq(vxx,h->X); - # fe_mul(vxx,vxx,v); - # fe_sub(check,vxx,u); /* vx^2-u */ $vxx = self::fe_sq($h->X); $vxx = self::fe_mul($vxx, $v); $check = self::fe_sub($vxx, $u); /* vx^2 - u */ - # if (fe_isnonzero(check)) { - # fe_add(check,vxx,u); /* vx^2+u */ - # if (fe_isnonzero(check)) { - # return -1; - # } - # fe_mul(h->X,h->X,sqrtm1); - # } if (self::fe_isnonzero($check)) { $check = self::fe_add($vxx, $u); /* vx^2 + u */ if (self::fe_isnonzero($check)) { @@ -1260,10 +1144,6 @@ public static function ge_frombytes_negate_vartime( ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(self::SQRTM1) ); } - - # if (fe_isnegative(h->X) == (s[31] >> 7)) { - # fe_neg(h->X,h->X); - # } $i = self::chrToInt($s[31]); if (self::fe_isnegative($h->X) === ($i >> 7)) { $h->X = self::fe_neg($h->X); @@ -1434,7 +1314,7 @@ public static function ge_p3_to_cached( $r = new ParagonIE_Sodium_Core_Curve25519_Ge_Cached(); $r->YplusX = self::fe_add($p->Y, $p->X); $r->YminusX = self::fe_sub($p->Y, $p->X); - $r->Z = self::fe_copy($p->Z); + $r->Z = clone $p->Z; $r->T2d = self::fe_mul($p->T, $d2); return $r; } @@ -1449,9 +1329,9 @@ public static function ge_p3_to_p2( ParagonIE_Sodium_Core_Curve25519_Ge_P3 $p ): ParagonIE_Sodium_Core_Curve25519_Ge_P2 { return new ParagonIE_Sodium_Core_Curve25519_Ge_P2( - self::fe_copy($p->X), - self::fe_copy($p->Y), - self::fe_copy($p->Z) + clone $p->X, + clone $p->Y, + clone $p->Z ); } @@ -1586,12 +1466,9 @@ public static function ge_cmov8_cached( #[SensitiveParameter] int $b ): ParagonIE_Sodium_Core_Curve25519_Ge_Cached { - // const unsigned char bnegative = negative(b); - // const unsigned char babs = b - (((-bnegative) & b) * ((signed char) 1 << 1)); $bnegative = self::negative($b); $babs = $b - (((-$bnegative) & $b) << 1); - // ge25519_cached_0(t); $t = new ParagonIE_Sodium_Core_Curve25519_Ge_Cached( self::fe_1(), self::fe_1(), @@ -1599,26 +1476,14 @@ public static function ge_cmov8_cached( self::fe_0() ); - // ge25519_cmov_cached(t, &cached[0], equal(babs, 1)); - // ge25519_cmov_cached(t, &cached[1], equal(babs, 2)); - // ge25519_cmov_cached(t, &cached[2], equal(babs, 3)); - // ge25519_cmov_cached(t, &cached[3], equal(babs, 4)); - // ge25519_cmov_cached(t, &cached[4], equal(babs, 5)); - // ge25519_cmov_cached(t, &cached[5], equal(babs, 6)); - // ge25519_cmov_cached(t, &cached[6], equal(babs, 7)); - // ge25519_cmov_cached(t, &cached[7], equal(babs, 8)); for ($x = 0; $x < 8; ++$x) { $t = self::ge_cmov_cached($t, $cached[$x], self::equal($babs, $x + 1)); } - // fe25519_copy(minust.YplusX, t->YminusX); - // fe25519_copy(minust.YminusX, t->YplusX); - // fe25519_copy(minust.Z, t->Z); - // fe25519_neg(minust.T2d, t->T2d); $minust = new ParagonIE_Sodium_Core_Curve25519_Ge_Cached( - self::fe_copy($t->YminusX), - self::fe_copy($t->YplusX), - self::fe_copy($t->Z), + clone $t->YminusX, + clone $t->YplusX, + clone $t->Z, self::fe_neg($t->T2d) ); return self::ge_cmov_cached($t, $minust, $bnegative); @@ -1666,8 +1531,8 @@ public static function ge_select(int $pos = 0, int $b = 0): ParagonIE_Sodium_Cor ); } $minusT = new ParagonIE_Sodium_Core_Curve25519_Ge_Precomp( - self::fe_copy($t->yminusx), - self::fe_copy($t->yplusx), + clone $t->yminusx, + clone $t->yplusx, self::fe_neg($t->xy2d) ); return self::cmov($t, $minusT, $bnegative); @@ -1763,38 +1628,21 @@ public static function ge_double_scalarmult_vartime( ); } - # slide(aslide,a); - # slide(bslide,b); /** @var array $aslide */ $aslide = self::slide($a); /** @var array $bslide */ $bslide = self::slide($b); - # ge_p3_to_cached(&Ai[0],A); - # ge_p3_dbl(&t,A); ge_p1p1_to_p3(&A2,&t); $Ai[0] = self::ge_p3_to_cached($A); $t = self::ge_p3_dbl($A); $A2 = self::ge_p1p1_to_p3($t); - # ge_add(&t,&A2,&Ai[0]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[1],&u); - # ge_add(&t,&A2,&Ai[1]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[2],&u); - # ge_add(&t,&A2,&Ai[2]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[3],&u); - # ge_add(&t,&A2,&Ai[3]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[4],&u); - # ge_add(&t,&A2,&Ai[4]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[5],&u); - # ge_add(&t,&A2,&Ai[5]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[6],&u); - # ge_add(&t,&A2,&Ai[6]); ge_p1p1_to_p3(&u,&t); ge_p3_to_cached(&Ai[7],&u); for ($i = 0; $i < 7; ++$i) { $t = self::ge_add($A2, $Ai[$i]); $u = self::ge_p1p1_to_p3($t); $Ai[$i + 1] = self::ge_p3_to_cached($u); } - - # ge_p2_0(r); $r = self::ge_p2_0(); - - # for (i = 255;i >= 0;--i) { - # if (aslide[i] || bslide[i]) break; - # } $i = 255; for (; $i >= 0; --$i) { if ($aslide[$i] || $bslide[$i]) { @@ -1802,24 +1650,16 @@ public static function ge_double_scalarmult_vartime( } } - # for (;i >= 0;--i) { for (; $i >= 0; --$i) { - # ge_p2_dbl(&t,r); $t = self::ge_p2_dbl($r); - # if (aslide[i] > 0) { if ($aslide[$i] > 0) { - # ge_p1p1_to_p3(&u,&t); - # ge_add(&t,&u,&Ai[aslide[i]/2]); $u = self::ge_p1p1_to_p3($t); $t = self::ge_add( $u, $Ai[(int) floor($aslide[$i] / 2)] ); - # } else if (aslide[i] < 0) { } elseif ($aslide[$i] < 0) { - # ge_p1p1_to_p3(&u,&t); - # ge_sub(&t,&u,&Ai[(-aslide[i])/2]); $u = self::ge_p1p1_to_p3($t); $t = self::ge_sub( $u, @@ -1827,24 +1667,16 @@ public static function ge_double_scalarmult_vartime( ); } - # if (bslide[i] > 0) { if ($bslide[$i] > 0) { - /** @var int $index */ $index = (int) floor($bslide[$i] / 2); - # ge_p1p1_to_p3(&u,&t); - # ge_madd(&t,&u,&Bi[bslide[i]/2]); $u = self::ge_p1p1_to_p3($t); $t = self::ge_madd($t, $u, $Bi[$index]); # } else if (bslide[i] < 0) { } elseif ($bslide[$i] < 0) { - /** @var int $index */ $index = (int) floor(-$bslide[$i] / 2); - # ge_p1p1_to_p3(&u,&t); - # ge_msub(&t,&u,&Bi[(-bslide[i])/2]); $u = self::ge_p1p1_to_p3($t); $t = self::ge_msub($t, $u, $Bi[$index]); } - # ge_p1p1_to_p2(r,&t); $r = self::ge_p1p1_to_p2($t); } return $r; @@ -1869,77 +1701,42 @@ public static function ge_scalarmult( /** @var ParagonIE_Sodium_Core_Curve25519_Ge_Cached[] $pi */ $pi = array(); - // ge25519_p3_to_cached(&pi[1 - 1], p); /* p */ $pi[0] = self::ge_p3_to_cached($p); - // ge25519_p3_dbl(&t2, p); - // ge25519_p1p1_to_p3(&p2, &t2); - // ge25519_p3_to_cached(&pi[2 - 1], &p2); /* 2p = 2*p */ $t2 = self::ge_p3_dbl($p); $p2 = self::ge_p1p1_to_p3($t2); $pi[1] = self::ge_p3_to_cached($p2); - // ge25519_add_cached(&t3, p, &pi[2 - 1]); - // ge25519_p1p1_to_p3(&p3, &t3); - // ge25519_p3_to_cached(&pi[3 - 1], &p3); /* 3p = 2p+p */ $t3 = self::ge_add($p, $pi[1]); $p3 = self::ge_p1p1_to_p3($t3); $pi[2] = self::ge_p3_to_cached($p3); - // ge25519_p3_dbl(&t4, &p2); - // ge25519_p1p1_to_p3(&p4, &t4); - // ge25519_p3_to_cached(&pi[4 - 1], &p4); /* 4p = 2*2p */ $t4 = self::ge_p3_dbl($p2); $p4 = self::ge_p1p1_to_p3($t4); $pi[3] = self::ge_p3_to_cached($p4); - // ge25519_add_cached(&t5, p, &pi[4 - 1]); - // ge25519_p1p1_to_p3(&p5, &t5); - // ge25519_p3_to_cached(&pi[5 - 1], &p5); /* 5p = 4p+p */ $t5 = self::ge_add($p, $pi[3]); $p5 = self::ge_p1p1_to_p3($t5); $pi[4] = self::ge_p3_to_cached($p5); - // ge25519_p3_dbl(&t6, &p3); - // ge25519_p1p1_to_p3(&p6, &t6); - // ge25519_p3_to_cached(&pi[6 - 1], &p6); /* 6p = 2*3p */ $t6 = self::ge_p3_dbl($p3); $p6 = self::ge_p1p1_to_p3($t6); $pi[5] = self::ge_p3_to_cached($p6); - // ge25519_add_cached(&t7, p, &pi[6 - 1]); - // ge25519_p1p1_to_p3(&p7, &t7); - // ge25519_p3_to_cached(&pi[7 - 1], &p7); /* 7p = 6p+p */ $t7 = self::ge_add($p, $pi[5]); $p7 = self::ge_p1p1_to_p3($t7); $pi[6] = self::ge_p3_to_cached($p7); - // ge25519_p3_dbl(&t8, &p4); - // ge25519_p1p1_to_p3(&p8, &t8); - // ge25519_p3_to_cached(&pi[8 - 1], &p8); /* 8p = 2*4p */ $t8 = self::ge_p3_dbl($p4); $p8 = self::ge_p1p1_to_p3($t8); $pi[7] = self::ge_p3_to_cached($p8); - - // for (i = 0; i < 32; ++i) { - // e[2 * i + 0] = (a[i] >> 0) & 15; - // e[2 * i + 1] = (a[i] >> 4) & 15; - // } for ($i = 0; $i < 32; ++$i) { $e[($i << 1) ] = self::chrToInt($a[$i]) & 15; $e[($i << 1) + 1] = (self::chrToInt($a[$i]) >> 4) & 15; } // /* each e[i] is between 0 and 15 */ // /* e[63] is between 0 and 7 */ - - // carry = 0; - // for (i = 0; i < 63; ++i) { - // e[i] += carry; - // carry = e[i] + 8; - // carry >>= 4; - // e[i] -= carry * ((signed char) 1 << 4); - // } $carry = 0; for ($i = 0; $i < 63; ++$i) { $e[$i] += $carry; @@ -1947,28 +1744,14 @@ public static function ge_scalarmult( $carry >>= 4; $e[$i] -= $carry << 4; } - // e[63] += carry; - // /* each e[i] is between -8 and 8 */ $e[63] += $carry; - // ge25519_p3_0(h); $h = self::ge_p3_0(); - // for (i = 63; i != 0; i--) { for ($i = 63; $i != 0; --$i) { - // ge25519_cmov8_cached(&t, pi, e[i]); $t = self::ge_cmov8_cached($pi, $e[$i]); - // ge25519_add_cached(&r, h, &t); $r = self::ge_add($h, $t); - // ge25519_p1p1_to_p2(&s, &r); - // ge25519_p2_dbl(&r, &s); - // ge25519_p1p1_to_p2(&s, &r); - // ge25519_p2_dbl(&r, &s); - // ge25519_p1p1_to_p2(&s, &r); - // ge25519_p2_dbl(&r, &s); - // ge25519_p1p1_to_p2(&s, &r); - // ge25519_p2_dbl(&r, &s); $s = self::ge_p1p1_to_p2($r); $r = self::ge_p2_dbl($s); $s = self::ge_p1p1_to_p2($r); @@ -1978,13 +1761,9 @@ public static function ge_scalarmult( $s = self::ge_p1p1_to_p2($r); $r = self::ge_p2_dbl($s); - // ge25519_p1p1_to_p3(h, &r); /* *16 */ $h = self::ge_p1p1_to_p3($r); /* *16 */ } - // ge25519_cmov8_cached(&t, pi, e[i]); - // ge25519_add_cached(&r, h, &t); - // ge25519_p1p1_to_p3(h, &r); $t = self::ge_cmov8_cached($pi, $e[0]); $r = self::ge_add($h, $t); return self::ge_p1p1_to_p3($r); @@ -2470,7 +2249,7 @@ public static function sc_muladd( $s11 += $carry10; $s10 -= $carry10 << 21; - $arr = array( + return self::intArrayToString([ (0xff & ($s0 >> 0)), (0xff & ($s0 >> 8)), (0xff & (($s0 >> 16) | $s1 << 5)), @@ -2503,8 +2282,7 @@ public static function sc_muladd( (0xff & ($s11 >> 1)), (0xff & ($s11 >> 9)), 0xff & ($s11 >> 17) - ); - return self::intArrayToString($arr); + ]); } /** @@ -2787,10 +2565,7 @@ public static function sc_reduce( $s11 += $carry10; $s10 -= $carry10 << 21; - /** - * @var array - */ - $arr = array( + return self::intArrayToString([ ($s0 >> 0), ($s0 >> 8), (($s0 >> 16) | $s1 << 5), @@ -2823,8 +2598,7 @@ public static function sc_reduce( ($s11 >> 1), ($s11 >> 9), $s11 >> 17 - ); - return self::intArrayToString($arr); + ]); } /** @@ -2854,19 +2628,13 @@ public static function ge_mul_l( /** @var array $Ai size 8 */ $Ai = array(); - # ge_p3_to_cached(&Ai[0], A); $Ai[0] = self::ge_p3_to_cached($A); - # ge_p3_dbl(&t, A); $t = self::ge_p3_dbl($A); - # ge_p1p1_to_p3(&A2, &t); $A2 = self::ge_p1p1_to_p3($t); for ($i = 1; $i < 8; ++$i) { - # ge_add(&t, &A2, &Ai[0]); $t = self::ge_add($A2, $Ai[$i - 1]); - # ge_p1p1_to_p3(&u, &t); $u = self::ge_p1p1_to_p3($t); - # ge_p3_to_cached(&Ai[i], &u); $Ai[$i] = self::ge_p3_to_cached($u); } @@ -2874,19 +2642,14 @@ public static function ge_mul_l( for ($i = 252; $i >= 0; --$i) { $t = self::ge_p3_dbl($r); if ($aslide[$i] > 0) { - # ge_p1p1_to_p3(&u, &t); $u = self::ge_p1p1_to_p3($t); - # ge_add(&t, &u, &Ai[aslide[i] / 2]); $t = self::ge_add($u, $Ai[(int)($aslide[$i] / 2)]); } elseif ($aslide[$i] < 0) { - # ge_p1p1_to_p3(&u, &t); $u = self::ge_p1p1_to_p3($t); - # ge_sub(&t, &u, &Ai[(-aslide[i]) / 2]); $t = self::ge_sub($u, $Ai[(int)(-$aslide[$i] / 2)]); } } - # ge_p1p1_to_p3(r, &t); return self::ge_p1p1_to_p3($t); } @@ -2901,18 +2664,6 @@ public static function sc25519_mul( #[SensitiveParameter] string $b ): string { - // int64_t a0 = 2097151 & load_3(a); - // int64_t a1 = 2097151 & (load_4(a + 2) >> 5); - // int64_t a2 = 2097151 & (load_3(a + 5) >> 2); - // int64_t a3 = 2097151 & (load_4(a + 7) >> 7); - // int64_t a4 = 2097151 & (load_4(a + 10) >> 4); - // int64_t a5 = 2097151 & (load_3(a + 13) >> 1); - // int64_t a6 = 2097151 & (load_4(a + 15) >> 6); - // int64_t a7 = 2097151 & (load_3(a + 18) >> 3); - // int64_t a8 = 2097151 & load_3(a + 21); - // int64_t a9 = 2097151 & (load_4(a + 23) >> 5); - // int64_t a10 = 2097151 & (load_3(a + 26) >> 2); - // int64_t a11 = (load_4(a + 28) >> 7); $a0 = 2097151 & self::load_3(self::substr($a, 0, 3)); $a1 = 2097151 & (self::load_4(self::substr($a, 2, 4)) >> 5); $a2 = 2097151 & (self::load_3(self::substr($a, 5, 3)) >> 2); @@ -2926,18 +2677,6 @@ public static function sc25519_mul( $a10 = 2097151 & (self::load_3(self::substr($a, 26, 3)) >> 2); $a11 = (self::load_4(self::substr($a, 28, 4)) >> 7); - // int64_t b0 = 2097151 & load_3(b); - // int64_t b1 = 2097151 & (load_4(b + 2) >> 5); - // int64_t b2 = 2097151 & (load_3(b + 5) >> 2); - // int64_t b3 = 2097151 & (load_4(b + 7) >> 7); - // int64_t b4 = 2097151 & (load_4(b + 10) >> 4); - // int64_t b5 = 2097151 & (load_3(b + 13) >> 1); - // int64_t b6 = 2097151 & (load_4(b + 15) >> 6); - // int64_t b7 = 2097151 & (load_3(b + 18) >> 3); - // int64_t b8 = 2097151 & load_3(b + 21); - // int64_t b9 = 2097151 & (load_4(b + 23) >> 5); - // int64_t b10 = 2097151 & (load_3(b + 26) >> 2); - // int64_t b11 = (load_4(b + 28) >> 7); $b0 = 2097151 & self::load_3(self::substr($b, 0, 3)); $b1 = 2097151 & (self::load_4(self::substr($b, 2, 4)) >> 5); $b2 = 2097151 & (self::load_3(self::substr($b, 5, 3)) >> 2); @@ -2951,40 +2690,6 @@ public static function sc25519_mul( $b10 = 2097151 & (self::load_3(self::substr($b, 26, 3)) >> 2); $b11 = (self::load_4(self::substr($b, 28, 4)) >> 7); - // s0 = a0 * b0; - // s1 = a0 * b1 + a1 * b0; - // s2 = a0 * b2 + a1 * b1 + a2 * b0; - // s3 = a0 * b3 + a1 * b2 + a2 * b1 + a3 * b0; - // s4 = a0 * b4 + a1 * b3 + a2 * b2 + a3 * b1 + a4 * b0; - // s5 = a0 * b5 + a1 * b4 + a2 * b3 + a3 * b2 + a4 * b1 + a5 * b0; - // s6 = a0 * b6 + a1 * b5 + a2 * b4 + a3 * b3 + a4 * b2 + a5 * b1 + a6 * b0; - // s7 = a0 * b7 + a1 * b6 + a2 * b5 + a3 * b4 + a4 * b3 + a5 * b2 + - // a6 * b1 + a7 * b0; - // s8 = a0 * b8 + a1 * b7 + a2 * b6 + a3 * b5 + a4 * b4 + a5 * b3 + - // a6 * b2 + a7 * b1 + a8 * b0; - // s9 = a0 * b9 + a1 * b8 + a2 * b7 + a3 * b6 + a4 * b5 + a5 * b4 + - // a6 * b3 + a7 * b2 + a8 * b1 + a9 * b0; - // s10 = a0 * b10 + a1 * b9 + a2 * b8 + a3 * b7 + a4 * b6 + a5 * b5 + - // a6 * b4 + a7 * b3 + a8 * b2 + a9 * b1 + a10 * b0; - // s11 = a0 * b11 + a1 * b10 + a2 * b9 + a3 * b8 + a4 * b7 + a5 * b6 + - // a6 * b5 + a7 * b4 + a8 * b3 + a9 * b2 + a10 * b1 + a11 * b0; - // s12 = a1 * b11 + a2 * b10 + a3 * b9 + a4 * b8 + a5 * b7 + a6 * b6 + - // a7 * b5 + a8 * b4 + a9 * b3 + a10 * b2 + a11 * b1; - // s13 = a2 * b11 + a3 * b10 + a4 * b9 + a5 * b8 + a6 * b7 + a7 * b6 + - // a8 * b5 + a9 * b4 + a10 * b3 + a11 * b2; - // s14 = a3 * b11 + a4 * b10 + a5 * b9 + a6 * b8 + a7 * b7 + a8 * b6 + - // a9 * b5 + a10 * b4 + a11 * b3; - // s15 = a4 * b11 + a5 * b10 + a6 * b9 + a7 * b8 + a8 * b7 + a9 * b6 + - // a10 * b5 + a11 * b4; - // s16 = - // a5 * b11 + a6 * b10 + a7 * b9 + a8 * b8 + a9 * b7 + a10 * b6 + a11 * b5; - // s17 = a6 * b11 + a7 * b10 + a8 * b9 + a9 * b8 + a10 * b7 + a11 * b6; - // s18 = a7 * b11 + a8 * b10 + a9 * b9 + a10 * b8 + a11 * b7; - // s19 = a8 * b11 + a9 * b10 + a10 * b9 + a11 * b8; - // s20 = a9 * b11 + a10 * b10 + a11 * b9; - // s21 = a10 * b11 + a11 * b10; - // s22 = a11 * b11; - // s23 = 0; $s0 = self::mul($a0, $b0, 22); $s1 = self::mul($a0, $b1, 22) + self::mul($a1, $b0, 22); $s2 = self::mul($a0, $b2, 22) + self::mul($a1, $b1, 22) + self::mul($a2, $b0, 22); @@ -3034,152 +2739,77 @@ public static function sc25519_mul( $s22 = self::mul($a11, $b11, 22); $s23 = 0; - // carry0 = (s0 + (int64_t) (1L << 20)) >> 21; - // s1 += carry0; - // s0 -= carry0 * ((uint64_t) 1L << 21); $carry0 = ($s0 + (1 << 20)) >> 21; $s1 += $carry0; $s0 -= $carry0 << 21; - // carry2 = (s2 + (int64_t) (1L << 20)) >> 21; - // s3 += carry2; - // s2 -= carry2 * ((uint64_t) 1L << 21); $carry2 = ($s2 + (1 << 20)) >> 21; $s3 += $carry2; $s2 -= $carry2 << 21; - // carry4 = (s4 + (int64_t) (1L << 20)) >> 21; - // s5 += carry4; - // s4 -= carry4 * ((uint64_t) 1L << 21); $carry4 = ($s4 + (1 << 20)) >> 21; $s5 += $carry4; $s4 -= $carry4 << 21; - // carry6 = (s6 + (int64_t) (1L << 20)) >> 21; - // s7 += carry6; - // s6 -= carry6 * ((uint64_t) 1L << 21); $carry6 = ($s6 + (1 << 20)) >> 21; $s7 += $carry6; $s6 -= $carry6 << 21; - // carry8 = (s8 + (int64_t) (1L << 20)) >> 21; - // s9 += carry8; - // s8 -= carry8 * ((uint64_t) 1L << 21); $carry8 = ($s8 + (1 << 20)) >> 21; $s9 += $carry8; $s8 -= $carry8 << 21; - // carry10 = (s10 + (int64_t) (1L << 20)) >> 21; - // s11 += carry10; - // s10 -= carry10 * ((uint64_t) 1L << 21); $carry10 = ($s10 + (1 << 20)) >> 21; $s11 += $carry10; $s10 -= $carry10 << 21; - // carry12 = (s12 + (int64_t) (1L << 20)) >> 21; - // s13 += carry12; - // s12 -= carry12 * ((uint64_t) 1L << 21); $carry12 = ($s12 + (1 << 20)) >> 21; $s13 += $carry12; $s12 -= $carry12 << 21; - // carry14 = (s14 + (int64_t) (1L << 20)) >> 21; - // s15 += carry14; - // s14 -= carry14 * ((uint64_t) 1L << 21); $carry14 = ($s14 + (1 << 20)) >> 21; $s15 += $carry14; $s14 -= $carry14 << 21; - // carry16 = (s16 + (int64_t) (1L << 20)) >> 21; - // s17 += carry16; - // s16 -= carry16 * ((uint64_t) 1L << 21); $carry16 = ($s16 + (1 << 20)) >> 21; $s17 += $carry16; $s16 -= $carry16 << 21; - // carry18 = (s18 + (int64_t) (1L << 20)) >> 21; - // s19 += carry18; - // s18 -= carry18 * ((uint64_t) 1L << 21); $carry18 = ($s18 + (1 << 20)) >> 21; $s19 += $carry18; $s18 -= $carry18 << 21; - // carry20 = (s20 + (int64_t) (1L << 20)) >> 21; - // s21 += carry20; - // s20 -= carry20 * ((uint64_t) 1L << 21); $carry20 = ($s20 + (1 << 20)) >> 21; $s21 += $carry20; $s20 -= $carry20 << 21; - // carry22 = (s22 + (int64_t) (1L << 20)) >> 21; - // s23 += carry22; - // s22 -= carry22 * ((uint64_t) 1L << 21); $carry22 = ($s22 + (1 << 20)) >> 21; $s23 += $carry22; $s22 -= $carry22 << 21; - // carry1 = (s1 + (int64_t) (1L << 20)) >> 21; - // s2 += carry1; - // s1 -= carry1 * ((uint64_t) 1L << 21); $carry1 = ($s1 + (1 << 20)) >> 21; $s2 += $carry1; $s1 -= $carry1 << 21; - // carry3 = (s3 + (int64_t) (1L << 20)) >> 21; - // s4 += carry3; - // s3 -= carry3 * ((uint64_t) 1L << 21); $carry3 = ($s3 + (1 << 20)) >> 21; $s4 += $carry3; $s3 -= $carry3 << 21; - // carry5 = (s5 + (int64_t) (1L << 20)) >> 21; - // s6 += carry5; - // s5 -= carry5 * ((uint64_t) 1L << 21); $carry5 = ($s5 + (1 << 20)) >> 21; $s6 += $carry5; $s5 -= $carry5 << 21; - // carry7 = (s7 + (int64_t) (1L << 20)) >> 21; - // s8 += carry7; - // s7 -= carry7 * ((uint64_t) 1L << 21); $carry7 = ($s7 + (1 << 20)) >> 21; $s8 += $carry7; $s7 -= $carry7 << 21; - // carry9 = (s9 + (int64_t) (1L << 20)) >> 21; - // s10 += carry9; - // s9 -= carry9 * ((uint64_t) 1L << 21); $carry9 = ($s9 + (1 << 20)) >> 21; $s10 += $carry9; $s9 -= $carry9 << 21; - // carry11 = (s11 + (int64_t) (1L << 20)) >> 21; - // s12 += carry11; - // s11 -= carry11 * ((uint64_t) 1L << 21); $carry11 = ($s11 + (1 << 20)) >> 21; $s12 += $carry11; $s11 -= $carry11 << 21; - // carry13 = (s13 + (int64_t) (1L << 20)) >> 21; - // s14 += carry13; - // s13 -= carry13 * ((uint64_t) 1L << 21); $carry13 = ($s13 + (1 << 20)) >> 21; $s14 += $carry13; $s13 -= $carry13 << 21; - // carry15 = (s15 + (int64_t) (1L << 20)) >> 21; - // s16 += carry15; - // s15 -= carry15 * ((uint64_t) 1L << 21); $carry15 = ($s15 + (1 << 20)) >> 21; $s16 += $carry15; $s15 -= $carry15 << 21; - // carry17 = (s17 + (int64_t) (1L << 20)) >> 21; - // s18 += carry17; - // s17 -= carry17 * ((uint64_t) 1L << 21); $carry17 = ($s17 + (1 << 20)) >> 21; $s18 += $carry17; $s17 -= $carry17 << 21; - // carry19 = (s19 + (int64_t) (1L << 20)) >> 21; - // s20 += carry19; - // s19 -= carry19 * ((uint64_t) 1L << 21); $carry19 = ($s19 + (1 << 20)) >> 21; $s20 += $carry19; $s19 -= $carry19 << 21; - // carry21 = (s21 + (int64_t) (1L << 20)) >> 21; - // s22 += carry21; - // s21 -= carry21 * ((uint64_t) 1L << 21); $carry21 = ($s21 + (1 << 20)) >> 21; $s22 += $carry21; $s21 -= $carry21 << 21; - // s11 += s23 * 666643; - // s12 += s23 * 470296; - // s13 += s23 * 654183; - // s14 -= s23 * 997805; - // s15 += s23 * 136657; - // s16 -= s23 * 683901; $s11 += self::mul($s23, 666643, 20); $s12 += self::mul($s23, 470296, 19); $s13 += self::mul($s23, 654183, 20); @@ -3187,12 +2817,6 @@ public static function sc25519_mul( $s15 += self::mul($s23, 136657, 18); $s16 -= self::mul($s23, 683901, 20); - // s10 += s22 * 666643; - // s11 += s22 * 470296; - // s12 += s22 * 654183; - // s13 -= s22 * 997805; - // s14 += s22 * 136657; - // s15 -= s22 * 683901; $s10 += self::mul($s22, 666643, 20); $s11 += self::mul($s22, 470296, 19); $s12 += self::mul($s22, 654183, 20); @@ -3200,12 +2824,6 @@ public static function sc25519_mul( $s14 += self::mul($s22, 136657, 18); $s15 -= self::mul($s22, 683901, 20); - // s9 += s21 * 666643; - // s10 += s21 * 470296; - // s11 += s21 * 654183; - // s12 -= s21 * 997805; - // s13 += s21 * 136657; - // s14 -= s21 * 683901; $s9 += self::mul($s21, 666643, 20); $s10 += self::mul($s21, 470296, 19); $s11 += self::mul($s21, 654183, 20); @@ -3213,12 +2831,6 @@ public static function sc25519_mul( $s13 += self::mul($s21, 136657, 18); $s14 -= self::mul($s21, 683901, 20); - // s8 += s20 * 666643; - // s9 += s20 * 470296; - // s10 += s20 * 654183; - // s11 -= s20 * 997805; - // s12 += s20 * 136657; - // s13 -= s20 * 683901; $s8 += self::mul($s20, 666643, 20); $s9 += self::mul($s20, 470296, 19); $s10 += self::mul($s20, 654183, 20); @@ -3226,12 +2838,6 @@ public static function sc25519_mul( $s12 += self::mul($s20, 136657, 18); $s13 -= self::mul($s20, 683901, 20); - // s7 += s19 * 666643; - // s8 += s19 * 470296; - // s9 += s19 * 654183; - // s10 -= s19 * 997805; - // s11 += s19 * 136657; - // s12 -= s19 * 683901; $s7 += self::mul($s19, 666643, 20); $s8 += self::mul($s19, 470296, 19); $s9 += self::mul($s19, 654183, 20); @@ -3239,12 +2845,6 @@ public static function sc25519_mul( $s11 += self::mul($s19, 136657, 18); $s12 -= self::mul($s19, 683901, 20); - // s6 += s18 * 666643; - // s7 += s18 * 470296; - // s8 += s18 * 654183; - // s9 -= s18 * 997805; - // s10 += s18 * 136657; - // s11 -= s18 * 683901; $s6 += self::mul($s18, 666643, 20); $s7 += self::mul($s18, 470296, 19); $s8 += self::mul($s18, 654183, 20); @@ -3252,80 +2852,41 @@ public static function sc25519_mul( $s10 += self::mul($s18, 136657, 18); $s11 -= self::mul($s18, 683901, 20); - // carry6 = (s6 + (int64_t) (1L << 20)) >> 21; - // s7 += carry6; - // s6 -= carry6 * ((uint64_t) 1L << 21); $carry6 = ($s6 + (1 << 20)) >> 21; $s7 += $carry6; $s6 -= $carry6 << 21; - // carry8 = (s8 + (int64_t) (1L << 20)) >> 21; - // s9 += carry8; - // s8 -= carry8 * ((uint64_t) 1L << 21); $carry8 = ($s8 + (1 << 20)) >> 21; $s9 += $carry8; $s8 -= $carry8 << 21; - // carry10 = (s10 + (int64_t) (1L << 20)) >> 21; - // s11 += carry10; - // s10 -= carry10 * ((uint64_t) 1L << 21); $carry10 = ($s10 + (1 << 20)) >> 21; $s11 += $carry10; $s10 -= $carry10 << 21; - // carry12 = (s12 + (int64_t) (1L << 20)) >> 21; - // s13 += carry12; - // s12 -= carry12 * ((uint64_t) 1L << 21); $carry12 = ($s12 + (1 << 20)) >> 21; $s13 += $carry12; $s12 -= $carry12 << 21; - // carry14 = (s14 + (int64_t) (1L << 20)) >> 21; - // s15 += carry14; - // s14 -= carry14 * ((uint64_t) 1L << 21); $carry14 = ($s14 + (1 << 20)) >> 21; $s15 += $carry14; $s14 -= $carry14 << 21; - // carry16 = (s16 + (int64_t) (1L << 20)) >> 21; - // s17 += carry16; - // s16 -= carry16 * ((uint64_t) 1L << 21); $carry16 = ($s16 + (1 << 20)) >> 21; $s17 += $carry16; $s16 -= $carry16 << 21; - // carry7 = (s7 + (int64_t) (1L << 20)) >> 21; - // s8 += carry7; - // s7 -= carry7 * ((uint64_t) 1L << 21); $carry7 = ($s7 + (1 << 20)) >> 21; $s8 += $carry7; $s7 -= $carry7 << 21; - // carry9 = (s9 + (int64_t) (1L << 20)) >> 21; - // s10 += carry9; - // s9 -= carry9 * ((uint64_t) 1L << 21); $carry9 = ($s9 + (1 << 20)) >> 21; $s10 += $carry9; $s9 -= $carry9 << 21; - // carry11 = (s11 + (int64_t) (1L << 20)) >> 21; - // s12 += carry11; - // s11 -= carry11 * ((uint64_t) 1L << 21); $carry11 = ($s11 + (1 << 20)) >> 21; $s12 += $carry11; $s11 -= $carry11 << 21; - // carry13 = (s13 + (int64_t) (1L << 20)) >> 21; - // s14 += carry13; - // s13 -= carry13 * ((uint64_t) 1L << 21); $carry13 = ($s13 + (1 << 20)) >> 21; $s14 += $carry13; $s13 -= $carry13 << 21; - // carry15 = (s15 + (int64_t) (1L << 20)) >> 21; - // s16 += carry15; - // s15 -= carry15 * ((uint64_t) 1L << 21); $carry15 = ($s15 + (1 << 20)) >> 21; $s16 += $carry15; $s15 -= $carry15 << 21; - // s5 += s17 * 666643; - // s6 += s17 * 470296; - // s7 += s17 * 654183; - // s8 -= s17 * 997805; - // s9 += s17 * 136657; - // s10 -= s17 * 683901; $s5 += self::mul($s17, 666643, 20); $s6 += self::mul($s17, 470296, 19); $s7 += self::mul($s17, 654183, 20); @@ -3333,12 +2894,6 @@ public static function sc25519_mul( $s9 += self::mul($s17, 136657, 18); $s10 -= self::mul($s17, 683901, 20); - // s4 += s16 * 666643; - // s5 += s16 * 470296; - // s6 += s16 * 654183; - // s7 -= s16 * 997805; - // s8 += s16 * 136657; - // s9 -= s16 * 683901; $s4 += self::mul($s16, 666643, 20); $s5 += self::mul($s16, 470296, 19); $s6 += self::mul($s16, 654183, 20); @@ -3346,12 +2901,6 @@ public static function sc25519_mul( $s8 += self::mul($s16, 136657, 18); $s9 -= self::mul($s16, 683901, 20); - // s3 += s15 * 666643; - // s4 += s15 * 470296; - // s5 += s15 * 654183; - // s6 -= s15 * 997805; - // s7 += s15 * 136657; - // s8 -= s15 * 683901; $s3 += self::mul($s15, 666643, 20); $s4 += self::mul($s15, 470296, 19); $s5 += self::mul($s15, 654183, 20); @@ -3359,12 +2908,6 @@ public static function sc25519_mul( $s7 += self::mul($s15, 136657, 18); $s8 -= self::mul($s15, 683901, 20); - // s2 += s14 * 666643; - // s3 += s14 * 470296; - // s4 += s14 * 654183; - // s5 -= s14 * 997805; - // s6 += s14 * 136657; - // s7 -= s14 * 683901; $s2 += self::mul($s14, 666643, 20); $s3 += self::mul($s14, 470296, 19); $s4 += self::mul($s14, 654183, 20); @@ -3372,12 +2915,6 @@ public static function sc25519_mul( $s6 += self::mul($s14, 136657, 18); $s7 -= self::mul($s14, 683901, 20); - // s1 += s13 * 666643; - // s2 += s13 * 470296; - // s3 += s13 * 654183; - // s4 -= s13 * 997805; - // s5 += s13 * 136657; - // s6 -= s13 * 683901; $s1 += self::mul($s13, 666643, 20); $s2 += self::mul($s13, 470296, 19); $s3 += self::mul($s13, 654183, 20); @@ -3385,13 +2922,6 @@ public static function sc25519_mul( $s5 += self::mul($s13, 136657, 18); $s6 -= self::mul($s13, 683901, 20); - // s0 += s12 * 666643; - // s1 += s12 * 470296; - // s2 += s12 * 654183; - // s3 -= s12 * 997805; - // s4 += s12 * 136657; - // s5 -= s12 * 683901; - // s12 = 0; $s0 += self::mul($s12, 666643, 20); $s1 += self::mul($s12, 470296, 19); $s2 += self::mul($s12, 654183, 20); @@ -3400,87 +2930,44 @@ public static function sc25519_mul( $s5 -= self::mul($s12, 683901, 20); $s12 = 0; - // carry0 = (s0 + (int64_t) (1L << 20)) >> 21; - // s1 += carry0; - // s0 -= carry0 * ((uint64_t) 1L << 21); $carry0 = ($s0 + (1 << 20)) >> 21; $s1 += $carry0; $s0 -= $carry0 << 21; - // carry2 = (s2 + (int64_t) (1L << 20)) >> 21; - // s3 += carry2; - // s2 -= carry2 * ((uint64_t) 1L << 21); $carry2 = ($s2 + (1 << 20)) >> 21; $s3 += $carry2; $s2 -= $carry2 << 21; - // carry4 = (s4 + (int64_t) (1L << 20)) >> 21; - // s5 += carry4; - // s4 -= carry4 * ((uint64_t) 1L << 21); $carry4 = ($s4 + (1 << 20)) >> 21; $s5 += $carry4; $s4 -= $carry4 << 21; - // carry6 = (s6 + (int64_t) (1L << 20)) >> 21; - // s7 += carry6; - // s6 -= carry6 * ((uint64_t) 1L << 21); $carry6 = ($s6 + (1 << 20)) >> 21; $s7 += $carry6; $s6 -= $carry6 << 21; - // carry8 = (s8 + (int64_t) (1L << 20)) >> 21; - // s9 += carry8; - // s8 -= carry8 * ((uint64_t) 1L << 21); $carry8 = ($s8 + (1 << 20)) >> 21; $s9 += $carry8; $s8 -= $carry8 << 21; - // carry10 = (s10 + (int64_t) (1L << 20)) >> 21; - // s11 += carry10; - // s10 -= carry10 * ((uint64_t) 1L << 21); $carry10 = ($s10 + (1 << 20)) >> 21; $s11 += $carry10; $s10 -= $carry10 << 21; - // carry1 = (s1 + (int64_t) (1L << 20)) >> 21; - // s2 += carry1; - // s1 -= carry1 * ((uint64_t) 1L << 21); $carry1 = ($s1 + (1 << 20)) >> 21; $s2 += $carry1; $s1 -= $carry1 << 21; - // carry3 = (s3 + (int64_t) (1L << 20)) >> 21; - // s4 += carry3; - // s3 -= carry3 * ((uint64_t) 1L << 21); $carry3 = ($s3 + (1 << 20)) >> 21; $s4 += $carry3; $s3 -= $carry3 << 21; - // carry5 = (s5 + (int64_t) (1L << 20)) >> 21; - // s6 += carry5; - // s5 -= carry5 * ((uint64_t) 1L << 21); $carry5 = ($s5 + (1 << 20)) >> 21; $s6 += $carry5; $s5 -= $carry5 << 21; - // carry7 = (s7 + (int64_t) (1L << 20)) >> 21; - // s8 += carry7; - // s7 -= carry7 * ((uint64_t) 1L << 21); $carry7 = ($s7 + (1 << 20)) >> 21; $s8 += $carry7; $s7 -= $carry7 << 21; - // carry9 = (s9 + (int64_t) (1L << 20)) >> 21; - // s10 += carry9; - // s9 -= carry9 * ((uint64_t) 1L << 21); $carry9 = ($s9 + (1 << 20)) >> 21; $s10 += $carry9; $s9 -= $carry9 << 21; - // carry11 = (s11 + (int64_t) (1L << 20)) >> 21; - // s12 += carry11; - // s11 -= carry11 * ((uint64_t) 1L << 21); $carry11 = ($s11 + (1 << 20)) >> 21; $s12 += $carry11; $s11 -= $carry11 << 21; - // s0 += s12 * 666643; - // s1 += s12 * 470296; - // s2 += s12 * 654183; - // s3 -= s12 * 997805; - // s4 += s12 * 136657; - // s5 -= s12 * 683901; - // s12 = 0; $s0 += self::mul($s12, 666643, 20); $s1 += self::mul($s12, 470296, 19); $s2 += self::mul($s12, 654183, 20); @@ -3489,85 +2976,43 @@ public static function sc25519_mul( $s5 -= self::mul($s12, 683901, 20); $s12 = 0; - // carry0 = s0 >> 21; - // s1 += carry0; - // s0 -= carry0 * ((uint64_t) 1L << 21); $carry0 = $s0 >> 21; $s1 += $carry0; $s0 -= $carry0 << 21; - // carry1 = s1 >> 21; - // s2 += carry1; - // s1 -= carry1 * ((uint64_t) 1L << 21); $carry1 = $s1 >> 21; $s2 += $carry1; $s1 -= $carry1 << 21; - // carry2 = s2 >> 21; - // s3 += carry2; - // s2 -= carry2 * ((uint64_t) 1L << 21); $carry2 = $s2 >> 21; $s3 += $carry2; $s2 -= $carry2 << 21; - // carry3 = s3 >> 21; - // s4 += carry3; - // s3 -= carry3 * ((uint64_t) 1L << 21); $carry3 = $s3 >> 21; $s4 += $carry3; $s3 -= $carry3 << 21; - // carry4 = s4 >> 21; - // s5 += carry4; - // s4 -= carry4 * ((uint64_t) 1L << 21); $carry4 = $s4 >> 21; $s5 += $carry4; $s4 -= $carry4 << 21; - // carry5 = s5 >> 21; - // s6 += carry5; - // s5 -= carry5 * ((uint64_t) 1L << 21); $carry5 = $s5 >> 21; $s6 += $carry5; $s5 -= $carry5 << 21; - // carry6 = s6 >> 21; - // s7 += carry6; - // s6 -= carry6 * ((uint64_t) 1L << 21); $carry6 = $s6 >> 21; $s7 += $carry6; $s6 -= $carry6 << 21; - // carry7 = s7 >> 21; - // s8 += carry7; - // s7 -= carry7 * ((uint64_t) 1L << 21); $carry7 = $s7 >> 21; $s8 += $carry7; $s7 -= $carry7 << 21; - // carry8 = s8 >> 21; - // s9 += carry8; - // s8 -= carry8 * ((uint64_t) 1L << 21); $carry8 = $s8 >> 21; $s9 += $carry8; $s8 -= $carry8 << 21; - // carry9 = s9 >> 21; - // s10 += carry9; - // s9 -= carry9 * ((uint64_t) 1L << 21); $carry9 = $s9 >> 21; $s10 += $carry9; $s9 -= $carry9 << 21; - // carry10 = s10 >> 21; - // s11 += carry10; - // s10 -= carry10 * ((uint64_t) 1L << 21); $carry10 = $s10 >> 21; $s11 += $carry10; $s10 -= $carry10 << 21; - // carry11 = s11 >> 21; - // s12 += carry11; - // s11 -= carry11 * ((uint64_t) 1L << 21); $carry11 = $s11 >> 21; $s12 += $carry11; $s11 -= $carry11 << 21; - // s0 += s12 * 666643; - // s1 += s12 * 470296; - // s2 += s12 * 654183; - // s3 -= s12 * 997805; - // s4 += s12 * 136657; - // s5 -= s12 * 683901; $s0 += self::mul($s12, 666643, 20); $s1 += self::mul($s12, 470296, 19); $s2 += self::mul($s12, 654183, 20); @@ -3575,137 +3020,72 @@ public static function sc25519_mul( $s4 += self::mul($s12, 136657, 18); $s5 -= self::mul($s12, 683901, 20); - // carry0 = s0 >> 21; - // s1 += carry0; - // s0 -= carry0 * ((uint64_t) 1L << 21); $carry0 = $s0 >> 21; $s1 += $carry0; $s0 -= $carry0 << 21; - // carry1 = s1 >> 21; - // s2 += carry1; - // s1 -= carry1 * ((uint64_t) 1L << 21); $carry1 = $s1 >> 21; $s2 += $carry1; $s1 -= $carry1 << 21; - // carry2 = s2 >> 21; - // s3 += carry2; - // s2 -= carry2 * ((uint64_t) 1L << 21); $carry2 = $s2 >> 21; $s3 += $carry2; $s2 -= $carry2 << 21; - // carry3 = s3 >> 21; - // s4 += carry3; - // s3 -= carry3 * ((uint64_t) 1L << 21); $carry3 = $s3 >> 21; $s4 += $carry3; $s3 -= $carry3 << 21; - // carry4 = s4 >> 21; - // s5 += carry4; - // s4 -= carry4 * ((uint64_t) 1L << 21); $carry4 = $s4 >> 21; $s5 += $carry4; $s4 -= $carry4 << 21; - // carry5 = s5 >> 21; - // s6 += carry5; - // s5 -= carry5 * ((uint64_t) 1L << 21); $carry5 = $s5 >> 21; $s6 += $carry5; $s5 -= $carry5 << 21; - // carry6 = s6 >> 21; - // s7 += carry6; - // s6 -= carry6 * ((uint64_t) 1L << 21); $carry6 = $s6 >> 21; $s7 += $carry6; $s6 -= $carry6 << 21; - // carry7 = s7 >> 21; - // s8 += carry7; - // s7 -= carry7 * ((uint64_t) 1L << 21); $carry7 = $s7 >> 21; $s8 += $carry7; $s7 -= $carry7 << 21; - // carry8 = s8 >> 21; - // s9 += carry8; - // s8 -= carry8 * ((uint64_t) 1L << 21); $carry8 = $s8 >> 21; $s9 += $carry8; $s8 -= $carry8 << 21; - // carry9 = s9 >> 21; - // s10 += carry9; - // s9 -= carry9 * ((uint64_t) 1L << 21); $carry9 = $s9 >> 21; $s10 += $carry9; $s9 -= $carry9 << 21; - // carry10 = s10 >> 21; - // s11 += carry10; - // s10 -= carry10 * ((uint64_t) 1L << 21); $carry10 = $s10 >> 21; $s11 += $carry10; $s10 -= $carry10 << 21; $s = array_fill(0, 32, 0); - // s[0] = s0 >> 0; $s[0] = $s0 >> 0; - // s[1] = s0 >> 8; $s[1] = $s0 >> 8; - // s[2] = (s0 >> 16) | (s1 * ((uint64_t) 1 << 5)); $s[2] = ($s0 >> 16) | ($s1 << 5); - // s[3] = s1 >> 3; $s[3] = $s1 >> 3; - // s[4] = s1 >> 11; $s[4] = $s1 >> 11; - // s[5] = (s1 >> 19) | (s2 * ((uint64_t) 1 << 2)); $s[5] = ($s1 >> 19) | ($s2 << 2); - // s[6] = s2 >> 6; $s[6] = $s2 >> 6; - // s[7] = (s2 >> 14) | (s3 * ((uint64_t) 1 << 7)); $s[7] = ($s2 >> 14) | ($s3 << 7); - // s[8] = s3 >> 1; $s[8] = $s3 >> 1; - // s[9] = s3 >> 9; $s[9] = $s3 >> 9; - // s[10] = (s3 >> 17) | (s4 * ((uint64_t) 1 << 4)); $s[10] = ($s3 >> 17) | ($s4 << 4); - // s[11] = s4 >> 4; $s[11] = $s4 >> 4; - // s[12] = s4 >> 12; $s[12] = $s4 >> 12; - // s[13] = (s4 >> 20) | (s5 * ((uint64_t) 1 << 1)); $s[13] = ($s4 >> 20) | ($s5 << 1); - // s[14] = s5 >> 7; $s[14] = $s5 >> 7; - // s[15] = (s5 >> 15) | (s6 * ((uint64_t) 1 << 6)); $s[15] = ($s5 >> 15) | ($s6 << 6); - // s[16] = s6 >> 2; $s[16] = $s6 >> 2; - // s[17] = s6 >> 10; $s[17] = $s6 >> 10; - // s[18] = (s6 >> 18) | (s7 * ((uint64_t) 1 << 3)); $s[18] = ($s6 >> 18) | ($s7 << 3); - // s[19] = s7 >> 5; $s[19] = $s7 >> 5; - // s[20] = s7 >> 13; $s[20] = $s7 >> 13; - // s[21] = s8 >> 0; $s[21] = $s8 >> 0; - // s[22] = s8 >> 8; $s[22] = $s8 >> 8; - // s[23] = (s8 >> 16) | (s9 * ((uint64_t) 1 << 5)); $s[23] = ($s8 >> 16) | ($s9 << 5); - // s[24] = s9 >> 3; $s[24] = $s9 >> 3; - // s[25] = s9 >> 11; $s[25] = $s9 >> 11; - // s[26] = (s9 >> 19) | (s10 * ((uint64_t) 1 << 2)); $s[26] = ($s9 >> 19) | ($s10 << 2); - // s[27] = s10 >> 6; $s[27] = $s10 >> 6; - // s[28] = (s10 >> 14) | (s11 * ((uint64_t) 1 << 7)); $s[28] = ($s10 >> 14) | ($s11 << 7); - // s[29] = s11 >> 1; $s[29] = $s11 >> 1; - // s[30] = s11 >> 9; $s[30] = $s11 >> 9; - // s[31] = s11 >> 17; $s[31] = $s11 >> 17; return self::intArrayToString($s); } @@ -3790,21 +3170,6 @@ public static function sc25519_invert( return self::sc25519_sqmul($recip, 8, $_11101011); } - /** - * @param string $s - * @return string - */ - public static function clamp( - #[SensitiveParameter] - string $s - ): string { - $s_ = self::stringToIntArray($s); - $s_[0] &= 248; - $s_[31] |= 64; - $s_[31] &= 128; - return self::intArrayToString($s_); - } - /** * Ensure limbs are less than 28 bits long to prevent float promotion. * @@ -3818,7 +3183,7 @@ public static function fe_normalize( ): ParagonIE_Sodium_Core_Curve25519_Fe { $x = (PHP_INT_SIZE << 3) - 1; // 31 or 63 - $g = self::fe_copy($f); + $g = clone $f; for ($i = 0; $i < 10; ++$i) { $mask = -(($g[$i] >> $x) & 1); diff --git a/src/Core/Curve25519/Fe.php b/src/Core/Curve25519/Fe.php index 75fc2243..d8127bc4 100644 --- a/src/Core/Curve25519/Fe.php +++ b/src/Core/Curve25519/Fe.php @@ -17,7 +17,7 @@ class ParagonIE_Sodium_Core_Curve25519_Fe implements ArrayAccess /** * @var array */ - protected array $container = array(); + protected array $container = []; /** * @var int @@ -63,7 +63,7 @@ public static function fromArray(array $array, bool $save_indexes = false): self * @return void */ #[ReturnTypeWillChange] - public function offsetSet($offset, $value) + public function offsetSet($offset, $value): void { if (is_null($offset)) { $this->container[] = $value; @@ -79,7 +79,7 @@ public function offsetSet($offset, $value) * @return bool */ #[ReturnTypeWillChange] - public function offsetExists($offset) + public function offsetExists($offset): bool { return isset($this->container[$offset]); } @@ -91,7 +91,7 @@ public function offsetExists($offset) * @return void */ #[ReturnTypeWillChange] - public function offsetUnset($offset) + public function offsetUnset($offset): void { unset($this->container[$offset]); } diff --git a/src/Core/Ed25519.php b/src/Core/Ed25519.php index fba4948d..f93fec68 100644 --- a/src/Core/Ed25519.php +++ b/src/Core/Ed25519.php @@ -53,8 +53,6 @@ public static function seed_keypair( if (self::strlen($seed) !== self::SEED_BYTES) { throw new RangeException('crypto_sign keypair seed must be 32 bytes long'); } - - /** @var string $pk */ $pk = self::publickey_from_secretkey($seed); $sk = $seed . $pk; return $sk; @@ -134,26 +132,16 @@ public static function pk_to_curve25519( if (!self::fe_isnonzero($p1->X)) { throw new SodiumException('Unexpected zero result'); } - - # fe_1(one_minus_y); - # fe_sub(one_minus_y, one_minus_y, A.Y); - # fe_invert(one_minus_y, one_minus_y); $one_minux_y = self::fe_invert( self::fe_sub( self::fe_1(), $A->Y ) ); - - # fe_1(x); - # fe_add(x, x, A.Y); - # fe_mul(x, x, one_minus_y); $x = self::fe_mul( self::fe_add(self::fe_1(), $A->Y), $one_minux_y ); - - # fe_tobytes(curve25519_pk, x); return self::fe_tobytes($x); } @@ -190,7 +178,6 @@ public static function sign( #[SensitiveParameter] string $sk ): string { - /** @var string $signature */ $signature = self::sign_detached($message, $sk); return $signature . $message; } @@ -208,7 +195,6 @@ public static function sign_open( string $message, string $pk ): string { - /** @var string $signature */ $signature = self::substr($message, 0, 64); /** @var string $message */ @@ -234,54 +220,36 @@ public static function sign_detached( #[SensitiveParameter] string $sk ): string { - # crypto_hash_sha512(az, sk, 32); $az = hash('sha512', self::substr($sk, 0, 32), true); - # az[0] &= 248; - # az[31] &= 63; - # az[31] |= 64; $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); - # crypto_hash_sha512_init(&hs); - # crypto_hash_sha512_update(&hs, az + 32, 32); - # crypto_hash_sha512_update(&hs, m, mlen); - # crypto_hash_sha512_final(&hs, nonce); $hs = hash_init('sha512'); hash_update($hs, self::substr($az, 32, 32)); hash_update($hs, $message); $nonceHash = hash_final($hs, true); - # memmove(sig + 32, sk + 32, 32); $pk = self::substr($sk, 32, 32); - # sc_reduce(nonce); - # ge_scalarmult_base(&R, nonce); - # ge_p3_tobytes(sig, &R); $nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32); $sig = self::ge_p3_tobytes( self::ge_scalarmult_base($nonce) ); - # crypto_hash_sha512_init(&hs); - # crypto_hash_sha512_update(&hs, sig, 64); - # crypto_hash_sha512_update(&hs, m, mlen); - # crypto_hash_sha512_final(&hs, hram); $hs = hash_init('sha512'); hash_update($hs, self::substr($sig, 0, 32)); hash_update($hs, self::substr($pk, 0, 32)); hash_update($hs, $message); $hramHash = hash_final($hs, true); - # sc_reduce(hram); - # sc_muladd(sig + 32, hram, az, nonce); $hram = self::sc_reduce($hramHash); $sigAfter = self::sc_muladd($hram, $az, $nonce); $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); try { ParagonIE_Sodium_Compat::memzero($az); - } catch (SodiumException $ex) { + } catch (SodiumException) { $az = null; } return $sig; @@ -322,16 +290,14 @@ public static function verify_detached( throw new SodiumException('All zero public key'); } - /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ + /* The original value of ParagonIE_Sodium_Compat::$fastMult */ $orig = ParagonIE_Sodium_Compat::$fastMult; // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification. ParagonIE_Sodium_Compat::$fastMult = true; - /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */ $A = self::ge_frombytes_negate_vartime($pk); - /** @var string $hDigest */ $hDigest = hash( 'sha512', self::substr($sig, 0, 32) . @@ -339,18 +305,12 @@ public static function verify_detached( $message, true ); - - /** @var string $h */ $h = self::sc_reduce($hDigest) . self::substr($hDigest, 32); - - /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */ $R = self::ge_double_scalarmult_vartime( $h, $A, self::substr($sig, 32) ); - - /** @var string $rcheck */ $rcheck = self::ge_tobytes($R); // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. @@ -393,7 +353,6 @@ public static function check_S_lt_L(string $S): bool (($x ^ $L[$i]) - 1) >> 8 ); } while ($i !== 0); - return $c === 0; } @@ -492,9 +451,7 @@ public static function small_order(string $R): bool 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff ) ); - /** @var int $countBlocklist */ $countBlocklist = count($blocklist); - for ($i = 0; $i < $countBlocklist; ++$i) { $c = 0; for ($j = 0; $j < 32; ++$j) { diff --git a/src/Core/HChaCha20.php b/src/Core/HChaCha20.php index 457d20c1..54eb6b2c 100644 --- a/src/Core/HChaCha20.php +++ b/src/Core/HChaCha20.php @@ -76,29 +76,15 @@ protected static function hChaCha20Bytes(array $ctx): string $x15 = (int) $ctx[15]; for ($i = 0; $i < 10; ++$i) { - # QUARTERROUND( x0, x4, x8, x12) - list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); + [$x0, $x4, $x8, $x12] = self::quarterRound($x0, $x4, $x8, $x12); + [$x1, $x5, $x9, $x13] = self::quarterRound($x1, $x5, $x9, $x13); + [$x2, $x6, $x10, $x14] = self::quarterRound($x2, $x6, $x10, $x14); + [$x3, $x7, $x11, $x15] = self::quarterRound($x3, $x7, $x11, $x15); - # QUARTERROUND( x1, x5, x9, x13) - list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); - - # QUARTERROUND( x2, x6, x10, x14) - list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); - - # QUARTERROUND( x3, x7, x11, x15) - list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); - - # QUARTERROUND( x0, x5, x10, x15) - list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); - - # QUARTERROUND( x1, x6, x11, x12) - list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); - - # QUARTERROUND( x2, x7, x8, x13) - list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); - - # QUARTERROUND( x3, x4, x9, x14) - list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); + [$x0, $x5, $x10, $x15] = self::quarterRound($x0, $x5, $x10, $x15); + [$x1, $x6, $x11, $x12] = self::quarterRound($x1, $x6, $x11, $x12); + [$x2, $x7, $x8, $x13] = self::quarterRound($x2, $x7, $x8, $x13); + [$x3, $x4, $x9, $x14] = self::quarterRound($x3, $x4, $x9, $x14); } return self::store32_le(($x0 & 0xffffffff)) . diff --git a/src/Core/Poly1305/State.php b/src/Core/Poly1305/State.php index 15c1de3f..f6f153a2 100644 --- a/src/Core/Poly1305/State.php +++ b/src/Core/Poly1305/State.php @@ -14,20 +14,11 @@ class ParagonIE_Sodium_Core_Poly1305_State extends ParagonIE_Sodium_Core_Util * @var array */ protected array $buffer = array(); - - /** - * @var bool - */ protected bool $final = false; - /** * @var array */ public array $h; - - /** - * @var int - */ protected int $leftover = 0; /** diff --git a/src/Core/Ristretto255.php b/src/Core/Ristretto255.php index 8c0ac5c4..364cfea4 100644 --- a/src/Core/Ristretto255.php +++ b/src/Core/Ristretto255.php @@ -173,8 +173,6 @@ public static function ristretto255_frombytes( $v = self::fe_sub($v, $u2u2); /* v = -(d*u1^2)-u2^2 */ $v_u2u2 = self::fe_mul($v, $u2u2); /* v_u2u2 = v*u2^2 */ - // fe25519_1(one); - // notsquare = ristretto255_sqrt_ratio_m1(inv_sqrt, one, v_u2u2); $one = self::fe_1(); $result = self::ristretto255_sqrt_ratio_m1($one, $v_u2u2); $inv_sqrt = $result['x']; @@ -216,8 +214,6 @@ public static function ristretto255_p3_tobytes( $u1_u2u2 = self::fe_mul(self::fe_sq($u2), $u1); /* u1_u2u2 = u1*u2^2 */ $one = self::fe_1(); - // fe25519_1(one); - // (void) ristretto255_sqrt_ratio_m1(inv_sqrt, one, u1_u2u2); $result = self::ristretto255_sqrt_ratio_m1($one, $u1_u2u2); $inv_sqrt = $result['x']; @@ -232,9 +228,9 @@ public static function ristretto255_p3_tobytes( $t_z_inv = self::fe_mul($h->T, $z_inv); /* t_z_inv = T*z_inv */ $rotate = self::fe_isnegative($t_z_inv); - $x_ = self::fe_copy($h->X); - $y_ = self::fe_copy($h->Y); - $den_inv = self::fe_copy($den2); + $x_ = clone $h->X; + $y_ = clone $h->Y; + $den_inv = clone $den2; $x_ = self::fe_cmov($x_, $iy, $rotate); $y_ = self::fe_cmov($y_, $ix, $rotate); @@ -244,10 +240,6 @@ public static function ristretto255_p3_tobytes( $y_ = self::fe_cneg($y_, self::fe_isnegative($x_z_inv)); - // fe25519_sub(s_, h->Z, y_); - // fe25519_mul(s_, den_inv, s_); - // fe25519_abs(s_, s_); - // fe25519_tobytes(s, s_); return self::fe_tobytes( self::fe_abs( self::fe_mul( @@ -299,10 +291,6 @@ public static function ristretto255_elligator( $s = self::fe_cmov($s, $s_prime, $wasnt_square); $c = self::fe_cmov($c, $r, $wasnt_square); - // fe25519_sub(n, r, one); /* n = r-1 */ - // fe25519_mul(n, n, c); /* n = c*(r-1) */ - // fe25519_mul(n, n, ed25519_sqdmone); /* n = c*(r-1)*(d-1)^2 */ - // fe25519_sub(n, n, v); /* n = c*(r-1)*(d-1)^2-v */ $n = self::fe_sub( self::fe_mul( self::fe_mul( @@ -344,25 +332,16 @@ public static function ristretto255_from_hash( if (self::strlen($h) !== 64) { throw new SodiumException('Hash must be 64 bytes'); } - //fe25519_frombytes(r0, h); - //fe25519_frombytes(r1, h + 32); $r0 = self::fe_frombytes(self::substr($h, 0, 32)); $r1 = self::fe_frombytes(self::substr($h, 32, 32)); - //ristretto255_elligator(&p0, r0); - //ristretto255_elligator(&p1, r1); $p0 = self::ristretto255_elligator($r0); $p1 = self::ristretto255_elligator($r1); - //ge25519_p3_to_cached(&p1_cached, &p1); - //ge25519_add_cached(&p_p1p1, &p0, &p1_cached); $p_p1p1 = self::ge_add( $p0, self::ge_p3_to_cached($p1) ); - - //ge25519_p1p1_to_p3(&p, &p_p1p1); - //ristretto255_p3_tobytes(s, &p); return self::ristretto255_p3_tobytes( self::ge_p1p1_to_p3($p_p1p1) ); diff --git a/src/Core/Salsa20.php b/src/Core/Salsa20.php index a1ddd920..1bd47eca 100644 --- a/src/Core/Salsa20.php +++ b/src/Core/Salsa20.php @@ -175,7 +175,7 @@ public static function salsa20( } try { ParagonIE_Sodium_Compat::memzero($kcopy); - } catch (SodiumException $ex) { + } catch (SodiumException) { $kcopy = null; } return $c; @@ -237,7 +237,7 @@ public static function salsa20_xor_ic( try { ParagonIE_Sodium_Compat::memzero($block); ParagonIE_Sodium_Compat::memzero($kcopy); - } catch (SodiumException $ex) { + } catch (SodiumException) { $block = null; $kcopy = null; } diff --git a/src/Core/SipHash.php b/src/Core/SipHash.php index ec869e3a..f52ee5e2 100644 --- a/src/Core/SipHash.php +++ b/src/Core/SipHash.php @@ -300,10 +300,7 @@ public static function sipHash24( # SIPROUND; # SIPROUND; # SIPROUND; - $v = self::sipRound($v); - $v = self::sipRound($v); - $v = self::sipRound($v); - $v = self::sipRound($v); + $v = self::sipRound(self::sipRound(self::sipRound(self::sipRound($v)))); # b = v0 ^ v1 ^ v2 ^ v3; # STORE64_LE( out, b ); diff --git a/src/Core/Util.php b/src/Core/Util.php index cc71d003..26811713 100644 --- a/src/Core/Util.php +++ b/src/Core/Util.php @@ -345,7 +345,6 @@ public static function load64_le( * @param string $left * @param string $right * @return int - * @throws SodiumException * @throws TypeError */ public static function memcmp( @@ -588,7 +587,6 @@ public static function substr( * @param string $a * @param string $b * @return bool - * @throws SodiumException * @throws TypeError */ public static function verify_16( diff --git a/src/Core/X25519.php b/src/Core/X25519.php index 8090d43d..6c854f14 100644 --- a/src/Core/X25519.php +++ b/src/Core/X25519.php @@ -163,115 +163,54 @@ public static function crypto_scalarmult_curve25519_ref10( #[SensitiveParameter] string $p ): string { - # for (i = 0;i < 32;++i) e[i] = n[i]; $e = $n; - # e[0] &= 248; $e[0] = self::intToChr( self::chrToInt($e[0]) & 248 ); - # e[31] &= 127; - # e[31] |= 64; $e[31] = self::intToChr( (self::chrToInt($e[31]) & 127) | 64 ); - # fe_frombytes(x1,p); $x1 = self::fe_frombytes($p); - # fe_1(x2); $x2 = self::fe_1(); - # fe_0(z2); $z2 = self::fe_0(); - # fe_copy(x3,x1); - $x3 = self::fe_copy($x1); - # fe_1(z3); + $x3 = clone $x1; $z3 = self::fe_1(); - # swap = 0; - /** @var int $swap */ $swap = 0; - - # for (pos = 254;pos >= 0;--pos) { for ($pos = 254; $pos >= 0; --$pos) { # b = e[pos / 8] >> (pos & 7); - /** @var int $b */ $b = self::chrToInt( $e[(int) floor($pos / 8)] ) >> ($pos & 7); - # b &= 1; $b &= 1; - # swap ^= b; $swap ^= $b; - # fe_cswap(x2,x3,swap); self::fe_cswap($x2, $x3, $swap); - # fe_cswap(z2,z3,swap); self::fe_cswap($z2, $z3, $swap); - # swap = b; $swap = $b; - # fe_sub(tmp0,x3,z3); $tmp0 = self::fe_sub($x3, $z3); - # fe_sub(tmp1,x2,z2); $tmp1 = self::fe_sub($x2, $z2); - - # fe_add(x2,x2,z2); $x2 = self::fe_add($x2, $z2); - - # fe_add(z2,x3,z3); $z2 = self::fe_add($x3, $z3); - - # fe_mul(z3,tmp0,x2); $z3 = self::fe_mul($tmp0, $x2); - - # fe_mul(z2,z2,tmp1); $z2 = self::fe_mul($z2, $tmp1); - - # fe_sq(tmp0,tmp1); $tmp0 = self::fe_sq($tmp1); - - # fe_sq(tmp1,x2); $tmp1 = self::fe_sq($x2); - - # fe_add(x3,z3,z2); $x3 = self::fe_add($z3, $z2); - - # fe_sub(z2,z3,z2); $z2 = self::fe_sub($z3, $z2); - - # fe_mul(x2,tmp1,tmp0); $x2 = self::fe_mul($tmp1, $tmp0); - - # fe_sub(tmp1,tmp1,tmp0); $tmp1 = self::fe_sub($tmp1, $tmp0); - - # fe_sq(z2,z2); $z2 = self::fe_sq($z2); - - # fe_mul121666(z3,tmp1); $z3 = self::fe_mul121666($tmp1); - - # fe_sq(x3,x3); $x3 = self::fe_sq($x3); - - # fe_add(tmp0,tmp0,z3); $tmp0 = self::fe_add($tmp0, $z3); - - # fe_mul(z3,x1,z2); $z3 = self::fe_mul($x1, $z2); - - # fe_mul(z2,tmp1,tmp0); $z2 = self::fe_mul($tmp1, $tmp0); } - - # fe_cswap(x2,x3,swap); self::fe_cswap($x2, $x3, $swap); - - # fe_cswap(z2,z3,swap); self::fe_cswap($z2, $z3, $swap); - # fe_invert(z2,z2); $z2 = self::fe_invert($z2); - - # fe_mul(x2,x2,z2); $x2 = self::fe_mul($x2, $z2); - # fe_tobytes(q,x2); return self::fe_tobytes($x2); } @@ -304,16 +243,10 @@ public static function crypto_scalarmult_curve25519_ref10_base( #[SensitiveParameter] string $n ): string { - # for (i = 0;i < 32;++i) e[i] = n[i]; $e = $n; - - # e[0] &= 248; $e[0] = self::intToChr( self::chrToInt($e[0]) & 248 ); - - # e[31] &= 127; - # e[31] |= 64; $e[31] = self::intToChr( (self::chrToInt($e[31]) & 127) | 64 ); diff --git a/src/File.php b/src/File.php index 239a8cc9..01943452 100644 --- a/src/File.php +++ b/src/File.php @@ -116,7 +116,7 @@ public static function box_open( try { ParagonIE_Sodium_Compat::memzero($nonce); ParagonIE_Sodium_Compat::memzero($ephKeypair); - } catch (SodiumException $ex) { + } catch (SodiumException) { if (isset($ephKeypair)) { unset($ephKeypair); } @@ -199,7 +199,7 @@ public static function box_seal( try { ParagonIE_Sodium_Compat::memzero($nonce); ParagonIE_Sodium_Compat::memzero($ephKeypair); - } catch (SodiumException $ex) { + } catch (SodiumException) { /** @psalm-suppress PossiblyUndefinedVariable */ unset($ephKeypair); } @@ -277,7 +277,7 @@ public static function box_seal_open( try { ParagonIE_Sodium_Compat::memzero($nonce); ParagonIE_Sodium_Compat::memzero($ephKeypair); - } catch (SodiumException $ex) { + } catch (SodiumException) { if (isset($ephKeypair)) { unset($ephKeypair); } @@ -450,7 +450,7 @@ public static function secretbox_open( fclose($ofp); try { ParagonIE_Sodium_Compat::memzero($key); - } catch (SodiumException $ex) { + } catch (SodiumException) { /** @psalm-suppress PossiblyUndefinedVariable */ unset($key); } @@ -532,7 +532,7 @@ public static function sign( try { ParagonIE_Sodium_Compat::memzero($az); - } catch (SodiumException $ex) { + } catch (SodiumException) { $az = null; } fclose($fp); @@ -786,7 +786,7 @@ protected static function secretbox_encrypt( try { ParagonIE_Sodium_Compat::memzero($block0); ParagonIE_Sodium_Compat::memzero($subkey); - } catch (SodiumException $ex) { + } catch (SodiumException) { $block0 = null; $subkey = null; } diff --git a/tests/compat/FileCompatTest.php b/tests/compat/FileCompatTest.php index 384f1a9b..606caf79 100644 --- a/tests/compat/FileCompatTest.php +++ b/tests/compat/FileCompatTest.php @@ -1,11 +1,12 @@ markTestSkipped('PHP < 7.2.0; skipping PHP 7.2 File compatibility test suite.'); @@ -16,7 +17,7 @@ public function before() /** * Ensure */ - public function testCompat() + public function testCompat(): void { $keypair = hex2bin( '5f2e1b83a832f890fc463fb3ff1cdf672e474eb07d26944d4c4bf5d385f49835' . diff --git a/tests/compat/NamespacedTest.php b/tests/compat/NamespacedTest.php index ef70c5e1..a9afd484 100644 --- a/tests/compat/NamespacedTest.php +++ b/tests/compat/NamespacedTest.php @@ -1,14 +1,16 @@ markTestSkipped('PHP < 5.3.0; skipping PHP 5.3+ compatibility test suite.'); @@ -19,7 +21,7 @@ public function before() /** * @covers ParagonIE_Sodium_Compat::crypto_secretbox() */ - public function testCryptoSecretBox() + public function testCryptoSecretBox(): void { $key = str_repeat("\x80", 32); $nonce = str_repeat("\x00", 24); diff --git a/tests/compat/PHP72Test.php b/tests/compat/PHP72Test.php index 940e5c83..098f9f51 100644 --- a/tests/compat/PHP72Test.php +++ b/tests/compat/PHP72Test.php @@ -1,14 +1,15 @@ markTestSkipped('PHP < 7.2.0; skipping PHP 7.2 compatibility test suite.'); @@ -19,7 +20,7 @@ public function before() /** * @throws SodiumException */ - public function testAdd() + public function testAdd(): void { $a = "\x12\x34\x56\x78"; $b = "\x01\x00\x00\x00"; @@ -41,7 +42,7 @@ public function testAdd() * @ref https://github.com/paragonie/sodium_compat/issues/125 * @throws SodiumException */ - public function testAeadXChaCha20EmptyAad() + public function testAeadXChaCha20EmptyAad(): void { $key = sodium_crypto_aead_xchacha20poly1305_ietf_keygen(); $nonce = random_bytes(24); @@ -76,7 +77,7 @@ public function testAeadXChaCha20EmptyAad() /** * @covers ParagonIE_Sodium_Core_Util::compare() */ - public function testCompare() + public function testCompare(): void { $a = pack('H*', '589a84d7ec2db8f982841cedca674ec1'); $b = $a; @@ -101,7 +102,7 @@ public function testCompare() /** * @covers ParagonIE_Sodium_Core_Util::bin2hex() */ - public function testBin2hex() + public function testBin2hex(): void { $str = random_bytes(random_int(1, 63)); $this->assertSame( @@ -113,7 +114,7 @@ public function testBin2hex() /** * @covers ParagonIE_Sodium_Core_Util::hex2bin() */ - public function testHex2bin() + public function testHex2bin(): void { $str = bin2hex(random_bytes(random_int(1, 63))); $this->assertSame( @@ -125,7 +126,7 @@ public function testHex2bin() /** * */ - public function testAeadChapoly() + public function testAeadChapoly(): void { $message = str_repeat("\x00", 128); $key = str_repeat("\x00", 32); @@ -189,7 +190,7 @@ public function testAeadChapoly() /** * */ - public function testAeadChapolyIetf() + public function testAeadChapolyIetf(): void { $message = str_repeat("\x00", 128); $key = str_repeat("\x00", 32); @@ -253,7 +254,7 @@ public function testAeadChapolyIetf() /** * */ - public function testCryptoAuth() + public function testCryptoAuth(): void { $message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; $key = random_bytes(32); @@ -272,7 +273,7 @@ public function testCryptoAuth() * @covers ParagonIE_Sodium_Compat::crypto_box() * @covers ParagonIE_Sodium_Compat::crypto_box_open() */ - public function testCryptoBox() + public function testCryptoBox(): void { $nonce = str_repeat("\x00", 24); $message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; @@ -331,7 +332,7 @@ public function testCryptoBox() ); } - public function testCryptoBoxSeal() + public function testCryptoBoxSeal(): void { $msg = ParagonIE_Sodium_Core_Util::hex2bin( '7375f4094f1151640bd853cb13dbc1a0ee9e13b0287a89d34fa2f6732be9de13f88457553d'. @@ -430,7 +431,7 @@ public function testCryptoBoxSeal() /** * @covers ParagonIE_Sodium_Compat::crypto_generichash() */ - public function testCryptoGenerichash() + public function testCryptoGenerichash(): void { $this->assertSame( bin2hex(sodium_crypto_generichash('apple')), @@ -462,7 +463,7 @@ public function testCryptoGenerichash() * @covers ParagonIE_Sodium_Compat::crypto_generichash_update() * @covers ParagonIE_Sodium_Compat::crypto_generichash_final() */ - public function testCryptoGenerichashStream() + public function testCryptoGenerichashStream(): void { $key = "\x1c" . str_repeat("\x80", 30) . "\xaf"; $ctx = sodium_crypto_generichash_init($key); @@ -486,7 +487,7 @@ public function testCryptoGenerichashStream() /** * @covers ParagonIE_Sodium_Compat::crypto_sign_seed_keypair() */ - public function testSignKeypair() + public function testSignKeypair(): void { $seed = random_bytes(32); $kp = sodium_crypto_sign_seed_keypair($seed); @@ -534,7 +535,7 @@ public function testSignKeypair() } } - public function testSignKeypair2() + public function testSignKeypair2(): void { $keypair = sodium_crypto_sign_keypair(); $secret = sodium_crypto_sign_secretkey($keypair); @@ -555,7 +556,7 @@ public function testSignKeypair2() * @covers ParagonIE_Sodium_Compat::crypto_sign_detached() * @covers ParagonIE_Sodium_Compat::crypto_sign_verify_detached() */ - public function testCryptoSign() + public function testCryptoSign(): void { $keypair = ParagonIE_Sodium_Core_Util::hex2bin( 'fcdf31aae72e280cc760186d83e41be216fe1f2c7407dd393ad3a45a2fa501a4' . @@ -635,7 +636,7 @@ public function testCryptoSign() /** * @covers ParagonIE_Sodium_Compat::crypto_secretbox() */ - public function testCryptoSecretBox() + public function testCryptoSecretBox(): void { $key = str_repeat("\x80", 32); $nonce = str_repeat("\x00", 24); @@ -705,7 +706,7 @@ public function testCryptoSecretBox() /** * @covers ParagonIE_Sodium_Compat::crypto_scalarmult_base() */ - public function testCryptoScalarmultBase() + public function testCryptoScalarmultBase(): void { $keypair = sodium_crypto_box_keypair(); $secret = sodium_crypto_box_secretkey($keypair); @@ -719,7 +720,7 @@ public function testCryptoScalarmultBase() /** * @covers ParagonIE_Sodium_Compat::crypto_scalarmult() */ - public function testCryptoScalarmult() + public function testCryptoScalarmult(): void { $alice_box_kp = sodium_crypto_box_keypair(); $alice_box_secretkey = sodium_crypto_box_secretkey($alice_box_kp); @@ -744,7 +745,7 @@ public function testCryptoScalarmult() * @covers ParagonIE_Sodium_Compat::crypto_box_secretkey() * @covers ParagonIE_Sodium_Compat::crypto_box_publickey() */ - public function testCryptoBoxKeypairs() + public function testCryptoBoxKeypairs(): void { $keypair = sodium_crypto_box_keypair(); $secret = sodium_crypto_box_secretkey($keypair); @@ -763,7 +764,7 @@ public function testCryptoBoxKeypairs() /** * @covers ParagonIE_Sodium_Compat::crypto_stream() */ - public function testCryptoStream() + public function testCryptoStream(): void { $key = str_repeat("\x80", 32); $nonce = str_repeat("\x00", 24); @@ -788,7 +789,7 @@ public function testCryptoStream() /** * @covers ParagonIE_Sodium_Compat::crypto_stream_xor() */ - public function testCryptoStreamXor() + public function testCryptoStreamXor(): void { $key = str_repeat("\x80", 32); $nonce = str_repeat("\x00", 24); @@ -817,7 +818,7 @@ public function testCryptoStreamXor() /** * @covers ParagonIE_Sodium_Compat::crypto_kx() */ - public function testCryptoKx() + public function testCryptoKx(): void { if (!is_callable('sodium_crypto_kx')) { $this->markTestSkipped('sodium_crypto_kx not defined'); @@ -854,7 +855,7 @@ public function testCryptoKx() * @throws SodiumException * @throws TypeError */ - public function testCryptoPwhash() + public function testCryptoPwhash(): void { if (!\extension_loaded('sodium')) { $this->markTestSkipped('Libsodium not loaded'); @@ -891,7 +892,7 @@ public function testCryptoPwhash() /** * @covers ParagonIE_Sodium_Compat::crypto_kdf_derive_from_key() */ - public function testKdf() + public function testKdf(): void { $key = ParagonIE_Sodium_Compat::crypto_kdf_keygen(); $subkey_id = random_int(1, PHP_INT_MAX); @@ -908,7 +909,7 @@ public function testKdf() /** * @throws SodiumException */ - public function testPwhashNeedsRehash() + public function testPwhashNeedsRehash(): void { if (!\extension_loaded('sodium')) { $this->markTestSkipped('Libsodium not loaded'); @@ -938,7 +939,7 @@ public function testPwhashNeedsRehash() /** * @throws SodiumException */ - public function testCryptoShorthash() + public function testCryptoShorthash(): void { $message = str_repeat("\x00", 8); $key = str_repeat("\x00", 16); @@ -971,7 +972,7 @@ public function testCryptoShorthash() * @throws SodiumException * @throws Exception */ - public function testSecretStreamStates() + public function testSecretStreamStates(): void { $key = str_repeat("A", 32); list($stateA, $header) = sodium_crypto_secretstream_xchacha20poly1305_init_push($key); @@ -1001,7 +1002,7 @@ public function testSecretStreamStates() $this->assertEquals(bin2hex($stateC), bin2hex($stateD)); } - public function testSecretStream() + public function testSecretStream(): void { $key = str_repeat("A", 32); // list($state, $header) = ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_init_push($key); @@ -1044,7 +1045,7 @@ public function testSecretStream() * @throws SodiumException * @throws Exception */ - public function testSodiumPad() + public function testSodiumPad(): void { for ($i = 0; $i < 100; ++$i) { $block = random_int(16, 256); @@ -1069,7 +1070,7 @@ public function testSodiumPad() /** * @throws SodiumException */ - public function testKeyExchange() + public function testKeyExchange(): void { $alice = ParagonIE_Sodium_Compat::crypto_kx_keypair(); $alice_pk = ParagonIE_Sodium_Compat::crypto_kx_publickey($alice); diff --git a/tests/compat/PHP84Test.php b/tests/compat/PHP84Test.php index a1b58759..04898927 100644 --- a/tests/compat/PHP84Test.php +++ b/tests/compat/PHP84Test.php @@ -1,11 +1,12 @@ markTestSkipped('PHP < 8.4.0; skipping PHP 8.4 compatibility test suite.'); @@ -13,7 +14,7 @@ public function before() ParagonIE_Sodium_Compat::$disableFallbackForUnitTests = true; } - public function testAegis128l() + public function testAegis128l(): void { $msg = ParagonIE_Sodium_Compat::randombytes_buf(ParagonIE_Sodium_Compat::randombytes_uniform(999) + 1); $nonce = ParagonIE_Sodium_Compat::randombytes_buf(ParagonIE_Sodium_Compat::CRYPTO_AEAD_AEGIS128L_NPUBBYTES); @@ -28,7 +29,7 @@ public function testAegis128l() $this->assertSame($msg, $msg2b); } - public function testAegis256() + public function testAegis256(): void { $msg = ParagonIE_Sodium_Compat::randombytes_buf(ParagonIE_Sodium_Compat::randombytes_uniform(999) + 1); $nonce = ParagonIE_Sodium_Compat::randombytes_buf(ParagonIE_Sodium_Compat::CRYPTO_AEAD_AEGIS256_NPUBBYTES); diff --git a/tests/compat/PedanticTest.php b/tests/compat/PedanticTest.php index 37872f8b..5ca757f2 100644 --- a/tests/compat/PedanticTest.php +++ b/tests/compat/PedanticTest.php @@ -1,4 +1,5 @@ markTestSkipped('Libsodium is not installed; skipping the compatibility test suite.'); @@ -37,7 +38,7 @@ public function before() /** * @after */ - public function after() + public function after(): void { ParagonIE_Sodium_Compat::$fastMult = $this->oldFastMult; } @@ -45,7 +46,7 @@ public function after() /** * @return array */ - public function getInteresting32BitInts() + public function getInteresting32BitInts(): array { return array( 0x00000000, 0x01234567, @@ -72,7 +73,7 @@ public function getInteresting32BitInts() * @return array * @throws TypeError */ - public function getInteresting16ByteStrings() + public function getInteresting16ByteStrings(): array { $strings = array(); foreach ($this->getInteresting32BitInts() as $i => $int) { @@ -86,7 +87,7 @@ public function getInteresting16ByteStrings() * @return array * @throws TypeError */ - public function getInteresting32ByteStrings() + public function getInteresting32ByteStrings(): array { $strings = array(); foreach ($this->getInteresting32BitInts() as $i => $int) { @@ -156,7 +157,7 @@ public function getInterestingStringsVaryingLength($min = 1, $max = self::DEFAUL * @covers ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_encrypt() * @covers ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_decrypt() */ - public function testCryptoAeadChapoly() + public function testCryptoAeadChapoly(): void { $keys = $this->getInteresting32ByteStrings(); $plaintexts = $this->getInterestingStringsVaryingLength(); @@ -187,7 +188,7 @@ public function testCryptoAeadChapoly() /** * @covers ParagonIE_Sodium_Compat::crypto_auth() */ - public function testCryptoAuth() + public function testCryptoAuth(): void { $keys = $this->getInteresting32ByteStrings(); $plaintexts = $this->getInterestingStringsVaryingLength(); @@ -212,7 +213,7 @@ public function testCryptoAuth() * @covers ParagonIE_Sodium_Compat::crypto_box() * @covers ParagonIE_Sodium_Compat::crypto_box_open() */ - public function testCryptoBox() + public function testCryptoBox(): void { $secretKeys = $this->getCryptoBoxKeys(); $publicKeys = array(); @@ -253,7 +254,7 @@ public function testCryptoBox() /** * @covers ParagonIE_Sodium_Compat::crypto_generichash() */ - public function testGenerichash() + public function testGenerichash(): void { $keys = $this->getInteresting32ByteStrings(); $plaintexts = $this->getInterestingStringsVaryingLength(); @@ -285,7 +286,7 @@ public function testGenerichash() * @covers ParagonIE_Sodium_Compat::crypto_secretbox() * @covers ParagonIE_Sodium_Compat::crypto_secretbox_open() */ - public function testCryptoSecretbox() + public function testCryptoSecretbox(): void { $keys = $this->getInteresting32ByteStrings(); $plaintexts = $this->getInterestingStringsVaryingLength(); @@ -311,7 +312,7 @@ public function testCryptoSecretbox() /** * @covers ParagonIE_Sodium_Compat::crypto_shorthash() */ - public function testShorthash() + public function testShorthash(): void { $keys = $this->getInteresting16ByteStrings(); $plaintexts = $this->getInterestingStringsVaryingLength(); @@ -333,7 +334,7 @@ public function testShorthash() * @covers ParagonIE_Sodium_Compat::crypto_sign_detached() * @covers ParagonIE_Sodium_Compat::crypto_sign_verify_detached() */ - public function testCryptoSign() + public function testCryptoSign(): void { $seeds = $this->getCryptoBoxKeys(); $secretKeys = array(); diff --git a/tests/compat/Ristretto255CompatTest.php b/tests/compat/Ristretto255CompatTest.php index fe384595..518fd303 100644 --- a/tests/compat/Ristretto255CompatTest.php +++ b/tests/compat/Ristretto255CompatTest.php @@ -1,15 +1,15 @@ markTestSkipped('ext/sodium is not installed; skipping the compatibility test suite.'); @@ -17,7 +17,7 @@ public function before() ParagonIE_Sodium_Compat::$disableFallbackForUnitTests = true; } - public function testRistrettoFromHash() + public function testRistrettoFromHash(): void { $seed = random_bytes(64); $fromhash_1 = ParagonIE_Sodium_Compat::ristretto255_from_hash($seed, true); @@ -28,7 +28,7 @@ public function testRistrettoFromHash() ); } - public function testRistrettoOps() + public function testRistrettoOps(): void { $p = sodium_crypto_core_ristretto255_random(); $q = sodium_crypto_core_ristretto255_random(); @@ -47,7 +47,7 @@ public function testRistrettoOps() /** * @throws SodiumException */ - public function testRistrettoScalarOps() + public function testRistrettoScalarOps(): void { $p = sodium_crypto_core_ristretto255_scalar_random(); $q = sodium_crypto_core_ristretto255_scalar_random(); @@ -97,7 +97,7 @@ public function testRistrettoScalarOps() * * @throws SodiumException */ - public function testExchange() + public function testExchange(): void { $x = random_bytes(ParagonIE_Sodium_Compat::CRYPTO_CORE_RISTRETTO255_HASHBYTES); $px1 = sodium_crypto_core_ristretto255_from_hash($x); @@ -165,7 +165,7 @@ public function brokenPHP81TestProvider() * @dataProvider brokenPHP81TestProvider * @throws SodiumException */ - public function testBrokenPHP81($k_hex, $a_hex, $expect) + public function testBrokenPHP81($k_hex, $a_hex, $expect): void { $k = sodium_hex2bin($k_hex); $a = sodium_hex2bin($a_hex); diff --git a/tests/phpunit-shim.php b/tests/phpunit-shim.php deleted file mode 100644 index b8462607..00000000 --- a/tests/phpunit-shim.php +++ /dev/null @@ -1,6 +0,0 @@ -assertSame($s, $expected); } - public function testAegis256lUpdate() + public function testAegis256lUpdate(): void { $state = ParagonIE_Sodium_Core_AEGIS_State256::initForUnitTests(array( ParagonIE_Sodium_Core_Util::hex2bin('1fa1207ed76c86f2c4bb40e8b395b43e'), @@ -286,7 +288,7 @@ public function testAegis128lVectors( $msg_hex = '', $ad_hex = '', $expect_fail = false - ) { + ): void { $key = ParagonIE_Sodium_Core_Util::hex2bin($key_hex); $nonce = ParagonIE_Sodium_Core_Util::hex2bin($nonce_hex); $expTag = ParagonIE_Sodium_Core_Util::hex2bin($expected_tag_hex); @@ -347,7 +349,7 @@ public function testAegis256Vectors( $msg_hex = '', $ad_hex = '', $expect_fail = false - ) { + ): void { $key = ParagonIE_Sodium_Core_Util::hex2bin($key_hex); $nonce = ParagonIE_Sodium_Core_Util::hex2bin($nonce_hex); $expTag = ParagonIE_Sodium_Core_Util::hex2bin($expected_tag_hex); @@ -387,7 +389,7 @@ public function testAegis256Vectors( $this->assertSame($got_pt, $msg, $name); } - public function testPublicAegis128l() + public function testPublicAegis128l(): void { $msg = ParagonIE_Sodium_Compat::randombytes_buf(ParagonIE_Sodium_Compat::randombytes_uniform(999) + 1); $nonce = ParagonIE_Sodium_Compat::randombytes_buf(ParagonIE_Sodium_Compat::CRYPTO_AEAD_AEGIS128L_NPUBBYTES); @@ -398,7 +400,7 @@ public function testPublicAegis128l() $this->assertSame($msg, $msg2); } - public function testPublicAegis256() + public function testPublicAegis256(): void { $msg = ParagonIE_Sodium_Compat::randombytes_buf(ParagonIE_Sodium_Compat::randombytes_uniform(999) + 1); $nonce = ParagonIE_Sodium_Compat::randombytes_buf(ParagonIE_Sodium_Compat::CRYPTO_AEAD_AEGIS256_NPUBBYTES); diff --git a/tests/unit/AESTest.php b/tests/unit/AESTest.php index 4371d790..faf4caa7 100644 --- a/tests/unit/AESTest.php +++ b/tests/unit/AESTest.php @@ -1,14 +1,15 @@ orthogonalize(); @@ -176,7 +177,7 @@ public function testOrtho(array $input, array $expected) /** * @covers ParagonIE_Sodium_Core_AES::addRoundKey */ - public function testAddRoundKey() + public function testAddRoundKey(): void { $q = ParagonIE_Sodium_Core_AES_Block::fromArray(array(1, 2, 3, 4, 5, 6, 7, 8)); $schedule = ParagonIE_Sodium_Core_AES::keySchedule('sodiumcompat1.21'); @@ -190,7 +191,7 @@ public function testAddRoundKey() /** * @covers ParagonIE_Sodium_Core_AES_Block::shiftRows */ - public function testShiftRows() + public function testShiftRows(): void { $q = ParagonIE_Sodium_Core_AES_Block::fromArray(array( 0x11111111, 0x22222222, 0x33333333, 0x44444444, @@ -207,12 +208,12 @@ public function testShiftRows() } } - public function testSubWord() + public function testSubWord(): void { $this->assertSame(0xfe76abd7, ParagonIE_Sodium_Core_AES::subWord(0x0c0f0e0d)); } - public function testMixColumns() + public function testMixColumns(): void { $q = ParagonIE_Sodium_Core_AES_Block::fromArray(array( 0xf8be2b17, 0xcaba63cb, 0x67b2a090, 0x8988c2d4, 0x1a70b1e8, 0xcabf96eb, 0x7ae7f79b, 0x615d60d8 @@ -240,7 +241,7 @@ public function testMixColumns() $this->assertSame(0x615d60d8, $q[7]); } - public function testKeySchedule() + public function testKeySchedule(): void { $ks = ParagonIE_Sodium_Core_AES::keySchedule(sodium_hex2bin("000102030405060708090a0b0c0d0e0f")); $expect = array( @@ -289,7 +290,7 @@ public function testKeySchedule() } } - public function testSkeyExpand() + public function testSkeyExpand(): void { // "000102030405060708090a0b0c0d0e0f" $ks = new ParagonIE_Sodium_Core_AES_KeySchedule(array( @@ -319,7 +320,7 @@ public function testSkeyExpand() } } - public function testAesRound() + public function testAesRound(): void { $in = ParagonIE_Sodium_Core_Util::hex2bin('000102030405060708090a0b0c0d0e0f'); $rk = ParagonIE_Sodium_Core_Util::hex2bin('101112131415161718191a1b1c1d1e1f'); @@ -331,7 +332,7 @@ public function testAesRound() ); } - public function testAesDoubleRound() + public function testAesDoubleRound(): void { $in = ParagonIE_Sodium_Core_Util::hex2bin('000102030405060708090a0b0c0d0e0f'); $rk = ParagonIE_Sodium_Core_Util::hex2bin('101112131415161718191a1b1c1d1e1f'); @@ -365,7 +366,7 @@ public function testAesDoubleRound() * @dataProvider aes128ecbProvider * @covers ParagonIE_Sodium_Core_AES::encryptBlockECB */ - public function testEncryptBlock128ECB($key_hex, $pt_hex, $ct_hex) + public function testEncryptBlock128ECB($key_hex, $pt_hex, $ct_hex): void { $key = ParagonIE_Sodium_Core_Util::hex2bin($key_hex); @@ -382,7 +383,7 @@ public function testEncryptBlock128ECB($key_hex, $pt_hex, $ct_hex) * @dataProvider aes192ecbProvider * @covers ParagonIE_Sodium_Core_AES::encryptBlockECB */ - public function testEncryptBlock192ECB($key_hex, $pt_hex, $ct_hex) + public function testEncryptBlock192ECB($key_hex, $pt_hex, $ct_hex): void { $key = ParagonIE_Sodium_Core_Util::hex2bin($key_hex); @@ -400,7 +401,7 @@ public function testEncryptBlock192ECB($key_hex, $pt_hex, $ct_hex) * @dataProvider aes256ecbProvider * @covers ParagonIE_Sodium_Core_AES::encryptBlockECB */ - public function testEncryptBlock256ECB($key_hex, $pt_hex, $ct_hex) + public function testEncryptBlock256ECB($key_hex, $pt_hex, $ct_hex): void { $key = ParagonIE_Sodium_Core_Util::hex2bin($key_hex); diff --git a/tests/unit/Aes256GcmTest.php b/tests/unit/Aes256GcmTest.php index e85693f9..8d6cc317 100644 --- a/tests/unit/Aes256GcmTest.php +++ b/tests/unit/Aes256GcmTest.php @@ -1,15 +1,13 @@ = 70000 && !class_exists('PHPUnit_Framework_TestCase')) { - require_once dirname(dirname(dirname(__FILE__))) . '/autoload-phpunit.php'; -} +use PHPUnit\Framework\TestCase; -class Aes256GcmTest extends PHPUnit_Framework_TestCase +class Aes256GcmTest extends TestCase { /** * @before */ - public function before() + public function before(): void { ParagonIE_Sodium_Compat::$disableFallbackForUnitTests = true; } @@ -17,7 +15,7 @@ public function before() /** * @covers ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_is_available() */ - public function testIsAvailable() + public function testIsAvailable(): void { $this->assertTrue( is_bool( @@ -31,7 +29,7 @@ public function testIsAvailable() * @covers ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_decrypt() * @covers ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_encrypt() */ - public function testAes256Gcm() + public function testAes256Gcm(): void { if (!ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_is_available()) { $this->markTestSkipped('Cannot test AES-256-GCM; it is not available.'); diff --git a/tests/unit/Blake2bTest.php b/tests/unit/Blake2bTest.php index 30925635..f5a0a6c3 100644 --- a/tests/unit/Blake2bTest.php +++ b/tests/unit/Blake2bTest.php @@ -1,11 +1,12 @@ assertSame( 'df654812bac492663825520ba2f6e67cf5ca5bdc13d4e7507a98cc4c2fcc3ad8', @@ -30,7 +31,7 @@ public function testGenericHash() /** * @throws SodiumException */ - public function testPersonalizedState() + public function testPersonalizedState(): void { $exp = ParagonIE_Sodium_Core_Util::hex2bin( '48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5e4e0d0cf4b636b35260e0d1fbf0e60ab' . @@ -96,7 +97,7 @@ public function testPersonalizedState() * @throws SodiumException * @throws TypeError */ - public function testGenericHashSaltPersonal() + public function testGenericHashSaltPersonal(): void { $outputs = array( 'ba', @@ -235,7 +236,7 @@ public function testGenericHashSaltPersonal() * @throws SodiumException * @throws TypeError */ - public function testGenericHashUpdate() + public function testGenericHashUpdate(): void { for ($h = 8; $h < 15; ++$h) { $hbuf = new SplFixedArray(1 << $h); @@ -274,7 +275,7 @@ public function testGenericHashUpdate() /** * @covers ParagonIE_Sodium_Core_BLAKE2b::increment_counter() */ - public function testCounter() + public function testCounter(): void { $ctx = ParagonIE_Sodium_Core_BLAKE2b::init(null, 32); @@ -305,7 +306,7 @@ public function testCounter() * @covers ParagonIE_Sodium_Core_BLAKE2b::contextToString() * @covers ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray() */ - public function testContext() + public function testContext(): void { $ctxA = ParagonIE_Sodium_Compat::crypto_generichash_init(); @@ -375,7 +376,7 @@ public function testContext() * @throws SodiumException * @throws Exception */ - public function testGenericHashStream() + public function testGenericHashStream(): void { $ctx = ParagonIE_Sodium_Compat::crypto_generichash_init(); $this->assertSame( @@ -453,7 +454,7 @@ public function testGenericHashStream() * @throws SodiumException * @throws TypeError */ - public function testRotate() + public function testRotate(): void { $int = ParagonIE_Sodium_Core_BLAKE2b::new64(0x7f000000, 0x3ffffff0); $expected = ParagonIE_Sodium_Core_BLAKE2b::new64(0x3f800000, 0x1ffffff8); diff --git a/tests/unit/BoxSealTest.php b/tests/unit/BoxSealTest.php index 4f016e20..a2907c19 100644 --- a/tests/unit/BoxSealTest.php +++ b/tests/unit/BoxSealTest.php @@ -1,16 +1,17 @@ assertSame("00000220", ParagonIE_Sodium_Core_Util::bin2hex($string)); } - public function testRuntimeSpeed() + public function testRuntimeSpeed(): void { if (ParagonIE_Sodium_Compat::polyfill_is_fast()) { $this->markTestSkipped('Polyfill is fast, no need to test this.'); @@ -38,7 +39,7 @@ public function testRuntimeSpeed() * @throws SodiumException * @throws Exception */ - public function testKeyExchange() + public function testKeyExchange(): void { $alice = ParagonIE_Sodium_Compat::crypto_kx_keypair(); $alice_pk = ParagonIE_Sodium_Compat::crypto_kx_publickey($alice); @@ -55,7 +56,7 @@ public function testKeyExchange() /** * @throws SodiumException */ - public function testSodiumPad() + public function testSodiumPad(): void { for ($i = 0; $i < 100; ++$i) { $block = random_int(16, 256); @@ -75,7 +76,7 @@ public function testSodiumPad() /** * @throws SodiumException */ - public function testEd25519Keypairs() + public function testEd25519Keypairs(): void { $keypair = ParagonIE_Sodium_Core_Util::hex2bin( '73eda3c0594270f19fbed39440c15453c647987b5fd3a38164c383adfa638ebe' . diff --git a/tests/unit/CryptoTest.php b/tests/unit/CryptoTest.php index 8854e2a2..85b6de17 100644 --- a/tests/unit/CryptoTest.php +++ b/tests/unit/CryptoTest.php @@ -1,11 +1,12 @@ assertEquals( ParagonIE_Sodium_Core_Curve25519::ge_select(0, 6), @@ -332,7 +333,7 @@ public function testGeSelect() /** * @covers ParagonIE_Sodium_Core_Curve25519::fe_mul() */ - public function testFeMul() + public function testFeMul(): void { $f = ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( array( @@ -490,7 +491,7 @@ public function testFeMul() /** * @covers ParagonIE_Sodium_Core_Curve25519::ge_madd() */ - public function testGeMAdd() + public function testGeMAdd(): void { $p = new ParagonIE_Sodium_Core_Curve25519_Ge_P3( ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( @@ -643,7 +644,7 @@ public function testGeMAdd() /** * @covers ParagonIE_Sodium_Core_Curve25519::ge_scalarmult_base() */ - public function testGeScalarmultBase() + public function testGeScalarmultBase(): void { $nonce = ParagonIE_Sodium_Core_Util::hex2bin( 'a5cdb7382d5282472312e739b7b8fded4b0bc73a8d3b7ac24e6ee259df74800a' . @@ -718,7 +719,7 @@ public function testGeScalarmultBase() * @throws SodiumException * @throws TypeError */ - public function testSlide() + public function testSlide(): void { $a = ParagonIE_Sodium_Core_Util::hex2bin( 'fc2ef90e2ddab38c55d0edbf41167048061a03b99d00112dcc92777c1b17300c' . @@ -774,7 +775,7 @@ public function testSlide() * @throws SodiumException * @throws TypeError */ - public function testGeDoubleScalarMultVartime() + public function testGeDoubleScalarMultVartime(): void { $h = ParagonIE_Sodium_Core_Util::hex2bin( 'fc2ef90e2ddab38c55d0edbf41167048061a03b99d00112dcc92777c1b17300c' . @@ -856,7 +857,7 @@ public function testGeDoubleScalarMultVartime() * @throws SodiumException * @throws TypeError */ - public function testGeP3Double() + public function testGeP3Double(): void { $h = new ParagonIE_Sodium_Core_Curve25519_Ge_P3( ParagonIE_Sodium_Core_Curve25519_Fe::fromArray( @@ -897,7 +898,7 @@ public function testGeP3Double() * @throws SodiumException * @throws TypeError */ - public function testGeP3ToBytes32() + public function testGeP3ToBytes32(): void { $R = new ParagonIE_Sodium_Core_Curve25519_Ge_P3( ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(array( @@ -962,7 +963,7 @@ public function testGeP3ToBytes32() * @throws SodiumException * @throws TypeError */ - public function testFromBytes() + public function testFromBytes(): void { $a = ParagonIE_Sodium_Core_Curve25519::fe_frombytes( ParagonIE_Sodium_Core_Util::hex2bin('f7efaafeb83be2f97cd351b48e78f9d158269cdecedbcb503913172158b69336') @@ -982,7 +983,7 @@ public function testFromBytes() /** * @covers ParagonIE_Sodium_Core_Curve25519::fe_mul() */ - public function test121666Mul() + public function test121666Mul(): void { $f = array( 6334098, -296341, -25402037, 14130508, 28301433, 10881396, -32579582, 21932206, 23531802, -8703561 diff --git a/tests/unit/Ed25519Test.php b/tests/unit/Ed25519Test.php index 19a8bf08..ac893d36 100644 --- a/tests/unit/Ed25519Test.php +++ b/tests/unit/Ed25519Test.php @@ -1,11 +1,12 @@ assertSame( 'edd3f55c1a631258d69cf7a2def9de1400000000000000000000000000000010', @@ -188,7 +189,7 @@ public function testConstant() * @covers ParagonIE_Sodium_Core_Ed25519::sign_detached() * @covers ParagonIE_Sodium_Core_Ed25519::verify_detached() */ - public function testVectors() + public function testVectors(): void { $secretKey = ParagonIE_Sodium_Core_Util::hex2bin( 'c4ffb94f252886b1378589af0d7d2004d9564b971ac73f09da827b80a5e39cd5' . diff --git a/tests/unit/ExceptionTest.php b/tests/unit/ExceptionTest.php index 8b89e2ab..1c6ec50a 100644 --- a/tests/unit/ExceptionTest.php +++ b/tests/unit/ExceptionTest.php @@ -1,16 +1,17 @@ assertSame( ParagonIE_Sodium_Compat::CRYPTO_AUTH_KEYBYTES, diff --git a/tests/unit/Poly1305Test.php b/tests/unit/Poly1305Test.php index 46ebe818..1e598785 100644 --- a/tests/unit/Poly1305Test.php +++ b/tests/unit/Poly1305Test.php @@ -1,11 +1,12 @@ assertEquals( 0x00001000, @@ -76,7 +77,7 @@ public function testRotate() /** * @covers ParagonIE_Sodium_Core_Salsa20::salsa20() */ - public function testVectors() + public function testVectors(): void { $key = "\x80" . str_repeat("\x00", 31); $iv = str_repeat("\x00", 8); @@ -113,7 +114,7 @@ public function testVectors() /** * @covers ParagonIE_Sodium_Core_Salsa20::core_salsa20() */ - public function testCoreSalsa20() + public function testCoreSalsa20(): void { $key = random_bytes(32); $iv = random_bytes(8); diff --git a/tests/unit/SecretStreamTest.php b/tests/unit/SecretStreamTest.php index 51b64cf6..277548d0 100644 --- a/tests/unit/SecretStreamTest.php +++ b/tests/unit/SecretStreamTest.php @@ -1,14 +1,15 @@ assertSame( array(0x00010000, 0x00000000), @@ -107,7 +108,7 @@ public function testRotl64() ); } - public function testSipRound() + public function testSipRound(): void { $v = array( 0x736f6d65, // 0 @@ -153,7 +154,7 @@ public function testSipRound() /** * @covers ParagonIE_Sodium_Core_SipHash::sipHash24() */ - public function testEvenBlock() + public function testEvenBlock(): void { $message = str_repeat("\xff", 32); $key = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"; @@ -169,7 +170,7 @@ public function testEvenBlock() /** * @covers ParagonIE_Sodium_Core_SipHash::sipHash24() */ - public function testCryptoShorthash() + public function testCryptoShorthash(): void { $message = 'this is just a test message'; $key = str_repeat("\x80", 16); diff --git a/tests/unit/StreamTest.php b/tests/unit/StreamTest.php index e4f66a8a..cf1f56b5 100644 --- a/tests/unit/StreamTest.php +++ b/tests/unit/StreamTest.php @@ -1,14 +1,15 @@ assertEquals(0, ParagonIE_Sodium_Core_Util::abs(0)); $this->assertEquals(1, ParagonIE_Sodium_Core_Util::abs(1)); @@ -47,7 +48,7 @@ public function testAbs() * @throws TypeError * @throws Exception */ - public function testBase64() + public function testBase64(): void { for ($i = 0; $i < 100; $i++) { $bin = $i === 0 ? '' : random_bytes($i); @@ -74,7 +75,7 @@ public function testBase64() * @throws TypeError * @throws Exception */ - public function testBin2hex() + public function testBin2hex(): void { $data = random_bytes(32); $this->assertSame( @@ -104,7 +105,7 @@ public function testBin2hex() * @covers ParagonIE_Sodium_Compat::randombytes_uniform() * @throws TypeError */ - public function testRandombytes() + public function testRandombytes(): void { $random = ParagonIE_Sodium_Compat::randombytes_buf(32); $this->assertSame(32, ParagonIE_Sodium_Core_Util::strlen($random)); @@ -126,7 +127,7 @@ public function testRandombytes() * @covers ParagonIE_Sodium_Core_Util::stringToIntArray() * @throws TypeError */ - public function testConversion() + public function testConversion(): void { $sample = array(80, 97, 114, 97, 103, 111, 110); @@ -146,7 +147,7 @@ public function testConversion() * @throws SodiumException * @throws TypeError */ - public function testHashEquals() + public function testHashEquals(): void { if (PHP_VERSION_ID < 50600) { for ($i = 0; $i < 65536; ++$i) { @@ -166,7 +167,7 @@ public function testHashEquals() * @covers ParagonIE_Sodium_Core_Util::load_3() * @throws TypeError */ - public function testLoad3() + public function testLoad3(): void { $this->assertSame( 8451279, @@ -189,7 +190,7 @@ public function testLoad3() * @covers ParagonIE_Sodium_Core_Util::load_4() * @throws TypeError */ - public function testLoad4() + public function testLoad4(): void { $this->assertSame( 8451279, @@ -208,7 +209,7 @@ public function testLoad4() * @throws SodiumException * @throws TypeError */ - public function testLoad64() + public function testLoad64(): void { if (PHP_INT_SIZE < 8) { $this->markTestSkipped('Public utility test for load64_le()'); @@ -228,7 +229,7 @@ public function testLoad64() /** * @covers ParagonIE_Sodium_Core_Util::strlen() */ - public function testStrlen() + public function testStrlen(): void { $this->assertSame(4, ParagonIE_Sodium_Core_Util::strlen("\xF0\x9D\x92\xB3")); } @@ -236,7 +237,7 @@ public function testStrlen() /** * @covers ParagonIE_Sodium_Core_Util::strlen() */ - public function testSubstr() + public function testSubstr(): void { $string = str_repeat("\xF0\x9D\x92\xB3", 4); $this->assertSame(ParagonIE_Sodium_Core_Util::substr($string, 0, 1), "\xF0"); @@ -249,7 +250,7 @@ public function testSubstr() /** * @covers ParagonIE_Sodium_Core_Util::store64_le() */ - public function testStore64() + public function testStore64(): void { if (PHP_INT_SIZE < 8) { $this->markTestSkipped('Public utility test for load64_le()'); @@ -270,7 +271,7 @@ public function testStore64() /** * @covers ParagonIE_Sodium_Core_Util::mul() */ - public function testMul() + public function testMul(): void { $arguments = array( array(1, 1), diff --git a/tests/unit/WycheproofTest.php b/tests/unit/WycheproofTest.php index dafd89ef..f12b831b 100644 --- a/tests/unit/WycheproofTest.php +++ b/tests/unit/WycheproofTest.php @@ -1,13 +1,14 @@ dir = dirname(__FILE__) . '/wycheproof/'; @@ -16,7 +17,7 @@ public function before() /** * @throws Exception */ - public function testChaCha20Poly1305() + public function testChaCha20Poly1305(): void { if (empty($this->dir)) { $this->before(); @@ -27,7 +28,7 @@ public function testChaCha20Poly1305() /** * @throws Exception */ - public function testXChaCha20Poly1305() + public function testXChaCha20Poly1305(): void { if (empty($this->dir)) { $this->before(); @@ -38,7 +39,7 @@ public function testXChaCha20Poly1305() /** * @throws Exception */ - public function testSipHash24() + public function testSipHash24(): void { if (empty($this->dir)) { $this->before(); @@ -49,7 +50,7 @@ public function testSipHash24() /** * @throws Exception */ - public function testX25519() + public function testX25519(): void { if (!defined('DO_PEDANTIC_TEST')) { $this->markTestSkipped('Skipping Wycheproof Tests. Use DO_PEDANTIC_TEST to enable.'); @@ -66,7 +67,7 @@ public function testX25519() * * @throws Exception */ - public function mainTestingLoop($filename, $method, $progress = false) + public function mainTestingLoop($filename, $method, $progress = false): void { $total = 0; $document = $this->getJson($this->dir . $filename); diff --git a/tests/unit/X25519Test.php b/tests/unit/X25519Test.php index 806b10e2..a75abe3d 100644 --- a/tests/unit/X25519Test.php +++ b/tests/unit/X25519Test.php @@ -1,14 +1,15 @@