Whamcloud - gitweb
LU-2053 crypto: Fix cfs_crypto_hash memleak
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-crypto.c
1 /* GPL HEADER START
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 only,
7  * as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License version 2 for more details (a copy is included
13  * in the LICENSE file that accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License
16  * version 2 along with this program; If not, see http://www.gnu.org/licenses
17  *
18  * Please  visit http://www.xyratex.com/contact if you need additional
19  * information or have any questions.
20  *
21  * GPL HEADER END
22  */
23
24 /*
25  * Copyright 2012 Xyratex Technology Limited
26  */
27
28 #include <linux/crypto.h>
29 #include <linux/scatterlist.h>
30 #include <libcfs/libcfs.h>
31 #include <libcfs/linux/linux-crypto.h>
32 /**
33  *  Array of  hash algorithm speed in MByte per second
34  */
35 static int cfs_crypto_hash_speeds[CFS_HASH_ALG_MAX];
36
37
38 #ifndef HAVE_STRUCT_HASH_DESC
39 /** 2.6.18 kernel have no crypto_hash function
40  *  this part was copied from lustre_compat25.h */
41 #define crypto_hash     crypto_tfm
42 struct hash_desc {
43         struct crypto_hash      *tfm;
44         unsigned int            flags;
45 };
46
47 static inline
48 struct crypto_hash *crypto_alloc_hash(const char *alg, unsigned int type,
49                                       unsigned int mask)
50 {
51         return crypto_alloc_tfm(alg, 0);
52 }
53
54 static inline void crypto_free_hash(struct crypto_hash *tfm)
55 {
56         crypto_free_tfm(tfm);
57 }
58
59 static inline int crypto_hash_init(struct hash_desc *desc)
60 {
61         crypto_digest_init(desc->tfm);
62         return 0;
63 }
64
65 static inline int crypto_hash_update(struct hash_desc *desc,
66                                      struct scatterlist *sg,
67                                      unsigned int nbytes)
68 {
69         if (desc->tfm->crt_digest.dit_update == NULL)
70                 return -1;
71
72         LASSERT(nbytes == sg->length);
73         crypto_digest_update(desc->tfm, sg, 1);
74
75         return 0;
76 }
77
78 static inline int crypto_hash_digest(struct hash_desc *desc,
79                                      struct scatterlist *sg,
80                                      unsigned int nbytes, unsigned char *out)
81 {
82         crypto_hash_update(desc, sg, nbytes);
83         crypto_digest_final(desc->tfm, out);
84         return 0;
85 }
86
87 static inline int crypto_hash_final(struct hash_desc *desc, unsigned char *out)
88 {
89         crypto_digest_final(desc->tfm, out);
90         return 0;
91 }
92
93 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
94 {
95         return tfm;
96 }
97
98 #define crypto_hash_setkey(tfm, key, keylen) \
99                 crypto_digest_setkey(tfm, key, keylen)
100 #define crypto_hash_digestsize(tfm)  crypto_tfm_alg_digestsize(tfm)
101 #define crypto_hash_blocksize(tfm)   crypto_tfm_alg_blocksize(tfm)
102 #endif
103
104 static int cfs_crypto_hash_alloc(unsigned char alg_id,
105                                  const struct cfs_crypto_hash_type **type,
106                                  struct hash_desc *desc, unsigned char *key,
107                                  unsigned int key_len)
108 {
109         int     err = 0;
110
111         *type = cfs_crypto_hash_type(alg_id);
112
113         if (*type == NULL) {
114                 CWARN("Unsupported hash algorithm id = %d, max id is %d\n",
115                       alg_id, CFS_HASH_ALG_MAX);
116                 return -EINVAL;
117         }
118         desc->tfm = crypto_alloc_hash((*type)->cht_name, 0, 0);
119
120         if (desc->tfm == NULL)
121                 return -EINVAL;
122
123         if (IS_ERR(desc->tfm)) {
124                 CDEBUG(D_INFO, "Failed to alloc crypto hash %s\n",
125                        (*type)->cht_name);
126                 return PTR_ERR(desc->tfm);
127         }
128
129         desc->flags = 0;
130
131         /** Shash have different logic for initialization then digest
132          * shash: crypto_hash_setkey, crypto_hash_init
133          * digest: crypto_digest_init, crypto_digest_setkey
134          * Skip this function for digest, because we use shash logic at
135          * cfs_crypto_hash_alloc.
136          */
137 #ifndef HAVE_STRUCT_SHASH_ALG
138         crypto_hash_init(desc);
139 #endif
140         if (key != NULL) {
141                 err = crypto_hash_setkey(desc->tfm, key, key_len);
142         } else if ((*type)->cht_key != 0) {
143                 err = crypto_hash_setkey(desc->tfm,
144                                          (unsigned char *)&((*type)->cht_key),
145                                          (*type)->cht_size);
146         }
147
148         if (err != 0) {
149                 crypto_free_hash(desc->tfm);
150                 return err;
151         }
152
153         CDEBUG(D_INFO, "Using crypto hash: %s (%s) speed %d MB/s\n",
154                (crypto_hash_tfm(desc->tfm))->__crt_alg->cra_name,
155                (crypto_hash_tfm(desc->tfm))->__crt_alg->cra_driver_name,
156                cfs_crypto_hash_speeds[alg_id]);
157
158 #ifdef HAVE_STRUCT_SHASH_ALG
159         return crypto_hash_init(desc);
160 #else
161         return 0;
162 #endif
163 }
164
165 int cfs_crypto_hash_digest(unsigned char alg_id,
166                            const void *buf, unsigned int buf_len,
167                            unsigned char *key, unsigned int key_len,
168                            unsigned char *hash, unsigned int *hash_len)
169 {
170         struct scatterlist      sl = {0};
171         struct hash_desc        hdesc;
172         int                     err;
173         const struct cfs_crypto_hash_type       *type;
174
175         if (buf == NULL || buf_len == 0 || hash_len == NULL)
176                 return -EINVAL;
177
178         err = cfs_crypto_hash_alloc(alg_id, &type, &hdesc, key, key_len);
179         if (err != 0)
180                 return err;
181
182         if (hash == NULL || *hash_len < type->cht_size) {
183                 *hash_len = type->cht_size;
184                 crypto_free_hash(hdesc.tfm);
185                 return -ENOSPC;
186         }
187         sg_set_buf(&sl, (void *)buf, buf_len);
188
189         hdesc.flags = 0;
190         err = crypto_hash_digest(&hdesc, &sl, sl.length, hash);
191         crypto_free_hash(hdesc.tfm);
192
193         return err;
194 }
195 EXPORT_SYMBOL(cfs_crypto_hash_digest);
196
197 struct cfs_crypto_hash_desc *
198         cfs_crypto_hash_init(unsigned char alg_id,
199                              unsigned char *key, unsigned int key_len)
200 {
201
202         struct  hash_desc       *hdesc;
203         int                  err;
204         const struct cfs_crypto_hash_type       *type;
205
206         hdesc = cfs_alloc(sizeof(*hdesc), 0);
207         if (hdesc == NULL)
208                 return ERR_PTR(-ENOMEM);
209
210         err = cfs_crypto_hash_alloc(alg_id, &type, hdesc, key, key_len);
211
212         if (err) {
213                 cfs_free(hdesc);
214                 return ERR_PTR(err);
215         }
216         return (struct cfs_crypto_hash_desc *)hdesc;
217 }
218 EXPORT_SYMBOL(cfs_crypto_hash_init);
219
220 int cfs_crypto_hash_update_page(struct cfs_crypto_hash_desc *hdesc,
221                                 cfs_page_t *page, unsigned int offset,
222                                 unsigned int len)
223 {
224         struct scatterlist      sl = {0};
225
226         sg_set_page(&sl, page, len, offset & ~CFS_PAGE_MASK);
227
228         return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
229 }
230 EXPORT_SYMBOL(cfs_crypto_hash_update_page);
231
232 int cfs_crypto_hash_update(struct cfs_crypto_hash_desc *hdesc,
233                            const void *buf, unsigned int buf_len)
234 {
235         struct scatterlist      sl = {0};
236
237         sg_set_buf(&sl, (void *)buf, buf_len);
238
239         return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
240 }
241 EXPORT_SYMBOL(cfs_crypto_hash_update);
242
243 /*      If hash_len pointer is NULL - destroy descriptor. */
244 int cfs_crypto_hash_final(struct cfs_crypto_hash_desc *hdesc,
245                           unsigned char *hash, unsigned int *hash_len)
246 {
247         int     err;
248         int     size = crypto_hash_digestsize(((struct hash_desc *)hdesc)->tfm);
249
250         if (hash_len == NULL) {
251                 crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
252                 cfs_free(hdesc);
253                 return 0;
254         }
255         if (hash == NULL || *hash_len < size) {
256                 *hash_len = size;
257                 return -ENOSPC;
258         }
259         err = crypto_hash_final((struct hash_desc *) hdesc, hash);
260
261         if (err < 0) {
262                 /* May be caller can fix error */
263                 return err;
264         }
265         crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
266         cfs_free(hdesc);
267         return err;
268 }
269 EXPORT_SYMBOL(cfs_crypto_hash_final);
270
271 static void cfs_crypto_performance_test(unsigned char alg_id,
272                                         const unsigned char *buf,
273                                         unsigned int buf_len)
274 {
275         unsigned long              start, end;
276         int                          bcount, err = 0;
277         int                          sec = 1; /* do test only 1 sec */
278         unsigned char              hash[64];
279         unsigned int                hash_len = 64;
280
281         for (start = jiffies, end = start + sec * HZ, bcount = 0;
282              time_before(jiffies, end); bcount++) {
283                 err = cfs_crypto_hash_digest(alg_id, buf, buf_len, NULL, 0,
284                                              hash, &hash_len);
285                 if (err)
286                         break;
287
288         }
289         end = jiffies;
290
291         if (err) {
292                 cfs_crypto_hash_speeds[alg_id] =  -1;
293                 CDEBUG(D_INFO, "Crypto hash algorithm %s, err = %d\n",
294                        cfs_crypto_hash_name(alg_id), err);
295         } else {
296                 unsigned long   tmp;
297                 tmp = ((bcount * buf_len / jiffies_to_msecs(end - start)) *
298                        1000) / (1024 * 1024);
299                 cfs_crypto_hash_speeds[alg_id] = (int)tmp;
300         }
301         CDEBUG(D_INFO, "Crypto hash algorithm %s speed = %d MB/s\n",
302                cfs_crypto_hash_name(alg_id), cfs_crypto_hash_speeds[alg_id]);
303 }
304
305 int cfs_crypto_hash_speed(unsigned char hash_alg)
306 {
307         if (hash_alg < CFS_HASH_ALG_MAX)
308                 return cfs_crypto_hash_speeds[hash_alg];
309         else
310                 return -1;
311 }
312 EXPORT_SYMBOL(cfs_crypto_hash_speed);
313
314 /**
315  * Do performance test for all hash algorithms.
316  */
317 static int cfs_crypto_test_hashes(void)
318 {
319         unsigned char      i;
320         unsigned char      *data;
321         unsigned int        j;
322         /* Data block size for testing hash. Maximum
323          * kmalloc size for 2.6.18 kernel is 128K */
324         unsigned int        data_len = 1 * 128 * 1024;
325
326         data = cfs_alloc(data_len, 0);
327         if (data == NULL) {
328                 CERROR("Failed to allocate mem\n");
329                 return -ENOMEM;
330         }
331
332         for (j = 0; j < data_len; j++)
333                 data[j] = j & 0xff;
334
335         for (i = 0; i < CFS_HASH_ALG_MAX; i++)
336                 cfs_crypto_performance_test(i, data, data_len);
337
338         cfs_free(data);
339         return 0;
340 }
341
342 static int crc32, adler32;
343
344 #ifdef CONFIG_X86
345 static int crc32pclmul;
346 #endif
347
348 int cfs_crypto_register(void)
349 {
350         crc32 = cfs_crypto_crc32_register();
351         adler32 = cfs_crypto_adler32_register();
352
353 #ifdef CONFIG_X86
354         crc32pclmul = cfs_crypto_crc32_pclmul_register();
355 #endif
356
357         /* check all algorithms and do perfermance test */
358         cfs_crypto_test_hashes();
359         return 0;
360 }
361 void cfs_crypto_unregister(void)
362 {
363         if (crc32 == 0)
364                 cfs_crypto_crc32_unregister();
365         if (adler32 == 0)
366                 cfs_crypto_adler32_unregister();
367
368 #ifdef CONFIG_X86
369         if (crc32pclmul == 0)
370                 cfs_crypto_crc32_pclmul_unregister();
371 #endif
372
373         return;
374 }