Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ tikv-jemallocator = "0.6"
tokio = { version = "1.34", features = ["io-util", "macros", "net", "rt"]}
webpki = { package = "rustls-webpki", version = "0.102.8", features = ["alloc"], default-features = false }
webpki-roots = "0.26"
x25519-dalek = "2"
x25519-dalek = { version = "2", features = ["static_secrets"] }
x509-parser = "0.16"
zeroize = "1.7"
zlib-rs = "0.4"
Expand Down
3 changes: 2 additions & 1 deletion rustls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ log = { workspace = true, optional = true }
once_cell = { version = "1.16", default-features = false, features = ["alloc", "race"] }
ring = { workspace = true, optional = true }
subtle = { workspace = true }
x25519-dalek = { workspace = true, optional = true }
webpki = { workspace = true }
pki-types = { workspace = true }
zeroize = { workspace = true }
Expand All @@ -39,7 +40,7 @@ logging = ["log"]
aws_lc_rs = ["dep:aws-lc-rs", "webpki/aws_lc_rs"]
aws-lc-rs = ["aws_lc_rs"] # Alias because Cargo features commonly use `-`
brotli = ["dep:brotli", "dep:brotli-decompressor", "std"]
ring = ["dep:ring", "webpki/ring"]
ring = ["dep:ring", "webpki/ring", "dep:x25519-dalek"]
custom-provider = []
tls12 = []
read_buf = ["rustversion", "std"]
Expand Down
38 changes: 10 additions & 28 deletions rustls/src/client/reality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,50 +148,32 @@ impl core::fmt::Display for RealityConfigError {
#[cfg(feature = "std")]
impl std::error::Error for RealityConfigError {}

/// Generate X25519 keypair using ring
/// Generate X25519 keypair using x25519-dalek (ring feature)
#[cfg(all(feature = "ring", not(feature = "aws_lc_rs")))]
fn x25519_generate_keypair(
secure_random: &dyn SecureRandom,
) -> Result<([u8; 32], [u8; 32]), Error> {
use ring::agreement;
use x25519_dalek::{PublicKey, StaticSecret};

// Generate random private key
let mut private_bytes = [0u8; 32];
secure_random.fill(&mut private_bytes)?;

// Compute public key from private key using PrivateKey
let private_key = agreement::PrivateKey::from_private_key(&agreement::X25519, &private_bytes)
.map_err(|_| Error::General("X25519 private key creation failed".into()))?;

let public_key_bytes = private_key
.compute_public_key()
.map_err(|_| Error::General("X25519 public key computation failed".into()))?;

let mut public = [0u8; 32];
public.copy_from_slice(public_key_bytes.as_ref());
let secret = StaticSecret::from(private_bytes);
let public: [u8; 32] = PublicKey::from(&secret).to_bytes();

Ok((private_bytes, public))
}

/// Perform X25519 ECDH using ring
/// Perform X25519 ECDH using x25519-dalek (ring feature)
#[cfg(all(feature = "ring", not(feature = "aws_lc_rs")))]
fn x25519_ecdh(private_key: &[u8; 32], peer_public_key: &[u8; 32]) -> Result<[u8; 32], Error> {
use ring::agreement;
use x25519_dalek::{PublicKey, StaticSecret};

let private_key = agreement::PrivateKey::from_private_key(&agreement::X25519, private_key)
.map_err(|_| Error::General("X25519 private key creation failed".into()))?;

let peer_public =
agreement::UnparsedPublicKey::new(&agreement::X25519, peer_public_key.as_ref());
let secret = StaticSecret::from(*private_key);
let peer_public = PublicKey::from(*peer_public_key);
let shared = secret.diffie_hellman(&peer_public);

let mut shared_secret = [0u8; 32];
agreement::agree(&private_key, &peer_public, |key_material| {
shared_secret.copy_from_slice(key_material);
Ok(())
})
.map_err(|_| Error::General("X25519 ECDH failed".into()))?;

Ok(shared_secret)
Ok(shared.to_bytes())
}

/// Generate X25519 keypair using aws-lc-rs
Expand Down