From 88d6517d9326e34f54de1a9d578ab40562919dba Mon Sep 17 00:00:00 2001 From: terrafrost Date: Tue, 5 Aug 2014 23:27:11 -0500 Subject: [PATCH 1/5] Hash: add sha256-96 and sha512-96 IPSec uses them and some versions of OpenSSH use them (even though RFC6668 makes no mention of them) --- phpseclib/Crypt/Hash.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/phpseclib/Crypt/Hash.php b/phpseclib/Crypt/Hash.php index 8123d33b..ea7824d7 100644 --- a/phpseclib/Crypt/Hash.php +++ b/phpseclib/Crypt/Hash.php @@ -5,7 +5,7 @@ * * Uses hash() or mhash() if available and an internal implementation, otherwise. Currently supports the following: * - * md2, md5, md5-96, sha1, sha1-96, sha256, sha384, and sha512 + * md2, md5, md5-96, sha1, sha1-96, sha256, sha256-96, sha384, and sha512, sha512-96 * * If {@link Crypt_Hash::setKey() setKey()} is called, {@link Crypt_Hash::hash() hash()} will return the HMAC as opposed to * the hash. If no valid algorithm is provided, sha1 will be used. @@ -207,6 +207,9 @@ class Crypt_Hash switch ($hash) { case 'md5-96': case 'sha1-96': + case 'sha256-96': + case 'sha512-96': + $hash = substr($hash, 0, -3); $this->l = 12; // 96 / 8 = 12 break; case 'md2': @@ -243,14 +246,12 @@ class Crypt_Hash case CRYPT_HASH_MODE_MHASH: switch ($hash) { case 'md5': - case 'md5-96': $this->hash = MHASH_MD5; break; case 'sha256': $this->hash = MHASH_SHA256; break; case 'sha1': - case 'sha1-96': default: $this->hash = MHASH_SHA1; } @@ -258,7 +259,6 @@ class Crypt_Hash case CRYPT_HASH_MODE_HASH: switch ($hash) { case 'md5': - case 'md5-96': $this->hash = 'md5'; return; case 'md2': @@ -268,7 +268,6 @@ class Crypt_Hash $this->hash = $hash; return; case 'sha1': - case 'sha1-96': default: $this->hash = 'sha1'; } @@ -281,7 +280,6 @@ class Crypt_Hash $this->hash = array($this, '_md2'); break; case 'md5': - case 'md5-96': $this->b = 64; $this->hash = array($this, '_md5'); break; @@ -295,7 +293,6 @@ class Crypt_Hash $this->hash = array($this, '_sha512'); break; case 'sha1': - case 'sha1-96': default: $this->b = 64; $this->hash = array($this, '_sha1'); From e3136ccd131cd71da528deff70afc84b0540f246 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 10 Aug 2014 02:01:21 -0500 Subject: [PATCH 2/5] Hash: add unit tests --- tests/Unit/Crypt/Hash/SHA256Test.php | 79 +++++++++++++++++++++++++ tests/Unit/Crypt/Hash/SHA256_96Test.php | 32 ++++++++++ tests/Unit/Crypt/Hash/SHA512_96Test.php | 32 ++++++++++ 3 files changed, 143 insertions(+) create mode 100644 tests/Unit/Crypt/Hash/SHA256Test.php create mode 100644 tests/Unit/Crypt/Hash/SHA256_96Test.php create mode 100644 tests/Unit/Crypt/Hash/SHA512_96Test.php diff --git a/tests/Unit/Crypt/Hash/SHA256Test.php b/tests/Unit/Crypt/Hash/SHA256Test.php new file mode 100644 index 00000000..61b54802 --- /dev/null +++ b/tests/Unit/Crypt/Hash/SHA256Test.php @@ -0,0 +1,79 @@ + + * @copyright MMXIV Andreas Fischer + * @license http://www.opensource.org/licenses/mit-license.html MIT License + */ + +class Unit_Crypt_Hash_SHA256Test extends Unit_Crypt_Hash_TestCase +{ + public function getInstance() + { + return new Crypt_Hash('sha256'); + } + + /** + * @dataProvider hashData() + */ + public function testHash($message, $result) + { + $this->assertHashesTo($this->getInstance(), $message, $result); + } + + static public function hashData() + { + return array( + array( + '', + 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855' + ), + array( + 'The quick brown fox jumps over the lazy dog', + 'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592', + ), + array( + 'The quick brown fox jumps over the lazy dog.', + 'ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c', + ), + ); + } + + /** + * @dataProvider hmacData() + */ + public function testHMAC($key, $message, $result) + { + $this->assertHMACsTo($this->getInstance(), $key, $message, $result); + } + + static public function hmacData() + { + return array( + // RFC 4231 + // Test Case 1 + array( + pack('H*', '0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b'), + pack('H*', '4869205468657265'), + 'b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7', + ), + // Test Case 2 + array( + pack('H*', '4a656665'), + pack('H*', '7768617420646f2079612077616e7420666f72206e6f7468696e673f'), + '5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843', + ), + // Test Case 3 + array( + pack('H*', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'), + pack('H*', 'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd'), + '773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe', + ), + // Test Case 4 + array( + pack('H*', '0102030405060708090a0b0c0d0e0f10111213141516171819'), + pack('H*', 'cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd'), + '82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b', + ), + ); + } +} diff --git a/tests/Unit/Crypt/Hash/SHA256_96Test.php b/tests/Unit/Crypt/Hash/SHA256_96Test.php new file mode 100644 index 00000000..ee635eb1 --- /dev/null +++ b/tests/Unit/Crypt/Hash/SHA256_96Test.php @@ -0,0 +1,32 @@ + + * @copyright MMXIV Andreas Fischer + * @license http://www.opensource.org/licenses/mit-license.html MIT License + */ + +class Unit_Crypt_Hash_SHA256_96Test extends Unit_Crypt_Hash_SHA256Test +{ + public function getInstance() + { + return new Crypt_Hash('sha256-96'); + } + + static public function hashData() + { + $tests = parent::hashData(); + foreach ($tests as &$test) { + $test[1] = substr($test[1], 0, 24); + } + return $tests; + } + + static public function hmacData() + { + $tests = parent::hashData(); + foreach ($tests as &$test) { + $test[3] = substr($test[3], 0, 24); + } + return $tests; + } +} diff --git a/tests/Unit/Crypt/Hash/SHA512_96Test.php b/tests/Unit/Crypt/Hash/SHA512_96Test.php new file mode 100644 index 00000000..c7fa8e93 --- /dev/null +++ b/tests/Unit/Crypt/Hash/SHA512_96Test.php @@ -0,0 +1,32 @@ + + * @copyright MMXIV Andreas Fischer + * @license http://www.opensource.org/licenses/mit-license.html MIT License + */ + +class Unit_Crypt_Hash_SHA512_96Test extends Unit_Crypt_Hash_SHA512Test +{ + public function getInstance() + { + return new Crypt_Hash('sha512-96'); + } + + static public function hashData() + { + $tests = parent::hashData(); + foreach ($tests as &$test) { + $test[1] = substr($test[1], 0, 24); + } + return $tests; + } + + static public function hmacData() + { + $tests = parent::hashData(); + foreach ($tests as &$test) { + $test[3] = substr($test[3], 0, 24); + } + return $tests; + } +} From a906e83f2a24e183af56b2f6f18d1585a805d032 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 10 Aug 2014 02:07:54 -0500 Subject: [PATCH 3/5] Hash: fix unit tests --- tests/Unit/Crypt/Hash/SHA256_96Test.php | 2 +- tests/Unit/Crypt/Hash/SHA512_96Test.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Unit/Crypt/Hash/SHA256_96Test.php b/tests/Unit/Crypt/Hash/SHA256_96Test.php index ee635eb1..303ed7aa 100644 --- a/tests/Unit/Crypt/Hash/SHA256_96Test.php +++ b/tests/Unit/Crypt/Hash/SHA256_96Test.php @@ -25,7 +25,7 @@ class Unit_Crypt_Hash_SHA256_96Test extends Unit_Crypt_Hash_SHA256Test { $tests = parent::hashData(); foreach ($tests as &$test) { - $test[3] = substr($test[3], 0, 24); + $test[3] = substr($test[2], 0, 24); } return $tests; } diff --git a/tests/Unit/Crypt/Hash/SHA512_96Test.php b/tests/Unit/Crypt/Hash/SHA512_96Test.php index c7fa8e93..85159662 100644 --- a/tests/Unit/Crypt/Hash/SHA512_96Test.php +++ b/tests/Unit/Crypt/Hash/SHA512_96Test.php @@ -25,7 +25,7 @@ class Unit_Crypt_Hash_SHA512_96Test extends Unit_Crypt_Hash_SHA512Test { $tests = parent::hashData(); foreach ($tests as &$test) { - $test[3] = substr($test[3], 0, 24); + $test[3] = substr($test[2], 0, 24); } return $tests; } From a52315a0cb634a858f4c55d66a6a80320943d393 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 10 Aug 2014 09:08:52 -0500 Subject: [PATCH 4/5] Hash: one more fix to unit test --- tests/Unit/Crypt/Hash/SHA256_96Test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Crypt/Hash/SHA256_96Test.php b/tests/Unit/Crypt/Hash/SHA256_96Test.php index 303ed7aa..8fd1e82d 100644 --- a/tests/Unit/Crypt/Hash/SHA256_96Test.php +++ b/tests/Unit/Crypt/Hash/SHA256_96Test.php @@ -25,7 +25,7 @@ class Unit_Crypt_Hash_SHA256_96Test extends Unit_Crypt_Hash_SHA256Test { $tests = parent::hashData(); foreach ($tests as &$test) { - $test[3] = substr($test[2], 0, 24); + $test[2] = substr($test[2], 0, 24); } return $tests; } From 4fa96e384ccca915150862700b4100d3e6b1d048 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 10 Aug 2014 09:14:33 -0500 Subject: [PATCH 5/5] Hash: apply fixes to sha512 unit tests as well --- tests/Unit/Crypt/Hash/SHA256_96Test.php | 2 +- tests/Unit/Crypt/Hash/SHA512_96Test.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Unit/Crypt/Hash/SHA256_96Test.php b/tests/Unit/Crypt/Hash/SHA256_96Test.php index 8fd1e82d..14029faf 100644 --- a/tests/Unit/Crypt/Hash/SHA256_96Test.php +++ b/tests/Unit/Crypt/Hash/SHA256_96Test.php @@ -23,7 +23,7 @@ class Unit_Crypt_Hash_SHA256_96Test extends Unit_Crypt_Hash_SHA256Test static public function hmacData() { - $tests = parent::hashData(); + $tests = parent::hmacData(); foreach ($tests as &$test) { $test[2] = substr($test[2], 0, 24); } diff --git a/tests/Unit/Crypt/Hash/SHA512_96Test.php b/tests/Unit/Crypt/Hash/SHA512_96Test.php index 85159662..d3f7721c 100644 --- a/tests/Unit/Crypt/Hash/SHA512_96Test.php +++ b/tests/Unit/Crypt/Hash/SHA512_96Test.php @@ -23,9 +23,9 @@ class Unit_Crypt_Hash_SHA512_96Test extends Unit_Crypt_Hash_SHA512Test static public function hmacData() { - $tests = parent::hashData(); + $tests = parent::hmacData(); foreach ($tests as &$test) { - $test[3] = substr($test[2], 0, 24); + $test[2] = substr($test[2], 0, 24); } return $tests; }