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