Skip to content
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

feat: Aes algorithm hardware acceleration #190

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion secio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ rand = "0.6"
ring = "0.16.5"
twofish = "0.2.0"
aes-ctr = "0.3.0"
aesni = { version = "0.6", default-features = false, features = ["nocheck"] }
ctr = "0.3.0"
unsigned-varint = "0.2.2"
bs58 = "0.2.0"

[dev-dependencies]
env_logger = "0.6"
criterion = "0.2"
criterion = "0.3"

[features]
default = []
Expand Down
56 changes: 43 additions & 13 deletions secio/src/stream_cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ use crate::codec::StreamCipher;
use aes_ctr::stream_cipher::generic_array::GenericArray;
use aes_ctr::stream_cipher::NewStreamCipher;
use aes_ctr::{Aes128Ctr, Aes256Ctr};
use aesni::{Aes128Ctr as NIAes128Ctr, Aes256Ctr as NIAes256Ctr};
use ctr::Ctr128;
use twofish::Twofish;

use std::sync::atomic;

static AES_NI: atomic::AtomicBool = atomic::AtomicBool::new(false);
static INIT: ::std::sync::Once = ::std::sync::Once::new();

/// Possible encryption ciphers.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Cipher {
Expand Down Expand Up @@ -39,18 +45,42 @@ impl Cipher {
/// Returns your stream cipher depending on `Cipher`.
#[inline]
pub fn ctr_init(key_size: Cipher, key: &[u8], iv: &[u8]) -> StreamCipher {
match key_size {
Cipher::Aes128 => Box::new(Aes128Ctr::new(
GenericArray::from_slice(key),
GenericArray::from_slice(iv),
)),
Cipher::Aes256 => Box::new(Aes256Ctr::new(
GenericArray::from_slice(key),
GenericArray::from_slice(iv),
)),
Cipher::TwofishCtr => Box::new(Ctr128::<Twofish>::new(
GenericArray::from_slice(key),
GenericArray::from_slice(iv),
)),
INIT.call_once(|| {
AES_NI.store(
is_x86_feature_detected!("aes") && is_x86_feature_detected!("sse3"),
atomic::Ordering::Relaxed,
);
});

if AES_NI.load(atomic::Ordering::Relaxed) {
match key_size {
Cipher::Aes128 => Box::new(NIAes128Ctr::new(
GenericArray::from_slice(key),
GenericArray::from_slice(iv),
)),
Cipher::Aes256 => Box::new(NIAes256Ctr::new(
GenericArray::from_slice(key),
GenericArray::from_slice(iv),
)),
Cipher::TwofishCtr => Box::new(Ctr128::<Twofish>::new(
GenericArray::from_slice(key),
GenericArray::from_slice(iv),
)),
}
} else {
match key_size {
Cipher::Aes128 => Box::new(Aes128Ctr::new(
GenericArray::from_slice(key),
GenericArray::from_slice(iv),
)),
Cipher::Aes256 => Box::new(Aes256Ctr::new(
GenericArray::from_slice(key),
GenericArray::from_slice(iv),
)),
Cipher::TwofishCtr => Box::new(Ctr128::<Twofish>::new(
GenericArray::from_slice(key),
GenericArray::from_slice(iv),
)),
}
}
}
4 changes: 2 additions & 2 deletions tests/test_kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ use tentacle::{
};

/// Get current used memory(bytes)
fn current_used_memory() -> Option<f32> {
fn current_used_memory() -> Option<f64> {
let sys = System::new();
match sys.memory() {
Ok(mem) => Some((mem.total.as_usize() - mem.free.as_usize()) as f32),
Ok(mem) => Some((mem.total.as_u64() - mem.free.as_u64()) as f64),
Err(_) => None,
}
}
Expand Down