//! AES Tests

//% CHIPS(quad): esp32s2
// The S3 dev kit in the HIL-tester has octal PSRAM.
//% CHIPS(octal): esp32s3
// ESP32 has no AES-DMA, no point in setting up PSRAM
//% CHIPS(no_psram): esp32 esp32c3 esp32c6 esp32h2

//% ENV(octal): ESP_HAL_CONFIG_PSRAM_MODE=octal
//% FEATURES(quad, octal): psram
//% FEATURES: unstable esp-alloc/nightly

#![no_std]
#![no_main]

use embassy_executor::SendSpawner;
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, signal::Signal};
#[cfg(aes_dma)]
use esp_hal::aes::dma::AesDmaBackend;
use esp_hal::{
    Config,
    aes::{
        Aes,
        AesBackend,
        AesContext,
        CipherState,
        Key,
        Operation,
        cipher_modes::{Cbc, Cfb8, Cfb128, Ctr, Ecb, Ofb},
    },
    clock::CpuClock,
};
use hil_test::mk_static;

const KEY: &[u8] = b"SUp4SeCp@sSw0rd";
const KEY_128: [u8; 16] = pad_to::<16>(KEY);
const KEY_256: [u8; 32] = pad_to::<32>(KEY);

const IV: [u8; 16] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];

const PLAINTEXT: &[u8] = b"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
const PLAINTEXT_BUF_SIZE: usize = const { PLAINTEXT.len().next_multiple_of(16) };

// DMA source data must be in RAM
fn fill_with_plaintext(buf: &mut [u8]) {
    let len = buf.len().min(PLAINTEXT.len());
    buf[..len].copy_from_slice(&PLAINTEXT[..len]);
}

