1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (c) 2013 The Chromium OS Authors.
4 * Coypright (c) 2013 Guntermann & Drunck GmbH
10 #include <tpm-common.h>
12 /* Useful constants */
14 TPM_REQUEST_HEADER_LENGTH = 10,
15 TPM_RESPONSE_HEADER_LENGTH = 10,
16 PCR_DIGEST_LENGTH = 20,
18 TPM_REQUEST_AUTH_LENGTH = 45,
19 TPM_RESPONSE_AUTH_LENGTH = 41,
20 /* some max lengths, valid for RSA keys <= 2048 bits */
21 TPM_KEY12_MAX_LENGTH = 618,
22 TPM_PUBKEY_MAX_LENGTH = 288,
25 enum tpm_startup_type {
26 TPM_ST_CLEAR = 0x0001,
27 TPM_ST_STATE = 0x0002,
28 TPM_ST_DEACTIVATED = 0x0003,
31 enum tpm_physical_presence {
32 TPM_PHYSICAL_PRESENCE_HW_DISABLE = 0x0200,
33 TPM_PHYSICAL_PRESENCE_CMD_DISABLE = 0x0100,
34 TPM_PHYSICAL_PRESENCE_LIFETIME_LOCK = 0x0080,
35 TPM_PHYSICAL_PRESENCE_HW_ENABLE = 0x0040,
36 TPM_PHYSICAL_PRESENCE_CMD_ENABLE = 0x0020,
37 TPM_PHYSICAL_PRESENCE_NOTPRESENT = 0x0010,
38 TPM_PHYSICAL_PRESENCE_PRESENT = 0x0008,
39 TPM_PHYSICAL_PRESENCE_LOCK = 0x0004,
43 TPM_NV_INDEX_LOCK = 0xffffffff,
44 TPM_NV_INDEX_0 = 0x00000000,
45 TPM_NV_INDEX_DIR = 0x10000001,
48 enum tpm_resource_type {
49 TPM_RT_KEY = 0x00000001,
50 TPM_RT_AUTH = 0x00000002,
51 TPM_RT_HASH = 0x00000003,
52 TPM_RT_TRANS = 0x00000004,
53 TPM_RT_CONTEXT = 0x00000005,
54 TPM_RT_COUNTER = 0x00000006,
55 TPM_RT_DELEGATE = 0x00000007,
56 TPM_RT_DAA_TPM = 0x00000008,
57 TPM_RT_DAA_V0 = 0x00000009,
58 TPM_RT_DAA_V1 = 0x0000000A,
61 enum tpm_capability_areas {
62 TPM_CAP_ORD = 0x00000001,
63 TPM_CAP_ALG = 0x00000002,
64 TPM_CAP_PID = 0x00000003,
65 TPM_CAP_FLAG = 0x00000004,
66 TPM_CAP_PROPERTY = 0x00000005,
67 TPM_CAP_VERSION = 0x00000006,
68 TPM_CAP_KEY_HANDLE = 0x00000007,
69 TPM_CAP_CHECK_LOADED = 0x00000008,
70 TPM_CAP_SYM_MODE = 0x00000009,
71 TPM_CAP_KEY_STATUS = 0x0000000C,
72 TPM_CAP_NV_LIST = 0x0000000D,
73 TPM_CAP_MFR = 0x00000010,
74 TPM_CAP_NV_INDEX = 0x00000011,
75 TPM_CAP_TRANS_ALG = 0x00000012,
76 TPM_CAP_HANDLE = 0x00000014,
77 TPM_CAP_TRANS_ES = 0x00000015,
78 TPM_CAP_AUTH_ENCRYPT = 0x00000017,
79 TPM_CAP_SELECT_SIZE = 0x00000018,
80 TPM_CAP_DA_LOGIC = 0x00000019,
81 TPM_CAP_VERSION_VAL = 0x0000001A,
84 #define TPM_NV_PER_GLOBALLOCK BIT(15)
85 #define TPM_NV_PER_PPREAD BIT(16)
86 #define TPM_NV_PER_PPWRITE BIT(0)
87 #define TPM_NV_PER_READ_STCLEAR BIT(31)
88 #define TPM_NV_PER_WRITE_STCLEAR BIT(14)
89 #define TPM_NV_PER_WRITEDEFINE BIT(13)
90 #define TPM_NV_PER_WRITEALL BIT(12)
97 * TPM return codes as defined in the TCG Main specification
98 * (TPM Main Part 2 Structures; Specification version 1.2)
100 enum tpm_return_code {
101 TPM_BASE = 0x00000000,
102 TPM_NON_FATAL = 0x00000800,
103 TPM_SUCCESS = TPM_BASE,
104 /* TPM-defined fatal error codes */
105 TPM_AUTHFAIL = TPM_BASE + 1,
106 TPM_BADINDEX = TPM_BASE + 2,
107 TPM_BAD_PARAMETER = TPM_BASE + 3,
108 TPM_AUDITFAILURE = TPM_BASE + 4,
109 TPM_CLEAR_DISABLED = TPM_BASE + 5,
110 TPM_DEACTIVATED = TPM_BASE + 6,
111 TPM_DISABLED = TPM_BASE + 7,
112 TPM_DISABLED_CMD = TPM_BASE + 8,
113 TPM_FAIL = TPM_BASE + 9,
114 TPM_BAD_ORDINAL = TPM_BASE + 10,
115 TPM_INSTALL_DISABLED = TPM_BASE + 11,
116 TPM_INVALID_KEYHANDLE = TPM_BASE + 12,
117 TPM_KEYNOTFOUND = TPM_BASE + 13,
118 TPM_INAPPROPRIATE_ENC = TPM_BASE + 14,
119 TPM_MIGRATE_FAIL = TPM_BASE + 15,
120 TPM_INVALID_PCR_INFO = TPM_BASE + 16,
121 TPM_NOSPACE = TPM_BASE + 17,
122 TPM_NOSRK = TPM_BASE + 18,
123 TPM_NOTSEALED_BLOB = TPM_BASE + 19,
124 TPM_OWNER_SET = TPM_BASE + 20,
125 TPM_RESOURCES = TPM_BASE + 21,
126 TPM_SHORTRANDOM = TPM_BASE + 22,
127 TPM_SIZE = TPM_BASE + 23,
128 TPM_WRONGPCRVAL = TPM_BASE + 24,
129 TPM_BAD_PARAM_SIZE = TPM_BASE + 25,
130 TPM_SHA_THREAD = TPM_BASE + 26,
131 TPM_SHA_ERROR = TPM_BASE + 27,
132 TPM_FAILEDSELFTEST = TPM_BASE + 28,
133 TPM_AUTH2FAIL = TPM_BASE + 29,
134 TPM_BADTAG = TPM_BASE + 30,
135 TPM_IOERROR = TPM_BASE + 31,
136 TPM_ENCRYPT_ERROR = TPM_BASE + 32,
137 TPM_DECRYPT_ERROR = TPM_BASE + 33,
138 TPM_INVALID_AUTHHANDLE = TPM_BASE + 34,
139 TPM_NO_ENDORSEMENT = TPM_BASE + 35,
140 TPM_INVALID_KEYUSAGE = TPM_BASE + 36,
141 TPM_WRONG_ENTITYTYPE = TPM_BASE + 37,
142 TPM_INVALID_POSTINIT = TPM_BASE + 38,
143 TPM_INAPPROPRIATE_SIG = TPM_BASE + 39,
144 TPM_BAD_KEY_PROPERTY = TPM_BASE + 40,
145 TPM_BAD_MIGRATION = TPM_BASE + 41,
146 TPM_BAD_SCHEME = TPM_BASE + 42,
147 TPM_BAD_DATASIZE = TPM_BASE + 43,
148 TPM_BAD_MODE = TPM_BASE + 44,
149 TPM_BAD_PRESENCE = TPM_BASE + 45,
150 TPM_BAD_VERSION = TPM_BASE + 46,
151 TPM_NO_WRAP_TRANSPORT = TPM_BASE + 47,
152 TPM_AUDITFAIL_UNSUCCESSFUL = TPM_BASE + 48,
153 TPM_AUDITFAIL_SUCCESSFUL = TPM_BASE + 49,
154 TPM_NOTRESETABLE = TPM_BASE + 50,
155 TPM_NOTLOCAL = TPM_BASE + 51,
156 TPM_BAD_TYPE = TPM_BASE + 52,
157 TPM_INVALID_RESOURCE = TPM_BASE + 53,
158 TPM_NOTFIPS = TPM_BASE + 54,
159 TPM_INVALID_FAMILY = TPM_BASE + 55,
160 TPM_NO_NV_PERMISSION = TPM_BASE + 56,
161 TPM_REQUIRES_SIGN = TPM_BASE + 57,
162 TPM_KEY_NOTSUPPORTED = TPM_BASE + 58,
163 TPM_AUTH_CONFLICT = TPM_BASE + 59,
164 TPM_AREA_LOCKED = TPM_BASE + 60,
165 TPM_BAD_LOCALITY = TPM_BASE + 61,
166 TPM_READ_ONLY = TPM_BASE + 62,
167 TPM_PER_NOWRITE = TPM_BASE + 63,
168 TPM_FAMILY_COUNT = TPM_BASE + 64,
169 TPM_WRITE_LOCKED = TPM_BASE + 65,
170 TPM_BAD_ATTRIBUTES = TPM_BASE + 66,
171 TPM_INVALID_STRUCTURE = TPM_BASE + 67,
172 TPM_KEY_OWNER_CONTROL = TPM_BASE + 68,
173 TPM_BAD_COUNTER = TPM_BASE + 69,
174 TPM_NOT_FULLWRITE = TPM_BASE + 70,
175 TPM_CONTEXT_GAP = TPM_BASE + 71,
176 TPM_MAXNVWRITES = TPM_BASE + 72,
177 TPM_NOOPERATOR = TPM_BASE + 73,
178 TPM_RESOURCEMISSING = TPM_BASE + 74,
179 TPM_DELEGATE_LOCK = TPM_BASE + 75,
180 TPM_DELEGATE_FAMILY = TPM_BASE + 76,
181 TPM_DELEGATE_ADMIN = TPM_BASE + 77,
182 TPM_TRANSPORT_NOTEXCLUSIVE = TPM_BASE + 78,
183 TPM_OWNER_CONTROL = TPM_BASE + 79,
184 TPM_DAA_RESOURCES = TPM_BASE + 80,
185 TPM_DAA_INPUT_DATA0 = TPM_BASE + 81,
186 TPM_DAA_INPUT_DATA1 = TPM_BASE + 82,
187 TPM_DAA_ISSUER_SETTINGS = TPM_BASE + 83,
188 TPM_DAA_TPM_SETTINGS = TPM_BASE + 84,
189 TPM_DAA_STAGE = TPM_BASE + 85,
190 TPM_DAA_ISSUER_VALIDITY = TPM_BASE + 86,
191 TPM_DAA_WRONG_W = TPM_BASE + 87,
192 TPM_BAD_HANDLE = TPM_BASE + 88,
193 TPM_BAD_DELEGATE = TPM_BASE + 89,
194 TPM_BADCONTEXT = TPM_BASE + 90,
195 TPM_TOOMANYCONTEXTS = TPM_BASE + 91,
196 TPM_MA_TICKET_SIGNATURE = TPM_BASE + 92,
197 TPM_MA_DESTINATION = TPM_BASE + 93,
198 TPM_MA_SOURCE = TPM_BASE + 94,
199 TPM_MA_AUTHORITY = TPM_BASE + 95,
200 TPM_PERMANENTEK = TPM_BASE + 97,
201 TPM_BAD_SIGNATURE = TPM_BASE + 98,
202 TPM_NOCONTEXTSPACE = TPM_BASE + 99,
203 /* TPM-defined non-fatal errors */
204 TPM_RETRY = TPM_BASE + TPM_NON_FATAL,
205 TPM_NEEDS_SELFTEST = TPM_BASE + TPM_NON_FATAL + 1,
206 TPM_DOING_SELFTEST = TPM_BASE + TPM_NON_FATAL + 2,
207 TPM_DEFEND_LOCK_RUNNING = TPM_BASE + TPM_NON_FATAL + 3,
210 struct tpm_permanent_flags {
216 u8 disable_owner_clear;
217 u8 allow_maintenance;
218 u8 physical_presence_lifetime_lock;
219 u8 physical_presence_hw_enable;
220 u8 physical_presence_cmd_enable;
231 u8 disable_full_da_logic_info;
235 * Issue a TPM_Startup command.
237 * @param mode TPM startup mode
238 * @return return code of the operation
240 u32 tpm_startup(enum tpm_startup_type mode);
243 * Issue a TPM_SelfTestFull command.
245 * @return return code of the operation
247 u32 tpm_self_test_full(void);
250 * Issue a TPM_ContinueSelfTest command.
252 * @return return code of the operation
254 u32 tpm_continue_self_test(void);
257 * Issue a TPM_NV_DefineSpace command. The implementation is limited
258 * to specify TPM_NV_ATTRIBUTES and size of the area. The area index
259 * could be one of the special value listed in enum tpm_nv_index.
261 * @param index index of the area
262 * @param perm TPM_NV_ATTRIBUTES of the area
263 * @param size size of the area
264 * @return return code of the operation
266 u32 tpm_nv_define_space(u32 index, u32 perm, u32 size);
269 * Issue a TPM_NV_ReadValue command. This implementation is limited
270 * to read the area from offset 0. The area index could be one of
271 * the special value listed in enum tpm_nv_index.
273 * @param index index of the area
274 * @param data output buffer of the area contents
275 * @param count size of output buffer
276 * @return return code of the operation
278 u32 tpm_nv_read_value(u32 index, void *data, u32 count);
281 * Issue a TPM_NV_WriteValue command. This implementation is limited
282 * to write the area from offset 0. The area index could be one of
283 * the special value listed in enum tpm_nv_index.
285 * @param index index of the area
286 * @param data input buffer to be wrote to the area
287 * @param length length of data bytes of input buffer
288 * @return return code of the operation
290 u32 tpm_nv_write_value(u32 index, const void *data, u32 length);
293 * Issue a TPM_Extend command.
295 * @param index index of the PCR
296 * @param in_digest 160-bit value representing the event to be
298 * @param out_digest 160-bit PCR value after execution of the
300 * @return return code of the operation
302 u32 tpm_extend(u32 index, const void *in_digest, void *out_digest);
305 * Issue a TPM_PCRRead command.
307 * @param index index of the PCR
308 * @param data output buffer for contents of the named PCR
309 * @param count size of output buffer
310 * @return return code of the operation
312 u32 tpm_pcr_read(u32 index, void *data, size_t count);
315 * Issue a TSC_PhysicalPresence command. TPM physical presence flag
316 * is bit-wise OR'ed of flags listed in enum tpm_physical_presence.
318 * @param presence TPM physical presence flag
319 * @return return code of the operation
321 u32 tpm_tsc_physical_presence(u16 presence);
324 * Issue a TPM_ReadPubek command.
326 * @param data output buffer for the public endorsement key
327 * @param count size of output buffer
328 * @return return code of the operation
330 u32 tpm_read_pubek(void *data, size_t count);
333 * Issue a TPM_ForceClear command.
335 * @return return code of the operation
337 u32 tpm_force_clear(void);
340 * Issue a TPM_PhysicalEnable command.
342 * @return return code of the operation
344 u32 tpm_physical_enable(void);
347 * Issue a TPM_PhysicalDisable command.
349 * @return return code of the operation
351 u32 tpm_physical_disable(void);
354 * Issue a TPM_PhysicalSetDeactivated command.
356 * @param state boolean state of the deactivated flag
357 * @return return code of the operation
359 u32 tpm_physical_set_deactivated(u8 state);
362 * Issue a TPM_GetCapability command. This implementation is limited
363 * to query sub_cap index that is 4-byte wide.
365 * @param cap_area partition of capabilities
366 * @param sub_cap further definition of capability, which is
367 * limited to be 4-byte wide
368 * @param cap output buffer for capability information
369 * @param count size of output buffer
370 * @return return code of the operation
372 u32 tpm_get_capability(u32 cap_area, u32 sub_cap, void *cap, size_t count);
375 * Issue a TPM_FlushSpecific command for a AUTH resource.
377 * @param auth_handle handle of the auth session
378 * @return return code of the operation
380 u32 tpm_terminate_auth_session(u32 auth_handle);
383 * Issue a TPM_OIAP command to setup an object independent authorization
385 * Information about the session is stored internally.
386 * If there was already an OIAP session active it is terminated and a new
389 * @param auth_handle pointer to the (new) auth handle or NULL.
390 * @return return code of the operation
392 u32 tpm_oiap(u32 *auth_handle);
395 * Ends an active OIAP session.
397 * @return return code of the operation
399 u32 tpm_end_oiap(void);
402 * Issue a TPM_LoadKey2 (Auth1) command using an OIAP session for authenticating
403 * the usage of the parent key.
405 * @param parent_handle handle of the parent key.
406 * @param key pointer to the key structure (TPM_KEY or TPM_KEY12).
407 * @param key_length size of the key structure
408 * @param parent_key_usage_auth usage auth for the parent key
409 * @param key_handle pointer to the key handle
410 * @return return code of the operation
412 u32 tpm_load_key2_oiap(u32 parent_handle, const void *key, size_t key_length,
413 const void *parent_key_usage_auth, u32 *key_handle);
416 * Issue a TPM_GetPubKey (Auth1) command using an OIAP session for
417 * authenticating the usage of the key.
419 * @param key_handle handle of the key
420 * @param usage_auth usage auth for the key
421 * @param pubkey pointer to the pub key buffer; may be NULL if the pubkey
422 * should not be stored.
423 * @param pubkey_len pointer to the pub key buffer len. On entry: the size of
424 * the provided pubkey buffer. On successful exit: the size
425 * of the stored TPM_PUBKEY structure (iff pubkey != NULL).
426 * @return return code of the operation
428 u32 tpm_get_pub_key_oiap(u32 key_handle, const void *usage_auth, void *pubkey,
432 * Get the TPM permanent flags value
434 * @param pflags Place to put permanent flags
435 * @return return code of the operation
437 u32 tpm_get_permanent_flags(struct tpm_permanent_flags *pflags);
440 * Get the TPM permissions
442 * @param perm Returns permissions value
443 * @return return code of the operation
445 u32 tpm_get_permissions(u32 index, u32 *perm);
448 * Flush a resource with a given handle and type from the TPM
450 * @param key_handle handle of the resource
451 * @param resource_type type of the resource
452 * @return return code of the operation
454 u32 tpm_flush_specific(u32 key_handle, u32 resource_type);
456 #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
458 * Search for a key by usage AuthData and the hash of the parent's pub key.
460 * @param auth Usage auth of the key to search for
461 * @param pubkey_digest SHA1 hash of the pub key structure of the key
462 * @param[out] handle The handle of the key (Non-null iff found)
463 * @return 0 if key was found in TPM; != 0 if not.
465 u32 tpm_find_key_sha1(const u8 auth[20], const u8 pubkey_digest[20],
467 #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
470 * Read random bytes from the TPM RNG. The implementation deals with the fact
471 * that the TPM may legally return fewer bytes than requested by retrying
472 * until @p count bytes have been received.
474 * @param data output buffer for the random bytes
475 * @param count size of output buffer
476 * @return return code of the operation
478 u32 tpm_get_random(void *data, u32 count);
480 #endif /* __TPM_V1_H */