Skip to content

Commit c6f61e5

Browse files
b49020Jarkko Sakkinen
authored andcommitted
KEYS: Use common tpm_buf for trusted and asymmetric keys
Switch to utilize common heap based tpm_buf code for TPM based trusted and asymmetric keys rather than using stack based tpm1_buf code. Also, remove tpm1_buf code. Suggested-by: Jarkko Sakkinen <[email protected]> Signed-off-by: Sumit Garg <[email protected]> Reviewed-by: Jerry Snitselaar <[email protected]> Reviewed-by: Jarkko Sakkinen <[email protected]> Tested-by: Jarkko Sakkinen <[email protected]> Signed-off-by: Jarkko Sakkinen <[email protected]>
1 parent 74edff2 commit c6f61e5

File tree

3 files changed

+89
-153
lines changed

3 files changed

+89
-153
lines changed

crypto/asymmetric_keys/asym_tpm.c

Lines changed: 45 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,13 @@
2121
#define TPM_ORD_LOADKEY2 65
2222
#define TPM_ORD_UNBIND 30
2323
#define TPM_ORD_SIGN 60
24-
#define TPM_LOADKEY2_SIZE 59
25-
#define TPM_FLUSHSPECIFIC_SIZE 18
26-
#define TPM_UNBIND_SIZE 63
27-
#define TPM_SIGN_SIZE 63
2824

2925
#define TPM_RT_KEY 0x00000001
3026

