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 <libcfs/libcfs.h>
33 #include <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];
40 #ifndef HAVE_STRUCT_HASH_DESC
41 /** 2.6.18 kernel have no crypto_hash function
42 * this part was copied from lustre_compat25.h */
43 #define crypto_hash crypto_tfm
45 struct crypto_hash *tfm;
50 struct crypto_hash *crypto_alloc_hash(const char *alg, unsigned int type,
53 return crypto_alloc_tfm(alg, 0);
56 static inline void crypto_free_hash(struct crypto_hash *tfm)
61 static inline int crypto_hash_init(struct hash_desc *desc)
63 crypto_digest_init(desc->tfm);
67 static inline int crypto_hash_update(struct hash_desc *desc,
68 struct scatterlist *sg,
71 if (desc->tfm->crt_digest.dit_update == NULL)
74 LASSERT(nbytes == sg->length);
75 crypto_digest_update(desc->tfm, sg, 1);
80 static inline int crypto_hash_digest(struct hash_desc *desc,
81 struct scatterlist *sg,
82 unsigned int nbytes, unsigned char *out)
84 crypto_hash_update(desc, sg, nbytes);
85 crypto_digest_final(desc->tfm, out);
89 static inline int crypto_hash_final(struct hash_desc *desc, unsigned char *out)
91 crypto_digest_final(desc->tfm, out);
95 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
100 #define crypto_hash_setkey(tfm, key, keylen) \
101 crypto_digest_setkey(tfm, key, keylen)
102 #define crypto_hash_digestsize(tfm) crypto_tfm_alg_digestsize(tfm)
103 #define crypto_hash_blocksize(tfm) crypto_tfm_alg_blocksize(tfm)
106 static int cfs_crypto_hash_alloc(unsigned char alg_id,
107 const struct cfs_crypto_hash_type **type,
108 struct hash_desc *desc, unsigned char *key,
109 unsigned int key_len)
113 *type = cfs_crypto_hash_type(alg_id);
116 CWARN("Unsupported hash algorithm id = %d, max id is %d\n",
117 alg_id, CFS_HASH_ALG_MAX);
120 desc->tfm = crypto_alloc_hash((*type)->cht_name, 0, 0);
122 if (desc->tfm == NULL)
125 if (IS_ERR(desc->tfm)) {
126 CDEBUG(D_INFO, "Failed to alloc crypto hash %s\n",
128 return PTR_ERR(desc->tfm);
133 /** Shash have different logic for initialization then digest
134 * shash: crypto_hash_setkey, crypto_hash_init
135 * digest: crypto_digest_init, crypto_digest_setkey
136 * Skip this function for digest, because we use shash logic at
137 * cfs_crypto_hash_alloc.
139 #ifndef HAVE_STRUCT_SHASH_ALG
140 crypto_hash_init(desc);
143 err = crypto_hash_setkey(desc->tfm, key, key_len);
144 } else if ((*type)->cht_key != 0) {
145 err = crypto_hash_setkey(desc->tfm,
146 (unsigned char *)&((*type)->cht_key),
151 crypto_free_hash(desc->tfm);
155 CDEBUG(D_INFO, "Using crypto hash: %s (%s) speed %d MB/s\n",
156 (crypto_hash_tfm(desc->tfm))->__crt_alg->cra_name,
157 (crypto_hash_tfm(desc->tfm))->__crt_alg->cra_driver_name,
158 cfs_crypto_hash_speeds[alg_id]);
160 #ifdef HAVE_STRUCT_SHASH_ALG
161 return crypto_hash_init(desc);
167 int cfs_crypto_hash_digest(unsigned char alg_id,
168 const void *buf, unsigned int buf_len,
169 unsigned char *key, unsigned int key_len,
170 unsigned char *hash, unsigned int *hash_len)
172 struct scatterlist sl;
173 struct hash_desc hdesc;
175 const struct cfs_crypto_hash_type *type;
177 if (buf == NULL || buf_len == 0 || hash_len == NULL)
180 err = cfs_crypto_hash_alloc(alg_id, &type, &hdesc, key, key_len);
184 if (hash == NULL || *hash_len < type->cht_size) {
185 *hash_len = type->cht_size;
186 crypto_free_hash(hdesc.tfm);
189 sg_init_one(&sl, (void *)buf, buf_len);
192 err = crypto_hash_digest(&hdesc, &sl, sl.length, hash);
193 crypto_free_hash(hdesc.tfm);
197 EXPORT_SYMBOL(cfs_crypto_hash_digest);
199 struct cfs_crypto_hash_desc *
200 cfs_crypto_hash_init(unsigned char alg_id,
201 unsigned char *key, unsigned int key_len)
204 struct hash_desc *hdesc;
206 const struct cfs_crypto_hash_type *type;
208 hdesc = cfs_alloc(sizeof(*hdesc), 0);
210 return ERR_PTR(-ENOMEM);
212 err = cfs_crypto_hash_alloc(alg_id, &type, hdesc, key, key_len);
218 return (struct cfs_crypto_hash_desc *)hdesc;
220 EXPORT_SYMBOL(cfs_crypto_hash_init);
222 int cfs_crypto_hash_update_page(struct cfs_crypto_hash_desc *hdesc,
223 cfs_page_t *page, unsigned int offset,
226 struct scatterlist sl;
228 sg_init_table(&sl, 1);
229 sg_set_page(&sl, page, len, offset & ~CFS_PAGE_MASK);
231 return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
233 EXPORT_SYMBOL(cfs_crypto_hash_update_page);
235 int cfs_crypto_hash_update(struct cfs_crypto_hash_desc *hdesc,
236 const void *buf, unsigned int buf_len)
238 struct scatterlist sl;
240 sg_init_one(&sl, (void *)buf, buf_len);
242 return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
244 EXPORT_SYMBOL(cfs_crypto_hash_update);
246 /* If hash_len pointer is NULL - destroy descriptor. */
247 int cfs_crypto_hash_final(struct cfs_crypto_hash_desc *hdesc,
248 unsigned char *hash, unsigned int *hash_len)
251 int size = crypto_hash_digestsize(((struct hash_desc *)hdesc)->tfm);
253 if (hash_len == NULL) {
254 crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
258 if (hash == NULL || *hash_len < size) {
262 err = crypto_hash_final((struct hash_desc *) hdesc, hash);
265 /* May be caller can fix error */
268 crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
272 EXPORT_SYMBOL(cfs_crypto_hash_final);
274 static void cfs_crypto_performance_test(unsigned char alg_id,
275 const unsigned char *buf,
276 unsigned int buf_len)
278 unsigned long start, end;
280 int sec = 1; /* do test only 1 sec */
281 unsigned char hash[64];
282 unsigned int hash_len = 64;
284 for (start = jiffies, end = start + sec * HZ, bcount = 0;
285 time_before(jiffies, end); bcount++) {
286 err = cfs_crypto_hash_digest(alg_id, buf, buf_len, NULL, 0,
295 cfs_crypto_hash_speeds[alg_id] = -1;
296 CDEBUG(D_INFO, "Crypto hash algorithm %s, err = %d\n",
297 cfs_crypto_hash_name(alg_id), err);
300 tmp = ((bcount * buf_len / jiffies_to_msecs(end - start)) *
301 1000) / (1024 * 1024);
302 cfs_crypto_hash_speeds[alg_id] = (int)tmp;
304 CDEBUG(D_INFO, "Crypto hash algorithm %s speed = %d MB/s\n",
305 cfs_crypto_hash_name(alg_id), cfs_crypto_hash_speeds[alg_id]);
308 int cfs_crypto_hash_speed(unsigned char hash_alg)
310 if (hash_alg < CFS_HASH_ALG_MAX)
311 return cfs_crypto_hash_speeds[hash_alg];
315 EXPORT_SYMBOL(cfs_crypto_hash_speed);
318 * Do performance test for all hash algorithms.
320 static int cfs_crypto_test_hashes(void)
325 /* Data block size for testing hash. Maximum
326 * kmalloc size for 2.6.18 kernel is 128K */
327 unsigned int data_len = 1 * 128 * 1024;
329 data = cfs_alloc(data_len, 0);
331 CERROR("Failed to allocate mem\n");
335 for (j = 0; j < data_len; j++)
338 for (i = 0; i < CFS_HASH_ALG_MAX; i++)
339 cfs_crypto_performance_test(i, data, data_len);
345 static int crc32, adler32;
348 static int crc32pclmul;
351 int cfs_crypto_register(void)
353 crc32 = cfs_crypto_crc32_register();
354 adler32 = cfs_crypto_adler32_register();
357 crc32pclmul = cfs_crypto_crc32_pclmul_register();
360 /* check all algorithms and do performance test */
361 cfs_crypto_test_hashes();
364 void cfs_crypto_unregister(void)
367 cfs_crypto_crc32_unregister();
369 cfs_crypto_adler32_unregister();
372 if (crc32pclmul == 0)
373 cfs_crypto_crc32_pclmul_unregister();