blob: dedf8dfebcc3fb7eb8069b755a97a3daad666161 [file] [log] [blame]
Angel Pons986d50e2020-04-02 23:48:53 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Christian Walter0bd84ed2019-07-23 10:26:30 +02002
Christian Walter0bd84ed2019-07-23 10:26:30 +02003#include <security/tpm/tspi.h>
Sergii Dmytruk47e9e8c2022-11-02 00:50:03 +02004#include <security/tpm/tss.h>
Christian Walter0bd84ed2019-07-23 10:26:30 +02005#include <security/vboot/tpm_common.h>
Jon Murphyd7b8dc92023-09-05 11:36:43 -06006#include <security/tpm/tss_errors.h>
Julius Wernerd96ca242022-08-08 18:08:35 -07007#include <vb2_api.h>
8#include <vb2_sha.h>
Christian Walter0bd84ed2019-07-23 10:26:30 +02009
10#define TPM_PCR_BOOT_MODE "VBOOT: boot mode"
11#define TPM_PCR_GBB_HWID_NAME "VBOOT: GBB HWID"
Yi Chou0f910e72023-08-11 14:40:37 +080012#define TPM_PCR_FIRMWARE_VERSION "VBOOT: firmware ver"
Christian Walter0bd84ed2019-07-23 10:26:30 +020013
Jon Murphyd7b8dc92023-09-05 11:36:43 -060014tpm_result_t vboot_setup_tpm(struct vb2_context *ctx)
Christian Walter0bd84ed2019-07-23 10:26:30 +020015{
Jon Murphyd7b8dc92023-09-05 11:36:43 -060016 tpm_result_t rc;
Christian Walter0bd84ed2019-07-23 10:26:30 +020017
Jon Murphy24604812023-09-05 10:37:05 -060018 rc = tpm_setup(ctx->flags & VB2_CONTEXT_S3_RESUME);
Jon Murphy056952e2023-09-05 10:44:09 -060019 if (rc == TPM_CB_MUST_REBOOT)
Christian Walter0bd84ed2019-07-23 10:26:30 +020020 ctx->flags |= VB2_CONTEXT_SECDATA_WANTS_REBOOT;
21
Jon Murphy24604812023-09-05 10:37:05 -060022 return rc;
Christian Walter0bd84ed2019-07-23 10:26:30 +020023}
24
Jon Murphyd7b8dc92023-09-05 11:36:43 -060025tpm_result_t vboot_extend_pcr(struct vb2_context *ctx, int pcr,
Joel Kitching220ac042019-07-31 14:19:00 +080026 enum vb2_pcr_digest which_digest)
Christian Walter0bd84ed2019-07-23 10:26:30 +020027{
28 uint8_t buffer[VB2_PCR_DIGEST_RECOMMENDED_SIZE];
29 uint32_t size = sizeof(buffer);
Christian Walter0bd84ed2019-07-23 10:26:30 +020030
Jon Murphyd7b8dc92023-09-05 11:36:43 -060031 if (vb2api_get_pcr_digest(ctx, which_digest, buffer, &size) != VB2_SUCCESS)
32 return TPM_CB_FAIL;
Christian Walter0bd84ed2019-07-23 10:26:30 +020033
Julius Werner74a0fad2021-03-22 17:25:20 -070034 /*
35 * On TPM 1.2, all PCRs are intended for use with SHA1. We truncate our
36 * SHA256 HWID hash to 20 bytes to make it fit. On TPM 2.0, we always
37 * want to use the SHA256 banks, even for the boot mode which is
38 * technically a SHA1 value for historical reasons. vboot has already
39 * zero-extended the buffer to 32 bytes for us, so we just take it like
40 * that and pretend it's a SHA256. In practice, this means we never care
41 * about the (*size) value returned from vboot (which indicates how many
42 * significant bytes vboot wrote, although it always extends zeroes up
43 * to the end of the buffer), we always use a hardcoded size instead.
44 */
45 _Static_assert(sizeof(buffer) >= VB2_SHA256_DIGEST_SIZE,
46 "Buffer needs to be able to fit at least a SHA256");
Sergii Dmytruk47e9e8c2022-11-02 00:50:03 +020047 enum vb2_hash_algorithm algo = tlcl_get_family() == TPM_1 ?
48 VB2_HASH_SHA1 : VB2_HASH_SHA256;
Julius Werner74a0fad2021-03-22 17:25:20 -070049
Christian Walter0bd84ed2019-07-23 10:26:30 +020050 switch (which_digest) {
51 /* SHA1 of (devmode|recmode|keyblock) bits */
52 case BOOT_MODE_PCR:
Julius Werner74a0fad2021-03-22 17:25:20 -070053 return tpm_extend_pcr(pcr, algo, buffer, vb2_digest_size(algo),
Christian Walter0bd84ed2019-07-23 10:26:30 +020054 TPM_PCR_BOOT_MODE);
55 /* SHA256 of HWID */
56 case HWID_DIGEST_PCR:
Julius Werner74a0fad2021-03-22 17:25:20 -070057 return tpm_extend_pcr(pcr, algo, buffer, vb2_digest_size(algo),
58 TPM_PCR_GBB_HWID_NAME);
Yi Chou0f910e72023-08-11 14:40:37 +080059 /* firmware version */
60 case FIRMWARE_VERSION_PCR:
61 return tpm_extend_pcr(pcr, algo, buffer, vb2_digest_size(algo),
62 TPM_PCR_FIRMWARE_VERSION);
Christian Walter0bd84ed2019-07-23 10:26:30 +020063 default:
Jon Murphyd7b8dc92023-09-05 11:36:43 -060064 return TPM_CB_FAIL;
Christian Walter0bd84ed2019-07-23 10:26:30 +020065 }
66}