Skip to content

Commit cd8168d

Browse files
committed
security/tpm: replace TPM_MEASURE_ALGO with tpm_log_alg()
No functional changes. This replaces a macro with an inline function to make code more readable and more convenient to extend in the future. Change-Id: I456bc3bb749a9b58fba72f5562195525e55290bf Signed-off-by: Sergii Dmytruk <[email protected]>
1 parent d526ae0 commit cd8168d

File tree

5 files changed

+39
-41
lines changed

5 files changed

+39
-41
lines changed

src/lib/cbfs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ static bool cbfs_file_hash_mismatch(const void *buffer, size_t size,
189189
struct vb2_hash calculated_hash;
190190

191191
/* No need to re-hash file if we already have it from verification. */
192-
if (!hash || hash->algo != TPM_MEASURE_ALGO) {
192+
if (!hash || hash->algo != tpm_log_alg()) {
193193
if (vb2_hash_calculate(vboot_hwcrypto_allowed(), buffer, size,
194-
TPM_MEASURE_ALGO, &calculated_hash))
194+
tpm_log_alg(), &calculated_hash))
195195
hash = NULL;
196196
else
197197
hash = &calculated_hash;

src/security/tpm/tspi.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,31 @@
1717
/* Assumption of 2K TCPA log size reserved for CAR/SRAM */
1818
#define MAX_PRERAM_TPM_LOG_ENTRIES 15
1919

20+
/**
21+
* Retrieves hash algorithm used by TPM event log or VB2_HASH_INVALID.
22+
*/
23+
static inline enum vb2_hash_algorithm tpm_log_alg(void)
24+
{
25+
if (CONFIG(TPM_LOG_CB))
26+
return (tlcl_get_family() == TPM_1 ? VB2_HASH_SHA1 : VB2_HASH_SHA256);
27+
28+
if (CONFIG(TPM_LOG_TPM1))
29+
return VB2_HASH_SHA1;
30+
31+
if (CONFIG(TPM_LOG_TPM2)) {
32+
if (CONFIG(TPM_HASH_SHA1))
33+
return VB2_HASH_SHA1;
34+
if (CONFIG(TPM_HASH_SHA256))
35+
return VB2_HASH_SHA256;
36+
if (CONFIG(TPM_HASH_SHA384))
37+
return VB2_HASH_SHA384;
38+
if (CONFIG(TPM_HASH_SHA512))
39+
return VB2_HASH_SHA512;
40+
}
41+
42+
return VB2_HASH_INVALID;
43+
}
44+
2045
/**
2146
* Get the pointer to the single instance of global
2247
* TPM log data, and initialize it when necessary

src/security/tpm/tspi/crtm.h

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,6 @@
99
#include <types.h>
1010
#include <vb2_sha.h>
1111

12-
#if CONFIG(TPM_LOG_CB)
13-
# define TPM_MEASURE_ALGO (tlcl_get_family() == TPM_1 ? VB2_HASH_SHA1 : VB2_HASH_SHA256)
14-
#elif CONFIG(TPM_LOG_TPM1)
15-
# define TPM_MEASURE_ALGO VB2_HASH_SHA1
16-
#elif CONFIG(TPM_LOG_TPM2)
17-
# if CONFIG(TPM_HASH_SHA1)
18-
# define TPM_MEASURE_ALGO VB2_HASH_SHA1
19-
# endif
20-
# if CONFIG(TPM_HASH_SHA256)
21-
# define TPM_MEASURE_ALGO VB2_HASH_SHA256
22-
# endif
23-
# if CONFIG(TPM_HASH_SHA384)
24-
# define TPM_MEASURE_ALGO VB2_HASH_SHA384
25-
# endif
26-
# if CONFIG(TPM_HASH_SHA512)
27-
# define TPM_MEASURE_ALGO VB2_HASH_SHA512
28-
# endif
29-
#endif
30-
31-
#if !defined(TPM_MEASURE_ALGO)
32-
# if !CONFIG(TPM_MEASURED_BOOT)
33-
# define TPM_MEASURE_ALGO VB2_HASH_INVALID
34-
# else
35-
# error "Misconfiguration: failed to determine TPM hashing algorithm"
36-
# endif
37-
#endif
38-
3912
/**
4013
* Measure digests cached in TPM log entries into PCRs
4114
*/

src/security/tpm/tspi/log-tpm2.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ void *tpm2_log_cbmem_init(void)
7171
hdr->spec_errata = 0x00;
7272
hdr->uintn_size = 0x02; // 64-bit UINT
7373
hdr->num_of_algorithms = htole32(1);
74-
hdr->digest_sizes[0].alg_id = htole16(tpmalg_from_vb2_hash(TPM_MEASURE_ALGO));
75-
hdr->digest_sizes[0].digest_size = htole16(vb2_digest_size(TPM_MEASURE_ALGO));
74+
hdr->digest_sizes[0].alg_id = htole16(tpmalg_from_vb2_hash(tpm_log_alg()));
75+
hdr->digest_sizes[0].digest_size = htole16(vb2_digest_size(tpm_log_alg()));
7676

7777
tclt->vendor_info_size = sizeof(tclt->vendor);
7878
tclt->vendor.reserved = 0;
@@ -98,8 +98,8 @@ void tpm2_log_dump(void)
9898
if (!tclt)
9999
return;
100100

101-
hash_size = vb2_digest_size(TPM_MEASURE_ALGO);
102-
alg_name = vb2_get_hash_algorithm_name(TPM_MEASURE_ALGO);
101+
hash_size = vb2_digest_size(tpm_log_alg());
102+
alg_name = vb2_get_hash_algorithm_name(tpm_log_alg());
103103

104104
printk(BIOS_INFO, "coreboot TPM 2.0 measurements:\n\n");
105105
for (i = 0; i < le16toh(tclt->vendor.num_entries); i++) {
@@ -134,13 +134,13 @@ void tpm2_log_add_table_entry(const char *name, const uint32_t pcr,
134134
return;
135135
}
136136

137-
if (digest_algo != TPM_MEASURE_ALGO) {
137+
if (digest_algo != tpm_log_alg()) {
138138
printk(BIOS_WARNING, "TPM LOG: digest is of unsupported type: %s\n",
139139
vb2_get_hash_algorithm_name(digest_algo));
140140
return;
141141
}
142142

143-
if (digest_len != vb2_digest_size(TPM_MEASURE_ALGO)) {
143+
if (digest_len != vb2_digest_size(tpm_log_alg())) {
144144
printk(BIOS_WARNING, "TPM LOG: digest has invalid length: %d\n",
145145
(int)digest_len);
146146
return;
@@ -158,8 +158,8 @@ void tpm2_log_add_table_entry(const char *name, const uint32_t pcr,
158158
tce->event_type = htole32(EV_ACTION);
159159

160160
tce->digest_count = htole32(1);
161-
tce->digest_type = htole16(tpmalg_from_vb2_hash(TPM_MEASURE_ALGO));
162-
memcpy(tce->digest, digest, vb2_digest_size(TPM_MEASURE_ALGO));
161+
tce->digest_type = htole16(tpmalg_from_vb2_hash(tpm_log_alg()));
162+
memcpy(tce->digest, digest, vb2_digest_size(tpm_log_alg()));
163163

164164
tce->data_length = htole32(sizeof(tce->data));
165165
strncpy((char *)tce->data, name, sizeof(tce->data) - 1);
@@ -183,7 +183,7 @@ int tpm2_log_get(int entry_idx, int *pcr, const uint8_t **digest_data,
183183

184184
*pcr = le32toh(tce->pcr);
185185
*digest_data = tce->digest;
186-
*digest_algo = TPM_MEASURE_ALGO; /* We validate algorithm on addition */
186+
*digest_algo = tpm_log_alg(); /* We validate algorithm on addition */
187187
*event_name = (char *)tce->data;
188188
return 0;
189189
}

src/security/tpm/tspi/tspi.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,9 @@ tpm_result_t tpm_measure_region(const struct region_device *rdev, uint8_t pcr,
265265
if (!rdev || !rname)
266266
return TPM_CB_INVALID_ARG;
267267

268-
digest_len = vb2_digest_size(TPM_MEASURE_ALGO);
268+
digest_len = vb2_digest_size(tpm_log_alg());
269269
assert(digest_len <= sizeof(digest));
270-
if (vb2_digest_init(&ctx, vboot_hwcrypto_allowed(), TPM_MEASURE_ALGO,
270+
if (vb2_digest_init(&ctx, vboot_hwcrypto_allowed(), tpm_log_alg(),
271271
region_device_sz(rdev))) {
272272
printk(BIOS_ERR, "TPM: Error initializing hash.\n");
273273
return TPM_CB_HASH_ERROR;
@@ -293,6 +293,6 @@ tpm_result_t tpm_measure_region(const struct region_device *rdev, uint8_t pcr,
293293
printk(BIOS_ERR, "TPM: Error finalizing hash.\n");
294294
return TPM_CB_HASH_ERROR;
295295
}
296-
return tpm_extend_pcr(pcr, TPM_MEASURE_ALGO, digest, digest_len, rname);
296+
return tpm_extend_pcr(pcr, tpm_log_alg(), digest, digest_len, rname);
297297
}
298298
#endif /* VBOOT_LIB */

0 commit comments

Comments
 (0)