pub struct SFrameContext { /* private fields */ }Expand description
An SFrame context (RFC 9605 Appendix A): one cipher suite, and the keys known for each KID.
A key is added for sending or for receiving and keeps that role. Encryption takes the KID to use and draws the CTR from the key’s own counter, so a (key, nonce) pair is never used twice; decryption finds the key by the KID in the SFrame header.
use rtci_sframe::{CipherSuite, SFrameContext};
let suite = CipherSuite::Aes128GcmSha256_128;
let base_key = [0x2a; 16]; // from the application's key management
let mut sender = SFrameContext::with_default_provider(suite)?;
sender.add_send_key(7, &base_key)?;
let mut receiver = SFrameContext::with_default_provider(suite)?;
receiver.add_recv_key(7, &base_key)?;
let metadata = b"what the application authenticates but sends elsewhere";
let ciphertext = sender.encrypt(7, metadata, b"an encoded frame")?;
assert_eq!(receiver.decrypt(metadata, &ciphertext)?, b"an encoded frame");Implementations§
Source§impl SFrameContext
impl SFrameContext
Sourcepub fn new(suite: CipherSuite, provider: Arc<dyn RTCCryptoProvider>) -> Self
pub fn new(suite: CipherSuite, provider: Arc<dyn RTCCryptoProvider>) -> Self
A context for suite with no keys and the default SFrameConfig.
Sourcepub fn with_config(
suite: CipherSuite,
provider: Arc<dyn RTCCryptoProvider>,
config: SFrameConfig,
) -> Self
pub fn with_config( suite: CipherSuite, provider: Arc<dyn RTCCryptoProvider>, config: SFrameConfig, ) -> Self
A context for suite with no keys.
Sourcepub fn with_default_provider(suite: CipherSuite) -> Result<Self>
pub fn with_default_provider(suite: CipherSuite) -> Result<Self>
A context for suite on the built-in crypto provider. Fails when the crate was built
without one.
Sourcepub fn cipher_suite(&self) -> CipherSuite
pub fn cipher_suite(&self) -> CipherSuite
The context’s cipher suite.
Sourcepub fn config(&self) -> &SFrameConfig
pub fn config(&self) -> &SFrameConfig
The context’s options.
Sourcepub fn provider(&self) -> &Arc<dyn RTCCryptoProvider> ⓘ
pub fn provider(&self) -> &Arc<dyn RTCCryptoProvider> ⓘ
The crypto provider the context was built with.
Sourcepub fn stats(&self) -> SFrameStats
pub fn stats(&self) -> SFrameStats
What the context has done so far.
Sourcepub fn max_overhead(&self) -> usize
pub fn max_overhead(&self) -> usize
The most bytes encryption adds to a plaintext: the longest header and the suite’s tag.
Sourcepub fn add_send_key(&mut self, kid: u64, base_key: &[u8]) -> Result<()>
pub fn add_send_key(&mut self, kid: u64, base_key: &[u8]) -> Result<()>
Adds base_key under kid for encryption, with its counter at 0.
The caller is the key’s one sender (RFC 9605 section 4.4.1). Fails with
Error::KeyRoleConflict when kid holds a decryption key, and with
Error::KeyReuse when kid already sends with this very key, since starting its
counter again would repeat nonces. Another key under the same KID replaces the old one.
Sourcepub fn add_send_key_at(
&mut self,
kid: u64,
base_key: &[u8],
next_ctr: u64,
) -> Result<()>
pub fn add_send_key_at( &mut self, kid: u64, base_key: &[u8], next_ctr: u64, ) -> Result<()>
Adds base_key under kid for encryption, with its counter at next_ctr.
For a context restored from storage (RFC 9605 section 9.1): store
next_counter before using it, and resume from the stored value,
so that a failure never leads to a counter being used twice.
Sourcepub fn add_recv_key(&mut self, kid: u64, base_key: &[u8]) -> Result<()>
pub fn add_recv_key(&mut self, kid: u64, base_key: &[u8]) -> Result<()>
Adds base_key under kid for decryption.
Fails with Error::KeyRoleConflict when kid holds an encryption key. Adding the key
kid already decrypts with changes nothing (its replay window is kept); another key
under the same KID replaces the old one.
Sourcepub fn remove_key(&mut self, kid: u64) -> bool
pub fn remove_key(&mut self, kid: u64) -> bool
Removes the key stored under kid. Returns whether there was one.
A removed encryption key takes its counter with it: adding the same base_key under the
same KID again would repeat nonces, and it is the application that must not do so
(RFC 9605 section 9.1).
Sourcepub fn retain_keys(&mut self, keep: impl FnMut(u64) -> bool)
pub fn retain_keys(&mut self, keep: impl FnMut(u64) -> bool)
Removes every key whose KID keep returns false for.
Sourcepub fn key_usage(&self, kid: u64) -> Option<KeyUsage>
pub fn key_usage(&self, kid: u64) -> Option<KeyUsage>
What the key stored under kid is for, if there is one.
Sourcepub fn next_counter(&self, kid: u64) -> Option<u64>
pub fn next_counter(&self, kid: u64) -> Option<u64>
The CTR the next encryption under kid will use. None when kid holds no encryption
key, or one that has used every counter.
Sourcepub fn encrypt(
&mut self,
kid: u64,
metadata: &[u8],
plaintext: &[u8],
) -> Result<Vec<u8>>
pub fn encrypt( &mut self, kid: u64, metadata: &[u8], plaintext: &[u8], ) -> Result<Vec<u8>>
Encrypts plaintext with the key stored under kid and returns the SFrame ciphertext:
header, encrypted data, authentication tag (RFC 9605 section 4.4.3).
metadata is authenticated with the header but is not part of the output; the receiver
must supply the same bytes.
Sourcepub fn encrypt_into(
&mut self,
kid: u64,
metadata: &[u8],
plaintext: &[u8],
out: &mut Vec<u8>,
) -> Result<Header>
pub fn encrypt_into( &mut self, kid: u64, metadata: &[u8], plaintext: &[u8], out: &mut Vec<u8>, ) -> Result<Header>
encrypt, appending the ciphertext to out so that a caller can reuse
one buffer. Returns the header that was written. On an error out is as it was.
The key’s counter advances before the cipher runs, so a counter is never used twice,
whatever happens. After 2^64 encryptions the key is exhausted
(Error::CounterExhausted).
Sourcepub fn decrypt(&mut self, metadata: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>>
pub fn decrypt(&mut self, metadata: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>>
Decrypts an SFrame ciphertext and returns the plaintext (RFC 9605 section 4.4.4).
metadata must be the bytes the sender authenticated. Nothing of an unauthenticated
ciphertext is returned. A ciphertext that fails authentication, is a replay or names a
KID without a decryption key costs the cipher and MAC work of a valid one of its length,
so that the outcome does not show in the time taken; only a malformed one, which has
nothing to process, returns at once.
The ciphertext is not consumed: when the failure is
Error::UnknownKeyId, the application may keep it (see
PendingCiphertexts) and decrypt again once the key has
arrived. Any other failure means the ciphertext is to be discarded.