3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
18 * Please visit http://www.xyratex.com/contact if you need additional
19 * information or have any questions.
25 * Copyright 2012 Xyratex Technology Limited
27 * Copyright (c) 2012, Intel Corporation.
30 #include <linux/crypto.h>
31 #include <linux/scatterlist.h>
32 #include <libcfs/libcfs.h>
33 #include <libcfs/linux/linux-crypto.h>
35 * Array of hash algorithm speed in MByte per second
37 static int cfs_crypto_hash_speeds[CFS_HASH_ALG_MAX];
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,
46 *type = cfs_crypto_hash_type(alg_id);
49 CWARN("Unsupported hash algorithm id = %d, max id is %d\n",
50 alg_id, CFS_HASH_ALG_MAX);
53 desc->tfm = crypto_alloc_hash((*type)->cht_name, 0, 0);
55 if (desc->tfm == NULL)
58 if (IS_ERR(desc->tfm)) {
59 CDEBUG(D_INFO, "Failed to alloc crypto hash %s\n",
61 return PTR_ERR(desc->tfm);
67 err = crypto_hash_setkey(desc->tfm, key, key_len);
68 } else if ((*type)->cht_key != 0) {
69 err = crypto_hash_setkey(desc->tfm,
70 (unsigned char *)&((*type)->cht_key),
75 crypto_free_hash(desc->tfm);
79 CDEBUG(D_INFO, "Using crypto hash: %s (%s) speed %d MB/s\n",
80 (crypto_hash_tfm(desc->tfm))->__crt_alg->cra_name,
81 (crypto_hash_tfm(desc->tfm))->__crt_alg->cra_driver_name,
82 cfs_crypto_hash_speeds[alg_id]);
84 return crypto_hash_init(desc);
87 int cfs_crypto_hash_digest(unsigned char alg_id,
88 const void *buf, unsigned int buf_len,
89 unsigned char *key, unsigned int key_len,
90 unsigned char *hash, unsigned int *hash_len)
92 struct scatterlist sl;
93 struct hash_desc hdesc;
95 const struct cfs_crypto_hash_type *type;
97 if (buf == NULL || buf_len == 0 || hash_len == NULL)
100 err = cfs_crypto_hash_alloc(alg_id, &type, &hdesc, key, key_len);
104 if (hash == NULL || *hash_len < type->cht_size) {
105 *hash_len = type->cht_size;
106 crypto_free_hash(hdesc.tfm);
109 sg_init_one(&sl, (void *)buf, buf_len);
112 err = crypto_hash_digest(&hdesc, &sl, sl.length, hash);
113 crypto_free_hash(hdesc.tfm);
117 EXPORT_SYMBOL(cfs_crypto_hash_digest);
119 struct cfs_crypto_hash_desc *
120 cfs_crypto_hash_init(unsigned char alg_id,
121 unsigned char *key, unsigned int key_len)
124 struct hash_desc *hdesc;
126 const struct cfs_crypto_hash_type *type;
128 hdesc = kmalloc(sizeof(*hdesc), 0);
130 return ERR_PTR(-ENOMEM);
132 err = cfs_crypto_hash_alloc(alg_id, &type, hdesc, key, key_len);
138 return (struct cfs_crypto_hash_desc *)hdesc;
140 EXPORT_SYMBOL(cfs_crypto_hash_init);
142 int cfs_crypto_hash_update_page(struct cfs_crypto_hash_desc *hdesc,
143 struct page *page, unsigned int offset,
146 struct scatterlist sl;
148 sg_init_table(&sl, 1);
149 sg_set_page(&sl, page, len, offset & ~CFS_PAGE_MASK);
151 return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
153 EXPORT_SYMBOL(cfs_crypto_hash_update_page);
155 int cfs_crypto_hash_update(struct cfs_crypto_hash_desc *hdesc,
156 const void *buf, unsigned int buf_len)
158 struct scatterlist sl;
160 sg_init_one(&sl, (void *)buf, buf_len);
162 return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
164 EXPORT_SYMBOL(cfs_crypto_hash_update);
166 /* If hash_len pointer is NULL - destroy descriptor. */
167 int cfs_crypto_hash_final(struct cfs_crypto_hash_desc *hdesc,
168 unsigned char *hash, unsigned int *hash_len)
171 int size = crypto_hash_digestsize(((struct hash_desc *)hdesc)->tfm);
173 if (hash_len == NULL) {
174 crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
178 if (hash == NULL || *hash_len < size) {
182 err = crypto_hash_final((struct hash_desc *) hdesc, hash);
185 /* May be caller can fix error */
188 crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
192 EXPORT_SYMBOL(cfs_crypto_hash_final);
194 static void cfs_crypto_performance_test(unsigned char alg_id,
195 const unsigned char *buf,
196 unsigned int buf_len)
198 unsigned long start, end;
200 int sec = 1; /* do test only 1 sec */
201 unsigned char hash[64];
202 unsigned int hash_len = 64;
204 for (start = jiffies, end = start + sec * HZ, bcount = 0;
205 time_before(jiffies, end); bcount++) {
206 err = cfs_crypto_hash_digest(alg_id, buf, buf_len, NULL, 0,
215 cfs_crypto_hash_speeds[alg_id] = -1;
216 CDEBUG(D_INFO, "Crypto hash algorithm %s, err = %d\n",
217 cfs_crypto_hash_name(alg_id), err);
220 tmp = ((bcount * buf_len / jiffies_to_msecs(end - start)) *
221 1000) / (1024 * 1024);
222 cfs_crypto_hash_speeds[alg_id] = (int)tmp;
224 CDEBUG(D_CONFIG, "Crypto hash algorithm %s speed = %d MB/s\n",
225 cfs_crypto_hash_name(alg_id), cfs_crypto_hash_speeds[alg_id]);
228 int cfs_crypto_hash_speed(unsigned char hash_alg)
230 if (hash_alg < CFS_HASH_ALG_MAX)
231 return cfs_crypto_hash_speeds[hash_alg];
235 EXPORT_SYMBOL(cfs_crypto_hash_speed);
238 * Do performance test for all hash algorithms.
240 static int cfs_crypto_test_hashes(void)
245 /* Data block size for testing hash. Maximum
246 * kmalloc size for 2.6.18 kernel is 128K */
247 unsigned int data_len = 1 * 128 * 1024;
249 data = kmalloc(data_len, 0);
251 CERROR("Failed to allocate mem\n");
255 for (j = 0; j < data_len; j++)
258 for (i = 0; i < CFS_HASH_ALG_MAX; i++)
259 cfs_crypto_performance_test(i, data, data_len);
270 #ifdef HAVE_PCLMULQDQ
271 #ifdef NEED_CRC32_ACCEL
272 static int crc32pclmul;
274 #ifdef NEED_CRC32C_ACCEL
275 static int crc32c_pclmul;
279 int cfs_crypto_register(void)
281 request_module("crc32c");
283 adler32 = cfs_crypto_adler32_register();
286 crc32 = cfs_crypto_crc32_register();
288 #ifdef HAVE_PCLMULQDQ
289 #ifdef NEED_CRC32_ACCEL
290 crc32pclmul = cfs_crypto_crc32_pclmul_register();
292 #ifdef NEED_CRC32C_ACCEL
293 crc32c_pclmul = cfs_crypto_crc32c_pclmul_register();
296 /* check all algorithms and do performance test */
297 cfs_crypto_test_hashes();
300 void cfs_crypto_unregister(void)
303 cfs_crypto_adler32_unregister();
307 cfs_crypto_crc32_unregister();
309 #ifdef HAVE_PCLMULQDQ
310 #ifdef NEED_CRC32_ACCEL
311 if (crc32pclmul == 0)
312 cfs_crypto_crc32_pclmul_unregister();
314 #ifdef NEED_CRC32C_ACCEL
315 if (crc32c_pclmul == 0)
316 cfs_crypto_crc32c_pclmul_unregister();