-
Notifications
You must be signed in to change notification settings - Fork 135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
aes: autodetection support for AES-NI #208
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
//! Autodetection support for hardware accelerated AES backends with fallback | ||
//! to the fixsliced "soft" implementation. | ||
|
||
use crate::{Block, ParBlocks}; | ||
use cipher::{ | ||
consts::{U16, U24, U32, U8}, | ||
generic_array::GenericArray, | ||
BlockCipher, BlockDecrypt, BlockEncrypt, NewBlockCipher, | ||
}; | ||
|
||
cpuid_bool::new!(aes_cpuid, "aes"); | ||
|
||
macro_rules! define_aes_impl { | ||
( | ||
$name:tt, | ||
$module:tt, | ||
$key_size:ty, | ||
$doc:expr | ||
) => { | ||
#[doc=$doc] | ||
#[derive(Clone)] | ||
pub struct $name { | ||
inner: $module::Inner, | ||
token: aes_cpuid::InitToken | ||
} | ||
|
||
mod $module { | ||
#[derive(Copy, Clone)] | ||
pub(super) union Inner { | ||
pub(super) ni: crate::ni::$name, | ||
pub(super) soft: crate::soft::$name, | ||
} | ||
} | ||
|
||
impl NewBlockCipher for $name { | ||
type KeySize = $key_size; | ||
|
||
#[inline] | ||
fn new(key: &GenericArray<u8, $key_size>) -> Self { | ||
let (token, aesni_present) = aes_cpuid::init_get(); | ||
|
||
let inner = if aesni_present { | ||
$module::Inner { ni: crate::ni::$name::new(key) } | ||
} else { | ||
$module::Inner { soft: crate::soft::$name::new(key) } | ||
}; | ||
|
||
Self { inner, token } | ||
} | ||
} | ||
|
||
impl BlockCipher for $name { | ||
type BlockSize = U16; | ||
type ParBlocks = U8; | ||
} | ||
|
||
impl BlockEncrypt for $name { | ||
#[inline] | ||
fn encrypt_block(&self, block: &mut Block) { | ||
if self.token.get() { | ||
unsafe { self.inner.ni.encrypt_block(block) } | ||
} else { | ||
unsafe { self.inner.soft.encrypt_block(block) } | ||
} | ||
} | ||
|
||
#[inline] | ||
fn encrypt_par_blocks(&self, blocks: &mut ParBlocks) { | ||
if self.token.get() { | ||
unsafe { self.inner.ni.encrypt_par_blocks(blocks) } | ||
} else { | ||
unsafe { self.inner.soft.encrypt_par_blocks(blocks) } | ||
} | ||
} | ||
} | ||
|
||
impl BlockDecrypt for $name { | ||
#[inline] | ||
fn decrypt_block(&self, block: &mut Block) { | ||
if self.token.get() { | ||
unsafe { self.inner.ni.decrypt_block(block) } | ||
} else { | ||
unsafe { self.inner.soft.decrypt_block(block) } | ||
} | ||
} | ||
|
||
#[inline] | ||
fn decrypt_par_blocks(&self, blocks: &mut ParBlocks) { | ||
if self.token.get() { | ||
unsafe { self.inner.ni.decrypt_par_blocks(blocks) } | ||
} else { | ||
unsafe { self.inner.soft.decrypt_par_blocks(blocks) } | ||
} | ||
} | ||
} | ||
|
||
opaque_debug::implement!($name); | ||
} | ||
} | ||
|
||
define_aes_impl!(Aes128, aes128, U16, "AES-128 block cipher instance"); | ||
define_aes_impl!(Aes192, aes192, U24, "AES-192 block cipher instance"); | ||
define_aes_impl!(Aes256, aes256, U32, "AES-256 block cipher instance"); | ||
|
||
#[cfg(feature = "ctr")] | ||
pub(crate) mod ctr { | ||
use super::{aes_cpuid, Aes128, Aes192, Aes256}; | ||
use cipher::{ | ||
block::BlockCipher, | ||
generic_array::GenericArray, | ||
stream::{ | ||
FromBlockCipher, LoopError, OverflowError, SeekNum, SyncStreamCipher, | ||
SyncStreamCipherSeek, | ||
}, | ||
}; | ||
|
||
macro_rules! define_aes_ctr_impl { | ||
( | ||
$name:tt, | ||
$cipher:ident, | ||
$module:tt, | ||
$doc:expr | ||
) => { | ||
#[doc=$doc] | ||
#[cfg_attr(docsrs, doc(cfg(feature = "ctr")))] | ||
pub struct $name { | ||
inner: $module::Inner, | ||
} | ||
|
||
mod $module { | ||
#[allow(clippy::large_enum_variant)] | ||
pub(super) enum Inner { | ||
Ni(crate::ni::$name), | ||
Soft(crate::soft::$name), | ||
} | ||
} | ||
|
||
impl FromBlockCipher for $name { | ||
type BlockCipher = $cipher; | ||
type NonceSize = <$cipher as BlockCipher>::BlockSize; | ||
|
||
fn from_block_cipher( | ||
cipher: $cipher, | ||
nonce: &GenericArray<u8, Self::NonceSize>, | ||
) -> Self { | ||
let inner = if aes_cpuid::get() { | ||
$module::Inner::Ni( | ||
crate::ni::$name::from_block_cipher( | ||
unsafe { cipher.inner.ni }, | ||
nonce | ||
) | ||
) | ||
} else { | ||
$module::Inner::Soft( | ||
crate::soft::$name::from_block_cipher( | ||
unsafe { cipher.inner.soft }, | ||
nonce | ||
) | ||
) | ||
}; | ||
|
||
Self { inner } | ||
} | ||
} | ||
|
||
impl SyncStreamCipher for $name { | ||
#[inline] | ||
fn try_apply_keystream(&mut self, data: &mut [u8]) -> Result<(), LoopError> { | ||
match &mut self.inner { | ||
$module::Inner::Ni(aes) => aes.try_apply_keystream(data), | ||
$module::Inner::Soft(aes) => aes.try_apply_keystream(data) | ||
} | ||
} | ||
} | ||
|
||
impl SyncStreamCipherSeek for $name { | ||
#[inline] | ||
fn try_current_pos<T: SeekNum>(&self) -> Result<T, OverflowError> { | ||
match &self.inner { | ||
$module::Inner::Ni(aes) => aes.try_current_pos(), | ||
$module::Inner::Soft(aes) => aes.try_current_pos() | ||
} | ||
} | ||
|
||
#[inline] | ||
fn try_seek<T: SeekNum>(&mut self, pos: T) -> Result<(), LoopError> { | ||
match &mut self.inner { | ||
$module::Inner::Ni(aes) => aes.try_seek(pos), | ||
$module::Inner::Soft(aes) => aes.try_seek(pos) | ||
} | ||
} | ||
} | ||
|
||
opaque_debug::implement!($name); | ||
} | ||
} | ||
|
||
define_aes_ctr_impl!(Aes128Ctr, Aes128, aes128ctr, "AES-128 in CTR mode"); | ||
define_aes_ctr_impl!(Aes192Ctr, Aes192, aes192ctr, "AES-192 in CTR mode"); | ||
define_aes_ctr_impl!(Aes256Ctr, Aes256, aes256ctr, "AES-256 in CTR mode"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We either need to add
ssse3
to this list or use a separateaes_ssse3_cpuid
module as you did before. Also it may be worth to a comment that SSE2 is implied by AES-NI, so we don't need to check for it separately. Or we could simply addsse2
to the list to be completely thorough. :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per my comments above, as far as I can tell every CPU with AES-NI has both SSE2 and SSSE3.
AES-NI was introduced in the Westmere architecture, which also has SSSE3. Unless there's some strange AMD/other CPU that has AES-NI but not SSSE3, I don't think it will be a problem.
If you're worried though, I can add back
aes_ssse3_cpuid
(perhaps as a separate PR).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per reference we can omit
sse2
check if we have verified thataes
orssse3
is enabled, but it does not indicate any implicit dependency betweenaes
andssse3
. So even though in practice there is probably no such CPU, for our code to be correct we have to check both those features. The check is cheap enough and will be executed only once, so I think it's worth to be extra-safe.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm ok, sorry just merged but I can follow up with this.