Skip to content

Commit

Permalink
style: fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Taowyoo committed Nov 2, 2023
1 parent bfbe0fd commit 1d79d36
Show file tree
Hide file tree
Showing 11 changed files with 377 additions and 953 deletions.
7 changes: 1 addition & 6 deletions rustls-mbedcrypto-provider/examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ fn main() {
.conn
.negotiated_cipher_suite()
.unwrap();
writeln!(
&mut std::io::stderr(),
"Current ciphersuite: {:?}",
ciphersuite.suite()
)
.unwrap();
writeln!(&mut std::io::stderr(), "Current ciphersuite: {:?}", ciphersuite.suite()).unwrap();
let mut plaintext = Vec::new();
tls.read_to_end(&mut plaintext).unwrap();
stdout().write_all(&plaintext).unwrap();
Expand Down
79 changes: 21 additions & 58 deletions rustls-mbedcrypto-provider/examples/internal/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,7 @@ impl BenchmarkParam {
ciphersuite: rustls::SupportedCipherSuite,
version: &'static rustls::SupportedProtocolVersion,
) -> Self {
Self {
key_type,
ciphersuite,
version,
}
Self { key_type, ciphersuite, version }
}
}

Expand Down Expand Up @@ -266,13 +262,11 @@ impl KeyType {
}

fn get_key(&self) -> PrivateKeyDer<'static> {
rustls_pemfile::pkcs8_private_keys(&mut io::BufReader::new(
fs::File::open(self.path_for("end.key")).unwrap(),
))
.next()
.unwrap()
.unwrap()
.into()
rustls_pemfile::pkcs8_private_keys(&mut io::BufReader::new(fs::File::open(self.path_for("end.key")).unwrap()))
.next()
.unwrap()
.unwrap()
.into()
}

fn get_client_chain(&self) -> Vec<CertificateDer<'static>> {
Expand All @@ -284,13 +278,11 @@ impl KeyType {
}

fn get_client_key(&self) -> PrivateKeyDer<'static> {
rustls_pemfile::pkcs8_private_keys(&mut io::BufReader::new(
fs::File::open(self.path_for("client.key")).unwrap(),
))
.next()
.unwrap()
.unwrap()
.into()
rustls_pemfile::pkcs8_private_keys(&mut io::BufReader::new(fs::File::open(self.path_for("client.key")).unwrap()))
.next()
.unwrap()
.unwrap()
.into()
}
}

Expand Down Expand Up @@ -335,17 +327,10 @@ fn make_server_config(
cfg
}

