|
| 1 | +//! Autodetection support for hardware accelerated AES backends with fallback |
| 2 | +//! to the fixsliced "soft" implementation. |
| 3 | +
|
| 4 | +use crate::{Block, ParBlocks}; |
| 5 | +use cipher::{ |
| 6 | + consts::{U16, U24, U32, U8}, |
| 7 | + generic_array::GenericArray, |
| 8 | + BlockCipher, BlockDecrypt, BlockEncrypt, NewBlockCipher, |
| 9 | +}; |
| 10 | + |
| 11 | +cpuid_bool::new!(aes_cpuid, "aes"); |
| 12 | + |
| 13 | +macro_rules! define_aes_impl { |
| 14 | + ( |
| 15 | + $name:tt, |
| 16 | + $module:tt, |
| 17 | + $key_size:ty, |
| 18 | + $doc:expr |
| 19 | + ) => { |
| 20 | + #[doc=$doc] |
| 21 | + #[derive(Clone)] |
| 22 | + pub struct $name { |
| 23 | + inner: $module::Inner, |
| 24 | + token: aes_cpuid::InitToken |
| 25 | + } |
| 26 | + |
| 27 | + mod $module { |
| 28 | + #[derive(Copy, Clone)] |
| 29 | + pub(super) union Inner { |
| 30 | + pub(super) ni: crate::ni::$name, |
| 31 | + pub(super) soft: crate::soft::$name, |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + impl NewBlockCipher for $name { |
| 36 | + type KeySize = $key_size; |
| 37 | + |
| 38 | + #[inline] |
| 39 | + fn new(key: &GenericArray<u8, $key_size>) -> Self { |
| 40 | + let (token, aesni_present) = aes_cpuid::init_get(); |
| 41 | + |
| 42 | + let inner = if aesni_present { |
| 43 | + $module::Inner { ni: crate::ni::$name::new(key) } |
| 44 | + } else { |
| 45 | + $module::Inner { soft: crate::soft::$name::new(key) } |
| 46 | + }; |
| 47 | + |
| 48 | + Self { inner, token } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + impl BlockCipher for $name { |
| 53 | + type BlockSize = U16; |
| 54 | + type ParBlocks = U8; |
| 55 | + } |
| 56 | + |
| 57 | + impl BlockEncrypt for $name { |
| 58 | + #[inline] |
| 59 | + fn encrypt_block(&self, block: &mut Block) { |
| 60 | + if self.token.get() { |
| 61 | + unsafe { self.inner.ni.encrypt_block(block) } |
| 62 | + } else { |
| 63 | + unsafe { self.inner.soft.encrypt_block(block) } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + #[inline] |
| 68 | + fn encrypt_par_blocks(&self, blocks: &mut ParBlocks) { |
| 69 | + if self.token.get() { |
| 70 | + unsafe { self.inner.ni.encrypt_par_blocks(blocks) } |
| 71 | + } else { |
| 72 | + unsafe { self.inner.soft.encrypt_par_blocks(blocks) } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + impl BlockDecrypt for $name { |
| 78 | + #[inline] |
| 79 | + fn decrypt_block(&self, block: &mut Block) { |
| 80 | + if self.token.get() { |
| 81 | + unsafe { self.inner.ni.decrypt_block(block) } |
| 82 | + } else { |
| 83 | + unsafe { self.inner.soft.decrypt_block(block) } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + #[inline] |
| 88 | + fn decrypt_par_blocks(&self, blocks: &mut ParBlocks) { |
| 89 | + if self.token.get() { |
| 90 | + unsafe { self.inner.ni.decrypt_par_blocks(blocks) } |
| 91 | + } else { |
| 92 | + unsafe { self.inner.soft.decrypt_par_blocks(blocks) } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + opaque_debug::implement!($name); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +define_aes_impl!(Aes128, aes128, U16, "AES-128 block cipher instance"); |
| 102 | +define_aes_impl!(Aes192, aes192, U24, "AES-192 block cipher instance"); |
| 103 | +define_aes_impl!(Aes256, aes256, U32, "AES-256 block cipher instance"); |
| 104 | + |
| 105 | +#[cfg(feature = "ctr")] |
| 106 | +pub(crate) mod ctr { |
| 107 | + use super::{Aes128, Aes192, Aes256}; |
| 108 | + use cipher::{ |
| 109 | + block::BlockCipher, |
| 110 | + generic_array::GenericArray, |
| 111 | + stream::{ |
| 112 | + FromBlockCipher, LoopError, OverflowError, SeekNum, SyncStreamCipher, |
| 113 | + SyncStreamCipherSeek, |
| 114 | + }, |
| 115 | + }; |
| 116 | + |
| 117 | + cpuid_bool::new!(aes_ssse3_cpuid, "aes", "ssse3"); |
| 118 | + |
| 119 | + macro_rules! define_aes_ctr_impl { |
| 120 | + ( |
| 121 | + $name:tt, |
| 122 | + $cipher:ident, |
| 123 | + $module:tt, |
| 124 | + $doc:expr |
| 125 | + ) => { |
| 126 | + #[doc=$doc] |
| 127 | + #[cfg_attr(docsrs, doc(cfg(feature = "ctr")))] |
| 128 | + pub struct $name { |
| 129 | + inner: $module::Inner, |
| 130 | + } |
| 131 | + |
| 132 | + mod $module { |
| 133 | + pub(super) enum Inner { |
| 134 | + Ni(crate::ni::$name), |
| 135 | + Soft(crate::soft::$name), |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + impl FromBlockCipher for $name { |
| 140 | + type BlockCipher = $cipher; |
| 141 | + type NonceSize = <$cipher as BlockCipher>::BlockSize; |
| 142 | + |
| 143 | + fn from_block_cipher( |
| 144 | + cipher: $cipher, |
| 145 | + nonce: &GenericArray<u8, Self::NonceSize>, |
| 146 | + ) -> Self { |
| 147 | + let (_, aesni_present) = aes_ssse3_cpuid::init_get(); |
| 148 | + |
| 149 | + let inner = if aesni_present { |
| 150 | + $module::Inner::Ni( |
| 151 | + crate::ni::$name::from_block_cipher( |
| 152 | + unsafe { cipher.inner.ni }, |
| 153 | + nonce |
| 154 | + ) |
| 155 | + ) |
| 156 | + } else { |
| 157 | + $module::Inner::Soft( |
| 158 | + crate::soft::$name::from_block_cipher( |
| 159 | + unsafe { cipher.inner.soft }, |
| 160 | + nonce |
| 161 | + ) |
| 162 | + ) |
| 163 | + }; |
| 164 | + |
| 165 | + Self { inner } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + impl SyncStreamCipher for $name { |
| 170 | + #[inline] |
| 171 | + fn try_apply_keystream(&mut self, data: &mut [u8]) -> Result<(), LoopError> { |
| 172 | + match &mut self.inner { |
| 173 | + $module::Inner::Ni(aes) => aes.try_apply_keystream(data), |
| 174 | + $module::Inner::Soft(aes) => aes.try_apply_keystream(data) |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + impl SyncStreamCipherSeek for $name { |
| 180 | + #[inline] |
| 181 | + fn try_current_pos<T: SeekNum>(&self) -> Result<T, OverflowError> { |
| 182 | + match &self.inner { |
| 183 | + $module::Inner::Ni(aes) => aes.try_current_pos(), |
| 184 | + $module::Inner::Soft(aes) => aes.try_current_pos() |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + #[inline] |
| 189 | + fn try_seek<T: SeekNum>(&mut self, pos: T) -> Result<(), LoopError> { |
| 190 | + match &mut self.inner { |
| 191 | + $module::Inner::Ni(aes) => aes.try_seek(pos), |
| 192 | + $module::Inner::Soft(aes) => aes.try_seek(pos) |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + opaque_debug::implement!($name); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + define_aes_ctr_impl!(Aes128Ctr, Aes128, aes128ctr, "AES-128 in CTR mode"); |
| 202 | + define_aes_ctr_impl!(Aes192Ctr, Aes192, aes192ctr, "AES-192 in CTR mode"); |
| 203 | + define_aes_ctr_impl!(Aes256Ctr, Aes256, aes256ctr, "AES-256 in CTR mode"); |
| 204 | +} |
0 commit comments