aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi / src / proto / tcg / v2.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! [TCG] (Trusted Computing Group) protocol for [TPM] (Trusted Platform |
| 4 | //! Module) 2.0. |
| 5 | //! |
| 6 | //! This protocol is defined in the [TCG EFI Protocol Specification _TPM |
| 7 | //! Family 2.0_][spec]. It is generally implemented only for TPM 2.0 |
| 8 | //! devices, but the spec indicates it can also be used for older TPM |
| 9 | //! devices. |
| 10 | //! |
| 11 | //! [spec]: https://trustedcomputinggroup.org/resource/tcg-efi-protocol-specification/ |
| 12 | //! [TCG]: https://trustedcomputinggroup.org/ |
| 13 | //! [TPM]: https://en.wikipedia.org/wiki/Trusted_Platform_Module |
| 14 | |
| 15 | use super::{AlgorithmId, EventType, HashAlgorithm, PcrIndex, v1}; |
| 16 | use crate::data_types::{Align, PhysicalAddress, UnalignedSlice}; |
| 17 | use crate::proto::unsafe_protocol; |
| 18 | use crate::util::{ptr_write_unaligned_and_add, usize_from_u32}; |
| 19 | use crate::{Error, Result, Status, StatusExt}; |
| 20 | use core::fmt::{self, Debug, Formatter}; |
| 21 | use core::marker::PhantomData; |
| 22 | use core::{ptr, slice}; |
| 23 | use ptr_meta::Pointee; |
| 24 | use uefi_raw::Boolean; |
| 25 | use uefi_raw::protocol::tcg::v2::{Tcg2EventHeader as EventHeader, Tcg2Protocol}; |
| 26 | |
| 27 | #[cfg(feature = "alloc")] |
| 28 | use {crate::mem::make_boxed, alloc::boxed::Box}; |
| 29 | |
| 30 | pub use uefi_raw::protocol::tcg::v2::{ |
| 31 | Tcg2EventLogFormat as EventLogFormat, Tcg2HashAlgorithmBitmap, |
| 32 | Tcg2HashLogExtendEventFlags as HashLogExtendEventFlags, Tcg2Version as Version, |
| 33 | }; |
| 34 | |
| 35 | /// Information about the protocol and the TPM device. |
| 36 | /// |
| 37 | /// Layout compatible with the C type `EFI_TCG2_BOOT_SERVICE_CAPABILITY`. |
| 38 | #[repr(C)] |
| 39 | #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)] |
| 40 | pub struct BootServiceCapability { |
| 41 | size: u8, |
| 42 | |
| 43 | /// Version of the EFI TCG2 protocol. |
| 44 | pub structure_version: Version, |
| 45 | |
| 46 | /// Version of the EFI TCG2 protocol. |
| 47 | pub protocol_version: Version, |
| 48 | |
| 49 | /// Bitmap of supported hash algorithms. |
| 50 | pub hash_algorithm_bitmap: HashAlgorithm, |
| 51 | |
| 52 | /// Event log formats supported by the firmware. |
| 53 | pub supported_event_logs: EventLogFormat, |
| 54 | |
| 55 | present_flag: u8, |
| 56 | |
| 57 | /// Maximum size (in bytes) of a command that can be sent to the TPM. |
| 58 | pub max_command_size: u16, |
| 59 | |
| 60 | /// Maximum size (in bytes) of a response that can be provided by the TPM. |
| 61 | pub max_response_size: u16, |
| 62 | |
| 63 | /// Manufacturer ID. |
| 64 | /// |
| 65 | /// See the [TCG Vendor ID registry]. |
| 66 | /// |
| 67 | /// [TCG Vendor ID registry]: https://trustedcomputinggroup.org/resource/vendor-id-registry/ |
| 68 | pub manufacturer_id: u32, |
| 69 | |
| 70 | /// Maximum number of supported PCR banks (hashing algorithms). |
| 71 | pub number_of_pcr_banks: u32, |
| 72 | |
| 73 | /// Bitmap of currently-active PCR banks (hashing algorithms). This |
| 74 | /// is a subset of the supported algorithms in [`hash_algorithm_bitmap`]. |
| 75 | /// |
| 76 | /// [`hash_algorithm_bitmap`]: Self::hash_algorithm_bitmap |
| 77 | pub active_pcr_banks: HashAlgorithm, |
| 78 | } |
| 79 | |
| 80 | impl Default for BootServiceCapability { |
| 81 | fn default() -> Self { |
| 82 | // OK to unwrap, the size is less than u8. |
| 83 | let struct_size = u8::try_from(size_of::<Self>()).unwrap(); |
| 84 | |
| 85 | Self { |
| 86 | size: struct_size, |
| 87 | structure_version: Version::default(), |
| 88 | protocol_version: Version::default(), |
| 89 | hash_algorithm_bitmap: HashAlgorithm::default(), |
| 90 | supported_event_logs: EventLogFormat::default(), |
| 91 | present_flag: 0, |
| 92 | max_command_size: 0, |
| 93 | max_response_size: 0, |
| 94 | manufacturer_id: 0, |
| 95 | number_of_pcr_banks: 0, |
| 96 | active_pcr_banks: HashAlgorithm::default(), |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | impl BootServiceCapability { |
| 102 | /// Whether the TPM device is present. |
| 103 | #[must_use] |
| 104 | pub const fn tpm_present(&self) -> bool { |
| 105 | self.present_flag != 0 |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /// Event type passed to [`Tcg::hash_log_extend_event`]. |
| 110 | /// |
| 111 | /// Layout compatible with the C type `EFI_TCG2_EVENT`. |
| 112 | /// |
| 113 | /// The TPM v1 spec uses a single generic event type for both creating a |
| 114 | /// new event and reading an event from the log. The v2 spec splits this |
| 115 | /// into two structs: `EFI_TCG2_EVENT` for creating events, and |
| 116 | /// `TCG_PCR_EVENT2` for reading events. To help clarify the usage, our |
| 117 | /// API renames these types to `PcrEventInputs` and `PcrEvent`, |
| 118 | /// respectively. |
| 119 | #[derive(Eq, Pointee)] |
| 120 | #[repr(C, packed)] |
| 121 | pub struct PcrEventInputs { |
| 122 | size: u32, |
| 123 | event_header: EventHeader, |
| 124 | event: [u8], |
| 125 | } |
| 126 | |
| 127 | impl PcrEventInputs { |
| 128 | /// Create a new `PcrEventInputs` using a byte buffer for storage. |
| 129 | /// |
| 130 | /// # Errors |
| 131 | /// |
| 132 | /// Returns [`Status::BUFFER_TOO_SMALL`] if the `buffer` is not large |
| 133 | /// enough. The required size will be returned in the error data. |
| 134 | /// |
| 135 | /// Returns [`Status::INVALID_PARAMETER`] if the `event_data` size is too |
| 136 | /// large. |
| 137 | pub fn new_in_buffer<'buf>( |
| 138 | buffer: &'buf mut [u8], |
| 139 | pcr_index: PcrIndex, |
| 140 | event_type: EventType, |
| 141 | event_data: &[u8], |
| 142 | ) -> Result<&'buf mut Self, Option<usize>> { |
| 143 | let required_size = size_of::<u32>() + size_of::<EventHeader>() + event_data.len(); |
| 144 | |
| 145 | if buffer.len() < required_size { |
| 146 | return Err(Error::new(Status::BUFFER_TOO_SMALL, Some(required_size))); |
| 147 | } |
| 148 | let size_field = u32::try_from(required_size) |
| 149 | .map_err(|_| Error::new(Status::INVALID_PARAMETER, None))?; |
| 150 | |
| 151 | let mut ptr: *mut u8 = buffer.as_mut_ptr().cast(); |
| 152 | |
| 153 | // SAFETY: The memory is valid. |
| 154 | unsafe { |
| 155 | ptr_write_unaligned_and_add(&mut ptr, size_field); |
| 156 | ptr_write_unaligned_and_add( |
| 157 | &mut ptr, |
| 158 | EventHeader { |
| 159 | header_size: u32::try_from(size_of::<EventHeader>()).unwrap(), |
| 160 | header_version: 1, |
| 161 | pcr_index: pcr_index.0, |
| 162 | event_type, |
| 163 | }, |
| 164 | ); |
| 165 | ptr::copy(event_data.as_ptr(), ptr, event_data.len()); |
| 166 | |
| 167 | let ptr: *mut Self = |
| 168 | ptr_meta::from_raw_parts_mut(buffer.as_mut_ptr().cast(), event_data.len()); |
| 169 | Ok(&mut *ptr) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /// Create a new `PcrEventInputs` in a [`Box`]. |
| 174 | /// |
| 175 | /// # Errors |
| 176 | /// |
| 177 | /// Returns [`Status::INVALID_PARAMETER`] if the `event_data` size is too |
| 178 | /// large. |
| 179 | #[cfg(feature = "alloc")] |
| 180 | pub fn new_in_box( |
| 181 | pcr_index: PcrIndex, |
| 182 | event_type: EventType, |
| 183 | event_data: &[u8], |
| 184 | ) -> Result<Box<Self>> { |
| 185 | make_boxed(|buf| Self::new_in_buffer(buf, pcr_index, event_type, event_data)) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | impl Align for PcrEventInputs { |
| 190 | fn alignment() -> usize { |
| 191 | 1 |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | impl Debug for PcrEventInputs { |
| 196 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 197 | f.debug_struct("PcrEventInputs") |
| 198 | .field("size", &{ self.size }) |
| 199 | .field("event_header", &self.event_header) |
| 200 | .field("event", &"<binary data>") |
| 201 | .finish() |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // Manual `PartialEq` implementation since it can't be derived for a packed DST. |
| 206 | impl PartialEq for PcrEventInputs { |
| 207 | fn eq(&self, other: &Self) -> bool { |
| 208 | self.size == other.size |
| 209 | && self.event_header == other.event_header |
| 210 | && self.event == other.event |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | #[repr(C, packed)] |
| 215 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 216 | struct AlgorithmDigestSize { |
| 217 | algorithm_id: AlgorithmId, |
| 218 | digest_size: u16, |
| 219 | } |
| 220 | |
| 221 | #[derive(Clone, Debug)] |
| 222 | struct AlgorithmDigestSizes<'a>(UnalignedSlice<'a, AlgorithmDigestSize>); |
| 223 | |
| 224 | impl AlgorithmDigestSizes<'_> { |
| 225 | fn get_size(&self, alg: AlgorithmId) -> Option<u16> { |
| 226 | self.0.iter().find_map(|elem| { |
| 227 | if { elem.algorithm_id } == alg { |
| 228 | Some(elem.digest_size) |
| 229 | } else { |
| 230 | None |
| 231 | } |
| 232 | }) |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | fn u32_le_from_bytes_at_offset(bytes: &[u8], offset: usize) -> Option<u32> { |
| 237 | let bytes = bytes.get(offset..offset + 4)?; |
| 238 | // OK to unwrap: we know `bytes` is now of length 4. |
| 239 | let val = u32::from_le_bytes(bytes.try_into().unwrap()); |
| 240 | Some(val) |
| 241 | } |
| 242 | |
| 243 | /// Header stored at the beginning of the event log. |
| 244 | /// |
| 245 | /// Layout compatible with the C type `TCG_EfiSpecIDEventStruct`. |
| 246 | #[derive(Clone, Debug)] |
| 247 | #[cfg_attr(not(test), expect(unused))] // needed for some fields |
| 248 | struct EventLogHeader<'a> { |
| 249 | platform_class: u32, |
| 250 | spec_version: ( |
| 251 | u8, /* major */ |
| 252 | u8, /* minor */ |
| 253 | u8, /* errata */ |
| 254 | ), |
| 255 | uintn_size: u8, |
| 256 | algorithm_digest_sizes: AlgorithmDigestSizes<'a>, |
| 257 | vendor_info: &'a [u8], |
| 258 | // Size of the whole header event, in bytes. |
| 259 | size_in_bytes: usize, |
| 260 | } |
| 261 | |
| 262 | impl<'a> EventLogHeader<'a> { |
| 263 | fn new(event: &'a v1::PcrEvent) -> Option<Self> { |
| 264 | if event.pcr_index() != PcrIndex(0) { |
| 265 | return None; |
| 266 | } |
| 267 | if { event.event_type() } != EventType::NO_ACTION { |
| 268 | return None; |
| 269 | } |
| 270 | if event.digest() != [0; 20] { |
| 271 | return None; |
| 272 | } |
| 273 | |
| 274 | let event = &event.event_data(); |
| 275 | if event.get(..16)? != *b"Spec ID Event03\0" { |
| 276 | return None; |
| 277 | } |
| 278 | let platform_class = u32_le_from_bytes_at_offset(event, 16)?; |
| 279 | let version_minor = *event.get(20)?; |
| 280 | let version_major = *event.get(21)?; |
| 281 | let version_errata = *event.get(22)?; |
| 282 | let uintn_size = *event.get(23)?; |
| 283 | let number_of_algorithms = usize_from_u32(u32_le_from_bytes_at_offset(event, 24)?); |
| 284 | // Checked arithmetic: on 32-bit targets a bogus count could wrap |
| 285 | // to a small offset that passes the bounds check below. |
| 286 | let vendor_info_size_byte_offset = number_of_algorithms |
| 287 | .checked_mul(size_of::<AlgorithmDigestSize>())? |
| 288 | .checked_add(28)?; |
| 289 | let vendor_info_size = usize::from(*event.get(vendor_info_size_byte_offset)?); |
| 290 | |
| 291 | // Safety: we know the slice is big enough because we just |
| 292 | // safely got the field after the slice (`vendor_info_size`). |
| 293 | // SAFETY: The memory is valid. |
| 294 | let algorithm_digest_sizes = unsafe { |
| 295 | let ptr: *const AlgorithmDigestSize = event.as_ptr().add(28).cast(); |
| 296 | AlgorithmDigestSizes(UnalignedSlice::new(ptr, number_of_algorithms)) |
| 297 | }; |
| 298 | |
| 299 | let vendor_info_byte_offset = vendor_info_size_byte_offset + 1; |
| 300 | let vendor_info = |
| 301 | event.get(vendor_info_byte_offset..vendor_info_byte_offset + vendor_info_size)?; |
| 302 | |
| 303 | // 32 is the size of PcrEventV1 excluding the event data. |
| 304 | let size_in_bytes = 32 + vendor_info_byte_offset + vendor_info_size; |
| 305 | |
| 306 | Some(Self { |
| 307 | platform_class, |
| 308 | spec_version: (version_major, version_minor, version_errata), |
| 309 | uintn_size, |
| 310 | algorithm_digest_sizes, |
| 311 | vendor_info, |
| 312 | size_in_bytes, |
| 313 | }) |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /// TPM event log as returned by [`Tcg::get_event_log_v2`]. |
| 318 | /// |
| 319 | /// This type of event log can contain multiple hash types (e.g. SHA-1, SHA-256, |
| 320 | /// SHA-512, etc). |
| 321 | #[derive(Debug)] |
| 322 | pub struct EventLog<'a> { |
| 323 | // Tie the lifetime to the protocol, and by extension, boot services. |
| 324 | _lifetime: PhantomData<&'a Tcg>, |
| 325 | |
| 326 | location: *const u8, |
| 327 | last_entry: *const u8, |
| 328 | |
| 329 | is_truncated: bool, |
| 330 | } |
| 331 | |
| 332 | impl EventLog<'_> { |
| 333 | /// Iterator of events in the log. |
| 334 | /// |
| 335 | /// The firmware reports where the last event starts, but not where the |
| 336 | /// log ends. Every event before the last one must end at or before the |
| 337 | /// last one, and the iterator stops at the first event that does not. |
| 338 | /// The size of the last event is taken from the log without a check. |
| 339 | #[must_use] |
| 340 | pub fn iter(&self) -> EventLogIter<'_> { |
| 341 | if let Some(header) = self.header() { |
| 342 | // Advance past the header |
| 343 | // SAFETY: The memory is valid. |
| 344 | let location = unsafe { self.location.add(header.size_in_bytes) }; |
| 345 | |
| 346 | EventLogIter { |
| 347 | log: self, |
| 348 | location, |
| 349 | header: self.header(), |
| 350 | } |
| 351 | } else { |
| 352 | EventLogIter { |
| 353 | log: self, |
| 354 | location: ptr::null(), |
| 355 | header: None, |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /// Header at the beginning of the event log. |
| 361 | fn header(&self) -> Option<EventLogHeader<'_>> { |
| 362 | // The spec is unclear if the header is present when there are |
| 363 | // no entries, so lets assume that `self.location` will be null |
| 364 | // if there's no header, and otherwise valid. |
| 365 | if self.location.is_null() { |
| 366 | None |
| 367 | } else { |
| 368 | // Safety: we trust that the protocol has given us a valid range |
| 369 | // of memory to read from. |
| 370 | // SAFETY: The memory is valid. |
| 371 | let event = unsafe { v1::PcrEvent::from_ptr(self.location, self.last_entry) }?; |
| 372 | EventLogHeader::new(event) |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /// Whether the event log is truncated due to not enough space in the log to |
| 377 | /// contain some events. |
| 378 | #[must_use] |
| 379 | pub const fn is_truncated(&self) -> bool { |
| 380 | self.is_truncated |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /// Digests in a PCR event. |
| 385 | #[derive(Clone)] |
| 386 | pub struct PcrEventDigests<'a> { |
| 387 | data: &'a [u8], |
| 388 | algorithm_digest_sizes: AlgorithmDigestSizes<'a>, |
| 389 | } |
| 390 | |
| 391 | impl Debug for PcrEventDigests<'_> { |
| 392 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 393 | f.debug_list().entries(self.clone()).finish() |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | impl<'a> IntoIterator for PcrEventDigests<'a> { |
| 398 | type Item = (AlgorithmId, &'a [u8]); |
| 399 | type IntoIter = PcrEventDigestIter<'a>; |
| 400 | |
| 401 | fn into_iter(self) -> Self::IntoIter { |
| 402 | PcrEventDigestIter { |
| 403 | digests: self, |
| 404 | offset: 0, |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | /// Iterator over a list of digests. |
| 410 | #[derive(Debug)] |
| 411 | pub struct PcrEventDigestIter<'a> { |
| 412 | digests: PcrEventDigests<'a>, |
| 413 | offset: usize, |
| 414 | } |
| 415 | |
| 416 | impl<'a> Iterator for PcrEventDigestIter<'a> { |
| 417 | type Item = (AlgorithmId, &'a [u8]); |
| 418 | |
| 419 | fn next(&mut self) -> Option<Self::Item> { |
| 420 | let data = &self.digests.data[self.offset..]; |
| 421 | let alg = data.get(..2)?; |
| 422 | let alg = AlgorithmId(u16::from_le_bytes([alg[0], alg[1]])); |
| 423 | let digest_size = usize::from(self.digests.algorithm_digest_sizes.get_size(alg)?); |
| 424 | let digest = data.get(2..2 + digest_size)?; |
| 425 | self.offset += 2 + digest_size; |
| 426 | Some((alg, digest)) |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | /// PCR event from an [`EventLog`]. |
| 431 | /// |
| 432 | /// This roughly corresponds to the C type `TCG_PCR_EVENT2`, but is not layout |
| 433 | /// compatible. |
| 434 | /// |
| 435 | /// The TPM v1 spec uses a single generic event type for both creating a |
| 436 | /// new event and reading an event from the log. The v2 spec splits this |
| 437 | /// into two structs: `EFI_TCG2_EVENT` for creating events, and |
| 438 | /// `TCG_PCR_EVENT2` for reading events. To help clarify the usage, our |
| 439 | /// API renames these types to `PcrEventInputs` and `PcrEvent`, |
| 440 | /// respectively. |
| 441 | #[derive(Debug)] |
| 442 | pub struct PcrEvent<'a> { |
| 443 | pcr_index: PcrIndex, |
| 444 | event_type: EventType, |
| 445 | digests: &'a [u8], |
| 446 | event_data: &'a [u8], |
| 447 | |
| 448 | // Precalculate the pointer to the next event. |
| 449 | next: *const u8, |
| 450 | |
| 451 | // This data from the v2 log header is needed to parse the digest data. |
| 452 | algorithm_digest_sizes: AlgorithmDigestSizes<'a>, |
| 453 | } |
| 454 | |
| 455 | impl<'a> PcrEvent<'a> { |
| 456 | /// Size of the fields before the digests: PCR index, event type and |
| 457 | /// digest count. |
| 458 | const DIGESTS_OFFSET: usize = size_of::<PcrIndex>() + size_of::<EventType>() + size_of::<u32>(); |
| 459 | |
| 460 | /// Parses the event at `ptr`. |
| 461 | /// |
| 462 | /// `last_entry` is the start of the last event in the log. Events are |
| 463 | /// contiguous, so an event before it must end at or before it, and |
| 464 | /// `None` is returned if it does not. The log does not report where the |
| 465 | /// last event ends, so the size of that event cannot be checked. |
| 466 | /// |
| 467 | /// # Safety |
| 468 | /// |
| 469 | /// The memory from `ptr` up to `last_entry`, or up to the end of the |
| 470 | /// event if `ptr` is the last entry, must be valid for reads. |
| 471 | unsafe fn from_ptr( |
| 472 | ptr: *const u8, |
| 473 | header: EventLogHeader<'a>, |
| 474 | last_entry: *const u8, |
| 475 | ) -> Option<Self> { |
| 476 | // `None` for the last event, whose end is unknown. |
| 477 | let available = (ptr < last_entry).then(|| last_entry.addr() - ptr.addr()); |
| 478 | let fits = |size: usize| available.is_none_or(|available| size <= available); |
| 479 | |
| 480 | if !fits(Self::DIGESTS_OFFSET) { |
| 481 | return None; |
| 482 | } |
| 483 | let ptr_u32: *const u32 = ptr.cast(); |
| 484 | // SAFETY: The source memory may be unaligned, so this uses unaligned access. |
| 485 | let pcr_index = PcrIndex(unsafe { ptr_u32.read_unaligned() }); |
| 486 | // SAFETY: The source memory may be unaligned, so this uses unaligned access. |
| 487 | let event_type = EventType(unsafe { ptr_u32.add(1).read_unaligned() }); |
| 488 | // SAFETY: The source memory may be unaligned, so this uses unaligned access. |
| 489 | let digests_count = unsafe { ptr_u32.add(2).read_unaligned() }; |
| 490 | // An event holds at most one digest per algorithm listed in the |
| 491 | // header. A larger count is malformed and would walk past the |
| 492 | // digest list below. |
| 493 | if usize_from_u32(digests_count) > header.algorithm_digest_sizes.0.len() { |
| 494 | return None; |
| 495 | } |
| 496 | // SAFETY: The memory is valid. |
| 497 | let digests_ptr: *const u8 = unsafe { ptr.add(Self::DIGESTS_OFFSET) }; |
| 498 | |
| 499 | // Get the byte size of the digests so that the digests iterator |
| 500 | // can be safe code. |
| 501 | let mut digests_byte_size = 0; |
| 502 | let mut elem_ptr = digests_ptr; |
| 503 | for _ in 0..digests_count { |
| 504 | if !fits(Self::DIGESTS_OFFSET + digests_byte_size + size_of::<AlgorithmId>()) { |
| 505 | return None; |
| 506 | } |
| 507 | // SAFETY: The source memory may be unaligned, so this uses unaligned access. |
| 508 | let algorithm_id = AlgorithmId(unsafe { elem_ptr.cast::<u16>().read_unaligned() }); |
| 509 | let alg_and_digest_size = size_of::<AlgorithmId>() |
| 510 | + usize::from(header.algorithm_digest_sizes.get_size(algorithm_id)?); |
| 511 | digests_byte_size += alg_and_digest_size; |
| 512 | if !fits(Self::DIGESTS_OFFSET + digests_byte_size) { |
| 513 | return None; |
| 514 | } |
| 515 | // SAFETY: The memory is valid. |
| 516 | elem_ptr = unsafe { elem_ptr.add(alg_and_digest_size) }; |
| 517 | } |
| 518 | |
| 519 | let event_size_offset = Self::DIGESTS_OFFSET + digests_byte_size; |
| 520 | if !fits(event_size_offset + size_of::<u32>()) { |
| 521 | return None; |
| 522 | } |
| 523 | // SAFETY: The pointer is valid for the requested slice length. |
| 524 | let digests = unsafe { slice::from_raw_parts(digests_ptr, digests_byte_size) }; |
| 525 | // SAFETY: The memory is valid. |
| 526 | let event_size_ptr = unsafe { digests_ptr.add(digests_byte_size) }; |
| 527 | // SAFETY: The source memory may be unaligned, so this uses unaligned access. |
| 528 | let event_size = usize_from_u32(unsafe { event_size_ptr.cast::<u32>().read_unaligned() }); |
| 529 | let event_data_offset = event_size_offset + size_of::<u32>(); |
| 530 | if !fits(event_data_offset.checked_add(event_size)?) { |
| 531 | return None; |
| 532 | } |
| 533 | // SAFETY: The memory is valid. |
| 534 | let event_data_ptr = unsafe { event_size_ptr.add(4) }; |
| 535 | // SAFETY: The pointer is valid for the requested slice length. |
| 536 | let event_data = unsafe { slice::from_raw_parts(event_data_ptr, event_size) }; |
| 537 | |
| 538 | Some(Self { |
| 539 | pcr_index, |
| 540 | event_type, |
| 541 | digests, |
| 542 | event_data, |
| 543 | // SAFETY: The memory is valid. |
| 544 | next: unsafe { event_data_ptr.add(event_size) }, |
| 545 | algorithm_digest_sizes: header.algorithm_digest_sizes, |
| 546 | }) |
| 547 | } |
| 548 | |
| 549 | /// PCR index for the event. |
| 550 | #[must_use] |
| 551 | pub const fn pcr_index(&self) -> PcrIndex { |
| 552 | self.pcr_index |
| 553 | } |
| 554 | |
| 555 | /// Type of event, indicating what type of data is stored in [`event_data`]. |
| 556 | /// |
| 557 | /// [`event_data`]: Self::event_data |
| 558 | #[must_use] |
| 559 | pub const fn event_type(&self) -> EventType { |
| 560 | self.event_type |
| 561 | } |
| 562 | |
| 563 | /// Raw event data. The meaning of this data can be determined from |
| 564 | /// the [`event_type`]. |
| 565 | /// |
| 566 | /// Note that this data is independent of what is hashed in [`digests`]. |
| 567 | /// |
| 568 | /// [`digests`]: Self::digests |
| 569 | /// [`event_type`]: Self::event_type |
| 570 | #[must_use] |
| 571 | pub const fn event_data(&self) -> &[u8] { |
| 572 | self.event_data |
| 573 | } |
| 574 | |
| 575 | /// Digests of the data hashed for this event. |
| 576 | #[must_use] |
| 577 | pub fn digests(&self) -> PcrEventDigests<'_> { |
| 578 | PcrEventDigests { |
| 579 | data: self.digests, |
| 580 | algorithm_digest_sizes: self.algorithm_digest_sizes.clone(), |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | /// Iterator for events in [`EventLog`]. |
| 586 | #[derive(Debug)] |
| 587 | pub struct EventLogIter<'a> { |
| 588 | log: &'a EventLog<'a>, |
| 589 | header: Option<EventLogHeader<'a>>, |
| 590 | location: *const u8, |
| 591 | } |
| 592 | |
| 593 | impl<'a> Iterator for EventLogIter<'a> { |
| 594 | type Item = PcrEvent<'a>; |
| 595 | |
| 596 | fn next(&mut self) -> Option<Self::Item> { |
| 597 | // The spec says that `last_entry` will be null if there are no |
| 598 | // events. Presumably `location` will be null as well, but check |
| 599 | // both just to be safe. |
| 600 | if self.location.is_null() || self.log.last_entry.is_null() { |
| 601 | return None; |
| 602 | } |
| 603 | |
| 604 | // `last_entry` points to the start of the last event, so the log |
| 605 | // ends once the location moves past it. |
| 606 | if self.location > self.log.last_entry { |
| 607 | return None; |
| 608 | } |
| 609 | |
| 610 | // Safety: we trust that the protocol has given us a valid range |
| 611 | // of memory to read from. |
| 612 | // SAFETY: The memory is valid. |
| 613 | let event = unsafe { |
| 614 | PcrEvent::from_ptr(self.location, self.header.clone()?, self.log.last_entry)? |
| 615 | }; |
| 616 | |
| 617 | self.location = event.next; |
| 618 | |
| 619 | Some(event) |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | /// Trusted Computing Group [`Protocol`] (TCG) for TPM 2.0 devices. |
| 624 | /// |
| 625 | /// The corresponding C type is `EFI_TCG2_PROTOCOL`. |
| 626 | /// |
| 627 | /// [`Protocol`]: uefi::proto::Protocol |
| 628 | #[derive(Debug)] |
| 629 | #[repr(transparent)] |
| 630 | #[unsafe_protocol(Tcg2Protocol::GUID)] |
| 631 | pub struct Tcg(Tcg2Protocol); |
| 632 | |
| 633 | impl Tcg { |
| 634 | /// Get information about the protocol and TPM device. |
| 635 | pub fn get_capability(&mut self) -> Result<BootServiceCapability> { |
| 636 | let mut capability = BootServiceCapability::default(); |
| 637 | // SAFETY: The memory is valid. |
| 638 | unsafe { |
| 639 | (self.0.get_capability)(&mut self.0, ptr::from_mut(&mut capability).cast()) |
| 640 | .to_result_with_val(|| capability) |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | /// Get the V1 event log. This provides events in the same format as a V1 |
| 645 | /// TPM, so all events use SHA-1 hashes. |
| 646 | pub fn get_event_log_v1(&mut self) -> Result<v1::EventLog<'_>> { |
| 647 | let mut location = 0; |
| 648 | let mut last_entry = 0; |
| 649 | let mut truncated = Boolean::default(); |
| 650 | |
| 651 | // SAFETY: The memory is valid. |
| 652 | let status = unsafe { |
| 653 | (self.0.get_event_log)( |
| 654 | &mut self.0, |
| 655 | EventLogFormat::TCG_1_2, |
| 656 | &mut location, |
| 657 | &mut last_entry, |
| 658 | &mut truncated, |
| 659 | ) |
| 660 | }; |
| 661 | |
| 662 | if status.is_success() { |
| 663 | // SAFETY: The memory is valid. |
| 664 | let log = unsafe { |
| 665 | v1::EventLog::new( |
| 666 | location as *const u8, |
| 667 | last_entry as *const u8, |
| 668 | truncated.into(), |
| 669 | ) |
| 670 | }; |
| 671 | |
| 672 | Ok(log) |
| 673 | } else { |
| 674 | Err(status.into()) |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | /// Get the V2 event log. This format allows for a flexible list of hash types. |
| 679 | pub fn get_event_log_v2(&mut self) -> Result<EventLog<'_>> { |
| 680 | let mut location = 0; |
| 681 | let mut last_entry = 0; |
| 682 | let mut truncated = Boolean::default(); |
| 683 | // SAFETY: The memory is valid. |
| 684 | let status = unsafe { |
| 685 | (self.0.get_event_log)( |
| 686 | &mut self.0, |
| 687 | EventLogFormat::TCG_2, |
| 688 | &mut location, |
| 689 | &mut last_entry, |
| 690 | &mut truncated, |
| 691 | ) |
| 692 | }; |
| 693 | |
| 694 | if status.is_success() { |
| 695 | let log = EventLog { |
| 696 | _lifetime: PhantomData, |
| 697 | location: location as *const u8, |
| 698 | last_entry: last_entry as *const u8, |
| 699 | is_truncated: truncated.into(), |
| 700 | }; |
| 701 | |
| 702 | Ok(log) |
| 703 | } else { |
| 704 | Err(status.into()) |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | /// Extend a PCR and add an entry to the event log. |
| 709 | pub fn hash_log_extend_event( |
| 710 | &mut self, |
| 711 | flags: HashLogExtendEventFlags, |
| 712 | data_to_hash: &[u8], |
| 713 | event: &PcrEventInputs, |
| 714 | ) -> Result { |
| 715 | let event: *const PcrEventInputs = event; |
| 716 | let (event, _event_size) = ptr_meta::to_raw_parts(event); |
| 717 | // SAFETY: The memory is valid. |
| 718 | unsafe { |
| 719 | (self.0.hash_log_extend_event)( |
| 720 | &mut self.0, |
| 721 | flags, |
| 722 | data_to_hash.as_ptr() as PhysicalAddress, |
| 723 | // OK to unwrap, usize fits in u64. |
| 724 | u64::try_from(data_to_hash.len()).unwrap(), |
| 725 | event.cast(), |
| 726 | ) |
| 727 | .to_result() |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | /// Send a command directly to the TPM. |
| 732 | /// |
| 733 | /// Constructing the input block and parsing the output block are outside |
| 734 | /// the scope of this crate. See the [TPM 2.0 Specification][spec], in |
| 735 | /// particular Part 2 (Structures) and Part 3 (Commands). |
| 736 | /// |
| 737 | /// Note that TPM structures are big endian. |
| 738 | /// |
| 739 | /// [spec]: https://trustedcomputinggroup.org/resource/tpm-library-specification/ |
| 740 | pub fn submit_command( |
| 741 | &mut self, |
| 742 | input_parameter_block: &[u8], |
| 743 | output_parameter_block: &mut [u8], |
| 744 | ) -> Result { |
| 745 | let input_parameter_block_len = u32::try_from(input_parameter_block.len()) |
| 746 | .map_err(|_| Error::from(Status::BAD_BUFFER_SIZE))?; |
| 747 | let output_parameter_block_len = u32::try_from(output_parameter_block.len()) |
| 748 | .map_err(|_| Error::from(Status::BAD_BUFFER_SIZE))?; |
| 749 | |
| 750 | // SAFETY: The memory is valid. |
| 751 | unsafe { |
| 752 | (self.0.submit_command)( |
| 753 | &mut self.0, |
| 754 | input_parameter_block_len, |
| 755 | input_parameter_block.as_ptr(), |
| 756 | output_parameter_block_len, |
| 757 | output_parameter_block.as_mut_ptr(), |
| 758 | ) |
| 759 | .to_result() |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | /// Get a bitmap of the active PCR banks. Each bank corresponds to a hash |
| 764 | /// algorithm. |
| 765 | pub fn get_active_pcr_banks(&mut self) -> Result<HashAlgorithm> { |
| 766 | let mut active_pcr_banks = Tcg2HashAlgorithmBitmap::empty(); |
| 767 | |
| 768 | // SAFETY: The memory is valid. |
| 769 | let status = unsafe { (self.0.get_active_pcr_banks)(&mut self.0, &mut active_pcr_banks) }; |
| 770 | |
| 771 | status.to_result_with_val(|| HashAlgorithm::from_bits_retain(active_pcr_banks.bits())) |
| 772 | } |
| 773 | |
| 774 | /// Set the active PCR banks. Each bank corresponds to a hash |
| 775 | /// algorithm. This change will not take effect until the system is |
| 776 | /// rebooted twice. |
| 777 | pub fn set_active_pcr_banks(&mut self, active_pcr_banks: HashAlgorithm) -> Result { |
| 778 | let active_pcr_banks = Tcg2HashAlgorithmBitmap::from_bits_retain(active_pcr_banks.bits()); |
| 779 | // SAFETY: The memory is valid. |
| 780 | unsafe { (self.0.set_active_pcr_banks)(&mut self.0, active_pcr_banks) }.to_result() |
| 781 | } |
| 782 | |
| 783 | /// Get the stored result of calling [`Tcg::set_active_pcr_banks`] in a |
| 784 | /// previous boot. |
| 785 | /// |
| 786 | /// If there was no attempt to set the active PCR banks in a previous boot, |
| 787 | /// this returns `None`. Otherwise, it returns a numeric response code: |
| 788 | /// * `0x00000000`: Success |
| 789 | /// * `0x00000001..=0x00000FFF`: TPM error code |
| 790 | /// * `0xfffffff0`: The operation was canceled by the user or timed out |
| 791 | /// * `0xfffffff1`: Firmware error |
| 792 | pub fn get_result_of_set_active_pcr_banks(&mut self) -> Result<Option<u32>> { |
| 793 | let mut operation_present = 0; |
| 794 | let mut response = 0; |
| 795 | |
| 796 | // SAFETY: The memory is valid. |
| 797 | let status = unsafe { |
| 798 | (self.0.get_result_of_set_active_pcr_banks)( |
| 799 | &mut self.0, |
| 800 | &mut operation_present, |
| 801 | &mut response, |
| 802 | ) |
| 803 | }; |
| 804 | |
| 805 | status.to_result_with_val(|| { |
| 806 | if operation_present == 0 { |
| 807 | None |
| 808 | } else { |
| 809 | Some(response) |
| 810 | } |
| 811 | }) |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | #[cfg(test)] |
| 816 | mod tests { |
| 817 | use super::*; |
| 818 | use alloc::vec::Vec; |
| 819 | use core::slice; |
| 820 | |
| 821 | #[test] |
| 822 | fn test_new_event() { |
| 823 | let mut buf = [0; 22]; |
| 824 | let event_data = [0x12, 0x13, 0x14, 0x15]; |
| 825 | let event = |
| 826 | PcrEventInputs::new_in_buffer(&mut buf, PcrIndex(4), EventType::IPL, &event_data) |
| 827 | .unwrap(); |
| 828 | |
| 829 | assert_eq!({ event.size }, 22); |
| 830 | assert_eq!( |
| 831 | event.event_header, |
| 832 | EventHeader { |
| 833 | header_size: 14, |
| 834 | header_version: 1, |
| 835 | pcr_index: 4, |
| 836 | event_type: EventType::IPL, |
| 837 | } |
| 838 | ); |
| 839 | |
| 840 | // Cast to a byte slice to check the data is exactly as expected. |
| 841 | let event_ptr: *const PcrEventInputs = event; |
| 842 | let event_ptr: *const u8 = event_ptr.cast(); |
| 843 | // SAFETY: The pointer is valid for the requested slice length. |
| 844 | let event_bytes = unsafe { slice::from_raw_parts(event_ptr, size_of_val(event)) }; |
| 845 | |
| 846 | #[rustfmt::skip] |
| 847 | assert_eq!(event_bytes, [ |
| 848 | // Size |
| 849 | 0x16, 0x00, 0x00, 0x00, |
| 850 | |
| 851 | // Header |
| 852 | // Header size |
| 853 | 0x0e, 0x00, 0x00, 0x00, |
| 854 | // Header version |
| 855 | 0x01, 0x00, |
| 856 | // PCR index |
| 857 | 0x04, 0x00, 0x00, 0x00, |
| 858 | // Event type |
| 859 | 0x0d, 0x00, 0x00, 0x00, |
| 860 | // Event data |
| 861 | 0x12, 0x13, 0x14, 0x15, |
| 862 | ]); |
| 863 | |
| 864 | // Check that `new_in_box` gives the same value. |
| 865 | assert_eq!( |
| 866 | event, |
| 867 | &*PcrEventInputs::new_in_box(PcrIndex(4), EventType::IPL, &event_data).unwrap() |
| 868 | ); |
| 869 | } |
| 870 | |
| 871 | /// Spec ID header event listing SHA-1 as the only algorithm. |
| 872 | #[rustfmt::skip] |
| 873 | const HEADER_SHA1_ONLY: [u8; 65] = [ |
| 874 | // PCR index |
| 875 | 0x00, 0x00, 0x00, 0x00, |
| 876 | // Event type |
| 877 | 0x03, 0x00, 0x00, 0x00, |
| 878 | // SHA1 digest |
| 879 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 880 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 881 | // Event data size |
| 882 | 0x21, 0x00, 0x00, 0x00, |
| 883 | // Spec ID event data |
| 884 | // Signature |
| 885 | 0x53, 0x70, 0x65, 0x63, |
| 886 | 0x20, 0x49, 0x44, 0x20, |
| 887 | 0x45, 0x76, 0x65, 0x6e, |
| 888 | 0x74, 0x30, 0x33, 0x00, |
| 889 | // Platform class |
| 890 | 0x00, 0x00, 0x00, 0x00, |
| 891 | // Spec version (minor, major, errata) |
| 892 | 0x00, 0x02, 0x00, |
| 893 | // Uintn size |
| 894 | 0x02, |
| 895 | // Number of algorithms |
| 896 | 0x01, 0x00, 0x00, 0x00, |
| 897 | // Digest sizes |
| 898 | // SHA1, size |
| 899 | 0x04, 0x00, |
| 900 | 0x14, 0x00, |
| 901 | // Vendor info size |
| 902 | 0x00, |
| 903 | ]; |
| 904 | |
| 905 | /// A log that holds only the header event: `last_entry` points to the |
| 906 | /// header, i.e. before the first location the iterator would read. |
| 907 | #[test] |
| 908 | fn test_event_log_v2_header_only() { |
| 909 | let bytes = HEADER_SHA1_ONLY; |
| 910 | |
| 911 | let log = EventLog { |
| 912 | _lifetime: PhantomData, |
| 913 | location: bytes.as_ptr(), |
| 914 | last_entry: bytes.as_ptr(), |
| 915 | is_truncated: false, |
| 916 | }; |
| 917 | |
| 918 | assert!(log.header().is_some()); |
| 919 | let mut iter = log.iter(); |
| 920 | assert!(iter.next().is_none()); |
| 921 | assert!(iter.next().is_none()); |
| 922 | } |
| 923 | |
| 924 | #[test] |
| 925 | fn test_event_log_v2_digest_count_too_large() { |
| 926 | let mut bytes = HEADER_SHA1_ONLY.to_vec(); |
| 927 | #[rustfmt::skip] |
| 928 | bytes.extend_from_slice(&[ |
| 929 | // PCR index |
| 930 | 0x00, 0x00, 0x00, 0x00, |
| 931 | // Event type |
| 932 | 0x08, 0x00, 0x00, 0x00, |
| 933 | // Digest count: more than the one algorithm in the header |
| 934 | 0x02, 0x00, 0x00, 0x00, |
| 935 | // Digests |
| 936 | // SHA1 |
| 937 | 0x04, 0x00, |
| 938 | 0x14, 0x89, 0xf9, 0x23, 0xc4, 0xdc, 0xa7, 0x29, 0x17, 0x8b, |
| 939 | 0x3e, 0x32, 0x33, 0x45, 0x85, 0x50, 0xd8, 0xdd, 0xdf, 0x29, |
| 940 | // The log ends here, so any further digest would be read |
| 941 | // out of bounds. |
| 942 | ]); |
| 943 | |
| 944 | let log = EventLog { |
| 945 | _lifetime: PhantomData, |
| 946 | location: bytes.as_ptr(), |
| 947 | // SAFETY: The memory is valid. |
| 948 | last_entry: unsafe { bytes.as_ptr().add(HEADER_SHA1_ONLY.len()) }, |
| 949 | is_truncated: false, |
| 950 | }; |
| 951 | |
| 952 | let mut iter = log.iter(); |
| 953 | assert!(iter.next().is_none()); |
| 954 | assert!(iter.next().is_none()); |
| 955 | } |
| 956 | |
| 957 | /// An event before the last one must end at or before `last_entry`. |
| 958 | /// Otherwise its size would turn into a slice past the log. |
| 959 | #[test] |
| 960 | fn test_event_log_v2_event_size_too_large() { |
| 961 | let mut bytes = HEADER_SHA1_ONLY.to_vec(); |
| 962 | #[rustfmt::skip] |
| 963 | bytes.extend_from_slice(&[ |
| 964 | // PCR index |
| 965 | 0x00, 0x00, 0x00, 0x00, |
| 966 | // Event type |
| 967 | 0x08, 0x00, 0x00, 0x00, |
| 968 | // Digest count |
| 969 | 0x01, 0x00, 0x00, 0x00, |
| 970 | // Digests |
| 971 | // SHA1 |
| 972 | 0x04, 0x00, |
| 973 | 0x14, 0x89, 0xf9, 0x23, 0xc4, 0xdc, 0xa7, 0x29, 0x17, 0x8b, |
| 974 | 0x3e, 0x32, 0x33, 0x45, 0x85, 0x50, 0xd8, 0xdd, 0xdf, 0x29, |
| 975 | // Event size: larger than the log |
| 976 | 0x00, 0x01, 0x00, 0x00, |
| 977 | // Event data |
| 978 | 0x00, 0x00, |
| 979 | ]); |
| 980 | |
| 981 | let log = EventLog { |
| 982 | _lifetime: PhantomData, |
| 983 | location: bytes.as_ptr(), |
| 984 | // `last_entry` points right after the event data. |
| 985 | // SAFETY: The memory is valid. |
| 986 | last_entry: unsafe { bytes.as_ptr().add(bytes.len()) }, |
| 987 | is_truncated: false, |
| 988 | }; |
| 989 | |
| 990 | let mut iter = log.iter(); |
| 991 | assert!(iter.next().is_none()); |
| 992 | assert!(iter.next().is_none()); |
| 993 | } |
| 994 | |
| 995 | #[test] |
| 996 | fn test_event_log_v2() { |
| 997 | // This data comes from dumping the TPM event log in a VM |
| 998 | // (truncated to just two entries after the header). |
| 999 | #[rustfmt::skip] |
| 1000 | let bytes = [ |
| 1001 | // Header event |
| 1002 | // PCR index |
| 1003 | 0x00, 0x00, 0x00, 0x00, |
| 1004 | // Event type |
| 1005 | 0x03, 0x00, 0x00, 0x00, |
| 1006 | // SHA1 digest |
| 1007 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 1008 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 1009 | // Event data size |
| 1010 | 0x2d, 0x00, 0x00, 0x00, |
| 1011 | // Spec ID event data |
| 1012 | // Signature |
| 1013 | 0x53, 0x70, 0x65, 0x63, |
| 1014 | 0x20, 0x49, 0x44, 0x20, |
| 1015 | 0x45, 0x76, 0x65, 0x6e, |
| 1016 | 0x74, 0x30, 0x33, 0x00, |
| 1017 | // Platform class |
| 1018 | 0x00, 0x00, 0x00, 0x00, |
| 1019 | // Spec version (minor, major, errata) (yes the order is weird) |
| 1020 | 0x00, 0x02, 0x00, |
| 1021 | // Uintn size |
| 1022 | 0x02, |
| 1023 | // Number of algorithms |
| 1024 | 0x04, 0x00, 0x00, 0x00, |
| 1025 | // Digest sizes |
| 1026 | // SHA1, size |
| 1027 | 0x04, 0x00, |
| 1028 | 0x14, 0x00, |
| 1029 | // SHA256, size |
| 1030 | 0x0b, 0x00, |
| 1031 | 0x20, 0x00, |
| 1032 | // SHA384, size |
| 1033 | 0x0c, 0x00, |
| 1034 | 0x30, 0x00, |
| 1035 | // SHA512, size |
| 1036 | 0x0d, 0x00, |
| 1037 | 0x40, 0x00, |
| 1038 | // Vendor info size |
| 1039 | 0x00, |
| 1040 | |
| 1041 | // Event 1 |
| 1042 | // PCR index |
| 1043 | 0x00, 0x00, 0x00, 0x00, |
| 1044 | // Event type |
| 1045 | 0x08, 0x00, 0x00, 0x00, |
| 1046 | // Digest count |
| 1047 | 0x04, 0x00, 0x00, 0x00, |
| 1048 | // Digests |
| 1049 | // SHA1 |
| 1050 | 0x04, 0x00, |
| 1051 | 0x14, 0x89, 0xf9, 0x23, 0xc4, 0xdc, 0xa7, 0x29, 0x17, 0x8b, |
| 1052 | 0x3e, 0x32, 0x33, 0x45, 0x85, 0x50, 0xd8, 0xdd, 0xdf, 0x29, |
| 1053 | // SHA256 |
| 1054 | 0x0b, 0x00, |
| 1055 | 0x96, 0xa2, 0x96, 0xd2, 0x24, 0xf2, 0x85, 0xc6, |
| 1056 | 0x7b, 0xee, 0x93, 0xc3, 0x0f, 0x8a, 0x30, 0x91, |
| 1057 | 0x57, 0xf0, 0xda, 0xa3, 0x5d, 0xc5, 0xb8, 0x7e, |
| 1058 | 0x41, 0x0b, 0x78, 0x63, 0x0a, 0x09, 0xcf, 0xc7, |
| 1059 | // SHA384 |
| 1060 | 0x0c, 0x00, |
| 1061 | 0x1d, 0xd6, 0xf7, 0xb4, 0x57, 0xad, 0x88, 0x0d, |
| 1062 | 0x84, 0x0d, 0x41, 0xc9, 0x61, 0x28, 0x3b, 0xab, |
| 1063 | 0x68, 0x8e, 0x94, 0xe4, 0xb5, 0x93, 0x59, 0xea, |
| 1064 | 0x45, 0x68, 0x65, 0x81, 0xe9, 0x0f, 0xec, 0xce, |
| 1065 | 0xa3, 0xc6, 0x24, 0xb1, 0x22, 0x61, 0x13, 0xf8, |
| 1066 | 0x24, 0xf3, 0x15, 0xeb, 0x60, 0xae, 0x0a, 0x7c, |
| 1067 | // SHA512 |
| 1068 | 0x0d, 0x00, |
| 1069 | 0x5e, 0xa7, 0x1d, 0xc6, 0xd0, 0xb4, 0xf5, 0x7b, |
| 1070 | 0xf3, 0x9a, 0xad, 0xd0, 0x7c, 0x20, 0x8c, 0x35, |
| 1071 | 0xf0, 0x6c, 0xd2, 0xba, 0xc5, 0xfd, 0xe2, 0x10, |
| 1072 | 0x39, 0x7f, 0x70, 0xde, 0x11, 0xd4, 0x39, 0xc6, |
| 1073 | 0x2e, 0xc1, 0xcd, 0xf3, 0x18, 0x37, 0x58, 0x86, |
| 1074 | 0x5f, 0xd3, 0x87, 0xfc, 0xea, 0x0b, 0xad, 0xa2, |
| 1075 | 0xf6, 0xc3, 0x7a, 0x4a, 0x17, 0x85, 0x1d, 0xd1, |
| 1076 | 0xd7, 0x8f, 0xef, 0xe6, 0xf2, 0x04, 0xee, 0x54, |
| 1077 | // Event size |
| 1078 | 0x02, 0x00, 0x00, 0x00, |
| 1079 | // Event data |
| 1080 | 0x00, 0x00, |
| 1081 | |
| 1082 | // Event 2 |
| 1083 | // PCR index |
| 1084 | 0x00, 0x00, 0x00, 0x00, |
| 1085 | // Event type |
| 1086 | 0x08, 0x00, 0x00, 0x80, |
| 1087 | // Digest count |
| 1088 | 0x04, 0x00, 0x00, 0x00, |
| 1089 | // SHA1 |
| 1090 | 0x04, 0x00, |
| 1091 | 0xc7, 0x06, 0xe7, 0xdd, 0x36, 0x39, 0x29, 0x84, 0xeb, 0x06, |
| 1092 | 0xaa, 0xa0, 0x8f, 0xf3, 0x36, 0x84, 0x40, 0x77, 0xb3, 0xed, |
| 1093 | // SHA256 |
| 1094 | 0x0b, 0x00, |
| 1095 | 0x3a, 0x30, 0x8e, 0x95, 0x87, 0x84, 0xbf, 0xd0, |
| 1096 | 0xf6, 0xe3, 0xf1, 0xbd, 0x4d, 0x42, 0x14, 0xd3, |
| 1097 | 0x0a, 0x4c, 0x55, 0x00, 0xa4, 0x5b, 0x06, 0xda, |
| 1098 | 0x96, 0xfc, 0x90, 0x33, 0x8f, 0x69, 0xb3, 0x61, |
| 1099 | // SHA384 |
| 1100 | 0x0c, 0x00, |
| 1101 | 0xc0, 0xd0, 0x75, 0x96, 0xc5, 0x9a, 0x90, 0x7b, |
| 1102 | 0x79, 0x71, 0x6f, 0xc9, 0xf3, 0x6a, 0xad, 0x8f, |
| 1103 | 0x0f, 0x26, 0xf2, 0x02, 0x67, 0x5b, 0x42, 0x5a, |
| 1104 | 0x52, 0x3f, 0x72, 0xec, 0xb6, 0xf2, 0x53, 0x99, |
| 1105 | 0x57, 0xf0, 0xd9, 0x2c, 0x0a, 0x7d, 0xce, 0xaa, |
| 1106 | 0xf9, 0x9e, 0x60, 0x0e, 0x54, 0x18, 0xf1, 0xdc, |
| 1107 | // SHA512 |
| 1108 | 0x0d, 0x00, |
| 1109 | 0x9a, 0xe9, 0x25, 0xdc, 0x9c, 0xd2, 0x9d, 0xf0, |
| 1110 | 0xe5, 0x80, 0x54, 0x35, 0xa5, 0x99, 0x06, 0x1f, |
| 1111 | 0xcf, 0x32, 0x98, 0xcc, 0x2a, 0x15, 0xe4, 0x87, |
| 1112 | 0x99, 0xa2, 0x0c, 0x9c, 0xe5, 0x6c, 0x8f, 0xe5, |
| 1113 | 0x84, 0x09, 0x75, 0xaf, 0xf0, 0xe1, 0xb6, 0x98, |
| 1114 | 0x20, 0x07, 0x5e, 0xe4, 0x29, 0x79, 0x8b, 0x5d, |
| 1115 | 0xbb, 0xe5, 0xd1, 0xa2, 0x74, 0x36, 0xab, 0x49, |
| 1116 | 0xf1, 0x9b, 0x7a, 0x04, 0x11, 0xd2, 0x96, 0x2c, |
| 1117 | // Event size |
| 1118 | 0x10, 0x00, 0x00, 0x00, |
| 1119 | // Event data |
| 1120 | 0x00, 0x00, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 1121 | 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 1122 | ]; |
| 1123 | |
| 1124 | let log = EventLog { |
| 1125 | _lifetime: PhantomData, |
| 1126 | location: bytes.as_ptr(), |
| 1127 | // SAFETY: The memory is valid. |
| 1128 | last_entry: unsafe { bytes.as_ptr().add(267) }, |
| 1129 | is_truncated: false, |
| 1130 | }; |
| 1131 | |
| 1132 | let header = log.header().unwrap(); |
| 1133 | assert_eq!(header.platform_class, 0); |
| 1134 | assert_eq!(header.spec_version, (2, 0, 0)); |
| 1135 | assert_eq!(header.uintn_size, 2); |
| 1136 | assert_eq!( |
| 1137 | header.algorithm_digest_sizes.0.to_vec(), |
| 1138 | [ |
| 1139 | AlgorithmDigestSize { |
| 1140 | algorithm_id: AlgorithmId::SHA1, |
| 1141 | digest_size: 20, |
| 1142 | }, |
| 1143 | AlgorithmDigestSize { |
| 1144 | algorithm_id: AlgorithmId::SHA256, |
| 1145 | digest_size: 32, |
| 1146 | }, |
| 1147 | AlgorithmDigestSize { |
| 1148 | algorithm_id: AlgorithmId::SHA384, |
| 1149 | digest_size: 48, |
| 1150 | }, |
| 1151 | AlgorithmDigestSize { |
| 1152 | algorithm_id: AlgorithmId::SHA512, |
| 1153 | digest_size: 64, |
| 1154 | }, |
| 1155 | ] |
| 1156 | ); |
| 1157 | assert_eq!(header.vendor_info, []); |
| 1158 | |
| 1159 | let mut iter = log.iter(); |
| 1160 | |
| 1161 | // Entry 1 |
| 1162 | let entry = iter.next().unwrap(); |
| 1163 | assert_eq!(entry.pcr_index, PcrIndex(0)); |
| 1164 | assert_eq!(entry.event_type, EventType::CRTM_VERSION); |
| 1165 | #[rustfmt::skip] |
| 1166 | assert_eq!( |
| 1167 | entry.digests().into_iter().collect::<Vec<_>>(), |
| 1168 | [ |
| 1169 | (AlgorithmId::SHA1, [ |
| 1170 | 0x14, 0x89, 0xf9, 0x23, 0xc4, 0xdc, 0xa7, 0x29, 0x17, 0x8b, |
| 1171 | 0x3e, 0x32, 0x33, 0x45, 0x85, 0x50, 0xd8, 0xdd, 0xdf, 0x29, |
| 1172 | ].as_slice()), |
| 1173 | (AlgorithmId::SHA256, [ |
| 1174 | 0x96, 0xa2, 0x96, 0xd2, 0x24, 0xf2, 0x85, 0xc6, |
| 1175 | 0x7b, 0xee, 0x93, 0xc3, 0x0f, 0x8a, 0x30, 0x91, |
| 1176 | 0x57, 0xf0, 0xda, 0xa3, 0x5d, 0xc5, 0xb8, 0x7e, |
| 1177 | 0x41, 0x0b, 0x78, 0x63, 0x0a, 0x09, 0xcf, 0xc7, |
| 1178 | ].as_slice()), |
| 1179 | (AlgorithmId::SHA384, [ |
| 1180 | 0x1d, 0xd6, 0xf7, 0xb4, 0x57, 0xad, 0x88, 0x0d, |
| 1181 | 0x84, 0x0d, 0x41, 0xc9, 0x61, 0x28, 0x3b, 0xab, |
| 1182 | 0x68, 0x8e, 0x94, 0xe4, 0xb5, 0x93, 0x59, 0xea, |
| 1183 | 0x45, 0x68, 0x65, 0x81, 0xe9, 0x0f, 0xec, 0xce, |
| 1184 | 0xa3, 0xc6, 0x24, 0xb1, 0x22, 0x61, 0x13, 0xf8, |
| 1185 | 0x24, 0xf3, 0x15, 0xeb, 0x60, 0xae, 0x0a, 0x7c, |
| 1186 | ].as_slice()), |
| 1187 | (AlgorithmId::SHA512, [ |
| 1188 | 0x5e, 0xa7, 0x1d, 0xc6, 0xd0, 0xb4, 0xf5, 0x7b, |
| 1189 | 0xf3, 0x9a, 0xad, 0xd0, 0x7c, 0x20, 0x8c, 0x35, |
| 1190 | 0xf0, 0x6c, 0xd2, 0xba, 0xc5, 0xfd, 0xe2, 0x10, |
| 1191 | 0x39, 0x7f, 0x70, 0xde, 0x11, 0xd4, 0x39, 0xc6, |
| 1192 | 0x2e, 0xc1, 0xcd, 0xf3, 0x18, 0x37, 0x58, 0x86, |
| 1193 | 0x5f, 0xd3, 0x87, 0xfc, 0xea, 0x0b, 0xad, 0xa2, |
| 1194 | 0xf6, 0xc3, 0x7a, 0x4a, 0x17, 0x85, 0x1d, 0xd1, |
| 1195 | 0xd7, 0x8f, 0xef, 0xe6, 0xf2, 0x04, 0xee, 0x54, |
| 1196 | ].as_slice()), |
| 1197 | ] |
| 1198 | ); |
| 1199 | assert_eq!(entry.event_data, [0, 0]); |
| 1200 | |
| 1201 | // Entry 2 |
| 1202 | let entry = iter.next().unwrap(); |
| 1203 | assert_eq!(entry.pcr_index, PcrIndex(0)); |
| 1204 | assert_eq!(entry.event_type, EventType::EFI_PLATFORM_FIRMWARE_BLOB); |
| 1205 | #[rustfmt::skip] |
| 1206 | assert_eq!( |
| 1207 | entry.digests().into_iter().collect::<Vec<_>>(), |
| 1208 | [ |
| 1209 | (AlgorithmId::SHA1, [ |
| 1210 | 0xc7, 0x06, 0xe7, 0xdd, 0x36, 0x39, 0x29, 0x84, 0xeb, 0x06, |
| 1211 | 0xaa, 0xa0, 0x8f, 0xf3, 0x36, 0x84, 0x40, 0x77, 0xb3, 0xed, |
| 1212 | ].as_slice()), |
| 1213 | (AlgorithmId::SHA256, [ |
| 1214 | 0x3a, 0x30, 0x8e, 0x95, 0x87, 0x84, 0xbf, 0xd0, |
| 1215 | 0xf6, 0xe3, 0xf1, 0xbd, 0x4d, 0x42, 0x14, 0xd3, |
| 1216 | 0x0a, 0x4c, 0x55, 0x00, 0xa4, 0x5b, 0x06, 0xda, |
| 1217 | 0x96, 0xfc, 0x90, 0x33, 0x8f, 0x69, 0xb3, 0x61, |
| 1218 | ].as_slice()), |
| 1219 | (AlgorithmId::SHA384, [ |
| 1220 | 0xc0, 0xd0, 0x75, 0x96, 0xc5, 0x9a, 0x90, 0x7b, |
| 1221 | 0x79, 0x71, 0x6f, 0xc9, 0xf3, 0x6a, 0xad, 0x8f, |
| 1222 | 0x0f, 0x26, 0xf2, 0x02, 0x67, 0x5b, 0x42, 0x5a, |
| 1223 | 0x52, 0x3f, 0x72, 0xec, 0xb6, 0xf2, 0x53, 0x99, |
| 1224 | 0x57, 0xf0, 0xd9, 0x2c, 0x0a, 0x7d, 0xce, 0xaa, |
| 1225 | 0xf9, 0x9e, 0x60, 0x0e, 0x54, 0x18, 0xf1, 0xdc, |
| 1226 | ].as_slice()), |
| 1227 | (AlgorithmId::SHA512, [ |
| 1228 | 0x9a, 0xe9, 0x25, 0xdc, 0x9c, 0xd2, 0x9d, 0xf0, |
| 1229 | 0xe5, 0x80, 0x54, 0x35, 0xa5, 0x99, 0x06, 0x1f, |
| 1230 | 0xcf, 0x32, 0x98, 0xcc, 0x2a, 0x15, 0xe4, 0x87, |
| 1231 | 0x99, 0xa2, 0x0c, 0x9c, 0xe5, 0x6c, 0x8f, 0xe5, |
| 1232 | 0x84, 0x09, 0x75, 0xaf, 0xf0, 0xe1, 0xb6, 0x98, |
| 1233 | 0x20, 0x07, 0x5e, 0xe4, 0x29, 0x79, 0x8b, 0x5d, |
| 1234 | 0xbb, 0xe5, 0xd1, 0xa2, 0x74, 0x36, 0xab, 0x49, |
| 1235 | 0xf1, 0x9b, 0x7a, 0x04, 0x11, 0xd2, 0x96, 0x2c, |
| 1236 | ].as_slice()), |
| 1237 | ] |
| 1238 | ); |
| 1239 | #[rustfmt::skip] |
| 1240 | assert_eq!(entry.event_data, [ |
| 1241 | 0x00, 0x00, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 1242 | 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 1243 | ]); |
| 1244 | |
| 1245 | assert!(iter.next().is_none()); |
| 1246 | } |
| 1247 | } |