tpm.h 13 KB

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