tpm.h 14 KB

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