-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aes: autodetection support for AES-NI
Adds an off-by-default `autodetect` feature which the `cpuid-bool` crate to detect whether AES-NI is available when compiling on i686/x86_64 architectures.
- Loading branch information
Showing
6 changed files
with
131 additions
and
1 deletion.
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,107 @@ | ||
//! 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, | ||
}; | ||
|
||
macro_rules! define_aes_impl { | ||
( | ||
$name:tt, | ||
$module:tt, | ||
$key_size:ty, | ||
$doc:expr | ||
) => { | ||
#[doc=$doc] | ||
#[derive(Clone)] | ||
pub struct $name($module::Inner); | ||
|
||
mod $module { | ||
#[derive(Clone)] | ||
pub(super) enum Inner { | ||
Ni(crate::ni::$name), | ||
Soft(crate::soft::$name), | ||
} | ||
} | ||
|
||
impl NewBlockCipher for $name { | ||
type KeySize = $key_size; | ||
|
||
#[inline] | ||
fn new(key: &GenericArray<u8, $key_size>) -> Self { | ||
if cpuid_bool::cpuid_bool!("aes") { | ||
$name($module::Inner::Ni(crate::ni::$name::new(key))) | ||
} else { | ||
$name($module::Inner::Soft(crate::soft::$name::new(key))) | ||
} | ||
} | ||
} | ||
|
||
impl BlockCipher for $name { | ||
type BlockSize = U16; | ||
type ParBlocks = U8; | ||
} | ||
|
||
impl BlockEncrypt for $name { | ||
#[inline] | ||
fn encrypt_block(&self, block: &mut Block) { | ||
match &self.0 { | ||
$module::Inner::Ni(aes) => aes.encrypt_block(block), | ||
$module::Inner::Soft(aes) => aes.encrypt_block(block), | ||
} | ||
} | ||
|
||
#[inline] | ||
fn encrypt_par_blocks(&self, blocks: &mut ParBlocks) { | ||
match &self.0 { | ||
$module::Inner::Ni(aes) => aes.encrypt_par_blocks(blocks), | ||
$module::Inner::Soft(aes) => aes.encrypt_par_blocks(blocks), | ||
} | ||
} | ||
} | ||
|
||
impl BlockDecrypt for $name { | ||
#[inline] | ||
fn decrypt_block(&self, block: &mut Block) { | ||
match &self.0 { | ||
$module::Inner::Ni(aes) => aes.decrypt_block(block), | ||
$module::Inner::Soft(aes) => aes.decrypt_block(block), | ||
} | ||
} | ||
|
||
#[inline] | ||
fn decrypt_par_blocks(&self, blocks: &mut ParBlocks) { | ||
match &self.0 { | ||
$module::Inner::Ni(aes) => aes.decrypt_par_blocks(blocks), | ||
$module::Inner::Soft(aes) => aes.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" | ||
); |
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