Debian Codegraph @

crt/s2n/bindings/rust/standard/benchmarks/src/lib.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 //! This module holds all of the "benchmark specific" configuration logic that is
5 //! used in the benchmark suites.
6 //!
7 //! Anything that is only relevant to the benchmarks should live in this module.
8 //! Similarly, anything that might be used outside of the benchmarks should _not_
9 //! live in this module.
10
11 use std::{error::Error, fmt::Debug};
12
13 mod setup;
14 #[cfg(test)]
15 mod test_utilities;
16
17 use tls_harness::{Mode, SigType, TlsConnPair, TlsConnection};
18
19 /// While ServerAuth and Resumption are not mutually exclusive, they are treated
20 /// as such for the purpose of benchmarking.
21 #[derive(Clone, Copy, Default, Eq, PartialEq, strum::EnumIter)]
22 pub enum HandshakeType {
23 #[default]
24 ServerAuth,
25 MutualAuth,
26 Resumption,
27 }
28
29 impl Debug for HandshakeType {
30 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31 match self {
32 HandshakeType::ServerAuth => write!(f, "server-auth"),
33 HandshakeType::MutualAuth => write!(f, "mTLS"),
34 HandshakeType::Resumption => write!(f, "resumption"),
35 }
36 }
37 }
38
39 // these parameters were the only ones readily usable for all three libaries:
40 // s2n-tls, rustls, and openssl
41 #[allow(non_camel_case_types)]
42 #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, strum::EnumIter)]
43 pub enum CipherSuite {
44 #[default]
45 TLS_AES_128_GCM_SHA256,
46 TLS_AES_256_GCM_SHA384,
47 }
48
49 #[derive(Clone, Copy, Default, strum::EnumIter)]
50 pub enum KXGroup {
51 Secp256R1,
52 #[default]
53 X25519,
54 X25519MLKEM768,
55 }
56
57 impl Debug for KXGroup {
58 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59 match self {
60 Self::Secp256R1 => write!(f, "secp256r1"),
61 Self::X25519 => write!(f, "x25519"),
62 Self::X25519MLKEM768 => write!(f, "X25519MLKEM768"),
63 }
64 }
65 }
66
67 #[derive(Clone, Copy, Debug, Default)]
68 pub struct CryptoConfig {
69 pub cipher_suite: CipherSuite,
70 pub kx_group: KXGroup,
71 pub sig_type: SigType,
72 }
73
74 impl CryptoConfig {
75 pub fn new(cipher_suite: CipherSuite, kx_group: KXGroup, sig_type: SigType) -> Self {
76 Self {
77 cipher_suite,
78 kx_group,
79 sig_type,
80 }
81 }
82 }
83
84 /// The TlsBenchConfig trait allows us to map benchmarking parameters to
85 /// a configuration object
86 pub trait TlsBenchConfig: Sized {
87 fn make_config(
88 mode: Mode,
89 crypto_config: CryptoConfig,
90 handshake_type: HandshakeType,
91 ) -> Result<Self, Box<dyn Error>>;
92 }
93
94 /// Initialize buffers, configs, and connections (pre-handshake)
95 pub fn new_bench_pair<C, S>(
96 crypto_config: CryptoConfig,
97 handshake_type: HandshakeType,
98 ) -> Result<TlsConnPair<C, S>, Box<dyn Error>>
99 where
100 C: TlsConnection,
101 S: TlsConnection,
102 C::Config: TlsBenchConfig,
103 S::Config: TlsBenchConfig,
104 {
105 // do an initial handshake to generate the session ticket
106 if handshake_type == HandshakeType::Resumption {
107 let server_config = S::Config::make_config(Mode::Server, crypto_config, handshake_type)?;
108 let client_config = C::Config::make_config(Mode::Client, crypto_config, handshake_type)?;
109
110 // handshake the client and server connections. This will result in
111 // session ticket getting stored in client_config
112 let mut pair = TlsConnPair::<C, S>::from_configs(&client_config, &server_config);
113 pair.handshake()?;
114 // NewSessionTicket messages are part of the application data and sent
115 // after the handshake is complete, so we must trigger an additional
116 // "read" on the client connection to ensure that the session ticket
117 // gets received and stored in the config
118 pair.round_trip_transfer(&mut [0]).unwrap();
119 // OpenSSL doesn't allow resumption unless the session was cleanly shutdown
120 pair.shutdown().unwrap();
121
122 // new_from_config is called interally by the TlsConnPair::new
123 // method and will check if a session ticket is available and set it
124 // on the connection. This results in the session ticket in
125 // client_config (from the previous handshake) getting set on the
126 // client connection.
127 return Ok(TlsConnPair::<C, S>::from_configs(
128 &client_config,
129 &server_config,
130 ));
131 }
132
133 Ok(TlsConnPair::<C, S>::from_configs(
134 &C::Config::make_config(Mode::Client, crypto_config, handshake_type).unwrap(),
135 &S::Config::make_config(Mode::Server, crypto_config, handshake_type).unwrap(),
136 ))
137 }
138
139 #[cfg(test)]
140 mod tests {
141 use super::*;
142 use std::io::ErrorKind;
143 use tls_harness::{
144 cohort::{OpenSslConnection, RustlsConnection, S2NConnection},
145 harness::TlsInfo,
146 TlsConnection,
147 };
148
149 #[test]
150 fn rustls_handshakes() {
151 test_utilities::all_handshakes::<RustlsConnection>();
152 }
153
154 #[test]
155 fn openssl_handshakes() {
156 test_utilities::all_handshakes::<OpenSslConnection>();
157 }
158
159 #[test]
160 fn s2n_handshakes() {
161 test_utilities::all_handshakes::<S2NConnection>();
162 }
163
164 #[test]
165 fn rustls_transfer() {
166 test_utilities::transfer::<RustlsConnection>();
167 }
168
169 #[test]
170 fn openssl_transfer() {
171 test_utilities::transfer::<OpenSslConnection>();
172 }
173
174 #[test]
175 fn s2n_transfer() {
176 test_utilities::transfer::<S2NConnection>();
177 }
178
179 fn session_resumption<C, S>()
180 where
181 S: TlsConnection + TlsInfo,
182 C: TlsConnection + TlsInfo,
183 C::Config: TlsBenchConfig,
184 S::Config: TlsBenchConfig,
185 {
186 println!("testing with client:{} server:{}", C::name(), S::name());
187 let mut conn_pair =
188 new_bench_pair::<C, S>(CryptoConfig::default(), HandshakeType::Resumption).unwrap();
189 conn_pair.handshake().unwrap();
190 // read the session tickets which were sent
191 let err = conn_pair.client_mut().recv(&mut [0]).unwrap_err();
192 assert_eq!(err.kind(), ErrorKind::WouldBlock);
193
194 assert!(conn_pair.server().resumed_connection());
195 conn_pair.shutdown().unwrap();
196 }
197
198 #[test]
199 fn session_resumption_interop() {
200 env_logger::builder()
201 .filter_level(log::LevelFilter::Debug)
202 .is_test(true)
203 .try_init()
204 .unwrap();
205 session_resumption::<S2NConnection, S2NConnection>();
206 session_resumption::<S2NConnection, RustlsConnection>();
207 session_resumption::<S2NConnection, OpenSslConnection>();
208
209 session_resumption::<RustlsConnection, RustlsConnection>();
210 session_resumption::<RustlsConnection, S2NConnection>();
211 session_resumption::<RustlsConnection, OpenSslConnection>();
212
213 session_resumption::<OpenSslConnection, OpenSslConnection>();
214 session_resumption::<OpenSslConnection, S2NConnection>();
215 session_resumption::<OpenSslConnection, RustlsConnection>();
216 }
217 }