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);
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);
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 == NULL || buf_len == 0 || hash_len == NULL)
105 err = cfs_crypto_hash_alloc(alg_id, &type, &hdesc, key, key_len);
109 if (hash == NULL || *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)
129 struct hash_desc *hdesc;
131 const struct cfs_crypto_hash_type *type;
133 hdesc = kmalloc(sizeof(*hdesc), 0);
135 return ERR_PTR(-ENOMEM);
137 err = cfs_crypto_hash_alloc(alg_id, &type, hdesc, key, key_len);
143 return (struct cfs_crypto_hash_desc *)hdesc;
145 EXPORT_SYMBOL(cfs_crypto_hash_init);
147 int cfs_crypto_hash_update_page(struct cfs_crypto_hash_desc *hdesc,
148 struct page *page, unsigned int offset,
151 struct scatterlist sl;
153 sg_init_table(&sl, 1);
154 sg_set_page(&sl, page, len, offset & ~CFS_PAGE_MASK);
156 return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
158 EXPORT_SYMBOL(cfs_crypto_hash_update_page);
160 int cfs_crypto_hash_update(struct cfs_crypto_hash_desc *hdesc,
161 const void *buf, unsigned int buf_len)
163 struct scatterlist sl;
165 sg_init_one(&sl, buf, buf_len);
167 return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
169 EXPORT_SYMBOL(cfs_crypto_hash_update);
171 /* If hash_len pointer is NULL - destroy descriptor. */
172 int cfs_crypto_hash_final(struct cfs_crypto_hash_desc *hdesc,
173 unsigned char *hash, unsigned int *hash_len)
176 int size = crypto_hash_digestsize(((struct hash_desc *)hdesc)->tfm);
178 if (hash_len == NULL) {
179 crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
183 if (hash == NULL || *hash_len < size) {
187 err = crypto_hash_final((struct hash_desc *) hdesc, hash);
190 /* May be caller can fix error */
193 crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
197 EXPORT_SYMBOL(cfs_crypto_hash_final);
199 static void cfs_crypto_performance_test(unsigned char alg_id,
200 const unsigned char *buf,
201 unsigned int buf_len)
203 unsigned long start, end;
205 int sec = 1; /* do test only 1 sec */
206 unsigned char hash[64];
207 unsigned int hash_len = 64;
209 for (start = jiffies, end = start + sec * HZ, bcount = 0;
210 time_before(jiffies, end); bcount++) {
211 err = cfs_crypto_hash_digest(alg_id, buf, buf_len, NULL, 0,
220 cfs_crypto_hash_speeds[alg_id] = -1;
221 CDEBUG(D_INFO, "Crypto hash algorithm %s, err = %d\n",
222 cfs_crypto_hash_name(alg_id), err);
226 tmp = ((bcount * buf_len / jiffies_to_msecs(end - start)) *
227 1000) / (1024 * 1024);
228 cfs_crypto_hash_speeds[alg_id] = (int)tmp;
230 CDEBUG(D_INFO, "Crypto hash algorithm %s speed = %d MB/s\n",
231 cfs_crypto_hash_name(alg_id), cfs_crypto_hash_speeds[alg_id]);
234 int cfs_crypto_hash_speed(unsigned char hash_alg)
236 if (hash_alg < CFS_HASH_ALG_MAX)
237 return cfs_crypto_hash_speeds[hash_alg];
241 EXPORT_SYMBOL(cfs_crypto_hash_speed);
244 * Do performance test for all hash algorithms.
246 static int cfs_crypto_test_hashes(void)
251 /* Data block size for testing hash. Maximum
252 * kmalloc size for 2.6.18 kernel is 128K */
253 unsigned int data_len = 1 * 128 * 1024;
255 data = kmalloc(data_len, 0);
257 CERROR("Failed to allocate mem\n");
261 for (j = 0; j < data_len; j++)
264 for (i = 0; i < CFS_HASH_ALG_MAX; i++)
265 cfs_crypto_performance_test(i, data, data_len);
273 int cfs_crypto_register(void)
275 request_module("crc32c");
277 adler32 = cfs_crypto_adler32_register();
279 /* check all algorithms and do performance test */
280 cfs_crypto_test_hashes();
284 void cfs_crypto_unregister(void)
287 cfs_crypto_adler32_unregister();