Add a simple test that makes sure both implementations match.

This commit is contained in:
2019-07-08 16:15:32 -05:00
parent e84f6a865e
commit 5b122b8c0c
3 changed files with 37 additions and 9 deletions

View File

@@ -12,7 +12,7 @@ use std::mem::uninitialized;
// //
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
struct AES128 { pub struct AES128 {
expanded_enc: [__m128i; 11], expanded_enc: [__m128i; 11],
expanded_dec: [__m128i; 11], expanded_dec: [__m128i; 11],
} }

View File

@@ -1,4 +1,32 @@
#[cfg(all(any(target_arch="x86", target_arch="x86_64"), #[cfg(all(any(target_arch="x86", target_arch="x86_64"),
target_feature = "aes"))] target_feature = "aes"))]
pub mod aesni; pub mod aesni;
pub mod portable; pub mod portable;
#[cfg(all(any(target_arch="x86", target_arch="x86_64"),
target_feature = "aes",
test))]
mod flexible {
use super::aesni;
use super::portable;
use super::portable::aes256::{RandomBlock,RandomKey};
quickcheck! {
fn aes128_implementations_match(key: RandomBlock, block: RandomBlock) -> bool {
let aesni_key = aesni::AES128::new(&key.block);
let portable_key = portable::AES128::new(&key.block);
let aesni_cipher = aesni_key.encrypt(&block.block);
let portable_cipher = portable_key.encrypt(&block.block);
aesni_cipher == portable_cipher
}
fn aes256_implementations_match(key: RandomKey, block: RandomBlock) -> bool {
let aesni_key = aesni::AES256::new(&key.key);
let portable_key = portable::AES256::new(&key.key);
let aesni_cipher = aesni_key.encrypt(&block.block);
let portable_cipher = portable_key.encrypt(&block.block);
aesni_cipher == portable_cipher
}
}
}

View File

@@ -380,7 +380,7 @@ const AES128_BLOCK_SIZE: usize = 4; // Nb
const AES128_NUM_ROUNDS: usize = 10; // Nr const AES128_NUM_ROUNDS: usize = 10; // Nr
const AES128_STATE_WORDS: usize = AES128_BLOCK_SIZE * (AES128_NUM_ROUNDS + 1); const AES128_STATE_WORDS: usize = AES128_BLOCK_SIZE * (AES128_NUM_ROUNDS + 1);
struct AES128 { pub struct AES128 {
expanded: [u32; AES128_STATE_WORDS] expanded: [u32; AES128_STATE_WORDS]
} }
@@ -591,7 +591,7 @@ const AES256_BLOCK_SIZE: usize = 4; // Nb
const AES256_NUM_ROUNDS: usize = 14; // Nr const AES256_NUM_ROUNDS: usize = 14; // Nr
const AES256_STATE_WORDS: usize = AES256_BLOCK_SIZE * (AES256_NUM_ROUNDS + 1); const AES256_STATE_WORDS: usize = AES256_BLOCK_SIZE * (AES256_NUM_ROUNDS + 1);
struct AES256 { pub struct AES256 {
expanded: [u32; AES256_STATE_WORDS] expanded: [u32; AES256_STATE_WORDS]
} }
@@ -694,7 +694,7 @@ impl AES256 {
} }
#[cfg(test)] #[cfg(test)]
mod aes256 { pub(crate) mod aes256 {
use quickcheck::{Arbitrary,Gen}; use quickcheck::{Arbitrary,Gen};
use super::*; use super::*;
use testing::run_test; use testing::run_test;
@@ -781,8 +781,8 @@ mod aes256 {
} }
#[derive(Clone,Debug)] #[derive(Clone,Debug)]
struct RandomKey { pub(crate) struct RandomKey {
key: [u8; 32] pub(crate) key: [u8; 32]
} }
impl Arbitrary for RandomKey { impl Arbitrary for RandomKey {
@@ -794,8 +794,8 @@ mod aes256 {
} }
#[derive(Clone,Debug)] #[derive(Clone,Debug)]
pub struct RandomBlock { pub(crate) struct RandomBlock {
pub block: [u8; 16] pub(crate) block: [u8; 16]
} }
impl Arbitrary for RandomBlock { impl Arbitrary for RandomBlock {