Struct wolf_crypto::aes::ctr::AesCtr

source ·
pub struct AesCtr { /* private fields */ }

Implementations§

source§

impl AesCtr

source

pub fn new(key: &Key, iv: &Iv) -> Self

Create a new AES CTR instance.

§Arguments
  • key - The key material to use (which determines the number of rounds).
  • iv - The initialization vector (nonce).
§Returns

A new AES instance in CTR mode.

§Note

This copies the key and nonce in the underlying C code and is out of scope of this Rust API. At the end of the AesCtr’s lifetime these will be zeroed. It may be desirable to immediately zero the key and nonce passed to this function by reference post invocation.

source

pub fn apply_keystream_sized<const S: usize>( &mut self, input: &[u8; S], output: &mut [u8; S], ) -> Res

Apply the underlying keystream to the output buffer, with the size of both the input and output buffers described at compile time to avoid most runtime checks.

§Arguments
  • input - The input to apply the keystream to.
  • output - The output buffer to store the result of applying the keystream.
§Errors
  • If the application of the keystream failed.
  • (Unlikely) If the size of the input and output buffer is greater than what can be represented by an unsigned int (u32).
§Example
use wolf_crypto::{buf::Iv, aes::{Key, ctr::AesCtr}};
// securely generate a random key and initialization vector ...

let mut input = [1u8; 32];
let mut output = [0u8; 32];

assert!(AesCtr::new(&key, &iv)
    .apply_keystream_sized(&input, &mut output)
    .is_ok());

assert_ne!(&input, &output);
assert_ne!(output, [0u8; 32]);

// and decrypt

let mut plain = [0u8; 32];
assert!(AesCtr::new(&key, &iv)
    .apply_keystream_sized(&output, &mut plain)
    .is_ok());

assert_eq!(&plain, &input);
key.zero();
source

pub fn try_apply_keystream(&mut self, input: &[u8], output: &mut [u8]) -> Res

Try to apply the underlying keystream to the output buffer.

§Arguments
  • input - The input to apply the keystream to.
  • output - The output buffer to store the result of applying the keystream.
§Errors
  • If the application of the keystream failed.
  • If the input buffer is larger than the output buffer.
  • (Unlikely) If the size of the input or output buffer is greater than what can be represented by an unsigned int (u32).
§Example
use wolf_crypto::{buf::Iv, aes::{Key, ctr::AesCtr}};
// securely generate a random key and initialization vector ...

let mut input = [1u8; 32];
let mut output = [0u8; 32];

assert!(AesCtr::new(&key, &iv)
    .try_apply_keystream(input.as_slice(), output.as_mut_slice())
    .is_ok());

assert_ne!(&input, &output);
assert_ne!(output, [0u8; 32]);

// and decrypt

let mut plain = [0u8; 32];
assert!(AesCtr::new(&key, &iv)
    .try_apply_keystream(output.as_slice(), plain.as_mut_slice())
    .is_ok());

assert_eq!(&plain, &input);
key.zero();
source

pub fn apply_keystream(&mut self, input: &[u8], output: &mut [u8])

Apply the underlying keystream to the output buffer using the encryption key.

§Arguments
  • input - The input to apply the keystream to.
  • output - The output buffer to store the result of applying the keystream.
§Panics
  • If the application of the keystream failed.
  • If the input buffer is larger than the output buffer.
  • (Unlikely) If the size of the input or output buffer is greater than what can be represented by an unsigned int (u32).
§Example
use wolf_crypto::{buf::Iv, aes::{Key, ctr::AesCtr}};
// securely generate a random key and initialization vector ...

let mut input = [1u8; 32];
let mut output = [0u8; 32];

AesCtr::new(&key, &iv)
    .apply_keystream(input.as_slice(), output.as_mut_slice());

assert_ne!(&input, &output);
assert_ne!(output, [0u8; 32]);

// and decrypt

let mut plain = [0u8; 32];
AesCtr::new(&key, &iv)
    .apply_keystream(output.as_slice(), plain.as_mut_slice());

assert_eq!(&plain, &input);
key.zero();
source

pub unsafe fn apply_keystream_unchecked( &mut self, input: &[u8], output: &mut [u8], ) -> Res

Apply the underlying keystream to the output buffer.

This method performs no runtime safety checks.

§Safety
  • The output buffer must be at least the size of the input.
  • The size of both buffers must be representable by an unsigned int (u32).
§Arguments
  • input - The input to apply the keystream to.
  • output - The output buffer to store the result of applying the keystream.
§Errors

If the application of the keystream failed.

§Example
use wolf_crypto::{buf::Iv, aes::{Key, ctr::AesCtr}};

// securely generate a random key and initialization vector ...

let mut input = [1u8; 32];
let mut output = [0u8; 32];

assert!(AesCtr::new(&key, &iv)
    .apply_keystream_unchecked(input.as_slice(), output.as_mut_slice())
    .is_ok());

assert_ne!(&output, &input);

// and decrypt

let mut original = [0u8; 32];
assert!(AesCtr::new(&key, &iv)
    .apply_keystream_unchecked(output.as_slice(), original.as_mut_slice())
    .is_ok());

assert_eq!(&original, &input);

key.zero();

Trait Implementations§

source§

impl Drop for AesCtr

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Send for AesCtr

source§

impl Sync for AesCtr

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.