-
Notifications
You must be signed in to change notification settings - Fork 315
Expand file tree
/
Copy pathmac.rs
More file actions
46 lines (39 loc) · 1.44 KB
/
mac.rs
File metadata and controls
46 lines (39 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#[cfg(not(feature = "reset"))]
use digest::new_mac_test as new_test;
#[cfg(feature = "reset")]
use digest::new_resettable_mac_test as new_test;
new_test!(blake2b_mac, "blake2b/mac", blake2::Blake2bMac512);
new_test!(blake2s_mac, "blake2s/mac", blake2::Blake2sMac256);
#[test]
fn blake2b_new_test() {
use blake2::digest::{array::Array, KeyInit, Mac};
fn run<T: Mac + KeyInit>(key: &[u8]) {
const DATA: &[u8] = &[42; 300];
let res1 = T::new(&Array::try_from(key).unwrap())
.chain_update(DATA)
.finalize()
.into_bytes();
let res2 = T::new_from_slice(key)
.unwrap()
.chain_update(DATA)
.finalize()
.into_bytes();
assert_eq!(res1, res2);
}
run::<blake2::Blake2sMac256>(&[0x42; 32]);
run::<blake2::Blake2bMac512>(&[0x42; 64]);
}
#[test]
fn mac_refuses_empty_keys() {
assert!(blake2::Blake2bMac512::new_with_salt_and_personal(&[], b"salt", b"persona").is_err());
assert!(blake2::Blake2sMac256::new_with_salt_and_personal(&[], b"salt", b"persona").is_err());
}
#[test]
fn blake2b_with_key_equivalence() {
use blake2::digest::FixedOutput;
let key = b"my_key";
// Those two calls are equivalent.
let ctx1 = blake2::Blake2bMac512::new_with_salt_and_personal(key, &[], &[]).unwrap();
let ctx2 = blake2::Blake2bMac512::new_with_key(key).unwrap();
assert_eq!(ctx1.finalize_fixed(), ctx2.finalize_fixed(),);
}