Struct stm32wlxx_hal::aes::Aes
source · pub struct Aes { /* private fields */ }
Expand description
AES driver.
Implementations§
source§impl Aes
impl Aes
sourcepub fn new(aes: AES, rcc: &mut RCC) -> Aes
pub fn new(aes: AES, rcc: &mut RCC) -> Aes
Create a new AES driver from an AES peripheral.
This will enable clocks and reset the AES peripheral.
Example
use stm32wlxx_hal::{aes::Aes, pac};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut aes: Aes = Aes::new(dp.AES, &mut dp.RCC);
sourcepub fn free(self) -> AES
pub fn free(self) -> AES
Free the AES peripheral from the driver.
Example
use stm32wlxx_hal::{aes::Aes, pac};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut aes: Aes = Aes::new(dp.AES, &mut dp.RCC);
// ... use AES
let aes: pac::AES = aes.free();
sourcepub unsafe fn pulse_reset(rcc: &mut RCC)
pub unsafe fn pulse_reset(rcc: &mut RCC)
sourcepub unsafe fn disable_clock(rcc: &mut RCC)
pub unsafe fn disable_clock(rcc: &mut RCC)
Disable the AES peripheral clock.
Safety
- Ensure nothing is using the AES peripheral before disabling the clock.
- You are responsible for re-enabling the clock before using the AES peripheral.
Example
use stm32wlxx_hal::{aes::Aes, pac};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut aes: Aes = Aes::new(dp.AES, &mut dp.RCC);
// ... use AES
// safety: AES is not in use
unsafe { Aes::disable_clock(&mut dp.RCC) };
// have a low power nap or something
Aes::enable_clock(&mut dp.RCC);
// ... use AES
sourcepub fn enable_clock(rcc: &mut RCC)
pub fn enable_clock(rcc: &mut RCC)
sourcepub const unsafe fn new_no_init(aes: AES) -> Aes
pub const unsafe fn new_no_init(aes: AES) -> Aes
Create a new AES driver from an AES peripheral without initialization.
This is a slightly safer version of steal
.
Safety
- You are responsible for resetting the AES peripheral and enabling the AES peripheral clock before use.
Example
use stm32wlxx_hal::{aes::Aes, pac};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
// safety: nothing is using the peripheral
unsafe { Aes::pulse_reset(&mut dp.RCC) };
Aes::enable_clock(&mut dp.RCC);
// safety: AES peripheral has been reset and clocks are enabled
let aes: Aes = unsafe { Aes::new_no_init(dp.AES) };
sourcepub unsafe fn steal() -> Aes
pub unsafe fn steal() -> Aes
Steal the AES peripheral from whatever is currently using it.
This will not initialize the AES peripheral (unlike new
).
Safety
- Ensure that the code stealing the AES peripheral has exclusive access. Singleton checks are bypassed with this method.
- You are responsible for resetting the AES peripheral and enabling the AES peripheral clock before use.
Example
use stm32wlxx_hal::{aes::Aes, pac};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
// AES cannot be used via registers now
let _: pac::AES = dp.AES;
// safety: nothing is using the peripheral
unsafe { Aes::pulse_reset(&mut dp.RCC) };
Aes::enable_clock(&mut dp.RCC);
// safety
// 1. We have exclusive access
// 2. peripheral has been setup
let aes: Aes = unsafe { Aes::steal() };
sourcepub unsafe fn unmask_irq()
Available on non-crate feature stm32wl5x_cm0p
and crate feature rt
only.
pub unsafe fn unmask_irq()
stm32wl5x_cm0p
and crate feature rt
only.sourcepub fn set_dataswap(&mut self, mode: SwapMode)
pub fn set_dataswap(&mut self, mode: SwapMode)
Set the way data is read from input and output registers according to section 23.4.13 (AES Data register and data swapping) of Reference Manual
sourcepub fn encrypt_ecb(
&mut self,
key: &[u32],
plaintext: &[u32; 4],
ciphertext: &mut [u32; 4]
) -> Result<(), Error>
pub fn encrypt_ecb( &mut self, key: &[u32], plaintext: &[u32; 4], ciphertext: &mut [u32; 4] ) -> Result<(), Error>
Encrypt using the electronic codebook chaining (ECB) algorithm.
Panics
- Key is not 128-bits long
[u32; 4]
or 256-bits long[u32; 8]
.
Example
use stm32wlxx_hal::{aes::Aes, pac};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut aes: Aes = Aes::new(dp.AES, &mut dp.RCC);
const KEY: [u32; 4] = [0; 4];
let plaintext: [u32; 4] = [0xf34481ec, 0x3cc627ba, 0xcd5dc3fb, 0x08f273e6];
let mut ciphertext: [u32; 4] = [0; 4];
aes.encrypt_ecb(&KEY, &plaintext, &mut ciphertext)?;
sourcepub fn encrypt_ecb_inplace(
&mut self,
key: &[u32],
plaintext: &mut [u32; 4]
) -> Result<(), Error>
pub fn encrypt_ecb_inplace( &mut self, key: &[u32], plaintext: &mut [u32; 4] ) -> Result<(), Error>
Encrypt using the electronic codebook chaining (ECB) algorithm in-place.
Panics
- Key is not 128-bits long
[u32; 4]
or 256-bits long[u32; 8]
.
Example
use stm32wlxx_hal::{aes::Aes, pac};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut aes: Aes = Aes::new(dp.AES, &mut dp.RCC);
const KEY: [u32; 4] = [0; 4];
let mut text: [u32; 4] = [0xf34481ec, 0x3cc627ba, 0xcd5dc3fb, 0x08f273e6];
aes.encrypt_ecb_inplace(&KEY, &mut text)?;
sourcepub fn encrypt_gcm_inplace(
&mut self,
key: &[u32],
iv: &[u32; 3],
aad: &[u8],
plaintext: &mut [u8],
tag: &mut [u32; 4]
) -> Result<(), Error>
pub fn encrypt_gcm_inplace( &mut self, key: &[u32], iv: &[u32; 3], aad: &[u8], plaintext: &mut [u8], tag: &mut [u32; 4] ) -> Result<(), Error>
Encrypt using the Galois counter mode (GCM) algorithm in-place.
Panics
- Key is not 128-bits long
[u32; 4]
or 256-bits long[u32; 8]
.
Example
use stm32wlxx_hal::{
aes::Aes,
pac,
rng::{self, Rng},
};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut aes: Aes = Aes::new(dp.AES, &mut dp.RCC);
let mut rng = Rng::new(dp.RNG, rng::Clk::Msi, &mut dp.RCC);
const KEY: [u32; 4] = [0; 4];
let mut iv: [u32; 3] = [0; 3];
rng.try_fill_u32(&mut iv)
.expect("failed to generate entropy");
let mut associated_data: [u8; 0] = [];
let mut plaintext: [u8; 13] = b"Hello, World!".clone();
let mut tag: [u32; 4] = [0; 4];
aes.encrypt_gcm_inplace(&KEY, &iv, &associated_data, &mut plaintext, &mut tag)?;
sourcepub fn encrypt_gcm_inplace_u32(
&mut self,
key: &[u32],
iv: &[u32; 3],
aad: &[u32],
plaintext: &mut [u32],
tag: &mut [u32; 4]
) -> Result<(), Error>
pub fn encrypt_gcm_inplace_u32( &mut self, key: &[u32], iv: &[u32; 3], aad: &[u32], plaintext: &mut [u32], tag: &mut [u32; 4] ) -> Result<(), Error>
Encrypt using the Galois counter mode (GCM) algorithm in-place.
u32
is the native AES peripheral data size.
This method skips byte packing / unpacking that occurs in
encrypt_gcm_inplace
.
Panics
- Key is not 128-bits long
[u32; 4]
or 256-bits long[u32; 8]
.
Example
use stm32wlxx_hal::{
aes::Aes,
pac,
rng::{self, Rng},
};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut aes: Aes = Aes::new(dp.AES, &mut dp.RCC);
let mut rng = Rng::new(dp.RNG, rng::Clk::Msi, &mut dp.RCC);
const KEY: [u32; 4] = [0; 4];
let mut iv: [u32; 3] = [0; 3];
rng.try_fill_u32(&mut iv)
.expect("failed to generate entropy");
let mut associated_data: [u32; 0] = [];
let mut plaintext: [u32; 1] = [0x12345678];
let mut tag: [u32; 4] = [0; 4];
aes.encrypt_gcm_inplace_u32(&KEY, &iv, &associated_data, &mut plaintext, &mut tag)?;
sourcepub fn decrypt_ecb(
&mut self,
key: &[u32],
ciphertext: &[u32; 4],
plaintext: &mut [u32; 4]
) -> Result<(), Error>
pub fn decrypt_ecb( &mut self, key: &[u32], ciphertext: &[u32; 4], plaintext: &mut [u32; 4] ) -> Result<(), Error>
Decrypt using the electronic codebook chaining (ECB) algorithm.
Panics
- Key is not 128-bits long
[u32; 4]
or 256-bits long[u32; 8]
.
Example
use stm32wlxx_hal::{aes::Aes, pac};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut aes: Aes = Aes::new(dp.AES, &mut dp.RCC);
const KEY: [u32; 4] = [0; 4];
let ciphertext: [u32; 4] = [0x0336763e, 0x966d9259, 0x5a567cc9, 0xce537f5e];
let mut plaintext: [u32; 4] = [0; 4];
aes.decrypt_ecb(&KEY, &ciphertext, &mut plaintext)?;
sourcepub fn decrypt_ecb_inplace(
&mut self,
key: &[u32],
ciphertext: &mut [u32; 4]
) -> Result<(), Error>
pub fn decrypt_ecb_inplace( &mut self, key: &[u32], ciphertext: &mut [u32; 4] ) -> Result<(), Error>
Decrypt using the electronic codebook chaining (ECB) algorithm in-place.
Panics
- Key is not 128-bits long
[u32; 4]
or 256-bits long[u32; 8]
.
Example
use stm32wlxx_hal::{aes::Aes, pac};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut aes: Aes = Aes::new(dp.AES, &mut dp.RCC);
const KEY: [u32; 4] = [0; 4];
let mut text: [u32; 4] = [0x0336763e, 0x966d9259, 0x5a567cc9, 0xce537f5e];
aes.decrypt_ecb_inplace(&KEY, &mut text)?;
sourcepub fn decrypt_gcm_inplace(
&mut self,
key: &[u32],
iv: &[u32; 3],
aad: &[u8],
ciphertext: &mut [u8],
tag: &mut [u32; 4]
) -> Result<(), Error>
pub fn decrypt_gcm_inplace( &mut self, key: &[u32], iv: &[u32; 3], aad: &[u8], ciphertext: &mut [u8], tag: &mut [u32; 4] ) -> Result<(), Error>
Decrypt using the Galois counter mode (GCM) algorithm in-place.
The resulting tag should be compared to the tag sent from the peer to verify the authenticity of the message.
Panics
- Key is not 128-bits long
[u32; 4]
or 256-bits long[u32; 8]
.
Example
use stm32wlxx_hal::{aes::Aes, pac};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut aes: Aes = Aes::new(dp.AES, &mut dp.RCC);
const KEY: [u32; 4] = [0; 4];
const IV: [u32; 3] = [0; 3];
let mut associated_data: [u8; 0] = [];
let mut ciphertext: [u8; 5] = [0xf3, 0x44, 0x81, 0xec, 0x3c];
let mut tag: [u32; 4] = [0; 4];
aes.decrypt_gcm_inplace(&KEY, &IV, &associated_data, &mut ciphertext, &mut tag)?;
sourcepub fn decrypt_gcm_inplace_u32(
&mut self,
key: &[u32],
iv: &[u32; 3],
aad: &[u32],
ciphertext: &mut [u32],
tag: &mut [u32; 4]
) -> Result<(), Error>
pub fn decrypt_gcm_inplace_u32( &mut self, key: &[u32], iv: &[u32; 3], aad: &[u32], ciphertext: &mut [u32], tag: &mut [u32; 4] ) -> Result<(), Error>
Decrypt using the Galois counter mode (GCM) algorithm in-place.
u32
is the native AES peripheral data size.
This method skips byte packing / unpacking that occurs in
decrypt_gcm_inplace
.
The resulting tag should be compared to the tag sent from the peer to verify the authenticity of the message.
Panics
- Key is not 128-bits long
[u32; 4]
or 256-bits long[u32; 8]
.
Example
use stm32wlxx_hal::{aes::Aes, pac};
let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut aes: Aes = Aes::new(dp.AES, &mut dp.RCC);
const KEY: [u32; 4] = [0; 4];
const IV: [u32; 3] = [0; 3];
let mut associated_data: [u32; 0] = [];
let mut ciphertext: [u32; 1] = [0xf34481ec];
let mut tag: [u32; 4] = [0; 4];
aes.decrypt_gcm_inplace_u32(&KEY, &IV, &associated_data, &mut ciphertext, &mut tag)?;