From 567b18c1b28fd8942260307326df0e4b5a61841a Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Sat, 26 Jan 2013 11:08:18 -0500 Subject: [PATCH] Support 3DES --- ...pt.php => openpgp_crypt_aes_tripledes.php} | 18 +++++++++++++----- tests/data/symmetric-3des.gpg | Bin 0 -> 71 bytes tests/phpseclib_suite.php | 8 ++++++-- 3 files changed, 19 insertions(+), 7 deletions(-) rename lib/{openpgp_phpseclib_crypt.php => openpgp_crypt_aes_tripledes.php} (71%) create mode 100644 tests/data/symmetric-3des.gpg 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 0000000000000000000000000000000000000000..0029caece648aa06ae12418a6341345290d06b54 GIT binary patch literal 71 zcmV-N0J#5*4Fm!M0^)EABgKS59AMHm0j!n(7TSmJyc<$OyV>u!G?U@!XE@fQjsV36 dc;~RSXPdata; 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");