Edit File by line
/home/zeestwma/richards.../wp-inclu.../sodium_c.../src
File: File.php
$res = self::secretbox_encrypt($ifp, $ofp, $size, $nonce, $key);
[500] Fix | Delete
fclose($ifp);
[501] Fix | Delete
fclose($ofp);
[502] Fix | Delete
return $res;
[503] Fix | Delete
}
[504] Fix | Delete
/**
[505] Fix | Delete
* Seal a file (rather than a string). Uses less memory than
[506] Fix | Delete
* ParagonIE_Sodium_Compat::crypto_secretbox_open(), but produces
[507] Fix | Delete
* the same result.
[508] Fix | Delete
*
[509] Fix | Delete
* Warning: Does not protect against TOCTOU attacks. You should
[510] Fix | Delete
* just load the file into memory and use crypto_secretbox_open() if
[511] Fix | Delete
* you are worried about those.
[512] Fix | Delete
*
[513] Fix | Delete
* @param string $inputFile
[514] Fix | Delete
* @param string $outputFile
[515] Fix | Delete
* @param string $nonce
[516] Fix | Delete
* @param string $key
[517] Fix | Delete
* @return bool
[518] Fix | Delete
* @throws SodiumException
[519] Fix | Delete
* @throws TypeError
[520] Fix | Delete
*/
[521] Fix | Delete
public static function secretbox_open(
[522] Fix | Delete
$inputFile,
[523] Fix | Delete
$outputFile,
[524] Fix | Delete
$nonce,
[525] Fix | Delete
#[\SensitiveParameter]
[526] Fix | Delete
$key
[527] Fix | Delete
) {
[528] Fix | Delete
/* Type checks: */
[529] Fix | Delete
if (!is_string($inputFile)) {
[530] Fix | Delete
throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
[531] Fix | Delete
}
[532] Fix | Delete
if (!is_string($outputFile)) {
[533] Fix | Delete
throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
[534] Fix | Delete
}
[535] Fix | Delete
if (!is_string($nonce)) {
[536] Fix | Delete
throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');
[537] Fix | Delete
}
[538] Fix | Delete
if (!is_string($key)) {
[539] Fix | Delete
throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.');
[540] Fix | Delete
}
[541] Fix | Delete
[542] Fix | Delete
/* Input validation: */
[543] Fix | Delete
if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) {
[544] Fix | Delete
throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_NONCEBYTES bytes');
[545] Fix | Delete
}
[546] Fix | Delete
if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) {
[547] Fix | Delete
throw new TypeError('Argument 4 must be CRYPTO_SECRETBOXBOX_KEYBYTES bytes');
[548] Fix | Delete
}
[549] Fix | Delete
[550] Fix | Delete
/** @var int $size */
[551] Fix | Delete
$size = filesize($inputFile);
[552] Fix | Delete
if (!is_int($size)) {
[553] Fix | Delete
throw new SodiumException('Could not obtain the file size');
[554] Fix | Delete
}
[555] Fix | Delete
[556] Fix | Delete
/** @var resource $ifp */
[557] Fix | Delete
$ifp = fopen($inputFile, 'rb');
[558] Fix | Delete
if (!is_resource($ifp)) {
[559] Fix | Delete
throw new SodiumException('Could not open input file for reading');
[560] Fix | Delete
}
[561] Fix | Delete
[562] Fix | Delete
/** @var resource $ofp */
[563] Fix | Delete
$ofp = fopen($outputFile, 'wb');
[564] Fix | Delete
if (!is_resource($ofp)) {
[565] Fix | Delete
fclose($ifp);
[566] Fix | Delete
throw new SodiumException('Could not open output file for writing');
[567] Fix | Delete
}
[568] Fix | Delete
[569] Fix | Delete
$res = self::secretbox_decrypt($ifp, $ofp, $size, $nonce, $key);
[570] Fix | Delete
fclose($ifp);
[571] Fix | Delete
fclose($ofp);
[572] Fix | Delete
try {
[573] Fix | Delete
ParagonIE_Sodium_Compat::memzero($key);
[574] Fix | Delete
} catch (SodiumException $ex) {
[575] Fix | Delete
/** @psalm-suppress PossiblyUndefinedVariable */
[576] Fix | Delete
unset($key);
[577] Fix | Delete
}
[578] Fix | Delete
return $res;
[579] Fix | Delete
}
[580] Fix | Delete
[581] Fix | Delete
/**
[582] Fix | Delete
* Sign a file (rather than a string). Uses less memory than
[583] Fix | Delete
* ParagonIE_Sodium_Compat::crypto_sign_detached(), but produces
[584] Fix | Delete
* the same result.
[585] Fix | Delete
*
[586] Fix | Delete
* @param string $filePath Absolute path to a file on the filesystem
[587] Fix | Delete
* @param string $secretKey Secret signing key
[588] Fix | Delete
*
[589] Fix | Delete
* @return string Ed25519 signature
[590] Fix | Delete
* @throws SodiumException
[591] Fix | Delete
* @throws TypeError
[592] Fix | Delete
*/
[593] Fix | Delete
public static function sign(
[594] Fix | Delete
$filePath,
[595] Fix | Delete
#[\SensitiveParameter]
[596] Fix | Delete
$secretKey
[597] Fix | Delete
) {
[598] Fix | Delete
/* Type checks: */
[599] Fix | Delete
if (!is_string($filePath)) {
[600] Fix | Delete
throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.');
[601] Fix | Delete
}
[602] Fix | Delete
if (!is_string($secretKey)) {
[603] Fix | Delete
throw new TypeError('Argument 2 must be a string, ' . gettype($secretKey) . ' given.');
[604] Fix | Delete
}
[605] Fix | Delete
[606] Fix | Delete
/* Input validation: */
[607] Fix | Delete
if (self::strlen($secretKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_SECRETKEYBYTES) {
[608] Fix | Delete
throw new TypeError('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES bytes');
[609] Fix | Delete
}
[610] Fix | Delete
if (PHP_INT_SIZE === 4) {
[611] Fix | Delete
return self::sign_core32($filePath, $secretKey);
[612] Fix | Delete
}
[613] Fix | Delete
[614] Fix | Delete
/** @var int $size */
[615] Fix | Delete
$size = filesize($filePath);
[616] Fix | Delete
if (!is_int($size)) {
[617] Fix | Delete
throw new SodiumException('Could not obtain the file size');
[618] Fix | Delete
}
[619] Fix | Delete
[620] Fix | Delete
/** @var resource $fp */
[621] Fix | Delete
$fp = fopen($filePath, 'rb');
[622] Fix | Delete
if (!is_resource($fp)) {
[623] Fix | Delete
throw new SodiumException('Could not open input file for reading');
[624] Fix | Delete
}
[625] Fix | Delete
[626] Fix | Delete
/** @var string $az */
[627] Fix | Delete
$az = hash('sha512', self::substr($secretKey, 0, 32), true);
[628] Fix | Delete
[629] Fix | Delete
$az[0] = self::intToChr(self::chrToInt($az[0]) & 248);
[630] Fix | Delete
$az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64);
[631] Fix | Delete
[632] Fix | Delete
$hs = hash_init('sha512');
[633] Fix | Delete
self::hash_update($hs, self::substr($az, 32, 32));
[634] Fix | Delete
/** @var resource $hs */
[635] Fix | Delete
$hs = self::updateHashWithFile($hs, $fp, $size);
[636] Fix | Delete
[637] Fix | Delete
/** @var string $nonceHash */
[638] Fix | Delete
$nonceHash = hash_final($hs, true);
[639] Fix | Delete
[640] Fix | Delete
/** @var string $pk */
[641] Fix | Delete
$pk = self::substr($secretKey, 32, 32);
[642] Fix | Delete
[643] Fix | Delete
/** @var string $nonce */
[644] Fix | Delete
$nonce = ParagonIE_Sodium_Core_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32);
[645] Fix | Delete
[646] Fix | Delete
/** @var string $sig */
[647] Fix | Delete
$sig = ParagonIE_Sodium_Core_Ed25519::ge_p3_tobytes(
[648] Fix | Delete
ParagonIE_Sodium_Core_Ed25519::ge_scalarmult_base($nonce)
[649] Fix | Delete
);
[650] Fix | Delete
[651] Fix | Delete
$hs = hash_init('sha512');
[652] Fix | Delete
self::hash_update($hs, self::substr($sig, 0, 32));
[653] Fix | Delete
self::hash_update($hs, self::substr($pk, 0, 32));
[654] Fix | Delete
/** @var resource $hs */
[655] Fix | Delete
$hs = self::updateHashWithFile($hs, $fp, $size);
[656] Fix | Delete
[657] Fix | Delete
/** @var string $hramHash */
[658] Fix | Delete
$hramHash = hash_final($hs, true);
[659] Fix | Delete
[660] Fix | Delete
/** @var string $hram */
[661] Fix | Delete
$hram = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hramHash);
[662] Fix | Delete
[663] Fix | Delete
/** @var string $sigAfter */
[664] Fix | Delete
$sigAfter = ParagonIE_Sodium_Core_Ed25519::sc_muladd($hram, $az, $nonce);
[665] Fix | Delete
[666] Fix | Delete
/** @var string $sig */
[667] Fix | Delete
$sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);
[668] Fix | Delete
[669] Fix | Delete
try {
[670] Fix | Delete
ParagonIE_Sodium_Compat::memzero($az);
[671] Fix | Delete
} catch (SodiumException $ex) {
[672] Fix | Delete
$az = null;
[673] Fix | Delete
}
[674] Fix | Delete
fclose($fp);
[675] Fix | Delete
return $sig;
[676] Fix | Delete
}
[677] Fix | Delete
[678] Fix | Delete
/**
[679] Fix | Delete
* Verify a file (rather than a string). Uses less memory than
[680] Fix | Delete
* ParagonIE_Sodium_Compat::crypto_sign_verify_detached(), but
[681] Fix | Delete
* produces the same result.
[682] Fix | Delete
*
[683] Fix | Delete
* @param string $sig Ed25519 signature
[684] Fix | Delete
* @param string $filePath Absolute path to a file on the filesystem
[685] Fix | Delete
* @param string $publicKey Signing public key
[686] Fix | Delete
*
[687] Fix | Delete
* @return bool
[688] Fix | Delete
* @throws SodiumException
[689] Fix | Delete
* @throws TypeError
[690] Fix | Delete
* @throws Exception
[691] Fix | Delete
*/
[692] Fix | Delete
public static function verify(
[693] Fix | Delete
$sig,
[694] Fix | Delete
$filePath,
[695] Fix | Delete
$publicKey
[696] Fix | Delete
) {
[697] Fix | Delete
/* Type checks: */
[698] Fix | Delete
if (!is_string($sig)) {
[699] Fix | Delete
throw new TypeError('Argument 1 must be a string, ' . gettype($sig) . ' given.');
[700] Fix | Delete
}
[701] Fix | Delete
if (!is_string($filePath)) {
[702] Fix | Delete
throw new TypeError('Argument 2 must be a string, ' . gettype($filePath) . ' given.');
[703] Fix | Delete
}
[704] Fix | Delete
if (!is_string($publicKey)) {
[705] Fix | Delete
throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.');
[706] Fix | Delete
}
[707] Fix | Delete
[708] Fix | Delete
/* Input validation: */
[709] Fix | Delete
if (self::strlen($sig) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_BYTES) {
[710] Fix | Delete
throw new TypeError('Argument 1 must be CRYPTO_SIGN_BYTES bytes');
[711] Fix | Delete
}
[712] Fix | Delete
if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_PUBLICKEYBYTES) {
[713] Fix | Delete
throw new TypeError('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES bytes');
[714] Fix | Delete
}
[715] Fix | Delete
if (self::strlen($sig) < 64) {
[716] Fix | Delete
throw new SodiumException('Signature is too short');
[717] Fix | Delete
}
[718] Fix | Delete
[719] Fix | Delete
if (PHP_INT_SIZE === 4) {
[720] Fix | Delete
return self::verify_core32($sig, $filePath, $publicKey);
[721] Fix | Delete
}
[722] Fix | Delete
[723] Fix | Delete
/* Security checks */
[724] Fix | Delete
if (
[725] Fix | Delete
(ParagonIE_Sodium_Core_Ed25519::chrToInt($sig[63]) & 240)
[726] Fix | Delete
&&
[727] Fix | Delete
ParagonIE_Sodium_Core_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))
[728] Fix | Delete
) {
[729] Fix | Delete
throw new SodiumException('S < L - Invalid signature');
[730] Fix | Delete
}
[731] Fix | Delete
if (ParagonIE_Sodium_Core_Ed25519::small_order($sig)) {
[732] Fix | Delete
throw new SodiumException('Signature is on too small of an order');
[733] Fix | Delete
}
[734] Fix | Delete
if ((self::chrToInt($sig[63]) & 224) !== 0) {
[735] Fix | Delete
throw new SodiumException('Invalid signature');
[736] Fix | Delete
}
[737] Fix | Delete
$d = 0;
[738] Fix | Delete
for ($i = 0; $i < 32; ++$i) {
[739] Fix | Delete
$d |= self::chrToInt($publicKey[$i]);
[740] Fix | Delete
}
[741] Fix | Delete
if ($d === 0) {
[742] Fix | Delete
throw new SodiumException('All zero public key');
[743] Fix | Delete
}
[744] Fix | Delete
[745] Fix | Delete
/** @var int $size */
[746] Fix | Delete
$size = filesize($filePath);
[747] Fix | Delete
if (!is_int($size)) {
[748] Fix | Delete
throw new SodiumException('Could not obtain the file size');
[749] Fix | Delete
}
[750] Fix | Delete
[751] Fix | Delete
/** @var resource $fp */
[752] Fix | Delete
$fp = fopen($filePath, 'rb');
[753] Fix | Delete
if (!is_resource($fp)) {
[754] Fix | Delete
throw new SodiumException('Could not open input file for reading');
[755] Fix | Delete
}
[756] Fix | Delete
[757] Fix | Delete
/** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */
[758] Fix | Delete
$orig = ParagonIE_Sodium_Compat::$fastMult;
[759] Fix | Delete
[760] Fix | Delete
// Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification.
[761] Fix | Delete
ParagonIE_Sodium_Compat::$fastMult = true;
[762] Fix | Delete
[763] Fix | Delete
/** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */
[764] Fix | Delete
$A = ParagonIE_Sodium_Core_Ed25519::ge_frombytes_negate_vartime($publicKey);
[765] Fix | Delete
[766] Fix | Delete
$hs = hash_init('sha512');
[767] Fix | Delete
self::hash_update($hs, self::substr($sig, 0, 32));
[768] Fix | Delete
self::hash_update($hs, self::substr($publicKey, 0, 32));
[769] Fix | Delete
/** @var resource $hs */
[770] Fix | Delete
$hs = self::updateHashWithFile($hs, $fp, $size);
[771] Fix | Delete
/** @var string $hDigest */
[772] Fix | Delete
$hDigest = hash_final($hs, true);
[773] Fix | Delete
[774] Fix | Delete
/** @var string $h */
[775] Fix | Delete
$h = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hDigest) . self::substr($hDigest, 32);
[776] Fix | Delete
[777] Fix | Delete
/** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */
[778] Fix | Delete
$R = ParagonIE_Sodium_Core_Ed25519::ge_double_scalarmult_vartime(
[779] Fix | Delete
$h,
[780] Fix | Delete
$A,
[781] Fix | Delete
self::substr($sig, 32)
[782] Fix | Delete
);
[783] Fix | Delete
[784] Fix | Delete
/** @var string $rcheck */
[785] Fix | Delete
$rcheck = ParagonIE_Sodium_Core_Ed25519::ge_tobytes($R);
[786] Fix | Delete
[787] Fix | Delete
// Close the file handle
[788] Fix | Delete
fclose($fp);
[789] Fix | Delete
[790] Fix | Delete
// Reset ParagonIE_Sodium_Compat::$fastMult to what it was before.
[791] Fix | Delete
ParagonIE_Sodium_Compat::$fastMult = $orig;
[792] Fix | Delete
return self::verify_32($rcheck, self::substr($sig, 0, 32));
[793] Fix | Delete
}
[794] Fix | Delete
[795] Fix | Delete
/**
[796] Fix | Delete
* @param resource $ifp
[797] Fix | Delete
* @param resource $ofp
[798] Fix | Delete
* @param int $mlen
[799] Fix | Delete
* @param string $nonce
[800] Fix | Delete
* @param string $boxKeypair
[801] Fix | Delete
* @return bool
[802] Fix | Delete
* @throws SodiumException
[803] Fix | Delete
* @throws TypeError
[804] Fix | Delete
*/
[805] Fix | Delete
protected static function box_encrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair)
[806] Fix | Delete
{
[807] Fix | Delete
if (PHP_INT_SIZE === 4) {
[808] Fix | Delete
return self::secretbox_encrypt(
[809] Fix | Delete
$ifp,
[810] Fix | Delete
$ofp,
[811] Fix | Delete
$mlen,
[812] Fix | Delete
$nonce,
[813] Fix | Delete
ParagonIE_Sodium_Crypto32::box_beforenm(
[814] Fix | Delete
ParagonIE_Sodium_Crypto32::box_secretkey($boxKeypair),
[815] Fix | Delete
ParagonIE_Sodium_Crypto32::box_publickey($boxKeypair)
[816] Fix | Delete
)
[817] Fix | Delete
);
[818] Fix | Delete
}
[819] Fix | Delete
return self::secretbox_encrypt(
[820] Fix | Delete
$ifp,
[821] Fix | Delete
$ofp,
[822] Fix | Delete
$mlen,
[823] Fix | Delete
$nonce,
[824] Fix | Delete
ParagonIE_Sodium_Crypto::box_beforenm(
[825] Fix | Delete
ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair),
[826] Fix | Delete
ParagonIE_Sodium_Crypto::box_publickey($boxKeypair)
[827] Fix | Delete
)
[828] Fix | Delete
);
[829] Fix | Delete
}
[830] Fix | Delete
[831] Fix | Delete
[832] Fix | Delete
/**
[833] Fix | Delete
* @param resource $ifp
[834] Fix | Delete
* @param resource $ofp
[835] Fix | Delete
* @param int $mlen
[836] Fix | Delete
* @param string $nonce
[837] Fix | Delete
* @param string $boxKeypair
[838] Fix | Delete
* @return bool
[839] Fix | Delete
* @throws SodiumException
[840] Fix | Delete
* @throws TypeError
[841] Fix | Delete
*/
[842] Fix | Delete
protected static function box_decrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair)
[843] Fix | Delete
{
[844] Fix | Delete
if (PHP_INT_SIZE === 4) {
[845] Fix | Delete
return self::secretbox_decrypt(
[846] Fix | Delete
$ifp,
[847] Fix | Delete
$ofp,
[848] Fix | Delete
$mlen,
[849] Fix | Delete
$nonce,
[850] Fix | Delete
ParagonIE_Sodium_Crypto32::box_beforenm(
[851] Fix | Delete
ParagonIE_Sodium_Crypto32::box_secretkey($boxKeypair),
[852] Fix | Delete
ParagonIE_Sodium_Crypto32::box_publickey($boxKeypair)
[853] Fix | Delete
)
[854] Fix | Delete
);
[855] Fix | Delete
}
[856] Fix | Delete
return self::secretbox_decrypt(
[857] Fix | Delete
$ifp,
[858] Fix | Delete
$ofp,
[859] Fix | Delete
$mlen,
[860] Fix | Delete
$nonce,
[861] Fix | Delete
ParagonIE_Sodium_Crypto::box_beforenm(
[862] Fix | Delete
ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair),
[863] Fix | Delete
ParagonIE_Sodium_Crypto::box_publickey($boxKeypair)
[864] Fix | Delete
)
[865] Fix | Delete
);
[866] Fix | Delete
}
[867] Fix | Delete
[868] Fix | Delete
/**
[869] Fix | Delete
* Encrypt a file
[870] Fix | Delete
*
[871] Fix | Delete
* @param resource $ifp
[872] Fix | Delete
* @param resource $ofp
[873] Fix | Delete
* @param int $mlen
[874] Fix | Delete
* @param string $nonce
[875] Fix | Delete
* @param string $key
[876] Fix | Delete
* @return bool
[877] Fix | Delete
* @throws SodiumException
[878] Fix | Delete
* @throws TypeError
[879] Fix | Delete
*/
[880] Fix | Delete
protected static function secretbox_encrypt($ifp, $ofp, $mlen, $nonce, $key)
[881] Fix | Delete
{
[882] Fix | Delete
if (PHP_INT_SIZE === 4) {
[883] Fix | Delete
return self::secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key);
[884] Fix | Delete
}
[885] Fix | Delete
[886] Fix | Delete
$plaintext = fread($ifp, 32);
[887] Fix | Delete
if (!is_string($plaintext)) {
[888] Fix | Delete
throw new SodiumException('Could not read input file');
[889] Fix | Delete
}
[890] Fix | Delete
$first32 = self::ftell($ifp);
[891] Fix | Delete
[892] Fix | Delete
/** @var string $subkey */
[893] Fix | Delete
$subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key);
[894] Fix | Delete
[895] Fix | Delete
/** @var string $realNonce */
[896] Fix | Delete
$realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8);
[897] Fix | Delete
[898] Fix | Delete
/** @var string $block0 */
[899] Fix | Delete
$block0 = str_repeat("\x00", 32);
[900] Fix | Delete
[901] Fix | Delete
/** @var int $mlen - Length of the plaintext message */
[902] Fix | Delete
$mlen0 = $mlen;
[903] Fix | Delete
if ($mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES) {
[904] Fix | Delete
$mlen0 = 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES;
[905] Fix | Delete
}
[906] Fix | Delete
$block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0);
[907] Fix | Delete
[908] Fix | Delete
/** @var string $block0 */
[909] Fix | Delete
$block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor(
[910] Fix | Delete
$block0,
[911] Fix | Delete
$realNonce,
[912] Fix | Delete
$subkey
[913] Fix | Delete
);
[914] Fix | Delete
[915] Fix | Delete
$state = new ParagonIE_Sodium_Core_Poly1305_State(
[916] Fix | Delete
ParagonIE_Sodium_Core_Util::substr(
[917] Fix | Delete
$block0,
[918] Fix | Delete
0,
[919] Fix | Delete
ParagonIE_Sodium_Crypto::onetimeauth_poly1305_KEYBYTES
[920] Fix | Delete
)
[921] Fix | Delete
);
[922] Fix | Delete
[923] Fix | Delete
// Pre-write 16 blank bytes for the Poly1305 tag
[924] Fix | Delete
$start = self::ftell($ofp);
[925] Fix | Delete
fwrite($ofp, str_repeat("\x00", 16));
[926] Fix | Delete
[927] Fix | Delete
/** @var string $c */
[928] Fix | Delete
$cBlock = ParagonIE_Sodium_Core_Util::substr(
[929] Fix | Delete
$block0,
[930] Fix | Delete
ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES
[931] Fix | Delete
);
[932] Fix | Delete
$state->update($cBlock);
[933] Fix | Delete
fwrite($ofp, $cBlock);
[934] Fix | Delete
$mlen -= 32;
[935] Fix | Delete
[936] Fix | Delete
/** @var int $iter */
[937] Fix | Delete
$iter = 1;
[938] Fix | Delete
[939] Fix | Delete
/** @var int $incr */
[940] Fix | Delete
$incr = self::BUFFER_SIZE >> 6;
[941] Fix | Delete
[942] Fix | Delete
/*
[943] Fix | Delete
* Set the cursor to the end of the first half-block. All future bytes will
[944] Fix | Delete
* generated from salsa20_xor_ic, starting from 1 (second block).
[945] Fix | Delete
*/
[946] Fix | Delete
fseek($ifp, $first32, SEEK_SET);
[947] Fix | Delete
[948] Fix | Delete
while ($mlen > 0) {
[949] Fix | Delete
$blockSize = $mlen > self::BUFFER_SIZE
[950] Fix | Delete
? self::BUFFER_SIZE
[951] Fix | Delete
: $mlen;
[952] Fix | Delete
$plaintext = fread($ifp, $blockSize);
[953] Fix | Delete
if (!is_string($plaintext)) {
[954] Fix | Delete
throw new SodiumException('Could not read input file');
[955] Fix | Delete
}
[956] Fix | Delete
$cBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic(
[957] Fix | Delete
$plaintext,
[958] Fix | Delete
$realNonce,
[959] Fix | Delete
$iter,
[960] Fix | Delete
$subkey
[961] Fix | Delete
);
[962] Fix | Delete
fwrite($ofp, $cBlock, $blockSize);
[963] Fix | Delete
$state->update($cBlock);
[964] Fix | Delete
[965] Fix | Delete
$mlen -= $blockSize;
[966] Fix | Delete
$iter += $incr;
[967] Fix | Delete
}
[968] Fix | Delete
try {
[969] Fix | Delete
ParagonIE_Sodium_Compat::memzero($block0);
[970] Fix | Delete
ParagonIE_Sodium_Compat::memzero($subkey);
[971] Fix | Delete
} catch (SodiumException $ex) {
[972] Fix | Delete
$block0 = null;
[973] Fix | Delete
$subkey = null;
[974] Fix | Delete
}
[975] Fix | Delete
$end = self::ftell($ofp);
[976] Fix | Delete
[977] Fix | Delete
/*
[978] Fix | Delete
* Write the Poly1305 authentication tag that provides integrity
[979] Fix | Delete
* over the ciphertext (encrypt-then-MAC)
[980] Fix | Delete
*/
[981] Fix | Delete
fseek($ofp, $start, SEEK_SET);
[982] Fix | Delete
fwrite($ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES);
[983] Fix | Delete
fseek($ofp, $end, SEEK_SET);
[984] Fix | Delete
unset($state);
[985] Fix | Delete
[986] Fix | Delete
return true;
[987] Fix | Delete
}
[988] Fix | Delete
[989] Fix | Delete
/**
[990] Fix | Delete
* Decrypt a file
[991] Fix | Delete
*
[992] Fix | Delete
* @param resource $ifp
[993] Fix | Delete
* @param resource $ofp
[994] Fix | Delete
* @param int $mlen
[995] Fix | Delete
* @param string $nonce
[996] Fix | Delete
* @param string $key
[997] Fix | Delete
* @return bool
[998] Fix | Delete
* @throws SodiumException
[999] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function