Skip to content

Commit 954650e

Browse files
Jarkko SakkinenPeterHuewe
authored andcommitted
tpm: seal/unseal for TPM 2.0
Added tpm_trusted_seal() and tpm_trusted_unseal() API for sealing trusted keys. This patch implements basic sealing and unsealing functionality for TPM 2.0: * Seal with a parent key using a 20 byte auth value. * Unseal with a parent key using a 20 byte auth value. Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Signed-off-by: Peter Huewe <peterhuewe@gmx.de>
1 parent fe351e8 commit 954650e

5 files changed

Lines changed: 366 additions & 3 deletions

File tree

‎drivers/char/tpm/tpm-interface.c‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,30 @@ int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
665665
return rc;
666666
}
667667

668+
/**
669+
* tpm_is_tpm2 - is the chip a TPM2 chip?
670+
* @chip_num: tpm idx # or ANY
671+
*
672+
* Returns < 0 on error, and 1 or 0 on success depending whether the chip
673+
* is a TPM2 chip.
674+
*/
675+
int tpm_is_tpm2(u32 chip_num)
676+
{
677+
struct tpm_chip *chip;
678+
int rc;
679+
680+
chip = tpm_chip_find_get(chip_num);
681+
if (chip == NULL)
682+
return -ENODEV;
683+
684+
rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
685+
686+
tpm_chip_put(chip);
687+
688+
return rc;
689+
}
690+
EXPORT_SYMBOL_GPL(tpm_is_tpm2);
691+
668692
/**
669693
* tpm_pcr_read - read a pcr value
670694
* @chip_num: tpm idx # or ANY
@@ -1021,6 +1045,58 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
10211045
}
10221046
EXPORT_SYMBOL_GPL(tpm_get_random);
10231047

1048+
/**
1049+
* tpm_seal_trusted() - seal a trusted key
1050+
* @chip_num: A specific chip number for the request or TPM_ANY_NUM
1051+
* @options: authentication values and other options
1052+
* @payload: the key data in clear and encrypted form
1053+
*
1054+
* Returns < 0 on error and 0 on success. At the moment, only TPM 2.0 chips
1055+
* are supported.
1056+
*/
1057+
int tpm_seal_trusted(u32 chip_num, struct trusted_key_payload *payload,
1058+
struct trusted_key_options *options)
1059+
{
1060+
struct tpm_chip *chip;
1061+
int rc;
1062+
1063+
chip = tpm_chip_find_get(chip_num);
1064+
if (chip == NULL || !(chip->flags & TPM_CHIP_FLAG_TPM2))
1065+
return -ENODEV;
1066+
1067+
rc = tpm2_seal_trusted(chip, payload, options);
1068+
1069+
tpm_chip_put(chip);
1070+
return rc;
1071+
}
1072+
EXPORT_SYMBOL_GPL(tpm_seal_trusted);
1073+
1074+
/**
1075+
* tpm_unseal_trusted() - unseal a trusted key
1076+
* @chip_num: A specific chip number for the request or TPM_ANY_NUM
1077+
* @options: authentication values and other options
1078+
* @payload: the key data in clear and encrypted form
1079+
*
1080+
* Returns < 0 on error and 0 on success. At the moment, only TPM 2.0 chips
1081+
* are supported.
1082+
*/
1083+
int tpm_unseal_trusted(u32 chip_num, struct trusted_key_payload *payload,
1084+
struct trusted_key_options *options)
1085+
{
1086+
struct tpm_chip *chip;
1087+
int rc;
1088+
1089+
chip = tpm_chip_find_get(chip_num);
1090+
if (chip == NULL || !(chip->flags & TPM_CHIP_FLAG_TPM2))
1091+
return -ENODEV;
1092+
1093+
rc = tpm2_unseal_trusted(chip, payload, options);
1094+
1095+
tpm_chip_put(chip);
1096+
return rc;
1097+
}
1098+
EXPORT_SYMBOL_GPL(tpm_unseal_trusted);
1099+
10241100
static int __init tpm_init(void)
10251101
{
10261102
int rc;

‎drivers/char/tpm/tpm.h‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,20 @@ enum tpm2_return_codes {
9090

9191
enum tpm2_algorithms {
9292
TPM2_ALG_SHA1 = 0x0004,
93+
TPM2_ALG_KEYEDHASH = 0x0008,
94+
TPM2_ALG_SHA256 = 0x000B,
95+
TPM2_ALG_NULL = 0x0010
9396
};
9497

9598
enum tpm2_command_codes {
9699
TPM2_CC_FIRST = 0x011F,
97100
TPM2_CC_SELF_TEST = 0x0143,
98101
TPM2_CC_STARTUP = 0x0144,
99102
TPM2_CC_SHUTDOWN = 0x0145,
103+
TPM2_CC_CREATE = 0x0153,
104+
TPM2_CC_LOAD = 0x0157,
105+
TPM2_CC_UNSEAL = 0x015E,
106+
TPM2_CC_FLUSH_CONTEXT = 0x0165,
100107
TPM2_CC_GET_CAPABILITY = 0x017A,
101108
TPM2_CC_GET_RANDOM = 0x017B,
102109
TPM2_CC_PCR_READ = 0x017E,
@@ -407,7 +414,7 @@ struct tpm_buf {
407414
u8 *data;
408415
};
409416

410-
static inline void tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
417+
static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
411418
{
412419
struct tpm_input_header *head;
413420

@@ -527,6 +534,12 @@ static inline void tpm_add_ppi(struct tpm_chip *chip)
527534
int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
528535
int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash);
529536
int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max);
537+
int tpm2_seal_trusted(struct tpm_chip *chip,
538+
struct trusted_key_payload *payload,
539+
struct trusted_key_options *options);
540+
int tpm2_unseal_trusted(struct tpm_chip *chip,
541+
struct trusted_key_payload *payload,
542+
struct trusted_key_options *options);
530543
ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,
531544
u32 *value, const char *desc);
532545

0 commit comments

Comments
 (0)