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 / v1.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! [TCG] (Trusted Computing Group) protocol for [TPM] (Trusted Platform |
| 4 | //! Module) 1.1 and 1.2. |
| 5 | //! |
| 6 | //! This protocol is defined in the [TCG EFI Protocol Specification _for |
| 7 | //! TPM Family 1.1 or 1.2_][spec]. |
| 8 | //! |
| 9 | //! [spec]: https://trustedcomputinggroup.org/resource/tcg-efi-protocol-specification/ |
| 10 | //! [TCG]: https://trustedcomputinggroup.org/ |
| 11 | //! [TPM]: https://en.wikipedia.org/wiki/Trusted_Platform_Module |
| 12 | |
| 13 | use super::{AlgorithmId, EventType, HashAlgorithm, PcrIndex}; |
| 14 | use crate::data_types::{Align, PhysicalAddress}; |
| 15 | use crate::proto::unsafe_protocol; |
| 16 | use crate::util::{ptr_write_unaligned_and_add, usize_from_u32}; |
| 17 | use crate::{Error, Result, Status, StatusExt}; |
| 18 | use core::fmt::{self, Debug, Formatter}; |
| 19 | use core::marker::PhantomData; |
| 20 | use core::ptr; |
| 21 | use ptr_meta::Pointee; |
| 22 | use uefi_raw::protocol::tcg::v1::{TcgBootServiceCapability, TcgProtocol}; |
| 23 | |
| 24 | #[cfg(feature = "alloc")] |
| 25 | use {crate::mem::make_boxed, alloc::boxed::Box}; |
| 26 | |
| 27 | pub use uefi_raw::protocol::tcg::v1::TcgVersion as Version; |
| 28 | |
| 29 | /// 20-byte SHA-1 digest. |
| 30 | pub type Sha1Digest = [u8; 20]; |
| 31 | |
| 32 | /// Information about the protocol and the TPM device. |
| 33 | /// |
| 34 | /// Layout compatible with the C type `TCG_EFI_BOOT_SERVICE_CAPABILITY`. |
| 35 | #[repr(C)] |
| 36 | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)] |
| 37 | pub struct BootServiceCapability(TcgBootServiceCapability); |
| 38 | |
| 39 | impl BootServiceCapability { |
| 40 | /// Version of the `BootServiceCapability` structure. |
| 41 | #[must_use] |
| 42 | pub const fn structure_version(&self) -> Version { |
| 43 | self.0.structure_version |
| 44 | } |
| 45 | |
| 46 | /// Version of the `Tcg` protocol. |
| 47 | #[must_use] |
| 48 | pub const fn protocol_spec_version(&self) -> Version { |
| 49 | self.0.protocol_spec_version |
| 50 | } |
| 51 | |
| 52 | /// Supported hash algorithms. |
| 53 | #[must_use] |
| 54 | pub fn hash_algorithm(&self) -> HashAlgorithm { |
| 55 | // Safety: the value should always be 0x1 (indicating SHA-1), but |
| 56 | // we don't care if it's some unexpected value. |
| 57 | HashAlgorithm::from_bits_retain(u32::from(self.0.hash_algorithm_bitmap)) |
| 58 | } |
| 59 | |
| 60 | /// Whether the TPM device is present. |
| 61 | #[must_use] |
| 62 | pub const fn tpm_present(&self) -> bool { |
| 63 | self.0.tpm_present_flag != 0 |
| 64 | } |
| 65 | |
| 66 | /// Whether the TPM device is deactivated. |
| 67 | #[must_use] |
| 68 | pub const fn tpm_deactivated(&self) -> bool { |
| 69 | self.0.tpm_deactivated_flag != 0 |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /// Entry in the [`EventLog`]. |
| 74 | /// |
| 75 | /// Layout compatible with the C type `TCG_PCR_EVENT`. |
| 76 | /// |
| 77 | /// Naming note: the spec refers to "event data" in two conflicting |
| 78 | /// ways: the `event_data` field and the data hashed in the digest |
| 79 | /// field. These two are independent; although the event data _can_ be |
| 80 | /// what is hashed in the digest field, it doesn't have to be. |
| 81 | #[repr(C, packed)] |
| 82 | #[derive(Eq, Pointee)] |
| 83 | pub struct PcrEvent { |
| 84 | pcr_index: PcrIndex, |
| 85 | event_type: EventType, |
| 86 | digest: Sha1Digest, |
| 87 | event_data_size: u32, |
| 88 | event_data: [u8], |
| 89 | } |
| 90 | |
| 91 | impl PcrEvent { |
| 92 | /// Size of everything but the event data. |
| 93 | const HEADER_SIZE: usize = |
| 94 | size_of::<PcrIndex>() + size_of::<EventType>() + size_of::<Sha1Digest>() + size_of::<u32>(); |
| 95 | |
| 96 | /// Creates a reference to the event at `ptr`. |
| 97 | /// |
| 98 | /// `last_entry` is the start of the last event in the log. Events are |
| 99 | /// contiguous, so an event before it must end at or before it, and |
| 100 | /// `None` is returned if it does not. The log does not report where the |
| 101 | /// last event ends, so the size of that event cannot be checked. |
| 102 | /// |
| 103 | /// # Safety |
| 104 | /// |
| 105 | /// The memory from `ptr` up to `last_entry`, or up to the end of the |
| 106 | /// event if `ptr` is the last entry, must be valid for reads. |
| 107 | pub(super) unsafe fn from_ptr<'a>(ptr: *const u8, last_entry: *const u8) -> Option<&'a Self> { |
| 108 | // `None` for the last event, whose end is unknown. |
| 109 | let available = (ptr < last_entry).then(|| last_entry.addr() - ptr.addr()); |
| 110 | let fits = |size: usize| available.is_none_or(|available| size <= available); |
| 111 | |
| 112 | if !fits(Self::HEADER_SIZE) { |
| 113 | return None; |
| 114 | } |
| 115 | // Get the `event_size` field. |
| 116 | let ptr_u32: *const u32 = ptr.cast(); |
| 117 | // SAFETY: The source memory may be unaligned, so this uses unaligned access. |
| 118 | let event_size = unsafe { ptr_u32.add(7).read_unaligned() }; |
| 119 | let event_size = usize_from_u32(event_size); |
| 120 | if !fits(Self::HEADER_SIZE.checked_add(event_size)?) { |
| 121 | return None; |
| 122 | } |
| 123 | // SAFETY: The memory is valid. |
| 124 | Some(unsafe { &*ptr_meta::from_raw_parts(ptr.cast(), event_size) }) |
| 125 | } |
| 126 | |
| 127 | /// Create a new `PcrEvent` using a byte buffer for storage. |
| 128 | /// |
| 129 | /// # Errors |
| 130 | /// |
| 131 | /// Returns [`Status::BUFFER_TOO_SMALL`] if the `buffer` is not large |
| 132 | /// enough. The required size will be returned in the error data. |
| 133 | /// |
| 134 | /// Returns [`Status::INVALID_PARAMETER`] if the `event_data` size is too |
| 135 | /// large. |
| 136 | pub fn new_in_buffer<'buf>( |
| 137 | buffer: &'buf mut [u8], |
| 138 | pcr_index: PcrIndex, |
| 139 | event_type: EventType, |
| 140 | digest: Sha1Digest, |
| 141 | event_data: &[u8], |
| 142 | ) -> Result<&'buf mut Self, Option<usize>> { |
| 143 | let event_data_size = u32::try_from(event_data.len()) |
| 144 | .map_err(|_| Error::new(Status::INVALID_PARAMETER, None))?; |
| 145 | |
| 146 | let required_size = size_of::<PcrIndex>() |
| 147 | + size_of::<EventType>() |
| 148 | + size_of::<Sha1Digest>() |
| 149 | + size_of::<u32>() |
| 150 | + event_data.len(); |
| 151 | |
| 152 | if buffer.len() < required_size { |
| 153 | return Err(Error::new(Status::BUFFER_TOO_SMALL, Some(required_size))); |
| 154 | } |
| 155 | |
| 156 | let mut ptr: *mut u8 = buffer.as_mut_ptr().cast(); |
| 157 | |
| 158 | // SAFETY: The memory is valid. |
| 159 | unsafe { |
| 160 | ptr_write_unaligned_and_add(&mut ptr, pcr_index); |
| 161 | ptr_write_unaligned_and_add(&mut ptr, event_type); |
| 162 | ptr_write_unaligned_and_add(&mut ptr, digest); |
| 163 | ptr_write_unaligned_and_add(&mut ptr, event_data_size); |
| 164 | ptr::copy(event_data.as_ptr(), ptr, event_data.len()); |
| 165 | |
| 166 | let ptr: *mut Self = |
| 167 | ptr_meta::from_raw_parts_mut(buffer.as_mut_ptr().cast(), event_data.len()); |
| 168 | Ok(&mut *ptr) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /// Create a new `PcrEvent` in a [`Box`]. |
| 173 | /// |
| 174 | /// # Errors |
| 175 | /// |
| 176 | /// Returns [`Status::INVALID_PARAMETER`] if the `event_data` size is too |
| 177 | /// large. |
| 178 | #[cfg(feature = "alloc")] |
| 179 | pub fn new_in_box( |
| 180 | pcr_index: PcrIndex, |
| 181 | event_type: EventType, |
| 182 | digest: Sha1Digest, |
| 183 | event_data: &[u8], |
| 184 | ) -> Result<Box<Self>> { |
| 185 | make_boxed(|buf| Self::new_in_buffer(buf, pcr_index, event_type, digest, event_data)) |
| 186 | } |
| 187 | |
| 188 | /// PCR index for the event. |
| 189 | #[must_use] |
| 190 | pub const fn pcr_index(&self) -> PcrIndex { |
| 191 | self.pcr_index |
| 192 | } |
| 193 | |
| 194 | /// Type of event, indicating what type of data is stored in [`event_data`]. |
| 195 | /// |
| 196 | /// [`event_data`]: Self::event_data |
| 197 | #[must_use] |
| 198 | pub const fn event_type(&self) -> EventType { |
| 199 | self.event_type |
| 200 | } |
| 201 | |
| 202 | /// Raw event data. The meaning of this data can be determined from |
| 203 | /// the [`event_type`]. |
| 204 | /// |
| 205 | /// Note that this data is independent of what is hashed [`digest`]. |
| 206 | /// |
| 207 | /// [`digest`]: Self::digest |
| 208 | /// [`event_type`]: Self::event_type |
| 209 | #[must_use] |
| 210 | pub const fn event_data(&self) -> &[u8] { |
| 211 | &self.event_data |
| 212 | } |
| 213 | |
| 214 | /// SHA-1 digest of the data hashed for this event. |
| 215 | #[must_use] |
| 216 | pub const fn digest(&self) -> Sha1Digest { |
| 217 | self.digest |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | impl Align for PcrEvent { |
| 222 | fn alignment() -> usize { |
| 223 | 1 |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // Manual `Debug` implementation since it can't be derived for a packed DST. |
| 228 | impl Debug for PcrEvent { |
| 229 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 230 | f.debug_struct("PcrEvent") |
| 231 | .field("pcr_index", &{ self.pcr_index }) |
| 232 | .field("event_type", &{ self.event_type }) |
| 233 | .field("digest", &self.digest) |
| 234 | .field("event_data_size", &{ self.event_data_size }) |
| 235 | .field("event_data", &&self.event_data) |
| 236 | .finish() |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // Manual `PartialEq` implementation since it can't be derived for a packed DST. |
| 241 | impl PartialEq for PcrEvent { |
| 242 | fn eq(&self, rhs: &Self) -> bool { |
| 243 | self.pcr_index() == rhs.pcr_index() |
| 244 | && self.event_type() == rhs.event_type() |
| 245 | && self.digest == rhs.digest |
| 246 | && self.event_data_size == rhs.event_data_size |
| 247 | && self.event_data == rhs.event_data |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | opaque_type! { |
| 252 | /// Opaque type that should be used to represent a pointer to a [`PcrEvent`] in |
| 253 | /// foreign function interfaces. This type produces a thin pointer, unlike |
| 254 | /// [`PcrEvent`]. |
| 255 | pub struct FfiPcrEvent; |
| 256 | } |
| 257 | |
| 258 | /// TPM event log. |
| 259 | /// |
| 260 | /// This type of event log always uses SHA-1 hashes. The [`v1::Tcg`] |
| 261 | /// protocol always uses this type of event log, but it can also be |
| 262 | /// provided by the [`v2::Tcg`] protocol via [`get_event_log_v2`]. |
| 263 | /// |
| 264 | /// [`v1::Tcg`]: Tcg |
| 265 | /// [`v2::Tcg`]: super::v2::Tcg |
| 266 | /// [`get_event_log_v2`]: super::v2::Tcg::get_event_log_v2 |
| 267 | #[derive(Debug)] |
| 268 | pub struct EventLog<'a> { |
| 269 | // Tie the lifetime to the protocol, and by extension, boot services. |
| 270 | _lifetime: PhantomData<&'a Tcg>, |
| 271 | |
| 272 | location: *const u8, |
| 273 | last_entry: *const u8, |
| 274 | |
| 275 | is_truncated: bool, |
| 276 | } |
| 277 | |
| 278 | impl EventLog<'_> { |
| 279 | pub(super) const unsafe fn new( |
| 280 | location: *const u8, |
| 281 | last_entry: *const u8, |
| 282 | is_truncated: bool, |
| 283 | ) -> Self { |
| 284 | Self { |
| 285 | _lifetime: PhantomData, |
| 286 | location, |
| 287 | last_entry, |
| 288 | is_truncated, |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /// Iterator of events in the log. |
| 293 | /// |
| 294 | /// The firmware reports where the last event starts, but not where the |
| 295 | /// log ends. Every event before the last one must end at or before the |
| 296 | /// last one, and the iterator stops at the first event that does not. |
| 297 | /// The size of the last event is taken from the log without a check. |
| 298 | #[must_use] |
| 299 | pub const fn iter(&self) -> EventLogIter<'_> { |
| 300 | EventLogIter { |
| 301 | log: self, |
| 302 | location: self.location, |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /// If true, the event log is missing one or more entries because |
| 307 | /// additional events would have exceeded the space allocated for |
| 308 | /// the log. |
| 309 | /// |
| 310 | /// This value is not reported for the [`v1::Tcg`] protocol, so it |
| 311 | /// is always `false` in that case. |
| 312 | /// |
| 313 | /// [`v1::Tcg`]: Tcg |
| 314 | #[must_use] |
| 315 | pub const fn is_truncated(&self) -> bool { |
| 316 | self.is_truncated |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /// Iterator for events in [`EventLog`]. |
| 321 | #[derive(Debug)] |
| 322 | pub struct EventLogIter<'a> { |
| 323 | log: &'a EventLog<'a>, |
| 324 | location: *const u8, |
| 325 | } |
| 326 | |
| 327 | impl<'a> Iterator for EventLogIter<'a> { |
| 328 | type Item = &'a PcrEvent; |
| 329 | |
| 330 | fn next(&mut self) -> Option<Self::Item> { |
| 331 | // The spec says that `last_entry` will be null if there are no |
| 332 | // events. Presumably `location` will be null as well, but check |
| 333 | // both just to be safe. |
| 334 | if self.location.is_null() || self.log.last_entry.is_null() { |
| 335 | return None; |
| 336 | } |
| 337 | |
| 338 | // `last_entry` points to the start of the last event, so the log |
| 339 | // ends once the location moves past it. Comparing with `>` rather |
| 340 | // than for equality also ends the iteration if `last_entry` is not |
| 341 | // on an event boundary, instead of walking past the end of the log. |
| 342 | if self.location > self.log.last_entry { |
| 343 | return None; |
| 344 | } |
| 345 | |
| 346 | // Safety: we trust that the protocol has given us a valid range |
| 347 | // of memory to read from. |
| 348 | // SAFETY: The memory is valid. |
| 349 | let event = unsafe { PcrEvent::from_ptr(self.location, self.log.last_entry) }?; |
| 350 | |
| 351 | // SAFETY: The memory is valid. |
| 352 | self.location = unsafe { self.location.add(size_of_val(event)) }; |
| 353 | |
| 354 | Some(event) |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /// Trusted Computing Group [`Protocol`] (TCG) for TPM 1.1 and 1.2 devices. |
| 359 | /// |
| 360 | /// The corresponding C type is `EFI_TCG_PROTOCOL`. |
| 361 | /// |
| 362 | /// [`Protocol`]: uefi::proto::Protocol |
| 363 | #[derive(Debug)] |
| 364 | #[repr(transparent)] |
| 365 | #[unsafe_protocol(TcgProtocol::GUID)] |
| 366 | pub struct Tcg(TcgProtocol); |
| 367 | |
| 368 | /// Return type of [`Tcg::status_check`]. |
| 369 | #[derive(Debug)] |
| 370 | pub struct StatusCheck<'a> { |
| 371 | /// Information about the protocol and the TPM device. |
| 372 | pub protocol_capability: BootServiceCapability, |
| 373 | |
| 374 | /// Feature flags. The spec does not define any feature flags, so |
| 375 | /// this is always expected to be zero. |
| 376 | pub feature_flags: u32, |
| 377 | |
| 378 | /// TPM event log. |
| 379 | pub event_log: EventLog<'a>, |
| 380 | } |
| 381 | |
| 382 | impl Tcg { |
| 383 | /// Get information about the protocol and TPM device, as well as |
| 384 | /// the TPM event log. |
| 385 | pub fn status_check(&mut self) -> Result<StatusCheck<'_>> { |
| 386 | let mut protocol_capability = TcgBootServiceCapability::default(); |
| 387 | let mut feature_flags = 0; |
| 388 | let mut event_log_location = 0; |
| 389 | let mut event_log_last_entry = 0; |
| 390 | |
| 391 | // SAFETY: The memory is valid. |
| 392 | let status = unsafe { |
| 393 | (self.0.status_check)( |
| 394 | &mut self.0, |
| 395 | &mut protocol_capability, |
| 396 | &mut feature_flags, |
| 397 | &mut event_log_location, |
| 398 | &mut event_log_last_entry, |
| 399 | ) |
| 400 | }; |
| 401 | |
| 402 | if status.is_success() { |
| 403 | // The truncated field is just there for the v2 protocol; |
| 404 | // always set it to false for v1. |
| 405 | let truncated = false; |
| 406 | // SAFETY: The memory is valid. |
| 407 | let event_log = unsafe { |
| 408 | EventLog::new( |
| 409 | event_log_location as *const u8, |
| 410 | event_log_last_entry as *const u8, |
| 411 | truncated, |
| 412 | ) |
| 413 | }; |
| 414 | |
| 415 | Ok(StatusCheck { |
| 416 | protocol_capability: BootServiceCapability(protocol_capability), |
| 417 | feature_flags, |
| 418 | event_log, |
| 419 | }) |
| 420 | } else { |
| 421 | Err(status.into()) |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | /// Add an entry to the event log without extending a PCR. |
| 426 | /// |
| 427 | /// Usually [`hash_log_extend_event`] should be used instead. An |
| 428 | /// entry added via `log_event` cannot be verified, so it is mainly |
| 429 | /// intended for adding an informational entry. |
| 430 | /// |
| 431 | /// [`hash_log_extend_event`]: Self::hash_log_extend_event |
| 432 | pub fn log_event(&mut self, event: &PcrEvent) -> Result { |
| 433 | // This is the only valid value; it indicates that the extend |
| 434 | // operation should not be performed. |
| 435 | let flags = 0x1; |
| 436 | |
| 437 | // Don't bother returning this, it's not very useful info. |
| 438 | let mut event_number = 0; |
| 439 | |
| 440 | let event_ptr: *const PcrEvent = event; |
| 441 | |
| 442 | // SAFETY: The memory is valid. |
| 443 | unsafe { |
| 444 | (self.0.log_event)(&mut self.0, event_ptr.cast(), &mut event_number, flags).to_result() |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | /// Extend a PCR and add an entry to the event log. |
| 449 | /// |
| 450 | /// If `data_to_hash` is `None` then the `digest` field of the `event` |
| 451 | /// should be used as-is. Otherwise, the `digest` field will be overwritten |
| 452 | /// with the SHA-1 hash of the data. |
| 453 | pub fn hash_log_extend_event( |
| 454 | &mut self, |
| 455 | event: &mut PcrEvent, |
| 456 | data_to_hash: Option<&[u8]>, |
| 457 | ) -> Result { |
| 458 | let hash_data; |
| 459 | let hash_data_len = if let Some(data_to_hash) = data_to_hash { |
| 460 | hash_data = data_to_hash.as_ptr() as PhysicalAddress; |
| 461 | u64::try_from(data_to_hash.len()).unwrap() |
| 462 | } else { |
| 463 | hash_data = 0; |
| 464 | 0 |
| 465 | }; |
| 466 | |
| 467 | // Don't bother returning these, it's not very useful info. |
| 468 | let mut event_number = 0; |
| 469 | let mut event_log_last_entry = 0; |
| 470 | |
| 471 | let event_ptr: *mut PcrEvent = event; |
| 472 | |
| 473 | // SAFETY: The memory is valid. |
| 474 | unsafe { |
| 475 | (self.0.hash_log_extend_event)( |
| 476 | &mut self.0, |
| 477 | hash_data, |
| 478 | hash_data_len, |
| 479 | AlgorithmId::SHA1.0.into(), |
| 480 | event_ptr.cast(), |
| 481 | &mut event_number, |
| 482 | &mut event_log_last_entry, |
| 483 | ) |
| 484 | .to_result() |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /// Send a command directly to the TPM. |
| 489 | /// |
| 490 | /// Constructing the input block and parsing the output block are outside |
| 491 | /// the scope of this crate. See the [TPM 1.2 Main Specification][spec] |
| 492 | /// documents for details of these blocks, in particular Part 3, Commands. |
| 493 | /// |
| 494 | /// Note that TPM structures are big endian. |
| 495 | /// |
| 496 | /// [spec]: https://trustedcomputinggroup.org/resource/tpm-main-specification/ |
| 497 | pub fn pass_through_to_tpm( |
| 498 | &mut self, |
| 499 | input_parameter_block: &[u8], |
| 500 | output_parameter_block: &mut [u8], |
| 501 | ) -> Result { |
| 502 | let input_parameter_block_len = u32::try_from(input_parameter_block.len()) |
| 503 | .map_err(|_| Error::from(Status::BAD_BUFFER_SIZE))?; |
| 504 | let output_parameter_block_len = u32::try_from(output_parameter_block.len()) |
| 505 | .map_err(|_| Error::from(Status::BAD_BUFFER_SIZE))?; |
| 506 | |
| 507 | // SAFETY: The memory is valid. |
| 508 | unsafe { |
| 509 | (self.0.pass_through_to_tpm)( |
| 510 | &mut self.0, |
| 511 | input_parameter_block_len, |
| 512 | input_parameter_block.as_ptr(), |
| 513 | output_parameter_block_len, |
| 514 | output_parameter_block.as_mut_ptr(), |
| 515 | ) |
| 516 | .to_result() |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | #[cfg(test)] |
| 522 | mod tests { |
| 523 | use super::*; |
| 524 | use core::slice; |
| 525 | |
| 526 | #[test] |
| 527 | fn test_new_pcr_event() { |
| 528 | let mut event_buf = [0; 256]; |
| 529 | #[rustfmt::skip] |
| 530 | let digest = [ |
| 531 | 0x00, 0x01, 0x02, 0x03, |
| 532 | 0x04, 0x05, 0x06, 0x07, |
| 533 | 0x08, 0x09, 0x0a, 0x0b, |
| 534 | 0x0c, 0x0d, 0x0e, 0x0f, |
| 535 | 0x10, 0x11, 0x12, 0x13, |
| 536 | ]; |
| 537 | let data = [0x14, 0x15, 0x16, 0x17]; |
| 538 | let event = |
| 539 | PcrEvent::new_in_buffer(&mut event_buf, PcrIndex(4), EventType::IPL, digest, &data) |
| 540 | .unwrap(); |
| 541 | assert_eq!(event.pcr_index(), PcrIndex(4)); |
| 542 | assert_eq!(event.event_type(), EventType::IPL); |
| 543 | assert_eq!(event.digest(), digest); |
| 544 | assert_eq!(event.event_data(), data); |
| 545 | |
| 546 | let event_ptr: *const PcrEvent = event; |
| 547 | // SAFETY: The pointer is valid for the requested slice length. |
| 548 | let bytes = unsafe { slice::from_raw_parts(event_ptr.cast::<u8>(), size_of_val(event)) }; |
| 549 | #[rustfmt::skip] |
| 550 | assert_eq!(bytes, [ |
| 551 | // PCR index |
| 552 | 0x04, 0x00, 0x00, 0x00, |
| 553 | // Event type |
| 554 | 0x0d, 0x00, 0x00, 0x00, |
| 555 | // Digest |
| 556 | 0x00, 0x01, 0x02, 0x03, |
| 557 | 0x04, 0x05, 0x06, 0x07, |
| 558 | 0x08, 0x09, 0x0a, 0x0b, |
| 559 | 0x0c, 0x0d, 0x0e, 0x0f, |
| 560 | 0x10, 0x11, 0x12, 0x13, |
| 561 | // Event data len |
| 562 | 0x04, 0x00, 0x00, 0x00, |
| 563 | // Event data |
| 564 | 0x14, 0x15, 0x16, 0x17, |
| 565 | ]); |
| 566 | |
| 567 | // Check that `new_in_box` gives the same value. |
| 568 | assert_eq!( |
| 569 | event, |
| 570 | &*PcrEvent::new_in_box(PcrIndex(4), EventType::IPL, digest, &data).unwrap() |
| 571 | ); |
| 572 | } |
| 573 | |
| 574 | /// A `last_entry` that is not on an event boundary must not make the |
| 575 | /// iterator walk past the end of the log. The first event already |
| 576 | /// extends past `last_entry`, so the log is malformed and empty. |
| 577 | #[test] |
| 578 | fn test_event_log_v1_last_entry_not_on_boundary() { |
| 579 | #[rustfmt::skip] |
| 580 | let bytes = [ |
| 581 | // Event 1 |
| 582 | // PCR index |
| 583 | 0x00, 0x00, 0x00, 0x00, |
| 584 | // Event type |
| 585 | 0x08, 0x00, 0x00, 0x00, |
| 586 | // SHA1 digest |
| 587 | 0x14, 0x89, 0xf9, 0x23, 0xc4, 0xdc, 0xa7, 0x29, 0x17, 0x8b, |
| 588 | 0x3e, 0x32, 0x33, 0x45, 0x85, 0x50, 0xd8, 0xdd, 0xdf, 0x29, |
| 589 | // Event data size |
| 590 | 0x02, 0x00, 0x00, 0x00, |
| 591 | // Event data |
| 592 | 0x00, 0x00, |
| 593 | |
| 594 | // Event 2 |
| 595 | // PCR index |
| 596 | 0x00, 0x00, 0x00, 0x00, |
| 597 | // Event type |
| 598 | 0x08, 0x00, 0x00, 0x80, |
| 599 | // SHA1 digest |
| 600 | 0xc7, 0x06, 0xe7, 0xdd, 0x36, 0x39, 0x29, 0x84, 0xeb, 0x06, |
| 601 | 0xaa, 0xa0, 0x8f, 0xf3, 0x36, 0x84, 0x40, 0x77, 0xb3, 0xed, |
| 602 | // Event data size |
| 603 | 0x02, 0x00, 0x00, 0x00, |
| 604 | // Event data |
| 605 | 0x00, 0x00, |
| 606 | ]; |
| 607 | |
| 608 | // `last_entry` points into the middle of event 1. |
| 609 | // SAFETY: The memory is valid. |
| 610 | let log = unsafe { EventLog::new(bytes.as_ptr(), bytes.as_ptr().add(1), false) }; |
| 611 | let mut iter = log.iter(); |
| 612 | assert!(iter.next().is_none()); |
| 613 | assert!(iter.next().is_none()); |
| 614 | } |
| 615 | |
| 616 | /// An event before the last one must end at or before `last_entry`. |
| 617 | /// Otherwise its size would turn into a reference past the log. |
| 618 | #[test] |
| 619 | fn test_event_log_v1_event_size_too_large() { |
| 620 | #[rustfmt::skip] |
| 621 | let bytes = [ |
| 622 | // PCR index |
| 623 | 0x00, 0x00, 0x00, 0x00, |
| 624 | // Event type |
| 625 | 0x08, 0x00, 0x00, 0x00, |
| 626 | // SHA1 digest |
| 627 | 0x14, 0x89, 0xf9, 0x23, 0xc4, 0xdc, 0xa7, 0x29, 0x17, 0x8b, |
| 628 | 0x3e, 0x32, 0x33, 0x45, 0x85, 0x50, 0xd8, 0xdd, 0xdf, 0x29, |
| 629 | // Event data size: larger than the log |
| 630 | 0x00, 0x01, 0x00, 0x00, |
| 631 | // Event data |
| 632 | 0x00, 0x00, |
| 633 | ]; |
| 634 | |
| 635 | // `last_entry` points right after the event data. |
| 636 | // SAFETY: The memory is valid. |
| 637 | let log = unsafe { EventLog::new(bytes.as_ptr(), bytes.as_ptr().add(bytes.len()), false) }; |
| 638 | let mut iter = log.iter(); |
| 639 | assert!(iter.next().is_none()); |
| 640 | assert!(iter.next().is_none()); |
| 641 | } |
| 642 | |
| 643 | #[test] |
| 644 | fn test_event_log_v1() { |
| 645 | // This data comes from dumping the TPM event log in a VM |
| 646 | // (truncated to just two entries). |
| 647 | #[rustfmt::skip] |
| 648 | let bytes = [ |
| 649 | // Event 1 |
| 650 | // PCR index |
| 651 | 0x00, 0x00, 0x00, 0x00, |
| 652 | // Event type |
| 653 | 0x08, 0x00, 0x00, 0x00, |
| 654 | // SHA1 digest |
| 655 | 0x14, 0x89, 0xf9, 0x23, 0xc4, 0xdc, 0xa7, 0x29, 0x17, 0x8b, |
| 656 | 0x3e, 0x32, 0x33, 0x45, 0x85, 0x50, 0xd8, 0xdd, 0xdf, 0x29, |
| 657 | // Event data size |
| 658 | 0x02, 0x00, 0x00, 0x00, |
| 659 | // Event data |
| 660 | 0x00, 0x00, |
| 661 | |
| 662 | // Event 2 |
| 663 | // PCR index |
| 664 | 0x00, 0x00, 0x00, 0x00, |
| 665 | // Event type |
| 666 | 0x08, 0x00, 0x00, 0x80, |
| 667 | // SHA1 digest |
| 668 | 0xc7, 0x06, 0xe7, 0xdd, 0x36, 0x39, 0x29, 0x84, 0xeb, 0x06, |
| 669 | 0xaa, 0xa0, 0x8f, 0xf3, 0x36, 0x84, 0x40, 0x77, 0xb3, 0xed, |
| 670 | // Event data size |
| 671 | 0x10, 0x00, 0x00, 0x00, |
| 672 | // Event data |
| 673 | 0x00, 0x00, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 674 | 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 675 | ]; |
| 676 | |
| 677 | // SAFETY: The memory is valid. |
| 678 | let log = unsafe { EventLog::new(bytes.as_ptr(), bytes.as_ptr().add(34), false) }; |
| 679 | let mut iter = log.iter(); |
| 680 | |
| 681 | // Entry 1 |
| 682 | let entry = iter.next().unwrap(); |
| 683 | assert_eq!(entry.pcr_index(), PcrIndex(0)); |
| 684 | assert_eq!(entry.event_type(), EventType::CRTM_VERSION); |
| 685 | #[rustfmt::skip] |
| 686 | assert_eq!( |
| 687 | entry.digest(), |
| 688 | [ |
| 689 | 0x14, 0x89, 0xf9, 0x23, 0xc4, 0xdc, 0xa7, 0x29, 0x17, 0x8b, |
| 690 | 0x3e, 0x32, 0x33, 0x45, 0x85, 0x50, 0xd8, 0xdd, 0xdf, 0x29, |
| 691 | ] |
| 692 | ); |
| 693 | assert_eq!(entry.event_data(), [0x00, 0x00]); |
| 694 | |
| 695 | // Entry 2 |
| 696 | let entry = iter.next().unwrap(); |
| 697 | assert_eq!(entry.pcr_index(), PcrIndex(0)); |
| 698 | assert_eq!(entry.event_type(), EventType::EFI_PLATFORM_FIRMWARE_BLOB); |
| 699 | #[rustfmt::skip] |
| 700 | assert_eq!( |
| 701 | entry.digest(), |
| 702 | [ |
| 703 | 0xc7, 0x06, 0xe7, 0xdd, 0x36, 0x39, 0x29, 0x84, 0xeb, 0x06, |
| 704 | 0xaa, 0xa0, 0x8f, 0xf3, 0x36, 0x84, 0x40, 0x77, 0xb3, 0xed, |
| 705 | ] |
| 706 | ); |
| 707 | #[rustfmt::skip] |
| 708 | assert_eq!( |
| 709 | entry.event_data(), |
| 710 | [ |
| 711 | 0x00, 0x00, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 712 | 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 713 | ] |
| 714 | ); |
| 715 | } |
| 716 | } |