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