tpm.h 13 KB

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