diff --git a/src/Common/Config.php b/src/Common/Config.php index 435de67..b7f33e9 100644 --- a/src/Common/Config.php +++ b/src/Common/Config.php @@ -88,6 +88,7 @@ public static function getPreferredHash(): HashAlgorithm */ public static function setPreferredHash(HashAlgorithm $hash): void { + Helper::assertHash($hash); self::$preferredHash = $hash; } @@ -110,6 +111,7 @@ public static function setPreferredSymmetric( SymmetricAlgorithm $symmetric ): void { + Helper::assertSymmetric($symmetric); self::$preferredSymmetric = $symmetric; } @@ -175,6 +177,7 @@ public static function getS2kHash(): HashAlgorithm */ public static function setS2kHash(HashAlgorithm $s2kHash): void { + Helper::assertHash($s2kHash); self::$s2kHash = $s2kHash; } diff --git a/src/Common/Helper.php b/src/Common/Helper.php index 6525cf3..9531254 100644 --- a/src/Common/Helper.php +++ b/src/Common/Helper.php @@ -9,6 +9,7 @@ namespace OpenPGP\Common; use OpenPGP\Enum\{ + HashAlgorithm, S2kType, SymmetricAlgorithm, }; @@ -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.", + ); + } + } } diff --git a/src/Packet/AbstractPacket.php b/src/Packet/AbstractPacket.php index cd4adef..772bda3 100644 --- a/src/Packet/AbstractPacket.php +++ b/src/Packet/AbstractPacket.php @@ -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 * diff --git a/src/Packet/AeadEncryptedData.php b/src/Packet/AeadEncryptedData.php index 6b1b8fe..7501e5f 100644 --- a/src/Packet/AeadEncryptedData.php +++ b/src/Packet/AeadEncryptedData.php @@ -8,7 +8,10 @@ namespace OpenPGP\Packet; -use OpenPGP\Common\Config; +use OpenPGP\Common\{ + Config, + Helper, +}; use OpenPGP\Enum\{ AeadAlgorithm, PacketTag, @@ -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(); diff --git a/src/Packet/SecretKey.php b/src/Packet/SecretKey.php index b450a35..a4961be 100644 --- a/src/Packet/SecretKey.php +++ b/src/Packet/SecretKey.php @@ -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) { diff --git a/src/Packet/Signature.php b/src/Packet/Signature.php index 93b2abb..ab48c6a 100644 --- a/src/Packet/Signature.php +++ b/src/Packet/Signature.php @@ -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.", @@ -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( diff --git a/src/Packet/SymEncryptedData.php b/src/Packet/SymEncryptedData.php index 11d4a43..5749b59 100644 --- a/src/Packet/SymEncryptedData.php +++ b/src/Packet/SymEncryptedData.php @@ -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())); diff --git a/src/Packet/SymEncryptedIntegrityProtectedData.php b/src/Packet/SymEncryptedIntegrityProtectedData.php index d9802b6..b1c3288 100644 --- a/src/Packet/SymEncryptedIntegrityProtectedData.php +++ b/src/Packet/SymEncryptedIntegrityProtectedData.php @@ -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( @@ -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; diff --git a/src/Packet/SymEncryptedSessionKey.php b/src/Packet/SymEncryptedSessionKey.php index ee1d2a4..6e1797f 100644 --- a/src/Packet/SymEncryptedSessionKey.php +++ b/src/Packet/SymEncryptedSessionKey.php @@ -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( @@ -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) :