/* Key creation commands long timeouts */
chip->duration[TPM_LONG_LONG] =
msecs_to_jiffies(TPM2_DURATION_LONG_LONG);
chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;
return 0;
}
/** * tpm2_ordinal_duration_index() - returns an index to the chip duration table * @ordinal: TPM command ordinal. * * The function returns an index to the chip duration table * (enum tpm_duration), that describes the maximum amount of * time the chip could take to return the result for a particular ordinal. * * The values of the MEDIUM, and LONG durations are taken * from the PC Client Profile (PTP) specification (750, 2000 msec) * * LONG_LONG is for commands that generates keys which empirically takes * a longer time on some systems. * * Return: * * TPM_MEDIUM * * TPM_LONG * * TPM_LONG_LONG * * TPM_UNDEFINED
*/ static u8 tpm2_ordinal_duration_index(u32 ordinal)
{ switch (ordinal) { /* Startup */ case TPM2_CC_STARTUP: /* 144 */ return TPM_MEDIUM;
case TPM2_CC_SELF_TEST: /* 143 */ return TPM_LONG;
case TPM2_CC_GET_RANDOM: /* 17B */ return TPM_LONG;
case TPM2_CC_SEQUENCE_UPDATE: /* 15C */ return TPM_MEDIUM; case TPM2_CC_SEQUENCE_COMPLETE: /* 13E */ return TPM_MEDIUM; case TPM2_CC_EVENT_SEQUENCE_COMPLETE: /* 185 */ return TPM_MEDIUM; case TPM2_CC_HASH_SEQUENCE_START: /* 186 */ return TPM_MEDIUM;
case TPM2_CC_VERIFY_SIGNATURE: /* 177 */ return TPM_LONG_LONG;
case TPM2_CC_PCR_EXTEND: /* 182 */ return TPM_MEDIUM;
case TPM2_CC_HIERARCHY_CONTROL: /* 121 */ return TPM_LONG; case TPM2_CC_HIERARCHY_CHANGE_AUTH: /* 129 */ return TPM_LONG;
case TPM2_CC_GET_CAPABILITY: /* 17A */ return TPM_MEDIUM;
case TPM2_CC_NV_READ: /* 14E */ return TPM_LONG;
case TPM2_CC_CREATE_PRIMARY: /* 131 */ return TPM_LONG_LONG; case TPM2_CC_CREATE: /* 153 */ return TPM_LONG_LONG; case TPM2_CC_CREATE_LOADED: /* 191 */ return TPM_LONG_LONG;
default: return TPM_UNDEFINED;
}
}
/** * tpm2_calc_ordinal_duration() - calculate the maximum command duration * @chip: TPM chip to use. * @ordinal: TPM command ordinal. * * The function returns the maximum amount of time the chip could take * to return the result for a particular ordinal in jiffies. * * Return: A maximal duration time for an ordinal in jiffies.
*/ unsignedlong tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
{ unsignedint index;
index = tpm2_ordinal_duration_index(ordinal);
if (index != TPM_UNDEFINED) return chip->duration[index]; else return msecs_to_jiffies(TPM2_DURATION_DEFAULT);
}
/** * tpm2_pcr_read() - read a PCR value * @chip: TPM chip to use. * @pcr_idx: index of the PCR to read. * @digest: PCR bank and buffer current PCR value is written to. * @digest_size_ptr: pointer to variable that stores the digest size. * * Return: Same as with tpm_transmit_cmd.
*/ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, struct tpm_digest *digest, u16 *digest_size_ptr)
{ int i; int rc; struct tpm_buf buf; struct tpm2_pcr_read_out *out;
u8 pcr_select[TPM2_PCR_SELECT_MIN] = {0};
u16 digest_size;
u16 expected_digest_size = 0;
if (pcr_idx >= TPM2_PLATFORM_PCR) return -EINVAL;
if (!digest_size_ptr) { for (i = 0; i < chip->nr_allocated_banks &&
chip->allocated_banks[i].alg_id != digest->alg_id; i++)
;
if (i == chip->nr_allocated_banks) return -EINVAL;
/** * tpm2_pcr_extend() - extend a PCR value * * @chip: TPM chip to use. * @pcr_idx: index of the PCR. * @digests: list of pcr banks and corresponding digest values to extend. * * Return: Same as with tpm_transmit_cmd.
*/ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, struct tpm_digest *digests)
{ struct tpm_buf buf; int rc; int i;
if (!disable_pcr_integrity) {
rc = tpm2_start_auth_session(chip); if (rc) return rc;
}
rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_EXTEND); if (rc) { if (!disable_pcr_integrity)
tpm2_end_auth_session(chip); return rc;
}
/** * tpm2_get_random() - get random bytes from the TPM RNG * * @chip: a &tpm_chip instance * @dest: destination buffer * @max: the max number of random bytes to pull * * Return: * size of the buffer on success, * -errno otherwise (positive TPM return codes are masked to -EIO)
*/ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
{ struct tpm2_get_random_out *out; struct tpm_header *head; struct tpm_buf buf;
u32 recd;
u32 num_bytes = max; int err; int total = 0; int retries = 5;
u8 *dest_ptr = dest;
off_t offset;
if (!num_bytes || max > TPM_MAX_RNG_DATA) return -EINVAL;
err = tpm2_start_auth_session(chip); if (err) return err;
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);
tpm2_end_auth_session(chip); return err;
}
/** * tpm2_flush_context() - execute a TPM2_FlushContext command * @chip: TPM chip to use * @handle: context handle
*/ void tpm2_flush_context(struct tpm_chip *chip, u32 handle)
{ struct tpm_buf buf; int rc;
rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT); if (rc) {
dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n",
handle); return;
}
/** * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property * @chip: a &tpm_chip instance * @property_id: property ID. * @value: output variable. * @desc: passed to 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, constchar *desc)
{ struct tpm2_get_cap_out *out; struct tpm_buf buf; int rc;
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, &buf, 0, NULL); if (!rc) {
out = (struct tpm2_get_cap_out *)
&buf.data[TPM_HEADER_SIZE]; /* * To prevent failing boot up of some systems, Infineon TPM2.0 * returns SUCCESS on TPM2_Startup in field upgrade mode. Also * the TPM2_Getcapability command returns a zero length list * in field upgrade mode.
*/ if (be32_to_cpu(out->property_cnt) > 0)
*value = be32_to_cpu(out->value); else
rc = -ENODATA;
}
tpm_buf_destroy(&buf); return rc;
}
EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
/** * 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: a &tpm_chip instance * @shutdown_type: TPM_SU_CLEAR or TPM_SU_STATE.
*/ void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
{ struct tpm_buf buf; int 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, &buf, 0, "stopping the TPM");
tpm_buf_destroy(&buf);
}
/** * tpm2_do_selftest() - ensure that all self tests have passed * * @chip: TPM chip to use * * Return: Same as with tpm_transmit_cmd. * * The TPM can either run all self tests synchronously and then return * RC_SUCCESS once all tests were successful. Or it can choose to run the tests * asynchronously and return RC_TESTING immediately while the self tests still * execute in the background. This function handles both cases and waits until * all tests have completed.
*/ staticint tpm2_do_selftest(struct tpm_chip *chip)
{ struct tpm_buf buf; int full; int rc;
for (full = 0; full < 2; full++) {
rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SELF_TEST); if (rc) return rc;
if (rc == TPM2_RC_TESTING)
rc = TPM2_RC_SUCCESS; if (rc == TPM2_RC_INITIALIZE || rc == TPM2_RC_SUCCESS) return rc;
}
return rc;
}
/** * tpm2_probe() - probe for the TPM 2.0 protocol * @chip: a &tpm_chip instance * * 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. * * Return: * 0 on success, * -errno otherwise
*/ int tpm2_probe(struct tpm_chip *chip)
{ struct tpm_header *out; struct tpm_buf buf; int rc;
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, TPM_PT_TOTAL_COMMANDS);
tpm_buf_append_u32(&buf, 1);
rc = tpm_transmit_cmd(chip, &buf, 0, NULL); /* We ignore TPM return codes on purpose. */ if (rc >= 0) {
out = (struct tpm_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);
/* * Avoid unnecessary PCR read operations to reduce overhead * and obtain identifiers of the crypto subsystem.
*/ for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) { enum hash_algo crypto_algo = tpm2_hash_map[i].crypto_id;
if (bank->alg_id != tpm2_hash_map[i].tpm_id) continue;
/** * tpm2_startup - turn on the TPM * @chip: TPM chip to use * * Normally the firmware should start the TPM. This function is provided as a * workaround if this does not happen. A legal case for this could be for * example when a TPM emulator is used. * * Return: same as tpm_transmit_cmd()
*/
staticint tpm2_startup(struct tpm_chip *chip)
{ struct tpm_buf buf; int rc;
dev_info(&chip->dev, "starting up the TPM manually\n");
tpm_buf_append_u16(&buf, TPM2_SU_CLEAR);
rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to start the TPM");
tpm_buf_destroy(&buf);
return rc;
}
/** * tpm2_auto_startup - Perform the standard automatic TPM initialization * sequence * @chip: TPM chip to use * * Returns 0 on success, < 0 in case of fatal error.
*/ int tpm2_auto_startup(struct tpm_chip *chip)
{ int rc;
out: /* * Infineon TPM in field upgrade mode will return no data for the number * of supported commands.
*/ if (rc == TPM2_RC_UPGRADE || rc == -ENODATA) {
dev_info(&chip->dev, "TPM in field upgrade mode, requires firmware upgrade\n");
chip->flags |= TPM_CHIP_FLAG_FIRMWARE_UPGRADE;
rc = 0;
}
if (rc > 0)
rc = -ENODEV; return rc;
}
int tpm2_find_cc(struct tpm_chip *chip, u32 cc)
{
u32 cc_mask; int i;
cc_mask = 1 << TPM2_CC_ATTR_VENDOR | GENMASK(15, 0); for (i = 0; i < chip->nr_commands; i++) if (cc == (chip->cc_attrs_tbl[i] & cc_mask)) return i;
return -1;
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.27 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.