2014-11-29 13:39:21 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Andreas Fischer <bantu@phpbb.com>
|
|
|
|
* @copyright MMXIII Andreas Fischer
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once 'Crypt/DES.php';
|
|
|
|
|
|
|
|
// the AES tests establish the correctness of the modes of operation. this test is inteded to establish the consistency of
|
|
|
|
// key and iv padding between the multiple engines
|
|
|
|
class Unit_Crypt_DES_TestCase extends PhpseclibTestCase
|
|
|
|
{
|
|
|
|
public function testEncryptPadding()
|
|
|
|
{
|
|
|
|
$des = new Crypt_DES(CRYPT_MODE_CBC);
|
|
|
|
$des->setKey('d');
|
|
|
|
$des->setIV('d');
|
|
|
|
|
2014-12-13 20:34:48 +00:00
|
|
|
$des->setPreferredEngine(CRYPT_ENGINE_INTERNAL);
|
2014-12-13 21:00:38 +00:00
|
|
|
|
|
|
|
$result = pack('H*', '3e7613642049af1e');
|
|
|
|
|
2014-11-29 13:39:21 +00:00
|
|
|
$internal = $des->encrypt('d');
|
2014-12-13 21:10:47 +00:00
|
|
|
$this->assertEquals($result, $internal, 'Failed asserting that the internal engine produced the correct result');
|
2014-11-29 13:39:21 +00:00
|
|
|
|
2014-12-13 20:34:48 +00:00
|
|
|
$des->setPreferredEngine(CRYPT_ENGINE_MCRYPT);
|
|
|
|
if ($des->getEngine() == CRYPT_ENGINE_MCRYPT) {
|
2014-11-29 13:39:21 +00:00
|
|
|
$mcrypt = $des->encrypt('d');
|
2014-12-13 21:00:38 +00:00
|
|
|
$this->assertEquals($result, $mcrypt, 'Failed asserting that the mcrypt engine produced the correct result');
|
2014-11-29 13:39:21 +00:00
|
|
|
} else {
|
|
|
|
self::markTestSkipped('Unable to initialize mcrypt engine');
|
|
|
|
}
|
|
|
|
|
2014-12-13 20:34:48 +00:00
|
|
|
$des->setPreferredEngine(CRYPT_ENGINE_OPENSSL);
|
|
|
|
if ($des->getEngine() == CRYPT_ENGINE_OPENSSL) {
|
2014-11-29 13:39:21 +00:00
|
|
|
$openssl = $des->encrypt('d');
|
2014-12-13 21:00:38 +00:00
|
|
|
$this->assertEquals($result, $openssl, 'Failed asserting that the OpenSSL engine produced the correct result');
|
2014-11-29 13:39:21 +00:00
|
|
|
} else {
|
|
|
|
self::markTestSkipped('Unable to initialize OpenSSL engine');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// phpseclib null pads ciphertext's if they're not long enough and you're in ecb / cbc mode. this silent failure mode is consistent
|
|
|
|
// with mcrypt's behavior. maybe throwing an exception would be better but whatever. this test is more intended to establish consistent
|
|
|
|
// behavior between the various engine's
|
|
|
|
public function testDecryptPadding()
|
|
|
|
{
|
|
|
|
$des = new Crypt_DES(CRYPT_MODE_CBC);
|
2014-12-13 21:00:38 +00:00
|
|
|
$des->disablePadding();
|
2014-11-29 13:39:21 +00:00
|
|
|
// when the key and iv are not specified they should be null padded
|
|
|
|
//$des->setKey();
|
|
|
|
//$des->setIV();
|
|
|
|
|
2014-12-13 20:34:48 +00:00
|
|
|
$des->setPreferredEngine(CRYPT_ENGINE_INTERNAL);
|
2014-11-29 13:39:21 +00:00
|
|
|
$internal = $des->decrypt('d');
|
|
|
|
|
2014-12-13 21:10:47 +00:00
|
|
|
$result = pack('H*', '36a86ebd0f9e048f');
|
|
|
|
$this->assertEquals($result, $internal, 'Failed asserting that the internal engine produced the correct result');
|
|
|
|
|
2014-12-13 20:34:48 +00:00
|
|
|
$des->setPreferredEngine(CRYPT_ENGINE_MCRYPT);
|
|
|
|
if ($des->getEngine() == CRYPT_ENGINE_MCRYPT) {
|
2014-11-29 13:39:21 +00:00
|
|
|
$mcrypt = $des->decrypt('d');
|
2014-12-13 21:10:47 +00:00
|
|
|
$this->assertEquals($result, $mcrypt, 'Failed asserting that the mcrypt engine produced the correct result');
|
2014-11-29 13:39:21 +00:00
|
|
|
} else {
|
|
|
|
self::markTestSkipped('Unable to initialize mcrypt engine');
|
|
|
|
}
|
|
|
|
|
2014-12-13 20:34:48 +00:00
|
|
|
$des->setPreferredEngine(CRYPT_ENGINE_OPENSSL);
|
|
|
|
if ($des->getEngine() == CRYPT_ENGINE_OPENSSL) {
|
2014-11-29 13:39:21 +00:00
|
|
|
$openssl = $des->decrypt('d');
|
2014-12-13 21:10:47 +00:00
|
|
|
$this->assertEquals($result, $openssl, 'Failed asserting that the OpenSSL engine produced the correct result');
|
2014-11-29 13:39:21 +00:00
|
|
|
} else {
|
|
|
|
self::markTestSkipped('Unable to initialize OpenSSL engine');
|
|
|
|
}
|
|
|
|
}
|
2014-12-13 19:47:37 +00:00
|
|
|
}
|