const CIPHERTEXT_ECB_128: [u8; PLAINTEXT_BUF_SIZE] = [
    0x4b, 0xfc, 0xd9, 0xd5, 0xc9, 0x5e, 0x65, 0x7b, 0xea, 0x8f, 0x37, 0x32, 0x45, 0xbe, 0x77, 0x73,
    0xdf, 0x85, 0xc6, 0x84, 0xb6, 0xf1, 0xab, 0x6c, 0xd1, 0x51, 0x7a, 0xdf, 0x7b, 0x8e, 0x76, 0x89,
    0xf5, 0x9d, 0xe, 0xc5, 0xe2, 0xb, 0x66, 0x7d, 0xf5, 0xdb, 0xc8, 0x66, 0xc5, 0x7, 0xa0, 0x58,
    0x9, 0x50, 0x9e, 0xe2, 0x69, 0x1c, 0x48, 0x56, 0xd1, 0x2c, 0x5b, 0x13, 0x45, 0x58, 0xf3, 0xf6,
    0x22, 0xc8, 0xc9, 0x29, 0x48, 0xcf, 0xfa, 0x32, 0x9a, 0x15, 0x2b, 0x41, 0x1a, 0xed, 0xef, 0xb4,
    0x30, 0x84, 0x58, 0x77, 0xc4, 0x80, 0xc9, 0xbb, 0x93, 0x57, 0xe8, 0xa3, 0xe9, 0x5f, 0x26, 0x3e,
    0xe2, 0xe4, 0x66, 0x26, 0x57, 0xcd, 0x59, 0x42, 0x2e, 0x93, 0xe4, 0x55, 0xe6, 0x48, 0x46, 0x2f,
    0x6e, 0xee, 0xbf, 0x98, 0xd8, 0x2, 0xf5, 0x1a, 0xef, 0x2e, 0x62, 0x4f, 0x3f, 0x25, 0x21, 0x4f,
    0x5d, 0x46, 0x41, 0xb2, 0xc1, 0x71, 0x69, 0xfe, 0x60, 0xa7, 0x1b, 0xe3, 0xed, 0xb3, 0x4e, 0xde,
    0x70, 0x60, 0x81, 0x8d, 0xaf, 0xdd, 0xb8, 0x77, 0x53, 0x8d, 0x21, 0x8e, 0x24, 0x3e, 0xf0, 0x55,
    0xe0, 0x8, 0xa6, 0xcc, 0xd2, 0xc1, 0xb3, 0xd6, 0x33, 0x6e, 0x7a, 0xa4, 0x6e, 0x3e, 0x5a, 0xe,
    0x19, 0xca, 0xd2, 0x9, 0x12, 0xbe, 0xfb, 0x99, 0xf1, 0x46, 0xa1, 0x83, 0xb8, 0x78, 0xcf, 0xe7,
    0x1, 0xdc, 0x2, 0x45, 0x58, 0xa4, 0x6b, 0x96, 0xa2, 0x2b, 0xe, 0x2a, 0x96, 0x2e, 0x1c, 0x1a,
    0xfa, 0xe0, 0x59, 0xeb, 0xca, 0x27, 0x28, 0xd1, 0x3f, 0x2b, 0xb8, 0xf5, 0x8e, 0xd2, 0x89, 0x4b,
    0x60, 0xcb, 0x18, 0x2e, 0xa7, 0xce, 0xb6, 0xa1, 0x7a, 0x14, 0xa2, 0x2f, 0x99, 0xfe, 0xcc, 0x5e,
    0x60, 0x6c, 0xba, 0x7b, 0x9d, 0x50, 0xf6, 0xd, 0x84, 0x8c, 0x77, 0x7a, 0x50, 0x98, 0xf5, 0x86,
];
#[cfg(any(esp32, esp32s2))]
const CIPHERTEXT_ECB_192: [u8; PLAINTEXT_BUF_SIZE] = [
    0x4c, 0x56, 0x58, 0xf5, 0x63, 0x62, 0xa1, 0xf7, 0x15, 0x60, 0xe3, 0xdb, 0x67, 0x99, 0x0, 0x6b,
    0xeb, 0xc5, 0x8f, 0xc, 0x65, 0x56, 0x43, 0x5d, 0xd7, 0x85, 0x94, 0x63, 0x0, 0x6, 0xfd, 0xd5,
    0xb4, 0x8f, 0x9b, 0x14, 0xe4, 0x9a, 0x64, 0x2, 0xdb, 0x74, 0x29, 0xc7, 0x81, 0x3b, 0x9c, 0xca,
    0xb0, 0xf1, 0xf5, 0xd4, 0xd1, 0xb8, 0xd4, 0x6e, 0xcb, 0x61, 0xaa, 0x5d, 0xc5, 0x8d, 0x57, 0xa2,
    0x6b, 0x6e, 0x4f, 0x3f, 0x6b, 0x58, 0xfb, 0x74, 0x6f, 0x4d, 0x78, 0x8f, 0x73, 0xdf, 0x66, 0x1,
    0xc9, 0x51, 0xb0, 0xd7, 0x6d, 0x6a, 0xa4, 0xe6, 0x22, 0xa5, 0x27, 0xca, 0x8a, 0xe6, 0x62, 0x1d,
    0x2, 0x59, 0xf4, 0xe7, 0xff, 0xc4, 0xb9, 0x17, 0x3b, 0x8d, 0xf9, 0x93, 0xc4, 0xd7, 0x3e, 0xe6,
    0xa2, 0xac, 0xde, 0x4b, 0x7b, 0x83, 0xef, 0xa6, 0x37, 0x98, 0xea, 0xbd, 0x1e, 0x31, 0x76, 0xb2,
    0x1d, 0x64, 0x1e, 0x3, 0x5e, 0xd2, 0xd1, 0xe8, 0x97, 0xfb, 0xcc, 0x46, 0x50, 0x61, 0xc5, 0x2,
    0x43, 0x10, 0x1d, 0x33, 0xd5, 0xd2, 0xc8, 0x8f, 0x58, 0x5f, 0xbf, 0xd8, 0x2f, 0x67, 0xca, 0xcc,
    0xa1, 0xc6, 0x83, 0x7f, 0xd6, 0xdf, 0x93, 0x7e, 0x11, 0x76, 0xf9, 0x11, 0xa0, 0xac, 0x2d, 0x26,
    0x3b, 0xa9, 0x6c, 0xbb, 0x91, 0xc9, 0x9, 0x82, 0x3d, 0x6, 0x70, 0xde, 0x62, 0x2b, 0x76, 0xc1,
    0xd5, 0x4e, 0x1b, 0x7d, 0xb2, 0xb7, 0xc8, 0xc8, 0x7b, 0x1f, 0x23, 0x64, 0x42, 0xe0, 0x52, 0x45,
    0x7c, 0xcf, 0x5b, 0xe4, 0x90, 0xc5, 0x46, 0x8e, 0xf1, 0x46, 0xf2, 0xfe, 0x6c, 0x4d, 0x76, 0xac,
    0xa4, 0x74, 0xaa, 0xf1, 0x6f, 0x70, 0xcf, 0x5f, 0x31, 0xed, 0x56, 0xfd, 0x1d, 0x2e, 0x42, 0x4f,
    0xed, 0x6b, 0x9f, 0xd3, 0x2c, 0x30, 0xcf, 0xfb, 0xa7, 0x78, 0xd0, 0xf9, 0xc7, 0xa7, 0x6c, 0x85,
];
const CIPHERTEXT_ECB_256: [u8; PLAINTEXT_BUF_SIZE] = [
    0x98, 0xf1, 0xd8, 0xfc, 0x4e, 0x3b, 0x4, 0xd3, 0xfa, 0xe5, 0xdf, 0x7a, 0x50, 0x10, 0x5f, 0xd7,
    0xfc, 0xbf, 0xd1, 0x46, 0xcf, 0xa1, 0x35, 0x2e, 0xf0, 0xb0, 0xc8, 0x3a, 0xc0, 0x3e, 0xc5, 0xc0,
    0xf8, 0x32, 0x31, 0xaa, 0xf2, 0xd6, 0x15, 0x78, 0x3d, 0xc9, 0x50, 0x77, 0x7b, 0x53, 0x39, 0x46,
    0x51, 0xb0, 0x61, 0x8b, 0xd1, 0xe3, 0x89, 0x49, 0x74, 0xf8, 0x49, 0x9c, 0xf, 0xf9, 0xc0, 0xe,
    0x82, 0xcc, 0xa6, 0xf3, 0xae, 0xbe, 0x89, 0x83, 0x7, 0xd5, 0xa2, 0xbf, 0x3e, 0xef, 0x2c, 0xc3,
    0xf6, 0x30, 0xa, 0x92, 0x95, 0x1d, 0xe4, 0xf8, 0xc3, 0x56, 0xea, 0xc0, 0xfb, 0xcb, 0x83, 0x5a,
    0xec, 0x8, 0x3c, 0x6f, 0x30, 0x66, 0x3b, 0xb2, 0xf6, 0x2a, 0x75, 0xf3, 0x72, 0x81, 0x69, 0x79,
    0x74, 0x9d, 0x4, 0x48, 0xb1, 0x60, 0x25, 0xc9, 0xb6, 0x7e, 0x57, 0xec, 0x64, 0xbf, 0x78, 0x7b,
    0xeb, 0x54, 0xea, 0x41, 0xd9, 0xa9, 0x68, 0x57, 0xc4, 0x7e, 0xdb, 0xe7, 0x7, 0xcf, 0xc8, 0xdc,
    0x97, 0x8a, 0x71, 0x6a, 0xa5, 0x72, 0x65, 0x43, 0x3e, 0xad, 0xd5, 0xb6, 0x78, 0x74, 0x53, 0xe1,
    0x2e, 0x52, 0x1b, 0x57, 0x3e, 0x1d, 0xc5, 0x75, 0x5d, 0x64, 0xc8, 0x46, 0xd3, 0xd4, 0xba, 0x5f,
    0x2a, 0x84, 0x9e, 0x9a, 0xb8, 0xd3, 0x5c, 0xfc, 0xbb, 0x65, 0xc, 0xb0, 0x90, 0x7c, 0xeb, 0x2a,
    0x40, 0xa5, 0x9e, 0x2d, 0x4f, 0xc4, 0x2e, 0x87, 0xcc, 0xa6, 0x63, 0xd8, 0x88, 0x50, 0x6, 0x4d,
    0xeb, 0xf6, 0x5a, 0x30, 0x1b, 0xa5, 0x90, 0x3b, 0x4a, 0xd2, 0x8b, 0x88, 0x10, 0xd6, 0xce, 0x4e,
    0xec, 0xd7, 0x28, 0x86, 0xee, 0x79, 0x76, 0xc2, 0xf3, 0x60, 0xad, 0x4, 0x99, 0x37, 0xf3, 0xa5,
    0xd4, 0x67, 0xa5, 0xcc, 0x80, 0x1e, 0x75, 0x85, 0x35, 0x2e, 0x39, 0xb5, 0xf2, 0x89, 0xde, 0xa1,
];

