diff --git a/lib/openpgp_phpseclib_crypt.php b/lib/openpgp_crypt_aes_tripledes.php similarity index 71% rename from lib/openpgp_phpseclib_crypt.php rename to lib/openpgp_crypt_aes_tripledes.php index cd14c04..8e0d27b 100644 --- a/lib/openpgp_phpseclib_crypt.php +++ b/lib/openpgp_crypt_aes_tripledes.php @@ -2,12 +2,18 @@ require_once dirname(__FILE__).'/openpgp.php'; require_once 'Crypt/AES.php'; +require_once 'Crypt/TripleDES.php'; -class OpenPGP_phpseclib_Crypt { +class OpenPGP_Crypt_AES_TripleDES { public static function decryptSymmetric($pass, $m) { foreach($m as $p) { if($p instanceof OpenPGP_SymmetricSessionKeyPacket) { switch($p->symmetric_algorithm) { + case 2: + $cipher = new Crypt_TripleDES(CRYPT_DES_MODE_CFB); + $key_bytes = 24; + $key_block_bytes = 8; + break; case 7: $cipher = new Crypt_AES(CRYPT_AES_MODE_CFB); $cipher->setKeyLength(128); @@ -22,17 +28,19 @@ class OpenPGP_phpseclib_Crypt { break; } if(!$cipher) continue; // Unsupported cipher + if(!isset($key_bytes)) $key_bytes = $cipher->key_size; + if(!isset($key_block_bytes)) $key_block_bytes = $cipher->block_size; - $cipher->setKey($p->s2k->make_key($pass, $cipher->key_size)); + $cipher->setKey($p->s2k->make_key($pass, $key_bytes)); $epacket = self::getEncryptedData($m); - $padAmount = $cipher->block_size - (strlen($epacket->data) % $cipher->block_size); + $padAmount = $key_block_bytes - (strlen($epacket->data) % $key_block_bytes); if(strlen($p->encrypted_data) < 1) { if($epacket instanceof OpenPGP_IntegrityProtectedDataPacket) { $data = substr($cipher->decrypt($epacket->data . str_repeat("\0", $padAmount)), 0, strlen($epacket->data)); - $prefix = substr($data, 0, $cipher->block_size + 2); + $prefix = substr($data, 0, $key_block_bytes + 2); $mdc = substr(substr($data, -22, 22), 2); - $data = substr($data, $cipher->block_size + 2, -22); + $data = substr($data, $key_block_bytes + 2, -22); $mkMDC = hash("sha1", $prefix . $data . "\xD3\x14", true); if($mkMDC !== $mdc) return false; diff --git a/tests/data/symmetric-3des.gpg b/tests/data/symmetric-3des.gpg new file mode 100644 index 0000000..0029cae Binary files /dev/null and b/tests/data/symmetric-3des.gpg differ diff --git a/tests/phpseclib_suite.php b/tests/phpseclib_suite.php index 559be3d..9501268 100644 --- a/tests/phpseclib_suite.php +++ b/tests/phpseclib_suite.php @@ -4,7 +4,7 @@ require_once dirname(__FILE__).'/../lib/openpgp.php'; require_once dirname(__FILE__).'/../lib/openpgp_crypt_rsa.php'; -require_once dirname(__FILE__).'/../lib/openpgp_phpseclib_crypt.php'; +require_once dirname(__FILE__).'/../lib/openpgp_crypt_aes_tripledes.php'; class MessageVerification extends PHPUnit_Framework_TestCase { public function oneMessageRSA($pkey, $path) { @@ -67,7 +67,7 @@ class KeyVerification extends PHPUnit_Framework_TestCase { class Decryption extends PHPUnit_Framework_TestCase { public function oneSymmetric($pass, $cnt, $path) { $m = OpenPGP_Message::parse(file_get_contents(dirname(__FILE__) . '/data/' . $path)); - $m2 = OpenPGP_phpseclib_Crypt::decryptSymmetric($pass, $m); + $m2 = OpenPGP_Crypt_AES_TripleDES::decryptSymmetric($pass, $m); while($m2[0] instanceof OpenPGP_CompressedDataPacket) $m2 = $m2[0]->data; foreach($m2 as $p) { if($p instanceof OpenPGP_LiteralDataPacket) { @@ -80,6 +80,10 @@ class Decryption extends PHPUnit_Framework_TestCase { $this->oneSymmetric("hello", "PGP\n", "symmetric-aes.gpg"); } + public function testDecrypt3DES() { + $this->oneSymmetric("hello", "PGP\n", "symmetric-3des.gpg"); + } + /* TODO public function testDecryptSessionKey() { $this->oneSymmetric("hello", "PGP\n", "symmetric-with-session-key.gpg");