Skip to content

Commit b4e5bb5

Browse files
committed
Merge tag 'keys-next-6.18' of git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd
Pull keys updates from Jarkko Sakkinen: "A few minor updates/fixes for keys" * tag 'keys-next-6.18' of git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd: security: keys: use menuconfig for KEYS symbol KEYS: encrypted: Use SHA-256 library instead of crypto_shash KEYS: trusted_tpm1: Move private functionality out of public header KEYS: trusted_tpm1: Use SHA-1 library instead of crypto_shash KEYS: trusted_tpm1: Compare HMAC values in constant time
2 parents 908057d + 8be70a8 commit b4e5bb5

File tree

5 files changed

+117
-331
lines changed

5 files changed

+117
-331
lines changed

include/keys/trusted_tpm.h

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -5,92 +5,13 @@
55
#include <keys/trusted-type.h>
66
#include <linux/tpm_command.h>
77

8-
/* implementation specific TPM constants */
9-
#define TPM_SIZE_OFFSET 2
10-
#define TPM_RETURN_OFFSET 6
11-
#define TPM_DATA_OFFSET 10
12-
13-
#define LOAD32(buffer, offset) (ntohl(*(uint32_t *)&buffer[offset]))
14-
#define LOAD32N(buffer, offset) (*(uint32_t *)&buffer[offset])
15-
#define LOAD16(buffer, offset) (ntohs(*(uint16_t *)&buffer[offset]))
16-
178
extern struct trusted_key_ops trusted_key_tpm_ops;
189

19-
struct osapsess {
20-
uint32_t handle;
21-
unsigned char secret[SHA1_DIGEST_SIZE];
22-
unsigned char enonce[TPM_NONCE_SIZE];
23-
};
24-
25-
/* discrete values, but have to store in uint16_t for TPM use */
26-
enum {
27-
SEAL_keytype = 1,
28-
SRK_keytype = 4
29-
};
30-
31-
int TSS_authhmac(unsigned char *digest, const unsigned char *key,
32-
unsigned int keylen, unsigned char *h1,
33-
unsigned char *h2, unsigned int h3, ...);
34-
int TSS_checkhmac1(unsigned char *buffer,
35-
const uint32_t command,
36-
const unsigned char *ononce,
37-
const unsigned char *key,
38-
unsigned int keylen, ...);
39-
40-
int trusted_tpm_send(unsigned char *cmd, size_t buflen);
41-
int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce);
42-
4310
int tpm2_seal_trusted(struct tpm_chip *chip,
4411
struct trusted_key_payload *payload,
4512
struct trusted_key_options *options);
4613
int tpm2_unseal_trusted(struct tpm_chip *chip,
4714
struct trusted_key_payload *payload,
4815
struct trusted_key_options *options);
4916

50-
#define TPM_DEBUG 0
51-
52-
#if TPM_DEBUG
53-
static inline void dump_options(struct trusted_key_options *o)
54-
{
55-
pr_info("sealing key type %d\n", o->keytype);
56-
pr_info("sealing key handle %0X\n", o->keyhandle);
57-
pr_info("pcrlock %d\n", o->pcrlock);
58-
pr_info("pcrinfo %d\n", o->pcrinfo_len);
59-
print_hex_dump(KERN_INFO, "pcrinfo ", DUMP_PREFIX_NONE,
60-
16, 1, o->pcrinfo, o->pcrinfo_len, 0);
61-
}
62-
63-
static inline void dump_sess(struct osapsess *s)
64-
{
65-
print_hex_dump(KERN_INFO, "trusted-key: handle ", DUMP_PREFIX_NONE,
66-
16, 1, &s->handle, 4, 0);
67-
pr_info("secret:\n");
68-
print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE,
69-
16, 1, &s->secret, SHA1_DIGEST_SIZE, 0);
70-
pr_info("trusted-key: enonce:\n");
71-
print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE,
72-
16, 1, &s->enonce, SHA1_DIGEST_SIZE, 0);
73-
}
74-
75-
static inline void dump_tpm_buf(unsigned char *buf)
76-
{
77-
int len;
78-
79-
pr_info("\ntpm buffer\n");
80-
len = LOAD32(buf, TPM_SIZE_OFFSET);
81-
print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1, buf, len, 0);
82-
}
83-
#else
84-
static inline void dump_options(struct trusted_key_options *o)
85-
{
86-
}
87-
88-
static inline void dump_sess(struct osapsess *s)
89-
{
90-
}
91-
92-
static inline void dump_tpm_buf(unsigned char *buf)
93-
{
94-
}
95-
#endif
9617
#endif

