1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
//! Advanced encryption standard

use crate::pac;
pub use pac::aes::cr::DATATYPE_A as SwapMode;
use pac::aes::cr::KEYSIZE_A as KeySize;

/// Algorithm modes.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[allow(dead_code)]
enum Algorithm {
    /// Electronic codebook chaining algorithm
    Ecb,
    /// Cipher block chaining algorithm
    Cbc,
    /// Counter mode chaining algorithm
    Ctr,
    /// Galois counter mode - Galois message authentication code
    Gcm,
    /// Counter with Cipher Mode
    Ccm,
}

impl Algorithm {
    /// Bit 16
    pub(crate) const fn chmod2(&self) -> bool {
        matches!(self, Algorithm::Ccm)
    }

    /// Bits 6:5
    pub(crate) const fn chmod10(&self) -> u8 {
        match self {
            Algorithm::Ecb => 0b00,
            Algorithm::Cbc => 0b01,
            Algorithm::Ctr => 0b10,
            Algorithm::Gcm => 0b11,
            Algorithm::Ccm => 0b00,
        }
    }
}

#[repr(u8)]
#[allow(dead_code)]
enum Mode {
    Encryption = 0b00,
    KeyDerivation = 0b01,
    Decryption = 0b10,
    /// ST does not document this!
    /// ST uses this in their HAL implementation and it passes NIST tests...
    KeyDerivationDecryption = 0b11,
}

impl Mode {
    pub const fn bits(self) -> u8 {
        self as u8
    }
}

impl From<Mode> for u8 {
    fn from(m: Mode) -> Self {
        m as u8
    }
}

/// Wrapper around [`Aes`] for safely disabling the peripheral clock.
#[derive(Debug)]
pub struct AesWrapClk {
    aes: Aes,
}

impl AesWrapClk {
    /// Run a function that accepts the AES driver as an argument,
    /// enabling the peripheral clock with [`enable_clock`] before calling the
    /// function, and disabling the peripheral clock with [`disable_clock`]
    /// after calling the function.
    ///
    /// This is useful for saving power in applications that need to access the
    /// AES peripheral infrequently.
    ///
    /// # Power Savings
    ///
    /// From DS13105 Rev 9:
    ///
    /// * Range 1: 2.50 μA/MHz
    /// * Range 2: 2.13 μA/MHz
    /// * LPRun and LPSleep: 1.80 μA/MHz
    ///
    /// # Example
    ///
    /// ```no_run
    /// use stm32wlxx_hal::{
    ///     aes::{Aes, AesWrapClk},
    ///     pac,
    /// };
    ///
    /// let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
    ///
    /// // safety:
    /// // * no need to reset because the AES memory-mapped IO has not been accessed since power-on
    /// // * the wrapper will handle enabling clocks
    /// let mut aeswrap: AesWrapClk = unsafe { Aes::new_no_init(dp.AES) }.into();
    ///
    /// const KEY: [u32; 4] = [0; 4];
    ///
    /// let mut text: [u32; 4] = [0xf34481ec, 0x3cc627ba, 0xcd5dc3fb, 0x08f273e6];
    /// aeswrap.with_clk(&mut dp.RCC, |aes| aes.encrypt_ecb_inplace(&KEY, &mut text))?;
    /// # Ok::<(), stm32wlxx_hal::aes::Error>(())
    /// ```
    ///
    /// [`enable_clock`]: Aes::enable_clock
    /// [`disable_clock`]: Aes::disable_clock
    #[inline]
    pub fn with_clk<F>(&mut self, rcc: &mut pac::RCC, f: F) -> Result<(), Error>
    where
        F: FnOnce(&mut Aes) -> Result<(), Error>,
    {
        Aes::enable_clock(rcc);
        let ret: Result<(), Error> = f(&mut self.aes);
        unsafe { Aes::disable_clock(rcc) };
        ret
    }
}

