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 Sep 24, 2024
1 parent 8196563 commit 5fb6161
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 48 deletions.
3 changes: 3 additions & 0 deletions src/Common/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public static function getPreferredHash(): HashAlgorithm
*/
public static function setPreferredHash(HashAlgorithm $hash): void
{
Helper::assertHash($hash);
self::$preferredHash = $hash;
}

Expand All @@ -110,6 +111,7 @@ public static function setPreferredSymmetric(
SymmetricAlgorithm $symmetric
): void
{
Helper::assertSymmetric($symmetric);
self::$preferredSymmetric = $symmetric;
}

Expand Down Expand Up @@ -175,6 +177,7 @@ public static function getS2kHash(): HashAlgorithm
*/
public static function setS2kHash(HashAlgorithm $s2kHash): void
{
Helper::assertHash($s2kHash);
self::$s2kHash = $s2kHash;
}

Expand Down
39 changes: 39 additions & 0 deletions src/Common/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace OpenPGP\Common;

use OpenPGP\Enum\{
HashAlgorithm,
S2kType,
SymmetricAlgorithm,
};
Expand Down Expand Up @@ -209,4 +210,42 @@ public static function simpleLength(int $length): string
return implode(["\xff", pack('N', $length)]);
}
}

/**
* Assert hash algorithm
*
* @param HashAlgorithm $hash
* @return void
*/
public static function assertHash(HashAlgorithm $hash): void
{
switch ($hash) {
case HashAlgorithm::Unknown:
case HashAlgorithm::Md5:
case HashAlgorithm::Sha1:
case HashAlgorithm::Ripemd160:
throw new \RuntimeException(
"Hash {$hash->name} is unsupported.",
);
}
}

/**
* Assert symmetric algorithm
*
* @param SymmetricAlgorithm $symmetric
* @return void
*/
public static function assertSymmetric(SymmetricAlgorithm $symmetric): void
{
switch ($symmetric) {
case SymmetricAlgorithm::Plaintext:
case SymmetricAlgorithm::Idea:
case SymmetricAlgorithm::TripleDes:
case SymmetricAlgorithm::Cast5:
throw new \RuntimeException(
"Symmetric {$symmetric->name} is unsupported.",
);
}
}
}
38 changes: 0 additions & 38 deletions src/Packet/AbstractPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,44 +108,6 @@ public function __toString(): string
*/
abstract public function toBytes(): string;

/**
* Assert hash algorithm
*
* @param HashAlgorithm $hash
* @return void
*/
protected static function assertHash(HashAlgorithm $hash): void
{
switch ($hash) {
case HashAlgorithm::Unknown:
case HashAlgorithm::Md5:
case HashAlgorithm::Sha1:
case HashAlgorithm::Ripemd160:
throw new \RuntimeException(
"Hash {$hash->name} is unsupported.",
);
}
}

/**
* Assert symmetric algorithm
*
* @param SymmetricAlgorithm $symmetric
* @return void
*/
protected static function assertSymmetric(SymmetricAlgorithm $symmetric): void
{
switch ($symmetric) {
case SymmetricAlgorithm::Plaintext:
case SymmetricAlgorithm::Idea:
case SymmetricAlgorithm::TripleDes:
case SymmetricAlgorithm::Cast5:
throw new \RuntimeException(
"Symmetric {$symmetric->name} is unsupported.",
);
}
}

/**
* Encode package to the openpgp partial body specifier
*
Expand Down
7 changes: 5 additions & 2 deletions src/Packet/AeadEncryptedData.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

namespace OpenPGP\Packet;

use OpenPGP\Common\Config;
use OpenPGP\Common\{
Config,
Helper,
};
use OpenPGP\Enum\{
AeadAlgorithm,
PacketTag,
Expand Down Expand Up @@ -109,7 +112,7 @@ public static function encryptPackets(
SymmetricAlgorithm $symmetric = SymmetricAlgorithm::Aes128,
): self
{
self::assertSymmetric($symmetric);
Helper::assertSymmetric($symmetric);

$aead = Config::getPreferredAead();
$chunkSize = Config::getAeadChunkSize();
Expand Down
2 changes: 1 addition & 1 deletion src/Packet/SecretKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ public function encrypt(
$this->getLogger()->debug(
'Encrypt secret key material with passphrase.'
);
self::assertSymmetric($symmetric);
Helper::assertSymmetric($symmetric);

$aeadProtect = $aead instanceof AeadAlgorithm;
if ($aeadProtect && $this->getVersion() !== PublicKey::VERSION_6) {
Expand Down
4 changes: 2 additions & 2 deletions src/Packet/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function __construct(
);
}
if ($version === self::VERSION_6) {
self::assertHash($hashAlgorithm);
Helper::assertHash($hashAlgorithm);
if ($keyAlgorithm === KeyAlgorithm::Dsa) {
throw new \InvalidArgumentException(
"Public key {$keyAlgorithm->name} cannot be used with v{$version} signature packet.",
Expand Down Expand Up @@ -217,7 +217,7 @@ public static function createSignature(
$version = $signKey->getVersion();
$keyAlgorithm = $signKey->getKeyAlgorithm();
$hashAlgorithm = $signKey->getPreferredHash($hashAlgorithm);
self::assertHash($hashAlgorithm);
Helper::assertHash($hashAlgorithm);

$hashedSubpackets = [
Signature\SignatureCreationTime::fromTime(
Expand Down
2 changes: 1 addition & 1 deletion src/Packet/SymEncryptedData.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static function encryptPackets(
SymmetricAlgorithm $symmetric = SymmetricAlgorithm::Aes128,
): self
{
self::assertSymmetric($symmetric);
Helper::assertSymmetric($symmetric);
$cipher = $symmetric->cipherEngine(Config::CIPHER_MODE);
$cipher->setKey($key);
$cipher->setIV(str_repeat(self::ZERO_CHAR, $symmetric->blockSize()));
Expand Down
4 changes: 2 additions & 2 deletions src/Packet/SymEncryptedIntegrityProtectedData.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function __construct(
}
$isV2 = $version === self::VERSION_2;
if ($symmetric instanceof SymmetricAlgorithm && $isV2) {
self::assertSymmetric($symmetric);
Helper::assertSymmetric($symmetric);
}
if ($aead instanceof AeadAlgorithm && !$isV2) {
throw new \InvalidArgumentException(
Expand Down Expand Up @@ -146,7 +146,7 @@ public static function encryptPackets(
{
$aeadProtect = $aead instanceof AeadAlgorithm;
$version = $aeadProtect ? self::VERSION_2 : self::VERSION_1;
self::assertSymmetric($symmetric);
Helper::assertSymmetric($symmetric);

$salt = '';
$chunkSize = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/Packet/SymEncryptedSessionKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function __construct(
);
}
if ($version === self::VERSION_6) {
self::assertSymmetric($symmetric);
Helper::assertSymmetric($symmetric);
}
if ($aead instanceof AeadAlgorithm && $version < self::VERSION_5) {
throw new \InvalidArgumentException(
Expand Down Expand Up @@ -154,7 +154,7 @@ public static function encryptSessionKey(
$aeadProtect = $aead instanceof AeadAlgorithm;
$version = $aeadProtect ? self::VERSION_6 : self::VERSION_4;
$symmetric = $sessionKey?->getSymmetric() ?? $symmetric;
self::assertSymmetric($symmetric);
Helper::assertSymmetric($symmetric);

$s2k = $aeadProtect && Argon2S2K::argon2Supported() ?
Helper::stringToKey(S2kType::Argon2) :
Expand Down

0 comments on commit 5fb6161

Please sign in to comment.