From 8b8d9ac42323d186aff3bf312f001f2b42d21248 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Mon, 26 Jan 2015 19:24:48 -0600 Subject: [PATCH] Tests/RC2: add test vectors --- tests/Unit/Crypt/RC2Test.php | 46 +++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/tests/Unit/Crypt/RC2Test.php b/tests/Unit/Crypt/RC2Test.php index fb70ce31..adcb592d 100644 --- a/tests/Unit/Crypt/RC2Test.php +++ b/tests/Unit/Crypt/RC2Test.php @@ -7,9 +7,38 @@ require_once 'Crypt/RC2.php'; -// this test is just confirming RC2's key expansion class Unit_Crypt_RC2Test extends PhpseclibTestCase { + var $engines = array( + CRYPT_ENGINE_INTERNAL => 'internal', + CRYPT_ENGINE_MCRYPT => 'mcrypt', + CRYPT_ENGINE_OPENSSL => 'OpenSSL', + ); + + public function engineVectors() + { + // tests from https://tools.ietf.org/html/rfc2268#page-8 + $tests = array( + // key, effective key length, plaintext, ciphertext + array('0000000000000000', 63, '0000000000000000', 'ebb773f993278eff'), + array('ffffffffffffffff', 64, 'ffffffffffffffff', '278b27e42e2f0d49'), + array('3000000000000000', 64, '1000000000000001', '30649edf9be7d2c2'), + array('88', 64, '0000000000000000', '61a8a244adacccf0'), + array('88bca90e90875a', 64, '0000000000000000', '6ccf4308974c267f'), + array('88bca90e90875a7f0f79c384627bafb2', 64, '0000000000000000', '1a807d272bbe5db1'), + array('88bca90e90875a7f0f79c384627bafb2', 128, '0000000000000000', '2269552ab0f85ca6'), + array('88bca90e90875a7f0f79c384627bafb216f80a6f85920584c42fceb0 be255daf1e', 129, '0000000000000000', '5b78d3a43dfff1f1') + ); + $result = array(); + // @codingStandardsIgnoreStart + foreach ($this->engines as $engine => $engineName) + foreach ($tests as $test) + $result[] = array($engine, $engineName, $test[0], $test[1], $test[2], $test[3]); + // @codingStandardsIgnoreEnd + return $result; + } + + // this test is just confirming RC2's key expansion public function testEncryptPadding() { $rc2 = new Crypt_RC2(CRYPT_MODE_ECB); @@ -71,4 +100,19 @@ class Unit_Crypt_RC2Test extends PhpseclibTestCase self::markTestSkipped('Unable to initialize OpenSSL engine'); } } + + public function testVectors($engine, $engineName, $key, $keyLen, $plaintext, $ciphertext) + { + $rc2 = new Crypt_RC2(); + $rc2->disablePadding(); + $rc2->setKeyLength($keyLen); + $rc2->setKey(pack('H*', $key)); // could also do $rc2->setKey(pack('H*', $key), $keyLen) + if (!$rc2->isValidEngine($engine)) { + self::markTestSkipped('Unable to initialize ' . $engineName . ' engine'); + } + $rc2->setPreferredEngine($engine); + + $result = bin2hex($rc2->encrypt(pack('H*', $plaintext))); + $this->assertEquals($result, $ciphertext, "Failed asserting that $plaintext yielded expected output in $engineName engine"); + } }