Skip to main content

AeadCtxRef

Struct AeadCtxRef 

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

A borrowed reference to a AeadCtx.

Implementations§

Source§

impl 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 ForeignTypeRef for AeadCtxRef

Source§

type CType = evp_aead_ctx_st

The raw C type.
Source§

unsafe fn from_ptr<'a>(ptr: *mut Self::CType) -> &'a Self

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

unsafe fn from_ptr_mut<'a>(ptr: *mut Self::CType) -> &'a mut Self

Constructs a mutable reference of this type from its raw type. Read more
Source§

fn as_ptr(&self) -> *mut Self::CType

Returns a raw pointer to the wrapped value.
Source§

impl Send for AeadCtxRef

Source§

impl Sync for AeadCtxRef

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