@@ -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.
7777pub 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) ]
8891impl 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 ) ]
9598impl 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) ]
101105impl 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.
120126pub 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) ]
204215pub struct Aes128 ;
205216
206217/// Marker type for AES-192
207- #[ cfg( any ( esp32 , esp32s2 ) ) ]
218+ #[ cfg( aes_key_length_192 ) ]
208219pub struct Aes192 ;
209220
210221/// Marker type for AES-256
222+ #[ cfg( aes_key_length_256) ]
211223pub struct Aes256 ;
212224
225+ #[ cfg( aes_key_length_128) ]
213226impl crate :: private:: Sealed for Aes128 { }
214- #[ cfg( any ( esp32 , esp32s2 ) ) ]
227+ #[ cfg( aes_key_length_192 ) ]
215228impl crate :: private:: Sealed for Aes192 { }
229+ #[ cfg( aes_key_length_256) ]
216230impl 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) ) ]
220249pub 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 ) ]
234263pub 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