1919#include <linux/scatterlist.h>
2020#include <linux/err.h>
2121#include <linux/slab.h>
22+ #include <crypto/hash.h>
2223#include "ima.h"
2324
24- static int init_desc (struct hash_desc * desc )
25+ static struct crypto_shash * ima_shash_tfm ;
26+
27+ int ima_init_crypto (void )
2528{
26- int rc ;
29+ long rc ;
2730
28- desc -> tfm = crypto_alloc_hash (ima_hash , 0 , CRYPTO_ALG_ASYNC );
29- if (IS_ERR (desc -> tfm )) {
30- pr_info ("IMA: failed to load %s transform: %ld\n" ,
31- ima_hash , PTR_ERR (desc -> tfm ));
32- rc = PTR_ERR (desc -> tfm );
31+ ima_shash_tfm = crypto_alloc_shash (ima_hash , 0 , 0 );
32+ if (IS_ERR (ima_shash_tfm )) {
33+ rc = PTR_ERR (ima_shash_tfm );
34+ pr_err ("Can not allocate %s (reason: %ld)\n" , ima_hash , rc );
3335 return rc ;
3436 }
35- desc -> flags = 0 ;
36- rc = crypto_hash_init (desc );
37- if (rc )
38- crypto_free_hash (desc -> tfm );
39- return rc ;
37+ return 0 ;
4038}
4139
4240/*
4341 * Calculate the MD5/SHA1 file digest
4442 */
4543int ima_calc_hash (struct file * file , char * digest )
4644{
47- struct hash_desc desc ;
48- struct scatterlist sg [1 ];
4945 loff_t i_size , offset = 0 ;
5046 char * rbuf ;
5147 int rc , read = 0 ;
48+ struct {
49+ struct shash_desc shash ;
50+ char ctx [crypto_shash_descsize (ima_shash_tfm )];
51+ } desc ;
5252
53- rc = init_desc (& desc );
53+ desc .shash .tfm = ima_shash_tfm ;
54+ desc .shash .flags = 0 ;
55+
56+ rc = crypto_shash_init (& desc .shash );
5457 if (rc != 0 )
5558 return rc ;
5659
@@ -75,19 +78,17 @@ int ima_calc_hash(struct file *file, char *digest)
7578 if (rbuf_len == 0 )
7679 break ;
7780 offset += rbuf_len ;
78- sg_init_one (sg , rbuf , rbuf_len );
7981
80- rc = crypto_hash_update (& desc , sg , rbuf_len );
82+ rc = crypto_shash_update (& desc . shash , rbuf , rbuf_len );
8183 if (rc )
8284 break ;
8385 }
8486 kfree (rbuf );
8587 if (!rc )
86- rc = crypto_hash_final (& desc , digest );
88+ rc = crypto_shash_final (& desc . shash , digest );
8789 if (read )
8890 file -> f_mode &= ~FMODE_READ ;
8991out :
90- crypto_free_hash (desc .tfm );
9192 return rc ;
9293}
9394
@@ -96,20 +97,15 @@ int ima_calc_hash(struct file *file, char *digest)
9697 */
9798int ima_calc_template_hash (int template_len , void * template , char * digest )
9899{
99- struct hash_desc desc ;
100- struct scatterlist sg [1 ];
101- int rc ;
100+ struct {
101+ struct shash_desc shash ;
102+ char ctx [crypto_shash_descsize (ima_shash_tfm )];
103+ } desc ;
102104
103- rc = init_desc (& desc );
104- if (rc != 0 )
105- return rc ;
105+ desc .shash .tfm = ima_shash_tfm ;
106+ desc .shash .flags = 0 ;
106107
107- sg_init_one (sg , template , template_len );
108- rc = crypto_hash_update (& desc , sg , template_len );
109- if (!rc )
110- rc = crypto_hash_final (& desc , digest );
111- crypto_free_hash (desc .tfm );
112- return rc ;
108+ return crypto_shash_digest (& desc .shash , template , template_len , digest );
113109}
114110
115111static void __init ima_pcrread (int idx , u8 * pcr )
@@ -126,24 +122,27 @@ static void __init ima_pcrread(int idx, u8 *pcr)
126122 */
127123int __init ima_calc_boot_aggregate (char * digest )
128124{
129- struct hash_desc desc ;
130- struct scatterlist sg ;
131125 u8 pcr_i [IMA_DIGEST_SIZE ];
132126 int rc , i ;
127+ struct {
128+ struct shash_desc shash ;
129+ char ctx [crypto_shash_descsize (ima_shash_tfm )];
130+ } desc ;
131+
132+ desc .shash .tfm = ima_shash_tfm ;
133+ desc .shash .flags = 0 ;
133134
134- rc = init_desc (& desc );
135+ rc = crypto_shash_init (& desc . shash );
135136 if (rc != 0 )
136137 return rc ;
137138
138139 /* cumulative sha1 over tpm registers 0-7 */
139140 for (i = TPM_PCR0 ; i < TPM_PCR8 ; i ++ ) {
140141 ima_pcrread (i , pcr_i );
141142 /* now accumulate with current aggregate */
142- sg_init_one (& sg , pcr_i , IMA_DIGEST_SIZE );
143- rc = crypto_hash_update (& desc , & sg , IMA_DIGEST_SIZE );
143+ rc = crypto_shash_update (& desc .shash , pcr_i , IMA_DIGEST_SIZE );
144144 }
145145 if (!rc )
146- crypto_hash_final (& desc , digest );
147- crypto_free_hash (desc .tfm );
146+ crypto_shash_final (& desc .shash , digest );
148147 return rc ;
149148}
0 commit comments