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