1// Copyright 2015-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.
1415#![cfg(target_arch = "x86_64")]
1617use super::{aes, gcm, Counter, BLOCK_LEN};
18use crate::{aead::aes::Overlapping, c, polyfill::slice::AsChunksMut};
19use core::num::NonZeroU32;
2021pub(super) fn seal_whole_vaes_clmul_avx2(
22 aes_key: &aes::hw::Key,
23 auth: &mut gcm::Context<gcm::vclmulavx2::Key>,
24 ctr: &mut Counter,
25mut in_out: AsChunksMut<u8, BLOCK_LEN>,
26) {
27prefixed_extern! {
28fn aes_gcm_enc_update_vaes_avx2(
29 input: *const u8,
30 output: *mut u8,
31 len: c::size_t,
32 key: &aes::AES_KEY,
33 ivec: &Counter,
34 Htable: &gcm::HTable,
35 Xi: &mut gcm::Xi);
36 }
3738let in_out = in_out.as_flattened_mut();
3940// Precondition: Since we have a `gcm::Context` then the number of blocks
41 // must fit in `u32`.
42let blocks = u32::try_from(in_out.len() / BLOCK_LEN).unwrap();
4344if let Some(blocks) = NonZeroU32::new(blocks) {
45let aes_key = aes_key.inner_less_safe();
46let (htable, xi) = auth.inner();
47let input = in_out.as_ptr();
48let output = in_out.as_mut_ptr();
49let len = in_out.len();
50unsafe { aes_gcm_enc_update_vaes_avx2(input, output, len, aes_key, ctr, htable, xi) };
51 ctr.increment_by_less_safe(blocks);
52 }
53}
5455pub(super) fn open_whole_vaes_clmul_avx2(
56 aes_key: &aes::hw::Key,
57 auth: &mut gcm::Context<gcm::vclmulavx2::Key>,
58 in_out: Overlapping,
59 ctr: &mut Counter,
60) {
61prefixed_extern! {
62fn aes_gcm_dec_update_vaes_avx2(
63 input: *const u8,
64 output: *mut u8,
65 len: c::size_t,
66 key: &aes::AES_KEY,
67 ivec: &mut Counter,
68 Htable: &gcm::HTable,
69 Xi: &mut gcm::Xi);
70 }
7172// Precondition. TODO: Create an overlapping::AsChunks for this.
73assert_eq!(in_out.len() % BLOCK_LEN, 0);
74// Precondition: Since we have a `gcm::Context` then the number of blocks
75 // must fit in `u32`.
76let blocks = u32::try_from(in_out.len() / BLOCK_LEN).unwrap();
7778if let Some(blocks) = NonZeroU32::new(blocks) {
79let aes_key = aes_key.inner_less_safe();
80let (htable, xi) = auth.inner();
81 in_out.with_input_output_len(|input, output, len| unsafe {
82 aes_gcm_dec_update_vaes_avx2(input, output, len, aes_key, ctr, htable, xi)
83 });
84 ctr.increment_by_less_safe(blocks);
85 }
86}