Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Nguyen Van Nguyen <[email protected]>
  • Loading branch information
nguyennv committed Nov 5, 2024
1 parent 7af75e7 commit 33bdb1b
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions src/Packet/SecretKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -623,39 +623,41 @@ protected function encryptKeyMaterial(
*/
protected function decryptKeyData(string $passphrase): KeyMaterialInterface
{
$clearText = "";
$packetTag = $this->getTagByte();
$kek = self::produceEncryptionKey(
$passphrase,
$this->symmetric,
$this->s2k,
$this->aead,
$packetTag
);

if ($this->aead instanceof AeadAlgorithm) {
$cipher = $this->aead->cipherEngine($kek, $this->symmetric);
$clearText = $cipher->decrypt(
$this->keyData,
$this->iv,
implode([$packetTag, $this->publicKey->toBytes()])
$keyData = $this->keyData;
if ($this->isEncrypted()) {
$packetTag = $this->getTagByte();
$kek = self::produceEncryptionKey(
$passphrase,
$this->symmetric,
$this->s2k,
$this->aead,
$packetTag
);
} else {
$cipher = $this->symmetric->cipherEngine(S2kUsage::Cfb->name);
$cipher->disablePadding();
$cipher->setIV($this->iv);
$cipher->setKey($kek);
$decrypted = $cipher->decrypt($this->keyData);
$length = strlen($decrypted) - HashAlgorithm::Sha1->digestSize();
$clearText = substr($decrypted, 0, $length);
$hashText = substr($decrypted, $length);
$hashed = hash(self::HASH_ALGO, $clearText, true);
if (strcmp($hashed, $hashText) !== 0) {
throw new \RuntimeException("Incorrect key passphrase.");

if ($this->aead instanceof AeadAlgorithm) {
$cipher = $this->aead->cipherEngine($kek, $this->symmetric);
$keyData = $cipher->decrypt(
$this->keyData,
$this->iv,
implode([$packetTag, $this->publicKey->toBytes()])
);
} else {
$cipher = $this->symmetric->cipherEngine(S2kUsage::Cfb->name);
$cipher->disablePadding();
$cipher->setIV($this->iv);
$cipher->setKey($kek);
$decrypted = $cipher->decrypt($this->keyData);
$length = strlen($decrypted) - HashAlgorithm::Sha1->digestSize();
$keyData = substr($decrypted, 0, $length);
$hashText = substr($decrypted, $length);
$hashed = hash(self::HASH_ALGO, $keyData, true);
if (strcmp($hashed, $hashText) !== 0) {
throw new \RuntimeException("Incorrect key passphrase.");
}
}
}

return self::readKeyMaterial($clearText, $this->publicKey);
return self::readKeyMaterial($keyData, $this->publicKey);
}

/**
Expand Down

0 comments on commit 33bdb1b

Please sign in to comment.