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
28 #include <linux/crypto.h>
29 #include <linux/scatterlist.h>
30 #include <libcfs/libcfs.h>
31 #include <libcfs/linux/linux-crypto.h>
33 * Array of hash algorithm speed in MByte per second
35 static int cfs_crypto_hash_speeds[CFS_HASH_ALG_MAX];
38 #ifndef HAVE_STRUCT_HASH_DESC
39 /** 2.6.18 kernel have no crypto_hash function
40 * this part was copied from lustre_compat25.h */
41 #define crypto_hash crypto_tfm
43 struct crypto_hash *tfm;
48 struct crypto_hash *crypto_alloc_hash(const char *alg, unsigned int type,
51 return crypto_alloc_tfm(alg, 0);
54 static inline void crypto_free_hash(struct crypto_hash *tfm)
59 static inline int crypto_hash_init(struct hash_desc *desc)
61 crypto_digest_init(desc->tfm);
65 static inline int crypto_hash_update(struct hash_desc *desc,
66 struct scatterlist *sg,
69 if (desc->tfm->crt_digest.dit_update == NULL)
72 LASSERT(nbytes == sg->length);
73 crypto_digest_update(desc->tfm, sg, 1);
78 static inline int crypto_hash_digest(struct hash_desc *desc,
79 struct scatterlist *sg,
80 unsigned int nbytes, unsigned char *out)
82 crypto_hash_update(desc, sg, nbytes);
83 crypto_digest_final(desc->tfm, out);
87 static inline int crypto_hash_final(struct hash_desc *desc, unsigned char *out)
89 crypto_digest_final(desc->tfm, out);
93 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
98 #define crypto_hash_setkey(tfm, key, keylen) \
99 crypto_digest_setkey(tfm, key, keylen)
100 #define crypto_hash_digestsize(tfm) crypto_tfm_alg_digestsize(tfm)
101 #define crypto_hash_blocksize(tfm) crypto_tfm_alg_blocksize(tfm)
104 static int cfs_crypto_hash_alloc(unsigned char alg_id,
105 const struct cfs_crypto_hash_type **type,
106 struct hash_desc *desc, unsigned char *key,
107 unsigned int key_len)
111 *type = cfs_crypto_hash_type(alg_id);
114 CWARN("Unsupported hash algorithm id = %d, max id is %d\n",
115 alg_id, CFS_HASH_ALG_MAX);
118 desc->tfm = crypto_alloc_hash((*type)->cht_name, 0, 0);
120 if (desc->tfm == NULL)
123 if (IS_ERR(desc->tfm)) {
124 CDEBUG(D_INFO, "Failed to alloc crypto hash %s\n",
126 return PTR_ERR(desc->tfm);
131 /** Shash have different logic for initialization then digest
132 * shash: crypto_hash_setkey, crypto_hash_init
133 * digest: crypto_digest_init, crypto_digest_setkey
134 * Skip this function for digest, because we use shash logic at
135 * cfs_crypto_hash_alloc.
137 #ifndef HAVE_STRUCT_SHASH_ALG
138 crypto_hash_init(desc);
141 err = crypto_hash_setkey(desc->tfm, key, key_len);
142 } else if ((*type)->cht_key != 0) {
143 err = crypto_hash_setkey(desc->tfm,
144 (unsigned char *)&((*type)->cht_key),
149 crypto_free_hash(desc->tfm);
153 CDEBUG(D_INFO, "Using crypto hash: %s (%s) speed %d MB/s\n",
154 (crypto_hash_tfm(desc->tfm))->__crt_alg->cra_name,
155 (crypto_hash_tfm(desc->tfm))->__crt_alg->cra_driver_name,
156 cfs_crypto_hash_speeds[alg_id]);
158 #ifdef HAVE_STRUCT_SHASH_ALG
159 return crypto_hash_init(desc);
165 int cfs_crypto_hash_digest(unsigned char alg_id,
166 const void *buf, unsigned int buf_len,
167 unsigned char *key, unsigned int key_len,
168 unsigned char *hash, unsigned int *hash_len)
170 struct scatterlist sl = {0};
171 struct hash_desc hdesc;
173 const struct cfs_crypto_hash_type *type;
175 if (buf == NULL || buf_len == 0 || hash_len == NULL)
178 err = cfs_crypto_hash_alloc(alg_id, &type, &hdesc, key, key_len);
182 if (hash == NULL || *hash_len < type->cht_size) {
183 *hash_len = type->cht_size;
184 crypto_free_hash(hdesc.tfm);
187 sg_set_buf(&sl, (void *)buf, buf_len);
190 err = crypto_hash_digest(&hdesc, &sl, sl.length, hash);
191 crypto_free_hash(hdesc.tfm);
195 EXPORT_SYMBOL(cfs_crypto_hash_digest);
197 struct cfs_crypto_hash_desc *
198 cfs_crypto_hash_init(unsigned char alg_id,
199 unsigned char *key, unsigned int key_len)
202 struct hash_desc *hdesc;
204 const struct cfs_crypto_hash_type *type;
206 hdesc = cfs_alloc(sizeof(*hdesc), 0);
208 return ERR_PTR(-ENOMEM);
210 err = cfs_crypto_hash_alloc(alg_id, &type, hdesc, key, key_len);
216 return (struct cfs_crypto_hash_desc *)hdesc;
218 EXPORT_SYMBOL(cfs_crypto_hash_init);
220 int cfs_crypto_hash_update_page(struct cfs_crypto_hash_desc *hdesc,
221 cfs_page_t *page, unsigned int offset,
224 struct scatterlist sl = {0};
226 sg_set_page(&sl, page, len, offset & ~CFS_PAGE_MASK);
228 return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
230 EXPORT_SYMBOL(cfs_crypto_hash_update_page);
232 int cfs_crypto_hash_update(struct cfs_crypto_hash_desc *hdesc,
233 const void *buf, unsigned int buf_len)
235 struct scatterlist sl = {0};
237 sg_set_buf(&sl, (void *)buf, buf_len);
239 return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
241 EXPORT_SYMBOL(cfs_crypto_hash_update);
243 /* If hash_len pointer is NULL - destroy descriptor. */
244 int cfs_crypto_hash_final(struct cfs_crypto_hash_desc *hdesc,
245 unsigned char *hash, unsigned int *hash_len)
248 int size = crypto_hash_digestsize(((struct hash_desc *)hdesc)->tfm);
250 if (hash_len == NULL) {
251 crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
254 if (hash == NULL || *hash_len < size) {
258 err = crypto_hash_final((struct hash_desc *) hdesc, hash);
261 /* May be caller can fix error */
264 crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
267 EXPORT_SYMBOL(cfs_crypto_hash_final);
269 static void cfs_crypto_performance_test(unsigned char alg_id,
270 const unsigned char *buf,
271 unsigned int buf_len)
273 unsigned long start, end;
275 int sec = 1; /* do test only 1 sec */
276 unsigned char hash[64];
277 unsigned int hash_len = 64;
279 for (start = jiffies, end = start + sec * HZ, bcount = 0;
280 time_before(jiffies, end); bcount++) {
281 err = cfs_crypto_hash_digest(alg_id, buf, buf_len, NULL, 0,
290 cfs_crypto_hash_speeds[alg_id] = -1;
291 CDEBUG(D_INFO, "Crypto hash algorithm %s, err = %d\n",
292 cfs_crypto_hash_name(alg_id), err);
295 tmp = ((bcount * buf_len / jiffies_to_msecs(end - start)) *
296 1000) / (1024 * 1024);
297 cfs_crypto_hash_speeds[alg_id] = (int)tmp;
299 CDEBUG(D_INFO, "Crypto hash algorithm %s speed = %d MB/s\n",
300 cfs_crypto_hash_name(alg_id), cfs_crypto_hash_speeds[alg_id]);
303 int cfs_crypto_hash_speed(unsigned char hash_alg)
305 if (hash_alg < CFS_HASH_ALG_MAX)
306 return cfs_crypto_hash_speeds[hash_alg];
310 EXPORT_SYMBOL(cfs_crypto_hash_speed);
313 * Do performance test for all hash algorithms.
315 static int cfs_crypto_test_hashes(void)
320 /* Data block size for testing hash. Maximum
321 * kmalloc size for 2.6.18 kernel is 128K */
322 unsigned int data_len = 1 * 128 * 1024;
324 data = cfs_alloc(data_len, 0);
326 CERROR("Failed to allocate mem\n");
330 for (j = 0; j < data_len; j++)
333 for (i = 0; i < CFS_HASH_ALG_MAX; i++)
334 cfs_crypto_performance_test(i, data, data_len);
340 static int crc32, adler32;
343 static int crc32pclmul;
346 int cfs_crypto_register(void)
348 crc32 = cfs_crypto_crc32_register();
349 adler32 = cfs_crypto_adler32_register();
352 crc32pclmul = cfs_crypto_crc32_pclmul_register();
355 /* check all algorithms and do perfermance test */
356 cfs_crypto_test_hashes();
359 void cfs_crypto_unregister(void)
362 cfs_crypto_crc32_unregister();
364 cfs_crypto_adler32_unregister();
367 if (crc32pclmul == 0)
368 cfs_crypto_crc32_pclmul_unregister();