Skip to main content

AeadCtx

Struct AeadCtx 

Source
pub struct AeadCtx(/* private fields */);
Expand description

An AEAD encryption/decryption context wrapping BoringSSL’s EVP_AEAD_CTX.

Holds the keying material for a specific Algorithm. Use AeadCtx::new_default_tag for the common case, or AeadCtx::new when you need a custom tag length.

See AeadCtxRef::seal_in_place and AeadCtxRef::open_in_place for the primary encryption/decryption API.

Implementations§

Source§

impl AeadCtx

Source

pub fn new( algorithm: &Algorithm, key: &[u8], tag_len: usize, ) -> Result<Self, ErrorStack>

Creates a new AEAD context.

tag_len controls the default tag length used by the context.

This corresponds to EVP_AEAD_CTX_new.

Source

pub fn new_default_tag( algorithm: &Algorithm, key: &[u8], ) -> Result<Self, ErrorStack>

Creates a new AEAD context using the algorithm’s full (maximum) tag length.

This is the recommended constructor for most use cases. The full tag length provides the strongest authentication guarantee for the algorithm. Use AeadCtx::new instead when your protocol requires a truncated tag.

Methods from Deref<Target = AeadCtxRef>§

Source

pub fn tag_len( &self, in_len: usize, extra_in_len: usize, ) -> Result<usize, ErrorStack>

Computes the exact tag length for a seal_scatter call with the given in_len and extra_in_len.

This is useful for sizing out_tag buffers precisely rather than relying on the worst-case Algorithm::max_overhead.

This corresponds to EVP_AEAD_CTX_tag_len.

Source

pub fn seal_scatter<'a>( &self, nonce: &[u8], in_out: &mut [u8], out_tag: &'a mut [u8], extra_in: Option<&[u8]>, associated_data: &[u8], ) -> Result<&'a mut [u8], ErrorStack>

Encrypts in_out in place and writes the authentication tag to out_tag.

extra_in is optional additional plaintext for protocols that split ciphertext output across buffers. When Some(extra) is provided, the ciphertext for extra is written to the start of out_tag, followed by the detached tag bytes.

In the common case, pass None and out_tag receives only the tag.

out_tag must be large enough for all detached output: extra_in.len() + tag_len (or conservatively extra_in.len() + Algorithm::max_overhead()).

§Parameters
  • nonce: Per-message nonce for this encryption operation.
  • in_out: Plaintext input and in-place ciphertext output.
  • out_tag: Detached output buffer for extra_in ciphertext (if any) and the authentication tag.
  • extra_in: Optional extra plaintext chunk written as ciphertext into out_tag before the tag.
  • associated_data: Additional authenticated data (AAD).

Returns the sub-slice of out_tag that was written to. This includes any encrypted extra_in bytes and the final tag.

§Examples
use boring::aead::{AeadCtx, Algorithm};

let algorithm = Algorithm::chacha20_poly1305();
let ctx = AeadCtx::new(&algorithm, &[7u8; 32], algorithm.max_tag_len()).unwrap();

let nonce = [1u8; 12];
let aad = b"frame-header";

// Main payload is encrypted in-place.
let mut main = b"hello".to_vec();
// Extra plaintext is encrypted into the detached buffer.
let extra = b" world";
let mut detached = vec![0u8; extra.len() + algorithm.max_overhead()];

let detached_written = ctx
    .seal_scatter(
        &nonce,
        main.as_mut_slice(),
        detached.as_mut_slice(),
        Some(extra),
        aad,
    )
    .unwrap();

// `detached_written` contains: extra ciphertext bytes followed by tag bytes.
let extra_ct_len = extra.len();
let tag = &detached_written[extra_ct_len..];

// Reconstruct the full ciphertext by appending extra ciphertext bytes.
let mut full_ciphertext = main.clone();
full_ciphertext.extend_from_slice(&detached_written[..extra_ct_len]);

