sha512_ssse3_glue.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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_flags = CRYPTO_ALG_TYPE_SHASH,
  96. .cra_blocksize = SHA512_BLOCK_SIZE,
  97. .cra_module = THIS_MODULE,
  98. }
  99. }, {
  100. .digestsize = SHA384_DIGEST_SIZE,
  101. .init = sha384_base_init,
  102. .update = sha512_ssse3_update,
  103. .final = sha512_ssse3_final,
  104. .finup = sha512_ssse3_finup,
  105. .descsize = sizeof(struct sha512_state),
  106. .base = {
  107. .cra_name = "sha384",
  108. .cra_driver_name = "sha384-ssse3",
  109. .cra_priority = 150,
  110. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  111. .cra_blocksize = SHA384_BLOCK_SIZE,
  112. .cra_module = THIS_MODULE,
  113. }
  114. } };
  115. static int register_sha512_ssse3(void)
  116. {
  117. if (boot_cpu_has(X86_FEATURE_SSSE3))
  118. return crypto_register_shashes(sha512_ssse3_algs,
  119. ARRAY_SIZE(sha512_ssse3_algs));
  120. return 0;
  121. }
  122. static void unregister_sha512_ssse3(void)
  123. {
  124. if (boot_cpu_has(X86_FEATURE_SSSE3))
  125. crypto_unregister_shashes(sha512_ssse3_algs,
  126. ARRAY_SIZE(sha512_ssse3_algs));
  127. }
  128. #ifdef CONFIG_AS_AVX
  129. asmlinkage void sha512_transform_avx(u64 *digest, const char *data,
  130. u64 rounds);
  131. static bool avx_usable(void)
  132. {
  133. if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL)) {
  134. if (cpu_has_avx)
  135. pr_info("AVX detected but unusable.\n");
  136. return false;
  137. }
  138. return true;
  139. }
  140. static int sha512_avx_update(struct shash_desc *desc, const u8 *data,
  141. unsigned int len)
  142. {
  143. return sha512_update(desc, data, len, sha512_transform_avx);
  144. }
  145. static int sha512_avx_finup(struct shash_desc *desc, const u8 *data,
  146. unsigned int len, u8 *out)
  147. {
  148. return sha512_finup(desc, data, len, out, sha512_transform_avx);
  149. }
  150. /* Add padding and return the message digest. */
  151. static int sha512_avx_final(struct shash_desc *desc, u8 *out)
  152. {
  153. return sha512_avx_finup(desc, NULL, 0, out);
  154. }
  155. static struct shash_alg sha512_avx_algs[] = { {
  156. .digestsize = SHA512_DIGEST_SIZE,
  157. .init = sha512_base_init,
  158. .update = sha512_avx_update,
  159. .final = sha512_avx_final,
  160. .finup = sha512_avx_finup,
  161. .descsize = sizeof(struct sha512_state),
  162. .base = {
  163. .cra_name = "sha512",
  164. .cra_driver_name = "sha512-avx",
  165. .cra_priority = 160,
  166. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  167. .cra_blocksize = SHA512_BLOCK_SIZE,
  168. .cra_module = THIS_MODULE,
  169. }
  170. }, {
  171. .digestsize = SHA384_DIGEST_SIZE,
  172. .init = sha384_base_init,
  173. .update = sha512_avx_update,
  174. .final = sha512_avx_final,
  175. .finup = sha512_avx_finup,
  176. .descsize = sizeof(struct sha512_state),
  177. .base = {
  178. .cra_name = "sha384",
  179. .cra_driver_name = "sha384-avx",
  180. .cra_priority = 160,
  181. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  182. .cra_blocksize = SHA384_BLOCK_SIZE,
  183. .cra_module = THIS_MODULE,
  184. }
  185. } };
  186. static int register_sha512_avx(void)
  187. {
  188. if (avx_usable())
  189. return crypto_register_shashes(sha512_avx_algs,
  190. ARRAY_SIZE(sha512_avx_algs));
  191. return 0;
  192. }
  193. static void unregister_sha512_avx(void)
  194. {
  195. if (avx_usable())
  196. crypto_unregister_shashes(sha512_avx_algs,
  197. ARRAY_SIZE(sha512_avx_algs));
  198. }
  199. #else
  200. static inline int register_sha512_avx(void) { return 0; }
  201. static inline void unregister_sha512_avx(void) { }
  202. #endif
  203. #if defined(CONFIG_AS_AVX2) && defined(CONFIG_AS_AVX)
  204. asmlinkage void sha512_transform_rorx(u64 *digest, const char *data,
  205. u64 rounds);
  206. static int sha512_avx2_update(struct shash_desc *desc, const u8 *data,
  207. unsigned int len)
  208. {
  209. return sha512_update(desc, data, len, sha512_transform_rorx);
  210. }
  211. static int sha512_avx2_finup(struct shash_desc *desc, const u8 *data,
  212. unsigned int len, u8 *out)
  213. {
  214. return sha512_finup(desc, data, len, out, sha512_transform_rorx);
  215. }
  216. /* Add padding and return the message digest. */
  217. static int sha512_avx2_final(struct shash_desc *desc, u8 *out)
  218. {
  219. return sha512_avx2_finup(desc, NULL, 0, out);
  220. }
  221. static struct shash_alg sha512_avx2_algs[] = { {
  222. .digestsize = SHA512_DIGEST_SIZE,
  223. .init = sha512_base_init,
  224. .update = sha512_avx2_update,
  225. .final = sha512_avx2_final,
  226. .finup = sha512_avx2_finup,
  227. .descsize = sizeof(struct sha512_state),
  228. .base = {
  229. .cra_name = "sha512",
  230. .cra_driver_name = "sha512-avx2",
  231. .cra_priority = 170,
  232. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  233. .cra_blocksize = SHA512_BLOCK_SIZE,
  234. .cra_module = THIS_MODULE,
  235. }
  236. }, {
  237. .digestsize = SHA384_DIGEST_SIZE,
  238. .init = sha384_base_init,
  239. .update = sha512_avx2_update,
  240. .final = sha512_avx2_final,
  241. .finup = sha512_avx2_finup,
  242. .descsize = sizeof(struct sha512_state),
  243. .base = {
  244. .cra_name = "sha384",
  245. .cra_driver_name = "sha384-avx2",
  246. .cra_priority = 170,
  247. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  248. .cra_blocksize = SHA384_BLOCK_SIZE,
  249. .cra_module = THIS_MODULE,
  250. }
  251. } };
  252. static bool avx2_usable(void)
  253. {
  254. if (avx_usable() && boot_cpu_has(X86_FEATURE_AVX2) &&
  255. boot_cpu_has(X86_FEATURE_BMI2))
  256. return true;
  257. return false;
  258. }
  259. static int register_sha512_avx2(void)
  260. {
  261. if (avx2_usable())
  262. return crypto_register_shashes(sha512_avx2_algs,
  263. ARRAY_SIZE(sha512_avx2_algs));
  264. return 0;
  265. }
  266. static void unregister_sha512_avx2(void)
  267. {
  268. if (avx2_usable())
  269. crypto_unregister_shashes(sha512_avx2_algs,
  270. ARRAY_SIZE(sha512_avx2_algs));
  271. }
  272. #else
  273. static inline int register_sha512_avx2(void) { return 0; }
  274. static inline void unregister_sha512_avx2(void) { }
  275. #endif
  276. static int __init sha512_ssse3_mod_init(void)
  277. {
  278. if (register_sha512_ssse3())
  279. goto fail;
  280. if (register_sha512_avx()) {
  281. unregister_sha512_ssse3();
  282. goto fail;
  283. }
  284. if (register_sha512_avx2()) {
  285. unregister_sha512_avx();
  286. unregister_sha512_ssse3();
  287. goto fail;
  288. }
  289. return 0;
  290. fail:
  291. return -ENODEV;
  292. }
  293. static void __exit sha512_ssse3_mod_fini(void)
  294. {
  295. unregister_sha512_avx2();
  296. unregister_sha512_avx();
  297. unregister_sha512_ssse3();
  298. }
  299. module_init(sha512_ssse3_mod_init);
  300. module_exit(sha512_ssse3_mod_fini);
  301. MODULE_LICENSE("GPL");
  302. MODULE_DESCRIPTION("SHA512 Secure Hash Algorithm, Supplemental SSE3 accelerated");
  303. MODULE_ALIAS_CRYPTO("sha512");
  304. MODULE_ALIAS_CRYPTO("sha384");