1#![cfg(any(
16 all(target_arch = "aarch64", target_endian = "little"),
17 target_arch = "x86",
18 target_arch = "x86_64"
19))]
20
21use super::{ffi::KeyValue, HTable, UpdateBlock, Xi};
22use crate::aead::gcm::ffi::BLOCK_LEN;
23use crate::cpu;
24#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
25use {super::UpdateBlocks, crate::polyfill::slice::AsChunks};
26
27#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
28pub(in super::super) type RequiredCpuFeatures = cpu::arm::PMull;
29
30#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
31pub(in super::super) type RequiredCpuFeatures = (cpu::intel::ClMul, cpu::intel::Ssse3);
32
33#[derive(Clone)]
34pub struct Key {
35 h_table: HTable,
36}
37
38impl Key {
39 #[cfg_attr(target_arch = "x86_64", inline(never))]
40 pub(in super::super) fn new(value: KeyValue, _cpu: RequiredCpuFeatures) -> Self {
41 Self {
42 h_table: unsafe { htable_new!(gcm_init_clmul, value) },
43 }
44 }
45
46 #[cfg(target_arch = "aarch64")]
47 pub(super) fn inner(&self) -> &HTable {
48 &self.h_table
49 }
50}
51
52impl UpdateBlock for Key {
53 #[cfg(target_arch = "aarch64")]
54 fn update_block(&self, xi: &mut Xi, a: [u8; BLOCK_LEN]) {
55 prefixed_extern! {
56 fn gcm_gmult_clmul(xi: &mut Xi, Htable: &HTable);
57 }
58 xi.bitxor_assign(a);
59 unsafe { self.h_table.gmult(gcm_gmult_clmul, xi) };
60 }
61
62 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
63 fn update_block(&self, xi: &mut Xi, a: [u8; BLOCK_LEN]) {
64 self.update_blocks(xi, (&a).into())
65 }
66}
67
68#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
69impl UpdateBlocks for Key {
70 fn update_blocks(&self, xi: &mut Xi, input: AsChunks<u8, { BLOCK_LEN }>) {
71 unsafe { ghash!(gcm_ghash_clmul, xi, &self.h_table, input) }
72 }
73}