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 "../../../include/linux/libcfs/libcfs.h"
33 #include "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);
58 if (IS_ERR(desc->tfm)) {
59 CDEBUG(D_INFO, "Failed to alloc crypto hash %s\n",
61 return PTR_ERR(desc->tfm);
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.
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),
80 crypto_free_hash(desc->tfm);
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]);
89 return crypto_hash_init(desc);
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)
97 struct scatterlist sl;
98 struct hash_desc hdesc;
100 const struct cfs_crypto_hash_type *type;
102 if (!buf || buf_len == 0 || !hash_len)
105 err = cfs_crypto_hash_alloc(alg_id, &type, &hdesc, key, key_len);
109 if (!hash || *hash_len < type->cht_size) {
110 *hash_len = type->cht_size;
111 crypto_free_hash(hdesc.tfm);
114 sg_init_one(&sl, buf, buf_len);
117 err = crypto_hash_digest(&hdesc, &sl, sl.length, hash);
118 crypto_free_hash(hdesc.tfm);
122 EXPORT_SYMBOL(cfs_crypto_hash_digest);
124 struct cfs_crypto_hash_desc *
125 cfs_crypto_hash_init(unsigned char alg_id,
126 unsigned char *key, unsigned int key_len)
128 struct hash_desc *hdesc;
130 const struct cfs_crypto_hash_type *type;
132 hdesc = kmalloc(sizeof(*hdesc), 0);
134 return ERR_PTR(-ENOMEM);
136 err = cfs_crypto_hash_alloc(alg_id, &type, hdesc, key, key_len);
142 return (struct cfs_crypto_hash_desc *)hdesc;
144 EXPORT_SYMBOL(cfs_crypto_hash_init);
146 int cfs_crypto_hash_update_page(struct cfs_crypto_hash_desc *hdesc,
147 struct page *page, unsigned int offset,
150 struct scatterlist sl;
152 sg_init_table(&sl, 1);
153 sg_set_page(&sl, page, len, offset & ~CFS_PAGE_MASK);
155 return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
157 EXPORT_SYMBOL(cfs_crypto_hash_update_page);
159 int cfs_crypto_hash_update(struct cfs_crypto_hash_desc *hdesc,
160 const void *buf, unsigned int buf_len)
162 struct scatterlist sl;
164 sg_init_one(&sl, buf, buf_len);
166 return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
168 EXPORT_SYMBOL(cfs_crypto_hash_update);
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)
175 int size = crypto_hash_digestsize(((struct hash_desc *)hdesc)->tfm);
178 crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
182 if (!hash || *hash_len < size) {
186 err = crypto_hash_final((struct hash_desc *)hdesc, hash);
189 /* May be caller can fix error */
192 crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
196 EXPORT_SYMBOL(cfs_crypto_hash_final);
198 static void cfs_crypto_performance_test(unsigned char alg_id,
199 const unsigned char *buf,
200 unsigned int buf_len)
202 unsigned long start, end;
204 int sec = 1; /* do test only 1 sec */
205 unsigned char hash[64];
206 unsigned int hash_len = 64;
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,
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);
224 tmp = ((bcount * buf_len / jiffies_to_msecs(end - start)) *
225 1000) / (1024 * 1024);
226 cfs_crypto_hash_speeds[alg_id] = (int)tmp;
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]);
232 int cfs_crypto_hash_speed(unsigned char hash_alg)
234 if (hash_alg < CFS_HASH_ALG_MAX)
235 return cfs_crypto_hash_speeds[hash_alg];
239 EXPORT_SYMBOL(cfs_crypto_hash_speed);
242 * Do performance test for all hash algorithms.
244 static int cfs_crypto_test_hashes(void)
249 /* Data block size for testing hash. Maximum
250 * kmalloc size for 2.6.18 kernel is 128K
252 unsigned int data_len = 1 * 128 * 1024;
254 data = kmalloc(data_len, 0);
256 CERROR("Failed to allocate mem\n");
260 for (j = 0; j < data_len; j++)
263 for (i = 0; i < CFS_HASH_ALG_MAX; i++)
264 cfs_crypto_performance_test(i, data, data_len);
272 int cfs_crypto_register(void)
274 request_module("crc32c");
276 adler32 = cfs_crypto_adler32_register();
278 /* check all algorithms and do performance test */
279 cfs_crypto_test_hashes();
283 void cfs_crypto_unregister(void)
286 cfs_crypto_adler32_unregister();