|
| 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 | + |
| 17 | +use super::{aes, gcm, Counter, BLOCK_LEN}; |
| 18 | +use crate::{aead::aes::Overlapping, c, polyfill::slice::AsChunksMut}; |
| 19 | +use core::num::{NonZeroU32, NonZeroUsize}; |
| 20 | + |
| 21 | +pub(super) fn seal_whole( |
| 22 | + aes_key: &aes::hw::Key, |
| 23 | + auth: &mut gcm::Context<gcm::vclmulavx512::Key>, |
| 24 | + ctr: &mut Counter, |
| 25 | + mut in_out: AsChunksMut<u8, BLOCK_LEN>, |
| 26 | +) { |
| 27 | + prefixed_extern! { |
| 28 | + fn aes_gcm_enc_update_vaes_avx512( |
| 29 | + input: *const u8, |
| 30 | + output: *mut u8, |
| 31 | + len: c::NonZero_size_t, // TODO? zero OK? |
| 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(len) = NonZeroUsize::new(in_out.len()) { |
| 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 | + unsafe { aes_gcm_enc_update_vaes_avx512(input, output, len, aes_key, ctr, htable, xi) }; |
| 50 | + let blocks = NonZeroU32::new(blocks).unwrap_or_else(|| { |
| 51 | + unreachable!() // Due to previous checks. |
| 52 | + }); |
| 53 | + ctr.increment_by_less_safe(blocks); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +pub(super) fn open_whole( |
| 58 | + aes_key: &aes::hw::Key, |
| 59 | + auth: &mut gcm::Context<gcm::vclmulavx512::Key>, |
| 60 | + in_out: Overlapping, |
| 61 | + ctr: &mut Counter, |
| 62 | +) { |
| 63 | + prefixed_extern! { |
| 64 | + fn aes_gcm_dec_update_vaes_avx512( |
| 65 | + input: *const u8, |
| 66 | + output: *mut u8, |
| 67 | + len: c::NonZero_size_t, // TODO? zero OK? |
| 68 | + key: &aes::AES_KEY, |
| 69 | + ivec: &mut Counter, |
| 70 | + Htable: &gcm::HTable, |
| 71 | + Xi: &mut gcm::Xi); |
| 72 | + } |
| 73 | + |
| 74 | + // Precondition. TODO: Create an overlapping::AsChunks for this. |
| 75 | + assert_eq!(in_out.len() % BLOCK_LEN, 0); |
| 76 | + // Precondition: Since we have a `gcm::Context` then the number of blocks |
| 77 | + // must fit in `u32`. |
| 78 | + let blocks = u32::try_from(in_out.len() / BLOCK_LEN).unwrap(); |
| 79 | + |
| 80 | + in_out.with_input_output_len(|input, output, len| { |
| 81 | + if let Some(len) = NonZeroUsize::new(len) { |
| 82 | + let aes_key = aes_key.inner_less_safe(); |
| 83 | + let (htable, xi) = auth.inner(); |
| 84 | + unsafe { aes_gcm_dec_update_vaes_avx512(input, output, len, aes_key, ctr, htable, xi) }; |
| 85 | + let blocks = NonZeroU32::new(blocks).unwrap_or_else(|| { |
| 86 | + unreachable!() // Due to previous checks. |
| 87 | + }); |
| 88 | + ctr.increment_by_less_safe(blocks); |
| 89 | + } |
| 90 | + }) |
| 91 | +} |
0 commit comments