|
|
@@ -27,46 +27,6 @@ enum tpm2_session_attributes {
|
|
|
TPM2_SA_CONTINUE_SESSION = BIT(0),
|
|
|
};
|
|
|
|
|
|
-struct tpm2_startup_in {
|
|
|
- __be16 startup_type;
|
|
|
-} __packed;
|
|
|
-
|
|
|
-struct tpm2_get_tpm_pt_in {
|
|
|
- __be32 cap_id;
|
|
|
- __be32 property_id;
|
|
|
- __be32 property_cnt;
|
|
|
-} __packed;
|
|
|
-
|
|
|
-struct tpm2_get_tpm_pt_out {
|
|
|
- u8 more_data;
|
|
|
- __be32 subcap_id;
|
|
|
- __be32 property_cnt;
|
|
|
- __be32 property_id;
|
|
|
- __be32 value;
|
|
|
-} __packed;
|
|
|
-
|
|
|
-struct tpm2_get_random_in {
|
|
|
- __be16 size;
|
|
|
-} __packed;
|
|
|
-
|
|
|
-struct tpm2_get_random_out {
|
|
|
- __be16 size;
|
|
|
- u8 buffer[TPM_MAX_RNG_DATA];
|
|
|
-} __packed;
|
|
|
-
|
|
|
-union tpm2_cmd_params {
|
|
|
- struct tpm2_startup_in startup_in;
|
|
|
- struct tpm2_get_tpm_pt_in get_tpm_pt_in;
|
|
|
- struct tpm2_get_tpm_pt_out get_tpm_pt_out;
|
|
|
- struct tpm2_get_random_in getrandom_in;
|
|
|
- struct tpm2_get_random_out getrandom_out;
|
|
|
-};
|
|
|
-
|
|
|
-struct tpm2_cmd {
|
|
|
- tpm_cmd_header header;
|
|
|
- union tpm2_cmd_params params;
|
|
|
-} __packed;
|
|
|
-
|
|
|
struct tpm2_hash {
|
|
|
unsigned int crypto_id;
|
|
|
unsigned int tpm_id;
|
|
|
@@ -321,82 +281,72 @@ int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count,
|
|
|
}
|
|
|
|
|
|
|
|
|
-#define TPM2_GETRANDOM_IN_SIZE \
|
|
|
- (sizeof(struct tpm_input_header) + \
|
|
|
- sizeof(struct tpm2_get_random_in))
|
|
|
-
|
|
|
-static const struct tpm_input_header tpm2_getrandom_header = {
|
|
|
- .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
|
|
|
- .length = cpu_to_be32(TPM2_GETRANDOM_IN_SIZE),
|
|
|
- .ordinal = cpu_to_be32(TPM2_CC_GET_RANDOM)
|
|
|
-};
|
|
|
+struct tpm2_get_random_out {
|
|
|
+ __be16 size;
|
|
|
+ u8 buffer[TPM_MAX_RNG_DATA];
|
|
|
+} __packed;
|
|
|
|
|
|
/**
|
|
|
* tpm2_get_random() - get random bytes from the TPM RNG
|
|
|
*
|
|
|
- * @chip: TPM chip to use
|
|
|
- * @out: destination buffer for the random bytes
|
|
|
- * @max: the max number of bytes to write to @out
|
|
|
+ * @chip: a &tpm_chip instance
|
|
|
+ * @dest: destination buffer
|
|
|
+ * @max: the max number of random bytes to pull
|
|
|
*
|
|
|
* Return:
|
|
|
- * Size of the output buffer, or -EIO on error.
|
|
|
+ * size of the buffer on success,
|
|
|
+ * -errno otherwise
|
|
|
*/
|
|
|
-int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max)
|
|
|
+int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
|
|
|
{
|
|
|
- struct tpm2_cmd cmd;
|
|
|
- u32 recd, rlength;
|
|
|
- u32 num_bytes;
|
|
|
+ struct tpm2_get_random_out *out;
|
|
|
+ struct tpm_buf buf;
|
|
|
+ u32 recd;
|
|
|
+ u32 num_bytes = max;
|
|
|
int err;
|
|
|
int total = 0;
|
|
|
int retries = 5;
|
|
|
- u8 *dest = out;
|
|
|
+ u8 *dest_ptr = dest;
|
|
|
|
|
|
- num_bytes = min_t(u32, max, sizeof(cmd.params.getrandom_out.buffer));
|
|
|
-
|
|
|
- if (!out || !num_bytes ||
|
|
|
- max > sizeof(cmd.params.getrandom_out.buffer))
|
|
|
+ if (!num_bytes || max > TPM_MAX_RNG_DATA)
|
|
|
return -EINVAL;
|
|
|
|
|
|
- do {
|
|
|
- cmd.header.in = tpm2_getrandom_header;
|
|
|
- cmd.params.getrandom_in.size = cpu_to_be16(num_bytes);
|
|
|
+ err = tpm_buf_init(&buf, 0, 0);
|
|
|
+ if (err)
|
|
|
+ return err;
|
|
|
|
|
|
- err = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd),
|
|
|
+ do {
|
|
|
+ tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_RANDOM);
|
|
|
+ tpm_buf_append_u16(&buf, num_bytes);
|
|
|
+ err = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
|
|
|
offsetof(struct tpm2_get_random_out,
|
|
|
buffer),
|
|
|
0, "attempting get random");
|
|
|
if (err)
|
|
|
- break;
|
|
|
+ goto out;
|
|
|
|
|
|
- recd = min_t(u32, be16_to_cpu(cmd.params.getrandom_out.size),
|
|
|
- num_bytes);
|
|
|
- rlength = be32_to_cpu(cmd.header.out.length);
|
|
|
- if (rlength < offsetof(struct tpm2_get_random_out, buffer) +
|
|
|
- recd)
|
|
|
- return -EFAULT;
|
|
|
- memcpy(dest, cmd.params.getrandom_out.buffer, recd);
|
|
|
+ out = (struct tpm2_get_random_out *)
|
|
|
+ &buf.data[TPM_HEADER_SIZE];
|
|
|
+ recd = min_t(u32, be16_to_cpu(out->size), num_bytes);
|
|
|
+ if (tpm_buf_length(&buf) <
|
|
|
+ offsetof(struct tpm2_get_random_out, buffer) + recd) {
|
|
|
+ err = -EFAULT;
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+ memcpy(dest_ptr, out->buffer, recd);
|
|
|
|
|
|
- dest += recd;
|
|
|
+ dest_ptr += recd;
|
|
|
total += recd;
|
|
|
num_bytes -= recd;
|
|
|
} while (retries-- && total < max);
|
|
|
|
|
|
+ tpm_buf_destroy(&buf);
|
|
|
return total ? total : -EIO;
|
|
|
+out:
|
|
|
+ tpm_buf_destroy(&buf);
|
|
|
+ return err;
|
|
|
}
|
|
|
|
|
|
-#define TPM2_GET_TPM_PT_IN_SIZE \
|
|
|
- (sizeof(struct tpm_input_header) + \
|
|
|
- sizeof(struct tpm2_get_tpm_pt_in))
|
|
|
-
|
|
|
-#define TPM2_GET_TPM_PT_OUT_BODY_SIZE \
|
|
|
- sizeof(struct tpm2_get_tpm_pt_out)
|
|
|
-
|
|
|
-static const struct tpm_input_header tpm2_get_tpm_pt_header = {
|
|
|
- .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
|
|
|
- .length = cpu_to_be32(TPM2_GET_TPM_PT_IN_SIZE),
|
|
|
- .ordinal = cpu_to_be32(TPM2_CC_GET_CAPABILITY)
|
|
|
-};
|
|
|
-
|
|
|
/**
|
|
|
* tpm2_flush_context_cmd() - execute a TPM2_FlushContext command
|
|
|
* @chip: TPM chip to use
|
|
|
@@ -471,7 +421,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
|
|
|
{
|
|
|
unsigned int blob_len;
|
|
|
struct tpm_buf buf;
|
|
|
- u32 hash, rlength;
|
|
|
+ u32 hash;
|
|
|
int i;
|
|
|
int rc;
|
|
|
|
|
|
@@ -546,8 +496,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
|
|
|
rc = -E2BIG;
|
|
|
goto out;
|
|
|
}
|
|
|
- rlength = be32_to_cpu(((struct tpm2_cmd *)&buf)->header.out.length);
|
|
|
- if (rlength < TPM_HEADER_SIZE + 4 + blob_len) {
|
|
|
+ if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 4 + blob_len) {
|
|
|
rc = -EFAULT;
|
|
|
goto out;
|
|
|
}
|
|
|
@@ -657,7 +606,6 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
|
|
|
u16 data_len;
|
|
|
u8 *data;
|
|
|
int rc;
|
|
|
- u32 rlength;
|
|
|
|
|
|
rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
|
|
|
if (rc)
|
|
|
@@ -685,9 +633,7 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
|
|
|
goto out;
|
|
|
}
|
|
|
|
|
|
- rlength = be32_to_cpu(((struct tpm2_cmd *)&buf)
|
|
|
- ->header.out.length);
|
|
|
- if (rlength < TPM_HEADER_SIZE + 6 + data_len) {
|
|
|
+ if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 6 + data_len) {
|
|
|
rc = -EFAULT;
|
|
|
goto out;
|
|
|
}
|
|
|
@@ -733,69 +679,71 @@ out:
|
|
|
return rc;
|
|
|
}
|
|
|
|
|
|
+struct tpm2_get_cap_out {
|
|
|
+ u8 more_data;
|
|
|
+ __be32 subcap_id;
|
|
|
+ __be32 property_cnt;
|
|
|
+ __be32 property_id;
|
|
|
+ __be32 value;
|
|
|
+} __packed;
|
|
|
+
|
|
|
/**
|
|
|
* tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
|
|
|
- * @chip: TPM chip to use.
|
|
|
+ * @chip: a &tpm_chip instance
|
|
|
* @property_id: property ID.
|
|
|
* @value: output variable.
|
|
|
* @desc: passed to tpm_transmit_cmd()
|
|
|
*
|
|
|
- * Return: Same as with tpm_transmit_cmd.
|
|
|
+ * Return:
|
|
|
+ * 0 on success,
|
|
|
+ * -errno or a TPM return code otherwise
|
|
|
*/
|
|
|
ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,
|
|
|
const char *desc)
|
|
|
{
|
|
|
- struct tpm2_cmd cmd;
|
|
|
+ struct tpm2_get_cap_out *out;
|
|
|
+ struct tpm_buf buf;
|
|
|
int rc;
|
|
|
|
|
|
- cmd.header.in = tpm2_get_tpm_pt_header;
|
|
|
- cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
|
|
|
- cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(property_id);
|
|
|
- cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
|
|
|
-
|
|
|
- rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd),
|
|
|
- TPM2_GET_TPM_PT_OUT_BODY_SIZE, 0, desc);
|
|
|
- if (!rc)
|
|
|
- *value = be32_to_cpu(cmd.params.get_tpm_pt_out.value);
|
|
|
-
|
|
|
+ rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
|
|
|
+ if (rc)
|
|
|
+ return rc;
|
|
|
+ tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
|
|
|
+ tpm_buf_append_u32(&buf, property_id);
|
|
|
+ tpm_buf_append_u32(&buf, 1);
|
|
|
+ rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, NULL);
|
|
|
+ if (!rc) {
|
|
|
+ out = (struct tpm2_get_cap_out *)
|
|
|
+ &buf.data[TPM_HEADER_SIZE];
|
|
|
+ *value = be32_to_cpu(out->value);
|
|
|
+ }
|
|
|
+ tpm_buf_destroy(&buf);
|
|
|
return rc;
|
|
|
}
|
|
|
EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
|
|
|
|
|
|
-#define TPM2_SHUTDOWN_IN_SIZE \
|
|
|
- (sizeof(struct tpm_input_header) + \
|
|
|
- sizeof(struct tpm2_startup_in))
|
|
|
-
|
|
|
-static const struct tpm_input_header tpm2_shutdown_header = {
|
|
|
- .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
|
|
|
- .length = cpu_to_be32(TPM2_SHUTDOWN_IN_SIZE),
|
|
|
- .ordinal = cpu_to_be32(TPM2_CC_SHUTDOWN)
|
|
|
-};
|
|
|
-
|
|
|
/**
|
|
|
- * tpm2_shutdown() - send shutdown command to the TPM chip
|
|
|
+ * tpm2_shutdown() - send a TPM shutdown command
|
|
|
+ *
|
|
|
+ * Sends a TPM shutdown command. The shutdown command is used in call
|
|
|
+ * sites where the system is going down. If it fails, there is not much
|
|
|
+ * that can be done except print an error message.
|
|
|
*
|
|
|
- * @chip: TPM chip to use.
|
|
|
- * @shutdown_type: shutdown type. The value is either
|
|
|
- * TPM_SU_CLEAR or TPM_SU_STATE.
|
|
|
+ * @chip: a &tpm_chip instance
|
|
|
+ * @shutdown_type: TPM_SU_CLEAR or TPM_SU_STATE.
|
|
|
*/
|
|
|
void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
|
|
|
{
|
|
|
- struct tpm2_cmd cmd;
|
|
|
+ struct tpm_buf buf;
|
|
|
int rc;
|
|
|
|
|
|
- cmd.header.in = tpm2_shutdown_header;
|
|
|
- cmd.params.startup_in.startup_type = cpu_to_be16(shutdown_type);
|
|
|
-
|
|
|
- rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0,
|
|
|
- "stopping the TPM");
|
|
|
-
|
|
|
- /* In places where shutdown command is sent there's no much we can do
|
|
|
- * except print the error code on a system failure.
|
|
|
- */
|
|
|
- if (rc < 0 && rc != -EPIPE)
|
|
|
- dev_warn(&chip->dev, "transmit returned %d while stopping the TPM",
|
|
|
- rc);
|
|
|
+ rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SHUTDOWN);
|
|
|
+ if (rc)
|
|
|
+ return;
|
|
|
+ tpm_buf_append_u16(&buf, shutdown_type);
|
|
|
+ tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
|
|
|
+ "stopping the TPM");
|
|
|
+ tpm_buf_destroy(&buf);
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
@@ -863,31 +811,37 @@ static int tpm2_do_selftest(struct tpm_chip *chip)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * tpm2_probe() - probe TPM 2.0
|
|
|
- * @chip: TPM chip to use
|
|
|
+ * tpm2_probe() - probe for the TPM 2.0 protocol
|
|
|
+ * @chip: a &tpm_chip instance
|
|
|
*
|
|
|
- * Return: < 0 error and 0 on success.
|
|
|
+ * Send an idempotent TPM 2.0 command and see whether there is TPM2 chip in the
|
|
|
+ * other end based on the response tag. The flag TPM_CHIP_FLAG_TPM2 is set by
|
|
|
+ * this function if this is the case.
|
|
|
*
|
|
|
- * Send idempotent TPM 2.0 command and see whether TPM 2.0 chip replied based on
|
|
|
- * the reply tag.
|
|
|
+ * Return:
|
|
|
+ * 0 on success,
|
|
|
+ * -errno otherwise
|
|
|
*/
|
|
|
int tpm2_probe(struct tpm_chip *chip)
|
|
|
{
|
|
|
- struct tpm2_cmd cmd;
|
|
|
+ struct tpm_output_header *out;
|
|
|
+ struct tpm_buf buf;
|
|
|
int rc;
|
|
|
|
|
|
- cmd.header.in = tpm2_get_tpm_pt_header;
|
|
|
- cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
|
|
|
- cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(0x100);
|
|
|
- cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
|
|
|
-
|
|
|
- rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0, NULL);
|
|
|
- if (rc < 0)
|
|
|
+ rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
|
|
|
+ if (rc)
|
|
|
return rc;
|
|
|
-
|
|
|
- if (be16_to_cpu(cmd.header.out.tag) == TPM2_ST_NO_SESSIONS)
|
|
|
- chip->flags |= TPM_CHIP_FLAG_TPM2;
|
|
|
-
|
|
|
+ tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
|
|
|
+ tpm_buf_append_u32(&buf, TPM_PT_TOTAL_COMMANDS);
|
|
|
+ tpm_buf_append_u32(&buf, 1);
|
|
|
+ rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, NULL);
|
|
|
+ /* We ignore TPM return codes on purpose. */
|
|
|
+ if (rc >= 0) {
|
|
|
+ out = (struct tpm_output_header *)buf.data;
|
|
|
+ if (be16_to_cpu(out->tag) == TPM2_ST_NO_SESSIONS)
|
|
|
+ chip->flags |= TPM_CHIP_FLAG_TPM2;
|
|
|
+ }
|
|
|
+ tpm_buf_destroy(&buf);
|
|
|
return 0;
|
|
|
}
|
|
|
EXPORT_SYMBOL_GPL(tpm2_probe);
|