From 912831f589862c205d5b9837b710aa391460e08d Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 4 Feb 2014 17:21:37 +0200 Subject: CI_Encryption: Fix some errors and add unit tests for hkdf() --- tests/codeigniter/libraries/Encryption_test.php | 81 +++++++++++++++++++++++++ tests/mocks/autoloader.php | 53 +++++++++++----- 2 files changed, 119 insertions(+), 15 deletions(-) create mode 100644 tests/codeigniter/libraries/Encryption_test.php (limited to 'tests') diff --git a/tests/codeigniter/libraries/Encryption_test.php b/tests/codeigniter/libraries/Encryption_test.php new file mode 100644 index 000000000..94a952677 --- /dev/null +++ b/tests/codeigniter/libraries/Encryption_test.php @@ -0,0 +1,81 @@ +ci_set_config('encryption_key', "Encryptin'glike@boss!"); + $this->encryption = new CI_Encryption(); + $this->ci_instance_var('encryption', $this->encryption); + } + + // -------------------------------------------------------------------- + + public function test_hkdf() + { + // Test vectors are described in RFC5869, Appendix A(1-3). + // Vectors 4-7 cover SHA-1, which we don't support. + // + // URL: https://tools.ietf.org/rfc/rfc5869.txt + // + // Our implementation doesn't split into hkdf_extract(), hkdf_expand() + // and therefore we can't test for the PRK value (it is included below + // just for consistency, hence why it's also commented out). + // + // As long as OKM is correct, then we're all fine though. + $vectors = array( + // Appendix A.1: Basic test case with SHA-256 + array( + 'digest' => 'sha256', + 'ikm' => "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", + 'salt' => "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c", + 'length' => 42, + 'info' => "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9", +// 'prk' => "\x07\x77\x09\x36\x2c\x2e\x32\xdf\x0d\xdc\x3f\x0d\xc4\x7b\xba\x63\x90\xb6\xc7\x3b\xb5\x0f\x9c\x31\x22\xec\x84\x4a\xd7\xc2\xb3\xe5", + 'okm' => "\x3c\xb2\x5f\x25\xfa\xac\xd5\x7a\x90\x43\x4f\x64\xd0\x36\x2f\x2a\x2d\x2d\x0a\x90\xcf\x1a\x5a\x4c\x5d\xb0\x2d\x56\xec\xc4\xc5\xbf\x34\x00\x72\x08\xd5\xb8\x87\x18\x58\x65" + ), + // Appendix A.2: Test with SHA-256 and longer inputs/outputs + array( + 'digest' => 'sha256', + 'ikm' => "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f", + 'salt' => "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf", + 'length' => 82, + 'info' => "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff", +// 'prk' => "\x06\xa6\xb8\x8c\x58\x53\x36\x1a\x06\x10\x4c\x9c\xeb\x35\xb4\x5c\xef\x76\x00\x14\x90\x46\x71\x01\x4a\x19\x3f\x40\xc1\x5f\xc2\x44", + 'okm' => "\xb1\x1e\x39\x8d\xc8\x03\x27\xa1\xc8\xe7\xf7\x8c\x59\x6a\x49\x34\x4f\x01\x2e\xda\x2d\x4e\xfa\xd8\xa0\x50\xcc\x4c\x19\xaf\xa9\x7c\x59\x04\x5a\x99\xca\xc7\x82\x72\x71\xcb\x41\xc6\x5e\x59\x0e\x09\xda\x32\x75\x60\x0c\x2f\x09\xb8\x36\x77\x93\xa9\xac\xa3\xdb\x71\xcc\x30\xc5\x81\x79\xec\x3e\x87\xc1\x4c\x01\xd5\xc1\xf3\x43\x4f\x1d\x87", + ), + // Appendix A.3: Test with SHA-256 and zero-length salt/info + array( + 'digest' => 'sha256', + 'ikm' => "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", + 'salt' => "", + 'length' => 42, + 'info' => "", +// 'prk' => "\x19\xef\x24\xa3\x2c\x71\x7b\x16\x7f\x33\xa9\x1d\x6f\x64\x8b\xdf\x96\x59\x67\x76\xaf\xdb\x63\x77\xac\x43\x4c\x1c\x29\x3c\xcb\x04", + 'okm' => "\x8d\xa4\xe7\x75\xa5\x63\xc1\x8f\x71\x5f\x80\x2a\x06\x3c\x5a\x31\xb8\xa1\x1f\x5c\x5e\xe1\x87\x9e\xc3\x45\x4e\x5f\x3c\x73\x8d\x2d\x9d\x20\x13\x95\xfa\xa4\xb6\x1a\x96\xc8", + ) + ); + + foreach ($vectors as $test) + { + $this->assertEquals( + $test['okm'], + $this->encryption->hkdf( + $test['ikm'], + $test['digest'], + $test['salt'], + $test['length'], + $test['info'] + ) + ); + } + + // Test default length, it must match the digest size + $this->assertEquals(64, strlen($this->encryption->hkdf('foobar', 'sha512'))); + + // Test maximum length (RFC5869 says that it must be up to 255 times the digest size) + $this->assertEquals(12240, strlen($this->encryption->hkdf('foobar', 'sha384', NULL, 48 * 255))); + $this->assertFalse($this->encryption->hkdf('foobar', 'sha224', NULL, 28 * 255 + 1)); + } + +} \ No newline at end of file diff --git a/tests/mocks/autoloader.php b/tests/mocks/autoloader.php index cc0a2e2f7..1bcde797d 100644 --- a/tests/mocks/autoloader.php +++ b/tests/mocks/autoloader.php @@ -14,26 +14,49 @@ function autoload($class) $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR; $ci_core = array( - 'Benchmark', 'Config', 'Controller', - 'Exceptions', 'Hooks', 'Input', - 'Lang', 'Loader', 'Log', 'Model', - 'Output', 'Router', 'Security', - 'URI', 'Utf8', + 'Benchmark', + 'Config', + 'Controller', + 'Exceptions', + 'Hooks', + 'Input', + 'Lang', + 'Loader', + 'Log', + 'Model', + 'Output', + 'Router', + 'Security', + 'URI', + 'Utf8' ); $ci_libraries = array( - 'Calendar', 'Cart', 'Driver_Library', - 'Email', 'Encrypt', 'Form_validation', - 'Ftp', 'Image_lib', 'Javascript', - 'Migration', 'Pagination', 'Parser', - 'Profiler', 'Table', 'Trackback', - 'Typography', 'Unit_test', 'Upload', - 'User_agent', 'Xmlrpc', 'Zip' + 'Calendar', + 'Cart', + 'Driver_Library', + 'Email', + 'Encrypt', + 'Encryption', + 'Form_validation', + 'Ftp', + 'Image_lib', + 'Javascript', + 'Migration', + 'Pagination', + 'Parser', + 'Profiler', + 'Table', + 'Trackback', + 'Typography', + 'Unit_test', + 'Upload', + 'User_agent', + 'Xmlrpc', + 'Zip' ); - $ci_drivers = array( - 'Session', - ); + $ci_drivers = array('Session', 'Cache'); if (strpos($class, 'Mock_') === 0) { -- cgit v1.2.3-24-g4f1b From 50ccc38a8c07b412aad0c26afb0775a0d9be18ee Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 4 Feb 2014 23:30:06 +0200 Subject: CI_Encryption: Fix more errors and add a 'portability' test case --- tests/codeigniter/libraries/Encryption_test.php | 51 ++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/codeigniter/libraries/Encryption_test.php b/tests/codeigniter/libraries/Encryption_test.php index 94a952677..3d091e8d8 100644 --- a/tests/codeigniter/libraries/Encryption_test.php +++ b/tests/codeigniter/libraries/Encryption_test.php @@ -4,13 +4,62 @@ class Encryption_test extends CI_TestCase { public function set_up() { - $this->ci_set_config('encryption_key', "Encryptin'glike@boss!"); + $this->ci_set_config('encryption_key', "\xd0\xc9\x08\xc4\xde\x52\x12\x6e\xf8\xcc\xdb\x03\xea\xa0\x3a\x5c"); $this->encryption = new CI_Encryption(); $this->ci_instance_var('encryption', $this->encryption); } // -------------------------------------------------------------------- + public function test_portability() + { + if ( ! $this->encryption->drivers['mcrypt'] OR ! $this->encryption->drivers['openssl']) + { + $this->markTestAsSkipped('Both MCrypt and OpenSSL support are required for portability tests.'); + return; + } + + $message = 'This is a message encrypted via MCrypt and decrypted via OpenSSL, or vice-versa.'; + + // As it turns out, only ciphers that happened to be a US standard have a + // somewhat consistent implementation between MCrypt and OpenSSL, so + // we can only test AES, DES and TripleDES. + // + // Format is: , , + $portable = array( + array('rijndael-128', 'aes-128', 16), + array('rijndael-128', 'aes-192', 24), + array('rijndael-128', 'aes-256', 32), + array('des', 'des', 7), + array('tripledes', 'des-ede3', 7), + array('tripledes', 'des-ede3', 14), + array('tripledes', 'des-ede3', 21) + ); + $driver_index = array('mcrypt', 'openssl'); + + foreach ($portable as &$test) + { + // Add some randomness to the selected driver + $driver = mt_rand(0,1); + $params = array( + 'cipher' => $test[$driver], + 'mode' => 'cbc', + 'key' => openssl_random_pseudo_bytes($test[2]) + ); + + $this->encryption->initialize(array('driver' => $driver_index[$driver])); + $ciphertext = $this->encryption->encrypt($message, $params); + + $driver = (int) ! $driver; + $params['cipher'] = $test[$driver]; + + $this->encryption->initialize(array('driver' => $driver_index[$driver])); + $this->assertEquals($message, $this->encryption->decrypt($ciphertext, $params)); + } + } + + // -------------------------------------------------------------------- + public function test_hkdf() { // Test vectors are described in RFC5869, Appendix A(1-3). -- cgit v1.2.3-24-g4f1b From d9a48da095e2b76c65b1b9ebd26b6f06c94cb0e5 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 5 Feb 2014 14:10:28 +0200 Subject: CI_Encryption: Add Blowfish to compatibility list --- tests/codeigniter/libraries/Encryption_test.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/codeigniter/libraries/Encryption_test.php b/tests/codeigniter/libraries/Encryption_test.php index 3d091e8d8..b13cb3140 100644 --- a/tests/codeigniter/libraries/Encryption_test.php +++ b/tests/codeigniter/libraries/Encryption_test.php @@ -21,10 +21,6 @@ class Encryption_test extends CI_TestCase { $message = 'This is a message encrypted via MCrypt and decrypted via OpenSSL, or vice-versa.'; - // As it turns out, only ciphers that happened to be a US standard have a - // somewhat consistent implementation between MCrypt and OpenSSL, so - // we can only test AES, DES and TripleDES. - // // Format is: , , $portable = array( array('rijndael-128', 'aes-128', 16), @@ -33,7 +29,9 @@ class Encryption_test extends CI_TestCase { array('des', 'des', 7), array('tripledes', 'des-ede3', 7), array('tripledes', 'des-ede3', 14), - array('tripledes', 'des-ede3', 21) + array('tripledes', 'des-ede3', 21), + array('blowfish', 'bf', 16), + array('blowfish', 'bf', 56) ); $driver_index = array('mcrypt', 'openssl'); -- cgit v1.2.3-24-g4f1b From f401767c04a24677e11a3b6c4e3590e8b2e06e88 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 5 Feb 2014 18:51:15 +0200 Subject: CI_Encryption: More MCrypt/OpenSSL compatibility and get rid of the MCRYPT_MODE_* constants --- tests/codeigniter/libraries/Encryption_test.php | 59 +++++++++++++++++++------ 1 file changed, 46 insertions(+), 13 deletions(-) (limited to 'tests') diff --git a/tests/codeigniter/libraries/Encryption_test.php b/tests/codeigniter/libraries/Encryption_test.php index b13cb3140..ca1a4cd60 100644 --- a/tests/codeigniter/libraries/Encryption_test.php +++ b/tests/codeigniter/libraries/Encryption_test.php @@ -21,17 +21,51 @@ class Encryption_test extends CI_TestCase { $message = 'This is a message encrypted via MCrypt and decrypted via OpenSSL, or vice-versa.'; - // Format is: , , + // Format is: , , $portable = array( - array('rijndael-128', 'aes-128', 16), - array('rijndael-128', 'aes-192', 24), - array('rijndael-128', 'aes-256', 32), - array('des', 'des', 7), - array('tripledes', 'des-ede3', 7), - array('tripledes', 'des-ede3', 14), - array('tripledes', 'des-ede3', 21), - array('blowfish', 'bf', 16), - array('blowfish', 'bf', 56) + array('aes-128', 'cbc', 16), + array('aes-128', 'cfb', 16), + array('aes-128', 'cfb8', 16), + array('aes-128', 'ofb', 16), + array('aes-128', 'ecb', 16), + array('aes-128', 'ctr', 16), + array('aes-192', 'cbc', 24), + array('aes-192', 'cfb', 24), + array('aes-192', 'cfb8', 24), + array('aes-192', 'ofb', 24), + array('aes-192', 'ecb', 24), + array('aes-192', 'ctr', 24), + array('aes-256', 'cbc', 32), + array('aes-256', 'cfb', 32), + array('aes-256', 'cfb8', 32), + array('aes-256', 'ofb', 32), + array('aes-256', 'ecb', 32), + array('aes-256', 'ctr', 32), + array('des', 'cbc', 7), + array('des', 'cfb', 7), + array('des', 'cfb8', 7), + array('des', 'ofb', 7), + array('des', 'ecb', 7), + array('tripledes', 'cbc', 7), + array('tripledes', 'cfb', 7), + array('tripledes', 'cfb8', 7), + array('tripledes', 'ofb', 7), + array('tripledes', 'cbc', 14), + array('tripledes', 'cfb', 14), + array('tripledes', 'cfb8', 14), + array('tripledes', 'ofb', 14), + array('tripledes', 'cbc', 21), + array('tripledes', 'cfb', 21), + array('tripledes', 'cfb8', 21), + array('tripledes', 'ofb', 21), + array('blowfish', 'cbc', 16), + array('blowfish', 'cfb', 16), + array('blowfish', 'ofb', 16), + array('blowfish', 'ecb', 16), + array('blowfish', 'cbc', 56), + array('blowfish', 'cfb', 56), + array('blowfish', 'ofb', 56), + array('blowfish', 'ecb', 56), ); $driver_index = array('mcrypt', 'openssl'); @@ -40,8 +74,8 @@ class Encryption_test extends CI_TestCase { // Add some randomness to the selected driver $driver = mt_rand(0,1); $params = array( - 'cipher' => $test[$driver], - 'mode' => 'cbc', + 'cipher' => $test[0], + 'mode' => $test[1], 'key' => openssl_random_pseudo_bytes($test[2]) ); @@ -49,7 +83,6 @@ class Encryption_test extends CI_TestCase { $ciphertext = $this->encryption->encrypt($message, $params); $driver = (int) ! $driver; - $params['cipher'] = $test[$driver]; $this->encryption->initialize(array('driver' => $driver_index[$driver])); $this->assertEquals($message, $this->encryption->decrypt($ciphertext, $params)); -- cgit v1.2.3-24-g4f1b From 8d33a9a4a8ac924678ae5bba6387ca7fce206f4b Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 5 Feb 2014 23:11:23 +0200 Subject: CI_Encryption: HMAC to not be derived from the encryption key --- tests/codeigniter/libraries/Encryption_test.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/codeigniter/libraries/Encryption_test.php b/tests/codeigniter/libraries/Encryption_test.php index ca1a4cd60..da9870fc5 100644 --- a/tests/codeigniter/libraries/Encryption_test.php +++ b/tests/codeigniter/libraries/Encryption_test.php @@ -74,18 +74,20 @@ class Encryption_test extends CI_TestCase { // Add some randomness to the selected driver $driver = mt_rand(0,1); $params = array( + 'driver' => $driver_index[$driver], 'cipher' => $test[0], 'mode' => $test[1], 'key' => openssl_random_pseudo_bytes($test[2]) ); - $this->encryption->initialize(array('driver' => $driver_index[$driver])); - $ciphertext = $this->encryption->encrypt($message, $params); + $this->encryption->initialize($params); + $ciphertext = $this->encryption->encrypt($message); $driver = (int) ! $driver; + $params['driver'] = $driver_index[$driver]; - $this->encryption->initialize(array('driver' => $driver_index[$driver])); - $this->assertEquals($message, $this->encryption->decrypt($ciphertext, $params)); + $this->encryption->initialize($params); + $this->assertEquals($message, $this->encryption->decrypt($ciphertext)); } } -- cgit v1.2.3-24-g4f1b From e8088d693d6bd8b08c1cdc397bbdebd7067844a5 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 6 Feb 2014 05:01:48 +0200 Subject: CI_Encryption: CAST-128/CAST5 and RC4/ARCFour compatibility --- tests/codeigniter/libraries/Encryption_test.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'tests') diff --git a/tests/codeigniter/libraries/Encryption_test.php b/tests/codeigniter/libraries/Encryption_test.php index da9870fc5..41617c2f4 100644 --- a/tests/codeigniter/libraries/Encryption_test.php +++ b/tests/codeigniter/libraries/Encryption_test.php @@ -66,6 +66,29 @@ class Encryption_test extends CI_TestCase { array('blowfish', 'cfb', 56), array('blowfish', 'ofb', 56), array('blowfish', 'ecb', 56), + array('cast5', 'cbc', 5), + array('cast5', 'cfb', 5), + array('cast5', 'ofb', 5), + array('cast5', 'ecb', 5), + array('cast5', 'cbc', 8), + array('cast5', 'cfb', 8), + array('cast5', 'ofb', 8), + array('cast5', 'ecb', 8), + array('cast5', 'cbc', 10), + array('cast5', 'cfb', 10), + array('cast5', 'ofb', 10), + array('cast5', 'ecb', 10), + array('cast5', 'cbc', 16), + array('cast5', 'cfb', 16), + array('cast5', 'ofb', 16), + array('cast5', 'ecb', 16), + array('rc4', 'stream', 5), + array('rc4', 'stream', 8), + array('rc4', 'stream', 16), + array('rc4', 'stream', 32), + array('rc4', 'stream', 64), + array('rc4', 'stream', 128), + array('rc4', 'stream', 256) ); $driver_index = array('mcrypt', 'openssl'); -- cgit v1.2.3-24-g4f1b From 81e10644861c86437cf197b54af096894b41cd63 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 7 Feb 2014 01:43:36 +0200 Subject: CI_Encryption: Optimizations and test cases --- tests/codeigniter/libraries/Encryption_test.php | 333 +++++++++++++++++++----- tests/mocks/libraries/encryption.php | 39 +++ 2 files changed, 306 insertions(+), 66 deletions(-) create mode 100644 tests/mocks/libraries/encryption.php (limited to 'tests') diff --git a/tests/codeigniter/libraries/Encryption_test.php b/tests/codeigniter/libraries/Encryption_test.php index 41617c2f4..6bcf17b14 100644 --- a/tests/codeigniter/libraries/Encryption_test.php +++ b/tests/codeigniter/libraries/Encryption_test.php @@ -4,13 +4,268 @@ class Encryption_test extends CI_TestCase { public function set_up() { - $this->ci_set_config('encryption_key', "\xd0\xc9\x08\xc4\xde\x52\x12\x6e\xf8\xcc\xdb\x03\xea\xa0\x3a\x5c"); - $this->encryption = new CI_Encryption(); - $this->ci_instance_var('encryption', $this->encryption); + $this->encryption = new Mock_Libraries_Encryption(); + } + + /** + * __construct test + * + * Covers behavior with $config['encryption_key'] set or not + */ + public function test___construct() + { + // Assume no configuration from set_up() + $this->assertNull($this->encryption->get_key()); + + // Try with an empty value + $this->ci_set_config('encryption_key'); + $this->encrypt = new Mock_Libraries_Encryption(); + $this->assertNull($this->encrypt->get_key()); + + $this->ci_set_config('encryption_key', str_repeat("\x0", 16)); + $this->encrypt = new Mock_Libraries_Encryption(); + $this->assertEquals(str_repeat("\x0", 16), $this->encrypt->get_key()); + } + + // -------------------------------------------------------------------- + + /** + * hkdf() test + * + * Applies test vectors described in Appendix A(1-3) RFC5869. + * Described vectors 4-7 SHA-1, which we don't support and are + * therefore excluded. + * + * Because our implementation is a single method instead of being + * split into hkdf_extract() and hkdf_expand(), we cannot test for + * the PRK value. As long as the OKM is correct though, it's fine. + * + * @link https://tools.ietf.org/rfc/rfc5869.txt + */ + public function test_hkdf() + { + $vectors = array( + // A.1: Basic test case with SHA-256 + array( + 'digest' => 'sha256', + 'ikm' => "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", + 'salt' => "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c", + 'length' => 42, + 'info' => "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9", + // 'prk' => "\x07\x77\x09\x36\x2c\x2e\x32\xdf\x0d\xdc\x3f\x0d\xc4\x7b\xba\x63\x90\xb6\xc7\x3b\xb5\x0f\x9c\x31\x22\xec\x84\x4a\xd7\xc2\xb3\xe5", + 'okm' => "\x3c\xb2\x5f\x25\xfa\xac\xd5\x7a\x90\x43\x4f\x64\xd0\x36\x2f\x2a\x2d\x2d\x0a\x90\xcf\x1a\x5a\x4c\x5d\xb0\x2d\x56\xec\xc4\xc5\xbf\x34\x00\x72\x08\xd5\xb8\x87\x18\x58\x65" + ), + // A.2: Test with SHA-256 and longer inputs/outputs + array( + 'digest' => 'sha256', + 'ikm' => "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f", + 'salt' => "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf", + 'length' => 82, + 'info' => "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff", + // 'prk' => "\x06\xa6\xb8\x8c\x58\x53\x36\x1a\x06\x10\x4c\x9c\xeb\x35\xb4\x5c\xef\x76\x00\x14\x90\x46\x71\x01\x4a\x19\x3f\x40\xc1\x5f\xc2\x44", + 'okm' => "\xb1\x1e\x39\x8d\xc8\x03\x27\xa1\xc8\xe7\xf7\x8c\x59\x6a\x49\x34\x4f\x01\x2e\xda\x2d\x4e\xfa\xd8\xa0\x50\xcc\x4c\x19\xaf\xa9\x7c\x59\x04\x5a\x99\xca\xc7\x82\x72\x71\xcb\x41\xc6\x5e\x59\x0e\x09\xda\x32\x75\x60\x0c\x2f\x09\xb8\x36\x77\x93\xa9\xac\xa3\xdb\x71\xcc\x30\xc5\x81\x79\xec\x3e\x87\xc1\x4c\x01\xd5\xc1\xf3\x43\x4f\x1d\x87", + ), + // A.3: Test with SHA-256 and zero-length salt/info + array( + 'digest' => 'sha256', + 'ikm' => "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", + 'salt' => '', + 'length' => 42, + 'info' => '', + // 'prk' => "\x19\xef\x24\xa3\x2c\x71\x7b\x16\x7f\x33\xa9\x1d\x6f\x64\x8b\xdf\x96\x59\x67\x76\xaf\xdb\x63\x77\xac\x43\x4c\x1c\x29\x3c\xcb\x04", + 'okm' => "\x8d\xa4\xe7\x75\xa5\x63\xc1\x8f\x71\x5f\x80\x2a\x06\x3c\x5a\x31\xb8\xa1\x1f\x5c\x5e\xe1\x87\x9e\xc3\x45\x4e\x5f\x3c\x73\x8d\x2d\x9d\x20\x13\x95\xfa\xa4\xb6\x1a\x96\xc8", + ) + ); + + foreach ($vectors as $test) + { + $this->assertEquals( + $test['okm'], + $this->encryption->hkdf( + $test['ikm'], + $test['digest'], + $test['salt'], + $test['length'], + $test['info'] + ) + ); + } + + // Test default length, it must match the digest size + $this->assertEquals(64, strlen($this->encryption->hkdf('foobar', 'sha512'))); + + // Test maximum length (RFC5869 says that it must be up to 255 times the digest size) + $this->assertEquals(12240, strlen($this->encryption->hkdf('foobar', 'sha384', NULL, 48 * 255))); + $this->assertFalse($this->encryption->hkdf('foobar', 'sha224', NULL, 28 * 255 + 1)); + + // CI-specific test for an invalid digest + $this->assertFalse($this->encryption->hkdf('fobar', 'sha1')); } // -------------------------------------------------------------------- + /** + * _get_params() test + * + * @uses Mock_Libraries_Encryption::__get_params() + */ + public function test__get_params() + { + $key = str_repeat("\x0", 16); + + // Invalid custom parameters + $params = array( + // No cipher, mode or key + array('cipher' => 'aes-128', 'mode' => 'cbc'), + array('cipher' => 'aes-128', 'key' => $key), + array('mode' => 'cbc', 'key' => $key), + // No HMAC key or not a valid digest + array('cipher' => 'aes-128', 'mode' => 'cbc', 'key' => $key), + array('cipher' => 'aes-128', 'mode' => 'cbc', 'key' => $key, 'hmac_digest' => 'sha1', 'hmac_key' => $key), + // Invalid mode + array('cipher' => 'aes-128', 'mode' => 'foo', 'key' => $key, 'hmac_digest' => 'sha256', 'hmac_key' => $key) + ); + + for ($i = 0, $c = count($params); $i < $c; $i++) + { + $this->assertFalse($this->encryption->__get_params($params[$i])); + } + + // Valid parameters + $params = array( + 'cipher' => 'aes-128', + 'mode' => 'cbc', + 'key' => str_repeat("\x0", 16), + 'hmac_key' => str_repeat("\x0", 16) + ); + + $this->assertTrue(is_array($this->encryption->__get_params($params))); + + $params['iv'] = NULL; + $params['base64'] = TRUE; + $params['hmac_digest'] = 'sha512'; + + // Including all parameters + $params = array( + 'cipher' => 'aes-128', + 'mode' => 'cbc', + 'key' => str_repeat("\x0", 16), + 'iv' => str_repeat("\x0", 16), + 'base64' => FALSE, + 'hmac_key' => str_repeat("\x0", 16), + 'hmac_digest' => 'sha256' + ); + + $output = $this->encryption->__get_params($params); + unset($output['handle']); + $this->assertEquals($params, $output); + + // HMAC disabled + unset($params['hmac_key'], $params['hmac_digest']); + $params['hmac'] = FALSE; + $output = $this->encryption->__get_params($params); + unset($output['handle'], $params['hmac']); + $params['hmac_digest'] = $params['hmac_key'] = NULL; + $this->assertEquals($params, $output); + } + + // -------------------------------------------------------------------- + + /** + * initialize(), encrypt(), decrypt() test + * + * Testing the three methods separately is not realistic as they are + * designed to work together. A more thorough test for initialize() + * though is the OpenSSL/MCrypt compatibility test. + */ + public function test_initialize_encrypt_decrypt() + { + $message = 'This is a plain-text message.'; + $key = "\xd0\xc9\x08\xc4\xde\x52\x12\x6e\xf8\xcc\xdb\x03\xea\xa0\x3a\x5c"; + + // Default state (AES-128/Rijndael-128 in CBC mode) + $this->encryption->initialize(array('key' => $key)); + + // Was the key properly set? + $this->assertEquals($key, $this->encryption->get_key()); + + $this->assertEquals($message, $this->encryption->decrypt($this->encryption->encrypt($message))); + + // Try DES in ECB mode, just for the sake of changing stuff + $this->encryption->initialize(array('cipher' => 'des', 'mode' => 'ecb')); + $this->assertEquals($message, $this->encryption->decrypt($this->encryption->encrypt($message))); + } + + // -------------------------------------------------------------------- + + /** + * encrypt(), decrypt test with custom parameters + */ + public function test_encrypt_decrypt_custom() + { + $message = 'Another plain-text message.'; + + // A random invalid parameter + $this->assertFalse($this->encryption->encrypt($message, array('foo'))); + $this->assertFalse($this->encryption->decrypt($message, array('foo'))); + + // Custom IV (we'll check it), no HMAC, binary output + $params = array( + 'cipher' => 'tripledes', + 'mode' => 'cfb', + 'key' => str_repeat("\x1", 16), + 'iv' => str_repeat("\x2", 8), + 'base64' => FALSE, + 'hmac' => FALSE + ); + + $ciphertext = $this->encryption->encrypt($message, $params); + $this->assertEquals(0, strncmp($params['iv'], $ciphertext, 8)); + + // IV should be found in the cipher-text, no matter if it was supplied or not + $this->assertEquals($message, $this->encryption->decrypt($ciphertext, $params)); + unset($params['iv']); + $this->assertEquals($message, $this->encryption->decrypt($ciphertext, $params)); + } + + // -------------------------------------------------------------------- + + /** + * _mcrypt_get_handle() test + */ + public function test__mcrypt_get_handle() + { + if ($this->encryption->drivers['mcrypt'] === FALSE) + { + return $this->markTestAsSkipped('Cannot test MCrypt because it is not available.'); + } + + $this->assertTrue(is_resource($this->encryption->__driver_get_handle('mcrypt', 'rijndael-128', 'cbc'))); + } + + // -------------------------------------------------------------------- + + /** + * _openssl_get_handle() test + */ + public function test__openssl_mcrypt_get_handle() + { + if ($this->encryption->drivers['openssl'] === FALSE) + { + return $this->markTestAsSkipped('Cannot test OpenSSL because it is not available.'); + } + + $this->assertEquals('aes-128-cbc', $this->encryption->__driver_get_handle('openssl', 'aes-128', 'cbc')); + $this->assertEquals('rc4-40', $this->encryption->__driver_get_handle('openssl', 'rc4-40', 'stream')); + } + + // -------------------------------------------------------------------- + + /** + * OpenSSL/MCrypt portability test + * + * Amongst the obvious stuff, _cipher_alias() is also tested here. + */ public function test_portability() { if ( ! $this->encryption->drivers['mcrypt'] OR ! $this->encryption->drivers['openssl']) @@ -116,71 +371,17 @@ class Encryption_test extends CI_TestCase { // -------------------------------------------------------------------- - public function test_hkdf() + /** + * __get() test + */ + public function test_magic_get() { - // Test vectors are described in RFC5869, Appendix A(1-3). - // Vectors 4-7 cover SHA-1, which we don't support. - // - // URL: https://tools.ietf.org/rfc/rfc5869.txt - // - // Our implementation doesn't split into hkdf_extract(), hkdf_expand() - // and therefore we can't test for the PRK value (it is included below - // just for consistency, hence why it's also commented out). - // - // As long as OKM is correct, then we're all fine though. - $vectors = array( - // Appendix A.1: Basic test case with SHA-256 - array( - 'digest' => 'sha256', - 'ikm' => "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", - 'salt' => "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c", - 'length' => 42, - 'info' => "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9", -// 'prk' => "\x07\x77\x09\x36\x2c\x2e\x32\xdf\x0d\xdc\x3f\x0d\xc4\x7b\xba\x63\x90\xb6\xc7\x3b\xb5\x0f\x9c\x31\x22\xec\x84\x4a\xd7\xc2\xb3\xe5", - 'okm' => "\x3c\xb2\x5f\x25\xfa\xac\xd5\x7a\x90\x43\x4f\x64\xd0\x36\x2f\x2a\x2d\x2d\x0a\x90\xcf\x1a\x5a\x4c\x5d\xb0\x2d\x56\xec\xc4\xc5\xbf\x34\x00\x72\x08\xd5\xb8\x87\x18\x58\x65" - ), - // Appendix A.2: Test with SHA-256 and longer inputs/outputs - array( - 'digest' => 'sha256', - 'ikm' => "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f", - 'salt' => "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf", - 'length' => 82, - 'info' => "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff", -// 'prk' => "\x06\xa6\xb8\x8c\x58\x53\x36\x1a\x06\x10\x4c\x9c\xeb\x35\xb4\x5c\xef\x76\x00\x14\x90\x46\x71\x01\x4a\x19\x3f\x40\xc1\x5f\xc2\x44", - 'okm' => "\xb1\x1e\x39\x8d\xc8\x03\x27\xa1\xc8\xe7\xf7\x8c\x59\x6a\x49\x34\x4f\x01\x2e\xda\x2d\x4e\xfa\xd8\xa0\x50\xcc\x4c\x19\xaf\xa9\x7c\x59\x04\x5a\x99\xca\xc7\x82\x72\x71\xcb\x41\xc6\x5e\x59\x0e\x09\xda\x32\x75\x60\x0c\x2f\x09\xb8\x36\x77\x93\xa9\xac\xa3\xdb\x71\xcc\x30\xc5\x81\x79\xec\x3e\x87\xc1\x4c\x01\xd5\xc1\xf3\x43\x4f\x1d\x87", - ), - // Appendix A.3: Test with SHA-256 and zero-length salt/info - array( - 'digest' => 'sha256', - 'ikm' => "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", - 'salt' => "", - 'length' => 42, - 'info' => "", -// 'prk' => "\x19\xef\x24\xa3\x2c\x71\x7b\x16\x7f\x33\xa9\x1d\x6f\x64\x8b\xdf\x96\x59\x67\x76\xaf\xdb\x63\x77\xac\x43\x4c\x1c\x29\x3c\xcb\x04", - 'okm' => "\x8d\xa4\xe7\x75\xa5\x63\xc1\x8f\x71\x5f\x80\x2a\x06\x3c\x5a\x31\xb8\xa1\x1f\x5c\x5e\xe1\x87\x9e\xc3\x45\x4e\x5f\x3c\x73\x8d\x2d\x9d\x20\x13\x95\xfa\xa4\xb6\x1a\x96\xc8", - ) - ); - - foreach ($vectors as $test) - { - $this->assertEquals( - $test['okm'], - $this->encryption->hkdf( - $test['ikm'], - $test['digest'], - $test['salt'], - $test['length'], - $test['info'] - ) - ); - } + $this->assertNull($this->encryption->foo); + $this->assertEquals(array('mcrypt', 'openssl'), array_keys($this->encryption->drivers)); - // Test default length, it must match the digest size - $this->assertEquals(64, strlen($this->encryption->hkdf('foobar', 'sha512'))); - - // Test maximum length (RFC5869 says that it must be up to 255 times the digest size) - $this->assertEquals(12240, strlen($this->encryption->hkdf('foobar', 'sha384', NULL, 48 * 255))); - $this->assertFalse($this->encryption->hkdf('foobar', 'sha224', NULL, 28 * 255 + 1)); + // 'stream' mode is translated into an empty string for OpenSSL + $this->encryption->initialize(array('cipher' => 'rc4', 'mode' => 'stream')); + $this->assertEquals('stream', $this->encryption->mode); } } \ No newline at end of file diff --git a/tests/mocks/libraries/encryption.php b/tests/mocks/libraries/encryption.php new file mode 100644 index 000000000..028eecc72 --- /dev/null +++ b/tests/mocks/libraries/encryption.php @@ -0,0 +1,39 @@ +_get_params($params); + } + + // -------------------------------------------------------------------- + + /** + * get_key() + * + * Allows checking for key changes. + */ + public function get_key() + { + return $this->_key; + } + + // -------------------------------------------------------------------- + + /** + * __driver_get_handle() + * + * Allows checking for _mcrypt_get_handle(), _openssl_get_handle() + */ + public function __driver_get_handle($driver, $cipher, $mode) + { + return $this->{'_'.$driver.'_get_handle'}($cipher, $mode); + } + +} \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 4b450651ed3b9413be0245401da706c218850f53 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 10 Feb 2014 06:59:54 +0200 Subject: CI_Encryption: Rename 'base64' parameter to 'raw_data' and add docs --- tests/codeigniter/libraries/Encryption_test.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/codeigniter/libraries/Encryption_test.php b/tests/codeigniter/libraries/Encryption_test.php index 6bcf17b14..54db2b42d 100644 --- a/tests/codeigniter/libraries/Encryption_test.php +++ b/tests/codeigniter/libraries/Encryption_test.php @@ -151,20 +151,22 @@ class Encryption_test extends CI_TestCase { 'mode' => 'cbc', 'key' => str_repeat("\x0", 16), 'iv' => str_repeat("\x0", 16), - 'base64' => FALSE, + 'raw_data' => TRUE, 'hmac_key' => str_repeat("\x0", 16), 'hmac_digest' => 'sha256' ); $output = $this->encryption->__get_params($params); - unset($output['handle']); + unset($output['handle'], $params['raw_data']); + $params['base64'] = FALSE; $this->assertEquals($params, $output); // HMAC disabled unset($params['hmac_key'], $params['hmac_digest']); - $params['hmac'] = FALSE; + $params['hmac'] = $params['raw_data'] = FALSE; $output = $this->encryption->__get_params($params); - unset($output['handle'], $params['hmac']); + unset($output['handle'], $params['hmac'], $params['raw_data']); + $params['base64'] = TRUE; $params['hmac_digest'] = $params['hmac_key'] = NULL; $this->assertEquals($params, $output); } -- cgit v1.2.3-24-g4f1b