impl From<Aes> for AesWrapClk {
    #[inline]
    fn from(aes: Aes) -> Self {
        Self { aes }
    }
}

/// AES errors.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
    /// Unexpected read operation from the `AES_DOUTR` register
    /// during computation or data input phase.
    Read,
    /// Unexpected write operation to the `AES_DINR` register
    /// during computation or data output phase.
    Write,
}

/// AES driver.
#[derive(Debug)]
pub struct Aes {
    aes: pac::AES,
    swap_mode: SwapMode,
}

impl Aes {
    /// Create a new AES driver from an AES peripheral.
    ///
    /// This will enable clocks and reset the AES peripheral.
    ///
    /// # Example
    ///
    /// ```no_run
    /// 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);
    /// ```
    #[inline]
    pub fn new(aes: pac::AES, rcc: &mut pac::RCC) -> Aes {
        Self::enable_clock(rcc);
        unsafe { Self::pulse_reset(rcc) };

        Aes {
            aes,
            swap_mode: SwapMode::None,
        }
    }

    /// Free the AES peripheral from the driver.
    ///
    /// # Example
    ///
    /// ```no_run
    /// 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();
    /// ```
    #[inline]
    pub fn free(self) -> pac::AES {
        self.aes
    }

    /// Reset the AES peripheral.
    ///
    /// [`new`](Self::new) will pulse reset for you.
    ///
    /// # Safety
    ///
    /// 1. Ensure nothing is using the AES peripheral before calling this function.
    ///
    /// # Example
    ///
    /// See [`steal`](Self::steal).
    #[inline]
    pub unsafe fn pulse_reset(rcc: &mut pac::RCC) {
        rcc.ahb3rstr.modify(|_, w| w.aesrst().set_bit());
        rcc.ahb3rstr.modify(|_, w| w.aesrst().clear_bit());
    }

    /// 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
    ///
    /// ```no_run
    /// 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
    /// ```
    #[inline]
    pub unsafe fn disable_clock(rcc: &mut pac::RCC) {
        rcc.ahb3enr.modify(|_, w| w.aesen().disabled());
    }

    /// Enable the AES peripheral clock.
    ///
    /// [`new`](Self::new) will enable clocks for you.
    ///
    /// # Example
    ///
    /// See [`steal`](Self::steal).
    #[inline]
    pub fn enable_clock(rcc: &mut pac::RCC) {
        rcc.ahb3enr.modify(|_, w| w.aesen().enabled());
        rcc.ahb3enr.read(); // delay after an RCC peripheral clock enabling
    }

    /// Create a new AES driver from an AES peripheral without initialization.
    ///
    /// This is a slightly safer version of [`steal`](Self::steal).
    ///
    /// # Safety
    ///
    /// 1. You are responsible for resetting the AES peripheral and enabling
    ///    the AES peripheral clock before use.
    ///
    /// # Example
    ///
    /// ```no_run
    /// 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) };
    /// ```
    #[inline]
    pub const unsafe fn new_no_init(aes: pac::AES) -> Aes {
        Aes {
            aes,
            swap_mode: SwapMode::None,
        }
    }

    /// 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
    ///
    /// ```no_run
    /// 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() };
    /// ```
    ///
    /// [`new`]: Aes::new
    #[inline]
    pub unsafe fn steal() -> Aes {
        let dp: pac::Peripherals = pac::Peripherals::steal();
        Aes {
            aes: dp.AES,
            swap_mode: SwapMode::None,
        }
    }

    /// Unmask the AES IRQ in the NVIC.
    ///
    /// # Safety
    ///
    /// This can break mask-based critical sections.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # #[cfg(all(not(feature = "stm32wl5x_cm0p"), feature = "rt"))]
    /// unsafe { stm32wlxx_hal::aes::Aes::unmask_irq() };
    /// ```
    #[cfg(all(not(feature = "stm32wl5x_cm0p"), feature = "rt"))]
    #[inline]
    pub unsafe fn unmask_irq() {
        pac::NVIC::unmask(pac::Interrupt::AES)
    }

