pub struct AesContext { /* private fields */ }Expand description
An AES work queue user.
Implementations§
Source§impl AesContext
 
impl AesContext
Sourcepub fn new(
    cipher_mode: impl Into<CipherState>,
    operation: Operation,
    key: impl Into<Key>,
) -> Self
 
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.
Sourcepub fn process<'t>(
    &'t mut self,
    input: &'t [u8],
    output: &'t mut [u8],
) -> Result<AesHandle<'t>, Error>
 
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.
Sourcepub fn process_in_place<'t>(
    &'t mut self,
    buffer: &'t mut [u8],
) -> Result<AesHandle<'t>, Error>
 
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.