All of lore.kernel.org
 help / color / mirror / Atom feed
From: Miquel Raynal <miquel.raynal@bootlin.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v4 24/32] tpm: add PCR authentication commands support
Date: Tue, 15 May 2018 11:57:20 +0200	[thread overview]
Message-ID: <20180515095728.16572-25-miquel.raynal@bootlin.com> (raw)
In-Reply-To: <20180515095728.16572-1-miquel.raynal@bootlin.com>

Add support for the TPM2_PCR_SetAuthPolicy and
TPM2_PCR_SetAuthValue commands.

Change the command file and the help accordingly.

Note: These commands could not be tested because the TPMs available
do not support them, however they could be useful for someone else.
The user is warned by the command help.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
---
 cmd/tpm-v2.c     |  48 ++++++++++++++++++++++++++
 include/tpm-v2.h |  29 ++++++++++++++++
 lib/tpm-v2.c     | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 177 insertions(+)

diff --git a/cmd/tpm-v2.c b/cmd/tpm-v2.c
index b52409846b..6d7d6cabbd 100644
--- a/cmd/tpm-v2.c
+++ b/cmd/tpm-v2.c
@@ -264,6 +264,43 @@ static int do_tpm_change_auth(cmd_tbl_t *cmdtp, int flag, int argc,
 						   oldpw, oldpw_sz));
 }
 
+static int do_tpm_pcr_setauthpolicy(cmd_tbl_t *cmdtp, int flag, int argc,
+				    char * const argv[])
+{
+	u32 index = simple_strtoul(argv[1], NULL, 0);
+	char *key = argv[2];
+	const char *pw = (argc < 4) ? NULL : argv[3];
+	const ssize_t pw_sz = pw ? strlen(pw) : 0;
+
+	if (strlen(key) != TPM2_DIGEST_LEN)
+		return -EINVAL;
+
+	if (argc < 3 || argc > 4)
+		return CMD_RET_USAGE;
+
+	return report_return_code(tpm2_pcr_setauthpolicy(pw, pw_sz, index,
+							 key));
+}
+
+static int do_tpm_pcr_setauthvalue(cmd_tbl_t *cmdtp, int flag,
+				   int argc, char * const argv[])
+{
+	u32 index = simple_strtoul(argv[1], NULL, 0);
+	char *key = argv[2];
+	const ssize_t key_sz = strlen(key);
+	const char *pw = (argc < 4) ? NULL : argv[3];
+	const ssize_t pw_sz = pw ? strlen(pw) : 0;
+
+	if (strlen(key) != TPM2_DIGEST_LEN)
+		return -EINVAL;
+
+	if (argc < 3 || argc > 4)
+		return CMD_RET_USAGE;
+
+	return report_return_code(tpm2_pcr_setauthvalue(pw, pw_sz, index,
+							key, key_sz));
+}
+
 static cmd_tbl_t tpm2_commands[] = {
 	U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
 	U_BOOT_CMD_MKENT(init, 0, 1, do_tpm_init, "", ""),
@@ -276,6 +313,10 @@ static cmd_tbl_t tpm2_commands[] = {
 	U_BOOT_CMD_MKENT(dam_reset, 0, 1, do_tpm_dam_reset, "", ""),
 	U_BOOT_CMD_MKENT(dam_parameters, 0, 1, do_tpm_dam_parameters, "", ""),
 	U_BOOT_CMD_MKENT(change_auth, 0, 1, do_tpm_change_auth, "", ""),
+	U_BOOT_CMD_MKENT(pcr_setauthpolicy, 0, 1,
+			 do_tpm_pcr_setauthpolicy, "", ""),
+	U_BOOT_CMD_MKENT(pcr_setauthvalue, 0, 1,
+			 do_tpm_pcr_setauthvalue, "", ""),
 };
 
 cmd_tbl_t *get_tpm_commands(unsigned int *size)
@@ -338,4 +379,11 @@ U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm, "Issue a TPMv2.x command",
 "    <hierarchy>: the hierarchy\n"
 "    <new_pw>: new password for <hierarchy>\n"
 "    <old_pw>: optional previous password of <hierarchy>\n"
+"pcr_setauthpolicy|pcr_setauthvalue <pcr> <key> [<password>]\n"
+"    Change the <key> to access PCR #<pcr>.\n"
+"    hierarchy and may be empty.\n"
+"    /!\\WARNING: untested function, use at your own risks !\n"
+"    <pcr>: index of the PCR\n"
+"    <key>: secret to protect the access of PCR #<pcr>\n"
+"    <password>: optional password of the PLATFORM hierarchy\n"
 );
diff --git a/include/tpm-v2.h b/include/tpm-v2.h
index 19f0ecd80b..b7820b405d 100644
--- a/include/tpm-v2.h
+++ b/include/tpm-v2.h
@@ -80,11 +80,13 @@ enum tpm2_command_codes {
 	TPM2_CC_CLEAR		= 0x0126,
 	TPM2_CC_CLEARCONTROL	= 0x0127,
 	TPM2_CC_HIERCHANGEAUTH	= 0x0129,
+	TPM2_CC_PCR_SETAUTHPOL	= 0x012C,
 	TPM2_CC_DAM_RESET	= 0x0139,
 	TPM2_CC_DAM_PARAMETERS	= 0x013A,
 	TPM2_CC_GET_CAPABILITY	= 0x017A,
 	TPM2_CC_PCR_READ	= 0x017E,
 	TPM2_CC_PCR_EXTEND	= 0x0182,
+	TPM2_CC_PCR_SETAUTHVAL	= 0x0183,
 };
 
 /**
@@ -230,4 +232,31 @@ u32 tpm2_dam_parameters(const char *pw, const ssize_t pw_sz,
 int tpm2_change_auth(u32 handle, const char *newpw, const ssize_t newpw_sz,
 		     const char *oldpw, const ssize_t oldpw_sz);
 
+/**
+ * Issue a TPM_PCR_SetAuthPolicy command.
+ *
+ * @pw		Platform password
+ * @pw_sz	Length of the password
+ * @index	Index of the PCR
+ * @digest	New key to access the PCR
+ *
+ * @return code of the operation
+ */
+u32 tpm2_pcr_setauthpolicy(const char *pw, const ssize_t pw_sz, u32 index,
+			   const char *key);
+
+/**
+ * Issue a TPM_PCR_SetAuthValue command.
+ *
+ * @pw		Platform password
+ * @pw_sz	Length of the password
+ * @index	Index of the PCR
+ * @digest	New key to access the PCR
+ * @key_sz	Length of the new key
+ *
+ * @return code of the operation
+ */
+u32 tpm2_pcr_setauthvalue(const char *pw, const ssize_t pw_sz, u32 index,
+			  const char *key, const ssize_t key_sz);
+
 #endif /* __TPM_V2_H */
diff --git a/lib/tpm-v2.c b/lib/tpm-v2.c
index 219f0ee941..2c2f4529b4 100644
--- a/lib/tpm-v2.c
+++ b/lib/tpm-v2.c
@@ -317,3 +317,103 @@ int tpm2_change_auth(u32 handle, const char *newpw, const ssize_t newpw_sz,
 
 	return tpm_sendrecv_command(command_v2, NULL, NULL);
 }
+
+u32 tpm2_pcr_setauthpolicy(const char *pw, const ssize_t pw_sz, u32 index,
+			   const char *key)
+{
+	u8 command_v2[COMMAND_BUFFER_SIZE] = {
+		tpm_u16(TPM2_ST_SESSIONS),	/* TAG */
+		tpm_u32(35 + pw_sz + TPM2_DIGEST_LEN), /* Length */
+		tpm_u32(TPM2_CC_PCR_SETAUTHPOL), /* Command code */
+
+		/* HANDLE */
+		tpm_u32(TPM2_RH_PLATFORM),	/* TPM resource handle */
+
+		/* AUTH_SESSION */
+		tpm_u32(9 + pw_sz),		/* Authorization size */
+		tpm_u32(TPM2_RS_PW),		/* session handle */
+		tpm_u16(0),			/* Size of <nonce> */
+						/* <nonce> (if any) */
+		0,				/* Attributes: Cont/Excl/Rst */
+		tpm_u16(pw_sz)			/* Size of <hmac/password> */
+		/* STRING(pw)			   <hmac/password> (if any) */
+
+		/* TPM2B_AUTH (TPM2B_DIGEST) */
+		/* tpm_u16(TPM2_DIGEST_LEN)	   Digest size length */
+		/* STRING(key)			   Digest buffer (PCR key) */
+
+		/* TPMI_ALG_HASH */
+		/* tpm_u16(TPM2_ALG_SHA256)   Algorithm of the hash */
+
+		/* TPMI_DH_PCR */
+		/* tpm_u32(index),		   PCR Index */
+	};
+	unsigned int offset = 27;
+	int ret;
+
+	/*
+	 * Fill the command structure starting from the first buffer:
+	 *     - the password (if any)
+	 *     - the PCR key length
+	 *     - the PCR key
+	 *     - the hash algorithm
+	 *     - the PCR index
+	 */
+	ret = pack_byte_string(command_v2, sizeof(command_v2), "swswd",
+			       offset, pw, pw_sz,
+			       offset + pw_sz, TPM2_DIGEST_LEN,
+			       offset + pw_sz + 2, key, TPM2_DIGEST_LEN,
+			       offset + pw_sz + 2 + TPM2_DIGEST_LEN,
+			       TPM2_ALG_SHA256,
+			       offset + pw_sz + 4 + TPM2_DIGEST_LEN, index);
+	offset += pw_sz + 2 + TPM2_DIGEST_LEN + 2 + 4;
+	if (ret)
+		return TPM_LIB_ERROR;
+
+	return tpm_sendrecv_command(command_v2, NULL, NULL);
+}
+
+u32 tpm2_pcr_setauthvalue(const char *pw, const ssize_t pw_sz, u32 index,
+			  const char *key, const ssize_t key_sz)
+{
+	u8 command_v2[COMMAND_BUFFER_SIZE] = {
+		tpm_u16(TPM2_ST_SESSIONS),	/* TAG */
+		tpm_u32(33 + pw_sz + TPM2_DIGEST_LEN), /* Length */
+		tpm_u32(TPM2_CC_PCR_SETAUTHVAL), /* Command code */
+
+		/* HANDLE */
+		tpm_u32(index),			/* Handle (PCR Index) */
+
+		/* AUTH_SESSION */
+		tpm_u32(9 + pw_sz),		/* Authorization size */
+		tpm_u32(TPM2_RS_PW),		/* session handle */
+		tpm_u16(0),			/* Size of <nonce> */
+						/* <nonce> (if any) */
+		0,				/* Attributes: Cont/Excl/Rst */
+		tpm_u16(pw_sz),			/* Size of <hmac/password> */
+		/* STRING(pw)			   <hmac/password> (if any) */
+
+		/* TPM2B_DIGEST */
+		/* tpm_u16(key_sz)		   Key length */
+		/* STRING(key)			   Key */
+	};
+	unsigned int offset = 27;
+	int ret;
+
+	/*
+	 * Fill the command structure starting from the first buffer:
+	 *     - the password (if any)
+	 *     - the number of digests, 1 in our case
+	 *     - the algorithm, sha256 in our case
+	 *     - the digest (64 bytes)
+	 */
+	ret = pack_byte_string(command_v2, sizeof(command_v2), "sws",
+			       offset, pw, pw_sz,
+			       offset + pw_sz, key_sz,
+			       offset + pw_sz + 2, key, key_sz);
+	offset += pw_sz + 2 + key_sz;
+	if (ret)
+		return TPM_LIB_ERROR;
+
+	return tpm_sendrecv_command(command_v2, NULL, NULL);
+}
-- 
2.14.1

  parent reply	other threads:[~2018-05-15  9:57 UTC|newest]

Thread overview: 112+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-15  9:56 [U-Boot] [PATCH v4 00/32] Introduce TPMv2.0 support Miquel Raynal
2018-05-15  9:56 ` [U-Boot] [PATCH v4 01/32] tpm: remove redundant blank line Miquel Raynal
2018-05-15 15:58   ` Tom Rini
2018-05-15 16:02   ` Simon Glass
2018-05-26 15:53   ` [U-Boot] [U-Boot,v4,01/32] " Tom Rini
2018-05-15  9:56 ` [U-Boot] [PATCH v4 02/32] tpm: remove extra spaces between a function and its opening bracket Miquel Raynal
2018-05-15 15:58   ` Tom Rini
2018-05-15 16:02   ` Simon Glass
2018-05-26 15:53   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:56 ` [U-Boot] [PATCH v4 03/32] tpm: substitute deprecated uint<x>_t types with their u<x> equivalent Miquel Raynal
2018-05-15 15:58   ` Tom Rini
2018-05-15 16:04   ` Simon Glass
2018-05-26 15:54   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 04/32] tpm: align arguments with open parenthesis Miquel Raynal
2018-05-15 15:58   ` Tom Rini
2018-05-15 16:02   ` Simon Glass
2018-05-26 15:54   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 05/32] tpm: use the BIT() macro where applicable Miquel Raynal
2018-05-15 15:58   ` Tom Rini
2018-05-15 16:03   ` Simon Glass
2018-05-26 15:54   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 06/32] tpm: fix spelling Miquel Raynal
2018-05-15 15:58   ` Tom Rini
2018-05-15 16:03   ` Simon Glass
2018-05-26 15:54   ` [U-Boot] [U-Boot,v4,06/32] " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 07/32] tpm: add extra blank lines between declarations and code Miquel Raynal
2018-05-15 15:58   ` Tom Rini
2018-05-15 16:04   ` Simon Glass
2018-05-26 15:54   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 08/32] tpm: add Revision ID field in the chip structure Miquel Raynal
2018-05-15 15:58   ` Tom Rini
2018-05-26 15:54   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 09/32] tpm: prepare introduction of TPMv2.x support in Kconfig Miquel Raynal
2018-05-15 15:58   ` Tom Rini
2018-05-26 15:54   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-27 22:22     ` Miquel Raynal
2018-05-15  9:57 ` [U-Boot] [PATCH v4 10/32] tpm: disociate TPMv1.x specific and generic code Miquel Raynal
2018-05-15 15:58   ` Tom Rini
2018-05-26 15:54   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 11/32] tpm: add missing parameter in private data structure description Miquel Raynal
2018-05-15 15:58   ` Tom Rini
2018-05-15 16:04   ` Simon Glass
2018-05-26 15:54   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 12/32] tpm: prepare support for TPMv2.x commands Miquel Raynal
2018-05-15 15:58   ` Tom Rini
2018-05-26 15:54   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 13/32] tpm: add macros to enhance TPM commands readability Miquel Raynal
2018-05-15 15:59   ` Tom Rini
2018-05-26 15:54   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 14/32] tpm: add possible traces to analyze buffers returned by the TPM Miquel Raynal
2018-05-15 15:59   ` Tom Rini
2018-05-26 15:54   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 15/32] tpm: report driver error code to upper layer Miquel Raynal
2018-05-15 16:00   ` Tom Rini
2018-05-26 15:54   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 16/32] tpm: add TPM2_Startup command support Miquel Raynal
2018-05-15 16:00   ` Tom Rini
2018-05-26 15:55   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 17/32] tpm: add TPM2_SelfTest " Miquel Raynal
2018-05-15 16:00   ` Tom Rini
2018-05-26 15:55   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 18/32] tpm: add TPM2_Clear " Miquel Raynal
2018-05-15 16:00   ` Tom Rini
2018-05-26 15:55   ` [U-Boot] [U-Boot,v4,18/32] " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 19/32] tpm: add TPM2_PCR_Extend " Miquel Raynal
2018-05-15 16:00   ` Tom Rini
2018-05-26 15:55   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 20/32] tpm: add TPM2_PCR_Read " Miquel Raynal
2018-05-15 16:00   ` Tom Rini
2018-05-26 15:55   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 21/32] tpm: add TPM2_GetCapability " Miquel Raynal
2018-05-15 16:00   ` Tom Rini
2018-05-26 15:55   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 22/32] tpm: add dictionary attack mitigation commands support Miquel Raynal
2018-05-15 16:00   ` Tom Rini
2018-05-26 15:55   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 23/32] tpm: add TPM2_HierarchyChangeAuth command support Miquel Raynal
2018-05-15 16:00   ` Tom Rini
2018-05-26 15:55   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` Miquel Raynal [this message]
2018-05-15 16:00   ` [U-Boot] [PATCH v4 24/32] tpm: add PCR authentication commands support Tom Rini
2018-05-26 15:55   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 25/32] tpm: add support for TPMv2.x SPI modules Miquel Raynal
2018-05-15 16:00   ` Tom Rini
2018-05-15 16:04   ` Simon Glass
2018-05-15 16:20     ` Miquel Raynal
2018-05-15 16:26       ` Simon Glass
2018-05-26 15:55   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 26/32] tpm: add the possibility to reset the chip with a gpio Miquel Raynal
2018-05-15 16:00   ` Tom Rini
2018-05-15 16:05   ` Simon Glass
2018-05-15 16:32     ` Miquel Raynal
2018-05-15 17:01       ` Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 27/32] doc: device-tree-bindings: add TIS TPMv2.0 SPI module info Miquel Raynal
2018-05-15 16:00   ` Tom Rini
2018-05-26 15:55   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 28/32] test/py: add TPMv2.x test suite Miquel Raynal
2018-05-15 16:00   ` Tom Rini
2018-05-26 15:55   ` [U-Boot] [U-Boot,v4,28/32] " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 29/32] tpm: add a Sandbox TPMv2.x driver Miquel Raynal
2018-05-15 16:00   ` Tom Rini
2018-05-26 15:55   ` [U-Boot] [U-Boot,v4,29/32] " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 30/32] doc: device-tree-bindings: add Sandbox TPMv2.0 module info Miquel Raynal
2018-05-15 16:00   ` Tom Rini
2018-05-26 15:56   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 31/32] sandbox: dts: add Sandbox TPMv2.x node Miquel Raynal
2018-05-15 16:01   ` Tom Rini
2018-05-26 15:56   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15  9:57 ` [U-Boot] [PATCH v4 32/32] configs: add TPMv2.x support in Sandbox Miquel Raynal
2018-05-15 16:01   ` Tom Rini
2018-05-26 15:56   ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-05-15 16:01 ` [U-Boot] [PATCH v4 00/32] Introduce TPMv2.0 support Tom Rini

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=20180515095728.16572-25-miquel.raynal@bootlin.com \
    --to=miquel.raynal@bootlin.com \
    --cc=u-boot@lists.denx.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.