You are here

public static function ParagonIE_Sodium_File::verify_core32 in Automatic Updates 8

Same name and namespace in other branches
  1. 7 vendor/paragonie/sodium_compat/src/File.php \ParagonIE_Sodium_File::verify_core32()

Verify a file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_sign_verify_detached(), but produces the same result. (32-bit)

Parameters

string $sig Ed25519 signature:

string $filePath Absolute path to a file on the filesystem:

string $publicKey Signing public key:

Return value

bool

Throws

SodiumException

Exception

1 call to ParagonIE_Sodium_File::verify_core32()
ParagonIE_Sodium_File::verify in vendor/paragonie/sodium_compat/src/File.php
Verify a file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_sign_verify_detached(), but produces the same result.

File

vendor/paragonie/sodium_compat/src/File.php, line 1235

Class

ParagonIE_Sodium_File
Class ParagonIE_Sodium_File

Code

public static function verify_core32($sig, $filePath, $publicKey) {

  /* Security checks */
  if (ParagonIE_Sodium_Core32_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))) {
    throw new SodiumException('S < L - Invalid signature');
  }
  if (ParagonIE_Sodium_Core32_Ed25519::small_order($sig)) {
    throw new SodiumException('Signature is on too small of an order');
  }
  if ((self::chrToInt($sig[63]) & 224) !== 0) {
    throw new SodiumException('Invalid signature');
  }
  $d = 0;
  for ($i = 0; $i < 32; ++$i) {
    $d |= self::chrToInt($publicKey[$i]);
  }
  if ($d === 0) {
    throw new SodiumException('All zero public key');
  }

  /** @var int|bool $size */
  $size = filesize($filePath);
  if (!is_int($size)) {
    throw new SodiumException('Could not obtain the file size');
  }

  /** @var int $size */

  /** @var resource|bool $fp */
  $fp = fopen($filePath, 'rb');
  if (!is_resource($fp)) {
    throw new SodiumException('Could not open input file for reading');
  }

  /** @var resource $fp */

  /** @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;

  /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */
  $A = ParagonIE_Sodium_Core32_Ed25519::ge_frombytes_negate_vartime($publicKey);
  $hs = hash_init('sha512');
  hash_update($hs, self::substr($sig, 0, 32));
  hash_update($hs, self::substr($publicKey, 0, 32));

  /** @var resource $hs */
  $hs = self::updateHashWithFile($hs, $fp, $size);

  /** @var string $hDigest */
  $hDigest = hash_final($hs, true);

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

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

  /** @var string $rcheck */
  $rcheck = ParagonIE_Sodium_Core32_Ed25519::ge_tobytes($R);

  // Close the file handle
  fclose($fp);

  // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before.
  ParagonIE_Sodium_Compat::$fastMult = $orig;
  return self::verify_32($rcheck, self::substr($sig, 0, 32));
}