const CIPHERTEXT_CBC_128: [u8; PLAINTEXT_BUF_SIZE] = [
    0x2e, 0xd1, 0x6a, 0xc7, 0x92, 0x2, 0xb9, 0x88, 0x31, 0xd7, 0xf8, 0xce, 0x9f, 0xf2, 0x24, 0xf7,
    0x1c, 0xaa, 0x4a, 0x43, 0xd6, 0xa7, 0xfd, 0x70, 0x13, 0x93, 0x7a, 0x54, 0xf7, 0xa9, 0xb0, 0xc3,
    0x44, 0xed, 0xe9, 0x19, 0x4b, 0x67, 0x74, 0x8d, 0x1, 0xf3, 0xc8, 0x21, 0x2c, 0xeb, 0xf4, 0x81,
    0x8b, 0x42, 0x32, 0x73, 0x1b, 0x3a, 0xcb, 0x1b, 0x9d, 0x1e, 0xdd, 0x81, 0xd3, 0xd7, 0xec, 0xc5,
    0x6b, 0x83, 0xda, 0xa, 0xc5, 0xe7, 0xc5, 0x31, 0xe, 0x6d, 0x44, 0xb3, 0xd9, 0x4d, 0x8a, 0x6d,
    0x8e, 0x6e, 0x84, 0x99, 0x6b, 0xbe, 0x0, 0x65, 0xce, 0xd1, 0x14, 0x4d, 0xe6, 0xc, 0x75, 0x91,
    0x1d, 0xd9, 0x9d, 0x47, 0xf3, 0x69, 0x9a, 0x6e, 0x2a, 0x63, 0xb2, 0xce, 0x4, 0x24, 0x2f, 0xb1,
    0xd3, 0x39, 0x0, 0xa6, 0x78, 0x51, 0xf2, 0xcc, 0x73, 0x79, 0xca, 0xef, 0x8a, 0x67, 0xb5, 0x1e,
    0xb0, 0x64, 0xfc, 0xed, 0x19, 0x3e, 0x8, 0xe, 0x33, 0x10, 0xae, 0x25, 0x6b, 0x5e, 0xa3, 0x3f,
    0xa4, 0x2, 0x72, 0x5d, 0x6b, 0xce, 0xec, 0xe9, 0xf3, 0x9e, 0xeb, 0x85, 0x50, 0x4e, 0x6d, 0x76,
    0x79, 0xb6, 0x75, 0x6a, 0xec, 0x1b, 0x15, 0x6b, 0x60, 0xb6, 0x76, 0x96, 0x14, 0xf1, 0x79, 0xf2,
    0x1f, 0x96, 0x91, 0xac, 0xaf, 0xdb, 0x26, 0xea, 0xc, 0x79, 0xc2, 0x4f, 0x9c, 0x22, 0xfb, 0x16,
    0x11, 0x8, 0x49, 0x56, 0x6c, 0x1c, 0x81, 0xff, 0x64, 0xb, 0x65, 0xd4, 0x5d, 0xe0, 0x81, 0x11,
    0x74, 0x1c, 0x60, 0xad, 0xfc, 0x80, 0x7a, 0xaf, 0x23, 0x8, 0x36, 0x8c, 0x57, 0xec, 0x2d, 0xe1,
    0xbc, 0xfa, 0x56, 0xe, 0xa7, 0x1, 0xa1, 0x63, 0x1f, 0xbf, 0xf6, 0xb9, 0x46, 0xa5, 0x5, 0x29,
    0x84, 0xe1, 0x23, 0xab, 0x8f, 0x3a, 0x1f, 0x89, 0xa1, 0x8e, 0x23, 0xce, 0x74, 0x31, 0x1a, 0x36,
];
#[cfg(esp32s2)]
const CIPHERTEXT_CBC_192: [u8; PLAINTEXT_BUF_SIZE] = [
    0xfe, 0x14, 0x13, 0xbf, 0x53, 0xe5, 0xbb, 0x1c, 0x66, 0x5b, 0x32, 0x2e, 0x9b, 0xb9, 0x35, 0xcb,
    0xf6, 0x6c, 0xb8, 0x26, 0x22, 0xca, 0x57, 0x9a, 0xe9, 0x98, 0x8f, 0x2f, 0xd1, 0xb1, 0xff, 0xa7,
    0xd, 0xd8, 0x92, 0x62, 0x47, 0x2d, 0x6b, 0xfc, 0x51, 0xaf, 0x84, 0x4d, 0xb4, 0x10, 0x19, 0xdf,
    0x5c, 0xb1, 0xa4, 0x5d, 0x11, 0xcf, 0x20, 0xb2, 0x2a, 0xd1, 0x43, 0x13, 0x27, 0x94, 0xd8, 0x50,
    0xfa, 0xd3, 0xb6, 0x79, 0xdb, 0xdd, 0x2f, 0x12, 0x14, 0xaa, 0x13, 0x5c, 0x9b, 0xc9, 0x7f, 0xf6,
    0xde, 0x3d, 0x63, 0x4, 0x5a, 0x0, 0x19, 0xeb, 0xf3, 0x8, 0x67, 0xda, 0x1b, 0xce, 0x28, 0xf5,
    0x1d, 0xb0, 0x6c, 0x12, 0xad, 0xfc, 0x61, 0xa9, 0xa5, 0x39, 0x7e, 0x85, 0xa9, 0xb7, 0xd5, 0xad,
    0x32, 0x1, 0x42, 0x7f, 0xd5, 0xda, 0xb0, 0x97, 0x31, 0xf8, 0xc5, 0x19, 0x44, 0xe7, 0x5, 0x79,
    0xe7, 0xd2, 0x96, 0xd0, 0x26, 0xc5, 0xf2, 0xc7, 0x23, 0x2f, 0x75, 0xde, 0x81, 0x45, 0x91, 0x5a,
    0x12, 0x7e, 0xfb, 0x5b, 0x4a, 0x3, 0xec, 0x46, 0x87, 0x34, 0x8f, 0xba, 0xf4, 0x5b, 0x29, 0x72,
    0xa7, 0x8f, 0x64, 0x2e, 0xfb, 0xb7, 0xc3, 0x9a, 0x17, 0x3, 0xae, 0xf5, 0x7e, 0xca, 0xc0, 0x1c,
    0xa3, 0x44, 0xb3, 0x84, 0x2e, 0x16, 0x77, 0xd2, 0xe5, 0x38, 0x49, 0x56, 0x9b, 0xe, 0xe8, 0xfd,
    0xfa, 0x30, 0x39, 0x0, 0xb, 0xa1, 0xaa, 0x58, 0x80, 0xfb, 0x92, 0xbb, 0xe0, 0xba, 0x5e, 0xdf,
    0xe6, 0x48, 0xf6, 0x94, 0xa8, 0xd0, 0x85, 0x63, 0xd8, 0xd3, 0x76, 0x58, 0x77, 0x2a, 0x34, 0x10,
    0x48, 0x30, 0x1f, 0x3d, 0xb0, 0x5e, 0xa, 0x3a, 0x12, 0x3d, 0x68, 0x83, 0xc9, 0x68, 0xa9, 0x44,
    0x49, 0xb2, 0x6, 0xe, 0x31, 0x3e, 0x5d, 0xe7, 0x8a, 0xea, 0xfe, 0x42, 0x95, 0xac, 0x53, 0x9a,
];
const CIPHERTEXT_CBC_256: [u8; PLAINTEXT_BUF_SIZE] = [
    0x29, 0xd4, 0xa0, 0x2e, 0x5c, 0x1d, 0xe, 0x75, 0x2, 0x2d, 0x2e, 0xdc, 0xf9, 0x56, 0x3d, 0x47,
    0xba, 0x86, 0xa2, 0xd1, 0x3b, 0xd1, 0x1d, 0xf9, 0xf2, 0x85, 0xa5, 0xa6, 0x7b, 0x8e, 0x88, 0x51,
    0x95, 0xa2, 0x3f, 0x2a, 0x61, 0x5e, 0x9b, 0x69, 0x9d, 0x9b, 0x32, 0xef, 0xbc, 0xab, 0x8a, 0x63,
    0x5a, 0x9e, 0x5c, 0xcb, 0xe6, 0xd2, 0xd0, 0xce, 0x30, 0xa8, 0x8f, 0xf8, 0x82, 0x32, 0xcf, 0xad,
    0xb2, 0xd2, 0x7d, 0xbd, 0x6c, 0xe0, 0xcf, 0xc9, 0xf3, 0xce, 0x5c, 0x6f, 0x9, 0x46, 0x34, 0x79,
    0x31, 0x83, 0xc8, 0x4c, 0x90, 0x7a, 0x6d, 0x8f, 0xb8, 0x8d, 0xa6, 0x9a, 0x30, 0x37, 0xc6, 0xe8,
    0x3f, 0xf5, 0x2f, 0xef, 0xd3, 0xf7, 0xd3, 0x77, 0xfb, 0x34, 0x44, 0x5e, 0xc, 0xfb, 0xa2, 0x9d,
    0x1a, 0x49, 0x6f, 0x10, 0xb6, 0x31, 0x76, 0xb0, 0x2a, 0xdf, 0x12, 0xaf, 0x1f, 0xdf, 0xfe, 0x57,
    0xd5, 0x24, 0x70, 0x89, 0x25, 0xa8, 0x38, 0x2b, 0xfa, 0xb9, 0x1e, 0x81, 0x74, 0xeb, 0x9b, 0x73,
    0x36, 0xa2, 0x70, 0x2b, 0x87, 0x24, 0xdb, 0xe0, 0x66, 0x2e, 0xd7, 0xea, 0xaf, 0x76, 0xe9, 0x15,
    0xa8, 0xc7, 0x78, 0xc0, 0x3b, 0xb7, 0x98, 0xbf, 0x60, 0xfe, 0x5d, 0x9f, 0x15, 0xb8, 0xb6, 0xee,
    0xb1, 0x7d, 0xf0, 0xc5, 0xcc, 0xfc, 0x6b, 0x9f, 0x94, 0xb4, 0x18, 0xa1, 0xe4, 0x8d, 0x39, 0xc8,
    0x3e, 0x92, 0x2c, 0x18, 0x26, 0xd5, 0x93, 0xc3, 0x69, 0x5c, 0x1, 0x53, 0x37, 0xa4, 0x94, 0xb5,
    0xbf, 0x87, 0xe9, 0x76, 0x70, 0x50, 0xcc, 0x8, 0x9c, 0xb9, 0x8d, 0x3a, 0x75, 0xae, 0xdc, 0x56,
    0x45, 0xd8, 0xa6, 0xd4, 0x54, 0x45, 0x75, 0x6d, 0x21, 0xcf, 0xe, 0x56, 0xd3, 0x2a, 0xe5, 0x78,
    0xc0, 0xf9, 0x75, 0x2c, 0x79, 0xcf, 0x24, 0x2e, 0x7f, 0xe0, 0xfc, 0x47, 0x16, 0x6f, 0xb8, 0x16,
];
// For the rest, we assume the other key sizes work, just to save some time on tests
const CIPHERTEXT_OFB: [u8; PLAINTEXT_BUF_SIZE] = [
    0x5, 0xcb, 0x8b, 0x29, 0x1, 0x13, 0x9e, 0xe3, 0x4f, 0x10, 0x8d, 0xdd, 0x2d, 0x58, 0x6b, 0xc7,
    0xe8, 0x8f, 0x20, 0x27, 0xbd, 0x81, 0x51, 0xa7, 0xe2, 0x5e, 0xe8, 0xa3, 0x50, 0xb1, 0xcc, 0x62,
    0x96, 0x50, 0x8a, 0x4, 0x6, 0x80, 0xd2, 0x1b, 0x9b, 0x77, 0xdc, 0x25, 0xb3, 0x1f, 0x30, 0xd0,
    0x83, 0x94, 0x5c, 0x9, 0x39, 0x10, 0x57, 0x9c, 0x3, 0xc2, 0x7e, 0x3a, 0x3a, 0x5a, 0x6c, 0xb7,
    0xc4, 0xe8, 0xc4, 0x3c, 0xfb, 0xab, 0xd8, 0xf0, 0x92, 0x79, 0xac, 0xd3, 0xd4, 0xcb, 0xab, 0xdd,
    0x40, 0x5, 0x98, 0x65, 0xc6, 0x9d, 0x0, 0x81, 0xe5, 0xd5, 0x13, 0x34, 0x80, 0xc3, 0xf6, 0xc6,
    0x4d, 0xc9, 0x52, 0x5e, 0xb2, 0xdf, 0x24, 0x4a, 0xeb, 0x5, 0x68, 0xf0, 0x63, 0xca, 0x4, 0xa0,
    0xb8, 0xc3, 0xa, 0x93, 0x74, 0x8f, 0x19, 0xec, 0x42, 0x79, 0xae, 0x3b, 0x50, 0x82, 0xc5, 0x64,
    0x70, 0x5, 0xe, 0x46, 0x36, 0xb2, 0xd1, 0x44, 0x49, 0xd0, 0xb8, 0xa1, 0x43, 0xd1, 0x65, 0x59,
    0xa9, 0xa0, 0x31, 0x68, 0x61, 0x90, 0x95, 0xda, 0xd1, 0x87, 0xeb, 0xc9, 0x8, 0x72, 0x54, 0x13,
    0x3, 0x82, 0x46, 0x98, 0xcb, 0x6e, 0x9b, 0x4c, 0xa4, 0x1d, 0xc6, 0x5b, 0xa0, 0xef, 0x84, 0xaf,
    0x3a, 0xed, 0x2e, 0x1f, 0x66, 0xad, 0xca, 0xc1, 0xf8, 0x48, 0x53, 0xda, 0xf4, 0x5c, 0xca, 0xf3,
    0xcc, 0xc1, 0x32, 0xf8, 0x76, 0x6f, 0x89, 0xa1, 0xfd, 0xfb, 0xf6, 0x21, 0xc2, 0x55, 0xf9, 0x4f,
    0x6e, 0x9e, 0x86, 0x41, 0xc6, 0xc4, 0x95, 0xc5, 0x5, 0xeb, 0xe3, 0xd4, 0xae, 0x15, 0xa3, 0x18,
    0x9c, 0xbc, 0x27, 0xd5, 0xfa, 0xe3, 0xdf, 0xed, 0xde, 0x4f, 0xa1, 0x92, 0x6d, 0x11, 0x72, 0xd6,
    0x22, 0x25, 0xe8, 0x83, 0x11, 0x55, 0x1d, 0x94, 0xa1, 0x8, 0x2c, 0x78, 0xc7, 0xf2, 0xa7, 0xae,
];
const CIPHERTEXT_CTR: [u8; PLAINTEXT_BUF_SIZE] = [
    0x5, 0xcb, 0x8b, 0x29, 0x1, 0x13, 0x9e, 0xe3, 0x4f, 0x10, 0x8d, 0xdd, 0x2d, 0x58, 0x6b, 0xc7,
    0xc4, 0x8f, 0x9a, 0x2c, 0xd8, 0x22, 0x27, 0xba, 0xda, 0x92, 0xc0, 0x36, 0xe3, 0x5, 0xb, 0x52,
    0xdb, 0xe2, 0xe5, 0x42, 0x34, 0x66, 0xfc, 0x71, 0x54, 0xfe, 0x19, 0x1e, 0xad, 0x8e, 0x12, 0x3f,
    0x72, 0x44, 0xc2, 0xdd, 0xd7, 0xb3, 0x9f, 0x6e, 0x3d, 0xff, 0xf4, 0x81, 0x49, 0x33, 0x24, 0x45,
    0x9d, 0x1b, 0xd1, 0xd5, 0x97, 0x9, 0xb1, 0x81, 0x4f, 0xec, 0x7c, 0xdb, 0x75, 0xc, 0x7c, 0x2b,
    0x27, 0x40, 0xf9, 0x9e, 0x97, 0x30, 0x53, 0x9f, 0x26, 0x75, 0xb4, 0xd1, 0x1f, 0xea, 0xdd, 0xb,
    0xa5, 0x7c, 0xc0, 0x36, 0x58, 0x47, 0x37, 0xf2, 0x41, 0x47, 0x6d, 0x44, 0x12, 0xd8, 0x10, 0x6e,
    0x86, 0xa5, 0x15, 0x1, 0x39, 0x21, 0x75, 0xd9, 0x4f, 0x2c, 0x64, 0x60, 0xad, 0xe9, 0x26, 0xde,
    0x3e, 0x3a, 0x15, 0x12, 0x19, 0x4d, 0xb4, 0x9f, 0xe2, 0x29, 0xf9, 0x23, 0xbb, 0xb0, 0xb3, 0xab,
    0x30, 0x9b, 0xe, 0xd, 0x1b, 0xe0, 0xd2, 0x2b, 0x19, 0x73, 0x13, 0xfa, 0xf1, 0x6b, 0x64, 0x9,
    0xe6, 0x7e, 0x2c, 0x0, 0x42, 0x65, 0x69, 0x25, 0x57, 0xfd, 0x8c, 0x5, 0x38, 0x4d, 0xa5, 0xcd,
    0xeb, 0xa6, 0xaf, 0x5c, 0x52, 0xbc, 0x89, 0x34, 0xe6, 0xc2, 0x1e, 0xe3, 0x1d, 0x8d, 0x75, 0x98,
    0x54, 0x5a, 0xad, 0xb0, 0x61, 0xf9, 0x96, 0x5, 0xf8, 0xde, 0x4f, 0xbe, 0xad, 0xd8, 0x9b, 0xb2,
    0xfa, 0x46, 0xe8, 0xeb, 0xf4, 0x45, 0x71, 0x39, 0xaa, 0x4d, 0x2a, 0xd2, 0xc7, 0x6e, 0x67, 0x7a,
    0x21, 0x9d, 0x58, 0x1e, 0x8b, 0xec, 0x4, 0xac, 0x75, 0x47, 0x44, 0x17, 0x2b, 0x26, 0x14, 0xca,
    0x7b, 0xe0, 0xe6, 0xa6, 0x27, 0x90, 0xca, 0x30, 0x41, 0xd2, 0xe3, 0x80, 0xe8, 0x43, 0x7, 0x86,
];
const CIPHERTEXT_CFB8: [u8; PLAINTEXT_BUF_SIZE] = [
    0x5, 0x31, 0xde, 0x6a, 0xd7, 0x13, 0x10, 0x22, 0xcb, 0x16, 0x53, 0x27, 0x9b, 0xc7, 0x2d, 0xd2,
    0x47, 0x89, 0xa4, 0x72, 0xb3, 0x67, 0x68, 0xb1, 0xad, 0xd8, 0x3f, 0x7d, 0xb3, 0xee, 0x19, 0xf6,
    0x1, 0x79, 0x3d, 0x35, 0x19, 0x68, 0xc6, 0xf1, 0x53, 0xf2, 0x62, 0x29, 0xdf, 0xd2, 0xd8, 0x7f,
    0x1, 0xaf, 0x76, 0x38, 0x27, 0x7a, 0xec, 0xd6, 0x30, 0x6d, 0x1, 0xcd, 0xcb, 0xd4, 0xbd, 0x39,
    0x3a, 0x75, 0x46, 0x6c, 0xa4, 0x28, 0xf2, 0x90, 0x3c, 0x6, 0xe2, 0x2f, 0x7e, 0x47, 0xb8, 0x97,
    0xcc, 0xd3, 0x69, 0xdd, 0x58, 0xff, 0x6f, 0x8e, 0x46, 0xf0, 0x9a, 0xb8, 0xa7, 0x50, 0x5b, 0xc9,
    0xe0, 0xac, 0x25, 0xb5, 0x70, 0x59, 0x33, 0x22, 0x97, 0x37, 0xe3, 0x74, 0x5d, 0x3a, 0x7c, 0x5f,
    0x27, 0x81, 0xba, 0xee, 0xd1, 0x22, 0x2c, 0x1b, 0xa5, 0xf3, 0xd2, 0xc7, 0x43, 0xf2, 0xd, 0xa4,
    0x4c, 0xd5, 0xd, 0xf3, 0xe5, 0x81, 0x47, 0x62, 0xa6, 0x53, 0x9e, 0x95, 0xb9, 0xd5, 0x6f, 0xd7,
    0x4c, 0x6, 0xef, 0x12, 0xe9, 0x90, 0x39, 0xaa, 0x52, 0xab, 0xc8, 0x21, 0xf8, 0x5e, 0x3b, 0x6a,
    0x4, 0x63, 0x5c, 0x94, 0x8a, 0xb3, 0xe2, 0x85, 0xf3, 0xd4, 0x35, 0xb2, 0xb4, 0x3c, 0x50, 0x89,
    0xe5, 0xc3, 0x2, 0x1a, 0x7f, 0xc7, 0x34, 0x65, 0x62, 0xc6, 0xb9, 0xe9, 0x4a, 0xa7, 0xf3, 0xd1,
    0x91, 0x57, 0x8, 0x2f, 0xd7, 0x8b, 0x4a, 0xd9, 0xa, 0xd9, 0x14, 0xfe, 0xfc, 0xab, 0x44, 0x73,
    0xb2, 0x95, 0xb, 0x7, 0x69, 0xc4, 0xdb, 0x4d, 0xcb, 0x35, 0x98, 0xd8, 0x10, 0x6, 0xcd, 0x67,
    0x3, 0xa3, 0x1b, 0x24, 0xef, 0xab, 0x11, 0xbd, 0x42, 0x94, 0x71, 0xd, 0xb8, 0xb0, 0xed, 0x6c,
    0xd8, 0x39, 0xec, 0x8c, 0x3f, 0x29, 0xbc, 0x19, 0x81, 0x3b, 0x89, 0x25, 0x3c, 0x36, 0x59, 0x7a,
];
const CIPHERTEXT_CFB128: [u8; PLAINTEXT_BUF_SIZE] = [
    0x5, 0xcb, 0x8b, 0x29, 0x1, 0x13, 0x9e, 0xe3, 0x4f, 0x10, 0x8d, 0xdd, 0x2d, 0x58, 0x6b, 0xc7,
    0xfc, 0x87, 0x8a, 0xb4, 0x87, 0xa2, 0x4, 0x40, 0xf5, 0x4e, 0x70, 0x53, 0x10, 0x89, 0x79, 0xab,
    0x83, 0x67, 0x12, 0xbb, 0x7d, 0x12, 0xd0, 0xf4, 0xcc, 0x89, 0x24, 0x8e, 0x51, 0x5a, 0x35, 0x17,
    0x51, 0xe3, 0x71, 0xd0, 0xcf, 0x67, 0xd2, 0x81, 0x8f, 0x2e, 0x69, 0x70, 0xe2, 0xc8, 0x8c, 0x52,
    0x62, 0x82, 0xa6, 0xea, 0x35, 0xbc, 0xe2, 0x50, 0xc7, 0x99, 0xe2, 0xb9, 0x66, 0x38, 0x76, 0x7c,
    0xcd, 0x95, 0xe8, 0x1c, 0xd2, 0x50, 0x98, 0x2c, 0x32, 0xe8, 0x1a, 0x88, 0xd1, 0xd2, 0xd0, 0xb4,
    0xa4, 0xa8, 0x9a, 0x86, 0x6a, 0x75, 0x9f, 0x20, 0xb6, 0xac, 0x24, 0x53, 0x41, 0x2d, 0xa4, 0x1a,
    0xa9, 0xd6, 0xde, 0x25, 0x56, 0x6f, 0xfb, 0xeb, 0xae, 0x27, 0xeb, 0xe0, 0x25, 0xd2, 0x27, 0xe6,
    0x20, 0xc1, 0x8a, 0x50, 0x52, 0x5, 0x32, 0xc8, 0x16, 0x75, 0x62, 0xec, 0xe3, 0xa, 0x5, 0xac,
    0x4e, 0x2c, 0x53, 0xfe, 0x23, 0x34, 0x33, 0xe2, 0x30, 0xcf, 0xd, 0x5d, 0x1e, 0x73, 0x35, 0xa7,
    0xb1, 0xef, 0x58, 0xea, 0x99, 0x3b, 0xe8, 0x70, 0x6a, 0x4d, 0xb2, 0xb5, 0x5e, 0x13, 0x39, 0x91,
    0x9, 0xa7, 0x60, 0x65, 0x9b, 0x58, 0x9b, 0xa5, 0x35, 0xcc, 0x3e, 0xc1, 0x12, 0x15, 0xc6, 0x6c,
    0xf7, 0xba, 0x1f, 0x9a, 0x75, 0x77, 0xe5, 0x38, 0x20, 0xe5, 0x4b, 0x60, 0x48, 0x58, 0x23, 0x50,
    0x86, 0xc9, 0x70, 0x3f, 0xe9, 0x2f, 0xeb, 0x73, 0xa4, 0x43, 0x77, 0xcf, 0xde, 0xee, 0x77, 0x2,
    0x27, 0xd9, 0xc5, 0xbb, 0xac, 0x7c, 0xdc, 0x1b, 0xfa, 0xb, 0x72, 0xd2, 0xd7, 0x1e, 0xeb, 0x68,
    0xa5, 0x1b, 0xae, 0xdc, 0x78, 0x4f, 0xcf, 0xcf, 0x31, 0xee, 0xb6, 0xc5, 0x7c, 0x2d, 0x81, 0x37,
];

