1
0
mirror of https://github.com/danog/tgseclib.git synced 2024-12-11 16:49:41 +01:00

Merge branch '2.0'

This commit is contained in:
terrafrost 2017-01-27 12:40:23 -06:00
commit 7d2e44b430
4 changed files with 87 additions and 16 deletions

View File

@ -333,7 +333,7 @@ class Blowfish extends BlockCipher
function isValidEngine($engine) function isValidEngine($engine)
{ {
if ($engine == self::ENGINE_OPENSSL) { if ($engine == self::ENGINE_OPENSSL) {
if ($this->key_length != 16) { if ($this->key_length < 16) {
return false; return false;
} }
$this->cipher_name_openssl_ecb = 'bf-ecb'; $this->cipher_name_openssl_ecb = 'bf-ecb';

View File

@ -144,21 +144,8 @@ class RC4 extends StreamCipher
*/ */
function isValidEngine($engine) function isValidEngine($engine)
{ {
switch ($engine) { if ($engine == self::ENGINE_OPENSSL) {
case self::ENGINE_OPENSSL: $this->cipher_name_openssl = 'rc4-40';
switch (strlen($this->key)) {
case 5:
$this->cipher_name_openssl = 'rc4-40';
break;
case 8:
$this->cipher_name_openssl = 'rc4-64';
break;
case 16:
$this->cipher_name_openssl = 'rc4';
break;
default:
return false;
}
} }
return parent::isValidEngine($engine); return parent::isValidEngine($engine);

View File

@ -7,6 +7,7 @@
use phpseclib\Crypt\Common\BlockCipher; use phpseclib\Crypt\Common\BlockCipher;
use phpseclib\Crypt\Blowfish; use phpseclib\Crypt\Blowfish;
use phpseclib\Crypt\Random;
class Unit_Crypt_BlowfishTest extends PhpseclibTestCase class Unit_Crypt_BlowfishTest extends PhpseclibTestCase
{ {
@ -85,4 +86,47 @@ class Unit_Crypt_BlowfishTest extends PhpseclibTestCase
$plaintext = bin2hex($plaintext); $plaintext = bin2hex($plaintext);
$this->assertEquals($result, $expected, "Failed asserting that $plaintext yielded expected output in $engineName engine"); $this->assertEquals($result, $expected, "Failed asserting that $plaintext yielded expected output in $engineName engine");
} }
public function testKeySizes()
{
$objects = $engines = array();
$temp = new Blowfish(Base::MODE_CTR);
$temp->setPreferredEngine(Base::ENGINE_INTERNAL);
$objects[] = $temp;
$engines[] = 'internal';
if ($temp->isValidEngine(Base::ENGINE_MCRYPT)) {
$temp = new Blowfish(Base::MODE_CTR);
$temp->setPreferredEngine(Base::ENGINE_MCRYPT);
$objects[] = $temp;
$engines[] = 'mcrypt';
}
if ($temp->isValidEngine(Base::ENGINE_OPENSSL)) {
$temp = new Blowfish(Base::MODE_CTR);
$temp->setPreferredEngine(Base::ENGINE_OPENSSL);
$objects[] = $temp;
$engines[] = 'OpenSSL';
}
if (count($objects) < 2) {
self::markTestSkipped('Unable to initialize two or more engines');
}
for ($i = 0; $i < count($objects); $i++) {
$objects[$i]->setIV(str_repeat('x', $objects[$i]->getBlockLength() >> 3));
}
$plaintext = str_repeat('.', 100);
for ($keyLen = 4; $keyLen <= 56; $keyLen++) {
$key = Random::string($keyLen);
$objects[0]->setKey($key);
$ref = $objects[0]->encrypt($plaintext);
for ($i = 1; $i < count($objects); $i++) {
$objects[$i]->setKey($key);
$this->assertEquals($ref, $objects[$i]->encrypt($plaintext), "Failed asserting that {$engines[$i]} yields the same output as the internal engine with a key size of $keyLen");
}
}
}
} }

View File

@ -7,6 +7,7 @@
use phpseclib\Crypt\Common\StreamCipher; use phpseclib\Crypt\Common\StreamCipher;
use phpseclib\Crypt\RC4; use phpseclib\Crypt\RC4;
use phpseclib\Crypt\Random;
class Unit_Crypt_RC4Test extends PhpseclibTestCase class Unit_Crypt_RC4Test extends PhpseclibTestCase
{ {
@ -209,4 +210,43 @@ class Unit_Crypt_RC4Test extends PhpseclibTestCase
$result = $rc4->encrypt(str_repeat("\0", $offset + 16)); $result = $rc4->encrypt(str_repeat("\0", $offset + 16));
$this->assertEquals(bin2hex(substr($result, -16)), $expected, "Failed asserting that key $key yielded expected output at offset $offset in $engineName engine"); $this->assertEquals(bin2hex(substr($result, -16)), $expected, "Failed asserting that key $key yielded expected output at offset $offset in $engineName engine");
} }
public function testKeySizes()
{
$objects = $engines = array();
$temp = new RC4(Base::MODE_CTR);
$temp->setPreferredEngine(Base::ENGINE_INTERNAL);
$objects[] = $temp;
$engines[] = 'internal';
if ($temp->isValidEngine(Base::ENGINE_MCRYPT)) {
$temp = new RC4(Base::MODE_CTR);
$temp->setPreferredEngine(Base::ENGINE_MCRYPT);
$objects[] = $temp;
$engines[] = 'mcrypt';
}
if ($temp->isValidEngine(Base::ENGINE_OPENSSL)) {
$temp = new RC4(Base::MODE_CTR);
$temp->setPreferredEngine(Base::ENGINE_OPENSSL);
$objects[] = $temp;
$engines[] = 'OpenSSL';
}
if (count($objects) < 2) {
self::markTestSkipped('Unable to initialize two or more engines');
}
$plaintext = str_repeat('.', 100);
for ($keyLen = 5; $keyLen <= 256; $keyLen++) {
$key = Random::string($keyLen);
$objects[0]->setKey($key);
$ref = $objects[0]->encrypt($plaintext);
for ($i = 1; $i < count($objects); $i++) {
$objects[$i]->setKey($key);
$this->assertEquals($ref, $objects[$i]->encrypt($plaintext), "Failed asserting that {$engines[$i]} yields the same output as the internal engine with a key size of $keyLen");
}
}
}
} }