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.
1415#![cfg(any(
16 all(target_arch = "aarch64", target_endian = "little"),
17 target_arch = "x86",
18 target_arch = "x86_64"
19))]
2021use super::{Block, Counter, EncryptBlock, EncryptCtr32, Iv, KeyBytes, Overlapping, AES_KEY};
22use crate::{cpu, error};
23use cfg_if::cfg_if;
2425cfg_if! {
26if #[cfg(all(target_arch = "aarch64", target_endian = "little"))] {
27pub(in super::super) type RequiredCpuFeatures = cpu::arm::Aes;
28pub(in super::super) type OptionalCpuFeatures = ();
29 } else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
30use cpu::intel::{Aes, Avx, Ssse3};
31// Some functions seem to have been written to require only SSE/SSE2
32 // but there seem to be no SSSE3-less CPUs with AES-NI, and we don't
33 // have feature detection for SSE2.
34pub(in super::super) type RequiredCpuFeatures = (Aes, Ssse3);
35pub(in super::super) type OptionalCpuFeatures = Avx;
36 }
37}
3839#[derive(Clone)]
40pub struct Key {
41 inner: AES_KEY,
42}
4344impl Key {
45#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
46pub(in super::super) fn new(
47 bytes: KeyBytes<'_>,
48 _required_cpu_features: RequiredCpuFeatures,
49 _optional_cpu_features: Option<OptionalCpuFeatures>,
50 ) -> Result<Self, error::Unspecified> {
51let inner = unsafe { set_encrypt_key!(aes_hw_set_encrypt_key, bytes) }?;
52Ok(Self { inner })
53 }
5455#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
56pub(in super::super) fn new(
57 bytes: KeyBytes<'_>,
58 (Aes { .. }, Ssse3 { .. }): RequiredCpuFeatures,
59 optional_cpu_features: Option<OptionalCpuFeatures>,
60 ) -> Result<Self, error::Unspecified> {
61// Ssse3 is required, but upstream only uses this if there is also Avx;
62 // presumably the base version is faster on pre-AVX CPUs.
63let inner = if let Some(Avx { .. }) = optional_cpu_features {
64unsafe { set_encrypt_key!(aes_hw_set_encrypt_key_alt, bytes) }?
65} else {
66unsafe { set_encrypt_key!(aes_hw_set_encrypt_key_base, bytes) }?
67};
68Ok(Self { inner })
69 }
7071#[cfg(any(
72 all(target_arch = "aarch64", target_endian = "little"),
73 target_arch = "x86_64"
74))]
75 #[must_use]
76pub(in super::super) fn inner_less_safe(&self) -> &AES_KEY {
77&self.inner
78 }
79}
8081impl EncryptBlock for Key {
82fn encrypt_block(&self, block: Block) -> Block {
83super::encrypt_block_using_encrypt_iv_xor_block(self, block)
84 }
8586fn encrypt_iv_xor_block(&self, iv: Iv, block: Block) -> Block {
87super::encrypt_iv_xor_block_using_ctr32(self, iv, block)
88 }
89}
9091impl EncryptCtr32 for Key {
92fn ctr32_encrypt_within(&self, in_out: Overlapping<'_>, ctr: &mut Counter) {
93unsafe { ctr32_encrypt_blocks!(aes_hw_ctr32_encrypt_blocks, in_out, &self.inner, ctr) }
94 }
95}