Skip to content

Commit

Permalink
Merge pull request #174 from paragonie/v2-cleanup
Browse files Browse the repository at this point in the history
Clean Up for Version 2
  • Loading branch information
paragonie-security authored Apr 19, 2024
2 parents 18048c1 + 878b86b commit 830d1aa
Show file tree
Hide file tree
Showing 52 changed files with 356 additions and 1,225 deletions.
8 changes: 1 addition & 7 deletions autoload-phpunit.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
<?php

require_once (dirname(__FILE__) . '/vendor/autoload.php');

if (PHP_VERSION_ID >= 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');
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
4 changes: 2 additions & 2 deletions src/Core/AES/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
}
Expand Down
4 changes: 1 addition & 3 deletions src/Core/BLAKE2b.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) | (
(
Expand Down Expand Up @@ -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
{
Expand Down
108 changes: 8 additions & 100 deletions src/Core/ChaCha20.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand All @@ -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));
Expand All @@ -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)) .
Expand Down
4 changes: 2 additions & 2 deletions src/Core/ChaCha20/Ctx.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -101,7 +101,7 @@ public function offsetExists($offset)
* @return void
*/
#[ReturnTypeWillChange]
public function offsetUnset($offset)
public function offsetUnset($offset): void
{
unset($this->container[$offset]);
}
Expand Down
Loading

0 comments on commit 830d1aa

Please sign in to comment.