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`]; [`scrub_stack`] overwrites what building a key schedule left in
9//! dead stack frames. The RustCrypto primitives the built-in providers use (`aes`, `ctr`, the
10//! HMAC-SHA1 of the ring provider) erase their key schedules when dropped. ring's own key
11//! objects (AEAD, header-protection and HMAC keys) do not erase themselves, so the ring provider
12//! keeps each in an allocation of its own and overwrites it after the drop
13//! (`docs/key-erasure.md`).
14
15mod algorithm;
16mod error;
17mod hkdf;
18#[cfg(feature = "openssl")]
19mod openssl_ctr;
20mod provider;
21mod rsa_pkcs1;
22mod secret;
23mod traits;
24
25#[cfg(any(feature = "crypto-ring", feature = "crypto-aws-lc-rs"))]
26mod common;
27pub mod providers;
28
29#[cfg(feature = "test-support")]
30pub mod conformance;
31
32/// Deterministic providers for fuzz targets. Gated behind `cfg(fuzzing)`; not part of the
33/// supported API.
34#[cfg(all(
35    any(fuzzing, test),
36    any(feature = "crypto-ring", feature = "crypto-aws-lc-rs")
37))]
38pub mod fuzzing;
39
40pub use algorithm::*;
41pub use error::CryptoError;
42pub use hkdf::{hkdf_expand, hkdf_expand_label, hkdf_extract};
43#[cfg(feature = "openssl")]
44pub use openssl_ctr::openssl_aes_ctr;
45pub use provider::default_provider;
46pub use rsa_pkcs1::{rsa_pkcs1_hash, verify_rsa_pkcs1_without_digest_parameters};
47pub use secret::{SecretVec, scrub_stack};
48pub use traits::*;
49
50/// Compares equal-length byte strings without data-dependent early exit.
51#[must_use]
52pub fn constant_time_eq(left: &[u8], right: &[u8]) -> bool {
53    use subtle::ConstantTimeEq;
54
55    left.len() == right.len() && bool::from(left.ct_eq(right))
56}
57
58const _: () = {
59    #[allow(dead_code)]
60    #[allow(clippy::too_many_arguments)]
61    fn assert_dyn_compatible(
62        _provider: &dyn RTCCryptoProvider,
63        _crypto: &dyn RTCCrypto,
64        _random: &dyn RTCRandom,
65        _mac: &dyn Mac,
66        _stream: &dyn StreamCipher,
67        _aead: &dyn AeadCipher,
68        _cbc: &dyn CbcCipher,
69        _mask: &dyn HeaderMask,
70        _exchange: &dyn ActiveKeyExchange,
71        _signing_key: &dyn SigningKey,
72    ) {
73    }
74};
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn secret_debug_is_redacted() {
82        let secret = SecretVec::new(vec![1, 2, 3, 4]);
83        let debug = format!("{secret:?}");
84        assert!(debug.contains("REDACTED"));
85        assert!(debug.contains("len: 4"));
86        assert!(!debug.contains("1, 2, 3, 4"));
87    }
88
89    #[test]
90    fn secret_into_bytes_is_explicit() {
91        let secret = SecretVec::new(vec![1, 2, 3]);
92        assert_eq!(secret.into_bytes(), vec![1, 2, 3]);
93    }
94
95    #[test]
96    fn errors_have_provider_neutral_text() {
97        assert_eq!(
98            CryptoError::AuthenticationFailed.to_string(),
99            "authentication failed"
100        );
101        assert_eq!(
102            CryptoError::InvalidSignature.to_string(),
103            "signature verification failed"
104        );
105    }
106
107    #[test]
108    fn constant_time_equality_checks_length_and_content() {
109        assert!(constant_time_eq(b"same", b"same"));
110        assert!(!constant_time_eq(b"same", b"diff"));
111        assert!(!constant_time_eq(b"same", b"same-longer"));
112    }
113}