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, 2014, Intel Corporation.
30 #include <linux/crypto.h>
31 #include <linux/scatterlist.h>
32 #include <libcfs/libcfs.h>
33 #include <libcfs/libcfs_crypto.h>
34 #include <libcfs/linux/linux-crypto.h>
36 * Array of hash algorithm speed in MByte per second
38 static int cfs_crypto_hash_speeds[CFS_HASH_ALG_MAX];
41 * Initialize the state descriptor for the specified hash algorithm.
43 * An internal routine to allocate the hash-specific state in \a hdesc for
44 * use with cfs_crypto_hash_digest() to compute the hash of a single message,
45 * though possibly in multiple chunks. The descriptor internal state should
46 * be freed with cfs_crypto_hash_final().
48 * \param[in] hash_alg hash algorithm id (CFS_HASH_ALG_*)
49 * \param[out] type pointer to the hash description in hash_types[] array
50 * \param[in,out] hdesc hash state descriptor to be initialized
51 * \param[in] key initial hash value/state, NULL to use default value
52 * \param[in] key_len length of \a key
54 * \retval 0 on success
55 * \retval negative errno on failure
57 static int cfs_crypto_hash_alloc(enum cfs_crypto_hash_alg hash_alg,
58 const struct cfs_crypto_hash_type **type,
59 struct hash_desc *hdesc, unsigned char *key,
64 *type = cfs_crypto_hash_type(hash_alg);
67 CWARN("Unsupported hash algorithm id = %d, max id is %d\n",
68 hash_alg, CFS_HASH_ALG_MAX);
71 hdesc->tfm = crypto_alloc_hash((*type)->cht_name, 0, 0);
73 if (hdesc->tfm == NULL)
76 if (IS_ERR(hdesc->tfm)) {
77 CDEBUG(D_INFO, "Failed to alloc crypto hash %s\n",
79 return PTR_ERR(hdesc->tfm);
85 err = crypto_hash_setkey(hdesc->tfm, key, key_len);
86 else if ((*type)->cht_key != 0)
87 err = crypto_hash_setkey(hdesc->tfm,
88 (unsigned char *)&((*type)->cht_key),
92 crypto_free_hash(hdesc->tfm);
96 CDEBUG(D_INFO, "Using crypto hash: %s (%s) speed %d MB/s\n",
97 (crypto_hash_tfm(hdesc->tfm))->__crt_alg->cra_name,
98 (crypto_hash_tfm(hdesc->tfm))->__crt_alg->cra_driver_name,
99 cfs_crypto_hash_speeds[hash_alg]);
101 return crypto_hash_init(hdesc);
105 * Calculate hash digest for the passed buffer.
107 * This should be used when computing the hash on a single contiguous buffer.
108 * It combines the hash initialization, computation, and cleanup.
110 * \param[in] hash_alg id of hash algorithm (CFS_HASH_ALG_*)
111 * \param[in] buf data buffer on which to compute hash
112 * \param[in] buf_len length of \a buf in bytes
113 * \param[in] key initial value/state for algorithm, if \a key = NULL
114 * use default initial value
115 * \param[in] key_len length of \a key in bytes
116 * \param[out] hash pointer to computed hash value, if \a hash = NULL then
117 * \a hash_len is to digest size in bytes, retval -ENOSPC
118 * \param[in,out] hash_len size of \a hash buffer
120 * \retval -EINVAL \a buf, \a buf_len, \a hash_len, \a alg_id invalid
121 * \retval -ENOENT \a hash_alg is unsupported
122 * \retval -ENOSPC \a hash is NULL, or \a hash_len less than digest size
123 * \retval 0 for success
124 * \retval negative errno for other errors from lower layers.
126 int cfs_crypto_hash_digest(enum cfs_crypto_hash_alg hash_alg,
127 const void *buf, unsigned int buf_len,
128 unsigned char *key, unsigned int key_len,
129 unsigned char *hash, unsigned int *hash_len)
131 struct scatterlist sl;
132 struct hash_desc hdesc;
134 const struct cfs_crypto_hash_type *type;
136 if (buf == NULL || buf_len == 0 || hash_len == NULL)
139 err = cfs_crypto_hash_alloc(hash_alg, &type, &hdesc, key, key_len);
143 if (hash == NULL || *hash_len < type->cht_size) {
144 *hash_len = type->cht_size;
145 crypto_free_hash(hdesc.tfm);
148 sg_init_one(&sl, (void *)buf, buf_len);
151 err = crypto_hash_digest(&hdesc, &sl, sl.length, hash);
152 crypto_free_hash(hdesc.tfm);
156 EXPORT_SYMBOL(cfs_crypto_hash_digest);
159 * Allocate and initialize desriptor for hash algorithm.
161 * This should be used to initialize a hash descriptor for multiple calls
162 * to a single hash function when computing the hash across multiple
163 * separate buffers or pages using cfs_crypto_hash_update{,_page}().
165 * The hash descriptor should be freed with cfs_crypto_hash_final().
167 * \param[in] hash_alg algorithm id (CFS_HASH_ALG_*)
168 * \param[in] key initial value/state for algorithm, if \a key = NULL
169 * use default initial value
170 * \param[in] key_len length of \a key in bytes
172 * \retval pointer to descriptor of hash instance
173 * \retval ERR_PTR(errno) in case of error
175 struct cfs_crypto_hash_desc *
176 cfs_crypto_hash_init(enum cfs_crypto_hash_alg hash_alg,
177 unsigned char *key, unsigned int key_len)
180 struct hash_desc *hdesc;
182 const struct cfs_crypto_hash_type *type;
184 hdesc = kmalloc(sizeof(*hdesc), 0);
186 return ERR_PTR(-ENOMEM);
188 err = cfs_crypto_hash_alloc(hash_alg, &type, hdesc, key, key_len);
192 hdesc = ERR_PTR(err);
194 return (struct cfs_crypto_hash_desc *)hdesc;
196 EXPORT_SYMBOL(cfs_crypto_hash_init);
199 * Update hash digest computed on data within the given \a page
201 * \param[in] hdesc hash state descriptor
202 * \param[in] page data page on which to compute the hash
203 * \param[in] offset offset within \a page at which to start hash
204 * \param[in] len length of data on which to compute hash
206 * \retval 0 for success
207 * \retval negative errno on failure
209 int cfs_crypto_hash_update_page(struct cfs_crypto_hash_desc *hdesc,
210 struct page *page, unsigned int offset,
213 struct scatterlist sl;
215 sg_init_table(&sl, 1);
216 sg_set_page(&sl, page, len, offset & ~PAGE_MASK);
218 return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
220 EXPORT_SYMBOL(cfs_crypto_hash_update_page);
223 * Update hash digest computed on the specified data
225 * \param[in] hdesc hash state descriptor
226 * \param[in] buf data buffer on which to compute the hash
227 * \param[in] buf_len length of \buf on which to compute hash
229 * \retval 0 for success
230 * \retval negative errno on failure
232 int cfs_crypto_hash_update(struct cfs_crypto_hash_desc *hdesc,
233 const void *buf, unsigned int buf_len)
235 struct scatterlist sl;
237 sg_init_one(&sl, (void *)buf, buf_len);
239 return crypto_hash_update((struct hash_desc *)hdesc, &sl, sl.length);
241 EXPORT_SYMBOL(cfs_crypto_hash_update);
244 * Finish hash calculation, copy hash digest to buffer, clean up hash descriptor
246 * \param[in] hdesc hash descriptor
247 * \param[out] hash pointer to hash buffer to store hash digest
248 * \param[in,out] hash_len pointer to hash buffer size, if \a hash == NULL
249 * or hash_len == NULL only free \a hdesc instead
250 * of computing the hash
252 * \retval 0 for success
253 * \retval -EOVERFLOW if hash_len is too small for the hash digest
254 * \retval negative errno for other errors from lower layers
256 int cfs_crypto_hash_final(struct cfs_crypto_hash_desc *hdesc,
257 unsigned char *hash, unsigned int *hash_len)
259 int size = crypto_hash_digestsize(((struct hash_desc *)hdesc)->tfm);
262 if (hash == NULL || hash_len == NULL) {
266 if (*hash_len < size) {
271 err = crypto_hash_final((struct hash_desc *)hdesc, hash);
275 crypto_free_hash(((struct hash_desc *)hdesc)->tfm);
280 EXPORT_SYMBOL(cfs_crypto_hash_final);
283 * Compute the speed of specified hash function
285 * Run a speed test on the given hash algorithm on buffer of the given size.
286 * The speed is stored internally in the cfs_crypto_hash_speeds[] array, and
287 * is available through the cfs_crypto_hash_speed() function.
289 * \param[in] hash_alg hash algorithm id (CFS_HASH_ALG_*)
290 * \param[in] buf data buffer on which to compute the hash
291 * \param[in] buf_len length of \buf on which to compute hash
293 static void cfs_crypto_performance_test(enum cfs_crypto_hash_alg hash_alg)
295 int buf_len = max(PAGE_SIZE, 1048576UL);
297 unsigned long start, end;
300 unsigned char hash[CFS_CRYPTO_HASH_DIGESTSIZE_MAX];
301 unsigned int hash_len = sizeof(hash);
303 page = alloc_page(GFP_KERNEL);
310 memset(buf, 0xAD, PAGE_SIZE);
313 for (start = jiffies, end = start + msecs_to_jiffies(MSEC_PER_SEC),
315 time_before(jiffies, end) && err == 0; bcount++) {
316 struct cfs_crypto_hash_desc *hdesc;
319 hdesc = cfs_crypto_hash_init(hash_alg, NULL, 0);
321 err = PTR_ERR(hdesc);
325 for (i = 0; i < buf_len / PAGE_SIZE; i++) {
326 err = cfs_crypto_hash_update_page(hdesc, page, 0,
332 err = cfs_crypto_hash_final(hdesc, hash, &hash_len);
340 cfs_crypto_hash_speeds[hash_alg] = err;
341 CDEBUG(D_INFO, "Crypto hash algorithm %s test error: rc = %d\n",
342 cfs_crypto_hash_name(hash_alg), err);
346 tmp = ((bcount * buf_len / jiffies_to_msecs(end - start)) *
347 1000) / (1024 * 1024);
348 cfs_crypto_hash_speeds[hash_alg] = (int)tmp;
349 CDEBUG(D_CONFIG, "Crypto hash algorithm %s speed = %d MB/s\n",
350 cfs_crypto_hash_name(hash_alg),
351 cfs_crypto_hash_speeds[hash_alg]);
356 * hash speed in Mbytes per second for valid hash algorithm
358 * Return the performance of the specified \a hash_alg that was previously
359 * computed using cfs_crypto_performance_test().
361 * \param[in] hash_alg hash algorithm id (CFS_HASH_ALG_*)
363 * \retval positive speed of the hash function in MB/s
364 * \retval -ENOENT if \a hash_alg is unsupported
365 * \retval negative errno if \a hash_alg speed is unavailable
367 int cfs_crypto_hash_speed(enum cfs_crypto_hash_alg hash_alg)
369 if (hash_alg < CFS_HASH_ALG_MAX)
370 return cfs_crypto_hash_speeds[hash_alg];
374 EXPORT_SYMBOL(cfs_crypto_hash_speed);
377 * Run the performance test for all hash algorithms.
379 * Run the cfs_crypto_performance_test() benchmark for all of the available
380 * hash functions using a 1MB buffer size. This is a reasonable buffer size
381 * for Lustre RPCs, even if the actual RPC size is larger or smaller.
383 * Since the setup cost and computation speed of various hash algorithms is
384 * a function of the buffer size (and possibly internal contention of offload
385 * engines), this speed only represents an estimate of the actual speed under
386 * actual usage, but is reasonable for comparing available algorithms.
388 * The actual speeds are available via cfs_crypto_hash_speed() for later
391 * \retval 0 on success
392 * \retval -ENOMEM if no memory is available for test buffer
394 static int cfs_crypto_test_hashes(void)
396 enum cfs_crypto_hash_alg hash_alg;
398 for (hash_alg = 0; hash_alg < CFS_HASH_ALG_MAX; hash_alg++)
399 cfs_crypto_performance_test(hash_alg);
409 #ifdef HAVE_PCLMULQDQ
410 #ifdef NEED_CRC32_ACCEL
411 static int crc32_pclmul;
413 #ifdef NEED_CRC32C_ACCEL
414 static int crc32c_pclmul;
416 #endif /* HAVE_PCLMULQDQ */
419 * Register available hash functions
423 int cfs_crypto_register(void)
425 request_module("crc32c");
427 adler32 = cfs_crypto_adler32_register();
430 crc32 = cfs_crypto_crc32_register();
432 #ifdef HAVE_PCLMULQDQ
433 #ifdef NEED_CRC32_ACCEL
434 crc32_pclmul = cfs_crypto_crc32_pclmul_register();
436 #ifdef NEED_CRC32C_ACCEL
437 crc32c_pclmul = cfs_crypto_crc32c_pclmul_register();
439 #endif /* HAVE_PCLMULQDQ */
441 /* check all algorithms and do performance test */
442 cfs_crypto_test_hashes();
448 * Unregister previously registered hash functions
450 void cfs_crypto_unregister(void)
453 cfs_crypto_adler32_unregister();
457 cfs_crypto_crc32_unregister();
459 #ifdef HAVE_PCLMULQDQ
460 #ifdef NEED_CRC32_ACCEL
461 if (crc32_pclmul == 0)
462 cfs_crypto_crc32_pclmul_unregister();
464 #ifdef NEED_CRC32C_ACCEL
465 if (crc32c_pclmul == 0)
466 cfs_crypto_crc32c_pclmul_unregister();
468 #endif /* HAVE_PCLMULQDQ */