sha512_ssse3_glue.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Glue code for the SHA512 Secure Hash Algorithm assembler
  5. * implementation using supplemental SSE3 / AVX / AVX2 instructions.
  6. *
  7. * This file is based on sha512_generic.c
  8. *
  9. * Copyright (C) 2013 Intel Corporation
  10. * Author: Tim Chen <tim.c.chen@linux.intel.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the Free
  14. * Software Foundation; either version 2 of the License, or (at your option)
  15. * any later version.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  21. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  22. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. *
  26. */
  27. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  28. #include <crypto/internal/hash.h>
  29. #include <linux/init.h>
  30. #include <linux/module.h>
  31. #include <linux/mm.h>
  32. #include <linux/cryptohash.h>
  33. #include <linux/types.h>
  34. #include <crypto/sha.h>
  35. #include <crypto/sha512_base.h>
  36. #include <asm/fpu/api.h>
  37. #include <linux/string.h>
  38. asmlinkage void sha512_transform_ssse3(u64 *digest, const char *data,
  39. u64 rounds);
  40. typedef void (sha512_transform_fn)(u64 *digest, const char *data, u64 rounds);
  41. static int sha512_update(struct shash_desc *desc, const u8 *data,
  42. unsigned int len, sha512_transform_fn *sha512_xform)
  43. {
  44. struct sha512_state *sctx = shash_desc_ctx(desc);
  45. if (!irq_fpu_usable() ||
  46. (sctx->count[0] % SHA512_BLOCK_SIZE) + len < SHA512_BLOCK_SIZE)
  47. return crypto_sha512_update(desc, data, len);
  48. /* make sure casting to sha512_block_fn() is safe */
  49. BUILD_BUG_ON(offsetof(struct sha512_state, state) != 0);
  50. kernel_fpu_begin();
  51. sha512_base_do_update(desc, data, len,
  52. (sha512_block_fn *)sha512_xform);
  53. kernel_fpu_end();
  54. return 0;
  55. }
  56. static int sha512_finup(struct shash_desc *desc, const u8 *data,
  57. unsigned int len, u8 *out, sha512_transform_fn *sha512_xform)
  58. {
  59. if (!irq_fpu_usable())
  60. return crypto_sha512_finup(desc, data, len, out);
  61. kernel_fpu_begin();
  62. if (len)
  63. sha512_base_do_update(desc, data, len,
  64. (sha512_block_fn *)sha512_xform);
  65. sha512_base_do_finalize(desc, (sha512_block_fn *)sha512_xform);
  66. kernel_fpu_end();
  67. return sha512_base_finish(desc, out);
  68. }
  69. static int sha512_ssse3_update(struct shash_desc *desc, const u8 *data,
  70. unsigned int len)
  71. {
  72. return sha512_update(desc, data, len, sha512_transform_ssse3);
  73. }
  74. static int sha512_ssse3_finup(struct shash_desc *desc, const u8 *data,
  75. unsigned int len, u8 *out)
  76. {
  77. return sha512_finup(desc, data, len, out, sha512_transform_ssse3);
  78. }
  79. /* Add padding and return the message digest. */
  80. static int sha512_ssse3_final(struct shash_desc *desc, u8 *out)
  81. {
  82. return sha512_ssse3_finup(desc, NULL, 0, out);
  83. }
  84. static struct shash_alg sha512_ssse3_algs[] = { {
  85. .digestsize = SHA512_DIGEST_SIZE,
  86. .init = sha512_base_init,
  87. .update = sha512_ssse3_update,
  88. .final = sha512_ssse3_final,
  89. .finup = sha512_ssse3_finup,
  90. .descsize = sizeof(struct sha512_state),
  91. .base = {
  92. .cra_name = "sha512",
  93. .cra_driver_name = "sha512-ssse3",
  94. .cra_priority = 150,
  95. .cra_blocksize = SHA512_BLOCK_SIZE,
  96. .cra_module = THIS_MODULE,
  97. }
  98. }, {
  99. .digestsize = SHA384_DIGEST_SIZE,
  100. .init = sha384_base_init,
  101. .update = sha512_ssse3_update,
  102. .final = sha512_ssse3_final,
  103. .finup = sha512_ssse3_finup,
  104. .descsize = sizeof(struct sha512_state),
  105. .base = {
  106. .cra_name = "sha384",
  107. .cra_driver_name = "sha384-ssse3",
  108. .cra_priority = 150,
  109. .cra_blocksize = SHA384_BLOCK_SIZE,
  110. .cra_module = THIS_MODULE,
  111. }
  112. } };
  113. static int register_sha512_ssse3(void)
  114. {
  115. if (boot_cpu_has(X86_FEATURE_SSSE3))
  116. return crypto_register_shashes(sha512_ssse3_algs,
  117. ARRAY_SIZE(sha512_ssse3_algs));
  118. return 0;
  119. }
  120. static void unregister_sha512_ssse3(void)
  121. {
  122. if (boot_cpu_has(X86_FEATURE_SSSE3))
  123. crypto_unregister_shashes(sha512_ssse3_algs,
  124. ARRAY_SIZE(sha512_ssse3_algs));
  125. }
  126. #ifdef CONFIG_AS_AVX
  127. asmlinkage void sha512_transform_avx(u64 *digest, const char *data,
  128. u64 rounds);
  129. static bool avx_usable(void)
  130. {
  131. if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL)) {
  132. if (boot_cpu_has(X86_FEATURE_AVX))
  133. pr_info("AVX detected but unusable.\n");
  134. return false;
  135. }
  136. return true;
  137. }
  138. static int sha512_avx_update(struct shash_desc *desc, const u8 *data,
  139. unsigned int len)
  140. {
  141. return sha512_update(desc, data, len, sha512_transform_avx);
  142. }
  143. static int sha512_avx_finup(struct shash_desc *desc, const u8 *data,
  144. unsigned int len, u8 *out)
  145. {
  146. return sha512_finup(desc, data, len, out, sha512_transform_avx);
  147. }
  148. /* Add padding and return the message digest. */
  149. static int sha512_avx_final(struct shash_desc *desc, u8 *out)
  150. {
  151. return sha512_avx_finup(desc, NULL, 0, out);
  152. }
  153. static struct shash_alg sha512_avx_algs[] = { {
  154. .digestsize = SHA512_DIGEST_SIZE,
  155. .init = sha512_base_init,
  156. .update = sha512_avx_update,
  157. .final = sha512_avx_final,
  158. .finup = sha512_avx_finup,
  159. .descsize = sizeof(struct sha512_state),
  160. .base = {
  161. .cra_name = "sha512",
  162. .cra_driver_name = "sha512-avx",
  163. .cra_priority = 160,
  164. .cra_blocksize = SHA512_BLOCK_SIZE,
  165. .cra_module = THIS_MODULE,
  166. }
  167. }, {
  168. .digestsize = SHA384_DIGEST_SIZE,
  169. .init = sha384_base_init,
  170. .update = sha512_avx_update,
  171. .final = sha512_avx_final,
  172. .finup = sha512_avx_finup,
  173. .descsize = sizeof(struct sha512_state),
  174. .base = {
  175. .cra_name = "sha384",
  176. .cra_driver_name = "sha384-avx",
  177. .cra_priority = 160,
  178. .cra_blocksize = SHA384_BLOCK_SIZE,
  179. .cra_module = THIS_MODULE,
  180. }
  181. } };
  182. static int register_sha512_avx(void)
  183. {
  184. if (avx_usable())
  185. return crypto_register_shashes(sha512_avx_algs,
  186. ARRAY_SIZE(sha512_avx_algs));
  187. return 0;
  188. }
  189. static void unregister_sha512_avx(void)
  190. {
  191. if (avx_usable())
  192. crypto_unregister_shashes(sha512_avx_algs,
  193. ARRAY_SIZE(sha512_avx_algs));
  194. }
  195. #else
  196. static inline int register_sha512_avx(void) { return 0; }
  197. static inline void unregister_sha512_avx(void) { }
  198. #endif
  199. #if defined(CONFIG_AS_AVX2) && defined(CONFIG_AS_AVX)
  200. asmlinkage void sha512_transform_rorx(u64 *digest, const char *data,
  201. u64 rounds);
  202. static int sha512_avx2_update(struct shash_desc *desc, const u8 *data,
  203. unsigned int len)
  204. {
  205. return sha512_update(desc, data, len, sha512_transform_rorx);
  206. }
  207. static int sha512_avx2_finup(struct shash_desc *desc, const u8 *data,
  208. unsigned int len, u8 *out)
  209. {
  210. return sha512_finup(desc, data, len, out, sha512_transform_rorx);
  211. }
  212. /* Add padding and return the message digest. */
  213. static int sha512_avx2_final(struct shash_desc *desc, u8 *out)
  214. {
  215. return sha512_avx2_finup(desc, NULL, 0, out);
  216. }
  217. static struct shash_alg sha512_avx2_algs[] = { {
  218. .digestsize = SHA512_DIGEST_SIZE,
  219. .init = sha512_base_init,
  220. .update = sha512_avx2_update,
  221. .final = sha512_avx2_final,
  222. .finup = sha512_avx2_finup,
  223. .descsize = sizeof(struct sha512_state),
  224. .base = {
  225. .cra_name = "sha512",
  226. .cra_driver_name = "sha512-avx2",
  227. .cra_priority = 170,
  228. .cra_blocksize = SHA512_BLOCK_SIZE,
  229. .cra_module = THIS_MODULE,
  230. }
  231. }, {
  232. .digestsize = SHA384_DIGEST_SIZE,
  233. .init = sha384_base_init,
  234. .update = sha512_avx2_update,
  235. .final = sha512_avx2_final,
  236. .finup = sha512_avx2_finup,
  237. .descsize = sizeof(struct sha512_state),
  238. .base = {
  239. .cra_name = "sha384",
  240. .cra_driver_name = "sha384-avx2",
  241. .cra_priority = 170,
  242. .cra_blocksize = SHA384_BLOCK_SIZE,
  243. .cra_module = THIS_MODULE,
  244. }
  245. } };
  246. static bool avx2_usable(void)
  247. {
  248. if (avx_usable() && boot_cpu_has(X86_FEATURE_AVX2) &&
  249. boot_cpu_has(X86_FEATURE_BMI2))
  250. return true;
  251. return false;
  252. }
  253. static int register_sha512_avx2(void)
  254. {
  255. if (avx2_usable())
  256. return crypto_register_shashes(sha512_avx2_algs,
  257. ARRAY_SIZE(sha512_avx2_algs));
  258. return 0;
  259. }
  260. static void unregister_sha512_avx2(void)
  261. {
  262. if (avx2_usable())
  263. crypto_unregister_shashes(sha512_avx2_algs,
  264. ARRAY_SIZE(sha512_avx2_algs));
  265. }
  266. #else
  267. static inline int register_sha512_avx2(void) { return 0; }
  268. static inline void unregister_sha512_avx2(void) { }
  269. #endif
  270. static int __init sha512_ssse3_mod_init(void)
  271. {
  272. if (register_sha512_ssse3())
  273. goto fail;
  274. if (register_sha512_avx()) {
  275. unregister_sha512_ssse3();
  276. goto fail;
  277. }
  278. if (register_sha512_avx2()) {
  279. unregister_sha512_avx();
  280. unregister_sha512_ssse3();
  281. goto fail;
  282. }
  283. return 0;
  284. fail:
  285. return -ENODEV;
  286. }
  287. static void __exit sha512_ssse3_mod_fini(void)
  288. {
  289. unregister_sha512_avx2();
  290. unregister_sha512_avx();
  291. unregister_sha512_ssse3();
  292. }
  293. module_init(sha512_ssse3_mod_init);
  294. module_exit(sha512_ssse3_mod_fini);
  295. MODULE_LICENSE("GPL");
  296. MODULE_DESCRIPTION("SHA512 Secure Hash Algorithm, Supplemental SSE3 accelerated");
  297. MODULE_ALIAS_CRYPTO("sha512");
  298. MODULE_ALIAS_CRYPTO("sha512-ssse3");
  299. MODULE_ALIAS_CRYPTO("sha512-avx");
  300. MODULE_ALIAS_CRYPTO("sha512-avx2");
  301. MODULE_ALIAS_CRYPTO("sha384");
  302. MODULE_ALIAS_CRYPTO("sha384-ssse3");
  303. MODULE_ALIAS_CRYPTO("sha384-avx");
  304. MODULE_ALIAS_CRYPTO("sha384-avx2");