-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
122 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/bash | ||
|
||
HAS_ISSUES=0 | ||
FIRST_FILE=1 | ||
|
||
for file in $(git diff --name-only --staged); do | ||
FMT_RESULT="$(rustfmt --edition 2021 --color auto --files-with-diff $file 2>/dev/null || true)" | ||
if [ "$FMT_RESULT" != "" ]; then | ||
if [ $FIRST_FILE -eq 0 ]; then | ||
echo -n ", " | ||
fi | ||
echo -n "$file" | ||
HAS_ISSUES=1 | ||
FIRST_FILE=0 | ||
fi | ||
done | ||
|
||
if [ $HAS_ISSUES -eq 0 ]; then | ||
exit 0 | ||
fi | ||
|
||
echo ". Your code has formatting issues in files listed above. Format your code with \`make format\` or call rustfmt manually." | ||
exit 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
init: | ||
rustup component add rustfmt && git config core.hooksPath .githooks | ||
|
||
format: | ||
cargo fmt -- --color auto --files-with-diff --verbose |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,114 @@ | ||
|
||
|
||
//use ring::{aead, error}; | ||
|
||
macro_rules! test_aead | ||
{( $pkg:ident ) => | ||
{ | ||
mod $pkg { | ||
|
||
use $pkg::{aead, error}; | ||
|
||
use aead::{AES_128_GCM, Algorithm, NonceSequence, OpeningKey, UnboundKey, BoundKey, SealingKey, Nonce, Aad}; | ||
use error::Unspecified; | ||
macro_rules! test_aead { | ||
( $pkg:ident ) => { | ||
mod $pkg { | ||
|
||
use $pkg::{aead, error}; | ||
|
||
const AES_128_TEST_KEY: [u8; 16] = [12, 124, 200, 31, 226, 11, 135, 192, 12, 124, 200, 31, 226, 11, 135, 192 ]; | ||
const TEST_NONCE: [u8; aead::NONCE_LEN] = [ 12, 124, 200, 31, 226, 11, 135, 192, 12, 124, 200, 31 ]; | ||
const PLAINTEXT: &[u8] = "plaintext to be encrypted".as_bytes(); | ||
use aead::{ | ||
Aad, Algorithm, BoundKey, Nonce, NonceSequence, OpeningKey, SealingKey, UnboundKey, | ||
AES_128_GCM, | ||
}; | ||
use error::Unspecified; | ||
|
||
struct NotANonce(Vec<u8>); | ||
const AES_128_TEST_KEY: [u8; 16] = [ | ||
12, 124, 200, 31, 226, 11, 135, 192, 12, 124, 200, 31, 226, 11, 135, 192, | ||
]; | ||
const TEST_NONCE: [u8; aead::NONCE_LEN] = | ||
[12, 124, 200, 31, 226, 11, 135, 192, 12, 124, 200, 31]; | ||
const PLAINTEXT: &[u8] = "plaintext to be encrypted".as_bytes(); | ||
|
||
impl NotANonce { | ||
fn from(value: Vec<u8>) -> Self { | ||
NotANonce(value) | ||
} | ||
} | ||
struct NotANonce(Vec<u8>); | ||
|
||
impl NonceSequence for NotANonce { | ||
fn advance(&mut self) -> Result<Nonce, Unspecified> { | ||
let mut nonce = [0u8; aead::NONCE_LEN]; | ||
nonce.copy_from_slice(&self.0[0..aead::NONCE_LEN]); | ||
Ok(Nonce::assume_unique_for_key(nonce)) | ||
} | ||
} | ||
|
||
struct AeadConfig { | ||
algorithm: &'static Algorithm, | ||
key: Vec<u8>, | ||
nonce: Vec<u8>, | ||
aad: String | ||
} | ||
|
||
impl AeadConfig { | ||
fn new(algorithm: &'static Algorithm, key: &[u8], nonce: &[u8], aad: &str) -> AeadConfig { | ||
AeadConfig { | ||
algorithm: algorithm, | ||
key: Vec::from(key), | ||
nonce: Vec::from(nonce), | ||
aad: String::from(aad) | ||
impl NotANonce { | ||
fn from(value: Vec<u8>) -> Self { | ||
NotANonce(value) | ||
} | ||
} | ||
} | ||
|
||
fn key(&self) -> UnboundKey { | ||
UnboundKey::new(self.algorithm, &self.key).unwrap() | ||
} | ||
fn aad(&self) -> Aad<String> { | ||
Aad::from(self.aad.clone()) | ||
} | ||
fn nonce(&self) -> impl NonceSequence { | ||
//RngNonce{} | ||
//NotANonce::new() | ||
NotANonce::from( self.nonce.clone()) | ||
} | ||
} | ||
impl NonceSequence for NotANonce { | ||
fn advance(&mut self) -> Result<Nonce, Unspecified> { | ||
let mut nonce = [0u8; aead::NONCE_LEN]; | ||
nonce.copy_from_slice(&self.0[0..aead::NONCE_LEN]); | ||
Ok(Nonce::assume_unique_for_key(nonce)) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_aes_128_gcm() -> Result<(), String> { | ||
let config = AeadConfig::new(&AES_128_GCM, &AES_128_TEST_KEY, &TEST_NONCE, "test"); | ||
let mut in_out = Vec::from(PLAINTEXT); | ||
struct AeadConfig { | ||
algorithm: &'static Algorithm, | ||
key: Vec<u8>, | ||
nonce: Vec<u8>, | ||
aad: String, | ||
} | ||
|
||
test_aead(config, &mut in_out)?; | ||
impl AeadConfig { | ||
fn new( | ||
algorithm: &'static Algorithm, | ||
key: &[u8], | ||
nonce: &[u8], | ||
aad: &str, | ||
) -> AeadConfig { | ||
AeadConfig { | ||
algorithm: algorithm, | ||
key: Vec::from(key), | ||
nonce: Vec::from(nonce), | ||
aad: String::from(aad), | ||
} | ||
} | ||
|
||
fn key(&self) -> UnboundKey { | ||
UnboundKey::new(self.algorithm, &self.key).unwrap() | ||
} | ||
fn aad(&self) -> Aad<String> { | ||
Aad::from(self.aad.clone()) | ||
} | ||
fn nonce(&self) -> impl NonceSequence { | ||
//RngNonce{} | ||
//NotANonce::new() | ||
NotANonce::from(self.nonce.clone()) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_aes_128_gcm() -> Result<(), String> { | ||
let config = AeadConfig::new(&AES_128_GCM, &AES_128_TEST_KEY, &TEST_NONCE, "test"); | ||
let mut in_out = Vec::from(PLAINTEXT); | ||
|
||
Ok(()) | ||
} | ||
test_aead(config, &mut in_out)?; | ||
|
||
fn test_aead(config: AeadConfig, in_out: &mut Vec<u8>) -> Result<Vec<u8>, String> { | ||
let mut sealing_key = SealingKey::new(config.key(), config.nonce()); | ||
let mut opening_key = OpeningKey::new(config.key(), config.nonce()); | ||
Ok(()) | ||
} | ||
|
||
let plaintext = in_out.clone(); | ||
println!("Plaintext: {:?}", plaintext); | ||
fn test_aead(config: AeadConfig, in_out: &mut Vec<u8>) -> Result<Vec<u8>, String> { | ||
let mut sealing_key = SealingKey::new(config.key(), config.nonce()); | ||
let mut opening_key = OpeningKey::new(config.key(), config.nonce()); | ||
|
||
let tag = sealing_key.seal_in_place_separate_tag(config.aad(), in_out.as_mut_slice()).map_err(|x| x.to_string() )?; | ||
let cipher_text = in_out.clone(); | ||
println!("Ciphertext: {:?}", cipher_text); | ||
assert_ne!(plaintext, cipher_text); | ||
let plaintext = in_out.clone(); | ||
println!("Plaintext: {:?}", plaintext); | ||
|
||
in_out.extend(tag.as_ref()); | ||
let tag = sealing_key | ||
.seal_in_place_separate_tag(config.aad(), in_out.as_mut_slice()) | ||
.map_err(|x| x.to_string())?; | ||
let cipher_text = in_out.clone(); | ||
println!("Ciphertext: {:?}", cipher_text); | ||
assert_ne!(plaintext, cipher_text); | ||
|
||
let result_plaintext = opening_key.open_in_place(config.aad(), in_out).map_err(|x| x.to_string() )?; | ||
assert_eq!(plaintext, result_plaintext); | ||
in_out.extend(tag.as_ref()); | ||
|
||
println!("Roundtrip: {:?}", result_plaintext); | ||
let result_plaintext = opening_key | ||
.open_in_place(config.aad(), in_out) | ||
.map_err(|x| x.to_string())?; | ||
assert_eq!(plaintext, result_plaintext); | ||
|
||
println!("Roundtrip: {:?}", result_plaintext); | ||
|
||
Ok(Vec::from(result_plaintext)) | ||
} | ||
}}} | ||
Ok(Vec::from(result_plaintext)) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
mod test_aead { | ||
test_aead!(ring); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters