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 <crypto/hash.h>
31 #include <linux/scatterlist.h>
32 #include <linux/pagemap.h>
33 #include <libcfs/libcfs.h>
34 #include <libcfs/libcfs_crypto.h>
35 #include <libcfs/linux/linux-crypto.h>
37 #ifndef HAVE_CRYPTO_HASH_HELPERS
38 static inline const char *crypto_ahash_alg_name(struct crypto_ahash *tfm)
40 return crypto_tfm_alg_name(crypto_ahash_tfm(tfm));
43 static inline const char *crypto_ahash_driver_name(struct crypto_ahash *tfm)
45 return crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
50 * Array of hash algorithm speed in MByte per second
52 static int cfs_crypto_hash_speeds[CFS_HASH_ALG_MAX];
55 * Initialize the state descriptor for the specified hash algorithm.
57 * An internal routine to allocate the hash-specific state in \a hdesc for
58 * use with cfs_crypto_hash_digest() to compute the hash of a single message,
59 * though possibly in multiple chunks. The descriptor internal state should
60 * be freed with cfs_crypto_hash_final().
62 * \param[in] hash_alg hash algorithm id (CFS_HASH_ALG_*)
63 * \param[out] type pointer to the hash description in hash_types[] array
64 * \param[in,out] req ahash request to be initialized
65 * \param[in] key initial hash value/state, NULL to use default value
66 * \param[in] key_len length of \a key
68 * \retval 0 on success
69 * \retval negative errno on failure
71 static int cfs_crypto_hash_alloc(enum cfs_crypto_hash_alg hash_alg,
72 const struct cfs_crypto_hash_type **type,
73 struct ahash_request **req,
77 struct crypto_ahash *tfm;
80 *type = cfs_crypto_hash_type(hash_alg);
82 CWARN("Unsupported hash algorithm id = %d, max id is %d\n",
83 hash_alg, CFS_HASH_ALG_MAX);
87 /* Keys are only supported for the hmac version */
88 if (key && key_len > 0) {
91 algo_name = kasprintf(GFP_KERNEL, "hmac(%s)",
96 tfm = crypto_alloc_ahash(algo_name, 0, CRYPTO_ALG_ASYNC);
99 tfm = crypto_alloc_ahash((*type)->cht_name, 0,
103 CDEBUG(D_INFO, "Failed to alloc crypto hash %s\n",
108 *req = ahash_request_alloc(tfm, GFP_KERNEL);
110 CDEBUG(D_INFO, "Failed to alloc ahash_request for %s\n",
112 GOTO(out_free_tfm, err = -ENOMEM);
115 ahash_request_set_callback(*req, 0, NULL, NULL);
118 err = crypto_ahash_setkey(tfm, key, key_len);
119 else if ((*type)->cht_key != 0)
120 err = crypto_ahash_setkey(tfm,
121 (unsigned char *)&((*type)->cht_key),
124 GOTO(out_free_req, err);
126 CDEBUG(D_INFO, "Using crypto hash: %s (%s) speed %d MB/s\n",
127 crypto_ahash_alg_name(tfm), crypto_ahash_driver_name(tfm),
128 cfs_crypto_hash_speeds[hash_alg]);
130 err = crypto_ahash_init(*req);
133 ahash_request_free(*req);
135 crypto_free_ahash(tfm);
141 * Calculate hash digest for the passed buffer.
143 * This should be used when computing the hash on a single contiguous buffer.
144 * It combines the hash initialization, computation, and cleanup.
146 * \param[in] hash_alg id of hash algorithm (CFS_HASH_ALG_*)
147 * \param[in] buf data buffer on which to compute hash
148 * \param[in] buf_len length of \a buf in bytes
149 * \param[in] key initial value/state for algorithm, if \a key = NULL
150 * use default initial value
151 * \param[in] key_len length of \a key in bytes
152 * \param[out] hash pointer to computed hash value, if \a hash = NULL then
153 * \a hash_len is to digest size in bytes, retval -ENOSPC
154 * \param[in,out] hash_len size of \a hash buffer
156 * \retval -EINVAL \a buf, \a buf_len, \a hash_len, \a hash_alg invalid
157 * \retval -ENOENT \a hash_alg is unsupported
158 * \retval -ENOSPC \a hash is NULL, or \a hash_len less than digest size
159 * \retval 0 for success
160 * \retval negative errno for other errors from lower layers.
162 int cfs_crypto_hash_digest(enum cfs_crypto_hash_alg hash_alg,
163 const void *buf, unsigned int buf_len,
164 unsigned char *key, unsigned int key_len,
165 unsigned char *hash, unsigned int *hash_len)
167 struct scatterlist sl;
168 struct ahash_request *req;
170 const struct cfs_crypto_hash_type *type;
172 if (!buf || buf_len == 0 || !hash_len)
175 err = cfs_crypto_hash_alloc(hash_alg, &type, &req, key, key_len);
179 if (!hash || *hash_len < type->cht_size) {
180 *hash_len = type->cht_size;
181 crypto_free_ahash(crypto_ahash_reqtfm(req));
182 ahash_request_free(req);
185 sg_init_one(&sl, (void *)buf, buf_len);
187 ahash_request_set_crypt(req, &sl, hash, sl.length);
188 err = crypto_ahash_digest(req);
189 crypto_free_ahash(crypto_ahash_reqtfm(req));
190 ahash_request_free(req);
194 EXPORT_SYMBOL(cfs_crypto_hash_digest);
197 * Allocate and initialize desriptor for hash algorithm.
199 * This should be used to initialize a hash descriptor for multiple calls
200 * to a single hash function when computing the hash across multiple
201 * separate buffers or pages using cfs_crypto_hash_update{,_page}().
203 * The hash descriptor should be freed with cfs_crypto_hash_final().
205 * \param[in] hash_alg algorithm id (CFS_HASH_ALG_*)
206 * \param[in] key initial value/state for algorithm, if \a key = NULL
207 * use default initial value
208 * \param[in] key_len length of \a key in bytes
210 * \retval pointer to descriptor of hash instance
211 * \retval ERR_PTR(errno) in case of error
213 struct cfs_crypto_hash_desc *
214 cfs_crypto_hash_init(enum cfs_crypto_hash_alg hash_alg,
215 unsigned char *key, unsigned int key_len)
217 struct ahash_request *req;
219 const struct cfs_crypto_hash_type *type;
221 err = cfs_crypto_hash_alloc(hash_alg, &type, &req, key, key_len);
224 return (struct cfs_crypto_hash_desc *)req;
226 EXPORT_SYMBOL(cfs_crypto_hash_init);
229 * Update hash digest computed on data within the given \a page
231 * \param[in] hdesc hash state descriptor
232 * \param[in] page data page on which to compute the hash
233 * \param[in] offset offset within \a page at which to start hash
234 * \param[in] len length of data on which to compute hash
236 * \retval 0 for success
237 * \retval negative errno on failure
239 int cfs_crypto_hash_update_page(struct cfs_crypto_hash_desc *hdesc,
240 struct page *page, unsigned int offset,
243 struct ahash_request *req = (void *)hdesc;
244 struct scatterlist sl;
246 sg_init_table(&sl, 1);
247 sg_set_page(&sl, page, len, offset & ~PAGE_MASK);
249 ahash_request_set_crypt(req, &sl, NULL, sl.length);
250 return crypto_ahash_update(req);
252 EXPORT_SYMBOL(cfs_crypto_hash_update_page);
255 * Update hash digest computed on the specified data
257 * \param[in] hdesc hash state descriptor
258 * \param[in] buf data buffer on which to compute the hash
259 * \param[in] buf_len length of \buf on which to compute hash
261 * \retval 0 for success
262 * \retval negative errno on failure
264 int cfs_crypto_hash_update(struct cfs_crypto_hash_desc *hdesc,
265 const void *buf, unsigned int buf_len)
267 struct ahash_request *req = (void *)hdesc;
268 struct scatterlist sl;
270 sg_init_one(&sl, (void *)buf, buf_len);
272 ahash_request_set_crypt(req, &sl, NULL, sl.length);
273 return crypto_ahash_update(req);
275 EXPORT_SYMBOL(cfs_crypto_hash_update);
278 * Finish hash calculation, copy hash digest to buffer, clean up hash descriptor
280 * \param[in] hdesc hash descriptor
281 * \param[out] hash pointer to hash buffer to store hash digest
282 * \param[in,out] hash_len pointer to hash buffer size, if \a hash == NULL
283 * or hash_len == NULL only free \a hdesc instead
284 * of computing the hash
286 * \retval 0 for success
287 * \retval -EOVERFLOW if hash_len is too small for the hash digest
288 * \retval negative errno for other errors from lower layers
290 int cfs_crypto_hash_final(struct cfs_crypto_hash_desc *hdesc,
291 unsigned char *hash, unsigned int *hash_len)
293 struct ahash_request *req = (void *)hdesc;
294 int size = crypto_ahash_digestsize(crypto_ahash_reqtfm(req));
297 if (!hash || !hash_len) {
301 if (*hash_len < size) {
306 ahash_request_set_crypt(req, NULL, hash, 0);
307 err = crypto_ahash_final(req);
311 crypto_free_ahash(crypto_ahash_reqtfm(req));
312 ahash_request_free(req);
316 EXPORT_SYMBOL(cfs_crypto_hash_final);
319 * Compute the speed of specified hash function
321 * Run a speed test on the given hash algorithm on buffer using a 1MB buffer
322 * size. This is a reasonable buffer size for Lustre RPCs, even if the actual
323 * RPC size is larger or smaller.
325 * The speed is stored internally in the cfs_crypto_hash_speeds[] array, and
326 * is available through the cfs_crypto_hash_speed() function.
328 * This function needs to stay the same as obd_t10_performance_test() so that
329 * the speeds are comparable.
331 * \param[in] hash_alg hash algorithm id (CFS_HASH_ALG_*)
332 * \param[in] buf data buffer on which to compute the hash
333 * \param[in] buf_len length of \buf on which to compute hash
335 static void cfs_crypto_performance_test(enum cfs_crypto_hash_alg hash_alg)
337 int buf_len = max(PAGE_SIZE, 1048576UL);
339 unsigned long start, end;
341 unsigned long bcount;
343 unsigned char hash[CFS_CRYPTO_HASH_DIGESTSIZE_MAX];
344 unsigned int hash_len = sizeof(hash);
346 page = alloc_page(GFP_KERNEL);
353 memset(buf, 0xAD, PAGE_SIZE);
356 for (start = jiffies, end = start + msecs_to_jiffies(MSEC_PER_SEC / 4),
357 bcount = 0; time_before(jiffies, end) && err == 0; bcount++) {
358 struct cfs_crypto_hash_desc *hdesc;
361 hdesc = cfs_crypto_hash_init(hash_alg, NULL, 0);
363 err = PTR_ERR(hdesc);
367 for (i = 0; i < buf_len / PAGE_SIZE; i++) {
368 err = cfs_crypto_hash_update_page(hdesc, page, 0,
374 err = cfs_crypto_hash_final(hdesc, hash, &hash_len);
382 cfs_crypto_hash_speeds[hash_alg] = err;
383 CDEBUG(D_INFO, "Crypto hash algorithm %s test error: rc = %d\n",
384 cfs_crypto_hash_name(hash_alg), err);
388 tmp = ((bcount * buf_len / jiffies_to_msecs(end - start)) *
389 1000) / (1024 * 1024);
390 cfs_crypto_hash_speeds[hash_alg] = (int)tmp;
391 CDEBUG(D_CONFIG, "Crypto hash algorithm %s speed = %d MB/s\n",
392 cfs_crypto_hash_name(hash_alg),
393 cfs_crypto_hash_speeds[hash_alg]);
398 * hash speed in Mbytes per second for valid hash algorithm
400 * Return the performance of the specified \a hash_alg that was
401 * computed using cfs_crypto_performance_test(). If the performance
402 * has not yet been computed, do that when it is first requested.
403 * That avoids computing the speed when it is not actually needed.
404 * To avoid competing threads computing the checksum speed at the
405 * same time, only compute a single checksum speed at one time.
407 * \param[in] hash_alg hash algorithm id (CFS_HASH_ALG_*)
409 * \retval positive speed of the hash function in MB/s
410 * \retval -ENOENT if \a hash_alg is unsupported
411 * \retval negative errno if \a hash_alg speed is unavailable
413 int cfs_crypto_hash_speed(enum cfs_crypto_hash_alg hash_alg)
415 if (hash_alg < CFS_HASH_ALG_MAX) {
416 if (unlikely(cfs_crypto_hash_speeds[hash_alg] == 0)) {
417 static DEFINE_MUTEX(crypto_hash_speed_mutex);
419 mutex_lock(&crypto_hash_speed_mutex);
420 if (cfs_crypto_hash_speeds[hash_alg] == 0)
421 cfs_crypto_performance_test(hash_alg);
422 mutex_unlock(&crypto_hash_speed_mutex);
424 return cfs_crypto_hash_speeds[hash_alg];
429 EXPORT_SYMBOL(cfs_crypto_hash_speed);
432 * Run the performance test for all hash algorithms.
434 * Run the cfs_crypto_performance_test() benchmark for some of the available
435 * hash functions at module load time. This can't be reliably done at runtime
436 * since the CPUs may be under load from thousands of connecting clients when
437 * the first client connects and the checksum speeds are needed.
439 * Since the setup cost and computation speed of various hash algorithms is
440 * a function of the buffer size (and possibly internal contention of offload
441 * engines), this speed only represents an estimate of the actual speed under
442 * actual usage, but is reasonable for comparing available algorithms.
444 * The actual speeds are available via cfs_crypto_hash_speed() for later
447 * \retval 0 on success
448 * \retval -ENOMEM if no memory is available for test buffer
450 static int cfs_crypto_test_hashes(void)
452 enum cfs_crypto_hash_alg hash_alg;
454 for (hash_alg = 1; hash_alg < CFS_HASH_ALG_SPEED_MAX; hash_alg++)
455 cfs_crypto_performance_test(hash_alg);
465 #ifdef HAVE_PCLMULQDQ
466 #ifdef NEED_CRC32_ACCEL
467 static int crc32_pclmul;
469 #ifdef NEED_CRC32C_ACCEL
470 static int crc32c_pclmul;
472 #endif /* HAVE_PCLMULQDQ */
475 * Register available hash functions
479 int cfs_crypto_register(void)
481 request_module("crc32c");
483 adler32 = cfs_crypto_adler32_register();
486 crc32 = cfs_crypto_crc32_register();
488 #ifdef HAVE_PCLMULQDQ
489 #ifdef NEED_CRC32_ACCEL
490 crc32_pclmul = cfs_crypto_crc32_pclmul_register();
492 #ifdef NEED_CRC32C_ACCEL
493 crc32c_pclmul = cfs_crypto_crc32c_pclmul_register();
495 #endif /* HAVE_PCLMULQDQ */
497 /* check all algorithms and do performance test */
498 cfs_crypto_test_hashes();
504 * Unregister previously registered hash functions
506 void cfs_crypto_unregister(void)
509 cfs_crypto_adler32_unregister();
513 cfs_crypto_crc32_unregister();
515 #ifdef HAVE_PCLMULQDQ
516 #ifdef NEED_CRC32_ACCEL
517 if (crc32_pclmul == 0)
518 cfs_crypto_crc32_pclmul_unregister();
520 #ifdef NEED_CRC32C_ACCEL
521 if (crc32c_pclmul == 0)
522 cfs_crypto_crc32c_pclmul_unregister();
524 #endif /* HAVE_PCLMULQDQ */