Skip to content

Commit e4537e6

Browse files
tpm: Add support for V2 event logs
The V1 event log format (which is also supported by TPMv2) always uses SHA-1 hashes. The V2 event log (which can only be used with TPMv2) provides a crypto-agile format, meaning it can store a variety of different hashes (e.g. SHA-256, SHA-512, etc).
1 parent 91aa6b8 commit e4537e6

File tree

3 files changed

+892
-5
lines changed

3 files changed

+892
-5
lines changed

uefi-test-runner/src/proto/tcg.rs

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use alloc::vec::Vec;
22
use core::mem::MaybeUninit;
3-
use uefi::proto::tcg::{v1, v2, EventType, HashAlgorithm, PcrIndex};
3+
use uefi::proto::tcg::{v1, v2, AlgorithmId, EventType, HashAlgorithm, PcrIndex};
44
use uefi::table::boot::BootServices;
55

66
// Environmental note:
@@ -276,6 +276,89 @@ pub fn test_tcg_v2(bt: &BootServices) {
276276

277277
// PCR 8 is initially zero.
278278
assert_eq!(tcg_v2_read_pcr_8(&mut tcg), [0; 20]);
279+
280+
// Create a PCR event.
281+
let pcr_index = PcrIndex(8);
282+
let mut event_buf = [MaybeUninit::uninit(); 256];
283+
let event_data = [0x12, 0x13, 0x14, 0x15];
284+
let data_to_hash = b"some-data";
285+
let event =
286+
v2::PcrEventInputs::new_in_buffer(&mut event_buf, pcr_index, EventType::IPL, &event_data)
287+
.unwrap();
288+
289+
// Extend a PCR and add the event to the log.
290+
tcg.hash_log_extend_event(v2::HashLogExtendEventFlags::empty(), data_to_hash, event)
291+
.unwrap();
292+
293+
// Hashes of `data_to_hash`.
294+
#[rustfmt::skip]
295+
let expected_hash_sha1 = [
296+
0x2e, 0x75, 0xc6, 0x98, 0x23, 0x96, 0x8a, 0x24, 0x4f, 0x0c,
297+
0x55, 0x59, 0xbb, 0x46, 0x8f, 0x36, 0x5f, 0x12, 0x11, 0xb6,
298+
];
299+
#[rustfmt::skip]
300+
let expected_hash_sha256 = [
301+
0x93, 0x32, 0xd9, 0x4d, 0x5e, 0xe6, 0x9a, 0xd1,
302+
0x7d, 0x31, 0x0e, 0x62, 0xcd, 0x10, 0x1d, 0x70,
303+
0xf5, 0x78, 0x02, 0x4f, 0xd5, 0xe8, 0xd1, 0x64,
304+
0x7f, 0x80, 0x73, 0xf8, 0x86, 0xc8, 0x94, 0xe1,
305+
];
306+
#[rustfmt::skip]
307+
let expected_hash_sha384 = [
308+
0xce, 0x4c, 0xbb, 0x09, 0x78, 0x37, 0x49, 0xbe,
309+
0xff, 0xc7, 0x17, 0x84, 0x5d, 0x27, 0x69, 0xae,
310+
0xd1, 0xe7, 0x23, 0x02, 0xdc, 0xeb, 0x95, 0xaf,
311+
0x34, 0xe7, 0xb4, 0xeb, 0xb9, 0xa8, 0x50, 0x25,
312+
0xc8, 0x40, 0xc1, 0xca, 0xf8, 0x9e, 0xb7, 0x36,
313+
0x23, 0x73, 0x09, 0x99, 0x82, 0x10, 0x82, 0x80,
314+
];
315+
#[rustfmt::skip]
316+
let expected_hash_sha512 = [
317+
0xe1, 0xc4, 0xfc, 0x67, 0xf1, 0x90, 0x9e, 0x35,
318+
0x08, 0x3c, 0xc5, 0x30, 0x9f, 0xcb, 0xa3, 0x6d,
319+
0x27, 0x43, 0x33, 0xa3, 0xc4, 0x00, 0x9a, 0x94,
320+
0xa9, 0x70, 0x52, 0x73, 0xe4, 0x1f, 0xc8, 0x8b,
321+
0x61, 0x89, 0xad, 0x15, 0x75, 0x51, 0xe3, 0xd3,
322+
0x9d, 0x1d, 0xaa, 0x44, 0x12, 0x26, 0x4d, 0x13,
323+
0x12, 0x0b, 0x67, 0x13, 0xc9, 0x9d, 0x3b, 0xe4,
324+
0xd6, 0x4c, 0x7d, 0xf4, 0xea, 0x7a, 0x4c, 0x7b,
325+
];
326+
327+
// Get the v1 log, and validate the last entry is the one we just added above.
328+
let log = tcg.get_event_log_v1().unwrap();
329+
assert!(!log.is_truncated());
330+
let entry = log.iter().last().unwrap();
331+
assert_eq!(entry.pcr_index(), pcr_index);
332+
assert_eq!(entry.event_type(), EventType::IPL);
333+
assert_eq!(entry.event_data(), event_data);
334+
#[rustfmt::skip]
335+
assert_eq!(entry.digest(), expected_hash_sha1);
336+
337+
// Get the v2 log, and validate the last entry is the one we just added above.
338+
let log = tcg.get_event_log_v2().unwrap();
339+
assert!(!log.is_truncated());
340+
let entry = log.iter().last().unwrap();
341+
assert_eq!(entry.pcr_index(), pcr_index);
342+
assert_eq!(entry.event_type(), EventType::IPL);
343+
assert_eq!(entry.event_data(), event_data);
344+
assert_eq!(
345+
entry.digests().into_iter().collect::<Vec<_>>(),
346+
[
347+
(AlgorithmId::SHA1, expected_hash_sha1.as_slice()),
348+
(AlgorithmId::SHA256, expected_hash_sha256.as_slice()),
349+
(AlgorithmId::SHA384, expected_hash_sha384.as_slice()),
350+
(AlgorithmId::SHA512, expected_hash_sha512.as_slice()),
351+
]
352+
);
353+
354+
// PCR 8 has been extended: `sha1([0; 20], sha1("some-data"))`.
355+
assert_eq!(
356+
tcg_v2_read_pcr_8(&mut tcg),
357+
[
358+
0x16, 0x53, 0x7d, 0xaa, 0x5d, 0xbd, 0xa8, 0x45, 0xe3, 0x30, 0x9e, 0x40, 0xe8, 0x74,
359+
0xd1, 0x50, 0x64, 0x73, 0x2f, 0x87,
360+
]
361+
);
279362
}
280363

281364
pub fn test(bt: &BootServices) {

uefi/src/proto/tcg/v1.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,13 @@ pub struct FfiPcrEvent {
238238

239239
/// TPM event log.
240240
///
241-
/// This type of event log always uses SHA-1 hashes.
241+
/// This type of event log always uses SHA-1 hashes. The [`v1::Tcg`]
242+
/// protocol always uses this type of event log, but it can also be
243+
/// provided by the [`v2::Tcg`] protocol via [`get_event_log_v2`].
242244
///
243245
/// [`v1::Tcg`]: Tcg
244246
/// [`v2::Tcg`]: super::v2::Tcg
247+
/// [`get_event_log_v2`]: super::v2::Tcg::get_event_log_v2
245248
pub struct EventLog<'a> {
246249
// Tie the lifetime to the protocol, and by extension, boot services.
247250
_lifetime: PhantomData<&'a Tcg>,

0 commit comments

Comments
 (0)