Skip to content

Commit

Permalink
Add pre-commit git hook
Browse files Browse the repository at this point in the history
  • Loading branch information
justsmth committed Aug 11, 2022
1 parent 25518f5 commit fa32136
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 83 deletions.
23 changes: 23 additions & 0 deletions .githooks/pre-commit
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
5 changes: 5 additions & 0 deletions Makefile
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
6 changes: 4 additions & 2 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ extern crate alloc;

use alloc::{format, string::String, vec::Vec};

use crate::{error};
use crate::error;

#[cfg(any(feature = "std", feature = "test_logging"))]
extern crate std;
Expand Down Expand Up @@ -534,6 +534,8 @@ mod tests {
#[test]
#[should_panic(expected = "Syntax error: Expected Key = Value.")]
fn syntax_error() {
test::run(test_file!("test/test_1_syntax_error_tests.txt"), |_, _| Ok(()));
test::run(test_file!("test/test_1_syntax_error_tests.txt"), |_, _| {
Ok(())
});
}
}
169 changes: 90 additions & 79 deletions tests/aes_test.rs
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);
}
}
2 changes: 0 additions & 2 deletions tests/digest_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


// Copyright 2015-2017 Brian Smith.
//
// Permission to use, copy, modify, and/or distribute this software for any
Expand Down

0 comments on commit fa32136

Please sign in to comment.