Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/wp-includes/sodium_compat/src/Core/ChaCha20.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ public static function encryptBytes(

$c = '';
for (;;) {
if ($j12 > 0xffffffff || $j13 > 0xffffffff) {
throw new SodiumException('Overflow');
}
if ($bytes < 64) {
$message .= str_repeat("\x00", 64 - $bytes);
}
Expand Down Expand Up @@ -258,9 +261,12 @@ public static function encryptBytes(
j13 = PLUSONE(j13);
}
*/
++$j12;
if ($j12 & 0xf0000000) {
throw new SodiumException('Overflow');
if (
++$j12 > 0xffffffff &&
!($ctx instanceof ParagonIE_Sodium_Core_ChaCha20_IetfCtx)
) {
$j12 = 0;
++$j13;
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/sodium_compat/src/Core/Curve25519.php
Original file line number Diff line number Diff line change
Expand Up @@ -3855,4 +3855,4 @@ public static function fe_normalize(ParagonIE_Sodium_Core_Curve25519_Fe $f)
$g->e9 = $e[9];
return $g;
}
}
}
55 changes: 31 additions & 24 deletions src/wp-includes/sodium_compat/src/Core/Ed25519.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,40 +317,47 @@ public static function verify_detached($sig, $message, $pk)
if ($d === 0) {
throw new SodiumException('All zero public key');
}
if (self::small_order($pk)) {
throw new SodiumException('Public key has small order');
}

/** @var bool 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;
try {
/** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */
$A = self::ge_frombytes_negate_vartime($pk);
if (!self::is_on_main_subgroup($A)) {
throw new SodiumException('Public key is not on a member of the main subgroup');
}

/** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */
$A = self::ge_frombytes_negate_vartime($pk);
if (!self::is_on_main_subgroup($A)) {
throw new SodiumException('Public key is not on a member of the main subgroup');
}

/** @var string $hDigest */
$hDigest = hash(
'sha512',
self::substr($sig, 0, 32) .
self::substr($pk, 0, 32) .
$message,
true
);
/** @var string $hDigest */
$hDigest = hash(
'sha512',
self::substr($sig, 0, 32) .
self::substr($pk, 0, 32) .
$message,
true
);

/** @var string $h */
$h = self::sc_reduce($hDigest) . self::substr($hDigest, 32);
/** @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 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);
/** @var string $rcheck */
$rcheck = self::ge_tobytes($R);
} catch (Exception $ex) {
ParagonIE_Sodium_Compat::$fastMult = $orig;
throw $ex;
}

// Reset ParagonIE_Sodium_Compat::$fastMult to what it was before.
ParagonIE_Sodium_Compat::$fastMult = $orig;
Expand Down
3 changes: 3 additions & 0 deletions src/wp-includes/sodium_compat/src/Core/Ristretto255.php
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,9 @@ public static function ristretto255_scalar_complement($s)
*/
public static function ristretto255_scalar_invert($s)
{
if (ParagonIE_Sodium_Compat::is_zero($s)) {
throw new SodiumException('Cannot invert a zero scalar');
}
return self::sc25519_invert($s);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function incrementCounter()
*/
public function needsRekey()
{
return ($this->counter & 0xffff) === 0;
return ($this->counter & 0xffffffff) === 0;
}

/**
Expand Down
12 changes: 11 additions & 1 deletion src/wp-includes/sodium_compat/src/Core32/ChaCha20.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ public static function encryptBytes(

$c = '';
for (;;) {
if ($ctx->isExhausted()) {
throw new SodiumException('Overflow');
}
if ($bytes < 64) {
$message .= str_repeat("\x00", 64 - $bytes);
}
Expand Down Expand Up @@ -264,7 +267,14 @@ public static function encryptBytes(
/** @var ParagonIE_Sodium_Core32_Int32 $j12 */
$j12 = $j12->addInt(1);
if ($j12->limbs[0] === 0 && $j12->limbs[1] === 0) {
$j13 = $j13->addInt(1);
if ($ctx instanceof ParagonIE_Sodium_Core32_ChaCha20_IetfCtx) {
$ctx->markExhausted();
} else {
$j13 = $j13->addInt(1);
if ($j13->limbs[0] === 0 && $j13->limbs[1] === 0) {
$ctx->markExhausted();
}
}
}

/*
Expand Down
15 changes: 15 additions & 0 deletions src/wp-includes/sodium_compat/src/Core32/ChaCha20/Ctx.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ class ParagonIE_Sodium_Core32_ChaCha20_Ctx extends ParagonIE_Sodium_Core32_Util
*/
protected $container;

/** @var bool */
protected $exhausted = false;

/** @return bool */
public function isExhausted()
{
return $this->exhausted;
}

/** @return void */
public function markExhausted()
{
$this->exhausted = true;
}

/**
* ParagonIE_Sodium_Core_ChaCha20_Ctx constructor.
*
Expand Down
52 changes: 28 additions & 24 deletions src/wp-includes/sodium_compat/src/Core32/Ed25519.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,34 +319,38 @@ public static function verify_detached($sig, $message, $pk)

// Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification.
ParagonIE_Sodium_Compat::$fastMult = true;
try {
/** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */
$A = self::ge_frombytes_negate_vartime($pk);
if (!self::is_on_main_subgroup($A)) {
throw new SodiumException('Public key is not on a member of the main subgroup');
}

/** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */
$A = self::ge_frombytes_negate_vartime($pk);
if (!self::is_on_main_subgroup($A)) {
throw new SodiumException('Public key is not on a member of the main subgroup');
}

/** @var string $hDigest */
$hDigest = hash(
'sha512',
self::substr($sig, 0, 32) .
self::substr($pk, 0, 32) .
$message,
true
);
/** @var string $hDigest */
$hDigest = hash(
'sha512',
self::substr($sig, 0, 32) .
self::substr($pk, 0, 32) .
$message,
true
);

/** @var string $h */
$h = self::sc_reduce($hDigest) . self::substr($hDigest, 32);
/** @var string $h */
$h = self::sc_reduce($hDigest) . self::substr($hDigest, 32);

/** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */
$R = self::ge_double_scalarmult_vartime(
$h,
$A,
self::substr($sig, 32)
);
/** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */
$R = self::ge_double_scalarmult_vartime(
$h,
$A,
self::substr($sig, 32)
);

/** @var string $rcheck */
$rcheck = self::ge_tobytes($R);
/** @var string $rcheck */
$rcheck = self::ge_tobytes($R);
} catch (Exception $ex) {
ParagonIE_Sodium_Compat::$fastMult = $orig;
throw $ex;
}

// Reset ParagonIE_Sodium_Compat::$fastMult to what it was before.
ParagonIE_Sodium_Compat::$fastMult = $orig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function incrementCounter()
*/
public function needsRekey()
{
return ($this->counter & 0xffff) === 0;
return ($this->counter & 0xffffffff) === 0;
}

/**
Expand Down
Loading
Loading