0001
0002 #ifndef __TRUSTED_TPM_H
0003 #define __TRUSTED_TPM_H
0004
0005 #include <keys/trusted-type.h>
0006 #include <linux/tpm_command.h>
0007
0008
0009 #define MAX_BUF_SIZE 1024
0010 #define TPM_GETRANDOM_SIZE 14
0011 #define TPM_SIZE_OFFSET 2
0012 #define TPM_RETURN_OFFSET 6
0013 #define TPM_DATA_OFFSET 10
0014
0015 #define LOAD32(buffer, offset) (ntohl(*(uint32_t *)&buffer[offset]))
0016 #define LOAD32N(buffer, offset) (*(uint32_t *)&buffer[offset])
0017 #define LOAD16(buffer, offset) (ntohs(*(uint16_t *)&buffer[offset]))
0018
0019 extern struct trusted_key_ops trusted_key_tpm_ops;
0020
0021 struct osapsess {
0022 uint32_t handle;
0023 unsigned char secret[SHA1_DIGEST_SIZE];
0024 unsigned char enonce[TPM_NONCE_SIZE];
0025 };
0026
0027
0028 enum {
0029 SEAL_keytype = 1,
0030 SRK_keytype = 4
0031 };
0032
0033 int TSS_authhmac(unsigned char *digest, const unsigned char *key,
0034 unsigned int keylen, unsigned char *h1,
0035 unsigned char *h2, unsigned int h3, ...);
0036 int TSS_checkhmac1(unsigned char *buffer,
0037 const uint32_t command,
0038 const unsigned char *ononce,
0039 const unsigned char *key,
0040 unsigned int keylen, ...);
0041
0042 int trusted_tpm_send(unsigned char *cmd, size_t buflen);
0043 int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce);
0044
0045 int tpm2_seal_trusted(struct tpm_chip *chip,
0046 struct trusted_key_payload *payload,
0047 struct trusted_key_options *options);
0048 int tpm2_unseal_trusted(struct tpm_chip *chip,
0049 struct trusted_key_payload *payload,
0050 struct trusted_key_options *options);
0051
0052 #define TPM_DEBUG 0
0053
0054 #if TPM_DEBUG
0055 static inline void dump_options(struct trusted_key_options *o)
0056 {
0057 pr_info("sealing key type %d\n", o->keytype);
0058 pr_info("sealing key handle %0X\n", o->keyhandle);
0059 pr_info("pcrlock %d\n", o->pcrlock);
0060 pr_info("pcrinfo %d\n", o->pcrinfo_len);
0061 print_hex_dump(KERN_INFO, "pcrinfo ", DUMP_PREFIX_NONE,
0062 16, 1, o->pcrinfo, o->pcrinfo_len, 0);
0063 }
0064
0065 static inline void dump_sess(struct osapsess *s)
0066 {
0067 print_hex_dump(KERN_INFO, "trusted-key: handle ", DUMP_PREFIX_NONE,
0068 16, 1, &s->handle, 4, 0);
0069 pr_info("secret:\n");
0070 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE,
0071 16, 1, &s->secret, SHA1_DIGEST_SIZE, 0);
0072 pr_info("trusted-key: enonce:\n");
0073 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE,
0074 16, 1, &s->enonce, SHA1_DIGEST_SIZE, 0);
0075 }
0076
0077 static inline void dump_tpm_buf(unsigned char *buf)
0078 {
0079 int len;
0080
0081 pr_info("\ntpm buffer\n");
0082 len = LOAD32(buf, TPM_SIZE_OFFSET);
0083 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1, buf, len, 0);
0084 }
0085 #else
0086 static inline void dump_options(struct trusted_key_options *o)
0087 {
0088 }
0089
0090 static inline void dump_sess(struct osapsess *s)
0091 {
0092 }
0093
0094 static inline void dump_tpm_buf(unsigned char *buf)
0095 {
0096 }
0097 #endif
0098 #endif