ring/aead/aes_gcm/
vaesclmulavx2.rs

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.
14
15#![cfg(target_arch = "x86_64")]
16
17use super::{aes, gcm, Counter, BLOCK_LEN};
18use crate::{aead::aes::Overlapping, c, polyfill::slice::AsChunksMut};
19use core::num::NonZeroU32;
20
21pub(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,
25    mut in_out: AsChunksMut<u8, BLOCK_LEN>,
26) {
27    prefixed_extern! {
28        fn 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    }
37
38    let in_out = in_out.as_flattened_mut();
39
40    // Precondition: Since we have a `gcm::Context` then the number of blocks
41    // must fit in `u32`.
42    let blocks = u32::try_from(in_out.len() / BLOCK_LEN).unwrap();
43
44    if let Some(blocks) = NonZeroU32::new(blocks) {
45        let aes_key = aes_key.inner_less_safe();
46        let (htable, xi) = auth.inner();
47        let input = in_out.as_ptr();
48        let output = in_out.as_mut_ptr();
49        let len = in_out.len();
50        unsafe { aes_gcm_enc_update_vaes_avx2(input, output, len, aes_key, ctr, htable, xi) };
51        ctr.increment_by_less_safe(blocks);
52    }
53}
54
55pub(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) {
61    prefixed_extern! {
62        fn 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    }
71
72    // Precondition. TODO: Create an overlapping::AsChunks for this.
73    assert_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`.
76    let blocks = u32::try_from(in_out.len() / BLOCK_LEN).unwrap();
77
78    if let Some(blocks) = NonZeroU32::new(blocks) {
79        let aes_key = aes_key.inner_less_safe();
80        let (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}