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 <linux/libcfs/libcfs.h>
33 #include <linux/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];
41 static int cfs_crypto_hash_alloc(unsigned char alg_id,
42 const struct cfs_crypto_hash_type **type,
43 struct hash_desc *desc, unsigned char *key,
48 *type = cfs_crypto_hash_type(alg_id);
51 CWARN("Unsupported hash algorithm id = %d, max id is %d\n",
52 alg_id, CFS_HASH_ALG_MAX);
55 desc->tfm = crypto_alloc_hash((*type)->cht_name, 0, 0);
57 if (desc->tfm == NULL)
60 if (IS_ERR(desc->tfm)) {
61 CDEBUG(D_INFO, "Failed to alloc crypto hash %s\n",
63 return PTR_ERR(desc->tfm);
68 /** Shash have different logic for initialization then digest
69 * shash: crypto_hash_setkey, crypto_hash_init
70 * digest: crypto_digest_init, crypto_digest_setkey
71 * Skip this function for digest, because we use shash logic at
72 * cfs_crypto_hash_alloc.
75 err = crypto_hash_setkey(desc->tfm, key, key_len);
76 } else if ((*type)->cht_key != 0) {
77 err = crypto_hash_setkey(desc->tfm,
78 (unsigned char *)&((*type)->cht_key),
83 crypto_free_hash(desc->tfm);
87 CDEBUG(D_INFO, "Using crypto hash: %s (%s) speed %d MB/s\n",
88 (crypto_hash_tfm(desc->tfm))->__crt_alg->cra_name,
89 (crypto_hash_tfm(desc->tfm))->__crt_alg->cra_driver_name,
90 cfs_crypto_hash_speeds[alg_id]);
92 return crypto_hash_init(desc);
95 int cfs_crypto_hash_digest(unsigned char alg_id,
96 const void *buf, unsigned int buf_len,
97 unsigned char *key, unsigned int key_len,
98 unsigned char *hash, unsigned int *hash_len)
100 struct scatterlist sl;
101 struct hash_desc hdesc;
103 const struct cfs_crypto_hash_type *type;
105 if (buf == NULL || buf_len == 0 || hash_len == NULL)
108 err = cfs_crypto_hash_alloc(alg_id, &type, &hdesc, key, key_len);
112 if (hash == NULL || *hash_len < type->cht_size) {
113 *hash_len = type->cht_size;
114 crypto_free_hash(hdesc.tfm);
117 sg_init_one(&sl, (void *)buf, buf_len);
120 err = crypto_hash_digest(&hdesc, &sl, sl.length, hash);
121 crypto_free_hash(hdesc.tfm);
125 EXPORT_SYMBOL(cfs_crypto_hash_digest);
127 struct cfs_crypto_hash_desc *
128 cfs_crypto_hash_init(unsigned char alg_id,
129 unsigned char *key, unsigned int key_len)
132 struct hash_desc *hdesc;
134 const struct cfs_crypto_hash_type *type;
136 hdesc = kmalloc(sizeof(*hdesc), 0);
138 return ERR_PTR(-ENOMEM);
140 err = cfs_crypto_hash_alloc(alg_id, &type, hdesc, key, key_len);
146 return (struct cfs_crypto_hash_desc *)hdesc;
148 EXPORT_SYMBOL(cfs_crypto_hash_init);
150 int cfs_crypto_hash_update_page(struct cfs_crypto_hash_desc *hdesc,
151 struct page *page, unsigned int offset,
154 struct scatterlist sl;
156 sg_init_table(&sl, 1);
157 sg_set_page(&sl, page, len, offset & ~CFS_PAGE_MASK);
159 return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
161 EXPORT_SYMBOL(cfs_crypto_hash_update_page);
163 int cfs_crypto_hash_update(struct cfs_crypto_hash_desc *hdesc,
164 const void *buf, unsigned int buf_len)
166 struct scatterlist sl;
168 sg_init_one(&sl, (void *)buf, buf_len);
170 return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
172 EXPORT_SYMBOL(cfs_crypto_hash_update);
174 /* If hash_len pointer is NULL - destroy descriptor. */
175 int cfs_crypto_hash_final(struct cfs_crypto_hash_desc *hdesc,
176 unsigned char *hash, unsigned int *hash_len)
179 int size = crypto_hash_digestsize(((struct hash_desc *)hdesc)->tfm);
181 if (hash_len == NULL) {
182 crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
186 if (hash == NULL || *hash_len < size) {
190 err = crypto_hash_final((struct hash_desc *) hdesc, hash);
193 /* May be caller can fix error */
196 crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
200 EXPORT_SYMBOL(cfs_crypto_hash_final);
202 static void cfs_crypto_performance_test(unsigned char alg_id,
203 const unsigned char *buf,
204 unsigned int buf_len)
206 unsigned long start, end;
208 int sec = 1; /* do test only 1 sec */
209 unsigned char hash[64];
210 unsigned int hash_len = 64;
212 for (start = jiffies, end = start + sec * HZ, bcount = 0;
213 time_before(jiffies, end); bcount++) {
214 err = cfs_crypto_hash_digest(alg_id, buf, buf_len, NULL, 0,
223 cfs_crypto_hash_speeds[alg_id] = -1;
224 CDEBUG(D_INFO, "Crypto hash algorithm %s, err = %d\n",
225 cfs_crypto_hash_name(alg_id), err);
228 tmp = ((bcount * buf_len / jiffies_to_msecs(end - start)) *
229 1000) / (1024 * 1024);
230 cfs_crypto_hash_speeds[alg_id] = (int)tmp;
232 CDEBUG(D_INFO, "Crypto hash algorithm %s speed = %d MB/s\n",
233 cfs_crypto_hash_name(alg_id), cfs_crypto_hash_speeds[alg_id]);
236 int cfs_crypto_hash_speed(unsigned char hash_alg)
238 if (hash_alg < CFS_HASH_ALG_MAX)
239 return cfs_crypto_hash_speeds[hash_alg];
243 EXPORT_SYMBOL(cfs_crypto_hash_speed);
246 * Do performance test for all hash algorithms.
248 static int cfs_crypto_test_hashes(void)
253 /* Data block size for testing hash. Maximum
254 * kmalloc size for 2.6.18 kernel is 128K */
255 unsigned int data_len = 1 * 128 * 1024;
257 data = kmalloc(data_len, 0);
259 CERROR("Failed to allocate mem\n");
263 for (j = 0; j < data_len; j++)
266 for (i = 0; i < CFS_HASH_ALG_MAX; i++)
267 cfs_crypto_performance_test(i, data, data_len);
275 int cfs_crypto_register(void)
277 adler32 = cfs_crypto_adler32_register();
279 /* check all algorithms and do performance test */
280 cfs_crypto_test_hashes();
283 void cfs_crypto_unregister(void)
286 cfs_crypto_adler32_unregister();