fn make_client_config(
params: &BenchmarkParam,
clientauth: ClientAuth,
resume: ResumptionParam,
) -> ClientConfig {
fn make_client_config(params: &BenchmarkParam, clientauth: ClientAuth, resume: ResumptionParam) -> ClientConfig {
let mut root_store = RootCertStore::empty();
let mut rootbuf =
io::BufReader::new(fs::File::open(params.key_type.path_for("ca.cert")).unwrap());
root_store.add_parsable_certificates(
rustls_pemfile::certs(&mut rootbuf).map(|result| result.unwrap()),
);
let mut rootbuf = io::BufReader::new(fs::File::open(params.key_type.path_for("ca.cert")).unwrap());
root_store.add_parsable_certificates(rustls_pemfile::certs(&mut rootbuf).map(|result| result.unwrap()));

let cfg = ClientConfig::builder_with_provider(rustls_mbedcrypto_provider::MBEDTLS)
.with_cipher_suites(&[params.ciphersuite])
Expand All @@ -355,11 +340,8 @@ fn make_client_config(
.with_root_certificates(root_store);

let mut cfg = if clientauth == ClientAuth::Yes {
cfg.with_client_auth_cert(
params.key_type.get_client_chain(),
params.key_type.get_client_key(),
)
.unwrap()
cfg.with_client_auth_cert(params.key_type.get_client_chain(), params.key_type.get_client_key())
.unwrap()
} else {
cfg.with_no_client_auth()
};
Expand Down Expand Up @@ -390,11 +372,7 @@ fn bench_handshake(params: &BenchmarkParam, clientauth: ClientAuth, resume: Resu

assert!(params.ciphersuite.version() == params.version);

let rounds = apply_work_multiplier(if resume == ResumptionParam::No {
512
} else {
4096
});
let rounds = apply_work_multiplier(if resume == ResumptionParam::No { 512 } else { 4096 });
let mut client_time = 0f64;
let mut server_time = 0f64;

Expand Down Expand Up @@ -460,11 +438,7 @@ fn do_handshake(client: &mut ClientConnection, server: &mut ServerConnection) {
}

fn bench_bulk(params: &BenchmarkParam, plaintext_size: u64, max_fragment_size: Option<usize>) {
let client_config = Arc::new(make_client_config(
params,
ClientAuth::No,
ResumptionParam::No,
));
let client_config = Arc::new(make_client_config(params, ClientAuth::No, ResumptionParam::No));
let server_config = Arc::new(make_server_config(
params,
ClientAuth::No,
Expand Down Expand Up @@ -522,17 +496,8 @@ fn bench_bulk(params: &BenchmarkParam, plaintext_size: u64, max_fragment_size: O
}

fn bench_memory(params: &BenchmarkParam, conn_count: u64) {
let client_config = Arc::new(make_client_config(
params,
ClientAuth::No,
ResumptionParam::No,
));
let server_config = Arc::new(make_server_config(
params,
ClientAuth::No,
ResumptionParam::No,
None,
));
let client_config = Arc::new(make_client_config(params, ClientAuth::No, ResumptionParam::No));
let server_config = Arc::new(make_server_config(params, ClientAuth::No, ResumptionParam::No, None));

// The target here is to end up with conn_count post-handshake
// server and client sessions.
Expand Down Expand Up @@ -573,9 +538,7 @@ fn bench_memory(params: &BenchmarkParam, conn_count: u64) {
fn lookup_matching_benches(name: &str) -> Vec<&BenchmarkParam> {
let r: Vec<&BenchmarkParam> = ALL_BENCHMARKS
.iter()
.filter(|params| {
format!("{:?}", params.ciphersuite.suite()).to_lowercase() == name.to_lowercase()
})
.filter(|params| format!("{:?}", params.ciphersuite.suite()).to_lowercase() == name.to_lowercase())
.collect();

if r.is_empty() {
Expand Down
5 changes: 1 addition & 4 deletions rustls-mbedcrypto-provider/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ pub(crate) struct MbedHashContext {
impl Clone for MbedHashContext {
fn clone(&self) -> Self {
let state = self.state.lock().unwrap();
Self {
state: Arc::new(Mutex::new(state.clone())),
hash_algo: self.hash_algo,
}
Self { state: Arc::new(Mutex::new(state.clone())), hash_algo: self.hash_algo }
}
}

Expand Down
37 changes: 11 additions & 26 deletions rustls-mbedcrypto-provider/src/kx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ impl SupportedKxGroup for KxGroup {
rustls::crypto::GetRandomFailed
})?;

fn get_key_pair(
pk: &mut PkMbed,
kx_group: &KxGroup,
) -> Result<KeyExchange, mbedtls::Error> {
fn get_key_pair(pk: &mut PkMbed, kx_group: &KxGroup) -> Result<KeyExchange, mbedtls::Error> {
let group = EcGroup::new(kx_group.agreement_algorithm.group_id)?;
let pub_key = pk
.ec_public()?
Expand Down Expand Up @@ -84,28 +81,19 @@ impl SupportedKxGroup for KxGroup {
}

/// Ephemeral ECDH on curve25519 (see RFC7748)
pub static X25519: &dyn SupportedKxGroup = &KxGroup {
name: NamedGroup::X25519,
agreement_algorithm: &agreement::X25519,
};
pub static X25519: &dyn SupportedKxGroup = &KxGroup { name: NamedGroup::X25519, agreement_algorithm: &agreement::X25519 };

/// Ephemeral ECDH on secp256r1 (aka NIST-P256)
pub static SECP256R1: &dyn SupportedKxGroup = &KxGroup {
name: NamedGroup::secp256r1,
agreement_algorithm: &agreement::ECDH_P256,
};
pub static SECP256R1: &dyn SupportedKxGroup =
&KxGroup { name: NamedGroup::secp256r1, agreement_algorithm: &agreement::ECDH_P256 };

/// Ephemeral ECDH on secp384r1 (aka NIST-P384)
pub static SECP384R1: &dyn SupportedKxGroup = &KxGroup {
name: NamedGroup::secp384r1,
agreement_algorithm: &agreement::ECDH_P384,
};
pub static SECP384R1: &dyn SupportedKxGroup =
&KxGroup { name: NamedGroup::secp384r1, agreement_algorithm: &agreement::ECDH_P384 };

/// Ephemeral ECDH on secp521r1 (aka NIST-P521)
pub static SECP521R1: &dyn SupportedKxGroup = &KxGroup {
name: NamedGroup::secp521r1,
agreement_algorithm: &agreement::ECDH_P521,
};
pub static SECP521R1: &dyn SupportedKxGroup =
&KxGroup { name: NamedGroup::secp521r1, agreement_algorithm: &agreement::ECDH_P521 };

/// A list of all the key exchange groups supported by mbedtls.
pub static ALL_KX_GROUPS: &[&dyn SupportedKxGroup] = &[X25519, SECP256R1, SECP384R1, SECP521R1];
Expand All @@ -130,18 +118,15 @@ impl crypto::ActiveKeyExchange for KeyExchange {
let ec_group = EcGroup::new(group_id).map_err(mbedtls_err_to_rustls_error)?;
let private_key = Mpi::from_binary(&self.priv_key).map_err(mbedtls_err_to_rustls_error)?;

let mut sk = PkMbed::private_from_ec_components(ec_group.clone(), private_key)
.map_err(mbedtls_err_to_rustls_error)?;
let mut sk = PkMbed::private_from_ec_components(ec_group.clone(), private_key).map_err(mbedtls_err_to_rustls_error)?;
if peer_public_key.len() != self.agreement_algorithm.public_key_len {
return Err(Error::General(format!(
"Failed to validate {:?} comping peer public key, invalid length",
group_id
)));
}
let public_point = EcPoint::from_binary_no_compress(&ec_group, peer_public_key)
.map_err(mbedtls_err_to_rustls_error)?;
let peer_pk = PkMbed::public_from_ec_components(ec_group.clone(), public_point)
.map_err(mbedtls_err_to_rustls_error)?;
let public_point = EcPoint::from_binary_no_compress(&ec_group, peer_public_key).map_err(mbedtls_err_to_rustls_error)?;
let peer_pk = PkMbed::public_from_ec_components(ec_group.clone(), public_point).map_err(mbedtls_err_to_rustls_error)?;

let mut shared_secret = vec![
0u8;
Expand Down
Loading

0 comments on commit 1d79d36

Please sign in to comment.