// `open_gather` takes ciphertext and detached tag separately.
ctx.open_gather(&nonce, full_ciphertext.as_mut_slice(), tag, aad)
    .unwrap();

assert_eq!(full_ciphertext.as_slice(), b"hello world");

This corresponds to EVP_AEAD_CTX_seal_scatter.

Source

pub fn open_gather( &self, nonce: &[u8], in_out: &mut [u8], in_tag: &[u8], associated_data: &[u8], ) -> Result<(), ErrorStack>

Decrypts in_out in place and verifies in_tag and associated_data.

When the corresponding seal_scatter call used extra_in, append the extra ciphertext prefix to in_out and pass only the tag suffix as in_tag. See the seal_scatter documentation for a full example.

§Parameters
  • nonce: The same nonce that was used during encryption.
  • in_out: Ciphertext input and in-place plaintext output.
  • in_tag: Detached tag bytes produced by seal_scatter.
  • associated_data: The same AAD that was passed during encryption.

This corresponds to EVP_AEAD_CTX_open_gather.

Source

pub fn seal_in_place<'a>( &self, nonce: &[u8], buffer: &mut [u8], tag: &'a mut [u8], associated_data: &[u8], ) -> Result<&'a mut [u8], ErrorStack>

Encrypts buffer in place and writes the authentication tag into tag.

This is a convenience wrapper around seal_scatter with extra_in = None.

§Parameters
  • nonce: Per-message nonce. Must match the length returned by Algorithm::nonce_len.
  • buffer: Plaintext on input, ciphertext on output (encrypted in place).
  • tag: Output buffer for the authentication tag. Must be at least Algorithm::max_overhead bytes; use AeadCtxRef::tag_len for the exact size.
  • associated_data: Additional authenticated data (AAD) that is authenticated but not encrypted.

Returns the sub-slice of tag that was written to.

Source

pub fn open_in_place( &self, nonce: &[u8], buffer: &mut [u8], tag: &[u8], associated_data: &[u8], ) -> Result<(), ErrorStack>

Decrypts buffer in place, verifying the authentication tag and associated_data.

This is a convenience wrapper around open_gather.

§Parameters
  • nonce: The same nonce that was used during encryption.
  • buffer: Ciphertext on input, plaintext on output (decrypted in place).
  • tag: The authentication tag produced by seal_in_place.
  • associated_data: The same AAD that was passed during encryption.

Trait Implementations§

Source§

impl AsMut<AeadCtxRef> for AeadCtx

Source§

fn as_mut(&mut self) -> &mut AeadCtxRef

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsRef<AeadCtxRef> for AeadCtx

Source§

fn as_ref(&self) -> &AeadCtxRef

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Borrow<AeadCtxRef> for AeadCtx

Source§

fn borrow(&self) -> &AeadCtxRef

Immutably borrows from an owned value. Read more
Source§

impl BorrowMut<AeadCtxRef> for AeadCtx

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl Deref for AeadCtx

Source§

type Target = AeadCtxRef

The resulting type after dereferencing.
Source§

fn deref(&self) -> &AeadCtxRef

Dereferences the value.
Source§

impl DerefMut for AeadCtx

Source§

fn deref_mut(&mut self) -> &mut AeadCtxRef

Mutably dereferences the value.
Source§

impl Drop for AeadCtx

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl ForeignType for AeadCtx

Source§

type CType = evp_aead_ctx_st

The raw C type.
Source§

type Ref = AeadCtxRef

The type representing a reference to this type.
Source§

unsafe fn from_ptr(ptr: *mut EVP_AEAD_CTX) -> AeadCtx

Constructs an instance of this type from its raw type. Read more
Source§

fn as_ptr(&self) -> *mut EVP_AEAD_CTX

Returns a raw pointer to the wrapped value.
Source§

fn into_ptr(self) -> *mut Self::CType

Consumes the wrapper and returns the raw pointer.
Source§

impl Send for AeadCtx

Source§

impl Sync for AeadCtx

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.