Implement basic hkdf hashing logic

This commit is contained in:
timvisee 2018-03-05 14:33:03 +01:00
parent a79eabe537
commit 5858cd01db
No known key found for this signature in database
GPG key ID: 109CBA0BF74036C2
2 changed files with 82 additions and 1 deletions

View file

@ -4,3 +4,6 @@ version = "0.1.0"
authors = ["timvisee <timvisee@gmail.com>"]
[dependencies]
rand = "0.4"
reqwest = "0.8"
rust-crypto = "0.2"

View file

@ -1,3 +1,81 @@
extern crate crypto;
extern crate rand;
extern crate reqwest;
//use bytes::BytesMut;
use crypto::digest::Digest;
use crypto::hkdf::{hkdf_extract, hkdf_expand};
use crypto::sha2::Sha256;
use rand::{Rng, thread_rng};
fn main() {
println!("Hello, world!");
// TODO: a fixed path for now, as upload test
let path = "/home/timvisee/Pictures/Avatar/1024x1024/Avatar.png";
// Create a new reqwest client
let client = reqwest::Client::new();
// Generate a secret and iv
let mut secret = [0u8; 16];
let mut iv = [0u8; 12];
thread_rng().fill_bytes(&mut secret);
thread_rng().fill_bytes(&mut iv);
// Derive keys
let encryptKey = derive_file_key(&secret);
let authKey = derive_auth_key(&secret, None, None);
let metaKey = derive_meta_key(&secret);
let form = reqwest::multipart::Form::new()
.file("data", path)
.unwrap();
let mut res = client.post("http://localhost:8080/api/upload")
.multipart(form)
.send()
.unwrap();
let text = res.text().unwrap();
// TODO: remove after debugging
println!("TEXT: {}", text);
}
fn derive_file_key(secret: &[u8]) -> Vec<u8> {
hkdf(16, secret, None, Some(b"encryption"))
}
fn derive_auth_key(secret: &[u8], password: Option<String>, url: Option<String>) -> Vec<u8> {
if password.is_none() {
hkdf(64, secret, None, Some(b"authentication"))
} else {
// TODO: implement this
unimplemented!();
}
}
fn derive_meta_key(secret: &[u8]) -> Vec<u8> {
hkdf(16, secret, None, Some(b"metadata"))
}
fn hkdf<'a>(
length: usize,
ikm: &[u8],
salt: Option<&[u8]>,
info: Option<&[u8]>
) -> Vec<u8> {
// Get the salt and info parameters, use defaults if undefined
let salt = salt.unwrap_or(b"");
let info = info.unwrap_or(b"");
// Define the digest to use
let mut digest = Sha256::new();
let mut pkr: Vec<u8> = vec![0u8; digest.output_bytes()];
hkdf_extract(digest, salt, ikm, &mut pkr);
let mut okm: Vec<u8> = vec![0u8; length];
hkdf_expand(digest, &pkr, info, &mut okm);
okm
}