sha256_ssse3_glue.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Glue code for the SHA256 Secure Hash Algorithm assembler
  5. * implementation using supplemental SSE3 / AVX / AVX2 instructions.
  6. *
  7. * This file is based on sha256_generic.c
  8. *
  9. * Copyright (C) 2013 Intel Corporation.
  10. *
  11. * Author:
  12. * Tim Chen <tim.c.chen@linux.intel.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the Free
  16. * Software Foundation; either version 2 of the License, or (at your option)
  17. * any later version.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  23. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  24. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  25. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. * SOFTWARE.
  27. */
  28. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <crypto/internal/hash.h>
  30. #include <linux/init.h>
  31. #include <linux/module.h>
  32. #include <linux/mm.h>
  33. #include <linux/cryptohash.h>
  34. #include <linux/types.h>
  35. #include <crypto/sha.h>
  36. #include <crypto/sha256_base.h>
  37. #include <asm/i387.h>
  38. #include <asm/xcr.h>
  39. #include <asm/xsave.h>
  40. #include <linux/string.h>
  41. asmlinkage void sha256_transform_ssse3(u32 *digest, const char *data,
  42. u64 rounds);
  43. #ifdef CONFIG_AS_AVX
  44. asmlinkage void sha256_transform_avx(u32 *digest, const char *data,
  45. u64 rounds);
  46. #endif
  47. #ifdef CONFIG_AS_AVX2
  48. asmlinkage void sha256_transform_rorx(u32 *digest, const char *data,
  49. u64 rounds);
  50. #endif
  51. static void (*sha256_transform_asm)(u32 *, const char *, u64);
  52. static int sha256_ssse3_update(struct shash_desc *desc, const u8 *data,
  53. unsigned int len)
  54. {
  55. struct sha256_state *sctx = shash_desc_ctx(desc);
  56. if (!irq_fpu_usable() ||
  57. (sctx->count % SHA256_BLOCK_SIZE) + len < SHA256_BLOCK_SIZE)
  58. return crypto_sha256_update(desc, data, len);
  59. /* make sure casting to sha256_block_fn() is safe */
  60. BUILD_BUG_ON(offsetof(struct sha256_state, state) != 0);
  61. kernel_fpu_begin();
  62. sha256_base_do_update(desc, data, len,
  63. (sha256_block_fn *)sha256_transform_asm);
  64. kernel_fpu_end();
  65. return 0;
  66. }
  67. static int sha256_ssse3_finup(struct shash_desc *desc, const u8 *data,
  68. unsigned int len, u8 *out)
  69. {
  70. if (!irq_fpu_usable())
  71. return crypto_sha256_finup(desc, data, len, out);
  72. kernel_fpu_begin();
  73. if (len)
  74. sha256_base_do_update(desc, data, len,
  75. (sha256_block_fn *)sha256_transform_asm);
  76. sha256_base_do_finalize(desc, (sha256_block_fn *)sha256_transform_asm);
  77. kernel_fpu_end();
  78. return sha256_base_finish(desc, out);
  79. }
  80. /* Add padding and return the message digest. */
  81. static int sha256_ssse3_final(struct shash_desc *desc, u8 *out)
  82. {
  83. return sha256_ssse3_finup(desc, NULL, 0, out);
  84. }
  85. static struct shash_alg algs[] = { {
  86. .digestsize = SHA256_DIGEST_SIZE,
  87. .init = sha256_base_init,
  88. .update = sha256_ssse3_update,
  89. .final = sha256_ssse3_final,
  90. .finup = sha256_ssse3_finup,
  91. .descsize = sizeof(struct sha256_state),
  92. .base = {
  93. .cra_name = "sha256",
  94. .cra_driver_name = "sha256-ssse3",
  95. .cra_priority = 150,
  96. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  97. .cra_blocksize = SHA256_BLOCK_SIZE,
  98. .cra_module = THIS_MODULE,
  99. }
  100. }, {
  101. .digestsize = SHA224_DIGEST_SIZE,
  102. .init = sha224_base_init,
  103. .update = sha256_ssse3_update,
  104. .final = sha256_ssse3_final,
  105. .finup = sha256_ssse3_finup,
  106. .descsize = sizeof(struct sha256_state),
  107. .base = {
  108. .cra_name = "sha224",
  109. .cra_driver_name = "sha224-ssse3",
  110. .cra_priority = 150,
  111. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  112. .cra_blocksize = SHA224_BLOCK_SIZE,
  113. .cra_module = THIS_MODULE,
  114. }
  115. } };
  116. #ifdef CONFIG_AS_AVX
  117. static bool __init avx_usable(void)
  118. {
  119. u64 xcr0;
  120. if (!cpu_has_avx || !cpu_has_osxsave)
  121. return false;
  122. xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
  123. if ((xcr0 & (XSTATE_SSE | XSTATE_YMM)) != (XSTATE_SSE | XSTATE_YMM)) {
  124. pr_info("AVX detected but unusable.\n");
  125. return false;
  126. }
  127. return true;
  128. }
  129. #endif
  130. static int __init sha256_ssse3_mod_init(void)
  131. {
  132. /* test for SSSE3 first */
  133. if (cpu_has_ssse3)
  134. sha256_transform_asm = sha256_transform_ssse3;
  135. #ifdef CONFIG_AS_AVX
  136. /* allow AVX to override SSSE3, it's a little faster */
  137. if (avx_usable()) {
  138. #ifdef CONFIG_AS_AVX2
  139. if (boot_cpu_has(X86_FEATURE_AVX2) && boot_cpu_has(X86_FEATURE_BMI2))
  140. sha256_transform_asm = sha256_transform_rorx;
  141. else
  142. #endif
  143. sha256_transform_asm = sha256_transform_avx;
  144. }
  145. #endif
  146. if (sha256_transform_asm) {
  147. #ifdef CONFIG_AS_AVX
  148. if (sha256_transform_asm == sha256_transform_avx)
  149. pr_info("Using AVX optimized SHA-256 implementation\n");
  150. #ifdef CONFIG_AS_AVX2
  151. else if (sha256_transform_asm == sha256_transform_rorx)
  152. pr_info("Using AVX2 optimized SHA-256 implementation\n");
  153. #endif
  154. else
  155. #endif
  156. pr_info("Using SSSE3 optimized SHA-256 implementation\n");
  157. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  158. }
  159. pr_info("Neither AVX nor SSSE3 is available/usable.\n");
  160. return -ENODEV;
  161. }
  162. static void __exit sha256_ssse3_mod_fini(void)
  163. {
  164. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  165. }
  166. module_init(sha256_ssse3_mod_init);
  167. module_exit(sha256_ssse3_mod_fini);
  168. MODULE_LICENSE("GPL");
  169. MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm, Supplemental SSE3 accelerated");
  170. MODULE_ALIAS_CRYPTO("sha256");
  171. MODULE_ALIAS_CRYPTO("sha224");