From 79c08436644b94ceb5f5ff3839f0d99de0a3fb5d Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Wed, 1 Jul 2020 23:33:50 +0300 Subject: [PATCH] Add dev module to the aead crate (#194) --- Cargo.lock | 17 ++++++++--- aead/CHANGELOG.md | 6 ++++ aead/Cargo.toml | 4 ++- aead/src/dev.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++++ aead/src/lib.rs | 4 +++ 5 files changed, 102 insertions(+), 6 deletions(-) create mode 100644 aead/src/dev.rs diff --git a/Cargo.lock b/Cargo.lock index f823e103a..432f8cda5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,8 +2,9 @@ # It is not intended for manual editing. [[package]] name = "aead" -version = "0.3.1" +version = "0.3.2" dependencies = [ + "blobby 0.3.0", "generic-array 0.14.2", "heapless", ] @@ -25,6 +26,12 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "517e75eae2ab6547f6a32ca5eefce861ecb5ec4c57a4c015e82503f71a7c63a9" +[[package]] +name = "blobby" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc52553543ecb104069b0ff9e0fcc5c739ad16202935528a112d974e8f1a4ee8" + [[package]] name = "block-buffer" version = "0.9.0" @@ -38,7 +45,7 @@ dependencies = [ name = "block-cipher" version = "0.7.1" dependencies = [ - "blobby", + "blobby 0.1.2", "generic-array 0.14.2", ] @@ -64,7 +71,7 @@ checksum = "6d375c433320f6c5057ae04a04376eef4d04ce2801448cf8863a78da99107be4" name = "crypto-mac" version = "0.8.0" dependencies = [ - "blobby", + "blobby 0.1.2", "generic-array 0.14.2", "subtle", ] @@ -86,7 +93,7 @@ dependencies = [ name = "digest" version = "0.9.0" dependencies = [ - "blobby", + "blobby 0.1.2", "generic-array 0.14.2", ] @@ -247,7 +254,7 @@ checksum = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" name = "stream-cipher" version = "0.4.1" dependencies = [ - "blobby", + "blobby 0.1.2", "block-cipher", "generic-array 0.14.2", ] diff --git a/aead/CHANGELOG.md b/aead/CHANGELOG.md index f250b2c3a..c10fb269d 100644 --- a/aead/CHANGELOG.md +++ b/aead/CHANGELOG.md @@ -5,6 +5,12 @@ 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.3.2 (2020-07-01) +### Added +- `dev` module ([#194]) + +[#194]: https://github.com/RustCrypto/traits/pull/194 + ## 0.3.1 (2020-06-12) ### Added - `NewAead::new_varkey` method ([#191]) diff --git a/aead/Cargo.toml b/aead/Cargo.toml index f653f3362..99b6fa646 100644 --- a/aead/Cargo.toml +++ b/aead/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "aead" -version = "0.3.1" +version = "0.3.2" authors = ["RustCrypto Developers"] edition = "2018" license = "MIT OR Apache-2.0" @@ -14,11 +14,13 @@ categories = ["cryptography", "no-std"] [dependencies] generic-array = { version = "0.14", default-features = false } heapless = { version = "0.5", optional = true } +blobby = { version = "0.3", optional = true } [features] default = ["alloc"] alloc = [] std = ["alloc"] +dev = ["blobby"] [package.metadata.docs.rs] all-features = true diff --git a/aead/src/dev.rs b/aead/src/dev.rs new file mode 100644 index 000000000..cfd531267 --- /dev/null +++ b/aead/src/dev.rs @@ -0,0 +1,77 @@ +//! Development-related functionality +pub use blobby; + +/// Define AEAD test +#[macro_export] +#[cfg_attr(docsrs, doc(cfg(feature = "dev")))] +macro_rules! new_test { + ($name:ident, $test_name:expr, $cipher:ty $(,)?) => { + #[test] + fn $name() { + use aead::dev::blobby::Blob6Iterator; + use aead::generic_array::typenum::Unsigned; + use aead::{generic_array::GenericArray, Aead, NewAead, Payload}; + use core::convert::TryInto; + + fn run_test( + key: &[u8], + nonce: &[u8], + aad: &[u8], + pt: &[u8], + ct: &[u8], + pass: bool, + ) -> Result<(), &'static str> { + let key = key.try_into().map_err(|_| "wrong key size")?; + let cipher = <$cipher>::new(key); + let nonce = nonce.try_into().map_err(|_| "wrong nonce size")?; + + if !pass { + let res = cipher.decrypt(nonce, Payload { aad: aad, msg: ct }); + if res.is_ok() { + return Err("decryption must return error"); + } + return Ok(()); + } + + let res = cipher + .encrypt(nonce, Payload { aad: aad, msg: pt }) + .map_err(|_| "encryption failure")?; + if res != ct { + return Err("encrypted data is different from target ciphertext"); + } + let res = cipher + .decrypt(nonce, Payload { aad: aad, msg: ct }) + .map_err(|_| "decryption failure")?; + if res != pt { + return Err("decrypted data is different from target plaintext"); + } + Ok(()) + } + + let data = include_bytes!(concat!("data/", $test_name, ".blb")); + for (i, row) in Blob6Iterator::new(data).unwrap().enumerate() { + let [key, nonce, aad, pt, ct, status] = row.unwrap(); + let pass = match status[0] { + 0 => false, + 1 => true, + _ => panic!("invalid value for pass flag"), + }; + if let Err(reason) = run_test(key, nonce, aad, pt, ct, pass) { + panic!( + "\n\ + Failed test №{}\n\ + reason: \t{:?}\n\ + key:\t{:?}\n\ + nonce:\t{:?}\n\ + aad:\t{:?}\n\ + plaintext:\t{:?}\n\ + ciphertext:\t{:?}\n\ + pass:\t{}\n\ + ", + i, reason, key, nonce, aad, pt, ct, pass, + ); + } + } + } + }; +} diff --git a/aead/src/lib.rs b/aead/src/lib.rs index d5c50d7c0..3f7ea4ca6 100644 --- a/aead/src/lib.rs +++ b/aead/src/lib.rs @@ -25,6 +25,10 @@ extern crate alloc; #[cfg(feature = "std")] extern crate std; +#[cfg(feature = "dev")] +#[cfg_attr(docsrs, doc(cfg(feature = "dev")))] +pub mod dev; + pub use generic_array::{self, typenum::consts}; #[cfg(feature = "heapless")]