]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/lustre/lustre/libcfs/linux/linux-crypto.c
staging/lustre: Remove space after cast in cfs_crypto_hash_final()
[karo-tx-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) {
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)
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)
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 || buf_len == 0 || !hash_len)
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 || *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         struct  hash_desc       *hdesc;
129         int                  err;
130         const struct cfs_crypto_hash_type       *type;
131
132         hdesc = kmalloc(sizeof(*hdesc), 0);
133         if (!hdesc)
134                 return ERR_PTR(-ENOMEM);
135
136         err = cfs_crypto_hash_alloc(alg_id, &type, hdesc, key, key_len);
137
138         if (err) {
139                 kfree(hdesc);
140                 return ERR_PTR(err);
141         }
142         return (struct cfs_crypto_hash_desc *)hdesc;
143 }
144 EXPORT_SYMBOL(cfs_crypto_hash_init);
145
146 int cfs_crypto_hash_update_page(struct cfs_crypto_hash_desc *hdesc,
147                                 struct page *page, unsigned int offset,
148                                 unsigned int len)
149 {
150         struct scatterlist sl;
151
152         sg_init_table(&sl, 1);
153         sg_set_page(&sl, page, len, offset & ~CFS_PAGE_MASK);
154
155         return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
156 }
157 EXPORT_SYMBOL(cfs_crypto_hash_update_page);
158
159 int cfs_crypto_hash_update(struct cfs_crypto_hash_desc *hdesc,
160                            const void *buf, unsigned int buf_len)
161 {
162         struct scatterlist sl;
163
164         sg_init_one(&sl, buf, buf_len);
165
166         return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
167 }
168 EXPORT_SYMBOL(cfs_crypto_hash_update);
169
170 /*      If hash_len pointer is NULL - destroy descriptor. */
171 int cfs_crypto_hash_final(struct cfs_crypto_hash_desc *hdesc,
172                           unsigned char *hash, unsigned int *hash_len)
173 {
174         int     err;
175         int     size = crypto_hash_digestsize(((struct hash_desc *)hdesc)->tfm);
176
177         if (!hash_len) {
178                 crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
179                 kfree(hdesc);
180                 return 0;
181         }
182         if (!hash || *hash_len < size) {
183                 *hash_len = size;
184                 return -ENOSPC;
185         }
186         err = crypto_hash_final((struct hash_desc *)hdesc, hash);
187
188         if (err < 0) {
189                 /* May be caller can fix error */
190                 return err;
191         }
192         crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
193         kfree(hdesc);
194         return err;
195 }
196 EXPORT_SYMBOL(cfs_crypto_hash_final);
197
198 static void cfs_crypto_performance_test(unsigned char alg_id,
199                                         const unsigned char *buf,
200                                         unsigned int buf_len)
201 {
202         unsigned long              start, end;
203         int                          bcount, err = 0;
204         int                          sec = 1; /* do test only 1 sec */
205         unsigned char              hash[64];
206         unsigned int                hash_len = 64;
207
208         for (start = jiffies, end = start + sec * HZ, bcount = 0;
209              time_before(jiffies, end); bcount++) {
210                 err = cfs_crypto_hash_digest(alg_id, buf, buf_len, NULL, 0,
211                                              hash, &hash_len);
212                 if (err)
213                         break;
214         }
215         end = jiffies;
216
217         if (err) {
218                 cfs_crypto_hash_speeds[alg_id] =  -1;
219                 CDEBUG(D_INFO, "Crypto hash algorithm %s, err = %d\n",
220                        cfs_crypto_hash_name(alg_id), err);
221         } else {
222                 unsigned long   tmp;
223
224                 tmp = ((bcount * buf_len / jiffies_to_msecs(end - start)) *
225                        1000) / (1024 * 1024);
226                 cfs_crypto_hash_speeds[alg_id] = (int)tmp;
227         }
228         CDEBUG(D_INFO, "Crypto hash algorithm %s speed = %d MB/s\n",
229                cfs_crypto_hash_name(alg_id), cfs_crypto_hash_speeds[alg_id]);
230 }
231
232 int cfs_crypto_hash_speed(unsigned char hash_alg)
233 {
234         if (hash_alg < CFS_HASH_ALG_MAX)
235                 return cfs_crypto_hash_speeds[hash_alg];
236         else
237                 return -1;
238 }
239 EXPORT_SYMBOL(cfs_crypto_hash_speed);
240
241 /**
242  * Do performance test for all hash algorithms.
243  */
244 static int cfs_crypto_test_hashes(void)
245 {
246         unsigned char      i;
247         unsigned char      *data;
248         unsigned int        j;
249         /* Data block size for testing hash. Maximum
250          * kmalloc size for 2.6.18 kernel is 128K
251          */
252         unsigned int        data_len = 1 * 128 * 1024;
253
254         data = kmalloc(data_len, 0);
255         if (!data) {
256                 CERROR("Failed to allocate mem\n");
257                 return -ENOMEM;
258         }
259
260         for (j = 0; j < data_len; j++)
261                 data[j] = j & 0xff;
262
263         for (i = 0; i < CFS_HASH_ALG_MAX; i++)
264                 cfs_crypto_performance_test(i, data, data_len);
265
266         kfree(data);
267         return 0;
268 }
269
270 static int adler32;
271
272 int cfs_crypto_register(void)
273 {
274         request_module("crc32c");
275
276         adler32 = cfs_crypto_adler32_register();
277
278         /* check all algorithms and do performance test */
279         cfs_crypto_test_hashes();
280         return 0;
281 }
282
283 void cfs_crypto_unregister(void)
284 {
285         if (adler32 == 0)
286                 cfs_crypto_adler32_unregister();
287 }