Matt DeVillier | 9ce755d | 2023-01-23 18:31:27 -0600 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include <security/vboot/antirollback.h> |
| 4 | #include <program_loading.h> |
| 5 | #include <vb2_api.h> |
| 6 | #include <security/tpm/tss.h> |
| 7 | #include <security/vboot/misc.h> |
| 8 | #include <security/vboot/vbios_cache_hash_tpm.h> |
| 9 | #include <console/console.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | void vbios_cache_update_hash(const uint8_t *data, size_t size) |
| 13 | { |
| 14 | struct vb2_hash hash; |
| 15 | |
| 16 | /* Initialize TPM driver. */ |
| 17 | if (tlcl_lib_init() != VB2_SUCCESS) { |
| 18 | printk(BIOS_ERR, "VBIOS_CACHE: TPM driver initialization failed.\n"); |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | /* Calculate hash of vbios data. */ |
| 23 | if (vb2_hash_calculate(vboot_hwcrypto_allowed(), data, size, |
| 24 | VB2_HASH_SHA256, &hash)) { |
| 25 | printk(BIOS_ERR, "VBIOS_CACHE: SHA-256 calculation failed for data; " |
| 26 | "clearing TPM hash space.\n"); |
| 27 | /* |
| 28 | * Since data is being updated in vbios cache, the hash |
| 29 | * currently stored in TPM hash space is no longer |
| 30 | * valid. If we are not able to calculate hash of the |
| 31 | * data being updated, reset all the bits in TPM hash |
| 32 | * space to zero to invalidate it. |
| 33 | */ |
| 34 | memset(hash.raw, 0, VB2_SHA256_DIGEST_SIZE); |
| 35 | } |
| 36 | |
| 37 | /* Write hash of data to TPM space. */ |
| 38 | if (antirollback_write_space_vbios_hash(hash.sha256, sizeof(hash.sha256)) |
| 39 | != TPM_SUCCESS) { |
| 40 | printk(BIOS_ERR, "VBIOS_CACHE: Could not save hash to TPM.\n"); |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | printk(BIOS_INFO, "VBIOS_CACHE: TPM NV idx 0x%x updated successfully.\n", |
| 45 | VBIOS_CACHE_NV_INDEX); |
| 46 | } |
| 47 | |
| 48 | enum cb_err vbios_cache_verify_hash(const uint8_t *data, size_t size) |
| 49 | { |
| 50 | struct vb2_hash tpm_hash = { .algo = VB2_HASH_SHA256 }; |
| 51 | |
| 52 | /* Initialize TPM driver. */ |
| 53 | if (tlcl_lib_init() != VB2_SUCCESS) { |
| 54 | printk(BIOS_ERR, "VBIOS_CACHE: TPM driver initialization failed.\n"); |
| 55 | return CB_ERR; |
| 56 | } |
| 57 | |
| 58 | /* Read hash of VBIOS data saved in TPM. */ |
| 59 | if (antirollback_read_space_vbios_hash(tpm_hash.sha256, sizeof(tpm_hash.sha256)) |
| 60 | != TPM_SUCCESS) { |
| 61 | printk(BIOS_ERR, "VBIOS_CACHE: Could not read hash from TPM.\n"); |
| 62 | return CB_ERR; |
| 63 | } |
| 64 | |
| 65 | /* Calculate hash of data read from VBIOS FMAP CACHE and compare. */ |
| 66 | if (vb2_hash_verify(vboot_hwcrypto_allowed(), data, size, &tpm_hash)) { |
| 67 | printk(BIOS_ERR, "VBIOS_CACHE: Hash comparison failed.\n"); |
| 68 | return CB_ERR; |
| 69 | } |
| 70 | |
| 71 | printk(BIOS_INFO, "VBIOS_CACHE: Hash idx 0x%x comparison successful.\n", |
| 72 | VBIOS_CACHE_NV_INDEX); |
| 73 | |
| 74 | return CB_SUCCESS; |
| 75 | } |