Skip to content

Commit 3eddef3

Browse files
uefi-raw: Add TCG protocols
1 parent 3317438 commit 3eddef3

File tree

6 files changed

+373
-0
lines changed

6 files changed

+373
-0
lines changed

uefi-raw/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Changed
44

55
- Added `protocol::string::UnicodeCollationProtocol`.
6+
- Added `protocol::tcg` module, containing the TCG v1 and v2 protocols.
67

78

89
# uefi-raw - 0.9.0 (2024-10-23)

uefi-raw/src/protocol/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ pub mod network;
1818
pub mod rng;
1919
pub mod shell_params;
2020
pub mod string;
21+
pub mod tcg;

uefi-raw/src/protocol/tcg.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//! [TCG] (Trusted Computing Group) protocols.
2+
//!
3+
//! These protocols provide access to the [TPM][tpm] (Trusted Platform Module).
4+
//!
5+
//! There are two versions of the protocol. The original protocol is in
6+
//! the [`v1`] module. It is used with TPM 1.1 and 1.2 devices. The
7+
//! newer protocol in the [`v2`] module is generally provided for TPM
8+
//! 2.0 devices, although the spec indicates it can be used for older
9+
//! TPM versions as well.
10+
//!
11+
//! [TCG]: https://trustedcomputinggroup.org/
12+
//! [TPM]: https://en.wikipedia.org/wiki/Trusted_Platform_Module
13+
14+
pub mod v1;
15+
pub mod v2;
16+
17+
mod enums;
18+
pub use enums::*;

uefi-raw/src/protocol/tcg/enums.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
newtype_enum! {
2+
/// Algorithm identifiers.
3+
///
4+
/// These values are defined in the [TCG Algorithm Registry].
5+
///
6+
/// [TCG Algorithm Registry]: https://trustedcomputinggroup.org/resource/tcg-algorithm-registry/
7+
pub enum AlgorithmId: u16 => {
8+
ERROR = 0x0000,
9+
RSA = 0x0001,
10+
TDES = 0x0003,
11+
SHA1 = 0x0004,
12+
HMAC = 0x0005,
13+
AES = 0x0006,
14+
MGF1 = 0x0007,
15+
KEYED_HASH = 0x0008,
16+
XOR = 0x000a,
17+
SHA256 = 0x000b,
18+
SHA384 = 0x000c,
19+
SHA512 = 0x000d,
20+
NULL = 0x0010,
21+
SM3_256 = 0x0012,
22+
SM4 = 0x0013,
23+
// TODO: there are a bunch more, but the above list is probably
24+
// more than sufficient for real devices.
25+
}
26+
}
27+
28+
newtype_enum! {
29+
/// Event types stored in the TPM event log. The event type defines
30+
/// which structure type is stored in the event data.
31+
///
32+
/// For details of each variant, see the [TCG PC Client Platform
33+
/// Firmware Protocol Specification][spec], in particular the Events
34+
/// table in the Event Logging chapter.
35+
///
36+
/// [spec]: https://trustedcomputinggroup.org/resource/pc-client-specific-platform-firmware-profile-specification/
37+
pub enum EventType: u32 => {
38+
PREBOOT_CERT = 0x0000_0000,
39+
POST_CODE = 0x0000_0001,
40+
UNUSED = 0x0000_0002,
41+
NO_ACTION = 0x0000_0003,
42+
SEPARATOR = 0x0000_0004,
43+
ACTION = 0x0000_0005,
44+
EVENT_TAG = 0x0000_0006,
45+
CRTM_CONTENTS = 0x0000_0007,
46+
CRTM_VERSION = 0x0000_0008,
47+
CPU_MICROCODE = 0x0000_0009,
48+
PLATFORM_CONFIG_FLAGS = 0x0000_000a,
49+
TABLE_OF_DEVICES = 0x0000_000b,
50+
COMPACT_HASH = 0x0000_000c,
51+
IPL = 0x0000_000d,
52+
IPL_PARTITION_DATA = 0x0000_000e,
53+
NONHOST_CODE = 0x0000_000f,
54+
NONHOST_CONFIG = 0x0000_0010,
55+
NONHOST_INFO = 0x0000_0011,
56+
OMIT_BOOT_DEVICE_EVENTS = 0x0000_0012,
57+
EFI_EVENT_BASE = 0x8000_0000,
58+
EFI_VARIABLE_DRIVER_CONFIG = 0x8000_0001,
59+
EFI_VARIABLE_BOOT = 0x8000_0002,
60+
EFI_BOOT_SERVICES_APPLICATION = 0x8000_0003,
61+
EFI_BOOT_SERVICES_DRIVER = 0x8000_0004,
62+
EFI_RUNTIME_SERVICES_DRIVER = 0x8000_0005,
63+
EFI_GPT_EVENT = 0x8000_0006,
64+
EFI_ACTION = 0x8000_0007,
65+
EFI_PLATFORM_FIRMWARE_BLOB = 0x8000_0008,
66+
EFI_HANDOFF_TABLES = 0x8000_0009,
67+
EFI_PLATFORM_FIRMWARE_BLOB2 = 0x8000_000a,
68+
EFI_HANDOFF_TABLES2 = 0x8000_000b,
69+
EFI_VARIABLE_BOOT2 = 0x8000_000c,
70+
EFI_HCRTM_EVENT = 0x8000_0010,
71+
EFI_VARIABLE_AUTHORITY = 0x8000_00e0,
72+
EFI_SPDM_FIRMWARE_BLOB = 0x8000_00e1,
73+
EFI_SPDM_FIRMWARE_CONFIG = 0x8000_00e2,
74+
}
75+
}

