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