Module esp32_hal::aes

source ·
Expand description

§Advanced Encryption Standard (AES) support.

§Overview

The AES module provides an interface to interact with the AES peripheral, provides encryption and decryption capabilities for ESP chips using the AES algorithm. We currently support the following AES encryption modes:

  • AES-128
  • AES-192
  • AES-256

§Example

§Initialization

let mut aes = Aes::new(peripherals.AES);

§Creating key and block Buffer

let keytext = "SUp4SeCp@sSw0rd".as_bytes();
let plaintext = "message".as_bytes();

// create an array with aes128 key size
let mut keybuf = [0_u8; 16];
keybuf[..keytext.len()].copy_from_slice(keytext);

// create an array with aes block size
let mut block_buf = [0_u8; 16];
block_buf[..plaintext.len()].copy_from_slice(plaintext);

§Encrypting and Decrypting (using hardware)

let mut block = block_buf.clone();
aes.process(&mut block, Mode::Encryption128, &keybuf);
let hw_encrypted = block.clone();

aes.process(&mut block, Mode::Decryption128, &keybuf);
let hw_decrypted = block;

§Encrypting and Decrypting (using software)

let key = GenericArray::from(keybuf);

let mut block = GenericArray::from(block_buf);
let cipher = Aes128SW::new(&key);
cipher.encrypt_block(&mut block);

let sw_encrypted = block.clone();
cipher.decrypt_block(&mut block);

let sw_decrypted = block;

§Implementation State

  • DMA mode is currently not supported on ESP32 and ESP32S2 ⚠️

§DMA-AES Mode

Supports 6 block cipher modes including ECB/CBC/OFB/CTR/CFB8/CFB128.

  • Initialization vector (IV) is currently not supported ⚠️

§Example

§Initialization

let dma = Gdma::new(peripherals.DMA);
let dma_channel = dma.channel0;

let mut descriptors = [0u32; 8 * 3];
let mut rx_descriptors = [0u32; 8 * 3];

let aes = Aes::new(peripherals.AES).with_dma(dma_channel.configure(
    false,
    &mut descriptors,
    &mut rx_descriptors,
    DmaPriority::Priority0,
));

§Operation

let transfer = aes
    .process(
        plaintext,
        hw_encrypted,
        Mode::Encryption128,
        CipherMode::Ecb,
        keybuf,
    )
    .unwrap();
let (hw_encrypted, plaintext, aes) = transfer.wait().unwrap();

Structs§

  • AES peripheral container
  • Marker type for AES-128
  • Marker type for AES-192
  • Marker type for AES-256

Enums§