blob: a56d8e1298f27e76d3c299de8b5d3870b77535d4 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Sumit Garg47f9c272019-10-16 10:44:54 +05302#ifndef __TRUSTED_TPM_H
3#define __TRUSTED_TPM_H
4
5#include <keys/trusted-type.h>
6#include <linux/tpm_command.h>
Mimi Zohard00a1c72010-11-23 17:50:34 -05007
8/* implementation specific TPM constants */
Denis Kenziore1ea9f82018-10-09 17:48:41 +01009#define MAX_BUF_SIZE 1024
Mimi Zohard00a1c72010-11-23 17:50:34 -050010#define TPM_GETRANDOM_SIZE 14
Mimi Zohard00a1c72010-11-23 17:50:34 -050011#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
Mimi Zohard00a1c72010-11-23 17:50:34 -050019struct osapsess {
20 uint32_t handle;
21 unsigned char secret[SHA1_DIGEST_SIZE];
22 unsigned char enonce[TPM_NONCE_SIZE];
23};
24
25/* discrete values, but have to store in uint16_t for TPM use */
26enum {
27 SEAL_keytype = 1,
28 SRK_keytype = 4
29};
30
Denis Kenziore1ea9f82018-10-09 17:48:41 +010031int TSS_authhmac(unsigned char *digest, const unsigned char *key,
32 unsigned int keylen, unsigned char *h1,
ndesaulniers@google.combe24b372018-10-22 16:43:57 -070033 unsigned char *h2, unsigned int h3, ...);
Denis Kenziore1ea9f82018-10-09 17:48:41 +010034int TSS_checkhmac1(unsigned char *buffer,
35 const uint32_t command,
36 const unsigned char *ononce,
37 const unsigned char *key,
38 unsigned int keylen, ...);
39
40int trusted_tpm_send(unsigned char *cmd, size_t buflen);
Sumit Gargc6f61e52019-10-16 10:44:53 +053041int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce);
Denis Kenziore1ea9f82018-10-09 17:48:41 +010042
Sumit Garg2e19e102019-10-16 10:44:55 +053043int tpm2_seal_trusted(struct tpm_chip *chip,
44 struct trusted_key_payload *payload,
45 struct trusted_key_options *options);
46int tpm2_unseal_trusted(struct tpm_chip *chip,
47 struct trusted_key_payload *payload,
48 struct trusted_key_options *options);
49
Mimi Zohard00a1c72010-11-23 17:50:34 -050050#define TPM_DEBUG 0
51
52#if TPM_DEBUG
53static inline void dump_options(struct trusted_key_options *o)
54{
55 pr_info("trusted_key: sealing key type %d\n", o->keytype);
56 pr_info("trusted_key: sealing key handle %0X\n", o->keyhandle);
57 pr_info("trusted_key: pcrlock %d\n", o->pcrlock);
58 pr_info("trusted_key: pcrinfo %d\n", o->pcrinfo_len);
59 print_hex_dump(KERN_INFO, "pcrinfo ", DUMP_PREFIX_NONE,
60 16, 1, o->pcrinfo, o->pcrinfo_len, 0);
61}
62
63static inline void dump_payload(struct trusted_key_payload *p)
64{
65 pr_info("trusted_key: key_len %d\n", p->key_len);
66 print_hex_dump(KERN_INFO, "key ", DUMP_PREFIX_NONE,
67 16, 1, p->key, p->key_len, 0);
68 pr_info("trusted_key: bloblen %d\n", p->blob_len);
69 print_hex_dump(KERN_INFO, "blob ", DUMP_PREFIX_NONE,
70 16, 1, p->blob, p->blob_len, 0);
71 pr_info("trusted_key: migratable %d\n", p->migratable);
72}
73
74static inline void dump_sess(struct osapsess *s)
75{
76 print_hex_dump(KERN_INFO, "trusted-key: handle ", DUMP_PREFIX_NONE,
77 16, 1, &s->handle, 4, 0);
78 pr_info("trusted-key: secret:\n");
79 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE,
80 16, 1, &s->secret, SHA1_DIGEST_SIZE, 0);
81 pr_info("trusted-key: enonce:\n");
82 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE,
83 16, 1, &s->enonce, SHA1_DIGEST_SIZE, 0);
84}
85
86static inline void dump_tpm_buf(unsigned char *buf)
87{
88 int len;
89
90 pr_info("\ntrusted-key: tpm buffer\n");
91 len = LOAD32(buf, TPM_SIZE_OFFSET);
92 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1, buf, len, 0);
93}
94#else
95static inline void dump_options(struct trusted_key_options *o)
96{
97}
98
99static inline void dump_payload(struct trusted_key_payload *p)
100{
101}
102
103static inline void dump_sess(struct osapsess *s)
104{
105}
106
107static inline void dump_tpm_buf(unsigned char *buf)
108{
109}
110#endif
Mimi Zohard00a1c72010-11-23 17:50:34 -0500111#endif