use std::fmt;
use std::str::FromStr;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SignatureAlgorithm {
MlDsa87,
EcdsaP256Sha256,
EcdsaP384Sha384,
EcdsaP521Sha384,
EcdsaP521Sha512,
Ed25519,
Rsa2048Sha384,
Rsa3072Sha384,
Rsa4096Sha384,
Rsa2048Sha384Pss,
Rsa3072Sha384Pss,
Rsa4096Sha384Pss,
}
impl SignatureAlgorithm {
pub const ALL: &'static [SignatureAlgorithm] = &[
SignatureAlgorithm::MlDsa87,
SignatureAlgorithm::EcdsaP256Sha256,
SignatureAlgorithm::EcdsaP384Sha384,
SignatureAlgorithm::EcdsaP521Sha384,
SignatureAlgorithm::EcdsaP521Sha512,
SignatureAlgorithm::Ed25519,
SignatureAlgorithm::Rsa2048Sha384,
SignatureAlgorithm::Rsa3072Sha384,
SignatureAlgorithm::Rsa4096Sha384,
SignatureAlgorithm::Rsa2048Sha384Pss,
SignatureAlgorithm::Rsa3072Sha384Pss,
SignatureAlgorithm::Rsa4096Sha384Pss,
];
#[cfg(test)]
pub(crate) const fn ordinal(self) -> usize {
match self {
SignatureAlgorithm::MlDsa87 => 0,
SignatureAlgorithm::EcdsaP256Sha256 => 1,
SignatureAlgorithm::EcdsaP384Sha384 => 2,
SignatureAlgorithm::EcdsaP521Sha384 => 3,
SignatureAlgorithm::EcdsaP521Sha512 => 4,
SignatureAlgorithm::Ed25519 => 5,
SignatureAlgorithm::Rsa2048Sha384 => 6,
SignatureAlgorithm::Rsa3072Sha384 => 7,
SignatureAlgorithm::Rsa4096Sha384 => 8,
SignatureAlgorithm::Rsa2048Sha384Pss => 9,
SignatureAlgorithm::Rsa3072Sha384Pss => 10,
SignatureAlgorithm::Rsa4096Sha384Pss => 11,
}
}
pub const fn as_str(self) -> &'static str {
match self {
SignatureAlgorithm::MlDsa87 => "ml-dsa-87",
SignatureAlgorithm::EcdsaP256Sha256 => "ecdsa-p256-sha256",
SignatureAlgorithm::EcdsaP384Sha384 => "ecdsa-p384-sha384",
SignatureAlgorithm::EcdsaP521Sha384 => "ecdsa-p521-sha384",
SignatureAlgorithm::EcdsaP521Sha512 => "ecdsa-p521-sha512",
SignatureAlgorithm::Ed25519 => "ed25519",
SignatureAlgorithm::Rsa2048Sha384 => "rsa2048-sha384",
SignatureAlgorithm::Rsa3072Sha384 => "rsa3072-sha384",
SignatureAlgorithm::Rsa4096Sha384 => "rsa4096-sha384",
SignatureAlgorithm::Rsa2048Sha384Pss => "rsa2048-sha384-pss",
SignatureAlgorithm::Rsa3072Sha384Pss => "rsa3072-sha384-pss",
SignatureAlgorithm::Rsa4096Sha384Pss => "rsa4096-sha384-pss",
}
}
pub const fn signature_oid(self) -> &'static str {
match self {
SignatureAlgorithm::MlDsa87 => "2.16.840.1.101.3.4.3.19",
SignatureAlgorithm::EcdsaP256Sha256 => "1.2.840.10045.4.3.2",
SignatureAlgorithm::EcdsaP384Sha384 | SignatureAlgorithm::EcdsaP521Sha384 => {
"1.2.840.10045.4.3.3"
}
SignatureAlgorithm::EcdsaP521Sha512 => "1.2.840.10045.4.3.4",
SignatureAlgorithm::Ed25519 => "1.3.101.112",
SignatureAlgorithm::Rsa2048Sha384
| SignatureAlgorithm::Rsa3072Sha384
| SignatureAlgorithm::Rsa4096Sha384 => "1.2.840.113549.1.1.12",
SignatureAlgorithm::Rsa2048Sha384Pss
| SignatureAlgorithm::Rsa3072Sha384Pss
| SignatureAlgorithm::Rsa4096Sha384Pss => "1.2.840.113549.1.1.10",
}
}
pub const fn tls_scheme(self) -> Option<TlsSignatureScheme> {
match self {
SignatureAlgorithm::MlDsa87 => Some(TlsSignatureScheme::MlDsa87),
SignatureAlgorithm::EcdsaP256Sha256 => Some(TlsSignatureScheme::EcdsaNistp256Sha256),
SignatureAlgorithm::EcdsaP384Sha384 => Some(TlsSignatureScheme::EcdsaNistp384Sha384),
SignatureAlgorithm::EcdsaP521Sha384 | SignatureAlgorithm::EcdsaP521Sha512 => {
Some(TlsSignatureScheme::EcdsaNistp521Sha512)
}
SignatureAlgorithm::Ed25519 => Some(TlsSignatureScheme::Ed25519),
SignatureAlgorithm::Rsa2048Sha384
| SignatureAlgorithm::Rsa3072Sha384
| SignatureAlgorithm::Rsa4096Sha384 => Some(TlsSignatureScheme::RsaPssSha384),
SignatureAlgorithm::Rsa2048Sha384Pss
| SignatureAlgorithm::Rsa3072Sha384Pss
| SignatureAlgorithm::Rsa4096Sha384Pss => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ParseAlgorithmError(pub String);
impl fmt::Display for ParseAlgorithmError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "unknown algorithm name {:?}", self.0)
}
}
impl std::error::Error for ParseAlgorithmError {}
impl FromStr for SignatureAlgorithm {
type Err = ParseAlgorithmError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
SignatureAlgorithm::ALL
.iter()
.copied()
.find(|a| a.as_str() == s)
.ok_or_else(|| ParseAlgorithmError(s.to_string()))
}
}
impl fmt::Display for SignatureAlgorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum TlsSignatureScheme {
EcdsaNistp256Sha256,
EcdsaNistp384Sha384,
EcdsaNistp521Sha512,
Ed25519,
RsaPssSha256,
RsaPssSha384,
RsaPssSha512,
RsaPkcs1Sha256,
RsaPkcs1Sha384,
RsaPkcs1Sha512,
MlDsa87,
}
impl TlsSignatureScheme {
pub const fn usable_in(self, v: TlsVersion) -> bool {
match self {
TlsSignatureScheme::RsaPkcs1Sha256
| TlsSignatureScheme::RsaPkcs1Sha384
| TlsSignatureScheme::RsaPkcs1Sha512 => matches!(v, TlsVersion::Tls12),
TlsSignatureScheme::MlDsa87 => matches!(v, TlsVersion::Tls13),
TlsSignatureScheme::EcdsaNistp256Sha256
| TlsSignatureScheme::EcdsaNistp384Sha384
| TlsSignatureScheme::EcdsaNistp521Sha512
| TlsSignatureScheme::Ed25519
| TlsSignatureScheme::RsaPssSha256
| TlsSignatureScheme::RsaPssSha384
| TlsSignatureScheme::RsaPssSha512 => true,
}
}
pub const fn iana(self) -> u16 {
match self {
TlsSignatureScheme::EcdsaNistp256Sha256 => 0x0403,
TlsSignatureScheme::EcdsaNistp384Sha384 => 0x0503,
TlsSignatureScheme::EcdsaNistp521Sha512 => 0x0603,
TlsSignatureScheme::Ed25519 => 0x0807,
TlsSignatureScheme::RsaPssSha256 => 0x0804,
TlsSignatureScheme::RsaPssSha384 => 0x0805,
TlsSignatureScheme::RsaPssSha512 => 0x0806,
TlsSignatureScheme::RsaPkcs1Sha256 => 0x0401,
TlsSignatureScheme::RsaPkcs1Sha384 => 0x0501,
TlsSignatureScheme::RsaPkcs1Sha512 => 0x0601,
TlsSignatureScheme::MlDsa87 => 0x0906,
}
}
pub const fn preference(self) -> u8 {
match self {
TlsSignatureScheme::MlDsa87 => 0,
TlsSignatureScheme::EcdsaNistp384Sha384 => 1,
TlsSignatureScheme::EcdsaNistp256Sha256 => 2,
TlsSignatureScheme::EcdsaNistp521Sha512 => 3,
TlsSignatureScheme::Ed25519 => 4,
TlsSignatureScheme::RsaPssSha384 => 5,
TlsSignatureScheme::RsaPssSha512 => 6,
TlsSignatureScheme::RsaPssSha256 => 7,
TlsSignatureScheme::RsaPkcs1Sha384 => 8,
TlsSignatureScheme::RsaPkcs1Sha512 => 9,
TlsSignatureScheme::RsaPkcs1Sha256 => 10,
}
}
pub const ALL: &'static [TlsSignatureScheme] = &[
TlsSignatureScheme::EcdsaNistp256Sha256,
TlsSignatureScheme::EcdsaNistp384Sha384,
TlsSignatureScheme::EcdsaNistp521Sha512,
TlsSignatureScheme::Ed25519,
TlsSignatureScheme::RsaPssSha256,
TlsSignatureScheme::RsaPssSha384,
TlsSignatureScheme::RsaPssSha512,
TlsSignatureScheme::RsaPkcs1Sha256,
TlsSignatureScheme::RsaPkcs1Sha384,
TlsSignatureScheme::RsaPkcs1Sha512,
TlsSignatureScheme::MlDsa87,
];
#[cfg(test)]
pub(crate) const fn ordinal(self) -> usize {
match self {
TlsSignatureScheme::EcdsaNistp256Sha256 => 0,
TlsSignatureScheme::EcdsaNistp384Sha384 => 1,
TlsSignatureScheme::EcdsaNistp521Sha512 => 2,
TlsSignatureScheme::Ed25519 => 3,
TlsSignatureScheme::RsaPssSha256 => 4,
TlsSignatureScheme::RsaPssSha384 => 5,
TlsSignatureScheme::RsaPssSha512 => 6,
TlsSignatureScheme::RsaPkcs1Sha256 => 7,
TlsSignatureScheme::RsaPkcs1Sha384 => 8,
TlsSignatureScheme::RsaPkcs1Sha512 => 9,
TlsSignatureScheme::MlDsa87 => 10,
}
}
}
const _: () = assert!(TlsSignatureScheme::ALL.len() <= u64::BITS as usize);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CipherSuite {
Tls13Aes256GcmSha384,
Tls13Aes128GcmSha256,
Tls13Chacha20Poly1305Sha256,
Tls12EcdheEcdsaAes256GcmSha384,
Tls12EcdheEcdsaAes128GcmSha256,
Tls12EcdheEcdsaChacha20Poly1305Sha256,
Tls12EcdheRsaAes256GcmSha384,
Tls12EcdheRsaAes128GcmSha256,
Tls12EcdheRsaChacha20Poly1305Sha256,
}
impl CipherSuite {
pub const fn signs_with(self, t: TlsSignatureScheme) -> bool {
use TlsSignatureScheme as T;
match self {
CipherSuite::Tls13Aes256GcmSha384
| CipherSuite::Tls13Aes128GcmSha256
| CipherSuite::Tls13Chacha20Poly1305Sha256 => t.usable_in(TlsVersion::Tls13),
CipherSuite::Tls12EcdheEcdsaAes256GcmSha384
| CipherSuite::Tls12EcdheEcdsaAes128GcmSha256
| CipherSuite::Tls12EcdheEcdsaChacha20Poly1305Sha256 => matches!(
t,
T::EcdsaNistp256Sha256
| T::EcdsaNistp384Sha384
| T::EcdsaNistp521Sha512
| T::Ed25519
),
CipherSuite::Tls12EcdheRsaAes256GcmSha384
| CipherSuite::Tls12EcdheRsaAes128GcmSha256
| CipherSuite::Tls12EcdheRsaChacha20Poly1305Sha256 => matches!(
t,
T::RsaPssSha256
| T::RsaPssSha384
| T::RsaPssSha512
| T::RsaPkcs1Sha256
| T::RsaPkcs1Sha384
| T::RsaPkcs1Sha512
),
}
}
pub const ALL: &'static [CipherSuite] = &[
CipherSuite::Tls13Aes256GcmSha384,
CipherSuite::Tls13Aes128GcmSha256,
CipherSuite::Tls13Chacha20Poly1305Sha256,
CipherSuite::Tls12EcdheEcdsaAes256GcmSha384,
CipherSuite::Tls12EcdheEcdsaAes128GcmSha256,
CipherSuite::Tls12EcdheEcdsaChacha20Poly1305Sha256,
CipherSuite::Tls12EcdheRsaAes256GcmSha384,
CipherSuite::Tls12EcdheRsaAes128GcmSha256,
CipherSuite::Tls12EcdheRsaChacha20Poly1305Sha256,
];
#[cfg(test)]
pub(crate) const fn ordinal(self) -> usize {
match self {
CipherSuite::Tls13Aes256GcmSha384 => 0,
CipherSuite::Tls13Aes128GcmSha256 => 1,
CipherSuite::Tls13Chacha20Poly1305Sha256 => 2,
CipherSuite::Tls12EcdheEcdsaAes256GcmSha384 => 3,
CipherSuite::Tls12EcdheEcdsaAes128GcmSha256 => 4,
CipherSuite::Tls12EcdheEcdsaChacha20Poly1305Sha256 => 5,
CipherSuite::Tls12EcdheRsaAes256GcmSha384 => 6,
CipherSuite::Tls12EcdheRsaAes128GcmSha256 => 7,
CipherSuite::Tls12EcdheRsaChacha20Poly1305Sha256 => 8,
}
}
pub const fn iana(self) -> u16 {
match self {
CipherSuite::Tls13Aes256GcmSha384 => 0x1302,
CipherSuite::Tls13Aes128GcmSha256 => 0x1301,
CipherSuite::Tls13Chacha20Poly1305Sha256 => 0x1303,
CipherSuite::Tls12EcdheEcdsaAes256GcmSha384 => 0xC02C,
CipherSuite::Tls12EcdheEcdsaAes128GcmSha256 => 0xC02B,
CipherSuite::Tls12EcdheEcdsaChacha20Poly1305Sha256 => 0xCCA9,
CipherSuite::Tls12EcdheRsaAes256GcmSha384 => 0xC030,
CipherSuite::Tls12EcdheRsaAes128GcmSha256 => 0xC02F,
CipherSuite::Tls12EcdheRsaChacha20Poly1305Sha256 => 0xCCA8,
}
}
pub const fn version(self) -> TlsVersion {
match self {
CipherSuite::Tls13Aes256GcmSha384
| CipherSuite::Tls13Aes128GcmSha256
| CipherSuite::Tls13Chacha20Poly1305Sha256 => TlsVersion::Tls13,
CipherSuite::Tls12EcdheEcdsaAes256GcmSha384
| CipherSuite::Tls12EcdheEcdsaAes128GcmSha256
| CipherSuite::Tls12EcdheEcdsaChacha20Poly1305Sha256
| CipherSuite::Tls12EcdheRsaAes256GcmSha384
| CipherSuite::Tls12EcdheRsaAes128GcmSha256
| CipherSuite::Tls12EcdheRsaChacha20Poly1305Sha256 => TlsVersion::Tls12,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum KxGroup {
MlKem1024,
Secp384r1MlKem1024,
X25519MlKem768,
Secp256r1MlKem768,
Secp384r1,
Secp256r1,
X25519,
}
impl KxGroup {
pub const fn usable_in(self, v: TlsVersion) -> bool {
match v {
TlsVersion::Tls13 => true,
TlsVersion::Tls12 => match self {
KxGroup::Secp384r1 | KxGroup::Secp256r1 | KxGroup::X25519 => true,
KxGroup::MlKem1024
| KxGroup::Secp384r1MlKem1024
| KxGroup::X25519MlKem768
| KxGroup::Secp256r1MlKem768 => false,
},
}
}
pub const fn is_post_quantum(self) -> bool {
match self {
KxGroup::MlKem1024
| KxGroup::Secp384r1MlKem1024
| KxGroup::X25519MlKem768
| KxGroup::Secp256r1MlKem768 => true,
KxGroup::Secp384r1 | KxGroup::Secp256r1 | KxGroup::X25519 => false,
}
}
pub const fn iana(self) -> u16 {
match self {
KxGroup::MlKem1024 => 0x0202,
KxGroup::Secp384r1MlKem1024 => 0x11ED,
KxGroup::X25519MlKem768 => 0x11EC,
KxGroup::Secp256r1MlKem768 => 0x11EB,
KxGroup::Secp384r1 => 0x0018,
KxGroup::Secp256r1 => 0x0017,
KxGroup::X25519 => 0x001D,
}
}
pub const ALL: &'static [KxGroup] = &[
KxGroup::MlKem1024,
KxGroup::Secp384r1MlKem1024,
KxGroup::X25519MlKem768,
KxGroup::Secp256r1MlKem768,
KxGroup::Secp384r1,
KxGroup::Secp256r1,
KxGroup::X25519,
];
#[cfg(test)]
pub(crate) const fn ordinal(self) -> usize {
match self {
KxGroup::MlKem1024 => 0,
KxGroup::Secp384r1MlKem1024 => 1,
KxGroup::X25519MlKem768 => 2,
KxGroup::Secp256r1MlKem768 => 3,
KxGroup::Secp384r1 => 4,
KxGroup::Secp256r1 => 5,
KxGroup::X25519 => 6,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum HashAlgorithm {
Sha256,
Sha384,
Sha512,
Sha3_256,
Sha3_384,
Sha3_512,
}
impl HashAlgorithm {
pub const ALL: &'static [HashAlgorithm] = &[
HashAlgorithm::Sha256,
HashAlgorithm::Sha384,
HashAlgorithm::Sha512,
HashAlgorithm::Sha3_256,
HashAlgorithm::Sha3_384,
HashAlgorithm::Sha3_512,
];
#[cfg(test)]
pub(crate) const fn ordinal(self) -> usize {
match self {
HashAlgorithm::Sha256 => 0,
HashAlgorithm::Sha384 => 1,
HashAlgorithm::Sha512 => 2,
HashAlgorithm::Sha3_256 => 3,
HashAlgorithm::Sha3_384 => 4,
HashAlgorithm::Sha3_512 => 5,
}
}
pub const fn as_str(self) -> &'static str {
match self {
HashAlgorithm::Sha256 => "sha256",
HashAlgorithm::Sha384 => "sha384",
HashAlgorithm::Sha512 => "sha512",
HashAlgorithm::Sha3_256 => "sha3-256",
HashAlgorithm::Sha3_384 => "sha3-384",
HashAlgorithm::Sha3_512 => "sha3-512",
}
}
pub const fn output_len(self) -> usize {
match self {
HashAlgorithm::Sha256 | HashAlgorithm::Sha3_256 => 32,
HashAlgorithm::Sha384 | HashAlgorithm::Sha3_384 => 48,
HashAlgorithm::Sha512 | HashAlgorithm::Sha3_512 => 64,
}
}
}
impl FromStr for HashAlgorithm {
type Err = ParseAlgorithmError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
HashAlgorithm::ALL
.iter()
.copied()
.find(|a| a.as_str() == s)
.ok_or_else(|| ParseAlgorithmError(s.to_string()))
}
}
impl fmt::Display for HashAlgorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MacAlgorithm {
HmacSha256,
HmacSha384,
HmacSha512,
}
impl MacAlgorithm {
pub const ALL: &'static [MacAlgorithm] = &[
MacAlgorithm::HmacSha256,
MacAlgorithm::HmacSha384,
MacAlgorithm::HmacSha512,
];
#[cfg(test)]
pub(crate) const fn ordinal(self) -> usize {
match self {
MacAlgorithm::HmacSha256 => 0,
MacAlgorithm::HmacSha384 => 1,
MacAlgorithm::HmacSha512 => 2,
}
}
pub const fn hash(self) -> HashAlgorithm {
match self {
MacAlgorithm::HmacSha256 => HashAlgorithm::Sha256,
MacAlgorithm::HmacSha384 => HashAlgorithm::Sha384,
MacAlgorithm::HmacSha512 => HashAlgorithm::Sha512,
}
}
pub const fn tag_len(self) -> usize {
self.hash().output_len()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum KdfAlgorithm {
HkdfSha256,
HkdfSha384,
HkdfSha512,
OneStepSha256,
OneStepSha384,
OneStepSha512,
}
impl KdfAlgorithm {
pub const ALL: &'static [KdfAlgorithm] = &[
KdfAlgorithm::HkdfSha256,
KdfAlgorithm::HkdfSha384,
KdfAlgorithm::HkdfSha512,
KdfAlgorithm::OneStepSha256,
KdfAlgorithm::OneStepSha384,
KdfAlgorithm::OneStepSha512,
];
#[cfg(test)]
pub(crate) const fn ordinal(self) -> usize {
match self {
KdfAlgorithm::HkdfSha256 => 0,
KdfAlgorithm::HkdfSha384 => 1,
KdfAlgorithm::HkdfSha512 => 2,
KdfAlgorithm::OneStepSha256 => 3,
KdfAlgorithm::OneStepSha384 => 4,
KdfAlgorithm::OneStepSha512 => 5,
}
}
pub const fn hash(self) -> HashAlgorithm {
match self {
KdfAlgorithm::HkdfSha256 | KdfAlgorithm::OneStepSha256 => HashAlgorithm::Sha256,
KdfAlgorithm::HkdfSha384 | KdfAlgorithm::OneStepSha384 => HashAlgorithm::Sha384,
KdfAlgorithm::HkdfSha512 | KdfAlgorithm::OneStepSha512 => HashAlgorithm::Sha512,
}
}
pub const fn max_output_len(self) -> usize {
match self {
KdfAlgorithm::HkdfSha256 | KdfAlgorithm::HkdfSha384 | KdfAlgorithm::HkdfSha512 => {
255 * self.hash().output_len()
}
KdfAlgorithm::OneStepSha256
| KdfAlgorithm::OneStepSha384
| KdfAlgorithm::OneStepSha512 => 1 << 30,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum KeyAgreementAlgorithm {
EcdhP256,
EcdhP384,
EcdhP521,
X25519,
}
impl KeyAgreementAlgorithm {
pub const ALL: &'static [KeyAgreementAlgorithm] = &[
KeyAgreementAlgorithm::EcdhP256,
KeyAgreementAlgorithm::EcdhP384,
KeyAgreementAlgorithm::EcdhP521,
KeyAgreementAlgorithm::X25519,
];
#[cfg(test)]
pub(crate) const fn ordinal(self) -> usize {
match self {
KeyAgreementAlgorithm::EcdhP256 => 0,
KeyAgreementAlgorithm::EcdhP384 => 1,
KeyAgreementAlgorithm::EcdhP521 => 2,
KeyAgreementAlgorithm::X25519 => 3,
}
}
pub const fn as_str(self) -> &'static str {
match self {
KeyAgreementAlgorithm::EcdhP256 => "ecdh-p256",
KeyAgreementAlgorithm::EcdhP384 => "ecdh-p384",
KeyAgreementAlgorithm::EcdhP521 => "ecdh-p521",
KeyAgreementAlgorithm::X25519 => "x25519",
}
}
}
impl FromStr for KeyAgreementAlgorithm {
type Err = ParseAlgorithmError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
KeyAgreementAlgorithm::ALL
.iter()
.copied()
.find(|a| a.as_str() == s)
.ok_or_else(|| ParseAlgorithmError(s.to_string()))
}
}
impl fmt::Display for KeyAgreementAlgorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum AeadAlgorithm {
Aes128Gcm,
Aes256Gcm,
Chacha20Poly1305,
}
impl AeadAlgorithm {
pub const ALL: &'static [AeadAlgorithm] = &[
AeadAlgorithm::Aes128Gcm,
AeadAlgorithm::Aes256Gcm,
AeadAlgorithm::Chacha20Poly1305,
];
#[cfg(test)]
pub(crate) const fn ordinal(self) -> usize {
match self {
AeadAlgorithm::Aes128Gcm => 0,
AeadAlgorithm::Aes256Gcm => 1,
AeadAlgorithm::Chacha20Poly1305 => 2,
}
}
pub const fn as_str(self) -> &'static str {
match self {
AeadAlgorithm::Aes128Gcm => "aes-128-gcm",
AeadAlgorithm::Aes256Gcm => "aes-256-gcm",
AeadAlgorithm::Chacha20Poly1305 => "chacha20-poly1305",
}
}
pub const fn key_len(self) -> usize {
match self {
AeadAlgorithm::Aes128Gcm => 16,
AeadAlgorithm::Aes256Gcm | AeadAlgorithm::Chacha20Poly1305 => 32,
}
}
pub const fn max_plaintext_len(self) -> u64 {
match self {
AeadAlgorithm::Aes128Gcm | AeadAlgorithm::Aes256Gcm => (1 << 36) - 32,
AeadAlgorithm::Chacha20Poly1305 => 274_877_906_879,
}
}
}
impl FromStr for AeadAlgorithm {
type Err = ParseAlgorithmError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
AeadAlgorithm::ALL
.iter()
.copied()
.find(|a| a.as_str() == s)
.ok_or_else(|| ParseAlgorithmError(s.to_string()))
}
}
impl fmt::Display for AeadAlgorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum TlsVersion {
Tls12,
Tls13,
}
impl TlsVersion {
pub const fn iana(self) -> u16 {
match self {
TlsVersion::Tls12 => 0x0303,
TlsVersion::Tls13 => 0x0304,
}
}
pub const ALL: &'static [TlsVersion] = &[TlsVersion::Tls12, TlsVersion::Tls13];
#[cfg(test)]
pub(crate) const fn ordinal(self) -> usize {
match self {
TlsVersion::Tls12 => 0,
TlsVersion::Tls13 => 1,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Algorithm {
Signature(SignatureAlgorithm),
Scheme(TlsSignatureScheme),
Kx(KxGroup),
Cipher(CipherSuite),
Hash(HashAlgorithm),
Protocol(TlsVersion),
Mac(MacAlgorithm),
Kdf(KdfAlgorithm),
KeyAgreement(KeyAgreementAlgorithm),
Aead(AeadAlgorithm),
}
impl Algorithm {
pub fn all() -> Vec<Algorithm> {
let mut v = Vec::new();
v.extend(
SignatureAlgorithm::ALL
.iter()
.copied()
.map(Algorithm::Signature)
.collect::<Vec<_>>(),
);
v.extend(
TlsSignatureScheme::ALL
.iter()
.copied()
.map(Algorithm::Scheme)
.collect::<Vec<_>>(),
);
v.extend(
KxGroup::ALL
.iter()
.copied()
.map(Algorithm::Kx)
.collect::<Vec<_>>(),
);
v.extend(
CipherSuite::ALL
.iter()
.copied()
.map(Algorithm::Cipher)
.collect::<Vec<_>>(),
);
v.extend(
HashAlgorithm::ALL
.iter()
.copied()
.map(Algorithm::Hash)
.collect::<Vec<_>>(),
);
v.extend(
TlsVersion::ALL
.iter()
.copied()
.map(Algorithm::Protocol)
.collect::<Vec<_>>(),
);
v.extend(
MacAlgorithm::ALL
.iter()
.copied()
.map(Algorithm::Mac)
.collect::<Vec<_>>(),
);
v.extend(
KdfAlgorithm::ALL
.iter()
.copied()
.map(Algorithm::Kdf)
.collect::<Vec<_>>(),
);
v.extend(
KeyAgreementAlgorithm::ALL
.iter()
.copied()
.map(Algorithm::KeyAgreement)
.collect::<Vec<_>>(),
);
v.extend(
AeadAlgorithm::ALL
.iter()
.copied()
.map(Algorithm::Aead)
.collect::<Vec<_>>(),
);
v
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn usability_helpers_decide_per_version() {
use TlsSignatureScheme as T;
assert!(KxGroup::Secp384r1.usable_in(TlsVersion::Tls12));
assert!(!KxGroup::MlKem1024.usable_in(TlsVersion::Tls12));
assert!(KxGroup::MlKem1024.usable_in(TlsVersion::Tls13));
assert!(T::RsaPkcs1Sha256.usable_in(TlsVersion::Tls12));
assert!(!T::RsaPkcs1Sha256.usable_in(TlsVersion::Tls13));
assert!(T::MlDsa87.usable_in(TlsVersion::Tls13));
assert!(!T::MlDsa87.usable_in(TlsVersion::Tls12));
assert!(T::Ed25519.usable_in(TlsVersion::Tls12));
let c = CipherSuite::Tls12EcdheEcdsaAes256GcmSha384;
assert!(c.signs_with(T::EcdsaNistp384Sha384));
assert!(!c.signs_with(T::RsaPssSha384));
let r = CipherSuite::Tls12EcdheRsaAes256GcmSha384;
assert!(r.signs_with(T::RsaPkcs1Sha384));
assert!(!r.signs_with(T::Ed25519));
let t13 = CipherSuite::Tls13Aes256GcmSha384;
assert!(t13.signs_with(T::MlDsa87));
assert!(!t13.signs_with(T::RsaPkcs1Sha384));
}
#[test]
fn preference_is_the_canonical_order() {
let mut all = TlsSignatureScheme::ALL.to_vec();
all.sort_by_key(|t| t.preference());
assert_eq!(
all.into_iter()
.map(TlsSignatureScheme::iana)
.collect::<Vec<_>>(),
[
0x0906, 0x0503, 0x0403, 0x0603, 0x0807, 0x0805, 0x0806, 0x0804, 0x0501, 0x0601,
0x0401
]
);
let mut ranks = TlsSignatureScheme::ALL
.iter()
.copied()
.map(TlsSignatureScheme::preference)
.collect::<Vec<_>>();
ranks.sort_unstable();
assert_eq!(ranks, std::array::from_fn::<u8, 11, _>(|i| i as u8));
}
#[test]
fn every_preference_is_a_bit_of_the_handshake_scheme_mask() {
for &t in TlsSignatureScheme::ALL {
assert!(u32::from(t.preference()) < u64::BITS, "{t:?}");
assert!(
usize::from(t.preference()) < TlsSignatureScheme::ALL.len(),
"{t:?}"
);
}
}
#[test]
fn post_quantum_groups_are_the_ml_kem_ones() {
assert_eq!(
KxGroup::ALL
.iter()
.copied()
.map(KxGroup::is_post_quantum)
.collect::<Vec<_>>(),
[true, true, true, true, false, false, false]
);
}
#[test]
fn signature_names_are_stable_and_round_trip() {
assert_eq!(
SignatureAlgorithm::ALL
.iter()
.copied()
.map(SignatureAlgorithm::as_str)
.collect::<Vec<_>>(),
[
"ml-dsa-87",
"ecdsa-p256-sha256",
"ecdsa-p384-sha384",
"ecdsa-p521-sha384",
"ecdsa-p521-sha512",
"ed25519",
"rsa2048-sha384",
"rsa3072-sha384",
"rsa4096-sha384",
"rsa2048-sha384-pss",
"rsa3072-sha384-pss",
"rsa4096-sha384-pss",
]
);
for &a in SignatureAlgorithm::ALL {
assert_eq!(a.as_str().parse::<SignatureAlgorithm>().unwrap(), a);
assert_eq!(a.to_string(), a.as_str());
}
assert_eq!(
ParseAlgorithmError("p384".to_string()).to_string(),
"unknown algorithm name \"p384\""
);
for bad in [
"",
"p384",
"ML-DSA-87",
"ml-dsa-65",
"rsa-2048",
"ecdsa-p256-sha384",
"rsa3072-pss",
"rsa3072-sha256-pss",
"RSA3072-SHA384-PSS",
"rsa3072-sha384-pss ",
] {
assert_eq!(
bad.parse::<SignatureAlgorithm>(),
Err(ParseAlgorithmError(bad.to_string())),
"{bad}"
);
}
}
#[test]
fn every_key_algorithm_has_its_tls_scheme() {
use SignatureAlgorithm as S;
use TlsSignatureScheme as T;
let table = [
(S::MlDsa87, Some(T::MlDsa87)),
(S::EcdsaP256Sha256, Some(T::EcdsaNistp256Sha256)),
(S::EcdsaP384Sha384, Some(T::EcdsaNistp384Sha384)),
(S::EcdsaP521Sha384, Some(T::EcdsaNistp521Sha512)),
(S::EcdsaP521Sha512, Some(T::EcdsaNistp521Sha512)),
(S::Ed25519, Some(T::Ed25519)),
(S::Rsa2048Sha384, Some(T::RsaPssSha384)),
(S::Rsa3072Sha384, Some(T::RsaPssSha384)),
(S::Rsa4096Sha384, Some(T::RsaPssSha384)),
(S::Rsa2048Sha384Pss, None),
(S::Rsa3072Sha384Pss, None),
(S::Rsa4096Sha384Pss, None),
];
assert_eq!(table.len(), SignatureAlgorithm::ALL.len());
for (s, t) in table {
assert_eq!(s.tls_scheme(), t, "{s}");
}
}
#[test]
fn cipher_suites_know_their_version() {
use TlsVersion::{Tls12, Tls13};
assert_eq!(
CipherSuite::ALL
.iter()
.copied()
.map(CipherSuite::version)
.collect::<Vec<_>>(),
[
Tls13, Tls13, Tls13, Tls12, Tls12, Tls12, Tls12, Tls12, Tls12
]
);
}
#[test]
fn iana_codepoints_are_the_registry_values() {
use CipherSuite as C;
use KxGroup as K;
use TlsSignatureScheme as T;
assert_eq!(
C::ALL.iter().copied().map(C::iana).collect::<Vec<_>>(),
[
0x1302, 0x1301, 0x1303, 0xC02C, 0xC02B, 0xCCA9, 0xC030, 0xC02F, 0xCCA8
]
);
assert_eq!(
K::ALL.iter().copied().map(K::iana).collect::<Vec<_>>(),
[0x0202, 0x11ED, 0x11EC, 0x11EB, 0x0018, 0x0017, 0x001D]
);
assert_eq!(
T::ALL.iter().copied().map(T::iana).collect::<Vec<_>>(),
[
0x0403, 0x0503, 0x0603, 0x0807, 0x0804, 0x0805, 0x0806, 0x0401, 0x0501, 0x0601,
0x0906
]
);
assert_eq!(
TlsVersion::ALL
.iter()
.copied()
.map(TlsVersion::iana)
.collect::<Vec<_>>(),
[0x0303, 0x0304]
);
}
#[test]
fn signature_oids_are_the_registry_values() {
use SignatureAlgorithm as S;
assert_eq!(
S::ALL
.iter()
.copied()
.map(S::signature_oid)
.collect::<Vec<_>>(),
[
"2.16.840.1.101.3.4.3.19",
"1.2.840.10045.4.3.2",
"1.2.840.10045.4.3.3",
"1.2.840.10045.4.3.3",
"1.2.840.10045.4.3.4",
"1.3.101.112",
"1.2.840.113549.1.1.12",
"1.2.840.113549.1.1.12",
"1.2.840.113549.1.1.12",
"1.2.840.113549.1.1.10",
"1.2.840.113549.1.1.10",
"1.2.840.113549.1.1.10",
]
);
}
#[test]
fn hash_lengths() {
assert_eq!(
HashAlgorithm::ALL
.iter()
.copied()
.map(HashAlgorithm::output_len)
.collect::<Vec<_>>(),
[32, 48, 64, 32, 48, 64]
);
}
#[test]
fn hash_names_are_stable_and_round_trip() {
assert_eq!(
HashAlgorithm::ALL
.iter()
.copied()
.map(HashAlgorithm::as_str)
.collect::<Vec<_>>(),
[
"sha256", "sha384", "sha512", "sha3-256", "sha3-384", "sha3-512"
]
);
for &h in HashAlgorithm::ALL {
assert_eq!(h.as_str().parse::<HashAlgorithm>(), Ok(h));
assert_eq!(h.to_string(), h.as_str());
}
for bad in [
"", "SHA256", "sha-256", "sha256 ", "sha3_256", "sha3256", "sha224", "sha3-224",
"shake256",
] {
assert_eq!(
bad.parse::<HashAlgorithm>(),
Err(ParseAlgorithmError(bad.to_string())),
"{bad:?}"
);
}
}
#[test]
fn algorithm_all_is_every_variant_once() {
let all = Algorithm::all();
let expected = SignatureAlgorithm::ALL.len()
+ TlsSignatureScheme::ALL.len()
+ KxGroup::ALL.len()
+ CipherSuite::ALL.len()
+ HashAlgorithm::ALL.len()
+ TlsVersion::ALL.len()
+ MacAlgorithm::ALL.len()
+ KdfAlgorithm::ALL.len()
+ KeyAgreementAlgorithm::ALL.len()
+ AeadAlgorithm::ALL.len();
assert_eq!(all.len(), expected);
assert_eq!(expected, 12 + 11 + 7 + 9 + 6 + 2 + 3 + 6 + 4 + 3);
for (i, a) in all.iter().enumerate() {
assert!(!all[i + 1..].contains(a), "{a:?} twice");
}
assert_eq!(
all[all.len() - 16..all.len() - 13],
MacAlgorithm::ALL
.iter()
.copied()
.map(Algorithm::Mac)
.collect::<Vec<_>>()
);
assert_eq!(
all[all.len() - 13..all.len() - 7],
KdfAlgorithm::ALL
.iter()
.copied()
.map(Algorithm::Kdf)
.collect::<Vec<_>>()
);
assert_eq!(
all[all.len() - 7..all.len() - 3],
KeyAgreementAlgorithm::ALL
.iter()
.copied()
.map(Algorithm::KeyAgreement)
.collect::<Vec<_>>()
);
assert_eq!(
all[all.len() - 3..],
AeadAlgorithm::ALL
.iter()
.copied()
.map(Algorithm::Aead)
.collect::<Vec<_>>()
);
}
#[test]
fn aead_algorithm_all_lists_every_variant_in_ordinal_order() {
assert_eq!(
AeadAlgorithm::ALL,
[
AeadAlgorithm::Aes128Gcm,
AeadAlgorithm::Aes256Gcm,
AeadAlgorithm::Chacha20Poly1305,
]
);
assert_eq!(
AeadAlgorithm::ALL
.iter()
.copied()
.map(AeadAlgorithm::ordinal)
.collect::<Vec<_>>(),
std::array::from_fn::<usize, 3, _>(|i| i)
);
}
#[test]
fn aead_names_are_stable_and_round_trip() {
assert_eq!(
AeadAlgorithm::ALL
.iter()
.copied()
.map(AeadAlgorithm::as_str)
.collect::<Vec<_>>(),
["aes-128-gcm", "aes-256-gcm", "chacha20-poly1305"]
);
for &a in AeadAlgorithm::ALL {
assert_eq!(a.as_str().parse::<AeadAlgorithm>(), Ok(a));
assert_eq!(a.to_string(), a.as_str());
}
for bad in [
"",
"aes256gcm",
"AES-256-GCM",
"aes-256-gcm ",
"aes-192-gcm",
"chacha20",
] {
assert_eq!(
bad.parse::<AeadAlgorithm>(),
Err(ParseAlgorithmError(bad.to_string())),
"{bad:?}"
);
}
}
#[test]
fn each_aead_knows_its_key_and_plaintext_limits() {
assert_eq!(
AeadAlgorithm::ALL
.iter()
.map(|a| (a.key_len(), a.max_plaintext_len()))
.collect::<Vec<_>>(),
[
(16, 68_719_476_704),
(32, 68_719_476_704),
(32, 274_877_906_879)
]
);
assert_eq!(
AeadAlgorithm::Aes256Gcm.max_plaintext_len(),
((1 << 39) - 256) / 8
);
assert_eq!(
AeadAlgorithm::Chacha20Poly1305.max_plaintext_len(),
64 * ((1 << 32) - 1) - 1
);
}
#[test]
fn mac_algorithm_all_lists_every_variant_in_ordinal_order() {
assert_eq!(
MacAlgorithm::ALL,
[
MacAlgorithm::HmacSha256,
MacAlgorithm::HmacSha384,
MacAlgorithm::HmacSha512
]
);
assert_eq!(
MacAlgorithm::ALL
.iter()
.copied()
.map(MacAlgorithm::ordinal)
.collect::<Vec<_>>(),
[0, 1, 2]
);
}
#[test]
fn each_mac_names_its_hash_and_a_full_length_tag() {
assert_eq!(
MacAlgorithm::ALL
.iter()
.copied()
.map(MacAlgorithm::hash)
.collect::<Vec<_>>(),
[
HashAlgorithm::Sha256,
HashAlgorithm::Sha384,
HashAlgorithm::Sha512
]
);
assert_eq!(
MacAlgorithm::ALL
.iter()
.copied()
.map(MacAlgorithm::tag_len)
.collect::<Vec<_>>(),
[32, 48, 64]
);
}
#[test]
fn kdf_algorithm_all_lists_every_variant_in_ordinal_order() {
assert_eq!(
KdfAlgorithm::ALL,
[
KdfAlgorithm::HkdfSha256,
KdfAlgorithm::HkdfSha384,
KdfAlgorithm::HkdfSha512,
KdfAlgorithm::OneStepSha256,
KdfAlgorithm::OneStepSha384,
KdfAlgorithm::OneStepSha512,
]
);
assert_eq!(
KdfAlgorithm::ALL
.iter()
.copied()
.map(KdfAlgorithm::ordinal)
.collect::<Vec<_>>(),
std::array::from_fn::<usize, 6, _>(|i| i)
);
}
#[test]
fn each_kdf_names_its_hash_and_its_output_limit() {
use HashAlgorithm::{Sha256, Sha384, Sha512};
assert_eq!(
KdfAlgorithm::ALL
.iter()
.copied()
.map(KdfAlgorithm::hash)
.collect::<Vec<_>>(),
[Sha256, Sha384, Sha512, Sha256, Sha384, Sha512]
);
assert_eq!(
KdfAlgorithm::ALL
.iter()
.copied()
.map(KdfAlgorithm::max_output_len)
.collect::<Vec<_>>(),
[
8160,
12240,
16320,
1_073_741_824,
1_073_741_824,
1_073_741_824
]
);
}
#[test]
fn key_agreement_algorithm_all_lists_every_variant_in_ordinal_order() {
assert_eq!(
KeyAgreementAlgorithm::ALL,
[
KeyAgreementAlgorithm::EcdhP256,
KeyAgreementAlgorithm::EcdhP384,
KeyAgreementAlgorithm::EcdhP521,
KeyAgreementAlgorithm::X25519,
]
);
assert_eq!(
KeyAgreementAlgorithm::ALL
.iter()
.copied()
.map(KeyAgreementAlgorithm::ordinal)
.collect::<Vec<_>>(),
std::array::from_fn::<usize, 4, _>(|i| i)
);
}
#[test]
fn key_agreement_names_are_stable_and_round_trip() {
assert_eq!(
KeyAgreementAlgorithm::ALL
.iter()
.copied()
.map(KeyAgreementAlgorithm::as_str)
.collect::<Vec<_>>(),
["ecdh-p256", "ecdh-p384", "ecdh-p521", "x25519"]
);
for &a in KeyAgreementAlgorithm::ALL {
assert_eq!(a.as_str().parse::<KeyAgreementAlgorithm>(), Ok(a));
assert_eq!(a.to_string(), a.as_str());
}
for bad in ["", "p384", "ECDH-P384", "ecdh-p384 ", "x448", "secp384r1"] {
assert_eq!(
bad.parse::<KeyAgreementAlgorithm>(),
Err(ParseAlgorithmError(bad.to_string())),
"{bad:?}"
);
}
}
#[test]
fn signature_algorithm_all_lists_every_variant_in_ordinal_order() {
assert_eq!(
SignatureAlgorithm::ALL[9..],
[
SignatureAlgorithm::Rsa2048Sha384Pss,
SignatureAlgorithm::Rsa3072Sha384Pss,
SignatureAlgorithm::Rsa4096Sha384Pss,
]
);
assert_eq!(
SignatureAlgorithm::ALL
.iter()
.copied()
.map(SignatureAlgorithm::ordinal)
.collect::<Vec<_>>(),
std::array::from_fn::<usize, 12, _>(|i| i)
);
}
#[test]
fn tls_signature_scheme_all_lists_every_variant_in_ordinal_order() {
assert_eq!(
TlsSignatureScheme::ALL
.iter()
.copied()
.map(TlsSignatureScheme::ordinal)
.collect::<Vec<_>>(),
std::array::from_fn::<usize, 11, _>(|i| i)
);
}
#[test]
fn cipher_suite_all_lists_every_variant_in_ordinal_order() {
assert_eq!(
CipherSuite::ALL
.iter()
.copied()
.map(CipherSuite::ordinal)
.collect::<Vec<_>>(),
std::array::from_fn::<usize, 9, _>(|i| i)
);
}
#[test]
fn kx_group_all_lists_every_variant_in_ordinal_order() {
assert_eq!(
KxGroup::ALL
.iter()
.copied()
.map(KxGroup::ordinal)
.collect::<Vec<_>>(),
std::array::from_fn::<usize, 7, _>(|i| i)
);
}
#[test]
fn hash_algorithm_all_lists_every_variant_in_ordinal_order() {
assert_eq!(
HashAlgorithm::ALL,
[
HashAlgorithm::Sha256,
HashAlgorithm::Sha384,
HashAlgorithm::Sha512,
HashAlgorithm::Sha3_256,
HashAlgorithm::Sha3_384,
HashAlgorithm::Sha3_512,
]
);
assert_eq!(
HashAlgorithm::ALL
.iter()
.copied()
.map(HashAlgorithm::ordinal)
.collect::<Vec<_>>(),
std::array::from_fn::<usize, 6, _>(|i| i)
);
}
#[test]
fn tls_version_all_lists_every_variant_in_ordinal_order() {
assert_eq!(
TlsVersion::ALL
.iter()
.copied()
.map(TlsVersion::ordinal)
.collect::<Vec<_>>(),
std::array::from_fn::<usize, 2, _>(|i| i)
);
}
}