feat: add tests
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
use aes::Aes128;
|
||||
use aes::{Aes128, Aes192, Aes256};
|
||||
use aes::cipher::{Block, BlockEncrypt, KeyInit};
|
||||
use aes::cipher::generic_array::GenericArray;
|
||||
|
||||
use crate::util::{gmul_128, inc_32, msb_s, normalize_nonce, u8to128};
|
||||
|
||||
pub struct Aes128GcmStreamEncryptor {
|
||||
crypto: Aes128,
|
||||
macro_rules! define_aes_gcm_stream_encryptor_impl {
|
||||
(
|
||||
$module:tt,
|
||||
$aesn:tt,
|
||||
$key_size:tt
|
||||
) => {
|
||||
|
||||
pub struct $module {
|
||||
crypto: $aesn,
|
||||
message_buffer: Vec<u8>,
|
||||
integrality_buffer: Vec<u8>,
|
||||
ghash_key: u128,
|
||||
@@ -16,10 +23,10 @@ pub struct Aes128GcmStreamEncryptor {
|
||||
message_len: usize,
|
||||
}
|
||||
|
||||
impl Aes128GcmStreamEncryptor {
|
||||
pub fn new(key: [u8; 16], nonce: &[u8]) -> Self {
|
||||
impl $module {
|
||||
pub fn new(key: [u8; $key_size], nonce: &[u8]) -> Self {
|
||||
let key = GenericArray::from(key);
|
||||
let aes = Aes128::new(&key);
|
||||
let aes = $aesn::new(&key);
|
||||
|
||||
let mut s = Self {
|
||||
crypto: aes,
|
||||
@@ -108,7 +115,7 @@ impl Aes128GcmStreamEncryptor {
|
||||
|
||||
fn calculate_tag(&mut self) -> Vec<u8> {
|
||||
let mut bs = self.init_nonce.to_be_bytes().clone();
|
||||
let block = Block::<Aes128>::from_mut_slice(&mut bs);
|
||||
let block = Block::<$aesn>::from_mut_slice(&mut bs);
|
||||
self.crypto.encrypt_block(block);
|
||||
let tag_trunk = self.ghash_val.to_be_bytes();
|
||||
let y = u8to128(&tag_trunk) ^ u8to128(&block.as_slice());
|
||||
@@ -130,7 +137,7 @@ impl Aes128GcmStreamEncryptor {
|
||||
|
||||
fn ghash_key(&mut self) -> u128 {
|
||||
let mut block = [0u8; 16];
|
||||
let block = Block::<Aes128>::from_mut_slice(&mut block);
|
||||
let block = Block::<$aesn>::from_mut_slice(&mut block);
|
||||
self.crypto.encrypt_block(block);
|
||||
u8to128(&block.as_slice())
|
||||
}
|
||||
@@ -139,4 +146,10 @@ impl Aes128GcmStreamEncryptor {
|
||||
let ghash_key = self.ghash_key();
|
||||
normalize_nonce(ghash_key, nonce_bytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
define_aes_gcm_stream_encryptor_impl!(Aes128GcmStreamEncryptor, Aes128, 16);
|
||||
define_aes_gcm_stream_encryptor_impl!(Aes192GcmStreamEncryptor, Aes192, 24);
|
||||
define_aes_gcm_stream_encryptor_impl!(Aes256GcmStreamEncryptor, Aes256, 32);
|
||||
|
||||
Reference in New Issue
Block a user