security/keys/Kconfig

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Key management configuration
44
#
55

6-
config KEYS
6+
menuconfig KEYS
77
bool "Enable access key retention support"
88
select ASSOCIATIVE_ARRAY
99
help
@@ -21,9 +21,10 @@ config KEYS
2121

2222
If you are unsure as to whether this is required, answer N.
2323

24+
if KEYS
25+
2426
config KEYS_REQUEST_CACHE
2527
bool "Enable temporary caching of the last request_key() result"
26-
depends on KEYS
2728
help
2829
This option causes the result of the last successful request_key()
2930
call that didn't upcall to the kernel to be cached temporarily in the
@@ -41,7 +42,6 @@ config KEYS_REQUEST_CACHE
4142

4243
config PERSISTENT_KEYRINGS
4344
bool "Enable register of persistent per-UID keyrings"
44-
depends on KEYS
4545
help
4646
This option provides a register of persistent per-UID keyrings,
4747
primarily aimed at Kerberos key storage. The keyrings are persistent
@@ -58,7 +58,6 @@ config PERSISTENT_KEYRINGS
5858

5959
config BIG_KEYS
6060
bool "Large payload keys"
61-
depends on KEYS
6261
depends on TMPFS
6362
select CRYPTO_LIB_CHACHA20POLY1305
6463
help
@@ -70,7 +69,6 @@ config BIG_KEYS
7069

7170
config TRUSTED_KEYS
7271
tristate "TRUSTED KEYS"
73-
depends on KEYS
7472
help
7573
This option provides support for creating, sealing, and unsealing
7674
keys in the kernel. Trusted keys are random number symmetric keys,
@@ -85,12 +83,10 @@ endif
8583

8684
config ENCRYPTED_KEYS
8785
tristate "ENCRYPTED KEYS"
88-
depends on KEYS
8986
select CRYPTO
90-
select CRYPTO_HMAC
9187
select CRYPTO_AES
9288
select CRYPTO_CBC
93-
select CRYPTO_SHA256
89+
select CRYPTO_LIB_SHA256
9490
select CRYPTO_RNG
9591
help
9692
This option provides support for create/encrypting/decrypting keys
@@ -114,7 +110,6 @@ config USER_DECRYPTED_DATA
114110

115111
config KEY_DH_OPERATIONS
116112
bool "Diffie-Hellman operations on retained keys"
117-
depends on KEYS
118113
select CRYPTO
119114
select CRYPTO_KDF800108_CTR
120115
select CRYPTO_DH
@@ -127,9 +122,11 @@ config KEY_DH_OPERATIONS
127122

128123
config KEY_NOTIFICATIONS
129124
bool "Provide key/keyring change notifications"
130-
depends on KEYS && WATCH_QUEUE
125+
depends on WATCH_QUEUE
131126
help
132127
This option provides support for getting change notifications
133128
on keys and keyrings on which the caller has View permission.
134129
This makes use of pipes to handle the notification buffer and
135130
provides KEYCTL_WATCH_KEY to enable/disable watches.
131+
132+
endif # KEYS

security/keys/encrypted-keys/encrypted.c

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include <linux/scatterlist.h>
2828
#include <linux/ctype.h>
2929
#include <crypto/aes.h>
30-
#include <crypto/hash.h>
3130
#include <crypto/sha2.h>
3231
#include <crypto/skcipher.h>
3332
#include <crypto/utils.h>
@@ -37,8 +36,6 @@
3736

3837
static const char KEY_TRUSTED_PREFIX[] = "trusted:";
3938
static const char KEY_USER_PREFIX[] = "user:";
40-
static const char hash_alg[] = "sha256";
41-
static const char hmac_alg[] = "hmac(sha256)";
4239
static const char blkcipher_alg[] = "cbc(aes)";
4340
static const char key_format_default[] = "default";
4441
static const char key_format_ecryptfs[] = "ecryptfs";
@@ -54,8 +51,6 @@ static int blksize;
5451
#define MIN_DATA_SIZE 20
5552
#define KEY_ENC32_PAYLOAD_LEN 32
5653

57-
static struct crypto_shash *hash_tfm;
58-
5954
enum {
6055
Opt_new, Opt_load, Opt_update, Opt_err
6156
};
@@ -329,26 +324,6 @@ static struct key *request_user_key(const char *master_desc, const u8 **master_k
329324
return ukey;
330325
}
331326

