From 5d43302c4e1203ef125dc5b39f0bfe80d734cc3a Mon Sep 17 00:00:00 2001 From: Paragon Initiative Enterprises Date: Wed, 29 Aug 2018 16:44:35 -0400 Subject: [PATCH 1/2] Use the 32-bit implementation in ParagonIE_Sodium_File::box_{en,de}crypt() --- src/File.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/File.php b/src/File.php index d5e4dddb..ffb15ef0 100644 --- a/src/File.php +++ b/src/File.php @@ -761,6 +761,18 @@ public static function verify($sig, $filePath, $publicKey) */ protected static function box_encrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) { + if (PHP_INT_SIZE === 4) { + return self::secretbox_encrypt( + $ifp, + $ofp, + $mlen, + $nonce, + ParagonIE_Sodium_Crypto32::box_beforenm( + ParagonIE_Sodium_Crypto32::box_secretkey($boxKeypair), + ParagonIE_Sodium_Crypto32::box_publickey($boxKeypair) + ) + ); + } return self::secretbox_encrypt( $ifp, $ofp, @@ -786,6 +798,18 @@ protected static function box_encrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) */ protected static function box_decrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) { + if (PHP_INT_SIZE === 4) { + return self::secretbox_decrypt( + $ifp, + $ofp, + $mlen, + $nonce, + ParagonIE_Sodium_Crypto32::box_beforenm( + ParagonIE_Sodium_Crypto32::box_secretkey($boxKeypair), + ParagonIE_Sodium_Crypto32::box_publickey($boxKeypair) + ) + ); + } return self::secretbox_decrypt( $ifp, $ofp, From 613c14fc6c55651a6578482292160159cba75b3d Mon Sep 17 00:00:00 2001 From: Paragon Initiative Enterprises Date: Wed, 29 Aug 2018 17:54:05 -0400 Subject: [PATCH 2/2] Add tests to Windows32Test --- tests/Windows32Test.php | 82 +++++++++++++++++++++++++++++++++++++++++ tests/windows-test.bat | 2 +- 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/tests/Windows32Test.php b/tests/Windows32Test.php index dde615ba..5392d2ca 100644 --- a/tests/Windows32Test.php +++ b/tests/Windows32Test.php @@ -261,6 +261,88 @@ public function testCryptoBox32() ); } + + /** + * @covers ParagonIE_Sodium_File::box() + * @covers ParagonIE_Sodium_File::box_open() + * @throws SodiumException + * @throws TypeError + * @throws Exception + */ + public function testFileBox() + { + $randomSeed = random_bytes(32); + $randomNonce = random_bytes(24); + $orig = ParagonIE_Sodium_Compat::$fastMult; + $pseudoRandom = ParagonIE_Sodium_Compat::crypto_stream( + 32, // random_int(1 << 9, 1 << 17), + $randomNonce, + $randomSeed + ); + $shortMsg = 'lessthan32bytes'; + file_put_contents('plaintext-box.data', $pseudoRandom); + file_put_contents('plaintext-box.data2', $shortMsg); + + $alice_secret = ParagonIE_Sodium_Core_Util::hex2bin( + '69f208412d8dd5db9d0c6d18512e86f0ec75665ab841372d57b042b27ef89d8c' + ); + $bob_public = ParagonIE_Sodium_Core_Util::hex2bin( + 'e8980c86e032f1eb2975052e8d65bddd15c3b59641174ec9678a53789d92c754' + ); + + $kp = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey($alice_secret, $bob_public); + + $raw = ParagonIE_Sodium_Compat::crypto_box( + $pseudoRandom, + $randomNonce, + $kp + ); + ParagonIE_Sodium_File::box('plaintext-box.data', 'ciphertext-box.data', $randomNonce, $kp); + $file = file_get_contents('ciphertext-box.data'); + + $this->assertSame(bin2hex($raw), bin2hex($file)); + + // Also verify decryption works. + $plain = ParagonIE_Sodium_Compat::crypto_box_open( + $file, + $randomNonce, + $kp + ); + $this->assertSame(bin2hex($pseudoRandom), bin2hex($plain)); + + ParagonIE_Sodium_File::box_open('ciphertext-box.data', 'plaintext-box2.data', $randomNonce, $kp); + $opened = file_get_contents('plaintext-box2.data'); + $this->assertSame(bin2hex($pseudoRandom), bin2hex($opened)); + + $raw = ParagonIE_Sodium_Compat::crypto_box( + $shortMsg, + $randomNonce, + $kp + ); + ParagonIE_Sodium_File::box('plaintext-box.data2', 'ciphertext-box.data2', $randomNonce, $kp); + $file = file_get_contents('ciphertext-box.data2'); + $this->assertSame(bin2hex($raw), bin2hex($file)); + + // Also verify decryption works. + $plain = ParagonIE_Sodium_Compat::crypto_box_open( + $file, + $randomNonce, + $kp + ); + $this->assertSame(bin2hex($shortMsg), bin2hex($plain)); + + ParagonIE_Sodium_File::box_open('ciphertext-box.data2', 'plaintext-box2.data', $randomNonce, $kp); + $opened = file_get_contents('plaintext-box2.data'); + $this->assertSame(bin2hex($shortMsg), bin2hex($opened)); + + ParagonIE_Sodium_Compat::$fastMult = $orig; + unlink('ciphertext-box.data'); + unlink('ciphertext-box.data2'); + unlink('plaintext-box.data'); + unlink('plaintext-box2.data'); + unlink('plaintext-box.data2'); + } + /** * @covers ParagonIE_Sodium_Compat::crypto_box_seal() * @covers ParagonIE_Sodium_Compat::crypto_box_seal_open() diff --git a/tests/windows-test.bat b/tests/windows-test.bat index 1e2839fc..65fa6e98 100644 --- a/tests/windows-test.bat +++ b/tests/windows-test.bat @@ -1,3 +1,3 @@ @echo off REM "This assumes C:\\php\\5.6-x64\\php.exe is the correct path to php.exe" -\php\5.6-x64\php.exe ..\vendor\phpunit\phpunit\phpunit -c ..\phpunit.xml.dist unit/CryptoTest +\php\5.6-x64\php.exe ..\vendor\phpunit\phpunit\phpunit -c ..\phpunit.xml.dist Windows32Test