[coreboot-gerrit] Patch set updated for coreboot: TPM: add measurement code
Georg Wicherski (gw@oxff.net)
gerrit at coreboot.org
Mon Mar 14 13:16:43 CET 2016
Georg Wicherski (gw at oxff.net) just uploaded a new patch set to gerrit, which you can find at https://review.coreboot.org/14038
-gerrit
commit 29ec80eb8edfbdba19c242d91a3465b3ecdc4e97
Author: Georg Wicherski <gw at oxff.net>
Date: Fri Mar 11 17:53:01 2016 +0100
TPM: add measurement code
WORK IN PROGRESS
Measure ramstage, PCI option ROMs, and payload, extend PCRs.
Change-Id: I1c15108e4a27e9929ae02c6409c3cae0bda073a5
---
src/device/pci_device.c | 12 ++-
src/drivers/tpm/lpc/Kconfig | 40 ++++++++
src/include/tpm/tspi.h | 23 +++++
src/lib/prog_loaders.c | 13 ++-
src/lib/tpm/Makefile.inc | 5 +
src/lib/tpm/sha1.c | 222 ++++++++++++++++++++++++++++++++++++++++++++
src/lib/tpm/tspi.c | 48 +++++++++-
7 files changed, 356 insertions(+), 7 deletions(-)
diff --git a/src/device/pci_device.c b/src/device/pci_device.c
index 5123229..0301ac7 100644
--- a/src/device/pci_device.c
+++ b/src/device/pci_device.c
@@ -42,6 +42,7 @@
#include <pc80/i8259.h>
#include <kconfig.h>
#include <vendorcode/google/chromeos/chromeos.h>
+#include <tpm/tspi.h>
u8 pci_moving_config8(struct device *dev, unsigned int reg)
{
@@ -711,10 +712,19 @@ void pci_dev_init(struct device *dev)
ram = pci_rom_load(dev, rom);
if (ram == NULL)
return;
+
+ if (IS_ENABLED(CONFIG_TPM_MEASURE_VGA_ROM)) {
+ char rom_name[64];
+ snprintf(rom_name, sizeof (rom_name),
+ "Option ROM %s",
+ dev_path(dev));
+ measure_thing((uint8_t *) ram,
+ ram->size * 512, 2, rom_name);
+ }
if (!should_run_oprom(dev))
return;
-
+
run_bios(dev, (unsigned long)ram);
gfx_set_init_done(1);
printk(BIOS_DEBUG, "VGA Option ROM was run\n");
diff --git a/src/drivers/tpm/lpc/Kconfig b/src/drivers/tpm/lpc/Kconfig
index ec17782..595db12 100644
--- a/src/drivers/tpm/lpc/Kconfig
+++ b/src/drivers/tpm/lpc/Kconfig
@@ -13,6 +13,46 @@ config LPC_TPM
If unsure, say N.
+config TPM_MEASURE
+ bool "Enable TPM PCR measurements"
+ depends on LPC_TPM
+ default n
+ help
+ Add code to compute SHA1 digests on coreboot components and the
+ payload; extend appropriate PCRs with these digests.
+
+ If unsure, say N.
+
+config TPM_MEASURE_RAMSTAGE
+ bool "Measure the coreboot ramstage"
+ depends on TPM_MEASURE
+ default n
+ help
+ Compute SHA1 digest on the ramstage after loading it. Extend PCR[0]
+ with the digest.
+
+ If unsure, say N.
+
+config TPM_MEASURE_VGA_ROM
+ bool "Measure PCI option ROMs"
+ depends on TPM_MEASURE && VGA_ROM_RUN
+ default n
+ help
+ Compute SHA1 digest on any option ROM after loading it. Extend PCR[2]
+ with the digest. Also measures ROMs that are not actually run.
+
+ If unsure, say N.
+
+config TPM_MEASURE_PAYLOAD
+ bool "Measure the coreboot payload"
+ depends on TPM_MEASURE
+ default n
+ help
+ Compute SHA1 digest on the payload as it is found in memory just
+ before mirroring it. Extend PCR[4] with the digest.
+
+ If unsure, say N.
+
config TPM_READ_INIT_PCRS
bool "Read PCR values after initialization"
depends on LPC_TPM
diff --git a/src/include/tpm/tspi.h b/src/include/tpm/tspi.h
index ba2f119..8c4b11b 100644
--- a/src/include/tpm/tspi.h
+++ b/src/include/tpm/tspi.h
@@ -17,10 +17,33 @@
#ifndef TPM_TSPI_H
#define TPM_TSPI_H
+#include <stdint.h>
+#include <program_loading.h>
+
/**
* TSPI Implementation for coreboot without vboot2
*/
void init_tpm(int s3resume);
+/**
+ * Compute SHA1 digest on arbitrary memory contents and
+ * extend requested PCR with the digest.
+ */
+void measure_thing(const uint8_t * data, size_t size, int pcr,
+ const char *thing_name);
+
+/**
+ * Compute SHA1 digest on program memory contents and
+ * extend requested PCR with the digest.
+ *
+ * Wraps measure_thing .
+ */
+void measure_prog(struct prog *program, int pcr, const char *prog_name);
+
+
+#define SHA1_BLOCK_LENGTH 64
+#define SHA1_DIGEST_LENGTH 20
+u8 *sha1(const u8 *data, unsigned long len, u8 *buf);
+
#endif /* TPM_TSPI_H */
diff --git a/src/lib/prog_loaders.c b/src/lib/prog_loaders.c
index 14e1299..c67882a 100644
--- a/src/lib/prog_loaders.c
+++ b/src/lib/prog_loaders.c
@@ -28,6 +28,7 @@
#include <stage_cache.h>
#include <symbols.h>
#include <timestamp.h>
+#include <tpm/tspi.h>
/* Only can represent up to 1 byte less than size_t. */
const struct mem_region_device addrspace_32bit = MEM_REGION_DEV_INIT(0, ~0UL);
@@ -53,7 +54,7 @@ void run_romstage(void)
if (prog_locate(&romstage))
goto fail;
-
+
timestamp_add_now(TS_START_COPYROM);
if (cbfs_prog_stage_load(&romstage))
@@ -115,8 +116,12 @@ void run_ramstage(void)
if (prog_locate(&ramstage))
goto fail;
- timestamp_add_now(TS_START_COPYRAM);
+ if (IS_ENABLED(CONFIG_TPM_MEASURE_RAMSTAGE)) {
+ measure_prog(&ramstage, 0, "coreboot ramstage");
+ }
+ timestamp_add_now(TS_START_COPYRAM);
+
if (IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)) {
if (load_relocatable_ramstage(&ramstage))
goto fail;
@@ -151,6 +156,10 @@ void payload_load(void)
if (prog_locate(payload))
goto out;
+
+ if (IS_ENABLED(CONFIG_TPM_MEASURE_PAYLOAD)) {
+ measure_prog(payload, 4, "coreboot payload");
+ }
mirror_payload(payload);
diff --git a/src/lib/tpm/Makefile.inc b/src/lib/tpm/Makefile.inc
index 81a3eb9..60bdfa2 100644
--- a/src/lib/tpm/Makefile.inc
+++ b/src/lib/tpm/Makefile.inc
@@ -1,10 +1,15 @@
ifneq ($(CONFIG_CHROMEOS),y)
romstage-$(CONFIG_LPC_TPM) += tss.c tspi.c
ramstage-$(CONFIG_LPC_TPM) += tss.c tspi.c
+bootblock-$(CONFIG_LPC_TPM) += tss.c tspi.c
else
romstage-$(CONFIG_LPC_TPM) += tspi.c
endif
+bootblock-$(CONFIG_TPM_MEASURE) += sha1.c
+romstage-$(CONFIG_TPM_MEASURE) += sha1.c
+ramstage-$(CONFIG_TPM_MEASURE) += sha1.c
+
ifeq ($(MOCK_TPM),1)
libverstage-y += mocked_tss.c
else
diff --git a/src/lib/tpm/sha1.c b/src/lib/tpm/sha1.c
new file mode 100644
index 0000000..2f1f9a6
--- /dev/null
+++ b/src/lib/tpm/sha1.c
@@ -0,0 +1,222 @@
+/*
+ * This file was copied from the libpayload project.
+ *
+ * It has originally been taken from the OpenBSD project.
+ */
+
+/* $OpenBSD: sha1.c,v 1.20 2005/08/08 08:05:35 espie Exp $ */
+
+/*
+ * SHA-1 in C
+ * By Steve Reid <steve at edmweb.com>
+ * 100% Public Domain
+ *
+ * Test Vectors (from FIPS PUB 180-1)
+ * "abc"
+ * A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
+ * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
+ * 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
+ * A million repetitions of "a"
+ * 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
+ */
+
+#include <tpm/tspi.h>
+#include <string.h>
+
+typedef u8 u_int8_t;
+typedef u32 u_int32_t;
+typedef u64 u_int64_t;
+typedef unsigned int u_int;
+
+typedef struct {
+ u32 state[5];
+ u64 count;
+ u8 buffer[SHA1_BLOCK_LENGTH];
+} SHA1_CTX;
+
+
+#define LITTLE_ENDIAN 1234
+#define BIG_ENDIAN 4321
+
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define BYTE_ORDER LITTLE_ENDIAN
+#else
+#define BYTE_ORDER BIG_ENDIAN
+#endif
+
+#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
+
+/*
+ * blk0() and blk() perform the initial expand.
+ * I got the idea of expanding during the round function from SSLeay
+ */
+#if BYTE_ORDER == LITTLE_ENDIAN
+# define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
+ |(rol(block->l[i],8)&0x00FF00FF))
+#else
+# define blk0(i) block->l[i]
+#endif
+#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
+ ^block->l[(i+2)&15]^block->l[i&15],1))
+
+/*
+ * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
+ */
+#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
+#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
+#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
+#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
+#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
+
+/*
+ * Hash a single 512-bit block. This is the core of the algorithm.
+ */
+static void
+SHA1Transform(u_int32_t state[5], const u_int8_t buffer[SHA1_BLOCK_LENGTH])
+{
+ u_int32_t a, b, c, d, e;
+ u_int8_t workspace[SHA1_BLOCK_LENGTH];
+ typedef union {
+ u_int8_t c[64];
+ u_int32_t l[16];
+ } CHAR64LONG16;
+ CHAR64LONG16 *block = (CHAR64LONG16 *)workspace;
+
+ (void)memcpy(block, buffer, SHA1_BLOCK_LENGTH);
+
+ /* Copy context->state[] to working vars */
+ a = state[0];
+ b = state[1];
+ c = state[2];
+ d = state[3];
+ e = state[4];
+
+ /* 4 rounds of 20 operations each. Loop unrolled. */
+ R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
+ R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
+ R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
+ R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
+ R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
+ R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
+ R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
+ R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
+ R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
+ R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
+ R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
+ R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
+ R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
+ R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
+ R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
+ R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
+ R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
+ R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
+ R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
+ R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
+
+ /* Add the working vars back into context.state[] */
+ state[0] += a;
+ state[1] += b;
+ state[2] += c;
+ state[3] += d;
+ state[4] += e;
+
+ /* Wipe variables */
+ a = b = c = d = e = 0;
+}
+
+
+/*
+ * SHA1Init - Initialize new context
+ */
+static void
+SHA1Init(SHA1_CTX *context)
+{
+
+ /* SHA1 initialization constants */
+ context->count = 0;
+ context->state[0] = 0x67452301;
+ context->state[1] = 0xEFCDAB89;
+ context->state[2] = 0x98BADCFE;
+ context->state[3] = 0x10325476;
+ context->state[4] = 0xC3D2E1F0;
+}
+
+
+/*
+ * Run your data through this.
+ */
+static void
+SHA1Update(SHA1_CTX *context, const u_int8_t *data, unsigned long len)
+{
+ size_t i, j;
+
+ j = (size_t)((context->count >> 3) & 63);
+ context->count += (len << 3);
+ if ((j + len) > 63) {
+ (void)memcpy(&context->buffer[j], data, (i = 64-j));
+ SHA1Transform(context->state, context->buffer);
+ for ( ; i + 63 < len; i += 64)
+ SHA1Transform(context->state, (u_int8_t *)&data[i]);
+ j = 0;
+ } else {
+ i = 0;
+ }
+ (void)memcpy(&context->buffer[j], &data[i], len - i);
+}
+
+
+/*
+ * Add padding and return the message digest.
+ */
+static void
+SHA1Pad(SHA1_CTX *context)
+{
+ u_int8_t finalcount[8];
+ u_int i;
+
+ for (i = 0; i < 8; i++) {
+ finalcount[i] = (u_int8_t)((context->count >>
+ ((7 - (i & 7)) * 8)) & 255); /* Endian independent */
+ }
+ SHA1Update(context, (u_int8_t *)"\200", 1);
+ while ((context->count & 504) != 448)
+ SHA1Update(context, (u_int8_t *)"\0", 1);
+ SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
+}
+
+static void
+SHA1Final(u_int8_t digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context)
+{
+ u_int i;
+
+ SHA1Pad(context);
+ if (digest) {
+ for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
+ digest[i] = (u_int8_t)
+ ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
+ }
+ memset(context, 0, sizeof(*context));
+ }
+}
+
+/**
+ * Compute the SHA-1 hash of the given data as specified by the 'data' and
+ * 'len' arguments, and place the result -- 160 bits (20 bytes) -- into the
+ * specified output buffer 'buf'.
+ *
+ * @param data Pointer to the input data that shall be hashed.
+ * @param len Length of the input data (in bytes).
+ * @param buf Buffer which will hold the resulting hash (must be at
+ * least 20 bytes in size).
+ * @return Pointer to the output buffer where the hash is stored.
+ */
+u8 *sha1(const u8 *data, unsigned long len, u8 *buf)
+{
+ SHA1_CTX ctx;
+
+ SHA1Init(&ctx);
+ SHA1Update(&ctx, data, len);
+ SHA1Final(buf, &ctx);
+
+ return buf;
+}
diff --git a/src/lib/tpm/tspi.c b/src/lib/tpm/tspi.c
index 0d9c6aa..bd3025b 100644
--- a/src/lib/tpm/tspi.c
+++ b/src/lib/tpm/tspi.c
@@ -19,14 +19,13 @@
#include <reset.h>
#include <tpm/tss.h>
-#define TPM_PCR_DIGEST_LENGTH 20
#include <tpm/tspi.h>
static inline void _debug_init_pcrs(void)
{
uint32_t result;
int pcr_index;
- uint8_t digest[TPM_PCR_DIGEST_LENGTH];
+ uint8_t digest[SHA1_DIGEST_LENGTH];
for (pcr_index = 0; pcr_index < 24; ++pcr_index) {
int digest_offset;
@@ -43,11 +42,11 @@ static inline void _debug_init_pcrs(void)
printk(BIOS_SPEW, "TPM: PCR %i value is ",
pcr_index);
for (digest_offset = 0;
- digest_offset < TPM_PCR_DIGEST_LENGTH;
+ digest_offset < SHA1_DIGEST_LENGTH;
++digest_offset) {
printk(BIOS_SPEW, "%02x%c",
digest[digest_offset],
- digest_offset < TPM_PCR_DIGEST_LENGTH - 1 ?
+ digest_offset < SHA1_DIGEST_LENGTH - 1 ?
':' : '\n');
}
}
@@ -125,3 +124,44 @@ void init_tpm(int s3resume)
hard_reset();
}
}
+
+void measure_thing(const uint8_t * data, size_t size, int pcr,
+ const char *thing_name)
+{
+ uint8_t digest[SHA1_DIGEST_LENGTH];
+
+ sha1(data, size, digest);
+
+ printk(BIOS_DEBUG, "TPM: Digest for \"%s\": ",
+ thing_name);
+ for(size = 0; size < SHA1_DIGEST_LENGTH; ++size) {
+ printk(BIOS_SPEW, "%02x%c",
+ digest[size],
+ size < SHA1_DIGEST_LENGTH - 1 ?
+ ':' : '\n');
+ }
+
+ if (tlcl_extend(pcr, digest, digest) == TPM_SUCCESS) {
+ printk(BIOS_DEBUG, "TPM: PCR[%i] after extension: ", pcr);
+
+ for(size = 0; size < SHA1_DIGEST_LENGTH; ++size) {
+ printk(BIOS_SPEW, "%02x%c",
+ digest[size],
+ size < SHA1_DIGEST_LENGTH - 1 ?
+ ':' : '\n');
+ }
+ }
+}
+
+void measure_prog(struct prog *program, int pcr, const char *prog_name) {
+ const uint8_t * start;
+ size_t size;
+
+ start = prog_start(program);
+ size = prog_size(program);
+
+ printk(BIOS_DEBUG, "TPM: Measuring program \"%s\" at %p (%lx bytes)\n",
+ prog_name, start, size);
+
+ return measure_thing(start, size, pcr, prog_name);
+}
More information about the coreboot-gerrit
mailing list