Skip to content

Commit da379f3

Browse files
author
Jarkko Sakkinen
committed
tpm: migrate pubek_show to struct tpm_buf
Migrated pubek_show to struct tpm_buf and cleaned up its implementation. Previously the output parameter structure was declared but left completely unused. Now it is used to refer different fields of the output. We can move it to tpm-sysfs.c as it does not have any use outside of that file. Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
1 parent 25eabb1 commit da379f3

File tree

2 files changed

+48
-52
lines changed

2 files changed

+48
-52
lines changed

drivers/char/tpm/tpm-sysfs.c

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,48 @@
2020
#include <linux/device.h>
2121
#include "tpm.h"
2222

23-
#define READ_PUBEK_RESULT_SIZE 314
23+
struct tpm_readpubek_out {
24+
u8 algorithm[4];
25+
u8 encscheme[2];
26+
u8 sigscheme[2];
27+
__be32 paramsize;
28+
u8 parameters[12];
29+
__be32 keysize;
30+
u8 modulus[256];
31+
u8 checksum[20];
32+
} __packed;
33+
2434
#define READ_PUBEK_RESULT_MIN_BODY_SIZE (28 + 256)
2535
#define TPM_ORD_READPUBEK 124
26-
static const struct tpm_input_header tpm_readpubek_header = {
27-
.tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
28-
.length = cpu_to_be32(30),
29-
.ordinal = cpu_to_be32(TPM_ORD_READPUBEK)
30-
};
36+
3137
static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
3238
char *buf)
3339
{
34-
u8 *data;
35-
struct tpm_cmd_t tpm_cmd;
36-
ssize_t err;
37-
int i, rc;
40+
struct tpm_buf tpm_buf;
41+
struct tpm_readpubek_out *out;
42+
ssize_t rc;
43+
int i;
3844
char *str = buf;
3945
struct tpm_chip *chip = to_tpm_chip(dev);
46+
char anti_replay[20];
4047

41-
memset(&tpm_cmd, 0, sizeof(tpm_cmd));
42-
43-
tpm_cmd.header.in = tpm_readpubek_header;
44-
err = tpm_transmit_cmd(chip, NULL, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
45-
READ_PUBEK_RESULT_MIN_BODY_SIZE, 0,
46-
"attempting to read the PUBEK");
47-
if (err)
48-
goto out;
49-
50-
/*
51-
ignore header 10 bytes
52-
algorithm 32 bits (1 == RSA )
53-
encscheme 16 bits
54-
sigscheme 16 bits
55-
parameters (RSA 12->bytes: keybit, #primes, expbit)
56-
keylenbytes 32 bits
57-
256 byte modulus
58-
ignore checksum 20 bytes
59-
*/
60-
data = tpm_cmd.params.readpubek_out_buffer;
48+
memset(&anti_replay, 0, sizeof(anti_replay));
49+
50+
rc = tpm_buf_init(&tpm_buf, TPM_TAG_RQU_COMMAND, TPM_ORD_READPUBEK);
51+
if (rc)
52+
return rc;
53+
54+
tpm_buf_append(&tpm_buf, anti_replay, sizeof(anti_replay));
55+
56+
rc = tpm_transmit_cmd(chip, NULL, tpm_buf.data, PAGE_SIZE,
57+
READ_PUBEK_RESULT_MIN_BODY_SIZE, 0,
58+
"attempting to read the PUBEK");
59+
if (rc) {
60+
tpm_buf_destroy(&tpm_buf);
61+
return 0;
62+
}
63+
64+
out = (struct tpm_readpubek_out *)&tpm_buf.data[10];
6165
str +=
6266
sprintf(str,
6367
"Algorithm: %02X %02X %02X %02X\n"
@@ -68,21 +72,26 @@ static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
6872
"%02X %02X %02X %02X\n"
6973
"Modulus length: %d\n"
7074
"Modulus:\n",
71-
data[0], data[1], data[2], data[3],
72-
data[4], data[5],
73-
data[6], data[7],
74-
data[12], data[13], data[14], data[15],
75-
data[16], data[17], data[18], data[19],
76-
data[20], data[21], data[22], data[23],
77-
be32_to_cpu(*((__be32 *) (data + 24))));
75+
out->algorithm[0], out->algorithm[1], out->algorithm[2],
76+
out->algorithm[3],
77+
out->encscheme[0], out->encscheme[1],
78+
out->sigscheme[0], out->sigscheme[1],
79+
out->parameters[0], out->parameters[1],
80+
out->parameters[2], out->parameters[3],
81+
out->parameters[4], out->parameters[5],
82+
out->parameters[6], out->parameters[7],
83+
out->parameters[8], out->parameters[9],
84+
out->parameters[10], out->parameters[11],
85+
be32_to_cpu(out->keysize));
7886

7987
for (i = 0; i < 256; i++) {
80-
str += sprintf(str, "%02X ", data[i + 28]);
88+
str += sprintf(str, "%02X ", out->modulus[i]);
8189
if ((i + 1) % 16 == 0)
8290
str += sprintf(str, "\n");
8391
}
84-
out:
92+
8593
rc = str - buf;
94+
tpm_buf_destroy(&tpm_buf);
8695
return rc;
8796
}
8897
static DEVICE_ATTR_RO(pubek);

drivers/char/tpm/tpm.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -345,17 +345,6 @@ enum tpm_sub_capabilities {
345345
TPM_CAP_PROP_TIS_DURATION = 0x120,
346346
};
347347

348-
struct tpm_readpubek_params_out {
349-
u8 algorithm[4];
350-
u8 encscheme[2];
351-
u8 sigscheme[2];
352-
__be32 paramsize;
353-
u8 parameters[12]; /*assuming RSA*/
354-
__be32 keysize;
355-
u8 modulus[256];
356-
u8 checksum[20];
357-
} __packed;
358-
359348
typedef union {
360349
struct tpm_input_header in;
361350
struct tpm_output_header out;
@@ -385,8 +374,6 @@ struct tpm_getrandom_in {
385374
} __packed;
386375

387376
typedef union {
388-
struct tpm_readpubek_params_out readpubek_out;
389-
u8 readpubek_out_buffer[sizeof(struct tpm_readpubek_params_out)];
390377
struct tpm_pcrread_in pcrread_in;
391378
struct tpm_pcrread_out pcrread_out;
392379
struct tpm_getrandom_in getrandom_in;

0 commit comments

Comments
 (0)