use num_enum::{FromPrimitive, IntoPrimitive};
#[derive(Debug, PartialEq, Eq, Clone, Copy, FromPrimitive, IntoPrimitive)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
#[repr(u8)]
#[non_exhaustive]
pub enum PublicKeyAlgorithm {
RSA = 1,
RSAEncrypt = 2,
RSASign = 3,
#[cfg_attr(test, proptest(skip))]
ElgamalEncrypt = 16,
DSA = 17,
ECDH = 18,
ECDSA = 19,
#[cfg_attr(test, proptest(skip))]
Elgamal = 20,
#[cfg_attr(test, proptest(skip))]
DiffieHellman = 21,
EdDSALegacy = 22,
X25519 = 25,
X448 = 26,
Ed25519 = 27,
Ed448 = 28,
#[cfg(feature = "draft-pqc")]
MlDsa65Ed25519 = 30,
#[cfg(feature = "draft-pqc")]
MlDsa87Ed448 = 31,
#[cfg(feature = "draft-pqc")]
SlhDsaShake128s = 32,
#[cfg(feature = "draft-pqc")]
SlhDsaShake128f = 33,
#[cfg(feature = "draft-pqc")]
SlhDsaShake256s = 34,
#[cfg(feature = "draft-pqc")]
MlKem768X25519 = 35,
#[cfg(feature = "draft-pqc")]
MlKem1024X448 = 36,
#[cfg_attr(test, proptest(skip))]
Private100 = 100,
#[cfg_attr(test, proptest(skip))]
Private101 = 101,
#[cfg_attr(test, proptest(skip))]
Private102 = 102,
#[cfg_attr(test, proptest(skip))]
Private103 = 103,
#[cfg_attr(test, proptest(skip))]
Private104 = 104,
#[cfg_attr(test, proptest(skip))]
Private105 = 105,
#[cfg_attr(test, proptest(skip))]
Private106 = 106,
#[cfg_attr(test, proptest(skip))]
Private107 = 107,
#[cfg_attr(test, proptest(skip))]
Private108 = 108,
#[cfg_attr(test, proptest(skip))]
Private109 = 109,
#[cfg_attr(test, proptest(skip))]
Private110 = 110,
#[num_enum(catch_all)]
#[cfg_attr(test, proptest(skip))]
Unknown(u8),
}
impl PublicKeyAlgorithm {
pub fn is_pqc(&self) -> bool {
match self {
#[cfg(feature = "draft-pqc")]
Self::MlDsa65Ed25519
| Self::MlDsa87Ed448
| Self::SlhDsaShake128s
| Self::SlhDsaShake128f
| Self::SlhDsaShake256s
| Self::MlKem768X25519
| Self::MlKem1024X448 => true,
_ => false,
}
}
pub fn can_sign(self) -> bool {
use PublicKeyAlgorithm::*;
#[cfg(feature = "draft-pqc")]
if matches!(
self,
MlDsa65Ed25519 | MlDsa87Ed448 | SlhDsaShake128s | SlhDsaShake128f | SlhDsaShake256s
) {
return true;
}
matches!(
self,
RSA | RSASign | Elgamal | DSA | ECDSA | EdDSALegacy | Ed25519 | Ed448
)
}
pub fn can_encrypt(self) -> bool {
use PublicKeyAlgorithm::*;
#[cfg(feature = "draft-pqc")]
if matches!(self, MlKem768X25519 | MlKem1024X448) {
return true;
}
matches!(
self,
RSA | RSAEncrypt | ECDH | DiffieHellman | Elgamal | ElgamalEncrypt | X25519 | X448
)
}
}