Skip to main content

rtci_crypto/
lib.rs

1//! Provider-neutral cryptography for the webrtc-rs RTC stack.
2//!
3//! The open traits in this crate allow applications to supply cryptography and randomness without
4//! registering global state. Built-in providers are selected with additive Cargo features.
5//!
6//! Key material: every struct holding secret key material derives `Zeroize` and
7//! `ZeroizeOnDrop`, or holds each secret in [`SecretVec`]; secret keys are drawn with
8//! [`RTCRandom::fill_secret`]; [`on_erased_stack`] runs key set-up and overwrites the stack it
9//! ran on, and [`scrub_stack`] is the overwrite by itself. The RustCrypto primitives the
10//! built-in providers use (`aes`, `ctr`, and the `hmac` / `sha1` of the ring provider's
11//! HMAC-SHA1) erase their key schedules and hash states when dropped, and their keyed objects
12//! are used in place: an AES-CTR keystream runs over a reference to the boxed schedule, an
13//! HMAC-SHA1 is computed in the boxed keyed object. ring's own key objects (AEAD,
14//! header-protection and HMAC keys) do not erase themselves, so the ring provider keeps each in
15//! an allocation of its own and overwrites it after the drop (`docs/key-erasure.md`).
16
17mod algorithm;
18mod erased_stack;
19mod error;
20mod hkdf;
21#[cfg(feature = "openssl")]
22mod openssl_ctr;
23mod provider;
24mod rsa_pkcs1;
25mod secret;
26mod traits;
27
28#[cfg(any(feature = "crypto-ring", feature = "crypto-aws-lc-rs"))]
29mod common;
30pub mod providers;
31
32#[cfg(feature = "test-support")]
33pub mod conformance;
34
35/// Deterministic providers for fuzz targets. Gated behind `cfg(fuzzing)`; not part of the
36/// supported API.
37#[cfg(all(
38    any(fuzzing, test),
39    any(feature = "crypto-ring", feature = "crypto-aws-lc-rs")
40))]
41pub mod fuzzing;
42
43pub use algorithm::*;
44pub use erased_stack::on_erased_stack;
45pub use error::CryptoError;
46pub use hkdf::{hkdf_expand, hkdf_expand_label, hkdf_extract};
47#[cfg(feature = "openssl")]
48pub use openssl_ctr::openssl_aes_ctr;
49pub use provider::default_provider;
50pub use rsa_pkcs1::{rsa_pkcs1_hash, verify_rsa_pkcs1_without_digest_parameters};
51pub use secret::{SecretVec, scrub_stack};
52pub use traits::*;
53
54/// Compares equal-length byte strings without data-dependent early exit.
55#[must_use]
56pub fn constant_time_eq(left: &[u8], right: &[u8]) -> bool {
57    use subtle::ConstantTimeEq;
58
59    left.len() == right.len() && bool::from(left.ct_eq(right))
60}
61
62const _: () = {
63    #[allow(dead_code)]
64    #[allow(clippy::too_many_arguments)]
65    fn assert_dyn_compatible(
66        _provider: &dyn RTCCryptoProvider,
67        _crypto: &dyn RTCCrypto,
68        _random: &dyn RTCRandom,
69        _mac: &dyn Mac,
70        _stream: &dyn StreamCipher,
71        _aead: &dyn AeadCipher,
72        _cbc: &dyn CbcCipher,
73        _mask: &dyn HeaderMask,
74        _exchange: &dyn ActiveKeyExchange,
75        _signing_key: &dyn SigningKey,
76    ) {
77    }
78};
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn secret_debug_is_redacted() {
86        let secret = SecretVec::new(vec![1, 2, 3, 4]);
87        let debug = format!("{secret:?}");
88        assert!(debug.contains("REDACTED"));
89        assert!(debug.contains("len: 4"));
90        assert!(!debug.contains("1, 2, 3, 4"));
91    }
92
93    #[test]
94    fn secret_into_bytes_is_explicit() {
95        let secret = SecretVec::new(vec![1, 2, 3]);
96        assert_eq!(secret.into_bytes(), vec![1, 2, 3]);
97    }
98
99    #[test]
100    fn errors_have_provider_neutral_text() {
101        assert_eq!(
102            CryptoError::AuthenticationFailed.to_string(),
103            "authentication failed"
104        );
105        assert_eq!(
106            CryptoError::InvalidSignature.to_string(),
107            "signature verification failed"
108        );
109    }
110
111    #[test]
112    fn constant_time_equality_checks_length_and_content() {
113        assert!(constant_time_eq(b"same", b"same"));
114        assert!(!constant_time_eq(b"same", b"diff"));
115        assert!(!constant_time_eq(b"same", b"same-longer"));
116    }
117}