| 1 | // Copyright 2018-2024 Brian Smith. |
| 2 | // |
| 3 | // Permission to use, copy, modify, and/or distribute this software for any |
| 4 | // purpose with or without fee is hereby granted, provided that the above |
| 5 | // copyright notice and this permission notice appear in all copies. |
| 6 | // |
| 7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES |
| 8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
| 10 | // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | |
| 15 | use super::{Block, Counter, EncryptBlock, EncryptCtr32, Iv, KeyBytes, Overlapping, AES_KEY}; |
| 16 | use crate::error; |
| 17 | |
| 18 | #[derive (Clone)] |
| 19 | pub struct Key { |
| 20 | inner: AES_KEY, |
| 21 | } |
| 22 | |
| 23 | impl Key { |
| 24 | pub(in super::super) fn new(bytes: KeyBytes<'_>) -> Result<Self, error::Unspecified> { |
| 25 | let inner: AES_KEY = unsafe { set_encrypt_key!(aes_nohw_set_encrypt_key, bytes) }?; |
| 26 | Ok(Self { inner }) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | impl EncryptBlock for Key { |
| 31 | fn encrypt_block(&self, block: Block) -> Block { |
| 32 | unsafe { encrypt_block!(aes_nohw_encrypt, block, &self.inner) } |
| 33 | } |
| 34 | |
| 35 | fn encrypt_iv_xor_block(&self, iv: Iv, block: Block) -> Block { |
| 36 | super::encrypt_iv_xor_block_using_encrypt_block(self, iv, block) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | impl EncryptCtr32 for Key { |
| 41 | fn ctr32_encrypt_within(&self, in_out: Overlapping<'_>, ctr: &mut Counter) { |
| 42 | unsafe { ctr32_encrypt_blocks!(aes_nohw_ctr32_encrypt_blocks, in_out, &self.inner, ctr) } |
| 43 | } |
| 44 | } |
| 45 | |