77 */
88
99#include <crypto/hash_info.h>
10+ #include <crypto/sha1.h>
1011#include <crypto/utils.h>
1112#include <linux/init.h>
1213#include <linux/slab.h>
1516#include <linux/err.h>
1617#include <keys/trusted-type.h>
1718#include <linux/key-type.h>
18- #include <linux/crypto.h>
19- #include <crypto/hash.h>
20- #include <crypto/sha1.h>
2119#include <linux/tpm.h>
2220#include <linux/tpm_command.h>
2321
2422#include <keys/trusted_tpm.h>
2523
26- static const char hmac_alg [] = "hmac(sha1)" ;
27- static const char hash_alg [] = "sha1" ;
2824static struct tpm_chip * chip ;
2925static struct tpm_digest * digests ;
3026
31- struct sdesc {
32- struct shash_desc shash ;
33- char ctx [];
34- };
35-
36- static struct crypto_shash * hashalg ;
37- static struct crypto_shash * hmacalg ;
38-
39- static struct sdesc * init_sdesc (struct crypto_shash * alg )
40- {
41- struct sdesc * sdesc ;
42- int size ;
43-
44- size = sizeof (struct shash_desc ) + crypto_shash_descsize (alg );
45- sdesc = kmalloc (size , GFP_KERNEL );
46- if (!sdesc )
47- return ERR_PTR (- ENOMEM );
48- sdesc -> shash .tfm = alg ;
49- return sdesc ;
50- }
51-
52- static int TSS_sha1 (const unsigned char * data , unsigned int datalen ,
53- unsigned char * digest )
54- {
55- struct sdesc * sdesc ;
56- int ret ;
57-
58- sdesc = init_sdesc (hashalg );
59- if (IS_ERR (sdesc )) {
60- pr_info ("can't alloc %s\n" , hash_alg );
61- return PTR_ERR (sdesc );
62- }
63-
64- ret = crypto_shash_digest (& sdesc -> shash , data , datalen , digest );
65- kfree_sensitive (sdesc );
66- return ret ;
67- }
68-
6927static int TSS_rawhmac (unsigned char * digest , const unsigned char * key ,
7028 unsigned int keylen , ...)
7129{
72- struct sdesc * sdesc ;
30+ struct hmac_sha1_ctx hmac_ctx ;
7331 va_list argp ;
7432 unsigned int dlen ;
7533 unsigned char * data ;
76- int ret ;
77-
78- sdesc = init_sdesc (hmacalg );
79- if (IS_ERR (sdesc )) {
80- pr_info ("can't alloc %s\n" , hmac_alg );
81- return PTR_ERR (sdesc );
82- }
34+ int ret = 0 ;
8335
84- ret = crypto_shash_setkey (hmacalg , key , keylen );
85- if (ret < 0 )
86- goto out ;
87- ret = crypto_shash_init (& sdesc -> shash );
88- if (ret < 0 )
89- goto out ;
36+ hmac_sha1_init_usingrawkey (& hmac_ctx , key , keylen );
9037
9138 va_start (argp , keylen );
9239 for (;;) {
@@ -98,15 +45,11 @@ static int TSS_rawhmac(unsigned char *digest, const unsigned char *key,
9845 ret = - EINVAL ;
9946 break ;
10047 }
101- ret = crypto_shash_update (& sdesc -> shash , data , dlen );
102- if (ret < 0 )
103- break ;
48+ hmac_sha1_update (& hmac_ctx , data , dlen );
10449 }
10550 va_end (argp );
10651 if (!ret )
107- ret = crypto_shash_final (& sdesc -> shash , digest );
108- out :
109- kfree_sensitive (sdesc );
52+ hmac_sha1_final (& hmac_ctx , digest );
11053 return ret ;
11154}
11255
@@ -118,26 +61,18 @@ int TSS_authhmac(unsigned char *digest, const unsigned char *key,
11861 unsigned char * h2 , unsigned int h3 , ...)
11962{
12063 unsigned char paramdigest [SHA1_DIGEST_SIZE ];
121- struct sdesc * sdesc ;
64+ struct sha1_ctx sha_ctx ;
12265 unsigned int dlen ;
12366 unsigned char * data ;
12467 unsigned char c ;
125- int ret ;
68+ int ret = 0 ;
12669 va_list argp ;
12770
12871 if (!chip )
12972 return - ENODEV ;
13073
131- sdesc = init_sdesc (hashalg );
132- if (IS_ERR (sdesc )) {
133- pr_info ("can't alloc %s\n" , hash_alg );
134- return PTR_ERR (sdesc );
135- }
136-
13774 c = !!h3 ;
138- ret = crypto_shash_init (& sdesc -> shash );
139- if (ret < 0 )
140- goto out ;
75+ sha1_init (& sha_ctx );
14176 va_start (argp , h3 );
14277 for (;;) {
14378 dlen = va_arg (argp , unsigned int );
@@ -148,19 +83,15 @@ int TSS_authhmac(unsigned char *digest, const unsigned char *key,
14883 ret = - EINVAL ;
14984 break ;
15085 }
151- ret = crypto_shash_update (& sdesc -> shash , data , dlen );
152- if (ret < 0 )
153- break ;
86+ sha1_update (& sha_ctx , data , dlen );
15487 }
15588 va_end (argp );
15689 if (!ret )
157- ret = crypto_shash_final ( & sdesc -> shash , paramdigest );
90+ sha1_final ( & sha_ctx , paramdigest );
15891 if (!ret )
15992 ret = TSS_rawhmac (digest , key , keylen , SHA1_DIGEST_SIZE ,
16093 paramdigest , TPM_NONCE_SIZE , h1 ,
16194 TPM_NONCE_SIZE , h2 , 1 , & c , 0 , 0 );
162- out :
163- kfree_sensitive (sdesc );
16495 return ret ;
16596}
16697EXPORT_SYMBOL_GPL (TSS_authhmac );
@@ -183,7 +114,7 @@ int TSS_checkhmac1(unsigned char *buffer,
183114 unsigned char * authdata ;
184115 unsigned char testhmac [SHA1_DIGEST_SIZE ];
185116 unsigned char paramdigest [SHA1_DIGEST_SIZE ];
186- struct sdesc * sdesc ;
117+ struct sha1_ctx sha_ctx ;
187118 unsigned int dlen ;
188119 unsigned int dpos ;
189120 va_list argp ;
@@ -204,49 +135,29 @@ int TSS_checkhmac1(unsigned char *buffer,
204135 continueflag = authdata - 1 ;
205136 enonce = continueflag - TPM_NONCE_SIZE ;
206137
207- sdesc = init_sdesc (hashalg );
208- if (IS_ERR (sdesc )) {
209- pr_info ("can't alloc %s\n" , hash_alg );
210- return PTR_ERR (sdesc );
211- }
212- ret = crypto_shash_init (& sdesc -> shash );
213- if (ret < 0 )
214- goto out ;
215- ret = crypto_shash_update (& sdesc -> shash , (const u8 * )& result ,
216- sizeof result );
217- if (ret < 0 )
218- goto out ;
219- ret = crypto_shash_update (& sdesc -> shash , (const u8 * )& ordinal ,
220- sizeof ordinal );
221- if (ret < 0 )
222- goto out ;
138+ sha1_init (& sha_ctx );
139+ sha1_update (& sha_ctx , (const u8 * )& result , sizeof (result ));
140+ sha1_update (& sha_ctx , (const u8 * )& ordinal , sizeof (ordinal ));
223141 va_start (argp , keylen );
224142 for (;;) {
225143 dlen = va_arg (argp , unsigned int );
226144 if (dlen == 0 )
227145 break ;
228146 dpos = va_arg (argp , unsigned int );
229- ret = crypto_shash_update (& sdesc -> shash , buffer + dpos , dlen );
230- if (ret < 0 )
231- break ;
147+ sha1_update (& sha_ctx , buffer + dpos , dlen );
232148 }
233149 va_end (argp );
234- if (!ret )
235- ret = crypto_shash_final (& sdesc -> shash , paramdigest );
236- if (ret < 0 )
237- goto out ;
150+ sha1_final (& sha_ctx , paramdigest );
238151
239152 ret = TSS_rawhmac (testhmac , key , keylen , SHA1_DIGEST_SIZE , paramdigest ,
240153 TPM_NONCE_SIZE , enonce , TPM_NONCE_SIZE , ononce ,
241154 1 , continueflag , 0 , 0 );
242155 if (ret < 0 )
243- goto out ;
156+ return ret ;
244157
245158 if (crypto_memneq (testhmac , authdata , SHA1_DIGEST_SIZE ))
246- ret = - EINVAL ;
247- out :
248- kfree_sensitive (sdesc );
249- return ret ;
159+ return - EINVAL ;
160+ return 0 ;
250161}
251162EXPORT_SYMBOL_GPL (TSS_checkhmac1 );
252163
@@ -274,7 +185,7 @@ static int TSS_checkhmac2(unsigned char *buffer,
274185 unsigned char testhmac1 [SHA1_DIGEST_SIZE ];
275186 unsigned char testhmac2 [SHA1_DIGEST_SIZE ];
276187 unsigned char paramdigest [SHA1_DIGEST_SIZE ];
277- struct sdesc * sdesc ;
188+ struct sha1_ctx sha_ctx ;
278189 unsigned int dlen ;
279190 unsigned int dpos ;
280191 va_list argp ;
@@ -297,58 +208,36 @@ static int TSS_checkhmac2(unsigned char *buffer,
297208 enonce1 = continueflag1 - TPM_NONCE_SIZE ;
298209 enonce2 = continueflag2 - TPM_NONCE_SIZE ;
299210
300- sdesc = init_sdesc (hashalg );
301- if (IS_ERR (sdesc )) {
302- pr_info ("can't alloc %s\n" , hash_alg );
303- return PTR_ERR (sdesc );
304- }
305- ret = crypto_shash_init (& sdesc -> shash );
306- if (ret < 0 )
307- goto out ;
308- ret = crypto_shash_update (& sdesc -> shash , (const u8 * )& result ,
309- sizeof result );
310- if (ret < 0 )
311- goto out ;
312- ret = crypto_shash_update (& sdesc -> shash , (const u8 * )& ordinal ,
313- sizeof ordinal );
314- if (ret < 0 )
315- goto out ;
211+ sha1_init (& sha_ctx );
212+ sha1_update (& sha_ctx , (const u8 * )& result , sizeof (result ));
213+ sha1_update (& sha_ctx , (const u8 * )& ordinal , sizeof (ordinal ));
316214
317215 va_start (argp , keylen2 );
318216 for (;;) {
319217 dlen = va_arg (argp , unsigned int );
320218 if (dlen == 0 )
321219 break ;
322220 dpos = va_arg (argp , unsigned int );
323- ret = crypto_shash_update (& sdesc -> shash , buffer + dpos , dlen );
324- if (ret < 0 )
325- break ;
221+ sha1_update (& sha_ctx , buffer + dpos , dlen );
326222 }
327223 va_end (argp );
328- if (!ret )
329- ret = crypto_shash_final (& sdesc -> shash , paramdigest );
330- if (ret < 0 )
331- goto out ;
224+ sha1_final (& sha_ctx , paramdigest );
332225
333226 ret = TSS_rawhmac (testhmac1 , key1 , keylen1 , SHA1_DIGEST_SIZE ,
334227 paramdigest , TPM_NONCE_SIZE , enonce1 ,
335228 TPM_NONCE_SIZE , ononce , 1 , continueflag1 , 0 , 0 );
336229 if (ret < 0 )
337- goto out ;
338- if (crypto_memneq (testhmac1 , authdata1 , SHA1_DIGEST_SIZE )) {
339- ret = - EINVAL ;
340- goto out ;
341- }
230+ return ret ;
231+ if (crypto_memneq (testhmac1 , authdata1 , SHA1_DIGEST_SIZE ))
232+ return - EINVAL ;
342233 ret = TSS_rawhmac (testhmac2 , key2 , keylen2 , SHA1_DIGEST_SIZE ,
343234 paramdigest , TPM_NONCE_SIZE , enonce2 ,
344235 TPM_NONCE_SIZE , ononce , 1 , continueflag2 , 0 , 0 );
345236 if (ret < 0 )
346- goto out ;
237+ return ret ;
347238 if (crypto_memneq (testhmac2 , authdata2 , SHA1_DIGEST_SIZE ))
348- ret = - EINVAL ;
349- out :
350- kfree_sensitive (sdesc );
351- return ret ;
239+ return - EINVAL ;
240+ return 0 ;
352241}
353242
354243/*
@@ -499,9 +388,7 @@ static int tpm_seal(struct tpm_buf *tb, uint16_t keytype,
499388 /* calculate encrypted authorization value */
500389 memcpy (td -> xorwork , sess .secret , SHA1_DIGEST_SIZE );
501390 memcpy (td -> xorwork + SHA1_DIGEST_SIZE , sess .enonce , SHA1_DIGEST_SIZE );
502- ret = TSS_sha1 (td -> xorwork , SHA1_DIGEST_SIZE * 2 , td -> xorhash );
503- if (ret < 0 )
504- goto out ;
391+ sha1 (td -> xorwork , SHA1_DIGEST_SIZE * 2 , td -> xorhash );
505392
506393 ret = tpm_get_random (chip , td -> nonceodd , TPM_NONCE_SIZE );
507394 if (ret < 0 )
@@ -990,40 +877,6 @@ static int trusted_tpm_get_random(unsigned char *key, size_t key_len)
990877 return tpm_get_random (chip , key , key_len );
991878}
992879
993- static void trusted_shash_release (void )
994- {
995- if (hashalg )
996- crypto_free_shash (hashalg );
997- if (hmacalg )
998- crypto_free_shash (hmacalg );
999- }
1000-
1001- static int __init trusted_shash_alloc (void )
1002- {
1003- int ret ;
1004-
1005- hmacalg = crypto_alloc_shash (hmac_alg , 0 , 0 );
1006- if (IS_ERR (hmacalg )) {
1007- pr_info ("could not allocate crypto %s\n" ,
1008- hmac_alg );
1009- return PTR_ERR (hmacalg );
1010- }
1011-
1012- hashalg = crypto_alloc_shash (hash_alg , 0 , 0 );
1013- if (IS_ERR (hashalg )) {
1014- pr_info ("could not allocate crypto %s\n" ,
1015- hash_alg );
1016- ret = PTR_ERR (hashalg );
1017- goto hashalg_fail ;
1018- }
1019-
1020- return 0 ;
1021-
1022- hashalg_fail :
1023- crypto_free_shash (hmacalg );
1024- return ret ;
1025- }
1026-
1027880static int __init init_digests (void )
1028881{
1029882 int i ;
@@ -1050,15 +903,10 @@ static int __init trusted_tpm_init(void)
1050903 ret = init_digests ();
1051904 if (ret < 0 )
1052905 goto err_put ;
1053- ret = trusted_shash_alloc ();
1054- if (ret < 0 )
1055- goto err_free ;
1056906 ret = register_key_type (& key_type_trusted );
1057907 if (ret < 0 )
1058- goto err_release ;
908+ goto err_free ;
1059909 return 0 ;
1060- err_release :
1061- trusted_shash_release ();
1062910err_free :
1063911 kfree (digests );
1064912err_put :
@@ -1071,7 +919,6 @@ static void trusted_tpm_exit(void)
1071919 if (chip ) {
1072920 put_device (& chip -> dev );
1073921 kfree (digests );
1074- trusted_shash_release ();
1075922 unregister_key_type (& key_type_trusted );
1076923 }
1077924}
0 commit comments