Skip to content

Commit fd66bd3

Browse files
authored
Encode AES key lengths in metadata (#3843)
1 parent 0d9f43d commit fd66bd3

20 files changed

+248
-88
lines changed

esp-hal/src/aes/esp32.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::aes::{ALIGN_SIZE, Aes, Aes128, Aes192, Aes256, AesFlavour, Endianness, Mode};
1+
use crate::aes::{ALIGN_SIZE, Aes, Endianness, Mode};
22

33
impl Aes<'_> {
44
pub(super) fn init(&mut self) {
@@ -66,15 +66,3 @@ impl Aes<'_> {
6666
.volatile_read_regset(self.regs().text(0).as_ptr(), block, text_len);
6767
}
6868
}
69-
70-
impl AesFlavour for Aes128 {
71-
type KeyType<'b> = &'b [u8; 16];
72-
}
73-
74-
impl AesFlavour for Aes192 {
75-
type KeyType<'b> = &'b [u8; 24];
76-
}
77-
78-
impl AesFlavour for Aes256 {
79-
type KeyType<'b> = &'b [u8; 32];
80-
}

esp-hal/src/aes/esp32cX.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::aes::{ALIGN_SIZE, Aes, Aes128, Aes256, AesFlavour, Mode};
1+
use crate::aes::{ALIGN_SIZE, Aes, Mode};
22

33
impl Aes<'_> {
44
pub(super) fn init(&mut self) {
@@ -42,11 +42,3 @@ impl Aes<'_> {
4242
.volatile_read_regset(self.regs().text_out(0).as_ptr(), block, 4);
4343
}
4444
}
45-
46-
impl AesFlavour for Aes128 {
47-
type KeyType<'b> = &'b [u8; 16];
48-
}
49-
50-
impl AesFlavour for Aes256 {
51-
type KeyType<'b> = &'b [u8; 32];
52-
}

esp-hal/src/aes/esp32s2.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::aes::{ALIGN_SIZE, Aes, Aes128, Aes192, Aes256, AesFlavour, Endianness, Mode};
1+
use crate::aes::{ALIGN_SIZE, Aes, Endianness, Mode};
22

33
impl Aes<'_> {
44
pub(super) fn init(&mut self) {
@@ -79,15 +79,3 @@ impl Aes<'_> {
7979
);
8080
}
8181
}
82-
83-
impl AesFlavour for Aes128 {
84-
type KeyType<'b> = &'b [u8; 16];
85-
}
86-
87-
impl AesFlavour for Aes192 {
88-
type KeyType<'b> = &'b [u8; 24];
89-
}
90-
91-
impl AesFlavour for Aes256 {
92-
type KeyType<'b> = &'b [u8; 32];
93-
}

esp-hal/src/aes/esp32s3.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::aes::{ALIGN_SIZE, Aes, Aes128, Aes256, AesFlavour, Mode};
1+
use crate::aes::{ALIGN_SIZE, Aes, Mode};
22

33
impl Aes<'_> {
44
pub(super) fn init(&mut self) {
@@ -51,11 +51,3 @@ impl Aes<'_> {
5151
);
5252
}
5353
}
54-
55-
impl AesFlavour for Aes128 {
56-
type KeyType<'b> = &'b [u8; 16];
57-
}
58-
59-
impl AesFlavour for Aes256 {
60-
type KeyType<'b> = &'b [u8; 32];
61-
}

esp-hal/src/aes/mod.rs

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,32 @@ const ALIGN_SIZE: usize = core::mem::size_of::<u32>();
7676
/// Represents the various key sizes allowed for AES encryption and decryption.
7777
pub enum Key {
7878
/// 128-bit AES key
79+
#[cfg(aes_key_length_128)]
7980
Key16([u8; 16]),
8081
/// 192-bit AES key
81-
#[cfg(any(esp32, esp32s2))]
82+
#[cfg(aes_key_length_192)]
8283
Key24([u8; 24]),
8384
/// 256-bit AES key
85+
#[cfg(aes_key_length_256)]
8486
Key32([u8; 32]),
8587
}
8688

8789
// Implementing From for easy conversion from array to Key enum.
90+
#[cfg(aes_key_length_128)]
8891
impl From<[u8; 16]> for Key {
8992
fn from(key: [u8; 16]) -> Self {
9093
Key::Key16(key)
9194
}
9295
}
9396

94-
#[cfg(any(esp32, esp32s2))]
97+
#[cfg(aes_key_length_192)]
9598
impl From<[u8; 24]> for Key {
9699
fn from(key: [u8; 24]) -> Self {
97100
Key::Key24(key)
98101
}
99102
}
100103

