21
21
#define TPM_ORD_LOADKEY2 65
22
22
#define TPM_ORD_UNBIND 30
23
23
#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
28
24
29
25
#define TPM_RT_KEY 0x00000001
30
26
31
27
/*
32
28
* Load a TPM key from the blob provided by userspace
33
29
*/
34
- static int tpm_loadkey2 (struct tpm1_buf * tb ,
30
+ static int tpm_loadkey2 (struct tpm_buf * tb ,
35
31
uint32_t keyhandle , unsigned char * keyauth ,
36
32
const unsigned char * keyblob , int keybloblen ,
37
33
uint32_t * newhandle )
@@ -68,16 +64,13 @@ static int tpm_loadkey2(struct tpm1_buf *tb,
68
64
return ret ;
69
65
70
66
/* 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 );
81
74
82
75
ret = trusted_tpm_send (tb -> data , MAX_BUF_SIZE );
83
76
if (ret < 0 ) {
@@ -99,14 +92,11 @@ static int tpm_loadkey2(struct tpm1_buf *tb,
99
92
/*
100
93
* Execute the FlushSpecific TPM command
101
94
*/
102
- static int tpm_flushspecific (struct tpm1_buf * tb , uint32_t handle )
95
+ static int tpm_flushspecific (struct tpm_buf * tb , uint32_t handle )
103
96
{
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 );
110
100
111
101
return trusted_tpm_send (tb -> data , MAX_BUF_SIZE );
112
102
}
@@ -115,7 +105,7 @@ static int tpm_flushspecific(struct tpm1_buf *tb, uint32_t handle)
115
105
* Decrypt a blob provided by userspace using a specific key handle.
116
106
* The handle is a well known handle or previously loaded by e.g. LoadKey2
117
107
*/
118
- static int tpm_unbind (struct tpm1_buf * tb ,
108
+ static int tpm_unbind (struct tpm_buf * tb ,
119
109
uint32_t keyhandle , unsigned char * keyauth ,
120
110
const unsigned char * blob , uint32_t bloblen ,
121
111
void * out , uint32_t outlen )
@@ -155,17 +145,14 @@ static int tpm_unbind(struct tpm1_buf *tb,
155
145
return ret ;
156
146
157
147
/* 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 );
169
156
170
157
ret = trusted_tpm_send (tb -> data , MAX_BUF_SIZE );
171
158
if (ret < 0 ) {
@@ -201,7 +188,7 @@ static int tpm_unbind(struct tpm1_buf *tb,
201
188
* up to key_length_in_bytes - 11 and not be limited to size 20 like the
202
189
* TPM_SS_RSASSAPKCS1v15_SHA1 signature scheme.
203
190
*/
204
- static int tpm_sign (struct tpm1_buf * tb ,
191
+ static int tpm_sign (struct tpm_buf * tb ,
205
192
uint32_t keyhandle , unsigned char * keyauth ,
206
193
const unsigned char * blob , uint32_t bloblen ,
207
194
void * out , uint32_t outlen )
@@ -241,17 +228,14 @@ static int tpm_sign(struct tpm1_buf *tb,
241
228
return ret ;
242
229
243
230
/* 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 );
255
239
256
240
ret = trusted_tpm_send (tb -> data , MAX_BUF_SIZE );
257
241
if (ret < 0 ) {
@@ -519,7 +503,7 @@ static int tpm_key_decrypt(struct tpm_key *tk,
519
503
struct kernel_pkey_params * params ,
520
504
const void * in , void * out )
521
505
{
522
- struct tpm1_buf * tb ;
506
+ struct tpm_buf tb ;
523
507
uint32_t keyhandle ;
524
508
uint8_t srkauth [SHA1_DIGEST_SIZE ];
525
509
uint8_t keyauth [SHA1_DIGEST_SIZE ];
@@ -533,14 +517,14 @@ static int tpm_key_decrypt(struct tpm_key *tk,
533
517
if (strcmp (params -> encoding , "pkcs1" ))
534
518
return - ENOPKG ;
535
519
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 ;
539
523
540
524
/* TODO: Handle a non-all zero SRK authorization */
541
525
memset (srkauth , 0 , sizeof (srkauth ));
542
526
543
- r = tpm_loadkey2 (tb , SRKHANDLE , srkauth ,
527
+ r = tpm_loadkey2 (& tb , SRKHANDLE , srkauth ,
544
528
tk -> blob , tk -> blob_len , & keyhandle );
545
529
if (r < 0 ) {
546
530
pr_devel ("loadkey2 failed (%d)\n" , r );
@@ -550,16 +534,16 @@ static int tpm_key_decrypt(struct tpm_key *tk,
550
534
/* TODO: Handle a non-all zero key authorization */
551
535
memset (keyauth , 0 , sizeof (keyauth ));
552
536
553
- r = tpm_unbind (tb , keyhandle , keyauth ,
537
+ r = tpm_unbind (& tb , keyhandle , keyauth ,
554
538
in , params -> in_len , out , params -> out_len );
555
539
if (r < 0 )
556
540
pr_devel ("tpm_unbind failed (%d)\n" , r );
557
541
558
- if (tpm_flushspecific (tb , keyhandle ) < 0 )
542
+ if (tpm_flushspecific (& tb , keyhandle ) < 0 )
559
543
pr_devel ("flushspecific failed (%d)\n" , r );
560
544
561
545
error :
562
- kzfree ( tb );
546
+ tpm_buf_destroy ( & tb );
563
547
pr_devel ("<==%s() = %d\n" , __func__ , r );
564
548
return r ;
565
549
}
@@ -643,7 +627,7 @@ static int tpm_key_sign(struct tpm_key *tk,
643
627
struct kernel_pkey_params * params ,
644
628
const void * in , void * out )
645
629
{
646
- struct tpm1_buf * tb ;
630
+ struct tpm_buf tb ;
647
631
uint32_t keyhandle ;
648
632
uint8_t srkauth [SHA1_DIGEST_SIZE ];
649
633
uint8_t keyauth [SHA1_DIGEST_SIZE ];
@@ -681,15 +665,14 @@ static int tpm_key_sign(struct tpm_key *tk,
681
665
goto error_free_asn1_wrapped ;
682
666
}
683
667
684
- r = - ENOMEM ;
685
- tb = kzalloc (sizeof (* tb ), GFP_KERNEL );
686
- if (!tb )
668
+ r = tpm_buf_init (& tb , 0 , 0 );
669
+ if (r )
687
670
goto error_free_asn1_wrapped ;
688
671
689
672
/* TODO: Handle a non-all zero SRK authorization */
690
673
memset (srkauth , 0 , sizeof (srkauth ));
691
674
692
- r = tpm_loadkey2 (tb , SRKHANDLE , srkauth ,
675
+ r = tpm_loadkey2 (& tb , SRKHANDLE , srkauth ,
693
676
tk -> blob , tk -> blob_len , & keyhandle );
694
677
if (r < 0 ) {
695
678
pr_devel ("loadkey2 failed (%d)\n" , r );
@@ -699,15 +682,15 @@ static int tpm_key_sign(struct tpm_key *tk,
699
682
/* TODO: Handle a non-all zero key authorization */
700
683
memset (keyauth , 0 , sizeof (keyauth ));
701
684
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 );
703
686
if (r < 0 )
704
687
pr_devel ("tpm_sign failed (%d)\n" , r );
705
688
706
- if (tpm_flushspecific (tb , keyhandle ) < 0 )
689
+ if (tpm_flushspecific (& tb , keyhandle ) < 0 )
707
690
pr_devel ("flushspecific failed (%d)\n" , r );
708
691
709
692
error_free_tb :
710
- kzfree ( tb );
693
+ tpm_buf_destroy ( & tb );
711
694
error_free_asn1_wrapped :
712
695
kfree (asn1_wrapped );
713
696
pr_devel ("<==%s() = %d\n" , __func__ , r );
0 commit comments