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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use prost::Message;
use tink_core::{utils::wrap_err, TinkError};
use tink_proto::HashType;
pub const HMAC_KEY_VERSION: u32 = 0;
pub const HMAC_TYPE_URL: &str = "type.googleapis.com/google.crypto.tink.HmacKey";
#[derive(Default)]
pub(crate) struct HmacKeyManager;
impl tink_core::registry::KeyManager for HmacKeyManager {
fn primitive(&self, serialized_key: &[u8]) -> Result<tink_core::Primitive, TinkError> {
if serialized_key.is_empty() {
return Err("HmacKeyManager: invalid key".into());
}
let key = tink_proto::HmacKey::decode(serialized_key)
.map_err(|e| wrap_err("HmacKeyManager: decode failed", e))?;
validate_key(&key)?;
let params = match &key.params {
None => return Err("HmacKeyManager: no key params".into()),
Some(p) => p,
};
let hash = HashType::from_i32(params.hash).unwrap_or(HashType::UnknownHash);
match crate::subtle::Hmac::new(hash, &key.key_value, params.tag_size as usize) {
Ok(p) => Ok(tink_core::Primitive::Mac(Box::new(p))),
Err(e) => Err(wrap_err("HmacKeyManager: cannot create new primitive", e)),
}
}
fn new_key(&self, serialized_key_format: &[u8]) -> Result<Vec<u8>, TinkError> {
if serialized_key_format.is_empty() {
return Err("HmacKeyManager: invalid key format".into());
}
let key_format = tink_proto::HmacKeyFormat::decode(serialized_key_format)
.map_err(|_| TinkError::new("HmacKeyManager: invalid key format"))?;
validate_key_format(&key_format)
.map_err(|e| wrap_err("HmacKeyManager: invalid key format", e))?;
let key_value = tink_core::subtle::random::get_random_bytes(key_format.key_size as usize);
let mut sk = Vec::new();
tink_proto::HmacKey {
version: HMAC_KEY_VERSION,
params: key_format.params,
key_value,
}
.encode(&mut sk)
.map_err(|e| wrap_err("HmacKeyManager: failed to encode new key", e))?;
Ok(sk)
}
fn type_url(&self) -> &'static str {
HMAC_TYPE_URL
}
fn key_material_type(&self) -> tink_proto::key_data::KeyMaterialType {
tink_proto::key_data::KeyMaterialType::Symmetric
}
}
fn validate_key(key: &tink_proto::HmacKey) -> Result<(), TinkError> {
tink_core::keyset::validate_key_version(key.version, HMAC_KEY_VERSION)
.map_err(|e| wrap_err("HmacKeyManager: invalid version", e))?;
let key_size = key.key_value.len();
match &key.params {
None => Err("HmacKeyManager: missing HMAC params".into()),
Some(params) => {
let hash = HashType::from_i32(params.hash).unwrap_or(HashType::UnknownHash);
crate::subtle::validate_hmac_params(hash, key_size, params.tag_size as usize)
}
}
}
fn validate_key_format(format: &tink_proto::HmacKeyFormat) -> Result<(), TinkError> {
match &format.params {
None => Err("missing HMAC params".into()),
Some(params) => {
let hash = HashType::from_i32(params.hash).unwrap_or(HashType::UnknownHash);
crate::subtle::validate_hmac_params(
hash,
format.key_size as usize,
params.tag_size as usize,
)
}
}
}