Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Sumit Garg | 47f9c27 | 2019-10-16 10:44:54 +0530 | [diff] [blame] | 2 | #ifndef __TRUSTED_TPM_H |
| 3 | #define __TRUSTED_TPM_H |
| 4 | |
| 5 | #include <keys/trusted-type.h> |
| 6 | #include <linux/tpm_command.h> |
Mimi Zohar | d00a1c7 | 2010-11-23 17:50:34 -0500 | [diff] [blame] | 7 | |
| 8 | /* implementation specific TPM constants */ |
Denis Kenzior | e1ea9f8 | 2018-10-09 17:48:41 +0100 | [diff] [blame] | 9 | #define MAX_BUF_SIZE 1024 |
Mimi Zohar | d00a1c7 | 2010-11-23 17:50:34 -0500 | [diff] [blame] | 10 | #define TPM_GETRANDOM_SIZE 14 |
Mimi Zohar | d00a1c7 | 2010-11-23 17:50:34 -0500 | [diff] [blame] | 11 | #define TPM_SIZE_OFFSET 2 |
| 12 | #define TPM_RETURN_OFFSET 6 |
| 13 | #define TPM_DATA_OFFSET 10 |
| 14 | |
| 15 | #define LOAD32(buffer, offset) (ntohl(*(uint32_t *)&buffer[offset])) |
| 16 | #define LOAD32N(buffer, offset) (*(uint32_t *)&buffer[offset]) |
| 17 | #define LOAD16(buffer, offset) (ntohs(*(uint16_t *)&buffer[offset])) |
| 18 | |
Sumit Garg | 5d0682b | 2021-03-01 18:41:24 +0530 | [diff] [blame] | 19 | extern struct trusted_key_ops trusted_key_tpm_ops; |
| 20 | |
Mimi Zohar | d00a1c7 | 2010-11-23 17:50:34 -0500 | [diff] [blame] | 21 | struct osapsess { |
| 22 | uint32_t handle; |
| 23 | unsigned char secret[SHA1_DIGEST_SIZE]; |
| 24 | unsigned char enonce[TPM_NONCE_SIZE]; |
| 25 | }; |
| 26 | |
| 27 | /* discrete values, but have to store in uint16_t for TPM use */ |
| 28 | enum { |
| 29 | SEAL_keytype = 1, |
| 30 | SRK_keytype = 4 |
| 31 | }; |
| 32 | |
Denis Kenzior | e1ea9f8 | 2018-10-09 17:48:41 +0100 | [diff] [blame] | 33 | int TSS_authhmac(unsigned char *digest, const unsigned char *key, |
| 34 | unsigned int keylen, unsigned char *h1, |
ndesaulniers@google.com | be24b37 | 2018-10-22 16:43:57 -0700 | [diff] [blame] | 35 | unsigned char *h2, unsigned int h3, ...); |
Denis Kenzior | e1ea9f8 | 2018-10-09 17:48:41 +0100 | [diff] [blame] | 36 | int TSS_checkhmac1(unsigned char *buffer, |
| 37 | const uint32_t command, |
| 38 | const unsigned char *ononce, |
| 39 | const unsigned char *key, |
| 40 | unsigned int keylen, ...); |
| 41 | |
| 42 | int trusted_tpm_send(unsigned char *cmd, size_t buflen); |
Sumit Garg | c6f61e5 | 2019-10-16 10:44:53 +0530 | [diff] [blame] | 43 | int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce); |
Denis Kenzior | e1ea9f8 | 2018-10-09 17:48:41 +0100 | [diff] [blame] | 44 | |
Sumit Garg | 2e19e10 | 2019-10-16 10:44:55 +0530 | [diff] [blame] | 45 | int tpm2_seal_trusted(struct tpm_chip *chip, |
| 46 | struct trusted_key_payload *payload, |
| 47 | struct trusted_key_options *options); |
| 48 | int tpm2_unseal_trusted(struct tpm_chip *chip, |
| 49 | struct trusted_key_payload *payload, |
| 50 | struct trusted_key_options *options); |
| 51 | |
Mimi Zohar | d00a1c7 | 2010-11-23 17:50:34 -0500 | [diff] [blame] | 52 | #define TPM_DEBUG 0 |
| 53 | |
| 54 | #if TPM_DEBUG |
| 55 | static inline void dump_options(struct trusted_key_options *o) |
| 56 | { |
Sumit Garg | 5d0682b | 2021-03-01 18:41:24 +0530 | [diff] [blame] | 57 | pr_info("sealing key type %d\n", o->keytype); |
| 58 | pr_info("sealing key handle %0X\n", o->keyhandle); |
| 59 | pr_info("pcrlock %d\n", o->pcrlock); |
| 60 | pr_info("pcrinfo %d\n", o->pcrinfo_len); |
Mimi Zohar | d00a1c7 | 2010-11-23 17:50:34 -0500 | [diff] [blame] | 61 | print_hex_dump(KERN_INFO, "pcrinfo ", DUMP_PREFIX_NONE, |
| 62 | 16, 1, o->pcrinfo, o->pcrinfo_len, 0); |
| 63 | } |
| 64 | |
Mimi Zohar | d00a1c7 | 2010-11-23 17:50:34 -0500 | [diff] [blame] | 65 | static inline void dump_sess(struct osapsess *s) |
| 66 | { |
| 67 | print_hex_dump(KERN_INFO, "trusted-key: handle ", DUMP_PREFIX_NONE, |
| 68 | 16, 1, &s->handle, 4, 0); |
Sumit Garg | 5d0682b | 2021-03-01 18:41:24 +0530 | [diff] [blame] | 69 | pr_info("secret:\n"); |
Mimi Zohar | d00a1c7 | 2010-11-23 17:50:34 -0500 | [diff] [blame] | 70 | print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, |
| 71 | 16, 1, &s->secret, SHA1_DIGEST_SIZE, 0); |
| 72 | pr_info("trusted-key: enonce:\n"); |
| 73 | print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, |
| 74 | 16, 1, &s->enonce, SHA1_DIGEST_SIZE, 0); |
| 75 | } |
| 76 | |
| 77 | static inline void dump_tpm_buf(unsigned char *buf) |
| 78 | { |
| 79 | int len; |
| 80 | |
Sumit Garg | 5d0682b | 2021-03-01 18:41:24 +0530 | [diff] [blame] | 81 | pr_info("\ntpm buffer\n"); |
Mimi Zohar | d00a1c7 | 2010-11-23 17:50:34 -0500 | [diff] [blame] | 82 | len = LOAD32(buf, TPM_SIZE_OFFSET); |
| 83 | print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1, buf, len, 0); |
| 84 | } |
| 85 | #else |
| 86 | static inline void dump_options(struct trusted_key_options *o) |
| 87 | { |
| 88 | } |
| 89 | |
Mimi Zohar | d00a1c7 | 2010-11-23 17:50:34 -0500 | [diff] [blame] | 90 | static inline void dump_sess(struct osapsess *s) |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | static inline void dump_tpm_buf(unsigned char *buf) |
| 95 | { |
| 96 | } |
| 97 | #endif |
Mimi Zohar | d00a1c7 | 2010-11-23 17:50:34 -0500 | [diff] [blame] | 98 | #endif |