Skip to main content

Cipher

Struct Cipher 

Source
pub struct Cipher { /* private fields */ }
Expand description

Stateful symmetric cipher context.

See the module-level documentation for the cycle of life and the buffer ownership model.

Implementations§

Source§

impl Cipher

Source

pub fn new(algo: Algorithm, mode: Mode, padding: Padding) -> Result<Self, Error>

Create a new cipher context with the given algorithm, mode and padding scheme. Validates that the combination is supported; the actual key and IV are loaded later by Self::init.

§Errors

Returns Error::InvalidPaddingForMode when the requested padding is incompatible with the requested mode (e.g. any padding other than None with Mode::Ctr).

Source

pub fn init( &mut self, direction: Direction, key: &[u8], iv: &[u8], ) -> Result<(), Error>

Initialise (or re-initialise) the context with a key, IV and direction. Loading a new key wipes any pending input from a previous run, so the same Cipher object can be reused across many independent encryptions or decryptions.

iv must be block_len() bytes long for CBC and CTR; for ECB the slice is ignored but a &[] is the conventional choice.

§Errors
Source

pub const fn block_len(&self) -> usize

Block size of the underlying cipher in bytes (16 for AES, 8 for DES / 3DES).

Source

pub const fn iv_len(&self) -> usize

IV / nonce length expected by Self::init for the configured mode. Returns 0 for ECB.

Source

pub fn update_output_size(&self, input_len: usize) -> usize

Safe upper bound on the number of bytes a single Self::update call would write into output, given an input_len-byte input. Use this to size caller-provided buffers.

Source

pub const fn finalize_output_size(&self) -> usize

Safe upper bound on the number of bytes Self::finalize would write into output. Always at most one block.

Source

pub fn update( &mut self, input: &[u8], output: &mut [u8], ) -> Result<usize, Error>

Feed input bytes through the cipher and write any complete output bytes into output. Returns the number of bytes actually written.

Either slice may be empty. Bytes that don’t yet form a full output block stay buffered inside the Cipher until enough data arrives or Self::finalize is called.

§Errors
Source

pub fn finalize(&mut self, output: &mut [u8]) -> Result<usize, Error>

Finish the operation: pad and emit the trailing block (encrypt) or strip the padding from the last buffered ciphertext block (decrypt). After this call the context is in a finalized state and must be re-initialised with Self::init before further use.

§Errors
Source

pub fn update_to_vec(&mut self, input: &[u8]) -> Result<Vec<u8>, Error>

Convenience wrapper around Self::update that returns the freshly produced bytes as a Vec<u8>. Allocates.

Source

pub fn finalize_to_vec(&mut self) -> Result<Vec<u8>, Error>

Convenience wrapper around Self::finalize that returns the trailing bytes as a Vec<u8>. Allocates.

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.