Whamcloud - gitweb
LU-1339 libcfs: add crc32 pclmulqdq implementation
[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                 return 0;
253         }
254         if (hash == NULL || *hash_len < size) {
255                 *hash_len = size;
256                 return -ENOSPC;
257         }
258         err = crypto_hash_final((struct hash_desc *) hdesc, hash);
259
260         if (err < 0) {
261                 /* May be caller can fix error */
262                 return err;
263         }
264         crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
265         return err;
266 }
267 EXPORT_SYMBOL(cfs_crypto_hash_final);
268
269 static void cfs_crypto_performance_test(unsigned char alg_id,
270                                         const unsigned char *buf,
271                                         unsigned int buf_len)
272 {
273         unsigned long              start, end;
274         int                          bcount, err = 0;
275         int                          sec = 1; /* do test only 1 sec */
276         unsigned char              hash[64];
277         unsigned int                hash_len = 64;
278
279         for (start = jiffies, end = start + sec * HZ, bcount = 0;
280              time_before(jiffies, end); bcount++) {
281                 err = cfs_crypto_hash_digest(alg_id, buf, buf_len, NULL, 0,
282                                              hash, &hash_len);
283                 if (err)
284                         break;
285
286         }
287         end = jiffies;
288
289         if (err) {
290                 cfs_crypto_hash_speeds[alg_id] =  -1;
291                 CDEBUG(D_INFO, "Crypto hash algorithm %s, err = %d\n",
292                        cfs_crypto_hash_name(alg_id), err);
293         } else {
294                 unsigned long   tmp;
295                 tmp = ((bcount * buf_len / jiffies_to_msecs(end - start)) *
296                        1000) / (1024 * 1024);
297                 cfs_crypto_hash_speeds[alg_id] = (int)tmp;
298         }
299         CDEBUG(D_INFO, "Crypto hash algorithm %s speed = %d MB/s\n",
300                cfs_crypto_hash_name(alg_id), cfs_crypto_hash_speeds[alg_id]);
301 }
302
303 int cfs_crypto_hash_speed(unsigned char hash_alg)
304 {
305         if (hash_alg < CFS_HASH_ALG_MAX)
306                 return cfs_crypto_hash_speeds[hash_alg];
307         else
308                 return -1;
309 }
310 EXPORT_SYMBOL(cfs_crypto_hash_speed);
311
312 /**
313  * Do performance test for all hash algorithms.
314  */
315 static int cfs_crypto_test_hashes(void)
316 {
317         unsigned char      i;
318         unsigned char      *data;
319         unsigned int        j;
320         /* Data block size for testing hash. Maximum
321          * kmalloc size for 2.6.18 kernel is 128K */
322         unsigned int        data_len = 1 * 128 * 1024;
323
324         data = cfs_alloc(data_len, 0);
325         if (data == NULL) {
326                 CERROR("Failed to allocate mem\n");
327                 return -ENOMEM;
328         }
329
330         for (j = 0; j < data_len; j++)
331                 data[j] = j & 0xff;
332
333         for (i = 0; i < CFS_HASH_ALG_MAX; i++)
334                 cfs_crypto_performance_test(i, data, data_len);
335
336         cfs_free(data);
337         return 0;
338 }
339
340 static int crc32, adler32;
341
342 #ifdef CONFIG_X86
343 static int crc32pclmul;
344 #endif
345
346 int cfs_crypto_register(void)
347 {
348         crc32 = cfs_crypto_crc32_register();
349         adler32 = cfs_crypto_adler32_register();
350
351 #ifdef CONFIG_X86
352         crc32pclmul = cfs_crypto_crc32_pclmul_register();
353 #endif
354
355         /* check all algorithms and do perfermance test */
356         cfs_crypto_test_hashes();
357         return 0;
358 }
359 void cfs_crypto_unregister(void)
360 {
361         if (crc32 == 0)
362                 cfs_crypto_crc32_unregister();
363         if (adler32 == 0)
364                 cfs_crypto_adler32_unregister();
365
366 #ifdef CONFIG_X86
367         if (crc32pclmul == 0)
368                 cfs_crypto_crc32_pclmul_unregister();
369 #endif
370
371         return;
372 }