Browse Source

Add authentication integration test

master v0.1.0
Hardcore Sushi 2 months ago
parent
commit
d818e52d45
Signed by: hardcoresushi GPG Key ID: 007F84120107191E
  1. 2
      src/crypto.rs
  2. 51
      tests/authentication.rs

2
src/crypto.rs

@ -60,7 +60,7 @@ pub struct EncryptionParams {
}
impl EncryptionParams {
fn get_params_len(&self) -> usize {
pub fn get_params_len(&self) -> usize {
SALT_LEN + 4*2 + 2 + self.cipher.get_nonce_size()
}

51
tests/authentication.rs

@ -0,0 +1,51 @@
use rand::Rng;
use doby::{
crypto::{
ArgonParams,
CipherAlgorithm,
EncryptionParams,
DobyCipher,
},
encrypt,
decrypt,
};
fn different_elements<T: Eq>(v1: &Vec<T>, v2: &Vec<T>) -> usize {
assert_eq!(v1.len(), v2.len());
v1.into_iter().enumerate().filter(|x| v2[x.0] != *x.1).count()
}
#[test]
fn authentication() {
const BLOCK_SIZE: usize = 65536;
const PLAINTEXT: &[u8; 13] = b"the plaintext";
const PASSWORD: &str = "the password";
let params = EncryptionParams::new(ArgonParams {
t_cost: 1,
m_cost: 8,
parallelism: 1,
}, CipherAlgorithm::AesCtr);
let encrypter = DobyCipher::new(PASSWORD.into(), &params).unwrap();
let mut ciphertext = Vec::with_capacity(PLAINTEXT.len()+158);
encrypt(&mut &PLAINTEXT[..], &mut ciphertext, &params, encrypter, BLOCK_SIZE, None).unwrap();
assert_eq!(ciphertext.len(), PLAINTEXT.len()+158);
for i in 0..ciphertext.len() {
let mut compromised = ciphertext.clone();
while compromised[i] == ciphertext[i] {
compromised[i] = rand::thread_rng().gen();
}
assert_eq!(different_elements(&compromised, &ciphertext), 1);
let decrypter = DobyCipher::new(PASSWORD.into(), &params).unwrap();
let mut decrypted = Vec::with_capacity(PLAINTEXT.len());
let verified = decrypt(&mut &compromised[..], &mut decrypted, decrypter, BLOCK_SIZE).unwrap();
assert_eq!(verified, false);
}
let decrypter = DobyCipher::new(PASSWORD.into(), &params).unwrap();
let mut decrypted = Vec::with_capacity(PLAINTEXT.len());
let verified = decrypt(&mut &ciphertext[4+params.get_params_len()..], &mut decrypted, decrypter, BLOCK_SIZE).unwrap();
assert_eq!(decrypted, PLAINTEXT);
assert_eq!(verified, true);
}
Loading…
Cancel
Save