AesContext

Struct AesContext 

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

An AES work queue user.

Implementations§

Source§

impl AesContext

Source

pub fn new( cipher_mode: impl Into<CipherState>, operation: Operation, key: impl Into<Key>, ) -> Self

Creates a new context to encrypt or decrypt data with the given block cipher mode of operation.

Source

pub fn process<'t>( &'t mut self, input: &'t [u8], output: &'t mut [u8], ) -> Result<AesHandle<'t>, Error>

Starts transforming the input buffer, and writes the result into the output buffer.

  • For encryption the input is the plaintext, the output is the ciphertext.
  • For decryption the input is the ciphertext, the output is the plaintext.

The returned Handle must be polled until it returns true. Dropping the handle before the operation finishes will cancel the operation.

For an example, see the documentation of AesBackend.

§Errors
  • If the lengths of the input and output buffers don’t match, an error is returned.
  • The ECB and OFB cipher modes require the data length to be a multiple of the block size (16), otherwise an error is returned.
Source

pub fn process_in_place<'t>( &'t mut self, buffer: &'t mut [u8], ) -> Result<AesHandle<'t>, Error>

Starts transforming the buffer.

The processed data will be written back to the buffer.

The returned Handle must be polled until it returns true. Dropping the handle before the operation finishes will cancel the operation.

This function operates similar to AesContext::process, but it overwrites the data buffer with the result of the transformation.

use esp_hal::aes::{AesBackend, AesContext, Operation, cipher_modes::Ecb};
let mut aes = AesBackend::new(peripherals.AES);
// Start the backend, which pins it in place and allows processing AES operations.
let _backend = aes.start();

// Create a new context with a 128-bit key. The context will use the ECB block cipher mode.
// The key length must be supported by the hardware.
let mut ecb_encrypt = AesContext::new(Ecb, Operation::Encrypt, *b"SUp4SeCp@sSw0rd\0");

// Process a block of data in this context, in place. The ECB mode of operation requires that
// the length of the data is a multiple of the block (16 bytes) size.
let mut buffer: [u8; 16] = *b"message\0\0\0\0\0\0\0\0\0";

let operation_handle = ecb_encrypt.process_in_place(&mut buffer).unwrap();
operation_handle.wait_blocking();

// Instead of the plaintext message, buffer now contains the ciphertext.
assert_eq!(
    buffer,
    [
        0xb3, 0xc8, 0xd2, 0x3b, 0xa7, 0x36, 0x5f, 0x18, 0x61, 0x70, 0x0, 0x3e, 0xd9, 0x3a,
        0x31, 0x96,
    ]
);
§Errors

The ECB and OFB cipher modes require the data length to be a multiple of the block size (16), otherwise an error is returned.

Auto Trait Implementations§

§

impl Freeze for AesContext

§

impl RefUnwindSafe for AesContext

§

impl !Send for AesContext

§

impl !Sync for AesContext

§

impl Unpin for AesContext

§

impl UnwindSafe for AesContext

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

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> Same for T

Source§

type Output = T

Should always be Self
§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

type Error = <U as TryFrom<T>>::Error

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

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.