Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Blake2b::new_keyed_salt_personal() #411

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions src/blake2b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,41 @@ impl Blake2b {
b
}

pub fn new_keyed_salt_personal(outlen: usize,
key: &[u8],
salt: &[u8],
personal: &[u8])
-> Blake2b {
assert!(outlen > 0 && outlen <= BLAKE2B_OUTBYTES);
assert!(key.len() > 0 && key.len() <= BLAKE2B_KEYBYTES);
assert!(salt.len() > 0 && salt.len() <= BLAKE2B_SALTBYTES);
assert!(personal.len() > 0 && personal.len() <= BLAKE2B_PERSONALBYTES);

let mut bp = [0; BLAKE2B_PERSONALBYTES];
bp[..personal.len()].clone_from_slice(&personal);

let mut bs = [0; BLAKE2B_SALTBYTES];
bs[..salt.len()].clone_from_slice(&salt);

let param = Blake2bParam {
digest_length: outlen as u8,
key_length: key.len() as u8,
fanout: 1,
depth: 1,
leaf_length: 0,
node_offset: 0,
node_depth: 0,
inner_length: 0,
reserved: [0; 14],
salt: bs,
personal: bp,
};

let mut b = Blake2b::init_param(param, key);
b.apply_key();
b
}

fn compress(&mut self) {
let mut ms: [u64; 16] = [0; 16];
let mut vs: [u64; 16] = [0; 16];
Expand Down Expand Up @@ -495,6 +530,77 @@ mod digest_tests {

test_hash(&tests[..]);
}

#[test]
fn test_blake2b_keyed_salt_personal() {
let expected: [[u8; 64]; 10] =
[[0xa0, 0xc7, 0x24, 0x40, 0x47, 0x28, 0xc8, 0xbb, 0x95, 0xe5, 0x43, 0x3e, 0xb6, 0xa9,
0x71, 0x61, 0x71, 0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 0xc7, 0x3c, 0xa9, 0x35, 0xb4],
[0x70, 0xf9, 0xb8, 0x3e, 0x46, 0x3f, 0xb4, 0x41, 0xe7, 0xa4, 0xc4, 0x32, 0x75, 0x12,
0x5c, 0xd5, 0xb1, 0x9d, 0x8e, 0x2e, 0x4a, 0x5d, 0x17, 0x9a, 0x39, 0xf5, 0xdb, 0x10,
0xbb, 0xce, 0x74, 0x5a, 0x19, 0x91, 0x04, 0x56, 0x3d, 0x30, 0x8c, 0xf8, 0xd4, 0xc6,
0xb2, 0x7b, 0xbb, 0x75, 0x9d, 0xed, 0x23, 0x2f, 0x5b, 0xdb, 0x7c, 0x36, 0x7d, 0xd6,
0x32, 0xa9, 0x67, 0x73, 0x20, 0xdf, 0xe4, 0x16]];

for i in 0..10 {
let mut sh =
Blake2b::new_keyed_salt_personal(64,
&[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
0x1c, 0x1d, 0x1e, 0x1f],
&[i, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
&"KDF test".as_bytes());
let mut out = [0u8; 64];
sh.result(&mut out);
assert_eq!(out.to_vec(), expected[i as usize].to_vec());
}
}
}


Expand Down