Skip to content

Commit

Permalink
Merge pull request #76 from paragonie/32bit-fixes
Browse files Browse the repository at this point in the history
Fix the 32-bit implementation
  • Loading branch information
paragonie-scott authored Aug 29, 2018
2 parents bbb7fac + 613c14f commit 3f2fd07
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
82 changes: 82 additions & 0 deletions tests/Windows32Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/windows-test.bat
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3f2fd07

Please sign in to comment.