    fn set_key(&mut self, key: &[u32]) -> KeySize {
        match key.len() {
            4 => {
                self.aes.cr.write(|w| w.en().disabled().keysize().bits128());
                self.aes.keyr3.write(|w| w.key().bits(key[0]));
                self.aes.keyr2.write(|w| w.key().bits(key[1]));
                self.aes.keyr1.write(|w| w.key().bits(key[2]));
                self.aes.keyr0.write(|w| w.key().bits(key[3]));
                KeySize::Bits128
            }
            8 => {
                self.aes.cr.write(|w| w.en().disabled().keysize().bits256());
                self.aes.keyr7.write(|w| w.key().bits(key[0]));
                self.aes.keyr6.write(|w| w.key().bits(key[1]));
                self.aes.keyr5.write(|w| w.key().bits(key[2]));
                self.aes.keyr4.write(|w| w.key().bits(key[3]));
                self.aes.keyr3.write(|w| w.key().bits(key[4]));
                self.aes.keyr2.write(|w| w.key().bits(key[5]));
                self.aes.keyr1.write(|w| w.key().bits(key[6]));
                self.aes.keyr0.write(|w| w.key().bits(key[7]));
                KeySize::Bits256
            }
            _ => panic!("Key must be 128-bit or 256-bit not {}-bit", key.len() * 32),
        }
    }

    fn poll_completion(&self) -> Result<(), Error> {
        loop {
            let sr = self.aes.sr.read();
            if sr.wrerr().bit_is_set() {
                return Err(Error::Write);
            }
            if sr.rderr().bit_is_set() {
                return Err(Error::Read);
            }
            if sr.ccf().bit_is_set() {
                return Ok(());
            }
        }
    }

    fn set_din(&mut self, din: &[u32; 4]) {
        din.iter()
            .for_each(|dw| self.aes.dinr.write(|w| w.din().bits(*dw)))
    }

    fn dout(&mut self, buf: &mut [u32; 4]) {
        buf.iter_mut()
            .for_each(|dw| *dw = self.aes.doutr.read().bits());
    }

    fn set_din_slice(&mut self, din: &[u32]) {
        (0..4).for_each(|idx| {
            self.aes
                .dinr
                .write(|w| w.din().bits(*din.get(idx).unwrap_or(&0)))
        });
    }

    fn dout_slice(&mut self, buf: &mut [u32]) {
        (0..4).for_each(|idx| {
            let dout: u32 = self.aes.doutr.read().bits();
            if let Some(dw) = buf.get_mut(idx) {
                *dw = dout;
            }
        });
    }

    // expensive copy for the sake of allowing unaligned u8 data
    #[allow(clippy::get_first)]
    fn set_din_block(&mut self, block: &[u8]) {
        for chunk in block.chunks(4) {
            let din: u32 = (chunk.get(0).copied().unwrap_or(0) as u32) << 24
                | (chunk.get(1).copied().unwrap_or(0) as u32) << 16
                | (chunk.get(2).copied().unwrap_or(0) as u32) << 8
                | (chunk.get(3).copied().unwrap_or(0) as u32);

            self.aes.dinr.write(|w| w.din().bits(din));
        }

        let remain_dw: usize = 4 - ((block.len() + 3) / 4);
        for _ in 0..remain_dw {
            self.aes.dinr.write(|w| w.din().bits(0));
        }
    }

