sha512_ssse3_glue.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/i387.h>
  37. #include <asm/xcr.h>
  38. #include <asm/xsave.h>
  39. #include <linux/string.h>
  40. asmlinkage void sha512_transform_ssse3(u64 *digest, const char *data,
  41. u64 rounds);
  42. #ifdef CONFIG_AS_AVX
  43. asmlinkage void sha512_transform_avx(u64 *digest, const char *data,
  44. u64 rounds);
  45. #endif
  46. #ifdef CONFIG_AS_AVX2
  47. asmlinkage void sha512_transform_rorx(u64 *digest, const char *data,
  48. u64 rounds);
  49. #endif
  50. static void (*sha512_transform_asm)(u64 *, const char *, u64);
  51. static int sha512_ssse3_update(struct shash_desc *desc, const u8 *data,
  52. unsigned int len)
  53. {
  54. struct sha512_state *sctx = shash_desc_ctx(desc);
  55. if (!irq_fpu_usable() ||
  56. (sctx->count[0] % SHA512_BLOCK_SIZE) + len < SHA512_BLOCK_SIZE)
  57. return crypto_sha512_update(desc, data, len);
  58. /* make sure casting to sha512_block_fn() is safe */
  59. BUILD_BUG_ON(offsetof(struct sha512_state, state) != 0);
  60. kernel_fpu_begin();
  61. sha512_base_do_update(desc, data, len,
  62. (sha512_block_fn *)sha512_transform_asm);
  63. kernel_fpu_end();
  64. return 0;
  65. }
  66. static int sha512_ssse3_finup(struct shash_desc *desc, const u8 *data,
  67. unsigned int len, u8 *out)
  68. {
  69. if (!irq_fpu_usable())
  70. return crypto_sha512_finup(desc, data, len, out);
  71. kernel_fpu_begin();
  72. if (len)
  73. sha512_base_do_update(desc, data, len,
  74. (sha512_block_fn *)sha512_transform_asm);
  75. sha512_base_do_finalize(desc, (sha512_block_fn *)sha512_transform_asm);
  76. kernel_fpu_end();
  77. return sha512_base_finish(desc, out);
  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 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. #ifdef CONFIG_AS_AVX
  116. static bool __init avx_usable(void)
  117. {
  118. u64 xcr0;
  119. if (!cpu_has_avx || !cpu_has_osxsave)
  120. return false;
  121. xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
  122. if ((xcr0 & (XSTATE_SSE | XSTATE_YMM)) != (XSTATE_SSE | XSTATE_YMM)) {
  123. pr_info("AVX detected but unusable.\n");
  124. return false;
  125. }
  126. return true;
  127. }
  128. #endif
  129. static int __init sha512_ssse3_mod_init(void)
  130. {
  131. /* test for SSSE3 first */
  132. if (cpu_has_ssse3)
  133. sha512_transform_asm = sha512_transform_ssse3;
  134. #ifdef CONFIG_AS_AVX
  135. /* allow AVX to override SSSE3, it's a little faster */
  136. if (avx_usable()) {
  137. #ifdef CONFIG_AS_AVX2
  138. if (boot_cpu_has(X86_FEATURE_AVX2))
  139. sha512_transform_asm = sha512_transform_rorx;
  140. else
  141. #endif
  142. sha512_transform_asm = sha512_transform_avx;
  143. }
  144. #endif
  145. if (sha512_transform_asm) {
  146. #ifdef CONFIG_AS_AVX
  147. if (sha512_transform_asm == sha512_transform_avx)
  148. pr_info("Using AVX optimized SHA-512 implementation\n");
  149. #ifdef CONFIG_AS_AVX2
  150. else if (sha512_transform_asm == sha512_transform_rorx)
  151. pr_info("Using AVX2 optimized SHA-512 implementation\n");
  152. #endif
  153. else
  154. #endif
  155. pr_info("Using SSSE3 optimized SHA-512 implementation\n");
  156. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  157. }
  158. pr_info("Neither AVX nor SSSE3 is available/usable.\n");
  159. return -ENODEV;
  160. }
  161. static void __exit sha512_ssse3_mod_fini(void)
  162. {
  163. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  164. }
  165. module_init(sha512_ssse3_mod_init);
  166. module_exit(sha512_ssse3_mod_fini);
  167. MODULE_LICENSE("GPL");
  168. MODULE_DESCRIPTION("SHA512 Secure Hash Algorithm, Supplemental SSE3 accelerated");
  169. MODULE_ALIAS_CRYPTO("sha512");
  170. MODULE_ALIAS_CRYPTO("sha384");