Convenience function for random AES keys

master
Valentin Ochs 2019-09-08 01:59:47 +02:00
parent f806cfec45
commit 5a53f1da6c
2 changed files with 13 additions and 20 deletions

View File

@ -1,4 +1,5 @@
use crate::bytes::Bytes;
use rand::Rng;
pub use openssl::symm::{Cipher, Crypter, Mode};
@ -21,6 +22,14 @@ impl Aes {
None
}
pub fn new_random_128(pad: bool) -> Aes {
let key: Vec<u8> = rand::thread_rng()
.sample_iter(rand::distributions::Standard)
.take(16)
.collect();
Self::new(&key, pad).unwrap()
}
pub fn cbc(&self, data: Vec<u8>, iv: &[u8], mode: Mode) -> Option<Vec<u8>> {
if (!self.pad) && (data.len() % Self::BLOCK_SIZE != 0) {
return None;
@ -106,7 +115,6 @@ impl Aes {
}
}
fn update(crypter: &mut Crypter, chunk: &[u8], output: &mut [u8]) {
assert!(
Self::BLOCK_SIZE

View File

@ -165,9 +165,8 @@ fn q11() {
let black_box = |mut data: Vec<u8>| -> (bool, Vec<u8>) {
let mut rng = rand::thread_rng();
let dist = rand::distributions::Standard;
let key: Vec<u8> = rng.sample_iter(&dist).take(16).collect();
let iv: Vec<u8> = rng.sample_iter(&dist).take(16).collect();
let aes = crypto::Aes::new(&key, true).unwrap();
let aes = crypto::Aes::new_random_128(true);
let mut start: Vec<u8> = rng.sample_iter(&dist).take(rng.gen_range(5, 11)).collect();
let mut end: Vec<u8> = rng.sample_iter(&dist).take(rng.gen_range(5, 11)).collect();
@ -191,12 +190,8 @@ fn q11() {
fn q12() {
println!("Running q12");
let key: Vec<u8> = rand::thread_rng()
.sample_iter(rand::distributions::Standard)
.take(16)
.collect();
let plaintext: Vec<u8> = Bytes::from_base64("Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK").into();
let aes = crate::crypto::Aes::new(&key, true).unwrap();
let aes = crate::crypto::Aes::new_random_128(true);
let blackbox = |mut data: Vec<u8>| -> Vec<u8> {
data.append(&mut plaintext.clone());
@ -252,11 +247,6 @@ fn q12() {
fn q13() {
println!("Running q13");
let key: Vec<u8> = rand::thread_rng()
.sample_iter(rand::distributions::Standard)
.take(16)
.collect();
let parse = |x: &str| -> std::collections::HashMap<String, String> {
let mut out = std::collections::HashMap::new();
for pair in x.split(|y| y == '&') {
@ -275,14 +265,13 @@ fn q13() {
)
};
let aes = crypto::Aes::new_random_128(true);
let encrypt = |profile: &str| -> Vec<u8> {
let profile: Vec<u8> = profile.bytes().collect();
let aes = crypto::Aes::new(&key, true).unwrap();
aes.ecb(profile, Mode::Encrypt).unwrap()
};
let is_admin = |profile: Vec<u8>| -> bool {
let aes = crypto::Aes::new(&key, true).unwrap();
let profile = aes.ecb(profile, Mode::Decrypt).unwrap();
let profile = String::from_utf8(profile).unwrap();
parse(&profile)["role"] == "admin"
@ -306,12 +295,8 @@ fn q13() {
fn q14() {
println!("Running q14");
let key: Vec<u8> = rand::thread_rng()
.sample_iter(rand::distributions::Standard)
.take(16)
.collect();
let plaintext: Vec<u8> = Bytes::from_base64("Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK").into();
let aes = crate::crypto::Aes::new(&key, true).unwrap();
let aes = crate::crypto::Aes::new_random_128(true);
let prefix: Vec<u8> = rand::thread_rng()
.sample_iter(rand::distributions::Standard)
.take(rand::thread_rng().gen_range(5, 11))