1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2018 Bootlin
4 * Author: Miquel Raynal <miquel.raynal@bootlin.com>
9 #include <tpm-common.h>
11 #include "tpm-utils.h"
13 u32 tpm2_startup(enum tpm2_startup_types mode)
15 const u8 command_v2[12] = {
16 tpm_u16(TPM2_ST_NO_SESSIONS),
18 tpm_u32(TPM2_CC_STARTUP),
24 * Note TPM2_Startup command will return RC_SUCCESS the first time,
25 * but will return RC_INITIALIZE otherwise.
27 ret = tpm_sendrecv_command(command_v2, NULL, NULL);
28 if (ret && ret != TPM2_RC_INITIALIZE)
34 u32 tpm2_self_test(enum tpm2_yes_no full_test)
36 const u8 command_v2[12] = {
37 tpm_u16(TPM2_ST_NO_SESSIONS),
39 tpm_u32(TPM2_CC_SELF_TEST),
43 return tpm_sendrecv_command(command_v2, NULL, NULL);
46 u32 tpm2_clear(u32 handle, const char *pw, const ssize_t pw_sz)
48 u8 command_v2[COMMAND_BUFFER_SIZE] = {
49 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
50 tpm_u32(27 + pw_sz), /* Length */
51 tpm_u32(TPM2_CC_CLEAR), /* Command code */
54 tpm_u32(handle), /* TPM resource handle */
57 tpm_u32(9 + pw_sz), /* Authorization size */
58 tpm_u32(TPM2_RS_PW), /* Session handle */
59 tpm_u16(0), /* Size of <nonce> */
60 /* <nonce> (if any) */
61 0, /* Attributes: Cont/Excl/Rst */
62 tpm_u16(pw_sz), /* Size of <hmac/password> */
63 /* STRING(pw) <hmac/password> (if any) */
65 unsigned int offset = 27;
69 * Fill the command structure starting from the first buffer:
70 * - the password (if any)
72 ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
78 return tpm_sendrecv_command(command_v2, NULL, NULL);
81 u32 tpm2_pcr_extend(u32 index, const uint8_t *digest)
83 u8 command_v2[COMMAND_BUFFER_SIZE] = {
84 tpm_u16(TPM2_ST_SESSIONS), /* TAG */
85 tpm_u32(33 + TPM2_DIGEST_LEN), /* Length */
86 tpm_u32(TPM2_CC_PCR_EXTEND), /* Command code */
89 tpm_u32(index), /* Handle (PCR Index) */
92 tpm_u32(9), /* Authorization size */
93 tpm_u32(TPM2_RS_PW), /* Session handle */
94 tpm_u16(0), /* Size of <nonce> */
95 /* <nonce> (if any) */
96 0, /* Attributes: Cont/Excl/Rst */
97 tpm_u16(0), /* Size of <hmac/password> */
98 /* <hmac/password> (if any) */
99 tpm_u32(1), /* Count (number of hashes) */
100 tpm_u16(TPM2_ALG_SHA256), /* Algorithm of the hash */
101 /* STRING(digest) Digest */
103 unsigned int offset = 33;
107 * Fill the command structure starting from the first buffer:
110 ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
111 offset, digest, TPM2_DIGEST_LEN);
112 offset += TPM2_DIGEST_LEN;
114 return TPM_LIB_ERROR;
116 return tpm_sendrecv_command(command_v2, NULL, NULL);
119 u32 tpm2_pcr_read(u32 idx, unsigned int idx_min_sz, void *data,
120 unsigned int *updates)
122 u8 idx_array_sz = max(idx_min_sz, DIV_ROUND_UP(idx, 8));
123 u8 command_v2[COMMAND_BUFFER_SIZE] = {
124 tpm_u16(TPM2_ST_NO_SESSIONS), /* TAG */
125 tpm_u32(17 + idx_array_sz), /* Length */
126 tpm_u32(TPM2_CC_PCR_READ), /* Command code */
128 /* TPML_PCR_SELECTION */
129 tpm_u32(1), /* Number of selections */
130 tpm_u16(TPM2_ALG_SHA256), /* Algorithm of the hash */
131 idx_array_sz, /* Array size for selection */
132 /* bitmap(idx) Selected PCR bitmap */
134 size_t response_len = COMMAND_BUFFER_SIZE;
135 u8 response[COMMAND_BUFFER_SIZE];
136 unsigned int pcr_sel_idx = idx / 8;
137 u8 pcr_sel_bit = BIT(idx % 8);
138 unsigned int counter = 0;
141 if (pack_byte_string(command_v2, COMMAND_BUFFER_SIZE, "b",
142 17 + pcr_sel_idx, pcr_sel_bit))
143 return TPM_LIB_ERROR;
145 ret = tpm_sendrecv_command(command_v2, response, &response_len);
149 if (unpack_byte_string(response, response_len, "ds",
151 response_len - TPM2_DIGEST_LEN, data,
153 return TPM_LIB_ERROR;