diff --git a/.github/workflows/crypto-mac.yml b/.github/workflows/crypto-mac.yml index 32333fc8b..490dc7cfa 100644 --- a/.github/workflows/crypto-mac.yml +++ b/.github/workflows/crypto-mac.yml @@ -51,6 +51,7 @@ jobs: toolchain: ${{ matrix.rust }} - run: cargo check --all-features - run: cargo test --release + - run: cargo test --features block-cipher --release - run: cargo test --features dev --release - run: cargo test --features std --release - run: cargo test --all-features --release diff --git a/Cargo.lock b/Cargo.lock index 181476f74..d80a03485 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -49,6 +49,15 @@ dependencies = [ "generic-array 0.14.4", ] +[[package]] +name = "block-cipher" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f337a3e6da609650eb74e02bc9fac7b735049f7623ab12f2e4c719316fcc7e80" +dependencies = [ + "generic-array 0.14.4", +] + [[package]] name = "byteorder" version = "1.3.4" @@ -69,15 +78,16 @@ checksum = "a2d9162b7289a46e86208d6af2c686ca5bfde445878c41a458a9fac706252d0b" [[package]] name = "cpuid-bool" -version = "0.1.0" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d375c433320f6c5057ae04a04376eef4d04ce2801448cf8863a78da99107be4" +checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634" [[package]] name = "crypto-mac" -version = "0.8.0" +version = "0.9.0" dependencies = [ - "blobby 0.2.0", + "blobby 0.3.0", + "block-cipher 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "generic-array 0.14.4", "subtle", ] @@ -87,7 +97,7 @@ name = "cryptography" version = "0.2.0" dependencies = [ "aead", - "block-cipher", + "block-cipher 0.8.0", "crypto-mac", "digest 0.9.0", "elliptic-curve", @@ -185,9 +195,9 @@ dependencies = [ [[package]] name = "hex-literal-impl" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d4c5c844e2fee0bf673d54c2c177f1713b3d2af2ff6e666b49cb7572e6cf42d" +checksum = "853f769599eb31de176303197b7ba4973299c38c7a7604a6bc88c3eef05b9b46" dependencies = [ "proc-macro-hack", ] @@ -200,24 +210,24 @@ checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" [[package]] name = "proc-macro-hack" -version = "0.5.15" +version = "0.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d659fe7c6d27f25e9d80a1a094c223f5246f6a6596453e09d7229bf42750b63" +checksum = "99c605b9a0adc77b7211c6b1f722dcb613d68d66859a44f3d485a6da332b0598" [[package]] name = "proc-macro2" -version = "1.0.12" +version = "1.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8872cf6f48eee44265156c111456a700ab3483686b3f96df4cf5481c89157319" +checksum = "04f5f085b5d71e2188cb8271e5da0161ad52c3f227a661a3c135fdf28e258b12" dependencies = [ "unicode-xid", ] [[package]] name = "quote" -version = "1.0.4" +version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c1f4b0efa5fc5e8ceb705136bfee52cfdb6a4e3509f770b478cd6ed434232a7" +checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" dependencies = [ "proc-macro2", ] @@ -264,16 +274,16 @@ dependencies = [ [[package]] name = "stable_deref_trait" -version = "1.1.1" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" [[package]] name = "stream-cipher" version = "0.6.0" dependencies = [ "blobby 0.3.0", - "block-cipher", + "block-cipher 0.8.0", "generic-array 0.14.4", ] @@ -285,9 +295,9 @@ checksum = "502d53007c02d7605a05df1c1a73ee436952781653da5d0bf57ad608f66932c1" [[package]] name = "syn" -version = "1.0.19" +version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8e5aa70697bb26ee62214ae3288465ecec0000f05182f039b477001f08f5ae7" +checksum = "e69abc24912995b3038597a7a593be5053eb0fb44f3cc5beec0deb421790c1f4" dependencies = [ "proc-macro2", "quote", @@ -296,9 +306,9 @@ dependencies = [ [[package]] name = "synstructure" -version = "0.12.3" +version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67656ea1dc1b41b1451851562ea232ec2e5a80242139f7e679ceccfb5d61f545" +checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" dependencies = [ "proc-macro2", "quote", @@ -314,9 +324,9 @@ checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" [[package]] name = "unicode-xid" -version = "0.2.0" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" [[package]] name = "universal-hash" diff --git a/crypto-mac/CHANGELOG.md b/crypto-mac/CHANGELOG.md index b0eb5094b..867676934 100644 --- a/crypto-mac/CHANGELOG.md +++ b/crypto-mac/CHANGELOG.md @@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 0.9.0 (2020-08-10) +### Added +- `FromBlockCipher` trait and blanket implementation of the `NewMac` trait +for it ([#217]) + +### Changed +- Updated test vectors storage to `blobby v0.3` ([#217]) + +### Removed +- `impl_write!` macro ([#217]) + +[#217]: https://github.com/RustCrypto/traits/pull/217 + ## 0.8.0 (2020-06-04) ### Added - `impl_write!` macro ([#134]) diff --git a/crypto-mac/Cargo.toml b/crypto-mac/Cargo.toml index ff5c9313d..b8350c22e 100644 --- a/crypto-mac/Cargo.toml +++ b/crypto-mac/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "crypto-mac" description = "Trait for Message Authentication Code (MAC) algorithms" -version = "0.8.0" +version = "0.9.0" authors = ["RustCrypto Developers"] license = "MIT OR Apache-2.0" readme = "README.md" @@ -13,8 +13,9 @@ categories = ["cryptography", "no-std"] [dependencies] generic-array = "0.14" +block-cipher = { version = "0.8", optional = true } subtle = { version = "2", default-features = false } -blobby = { version = "0.2", optional = true } +blobby = { version = "0.3", optional = true } [features] dev = ["blobby"] diff --git a/crypto-mac/src/dev.rs b/crypto-mac/src/dev.rs index f338cab74..e2348501e 100644 --- a/crypto-mac/src/dev.rs +++ b/crypto-mac/src/dev.rs @@ -39,9 +39,7 @@ macro_rules! new_test { let data = include_bytes!(concat!("data/", $test_name, ".blb")); for (i, row) in Blob3Iterator::new(data).unwrap().enumerate() { - let key = row[0]; - let input = row[1]; - let tag = row[2]; + let [key, input, tag] = row.unwrap(); if let Some(desc) = run_test(key, input, tag) { panic!( "\n\ diff --git a/crypto-mac/src/lib.rs b/crypto-mac/src/lib.rs index 8b4768d6a..6a44060ab 100644 --- a/crypto-mac/src/lib.rs +++ b/crypto-mac/src/lib.rs @@ -9,6 +9,9 @@ #[cfg(feature = "std")] extern crate std; +#[cfg(feature = "block-cipher")] +use block_cipher::{BlockCipher, NewBlockCipher}; + #[cfg(feature = "dev")] #[cfg_attr(docsrs, doc(cfg(feature = "dev")))] pub mod dev; @@ -118,20 +121,34 @@ impl PartialEq for Output { impl Eq for Output {} -#[macro_export] -/// Implements `std::io::Write` trait for implementer of [`Mac`] -macro_rules! impl_write { - ($mac:ident) => { - #[cfg(feature = "std")] - impl std::io::Write for $mac { - fn write(&mut self, buf: &[u8]) -> std::io::Result { - Mac::update(self, buf); - Ok(buf.len()) - } - - fn flush(&mut self) -> std::io::Result<()> { - Ok(()) - } - } - }; +#[cfg(feature = "block-cipher")] +#[cfg_attr(docsrs, doc(cfg(feature = "block-cipher")))] +/// Trait for MAC functions which can be created from block cipher. +pub trait FromBlockCipher { + /// Block cipher type + type Cipher: BlockCipher; + + /// Create new MAC isntance from provided block cipher. + fn from_cipher(cipher: Self::Cipher) -> Self; +} + +#[cfg(feature = "block-cipher")] +#[cfg_attr(docsrs, doc(cfg(feature = "block-cipher")))] +impl NewMac for T +where + T: FromBlockCipher, + T::Cipher: NewBlockCipher, +{ + type KeySize = <::Cipher as NewBlockCipher>::KeySize; + + fn new(key: &Key) -> Self { + let cipher = ::Cipher::new(key); + Self::from_cipher(cipher) + } + + fn new_varkey(key: &[u8]) -> Result { + ::Cipher::new_varkey(key) + .map_err(|_| InvalidKeyLength) + .map(Self::from_cipher) + } } diff --git a/cryptography/Cargo.toml b/cryptography/Cargo.toml index 38b0afe16..a0895e936 100644 --- a/cryptography/Cargo.toml +++ b/cryptography/Cargo.toml @@ -16,7 +16,7 @@ aead = { version = "0.3", optional = true, path = "../aead" } block-cipher = { version = "0.8", optional = true, path = "../block-cipher" } digest = { version = "0.9", optional = true, path = "../digest" } elliptic-curve = { version = "= 0.5.0-pre", optional = true, path = "../elliptic-curve" } -mac = { version = "0.8", package = "crypto-mac", optional = true, path = "../crypto-mac" } +mac = { version = "0.9", package = "crypto-mac", optional = true, path = "../crypto-mac" } signature = { version = "1.1.0", optional = true, default-features = false, path = "../signature" } stream-cipher = { version = "0.6", optional = true, path = "../stream-cipher" } universal-hash = { version = "0.4", optional = true, path = "../universal-hash" }