From: Stefan Berger stefanb@linux.vnet.ibm.com
Introduce a log_entry 'object' that we use to hold the data to be logged in one log entry. Pass this object around rather than the TPM 1.2 specific 'pcpes' structure.
Write this log_entry in the format that is appropriate for the version of the host's TPM. For TPM 1.2 write it in the 'pcpes' structure's format, for TPM 2 in the new TPM 2 format.
By using this method we can keep the API interface on systems with a TPM 2 even though applications pass in the 'pcpes' structures directly. The log will still be written in the appropriate format.
The TPM 2 log contains a TPM 1.2 type of entry of event type EV_NO_ACTION and entry of type TCG_EfiSpeIdEventStruct as the first entry. This is described in the EFI specification (section 5.3):
TCG EFI Protocol Specification for TPM Family 2.0
http://www.trustedcomputinggroup.org/resources/tcg_efi_protocol_specificatio...
Signed-off-by: Stefan Berger stefanb@linux.vnet.ibm.com --- src/std/tcg.h | 35 ++++++++++++ src/tcgbios.c | 173 +++++++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 183 insertions(+), 25 deletions(-)
diff --git a/src/std/tcg.h b/src/std/tcg.h index 8466b14..1e8b250 100644 --- a/src/std/tcg.h +++ b/src/std/tcg.h @@ -89,6 +89,7 @@ enum irq_ids {
/* event types: 10.4.1 / table 11 */ #define EV_POST_CODE 1 +#define EV_NO_ACTION 3 #define EV_SEPARATOR 4 #define EV_ACTION 5 #define EV_EVENT_TAG 6 @@ -472,4 +473,38 @@ struct tpm2_req_hierarchycontrol { u8 state; } PACKED;
+/* TPM 2 log entry */ + +struct tpml_digest_values_sha1 { + u16 hashtype; + u8 sha1[SHA1_BUFSIZE]; +}; + +struct tcg_pcr_event2_sha1 { + u32 pcrindex; + u32 eventtype; + u32 count; /* number of digests */ + struct tpml_digest_values_sha1 digests[1]; + u32 eventsize; + u8 event[0]; +} PACKED; + +struct TCG_EfiSpecIdEventStruct { + u8 signature[16]; + u32 platformClass; + u8 specVersionMinor; + u8 specVersionMajor; + u8 specErrata; + u8 uintnSize; + u32 numberOfAlgorithms; + struct TCG_EfiSpecIdEventAlgorithmSize { + u16 algorithmId; + u16 digestSize; + } digestSizes[1]; + u8 vendorInfoSize; + u8 vendorInfo[0]; +}; + +#define TPM_TCPA_ACPI_CLASS_CLIENT 0 + #endif // tcg.h diff --git a/src/tcgbios.c b/src/tcgbios.c index f5500b1..369736c 100644 --- a/src/tcgbios.c +++ b/src/tcgbios.c @@ -52,6 +52,71 @@ static const u8 TPM2_SelfTest_YES[] = { TPM2_YES }; /* full test */
typedef u8 tpm_ppi_code;
+ +static TPMVersion TPM_version; + +/**************************************************************** + * Log entry 'object' + ****************************************************************/ + +struct log_entry { + u32 pcrindex; + u32 eventtype; + /* we only support SHA1 for TPM 2 */ + u8 sha1[SHA1_BUFSIZE]; + u32 eventdatasize; + const void *event; /* TPM 1.2 & 2 */ +}; + +static u32 +log_entry_write_size(struct log_entry *le) +{ + switch (TPM_version) { + case TPM_VERSION_NONE: + return 0; + case TPM_VERSION_1_2: + return sizeof(struct pcpes) + le->eventdatasize; + case TPM_VERSION_2: + return sizeof(struct tcg_pcr_event2_sha1); + } + return 0; +} + +static void +log_entry_write(struct log_entry *le, void *dest) +{ + struct pcpes *pcpes; + struct tcg_pcr_event2_sha1 *tpes; + + switch (TPM_version) { + case TPM_VERSION_NONE: + return; + case TPM_VERSION_1_2: + pcpes = dest; + pcpes->pcrindex = le->pcrindex; + pcpes->eventtype = le->eventtype; + memcpy(pcpes->digest, le->sha1, sizeof(pcpes->digest)); + pcpes->eventdatasize = le->eventdatasize; + + dest += sizeof(*pcpes); + + break; + case TPM_VERSION_2: + tpes = dest; + tpes->pcrindex = le->pcrindex; + tpes->eventtype = le->eventtype; + tpes->count = 1; + tpes->digests[0].hashtype = TPM2_ALG_SHA1; + memcpy(tpes->digests[0].sha1, le->sha1, sizeof(tpes->digests[0].sha1)); + tpes->eventsize = le->eventdatasize; + + dest += sizeof(*tpes); + + break; + } + memcpy(dest, le->event, le->eventdatasize); +} + /**************************************************************** * ACPI TCPA table interface ****************************************************************/ @@ -75,8 +140,6 @@ struct {
static int TPM_has_physical_presence;
-static TPMVersion TPM_version; - static struct tcpa_descriptor_rev2 * find_tcpa_by_rsdp(struct rsdp_descriptor *rsdp) { @@ -141,7 +204,7 @@ tpm_tcpa_probe(void) * Returns an error code in case of faiure, 0 in case of success */ static int -tpm_log_event(struct pcpes *pcpes, const void *event) +tpm_log_event(struct log_entry *le) { dprintf(DEBUG_tcg, "TCGBIOS: LASA = %p, next entry = %p\n", tpm_state.log_area_start_address, tpm_state.log_area_next_entry); @@ -149,7 +212,7 @@ tpm_log_event(struct pcpes *pcpes, const void *event) if (tpm_state.log_area_next_entry == NULL) return -1;
- u32 size = sizeof(*pcpes) + pcpes->eventdatasize; + u32 size = log_entry_write_size(le);
if ((tpm_state.log_area_next_entry + size - tpm_state.log_area_start_address) > tpm_state.log_area_minimum_length) { @@ -157,9 +220,7 @@ tpm_log_event(struct pcpes *pcpes, const void *event) return -1; }
- memcpy(tpm_state.log_area_next_entry, pcpes, sizeof(*pcpes)); - memcpy(tpm_state.log_area_next_entry + sizeof(*pcpes), - event, pcpes->eventdatasize); + log_entry_write(le, tpm_state.log_area_next_entry);
tpm_state.log_area_last_entry = tpm_state.log_area_next_entry; tpm_state.log_area_next_entry += size; @@ -168,6 +229,43 @@ tpm_log_event(struct pcpes *pcpes, const void *event) return 0; }
+/* + * Initialize the log; a TPM2 log needs a special TPM 1.2 log entry + * as the first entry serving identification purposes + */ +void tpm_log_init(void) +{ + struct TCG_EfiSpecIdEventStruct event = { + .signature = "Spec ID Event03", + .platformClass = TPM_TCPA_ACPI_CLASS_CLIENT, + .specVersionMinor = 0, + .specVersionMajor = 2, + .specErrata = 0, + .uintnSize = 2, + .numberOfAlgorithms = 1, + .digestSizes[0] = { + .algorithmId = TPM2_ALG_SHA1, + .digestSize = SHA1_BUFSIZE, + }, + .vendorInfoSize = 0, + }; + struct log_entry le = { + .eventtype = EV_NO_ACTION, + .event = &event, + }; + + switch (TPM_version) { + case TPM_VERSION_NONE: + case TPM_VERSION_1_2: + break; + case TPM_VERSION_2: + /* write a 1.2 type of entry */ + TPM_version = 1; + tpm_log_event(&le); + TPM_version = 2; + } +} +
/**************************************************************** * Helper functions @@ -449,20 +547,27 @@ tpm_extend(u32 pcrindex, const u8 *digest) }
static int -tpm_log_extend_event(struct pcpes *pcpes, const void *event) +tpm_log_extend_event(struct log_entry *le) { - int ret = tpm_extend(pcpes->pcrindex, pcpes->digest); + int ret = tpm_extend(le->pcrindex, le->sha1); if (ret) return -1;
- return tpm_log_event(pcpes, event); + return tpm_log_event(le); }
+/* + * hash hashdata; if hashdata is not given, caller has calculated + * the hash, so it is in pcpes->digest + */ static void -tpm_fill_hash(struct pcpes *pcpes, const void *hashdata, u32 hashdata_length) +tpm_fill_hash(struct log_entry *le, const void *hashdata, u32 hashdata_length, + struct pcpes *pcpes) { if (hashdata) - sha1(hashdata, hashdata_length, pcpes->digest); + sha1(hashdata, hashdata_length, le->sha1); + else if (pcpes) + memcpy(le->sha1, pcpes->digest, sizeof(le->sha1)); }
/* @@ -485,13 +590,14 @@ tpm_add_measurement_to_log(u32 pcrindex, u32 event_type, if (!tpm_is_working()) return;
- struct pcpes pcpes = { + struct log_entry le = { .pcrindex = pcrindex, .eventtype = event_type, .eventdatasize = event_length, + .event = event, }; - tpm_fill_hash(&pcpes, hashdata, hashdata_length); - int ret = tpm_log_extend_event(&pcpes, event); + tpm_fill_hash(&le, hashdata, hashdata_length, NULL); + int ret = tpm_log_extend_event(&le); if (ret) tpm_set_failure(); } @@ -717,6 +823,8 @@ tpm_setup(void) if (ret) return;
+ tpm_log_init(); + TPM_working = 1;
if (runningOnXen()) @@ -1052,8 +1160,15 @@ hash_log_extend_event_int(const struct hleei_short *hleei_s, goto err_exit; }
- tpm_fill_hash(pcpes, hleei_s->hashdataptr, hleei_s->hashdatalen); - int ret = tpm_log_extend_event(pcpes, pcpes->event); + struct log_entry le = { + .pcrindex = pcpes->pcrindex, + .eventtype = pcpes->eventtype, + .eventdatasize = pcpes->eventdatasize, + .event = pcpes->event, + }; + + tpm_fill_hash(&le, hleei_s->hashdataptr, hleei_s->hashdatalen, pcpes); + int ret = tpm_log_extend_event(&le); if (ret) { rc = TCG_TCG_COMMAND_ERROR; goto err_exit; @@ -1062,7 +1177,7 @@ hash_log_extend_event_int(const struct hleei_short *hleei_s, hleeo->opblength = sizeof(struct hleeo); hleeo->reserved = 0; hleeo->eventnumber = hleo.eventnumber; - memcpy(hleeo->digest, pcpes->digest, sizeof(hleeo->digest)); + memcpy(hleeo->digest, le.sha1, sizeof(hleeo->digest));
err_exit: if (rc != 0) { @@ -1135,8 +1250,15 @@ hash_log_event_int(const struct hlei *hlei, struct hleo *hleo) goto err_exit; }
- tpm_fill_hash(pcpes, hlei->hashdataptr, hlei->hashdatalen); - int ret = tpm_log_event(pcpes, pcpes->event); + struct log_entry le = { + .pcrindex = pcpes->pcrindex, + .eventtype = pcpes->eventtype, + .eventdatasize = pcpes->eventdatasize, + .event = pcpes->event, + }; + + tpm_fill_hash(&le, hlei->hashdataptr, hlei->hashdatalen, pcpes); + int ret = tpm_log_event(&le); if (ret) { rc = TCG_PC_LOGOVERFLOW; goto err_exit; @@ -1185,14 +1307,15 @@ compact_hash_log_extend_event_int(u8 *buffer, u32 pcrindex, u32 *edx_ptr) { - struct pcpes pcpes = { - .pcrindex = pcrindex, - .eventtype = EV_COMPACT_HASH, + struct log_entry le = { + .pcrindex = pcrindex, + .eventtype = EV_COMPACT_HASH, .eventdatasize = sizeof(info), + .event = &info, };
- tpm_fill_hash(&pcpes, buffer, length); - int ret = tpm_log_extend_event(&pcpes, &info); + tpm_fill_hash(&le, buffer, length, NULL); + int ret = tpm_log_extend_event(&le); if (ret) return TCG_TCG_COMMAND_ERROR; *edx_ptr = tpm_state.entry_count;