tpm.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2004,2007,2008 IBM Corporation
  4. *
  5. * Authors:
  6. * Leendert van Doorn <[email protected]>
  7. * Dave Safford <[email protected]>
  8. * Reiner Sailer <[email protected]>
  9. * Kylene Hall <[email protected]>
  10. * Debora Velarde <[email protected]>
  11. *
  12. * Maintained by: <[email protected]>
  13. *
  14. * Device driver for TCG/TCPA TPM (trusted platform module).
  15. * Specifications at www.trustedcomputinggroup.org
  16. */
  17. #ifndef __LINUX_TPM_H__
  18. #define __LINUX_TPM_H__
  19. #include <linux/hw_random.h>
  20. #include <linux/acpi.h>
  21. #include <linux/cdev.h>
  22. #include <linux/fs.h>
  23. #include <linux/highmem.h>
  24. #include <crypto/hash_info.h>
  25. #define TPM_DIGEST_SIZE 20 /* Max TPM v1.2 PCR size */
  26. #define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
  27. struct tpm_chip;
  28. struct trusted_key_payload;
  29. struct trusted_key_options;
  30. /* if you add a new hash to this, increment TPM_MAX_HASHES below */
  31. enum tpm_algorithms {
  32. TPM_ALG_ERROR = 0x0000,
  33. TPM_ALG_SHA1 = 0x0004,
  34. TPM_ALG_KEYEDHASH = 0x0008,
  35. TPM_ALG_SHA256 = 0x000B,
  36. TPM_ALG_SHA384 = 0x000C,
  37. TPM_ALG_SHA512 = 0x000D,
  38. TPM_ALG_NULL = 0x0010,
  39. TPM_ALG_SM3_256 = 0x0012,
  40. };
  41. /*
  42. * maximum number of hashing algorithms a TPM can have. This is
  43. * basically a count of every hash in tpm_algorithms above
  44. */
  45. #define TPM_MAX_HASHES 5
  46. struct tpm_digest {
  47. u16 alg_id;
  48. u8 digest[TPM_MAX_DIGEST_SIZE];
  49. } __packed;
  50. struct tpm_bank_info {
  51. u16 alg_id;
  52. u16 digest_size;
  53. u16 crypto_id;
  54. };
  55. enum TPM_OPS_FLAGS {
  56. TPM_OPS_AUTO_STARTUP = BIT(0),
  57. };
  58. struct tpm_class_ops {
  59. unsigned int flags;
  60. const u8 req_complete_mask;
  61. const u8 req_complete_val;
  62. bool (*req_canceled)(struct tpm_chip *chip, u8 status);
  63. int (*recv) (struct tpm_chip *chip, u8 *buf, size_t len);
  64. int (*send) (struct tpm_chip *chip, u8 *buf, size_t len);
  65. void (*cancel) (struct tpm_chip *chip);
  66. u8 (*status) (struct tpm_chip *chip);
  67. void (*update_timeouts)(struct tpm_chip *chip,
  68. unsigned long *timeout_cap);
  69. void (*update_durations)(struct tpm_chip *chip,
  70. unsigned long *duration_cap);
  71. int (*go_idle)(struct tpm_chip *chip);
  72. int (*cmd_ready)(struct tpm_chip *chip);
  73. int (*request_locality)(struct tpm_chip *chip, int loc);
  74. int (*relinquish_locality)(struct tpm_chip *chip, int loc);
  75. void (*clk_enable)(struct tpm_chip *chip, bool value);
  76. };
  77. #define TPM_NUM_EVENT_LOG_FILES 3
  78. /* Indexes the duration array */
  79. enum tpm_duration {
  80. TPM_SHORT = 0,
  81. TPM_MEDIUM = 1,
  82. TPM_LONG = 2,
  83. TPM_LONG_LONG = 3,
  84. TPM_UNDEFINED,
  85. TPM_NUM_DURATIONS = TPM_UNDEFINED,
  86. };
  87. #define TPM_PPI_VERSION_LEN 3
  88. struct tpm_space {
  89. u32 context_tbl[3];
  90. u8 *context_buf;
  91. u32 session_tbl[3];
  92. u8 *session_buf;
  93. u32 buf_size;
  94. };
  95. struct tpm_bios_log {
  96. void *bios_event_log;
  97. void *bios_event_log_end;
  98. };
  99. struct tpm_chip_seqops {
  100. struct tpm_chip *chip;
  101. const struct seq_operations *seqops;
  102. };
  103. struct tpm_chip {
  104. struct device dev;
  105. struct device devs;
  106. struct cdev cdev;
  107. struct cdev cdevs;
  108. /* A driver callback under ops cannot be run unless ops_sem is held
  109. * (sometimes implicitly, eg for the sysfs code). ops becomes null
  110. * when the driver is unregistered, see tpm_try_get_ops.
  111. */
  112. struct rw_semaphore ops_sem;
  113. const struct tpm_class_ops *ops;
  114. struct tpm_bios_log log;
  115. struct tpm_chip_seqops bin_log_seqops;
  116. struct tpm_chip_seqops ascii_log_seqops;
  117. unsigned int flags;
  118. int dev_num; /* /dev/tpm# */
  119. unsigned long is_open; /* only one allowed */
  120. char hwrng_name[64];
  121. struct hwrng hwrng;
  122. struct mutex tpm_mutex; /* tpm is processing */
  123. unsigned long timeout_a; /* jiffies */
  124. unsigned long timeout_b; /* jiffies */
  125. unsigned long timeout_c; /* jiffies */
  126. unsigned long timeout_d; /* jiffies */
  127. bool timeout_adjusted;
  128. unsigned long duration[TPM_NUM_DURATIONS]; /* jiffies */
  129. bool duration_adjusted;
  130. struct dentry *bios_dir[TPM_NUM_EVENT_LOG_FILES];
  131. const struct attribute_group *groups[3 + TPM_MAX_HASHES];
  132. unsigned int groups_cnt;
  133. u32 nr_allocated_banks;
  134. struct tpm_bank_info *allocated_banks;
  135. #ifdef CONFIG_ACPI
  136. acpi_handle acpi_dev_handle;
  137. char ppi_version[TPM_PPI_VERSION_LEN + 1];
  138. #endif /* CONFIG_ACPI */
  139. struct tpm_space work_space;
  140. u32 last_cc;
  141. u32 nr_commands;
  142. u32 *cc_attrs_tbl;
  143. /* active locality */
  144. int locality;
  145. };
  146. #define TPM_HEADER_SIZE 10
  147. enum tpm2_const {
  148. TPM2_PLATFORM_PCR = 24,
  149. TPM2_PCR_SELECT_MIN = ((TPM2_PLATFORM_PCR + 7) / 8),
  150. };
  151. enum tpm2_timeouts {
  152. TPM2_TIMEOUT_A = 750,
  153. TPM2_TIMEOUT_B = 2000,
  154. TPM2_TIMEOUT_C = 200,
  155. TPM2_TIMEOUT_D = 30,
  156. TPM2_DURATION_SHORT = 20,
  157. TPM2_DURATION_MEDIUM = 750,
  158. TPM2_DURATION_LONG = 2000,
  159. TPM2_DURATION_LONG_LONG = 300000,
  160. TPM2_DURATION_DEFAULT = 120000,
  161. };
  162. enum tpm2_structures {
  163. TPM2_ST_NO_SESSIONS = 0x8001,
  164. TPM2_ST_SESSIONS = 0x8002,
  165. };
  166. /* Indicates from what layer of the software stack the error comes from */
  167. #define TSS2_RC_LAYER_SHIFT 16
  168. #define TSS2_RESMGR_TPM_RC_LAYER (11 << TSS2_RC_LAYER_SHIFT)
  169. enum tpm2_return_codes {
  170. TPM2_RC_SUCCESS = 0x0000,
  171. TPM2_RC_HASH = 0x0083, /* RC_FMT1 */
  172. TPM2_RC_HANDLE = 0x008B,
  173. TPM2_RC_INITIALIZE = 0x0100, /* RC_VER1 */
  174. TPM2_RC_FAILURE = 0x0101,
  175. TPM2_RC_DISABLED = 0x0120,
  176. TPM2_RC_UPGRADE = 0x012D,
  177. TPM2_RC_COMMAND_CODE = 0x0143,
  178. TPM2_RC_TESTING = 0x090A, /* RC_WARN */
  179. TPM2_RC_REFERENCE_H0 = 0x0910,
  180. TPM2_RC_RETRY = 0x0922,
  181. };
  182. enum tpm2_command_codes {
  183. TPM2_CC_FIRST = 0x011F,
  184. TPM2_CC_HIERARCHY_CONTROL = 0x0121,
  185. TPM2_CC_HIERARCHY_CHANGE_AUTH = 0x0129,
  186. TPM2_CC_CREATE_PRIMARY = 0x0131,
  187. TPM2_CC_SEQUENCE_COMPLETE = 0x013E,
  188. TPM2_CC_SELF_TEST = 0x0143,
  189. TPM2_CC_STARTUP = 0x0144,
  190. TPM2_CC_SHUTDOWN = 0x0145,
  191. TPM2_CC_NV_READ = 0x014E,
  192. TPM2_CC_CREATE = 0x0153,
  193. TPM2_CC_LOAD = 0x0157,
  194. TPM2_CC_SEQUENCE_UPDATE = 0x015C,
  195. TPM2_CC_UNSEAL = 0x015E,
  196. TPM2_CC_CONTEXT_LOAD = 0x0161,
  197. TPM2_CC_CONTEXT_SAVE = 0x0162,
  198. TPM2_CC_FLUSH_CONTEXT = 0x0165,
  199. TPM2_CC_VERIFY_SIGNATURE = 0x0177,
  200. TPM2_CC_GET_CAPABILITY = 0x017A,
  201. TPM2_CC_GET_RANDOM = 0x017B,
  202. TPM2_CC_PCR_READ = 0x017E,
  203. TPM2_CC_PCR_EXTEND = 0x0182,
  204. TPM2_CC_EVENT_SEQUENCE_COMPLETE = 0x0185,
  205. TPM2_CC_HASH_SEQUENCE_START = 0x0186,
  206. TPM2_CC_CREATE_LOADED = 0x0191,
  207. TPM2_CC_LAST = 0x0193, /* Spec 1.36 */
  208. };
  209. enum tpm2_permanent_handles {
  210. TPM2_RS_PW = 0x40000009,
  211. };
  212. enum tpm2_capabilities {
  213. TPM2_CAP_HANDLES = 1,
  214. TPM2_CAP_COMMANDS = 2,
  215. TPM2_CAP_PCRS = 5,
  216. TPM2_CAP_TPM_PROPERTIES = 6,
  217. };
  218. enum tpm2_properties {
  219. TPM_PT_TOTAL_COMMANDS = 0x0129,
  220. };
  221. enum tpm2_startup_types {
  222. TPM2_SU_CLEAR = 0x0000,
  223. TPM2_SU_STATE = 0x0001,
  224. };
  225. enum tpm2_cc_attrs {
  226. TPM2_CC_ATTR_CHANDLES = 25,
  227. TPM2_CC_ATTR_RHANDLE = 28,
  228. };
  229. #define TPM_VID_INTEL 0x8086
  230. #define TPM_VID_WINBOND 0x1050
  231. #define TPM_VID_STM 0x104A
  232. #define TPM_VID_ATML 0x1114
  233. enum tpm_chip_flags {
  234. TPM_CHIP_FLAG_BOOTSTRAPPED = BIT(0),
  235. TPM_CHIP_FLAG_TPM2 = BIT(1),
  236. TPM_CHIP_FLAG_IRQ = BIT(2),
  237. TPM_CHIP_FLAG_VIRTUAL = BIT(3),
  238. TPM_CHIP_FLAG_HAVE_TIMEOUTS = BIT(4),
  239. TPM_CHIP_FLAG_ALWAYS_POWERED = BIT(5),
  240. TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED = BIT(6),
  241. TPM_CHIP_FLAG_FIRMWARE_UPGRADE = BIT(7),
  242. TPM_CHIP_FLAG_SUSPENDED = BIT(8),
  243. TPM_CHIP_FLAG_HWRNG_DISABLED = BIT(9),
  244. };
  245. #define to_tpm_chip(d) container_of(d, struct tpm_chip, dev)
  246. struct tpm_header {
  247. __be16 tag;
  248. __be32 length;
  249. union {
  250. __be32 ordinal;
  251. __be32 return_code;
  252. };
  253. } __packed;
  254. /* A string buffer type for constructing TPM commands. This is based on the
  255. * ideas of string buffer code in security/keys/trusted.h but is heap based
  256. * in order to keep the stack usage minimal.
  257. */
  258. enum tpm_buf_flags {
  259. TPM_BUF_OVERFLOW = BIT(0),
  260. };
  261. struct tpm_buf {
  262. unsigned int flags;
  263. u8 *data;
  264. };
  265. enum tpm2_object_attributes {
  266. TPM2_OA_FIXED_TPM = BIT(1),
  267. TPM2_OA_FIXED_PARENT = BIT(4),
  268. TPM2_OA_USER_WITH_AUTH = BIT(6),
  269. };
  270. enum tpm2_session_attributes {
  271. TPM2_SA_CONTINUE_SESSION = BIT(0),
  272. };
  273. struct tpm2_hash {
  274. unsigned int crypto_id;
  275. unsigned int tpm_id;
  276. };
  277. static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)
  278. {
  279. struct tpm_header *head = (struct tpm_header *)buf->data;
  280. head->tag = cpu_to_be16(tag);
  281. head->length = cpu_to_be32(sizeof(*head));
  282. head->ordinal = cpu_to_be32(ordinal);
  283. }
  284. static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
  285. {
  286. buf->data = (u8 *)__get_free_page(GFP_KERNEL);
  287. if (!buf->data)
  288. return -ENOMEM;
  289. buf->flags = 0;
  290. tpm_buf_reset(buf, tag, ordinal);
  291. return 0;
  292. }
  293. static inline void tpm_buf_destroy(struct tpm_buf *buf)
  294. {
  295. free_page((unsigned long)buf->data);
  296. }
  297. static inline u32 tpm_buf_length(struct tpm_buf *buf)
  298. {
  299. struct tpm_header *head = (struct tpm_header *)buf->data;
  300. return be32_to_cpu(head->length);
  301. }
  302. static inline u16 tpm_buf_tag(struct tpm_buf *buf)
  303. {
  304. struct tpm_header *head = (struct tpm_header *)buf->data;
  305. return be16_to_cpu(head->tag);
  306. }
  307. static inline void tpm_buf_append(struct tpm_buf *buf,
  308. const unsigned char *new_data,
  309. unsigned int new_len)
  310. {
  311. struct tpm_header *head = (struct tpm_header *)buf->data;
  312. u32 len = tpm_buf_length(buf);
  313. /* Return silently if overflow has already happened. */
  314. if (buf->flags & TPM_BUF_OVERFLOW)
  315. return;
  316. if ((len + new_len) > PAGE_SIZE) {
  317. WARN(1, "tpm_buf: overflow\n");
  318. buf->flags |= TPM_BUF_OVERFLOW;
  319. return;
  320. }
  321. memcpy(&buf->data[len], new_data, new_len);
  322. head->length = cpu_to_be32(len + new_len);
  323. }
  324. static inline void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value)
  325. {
  326. tpm_buf_append(buf, &value, 1);
  327. }
  328. static inline void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value)
  329. {
  330. __be16 value2 = cpu_to_be16(value);
  331. tpm_buf_append(buf, (u8 *) &value2, 2);
  332. }
  333. static inline void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value)
  334. {
  335. __be32 value2 = cpu_to_be32(value);
  336. tpm_buf_append(buf, (u8 *) &value2, 4);
  337. }
  338. /*
  339. * Check if TPM device is in the firmware upgrade mode.
  340. */
  341. static inline bool tpm_is_firmware_upgrade(struct tpm_chip *chip)
  342. {
  343. return chip->flags & TPM_CHIP_FLAG_FIRMWARE_UPGRADE;
  344. }
  345. static inline u32 tpm2_rc_value(u32 rc)
  346. {
  347. return (rc & BIT(7)) ? rc & 0xff : rc;
  348. }
  349. #if defined(CONFIG_TCG_TPM) || defined(CONFIG_TCG_TPM_MODULE)
  350. extern int tpm_is_tpm2(struct tpm_chip *chip);
  351. extern __must_check int tpm_try_get_ops(struct tpm_chip *chip);
  352. extern void tpm_put_ops(struct tpm_chip *chip);
  353. extern ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf,
  354. size_t min_rsp_body_length, const char *desc);
  355. extern int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
  356. struct tpm_digest *digest);
  357. extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
  358. struct tpm_digest *digests);
  359. extern int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen);
  360. extern int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max);
  361. extern struct tpm_chip *tpm_default_chip(void);
  362. void tpm2_flush_context(struct tpm_chip *chip, u32 handle);
  363. #else
  364. static inline int tpm_is_tpm2(struct tpm_chip *chip)
  365. {
  366. return -ENODEV;
  367. }
  368. static inline int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx,
  369. struct tpm_digest *digest)
  370. {
  371. return -ENODEV;
  372. }
  373. static inline int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
  374. struct tpm_digest *digests)
  375. {
  376. return -ENODEV;
  377. }
  378. static inline int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
  379. {
  380. return -ENODEV;
  381. }
  382. static inline int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max)
  383. {
  384. return -ENODEV;
  385. }
  386. static inline struct tpm_chip *tpm_default_chip(void)
  387. {
  388. return NULL;
  389. }
  390. #endif
  391. #endif