All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: U-Boot Mailing List <u-boot@lists.denx.de>
Cc: Ilias Apalodimas <ilias.apalodimas@linaro.org>,
	Thirupathaiah Annapureddy <thiruan@linux.microsoft.com>,
	Simon Glass <sjg@chromium.org>,
	Heinrich Schuchardt <xypron.glpk@gmx.de>
Subject: [PATCH v2 10/10] sandbox: tpm: Support extending a PCR multiple times
Date: Sun, 18 Jul 2021 14:18:06 -0600	[thread overview]
Message-ID: <20210718201806.761202-11-sjg@chromium.org> (raw)
In-Reply-To: <20210718201806.761202-1-sjg@chromium.org>

It is fairly easy to handle this case and it makes the emulator more
useful, since PCRs are commonly extended several times.

Add support for this, using U-Boot's sha256 support.

For now sandbox only supports a single PCR, but that is enough for the
tests that currently exist.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

Changes in v2:
- Drop the constant sandbox_extended_once_pcr since we can calculate it
- Update the commit message to explain that there is only one PCR

 drivers/tpm/tpm2_tis_sandbox.c | 24 ++++++++++--------------
 test/py/tests/test_tpm2.py     | 18 +++++++++++++++++-
 2 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/drivers/tpm/tpm2_tis_sandbox.c b/drivers/tpm/tpm2_tis_sandbox.c
index 3c4bbcdf2ee..ac6eb143539 100644
--- a/drivers/tpm/tpm2_tis_sandbox.c
+++ b/drivers/tpm/tpm2_tis_sandbox.c
@@ -11,6 +11,7 @@
 #include <asm/unaligned.h>
 #include <linux/bitops.h>
 #include <u-boot/crc.h>
+#include <u-boot/sha256.h>
 #include "sandbox_common.h"
 
 /* Hierarchies */
