Skip to main content

SFrameContext

Struct SFrameContext 

Source
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

Source

pub fn new(suite: CipherSuite, provider: Arc<dyn RTCCryptoProvider>) -> Self

A context for suite with no keys and the default SFrameConfig.

Source

pub fn with_config( suite: CipherSuite, provider: Arc<dyn RTCCryptoProvider>, config: SFrameConfig, ) -> Self

A context for suite with no keys.

Source

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.

Source

pub fn cipher_suite(&self) -> CipherSuite

The context’s cipher suite.

Source

pub fn config(&self) -> &SFrameConfig

The context’s options.

Source

pub fn provider(&self) -> &Arc<dyn RTCCryptoProvider> ⓘ

The crypto provider the context was built with.

Source

pub fn stats(&self) -> SFrameStats

What the context has done so far.

Source

pub fn max_overhead(&self) -> usize

The most bytes encryption adds to a plaintext: the longest header and the suite’s tag.

Source

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.

Source

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.

Source

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.

Source

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).

Source

pub fn retain_keys(&mut self, keep: impl FnMut(u64) -> bool)

Removes every key whose KID keep returns false for.

Source

pub fn key_usage(&self, kid: u64) -> Option<KeyUsage>

What the key stored under kid is for, if there is one.

Source

pub fn key_count(&self) -> usize

The number of keys stored.

Source

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.

Source

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.

Source

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).

Source

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.

Source

pub fn decrypt_into( &mut self, metadata: &[u8], ciphertext: &[u8], out: &mut Vec<u8>, ) -> Result<Header>

decrypt, appending the plaintext to out so that a caller can reuse one buffer. Returns the header of the ciphertext. On an error out is as it was.

Trait Implementations§

Source§

impl Debug for SFrameContext

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.