    // expensive copy for the sake of allowing unaligned u8 data
    fn dout_block(&mut self, block: &mut [u8]) {
        for chunk in block.chunks_mut(4) {
            let dout: u32 = self.aes.doutr.read().bits();
            if let Some(byte) = chunk.get_mut(0) {
                *byte = (dout >> 24) as u8
            }
            if let Some(byte) = chunk.get_mut(1) {
                *byte = (dout >> 16) as u8
            }
            if let Some(byte) = chunk.get_mut(2) {
                *byte = (dout >> 8) as u8
            }
            if let Some(byte) = chunk.get_mut(3) {
                *byte = dout as u8
            }
        }

        let remain_dw: usize = 4 - ((block.len() + 3) / 4);
        for _ in 0..remain_dw {
            let _: u32 = self.aes.doutr.read().bits();
        }
    }

    fn gcm_init_phase<const MODE: u8>(
        &mut self,
        key: &[u32],
        iv: &[u32; 3],
    ) -> Result<KeySize, Error> {
        const ALGO: Algorithm = Algorithm::Gcm;
        const CHMOD2: bool = ALGO.chmod2();
        const CHMOD10: u8 = ALGO.chmod10();

        let keysize: KeySize = self.set_key(key);

        self.aes.ivr0.write(|w| w.ivi().bits(2));
        self.aes.ivr1.write(|w| w.ivi().bits(iv[2]));
        self.aes.ivr2.write(|w| w.ivi().bits(iv[1]));
        self.aes.ivr3.write(|w| w.ivi().bits(iv[0]));
        self.aes.cr.write(|w| {
            w.en().enabled();
            w.datatype().variant(self.swap_mode);
            w.mode().bits(MODE);
            w.chmod2().bit(CHMOD2);
            w.chmod().bits(CHMOD10);
            w.ccfc().clear();
            w.errc().clear();
            w.ccfie().disabled();
            w.errie().disabled();
            w.dmainen().disabled();
            w.dmaouten().disabled();
            w.gcmph().init();
            w.keysize().variant(keysize);
            w.npblb().bits(0)
        });
        self.poll_completion()?;
        Ok(keysize)
    }

    fn gcm_final_phase<const MODE: u8>(
        &mut self,
        keysize: KeySize,
        aad_len: usize,
        buf_len: usize,
        tag: &mut [u32; 4],
    ) -> Result<(), Error> {
        const ALGO: Algorithm = Algorithm::Gcm;
        const CHMOD2: bool = ALGO.chmod2();
        const CHMOD10: u8 = ALGO.chmod10();

        self.aes.cr.write(|w| {
            w.en().enabled();
            w.datatype().variant(self.swap_mode);
            w.mode().bits(MODE);
            w.chmod2().bit(CHMOD2);
            w.chmod().bits(CHMOD10);
            w.ccfc().clear();
            w.errc().clear();
            w.ccfie().disabled();
            w.errie().disabled();
            w.dmainen().disabled();
            w.dmaouten().disabled();
            w.gcmph().final_();
            w.keysize().variant(keysize);
            w.npblb().bits(0)
        });

        // byte length to bit lengths
        // impossible to overflow, not enough RAM for [u8; (u32::MAX >> 3) + 1]
        let aad_len: u32 = (aad_len as u32) << 3;
        let buf_len: u32 = (buf_len as u32) << 3;

        self.aes.dinr.write(|w| w.din().bits(0));
        self.aes.dinr.write(|w| w.din().bits(aad_len));
        self.aes.dinr.write(|w| w.din().bits(0));
        self.aes.dinr.write(|w| w.din().bits(buf_len));

        self.poll_completion()?;
        self.dout(tag);
        Ok(())
    }

