Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004,2007,2008 IBM Corporation |
| 3 | * |
| 4 | * Authors: |
| 5 | * Leendert van Doorn <leendert@watson.ibm.com> |
| 6 | * Dave Safford <safford@watson.ibm.com> |
| 7 | * Reiner Sailer <sailer@watson.ibm.com> |
| 8 | * Kylene Hall <kjhall@us.ibm.com> |
| 9 | * Debora Velarde <dvelarde@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 __LINUX_TPM_H__ |
| 23 | #define __LINUX_TPM_H__ |
| 24 | |
Roberto Sassu | 901615c | 2019-02-06 17:24:50 +0100 | [diff] [blame] | 25 | #include <linux/hw_random.h> |
| 26 | #include <linux/acpi.h> |
| 27 | #include <linux/cdev.h> |
| 28 | #include <linux/fs.h> |
Roberto Sassu | aa04247 | 2019-02-06 17:24:48 +0100 | [diff] [blame] | 29 | #include <crypto/hash_info.h> |
| 30 | |
Mimi Zohar | 1c16c96 | 2013-05-21 10:40:47 -0400 | [diff] [blame] | 31 | #define TPM_DIGEST_SIZE 20 /* Max TPM v1.2 PCR size */ |
Roberto Sassu | aa04247 | 2019-02-06 17:24:48 +0100 | [diff] [blame] | 32 | #define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE |
Mimi Zohar | 1c16c96 | 2013-05-21 10:40:47 -0400 | [diff] [blame] | 33 | |
Jason Gunthorpe | 01ad1fa | 2013-11-26 13:30:43 -0700 | [diff] [blame] | 34 | struct tpm_chip; |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 35 | struct trusted_key_payload; |
| 36 | struct trusted_key_options; |
Jason Gunthorpe | 01ad1fa | 2013-11-26 13:30:43 -0700 | [diff] [blame] | 37 | |
Roberto Sassu | aa04247 | 2019-02-06 17:24:48 +0100 | [diff] [blame] | 38 | enum tpm_algorithms { |
| 39 | TPM_ALG_ERROR = 0x0000, |
| 40 | TPM_ALG_SHA1 = 0x0004, |
| 41 | TPM_ALG_KEYEDHASH = 0x0008, |
| 42 | TPM_ALG_SHA256 = 0x000B, |
| 43 | TPM_ALG_SHA384 = 0x000C, |
| 44 | TPM_ALG_SHA512 = 0x000D, |
| 45 | TPM_ALG_NULL = 0x0010, |
| 46 | TPM_ALG_SM3_256 = 0x0012, |
| 47 | }; |
| 48 | |
| 49 | struct tpm_digest { |
| 50 | u16 alg_id; |
| 51 | u8 digest[TPM_MAX_DIGEST_SIZE]; |
| 52 | } __packed; |
| 53 | |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 54 | struct tpm_bank_info { |
| 55 | u16 alg_id; |
| 56 | u16 digest_size; |
| 57 | u16 crypto_id; |
| 58 | }; |
| 59 | |
Jason Gunthorpe | cae8b44 | 2016-07-12 11:41:49 -0600 | [diff] [blame] | 60 | enum TPM_OPS_FLAGS { |
| 61 | TPM_OPS_AUTO_STARTUP = BIT(0), |
| 62 | }; |
| 63 | |
Jason Gunthorpe | 01ad1fa | 2013-11-26 13:30:43 -0700 | [diff] [blame] | 64 | struct tpm_class_ops { |
Jason Gunthorpe | cae8b44 | 2016-07-12 11:41:49 -0600 | [diff] [blame] | 65 | unsigned int flags; |
Jason Gunthorpe | 01ad1fa | 2013-11-26 13:30:43 -0700 | [diff] [blame] | 66 | const u8 req_complete_mask; |
| 67 | const u8 req_complete_val; |
| 68 | bool (*req_canceled)(struct tpm_chip *chip, u8 status); |
| 69 | int (*recv) (struct tpm_chip *chip, u8 *buf, size_t len); |
| 70 | int (*send) (struct tpm_chip *chip, u8 *buf, size_t len); |
| 71 | void (*cancel) (struct tpm_chip *chip); |
| 72 | u8 (*status) (struct tpm_chip *chip); |
Jerry Snitselaar | 36ce089 | 2019-01-30 15:06:58 -0700 | [diff] [blame] | 73 | void (*update_timeouts)(struct tpm_chip *chip, |
Jason Gunthorpe | 8e54caf | 2014-05-21 18:26:44 -0600 | [diff] [blame] | 74 | unsigned long *timeout_cap); |
Tomas Winkler | 627448e | 2018-06-28 18:13:33 +0300 | [diff] [blame] | 75 | int (*go_idle)(struct tpm_chip *chip); |
| 76 | int (*cmd_ready)(struct tpm_chip *chip); |
Jarkko Sakkinen | 877c57d | 2017-03-24 11:45:49 +0200 | [diff] [blame] | 77 | int (*request_locality)(struct tpm_chip *chip, int loc); |
Tomas Winkler | 888d867 | 2018-03-05 13:34:49 +0200 | [diff] [blame] | 78 | int (*relinquish_locality)(struct tpm_chip *chip, int loc); |
Azhar Shaikh | b3e958c | 2017-12-22 12:13:44 -0800 | [diff] [blame] | 79 | void (*clk_enable)(struct tpm_chip *chip, bool value); |
Jason Gunthorpe | 01ad1fa | 2013-11-26 13:30:43 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
Roberto Sassu | 901615c | 2019-02-06 17:24:50 +0100 | [diff] [blame] | 82 | #define TPM_NUM_EVENT_LOG_FILES 3 |
| 83 | |
| 84 | /* Indexes the duration array */ |
| 85 | enum tpm_duration { |
| 86 | TPM_SHORT = 0, |
| 87 | TPM_MEDIUM = 1, |
| 88 | TPM_LONG = 2, |
| 89 | TPM_LONG_LONG = 3, |
| 90 | TPM_UNDEFINED, |
| 91 | TPM_NUM_DURATIONS = TPM_UNDEFINED, |
| 92 | }; |
| 93 | |
| 94 | #define TPM_PPI_VERSION_LEN 3 |
| 95 | |
| 96 | struct tpm_space { |
| 97 | u32 context_tbl[3]; |
| 98 | u8 *context_buf; |
| 99 | u32 session_tbl[3]; |
| 100 | u8 *session_buf; |
| 101 | }; |
| 102 | |
| 103 | struct tpm_bios_log { |
| 104 | void *bios_event_log; |
| 105 | void *bios_event_log_end; |
| 106 | }; |
| 107 | |
| 108 | struct tpm_chip_seqops { |
| 109 | struct tpm_chip *chip; |
| 110 | const struct seq_operations *seqops; |
| 111 | }; |
| 112 | |
| 113 | struct tpm_chip { |
| 114 | struct device dev; |
| 115 | struct device devs; |
| 116 | struct cdev cdev; |
| 117 | struct cdev cdevs; |
| 118 | |
| 119 | /* A driver callback under ops cannot be run unless ops_sem is held |
| 120 | * (sometimes implicitly, eg for the sysfs code). ops becomes null |
| 121 | * when the driver is unregistered, see tpm_try_get_ops. |
| 122 | */ |
| 123 | struct rw_semaphore ops_sem; |
| 124 | const struct tpm_class_ops *ops; |
| 125 | |
| 126 | struct tpm_bios_log log; |
| 127 | struct tpm_chip_seqops bin_log_seqops; |
| 128 | struct tpm_chip_seqops ascii_log_seqops; |
| 129 | |
| 130 | unsigned int flags; |
| 131 | |
| 132 | int dev_num; /* /dev/tpm# */ |
| 133 | unsigned long is_open; /* only one allowed */ |
| 134 | |
| 135 | char hwrng_name[64]; |
| 136 | struct hwrng hwrng; |
| 137 | |
| 138 | struct mutex tpm_mutex; /* tpm is processing */ |
| 139 | |
| 140 | unsigned long timeout_a; /* jiffies */ |
| 141 | unsigned long timeout_b; /* jiffies */ |
| 142 | unsigned long timeout_c; /* jiffies */ |
| 143 | unsigned long timeout_d; /* jiffies */ |
| 144 | bool timeout_adjusted; |
| 145 | unsigned long duration[TPM_NUM_DURATIONS]; /* jiffies */ |
| 146 | bool duration_adjusted; |
| 147 | |
| 148 | struct dentry *bios_dir[TPM_NUM_EVENT_LOG_FILES]; |
| 149 | |
| 150 | const struct attribute_group *groups[3]; |
| 151 | unsigned int groups_cnt; |
| 152 | |
| 153 | u32 nr_allocated_banks; |
| 154 | struct tpm_bank_info *allocated_banks; |
| 155 | #ifdef CONFIG_ACPI |
| 156 | acpi_handle acpi_dev_handle; |
| 157 | char ppi_version[TPM_PPI_VERSION_LEN + 1]; |
| 158 | #endif /* CONFIG_ACPI */ |
| 159 | |
| 160 | struct tpm_space work_space; |
| 161 | u32 last_cc; |
| 162 | u32 nr_commands; |
| 163 | u32 *cc_attrs_tbl; |
| 164 | |
| 165 | /* active locality */ |
| 166 | int locality; |
| 167 | }; |
| 168 | |
Randy Dunlap | ff76ec1 | 2009-10-28 12:26:39 -0700 | [diff] [blame] | 169 | #if defined(CONFIG_TCG_TPM) || defined(CONFIG_TCG_TPM_MODULE) |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 170 | |
Jarkko Sakkinen | aad887f | 2017-11-05 13:16:26 +0200 | [diff] [blame] | 171 | extern int tpm_is_tpm2(struct tpm_chip *chip); |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 172 | extern int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, |
| 173 | struct tpm_digest *digest); |
Roberto Sassu | 0b6cf6b | 2019-02-06 17:24:52 +0100 | [diff] [blame^] | 174 | extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, |
| 175 | struct tpm_digest *digests); |
Jarkko Sakkinen | aad887f | 2017-11-05 13:16:26 +0200 | [diff] [blame] | 176 | extern int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen); |
| 177 | extern int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max); |
| 178 | extern int tpm_seal_trusted(struct tpm_chip *chip, |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 179 | struct trusted_key_payload *payload, |
| 180 | struct trusted_key_options *options); |
Jarkko Sakkinen | aad887f | 2017-11-05 13:16:26 +0200 | [diff] [blame] | 181 | extern int tpm_unseal_trusted(struct tpm_chip *chip, |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 182 | struct trusted_key_payload *payload, |
| 183 | struct trusted_key_options *options); |
Stefan Berger | aaae815 | 2018-06-26 15:09:30 -0400 | [diff] [blame] | 184 | extern struct tpm_chip *tpm_default_chip(void); |
Mimi Zohar | d6ba452 | 2009-10-26 09:26:18 -0400 | [diff] [blame] | 185 | #else |
Jarkko Sakkinen | aad887f | 2017-11-05 13:16:26 +0200 | [diff] [blame] | 186 | static inline int tpm_is_tpm2(struct tpm_chip *chip) |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 187 | { |
| 188 | return -ENODEV; |
| 189 | } |
Tomas Winkler | 95adc6b | 2018-10-19 21:23:07 +0300 | [diff] [blame] | 190 | |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 191 | static inline int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, |
| 192 | struct tpm_digest *digest) |
Jarkko Sakkinen | aad887f | 2017-11-05 13:16:26 +0200 | [diff] [blame] | 193 | { |
Mimi Zohar | d6ba452 | 2009-10-26 09:26:18 -0400 | [diff] [blame] | 194 | return -ENODEV; |
| 195 | } |
Tomas Winkler | 95adc6b | 2018-10-19 21:23:07 +0300 | [diff] [blame] | 196 | |
| 197 | static inline int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, |
Roberto Sassu | 0b6cf6b | 2019-02-06 17:24:52 +0100 | [diff] [blame^] | 198 | struct tpm_digest *digests) |
Jarkko Sakkinen | aad887f | 2017-11-05 13:16:26 +0200 | [diff] [blame] | 199 | { |
Mimi Zohar | d6ba452 | 2009-10-26 09:26:18 -0400 | [diff] [blame] | 200 | return -ENODEV; |
| 201 | } |
Tomas Winkler | 95adc6b | 2018-10-19 21:23:07 +0300 | [diff] [blame] | 202 | |
Jarkko Sakkinen | aad887f | 2017-11-05 13:16:26 +0200 | [diff] [blame] | 203 | static inline int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen) |
| 204 | { |
Mimi Zohar | c749ba9 | 2010-11-23 18:54:16 -0500 | [diff] [blame] | 205 | return -ENODEV; |
| 206 | } |
Jarkko Sakkinen | aad887f | 2017-11-05 13:16:26 +0200 | [diff] [blame] | 207 | static inline int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max) |
| 208 | { |
Kent Yoder | 41ab999 | 2012-06-07 13:47:14 -0500 | [diff] [blame] | 209 | return -ENODEV; |
| 210 | } |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 211 | |
Jarkko Sakkinen | aad887f | 2017-11-05 13:16:26 +0200 | [diff] [blame] | 212 | static inline int tpm_seal_trusted(struct tpm_chip *chip, |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 213 | struct trusted_key_payload *payload, |
| 214 | struct trusted_key_options *options) |
| 215 | { |
| 216 | return -ENODEV; |
| 217 | } |
Jarkko Sakkinen | aad887f | 2017-11-05 13:16:26 +0200 | [diff] [blame] | 218 | static inline int tpm_unseal_trusted(struct tpm_chip *chip, |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 219 | struct trusted_key_payload *payload, |
| 220 | struct trusted_key_options *options) |
| 221 | { |
| 222 | return -ENODEV; |
| 223 | } |
Stefan Berger | aaae815 | 2018-06-26 15:09:30 -0400 | [diff] [blame] | 224 | static inline struct tpm_chip *tpm_default_chip(void) |
| 225 | { |
| 226 | return NULL; |
| 227 | } |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 228 | #endif |
| 229 | #endif |