ring/aead/gcm/vclmulavx2.rs
1// Copyright 2018-2025 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 AUTHOR DISCLAIMS ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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
17use super::{ffi::KeyValue, HTable, UpdateBlock, Xi};
18use crate::{
19 aead::gcm::ffi::BLOCK_LEN,
20 cpu::intel::{Avx2, VAesClmul},
21 polyfill::slice::AsChunks,
22};
23
24#[derive(Clone)]
25pub struct Key {
26 h_table: HTable,
27}
28
29impl Key {
30 pub(in super::super) fn new(value: KeyValue, _cpu: (Avx2, VAesClmul)) -> Self {
31 Self {
32 h_table: unsafe { htable_new!(gcm_init_vpclmulqdq_avx2, value) },
33 }
34 }
35
36 pub(super) fn inner(&self) -> &HTable {
37 &self.h_table
38 }
39}
40
41impl UpdateBlock for Key {
42 fn update_block(&self, xi: &mut Xi, a: [u8; BLOCK_LEN]) {
43 let input: AsChunks<u8, BLOCK_LEN> = (&a).into();
44 unsafe { ghash!(gcm_ghash_vpclmulqdq_avx2_1, xi, &self.h_table, input) }
45 }
46}