Skip to content

Commit

Permalink
Add dev module to the aead crate (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov authored Jul 1, 2020
1 parent 8d00a12 commit 79c0843
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 6 deletions.
17 changes: 12 additions & 5 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions aead/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
4 changes: 3 additions & 1 deletion aead/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down
77 changes: 77 additions & 0 deletions aead/src/dev.rs
Original file line number Diff line number Diff line change
@@ -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,
);
}
}
}
};
}
4 changes: 4 additions & 0 deletions aead/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit 79c0843

Please sign in to comment.