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
32 changes: 22 additions & 10 deletions Cargo.lock

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

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ rcgen = { version = "0.14", default-features = false }
# default-features off so each consumer picks its own TLS backend and body
# features; nothing here wants the default native-tls stack.
reqwest = { version = "0.13", default-features = false }
rind = "0.1.12"
rind-jsonwebtoken = "0.1.2"
rind-object-store = "0.1.2"
rind-rcgen = "0.1.12"
rind-rustls = "0.1.12"
rind = { version = "0.3.1", features = ["wipe"] }
rind-jsonwebtoken = "11.0.2"
rind-object-store = "0.14.2"
rind-rcgen = "0.14.2"
rind-rustls = "0.23.2"
rustc-hash = "2.1.3"
rustls-native-certs = "0.8.5"
sd-notify = "0.5"
Expand Down
9 changes: 6 additions & 3 deletions rs/moq-auth/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
/// rind panics on such a CPU instead, so every key runs this before its first operation.
pub(crate) fn init() -> crate::Result<()> {
moq_crypto::cpu::check()?;
let provider = rind_jsonwebtoken::default_provider()
.expect("rind-jsonwebtoken refuses only the CPUs rind::cpu::check refuses");
// An error means the process already picked a provider, which it is entitled to.
let _ = rind_jsonwebtoken::DEFAULT_PROVIDER.install_default();
let _ = provider.install_default();
Ok(())
}

Expand All @@ -32,7 +34,8 @@ mod tests {
#[test]
fn installs_rind_when_none_is_installed() {
super::init().unwrap();
let installed = rind_jsonwebtoken::DEFAULT_PROVIDER.install_default().unwrap_err();
assert!(std::ptr::eq(installed, &rind_jsonwebtoken::DEFAULT_PROVIDER));
let rind = rind_jsonwebtoken::default_provider().unwrap();
let installed = rind.install_default().unwrap_err();
assert!(std::ptr::eq(installed, rind));
}
}
5 changes: 3 additions & 2 deletions rs/moq-auth/src/generate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{Algorithm, EllipticCurve, Error, Jwk, Key, KeyMaterial, RsaPublicKey};
use pkcs1::der::Decode as _;
use pkcs8::der::Decode as _;
use rind::signature::{EcdsaKeyPair, EcdsaSigningAlgorithm, Ed25519KeyPair, RsaKeyPair, RsaKeySize, sign};
use rind::rsa::KeySize;
use rind::signature::{EcdsaKeyPair, EcdsaSigningAlgorithm, Ed25519KeyPair, RsaKeyPair, sign};

