| 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 | #![cfg (target_arch = "x86_64" )] |
| 16 | |
| 17 | use super::{HTable, KeyValue, UpdateBlock, UpdateBlocks, Xi, BLOCK_LEN}; |
| 18 | use crate::{cpu, polyfill::slice::AsChunks}; |
| 19 | |
| 20 | pub(in super::super) type RequiredCpuFeatures = |
| 21 | (cpu::intel::ClMul, cpu::intel::Avx, cpu::intel::Movbe); |
| 22 | |
| 23 | #[derive (Clone)] |
| 24 | pub struct Key { |
| 25 | h_table: HTable, |
| 26 | } |
| 27 | |
| 28 | impl Key { |
| 29 | pub(in super::super) fn new(value: KeyValue, _cpu: RequiredCpuFeatures) -> Self { |
| 30 | Self { |
| 31 | h_table: unsafe { htable_new!(gcm_init_avx, value) }, |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | pub(super) fn inner(&self) -> &HTable { |
| 36 | &self.h_table |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | impl UpdateBlock for Key { |
| 41 | fn update_block(&self, xi: &mut Xi, a: [u8; BLOCK_LEN]) { |
| 42 | self.update_blocks(xi, (&a).into()) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | impl UpdateBlocks for Key { |
| 47 | fn update_blocks(&self, xi: &mut Xi, input: AsChunks<u8, BLOCK_LEN>) { |
| 48 | unsafe { ghash!(gcm_ghash_avx, xi, self.inner(), input) } |
| 49 | } |
| 50 | } |
| 51 | |