openhcl_attestation_protocol/
vmgs.rs1use zerocopy::FromBytes;
7use zerocopy::Immutable;
8use zerocopy::IntoBytes;
9use zerocopy::KnownLayout;
10
11pub const NUMBER_KP: usize = 2;
14
15pub const DEK_BUFFER_SIZE: usize = 512;
17
18pub const GSP_BUFFER_SIZE: usize = 512;
20
21pub const KEY_PROTECTOR_SIZE: usize = size_of::<KeyProtector>();
23
24#[repr(C)]
26#[derive(Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
27pub struct DekKp {
28 pub dek_buffer: [u8; DEK_BUFFER_SIZE],
30}
31
32#[repr(C)]
34#[derive(Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
35pub struct GspKp {
36 pub gsp_length: u32,
38 pub gsp_buffer: [u8; GSP_BUFFER_SIZE],
40}
41
42#[repr(C)]
44#[derive(Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
45pub struct KeyProtector {
46 pub dek: [DekKp; NUMBER_KP],
48 pub gsp: [GspKp; NUMBER_KP],
50 pub active_kp: u32,
52}
53
54#[repr(C)]
56#[derive(Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
57pub struct KeyProtectorById {
58 pub id_guid: guid::Guid,
60 pub ported: u8,
62 pub pad: [u8; 3],
64}
65
66pub const AGENT_DATA_MAX_SIZE: usize = 2048;
68
69#[repr(C)]
71#[derive(Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
72pub struct SecurityProfile {
73 pub agent_data: [u8; AGENT_DATA_MAX_SIZE],
75}
76
77pub const HW_KEY_PROTECTOR_VERSION_1: u32 = 1;
83pub const HW_KEY_PROTECTOR_VERSION_2: u32 = 2;
84pub const HW_KEY_PROTECTOR_VERSION_3: u32 = 3;
90pub const HW_KEY_PROTECTOR_CURRENT_VERSION: u32 = HW_KEY_PROTECTOR_VERSION_3;
91
92pub const HW_KEY_PROTECTOR_TEE_TYPE_SNP: u32 = 0;
94pub const HW_KEY_PROTECTOR_TEE_TYPE_TDX: u32 = 1;
96
97pub const HW_KEY_PROTECTOR_SIZE: usize = size_of::<HardwareKeyProtector>();
99
100pub const HW_KEY_PROTECTOR_V3_SIZE: usize = size_of::<HardwareKeyProtectorV3>();
102
103const _: () = assert!(HW_KEY_PROTECTOR_SIZE != HW_KEY_PROTECTOR_V3_SIZE);
106
107pub const HW_KEY_PROTECTOR_SVN_SIZE: usize = 32;
109
110pub const AES_GCM_KEY_LENGTH: usize = 32;
112
113pub const AES_CBC_KEY_LENGTH: usize = AES_GCM_KEY_LENGTH;
115
116pub const AES_CBC_IV_LENGTH: usize = 16;
118
119pub const HMAC_SHA_256_KEY_LENGTH: usize = 32;
121
122#[repr(C)]
124#[derive(Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
125pub struct HardwareKeyProtectorHeader {
126 pub version: u32,
128 pub length: u32,
130 pub tcb_version: u64,
132 pub mix_measurement: u8,
135 pub _reserved: [u8; 7],
137}
138
139impl HardwareKeyProtectorHeader {
140 pub fn new(version: u32, length: u32, tcb_version: u64, mix_measurement: u8) -> Self {
142 Self {
143 version,
144 length,
145 tcb_version,
146 mix_measurement,
147 _reserved: [0; 7],
148 }
149 }
150}
151
152#[repr(C)]
154#[derive(Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
155pub struct HardwareKeyProtector {
156 pub header: HardwareKeyProtectorHeader,
158 pub iv: [u8; AES_CBC_IV_LENGTH],
160 pub ciphertext: [u8; AES_GCM_KEY_LENGTH],
162 pub hmac: [u8; HMAC_SHA_256_KEY_LENGTH],
164}
165
166#[repr(C)]
176#[derive(Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
177pub struct HardwareKeyProtectorHeaderV3 {
178 pub version: u32,
180 pub length: u32,
182 pub tee_type: u32,
184 pub svn: [u8; HW_KEY_PROTECTOR_SVN_SIZE],
186 pub mix_measurement: u8,
188 pub _reserved: [u8; 3],
190}
191
192impl HardwareKeyProtectorHeaderV3 {
193 pub fn new(
195 length: u32,
196 tee_type: u32,
197 svn: [u8; HW_KEY_PROTECTOR_SVN_SIZE],
198 mix_measurement: u8,
199 ) -> Self {
200 Self {
201 version: HW_KEY_PROTECTOR_VERSION_3,
202 length,
203 tee_type,
204 svn,
205 mix_measurement,
206 _reserved: [0; 3],
207 }
208 }
209}
210
211#[repr(C)]
213#[derive(Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
214pub struct HardwareKeyProtectorV3 {
215 pub header: HardwareKeyProtectorHeaderV3,
217 pub iv: [u8; AES_CBC_IV_LENGTH],
219 pub ciphertext: [u8; AES_GCM_KEY_LENGTH],
221 pub hmac: [u8; HMAC_SHA_256_KEY_LENGTH],
223}
224
225pub const GUEST_SECRET_KEY_MAX_SIZE: usize = 2048;
227
228#[repr(C)]
230#[derive(Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
231pub struct GuestSecretKey {
232 pub guest_secret_key: [u8; GUEST_SECRET_KEY_MAX_SIZE],
234}
235
236#[cfg(test)]
237mod tests {
238 use super::*;
239
240 #[test]
241 fn hardware_key_protector_header_new() {
242 let h = HardwareKeyProtectorHeader::new(2, 104, 0x1234, 1);
243 assert_eq!(h.version, 2);
244 assert_eq!(h.length, 104);
245 assert_eq!(h.tcb_version, 0x1234);
246 assert_eq!(h.mix_measurement, 1);
247 assert_eq!(h._reserved, [0; 7]);
248 }
249}