summaryrefslogtreecommitdiffstats
path: root/src/security/vboot/vbios_cache_hash_tpm.c
blob: 6ad09ace8e178e90b667106d8d4e2c69f6c3f44a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* SPDX-License-Identifier: GPL-2.0-only */

#include <security/vboot/antirollback.h>
#include <program_loading.h>
#include <vb2_api.h>
#include <security/tpm/tss.h>
#include <security/vboot/misc.h>
#include <security/vboot/vbios_cache_hash_tpm.h>
#include <console/console.h>
#include <string.h>

void vbios_cache_update_hash(const uint8_t *data, size_t size)
{
	struct vb2_hash hash;

	/* Initialize TPM driver. */
	if (tlcl_lib_init() != VB2_SUCCESS) {
		printk(BIOS_ERR, "VBIOS_CACHE: TPM driver initialization failed.\n");
		return;
	}

	/* Calculate hash of vbios data. */
	if (vb2_hash_calculate(vboot_hwcrypto_allowed(), data, size,
			       VB2_HASH_SHA256, &hash)) {
		printk(BIOS_ERR, "VBIOS_CACHE: SHA-256 calculation failed for data; "
		       "clearing TPM hash space.\n");
		/*
		 * Since data is being updated in vbios cache, the hash
		 * currently stored in TPM hash space is no longer
		 * valid. If we are not able to calculate hash of the
		 * data being updated, reset all the bits in TPM hash
		 * space to zero to invalidate it.
		 */
		memset(hash.raw, 0, VB2_SHA256_DIGEST_SIZE);
	}

	/* Write hash of data to TPM space. */
	if (antirollback_write_space_vbios_hash(hash.sha256, sizeof(hash.sha256))
			!= TPM_SUCCESS) {
		printk(BIOS_ERR, "VBIOS_CACHE: Could not save hash to TPM.\n");
		return;
	}

	printk(BIOS_INFO, "VBIOS_CACHE: TPM NV idx %#x updated successfully.\n",
			VBIOS_CACHE_NV_INDEX);
}

enum cb_err vbios_cache_verify_hash(const uint8_t *data, size_t size)
{
	struct vb2_hash tpm_hash = { .algo = VB2_HASH_SHA256 };

	/* Initialize TPM driver. */
	if (tlcl_lib_init() != VB2_SUCCESS) {
		printk(BIOS_ERR, "VBIOS_CACHE: TPM driver initialization failed.\n");
		return CB_ERR;
	}

	/* Read hash of VBIOS data saved in TPM. */
	if (antirollback_read_space_vbios_hash(tpm_hash.sha256,	sizeof(tpm_hash.sha256))
			!= TPM_SUCCESS) {
		printk(BIOS_ERR, "VBIOS_CACHE: Could not read hash from TPM.\n");
		return CB_ERR;
	}

	/* Calculate hash of data read from VBIOS FMAP CACHE and compare. */
	if (vb2_hash_verify(vboot_hwcrypto_allowed(), data, size, &tpm_hash)) {
		printk(BIOS_ERR, "VBIOS_CACHE: Hash comparison failed.\n");
		return CB_ERR;
	}

	printk(BIOS_INFO, "VBIOS_CACHE: Hash idx %#x comparison successful.\n",
			VBIOS_CACHE_NV_INDEX);

	return CB_SUCCESS;
}