tpm.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * Copyright (C) 2004 IBM Corporation
  3. * Copyright (C) 2015 Intel Corporation
  4. *
  5. * Authors:
  6. * Leendert van Doorn <leendert@watson.ibm.com>
  7. * Dave Safford <safford@watson.ibm.com>
  8. * Reiner Sailer <sailer@watson.ibm.com>
  9. * Kylene Hall <kjhall@us.ibm.com>
  10. *
  11. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  12. *
  13. * Device driver for TCG/TCPA TPM (trusted platform module).
  14. * Specifications at www.trustedcomputinggroup.org
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation, version 2 of the
  19. * License.
  20. *
  21. */
  22. #ifndef __TPM_H__
  23. #define __TPM_H__
  24. #include <linux/module.h>
  25. #include <linux/delay.h>
  26. #include <linux/fs.h>
  27. #include <linux/mutex.h>
  28. #include <linux/sched.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/io.h>
  31. #include <linux/tpm.h>
  32. #include <linux/acpi.h>
  33. #include <linux/cdev.h>
  34. #include <linux/highmem.h>
  35. enum tpm_const {
  36. TPM_MINOR = 224, /* officially assigned */
  37. TPM_BUFSIZE = 4096,
  38. TPM_NUM_DEVICES = 65536,
  39. TPM_RETRY = 50, /* 5 seconds */
  40. };
  41. enum tpm_timeout {
  42. TPM_TIMEOUT = 5, /* msecs */
  43. TPM_TIMEOUT_RETRY = 100 /* msecs */
  44. };
  45. /* TPM addresses */
  46. enum tpm_addr {
  47. TPM_SUPERIO_ADDR = 0x2E,
  48. TPM_ADDR = 0x4E,
  49. };
  50. /* Indexes the duration array */
  51. enum tpm_duration {
  52. TPM_SHORT = 0,
  53. TPM_MEDIUM = 1,
  54. TPM_LONG = 2,
  55. TPM_UNDEFINED,
  56. };
  57. #define TPM_WARN_RETRY 0x800
  58. #define TPM_WARN_DOING_SELFTEST 0x802
  59. #define TPM_ERR_DEACTIVATED 0x6
  60. #define TPM_ERR_DISABLED 0x7
  61. #define TPM_ERR_INVALID_POSTINIT 38
  62. #define TPM_HEADER_SIZE 10
  63. enum tpm2_const {
  64. TPM2_PLATFORM_PCR = 24,
  65. TPM2_PCR_SELECT_MIN = ((TPM2_PLATFORM_PCR + 7) / 8),
  66. TPM2_TIMEOUT_A = 750,
  67. TPM2_TIMEOUT_B = 2000,
  68. TPM2_TIMEOUT_C = 200,
  69. TPM2_TIMEOUT_D = 30,
  70. TPM2_DURATION_SHORT = 20,
  71. TPM2_DURATION_MEDIUM = 750,
  72. TPM2_DURATION_LONG = 2000,
  73. };
  74. enum tpm2_structures {
  75. TPM2_ST_NO_SESSIONS = 0x8001,
  76. TPM2_ST_SESSIONS = 0x8002,
  77. };
  78. enum tpm2_return_codes {
  79. TPM2_RC_HASH = 0x0083, /* RC_FMT1 */
  80. TPM2_RC_INITIALIZE = 0x0100, /* RC_VER1 */
  81. TPM2_RC_DISABLED = 0x0120,
  82. TPM2_RC_TESTING = 0x090A, /* RC_WARN */
  83. };
  84. enum tpm2_algorithms {
  85. TPM2_ALG_SHA1 = 0x0004,
  86. TPM2_ALG_KEYEDHASH = 0x0008,
  87. TPM2_ALG_SHA256 = 0x000B,
  88. TPM2_ALG_SHA384 = 0x000C,
  89. TPM2_ALG_SHA512 = 0x000D,
  90. TPM2_ALG_NULL = 0x0010,
  91. TPM2_ALG_SM3_256 = 0x0012,
  92. };
  93. enum tpm2_command_codes {
  94. TPM2_CC_FIRST = 0x011F,
  95. TPM2_CC_SELF_TEST = 0x0143,
  96. TPM2_CC_STARTUP = 0x0144,
  97. TPM2_CC_SHUTDOWN = 0x0145,
  98. TPM2_CC_CREATE = 0x0153,
  99. TPM2_CC_LOAD = 0x0157,
  100. TPM2_CC_UNSEAL = 0x015E,
  101. TPM2_CC_FLUSH_CONTEXT = 0x0165,
  102. TPM2_CC_GET_CAPABILITY = 0x017A,
  103. TPM2_CC_GET_RANDOM = 0x017B,
  104. TPM2_CC_PCR_READ = 0x017E,
  105. TPM2_CC_PCR_EXTEND = 0x0182,
  106. TPM2_CC_LAST = 0x018F,
  107. };
  108. enum tpm2_permanent_handles {
  109. TPM2_RS_PW = 0x40000009,
  110. };
  111. enum tpm2_capabilities {
  112. TPM2_CAP_TPM_PROPERTIES = 6,
  113. };
  114. enum tpm2_startup_types {
  115. TPM2_SU_CLEAR = 0x0000,
  116. TPM2_SU_STATE = 0x0001,
  117. };
  118. #define TPM_VID_INTEL 0x8086
  119. #define TPM_VID_WINBOND 0x1050
  120. #define TPM_VID_STM 0x104A
  121. #define TPM_PPI_VERSION_LEN 3
  122. enum tpm_chip_flags {
  123. TPM_CHIP_FLAG_REGISTERED = BIT(0),
  124. TPM_CHIP_FLAG_TPM2 = BIT(1),
  125. TPM_CHIP_FLAG_IRQ = BIT(2),
  126. TPM_CHIP_FLAG_VIRTUAL = BIT(3),
  127. TPM_CHIP_FLAG_ALWAYS_POWERED = BIT(5),
  128. };
  129. struct tpm_chip {
  130. struct device dev;
  131. struct cdev cdev;
  132. /* A driver callback under ops cannot be run unless ops_sem is held
  133. * (sometimes implicitly, eg for the sysfs code). ops becomes null
  134. * when the driver is unregistered, see tpm_try_get_ops.
  135. */
  136. struct rw_semaphore ops_sem;
  137. const struct tpm_class_ops *ops;
  138. unsigned int flags;
  139. int dev_num; /* /dev/tpm# */
  140. unsigned long is_open; /* only one allowed */
  141. struct mutex tpm_mutex; /* tpm is processing */
  142. unsigned long timeout_a; /* jiffies */
  143. unsigned long timeout_b; /* jiffies */
  144. unsigned long timeout_c; /* jiffies */
  145. unsigned long timeout_d; /* jiffies */
  146. bool timeout_adjusted;
  147. unsigned long duration[3]; /* jiffies */
  148. bool duration_adjusted;
  149. struct dentry **bios_dir;
  150. const struct attribute_group *groups[3];
  151. unsigned int groups_cnt;
  152. #ifdef CONFIG_ACPI
  153. acpi_handle acpi_dev_handle;
  154. char ppi_version[TPM_PPI_VERSION_LEN + 1];
  155. #endif /* CONFIG_ACPI */
  156. };
  157. #define to_tpm_chip(d) container_of(d, struct tpm_chip, dev)
  158. static inline int tpm_read_index(int base, int index)
  159. {
  160. outb(index, base);
  161. return inb(base+1) & 0xFF;
  162. }
  163. static inline void tpm_write_index(int base, int index, int value)
  164. {
  165. outb(index, base);
  166. outb(value & 0xFF, base+1);
  167. }
  168. struct tpm_input_header {
  169. __be16 tag;
  170. __be32 length;
  171. __be32 ordinal;
  172. } __packed;
  173. struct tpm_output_header {
  174. __be16 tag;
  175. __be32 length;
  176. __be32 return_code;
  177. } __packed;
  178. #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
  179. struct stclear_flags_t {
  180. __be16 tag;
  181. u8 deactivated;
  182. u8 disableForceClear;
  183. u8 physicalPresence;
  184. u8 physicalPresenceLock;
  185. u8 bGlobalLock;
  186. } __packed;
  187. struct tpm_version_t {
  188. u8 Major;
  189. u8 Minor;
  190. u8 revMajor;
  191. u8 revMinor;
  192. } __packed;
  193. struct tpm_version_1_2_t {
  194. __be16 tag;
  195. u8 Major;
  196. u8 Minor;
  197. u8 revMajor;
  198. u8 revMinor;
  199. } __packed;
  200. struct timeout_t {
  201. __be32 a;
  202. __be32 b;
  203. __be32 c;
  204. __be32 d;
  205. } __packed;
  206. struct duration_t {
  207. __be32 tpm_short;
  208. __be32 tpm_medium;
  209. __be32 tpm_long;
  210. } __packed;
  211. struct permanent_flags_t {
  212. __be16 tag;
  213. u8 disable;
  214. u8 ownership;
  215. u8 deactivated;
  216. u8 readPubek;
  217. u8 disableOwnerClear;
  218. u8 allowMaintenance;
  219. u8 physicalPresenceLifetimeLock;
  220. u8 physicalPresenceHWEnable;
  221. u8 physicalPresenceCMDEnable;
  222. u8 CEKPUsed;
  223. u8 TPMpost;
  224. u8 TPMpostLock;
  225. u8 FIPS;
  226. u8 operator;
  227. u8 enableRevokeEK;
  228. u8 nvLocked;
  229. u8 readSRKPub;
  230. u8 tpmEstablished;
  231. u8 maintenanceDone;
  232. u8 disableFullDALogicInfo;
  233. } __packed;
  234. typedef union {
  235. struct permanent_flags_t perm_flags;
  236. struct stclear_flags_t stclear_flags;
  237. bool owned;
  238. __be32 num_pcrs;
  239. struct tpm_version_t tpm_version;
  240. struct tpm_version_1_2_t tpm_version_1_2;
  241. __be32 manufacturer_id;
  242. struct timeout_t timeout;
  243. struct duration_t duration;
  244. } cap_t;
  245. enum tpm_capabilities {
  246. TPM_CAP_FLAG = cpu_to_be32(4),
  247. TPM_CAP_PROP = cpu_to_be32(5),
  248. CAP_VERSION_1_1 = cpu_to_be32(0x06),
  249. CAP_VERSION_1_2 = cpu_to_be32(0x1A)
  250. };
  251. enum tpm_sub_capabilities {
  252. TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
  253. TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
  254. TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
  255. TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
  256. TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
  257. TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
  258. TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
  259. };
  260. struct tpm_getcap_params_in {
  261. __be32 cap;
  262. __be32 subcap_size;
  263. __be32 subcap;
  264. } __packed;
  265. struct tpm_getcap_params_out {
  266. __be32 cap_size;
  267. cap_t cap;
  268. } __packed;
  269. struct tpm_readpubek_params_out {
  270. u8 algorithm[4];
  271. u8 encscheme[2];
  272. u8 sigscheme[2];
  273. __be32 paramsize;
  274. u8 parameters[12]; /*assuming RSA*/
  275. __be32 keysize;
  276. u8 modulus[256];
  277. u8 checksum[20];
  278. } __packed;
  279. typedef union {
  280. struct tpm_input_header in;
  281. struct tpm_output_header out;
  282. } tpm_cmd_header;
  283. struct tpm_pcrread_out {
  284. u8 pcr_result[TPM_DIGEST_SIZE];
  285. } __packed;
  286. struct tpm_pcrread_in {
  287. __be32 pcr_idx;
  288. } __packed;
  289. struct tpm_pcrextend_in {
  290. __be32 pcr_idx;
  291. u8 hash[TPM_DIGEST_SIZE];
  292. } __packed;
  293. /* 128 bytes is an arbitrary cap. This could be as large as TPM_BUFSIZE - 18
  294. * bytes, but 128 is still a relatively large number of random bytes and
  295. * anything much bigger causes users of struct tpm_cmd_t to start getting
  296. * compiler warnings about stack frame size. */
  297. #define TPM_MAX_RNG_DATA 128
  298. struct tpm_getrandom_out {
  299. __be32 rng_data_len;
  300. u8 rng_data[TPM_MAX_RNG_DATA];
  301. } __packed;
  302. struct tpm_getrandom_in {
  303. __be32 num_bytes;
  304. } __packed;
  305. struct tpm_startup_in {
  306. __be16 startup_type;
  307. } __packed;
  308. typedef union {
  309. struct tpm_getcap_params_out getcap_out;
  310. struct tpm_readpubek_params_out readpubek_out;
  311. u8 readpubek_out_buffer[sizeof(struct tpm_readpubek_params_out)];
  312. struct tpm_getcap_params_in getcap_in;
  313. struct tpm_pcrread_in pcrread_in;
  314. struct tpm_pcrread_out pcrread_out;
  315. struct tpm_pcrextend_in pcrextend_in;
  316. struct tpm_getrandom_in getrandom_in;
  317. struct tpm_getrandom_out getrandom_out;
  318. struct tpm_startup_in startup_in;
  319. } tpm_cmd_params;
  320. struct tpm_cmd_t {
  321. tpm_cmd_header header;
  322. tpm_cmd_params params;
  323. } __packed;
  324. /* A string buffer type for constructing TPM commands. This is based on the
  325. * ideas of string buffer code in security/keys/trusted.h but is heap based
  326. * in order to keep the stack usage minimal.
  327. */
  328. enum tpm_buf_flags {
  329. TPM_BUF_OVERFLOW = BIT(0),
  330. };
  331. struct tpm_buf {
  332. struct page *data_page;
  333. unsigned int flags;
  334. u8 *data;
  335. };
  336. static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
  337. {
  338. struct tpm_input_header *head;
  339. buf->data_page = alloc_page(GFP_HIGHUSER);
  340. if (!buf->data_page)
  341. return -ENOMEM;
  342. buf->flags = 0;
  343. buf->data = kmap(buf->data_page);
  344. head = (struct tpm_input_header *) buf->data;
  345. head->tag = cpu_to_be16(tag);
  346. head->length = cpu_to_be32(sizeof(*head));
  347. head->ordinal = cpu_to_be32(ordinal);
  348. return 0;
  349. }
  350. static inline void tpm_buf_destroy(struct tpm_buf *buf)
  351. {
  352. kunmap(buf->data_page);
  353. __free_page(buf->data_page);
  354. }
  355. static inline u32 tpm_buf_length(struct tpm_buf *buf)
  356. {
  357. struct tpm_input_header *head = (struct tpm_input_header *) buf->data;
  358. return be32_to_cpu(head->length);
  359. }
  360. static inline u16 tpm_buf_tag(struct tpm_buf *buf)
  361. {
  362. struct tpm_input_header *head = (struct tpm_input_header *) buf->data;
  363. return be16_to_cpu(head->tag);
  364. }
  365. static inline void tpm_buf_append(struct tpm_buf *buf,
  366. const unsigned char *new_data,
  367. unsigned int new_len)
  368. {
  369. struct tpm_input_header *head = (struct tpm_input_header *) buf->data;
  370. u32 len = tpm_buf_length(buf);
  371. /* Return silently if overflow has already happened. */
  372. if (buf->flags & TPM_BUF_OVERFLOW)
  373. return;
  374. if ((len + new_len) > PAGE_SIZE) {
  375. WARN(1, "tpm_buf: overflow\n");
  376. buf->flags |= TPM_BUF_OVERFLOW;
  377. return;
  378. }
  379. memcpy(&buf->data[len], new_data, new_len);
  380. head->length = cpu_to_be32(len + new_len);
  381. }
  382. static inline void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value)
  383. {
  384. tpm_buf_append(buf, &value, 1);
  385. }
  386. static inline void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value)
  387. {
  388. __be16 value2 = cpu_to_be16(value);
  389. tpm_buf_append(buf, (u8 *) &value2, 2);
  390. }
  391. static inline void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value)
  392. {
  393. __be32 value2 = cpu_to_be32(value);
  394. tpm_buf_append(buf, (u8 *) &value2, 4);
  395. }
  396. extern struct class *tpm_class;
  397. extern dev_t tpm_devt;
  398. extern const struct file_operations tpm_fops;
  399. extern struct idr dev_nums_idr;
  400. enum tpm_transmit_flags {
  401. TPM_TRANSMIT_UNLOCKED = BIT(0),
  402. };
  403. ssize_t tpm_transmit(struct tpm_chip *chip, const u8 *buf, size_t bufsiz,
  404. unsigned int flags);
  405. ssize_t tpm_transmit_cmd(struct tpm_chip *chip, const void *cmd, int len,
  406. unsigned int flags, const char *desc);
  407. ssize_t tpm_getcap(struct tpm_chip *chip, __be32 subcap_id, cap_t *cap,
  408. const char *desc);
  409. int tpm_get_timeouts(struct tpm_chip *);
  410. int tpm1_auto_startup(struct tpm_chip *chip);
  411. int tpm_do_selftest(struct tpm_chip *chip);
  412. unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
  413. int tpm_pm_suspend(struct device *dev);
  414. int tpm_pm_resume(struct device *dev);
  415. int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
  416. wait_queue_head_t *queue, bool check_cancel);
  417. struct tpm_chip *tpm_chip_find_get(int chip_num);
  418. __must_check int tpm_try_get_ops(struct tpm_chip *chip);
  419. void tpm_put_ops(struct tpm_chip *chip);
  420. struct tpm_chip *tpm_chip_alloc(struct device *dev,
  421. const struct tpm_class_ops *ops);
  422. struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
  423. const struct tpm_class_ops *ops);
  424. int tpm_chip_register(struct tpm_chip *chip);
  425. void tpm_chip_unregister(struct tpm_chip *chip);
  426. void tpm_sysfs_add_device(struct tpm_chip *chip);
  427. int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
  428. #ifdef CONFIG_ACPI
  429. extern void tpm_add_ppi(struct tpm_chip *chip);
  430. #else
  431. static inline void tpm_add_ppi(struct tpm_chip *chip)
  432. {
  433. }
  434. #endif
  435. static inline inline u32 tpm2_rc_value(u32 rc)
  436. {
  437. return (rc & BIT(7)) ? rc & 0xff : rc;
  438. }
  439. int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
  440. int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash);
  441. int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max);
  442. int tpm2_seal_trusted(struct tpm_chip *chip,
  443. struct trusted_key_payload *payload,
  444. struct trusted_key_options *options);
  445. int tpm2_unseal_trusted(struct tpm_chip *chip,
  446. struct trusted_key_payload *payload,
  447. struct trusted_key_options *options);
  448. ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,
  449. u32 *value, const char *desc);
  450. int tpm2_auto_startup(struct tpm_chip *chip);
  451. void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type);
  452. unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
  453. int tpm2_probe(struct tpm_chip *chip);
  454. #endif