1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include <linux/crypto.h>
31#include <linux/scatterlist.h>
32#include "../../../include/linux/libcfs/libcfs.h"
33#include "linux-crypto.h"
34
35
36
37static int cfs_crypto_hash_speeds[CFS_HASH_ALG_MAX];
38
39
40
41static int cfs_crypto_hash_alloc(unsigned char alg_id,
42 const struct cfs_crypto_hash_type **type,
43 struct hash_desc *desc, unsigned char *key,
44 unsigned int key_len)
45{
46 int err = 0;
47
48 *type = cfs_crypto_hash_type(alg_id);
49
50 if (*type == NULL) {
51 CWARN("Unsupported hash algorithm id = %d, max id is %d\n",
52 alg_id, CFS_HASH_ALG_MAX);
53 return -EINVAL;
54 }
55 desc->tfm = crypto_alloc_hash((*type)->cht_name, 0, 0);
56
57 if (desc->tfm == NULL)
58 return -EINVAL;
59
60 if (IS_ERR(desc->tfm)) {
61 CDEBUG(D_INFO, "Failed to alloc crypto hash %s\n",
62 (*type)->cht_name);
63 return PTR_ERR(desc->tfm);
64 }
65
66 desc->flags = 0;
67
68
69
70
71
72
73
74 if (key != NULL) {
75 err = crypto_hash_setkey(desc->tfm, key, key_len);
76 } else if ((*type)->cht_key != 0) {
77 err = crypto_hash_setkey(desc->tfm,
78 (unsigned char *)&((*type)->cht_key),
79 (*type)->cht_size);
80 }
81
82 if (err != 0) {
83 crypto_free_hash(desc->tfm);
84 return err;
85 }
86
87 CDEBUG(D_INFO, "Using crypto hash: %s (%s) speed %d MB/s\n",
88 (crypto_hash_tfm(desc->tfm))->__crt_alg->cra_name,
89 (crypto_hash_tfm(desc->tfm))->__crt_alg->cra_driver_name,
90 cfs_crypto_hash_speeds[alg_id]);
91
92 return crypto_hash_init(desc);
93}
94
95int cfs_crypto_hash_digest(unsigned char alg_id,
96 const void *buf, unsigned int buf_len,
97 unsigned char *key, unsigned int key_len,
98 unsigned char *hash, unsigned int *hash_len)
99{
100 struct scatterlist sl;
101 struct hash_desc hdesc;
102 int err;
103 const struct cfs_crypto_hash_type *type;
104
105 if (buf == NULL || buf_len == 0 || hash_len == NULL)
106 return -EINVAL;
107
108 err = cfs_crypto_hash_alloc(alg_id, &type, &hdesc, key, key_len);
109 if (err != 0)
110 return err;
111
112 if (hash == NULL || *hash_len < type->cht_size) {
113 *hash_len = type->cht_size;
114 crypto_free_hash(hdesc.tfm);
115 return -ENOSPC;
116 }
117 sg_init_one(&sl, (void *)buf, buf_len);
118
119 hdesc.flags = 0;
120 err = crypto_hash_digest(&hdesc, &sl, sl.length, hash);
121 crypto_free_hash(hdesc.tfm);
122
123 return err;
124}
125EXPORT_SYMBOL(cfs_crypto_hash_digest);
126
127struct cfs_crypto_hash_desc *
128 cfs_crypto_hash_init(unsigned char alg_id,
129 unsigned char *key, unsigned int key_len)
130{
131
132 struct hash_desc *hdesc;
133 int err;
134 const struct cfs_crypto_hash_type *type;
135
136 hdesc = kmalloc(sizeof(*hdesc), 0);
137 if (hdesc == NULL)
138 return ERR_PTR(-ENOMEM);
139
140 err = cfs_crypto_hash_alloc(alg_id, &type, hdesc, key, key_len);
141
142 if (err) {
143 kfree(hdesc);
144 return ERR_PTR(err);
145 }
146 return (struct cfs_crypto_hash_desc *)hdesc;
147}
148EXPORT_SYMBOL(cfs_crypto_hash_init);
149
150int cfs_crypto_hash_update_page(struct cfs_crypto_hash_desc *hdesc,
151 struct page *page, unsigned int offset,
152 unsigned int len)
153{
154 struct scatterlist sl;
155
156 sg_init_table(&sl, 1);
157 sg_set_page(&sl, page, len, offset & ~CFS_PAGE_MASK);
158
159 return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
160}
161EXPORT_SYMBOL(cfs_crypto_hash_update_page);
162
163int cfs_crypto_hash_update(struct cfs_crypto_hash_desc *hdesc,
164 const void *buf, unsigned int buf_len)
165{
166 struct scatterlist sl;
167
168 sg_init_one(&sl, (void *)buf, buf_len);
169
170 return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
171}
172EXPORT_SYMBOL(cfs_crypto_hash_update);
173
174
175int cfs_crypto_hash_final(struct cfs_crypto_hash_desc *hdesc,
176 unsigned char *hash, unsigned int *hash_len)
177{
178 int err;
179 int size = crypto_hash_digestsize(((struct hash_desc *)hdesc)->tfm);
180
181 if (hash_len == NULL) {
182 crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
183 kfree(hdesc);
184 return 0;
185 }
186 if (hash == NULL || *hash_len < size) {
187 *hash_len = size;
188 return -ENOSPC;
189 }
190 err = crypto_hash_final((struct hash_desc *) hdesc, hash);
191
192 if (err < 0) {
193
194 return err;
195 }
196 crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
197 kfree(hdesc);
198 return err;
199}
200EXPORT_SYMBOL(cfs_crypto_hash_final);
201
202static void cfs_crypto_performance_test(unsigned char alg_id,
203 const unsigned char *buf,
204 unsigned int buf_len)
205{
206 unsigned long start, end;
207 int bcount, err = 0;
208 int sec = 1;
209 unsigned char hash[64];
210 unsigned int hash_len = 64;
211
212 for (start = jiffies, end = start + sec * HZ, bcount = 0;
213 time_before(jiffies, end); bcount++) {
214 err = cfs_crypto_hash_digest(alg_id, buf, buf_len, NULL, 0,
215 hash, &hash_len);
216 if (err)
217 break;
218
219 }
220 end = jiffies;
221
222 if (err) {
223 cfs_crypto_hash_speeds[alg_id] = -1;
224 CDEBUG(D_INFO, "Crypto hash algorithm %s, err = %d\n",
225 cfs_crypto_hash_name(alg_id), err);
226 } else {
227 unsigned long tmp;
228 tmp = ((bcount * buf_len / jiffies_to_msecs(end - start)) *
229 1000) / (1024 * 1024);
230 cfs_crypto_hash_speeds[alg_id] = (int)tmp;
231 }
232 CDEBUG(D_INFO, "Crypto hash algorithm %s speed = %d MB/s\n",
233 cfs_crypto_hash_name(alg_id), cfs_crypto_hash_speeds[alg_id]);
234}
235
236int cfs_crypto_hash_speed(unsigned char hash_alg)
237{
238 if (hash_alg < CFS_HASH_ALG_MAX)
239 return cfs_crypto_hash_speeds[hash_alg];
240 else
241 return -1;
242}
243EXPORT_SYMBOL(cfs_crypto_hash_speed);
244
245
246
247
248static int cfs_crypto_test_hashes(void)
249{
250 unsigned char i;
251 unsigned char *data;
252 unsigned int j;
253
254
255 unsigned int data_len = 1 * 128 * 1024;
256
257 data = kmalloc(data_len, 0);
258 if (data == NULL) {
259 CERROR("Failed to allocate mem\n");
260 return -ENOMEM;
261 }
262
263 for (j = 0; j < data_len; j++)
264 data[j] = j & 0xff;
265
266 for (i = 0; i < CFS_HASH_ALG_MAX; i++)
267 cfs_crypto_performance_test(i, data, data_len);
268
269 kfree(data);
270 return 0;
271}
272
273static int adler32;
274
275int cfs_crypto_register(void)
276{
277 request_module("crc32c");
278
279 adler32 = cfs_crypto_adler32_register();
280
281
282 cfs_crypto_test_hashes();
283 return 0;
284}
285void cfs_crypto_unregister(void)
286{
287 if (adler32 == 0)
288 cfs_crypto_adler32_unregister();
289
290 return;
291}
292