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