blob: 2f2e66de195dacd385649413c1ac319f6d7f2d31 [file] [log] [blame]
Miquel Raynalff322452018-05-15 11:57:08 +02001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Copyright (c) 2018 Bootlin
4 * Author: Miquel Raynal <miquel.raynal@bootlin.com>
5 */
6
7#ifndef __TPM_V2_H
8#define __TPM_V2_H
9
10#include <tpm-common.h>
11
12#define TPM2_DIGEST_LEN 32
13
14/**
15 * TPM2 Structure Tags for command/response buffers.
16 *
17 * @TPM2_ST_NO_SESSIONS: the command does not need an authentication.
18 * @TPM2_ST_SESSIONS: the command needs an authentication.
19 */
20enum tpm2_structures {
21 TPM2_ST_NO_SESSIONS = 0x8001,
22 TPM2_ST_SESSIONS = 0x8002,
23};
24
25/**
26 * TPM2 type of boolean.
27 */
28enum tpm2_yes_no {
29 TPMI_YES = 1,
30 TPMI_NO = 0,
31};
32
33/**
34 * TPM2 startup values.
35 *
36 * @TPM2_SU_CLEAR: reset the internal state.
37 * @TPM2_SU_STATE: restore saved state (if any).
38 */
39enum tpm2_startup_types {
40 TPM2_SU_CLEAR = 0x0000,
41 TPM2_SU_STATE = 0x0001,
42};
43
44/**
45 * TPM2 permanent handles.
46 *
47 * @TPM2_RH_OWNER: refers to the 'owner' hierarchy.
48 * @TPM2_RS_PW: indicates a password.
49 * @TPM2_RH_LOCKOUT: refers to the 'lockout' hierarchy.
50 * @TPM2_RH_ENDORSEMENT: refers to the 'endorsement' hierarchy.
51 * @TPM2_RH_PLATFORM: refers to the 'platform' hierarchy.
52 */
53enum tpm2_handles {
54 TPM2_RH_OWNER = 0x40000001,
55 TPM2_RS_PW = 0x40000009,
56 TPM2_RH_LOCKOUT = 0x4000000A,
57 TPM2_RH_ENDORSEMENT = 0x4000000B,
58 TPM2_RH_PLATFORM = 0x4000000C,
59};
60
61/**
62 * TPM2 command codes used at the beginning of a buffer, gives the command.
63 *
64 * @TPM2_CC_STARTUP: TPM2_Startup().
65 * @TPM2_CC_SELF_TEST: TPM2_SelfTest().
66 * @TPM2_CC_CLEAR: TPM2_Clear().
67 * @TPM2_CC_CLEARCONTROL: TPM2_ClearControl().
68 * @TPM2_CC_HIERCHANGEAUTH: TPM2_HierarchyChangeAuth().
69 * @TPM2_CC_PCR_SETAUTHPOL: TPM2_PCR_SetAuthPolicy().
70 * @TPM2_CC_DAM_RESET: TPM2_DictionaryAttackLockReset().
71 * @TPM2_CC_DAM_PARAMETERS: TPM2_DictionaryAttackParameters().
72 * @TPM2_CC_GET_CAPABILITY: TPM2_GetCapibility().
73 * @TPM2_CC_PCR_READ: TPM2_PCR_Read().
74 * @TPM2_CC_PCR_EXTEND: TPM2_PCR_Extend().
75 * @TPM2_CC_PCR_SETAUTHVAL: TPM2_PCR_SetAuthValue().
76 */
77enum tpm2_command_codes {
78 TPM2_CC_STARTUP = 0x0144,
79 TPM2_CC_SELF_TEST = 0x0143,
80 TPM2_CC_CLEAR = 0x0126,
81 TPM2_CC_CLEARCONTROL = 0x0127,
82 TPM2_CC_HIERCHANGEAUTH = 0x0129,
Miquel Raynalb9dd4fab2018-05-15 11:57:20 +020083 TPM2_CC_PCR_SETAUTHPOL = 0x012C,
Miquel Raynalff322452018-05-15 11:57:08 +020084 TPM2_CC_DAM_RESET = 0x0139,
85 TPM2_CC_DAM_PARAMETERS = 0x013A,
Simon Glass998af312018-10-01 11:55:17 -060086 TPM2_CC_NV_READ = 0x014E,
Miquel Raynalff322452018-05-15 11:57:08 +020087 TPM2_CC_GET_CAPABILITY = 0x017A,
88 TPM2_CC_PCR_READ = 0x017E,
89 TPM2_CC_PCR_EXTEND = 0x0182,
Miquel Raynalb9dd4fab2018-05-15 11:57:20 +020090 TPM2_CC_PCR_SETAUTHVAL = 0x0183,
Miquel Raynalff322452018-05-15 11:57:08 +020091};
92
93/**
94 * TPM2 return codes.
95 */
96enum tpm2_return_codes {
97 TPM2_RC_SUCCESS = 0x0000,
98 TPM2_RC_BAD_TAG = 0x001E,
99 TPM2_RC_FMT1 = 0x0080,
100 TPM2_RC_HASH = TPM2_RC_FMT1 + 0x0003,
101 TPM2_RC_VALUE = TPM2_RC_FMT1 + 0x0004,
102 TPM2_RC_SIZE = TPM2_RC_FMT1 + 0x0015,
103 TPM2_RC_BAD_AUTH = TPM2_RC_FMT1 + 0x0022,
104 TPM2_RC_HANDLE = TPM2_RC_FMT1 + 0x000B,
105 TPM2_RC_VER1 = 0x0100,
106 TPM2_RC_INITIALIZE = TPM2_RC_VER1 + 0x0000,
107 TPM2_RC_FAILURE = TPM2_RC_VER1 + 0x0001,
108 TPM2_RC_DISABLED = TPM2_RC_VER1 + 0x0020,
109 TPM2_RC_AUTH_MISSING = TPM2_RC_VER1 + 0x0025,
110 TPM2_RC_COMMAND_CODE = TPM2_RC_VER1 + 0x0043,
111 TPM2_RC_AUTHSIZE = TPM2_RC_VER1 + 0x0044,
112 TPM2_RC_AUTH_CONTEXT = TPM2_RC_VER1 + 0x0045,
113 TPM2_RC_NEEDS_TEST = TPM2_RC_VER1 + 0x0053,
114 TPM2_RC_WARN = 0x0900,
115 TPM2_RC_TESTING = TPM2_RC_WARN + 0x000A,
116 TPM2_RC_REFERENCE_H0 = TPM2_RC_WARN + 0x0010,
117 TPM2_RC_LOCKOUT = TPM2_RC_WARN + 0x0021,
118};
119
120/**
121 * TPM2 algorithms.
122 */
123enum tpm2_algorithms {
124 TPM2_ALG_XOR = 0x0A,
125 TPM2_ALG_SHA256 = 0x0B,
126 TPM2_ALG_SHA384 = 0x0C,
127 TPM2_ALG_SHA512 = 0x0D,
128 TPM2_ALG_NULL = 0x10,
129};
130
Miquel Raynal1922df22018-05-15 11:57:12 +0200131/**
132 * Issue a TPM2_Startup command.
133 *
Simon Glassabdc7b82018-11-18 14:22:27 -0700134 * @dev TPM device
Miquel Raynal1922df22018-05-15 11:57:12 +0200135 * @mode TPM startup mode
136 *
137 * @return code of the operation
138 */
Simon Glassabdc7b82018-11-18 14:22:27 -0700139u32 tpm2_startup(struct udevice *dev, enum tpm2_startup_types mode);
Miquel Raynal1922df22018-05-15 11:57:12 +0200140
Miquel Raynal2dc6d972018-05-15 11:57:13 +0200141/**
142 * Issue a TPM2_SelfTest command.
143 *
Simon Glassabdc7b82018-11-18 14:22:27 -0700144 * @dev TPM device
Miquel Raynal2dc6d972018-05-15 11:57:13 +0200145 * @full_test Asking to perform all tests or only the untested ones
146 *
147 * @return code of the operation
148 */
Simon Glassabdc7b82018-11-18 14:22:27 -0700149u32 tpm2_self_test(struct udevice *dev, enum tpm2_yes_no full_test);
Miquel Raynal2dc6d972018-05-15 11:57:13 +0200150
Miquel Raynalbad8ff52018-05-15 11:57:14 +0200151/**
152 * Issue a TPM2_Clear command.
153 *
Simon Glassabdc7b82018-11-18 14:22:27 -0700154 * @dev TPM device
Miquel Raynalbad8ff52018-05-15 11:57:14 +0200155 * @handle Handle
156 * @pw Password
157 * @pw_sz Length of the password
158 *
159 * @return code of the operation
160 */
Simon Glassabdc7b82018-11-18 14:22:27 -0700161u32 tpm2_clear(struct udevice *dev, u32 handle, const char *pw,
162 const ssize_t pw_sz);
Miquel Raynalbad8ff52018-05-15 11:57:14 +0200163
Miquel Raynal6284be52018-05-15 11:57:15 +0200164/**
165 * Issue a TPM2_PCR_Extend command.
166 *
Simon Glassabdc7b82018-11-18 14:22:27 -0700167 * @dev TPM device
Miquel Raynal6284be52018-05-15 11:57:15 +0200168 * @index Index of the PCR
169 * @digest Value representing the event to be recorded
170 *
171 * @return code of the operation
172 */
Simon Glassabdc7b82018-11-18 14:22:27 -0700173u32 tpm2_pcr_extend(struct udevice *dev, u32 index, const uint8_t *digest);
Miquel Raynal6284be52018-05-15 11:57:15 +0200174
Miquel Raynal1c4ea8f2018-05-15 11:57:16 +0200175/**
176 * Issue a TPM2_PCR_Read command.
177 *
Simon Glassabdc7b82018-11-18 14:22:27 -0700178 * @dev TPM device
Miquel Raynal1c4ea8f2018-05-15 11:57:16 +0200179 * @idx Index of the PCR
180 * @idx_min_sz Minimum size in bytes of the pcrSelect array
181 * @data Output buffer for contents of the named PCR
182 * @updates Optional out parameter: number of updates for this PCR
183 *
184 * @return code of the operation
185 */
Simon Glassabdc7b82018-11-18 14:22:27 -0700186u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz,
187 void *data, unsigned int *updates);
Miquel Raynal1c4ea8f2018-05-15 11:57:16 +0200188
Miquel Raynal69cd8f02018-05-15 11:57:17 +0200189/**
190 * Issue a TPM2_GetCapability command. This implementation is limited
191 * to query property index that is 4-byte wide.
192 *
Simon Glassabdc7b82018-11-18 14:22:27 -0700193 * @dev TPM device
Miquel Raynal69cd8f02018-05-15 11:57:17 +0200194 * @capability Partition of capabilities
195 * @property Further definition of capability, limited to be 4 bytes wide
196 * @buf Output buffer for capability information
197 * @prop_count Size of output buffer
198 *
199 * @return code of the operation
200 */
Simon Glassabdc7b82018-11-18 14:22:27 -0700201u32 tpm2_get_capability(struct udevice *dev, u32 capability, u32 property,
202 void *buf, size_t prop_count);
Miquel Raynal69cd8f02018-05-15 11:57:17 +0200203
Miquel Raynalda9c3392018-05-15 11:57:18 +0200204/**
205 * Issue a TPM2_DictionaryAttackLockReset command.
206 *
Simon Glassabdc7b82018-11-18 14:22:27 -0700207 * @dev TPM device
Miquel Raynalda9c3392018-05-15 11:57:18 +0200208 * @pw Password
209 * @pw_sz Length of the password
210 *
211 * @return code of the operation
212 */
Simon Glassabdc7b82018-11-18 14:22:27 -0700213u32 tpm2_dam_reset(struct udevice *dev, const char *pw, const ssize_t pw_sz);
Miquel Raynalda9c3392018-05-15 11:57:18 +0200214
215/**
216 * Issue a TPM2_DictionaryAttackParameters command.
217 *
Simon Glassabdc7b82018-11-18 14:22:27 -0700218 * @dev TPM device
Miquel Raynalda9c3392018-05-15 11:57:18 +0200219 * @pw Password
220 * @pw_sz Length of the password
221 * @max_tries Count of authorizations before lockout
222 * @recovery_time Time before decrementation of the failure count
223 * @lockout_recovery Time to wait after a lockout
224 *
225 * @return code of the operation
226 */
Simon Glassabdc7b82018-11-18 14:22:27 -0700227u32 tpm2_dam_parameters(struct udevice *dev, const char *pw,
228 const ssize_t pw_sz, unsigned int max_tries,
229 unsigned int recovery_time,
Miquel Raynalda9c3392018-05-15 11:57:18 +0200230 unsigned int lockout_recovery);
231
Miquel Raynaldc26e912018-05-15 11:57:19 +0200232/**
233 * Issue a TPM2_HierarchyChangeAuth command.
234 *
Simon Glassabdc7b82018-11-18 14:22:27 -0700235 * @dev TPM device
Miquel Raynaldc26e912018-05-15 11:57:19 +0200236 * @handle Handle
237 * @newpw New password
238 * @newpw_sz Length of the new password
239 * @oldpw Old password
240 * @oldpw_sz Length of the old password
241 *
242 * @return code of the operation
243 */
Simon Glassabdc7b82018-11-18 14:22:27 -0700244int tpm2_change_auth(struct udevice *dev, u32 handle, const char *newpw,
245 const ssize_t newpw_sz, const char *oldpw,
246 const ssize_t oldpw_sz);
Miquel Raynaldc26e912018-05-15 11:57:19 +0200247
Miquel Raynalb9dd4fab2018-05-15 11:57:20 +0200248/**
249 * Issue a TPM_PCR_SetAuthPolicy command.
250 *
Simon Glassabdc7b82018-11-18 14:22:27 -0700251 * @dev TPM device
Miquel Raynalb9dd4fab2018-05-15 11:57:20 +0200252 * @pw Platform password
253 * @pw_sz Length of the password
254 * @index Index of the PCR
255 * @digest New key to access the PCR
256 *
257 * @return code of the operation
258 */
Simon Glassabdc7b82018-11-18 14:22:27 -0700259u32 tpm2_pcr_setauthpolicy(struct udevice *dev, const char *pw,
260 const ssize_t pw_sz, u32 index, const char *key);
Miquel Raynalb9dd4fab2018-05-15 11:57:20 +0200261
262/**
263 * Issue a TPM_PCR_SetAuthValue command.
264 *
Simon Glassabdc7b82018-11-18 14:22:27 -0700265 * @dev TPM device
Miquel Raynalb9dd4fab2018-05-15 11:57:20 +0200266 * @pw Platform password
267 * @pw_sz Length of the password
268 * @index Index of the PCR
269 * @digest New key to access the PCR
270 * @key_sz Length of the new key
271 *
272 * @return code of the operation
273 */
Simon Glassabdc7b82018-11-18 14:22:27 -0700274u32 tpm2_pcr_setauthvalue(struct udevice *dev, const char *pw,
275 const ssize_t pw_sz, u32 index, const char *key,
276 const ssize_t key_sz);
Miquel Raynalb9dd4fab2018-05-15 11:57:20 +0200277
Miquel Raynalff322452018-05-15 11:57:08 +0200278#endif /* __TPM_V2_H */