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 <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"
36 * Array of hash algorithm speed in MByte per second
38 static int cfs_crypto_hash_speeds[CFS_HASH_ALG_MAX];
40 static int cfs_crypto_hash_alloc(unsigned char alg_id,
41 const struct cfs_crypto_hash_type **type,
42 struct ahash_request **req,
46 struct crypto_ahash *tfm;
49 *type = cfs_crypto_hash_type(alg_id);
52 CWARN("Unsupported hash algorithm id = %d, max id is %d\n",
53 alg_id, CFS_HASH_ALG_MAX);
56 tfm = crypto_alloc_ahash((*type)->cht_name, 0, CRYPTO_ALG_ASYNC);
59 CDEBUG(D_INFO, "Failed to alloc crypto hash %s\n",
64 *req = ahash_request_alloc(tfm, GFP_KERNEL);
66 CDEBUG(D_INFO, "Failed to alloc ahash_request for %s\n",
68 crypto_free_ahash(tfm);
72 ahash_request_set_callback(*req, 0, NULL, NULL);
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.
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),
88 crypto_free_ahash(tfm);
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]);
96 err = crypto_ahash_init(*req);
98 ahash_request_free(*req);
99 crypto_free_ahash(tfm);
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)
109 struct scatterlist sl;
110 struct ahash_request *req;
112 const struct cfs_crypto_hash_type *type;
114 if (!buf || buf_len == 0 || !hash_len)
117 err = cfs_crypto_hash_alloc(alg_id, &type, &req, key, key_len);
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);
127 sg_init_one(&sl, buf, buf_len);
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);
136 EXPORT_SYMBOL(cfs_crypto_hash_digest);
138 struct cfs_crypto_hash_desc *
139 cfs_crypto_hash_init(unsigned char alg_id,
140 unsigned char *key, unsigned int key_len)
142 struct ahash_request *req;
144 const struct cfs_crypto_hash_type *type;
146 err = cfs_crypto_hash_alloc(alg_id, &type, &req, key, key_len);
150 return (struct cfs_crypto_hash_desc *)req;
152 EXPORT_SYMBOL(cfs_crypto_hash_init);
154 int cfs_crypto_hash_update_page(struct cfs_crypto_hash_desc *hdesc,
155 struct page *page, unsigned int offset,
158 struct ahash_request *req = (void *)hdesc;
159 struct scatterlist sl;
161 sg_init_table(&sl, 1);
162 sg_set_page(&sl, page, len, offset & ~CFS_PAGE_MASK);
164 ahash_request_set_crypt(req, &sl, NULL, sl.length);
165 return crypto_ahash_update(req);
167 EXPORT_SYMBOL(cfs_crypto_hash_update_page);
169 int cfs_crypto_hash_update(struct cfs_crypto_hash_desc *hdesc,
170 const void *buf, unsigned int buf_len)
172 struct ahash_request *req = (void *)hdesc;
173 struct scatterlist sl;
175 sg_init_one(&sl, buf, buf_len);
177 ahash_request_set_crypt(req, &sl, NULL, sl.length);
178 return crypto_ahash_update(req);
180 EXPORT_SYMBOL(cfs_crypto_hash_update);
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)
187 struct ahash_request *req = (void *)hdesc;
188 int size = crypto_ahash_digestsize(crypto_ahash_reqtfm(req));
191 crypto_free_ahash(crypto_ahash_reqtfm(req));
192 ahash_request_free(req);
195 if (!hash || *hash_len < size) {
199 ahash_request_set_crypt(req, NULL, hash, 0);
200 err = crypto_ahash_final(req);
203 /* May be caller can fix error */
206 crypto_free_ahash(crypto_ahash_reqtfm(req));
207 ahash_request_free(req);
210 EXPORT_SYMBOL(cfs_crypto_hash_final);
212 static void cfs_crypto_performance_test(unsigned char alg_id,
213 const unsigned char *buf,
214 unsigned int buf_len)
216 unsigned long start, end;
218 int sec = 1; /* do test only 1 sec */
219 unsigned char hash[64];
220 unsigned int hash_len = 64;
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,
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);
238 tmp = ((bcount * buf_len / jiffies_to_msecs(end - start)) *
239 1000) / (1024 * 1024);
240 cfs_crypto_hash_speeds[alg_id] = (int)tmp;
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]);
246 int cfs_crypto_hash_speed(unsigned char hash_alg)
248 if (hash_alg < CFS_HASH_ALG_MAX)
249 return cfs_crypto_hash_speeds[hash_alg];
252 EXPORT_SYMBOL(cfs_crypto_hash_speed);
255 * Do performance test for all hash algorithms.
257 static int cfs_crypto_test_hashes(void)
262 /* Data block size for testing hash. Maximum
263 * kmalloc size for 2.6.18 kernel is 128K
265 unsigned int data_len = 1 * 128 * 1024;
267 data = kmalloc(data_len, 0);
271 for (j = 0; j < data_len; j++)
274 for (i = 0; i < CFS_HASH_ALG_MAX; i++)
275 cfs_crypto_performance_test(i, data, data_len);
283 int cfs_crypto_register(void)
285 request_module("crc32c");
287 adler32 = cfs_crypto_adler32_register();
289 /* check all algorithms and do performance test */
290 cfs_crypto_test_hashes();
294 void cfs_crypto_unregister(void)
297 cfs_crypto_adler32_unregister();