|
| 1 | +//! [TCG] (Trusted Computing Group) protocol for [TPM] (Trusted Platform |
| 2 | +//! Module) 2.0. |
| 3 | +//! |
| 4 | +//! This protocol is defined in the [TCG EFI Protocol Specification _TPM |
| 5 | +//! Family 2.0_][spec]. It is generally implemented only for TPM 2.0 |
| 6 | +//! devices, but the spec indicates it can also be used for older TPM |
| 7 | +//! devices. |
| 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::EventType; |
| 14 | +use crate::{guid, Guid, PhysicalAddress, Status}; |
| 15 | +use bitflags::bitflags; |
| 16 | +use core::ffi::c_void; |
| 17 | + |
| 18 | +/// Version information. |
| 19 | +#[repr(C)] |
| 20 | +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)] |
| 21 | +pub struct Tcg2Version { |
| 22 | + /// Major version. |
| 23 | + pub major: u8, |
| 24 | + /// Minor version. |
| 25 | + pub minor: u8, |
| 26 | +} |
| 27 | + |
| 28 | +bitflags! { |
| 29 | + /// Event log formats supported by the firmware. |
| 30 | + #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)] |
| 31 | + #[repr(transparent)] |
| 32 | + pub struct Tcg2EventLogBitmap: u32 { |
| 33 | + /// Firmware supports the SHA-1 log format. |
| 34 | + const TCG_1_2 = 0x0000_0001; |
| 35 | + |
| 36 | + /// Firmware supports the crypto-agile log format. |
| 37 | + const TCG_2 = 0x0000_0002; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/// Event log formats supported by the firmware. |
| 42 | +pub type Tcg2EventLogFormat = Tcg2EventLogBitmap; |
| 43 | + |
| 44 | +bitflags! { |
| 45 | + /// Hash algorithms the protocol can provide. |
| 46 | + #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)] |
| 47 | + #[repr(transparent)] |
| 48 | + pub struct Tcg2HashAlgorithmBitmap: u32 { |
| 49 | + /// SHA-1 hash. |
| 50 | + const SHA1 = 0x0000_0001; |
| 51 | + |
| 52 | + /// SHA-256 hash. |
| 53 | + const SHA256 = 0x0000_0002; |
| 54 | + |
| 55 | + /// SHA-384 hash. |
| 56 | + const SHA384 = 0x0000_0004; |
| 57 | + |
| 58 | + /// SHA-512 hash. |
| 59 | + const SHA512 = 0x0000_0008; |
| 60 | + |
| 61 | + /// SM3-256 hash. |
| 62 | + const SM3_256 = 0x0000_0010; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/// Information about the protocol and the TPM device. |
| 67 | +#[repr(C)] |
| 68 | +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)] |
| 69 | +pub struct Tcg2BootServiceCapability { |
| 70 | + /// Size of this structure. |
| 71 | + pub size: u8, |
| 72 | + |
| 73 | + /// Version of the EFI TCG2 protocol. |
| 74 | + pub structure_version: Tcg2Version, |
| 75 | + |
| 76 | + /// Version of the EFI TCG2 protocol. |
| 77 | + pub protocol_version: Tcg2Version, |
| 78 | + |
| 79 | + /// Bitmap of supported hash algorithms. |
| 80 | + pub hash_algorithm_bitmap: Tcg2HashAlgorithmBitmap, |
| 81 | + |
| 82 | + /// Event log formats supported by the firmware. |
| 83 | + pub supported_event_logs: Tcg2EventLogBitmap, |
| 84 | + |
| 85 | + /// Whether the TPM is present or not. |
| 86 | + pub tpm_present_flag: u8, |
| 87 | + |
| 88 | + /// Maximum size (in bytes) of a command that can be sent to the TPM. |
| 89 | + pub max_command_size: u16, |
| 90 | + |
| 91 | + /// Maximum size (in bytes) of a response that can be provided by the TPM. |
| 92 | + pub max_response_size: u16, |
| 93 | + |
| 94 | + /// Manufacturer ID. |
| 95 | + /// |
| 96 | + /// See the [TCG Vendor ID registry]. |
| 97 | + /// |
| 98 | + /// [TCG Vendor ID registry]: https://trustedcomputinggroup.org/resource/vendor-id-registry/ |
| 99 | + pub manufacturer_id: u32, |
| 100 | + |
| 101 | + /// Maximum number of supported PCR banks (hashing algorithms). |
| 102 | + pub number_of_pcr_banks: u32, |
| 103 | + |
| 104 | + /// Bitmap of currently-active PCR banks (hashing algorithms). This |
| 105 | + /// is a subset of the supported algorithms in [`hash_algorithm_bitmap`]. |
| 106 | + /// |
| 107 | + /// [`hash_algorithm_bitmap`]: Self::hash_algorithm_bitmap |
| 108 | + pub active_pcr_banks: Tcg2HashAlgorithmBitmap, |
| 109 | +} |
| 110 | + |
| 111 | +bitflags! { |
| 112 | + /// Flags for the [`Tcg::hash_log_extend_event`] function. |
| 113 | + #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)] |
| 114 | + #[repr(transparent)] |
| 115 | + pub struct Tcg2HashLogExtendEventFlags: u64 { |
| 116 | + /// Extend an event but don't log it. |
| 117 | + const EFI_TCG2_EXTEND_ONLY = 0x0000_0000_0000_0001; |
| 118 | + |
| 119 | + /// Use when measuring a PE/COFF image. |
| 120 | + const PE_COFF_IMAGE = 0x0000_0000_0000_0010; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 125 | +#[repr(C, packed)] |
| 126 | +pub struct Tcg2EventHeader { |
| 127 | + pub header_size: u32, |
| 128 | + pub header_version: u16, |
| 129 | + pub pcr_index: u32, |
| 130 | + pub event_type: EventType, |
| 131 | +} |
| 132 | + |
| 133 | +/// Protocol for interacting with TPM devices. |
| 134 | +/// |
| 135 | +/// This protocol can be used for interacting with older TPM 1.1/1.2 |
| 136 | +/// devices, but most firmware only uses it for TPM 2.0. |
| 137 | +/// |
| 138 | +/// The corresponding C type is `EFI_TCG2_PROTOCOL`. |
| 139 | +#[derive(Debug)] |
| 140 | +#[repr(C)] |
| 141 | +pub struct Tcg2Protocol { |
| 142 | + pub get_capability: unsafe extern "efiapi" fn( |
| 143 | + this: *mut Self, |
| 144 | + protocol_capability: *mut Tcg2BootServiceCapability, |
| 145 | + ) -> Status, |
| 146 | + |
| 147 | + pub get_event_log: unsafe extern "efiapi" fn( |
| 148 | + this: *mut Self, |
| 149 | + event_log_format: Tcg2EventLogFormat, |
| 150 | + event_log_location: *mut PhysicalAddress, |
| 151 | + event_log_last_entry: *mut PhysicalAddress, |
| 152 | + event_log_truncated: *mut u8, |
| 153 | + ) -> Status, |
| 154 | + |
| 155 | + pub hash_log_extend_event: unsafe extern "efiapi" fn( |
| 156 | + this: *mut Self, |
| 157 | + flags: Tcg2HashLogExtendEventFlags, |
| 158 | + data_to_hash: PhysicalAddress, |
| 159 | + data_to_hash_len: u64, |
| 160 | + event: *const c_void, |
| 161 | + ) -> Status, |
| 162 | + |
| 163 | + pub submit_command: unsafe extern "efiapi" fn( |
| 164 | + this: *mut Self, |
| 165 | + input_parameter_block_size: u32, |
| 166 | + input_parameter_block: *const u8, |
| 167 | + output_parameter_block_size: u32, |
| 168 | + output_parameter_block: *mut u8, |
| 169 | + ) -> Status, |
| 170 | + |
| 171 | + pub get_active_pcr_banks: unsafe extern "efiapi" fn( |
| 172 | + this: *mut Self, |
| 173 | + active_pcr_banks: *mut Tcg2HashAlgorithmBitmap, |
| 174 | + ) -> Status, |
| 175 | + |
| 176 | + pub set_active_pcr_banks: unsafe extern "efiapi" fn( |
| 177 | + this: *mut Self, |
| 178 | + active_pcr_banks: Tcg2HashAlgorithmBitmap, |
| 179 | + ) -> Status, |
| 180 | + |
| 181 | + pub get_result_of_set_active_pcr_banks: unsafe extern "efiapi" fn( |
| 182 | + this: *mut Self, |
| 183 | + operation_present: *mut u32, |
| 184 | + response: *mut u32, |
| 185 | + ) -> Status, |
| 186 | +} |
| 187 | + |
| 188 | +impl Tcg2Protocol { |
| 189 | + pub const GUID: Guid = guid!("607f766c-7455-42be-930b-e4d76db2720f"); |
| 190 | +} |
0 commit comments