    fn gcm_inplace<const MODE: u8>(
        &mut self,
        key: &[u32],
        iv: &[u32; 3],
        aad: &[u8],
        buf: &mut [u8],
        tag: &mut [u32; 4],
    ) -> Result<(), Error> {
        const ALGO: Algorithm = Algorithm::Gcm;
        const CHMOD2: bool = ALGO.chmod2();
        const CHMOD10: u8 = ALGO.chmod10();

        // init phase
        let keysize: KeySize = self.gcm_init_phase::<MODE>(key, iv)?;

        // header phase
        for block in aad.chunks(16) {
            self.aes.cr.write(|w| {
                w.en().enabled();
                w.datatype().variant(self.swap_mode);
                w.mode().bits(MODE);
                w.chmod2().bit(CHMOD2);
                w.chmod().bits(CHMOD10);
                w.ccfc().clear();
                w.errc().clear();
                w.ccfie().disabled();
                w.errie().disabled();
                w.dmainen().disabled();
                w.dmaouten().disabled();
                w.gcmph().header();
                w.keysize().variant(keysize);
                w.npblb().bits(0) // not used in header phase
            });
            self.set_din_block(block);
            self.poll_completion()?;
        }

        // payload phase
        for block in buf.chunks_mut(16) {
            self.aes.cr.write(|w| {
                w.en().enabled();
                w.datatype().variant(self.swap_mode);
                w.mode().bits(MODE);
                w.chmod2().bit(CHMOD2);
                w.chmod().bits(CHMOD10);
                w.ccfc().clear();
                w.errc().clear();
                w.ccfie().disabled();
                w.errie().disabled();
                w.dmainen().disabled();
                w.dmaouten().disabled();
                w.gcmph().payload();
                w.keysize().variant(keysize);
                w.npblb().bits(16 - (block.len() as u8))
            });
            self.set_din_block(block);
            self.poll_completion()?;
            self.dout_block(block);
        }

        self.gcm_final_phase::<MODE>(keysize, aad.len(), buf.len(), tag)
    }

    fn gcm_inplace_u32<const MODE: u8>(
        &mut self,
        key: &[u32],
        iv: &[u32; 3],
        aad: &[u32],
        buf: &mut [u32],
        tag: &mut [u32; 4],
    ) -> Result<(), Error> {
        const ALGO: Algorithm = Algorithm::Gcm;
        const CHMOD2: bool = ALGO.chmod2();
        const CHMOD10: u8 = ALGO.chmod10();

        // init phase
        let keysize: KeySize = self.gcm_init_phase::<MODE>(key, iv)?;

        // header phase
        for block in aad.chunks(4) {
            self.aes.cr.write(|w| {
                w.en().enabled();
                w.datatype().variant(self.swap_mode);
                w.mode().bits(MODE);
                w.chmod2().bit(CHMOD2);
                w.chmod().bits(CHMOD10);
                w.ccfc().clear();
                w.errc().clear();
                w.ccfie().disabled();
                w.errie().disabled();
                w.dmainen().disabled();
                w.dmaouten().disabled();
                w.gcmph().header();
                w.keysize().variant(keysize);
                w.npblb().bits(0) // not used in header phase
            });
            self.set_din_slice(block);
            self.poll_completion()?;
        }

        // payload phase
        for block in buf.chunks_mut(4) {
            self.aes.cr.write(|w| {
                w.en().enabled();
                w.datatype().variant(self.swap_mode);
                w.mode().bits(MODE);
                w.chmod2().bit(CHMOD2);
                w.chmod().bits(CHMOD10);
                w.ccfc().clear();
                w.errc().clear();
                w.ccfie().disabled();
                w.errie().disabled();
                w.dmainen().disabled();
                w.dmaouten().disabled();
                w.gcmph().payload();
                w.keysize().variant(keysize);
                w.npblb().bits(16 - (core::mem::size_of_val(block) as u8))
            });
            self.set_din_slice(block);
            self.poll_completion()?;
            self.dout_slice(block);
        }

        // final phase
        self.gcm_final_phase::<MODE>(
            keysize,
            core::mem::size_of_val(aad),
            core::mem::size_of_val(buf),
            tag,
        )
    }

    /// 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
    pub fn set_dataswap(&mut self, mode: SwapMode) {
        self.swap_mode = mode;
    }

