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
use prost::Message;
use tink_proto::KeyTemplate;
pub fn hmac_sha256_tag128_key_template() -> KeyTemplate {
create_hmac_key_template(32, 16, tink_proto::HashType::Sha256)
}
pub fn hmac_sha256_tag256_key_template() -> KeyTemplate {
create_hmac_key_template(32, 32, tink_proto::HashType::Sha256)
}
pub fn hmac_sha512_tag256_key_template() -> KeyTemplate {
create_hmac_key_template(64, 32, tink_proto::HashType::Sha512)
}
pub fn hmac_sha512_tag512_key_template() -> KeyTemplate {
create_hmac_key_template(64, 64, tink_proto::HashType::Sha512)
}
pub fn aes_cmac_tag128_key_template() -> KeyTemplate {
create_cmac_key_template(32, 16)
}
fn create_hmac_key_template(
key_size: u32,
tag_size: u32,
hash_type: tink_proto::HashType,
) -> KeyTemplate {
let params = tink_proto::HmacParams {
hash: hash_type as i32,
tag_size,
};
let format = tink_proto::HmacKeyFormat {
version: crate::HMAC_KEY_VERSION,
params: Some(params),
key_size,
};
let mut serialized_format = Vec::new();
format.encode(&mut serialized_format).unwrap();
KeyTemplate {
type_url: crate::HMAC_TYPE_URL.to_string(),
value: serialized_format,
output_prefix_type: tink_proto::OutputPrefixType::Tink as i32,
}
}
fn create_cmac_key_template(key_size: u32, tag_size: u32) -> KeyTemplate {
let params = tink_proto::AesCmacParams { tag_size };
let format = tink_proto::AesCmacKeyFormat {
params: Some(params),
key_size,
};
let mut serialized_format = Vec::new();
format.encode(&mut serialized_format).unwrap();
KeyTemplate {
type_url: crate::CMAC_TYPE_URL.to_string(),
value: serialized_format,
output_prefix_type: tink_proto::OutputPrefixType::Tink as i32,
}
}