Skip to content
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
70 changes: 14 additions & 56 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions eax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ rust-version = "1.56"

[dependencies]
aead = { version = "0.5", default-features = false }
cipher = "0.3"
cmac = "0.6"
ctr = "0.8"
cipher = "0.4"
cmac = "0.7"
ctr = "0.9"
subtle = { version = "2", default-features = false }

[dev-dependencies]
aead = { version = "0.5", features = ["dev"], default-features = false }
aes = "0.7"
aes = "0.8"

[features]
default = ["alloc", "getrandom"]
Expand Down
48 changes: 23 additions & 25 deletions eax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,16 @@
#![deny(unsafe_code)]
#![warn(missing_docs, rust_2018_idioms)]

pub use aead::{self, AeadCore, AeadInPlace, Error, KeyInit, KeySizeUser};
pub use aead::{self, AeadCore, AeadInPlace, Error, Key, KeyInit, KeySizeUser};
pub use cipher;

use cipher::{
consts::{U0, U16},
generic_array::{functional::FunctionalSequence, ArrayLength, GenericArray},
Block, BlockCipher, BlockCipherKey, BlockEncrypt, FromBlockCipher, NewBlockCipher,
StreamCipher,
crypto_common::OutputSizeUser,
generic_array::{functional::FunctionalSequence, GenericArray},
BlockCipher, BlockEncrypt, InnerIvInit, StreamCipherCore,
};
use cmac::{crypto_mac::NewMac, Cmac, Mac};
use cmac::{Cmac, Mac};
use core::marker::PhantomData;

mod traits;
Expand All @@ -151,6 +151,9 @@ pub type Tag<TagSize> = GenericArray<u8, TagSize>;

pub mod online;

/// Counter mode with a 128-bit big endian counter.
type Ctr128BE<C> = ctr::CtrCore<C, ctr::flavors::Ctr128BE>;

/// EAX: generic over an underlying block cipher implementation.
///
/// This type is generic to support substituting alternative cipher
Expand All @@ -164,31 +167,28 @@ pub mod online;
#[derive(Clone)]
pub struct Eax<Cipher, M = U16>
where
Cipher: BlockCipher<BlockSize = U16> + BlockEncrypt + NewBlockCipher + Clone,
Cipher::ParBlocks: ArrayLength<Block<Cipher>>,
Cipher: BlockCipher<BlockSize = U16> + BlockEncrypt + Clone + KeyInit,
M: TagSize,
{
/// Encryption key
key: BlockCipherKey<Cipher>,
key: Key<Cipher>,
_tag_size: PhantomData<M>,
}

impl<Cipher, M> KeySizeUser for Eax<Cipher, M>
where
Cipher: BlockCipher<BlockSize = U16> + BlockEncrypt + NewBlockCipher + Clone,
Cipher::ParBlocks: ArrayLength<Block<Cipher>>,
Cipher: BlockCipher<BlockSize = U16> + BlockEncrypt + Clone + KeyInit,
M: TagSize,
{
type KeySize = Cipher::KeySize;
}

impl<Cipher, M> KeyInit for Eax<Cipher, M>
where
Cipher: BlockCipher<BlockSize = U16> + BlockEncrypt + NewBlockCipher + Clone,
Cipher::ParBlocks: ArrayLength<Block<Cipher>>,
Cipher: BlockCipher<BlockSize = U16> + BlockEncrypt + Clone + KeyInit,
M: TagSize,
{
fn new(key: &BlockCipherKey<Cipher>) -> Self {
fn new(key: &Key<Cipher>) -> Self {
Self {
key: key.clone(),
_tag_size: PhantomData,
Expand All @@ -198,8 +198,7 @@ where

impl<Cipher, M> AeadCore for Eax<Cipher, M>
where
Cipher: BlockCipher<BlockSize = U16> + BlockEncrypt + NewBlockCipher + Clone,
Cipher::ParBlocks: ArrayLength<Block<Cipher>>,
Cipher: BlockCipher<BlockSize = U16> + BlockEncrypt + Clone + KeyInit,
M: TagSize,
{
type NonceSize = Cipher::BlockSize;
Expand All @@ -209,8 +208,7 @@ where

impl<Cipher, M> AeadInPlace for Eax<Cipher, M>
where
Cipher: BlockCipher<BlockSize = U16> + BlockEncrypt + NewBlockCipher + Clone,
Cipher::ParBlocks: ArrayLength<Block<Cipher>>,
Cipher: BlockCipher<BlockSize = U16> + BlockEncrypt + Clone + KeyInit,
M: TagSize,
{
fn encrypt_in_place_detached(
Expand All @@ -235,8 +233,8 @@ where
let h = Self::cmac_with_iv(&self.key, 1, associated_data);

// 3. enc ← CTR(M) using n as iv
let mut cipher = ctr::Ctr128BE::<Cipher>::from_block_cipher(Cipher::new(&self.key), &n);
cipher.apply_keystream(buffer);
Ctr128BE::<Cipher>::inner_iv_init(Cipher::new(&self.key), &n)
.apply_keystream_partial(buffer.into());

// 4. c ← OMAC(2 || enc)
let c = Self::cmac_with_iv(&self.key, 2, buffer);
Expand Down Expand Up @@ -278,8 +276,9 @@ where
use subtle::ConstantTimeEq;
if expected_tag.ct_eq(tag).into() {
// Decrypt
let mut cipher = ctr::Ctr128BE::<Cipher>::from_block_cipher(Cipher::new(&self.key), &n);
cipher.apply_keystream(buffer);
Ctr128BE::<Cipher>::inner_iv_init(Cipher::new(&self.key), &n)
.apply_keystream_partial(buffer.into());

Ok(())
} else {
Err(Error)
Expand All @@ -289,8 +288,7 @@ where

impl<Cipher, M> Eax<Cipher, M>
where
Cipher: BlockCipher<BlockSize = U16> + BlockEncrypt + NewBlockCipher + Clone,
Cipher::ParBlocks: ArrayLength<Block<Cipher>>,
Cipher: BlockCipher<BlockSize = U16> + BlockEncrypt + Clone + KeyInit,
M: TagSize,
{
/// CMAC/OMAC1
Expand All @@ -301,8 +299,8 @@ where
key: &GenericArray<u8, Cipher::KeySize>,
iv: u8,
data: &[u8],
) -> GenericArray<u8, <Cmac<Cipher> as Mac>::OutputSize> {
let mut mac = Cmac::<Cipher>::new(key);
) -> GenericArray<u8, <Cmac<Cipher> as OutputSizeUser>::OutputSize> {
let mut mac = <Cmac<Cipher> as Mac>::new(key);
mac.update(&[0; 15]);
mac.update(&[iv]);
mac.update(data);
Expand Down
Loading