/// Generate a key pair for the given algorithm, returning the private and public keys.
pub fn generate(algorithm: Algorithm, id: Option<crate::KeyId>) -> crate::Result<Key> {
Expand Down Expand Up @@ -35,7 +36,7 @@ fn generate_hmac_key<const SIZE: usize>() -> crate::Result<KeyMaterial> {

// rind exports an RSA private key only as PKCS#8, so each JWK field is read back out of it.
fn generate_rsa_key() -> crate::Result<KeyMaterial> {
let pkcs8 = RsaKeyPair::generate(RsaKeySize::Rsa2048)
let pkcs8 = RsaKeyPair::generate(KeySize::Rsa2048)
.map_err(Error::crypto)?
.to_pkcs8();
let info = pkcs8::PrivateKeyInfoRef::from_der(pkcs8.as_ref()).map_err(Error::crypto)?;
Expand Down
4 changes: 2 additions & 2 deletions rs/moq-auth/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,8 @@ impl Key {

// rind refuses a `d` whose public key is not this `x` and `y`.
let point = [&[0x04][..], x.as_slice(), y.as_slice()].concat();
let pair = EcdsaKeyPair::from_private_key_and_public_key(algorithm, d, &point)
.map_err(crate::Error::crypto)?;
let pair =
EcdsaKeyPair::from_scalar_and_public_key(algorithm, d, &point).map_err(crate::Error::crypto)?;
EncodingKey::from_ec_der(pair.to_pkcs8().as_ref())
}
KeyMaterial::OKP {
Expand Down
6 changes: 6 additions & 0 deletions rs/moq-crypto/src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
#[error("unsupported CPU: {0}")]
pub struct UnsupportedCpu(pub(crate) rind::cpu::MissingFeature);

/// Holds data-independent timing on for this thread until dropped.
///
/// rind otherwise turns DIT on and off around every call that handles a secret, which costs
/// about 50 ns a call on Apple silicon, so a transport holds one across each batch of packets.
pub use rind::cpu::Dit;

/// Refuse a CPU that rind cannot run on, naming the missing feature.
///
/// Call this before using rind's primitives directly; the rustls provider checks it too.
Expand Down
7 changes: 6 additions & 1 deletion rs/moq-crypto/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ pub fn provider() -> Result<Provider, UnsupportedCpu> {
// CPU features never change while the process runs, so the first answer is the answer.
static PROVIDER: OnceLock<Result<Provider, UnsupportedCpu>> = OnceLock::new();
PROVIDER
.get_or_init(|| rind_rustls::default_provider().map(Arc::new).map_err(UnsupportedCpu))
.get_or_init(|| {
crate::cpu::check()?;
let provider =
rind_rustls::default_provider().expect("rind-rustls refuses only the CPUs rind::cpu::check refuses");
Ok(Arc::new(provider))
})
.clone()
}

Expand Down
2 changes: 2 additions & 0 deletions rs/moq-tokio/src/quic/driver/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ impl Future for ConnectionDriver {
let waiter = this.park.hold(cx);
let conn = &mut *this.conn.state.lock();
let _guard = this.span.enter();
// Every packet this poll opens or seals pays one DIT toggle rather than one per rind call.
let _dit = moq_crypto::cpu::Dit::hold();

if let Err(e) = conn.process_conn_events(cx) {
conn.terminate(e);
Expand Down
2 changes: 2 additions & 0 deletions rs/moq-uring/src/quic/driver/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,8 @@ impl Driver {
// connection after this turn's checks still re-polls us.
waiter.register(&mut state.driver);
}
// Every packet this turn seals pays one DIT toggle rather than one per rind call.
let _dit = moq_crypto::cpu::Dit::hold();

loop {
self.endpoint_events();
Expand Down
2 changes: 2 additions & 0 deletions rs/moq-uring/src/quic/driver/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ impl Inner {
let mut buf = Vec::new();

let mut fed = Fed::default();
// Every datagram in this receive opens under one DIT toggle rather than one per rind call.
let _dit = moq_crypto::cpu::Dit::hold();

// moq-quic takes each datagram as an owned `BytesMut`: copy the whole
// completion out of the receive pool once and split it per datagram.
Expand Down
11 changes: 4 additions & 7 deletions rs/moq-webrtc/src/vendor/str0m_rind/srtp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn ecb_round(algorithm: &'static cipher::Algorithm, key: &[u8], input: &[u8], ou

impl SupportedAes128CmSha1_80 for RindSrtp {
fn create_cipher(&self, key: [u8; 16], _encrypt: bool) -> Box<dyn Aes128CmSha1_80Cipher> {
Box::new(Cm(AesCtr::new(&cipher::AES_128, &key).unwrap()))
Box::new(Cm(AesCtr::new(&cipher::AES_128, &key, 16).unwrap()))
}
}

Expand Down Expand Up @@ -88,13 +88,10 @@ impl Cm {
.ok_or_else(|| CryptoError::Other("AES-CM output too short".into()))?;
output.copy_from_slice(input);

// SRTP's 128-bit counter block splits into rind's 96-bit nonce and
// 32-bit counter. str0m's IVs leave the low 16 bits zero, so a packet
// never carries past the counter; one that would is refused.
let (nonce, counter) = iv.split_first_chunk::<12>().unwrap();
let counter = u32::from_be_bytes(counter.try_into().unwrap());
// SRTP AES-CM counts over the IV's low 16 bits, which str0m leaves
// zero, so rind refuses a packet past 2^16 blocks rather than wrap.
self.0
.apply_keystream(nonce, counter, output)
.apply_keystream(iv, output)
.map_err(|e| CryptoError::Other(format!("AES-CM failed: {e:?}")))
}
}
Expand Down
Loading