uefi-raw/src/protocol/tcg/v1.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//! [TCG] (Trusted Computing Group) protocol for [TPM] (Trusted Platform
2+
//! Module) 1.1 and 1.2.
3+
//!
4+
//! This protocol is defined in the [TCG EFI Protocol Specification _for
5+
//! TPM Family 1.1 or 1.2_][spec].
6+
//!
7+
//! [spec]: https://trustedcomputinggroup.org/resource/tcg-efi-protocol-specification/
8+
//! [TCG]: https://trustedcomputinggroup.org/
9+
//! [TPM]: https://en.wikipedia.org/wiki/Trusted_Platform_Module
10+
11+
use crate::{guid, Guid, PhysicalAddress, Status};
12+
use core::ffi::c_void;
13+
14+
/// Information about the protocol and the TPM device.
15+
#[repr(C)]
16+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
17+
pub struct TcgBootServiceCapability {
18+
pub size: u8,
19+
pub structure_version: TcgVersion,
20+
pub protocol_spec_version: TcgVersion,
21+
pub hash_algorithm_bitmap: u8,
22+
pub tpm_present_flag: u8,
23+
pub tpm_deactivated_flag: u8,
24+
}
25+
26+
/// Version information.
27+
#[repr(C)]
28+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
29+
pub struct TcgVersion {
30+
/// Major version.
31+
pub major: u8,
32+
/// Minor version.
33+
pub minor: u8,
34+
35+
pub rev_major: u8,
36+
pub rev_minor: u8,
37+
}
38+
39+
/// Protocol for interacting with TPM 1.1 and 1.2 devices.
40+
#[derive(Debug)]
41+
#[repr(C)]
42+
pub struct TcgProtocol {
43+
pub status_check: unsafe extern "efiapi" fn(
44+
this: *mut Self,
45+
protocol_capability: *mut TcgBootServiceCapability,
46+
feature_flags: *mut u32,
47+
event_log_location: *mut PhysicalAddress,
48+
event_log_last_entry: *mut PhysicalAddress,
49+
) -> Status,
50+
51+
pub hash_all: unsafe extern "efiapi" fn(
52+
this: *mut Self,
53+
hash_data: *const u8,
54+
hash_data_len: u64,
55+
algorithm_id: u32,
56+
hashed_data_len: *mut u64,
57+
hashed_data_result: *mut *mut u8,
58+
) -> Status,
59+
60+
pub log_event: unsafe extern "efiapi" fn(
61+
this: *mut Self,
62+
event: *const c_void,
63+
event_number: *mut u32,
64+
flags: u32,
65+
) -> Status,
66+
67+
pub pass_through_to_tpm: unsafe extern "efiapi" fn(
68+
this: *mut Self,
69+
tpm_input_parameter_block_size: u32,
70+
tpm_input_parameter_block: *const u8,
71+
tpm_output_parameter_block_size: u32,
72+
tpm_output_parameter_block: *mut u8,
73+
) -> Status,
74+
75+
pub hash_log_extend_event: unsafe extern "efiapi" fn(
76+
this: *mut Self,
77+
hash_data: PhysicalAddress,
78+
hash_data_len: u64,
79+
algorithm_id: u32,
80+
event: *mut c_void,
81+
event_number: *mut u32,
82+
event_log_last_entry: *mut PhysicalAddress,
83+
) -> Status,
84+
}
85+
86+
impl TcgProtocol {
87+
pub const GUID: Guid = guid!("f541796d-a62e-4954-a775-9584f61b9cdd");
88+
}

uefi-raw/src/protocol/tcg/v2.rs

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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

Comments
 (0)