ring/aead/gcm/clmulavxmovbe.rs
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 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::{HTable, KeyValue, UpdateBlock, UpdateBlocks, Xi, BLOCK_LEN};
18use crate::{cpu::intel, polyfill::slice::AsChunks};
19
20#[derive(Clone)]
21pub struct Key {
22 h_table: HTable,
23}
24
25impl Key {
26 #[inline(never)]
27 pub(in super::super) fn new(
28 value: KeyValue,
29 _required_cpu_features: (intel::ClMul, intel::Avx, intel::Movbe),
30 ) -> Self {
31 Self {
32 h_table: unsafe { htable_new!(gcm_init_avx, 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 self.update_blocks(xi, (&a).into())
44 }
45}
46
47impl UpdateBlocks for Key {
48 fn update_blocks(&self, xi: &mut Xi, input: AsChunks<u8, BLOCK_LEN>) {
49 unsafe { ghash!(gcm_ghash_avx, xi, self.inner(), input) }
50 }
51}