332-
static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen,
333-
const u8 *buf, unsigned int buflen)
334-
{
335-
struct crypto_shash *tfm;
336-
int err;
337-
338-
tfm = crypto_alloc_shash(hmac_alg, 0, 0);
339-
if (IS_ERR(tfm)) {
340-
pr_err("encrypted_key: can't alloc %s transform: %ld\n",
341-
hmac_alg, PTR_ERR(tfm));
342-
return PTR_ERR(tfm);
343-
}
344-
345-
err = crypto_shash_setkey(tfm, key, keylen);
346-
if (!err)
347-
err = crypto_shash_tfm_digest(tfm, buf, buflen, digest);
348-
crypto_free_shash(tfm);
349-
return err;
350-
}
351-
352327
enum derived_key_type { ENC_KEY, AUTH_KEY };
353328

354329
/* Derive authentication/encryption key from trusted key */
@@ -357,7 +332,6 @@ static int get_derived_key(u8 *derived_key, enum derived_key_type key_type,
357332
{
358333
u8 *derived_buf;
359334
unsigned int derived_buf_len;
360-
int ret;
361335

362336
derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen;
363337
if (derived_buf_len < HASH_SIZE)
@@ -374,10 +348,9 @@ static int get_derived_key(u8 *derived_key, enum derived_key_type key_type,
374348

375349
memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
376350
master_keylen);
377-
ret = crypto_shash_tfm_digest(hash_tfm, derived_buf, derived_buf_len,
378-
derived_key);
351+
sha256(derived_buf, derived_buf_len, derived_key);
379352
kfree_sensitive(derived_buf);
380-
return ret;
353+
return 0;
381354
}
382355

383356
static struct skcipher_request *init_skcipher_req(const u8 *key,
@@ -503,10 +476,10 @@ static int datablob_hmac_append(struct encrypted_key_payload *epayload,
503476
goto out;
504477

505478
digest = epayload->format + epayload->datablob_len;
506-
ret = calc_hmac(digest, derived_key, sizeof derived_key,
507-
epayload->format, epayload->datablob_len);
508-
if (!ret)
509-
dump_hmac(NULL, digest, HASH_SIZE);
479+
hmac_sha256_usingrawkey(derived_key, sizeof(derived_key),
480+
epayload->format, epayload->datablob_len,
481+
digest);
482+
dump_hmac(NULL, digest, HASH_SIZE);
510483
out:
511484
memzero_explicit(derived_key, sizeof(derived_key));
512485
return ret;
@@ -534,9 +507,8 @@ static int datablob_hmac_verify(struct encrypted_key_payload *epayload,
534507
} else
535508
p = epayload->format;
536509

537-
ret = calc_hmac(digest, derived_key, sizeof derived_key, p, len);
538-
if (ret < 0)
539-
goto out;
510+
hmac_sha256_usingrawkey(derived_key, sizeof(derived_key), p, len,
511+
digest);
540512
ret = crypto_memneq(digest, epayload->format + epayload->datablob_len,
541513
sizeof(digest));
542514
if (ret) {
@@ -1011,29 +983,14 @@ static int __init init_encrypted(void)
1011983
{
1012984
int ret;
1013985

1014-
hash_tfm = crypto_alloc_shash(hash_alg, 0, 0);
1015-
if (IS_ERR(hash_tfm)) {
1016-
pr_err("encrypted_key: can't allocate %s transform: %ld\n",
1017-
hash_alg, PTR_ERR(hash_tfm));
1018-
return PTR_ERR(hash_tfm);
1019-
}
1020-
1021986
ret = aes_get_sizes();
1022987
if (ret < 0)
1023-
goto out;
1024-
ret = register_key_type(&key_type_encrypted);
1025-
if (ret < 0)
1026-
goto out;
1027-
return 0;
1028-
out:
1029-
crypto_free_shash(hash_tfm);
1030-
return ret;
1031-
988+
return ret;
989+
return register_key_type(&key_type_encrypted);
1032990
}
1033991

1034992
static void __exit cleanup_encrypted(void)
1035993
{
1036-
crypto_free_shash(hash_tfm);
1037994
unregister_key_type(&key_type_encrypted);
1038995
}
1039996

security/keys/trusted-keys/Kconfig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ config TRUSTED_KEYS_TPM
55
bool "TPM-based trusted keys"
66
depends on TCG_TPM >= TRUSTED_KEYS
77
default y
8-
select CRYPTO
9-
select CRYPTO_HMAC
10-
select CRYPTO_SHA1
118
select CRYPTO_HASH_INFO
9+
select CRYPTO_LIB_SHA1
10+
select CRYPTO_LIB_UTILS
1211
select ASN1_ENCODER
1312
select OID_REGISTRY
1413
select ASN1

0 commit comments

Comments
 (0)