    /// Encrypt using the electronic codebook chaining (ECB) algorithm.
    ///
    /// # Panics
    ///
    /// * Key is not 128-bits long `[u32; 4]` or 256-bits long `[u32; 8]`.
    ///
    /// # Example
    ///
    /// ```no_run
    /// 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)?;
    /// # Ok::<(), stm32wlxx_hal::aes::Error>(())
    /// ```
    pub fn encrypt_ecb(
        &mut self,
        key: &[u32],
        plaintext: &[u32; 4],
        ciphertext: &mut [u32; 4],
    ) -> Result<(), Error> {
        const ALGO: Algorithm = Algorithm::Ecb;
        const CHMOD2: bool = ALGO.chmod2();
        const CHMOD10: u8 = ALGO.chmod10();
        const MODE: u8 = Mode::Encryption.bits();

        let keysize: KeySize = self.set_key(key);

        self.aes.cr.write(|w| {
            w.en().enabled();
            w.datatype().variant(self.swap_mode);
            w.mode().bits(MODE);
            w.chmod2().bit(CHMOD2);
            w.chmod().bits(CHMOD10);
            w.ccfc().clear();
            w.errc().clear();
            w.ccfie().disabled();
            w.errie().disabled();
            w.dmainen().disabled();
            w.dmaouten().disabled();
            w.gcmph().bits(0); // do not care for ECB
            w.keysize().variant(keysize);
            w.npblb().bits(0) // no padding
        });

        self.set_din(plaintext);
        self.poll_completion()?;
        self.dout(ciphertext);
        Ok(())
    }

    /// 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
    ///
    /// ```no_run
    /// 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)?;
    /// # Ok::<(), stm32wlxx_hal::aes::Error>(())
    /// ```
    pub fn encrypt_ecb_inplace(
        &mut self,
        key: &[u32],
        plaintext: &mut [u32; 4],
    ) -> Result<(), Error> {
        const ALGO: Algorithm = Algorithm::Ecb;
        const CHMOD2: bool = ALGO.chmod2();
        const CHMOD10: u8 = ALGO.chmod10();
        const MODE: u8 = Mode::Encryption.bits();

        let keysize: KeySize = self.set_key(key);

        self.aes.cr.write(|w| {
            w.en().enabled();
            w.datatype().variant(self.swap_mode);
            w.mode().bits(MODE);
            w.chmod2().bit(CHMOD2);
            w.chmod().bits(CHMOD10);
            w.ccfc().clear();
            w.errc().clear();
            w.ccfie().disabled();
            w.errie().disabled();
            w.dmainen().disabled();
            w.dmaouten().disabled();
            w.gcmph().bits(0); // do not care for ECB
            w.keysize().variant(keysize);
            w.npblb().bits(0) // no padding
        });

        self.set_din(plaintext);
        self.poll_completion()?;
        self.dout(plaintext);
        Ok(())
    }

    /// 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
    ///
    /// ```no_run
    /// 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)?;
    /// # Ok::<(), stm32wlxx_hal::aes::Error>(())
    /// ```
    pub fn encrypt_gcm_inplace(
        &mut self,
        key: &[u32],
        iv: &[u32; 3],
        aad: &[u8],
        plaintext: &mut [u8],
        tag: &mut [u32; 4],
    ) -> Result<(), Error> {
        const MODE: u8 = Mode::Encryption.bits();
        self.gcm_inplace::<MODE>(key, iv, aad, plaintext, tag)
    }

