use std::fmt;
use std::str::FromStr;
use crypto::{AeadAlgorithm, HmacAlgorithm, StreamCipherAlgorithm};
pub const NONCE_LEN: usize = 12;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CipherSuite {
Aes128CtrHmacSha256_80,
Aes128CtrHmacSha256_64,
Aes128CtrHmacSha256_32,
Aes128GcmSha256_128,
Aes256GcmSha512_128,
Aes256CtrHmacSha512_80,
Aes256CtrHmacSha512_64,
Aes256CtrHmacSha512_32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AeadKind {
Gcm(AeadAlgorithm),
CtrHmac(StreamCipherAlgorithm),
}
impl CipherSuite {
pub const ALL: [CipherSuite; 8] = [
Self::Aes128CtrHmacSha256_80,
Self::Aes128CtrHmacSha256_64,
Self::Aes128CtrHmacSha256_32,
Self::Aes128GcmSha256_128,
Self::Aes256GcmSha512_128,
Self::Aes256CtrHmacSha512_80,
Self::Aes256CtrHmacSha512_64,
Self::Aes256CtrHmacSha512_32,
];
#[must_use]
pub const fn id(self) -> u16 {
match self {
Self::Aes128CtrHmacSha256_80 => 0x0001,
Self::Aes128CtrHmacSha256_64 => 0x0002,
Self::Aes128CtrHmacSha256_32 => 0x0003,
Self::Aes128GcmSha256_128 => 0x0004,
Self::Aes256GcmSha512_128 => 0x0005,
Self::Aes256CtrHmacSha512_80 => 0x0006,
Self::Aes256CtrHmacSha512_64 => 0x0007,
Self::Aes256CtrHmacSha512_32 => 0x0008,
}
}
#[must_use]
pub const fn from_id(id: u16) -> Option<Self> {
Some(match id {
0x0001 => Self::Aes128CtrHmacSha256_80,
0x0002 => Self::Aes128CtrHmacSha256_64,
0x0003 => Self::Aes128CtrHmacSha256_32,
0x0004 => Self::Aes128GcmSha256_128,
0x0005 => Self::Aes256GcmSha512_128,
0x0006 => Self::Aes256CtrHmacSha512_80,
0x0007 => Self::Aes256CtrHmacSha512_64,
0x0008 => Self::Aes256CtrHmacSha512_32,
_ => return None,
})
}
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Aes128CtrHmacSha256_80 => "AES_128_CTR_HMAC_SHA256_80",
Self::Aes128CtrHmacSha256_64 => "AES_128_CTR_HMAC_SHA256_64",
Self::Aes128CtrHmacSha256_32 => "AES_128_CTR_HMAC_SHA256_32",
Self::Aes128GcmSha256_128 => "AES_128_GCM_SHA256_128",
Self::Aes256GcmSha512_128 => "AES_256_GCM_SHA512_128",
Self::Aes256CtrHmacSha512_80 => "AES_256_CTR_HMAC_SHA512_80",
Self::Aes256CtrHmacSha512_64 => "AES_256_CTR_HMAC_SHA512_64",
Self::Aes256CtrHmacSha512_32 => "AES_256_CTR_HMAC_SHA512_32",
}
}
#[must_use]
pub const fn hash_len(self) -> usize {
match self {
Self::Aes128CtrHmacSha256_80
| Self::Aes128CtrHmacSha256_64
| Self::Aes128CtrHmacSha256_32
| Self::Aes128GcmSha256_128 => 32,
Self::Aes256GcmSha512_128
| Self::Aes256CtrHmacSha512_80
| Self::Aes256CtrHmacSha512_64
| Self::Aes256CtrHmacSha512_32 => 64,
}
}
#[must_use]
pub const fn aes_ctr_key_len(self) -> Option<usize> {
match self {
Self::Aes128CtrHmacSha256_80
| Self::Aes128CtrHmacSha256_64
| Self::Aes128CtrHmacSha256_32 => Some(16),
Self::Aes256CtrHmacSha512_80
| Self::Aes256CtrHmacSha512_64
| Self::Aes256CtrHmacSha512_32 => Some(32),
Self::Aes128GcmSha256_128 | Self::Aes256GcmSha512_128 => None,
}
}
#[must_use]
pub const fn key_len(self) -> usize {
match self {
Self::Aes128GcmSha256_128 => 16,
Self::Aes256GcmSha512_128 => 32,
Self::Aes128CtrHmacSha256_80
| Self::Aes128CtrHmacSha256_64
| Self::Aes128CtrHmacSha256_32 => 16 + 32,
Self::Aes256CtrHmacSha512_80
| Self::Aes256CtrHmacSha512_64
| Self::Aes256CtrHmacSha512_32 => 32 + 64,
}
}
#[must_use]
pub const fn nonce_len(self) -> usize {
NONCE_LEN
}
#[must_use]
pub const fn tag_len(self) -> usize {
match self {
Self::Aes128CtrHmacSha256_80 | Self::Aes256CtrHmacSha512_80 => 10,
Self::Aes128CtrHmacSha256_64 | Self::Aes256CtrHmacSha512_64 => 8,
Self::Aes128CtrHmacSha256_32 | Self::Aes256CtrHmacSha512_32 => 4,
Self::Aes128GcmSha256_128 | Self::Aes256GcmSha512_128 => 16,
}
}
pub(crate) const fn hmac(self) -> HmacAlgorithm {
match self.hash_len() {
32 => HmacAlgorithm::Sha256,
_ => HmacAlgorithm::Sha512,
}
}
pub(crate) const fn aead_kind(self) -> AeadKind {
match self {
Self::Aes128GcmSha256_128 => AeadKind::Gcm(AeadAlgorithm::Aes128Gcm),
Self::Aes256GcmSha512_128 => AeadKind::Gcm(AeadAlgorithm::Aes256Gcm),
Self::Aes128CtrHmacSha256_80
| Self::Aes128CtrHmacSha256_64
| Self::Aes128CtrHmacSha256_32 => AeadKind::CtrHmac(StreamCipherAlgorithm::Aes128Ctr),
Self::Aes256CtrHmacSha512_80
| Self::Aes256CtrHmacSha512_64
| Self::Aes256CtrHmacSha512_32 => AeadKind::CtrHmac(StreamCipherAlgorithm::Aes256Ctr),
}
}
}
impl fmt::Display for CipherSuite {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.name())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[error("unknown SFrame cipher suite name")]
pub struct UnknownCipherSuite;
impl FromStr for CipherSuite {
type Err = UnknownCipherSuite;
fn from_str(name: &str) -> Result<Self, Self::Err> {
Self::ALL
.into_iter()
.find(|suite| suite.name() == name)
.ok_or(UnknownCipherSuite)
}
}
impl TryFrom<u16> for CipherSuite {
type Error = UnknownCipherSuite;
fn try_from(id: u16) -> Result<Self, Self::Error> {
Self::from_id(id).ok_or(UnknownCipherSuite)
}
}
impl From<CipherSuite> for u16 {
fn from(suite: CipherSuite) -> Self {
suite.id()
}
}