3127
/*
3228
* Load a TPM key from the blob provided by userspace
3329
*/
34-
static int tpm_loadkey2(struct tpm1_buf *tb,
30+
static int tpm_loadkey2(struct tpm_buf *tb,
3531
uint32_t keyhandle, unsigned char *keyauth,
3632
const unsigned char *keyblob, int keybloblen,
3733
uint32_t *newhandle)
@@ -68,16 +64,13 @@ static int tpm_loadkey2(struct tpm1_buf *tb,
6864
return ret;
6965

7066
/* build the request buffer */
71-
INIT_BUF(tb);
72-
store16(tb, TPM_TAG_RQU_AUTH1_COMMAND);
73-
store32(tb, TPM_LOADKEY2_SIZE + keybloblen);
74-
store32(tb, TPM_ORD_LOADKEY2);
75-
store32(tb, keyhandle);
76-
storebytes(tb, keyblob, keybloblen);
77-
store32(tb, authhandle);
78-
storebytes(tb, nonceodd, TPM_NONCE_SIZE);
79-
store8(tb, cont);
80-
storebytes(tb, authdata, SHA1_DIGEST_SIZE);
67+
tpm_buf_reset(tb, TPM_TAG_RQU_AUTH1_COMMAND, TPM_ORD_LOADKEY2);
68+
tpm_buf_append_u32(tb, keyhandle);
69+
tpm_buf_append(tb, keyblob, keybloblen);
70+
tpm_buf_append_u32(tb, authhandle);
71+
tpm_buf_append(tb, nonceodd, TPM_NONCE_SIZE);
72+
tpm_buf_append_u8(tb, cont);
73+
tpm_buf_append(tb, authdata, SHA1_DIGEST_SIZE);
8174

8275
ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
8376
if (ret < 0) {
@@ -99,14 +92,11 @@ static int tpm_loadkey2(struct tpm1_buf *tb,
9992
/*
10093
* Execute the FlushSpecific TPM command
10194
*/
102-
static int tpm_flushspecific(struct tpm1_buf *tb, uint32_t handle)
95+
static int tpm_flushspecific(struct tpm_buf *tb, uint32_t handle)
10396
{
104-
INIT_BUF(tb);
105-
store16(tb, TPM_TAG_RQU_COMMAND);
106-
store32(tb, TPM_FLUSHSPECIFIC_SIZE);
107-
store32(tb, TPM_ORD_FLUSHSPECIFIC);
108-
store32(tb, handle);
109-
store32(tb, TPM_RT_KEY);
97+
tpm_buf_reset(tb, TPM_TAG_RQU_COMMAND, TPM_ORD_FLUSHSPECIFIC);
98+
tpm_buf_append_u32(tb, handle);
99+
tpm_buf_append_u32(tb, TPM_RT_KEY);
110100

111101
return trusted_tpm_send(tb->data, MAX_BUF_SIZE);
112102
}
@@ -115,7 +105,7 @@ static int tpm_flushspecific(struct tpm1_buf *tb, uint32_t handle)
115105
* Decrypt a blob provided by userspace using a specific key handle.
116106
* The handle is a well known handle or previously loaded by e.g. LoadKey2
117107
*/
118-
static int tpm_unbind(struct tpm1_buf *tb,
108+
static int tpm_unbind(struct tpm_buf *tb,
119109
uint32_t keyhandle, unsigned char *keyauth,
120110
const unsigned char *blob, uint32_t bloblen,
121111
void *out, uint32_t outlen)
@@ -155,17 +145,14 @@ static int tpm_unbind(struct tpm1_buf *tb,
155145
return ret;
156146

157147
/* build the request buffer */
158-
INIT_BUF(tb);
159-
store16(tb, TPM_TAG_RQU_AUTH1_COMMAND);
160-
store32(tb, TPM_UNBIND_SIZE + bloblen);
161-
store32(tb, TPM_ORD_UNBIND);
162-
store32(tb, keyhandle);
163-
store32(tb, bloblen);
164-
storebytes(tb, blob, bloblen);
165-
store32(tb, authhandle);
166-
storebytes(tb, nonceodd, TPM_NONCE_SIZE);
167-
store8(tb, cont);
168-
storebytes(tb, authdata, SHA1_DIGEST_SIZE);
148+
tpm_buf_reset(tb, TPM_TAG_RQU_AUTH1_COMMAND, TPM_ORD_UNBIND);
149+
tpm_buf_append_u32(tb, keyhandle);
150+
tpm_buf_append_u32(tb, bloblen);
151+
tpm_buf_append(tb, blob, bloblen);
152+
tpm_buf_append_u32(tb, authhandle);
153+
tpm_buf_append(tb, nonceodd, TPM_NONCE_SIZE);
154+
tpm_buf_append_u8(tb, cont);
155+
tpm_buf_append(tb, authdata, SHA1_DIGEST_SIZE);
169156

170157
ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
171158
if (ret < 0) {
@@ -201,7 +188,7 @@ static int tpm_unbind(struct tpm1_buf *tb,
201188
* up to key_length_in_bytes - 11 and not be limited to size 20 like the
202189
* TPM_SS_RSASSAPKCS1v15_SHA1 signature scheme.
203190
*/
204-
static int tpm_sign(struct tpm1_buf *tb,
191+
static int tpm_sign(struct tpm_buf *tb,
205192
uint32_t keyhandle, unsigned char *keyauth,
206193
const unsigned char *blob, uint32_t bloblen,
207194
void *out, uint32_t outlen)
@@ -241,17 +228,14 @@ static int tpm_sign(struct tpm1_buf *tb,
241228
return ret;
242229

243230
/* build the request buffer */
244-
INIT_BUF(tb);
245-
store16(tb, TPM_TAG_RQU_AUTH1_COMMAND);
246-
store32(tb, TPM_SIGN_SIZE + bloblen);
247-
store32(tb, TPM_ORD_SIGN);
248-
store32(tb, keyhandle);
249-
store32(tb, bloblen);
250-
storebytes(tb, blob, bloblen);
251-
store32(tb, authhandle);
252-
storebytes(tb, nonceodd, TPM_NONCE_SIZE);
253-
store8(tb, cont);
254-
storebytes(tb, authdata, SHA1_DIGEST_SIZE);
231+
tpm_buf_reset(tb, TPM_TAG_RQU_AUTH1_COMMAND, TPM_ORD_SIGN);
232+
tpm_buf_append_u32(tb, keyhandle);
233+
tpm_buf_append_u32(tb, bloblen);
234+
tpm_buf_append(tb, blob, bloblen);
235+
tpm_buf_append_u32(tb, authhandle);
236+
tpm_buf_append(tb, nonceodd, TPM_NONCE_SIZE);
237+
tpm_buf_append_u8(tb, cont);
238+
tpm_buf_append(tb, authdata, SHA1_DIGEST_SIZE);
255239

256240
ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
257241
if (ret < 0) {
@@ -519,7 +503,7 @@ static int tpm_key_decrypt(struct tpm_key *tk,
519503
struct kernel_pkey_params *params,
520504
const void *in, void *out)
521505
{
522-
struct tpm1_buf *tb;
506+
struct tpm_buf tb;
523507
uint32_t keyhandle;
524508
uint8_t srkauth[SHA1_DIGEST_SIZE];
525509
uint8_t keyauth[SHA1_DIGEST_SIZE];
@@ -533,14 +517,14 @@ static int tpm_key_decrypt(struct tpm_key *tk,
533517
if (strcmp(params->encoding, "pkcs1"))
534518
return -ENOPKG;
535519

536-
tb = kzalloc(sizeof(*tb), GFP_KERNEL);
537-
if (!tb)
538-
return -ENOMEM;
520+
r = tpm_buf_init(&tb, 0, 0);
521+
if (r)
522+
return r;
539523

540524
/* TODO: Handle a non-all zero SRK authorization */
541525
memset(srkauth, 0, sizeof(srkauth));
542526

543-
r = tpm_loadkey2(tb, SRKHANDLE, srkauth,
527+
r = tpm_loadkey2(&tb, SRKHANDLE, srkauth,
544528
tk->blob, tk->blob_len, &keyhandle);
545529
if (r < 0) {
546530
pr_devel("loadkey2 failed (%d)\n", r);
@@ -550,16 +534,16 @@ static int tpm_key_decrypt(struct tpm_key *tk,
550534
/* TODO: Handle a non-all zero key authorization */
551535
memset(keyauth, 0, sizeof(keyauth));
552536

553-
r = tpm_unbind(tb, keyhandle, keyauth,
537+
r = tpm_unbind(&tb, keyhandle, keyauth,
554538
in, params->in_len, out, params->out_len);
555539
if (r < 0)
556540
pr_devel("tpm_unbind failed (%d)\n", r);
557541

558-
if (tpm_flushspecific(tb, keyhandle) < 0)
542+
if (tpm_flushspecific(&tb, keyhandle) < 0)
559543
pr_devel("flushspecific failed (%d)\n", r);
560544

561545
error:
562-
kzfree(tb);
546+
tpm_buf_destroy(&tb);
563547
pr_devel("<==%s() = %d\n", __func__, r);
564548
return r;
565549
}
@@ -643,7 +627,7 @@ static int tpm_key_sign(struct tpm_key *tk,
643627
struct kernel_pkey_params *params,
644628
const void *in, void *out)
645629
{
646-
struct tpm1_buf *tb;
630+
struct tpm_buf tb;
647631
uint32_t keyhandle;
648632
uint8_t srkauth[SHA1_DIGEST_SIZE];
649633
uint8_t keyauth[SHA1_DIGEST_SIZE];
@@ -681,15 +665,14 @@ static int tpm_key_sign(struct tpm_key *tk,
681665
goto error_free_asn1_wrapped;
682666
}
683667

684-
r = -ENOMEM;
685-
tb = kzalloc(sizeof(*tb), GFP_KERNEL);
686-
if (!tb)
668+
r = tpm_buf_init(&tb, 0, 0);
669+
if (r)
687670
goto error_free_asn1_wrapped;
688671

689672
/* TODO: Handle a non-all zero SRK authorization */
690673
memset(srkauth, 0, sizeof(srkauth));
691674

692-
r = tpm_loadkey2(tb, SRKHANDLE, srkauth,
675+
r = tpm_loadkey2(&tb, SRKHANDLE, srkauth,
693676
tk->blob, tk->blob_len, &keyhandle);
694677
if (r < 0) {
695678
pr_devel("loadkey2 failed (%d)\n", r);
@@ -699,15 +682,15 @@ static int tpm_key_sign(struct tpm_key *tk,
699682
/* TODO: Handle a non-all zero key authorization */
700683
memset(keyauth, 0, sizeof(keyauth));
701684

702-
r = tpm_sign(tb, keyhandle, keyauth, in, in_len, out, params->out_len);
685+
r = tpm_sign(&tb, keyhandle, keyauth, in, in_len, out, params->out_len);
703686
if (r < 0)
704687
pr_devel("tpm_sign failed (%d)\n", r);
705688

706-
if (tpm_flushspecific(tb, keyhandle) < 0)
689+
if (tpm_flushspecific(&tb, keyhandle) < 0)
707690
pr_devel("flushspecific failed (%d)\n", r);
708691

709692
error_free_tb:
710-
kzfree(tb);
693+
tpm_buf_destroy(&tb);
711694
error_free_asn1_wrapped:
712695
kfree(asn1_wrapped);
713696
pr_devel("<==%s() = %d\n", __func__, r);

include/keys/trusted.h

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
/* implementation specific TPM constants */
66
#define MAX_BUF_SIZE 1024
77
#define TPM_GETRANDOM_SIZE 14
8-
#define TPM_OSAP_SIZE 36
9-
#define TPM_OIAP_SIZE 10
10-
#define TPM_SEAL_SIZE 87
11-
#define TPM_UNSEAL_SIZE 104
128
#define TPM_SIZE_OFFSET 2
139
#define TPM_RETURN_OFFSET 6
1410
#define TPM_DATA_OFFSET 10
@@ -17,13 +13,6 @@
1713
#define LOAD32N(buffer, offset) (*(uint32_t *)&buffer[offset])
1814
#define LOAD16(buffer, offset) (ntohs(*(uint16_t *)&buffer[offset]))
1915

20-
struct tpm1_buf {
21-
int len;
22-
unsigned char data[MAX_BUF_SIZE];
23-
};
24-
25-
#define INIT_BUF(tb) (tb->len = 0)
26-
2716
struct osapsess {
2817
uint32_t handle;
2918
unsigned char secret[SHA1_DIGEST_SIZE];
@@ -46,7 +35,7 @@ int TSS_checkhmac1(unsigned char *buffer,
4635
unsigned int keylen, ...);
4736

4837
int trusted_tpm_send(unsigned char *cmd, size_t buflen);
49-
int oiap(struct tpm1_buf *tb, uint32_t *handle, unsigned char *nonce);
38+
int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce);
5039

5140
#define TPM_DEBUG 0
5241

@@ -109,28 +98,4 @@ static inline void dump_tpm_buf(unsigned char *buf)
10998
{
11099
}
111100
#endif
112-
113-
static inline void store8(struct tpm1_buf *buf, const unsigned char value)
114-
{
115-
buf->data[buf->len++] = value;
116-
}
117-
118-
static inline void store16(struct tpm1_buf *buf, const uint16_t value)
119-
{
120-
*(uint16_t *) & buf->data[buf->len] = htons(value);
121-
buf->len += sizeof value;
122-
}
123-
124-
static inline void store32(struct tpm1_buf *buf, const uint32_t value)
125-
{
126-
*(uint32_t *) & buf->data[buf->len] = htonl(value);
127-
buf->len += sizeof value;
128-
}
129-
130-
static inline void storebytes(struct tpm1_buf *buf, const unsigned char *in,
131-
const int len)
132-
{
133-
memcpy(buf->data + buf->len, in, len);
134-
buf->len += len;
135-
}
136101
#endif

0 commit comments

Comments
 (0)