Struct wolf_crypto::aes::ctr::AesCtr
source · pub struct AesCtr { /* private fields */ }Implementations§
source§impl AesCtr
impl AesCtr
sourcepub fn new(key: &Key, iv: &Iv) -> Self
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.
sourcepub fn apply_keystream_sized<const S: usize>(
&mut self,
input: &[u8; S],
output: &mut [u8; S],
) -> Res
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();sourcepub fn try_apply_keystream(&mut self, input: &[u8], output: &mut [u8]) -> Res
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
inputbuffer is larger than theoutputbuffer. - (Unlikely) If the size of the
inputoroutputbuffer is greater than what can be represented by anunsigned 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();sourcepub fn apply_keystream(&mut self, input: &[u8], output: &mut [u8])
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
inputbuffer is larger than theoutputbuffer. - (Unlikely) If the size of the
inputoroutputbuffer is greater than what can be represented by anunsigned 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();sourcepub unsafe fn apply_keystream_unchecked(
&mut self,
input: &[u8],
output: &mut [u8],
) -> Res
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
outputbuffer must be at least the size of theinput. - 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();