1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3  * Copyright (c) 2022, Linaro Limited
4  *
5  * This file refers the following TCG specification.
6  * TCG PC Client Platform Firmware Profile Specification
7  */
8 
9 #ifndef __KERNEL_TCG_H__
10 #define __KERNEL_TCG_H__
11 
12 #include <tee_api_types.h>
13 #include <tpm2.h>
14 
15 #define TPM2_EVENT_LOG_SIZE		4096
16 
17 /*
18  * SHA1 Event Log Entry Format
19  *
20  * @pcr_index:  PCRIndex event extended to
21  * @event_type: Type of event (see EFI specs)
22  * @digest:     Value extended into PCR index
23  * @event_size: Size of event
24  * @event:      Event data
25  */
26 struct tcg_pcr_event {
27 	uint32_t pcr_index;
28 	uint32_t event_type;
29 	uint8_t digest[TPM2_SHA1_DIGEST_SIZE];
30 	uint32_t event_size;
31 	uint8_t event[];
32 };
33 
34 /*
35  * Crypto Agile Log Entry Format
36  *
37  * @pcr_index:	PCRIndex event extended to
38  * @event_type:	Type of event
39  * @digests:	List of digests extended to PCR index
40  * @event_size: Size of the event data
41  * @event:	Event data
42  */
43 struct tcg_pcr_event2 {
44 	uint32_t pcr_index;
45 	uint32_t event_type;
46 	struct tpml_digest_values digests;
47 	uint32_t event_size;
48 	uint8_t event[];
49 } __packed;
50 
51 #define TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03 "Spec ID Event03"
52 #define TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2 2
53 #define TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2 0
54 #define TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_ERRATA_TPM2 2
55 
56 /*
57  *  struct TCG_EfiSpecIdEventAlgorithmSize - hashing algorithm information
58  *
59  *  @algorithm_id:	algorithm defined in enum tpm2_algorithms
60  *  @digest_size:	size of the algorithm
61  */
62 struct tcg_efi_spec_id_event_algorithm_size {
63 	uint16_t      algorithm_id;
64 	uint16_t      digest_size;
65 };
66 
67 /**
68  * struct TCG_EfiSpecIDEventStruct - content of the event log header
69  *
70  * @signature:                  signature, set to Spec ID Event03
71  * @platform_class:             class defined in TCG ACPI Specification
72  *                              Client  Common Header.
73  * @spec_version_minor:         minor version
74  * @spec_version_major:         major version
75  * @spec_errata:                major version
76  * @uintn_size:                 size of the efi_uintn_t fields used in various
77  *                              data structures used in this specification.
78  *                              0x01 indicates uint32_t and 0x02 indicates
79  *                              uint64_t
80  * @number_of_algorithms:       hashing algorithms used in this event log
81  * @digest_sizes:               array of number_of_algorithms pairs
82  *                              1st member defines the algorithm id
83  *                              2nd member defines the algorithm size
84  */
85 struct tcg_efi_spec_id_event {
86 	uint8_t signature[16];
87 	uint32_t platform_class;
88 	uint8_t spec_version_minor;
89 	uint8_t spec_version_major;
90 	uint8_t spec_errata;
91 	uint8_t uintn_size;
92 	uint32_t number_of_algorithms;
93 	struct tcg_efi_spec_id_event_algorithm_size digest_sizes[];
94 } __packed;
95 
96 /*
97  * event types, cf.
98  * "TCG Server Management Domain Firmware Profile Specification",
99  * rev 1.00, 2020-05-01
100  */
101 #define EV_NO_ACTION			U(0x00000003)
102 
103 struct tcg_pcr_ops {
104 	/*
105 	 * pcr_info() - get the supported, active PCRs and number of banks
106 	 *
107 	 * @selection_mask:	bitmask with the algorithms supported
108 	 * @active_mask:	bitmask with the active algorithms
109 	 * @num_pcr:		number of PCR banks
110 	 *
111 	 */
112 	TEE_Result (*pcr_info)(uint32_t *selection_mask, uint32_t *active_mask,
113 			       uint32_t *num_pcr);
114 	/*
115 	 * pcr_extend() - Extend a PCR for a given tpml_digest_values
116 	 *
117 	 * @pcr_idx:		PCR Index
118 	 * @alg:		algorithm of digest
119 	 * @digest:		buffer containing the digest
120 	 * @digest_len:		length of the buffer
121 	 *
122 	 * @Return: status code
123 	 */
124 	TEE_Result (*pcr_extend)(uint8_t pcr_idx, uint16_t alg, void *digest,
125 				 uint32_t digest_len);
126 };
127 
128 #if defined(CFG_CORE_TCG_PROVIDER)
129 
130 /*
131  * Eventlog is the informational record of measurements. These measurements
132  * need to be extended to PCR's if the firmware passing the evenlog has
133  * not done so. The function parses the TPM evenlog information received
134  * from earlier firmware and extends the PCRs. The device supporting the
135  * PCRs needs to be registered with the TCG framework.
136  */
137 TEE_Result tcg_process_fw_eventlog(void);
138 
139 /*
140  * TCG PC Client Platform Firmware profile Specification talks about
141  * eventlogging. These eventlogs need to be extended into PCR's. The PCRs
142  * are available with TPM's. There may be other HSM's which may support PCRs.
143  * The HSM's or TPM needs to provide interface to get PCR info and extend the
144  * digests into PCR's. The platform needs to register the PCR providers
145  * with the TCG framework.
146  */
147 TEE_Result register_tcg_pcr_provider(struct tcg_pcr_ops *ops);
148 
149 #else
150 
tcg_process_fw_eventlog(void)151 static inline TEE_Result tcg_process_fw_eventlog(void)
152 {
153 	return TEE_ERROR_NOT_SUPPORTED;
154 }
155 
156 static inline TEE_Result
register_tcg_pcr_provider(struct tcg_pcr_ops * ops __unused)157 register_tcg_pcr_provider(struct tcg_pcr_ops *ops __unused)
158 {
159 	return TEE_ERROR_NOT_SUPPORTED;
160 }
161 
162 #endif
163 
164 #endif /* __KERNEL_TCG_H__ */
165