#[cfg(aes_dma)]
extern crate alloc;

const fn pad_to<const K: usize>(input: &[u8]) -> [u8; K] {
    let mut out = [0; K];

    let in_bytes = input.len();
    assert!(in_bytes <= K);

    let mut i = 0;
    while i < in_bytes {
        out[i] = input[i];
        i += 1;
    }

    out
}
fn aes_ll_roundtrip<const K: usize>(aes: &mut Aes<'_>, plaintext: [u8; 16], ciphertext: [u8; 16])
where
    Key: From<[u8; K]>,
{
    let mut block_buf = plaintext;
    aes.encrypt(&mut block_buf, pad_to::<K>(KEY));
    hil_test::assert_eq!(&block_buf[..ciphertext.len()], ciphertext);

    let mut block_buf = ciphertext;
    aes.decrypt(&mut block_buf, pad_to::<K>(KEY));
    hil_test::assert_eq!(&block_buf[..plaintext.len()], plaintext);
}

fn aes_roundtrip<const K: usize>(
    tag: &'static str,
    block_ctx: impl Into<CipherState>,
    plaintext: &[u8],
    ciphertext: &[u8],
    buffer: &mut [u8],
) where
    Key: From<[u8; K]>,
{
    let block_ctx = block_ctx.into();
    defmt::info!("{}: buffer @ {:x}", tag, buffer.as_ptr() as usize);
    let mut ecb_encrypt = AesContext::new(block_ctx.clone(), Operation::Encrypt, pad_to::<K>(KEY));
    let handle = ecb_encrypt.process(plaintext, buffer).unwrap();
    handle.wait_blocking();
    hil_test::assert_eq!(
        buffer,
        ciphertext,
        "Failed to encrypt with {}, K={}",
        tag,
        K
    );

    let mut ecb_decrypt = AesContext::new(block_ctx, Operation::Decrypt, pad_to::<K>(KEY));
    let handle = ecb_decrypt.process_in_place(buffer).unwrap();
    handle.wait_blocking();
    hil_test::assert_eq!(buffer, plaintext, "Failed to encrypt with {}, K={}", tag, K);
}

fn run_cipher_tests(buffer: &mut [u8]) {
    let mut plaintext = [0; PLAINTEXT_BUF_SIZE];
    fill_with_plaintext(&mut plaintext);

    // Let's use ECB as a test case for short unaligned DMA transfers.
    let short_len = 16;
    aes_roundtrip::<16>(
        "Short ECB",
        Ecb,
        &plaintext[0..short_len],
        &CIPHERTEXT_ECB_128[0..short_len],
        &mut buffer[0..short_len],
    );

    aes_roundtrip::<16>("ECB", Ecb, &plaintext, &CIPHERTEXT_ECB_128, buffer);
    #[cfg(any(esp32, esp32s2))]
    aes_roundtrip::<24>("ECB", Ecb, &plaintext, &CIPHERTEXT_ECB_192, buffer);
    aes_roundtrip::<32>("ECB", Ecb, &plaintext, &CIPHERTEXT_ECB_256, buffer);

    aes_roundtrip::<16>("CBC", Cbc::new(IV), &plaintext, &CIPHERTEXT_CBC_128, buffer);
    #[cfg(esp32s2)]
    aes_roundtrip::<24>("CBC", Cbc::new(IV), &plaintext, &CIPHERTEXT_CBC_192, buffer);
    aes_roundtrip::<32>("CBC", Cbc::new(IV), &plaintext, &CIPHERTEXT_CBC_256, buffer);

    aes_roundtrip::<16>("OFB", Ofb::new(IV), &plaintext, &CIPHERTEXT_OFB, buffer);
    aes_roundtrip::<16>("CTR", Ctr::new(IV), &plaintext, &CIPHERTEXT_CTR, buffer);
    aes_roundtrip::<16>("CFB-8", Cfb8::new(IV), &plaintext, &CIPHERTEXT_CFB8, buffer);
    aes_roundtrip::<16>(
        "CFB-128",
        Cfb128::new(IV),
        &plaintext,
        &CIPHERTEXT_CFB128,
        buffer,
    );
}

#[cfg(aes_dma)]
fn run_unaligned_dma_tests<const MAX_SHIFT: usize>(memory: &mut [u8]) {
    let zeros = [0; MAX_SHIFT];

    // Different alignments in internal memory
    for shift in 0..MAX_SHIFT {
        memory.fill(0);
        let buffer = &mut memory[shift..][..PLAINTEXT_BUF_SIZE];
        run_cipher_tests(buffer);
        hil_test::assert_eq!(
            memory[..shift],
            zeros[..shift],
            "Out of bounds write with shift {}",
            shift
        );
    }
}

#[embedded_test::tests(default_timeout = 10, executor = hil_test::Executor::new())] // defmt slows the tests down a bit
mod tests {
    use super::*;

    #[test]
    fn test_aes_block_operation() {
        let p = esp_hal::init(Config::default().with_cpu_clock(CpuClock::max()));
        let mut aes = Aes::new(p.AES);

        aes_ll_roundtrip::<16>(
            &mut aes,
            PLAINTEXT[0..16].try_into().unwrap(),
            CIPHERTEXT_ECB_128[0..16].try_into().unwrap(),
        );

        #[cfg(any(esp32, esp32s2))]
        aes_ll_roundtrip::<24>(
            &mut aes,
            PLAINTEXT[0..16].try_into().unwrap(),
            CIPHERTEXT_ECB_192[0..16].try_into().unwrap(),
        );

        aes_ll_roundtrip::<32>(
            &mut aes,
            PLAINTEXT[0..16].try_into().unwrap(),
            CIPHERTEXT_ECB_256[0..16].try_into().unwrap(),
        );
    }

    #[test]
    #[cfg(not(esp32))]
    fn test_aes_dma_ecb() {
        use esp_hal::{
            aes::dma::AesDma,
            dma::{DmaRxBuf, DmaTxBuf},
            dma_buffers,
        };

        fn test_aes_ecb<const K: usize>(
            mut aes: AesDma<'_>,
            plaintext: [u8; 16],
            ciphertext: [u8; 16],
        ) -> AesDma<'_>
        where
            Key: From<[u8; K]>,
        {
            use esp_hal::aes::dma::DmaCipherState;

            const DMA_BUFFER_SIZE: usize = 16;

            let (output, rx_descriptors, input, tx_descriptors) = dma_buffers!(DMA_BUFFER_SIZE);
            let mut output = DmaRxBuf::new(rx_descriptors, output).unwrap();
            let mut input = DmaTxBuf::new(tx_descriptors, input).unwrap();

            // Encrypt
            input.as_mut_slice().copy_from_slice(&plaintext);
            let transfer = aes
                .process(
                    1,
                    output,
                    input,
                    Operation::Encrypt,
                    &DmaCipherState::from(Ecb),
                    pad_to::<K>(KEY),
                )
                .map_err(|e| e.0)
                .unwrap();
            (aes, output, input) = transfer.wait();
            hil_test::assert_eq!(output.as_slice(), ciphertext);

            // Decrypt
            input.as_mut_slice().copy_from_slice(&ciphertext);
            let transfer = aes
                .process(
                    1,
                    output,
                    input,
                    Operation::Decrypt,
                    &DmaCipherState::from(Ecb),
                    pad_to::<K>(KEY),
                )
                .map_err(|e| e.0)
                .unwrap();
            (aes, output, _) = transfer.wait();
            hil_test::assert_eq!(output.as_slice(), plaintext);

            aes
        }
        let peripherals = esp_hal::init(Config::default().with_cpu_clock(CpuClock::max()));

        cfg_if::cfg_if! {
            if #[cfg(esp32s2)] {
                let dma_channel = peripherals.DMA_CRYPTO;
            } else {
                let dma_channel = peripherals.DMA_CH0;
            }
        }

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

        let aes = test_aes_ecb::<16>(
            aes,
            PLAINTEXT[0..16].try_into().unwrap(),
            CIPHERTEXT_ECB_128[0..16].try_into().unwrap(),
        );
        #[cfg(esp32s2)]
        let aes = test_aes_ecb::<24>(
            aes,
            PLAINTEXT[0..16].try_into().unwrap(),
            CIPHERTEXT_ECB_192[0..16].try_into().unwrap(),
        );
        let _ = test_aes_ecb::<32>(
            aes,
            PLAINTEXT[0..16].try_into().unwrap(),
            CIPHERTEXT_ECB_256[0..16].try_into().unwrap(),
        );
    }

    #[test]
    fn test_aes_work_queue_cpu() {
        let p = esp_hal::init(Config::default().with_cpu_clock(CpuClock::max()));

        let mut aes = AesBackend::new(p.AES);
        let _backend = aes.start();

        let mut buffer = [0; PLAINTEXT_BUF_SIZE];
        run_cipher_tests(&mut buffer);
    }

    #[test]
    #[cfg(aes_dma)]
    fn test_aes_dma_work_queue() {
        use allocator_api2::vec::Vec;

        let p = esp_hal::init(Config::default().with_cpu_clock(CpuClock::max()));

        esp_alloc::heap_allocator!(size: 32 * 1024);

        cfg_if::cfg_if! {
            if #[cfg(esp32s2)] {
                let mut aes = AesDmaBackend::new(p.AES, p.DMA_CRYPTO);
            } else {
                let mut aes = AesDmaBackend::new(p.AES, p.DMA_CH0);
            }
        }
        let _backend = aes.start();

        const MAX_SHIFT: usize = 15;

        let mut internal_memory =
            Vec::with_capacity_in(PLAINTEXT_BUF_SIZE + MAX_SHIFT, esp_alloc::InternalMemory);
        internal_memory.resize(PLAINTEXT_BUF_SIZE + MAX_SHIFT, 0);

        // Different alignments in internal memory
        run_unaligned_dma_tests::<MAX_SHIFT>(&mut internal_memory);
    }

    #[test]
    #[cfg(all(aes_dma, psram))]
    fn test_aes_dma_work_queue_psram() {
        use allocator_api2::vec::Vec;

        let p = esp_hal::init(Config::default().with_cpu_clock(CpuClock::max()));
        esp_alloc::psram_allocator!(p.PSRAM, esp_hal::psram);

        cfg_if::cfg_if! {
            if #[cfg(esp32s2)] {
                let mut aes = AesDmaBackend::new(p.AES, p.DMA_CRYPTO);
            } else {
                let mut aes = AesDmaBackend::new(p.AES, p.DMA_CH0);
            }
        }
        let _backend = aes.start();

        let mut plaintext = [0; PLAINTEXT_BUF_SIZE];
        fill_with_plaintext(&mut plaintext);

        const MAX_SHIFT: usize = 15;

        let mut external_memory =
            Vec::with_capacity_in(PLAINTEXT_BUF_SIZE + MAX_SHIFT, esp_alloc::ExternalMemory);
        external_memory.resize(PLAINTEXT_BUF_SIZE + MAX_SHIFT, 0);

        // Different alignments in external memory
        run_unaligned_dma_tests::<MAX_SHIFT>(&mut external_memory);
    }

    #[test]
    fn test_aes_work_queue_work_posted_before_queue_started() {
        let p = esp_hal::init(Config::default().with_cpu_clock(CpuClock::max()));

        let mut output = [0; PLAINTEXT_BUF_SIZE];

        let mut plaintext = [0; PLAINTEXT_BUF_SIZE];
        fill_with_plaintext(&mut plaintext);

        let mut ecb_encrypt = AesContext::new(Ecb, Operation::Encrypt, KEY_128);
        let handle = ecb_encrypt.process(&plaintext, &mut output).unwrap();

        let mut aes = AesBackend::new(p.AES);
        let _backend = aes.start();

        handle.wait_blocking();

        hil_test::assert_eq!(output, CIPHERTEXT_ECB_128);
    }

    #[test]
    async fn test_aes_work_queue_work_posted_before_queue_started_async() {
        #[embassy_executor::task]
        async fn aes_task(signal: &'static Signal<CriticalSectionRawMutex, ()>) {
            let mut output = [0; PLAINTEXT_BUF_SIZE];
            let mut plaintext = [0; PLAINTEXT_BUF_SIZE];
            fill_with_plaintext(&mut plaintext);

            let mut ecb_encrypt = AesContext::new(Ecb, Operation::Encrypt, KEY_128);
            let mut handle = ecb_encrypt.process(&plaintext, &mut output).unwrap();

            // Backend can start now
            signal.signal(());

            handle.wait().await;
            core::mem::drop(handle);

            hil_test::assert_eq!(output, CIPHERTEXT_ECB_128);

            // Test can end now
            signal.signal(());
        }

        let p = esp_hal::init(Config::default().with_cpu_clock(CpuClock::max()));

        let signal = mk_static!(Signal<CriticalSectionRawMutex, ()>, Signal::new());

        // Start task before we'd start the AES operation
        let spawner = SendSpawner::for_current_executor().await;
        spawner.must_spawn(aes_task(signal));

        signal.wait().await;

        let mut aes = AesBackend::new(p.AES);
        let _backend = aes.start();

        signal.wait().await;
    }

    #[test]
    fn test_aes_work_queue_in_place() {
        let p = esp_hal::init(Config::default().with_cpu_clock(CpuClock::max()));

        let mut buffer = [0; PLAINTEXT_BUF_SIZE];
        fill_with_plaintext(&mut buffer);

        let mut ecb_encrypt = AesContext::new(Ecb, Operation::Encrypt, KEY_128);
        let handle = ecb_encrypt.process_in_place(&mut buffer).unwrap();

        let mut aes = AesBackend::new(p.AES);
        let _backend = aes.start();

        handle.wait_blocking();

        hil_test::assert_eq!(buffer, CIPHERTEXT_ECB_128);
    }

    #[test]
    fn test_aes_cancelling_work() {
        // In this test, we post two work items, and cancel the first one before starting the
        // backend. We will assert that, when the second item has finished correctly, the first did
        // not modify its output buffer.
        // Note that this result is not guaranteed. The cancellation can come later than the work
        // item has finished processing. We can only reliably test it because we start the backend
        // after cancelling the operation.
        let p = esp_hal::init(Config::default().with_cpu_clock(CpuClock::max()));

        let mut buffer1 = [0; PLAINTEXT_BUF_SIZE];
        fill_with_plaintext(&mut buffer1);
        let mut ecb_encrypt = AesContext::new(Ecb, Operation::Encrypt, KEY_128);
        let handle1 = ecb_encrypt.process_in_place(&mut buffer1).unwrap();

        let mut buffer2 = [0; PLAINTEXT_BUF_SIZE];
        fill_with_plaintext(&mut buffer2);
        let mut ecb_encrypt = AesContext::new(Ecb, Operation::Encrypt, KEY_256);
        let handle2 = ecb_encrypt.process_in_place(&mut buffer2).unwrap();

        let mut buffer3 = [0; PLAINTEXT_BUF_SIZE];
        fill_with_plaintext(&mut buffer3);
        let mut ecb_encrypt = AesContext::new(Ecb, Operation::Encrypt, KEY_128);
        let handle3 = ecb_encrypt.process_in_place(&mut buffer3).unwrap();

        core::mem::drop(handle1);

        let mut aes = AesBackend::new(p.AES);
        let _backend = aes.start();

        handle3.wait_blocking();
        handle2.wait_blocking();

        hil_test::assert_eq!(&buffer1[..PLAINTEXT.len()], PLAINTEXT);
        hil_test::assert_eq!(buffer2, CIPHERTEXT_ECB_256);
        hil_test::assert_eq!(buffer3, CIPHERTEXT_ECB_128);
    }
}
