sha512_ssse3_glue.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #ifdef CONFIG_AS_AVX
  41. asmlinkage void sha512_transform_avx(u64 *digest, const char *data,
  42. u64 rounds);
  43. #endif
  44. #ifdef CONFIG_AS_AVX2
  45. asmlinkage void sha512_transform_rorx(u64 *digest, const char *data,
  46. u64 rounds);
  47. #endif
  48. static void (*sha512_transform_asm)(u64 *, const char *, u64);
  49. static int sha512_ssse3_update(struct shash_desc *desc, const u8 *data,
  50. unsigned int len)
  51. {
  52. struct sha512_state *sctx = shash_desc_ctx(desc);
  53. if (!irq_fpu_usable() ||
  54. (sctx->count[0] % SHA512_BLOCK_SIZE) + len < SHA512_BLOCK_SIZE)
  55. return crypto_sha512_update(desc, data, len);
  56. /* make sure casting to sha512_block_fn() is safe */
  57. BUILD_BUG_ON(offsetof(struct sha512_state, state) != 0);
  58. kernel_fpu_begin();
  59. sha512_base_do_update(desc, data, len,
  60. (sha512_block_fn *)sha512_transform_asm);
  61. kernel_fpu_end();
  62. return 0;
  63. }
  64. static int sha512_ssse3_finup(struct shash_desc *desc, const u8 *data,
  65. unsigned int len, u8 *out)
  66. {
  67. if (!irq_fpu_usable())
  68. return crypto_sha512_finup(desc, data, len, out);
  69. kernel_fpu_begin();
  70. if (len)
  71. sha512_base_do_update(desc, data, len,
  72. (sha512_block_fn *)sha512_transform_asm);
  73. sha512_base_do_finalize(desc, (sha512_block_fn *)sha512_transform_asm);
  74. kernel_fpu_end();
  75. return sha512_base_finish(desc, out);
  76. }
  77. /* Add padding and return the message digest. */
  78. static int sha512_ssse3_final(struct shash_desc *desc, u8 *out)
  79. {
  80. return sha512_ssse3_finup(desc, NULL, 0, out);
  81. }
  82. static struct shash_alg algs[] = { {
  83. .digestsize = SHA512_DIGEST_SIZE,
  84. .init = sha512_base_init,
  85. .update = sha512_ssse3_update,
  86. .final = sha512_ssse3_final,
  87. .finup = sha512_ssse3_finup,
  88. .descsize = sizeof(struct sha512_state),
  89. .base = {
  90. .cra_name = "sha512",
  91. .cra_driver_name = "sha512-ssse3",
  92. .cra_priority = 150,
  93. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  94. .cra_blocksize = SHA512_BLOCK_SIZE,
  95. .cra_module = THIS_MODULE,
  96. }
  97. }, {
  98. .digestsize = SHA384_DIGEST_SIZE,
  99. .init = sha384_base_init,
  100. .update = sha512_ssse3_update,
  101. .final = sha512_ssse3_final,
  102. .finup = sha512_ssse3_finup,
  103. .descsize = sizeof(struct sha512_state),
  104. .base = {
  105. .cra_name = "sha384",
  106. .cra_driver_name = "sha384-ssse3",
  107. .cra_priority = 150,
  108. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  109. .cra_blocksize = SHA384_BLOCK_SIZE,
  110. .cra_module = THIS_MODULE,
  111. }
  112. } };
  113. #ifdef CONFIG_AS_AVX
  114. static bool __init avx_usable(void)
  115. {
  116. if (!cpu_has_xfeatures(XSTATE_SSE | XSTATE_YMM, NULL)) {
  117. if (cpu_has_avx)
  118. pr_info("AVX detected but unusable.\n");
  119. return false;
  120. }
  121. return true;
  122. }
  123. #endif
  124. static int __init sha512_ssse3_mod_init(void)
  125. {
  126. /* test for SSSE3 first */
  127. if (cpu_has_ssse3)
  128. sha512_transform_asm = sha512_transform_ssse3;
  129. #ifdef CONFIG_AS_AVX
  130. /* allow AVX to override SSSE3, it's a little faster */
  131. if (avx_usable()) {
  132. #ifdef CONFIG_AS_AVX2
  133. if (boot_cpu_has(X86_FEATURE_AVX2))
  134. sha512_transform_asm = sha512_transform_rorx;
  135. else
  136. #endif
  137. sha512_transform_asm = sha512_transform_avx;
  138. }
  139. #endif
  140. if (sha512_transform_asm) {
  141. #ifdef CONFIG_AS_AVX
  142. if (sha512_transform_asm == sha512_transform_avx)
  143. pr_info("Using AVX optimized SHA-512 implementation\n");
  144. #ifdef CONFIG_AS_AVX2
  145. else if (sha512_transform_asm == sha512_transform_rorx)
  146. pr_info("Using AVX2 optimized SHA-512 implementation\n");
  147. #endif
  148. else
  149. #endif
  150. pr_info("Using SSSE3 optimized SHA-512 implementation\n");
  151. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  152. }
  153. pr_info("Neither AVX nor SSSE3 is available/usable.\n");
  154. return -ENODEV;
  155. }
  156. static void __exit sha512_ssse3_mod_fini(void)
  157. {
  158. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  159. }
  160. module_init(sha512_ssse3_mod_init);
  161. module_exit(sha512_ssse3_mod_fini);
  162. MODULE_LICENSE("GPL");
  163. MODULE_DESCRIPTION("SHA512 Secure Hash Algorithm, Supplemental SSE3 accelerated");
  164. MODULE_ALIAS_CRYPTO("sha512");
  165. MODULE_ALIAS_CRYPTO("sha384");