PageRenderTime 23ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/staging/lustre/lnet/libcfs/linux/linux-crypto.c

https://gitlab.com/felipe_artur/linux-stable
C | 444 lines | 221 code | 48 blank | 175 comment | 35 complexity | 961219577aa1f9249c5f08cea68d8add MD5 | raw file
  1. /* GPL HEADER START
  2. *
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  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.
  8. *
  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).
  14. *
  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
  17. *
  18. * Please visit http://www.xyratex.com/contact if you need additional
  19. * information or have any questions.
  20. *
  21. * GPL HEADER END
  22. */
  23. /*
  24. * Copyright 2012 Xyratex Technology Limited
  25. *
  26. * Copyright (c) 2012, Intel Corporation.
  27. */
  28. #include <crypto/hash.h>
  29. #include <linux/scatterlist.h>
  30. #include "../../../include/linux/libcfs/libcfs.h"
  31. #include "../../../include/linux/libcfs/libcfs_crypto.h"
  32. #include "linux-crypto.h"
  33. /**
  34. * Array of hash algorithm speed in MByte per second
  35. */
  36. static int cfs_crypto_hash_speeds[CFS_HASH_ALG_MAX];
  37. /**
  38. * Initialize the state descriptor for the specified hash algorithm.
  39. *
  40. * An internal routine to allocate the hash-specific state in \a hdesc for
  41. * use with cfs_crypto_hash_digest() to compute the hash of a single message,
  42. * though possibly in multiple chunks. The descriptor internal state should
  43. * be freed with cfs_crypto_hash_final().
  44. *
  45. * \param[in] hash_alg hash algorithm id (CFS_HASH_ALG_*)
  46. * \param[out] type pointer to the hash description in hash_types[]
  47. * array
  48. * \param[in,out] hdesc hash state descriptor to be initialized
  49. * \param[in] key initial hash value/state, NULL to use default
  50. * value
  51. * \param[in] key_len length of \a key
  52. *
  53. * \retval 0 on success
  54. * \retval negative errno on failure
  55. */
  56. static int cfs_crypto_hash_alloc(enum cfs_crypto_hash_alg hash_alg,
  57. const struct cfs_crypto_hash_type **type,
  58. struct ahash_request **req,
  59. unsigned char *key,
  60. unsigned int key_len)
  61. {
  62. struct crypto_ahash *tfm;
  63. int err = 0;
  64. *type = cfs_crypto_hash_type(hash_alg);
  65. if (!*type) {
  66. CWARN("Unsupported hash algorithm id = %d, max id is %d\n",
  67. hash_alg, CFS_HASH_ALG_MAX);
  68. return -EINVAL;
  69. }
  70. tfm = crypto_alloc_ahash((*type)->cht_name, 0, CRYPTO_ALG_ASYNC);
  71. if (IS_ERR(tfm)) {
  72. CDEBUG(D_INFO, "Failed to alloc crypto hash %s\n",
  73. (*type)->cht_name);
  74. return PTR_ERR(tfm);
  75. }
  76. *req = ahash_request_alloc(tfm, GFP_KERNEL);
  77. if (!*req) {
  78. CDEBUG(D_INFO, "Failed to alloc ahash_request for %s\n",
  79. (*type)->cht_name);
  80. crypto_free_ahash(tfm);
  81. return -ENOMEM;
  82. }
  83. ahash_request_set_callback(*req, 0, NULL, NULL);
  84. if (key)
  85. err = crypto_ahash_setkey(tfm, key, key_len);
  86. else if ((*type)->cht_key != 0)
  87. err = crypto_ahash_setkey(tfm,
  88. (unsigned char *)&((*type)->cht_key),
  89. (*type)->cht_size);
  90. if (err != 0) {
  91. crypto_free_ahash(tfm);
  92. return err;
  93. }
  94. CDEBUG(D_INFO, "Using crypto hash: %s (%s) speed %d MB/s\n",
  95. crypto_ahash_alg_name(tfm), crypto_ahash_driver_name(tfm),
  96. cfs_crypto_hash_speeds[hash_alg]);
  97. err = crypto_ahash_init(*req);
  98. if (err) {
  99. ahash_request_free(*req);
  100. crypto_free_ahash(tfm);
  101. }
  102. return err;
  103. }
  104. /**
  105. * Calculate hash digest for the passed buffer.
  106. *
  107. * This should be used when computing the hash on a single contiguous buffer.
  108. * It combines the hash initialization, computation, and cleanup.
  109. *
  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,
  114. * if \a key = NULL use default initial value
  115. * \param[in] key_len length of \a key in bytes
  116. * \param[out] hash pointer to computed hash value,
  117. * if \a hash = NULL then \a hash_len is to digest
  118. * size in bytes, retval -ENOSPC
  119. * \param[in,out] hash_len size of \a hash buffer
  120. *
  121. * \retval -EINVAL \a buf, \a buf_len, \a hash_len,
  122. * \a hash_alg invalid
  123. * \retval -ENOENT \a hash_alg is unsupported
  124. * \retval -ENOSPC \a hash is NULL, or \a hash_len less than
  125. * digest size
  126. * \retval 0 for success
  127. * \retval negative errno for other errors from lower
  128. * layers.
  129. */
  130. int cfs_crypto_hash_digest(enum cfs_crypto_hash_alg hash_alg,
  131. const void *buf, unsigned int buf_len,
  132. unsigned char *key, unsigned int key_len,
  133. unsigned char *hash, unsigned int *hash_len)
  134. {
  135. struct scatterlist sl;
  136. struct ahash_request *req;
  137. int err;
  138. const struct cfs_crypto_hash_type *type;
  139. if (!buf || buf_len == 0 || !hash_len)
  140. return -EINVAL;
  141. err = cfs_crypto_hash_alloc(hash_alg, &type, &req, key, key_len);
  142. if (err != 0)
  143. return err;
  144. if (!hash || *hash_len < type->cht_size) {
  145. *hash_len = type->cht_size;
  146. crypto_free_ahash(crypto_ahash_reqtfm(req));
  147. ahash_request_free(req);
  148. return -ENOSPC;
  149. }
  150. sg_init_one(&sl, buf, buf_len);
  151. ahash_request_set_crypt(req, &sl, hash, sl.length);
  152. err = crypto_ahash_digest(req);
  153. crypto_free_ahash(crypto_ahash_reqtfm(req));
  154. ahash_request_free(req);
  155. return err;
  156. }
  157. EXPORT_SYMBOL(cfs_crypto_hash_digest);
  158. /**
  159. * Allocate and initialize desriptor for hash algorithm.
  160. *
  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}().
  164. *
  165. * The hash descriptor should be freed with cfs_crypto_hash_final().
  166. *
  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
  171. *
  172. * \retval pointer to descriptor of hash instance
  173. * \retval ERR_PTR(errno) in case of error
  174. */
  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)
  178. {
  179. struct ahash_request *req;
  180. int err;
  181. const struct cfs_crypto_hash_type *type;
  182. err = cfs_crypto_hash_alloc(hash_alg, &type, &req, key, key_len);
  183. if (err)
  184. return ERR_PTR(err);
  185. return (struct cfs_crypto_hash_desc *)req;
  186. }
  187. EXPORT_SYMBOL(cfs_crypto_hash_init);
  188. /**
  189. * Update hash digest computed on data within the given \a page
  190. *
  191. * \param[in] hdesc hash state descriptor
  192. * \param[in] page data page on which to compute the hash
  193. * \param[in] offset offset within \a page at which to start hash
  194. * \param[in] len length of data on which to compute hash
  195. *
  196. * \retval 0 for success
  197. * \retval negative errno on failure
  198. */
  199. int cfs_crypto_hash_update_page(struct cfs_crypto_hash_desc *hdesc,
  200. struct page *page, unsigned int offset,
  201. unsigned int len)
  202. {
  203. struct ahash_request *req = (void *)hdesc;
  204. struct scatterlist sl;
  205. sg_init_table(&sl, 1);
  206. sg_set_page(&sl, page, len, offset & ~PAGE_MASK);
  207. ahash_request_set_crypt(req, &sl, NULL, sl.length);
  208. return crypto_ahash_update(req);
  209. }
  210. EXPORT_SYMBOL(cfs_crypto_hash_update_page);
  211. /**
  212. * Update hash digest computed on the specified data
  213. *
  214. * \param[in] hdesc hash state descriptor
  215. * \param[in] buf data buffer on which to compute the hash
  216. * \param[in] buf_len length of \buf on which to compute hash
  217. *
  218. * \retval 0 for success
  219. * \retval negative errno on failure
  220. */
  221. int cfs_crypto_hash_update(struct cfs_crypto_hash_desc *hdesc,
  222. const void *buf, unsigned int buf_len)
  223. {
  224. struct ahash_request *req = (void *)hdesc;
  225. struct scatterlist sl;
  226. sg_init_one(&sl, buf, buf_len);
  227. ahash_request_set_crypt(req, &sl, NULL, sl.length);
  228. return crypto_ahash_update(req);
  229. }
  230. EXPORT_SYMBOL(cfs_crypto_hash_update);
  231. /**
  232. * Finish hash calculation, copy hash digest to buffer, clean up hash descriptor
  233. *
  234. * \param[in] hdesc hash descriptor
  235. * \param[out] hash pointer to hash buffer to store hash digest
  236. * \param[in,out] hash_len pointer to hash buffer size, if \a hdesc = NULL
  237. * only free \a hdesc instead of computing the hash
  238. *
  239. * \retval 0 for success
  240. * \retval -EOVERFLOW if hash_len is too small for the hash digest
  241. * \retval negative errno for other errors from lower layers
  242. */
  243. int cfs_crypto_hash_final(struct cfs_crypto_hash_desc *hdesc,
  244. unsigned char *hash, unsigned int *hash_len)
  245. {
  246. int err;
  247. struct ahash_request *req = (void *)hdesc;
  248. int size = crypto_ahash_digestsize(crypto_ahash_reqtfm(req));
  249. if (!hash || !hash_len) {
  250. err = 0;
  251. goto free_ahash;
  252. }
  253. if (*hash_len < size) {
  254. err = -EOVERFLOW;
  255. goto free_ahash;
  256. }
  257. ahash_request_set_crypt(req, NULL, hash, 0);
  258. err = crypto_ahash_final(req);
  259. if (!err)
  260. *hash_len = size;
  261. free_ahash:
  262. crypto_free_ahash(crypto_ahash_reqtfm(req));
  263. ahash_request_free(req);
  264. return err;
  265. }
  266. EXPORT_SYMBOL(cfs_crypto_hash_final);
  267. /**
  268. * Compute the speed of specified hash function
  269. *
  270. * Run a speed test on the given hash algorithm on buffer of the given size.
  271. * The speed is stored internally in the cfs_crypto_hash_speeds[] array, and
  272. * is available through the cfs_crypto_hash_speed() function.
  273. *
  274. * \param[in] hash_alg hash algorithm id (CFS_HASH_ALG_*)
  275. * \param[in] buf data buffer on which to compute the hash
  276. * \param[in] buf_len length of \buf on which to compute hash
  277. */
  278. static void cfs_crypto_performance_test(enum cfs_crypto_hash_alg hash_alg)
  279. {
  280. int buf_len = max(PAGE_SIZE, 1048576UL);
  281. void *buf;
  282. unsigned long start, end;
  283. int bcount, err = 0;
  284. struct page *page;
  285. unsigned char hash[CFS_CRYPTO_HASH_DIGESTSIZE_MAX];
  286. unsigned int hash_len = sizeof(hash);
  287. page = alloc_page(GFP_KERNEL);
  288. if (!page) {
  289. err = -ENOMEM;
  290. goto out_err;
  291. }
  292. buf = kmap(page);
  293. memset(buf, 0xAD, PAGE_SIZE);
  294. kunmap(page);
  295. for (start = jiffies, end = start + msecs_to_jiffies(MSEC_PER_SEC),
  296. bcount = 0; time_before(jiffies, end); bcount++) {
  297. struct cfs_crypto_hash_desc *hdesc;
  298. int i;
  299. hdesc = cfs_crypto_hash_init(hash_alg, NULL, 0);
  300. if (IS_ERR(hdesc)) {
  301. err = PTR_ERR(hdesc);
  302. break;
  303. }
  304. for (i = 0; i < buf_len / PAGE_SIZE; i++) {
  305. err = cfs_crypto_hash_update_page(hdesc, page, 0,
  306. PAGE_SIZE);
  307. if (err)
  308. break;
  309. }
  310. err = cfs_crypto_hash_final(hdesc, hash, &hash_len);
  311. if (err)
  312. break;
  313. }
  314. end = jiffies;
  315. __free_page(page);
  316. out_err:
  317. if (err) {
  318. cfs_crypto_hash_speeds[hash_alg] = err;
  319. CDEBUG(D_INFO, "Crypto hash algorithm %s test error: rc = %d\n",
  320. cfs_crypto_hash_name(hash_alg), err);
  321. } else {
  322. unsigned long tmp;
  323. tmp = ((bcount * buf_len / jiffies_to_msecs(end - start)) *
  324. 1000) / (1024 * 1024);
  325. cfs_crypto_hash_speeds[hash_alg] = (int)tmp;
  326. CDEBUG(D_CONFIG, "Crypto hash algorithm %s speed = %d MB/s\n",
  327. cfs_crypto_hash_name(hash_alg),
  328. cfs_crypto_hash_speeds[hash_alg]);
  329. }
  330. }
  331. /**
  332. * hash speed in Mbytes per second for valid hash algorithm
  333. *
  334. * Return the performance of the specified \a hash_alg that was previously
  335. * computed using cfs_crypto_performance_test().
  336. *
  337. * \param[in] hash_alg hash algorithm id (CFS_HASH_ALG_*)
  338. *
  339. * \retval positive speed of the hash function in MB/s
  340. * \retval -ENOENT if \a hash_alg is unsupported
  341. * \retval negative errno if \a hash_alg speed is unavailable
  342. */
  343. int cfs_crypto_hash_speed(enum cfs_crypto_hash_alg hash_alg)
  344. {
  345. if (hash_alg < CFS_HASH_ALG_MAX)
  346. return cfs_crypto_hash_speeds[hash_alg];
  347. return -ENOENT;
  348. }
  349. EXPORT_SYMBOL(cfs_crypto_hash_speed);
  350. /**
  351. * Run the performance test for all hash algorithms.
  352. *
  353. * Run the cfs_crypto_performance_test() benchmark for all of the available
  354. * hash functions using a 1MB buffer size. This is a reasonable buffer size
  355. * for Lustre RPCs, even if the actual RPC size is larger or smaller.
  356. *
  357. * Since the setup cost and computation speed of various hash algorithms is
  358. * a function of the buffer size (and possibly internal contention of offload
  359. * engines), this speed only represents an estimate of the actual speed under
  360. * actual usage, but is reasonable for comparing available algorithms.
  361. *
  362. * The actual speeds are available via cfs_crypto_hash_speed() for later
  363. * comparison.
  364. *
  365. * \retval 0 on success
  366. * \retval -ENOMEM if no memory is available for test buffer
  367. */
  368. static int cfs_crypto_test_hashes(void)
  369. {
  370. enum cfs_crypto_hash_alg hash_alg;
  371. for (hash_alg = 0; hash_alg < CFS_HASH_ALG_MAX; hash_alg++)
  372. cfs_crypto_performance_test(hash_alg);
  373. return 0;
  374. }
  375. static int adler32;
  376. /**
  377. * Register available hash functions
  378. *
  379. * \retval 0
  380. */
  381. int cfs_crypto_register(void)
  382. {
  383. request_module("crc32c");
  384. adler32 = cfs_crypto_adler32_register();
  385. /* check all algorithms and do performance test */
  386. cfs_crypto_test_hashes();
  387. return 0;
  388. }
  389. /**
  390. * Unregister previously registered hash functions
  391. */
  392. void cfs_crypto_unregister(void)
  393. {
  394. if (adler32 == 0)
  395. cfs_crypto_adler32_unregister();
  396. }