ParagonIE_Sodium_File::box_seal_open( string $inputFile, string $outputFile, string $ecdhKeypair ): bool
- Source
wp-includes/sodium_compat/src/File.php:288
Description
Warning: Does not protect against TOCTOU attacks. You should just load the file into memory and use crypto_box_seal_open() if you are worried about those.
Compatibility
- WordPress
- core
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$inputFilestring$outputFilestring$ecdhKeypairstring
Return value
bool
Performance profile
How much work a call to ParagonIE_Sodium_File::box_seal_open() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 12–95
- Plugin surface
- None
- Called by
- 0
Reads or writes the filesystem via file_exists().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 158.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- filesystemfilesystem access
file_exists()called directly
What one call costs · 9 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost ParagonIE_Sodium_File::box_seal_open() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 12–16 | none |
is_string($inputFile) && is_string($outputFile) && is_string($ecdhKeypair) | 19 | ::strlen() |
is_string($inputFile) && is_string($outputFile) && is_string($ecdhKeypair) && !file_exists() | 27 | ::strlen(), ::ParagonIE_Sodium_Compat(), file_exists() |
is_string($inputFile) && is_string($outputFile) && is_string($ecdhKeypair) && file_exists() && !is_int($size) | 33 | ::strlen(), ::ParagonIE_Sodium_Compat(), file_exists(), filesize() |
is_string($inputFile) && is_string($outputFile) && is_string($ecdhKeypair) && file_exists() && is_int($size) && !is_resource($ifp) | 40 | ::strlen(), ::ParagonIE_Sodium_Compat(), file_exists(), filesize(), fopen() |
is_string($inputFile) && is_string($outputFile) && is_string($ecdhKeypair) && file_exists() && is_int($size) && is_resource($ifp) && !is_resource($ofp) | 52 | ::strlen(), ::ParagonIE_Sodium_Compat(), file_exists(), filesize(), fopen(), fopen(), fclose() |
is_string($inputFile) && is_string($outputFile) && is_string($ecdhKeypair) && file_exists() && is_int($size) && is_resource($ifp) && is_resource($ofp) && !is_string($ephemeralPK) | 57 | ::strlen(), ::ParagonIE_Sodium_Compat(), file_exists(), filesize(), fopen(), fopen(), fread() |
is_string($inputFile) && is_string($outputFile) && is_string($ecdhKeypair) && file_exists() && is_int($size) && is_resource($ifp) && is_resource($ofp) && is_string($ephemeralPK) | 69 | ::strlen(), ::ParagonIE_Sodium_Compat(), file_exists(), filesize(), fopen(), fopen(), fread(), ::strlen(), fclose(), fclose() |
is_string($inputFile) && is_string($outputFile) && is_string($ecdhKeypair) && file_exists() && is_int($size) && is_resource($ifp) && is_resource($ofp) && is_string($ephemeralPK) | 95 | ::strlen(), ::ParagonIE_Sodium_Compat(), file_exists(), filesize(), fopen(), fopen(), fread(), ::strlen(), ::ParagonIE_Sodium_Compat(), ::ParagonIE_Sodium_Compat(), ::ParagonIE_Sodium_Compat(), ::box_decrypt(), fclose(), fclose(), ::ParagonIE_Sodium_Compat(), ::ParagonIE_Sodium_Compat() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 158 instructions, 12–95 executed per call, 11 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 9
- TypeError::__construct()
- ParagonIE_Sodium_File::strlen()
- ParagonIE_Sodium_Compat::crypto_box_publickey()Extract the public key from a crypto_box keypair.
- SodiumException::__construct()
- ParagonIE_Sodium_Compat::crypto_generichash()Calculates a BLAKE2b hash, with an optional key.
- ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey()Combine two keys into a keypair for use in library methods that expect a keypair. This doesn't necessarily have to be the same person's keys.
- ParagonIE_Sodium_Compat::crypto_box_secretkey()Extract the secret key from a crypto_box keypair.
- ParagonIE_Sodium_File::box_decrypt()
- ParagonIE_Sodium_Compat::memzero()It's actually not possible to zero memory buffers in PHP. You need the native library for that.
Source code
public static function box_seal_open( $inputFile, $outputFile, #[\SensitiveParameter] $ecdhKeypair ) { /* Type checks: */ if (!is_string($inputFile)) { throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); } if (!is_string($outputFile)) { throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); } if (!is_string($ecdhKeypair)) { throw new TypeError('Argument 3 must be a string, ' . gettype($ecdhKeypair) . ' given.'); } /* Input validation: */ if (self::strlen($ecdhKeypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { throw new TypeError('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); } $publicKey = ParagonIE_Sodium_Compat::crypto_box_publickey($ecdhKeypair); if (!file_exists($inputFile)) { throw new SodiumException('Input file does not exist'); } /** @var int $size */ $size = filesize($inputFile); if (!is_int($size)) { throw new SodiumException('Could not obtain the file size'); } /** @var resource $ifp */ $ifp = fopen($inputFile, 'rb'); if (!is_resource($ifp)) { throw new SodiumException('Could not open input file for reading'); } /** @var resource $ofp */ $ofp = @fopen($outputFile, 'wb'); if (!is_resource($ofp)) { fclose($ifp); throw new SodiumException('Could not open output file for writing'); } $ephemeralPK = fread($ifp, ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES); if (!is_string($ephemeralPK)) { throw new SodiumException('Could not read input file'); } if (self::strlen($ephemeralPK) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { fclose($ifp); fclose($ofp); throw new SodiumException('Could not read public key from sealed file'); } $nonce = ParagonIE_Sodium_Compat::crypto_generichash( $ephemeralPK . $publicKey, '', 24 ); $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey( ParagonIE_Sodium_Compat::crypto_box_secretkey($ecdhKeypair), $ephemeralPK ); $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $msgKeypair); fclose($ifp); fclose($ofp); try { ParagonIE_Sodium_Compat::memzero($nonce); ParagonIE_Sodium_Compat::memzero($ephKeypair); } catch (SodiumException $ex) { if (isset($ephKeypair)) { unset($ephKeypair); } } return $res; }Changelog
Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/sodium_compat/src/File.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.