Struct stm32wlxx_hal::aes::Aes

source ·
pub struct Aes { /* private fields */ }
Expand description

AES driver.

Implementations§

source§

impl Aes

source

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);
source

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();
source

pub unsafe fn pulse_reset(rcc: &mut RCC)

Reset the AES peripheral.

new will pulse reset for you.

Safety
  1. Ensure nothing is using the AES peripheral before calling this function.
Example

See steal.

source

pub unsafe fn disable_clock(rcc: &mut RCC)

Disable the AES peripheral clock.

Safety
  1. Ensure nothing is using the AES peripheral before disabling the clock.
  2. 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
source

pub fn enable_clock(rcc: &mut RCC)

Enable the AES peripheral clock.

new will enable clocks for you.

Example

See steal.

source

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
  1. 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) };
source

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
  1. Ensure that the code stealing the AES peripheral has exclusive access. Singleton checks are bypassed with this method.
  2. 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() };
source

pub unsafe fn unmask_irq()

Available on non-crate feature stm32wl5x_cm0p and crate feature rt only.

Unmask the AES IRQ in the NVIC.

Safety

This can break mask-based critical sections.

Example
unsafe { stm32wlxx_hal::aes::Aes::unmask_irq() };
source

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

source

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)?;
source

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)?;
source

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)?;
source

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)?;
source

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)?;
source

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)?;
source

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)?;
source

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)?;

Trait Implementations§

source§

impl Debug for Aes

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<Aes> for AesWrapClk

source§

fn from(aes: Aes) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl RefUnwindSafe for Aes

§

impl Send for Aes

§

impl !Sync for Aes

§

impl Unpin for Aes

§

impl UnwindSafe for Aes

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.