Skip to main content

rtci_sframe/
lib.rs

1#![warn(rust_2018_idioms)]
2#![warn(missing_docs)]
3
4//! SFrame for the Sans-I/O WebRTC stack.
5//!
6//! Secure Frame ([RFC 9605]) is authenticated encryption for media that stays end-to-end when
7//! the transport is not: a selective forwarding unit terminates SRTP and still cannot read or
8//! alter what SFrame protects. An SFrame ciphertext is a short header (a key id and a counter),
9//! the encrypted data and an authentication tag; it is produced from a whole encoded frame or
10//! from one packet's payload, and travels as the payload of whatever carries the media.
11//!
12//! The crate does no I/O, reads no clock and runs no key exchange. The application brings the
13//! keys (`base_key` values labelled with a KID) from its key management, and decides what a
14//! ciphertext covers and how it is packetized.
15//!
16//! # Structure
17//!
18//! * [`SFrameContext`]: the key store and the two operations, [`encrypt`](SFrameContext::encrypt)
19//!   and [`decrypt`](SFrameContext::decrypt). A key is added for sending or for receiving and
20//!   keeps that role; a sending key owns its counter, so a (key, nonce) pair is never used
21//!   twice; a receiving key has an anti-replay window.
22//! * [`CipherSuite`]: the five suites of RFC 9605 and the three AES-256-CTR suites of the IANA
23//!   registry.
24//! * [`Header`]: the SFrame header codec.
25//! * [`SenderKeyIdLayout`], [`SenderKeyRatchet`], [`SenderKeyReceiver`], [`ratchet_base_key`]:
26//!   the sender-keys scheme of section 5.1.
27//! * [`MlsKeyIdLayout`], [`MlsContext`]: SFrame keyed from MLS epochs, section 5.2. The
28//!   application runs MLS and supplies each epoch's exported secret.
29//! * [`PendingCiphertexts`]: a bounded store for ciphertexts whose key has not arrived yet.
30//!
31//! # Example
32//!
33//! ```
34//! use rtci_sframe::{CipherSuite, Error, SFrameContext};
35//!
36//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
37//! let suite = CipherSuite::Aes128CtrHmacSha256_80;
38//! let base_key = b"sixteen byte key"; // from the application's key management
39//!
40//! let mut alice = SFrameContext::with_default_provider(suite)?;
41//! alice.add_send_key(1, base_key)?;
42//! let mut bob = SFrameContext::with_default_provider(suite)?;
43//! bob.add_recv_key(1, base_key)?;
44//!
45//! // The metadata is authenticated but not sent in the ciphertext.
46//! let ciphertext = alice.encrypt(1, b"metadata", b"an encoded frame")?;
47//! assert_eq!(ciphertext.len(), 1 + b"an encoded frame".len() + suite.tag_len());
48//! assert_eq!(bob.decrypt(b"metadata", &ciphertext)?, b"an encoded frame");
49//!
50//! // A ciphertext is accepted once.
51//! assert!(matches!(
52//!     bob.decrypt(b"metadata", &ciphertext),
53//!     Err(Error::Replayed { kid: 1, ctr: 0 })
54//! ));
55//! # Ok(())
56//! # }
57//! ```
58//!
59//! # What stays with the application
60//!
61//! RFC 9605 leaves these to the application, and so does this crate: that a `base_key` has one
62//! sender, and that a (`base_key`, KID) pair is never installed for sending twice (section
63//! 9.1); the key exchange and when keys change (sections 5, 7.3 and 9.2); what goes into the
64//! metadata, and that nothing uses it before decryption has authenticated it (section 9.4);
65//! and reassembling a ciphertext that was sent in several packets (section 4.4.4).
66//!
67//! [RFC 9605]: https://www.rfc-editor.org/rfc/rfc9605
68
69mod aead;
70mod cipher_suite;
71mod context;
72mod error;
73mod header;
74mod kdf;
75mod key_id;
76mod mls;
77mod pending;
78mod replay;
79mod sender_key;
80
81/// Entry points for the fuzz targets. Gated behind `cfg(fuzzing)`; not part of the supported
82/// API.
83#[cfg(any(fuzzing, test))]
84pub mod fuzzing;
85
86/// The crypto provider API.
87///
88/// Re-exported because this crate's public constructors take an
89/// [`Arc<dyn RTCCryptoProvider>`](crypto::RTCCryptoProvider), which a caller must be able to name
90/// without adding, and version-matching, a direct `rtci-crypto` dependency.
91pub use crypto;
92
93pub use cipher_suite::{CipherSuite, NONCE_LEN, UnknownCipherSuite};
94pub use context::{KeyUsage, SFrameConfig, SFrameContext, SFrameStats};
95pub use error::{Error, ErrorKind, Result};
96pub use header::{Header, MAX_HEADER_LEN};
97pub use kdf::{derive_ssrc_key, ratchet_base_key};
98pub use key_id::{MlsKeyId, MlsKeyIdLayout, SenderKeyId, SenderKeyIdLayout};
99pub use mls::{DEFAULT_MAX_MLS_KEYS, MLS_EXPORTER_LABEL, MlsContext};
100pub use pending::{PendingCiphertext, PendingCiphertexts};
101pub use replay::{DEFAULT_REPLAY_WINDOW, MAX_REPLAY_WINDOW};
102pub use sender_key::{
103    DEFAULT_MAX_RATCHET_STEPS, DEFAULT_RETAINED_OLD_KEYS, SenderKeyRatchet, SenderKeyReceiver,
104};
105
106#[cfg(test)]
107mod aead_test;
108#[cfg(test)]
109mod context_test;
110#[cfg(test)]
111mod fuzzing_test;
112#[cfg(test)]
113mod header_test;
114#[cfg(test)]
115mod header_vectors;
116#[cfg(test)]
117mod kdf_test;
118#[cfg(test)]
119mod key_management_test;
120#[cfg(test)]
121mod test_util;
122#[cfg(test)]
123mod test_vectors;