104+
#[cfg(aes_key_length_256)]
101105
impl From<[u8; 32]> for Key {
102106
fn from(key: [u8; 32]) -> Self {
103107
Key::Key32(key)
@@ -108,9 +112,11 @@ impl Key {
108112
/// Returns a slice representation of the AES key.
109113
fn as_slice(&self) -> &[u8] {
110114
match self {
115+
#[cfg(aes_key_length_128)]
111116
Key::Key16(key) => key,
112-
#[cfg(any(esp32, esp32s2))]
117+
#[cfg(aes_key_length_192)]
113118
Key::Key24(key) => key,
119+
#[cfg(aes_key_length_256)]
114120
Key::Key32(key) => key,
115121
}
116122
}
@@ -119,18 +125,22 @@ impl Key {
119125
/// Defines the operating modes for AES encryption and decryption.
120126
pub enum Mode {
121127
/// Encryption mode with 128-bit key
128+
#[cfg(aes_key_length_128)]
122129
Encryption128 = 0,
123130
/// Encryption mode with 192-bit key
124-
#[cfg(any(esp32, esp32s2))]
131+
#[cfg(aes_key_length_192)]
125132
Encryption192 = 1,
126133
/// Encryption mode with 256-bit key
134+
#[cfg(aes_key_length_256)]
127135
Encryption256 = 2,
128136
/// Decryption mode with 128-bit key
137+
#[cfg(aes_key_length_128)]
129138
Decryption128 = 4,
130139
/// Decryption mode with 192-bit key
131-
#[cfg(any(esp32, esp32s2))]
140+
#[cfg(aes_key_length_192)]
132141
Decryption192 = 5,
133142
/// Decryption mode with 256-bit key
143+
#[cfg(aes_key_length_256)]
134144
Decryption256 = 6,
135145
}
136146

@@ -201,20 +211,39 @@ pub trait AesFlavour: crate::private::Sealed {
201211
}
202212

203213
/// Marker type for AES-128
214+
#[cfg(aes_key_length_128)]
204215
pub struct Aes128;
205216

206217
/// Marker type for AES-192
207-
#[cfg(any(esp32, esp32s2))]
218+
#[cfg(aes_key_length_192)]
208219
pub struct Aes192;
209220

210221
/// Marker type for AES-256
222+
#[cfg(aes_key_length_256)]
211223
pub struct Aes256;
212224

225+
#[cfg(aes_key_length_128)]
213226
impl crate::private::Sealed for Aes128 {}
214-
#[cfg(any(esp32, esp32s2))]
227+
#[cfg(aes_key_length_192)]
215228
impl crate::private::Sealed for Aes192 {}
229+
#[cfg(aes_key_length_256)]
216230
impl crate::private::Sealed for Aes256 {}
217231

232+
#[cfg(aes_key_length_128)]
233+
impl AesFlavour for Aes128 {
234+
type KeyType<'b> = &'b [u8; 16];
235+
}
236+
237+
#[cfg(aes_key_length_192)]
238+
impl AesFlavour for Aes192 {
239+
type KeyType<'b> = &'b [u8; 24];
240+
}
241+
242+
#[cfg(aes_key_length_256)]
243+
impl AesFlavour for Aes256 {
244+
type KeyType<'b> = &'b [u8; 32];
245+
}
246+
218247
/// State matrix endianness
219248
#[cfg(any(esp32, esp32s2))]
220249
pub enum Endianness {
@@ -230,7 +259,7 @@ pub enum Endianness {
230259
/// transfer, which can significantly speed up operations when dealing with
231260
/// large data volumes. It supports various cipher modes such as ECB, CBC, OFB,
232261
/// CTR, CFB8, and CFB128.
233-
#[cfg(any(esp32c3, esp32c6, esp32h2, esp32s2, esp32s3))]
262+
#[cfg(aes_dma)]
234263
pub mod dma {
235264
use core::mem::ManuallyDrop;
236265

@@ -246,6 +275,7 @@ pub mod dma {
246275
PeripheralDmaChannel,
247276
},
248277
peripherals::AES,
278+
system::{Peripheral, PeripheralClockControl},
249279
};
250280

251281
const ALIGN_SIZE: usize = core::mem::size_of::<u32>();
@@ -254,17 +284,23 @@ pub mod dma {
254284
#[derive(Clone, Copy, PartialEq, Eq)]
255285
pub enum CipherMode {
256286
/// Electronic Codebook Mode
287+
#[cfg(aes_dma_mode_ecb)]
257288
Ecb = 0,
258289
/// Cipher Block Chaining Mode
259290
Cbc,
260291
/// Output Feedback Mode
292+
#[cfg(aes_dma_mode_ofb)]
261293
Ofb,
262294
/// Counter Mode.
295+
#[cfg(aes_dma_mode_ctr)]
263296
Ctr,
264297
/// Cipher Feedback Mode with 8-bit shifting.
298+
#[cfg(aes_dma_mode_cfb8)]
265299
Cfb8,
266300
/// Cipher Feedback Mode with 128-bit shifting.
301+
#[cfg(aes_dma_mode_cfb128)]
267302
Cfb128,
303+
// TODO: GCM needs different handling, not supported yet
268304
}
269305

270306
/// A DMA capable AES instance.
@@ -371,28 +407,8 @@ pub mod dma {
371407
})
372408
}
373409

374-
#[cfg(any(esp32c3, esp32s2, esp32s3))]
375-
fn reset_aes(&self) {
376-
use crate::peripherals::SYSTEM;
377-
378-
SYSTEM::regs()
379-
.perip_rst_en1()
380-
.modify(|_, w| w.crypto_aes_rst().set_bit());
381-
SYSTEM::regs()
382-
.perip_rst_en1()
383-
.modify(|_, w| w.crypto_aes_rst().clear_bit());
384-
}
385-
386-
#[cfg(any(esp32c6, esp32h2))]
387410
fn reset_aes(&self) {
388-
use crate::peripherals::PCR;
389-
390-
PCR::regs()
391-
.aes_conf()
392-
.modify(|_, w| w.aes_rst_en().set_bit());
393-
PCR::regs()
394-
.aes_conf()
395-
.modify(|_, w| w.aes_rst_en().clear_bit());
411+
PeripheralClockControl::reset(Peripheral::Aes);
396412
}
397413

398414
fn dma_peripheral(&self) -> DmaPeripheral {

0 commit comments

Comments
 (0)