From 2e02976724631c3f9ff7a15fc005bb3db4af9a4e Mon Sep 17 00:00:00 2001 From: Paragon Initiative Enterprises Date: Thu, 19 Jan 2017 15:10:10 -0500 Subject: [PATCH 1/2] Streaming Ed25519 verification. --- src/Core/Ed25519.php | 2 +- src/File.php | 151 ++++++++++++++++++++++++++++++++++++++++ tests/unit/FileTest.php | 28 ++++++++ 3 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 src/File.php create mode 100644 tests/unit/FileTest.php diff --git a/src/Core/Ed25519.php b/src/Core/Ed25519.php index 3bcef900..091e6059 100644 --- a/src/Core/Ed25519.php +++ b/src/Core/Ed25519.php @@ -202,7 +202,7 @@ public static function verify_detached($sig, $message, $pk) $d |= self::chrToInt($pk[$i]); } if ($d === 0) { - throw new \Exception('All zero public key'); + throw new Exception('All zero public key'); } $hDigest = hash('sha512', self::substr($sig, 0, 32) . $pk . $message, true); diff --git a/src/File.php b/src/File.php new file mode 100644 index 00000000..3c01a558 --- /dev/null +++ b/src/File.php @@ -0,0 +1,151 @@ + 8192) + ? $size - ($i * 8192) + : 8192 + ); + hash_update($hash, $message); + } + fseek($fp, 0, SEEK_SET); + return $hash; + } +} diff --git a/tests/unit/FileTest.php b/tests/unit/FileTest.php new file mode 100644 index 00000000..1a5369e5 --- /dev/null +++ b/tests/unit/FileTest.php @@ -0,0 +1,28 @@ +assertSame(bin2hex($signed), bin2hex($stored)); + + $this->assertTrue(ParagonIE_Sodium_File::verify_file($signed, 'random.data', $sign_pk)); + unlink('random.data'); + } +} From 1e301ef48d5cfe248b888aed73a0f7739958a928 Mon Sep 17 00:00:00 2001 From: Paragon Initiative Enterprises Date: Thu, 19 Jan 2017 15:15:36 -0500 Subject: [PATCH 2/2] Address psalm errors. --- src/File.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/File.php b/src/File.php index 3c01a558..71e37941 100644 --- a/src/File.php +++ b/src/File.php @@ -15,10 +15,10 @@ class ParagonIE_Sodium_File extends ParagonIE_Sodium_Core_Util public static function sign_file($filePath, $secretKey) { $fp = fopen($filePath, 'rb'); - if ($fp === false) { + $size = filesize($filePath); + if ($size === false || !is_resource($fp)) { throw new Error('Could not open file for reading'); } - $size = filesize($filePath); # crypto_hash_sha512(az, sk, 32); $az = hash('sha512', ParagonIE_Sodium_Core_Ed25519::substr($secretKey, 0, 32), true); @@ -85,10 +85,10 @@ public static function sign_file($filePath, $secretKey) public static function verify_file($sig, $filePath, $publicKey) { $fp = fopen($filePath, 'rb'); - if ($fp === false) { + $size = filesize($filePath); + if ($size === false || !is_resource($fp)) { throw new Error('Could not open file for reading'); } - $size = filesize($filePath); if (self::strlen($sig) < 64) { throw new Exception('Signature is too short'); }