use crate::crypto::cipher::{
make_tls13_aad, AeadKey, Iv, MessageDecrypter, MessageEncrypter, Nonce, Tls13AeadAlgorithm,
};
use crate::enums::{CipherSuite, ContentType, ProtocolVersion};
use crate::error::Error;
use crate::msgs::codec::Codec;
use crate::msgs::message::{BorrowedPlainMessage, OpaqueMessage, PlainMessage};
#[cfg(feature = "secret_extraction")]
use crate::suites::ConnectionTrafficSecrets;
use crate::suites::{CipherSuiteCommon, SupportedCipherSuite};
use crate::tls13::Tls13CipherSuite;
use ring::aead;
pub static TLS13_CHACHA20_POLY1305_SHA256: SupportedCipherSuite =
SupportedCipherSuite::Tls13(TLS13_CHACHA20_POLY1305_SHA256_INTERNAL);
pub(crate) static TLS13_CHACHA20_POLY1305_SHA256_INTERNAL: &Tls13CipherSuite = &Tls13CipherSuite {
common: CipherSuiteCommon {
suite: CipherSuite::TLS13_CHACHA20_POLY1305_SHA256,
hash_provider: &super::hash::SHA256,
},
hmac_provider: &super::hmac::HMAC_SHA256,
aead_alg: &Chacha20Poly1305Aead(AeadAlgorithm(&ring::aead::CHACHA20_POLY1305)),
#[cfg(feature = "quic")]
confidentiality_limit: u64::MAX,
#[cfg(feature = "quic")]
integrity_limit: 1 << 36,
#[cfg(feature = "quic")]
quic: &super::quic::KeyBuilder(&ring::aead::CHACHA20_POLY1305, &ring::aead::quic::CHACHA20),
};
pub static TLS13_AES_256_GCM_SHA384: SupportedCipherSuite =
SupportedCipherSuite::Tls13(&Tls13CipherSuite {
common: CipherSuiteCommon {
suite: CipherSuite::TLS13_AES_256_GCM_SHA384,
hash_provider: &super::hash::SHA384,
},
hmac_provider: &super::hmac::HMAC_SHA384,
aead_alg: &Aes256GcmAead(AeadAlgorithm(&ring::aead::AES_256_GCM)),
#[cfg(feature = "quic")]
confidentiality_limit: 1 << 23,
#[cfg(feature = "quic")]
integrity_limit: 1 << 52,
#[cfg(feature = "quic")]
quic: &super::quic::KeyBuilder(&ring::aead::AES_256_GCM, &aead::quic::AES_256),
});
pub static TLS13_AES_128_GCM_SHA256: SupportedCipherSuite =
SupportedCipherSuite::Tls13(TLS13_AES_128_GCM_SHA256_INTERNAL);
pub(crate) static TLS13_AES_128_GCM_SHA256_INTERNAL: &Tls13CipherSuite = &Tls13CipherSuite {
common: CipherSuiteCommon {
suite: CipherSuite::TLS13_AES_128_GCM_SHA256,
hash_provider: &super::hash::SHA256,
},
hmac_provider: &super::hmac::HMAC_SHA256,
aead_alg: &Aes128GcmAead(AeadAlgorithm(&ring::aead::AES_128_GCM)),
#[cfg(feature = "quic")]
confidentiality_limit: 1 << 23,
#[cfg(feature = "quic")]
integrity_limit: 1 << 52,
#[cfg(feature = "quic")]
quic: &super::quic::KeyBuilder(&ring::aead::AES_128_GCM, &aead::quic::AES_128),
};
struct Chacha20Poly1305Aead(AeadAlgorithm);
impl Tls13AeadAlgorithm for Chacha20Poly1305Aead {
fn encrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageEncrypter> {
self.0.encrypter(key, iv)
}
fn decrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageDecrypter> {
self.0.decrypter(key, iv)
}
fn key_len(&self) -> usize {
self.0.key_len()
}
#[cfg(feature = "secret_extraction")]
fn extract_keys(&self, key: AeadKey, iv: Iv) -> ConnectionTrafficSecrets {
let (key, iv) = (slice_to_array(key.as_ref()), slice_to_array(&iv.0));
ConnectionTrafficSecrets::Chacha20Poly1305 { key, iv }
}
}
struct Aes256GcmAead(AeadAlgorithm);
impl Tls13AeadAlgorithm for Aes256GcmAead {
fn encrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageEncrypter> {
self.0.encrypter(key, iv)
}
fn decrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageDecrypter> {
self.0.decrypter(key, iv)
}
fn key_len(&self) -> usize {
self.0.key_len()
}
#[cfg(feature = "secret_extraction")]
fn extract_keys(&self, key: AeadKey, iv: Iv) -> ConnectionTrafficSecrets {
let (key, salt, iv) = slices_to_arrays(key.as_ref(), &iv.0[..4], &iv.0[4..]);
ConnectionTrafficSecrets::Aes256Gcm { key, salt, iv }
}
}
struct Aes128GcmAead(AeadAlgorithm);
impl Tls13AeadAlgorithm for Aes128GcmAead {
fn encrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageEncrypter> {
self.0.encrypter(key, iv)
}
fn decrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageDecrypter> {
self.0.decrypter(key, iv)
}
fn key_len(&self) -> usize {
self.0.key_len()
}
#[cfg(feature = "secret_extraction")]
fn extract_keys(&self, key: AeadKey, iv: Iv) -> ConnectionTrafficSecrets {
let (key, salt, iv) = slices_to_arrays(key.as_ref(), &iv.0[..4], &iv.0[4..]);
ConnectionTrafficSecrets::Aes128Gcm { key, salt, iv }
}
}
struct AeadAlgorithm(&'static ring::aead::Algorithm);
impl AeadAlgorithm {
fn encrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageEncrypter> {
Box::new(Tls13MessageEncrypter {
enc_key: aead::LessSafeKey::new(aead::UnboundKey::new(self.0, key.as_ref()).unwrap()),
iv,
})
}
fn decrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageDecrypter> {
Box::new(Tls13MessageDecrypter {
dec_key: aead::LessSafeKey::new(aead::UnboundKey::new(self.0, key.as_ref()).unwrap()),
iv,
})
}
fn key_len(&self) -> usize {
self.0.key_len()
}
}
struct Tls13MessageEncrypter {
enc_key: ring::aead::LessSafeKey,
iv: Iv,
}
struct Tls13MessageDecrypter {
dec_key: ring::aead::LessSafeKey,
iv: Iv,
}
impl MessageEncrypter for Tls13MessageEncrypter {
fn encrypt(&self, msg: BorrowedPlainMessage, seq: u64) -> Result<OpaqueMessage, Error> {
let total_len = msg.payload.len() + 1 + self.enc_key.algorithm().tag_len();
let mut payload = Vec::with_capacity(total_len);
payload.extend_from_slice(msg.payload);
msg.typ.encode(&mut payload);
let nonce = aead::Nonce::assume_unique_for_key(Nonce::new(&self.iv, seq).0);
let aad = aead::Aad::from(make_tls13_aad(total_len));
self.enc_key
.seal_in_place_append_tag(nonce, aad, &mut payload)
.map_err(|_| Error::EncryptError)?;
Ok(OpaqueMessage::new(
ContentType::ApplicationData,
ProtocolVersion::TLSv1_2,
payload,
))
}
}
impl MessageDecrypter for Tls13MessageDecrypter {
fn decrypt(&self, mut msg: OpaqueMessage, seq: u64) -> Result<PlainMessage, Error> {
let payload = msg.payload_mut();
if payload.len() < self.dec_key.algorithm().tag_len() {
return Err(Error::DecryptError);
}
let nonce = aead::Nonce::assume_unique_for_key(Nonce::new(&self.iv, seq).0);
let aad = aead::Aad::from(make_tls13_aad(payload.len()));
let plain_len = self
.dec_key
.open_in_place(nonce, aad, payload)
.map_err(|_| Error::DecryptError)?
.len();
payload.truncate(plain_len);
msg.into_tls13_unpadded_message()
}
}
#[cfg(feature = "secret_extraction")]
fn slices_to_arrays<const NK: usize, const NS: usize, const NI: usize>(
k: &[u8],
s: &[u8],
i: &[u8],
) -> ([u8; NK], [u8; NS], [u8; NI]) {
(slice_to_array(k), slice_to_array(s), slice_to_array(i))
}
#[cfg(feature = "secret_extraction")]
fn slice_to_array<const N: usize>(slice: &[u8]) -> [u8; N] {
debug_assert_eq!(N, slice.len());
slice.try_into().unwrap()
}