1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2023 Linaro Limited
7 #include <dm/of_access.h>
9 #include <tpm-common.h>
12 #include <u-boot/sha1.h>
13 #include <u-boot/sha256.h>
14 #include <u-boot/sha512.h>
15 #include <version_string.h>
17 #include <linux/bitops.h>
18 #include <linux/unaligned/be_byteshift.h>
19 #include <linux/unaligned/generic.h>
20 #include <linux/unaligned/le_byteshift.h>
21 #include "tpm-utils.h"
23 int tcg2_get_pcr_info(struct udevice *dev, u32 *supported_pcr, u32 *active_pcr,
26 u8 response[(sizeof(struct tpms_capability_data) -
27 offsetof(struct tpms_capability_data, data))];
28 struct tpml_pcr_selection pcrs;
35 memset(response, 0, sizeof(response));
37 ret = tpm2_get_pcr_info(dev, &pcrs);
41 for (i = 0; i < pcrs.count; i++) {
42 u32 hash_mask = tcg2_algorithm_to_mask(pcrs.selection[i].hash);
45 *supported_pcr |= hash_mask;
46 if (tpm2_is_active_pcr(&pcrs.selection[i]))
47 *active_pcr |= hash_mask;
49 printf("%s: unknown algorithm %x\n", __func__,
50 pcrs.selection[i].hash);
54 *pcr_banks = pcrs.count;
59 int tcg2_get_active_pcr_banks(struct udevice *dev, u32 *active_pcr_banks)
66 rc = tcg2_get_pcr_info(dev, &supported, &active, &pcr_banks);
70 *active_pcr_banks = active;
75 u32 tcg2_event_get_size(struct tpml_digest_values *digest_list)
80 len = offsetof(struct tcg_pcr_event2, digests);
81 len += offsetof(struct tpml_digest_values, digests);
82 for (i = 0; i < digest_list->count; ++i) {
83 u16 l = tpm2_algorithm_to_len(digest_list->digests[i].hash_alg);
88 len += l + offsetof(struct tpmt_ha, digest);
95 int tcg2_create_digest(struct udevice *dev, const u8 *input, u32 length,
96 struct tpml_digest_values *digest_list)
98 u8 final[sizeof(union tpmu_ha)];
99 sha256_context ctx_256;
100 sha512_context ctx_512;
107 rc = tcg2_get_active_pcr_banks(dev, &active);
111 digest_list->count = 0;
112 for (i = 0; i < ARRAY_SIZE(hash_algo_list); ++i) {
113 if (!(active & hash_algo_list[i].hash_mask))
116 switch (hash_algo_list[i].hash_alg) {
119 sha1_update(&ctx, input, length);
120 sha1_finish(&ctx, final);
121 len = TPM2_SHA1_DIGEST_SIZE;
123 case TPM2_ALG_SHA256:
124 sha256_starts(&ctx_256);
125 sha256_update(&ctx_256, input, length);
126 sha256_finish(&ctx_256, final);
127 len = TPM2_SHA256_DIGEST_SIZE;
129 case TPM2_ALG_SHA384:
130 sha384_starts(&ctx_512);
131 sha384_update(&ctx_512, input, length);
132 sha384_finish(&ctx_512, final);
133 len = TPM2_SHA384_DIGEST_SIZE;
135 case TPM2_ALG_SHA512:
136 sha512_starts(&ctx_512);
137 sha512_update(&ctx_512, input, length);
138 sha512_finish(&ctx_512, final);
139 len = TPM2_SHA512_DIGEST_SIZE;
142 printf("%s: unsupported algorithm %x\n", __func__,
143 hash_algo_list[i].hash_alg);
147 digest_list->digests[digest_list->count].hash_alg =
148 hash_algo_list[i].hash_alg;
149 memcpy(&digest_list->digests[digest_list->count].digest, final,
151 digest_list->count++;
157 void tcg2_log_append(u32 pcr_index, u32 event_type,
158 struct tpml_digest_values *digest_list, u32 size,
159 const u8 *event, u8 *log)
165 pos = offsetof(struct tcg_pcr_event2, pcr_index);
166 put_unaligned_le32(pcr_index, log);
167 pos = offsetof(struct tcg_pcr_event2, event_type);
168 put_unaligned_le32(event_type, log + pos);
169 pos = offsetof(struct tcg_pcr_event2, digests) +
170 offsetof(struct tpml_digest_values, count);
171 put_unaligned_le32(digest_list->count, log + pos);
173 pos = offsetof(struct tcg_pcr_event2, digests) +
174 offsetof(struct tpml_digest_values, digests);
175 for (i = 0; i < digest_list->count; ++i) {
176 u16 hash_alg = digest_list->digests[i].hash_alg;
178 len = tpm2_algorithm_to_len(hash_alg);
182 pos += offsetof(struct tpmt_ha, hash_alg);
183 put_unaligned_le16(hash_alg, log + pos);
184 pos += offsetof(struct tpmt_ha, digest);
185 memcpy(log + pos, (u8 *)&digest_list->digests[i].digest, len);
189 put_unaligned_le32(size, log + pos);
191 memcpy(log + pos, event, size);
194 static int tcg2_log_append_check(struct tcg2_event_log *elog, u32 pcr_index,
196 struct tpml_digest_values *digest_list,
197 u32 size, const u8 *event)
202 event_size = size + tcg2_event_get_size(digest_list);
203 if (elog->log_position + event_size > elog->log_size) {
204 printf("%s: log too large: %u + %u > %u\n", __func__,
205 elog->log_position, event_size, elog->log_size);
209 log = elog->log + elog->log_position;
210 elog->log_position += event_size;
212 tcg2_log_append(pcr_index, event_type, digest_list, size, event, log);
217 static int tcg2_log_init(struct udevice *dev, struct tcg2_event_log *elog)
219 struct tcg_efi_spec_id_event *ev;
220 struct tcg_pcr_event *log;
229 rc = tcg2_get_active_pcr_banks(dev, &active);
233 event_size = offsetof(struct tcg_efi_spec_id_event, digest_sizes);
234 for (i = 0; i < ARRAY_SIZE(hash_algo_list); ++i) {
235 if (!(active & hash_algo_list[i].hash_mask))
238 switch (hash_algo_list[i].hash_alg) {
240 case TPM2_ALG_SHA256:
241 case TPM2_ALG_SHA384:
242 case TPM2_ALG_SHA512:
251 (sizeof(struct tcg_efi_spec_id_event_algorithm_size) * count);
252 log_size = offsetof(struct tcg_pcr_event, event) + event_size;
254 if (log_size > elog->log_size) {
255 printf("%s: log too large: %u > %u\n", __func__, log_size,
260 log = (struct tcg_pcr_event *)elog->log;
261 put_unaligned_le32(0, &log->pcr_index);
262 put_unaligned_le32(EV_NO_ACTION, &log->event_type);
263 memset(&log->digest, 0, sizeof(log->digest));
264 put_unaligned_le32(event_size, &log->event_size);
266 ev = (struct tcg_efi_spec_id_event *)log->event;
267 strlcpy((char *)ev->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03,
268 sizeof(ev->signature));
269 put_unaligned_le32(0, &ev->platform_class);
270 ev->spec_version_minor = TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2;
271 ev->spec_version_major = TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2;
272 ev->spec_errata = TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_ERRATA_TPM2;
273 ev->uintn_size = sizeof(size_t) / sizeof(u32);
274 put_unaligned_le32(count, &ev->number_of_algorithms);
277 for (i = 0; i < ARRAY_SIZE(hash_algo_list); ++i) {
278 if (!(active & hash_algo_list[i].hash_mask))
281 len = hash_algo_list[i].hash_len;
285 put_unaligned_le16(hash_algo_list[i].hash_alg,
286 &ev->digest_sizes[count].algorithm_id);
287 put_unaligned_le16(len, &ev->digest_sizes[count].digest_size);
291 *((u8 *)ev + (event_size - 1)) = 0;
292 elog->log_position = log_size;
297 static int tcg2_replay_eventlog(struct tcg2_event_log *elog,
299 struct tpml_digest_values *digest_list,
302 const u32 offset = offsetof(struct tcg_pcr_event2, digests) +
303 offsetof(struct tpml_digest_values, digests);
314 while (log_position + offset < elog->log_size) {
315 log = elog->log + log_position;
317 pos = offsetof(struct tcg_pcr_event2, pcr_index);
318 pcr = get_unaligned_le32(log + pos);
319 pos = offsetof(struct tcg_pcr_event2, event_type);
320 if (!get_unaligned_le32(log + pos))
323 pos = offsetof(struct tcg_pcr_event2, digests) +
324 offsetof(struct tpml_digest_values, count);
325 count = get_unaligned_le32(log + pos);
326 if (count > ARRAY_SIZE(hash_algo_list) ||
327 (digest_list->count && digest_list->count != count))
330 pos = offsetof(struct tcg_pcr_event2, digests) +
331 offsetof(struct tpml_digest_values, digests);
332 for (i = 0; i < count; ++i) {
333 pos += offsetof(struct tpmt_ha, hash_alg);
334 if (log_position + pos + sizeof(u16) >= elog->log_size)
337 algo = get_unaligned_le16(log + pos);
338 pos += offsetof(struct tpmt_ha, digest);
341 case TPM2_ALG_SHA256:
342 case TPM2_ALG_SHA384:
343 case TPM2_ALG_SHA512:
344 len = tpm2_algorithm_to_len(algo);
350 if (digest_list->count) {
351 if (algo != digest_list->digests[i].hash_alg ||
352 log_position + pos + len >= elog->log_size)
355 memcpy(digest_list->digests[i].digest.sha512,
362 if (log_position + pos + sizeof(u32) >= elog->log_size)
365 event_size = get_unaligned_le32(log + pos);
366 pos += event_size + sizeof(u32);
367 if (log_position + pos > elog->log_size)
370 if (digest_list->count) {
371 rc = tcg2_pcr_extend(dev, pcr, digest_list);
379 elog->log_position = log_position;
384 static int tcg2_log_parse(struct udevice *dev, struct tcg2_event_log *elog)
386 struct tpml_digest_values digest_list;
387 struct tcg_efi_spec_id_event *event;
388 struct tcg_pcr_event *log;
401 if (elog->log_size <= offsetof(struct tcg_pcr_event, event))
404 log = (struct tcg_pcr_event *)elog->log;
405 if (get_unaligned_le32(&log->pcr_index) != 0 ||
406 get_unaligned_le32(&log->event_type) != EV_NO_ACTION)
409 for (i = 0; i < sizeof(log->digest); i++) {
414 evsz = get_unaligned_le32(&log->event_size);
415 if (evsz < offsetof(struct tcg_efi_spec_id_event, digest_sizes) ||
416 evsz + offsetof(struct tcg_pcr_event, event) > elog->log_size)
419 event = (struct tcg_efi_spec_id_event *)log->event;
420 if (memcmp(event->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03,
421 sizeof(TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03)))
424 if (event->spec_version_minor != TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2 ||
425 event->spec_version_major != TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2)
428 count = get_unaligned_le32(&event->number_of_algorithms);
429 if (count > ARRAY_SIZE(hash_algo_list))
432 calc_size = offsetof(struct tcg_efi_spec_id_event, digest_sizes) +
433 (sizeof(struct tcg_efi_spec_id_event_algorithm_size) * count) +
435 if (evsz != calc_size)
438 rc = tcg2_get_active_pcr_banks(dev, &active);
442 digest_list.count = 0;
445 for (i = 0; i < count; ++i) {
446 algo = get_unaligned_le16(&event->digest_sizes[i].algorithm_id);
447 mask = tcg2_algorithm_to_mask(algo);
449 if (!(active & mask))
454 case TPM2_ALG_SHA256:
455 case TPM2_ALG_SHA384:
456 case TPM2_ALG_SHA512:
457 len = get_unaligned_le16(&event->digest_sizes[i].digest_size);
458 if (tpm2_algorithm_to_len(algo) != len)
460 digest_list.digests[digest_list.count++].hash_alg = algo;
469 /* Ensure the previous firmware extended all the PCRs. */
470 if (log_active != active)
473 /* Read PCR0 to check if previous firmware extended the PCRs or not. */
474 rc = tcg2_pcr_read(dev, 0, &digest_list);
478 for (i = 0; i < digest_list.count; ++i) {
479 len = tpm2_algorithm_to_len(digest_list.digests[i].hash_alg);
480 for (j = 0; j < len; ++j) {
481 if (digest_list.digests[i].digest.sha512[j])
485 /* PCR is non-zero; it has been extended, so skip extending. */
487 digest_list.count = 0;
492 return tcg2_replay_eventlog(elog, dev, &digest_list,
493 offsetof(struct tcg_pcr_event, event) +
497 int tcg2_pcr_extend(struct udevice *dev, u32 pcr_index,
498 struct tpml_digest_values *digest_list)
503 for (i = 0; i < digest_list->count; i++) {
504 u32 alg = digest_list->digests[i].hash_alg;
506 rc = tpm2_pcr_extend(dev, pcr_index, alg,
507 (u8 *)&digest_list->digests[i].digest,
508 tpm2_algorithm_to_len(alg));
510 printf("%s: error pcr:%u alg:%08x\n", __func__,
519 int tcg2_pcr_read(struct udevice *dev, u32 pcr_index,
520 struct tpml_digest_values *digest_list)
522 struct tpm_chip_priv *priv;
526 priv = dev_get_uclass_priv(dev);
530 for (i = 0; i < digest_list->count; i++) {
531 u32 alg = digest_list->digests[i].hash_alg;
532 u8 *digest = (u8 *)&digest_list->digests[i].digest;
534 rc = tpm2_pcr_read(dev, pcr_index, priv->pcr_select_min, alg,
535 digest, tpm2_algorithm_to_len(alg), NULL);
537 printf("%s: error pcr:%u alg:%08x\n", __func__,
546 int tcg2_measure_data(struct udevice *dev, struct tcg2_event_log *elog,
547 u32 pcr_index, u32 size, const u8 *data, u32 event_type,
548 u32 event_size, const u8 *event)
550 struct tpml_digest_values digest_list;
554 rc = tcg2_create_digest(dev, data, size, &digest_list);
556 rc = tcg2_create_digest(dev, event, event_size, &digest_list);
560 rc = tcg2_pcr_extend(dev, pcr_index, &digest_list);
564 return tcg2_log_append_check(elog, pcr_index, event_type, &digest_list,
568 int tcg2_log_prepare_buffer(struct udevice *dev, struct tcg2_event_log *elog,
569 bool ignore_existing_log)
571 struct tcg2_event_log log;
574 elog->log_position = 0;
577 rc = tcg2_platform_get_log(dev, (void **)&log.log, &log.log_size);
579 log.log_position = 0;
582 if (!ignore_existing_log) {
583 rc = tcg2_log_parse(dev, &log);
588 if (elog->log_size) {
590 if (elog->log_size < log.log_position)
594 * Copy the discovered log into the user buffer
595 * if there's enough space.
597 memcpy(elog->log, log.log, log.log_position);
600 unmap_physmem(log.log, MAP_NOCACHE);
603 elog->log_size = log.log_size;
606 elog->log_position = log.log_position;
607 elog->found = log.found;
611 * Initialize the log buffer if no log was discovered and the buffer is
612 * valid. User's can pass in their own buffer as a fallback if no
613 * memory region is found.
615 if (!elog->found && elog->log_size)
616 rc = tcg2_log_init(dev, elog);
621 int tcg2_measurement_init(struct udevice **dev, struct tcg2_event_log *elog,
622 bool ignore_existing_log)
626 rc = tcg2_platform_get_tpm2(dev);
630 rc = tpm_auto_start(*dev);
634 rc = tcg2_log_prepare_buffer(*dev, elog, ignore_existing_log);
636 tcg2_measurement_term(*dev, elog, true);
640 rc = tcg2_measure_event(*dev, elog, 0, EV_S_CRTM_VERSION,
641 strlen(version_string) + 1,
642 (u8 *)version_string);
644 tcg2_measurement_term(*dev, elog, true);
651 void tcg2_measurement_term(struct udevice *dev, struct tcg2_event_log *elog,
654 u32 event = error ? 0x1 : 0xffffffff;
657 for (i = 0; i < 8; ++i)
658 tcg2_measure_event(dev, elog, i, EV_SEPARATOR, sizeof(event),
662 unmap_physmem(elog->log, MAP_NOCACHE);
665 __weak int tcg2_platform_get_log(struct udevice *dev, void **addr, u32 *size)
667 const __be32 *addr_prop;
668 const __be32 *size_prop;
675 addr_prop = dev_read_prop(dev, "tpm_event_log_addr", &asize);
677 addr_prop = dev_read_prop(dev, "linux,sml-base", &asize);
679 size_prop = dev_read_prop(dev, "tpm_event_log_size", &ssize);
681 size_prop = dev_read_prop(dev, "linux,sml-size", &ssize);
683 if (addr_prop && size_prop) {
684 u64 a = of_read_number(addr_prop, asize / sizeof(__be32));
685 u64 s = of_read_number(size_prop, ssize / sizeof(__be32));
687 *addr = map_physmem(a, s, MAP_NOCACHE);
690 struct ofnode_phandle_args args;
694 if (dev_read_phandle_with_args(dev, "memory-region", NULL, 0,
698 a = ofnode_get_addr_size(args.node, "reg", &s);
699 if (a == FDT_ADDR_T_NONE)
702 *addr = map_physmem(a, s, MAP_NOCACHE);
709 __weak int tcg2_platform_get_tpm2(struct udevice **dev)
711 for_each_tpm_device(*dev) {
712 if (tpm_get_version(*dev) == TPM_V2)
719 u32 tcg2_algorithm_to_mask(enum tpm2_algorithms algo)
723 for (i = 0; i < ARRAY_SIZE(hash_algo_list); i++) {
724 if (hash_algo_list[i].hash_alg == algo)
725 return hash_algo_list[i].hash_mask;
731 __weak void tcg2_platform_startup_error(struct udevice *dev, int rc) {}