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: remove aesctr, add x25519 support #271

Merged
merged 1 commit into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions fuzz/fuzz_targets/secio/crypto/decrypt_cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,10 @@ fn new_decrypt_cipher(cipher_type: CipherType) -> BoxStreamCipher {
let key = (0..cipher_type.key_size())
.map(|_| rand::random::<u8>())
.collect::<Vec<_>>();
let iv = (0..cipher_type.iv_size())
.map(|_| rand::random::<u8>())
.collect::<Vec<_>>();
new_stream(cipher_type, &key, &iv, CryptoMode::Decrypt)
new_stream(cipher_type, &key, CryptoMode::Decrypt)
}

fuzz_target!(|data: &[u8]| {
let mut cipher = new_decrypt_cipher(CipherType::Aes128Ctr);
let _ = cipher.decrypt(data);

let mut cipher = new_decrypt_cipher(CipherType::Aes256Ctr);
let _ = cipher.decrypt(data);

let mut cipher = new_decrypt_cipher(CipherType::Aes128Gcm);
let _ = cipher.decrypt(data);

Expand Down
11 changes: 1 addition & 10 deletions fuzz/fuzz_targets/secio/crypto/encrypt_cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,10 @@ fn new_encrypt_cipher(cipher_type: CipherType) -> BoxStreamCipher {
let key = (0..cipher_type.key_size())
.map(|_| rand::random::<u8>())
.collect::<Vec<_>>();
let iv = (0..cipher_type.iv_size())
.map(|_| rand::random::<u8>())
.collect::<Vec<_>>();
new_stream(cipher_type, &key, &iv, CryptoMode::Encrypt)
new_stream(cipher_type, &key, CryptoMode::Encrypt)
}

fuzz_target!(|data: &[u8]| {
let mut cipher = new_encrypt_cipher(CipherType::Aes128Ctr);
let _ = cipher.encrypt(data);

let mut cipher = new_encrypt_cipher(CipherType::Aes256Ctr);
let _ = cipher.encrypt(data);

let mut cipher = new_encrypt_cipher(CipherType::Aes128Gcm);
let _ = cipher.encrypt(data);

Expand Down
2 changes: 1 addition & 1 deletion secio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ openssl-sys = "0.9"
[dev-dependencies]
env_logger = "0.6"
criterion = "0.3"
tokio = { version = "0.2.0", features = ["tcp", "rt-core"] }
tokio = { version = "0.2.0", features = ["tcp", "rt-core", "dns"] }

[features]
default = []
Expand Down
64 changes: 4 additions & 60 deletions secio/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,15 @@
use criterion::{criterion_group, criterion_main, Bencher, Criterion};
use tentacle_secio::{
codec::Hmac,
crypto::{cipher::CipherType, new_stream, CryptoMode},
};
use tentacle_secio::crypto::{cipher::CipherType, new_stream, CryptoMode};

fn decode_encode(data: &[u8], cipher: CipherType) {
let cipher_key = (0..cipher.key_size())
.map(|_| rand::random::<u8>())
.collect::<Vec<_>>();
let _hmac_key: [u8; 32] = rand::random();
let iv = (0..cipher.iv_size())
.map(|_| rand::random::<u8>())
.collect::<Vec<_>>();

let mut encode_cipher = new_stream(cipher, &cipher_key, &iv, CryptoMode::Encrypt);
let mut decode_cipher = new_stream(cipher, &cipher_key, &iv, CryptoMode::Decrypt);
let (mut decode_hmac, mut encode_hmac): (Option<Hmac>, Option<Hmac>) = match cipher {
CipherType::ChaCha20Poly1305 | CipherType::Aes128Gcm | CipherType::Aes256Gcm => {
(None, None)
}
#[cfg(unix)]
_ => {
use tentacle_secio::Digest;
let encode_hmac = Hmac::from_key(Digest::Sha256, &_hmac_key);
let decode_hmac = encode_hmac.clone();
(Some(decode_hmac), Some(encode_hmac))
}
};

let mut encode_data = encode_cipher.encrypt(&data[..]).unwrap();
if encode_hmac.is_some() {
let signature = encode_hmac.as_mut().unwrap().sign(&encode_data[..]);
encode_data.extend_from_slice(signature.as_ref());
}

if decode_hmac.is_some() {
let content_length = encode_data.len() - decode_hmac.as_mut().unwrap().num_bytes();
let mut encode_cipher = new_stream(cipher, &cipher_key, CryptoMode::Encrypt);
let mut decode_cipher = new_stream(cipher, &cipher_key, CryptoMode::Decrypt);

let (crypted_data, expected_hash) = encode_data.split_at(content_length);

assert!(decode_hmac
.as_mut()
.unwrap()
.verify(crypted_data, expected_hash));

encode_data.truncate(content_length);
}
let encode_data = encode_cipher.encrypt(&data[..]).unwrap();

let decode_data = decode_cipher.decrypt(&encode_data).unwrap();

Expand All @@ -62,16 +26,6 @@ fn criterion_benchmark(bench: &mut Criterion) {
let data = (0..1024 * 256)
.map(|_| rand::random::<u8>())
.collect::<Vec<_>>();
#[cfg(unix)]
bench.bench_function("1kb_aes128ctr", {
let data = data.clone();
move |b| bench_test(b, CipherType::Aes128Ctr, &data)
});
#[cfg(unix)]
bench.bench_function("1kb_aes256ctr", {
let data = data.clone();
move |b| bench_test(b, CipherType::Aes256Ctr, &data)
});
bench.bench_function("1kb_aes128gcm", {
let data = data.clone();
move |b| bench_test(b, CipherType::Aes128Gcm, &data)
Expand All @@ -87,16 +41,6 @@ fn criterion_benchmark(bench: &mut Criterion) {
let data = (0..1024 * 1024)
.map(|_| rand::random::<u8>())
.collect::<Vec<_>>();
#[cfg(unix)]
bench.bench_function("1mb_aes128ctr", {
let data = data.clone();
move |b| bench_test(b, CipherType::Aes128Ctr, &data)
});
#[cfg(unix)]
bench.bench_function("1mb_aes256ctr", {
let data = data.clone();
move |b| bench_test(b, CipherType::Aes256Ctr, &data)
});
bench.bench_function("1mb_aes128gcm", {
let data = data.clone();
move |b| bench_test(b, CipherType::Aes128Gcm, &data)
Expand Down
Loading