diff --git a/phpseclib/Crypt/Rijndael.php b/phpseclib/Crypt/Rijndael.php index 77a03b79..ef49dce8 100644 --- a/phpseclib/Crypt/Rijndael.php +++ b/phpseclib/Crypt/Rijndael.php @@ -798,7 +798,6 @@ class Rijndael extends BlockCipher // Generating encrypt code: $init_encrypt .= ' - static $tables; if (empty($tables)) { $tables = &$this->getTables(); } @@ -855,7 +854,6 @@ class Rijndael extends BlockCipher // Generating decrypt code: $init_decrypt .= ' - static $invtables; if (empty($invtables)) { $invtables = &$this->getInvTables(); } @@ -912,7 +910,7 @@ class Rijndael extends BlockCipher $this->inline_crypt = $this->createInlineCryptFunction( [ - 'init_crypt' => '', + 'init_crypt' => 'static $tables; static $invtables;', 'init_encrypt' => $init_encrypt, 'init_decrypt' => $init_decrypt, 'encrypt_block' => $encrypt_block, diff --git a/tests/Unit/Crypt/AES/TestCase.php b/tests/Unit/Crypt/AES/TestCase.php index 6a984aaa..51434fc2 100644 --- a/tests/Unit/Crypt/AES/TestCase.php +++ b/tests/Unit/Crypt/AES/TestCase.php @@ -385,6 +385,35 @@ abstract class TestCase extends PhpseclibTestCase $this->assertSame($aes->getKeyLength(), 256); } + public function testInvalidLengthKeyWithAES() + { + $plaintext = str_repeat('x', 16); + + $aes = new Crypt_Rijndael(); + $aes->setKey(str_repeat('a', 19)); + $this->assertSame($aes->getKeyLength(), 160); + + $ref = new Crypt_Rijndael(); + $ref->setKey(str_repeat('a', 19) . "\0"); + $this->assertSame( + bin2hex($aes->encrypt($plaintext)), + bin2hex($ref->encrypt($plaintext)), + 'actual and expected value do not match for 168 bit Rijndael' + ); + + $aes = new Crypt_AES(); + $aes->setKey(str_repeat('a', 19)); + $this->assertSame($aes->getKeyLength(), 192); + + $ref = new Crypt_AES(); + $ref->setKey(str_repeat('a', 19) . "\0\0\0\0\0"); + $this->assertSame( + bin2hex($aes->encrypt($plaintext)), + bin2hex($ref->encrypt($plaintext)), + 'actual and expected value do not match for 168 bit AES' + ); + } + /** * @group github938 */