    /// 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`](Self::encrypt_gcm_inplace).
    ///
    /// # Panics
    ///
    /// * Key is not 128-bits long `[u32; 4]` or 256-bits long `[u32; 8]`.
    ///
    /// # Example
    ///
    /// ```no_run
    /// 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)?;
    /// # Ok::<(), stm32wlxx_hal::aes::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> {
        const MODE: u8 = Mode::Encryption.bits();
        self.gcm_inplace_u32::<MODE>(key, iv, aad, plaintext, tag)
    }

    /// Decrypt using the electronic codebook chaining (ECB) algorithm.
    ///
    /// # Panics
    ///
    /// * Key is not 128-bits long `[u32; 4]` or 256-bits long `[u32; 8]`.
    ///
    /// # Example
    ///
    /// ```no_run
    /// 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)?;
    /// # Ok::<(), stm32wlxx_hal::aes::Error>(())
    /// ```
    pub fn decrypt_ecb(
        &mut self,
        key: &[u32],
        ciphertext: &[u32; 4],
        plaintext: &mut [u32; 4],
    ) -> Result<(), Error> {
        const ALGO: Algorithm = Algorithm::Ecb;
        const CHMOD2: bool = ALGO.chmod2();
        const CHMOD10: u8 = ALGO.chmod10();
        const MODE: u8 = Mode::KeyDerivationDecryption.bits();

        let keysize: KeySize = self.set_key(key);

        self.aes.cr.write(|w| {
            w.en().enabled();
            w.datatype().variant(self.swap_mode);
            w.mode().bits(MODE);
            w.chmod2().bit(CHMOD2);
            w.chmod().bits(CHMOD10);
            w.ccfc().clear();
            w.errc().clear();
            w.ccfie().disabled();
            w.errie().disabled();
            w.dmainen().disabled();
            w.dmaouten().disabled();
            w.gcmph().bits(0); // do not care for ECB
            w.keysize().variant(keysize);
            w.npblb().bits(0) // no padding
        });

        self.set_din(ciphertext);
        self.poll_completion()?;
        self.dout(plaintext);
        Ok(())
    }

    /// 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
    ///
    /// ```no_run
    /// 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)?;
    /// # Ok::<(), stm32wlxx_hal::aes::Error>(())
    /// ```
    pub fn decrypt_ecb_inplace(
        &mut self,
        key: &[u32],
        ciphertext: &mut [u32; 4],
    ) -> Result<(), Error> {
        const ALGO: Algorithm = Algorithm::Ecb;
        const CHMOD2: bool = ALGO.chmod2();
        const CHMOD10: u8 = ALGO.chmod10();
        const MODE: u8 = Mode::KeyDerivationDecryption.bits();
        let keysize: KeySize = self.set_key(key);

        self.aes.cr.write(|w| {
            w.en().enabled();
            w.datatype().variant(self.swap_mode);
            w.mode().bits(MODE);
            w.chmod2().bit(CHMOD2);
            w.chmod().bits(CHMOD10);
            w.ccfc().clear();
            w.errc().clear();
            w.ccfie().disabled();
            w.errie().disabled();
            w.dmainen().disabled();
            w.dmaouten().disabled();
            w.gcmph().bits(0); // do not care for ECB
            w.keysize().variant(keysize);
            w.npblb().bits(0) // no padding
        });

        self.set_din(ciphertext);
        self.poll_completion()?;
        self.dout(ciphertext);
        Ok(())
    }

    /// 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
    ///
    /// ```no_run
    /// 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)?;
    /// # Ok::<(), stm32wlxx_hal::aes::Error>(())
    /// ```
    pub fn decrypt_gcm_inplace(
        &mut self,
        key: &[u32],
        iv: &[u32; 3],
        aad: &[u8],
        ciphertext: &mut [u8],
        tag: &mut [u32; 4],
    ) -> Result<(), Error> {
        const MODE: u8 = Mode::Decryption.bits();
        self.gcm_inplace::<MODE>(key, iv, aad, ciphertext, tag)
    }

    /// 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`](Self::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
    ///
    /// ```no_run
    /// 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)?;
    /// # Ok::<(), stm32wlxx_hal::aes::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> {
        const MODE: u8 = Mode::Decryption.bits();
        self.gcm_inplace_u32::<MODE>(key, iv, aad, ciphertext, tag)
    }
}