coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
tpm_common.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <security/tpm/tspi.h>
4 #include <vb2_api.h>
6 
7 #define TPM_PCR_BOOT_MODE "VBOOT: boot mode"
8 #define TPM_PCR_GBB_HWID_NAME "VBOOT: GBB HWID"
9 
10 uint32_t vboot_setup_tpm(struct vb2_context *ctx)
11 {
13 
14  result = tpm_setup(ctx->flags & VB2_CONTEXT_S3_RESUME);
16  ctx->flags |= VB2_CONTEXT_SECDATA_WANTS_REBOOT;
17 
18  return result;
19 }
20 
21 vb2_error_t vboot_extend_pcr(struct vb2_context *ctx, int pcr,
22  enum vb2_pcr_digest which_digest)
23 {
24  uint8_t buffer[VB2_PCR_DIGEST_RECOMMENDED_SIZE];
25  uint32_t size = sizeof(buffer);
26  vb2_error_t rv;
27 
28  rv = vb2api_get_pcr_digest(ctx, which_digest, buffer, &size);
29  if (rv != VB2_SUCCESS)
30  return rv;
31  if (size < TPM_PCR_MINIMUM_DIGEST_SIZE)
32  return VB2_ERROR_UNKNOWN;
33 
34  /*
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");
47  enum vb2_hash_algorithm algo = CONFIG(TPM1) ? VB2_HASH_SHA1 : VB2_HASH_SHA256;
48 
49  switch (which_digest) {
50  /* SHA1 of (devmode|recmode|keyblock) bits */
51  case BOOT_MODE_PCR:
52  return tpm_extend_pcr(pcr, algo, buffer, vb2_digest_size(algo),
54  /* SHA256 of HWID */
55  case HWID_DIGEST_PCR:
56  return tpm_extend_pcr(pcr, algo, buffer, vb2_digest_size(algo),
58  default:
59  return VB2_ERROR_UNKNOWN;
60  }
61 }
@ CONFIG
Definition: dsi_common.h:201
_Static_assert(sizeof(hls_t)==HLS_SIZE, "HLS_SIZE must equal to sizeof(hls_t)")
result
Definition: mrc_cache.c:35
u8 buffer[C2P_BUFFER_MAXSIZE]
Definition: psp_smm.c:18
unsigned int uint32_t
Definition: stdint.h:14
unsigned char uint8_t
Definition: stdint.h:8
#define TPM_PCR_BOOT_MODE
Definition: tpm_common.c:7
#define TPM_PCR_GBB_HWID_NAME
Definition: tpm_common.c:8
vb2_error_t vboot_extend_pcr(struct vb2_context *ctx, int pcr, enum vb2_pcr_digest which_digest)
Definition: tpm_common.c:21
uint32_t vboot_setup_tpm(struct vb2_context *ctx)
Definition: tpm_common.c:10
uint32_t tpm_extend_pcr(int pcr, enum vb2_hash_algorithm digest_algo, const uint8_t *digest, size_t digest_len, const char *name)
Ask vboot for a digest and extend a TPM PCR with it.
Definition: tspi.c:220
uint32_t tpm_setup(int s3flag)
Start the TPM and establish the root of trust.
Definition: tspi.c:135
#define TPM_PCR_MINIMUM_DIGEST_SIZE
Definition: tss_common.h:8
#define TPM_E_MUST_REBOOT
Definition: tss_errors.h:31