/
mod.rs
1917 lines (1674 loc) · 68.1 KB
/
mod.rs
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
//! Crypto Accelerator (CRYP)
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
use core::cmp::min;
use core::marker::PhantomData;
use core::ptr;
use embassy_hal_internal::{into_ref, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker;
use crate::dma::{NoDma, Transfer, TransferOptions};
use crate::interrupt::typelevel::Interrupt;
use crate::{interrupt, pac, peripherals, Peripheral};
const DES_BLOCK_SIZE: usize = 8; // 64 bits
const AES_BLOCK_SIZE: usize = 16; // 128 bits
static CRYP_WAKER: AtomicWaker = AtomicWaker::new();
/// CRYP interrupt handler.
pub struct InterruptHandler<T: Instance> {
_phantom: PhantomData<T>,
}
impl<T: Instance> interrupt::typelevel::Handler<T::Interrupt> for InterruptHandler<T> {
unsafe fn on_interrupt() {
let bits = T::regs().misr().read();
if bits.inmis() {
T::regs().imscr().modify(|w| w.set_inim(false));
CRYP_WAKER.wake();
}
if bits.outmis() {
T::regs().imscr().modify(|w| w.set_outim(false));
CRYP_WAKER.wake();
}
}
}
/// This trait encapsulates all cipher-specific behavior/
pub trait Cipher<'c> {
/// Processing block size. Determined by the processor and the algorithm.
const BLOCK_SIZE: usize;
/// Indicates whether the cipher requires the application to provide padding.
/// If `true`, no partial blocks will be accepted (a panic will occur).
const REQUIRES_PADDING: bool = false;
/// Returns the symmetric key.
fn key(&self) -> &[u8];
/// Returns the initialization vector.
fn iv(&self) -> &[u8];
/// Sets the processor algorithm mode according to the associated cipher.
fn set_algomode(&self, p: &pac::cryp::Cryp);
/// Performs any key preparation within the processor, if necessary.
fn prepare_key(&self, _p: &pac::cryp::Cryp) {}
/// Performs any cipher-specific initialization.
fn init_phase_blocking<T: Instance, DmaIn, DmaOut>(&self, _p: &pac::cryp::Cryp, _cryp: &Cryp<T, DmaIn, DmaOut>) {}
/// Performs any cipher-specific initialization.
async fn init_phase<T: Instance, DmaIn, DmaOut>(&self, _p: &pac::cryp::Cryp, _cryp: &mut Cryp<'_, T, DmaIn, DmaOut>)
where
DmaIn: crate::cryp::DmaIn<T>,
DmaOut: crate::cryp::DmaOut<T>,
{
}
/// Called prior to processing the last data block for cipher-specific operations.
fn pre_final(&self, _p: &pac::cryp::Cryp, _dir: Direction, _padding_len: usize) -> [u32; 4] {
return [0; 4];
}
/// Called after processing the last data block for cipher-specific operations.
fn post_final_blocking<T: Instance, DmaIn, DmaOut>(
&self,
_p: &pac::cryp::Cryp,
_cryp: &Cryp<T, DmaIn, DmaOut>,
_dir: Direction,
_int_data: &mut [u8; AES_BLOCK_SIZE],
_temp1: [u32; 4],
_padding_mask: [u8; 16],
) {
}
/// Called after processing the last data block for cipher-specific operations.
async fn post_final<T: Instance, DmaIn, DmaOut>(
&self,
_p: &pac::cryp::Cryp,
_cryp: &mut Cryp<'_, T, DmaIn, DmaOut>,
_dir: Direction,
_int_data: &mut [u8; AES_BLOCK_SIZE],
_temp1: [u32; 4],
_padding_mask: [u8; 16],
) where
DmaIn: crate::cryp::DmaIn<T>,
DmaOut: crate::cryp::DmaOut<T>,
{
}
/// Returns the AAD header block as required by the cipher.
fn get_header_block(&self) -> &[u8] {
return [0; 0].as_slice();
}
}
/// This trait enables restriction of ciphers to specific key sizes.
pub trait CipherSized {}
/// This trait enables restriction of initialization vectors to sizes compatibile with a cipher mode.
pub trait IVSized {}
/// This trait enables restriction of a header phase to authenticated ciphers only.
pub trait CipherAuthenticated<const TAG_SIZE: usize> {
/// Defines the authentication tag size.
const TAG_SIZE: usize = TAG_SIZE;
}
/// TDES-ECB Cipher Mode
pub struct TdesEcb<'c, const KEY_SIZE: usize> {
iv: &'c [u8; 0],
key: &'c [u8; KEY_SIZE],
}
impl<'c, const KEY_SIZE: usize> TdesEcb<'c, KEY_SIZE> {
/// Constructs a new AES-ECB cipher for a cryptographic operation.
pub fn new(key: &'c [u8; KEY_SIZE]) -> Self {
return Self { key: key, iv: &[0; 0] };
}
}
impl<'c, const KEY_SIZE: usize> Cipher<'c> for TdesEcb<'c, KEY_SIZE> {
const BLOCK_SIZE: usize = DES_BLOCK_SIZE;
const REQUIRES_PADDING: bool = true;
fn key(&self) -> &'c [u8] {
self.key
}
fn iv(&self) -> &'c [u8] {
self.iv
}
fn set_algomode(&self, p: &pac::cryp::Cryp) {
#[cfg(cryp_v1)]
{
p.cr().modify(|w| w.set_algomode(0));
}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
{
p.cr().modify(|w| w.set_algomode0(0));
p.cr().modify(|w| w.set_algomode3(false));
}
}
}
impl<'c> CipherSized for TdesEcb<'c, { 112 / 8 }> {}
impl<'c> CipherSized for TdesEcb<'c, { 168 / 8 }> {}
impl<'c, const KEY_SIZE: usize> IVSized for TdesEcb<'c, KEY_SIZE> {}
/// TDES-CBC Cipher Mode
pub struct TdesCbc<'c, const KEY_SIZE: usize> {
iv: &'c [u8; 8],
key: &'c [u8; KEY_SIZE],
}
impl<'c, const KEY_SIZE: usize> TdesCbc<'c, KEY_SIZE> {
/// Constructs a new TDES-CBC cipher for a cryptographic operation.
pub fn new(key: &'c [u8; KEY_SIZE], iv: &'c [u8; 8]) -> Self {
return Self { key: key, iv: iv };
}
}
impl<'c, const KEY_SIZE: usize> Cipher<'c> for TdesCbc<'c, KEY_SIZE> {
const BLOCK_SIZE: usize = DES_BLOCK_SIZE;
const REQUIRES_PADDING: bool = true;
fn key(&self) -> &'c [u8] {
self.key
}
fn iv(&self) -> &'c [u8] {
self.iv
}
fn set_algomode(&self, p: &pac::cryp::Cryp) {
#[cfg(cryp_v1)]
{
p.cr().modify(|w| w.set_algomode(1));
}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
{
p.cr().modify(|w| w.set_algomode0(1));
p.cr().modify(|w| w.set_algomode3(false));
}
}
}
impl<'c> CipherSized for TdesCbc<'c, { 112 / 8 }> {}
impl<'c> CipherSized for TdesCbc<'c, { 168 / 8 }> {}
impl<'c, const KEY_SIZE: usize> IVSized for TdesCbc<'c, KEY_SIZE> {}
/// DES-ECB Cipher Mode
pub struct DesEcb<'c, const KEY_SIZE: usize> {
iv: &'c [u8; 0],
key: &'c [u8; KEY_SIZE],
}
impl<'c, const KEY_SIZE: usize> DesEcb<'c, KEY_SIZE> {
/// Constructs a new AES-ECB cipher for a cryptographic operation.
pub fn new(key: &'c [u8; KEY_SIZE]) -> Self {
return Self { key: key, iv: &[0; 0] };
}
}
impl<'c, const KEY_SIZE: usize> Cipher<'c> for DesEcb<'c, KEY_SIZE> {
const BLOCK_SIZE: usize = DES_BLOCK_SIZE;
const REQUIRES_PADDING: bool = true;
fn key(&self) -> &'c [u8] {
self.key
}
fn iv(&self) -> &'c [u8] {
self.iv
}
fn set_algomode(&self, p: &pac::cryp::Cryp) {
#[cfg(cryp_v1)]
{
p.cr().modify(|w| w.set_algomode(2));
}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
{
p.cr().modify(|w| w.set_algomode0(2));
p.cr().modify(|w| w.set_algomode3(false));
}
}
}
impl<'c> CipherSized for DesEcb<'c, { 56 / 8 }> {}
impl<'c, const KEY_SIZE: usize> IVSized for DesEcb<'c, KEY_SIZE> {}
/// DES-CBC Cipher Mode
pub struct DesCbc<'c, const KEY_SIZE: usize> {
iv: &'c [u8; 8],
key: &'c [u8; KEY_SIZE],
}
impl<'c, const KEY_SIZE: usize> DesCbc<'c, KEY_SIZE> {
/// Constructs a new AES-CBC cipher for a cryptographic operation.
pub fn new(key: &'c [u8; KEY_SIZE], iv: &'c [u8; 8]) -> Self {
return Self { key: key, iv: iv };
}
}
impl<'c, const KEY_SIZE: usize> Cipher<'c> for DesCbc<'c, KEY_SIZE> {
const BLOCK_SIZE: usize = DES_BLOCK_SIZE;
const REQUIRES_PADDING: bool = true;
fn key(&self) -> &'c [u8] {
self.key
}
fn iv(&self) -> &'c [u8] {
self.iv
}
fn set_algomode(&self, p: &pac::cryp::Cryp) {
#[cfg(cryp_v1)]
{
p.cr().modify(|w| w.set_algomode(3));
}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
{
p.cr().modify(|w| w.set_algomode0(3));
p.cr().modify(|w| w.set_algomode3(false));
}
}
}
impl<'c> CipherSized for DesCbc<'c, { 56 / 8 }> {}
impl<'c, const KEY_SIZE: usize> IVSized for DesCbc<'c, KEY_SIZE> {}
/// AES-ECB Cipher Mode
pub struct AesEcb<'c, const KEY_SIZE: usize> {
iv: &'c [u8; 0],
key: &'c [u8; KEY_SIZE],
}
impl<'c, const KEY_SIZE: usize> AesEcb<'c, KEY_SIZE> {
/// Constructs a new AES-ECB cipher for a cryptographic operation.
pub fn new(key: &'c [u8; KEY_SIZE]) -> Self {
return Self { key: key, iv: &[0; 0] };
}
}
impl<'c, const KEY_SIZE: usize> Cipher<'c> for AesEcb<'c, KEY_SIZE> {
const BLOCK_SIZE: usize = AES_BLOCK_SIZE;
const REQUIRES_PADDING: bool = true;
fn key(&self) -> &'c [u8] {
self.key
}
fn iv(&self) -> &'c [u8] {
self.iv
}
fn prepare_key(&self, p: &pac::cryp::Cryp) {
#[cfg(cryp_v1)]
{
p.cr().modify(|w| w.set_algomode(7));
}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
{
p.cr().modify(|w| w.set_algomode0(7));
p.cr().modify(|w| w.set_algomode3(false));
}
p.cr().modify(|w| w.set_crypen(true));
while p.sr().read().busy() {}
}
fn set_algomode(&self, p: &pac::cryp::Cryp) {
#[cfg(cryp_v1)]
{
p.cr().modify(|w| w.set_algomode(2));
}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
{
p.cr().modify(|w| w.set_algomode0(2));
p.cr().modify(|w| w.set_algomode3(false));
}
}
}
impl<'c> CipherSized for AesEcb<'c, { 128 / 8 }> {}
impl<'c> CipherSized for AesEcb<'c, { 192 / 8 }> {}
impl<'c> CipherSized for AesEcb<'c, { 256 / 8 }> {}
impl<'c, const KEY_SIZE: usize> IVSized for AesEcb<'c, KEY_SIZE> {}
/// AES-CBC Cipher Mode
pub struct AesCbc<'c, const KEY_SIZE: usize> {
iv: &'c [u8; 16],
key: &'c [u8; KEY_SIZE],
}
impl<'c, const KEY_SIZE: usize> AesCbc<'c, KEY_SIZE> {
/// Constructs a new AES-CBC cipher for a cryptographic operation.
pub fn new(key: &'c [u8; KEY_SIZE], iv: &'c [u8; 16]) -> Self {
return Self { key: key, iv: iv };
}
}
impl<'c, const KEY_SIZE: usize> Cipher<'c> for AesCbc<'c, KEY_SIZE> {
const BLOCK_SIZE: usize = AES_BLOCK_SIZE;
const REQUIRES_PADDING: bool = true;
fn key(&self) -> &'c [u8] {
self.key
}
fn iv(&self) -> &'c [u8] {
self.iv
}
fn prepare_key(&self, p: &pac::cryp::Cryp) {
#[cfg(cryp_v1)]
{
p.cr().modify(|w| w.set_algomode(7));
}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
{
p.cr().modify(|w| w.set_algomode0(7));
p.cr().modify(|w| w.set_algomode3(false));
}
p.cr().modify(|w| w.set_crypen(true));
while p.sr().read().busy() {}
}
fn set_algomode(&self, p: &pac::cryp::Cryp) {
#[cfg(cryp_v1)]
{
p.cr().modify(|w| w.set_algomode(5));
}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
{
p.cr().modify(|w| w.set_algomode0(5));
p.cr().modify(|w| w.set_algomode3(false));
}
}
}
impl<'c> CipherSized for AesCbc<'c, { 128 / 8 }> {}
impl<'c> CipherSized for AesCbc<'c, { 192 / 8 }> {}
impl<'c> CipherSized for AesCbc<'c, { 256 / 8 }> {}
impl<'c, const KEY_SIZE: usize> IVSized for AesCbc<'c, KEY_SIZE> {}
/// AES-CTR Cipher Mode
pub struct AesCtr<'c, const KEY_SIZE: usize> {
iv: &'c [u8; 16],
key: &'c [u8; KEY_SIZE],
}
impl<'c, const KEY_SIZE: usize> AesCtr<'c, KEY_SIZE> {
/// Constructs a new AES-CTR cipher for a cryptographic operation.
pub fn new(key: &'c [u8; KEY_SIZE], iv: &'c [u8; 16]) -> Self {
return Self { key: key, iv: iv };
}
}
impl<'c, const KEY_SIZE: usize> Cipher<'c> for AesCtr<'c, KEY_SIZE> {
const BLOCK_SIZE: usize = AES_BLOCK_SIZE;
fn key(&self) -> &'c [u8] {
self.key
}
fn iv(&self) -> &'c [u8] {
self.iv
}
fn set_algomode(&self, p: &pac::cryp::Cryp) {
#[cfg(cryp_v1)]
{
p.cr().modify(|w| w.set_algomode(6));
}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
{
p.cr().modify(|w| w.set_algomode0(6));
p.cr().modify(|w| w.set_algomode3(false));
}
}
}
impl<'c> CipherSized for AesCtr<'c, { 128 / 8 }> {}
impl<'c> CipherSized for AesCtr<'c, { 192 / 8 }> {}
impl<'c> CipherSized for AesCtr<'c, { 256 / 8 }> {}
impl<'c, const KEY_SIZE: usize> IVSized for AesCtr<'c, KEY_SIZE> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
///AES-GCM Cipher Mode
pub struct AesGcm<'c, const KEY_SIZE: usize> {
iv: [u8; 16],
key: &'c [u8; KEY_SIZE],
}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize> AesGcm<'c, KEY_SIZE> {
/// Constucts a new AES-GCM cipher for a cryptographic operation.
pub fn new(key: &'c [u8; KEY_SIZE], iv: &'c [u8; 12]) -> Self {
let mut new_gcm = Self { key: key, iv: [0; 16] };
new_gcm.iv[..12].copy_from_slice(iv);
new_gcm.iv[15] = 2;
new_gcm
}
}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize> Cipher<'c> for AesGcm<'c, KEY_SIZE> {
const BLOCK_SIZE: usize = AES_BLOCK_SIZE;
fn key(&self) -> &'c [u8] {
self.key
}
fn iv(&self) -> &[u8] {
self.iv.as_slice()
}
fn set_algomode(&self, p: &pac::cryp::Cryp) {
p.cr().modify(|w| w.set_algomode0(0));
p.cr().modify(|w| w.set_algomode3(true));
}
fn init_phase_blocking<T: Instance, DmaIn, DmaOut>(&self, p: &pac::cryp::Cryp, _cryp: &Cryp<T, DmaIn, DmaOut>) {
p.cr().modify(|w| w.set_gcm_ccmph(0));
p.cr().modify(|w| w.set_crypen(true));
while p.cr().read().crypen() {}
}
async fn init_phase<T: Instance, DmaIn, DmaOut>(
&self,
p: &pac::cryp::Cryp,
_cryp: &mut Cryp<'_, T, DmaIn, DmaOut>,
) {
p.cr().modify(|w| w.set_gcm_ccmph(0));
p.cr().modify(|w| w.set_crypen(true));
while p.cr().read().crypen() {}
}
#[cfg(cryp_v2)]
fn pre_final(&self, p: &pac::cryp::Cryp, dir: Direction, _padding_len: usize) -> [u32; 4] {
//Handle special GCM partial block process.
if dir == Direction::Encrypt {
p.cr().modify(|w| w.set_crypen(false));
p.cr().modify(|w| w.set_algomode3(false));
p.cr().modify(|w| w.set_algomode0(6));
let iv1r = p.csgcmccmr(7).read() - 1;
p.init(1).ivrr().write_value(iv1r);
p.cr().modify(|w| w.set_crypen(true));
}
[0; 4]
}
#[cfg(any(cryp_v3, cryp_v4))]
fn pre_final(&self, p: &pac::cryp::Cryp, _dir: Direction, padding_len: usize) -> [u32; 4] {
//Handle special GCM partial block process.
p.cr().modify(|w| w.set_npblb(padding_len as u8));
[0; 4]
}
#[cfg(cryp_v2)]
fn post_final_blocking<T: Instance, DmaIn, DmaOut>(
&self,
p: &pac::cryp::Cryp,
cryp: &Cryp<T, DmaIn, DmaOut>,
dir: Direction,
int_data: &mut [u8; AES_BLOCK_SIZE],
_temp1: [u32; 4],
padding_mask: [u8; AES_BLOCK_SIZE],
) {
if dir == Direction::Encrypt {
//Handle special GCM partial block process.
p.cr().modify(|w| w.set_crypen(false));
p.cr().modify(|w| w.set_algomode3(true));
p.cr().modify(|w| w.set_algomode0(0));
for i in 0..AES_BLOCK_SIZE {
int_data[i] = int_data[i] & padding_mask[i];
}
p.cr().modify(|w| w.set_crypen(true));
p.cr().modify(|w| w.set_gcm_ccmph(3));
cryp.write_bytes_blocking(Self::BLOCK_SIZE, int_data);
cryp.read_bytes_blocking(Self::BLOCK_SIZE, int_data);
}
}
#[cfg(cryp_v2)]
async fn post_final<T: Instance, DmaIn, DmaOut>(
&self,
p: &pac::cryp::Cryp,
cryp: &mut Cryp<'_, T, DmaIn, DmaOut>,
dir: Direction,
int_data: &mut [u8; AES_BLOCK_SIZE],
_temp1: [u32; 4],
padding_mask: [u8; AES_BLOCK_SIZE],
) where
DmaIn: crate::cryp::DmaIn<T>,
DmaOut: crate::cryp::DmaOut<T>,
{
if dir == Direction::Encrypt {
// Handle special GCM partial block process.
p.cr().modify(|w| w.set_crypen(false));
p.cr().modify(|w| w.set_algomode3(true));
p.cr().modify(|w| w.set_algomode0(0));
for i in 0..AES_BLOCK_SIZE {
int_data[i] = int_data[i] & padding_mask[i];
}
p.cr().modify(|w| w.set_crypen(true));
p.cr().modify(|w| w.set_gcm_ccmph(3));
let mut out_data: [u8; AES_BLOCK_SIZE] = [0; AES_BLOCK_SIZE];
let read = Cryp::<T, DmaIn, DmaOut>::read_bytes(&mut cryp.outdma, Self::BLOCK_SIZE, &mut out_data);
let write = Cryp::<T, DmaIn, DmaOut>::write_bytes(&mut cryp.indma, Self::BLOCK_SIZE, int_data);
embassy_futures::join::join(read, write).await;
int_data.copy_from_slice(&out_data);
}
}
}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c> CipherSized for AesGcm<'c, { 128 / 8 }> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c> CipherSized for AesGcm<'c, { 192 / 8 }> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c> CipherSized for AesGcm<'c, { 256 / 8 }> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize> CipherAuthenticated<16> for AesGcm<'c, KEY_SIZE> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize> IVSized for AesGcm<'c, KEY_SIZE> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
/// AES-GMAC Cipher Mode
pub struct AesGmac<'c, const KEY_SIZE: usize> {
iv: [u8; 16],
key: &'c [u8; KEY_SIZE],
}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize> AesGmac<'c, KEY_SIZE> {
/// Constructs a new AES-GMAC cipher for a cryptographic operation.
pub fn new(key: &'c [u8; KEY_SIZE], iv: &'c [u8; 12]) -> Self {
let mut new_gmac = Self { key: key, iv: [0; 16] };
new_gmac.iv[..12].copy_from_slice(iv);
new_gmac.iv[15] = 2;
new_gmac
}
}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize> Cipher<'c> for AesGmac<'c, KEY_SIZE> {
const BLOCK_SIZE: usize = AES_BLOCK_SIZE;
fn key(&self) -> &'c [u8] {
self.key
}
fn iv(&self) -> &[u8] {
self.iv.as_slice()
}
fn set_algomode(&self, p: &pac::cryp::Cryp) {
p.cr().modify(|w| w.set_algomode0(0));
p.cr().modify(|w| w.set_algomode3(true));
}
fn init_phase_blocking<T: Instance, DmaIn, DmaOut>(&self, p: &pac::cryp::Cryp, _cryp: &Cryp<T, DmaIn, DmaOut>) {
p.cr().modify(|w| w.set_gcm_ccmph(0));
p.cr().modify(|w| w.set_crypen(true));
while p.cr().read().crypen() {}
}
async fn init_phase<T: Instance, DmaIn, DmaOut>(
&self,
p: &pac::cryp::Cryp,
_cryp: &mut Cryp<'_, T, DmaIn, DmaOut>,
) {
p.cr().modify(|w| w.set_gcm_ccmph(0));
p.cr().modify(|w| w.set_crypen(true));
while p.cr().read().crypen() {}
}
#[cfg(cryp_v2)]
fn pre_final(&self, p: &pac::cryp::Cryp, dir: Direction, _padding_len: usize) -> [u32; 4] {
//Handle special GCM partial block process.
if dir == Direction::Encrypt {
p.cr().modify(|w| w.set_crypen(false));
p.cr().modify(|w| w.set_algomode3(false));
p.cr().modify(|w| w.set_algomode0(6));
let iv1r = p.csgcmccmr(7).read() - 1;
p.init(1).ivrr().write_value(iv1r);
p.cr().modify(|w| w.set_crypen(true));
}
[0; 4]
}
#[cfg(any(cryp_v3, cryp_v4))]
fn pre_final(&self, p: &pac::cryp::Cryp, _dir: Direction, padding_len: usize) -> [u32; 4] {
//Handle special GCM partial block process.
p.cr().modify(|w| w.set_npblb(padding_len as u8));
[0; 4]
}
#[cfg(cryp_v2)]
fn post_final_blocking<T: Instance, DmaIn, DmaOut>(
&self,
p: &pac::cryp::Cryp,
cryp: &Cryp<T, DmaIn, DmaOut>,
dir: Direction,
int_data: &mut [u8; AES_BLOCK_SIZE],
_temp1: [u32; 4],
padding_mask: [u8; AES_BLOCK_SIZE],
) {
if dir == Direction::Encrypt {
//Handle special GCM partial block process.
p.cr().modify(|w| w.set_crypen(false));
p.cr().modify(|w| w.set_algomode3(true));
p.cr().modify(|w| w.set_algomode0(0));
for i in 0..AES_BLOCK_SIZE {
int_data[i] = int_data[i] & padding_mask[i];
}
p.cr().modify(|w| w.set_crypen(true));
p.cr().modify(|w| w.set_gcm_ccmph(3));
cryp.write_bytes_blocking(Self::BLOCK_SIZE, int_data);
cryp.read_bytes_blocking(Self::BLOCK_SIZE, int_data);
}
}
#[cfg(cryp_v2)]
async fn post_final<T: Instance, DmaIn, DmaOut>(
&self,
p: &pac::cryp::Cryp,
cryp: &mut Cryp<'_, T, DmaIn, DmaOut>,
dir: Direction,
int_data: &mut [u8; AES_BLOCK_SIZE],
_temp1: [u32; 4],
padding_mask: [u8; AES_BLOCK_SIZE],
) where
DmaIn: crate::cryp::DmaIn<T>,
DmaOut: crate::cryp::DmaOut<T>,
{
if dir == Direction::Encrypt {
// Handle special GCM partial block process.
p.cr().modify(|w| w.set_crypen(false));
p.cr().modify(|w| w.set_algomode3(true));
p.cr().modify(|w| w.set_algomode0(0));
for i in 0..AES_BLOCK_SIZE {
int_data[i] = int_data[i] & padding_mask[i];
}
p.cr().modify(|w| w.set_crypen(true));
p.cr().modify(|w| w.set_gcm_ccmph(3));
let mut out_data: [u8; AES_BLOCK_SIZE] = [0; AES_BLOCK_SIZE];
let read = Cryp::<T, DmaIn, DmaOut>::read_bytes(&mut cryp.outdma, Self::BLOCK_SIZE, &mut out_data);
let write = Cryp::<T, DmaIn, DmaOut>::write_bytes(&mut cryp.indma, Self::BLOCK_SIZE, int_data);
embassy_futures::join::join(read, write).await;
}
}
}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c> CipherSized for AesGmac<'c, { 128 / 8 }> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c> CipherSized for AesGmac<'c, { 192 / 8 }> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c> CipherSized for AesGmac<'c, { 256 / 8 }> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize> CipherAuthenticated<16> for AesGmac<'c, KEY_SIZE> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize> IVSized for AesGmac<'c, KEY_SIZE> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
/// AES-CCM Cipher Mode
pub struct AesCcm<'c, const KEY_SIZE: usize, const TAG_SIZE: usize, const IV_SIZE: usize> {
key: &'c [u8; KEY_SIZE],
aad_header: [u8; 6],
aad_header_len: usize,
block0: [u8; 16],
ctr: [u8; 16],
}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize, const TAG_SIZE: usize, const IV_SIZE: usize> AesCcm<'c, KEY_SIZE, TAG_SIZE, IV_SIZE> {
/// Constructs a new AES-CCM cipher for a cryptographic operation.
pub fn new(key: &'c [u8; KEY_SIZE], iv: &'c [u8; IV_SIZE], aad_len: usize, payload_len: usize) -> Self {
let mut aad_header: [u8; 6] = [0; 6];
let mut aad_header_len = 0;
let mut block0: [u8; 16] = [0; 16];
if aad_len != 0 {
if aad_len < 65280 {
aad_header[0] = (aad_len >> 8) as u8 & 0xFF;
aad_header[1] = aad_len as u8 & 0xFF;
aad_header_len = 2;
} else {
aad_header[0] = 0xFF;
aad_header[1] = 0xFE;
let aad_len_bytes: [u8; 4] = aad_len.to_be_bytes();
aad_header[2] = aad_len_bytes[0];
aad_header[3] = aad_len_bytes[1];
aad_header[4] = aad_len_bytes[2];
aad_header[5] = aad_len_bytes[3];
aad_header_len = 6;
}
}
let total_aad_len = aad_header_len + aad_len;
let mut aad_padding_len = 16 - (total_aad_len % 16);
if aad_padding_len == 16 {
aad_padding_len = 0;
}
aad_header_len += aad_padding_len;
let total_aad_len_padded = aad_header_len + aad_len;
if total_aad_len_padded > 0 {
block0[0] = 0x40;
}
block0[0] |= ((((TAG_SIZE as u8) - 2) >> 1) & 0x07) << 3;
block0[0] |= ((15 - (iv.len() as u8)) - 1) & 0x07;
block0[1..1 + iv.len()].copy_from_slice(iv);
let payload_len_bytes: [u8; 4] = payload_len.to_be_bytes();
if iv.len() <= 11 {
block0[12] = payload_len_bytes[0];
} else if payload_len_bytes[0] > 0 {
panic!("Message is too large for given IV size.");
}
if iv.len() <= 12 {
block0[13] = payload_len_bytes[1];
} else if payload_len_bytes[1] > 0 {
panic!("Message is too large for given IV size.");
}
block0[14] = payload_len_bytes[2];
block0[15] = payload_len_bytes[3];
let mut ctr: [u8; 16] = [0; 16];
ctr[0] = block0[0] & 0x07;
ctr[1..1 + iv.len()].copy_from_slice(&block0[1..1 + iv.len()]);
ctr[15] = 0x01;
return Self {
key: key,
aad_header: aad_header,
aad_header_len: aad_header_len,
block0: block0,
ctr: ctr,
};
}
}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize, const TAG_SIZE: usize, const IV_SIZE: usize> Cipher<'c>
for AesCcm<'c, KEY_SIZE, TAG_SIZE, IV_SIZE>
{
const BLOCK_SIZE: usize = AES_BLOCK_SIZE;
fn key(&self) -> &'c [u8] {
self.key
}
fn iv(&self) -> &[u8] {
self.ctr.as_slice()
}
fn set_algomode(&self, p: &pac::cryp::Cryp) {
p.cr().modify(|w| w.set_algomode0(1));
p.cr().modify(|w| w.set_algomode3(true));
}
fn init_phase_blocking<T: Instance, DmaIn, DmaOut>(&self, p: &pac::cryp::Cryp, cryp: &Cryp<T, DmaIn, DmaOut>) {
p.cr().modify(|w| w.set_gcm_ccmph(0));
cryp.write_bytes_blocking(Self::BLOCK_SIZE, &self.block0);
p.cr().modify(|w| w.set_crypen(true));
while p.cr().read().crypen() {}
}
async fn init_phase<T: Instance, DmaIn, DmaOut>(&self, p: &pac::cryp::Cryp, cryp: &mut Cryp<'_, T, DmaIn, DmaOut>)
where
DmaIn: crate::cryp::DmaIn<T>,
DmaOut: crate::cryp::DmaOut<T>,
{
p.cr().modify(|w| w.set_gcm_ccmph(0));
Cryp::<T, DmaIn, DmaOut>::write_bytes(&mut cryp.indma, Self::BLOCK_SIZE, &self.block0).await;
p.cr().modify(|w| w.set_crypen(true));
while p.cr().read().crypen() {}
}
fn get_header_block(&self) -> &[u8] {
return &self.aad_header[0..self.aad_header_len];
}
#[cfg(cryp_v2)]
fn pre_final(&self, p: &pac::cryp::Cryp, dir: Direction, _padding_len: usize) -> [u32; 4] {
//Handle special CCM partial block process.
let mut temp1 = [0; 4];
if dir == Direction::Decrypt {
p.cr().modify(|w| w.set_crypen(false));
let iv1temp = p.init(1).ivrr().read();
temp1[0] = p.csgcmccmr(0).read().swap_bytes();
temp1[1] = p.csgcmccmr(1).read().swap_bytes();
temp1[2] = p.csgcmccmr(2).read().swap_bytes();
temp1[3] = p.csgcmccmr(3).read().swap_bytes();
p.init(1).ivrr().write_value(iv1temp);
p.cr().modify(|w| w.set_algomode3(false));
p.cr().modify(|w| w.set_algomode0(6));
p.cr().modify(|w| w.set_crypen(true));
}
return temp1;
}
#[cfg(any(cryp_v3, cryp_v4))]
fn pre_final(&self, p: &pac::cryp::Cryp, _dir: Direction, padding_len: usize) -> [u32; 4] {
//Handle special GCM partial block process.
p.cr().modify(|w| w.set_npblb(padding_len as u8));
[0; 4]
}
#[cfg(cryp_v2)]
fn post_final_blocking<T: Instance, DmaIn, DmaOut>(
&self,
p: &pac::cryp::Cryp,
cryp: &Cryp<T, DmaIn, DmaOut>,
dir: Direction,
int_data: &mut [u8; AES_BLOCK_SIZE],
temp1: [u32; 4],
padding_mask: [u8; 16],
) {
if dir == Direction::Decrypt {
//Handle special CCM partial block process.
let mut temp2 = [0; 4];
temp2[0] = p.csgcmccmr(0).read().swap_bytes();
temp2[1] = p.csgcmccmr(1).read().swap_bytes();
temp2[2] = p.csgcmccmr(2).read().swap_bytes();
temp2[3] = p.csgcmccmr(3).read().swap_bytes();
p.cr().modify(|w| w.set_algomode3(true));
p.cr().modify(|w| w.set_algomode0(1));
p.cr().modify(|w| w.set_gcm_ccmph(3));
// Header phase
p.cr().modify(|w| w.set_gcm_ccmph(1));
for i in 0..AES_BLOCK_SIZE {
int_data[i] = int_data[i] & padding_mask[i];
}
let mut in_data: [u32; 4] = [0; 4];
for i in 0..in_data.len() {
let mut int_bytes: [u8; 4] = [0; 4];
int_bytes.copy_from_slice(&int_data[(i * 4)..(i * 4) + 4]);
let int_word = u32::from_le_bytes(int_bytes);
in_data[i] = int_word;
in_data[i] = in_data[i] ^ temp1[i] ^ temp2[i];
}
cryp.write_words_blocking(Self::BLOCK_SIZE, &in_data);
}
}
#[cfg(cryp_v2)]
async fn post_final<T: Instance, DmaIn, DmaOut>(
&self,
p: &pac::cryp::Cryp,
cryp: &mut Cryp<'_, T, DmaIn, DmaOut>,
dir: Direction,
int_data: &mut [u8; AES_BLOCK_SIZE],
temp1: [u32; 4],
padding_mask: [u8; 16],
) where
DmaIn: crate::cryp::DmaIn<T>,
DmaOut: crate::cryp::DmaOut<T>,
{
if dir == Direction::Decrypt {
//Handle special CCM partial block process.
let mut temp2 = [0; 4];
temp2[0] = p.csgcmccmr(0).read().swap_bytes();
temp2[1] = p.csgcmccmr(1).read().swap_bytes();
temp2[2] = p.csgcmccmr(2).read().swap_bytes();
temp2[3] = p.csgcmccmr(3).read().swap_bytes();
p.cr().modify(|w| w.set_algomode3(true));
p.cr().modify(|w| w.set_algomode0(1));
p.cr().modify(|w| w.set_gcm_ccmph(3));
// Header phase
p.cr().modify(|w| w.set_gcm_ccmph(1));
for i in 0..AES_BLOCK_SIZE {
int_data[i] = int_data[i] & padding_mask[i];
}
let mut in_data: [u32; 4] = [0; 4];
for i in 0..in_data.len() {
let mut int_bytes: [u8; 4] = [0; 4];
int_bytes.copy_from_slice(&int_data[(i * 4)..(i * 4) + 4]);
let int_word = u32::from_le_bytes(int_bytes);
in_data[i] = int_word;
in_data[i] = in_data[i] ^ temp1[i] ^ temp2[i];
}
Cryp::<T, DmaIn, DmaOut>::write_words(&mut cryp.indma, Self::BLOCK_SIZE, &in_data).await;
}
}
}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const TAG_SIZE: usize, const IV_SIZE: usize> CipherSized for AesCcm<'c, { 128 / 8 }, TAG_SIZE, IV_SIZE> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const TAG_SIZE: usize, const IV_SIZE: usize> CipherSized for AesCcm<'c, { 192 / 8 }, TAG_SIZE, IV_SIZE> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const TAG_SIZE: usize, const IV_SIZE: usize> CipherSized for AesCcm<'c, { 256 / 8 }, TAG_SIZE, IV_SIZE> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize, const IV_SIZE: usize> CipherAuthenticated<4> for AesCcm<'c, KEY_SIZE, 4, IV_SIZE> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize, const IV_SIZE: usize> CipherAuthenticated<6> for AesCcm<'c, KEY_SIZE, 6, IV_SIZE> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize, const IV_SIZE: usize> CipherAuthenticated<8> for AesCcm<'c, KEY_SIZE, 8, IV_SIZE> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize, const IV_SIZE: usize> CipherAuthenticated<10> for AesCcm<'c, KEY_SIZE, 10, IV_SIZE> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize, const IV_SIZE: usize> CipherAuthenticated<12> for AesCcm<'c, KEY_SIZE, 12, IV_SIZE> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize, const IV_SIZE: usize> CipherAuthenticated<14> for AesCcm<'c, KEY_SIZE, 14, IV_SIZE> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize, const IV_SIZE: usize> CipherAuthenticated<16> for AesCcm<'c, KEY_SIZE, 16, IV_SIZE> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize, const TAG_SIZE: usize> IVSized for AesCcm<'c, KEY_SIZE, TAG_SIZE, 7> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize, const TAG_SIZE: usize> IVSized for AesCcm<'c, KEY_SIZE, TAG_SIZE, 8> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize, const TAG_SIZE: usize> IVSized for AesCcm<'c, KEY_SIZE, TAG_SIZE, 9> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize, const TAG_SIZE: usize> IVSized for AesCcm<'c, KEY_SIZE, TAG_SIZE, 10> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize, const TAG_SIZE: usize> IVSized for AesCcm<'c, KEY_SIZE, TAG_SIZE, 11> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize, const TAG_SIZE: usize> IVSized for AesCcm<'c, KEY_SIZE, TAG_SIZE, 12> {}
#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]
impl<'c, const KEY_SIZE: usize, const TAG_SIZE: usize> IVSized for AesCcm<'c, KEY_SIZE, TAG_SIZE, 13> {}
#[allow(dead_code)]
/// Holds the state information for a cipher operation.
/// Allows suspending/resuming of cipher operations.
pub struct Context<'c, C: Cipher<'c> + CipherSized> {
phantom_data: PhantomData<&'c C>,
cipher: &'c C,
dir: Direction,
last_block_processed: bool,
header_processed: bool,
aad_complete: bool,
cr: u32,
iv: [u32; 4],
csgcmccm: [u32; 8],