|
| 1 | +use core::marker::PhantomData; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + peripheral::{Peripheral, PeripheralRef}, |
| 5 | + peripherals::{ |
| 6 | + generic::{Readable, Reg, RegisterSpec, Resettable, Writable}, |
| 7 | + AES, |
| 8 | + }, |
| 9 | + system::{Peripheral as PeripheralEnable, PeripheralClockControl}, |
| 10 | +}; |
| 11 | + |
| 12 | +const ALIGN_SIZE: usize = core::mem::size_of::<u32>(); |
| 13 | + |
| 14 | +pub struct Aes<'d> { |
| 15 | + aes: PeripheralRef<'d, AES>, |
| 16 | +} |
| 17 | + |
| 18 | +impl<'d> Aes<'d> { |
| 19 | + pub fn new( |
| 20 | + aes: impl Peripheral<P = AES> + 'd, |
| 21 | + peripheral_clock_control: &mut PeripheralClockControl, |
| 22 | + ) -> Self { |
| 23 | + crate::into_ref!(aes); |
| 24 | + peripheral_clock_control.enable(PeripheralEnable::Aes); |
| 25 | + Self { aes: aes } |
| 26 | + } |
| 27 | + |
| 28 | + fn write_key(&mut self, key: &[u8]) { |
| 29 | + debug_assert!(key.len() <= self.aes.key_.len() * ALIGN_SIZE); |
| 30 | + debug_assert_eq!(key.len() % ALIGN_SIZE, 0); |
| 31 | + Self::write_to_regset(key, &mut self.aes.key_); |
| 32 | + } |
| 33 | + |
| 34 | + fn write_block(&mut self, block: &[u8]) { |
| 35 | + debug_assert_eq!(block.len(), self.aes.text_.len() * ALIGN_SIZE); |
| 36 | + Self::write_to_regset(block, &mut self.aes.text_); |
| 37 | + } |
| 38 | + |
| 39 | + fn write_mode(&mut self, mode: u32) { |
| 40 | + Self::write_to_register(&mut self.aes.mode, mode); |
| 41 | + } |
| 42 | + |
| 43 | + fn write_endianness(&mut self, val: u32) { |
| 44 | + Self::write_to_register(&mut self.aes.endian, val) |
| 45 | + } |
| 46 | + |
| 47 | + fn write_start(&mut self) { |
| 48 | + self.aes.start.write(|w| w.start().set_bit()) |
| 49 | + } |
| 50 | + |
| 51 | + fn read_idle(&mut self) -> bool { |
| 52 | + self.aes.idle.read().idle().bit_is_set() |
| 53 | + } |
| 54 | + |
| 55 | + fn read_block(&self, block: &mut [u8]) { |
| 56 | + debug_assert_eq!(block.len(), self.aes.text_.len() * ALIGN_SIZE); |
| 57 | + Self::read_from_regset(block, &self.aes.text_); |
| 58 | + } |
| 59 | + |
| 60 | + fn write_to_regset<T>(input: &[u8], reg_set: &mut [Reg<T>]) |
| 61 | + where |
| 62 | + T: RegisterSpec<Ux = u32> + Resettable + Writable, |
| 63 | + { |
| 64 | + let chunks = input.chunks_exact(ALIGN_SIZE); |
| 65 | + for (reg, chunk) in reg_set.iter_mut().zip(chunks) { |
| 66 | + let to_write = u32::from_ne_bytes(chunk.try_into().unwrap()); |
| 67 | + Self::write_to_register(reg, to_write); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + fn read_from_regset<T>(out_buf: &mut [u8], reg_set: &[Reg<T>]) |
| 72 | + where |
| 73 | + T: RegisterSpec<Ux = u32> + Readable, |
| 74 | + { |
| 75 | + let chunks = out_buf.chunks_exact_mut(ALIGN_SIZE); |
| 76 | + for (reg, chunk) in reg_set.iter().zip(chunks) { |
| 77 | + let read_val: [u8; 4] = Self::read_from_register(reg).to_ne_bytes(); |
| 78 | + chunk.copy_from_slice(&read_val); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + fn write_to_register<T>(reg: &mut Reg<T>, data: u32) |
| 83 | + where |
| 84 | + T: RegisterSpec<Ux = u32> + Resettable + Writable, |
| 85 | + { |
| 86 | + reg.write(|w| unsafe { w.bits(data) }); |
| 87 | + } |
| 88 | + |
| 89 | + fn read_from_register<T>(reg: &Reg<T>) -> u32 |
| 90 | + where |
| 91 | + T: RegisterSpec<Ux = u32> + Readable, |
| 92 | + { |
| 93 | + reg.read().bits() |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +pub trait AesFlavour { |
| 98 | + type KeyType<'b>; |
| 99 | + const ENCRYPT_MODE: u32; |
| 100 | + const DECRYPT_MODE: u32; |
| 101 | +} |
| 102 | + |
| 103 | +pub struct Aes128; |
| 104 | + |
| 105 | +impl AesFlavour for Aes128 { |
| 106 | + type KeyType<'b> = &'b [u8; 16]; |
| 107 | + const ENCRYPT_MODE: u32 = 0; |
| 108 | + const DECRYPT_MODE: u32 = 4; |
| 109 | +} |
| 110 | + |
| 111 | +pub struct Aes192; |
| 112 | + |
| 113 | +impl AesFlavour for Aes192 { |
| 114 | + type KeyType<'b> = &'b [u8; 24]; |
| 115 | + const ENCRYPT_MODE: u32 = 1; |
| 116 | + const DECRYPT_MODE: u32 = 5; |
| 117 | +} |
| 118 | + |
| 119 | +pub struct Aes256; |
| 120 | + |
| 121 | +impl AesFlavour for Aes256 { |
| 122 | + type KeyType<'b> = &'b [u8; 32]; |
| 123 | + const ENCRYPT_MODE: u32 = 2; |
| 124 | + const DECRYPT_MODE: u32 = 6; |
| 125 | +} |
| 126 | + |
| 127 | +pub struct Cipher<'a, T: AesFlavour> { |
| 128 | + aes: &'a mut Aes<'a>, |
| 129 | + phantom: PhantomData<T>, |
| 130 | +} |
| 131 | + |
| 132 | +impl<'a, T: AesFlavour> Cipher<'a, T> { |
| 133 | + pub fn new(aes: &'a mut Aes<'a>, key: &Key<T>) -> Self { |
| 134 | + aes.write_endianness(63); |
| 135 | + aes.write_key(key.key); |
| 136 | + Self { |
| 137 | + aes, |
| 138 | + phantom: PhantomData, |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + pub fn encrypt_block(&mut self, block: &mut [u8; 16]) { |
| 143 | + self.set_mode(T::ENCRYPT_MODE); |
| 144 | + self.set_block(block); |
| 145 | + self.start(); |
| 146 | + while !(self.is_idle()) {} |
| 147 | + self.get_block(block); |
| 148 | + } |
| 149 | + |
| 150 | + pub fn decrypt_block(&mut self, block: &mut [u8; 16]) { |
| 151 | + self.set_mode(T::DECRYPT_MODE); |
| 152 | + self.set_block(block); |
| 153 | + self.start(); |
| 154 | + while !(self.is_idle()) {} |
| 155 | + self.get_block(block); |
| 156 | + } |
| 157 | + |
| 158 | + fn set_mode(&mut self, mode: u32) { |
| 159 | + self.aes.write_mode(mode); |
| 160 | + } |
| 161 | + |
| 162 | + fn is_idle(&mut self) -> bool { |
| 163 | + self.aes.read_idle() |
| 164 | + } |
| 165 | + |
| 166 | + fn set_block(&mut self, block: &[u8; 16]) { |
| 167 | + self.aes.write_block(block); |
| 168 | + } |
| 169 | + |
| 170 | + fn get_block(&self, block: &mut [u8; 16]) { |
| 171 | + self.aes.read_block(block); |
| 172 | + } |
| 173 | + |
| 174 | + fn start(&mut self) { |
| 175 | + self.aes.write_start(); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +pub struct Key<'b, T: AesFlavour> { |
| 180 | + key: &'b [u8], |
| 181 | + phantom: PhantomData<T>, |
| 182 | +} |
| 183 | + |
| 184 | +impl<'b, T, const N: usize> From<&'b [u8; N]> for Key<'b, T> |
| 185 | +where |
| 186 | + T: AesFlavour<KeyType<'b> = &'b [u8; N]>, |
| 187 | +{ |
| 188 | + fn from(value: T::KeyType<'b>) -> Self { |
| 189 | + Key { |
| 190 | + key: value, |
| 191 | + phantom: PhantomData, |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments