* [PATCH] tpm: add TPM2_GetRandom command support
@ 2020-06-04 23:43 Dhananjay Phadke
2020-06-07 13:45 ` Simon Glass
2020-07-09 0:23 ` Tom Rini
0 siblings, 2 replies; 3+ messages in thread
From: Dhananjay Phadke @ 2020-06-04 23:43 UTC (permalink / raw)
To: u-boot
Add support for TPM2 GetRandom command
Signed-off-by: Dhananjay Phadke <dphadke@linux.microsoft.com>
---
include/tpm-v2.h | 13 +++++++++++++
lib/tpm-v2.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 57 insertions(+)
diff --git a/include/tpm-v2.h b/include/tpm-v2.h
index ae00803f6d..513697e9a1 100644
--- a/include/tpm-v2.h
+++ b/include/tpm-v2.h
@@ -70,6 +70,7 @@ enum tpm2_handles {
* @TPM2_CC_DAM_RESET: TPM2_DictionaryAttackLockReset().
* @TPM2_CC_DAM_PARAMETERS: TPM2_DictionaryAttackParameters().
* @TPM2_CC_GET_CAPABILITY: TPM2_GetCapibility().
+ * @TPM2_CC_GET_RANDOM: TPM2_GetRandom().
* @TPM2_CC_PCR_READ: TPM2_PCR_Read().
* @TPM2_CC_PCR_EXTEND: TPM2_PCR_Extend().
* @TPM2_CC_PCR_SETAUTHVAL: TPM2_PCR_SetAuthValue().
@@ -85,6 +86,7 @@ enum tpm2_command_codes {
TPM2_CC_DAM_PARAMETERS = 0x013A,
TPM2_CC_NV_READ = 0x014E,
TPM2_CC_GET_CAPABILITY = 0x017A,
+ TPM2_CC_GET_RANDOM = 0x017B,
TPM2_CC_PCR_READ = 0x017E,
TPM2_CC_PCR_EXTEND = 0x0182,
TPM2_CC_PCR_SETAUTHVAL = 0x0183,
@@ -308,4 +310,15 @@ u32 tpm2_pcr_setauthvalue(struct udevice *dev, const char *pw,
const ssize_t pw_sz, u32 index, const char *key,
const ssize_t key_sz);
+/**
+ * Issue a TPM2_GetRandom command.
+ *
+ * @dev TPM device
+ * @param data output buffer for the random bytes
+ * @param count size of output buffer
+ *
+ * @return return code of the operation
+ */
+u32 tpm2_get_random(struct udevice *dev, void *data, u32 count);
+
#endif /* __TPM_V2_H */
diff --git a/lib/tpm-v2.c b/lib/tpm-v2.c
index f89592d6e2..9d078877bc 100644
--- a/lib/tpm-v2.c
+++ b/lib/tpm-v2.c
@@ -421,3 +421,47 @@ u32 tpm2_pcr_setauthvalue(struct udevice *dev, const char *pw,
return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
}
+
+u32 tpm2_get_random(struct udevice *dev, void *data, u32 count)
+{
+ const u8 command_v2[10] = {
+ tpm_u16(TPM2_ST_NO_SESSIONS),
+ tpm_u32(12),
+ tpm_u32(TPM2_CC_GET_RANDOM),
+ };
+ u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE];
+
+ const size_t data_size_offset = 10;
+ const size_t data_offset = 12;
+ size_t response_length = sizeof(response);
+ u32 data_size;
+ u8 *out = data;
+
+ while (count > 0) {
+ u32 this_bytes = min((size_t)count,
+ sizeof(response) - data_offset);
+ u32 err;
+
+ if (pack_byte_string(buf, sizeof(buf), "sw",
+ 0, command_v2, sizeof(command_v2),
+ sizeof(command_v2), this_bytes))
+ return TPM_LIB_ERROR;
+ err = tpm_sendrecv_command(dev, buf, response,
+ &response_length);
+ if (err)
+ return err;
+ if (unpack_byte_string(response, response_length, "w",
+ data_size_offset, &data_size))
+ return TPM_LIB_ERROR;
+ if (data_size > this_bytes)
+ return TPM_LIB_ERROR;
+ if (unpack_byte_string(response, response_length, "s",
+ data_offset, out, data_size))
+ return TPM_LIB_ERROR;
+
+ count -= data_size;
+ out += data_size;
+ }
+
+ return 0;
+}
--
2.17.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH] tpm: add TPM2_GetRandom command support
2020-06-04 23:43 [PATCH] tpm: add TPM2_GetRandom command support Dhananjay Phadke
@ 2020-06-07 13:45 ` Simon Glass
2020-07-09 0:23 ` Tom Rini
1 sibling, 0 replies; 3+ messages in thread
From: Simon Glass @ 2020-06-07 13:45 UTC (permalink / raw)
To: u-boot
On Thu, 4 Jun 2020 at 17:44, Dhananjay Phadke
<dphadke@linux.microsoft.com> wrote:
>
> Add support for TPM2 GetRandom command
>
> Signed-off-by: Dhananjay Phadke <dphadke@linux.microsoft.com>
> ---
> include/tpm-v2.h | 13 +++++++++++++
> lib/tpm-v2.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 57 insertions(+)
>
Reviewed-by: Simon Glass <sjg@chromium.org>
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH] tpm: add TPM2_GetRandom command support
2020-06-04 23:43 [PATCH] tpm: add TPM2_GetRandom command support Dhananjay Phadke
2020-06-07 13:45 ` Simon Glass
@ 2020-07-09 0:23 ` Tom Rini
1 sibling, 0 replies; 3+ messages in thread
From: Tom Rini @ 2020-07-09 0:23 UTC (permalink / raw)
To: u-boot
On Thu, Jun 04, 2020 at 04:43:59PM -0700, Dhananjay Phadke wrote:
> Add support for TPM2 GetRandom command
>
> Signed-off-by: Dhananjay Phadke <dphadke@linux.microsoft.com>
> Reviewed-by: Simon Glass <sjg@chromium.org>
Applied to u-boot/master, thanks!
--
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200708/bc5dd76c/attachment.sig>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2020-07-09 0:23 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-04 23:43 [PATCH] tpm: add TPM2_GetRandom command support Dhananjay Phadke
2020-06-07 13:45 ` Simon Glass
2020-07-09 0:23 ` Tom Rini
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.