openpgp-php/lib/openpgp_crypt_aes_tripledes.php

81 lines
2.8 KiB
PHP
Raw Normal View History

2013-01-21 23:18:13 +00:00
<?php
require_once dirname(__FILE__).'/openpgp.php';
require_once 'Crypt/AES.php';
2013-01-26 16:08:18 +00:00
require_once 'Crypt/TripleDES.php';
2013-01-21 23:18:13 +00:00
2013-01-26 16:08:18 +00:00
class OpenPGP_Crypt_AES_TripleDES {
2013-01-21 23:18:13 +00:00
public static function decryptSymmetric($pass, $m) {
foreach($m as $p) {
if($p instanceof OpenPGP_SymmetricSessionKeyPacket) {
switch($p->symmetric_algorithm) {
2013-01-26 16:08:18 +00:00
case 2:
$cipher = new Crypt_TripleDES(CRYPT_DES_MODE_CFB);
$key_bytes = 24;
$key_block_bytes = 8;
break;
2013-01-21 23:18:13 +00:00
case 7:
$cipher = new Crypt_AES(CRYPT_AES_MODE_CFB);
$cipher->setKeyLength(128);
break;
case 8:
$cipher = new Crypt_AES(CRYPT_AES_MODE_CFB);
$cipher->setKeyLength(192);
break;
case 9:
$cipher = new Crypt_AES(CRYPT_AES_MODE_CFB);
$cipher->setKeyLength(256);
break;
}
if(!$cipher) continue; // Unsupported cipher
2013-01-26 16:08:18 +00:00
if(!isset($key_bytes)) $key_bytes = $cipher->key_size;
if(!isset($key_block_bytes)) $key_block_bytes = $cipher->block_size;
2013-01-21 23:18:13 +00:00
2013-01-26 16:08:18 +00:00
$cipher->setKey($p->s2k->make_key($pass, $key_bytes));
2013-01-21 23:18:13 +00:00
$epacket = self::getEncryptedData($m);
2013-01-26 16:08:18 +00:00
$padAmount = $key_block_bytes - (strlen($epacket->data) % $key_block_bytes);
2013-01-21 23:18:13 +00:00
if(strlen($p->encrypted_data) < 1) {
if($epacket instanceof OpenPGP_IntegrityProtectedDataPacket) {
2013-01-26 16:17:11 +00:00
$data = substr($cipher->decrypt($epacket->data . str_repeat("\0", $padAmount)), 0, strlen($epacket->data));
$prefix = substr($data, 0, $key_block_bytes + 2);
$mdc = substr(substr($data, -22, 22), 2);
$data = substr($data, $key_block_bytes + 2, -22);
2013-01-21 23:18:13 +00:00
$mkMDC = hash("sha1", $prefix . $data . "\xD3\x14", true);
if($mkMDC !== $mdc) return false;
2013-01-26 16:10:45 +00:00
try {
$msg = OpenPGP_Message::parse($data);
} catch (Exception $ex) { $msg = NULL; }
if($msg) return $msg; /* Otherwise keep trying */
2013-01-21 23:18:13 +00:00
} else {
2013-01-26 16:17:11 +00:00
// No MDC mean decrypt with resync
$iv = substr($epacket->data, 2, $key_block_bytes);
$edata = substr($epacket->data, $key_block_bytes + 2);
$cipher->setIV($iv);
$data = substr($cipher->decrypt($edata . str_repeat("\0", $padAmount)), 0, strlen($edata));
try {
$msg = OpenPGP_Message::parse($data);
} catch (Exception $ex) { $msg = NULL; }
if($msg) return $msg; /* Otherwise keep trying */
2013-01-21 23:18:13 +00:00
}
} else {
// TODO
}
}
}
2013-01-26 16:10:45 +00:00
return NULL; /* If we get here, we failed */
2013-01-21 23:18:13 +00:00
}
public static function getEncryptedData($m) {
foreach($m as $p) {
if($p instanceof OpenPGP_EncryptedDataPacket) return $p;
}
throw new Exception("Can only decrypt EncryptedDataPacket");
}
}