tpm: introduce struct tpm_buf
PATCH v3 1/1 tpm: introduce struct tpm_buf
This patch introduces struct tpm_buf that provides a string buffer for constructing TPM commands. This allows to construct variable sized TPM commands. This feature is needed for TPM 2.0 commands in order to allow policy authentication and algorithmic agility.
The commands in the tpm2-cmd.c have been updated to use struct tpm_buf. Lots of awkward length calculations could be dropped because the buffer knows its length.
The code is is along the lines of the string buffer code in security/trusted/trusted.h.
Changes since v1:
- Fixed potential alignment issues in tpm_buf_tag().
Changes since v2:
- Use __aligned(8) instead of __attribute__((aligned(8))) (checkpatch.pl)
- Check in tpm_buf_store() that the length in the header is not surpassed.
--- drivers/char/tpm/tpm.h | 89 ++++++++++++ drivers/char/tpm/tpm2-cmd.c | 321 ++++++++++++++++---------------------------- 2 files changed, 202 insertions(+), 208 deletions(-)
drivers/char/tpm/tpm.h +89 -0
@@ -382,6 +382,95 @@ struct tpm_cmd_t {tpm_cmd_params params; } __packed; +/* A string buffer type for constructing TPM commands. This is based on the +* code in security/keys/trusted.h. +*/ + +#define TPM_BUF_SIZE 512 + +struct tpm_buf { +u8 data[TPM_BUF_SIZE ]; +}__aligned(8) ; + +static inline void tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal) +{ +struct tpm_input_header *head = (struct tpm_input_header *) buf->data; + + head->tag =cpu_to_be16 (tag); + head->length =cpu_to_be32 (sizeof (*head)); + head->ordinal =cpu_to_be32 (ordinal); +} + +static inline u32 tpm_buf_length (struct tpm_buf *buf) +{ +struct tpm_input_header *head = (struct tpm_input_header *) buf->data; + +return be32_to_cpu (head->length); +} + +static inline u16 tpm_buf_tag (struct tpm_buf *buf) +{ +struct tpm_input_header *head = (struct tpm_input_header *) buf->data; + +return be16_to_cpu (head->tag); +} + +static inline u8 *tpm_buf_out (struct tpm_buf *buf) +{ +return &buf->data[TPM_HEADER_SIZE ]; +} + +static inline void tpm_buf_append (struct tpm_buf *buf, +const unsigned char *data, +unsigned int len) +{ +struct tpm_input_header *head = (struct tpm_input_header *) buf->data; + +BUG_ON ((len +tpm_buf_length (buf)) >TPM_BUF_SIZE ); + +memcpy (&buf->data[tpm_buf_length (buf)], data, len); + head->length =cpu_to_be32 (tpm_buf_length (buf) + len); +} + +static inline void tpm_buf_store (struct tpm_buf *buf, +unsigned int pos, +const unsigned char *data, +unsigned int len) +{ +/* Use separate checks in order catch the root cause easily. */ +BUG_ON ((pos + len) >TPM_BUF_SIZE ); +BUG_ON ((pos + len) >tpm_buf_length (buf)); + +memcpy (&buf->data[pos], data, len); +} + +static inline void tpm_buf_append_u8 (struct tpm_buf *buf,const u8 value) +{ +tpm_buf_append (buf, &value,1 ); +} + +static inline void tpm_buf_append_u16 (struct tpm_buf *buf,const u16 value) +{ +__be16 value2 =cpu_to_be16 (value); + +tpm_buf_append (buf, (u8 *) &value2,2 ); +} + +static inline void tpm_buf_append_u32 (struct tpm_buf *buf,const u32 value) +{ +__be32 value2 =cpu_to_be32 (value); + +tpm_buf_append (buf, (u8 *) &value2,4 ); +} + +static inline void tpm_buf_store_u32 (struct tpm_buf *buf,unsigned int pos, +const u32 value) +{ +__be32 value2 =cpu_to_be32 (value); + +tpm_buf_store (buf, pos, (u8 *) &value2,4 ); +} +extern struct class *tpm_class;extern dev_t tpm_devt;extern const struct file_operations tpm_fops;
drivers/char/tpm/tpm2-cmd.c +113 -208
486 lines not on this page · raw message
Signature · 1 line
2.1.4