Verified Commit 470b4118 authored by Katharina Fey's avatar Katharina Fey 🏴
Browse files

Improving testing & fixing bugs because of it :)

parent 7321c324
......@@ -2,9 +2,9 @@
#[macro_use]
extern crate serde_derive;
extern crate pbkdf2;
extern crate blake2;
extern crate hmac;
extern crate pbkdf2;
extern crate rand;
extern crate serde;
......@@ -85,10 +85,10 @@ impl PartialEq for Key {
};
let mut ret = true;
(0..len).for_each(move |i| unsafe {
(0..len).for_each(|i| unsafe {
if match self.tt {
Aes128 => self.key._32[i] != o.key._32[i],
Aes256 => self.key._32[i] != o.key._32[i],
Aes256 => self.key._64[i] != o.key._64[i],
} {
ret = false;
}
......
extern crate keybob;
extern crate miscreant;
mod derivation {
use keybob::{Key, KeyType};
#[test]
fn basic_128() {
let k1 = Key::from_pw(KeyType::Aes128, "123456789", "spacekookie");
let k2 = Key::from_pw(KeyType::Aes128, "123456789", "spacekookie");
assert_eq!(k1, k2);
}
#[test]
fn basic_256() {
let k1 = Key::from_pw(KeyType::Aes256, "123456789", "spacekookie");
let k2 = Key::from_pw(KeyType::Aes256, "123456789", "spacekookie");
assert_eq!(k1, k2);
}
#[test]
fn colliding_pw_128() {
let k1 = Key::from_pw(KeyType::Aes128, "123456789", "spacekookie");
let k2 = Key::from_pw(KeyType::Aes128, "123456789", "bob");
assert!(k1 != k2);
}
#[test]
fn colliding_pw_256() {
let k1 = Key::from_pw(KeyType::Aes256, "123456789", "spacekookie");
let k2 = Key::from_pw(KeyType::Aes256, "123456789", "bob");
assert!(k1 != k2);
}
}
mod workflow {
use keybob::{Key, KeyType};
use miscreant::aead::{Aes256Siv, Algorithm};
#[test]
fn miscreant() {
let key = Key::new(KeyType::Aes256);
let mut aes: Aes256Siv = Aes256Siv::new(key.as_slice());
/* Technically just random data, you can also use a "key" as iv and nonce */
let iv = Key::new(KeyType::Aes256);
let nonce = Key::new(KeyType::Aes256);
let data_in = "This is a message!";
let encrypted = aes.seal(nonce.as_slice(), iv.as_slice(), data_in.as_bytes());
let decrypted = aes
.open(nonce.as_slice(), iv.as_slice(), encrypted.as_slice())
.unwrap();
let data_out = ::std::str::from_utf8(&decrypted.as_slice()).unwrap();
assert_eq!(data_in, data_out);
}
}
//! Simply show how to derive a key from a user password/ name
extern crate keybob;
use keybob::{Key, KeyType};
fn main() {
let k = Key::from_pw(KeyType::Aes128, "123456789", "spacekookie");
println!("{:?}", k);
}
//! This example shows how to integrate with miscreant
//!
//! At the core is an Aes256 context which is created with a simple
//! Aes256 key. We generate a new one here, but you could very well
//! also create one from a user password.
extern crate keybob;
extern crate miscreant;
use keybob::{Key, KeyType};
use miscreant::aead::{Aes256Siv, Algorithm};
fn main() {
let key = Key::new(KeyType::Aes256);
let mut aes: Aes256Siv = Aes256Siv::new(key.as_slice());
/* Technically just random data, you can also use a "key" as iv and nonce */
let iv = Key::new(KeyType::Aes256);
let nonce = Key::new(KeyType::Aes256);
let data_in = "This is a message!";
let encrypted = aes.seal(nonce.as_slice(), iv.as_slice(), data_in.as_bytes());
let decrypted = aes
.open(nonce.as_slice(), iv.as_slice(), encrypted.as_slice())
.unwrap();
let data_out = ::std::str::from_utf8(&decrypted.as_slice()).unwrap();
assert_eq!(data_in, data_out);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment