Support 3DES

This commit is contained in:
Stephen Paul Weber 2013-01-26 11:08:18 -05:00
parent bf8201f432
commit 567b18c1b2
3 changed files with 19 additions and 7 deletions

View File

@ -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;

Binary file not shown.

View File

@ -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");