@@ -39,13 +40,6 @@ enum tpm2_cap_tpm_property {
 
 #define SANDBOX_TPM_PCR_NB 1
 
-static const u8 sandbox_extended_once_pcr[] = {
-	0xf5, 0xa5, 0xfd, 0x42, 0xd1, 0x6a, 0x20, 0x30,
-	0x27, 0x98, 0xef, 0x6e, 0xd3, 0x09, 0x97, 0x9b,
-	0x43, 0x00, 0x3d, 0x23, 0x20, 0xd9, 0xf0, 0xe8,
-	0xea, 0x98, 0x31, 0xa9, 0x27, 0x59, 0xfb, 0x4b,
-};
-
 /*
  * Information about our TPM emulation. This is preserved in the sandbox
  * state file if enabled.
@@ -407,15 +401,17 @@ static int sandbox_tpm2_extend(struct udevice *dev, int pcr_index,
 			       const u8 *extension)
 {
 	struct sandbox_tpm2 *tpm = dev_get_priv(dev);
-	int i;
+	sha256_context ctx;
+
+	/* Zero the PCR if this is the first use */
+	if (!tpm->pcr_extensions[pcr_index])
+		memset(tpm->pcr[pcr_index], '\0', TPM2_DIGEST_LEN);
 
-	/* Only simulate the first extensions from all '0' with only '0' */
-	for (i = 0; i < TPM2_DIGEST_LEN; i++)
-		if (tpm->pcr[pcr_index][i] || extension[i])
-			return TPM2_RC_FAILURE;
+	sha256_starts(&ctx);
+	sha256_update(&ctx, tpm->pcr[pcr_index], TPM2_DIGEST_LEN);
+	sha256_update(&ctx, extension, TPM2_DIGEST_LEN);
+	sha256_finish(&ctx, tpm->pcr[pcr_index]);
 
-	memcpy(tpm->pcr[pcr_index], sandbox_extended_once_pcr,
-	       TPM2_DIGEST_LEN);
 	tpm->pcr_extensions[pcr_index]++;
 
 	return 0;
diff --git a/test/py/tests/test_tpm2.py b/test/py/tests/test_tpm2.py
index 70f906da511..ac04f7191ec 100644
--- a/test/py/tests/test_tpm2.py
+++ b/test/py/tests/test_tpm2.py
@@ -216,7 +216,9 @@ def test_tpm2_pcr_extend(u_boot_console):
     output = u_boot_console.run_command('echo $?')
     assert output.endswith('0')
 
-    read_pcr = u_boot_console.run_command('tpm2 pcr_read 0 0x%x' % ram)
+    # Read the value back into a different place so we can still use 'ram' as
+    # our zero bytes
+    read_pcr = u_boot_console.run_command('tpm2 pcr_read 0 0x%x' % (ram + 0x20))
     output = u_boot_console.run_command('echo $?')
     assert output.endswith('0')
     assert 'f5 a5 fd 42 d1 6a 20 30 27 98 ef 6e d3 09 97 9b' in read_pcr
@@ -226,6 +228,20 @@ def test_tpm2_pcr_extend(u_boot_console):
     new_updates = int(re.findall(r'\d+', str)[0])
     assert (updates + 1) == new_updates
 
+    u_boot_console.run_command('tpm2 pcr_extend 0 0x%x' % ram)
+    output = u_boot_console.run_command('echo $?')
+    assert output.endswith('0')
+
+    read_pcr = u_boot_console.run_command('tpm2 pcr_read 0 0x%x' % (ram + 0x20))
+    output = u_boot_console.run_command('echo $?')
+    assert output.endswith('0')
+    assert '7a 05 01 f5 95 7b df 9c b3 a8 ff 49 66 f0 22 65' in read_pcr
+    assert 'f9 68 65 8b 7a 9c 62 64 2c ba 11 65 e8 66 42 f5' in read_pcr
+
+    str = re.findall(r'\d+ known updates', read_pcr)[0]
+    new_updates = int(re.findall(r'\d+', str)[0])
+    assert (updates + 2) == new_updates
+
 @pytest.mark.buildconfigspec('cmd_tpm_v2')
 def test_tpm2_cleanup(u_boot_console):
     """Ensure the TPM is cleared from password or test related configuration."""
-- 
2.32.0.402.g57bb445576-goog


  parent reply	other threads:[~2021-07-18 20:19 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-18 20:17 [PATCH v2 00/10] tpm: Enhance sandbox tpm2 emulation Simon Glass
2021-07-18 20:17 ` [PATCH v2 01/10] sandbox: tpm: Split out common nvdata code Simon Glass
2021-07-18 20:17 ` [PATCH v2 02/10] sandbox: tpm: Tidy up reading and writing of device state Simon Glass
2021-07-18 20:17 ` [PATCH v2 03/10] sandbox: tpm: Support the define-space command Simon Glass
2021-07-18 20:18 ` [PATCH v2 04/10] sandbox: tpm: Correct handling of get-capability Simon Glass
2021-07-18 20:18 ` [PATCH v2 05/10] sandbox: tpm: Finish comments for struct sandbox_tpm2 Simon Glass
2021-07-18 20:18 ` [PATCH v2 06/10] sandbox: tpm: Track whether the state is valid Simon Glass
2021-07-18 20:18 ` [PATCH v2 07/10] sandbox: tpm: Support nvdata in TPM2 Simon Glass
2021-07-18 20:18 ` [PATCH v2 08/10] sandbox: tpm: Support storing device state in tpm2 Simon Glass
2021-07-18 20:18 ` [PATCH v2 09/10] sandbox: tpm: Correct handling of SANDBOX_TPM_PCR_NB Simon Glass
2021-07-18 20:18 ` Simon Glass [this message]
2021-07-24 21:11 ` [PATCH v2 10/10] sandbox: tpm: Support extending a PCR multiple times Simon Glass
2021-07-24 21:12 ` [PATCH v2 09/10] sandbox: tpm: Correct handling of SANDBOX_TPM_PCR_NB Simon Glass
2021-07-24 21:12 ` [PATCH v2 08/10] sandbox: tpm: Support storing device state in tpm2 Simon Glass
2021-07-24 21:12 ` [PATCH v2 07/10] sandbox: tpm: Support nvdata in TPM2 Simon Glass
2021-07-24 21:12 ` [PATCH v2 06/10] sandbox: tpm: Track whether the state is valid Simon Glass
2021-07-24 21:12 ` [PATCH v2 05/10] sandbox: tpm: Finish comments for struct sandbox_tpm2 Simon Glass
2021-07-24 21:12 ` [PATCH v2 04/10] sandbox: tpm: Correct handling of get-capability Simon Glass
2021-07-24 21:12 ` [PATCH v2 03/10] sandbox: tpm: Support the define-space command Simon Glass
2021-07-24 21:12 ` [PATCH v2 02/10] sandbox: tpm: Tidy up reading and writing of device state Simon Glass
2021-07-24 21:12 ` [PATCH v2 01/10] sandbox: tpm: Split out common nvdata code Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210718201806.761202-11-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=ilias.apalodimas@linaro.org \
    --cc=thiruan@linux.microsoft.com \
    --cc=u-boot@lists.denx.de \
    --cc=xypron.glpk@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.