tpm-v1.h 14 KB

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