portable_rustls/tls13/
mod.rs1use core::fmt;
2
3use crate::crypto;
4use crate::crypto::hash;
5use crate::suites::{CipherSuiteCommon, SupportedCipherSuite};
6
7pub(crate) mod key_schedule;
8
9pub struct Tls13CipherSuite {
11 pub common: CipherSuiteCommon,
13
14 pub hkdf_provider: &'static dyn crypto::tls13::Hkdf,
22
23 pub aead_alg: &'static dyn crypto::cipher::Tls13AeadAlgorithm,
29
30 pub quic: Option<&'static dyn crate::quic::Algorithm>,
36}
37
38impl Tls13CipherSuite {
39 pub fn can_resume_from(&self, prev: &'static Self) -> Option<&'static Self> {
41 (prev.common.hash_provider.algorithm() == self.common.hash_provider.algorithm())
42 .then_some(prev)
43 }
44
45 #[cfg(unstable_api_not_supported)] pub fn fips(&self) -> bool {
50 let Self {
51 common,
52 hkdf_provider,
53 aead_alg,
54 quic,
55 } = self;
56 common.fips()
57 && hkdf_provider.fips()
58 && aead_alg.fips()
59 && quic.map(|q| q.fips()).unwrap_or(true)
60 }
61
62 pub fn quic_suite(&'static self) -> Option<crate::quic::Suite> {
64 self.quic
65 .map(|quic| crate::quic::Suite { quic, suite: self })
66 }
67}
68
69impl From<&'static Tls13CipherSuite> for SupportedCipherSuite {
70 fn from(s: &'static Tls13CipherSuite) -> Self {
71 Self::Tls13(s)
72 }
73}
74
75impl PartialEq for Tls13CipherSuite {
76 fn eq(&self, other: &Self) -> bool {
77 self.common.suite == other.common.suite
78 }
79}
80
81impl fmt::Debug for Tls13CipherSuite {
82 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83 f.debug_struct("Tls13CipherSuite")
84 .field("suite", &self.common.suite)
85 .finish()
86 }
87}
88
89pub(crate) fn construct_client_verify_message(handshake_hash: &hash::Output) -> VerifyMessage {
91 VerifyMessage::new(handshake_hash, CLIENT_CONSTANT)
92}
93
94pub(crate) fn construct_server_verify_message(handshake_hash: &hash::Output) -> VerifyMessage {
96 VerifyMessage::new(handshake_hash, SERVER_CONSTANT)
97}
98
99pub(crate) struct VerifyMessage {
100 buf: [u8; MAX_VERIFY_MSG],
101 used: usize,
102}
103
104impl VerifyMessage {
105 fn new(handshake_hash: &hash::Output, context_string_with_0: &[u8; 34]) -> Self {
106 let used = 64 + context_string_with_0.len() + handshake_hash.as_ref().len();
107 let mut buf = [0x20u8; MAX_VERIFY_MSG];
108
109 let (_spaces, context) = buf.split_at_mut(64);
110 let (context, hash) = context.split_at_mut(34);
111 context.copy_from_slice(context_string_with_0);
112 hash[..handshake_hash.as_ref().len()].copy_from_slice(handshake_hash.as_ref());
113
114 Self { buf, used }
115 }
116}
117
118impl AsRef<[u8]> for VerifyMessage {
119 fn as_ref(&self) -> &[u8] {
120 &self.buf[..self.used]
121 }
122}
123
124const SERVER_CONSTANT: &[u8; 34] = b"TLS 1.3, server CertificateVerify\x00";
125const CLIENT_CONSTANT: &[u8; 34] = b"TLS 1.3, client CertificateVerify\x00";
126const MAX_VERIFY_MSG: usize = 64 + CLIENT_CONSTANT.len() + hash::Output::MAX_LEN;