diff --git a/block-cipher-trait/src/lib.rs b/block-cipher-trait/src/lib.rs index c5399ebc..7c232de0 100644 --- a/block-cipher-trait/src/lib.rs +++ b/block-cipher-trait/src/lib.rs @@ -29,7 +29,7 @@ pub type Block = GenericArray::BlockSize>; /// Blocks being acted over in parallel. pub type ParBlocks = GenericArray, ::ParBlocks>; -/// Instantiate a `BlockCipher` algorithm. +/// Instantiate a [`BlockCipher`] algorithm. pub trait NewBlockCipher: Sized { /// Key size in bytes with which cipher guaranteed to be initialized. type KeySize: ArrayLength; diff --git a/crypto-mac/src/lib.rs b/crypto-mac/src/lib.rs index 4c04c3ad..fa348fed 100644 --- a/crypto-mac/src/lib.rs +++ b/crypto-mac/src/lib.rs @@ -20,16 +20,16 @@ use generic_array::typenum::Unsigned; use generic_array::{ArrayLength, GenericArray}; use subtle::{Choice, ConstantTimeEq}; -/// The [`Mac`] trait defines methods for a Message Authentication algorithm. -pub trait Mac: Clone { - /// Output size of the [[`Mac`]] - type OutputSize: ArrayLength; +/// Key for an algorithm that implements [`NewMac`]. +pub type Key = GenericArray::KeySize>; - /// Keys size of the [[`Mac`]] +/// Instantiate a [`Mac`] algorithm. +pub trait NewMac: Sized { + /// Key size in bytes with which cipher guaranteed to be initialized. type KeySize: ArrayLength; /// Initialize new MAC instance from key with fixed size. - fn new(key: &GenericArray) -> Self; + fn new(key: &Key) -> Self; /// Initialize new MAC instance from key with variable size. /// @@ -42,6 +42,12 @@ pub trait Mac: Clone { Ok(Self::new(GenericArray::from_slice(key))) } } +} + +/// The [`Mac`] trait defines methods for a Message Authentication algorithm. +pub trait Mac: Clone { + /// Output size of the [[`Mac`]] + type OutputSize: ArrayLength; /// Update MAC state with the given data. fn update(&mut self, data: &[u8]); @@ -51,11 +57,11 @@ pub trait Mac: Clone { /// Obtain the result of a [`Mac`] computation as a [`Output`] and consume /// [`Mac`] instance. - fn result(self) -> Output; + fn result(self) -> Output; /// Obtain the result of a [`Mac`] computation as a [`Output`] and reset /// [`Mac`] instance. - fn result_reset(&mut self) -> Output { + fn result_reset(&mut self) -> Output { let res = self.clone().result(); self.reset(); res @@ -75,16 +81,13 @@ pub trait Mac: Clone { /// [`Output`] is a thin wrapper around bytes array which provides a safe `Eq` /// implementation that runs in a fixed time. #[derive(Clone)] -pub struct Output> { - code: GenericArray, +pub struct Output { + code: GenericArray, } -impl Output -where - N: ArrayLength, -{ +impl Output { /// Create a new MAC [`Output`]. - pub fn new(code: GenericArray) -> Output { + pub fn new(code: GenericArray) -> Output { Output { code } } @@ -93,27 +96,21 @@ where /// Be very careful using this method, since incorrect use of the code value /// may permit timing attacks which defeat the security provided by the /// [`Mac`] trait. - pub fn into_bytes(self) -> GenericArray { + pub fn into_bytes(self) -> GenericArray { self.code } } -impl ConstantTimeEq for Output -where - N: ArrayLength, -{ +impl ConstantTimeEq for Output { fn ct_eq(&self, other: &Self) -> Choice { self.code.ct_eq(&other.code) } } -impl PartialEq for Output -where - N: ArrayLength, -{ - fn eq(&self, x: &Output) -> bool { +impl PartialEq for Output { + fn eq(&self, x: &Output) -> bool { self.ct_eq(x).unwrap_u8() == 1 } } -impl Eq for Output where N: ArrayLength {} +impl Eq for Output {}