Skip to content

Commit 127fa2a

Browse files
committed
KEYS: trusted: Replace a redundant instance of tpm2_hash_map
'trusted_tpm2' duplicates 'tpm2_hash_map' originally part of the TPN driver, which is suboptimal. Implement and export `tpm2_find_hash_alg()` in the driver, and substitute the redundant code in 'trusted_tpm2' with a call to the new function. Reviewed-by: Jonathan McDowell <noodles@meta.com> Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
1 parent e1afacb commit 127fa2a

3 files changed

Lines changed: 18 additions & 20 deletions

File tree

drivers/char/tpm/tpm2-cmd.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,26 @@ static bool disable_pcr_integrity;
1818
module_param(disable_pcr_integrity, bool, 0444);
1919
MODULE_PARM_DESC(disable_pcr_integrity, "Disable integrity protection of TPM2_PCR_Extend");
2020

21-
static struct tpm2_hash tpm2_hash_map[] = {
21+
struct tpm2_hash tpm2_hash_map[] = {
2222
{HASH_ALGO_SHA1, TPM_ALG_SHA1},
2323
{HASH_ALGO_SHA256, TPM_ALG_SHA256},
2424
{HASH_ALGO_SHA384, TPM_ALG_SHA384},
2525
{HASH_ALGO_SHA512, TPM_ALG_SHA512},
2626
{HASH_ALGO_SM3_256, TPM_ALG_SM3_256},
2727
};
2828

29+
int tpm2_find_hash_alg(unsigned int crypto_id)
30+
{
31+
int i;
32+
33+
for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++)
34+
if (crypto_id == tpm2_hash_map[i].crypto_id)
35+
return tpm2_hash_map[i].tpm_id;
36+
37+
return -EINVAL;
38+
}
39+
EXPORT_SYMBOL_GPL(tpm2_find_hash_alg);
40+
2941
int tpm2_get_timeouts(struct tpm_chip *chip)
3042
{
3143
chip->timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);

include/linux/tpm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
473473
extern int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max);
474474
extern struct tpm_chip *tpm_default_chip(void);
475475
void tpm2_flush_context(struct tpm_chip *chip, u32 handle);
476+
int tpm2_find_hash_alg(unsigned int crypto_id);
476477

477478
static inline void tpm_buf_append_empty_auth(struct tpm_buf *buf, u32 handle)
478479
{

security/keys/trusted-keys/trusted_tpm2.c

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@
1818

1919
#include "tpm2key.asn1.h"
2020

21-
static struct tpm2_hash tpm2_hash_map[] = {
22-
{HASH_ALGO_SHA1, TPM_ALG_SHA1},
23-
{HASH_ALGO_SHA256, TPM_ALG_SHA256},
24-
{HASH_ALGO_SHA384, TPM_ALG_SHA384},
25-
{HASH_ALGO_SHA512, TPM_ALG_SHA512},
26-
{HASH_ALGO_SM3_256, TPM_ALG_SM3_256},
27-
};
28-
2921
static u32 tpm2key_oid[] = { 2, 23, 133, 10, 1, 5 };
3022

3123
static int tpm2_key_encode(struct trusted_key_payload *payload,
@@ -244,20 +236,13 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
244236
off_t offset = TPM_HEADER_SIZE;
245237
struct tpm_buf buf, sized;
246238
int blob_len = 0;
247-
u32 hash;
239+
int hash;
248240
u32 flags;
249-
int i;
250241
int rc;
251242

252-
for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
253-
if (options->hash == tpm2_hash_map[i].crypto_id) {
254-
hash = tpm2_hash_map[i].tpm_id;
255-
break;
256-
}
257-
}
258-
259-
if (i == ARRAY_SIZE(tpm2_hash_map))
260-
return -EINVAL;
243+
hash = tpm2_find_hash_alg(options->hash);
244+
if (hash < 0)
245+
return hash;
261246

262247
if (!options->keyhandle)
263248
return -EINVAL;

0 commit comments

Comments
 (0)