sha1_ssse3_glue.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Glue code for the SHA1 Secure Hash Algorithm assembler implementation using
  5. * Supplemental SSE3 instructions.
  6. *
  7. * This file is based on sha1_generic.c
  8. *
  9. * Copyright (c) Alan Smithee.
  10. * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
  11. * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
  12. * Copyright (c) Mathias Krause <minipli@googlemail.com>
  13. * Copyright (c) Chandramouli Narayanan <mouli@linux.intel.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the Free
  17. * Software Foundation; either version 2 of the License, or (at your option)
  18. * any later version.
  19. *
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <crypto/internal/hash.h>
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/mm.h>
  26. #include <linux/cryptohash.h>
  27. #include <linux/types.h>
  28. #include <crypto/sha.h>
  29. #include <crypto/sha1_base.h>
  30. #include <asm/fpu/api.h>
  31. asmlinkage void sha1_transform_ssse3(u32 *digest, const char *data,
  32. unsigned int rounds);
  33. #ifdef CONFIG_AS_AVX
  34. asmlinkage void sha1_transform_avx(u32 *digest, const char *data,
  35. unsigned int rounds);
  36. #endif
  37. #ifdef CONFIG_AS_AVX2
  38. #define SHA1_AVX2_BLOCK_OPTSIZE 4 /* optimal 4*64 bytes of SHA1 blocks */
  39. asmlinkage void sha1_transform_avx2(u32 *digest, const char *data,
  40. unsigned int rounds);
  41. #endif
  42. static void (*sha1_transform_asm)(u32 *, const char *, unsigned int);
  43. static int sha1_ssse3_update(struct shash_desc *desc, const u8 *data,
  44. unsigned int len)
  45. {
  46. struct sha1_state *sctx = shash_desc_ctx(desc);
  47. if (!irq_fpu_usable() ||
  48. (sctx->count % SHA1_BLOCK_SIZE) + len < SHA1_BLOCK_SIZE)
  49. return crypto_sha1_update(desc, data, len);
  50. /* make sure casting to sha1_block_fn() is safe */
  51. BUILD_BUG_ON(offsetof(struct sha1_state, state) != 0);
  52. kernel_fpu_begin();
  53. sha1_base_do_update(desc, data, len,
  54. (sha1_block_fn *)sha1_transform_asm);
  55. kernel_fpu_end();
  56. return 0;
  57. }
  58. static int sha1_ssse3_finup(struct shash_desc *desc, const u8 *data,
  59. unsigned int len, u8 *out)
  60. {
  61. if (!irq_fpu_usable())
  62. return crypto_sha1_finup(desc, data, len, out);
  63. kernel_fpu_begin();
  64. if (len)
  65. sha1_base_do_update(desc, data, len,
  66. (sha1_block_fn *)sha1_transform_asm);
  67. sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_transform_asm);
  68. kernel_fpu_end();
  69. return sha1_base_finish(desc, out);
  70. }
  71. /* Add padding and return the message digest. */
  72. static int sha1_ssse3_final(struct shash_desc *desc, u8 *out)
  73. {
  74. return sha1_ssse3_finup(desc, NULL, 0, out);
  75. }
  76. #ifdef CONFIG_AS_AVX2
  77. static void sha1_apply_transform_avx2(u32 *digest, const char *data,
  78. unsigned int rounds)
  79. {
  80. /* Select the optimal transform based on data block size */
  81. if (rounds >= SHA1_AVX2_BLOCK_OPTSIZE)
  82. sha1_transform_avx2(digest, data, rounds);
  83. else
  84. sha1_transform_avx(digest, data, rounds);
  85. }
  86. #endif
  87. static struct shash_alg alg = {
  88. .digestsize = SHA1_DIGEST_SIZE,
  89. .init = sha1_base_init,
  90. .update = sha1_ssse3_update,
  91. .final = sha1_ssse3_final,
  92. .finup = sha1_ssse3_finup,
  93. .descsize = sizeof(struct sha1_state),
  94. .base = {
  95. .cra_name = "sha1",
  96. .cra_driver_name= "sha1-ssse3",
  97. .cra_priority = 150,
  98. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  99. .cra_blocksize = SHA1_BLOCK_SIZE,
  100. .cra_module = THIS_MODULE,
  101. }
  102. };
  103. #ifdef CONFIG_AS_AVX
  104. static bool __init avx_usable(void)
  105. {
  106. if (!cpu_has_xfeatures(XSTATE_SSE | XSTATE_YMM, NULL)) {
  107. if (cpu_has_avx)
  108. pr_info("AVX detected but unusable.\n");
  109. return false;
  110. }
  111. return true;
  112. }
  113. #ifdef CONFIG_AS_AVX2
  114. static bool __init avx2_usable(void)
  115. {
  116. if (avx_usable() && cpu_has_avx2 && boot_cpu_has(X86_FEATURE_BMI1) &&
  117. boot_cpu_has(X86_FEATURE_BMI2))
  118. return true;
  119. return false;
  120. }
  121. #endif
  122. #endif
  123. static int __init sha1_ssse3_mod_init(void)
  124. {
  125. char *algo_name;
  126. /* test for SSSE3 first */
  127. if (cpu_has_ssse3) {
  128. sha1_transform_asm = sha1_transform_ssse3;
  129. algo_name = "SSSE3";
  130. }
  131. #ifdef CONFIG_AS_AVX
  132. /* allow AVX to override SSSE3, it's a little faster */
  133. if (avx_usable()) {
  134. sha1_transform_asm = sha1_transform_avx;
  135. algo_name = "AVX";
  136. #ifdef CONFIG_AS_AVX2
  137. /* allow AVX2 to override AVX, it's a little faster */
  138. if (avx2_usable()) {
  139. sha1_transform_asm = sha1_apply_transform_avx2;
  140. algo_name = "AVX2";
  141. }
  142. #endif
  143. }
  144. #endif
  145. if (sha1_transform_asm) {
  146. pr_info("Using %s optimized SHA-1 implementation\n", algo_name);
  147. return crypto_register_shash(&alg);
  148. }
  149. pr_info("Neither AVX nor AVX2 nor SSSE3 is available/usable.\n");
  150. return -ENODEV;
  151. }
  152. static void __exit sha1_ssse3_mod_fini(void)
  153. {
  154. crypto_unregister_shash(&alg);
  155. }
  156. module_init(sha1_ssse3_mod_init);
  157. module_exit(sha1_ssse3_mod_fini);
  158. MODULE_LICENSE("GPL");
  159. MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, Supplemental SSE3 accelerated");
  160. MODULE_ALIAS_CRYPTO("sha1");