|
| 1 | +use crate::{ |
| 2 | + aes::{Aes, Aes128, Aes192, Aes256, AesFlavour, Endianness, ALIGN_SIZE}, |
| 3 | + system::{Peripheral as PeripheralEnable, PeripheralClockControl}, |
| 4 | +}; |
| 5 | + |
| 6 | +impl<'d> Aes<'d> { |
| 7 | + pub(super) fn init(&mut self, peripheral_clock_control: &mut PeripheralClockControl) { |
| 8 | + peripheral_clock_control.enable(PeripheralEnable::Aes); |
| 9 | + self.write_dma(false); |
| 10 | + self.write_endianness( |
| 11 | + Endianness::BigEndian, |
| 12 | + Endianness::BigEndian, |
| 13 | + Endianness::BigEndian, |
| 14 | + Endianness::BigEndian, |
| 15 | + Endianness::BigEndian, |
| 16 | + Endianness::BigEndian, |
| 17 | + ); |
| 18 | + } |
| 19 | + |
| 20 | + fn write_dma(&mut self, enable_dma: bool) { |
| 21 | + match enable_dma { |
| 22 | + true => self.aes.dma_enable.write(|w| w.dma_enable().set_bit()), |
| 23 | + false => self.aes.dma_enable.write(|w| w.dma_enable().clear_bit()), |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + pub(super) fn write_key(&mut self, key: &[u8]) { |
| 28 | + debug_assert!(key.len() <= self.aes.key_.len() * ALIGN_SIZE); |
| 29 | + debug_assert_eq!(key.len() % ALIGN_SIZE, 0); |
| 30 | + Self::write_to_regset(key, self.aes.key_.len(), &mut self.aes.key_[0]); |
| 31 | + } |
| 32 | + |
| 33 | + pub(super) fn write_block(&mut self, block: &[u8]) { |
| 34 | + debug_assert_eq!(block.len(), self.aes.text_in_.len() * ALIGN_SIZE); |
| 35 | + Self::write_to_regset(block, self.aes.text_in_.len(), &mut self.aes.text_in_[0]); |
| 36 | + } |
| 37 | + |
| 38 | + pub(super) fn write_mode(&mut self, mode: u32) { |
| 39 | + Self::write_to_register(&mut self.aes.mode, mode); |
| 40 | + } |
| 41 | + |
| 42 | + /// Configures how the state matrix would be laid out. |
| 43 | + pub fn write_endianness( |
| 44 | + &mut self, |
| 45 | + input_text_word_endianess: Endianness, |
| 46 | + input_text_byte_endianess: Endianness, |
| 47 | + output_text_word_endianess: Endianness, |
| 48 | + output_text_byte_endianess: Endianness, |
| 49 | + key_word_endianess: Endianness, |
| 50 | + key_byte_endianess: Endianness, |
| 51 | + ) { |
| 52 | + let mut to_write = 0_u32; |
| 53 | + to_write |= key_byte_endianess as u32; |
| 54 | + to_write |= (key_word_endianess as u32) << 1; |
| 55 | + to_write |= (input_text_byte_endianess as u32) << 2; |
| 56 | + to_write |= (input_text_word_endianess as u32) << 3; |
| 57 | + to_write |= (output_text_byte_endianess as u32) << 4; |
| 58 | + to_write |= (output_text_word_endianess as u32) << 5; |
| 59 | + Self::write_to_register(&mut self.aes.endian, to_write); |
| 60 | + } |
| 61 | + |
| 62 | + pub(super) fn write_start(&mut self) { |
| 63 | + self.aes.trigger.write(|w| w.trigger().set_bit()) |
| 64 | + } |
| 65 | + |
| 66 | + pub(super) fn read_idle(&mut self) -> bool { |
| 67 | + self.aes.state.read().state().bits() == 0 |
| 68 | + } |
| 69 | + |
| 70 | + pub(super) fn read_block(&self, block: &mut [u8]) { |
| 71 | + debug_assert_eq!(block.len(), self.aes.text_out_.len() * ALIGN_SIZE); |
| 72 | + Self::read_from_regset(block, self.aes.text_out_.len(), &self.aes.text_out_[0]); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl AesFlavour for Aes128 { |
| 77 | + type KeyType<'b> = &'b [u8; 16]; |
| 78 | + const ENCRYPT_MODE: u32 = 0; |
| 79 | + const DECRYPT_MODE: u32 = 4; |
| 80 | +} |
| 81 | + |
| 82 | +impl AesFlavour for Aes192 { |
| 83 | + type KeyType<'b> = &'b [u8; 24]; |
| 84 | + const ENCRYPT_MODE: u32 = 1; |
| 85 | + const DECRYPT_MODE: u32 = 5; |
| 86 | +} |
| 87 | + |
| 88 | +impl AesFlavour for Aes256 { |
| 89 | + type KeyType<'b> = &'b [u8; 32]; |
| 90 | + const ENCRYPT_MODE: u32 = 2; |
| 91 | + const DECRYPT_MODE: u32 = 6; |
| 92 | +} |
0 commit comments