Skip to content

Commit 74edff2

Browse files
b49020Jarkko Sakkinen
authored andcommitted
tpm: Move tpm_buf code to include/linux/
Move tpm_buf code to common include/linux/tpm.h header so that it can be reused via other subsystems like trusted keys etc. Also rename trusted keys and asymmetric keys usage of TPM 1.x buffer implementation to tpm1_buf to avoid any compilation errors. Suggested-by: Jarkko Sakkinen <[email protected]> Signed-off-by: Sumit Garg <[email protected]> Reviewed-by: Jerry Snitselaar <[email protected]> Reviewed-by: Jarkko Sakkinen <[email protected]> Tested-by: Jarkko Sakkinen <[email protected]> Signed-off-by: Jarkko Sakkinen <[email protected]>
1 parent 44abdb3 commit 74edff2

File tree

5 files changed

+230
-230
lines changed

5 files changed

+230
-230
lines changed

crypto/asymmetric_keys/asym_tpm.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
/*
3232
* Load a TPM key from the blob provided by userspace
3333
*/
34-
static int tpm_loadkey2(struct tpm_buf *tb,
34+
static int tpm_loadkey2(struct tpm1_buf *tb,
3535
uint32_t keyhandle, unsigned char *keyauth,
3636
const unsigned char *keyblob, int keybloblen,
3737
uint32_t *newhandle)
@@ -99,7 +99,7 @@ static int tpm_loadkey2(struct tpm_buf *tb,
9999
/*
100100
* Execute the FlushSpecific TPM command
101101
*/
102-
static int tpm_flushspecific(struct tpm_buf *tb, uint32_t handle)
102+
static int tpm_flushspecific(struct tpm1_buf *tb, uint32_t handle)
103103
{
104104
INIT_BUF(tb);
105105
store16(tb, TPM_TAG_RQU_COMMAND);
@@ -115,7 +115,7 @@ static int tpm_flushspecific(struct tpm_buf *tb, uint32_t handle)
115115
* Decrypt a blob provided by userspace using a specific key handle.
116116
* The handle is a well known handle or previously loaded by e.g. LoadKey2
117117
*/
118-
static int tpm_unbind(struct tpm_buf *tb,
118+
static int tpm_unbind(struct tpm1_buf *tb,
119119
uint32_t keyhandle, unsigned char *keyauth,
120120
const unsigned char *blob, uint32_t bloblen,
121121
void *out, uint32_t outlen)
@@ -201,7 +201,7 @@ static int tpm_unbind(struct tpm_buf *tb,
201201
* up to key_length_in_bytes - 11 and not be limited to size 20 like the
202202
* TPM_SS_RSASSAPKCS1v15_SHA1 signature scheme.
203203
*/
204-
static int tpm_sign(struct tpm_buf *tb,
204+
static int tpm_sign(struct tpm1_buf *tb,
205205
uint32_t keyhandle, unsigned char *keyauth,
206206
const unsigned char *blob, uint32_t bloblen,
207207
void *out, uint32_t outlen)
@@ -519,7 +519,7 @@ static int tpm_key_decrypt(struct tpm_key *tk,
519519
struct kernel_pkey_params *params,
520520
const void *in, void *out)
521521
{
522-
struct tpm_buf *tb;
522+
struct tpm1_buf *tb;
523523
uint32_t keyhandle;
524524
uint8_t srkauth[SHA1_DIGEST_SIZE];
525525
uint8_t keyauth[SHA1_DIGEST_SIZE];
@@ -643,7 +643,7 @@ static int tpm_key_sign(struct tpm_key *tk,
643643
struct kernel_pkey_params *params,
644644
const void *in, void *out)
645645
{
646-
struct tpm_buf *tb;
646+
struct tpm1_buf *tb;
647647
uint32_t keyhandle;
648648
uint8_t srkauth[SHA1_DIGEST_SIZE];
649649
uint8_t keyauth[SHA1_DIGEST_SIZE];

drivers/char/tpm/tpm.h

Lines changed: 0 additions & 212 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include <linux/platform_device.h>
2626
#include <linux/io.h>
2727
#include <linux/tpm.h>
28-
#include <linux/highmem.h>
2928
#include <linux/tpm_eventlog.h>
3029

3130
#ifdef CONFIG_X86
@@ -58,124 +57,6 @@ enum tpm_addr {
5857
#define TPM_ERR_DISABLED 0x7
5958
#define TPM_ERR_INVALID_POSTINIT 38
6059

61-
#define TPM_HEADER_SIZE 10
62-
63-
enum tpm2_const {
64-
TPM2_PLATFORM_PCR = 24,
65-
TPM2_PCR_SELECT_MIN = ((TPM2_PLATFORM_PCR + 7) / 8),
66-
};
67-
68-
enum tpm2_timeouts {
69-
TPM2_TIMEOUT_A = 750,
70-
TPM2_TIMEOUT_B = 2000,
71-
TPM2_TIMEOUT_C = 200,
72-
TPM2_TIMEOUT_D = 30,
73-
TPM2_DURATION_SHORT = 20,
74-
TPM2_DURATION_MEDIUM = 750,
75-
TPM2_DURATION_LONG = 2000,
76-
TPM2_DURATION_LONG_LONG = 300000,
77-
TPM2_DURATION_DEFAULT = 120000,
78-
};
79-
80-
enum tpm2_structures {
81-
TPM2_ST_NO_SESSIONS = 0x8001,
82-
TPM2_ST_SESSIONS = 0x8002,
83-
};
84-
85-
/* Indicates from what layer of the software stack the error comes from */
86-
#define TSS2_RC_LAYER_SHIFT 16
87-
#define TSS2_RESMGR_TPM_RC_LAYER (11 << TSS2_RC_LAYER_SHIFT)
88-
89-
enum tpm2_return_codes {
90-
TPM2_RC_SUCCESS = 0x0000,
91-
TPM2_RC_HASH = 0x0083, /* RC_FMT1 */
92-
TPM2_RC_HANDLE = 0x008B,
93-
TPM2_RC_INITIALIZE = 0x0100, /* RC_VER1 */
94-
TPM2_RC_FAILURE = 0x0101,
95-
TPM2_RC_DISABLED = 0x0120,
96-
TPM2_RC_COMMAND_CODE = 0x0143,
97-
TPM2_RC_TESTING = 0x090A, /* RC_WARN */
98-
TPM2_RC_REFERENCE_H0 = 0x0910,
99-
TPM2_RC_RETRY = 0x0922,
100-
};
101-
102-
enum tpm2_command_codes {
103-
TPM2_CC_FIRST = 0x011F,
104-
TPM2_CC_HIERARCHY_CONTROL = 0x0121,
105-
TPM2_CC_HIERARCHY_CHANGE_AUTH = 0x0129,
106-
TPM2_CC_CREATE_PRIMARY = 0x0131,
107-
TPM2_CC_SEQUENCE_COMPLETE = 0x013E,
108-
TPM2_CC_SELF_TEST = 0x0143,
109-
TPM2_CC_STARTUP = 0x0144,
110-
TPM2_CC_SHUTDOWN = 0x0145,
111-
TPM2_CC_NV_READ = 0x014E,
112-
TPM2_CC_CREATE = 0x0153,
113-
TPM2_CC_LOAD = 0x0157,
114-
TPM2_CC_SEQUENCE_UPDATE = 0x015C,
115-
TPM2_CC_UNSEAL = 0x015E,
116-
TPM2_CC_CONTEXT_LOAD = 0x0161,
117-
TPM2_CC_CONTEXT_SAVE = 0x0162,
118-
TPM2_CC_FLUSH_CONTEXT = 0x0165,
119-
TPM2_CC_VERIFY_SIGNATURE = 0x0177,
120-
TPM2_CC_GET_CAPABILITY = 0x017A,
121-
TPM2_CC_GET_RANDOM = 0x017B,
122-
TPM2_CC_PCR_READ = 0x017E,
123-
TPM2_CC_PCR_EXTEND = 0x0182,
124-
TPM2_CC_EVENT_SEQUENCE_COMPLETE = 0x0185,
125-
TPM2_CC_HASH_SEQUENCE_START = 0x0186,
126-
TPM2_CC_CREATE_LOADED = 0x0191,
127-
TPM2_CC_LAST = 0x0193, /* Spec 1.36 */
128-
};
129-
130-
enum tpm2_permanent_handles {
131-
TPM2_RS_PW = 0x40000009,
132-
};
133-
134-
enum tpm2_capabilities {
135-
TPM2_CAP_HANDLES = 1,
136-
TPM2_CAP_COMMANDS = 2,
137-
TPM2_CAP_PCRS = 5,
138-
TPM2_CAP_TPM_PROPERTIES = 6,
139-
};
140-
141-
enum tpm2_properties {
142-
TPM_PT_TOTAL_COMMANDS = 0x0129,
143-
};
144-
145-
enum tpm2_startup_types {
146-
TPM2_SU_CLEAR = 0x0000,
147-
TPM2_SU_STATE = 0x0001,
148-
};
149-
150-
enum tpm2_cc_attrs {
151-
TPM2_CC_ATTR_CHANDLES = 25,
152-
TPM2_CC_ATTR_RHANDLE = 28,
153-
};
154-
155-
#define TPM_VID_INTEL 0x8086
156-
#define TPM_VID_WINBOND 0x1050
157-
#define TPM_VID_STM 0x104A
158-
159-
enum tpm_chip_flags {
160-
TPM_CHIP_FLAG_TPM2 = BIT(1),
161-
TPM_CHIP_FLAG_IRQ = BIT(2),
162-
TPM_CHIP_FLAG_VIRTUAL = BIT(3),
163-
TPM_CHIP_FLAG_HAVE_TIMEOUTS = BIT(4),
164-
TPM_CHIP_FLAG_ALWAYS_POWERED = BIT(5),
165-
TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED = BIT(6),
166-
};
167-
168-
#define to_tpm_chip(d) container_of(d, struct tpm_chip, dev)
169-
170-
struct tpm_header {
171-
__be16 tag;
172-
__be32 length;
173-
union {
174-
__be32 ordinal;
175-
__be32 return_code;
176-
};
177-
} __packed;
178-
17960
#define TPM_TAG_RQU_COMMAND 193
18061

18162
struct stclear_flags_t {
@@ -272,99 +153,6 @@ enum tpm_sub_capabilities {
272153
* compiler warnings about stack frame size. */
273154
#define TPM_MAX_RNG_DATA 128
274155

275-
/* A string buffer type for constructing TPM commands. This is based on the
276-
* ideas of string buffer code in security/keys/trusted.h but is heap based
277-
* in order to keep the stack usage minimal.
278-
*/
279-
280-
enum tpm_buf_flags {
281-
TPM_BUF_OVERFLOW = BIT(0),
282-
};
283-
284-
struct tpm_buf {
285-
unsigned int flags;
286-
u8 *data;
287-
};
288-
289-
static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)
290-
{
291-
struct tpm_header *head = (struct tpm_header *)buf->data;
292-
293-
head->tag = cpu_to_be16(tag);
294-
head->length = cpu_to_be32(sizeof(*head));
295-
head->ordinal = cpu_to_be32(ordinal);
296-
}
297-
298-
static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
299-
{
300-
buf->data = (u8 *)__get_free_page(GFP_KERNEL);
301-
if (!buf->data)
302-
return -ENOMEM;
303-
304-
buf->flags = 0;
305-
tpm_buf_reset(buf, tag, ordinal);
306-
return 0;
307-
}
308-
309-
static inline void tpm_buf_destroy(struct tpm_buf *buf)
310-
{
311-
free_page((unsigned long)buf->data);
312-
}
313-
314-
static inline u32 tpm_buf_length(struct tpm_buf *buf)
315-
{
316-
struct tpm_header *head = (struct tpm_header *)buf->data;
317-
318-
return be32_to_cpu(head->length);
319-
}
320-
321-
static inline u16 tpm_buf_tag(struct tpm_buf *buf)
322-
{
323-
struct tpm_header *head = (struct tpm_header *)buf->data;
324-
325-
return be16_to_cpu(head->tag);
326-
}
327-
328-
static inline void tpm_buf_append(struct tpm_buf *buf,
329-
const unsigned char *new_data,
330-
unsigned int new_len)
331-
{
332-
struct tpm_header *head = (struct tpm_header *)buf->data;
333-
u32 len = tpm_buf_length(buf);
334-
335-
/* Return silently if overflow has already happened. */
336-
if (buf->flags & TPM_BUF_OVERFLOW)
337-
return;
338-
339-
if ((len + new_len) > PAGE_SIZE) {
340-
WARN(1, "tpm_buf: overflow\n");
341-
buf->flags |= TPM_BUF_OVERFLOW;
342-
return;
343-
}
344-
345-
memcpy(&buf->data[len], new_data, new_len);
346-
head->length = cpu_to_be32(len + new_len);
347-
}
348-
349-
static inline void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value)
350-
{
351-
tpm_buf_append(buf, &value, 1);
352-
}
353-
354-
static inline void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value)
355-
{
356-
__be16 value2 = cpu_to_be16(value);
357-
358-
tpm_buf_append(buf, (u8 *) &value2, 2);
359-
}
360-
361-
static inline void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value)
362-
{
363-
__be32 value2 = cpu_to_be32(value);
364-
365-
tpm_buf_append(buf, (u8 *) &value2, 4);
366-
}
367-
368156
extern struct class *tpm_class;
369157
extern struct class *tpmrm_class;
370158
extern dev_t tpm_devt;

include/keys/trusted.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#define LOAD32N(buffer, offset) (*(uint32_t *)&buffer[offset])
1818
#define LOAD16(buffer, offset) (ntohs(*(uint16_t *)&buffer[offset]))
1919

20-
struct tpm_buf {
20+
struct tpm1_buf {
2121
int len;
2222
unsigned char data[MAX_BUF_SIZE];
2323
};
@@ -46,7 +46,7 @@ int TSS_checkhmac1(unsigned char *buffer,
4646
unsigned int keylen, ...);
4747

4848
int trusted_tpm_send(unsigned char *cmd, size_t buflen);
49-
int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce);
49+
int oiap(struct tpm1_buf *tb, uint32_t *handle, unsigned char *nonce);
5050

5151
#define TPM_DEBUG 0
5252

@@ -110,24 +110,24 @@ static inline void dump_tpm_buf(unsigned char *buf)
110110
}
111111
#endif
112112

113-
static inline void store8(struct tpm_buf *buf, const unsigned char value)
113+
static inline void store8(struct tpm1_buf *buf, const unsigned char value)
114114
{
115115
buf->data[buf->len++] = value;
116116
}
117117

118-
static inline void store16(struct tpm_buf *buf, const uint16_t value)
118+
static inline void store16(struct tpm1_buf *buf, const uint16_t value)
119119
{
120120
*(uint16_t *) & buf->data[buf->len] = htons(value);
121121
buf->len += sizeof value;
122122
}
123123

124-
static inline void store32(struct tpm_buf *buf, const uint32_t value)
124+
static inline void store32(struct tpm1_buf *buf, const uint32_t value)
125125
{
126126
*(uint32_t *) & buf->data[buf->len] = htonl(value);
127127
buf->len += sizeof value;
128128
}
129129

130-
static inline void storebytes(struct tpm_buf *buf, const unsigned char *in,
130+
static inline void storebytes(struct tpm1_buf *buf, const unsigned char *in,
131131
const int len)
132132
{
133133
memcpy(buf->data + buf->len, in, len);

0 commit comments

Comments
 (0)