diff options
author | Dmitry Kasatkin <d.kasatkin@samsung.com> | 2013-07-04 10:40:01 -0400 |
---|---|---|
committer | Mimi Zohar <zohar@linux.vnet.ibm.com> | 2013-10-25 17:17:01 -0400 |
commit | 723326b927b675daf4223fe31d7428eca68f194b (patch) | |
tree | 499ca34fc60793d66ae07dc9ee453a401bef3cc8 /security/integrity/ima/ima_crypto.c | |
parent | 140d802240a4ba3351494b4ab199964b96f87493 (diff) |
ima: provide dedicated hash algo allocation function
This patch provides dedicated hash algo allocation and
deallocation function which can be used by different clients.
Signed-off-by: Dmitry Kasatkin <d.kasatkin@samsung.com>
Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
Diffstat (limited to 'security/integrity/ima/ima_crypto.c')
-rw-r--r-- | security/integrity/ima/ima_crypto.c | 43 |
1 files changed, 29 insertions, 14 deletions
diff --git a/security/integrity/ima/ima_crypto.c b/security/integrity/ima/ima_crypto.c index 872c6698067c..e5d3ebf18436 100644 --- a/security/integrity/ima/ima_crypto.c +++ b/security/integrity/ima/ima_crypto.c | |||
@@ -39,6 +39,28 @@ int ima_init_crypto(void) | |||
39 | return 0; | 39 | return 0; |
40 | } | 40 | } |
41 | 41 | ||
42 | static struct crypto_shash *ima_alloc_tfm(enum hash_algo algo) | ||
43 | { | ||
44 | struct crypto_shash *tfm = ima_shash_tfm; | ||
45 | int rc; | ||
46 | |||
47 | if (algo != ima_hash_algo && algo < HASH_ALGO__LAST) { | ||
48 | tfm = crypto_alloc_shash(hash_algo_name[algo], 0, 0); | ||
49 | if (IS_ERR(tfm)) { | ||
50 | rc = PTR_ERR(tfm); | ||
51 | pr_err("Can not allocate %s (reason: %d)\n", | ||
52 | hash_algo_name[algo], rc); | ||
53 | } | ||
54 | } | ||
55 | return tfm; | ||
56 | } | ||
57 | |||
58 | static void ima_free_tfm(struct crypto_shash *tfm) | ||
59 | { | ||
60 | if (tfm != ima_shash_tfm) | ||
61 | crypto_free_shash(tfm); | ||
62 | } | ||
63 | |||
42 | /* | 64 | /* |
43 | * Calculate the MD5/SHA1 file digest | 65 | * Calculate the MD5/SHA1 file digest |
44 | */ | 66 | */ |
@@ -57,6 +79,8 @@ static int ima_calc_file_hash_tfm(struct file *file, | |||
57 | desc.shash.tfm = tfm; | 79 | desc.shash.tfm = tfm; |
58 | desc.shash.flags = 0; | 80 | desc.shash.flags = 0; |
59 | 81 | ||
82 | hash->length = crypto_shash_digestsize(tfm); | ||
83 | |||
60 | rc = crypto_shash_init(&desc.shash); | 84 | rc = crypto_shash_init(&desc.shash); |
61 | if (rc != 0) | 85 | if (rc != 0) |
62 | return rc; | 86 | return rc; |
@@ -98,25 +122,16 @@ out: | |||
98 | 122 | ||
99 | int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash) | 123 | int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash) |
100 | { | 124 | { |
101 | struct crypto_shash *tfm = ima_shash_tfm; | 125 | struct crypto_shash *tfm; |
102 | int rc; | 126 | int rc; |
103 | 127 | ||
104 | if (hash->algo != ima_hash_algo && hash->algo < HASH_ALGO__LAST) { | 128 | tfm = ima_alloc_tfm(hash->algo); |
105 | tfm = crypto_alloc_shash(hash_algo_name[hash->algo], 0, 0); | 129 | if (IS_ERR(tfm)) |
106 | if (IS_ERR(tfm)) { | 130 | return PTR_ERR(tfm); |
107 | rc = PTR_ERR(tfm); | ||
108 | pr_err("Can not allocate %s (reason: %d)\n", | ||
109 | hash_algo_name[hash->algo], rc); | ||
110 | return rc; | ||
111 | } | ||
112 | } | ||
113 | |||
114 | hash->length = crypto_shash_digestsize(tfm); | ||
115 | 131 | ||
116 | rc = ima_calc_file_hash_tfm(file, hash, tfm); | 132 | rc = ima_calc_file_hash_tfm(file, hash, tfm); |
117 | 133 | ||
118 | if (tfm != ima_shash_tfm) | 134 | ima_free_tfm(tfm); |
119 | crypto_free_shash(tfm); | ||
120 | 135 | ||
121 | return rc; | 136 | return rc; |
122 | } | 137 | } |