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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use tink_core::{utils::wrap_err, Prf, TinkError};
const MIN_CMAC_KEY_SIZE_IN_BYTES: usize = 16;
const RECOMMENDED_CMAC_KEY_SIZE_IN_BYTES: usize = 32;
const MIN_TAG_LENGTH_IN_BYTES: usize = 10;
const MAX_TAG_LENGTH_IN_BYTES: usize = 16;
#[derive(Clone)]
pub struct AesCmac {
prf: tink_prf::subtle::AesCmacPrf,
tag_size: usize,
}
impl AesCmac {
pub fn new(key: &[u8], tag_size: usize) -> Result<AesCmac, TinkError> {
if key.len() < MIN_CMAC_KEY_SIZE_IN_BYTES {
return Err("AesCmac: Only 256 bit keys are allowed".into());
}
if tag_size < MIN_TAG_LENGTH_IN_BYTES {
return Err(format!(
"AesCmac: tag length {} is shorter than minimum tag length {}",
tag_size, MIN_TAG_LENGTH_IN_BYTES
)
.into());
}
if tag_size > MAX_TAG_LENGTH_IN_BYTES {
return Err(format!(
"AesCmac: tag length {} is longer than maximum tag length {}",
tag_size, MIN_TAG_LENGTH_IN_BYTES
)
.into());
}
let prf = tink_prf::subtle::AesCmacPrf::new(key)
.map_err(|e| wrap_err("AesCmac: could not create AES-CMAC prf", e))?;
Ok(AesCmac { prf, tag_size })
}
}
impl tink_core::Mac for AesCmac {
fn compute_mac(&self, data: &[u8]) -> Result<Vec<u8>, TinkError> {
self.prf.compute_prf(data, self.tag_size)
}
}
pub fn validate_cmac_params(key_size: usize, tag_size: usize) -> Result<(), TinkError> {
if key_size != RECOMMENDED_CMAC_KEY_SIZE_IN_BYTES {
return Err(format!(
"Only {} sized keys are allowed with Tink's AES-CMAC",
RECOMMENDED_CMAC_KEY_SIZE_IN_BYTES
)
.into());
}
if tag_size < MIN_TAG_LENGTH_IN_BYTES {
return Err("Tag size too short".into());
}
if tag_size > MAX_TAG_LENGTH_IN_BYTES {
return Err("Tag size too long".into());
}
Ok(())
}