Skip to content

Commit

Permalink
use Key in key::derive_phase_key()
Browse files Browse the repository at this point in the history
  • Loading branch information
warner committed May 26, 2018
1 parent 5fc4882 commit b1937fa
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions core/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ fn build_version_msg(
versions: &Value,
) -> (String, Vec<u8>) {
let phase = "version";
let data_key = derive_phase_key(side, key, &phase);
let data_key = derive_phase_key(side, &key, &phase);
let plaintext = versions.to_string();
let (_nonce, encrypted) = encrypt_data(data_key, &plaintext.as_bytes());
(phase.to_string(), encrypted)
Expand Down Expand Up @@ -195,7 +195,7 @@ pub fn derive_key(key: &[u8], purpose: &[u8], length: usize) -> Vec<u8> {
hk.expand(purpose, length)
}

pub fn derive_phase_key(side: &str, key: &[u8], phase: &str) -> Vec<u8> {
pub fn derive_phase_key(side: &str, key: &Key, phase: &str) -> Vec<u8> {
let side_bytes = side.as_bytes();
let phase_bytes = phase.as_bytes();
let side_digest: Vec<u8> = sha256_digest(side_bytes)
Expand All @@ -215,7 +215,7 @@ pub fn derive_phase_key(side: &str, key: &[u8], phase: &str) -> Vec<u8> {
purpose_vec.extend(phase_digest);

let length = sodiumoxide::crypto::secretbox::KEYBYTES;
derive_key(key, &purpose_vec, length)
derive_key(&key.to_vec(), &purpose_vec, length)
}

#[cfg(test)]
Expand Down Expand Up @@ -245,10 +245,10 @@ mod test {
// hexlified output: fe9315729668a6278a97449dc99a5f4c2102a668c6853338152906bb75526a96
let _k = KeyMachine::new("appid1", "side");

let key = "key".as_bytes();
let key = Key("key".as_bytes().to_vec());
let side = "side";
let phase = "phase1";
let phase1_key = derive_phase_key(side, key, phase);
let phase1_key = derive_phase_key(side, &key, phase);

assert_eq!(
hex::encode(phase1_key),
Expand All @@ -260,10 +260,10 @@ mod test {
fn test_encrypt_data_decrypt_data_roundtrip() {
use super::*;

let key = "key".as_bytes();
let key = Key("key".as_bytes().to_vec());
let side = "side";
let phase = "phase";
let data_key = derive_phase_key(side, key, phase);
let data_key = derive_phase_key(side, &key, phase);
let plaintext = "hello world";

let (_nonce, encrypted) =
Expand Down
2 changes: 1 addition & 1 deletion core/src/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl ReceiveMachine {
phase: &str,
body: Vec<u8>,
) -> Option<Vec<u8>> {
let data_key = key::derive_phase_key(&side, &key.to_vec(), &phase);
let data_key = key::derive_phase_key(&side, &key, &phase);

key::decrypt_data(data_key.clone(), &body)
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl SendMachine {
let mut es = Events::new();

for &(ref phase, ref plaintext) in &self.queue {
let data_key = key::derive_phase_key(&self.side, &key.to_vec(), phase);
let data_key = key::derive_phase_key(&self.side, &key, phase);
let (_nonce, encrypted) = key::encrypt_data(data_key, plaintext);
es.push(M_AddMessage(phase.to_string(), encrypted));
}
Expand All @@ -74,7 +74,7 @@ impl SendMachine {
phase: String,
plaintext: Vec<u8>,
) -> Events {
let data_key = key::derive_phase_key(&self.side, &key.to_vec(), &phase);
let data_key = key::derive_phase_key(&self.side, &key, &phase);
let (_nonce, encrypted) = key::encrypt_data(data_key, &plaintext);
events![M_AddMessage(phase, encrypted)]
}
Expand Down

0 comments on commit b1937fa

Please sign in to comment.