sha1_ssse3_glue.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. typedef void (sha1_transform_fn)(u32 *digest, const char *data,
  32. unsigned int rounds);
  33. static int sha1_update(struct shash_desc *desc, const u8 *data,
  34. unsigned int len, sha1_transform_fn *sha1_xform)
  35. {
  36. struct sha1_state *sctx = shash_desc_ctx(desc);
  37. if (!irq_fpu_usable() ||
  38. (sctx->count % SHA1_BLOCK_SIZE) + len < SHA1_BLOCK_SIZE)
  39. return crypto_sha1_update(desc, data, len);
  40. /* make sure casting to sha1_block_fn() is safe */
  41. BUILD_BUG_ON(offsetof(struct sha1_state, state) != 0);
  42. kernel_fpu_begin();
  43. sha1_base_do_update(desc, data, len,
  44. (sha1_block_fn *)sha1_xform);
  45. kernel_fpu_end();
  46. return 0;
  47. }
  48. static int sha1_finup(struct shash_desc *desc, const u8 *data,
  49. unsigned int len, u8 *out, sha1_transform_fn *sha1_xform)
  50. {
  51. if (!irq_fpu_usable())
  52. return crypto_sha1_finup(desc, data, len, out);
  53. kernel_fpu_begin();
  54. if (len)
  55. sha1_base_do_update(desc, data, len,
  56. (sha1_block_fn *)sha1_xform);
  57. sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_xform);
  58. kernel_fpu_end();
  59. return sha1_base_finish(desc, out);
  60. }
  61. asmlinkage void sha1_transform_ssse3(u32 *digest, const char *data,
  62. unsigned int rounds);
  63. static int sha1_ssse3_update(struct shash_desc *desc, const u8 *data,
  64. unsigned int len)
  65. {
  66. return sha1_update(desc, data, len,
  67. (sha1_transform_fn *) sha1_transform_ssse3);
  68. }
  69. static int sha1_ssse3_finup(struct shash_desc *desc, const u8 *data,
  70. unsigned int len, u8 *out)
  71. {
  72. return sha1_finup(desc, data, len, out,
  73. (sha1_transform_fn *) sha1_transform_ssse3);
  74. }
  75. /* Add padding and return the message digest. */
  76. static int sha1_ssse3_final(struct shash_desc *desc, u8 *out)
  77. {
  78. return sha1_ssse3_finup(desc, NULL, 0, out);
  79. }
  80. static struct shash_alg sha1_ssse3_alg = {
  81. .digestsize = SHA1_DIGEST_SIZE,
  82. .init = sha1_base_init,
  83. .update = sha1_ssse3_update,
  84. .final = sha1_ssse3_final,
  85. .finup = sha1_ssse3_finup,
  86. .descsize = sizeof(struct sha1_state),
  87. .base = {
  88. .cra_name = "sha1",
  89. .cra_driver_name = "sha1-ssse3",
  90. .cra_priority = 150,
  91. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  92. .cra_blocksize = SHA1_BLOCK_SIZE,
  93. .cra_module = THIS_MODULE,
  94. }
  95. };
  96. static int register_sha1_ssse3(void)
  97. {
  98. if (boot_cpu_has(X86_FEATURE_SSSE3))
  99. return crypto_register_shash(&sha1_ssse3_alg);
  100. return 0;
  101. }
  102. static void unregister_sha1_ssse3(void)
  103. {
  104. if (boot_cpu_has(X86_FEATURE_SSSE3))
  105. crypto_unregister_shash(&sha1_ssse3_alg);
  106. }
  107. #ifdef CONFIG_AS_AVX
  108. asmlinkage void sha1_transform_avx(u32 *digest, const char *data,
  109. unsigned int rounds);
  110. static int sha1_avx_update(struct shash_desc *desc, const u8 *data,
  111. unsigned int len)
  112. {
  113. return sha1_update(desc, data, len,
  114. (sha1_transform_fn *) sha1_transform_avx);
  115. }
  116. static int sha1_avx_finup(struct shash_desc *desc, const u8 *data,
  117. unsigned int len, u8 *out)
  118. {
  119. return sha1_finup(desc, data, len, out,
  120. (sha1_transform_fn *) sha1_transform_avx);
  121. }
  122. static int sha1_avx_final(struct shash_desc *desc, u8 *out)
  123. {
  124. return sha1_avx_finup(desc, NULL, 0, out);
  125. }
  126. static struct shash_alg sha1_avx_alg = {
  127. .digestsize = SHA1_DIGEST_SIZE,
  128. .init = sha1_base_init,
  129. .update = sha1_avx_update,
  130. .final = sha1_avx_final,
  131. .finup = sha1_avx_finup,
  132. .descsize = sizeof(struct sha1_state),
  133. .base = {
  134. .cra_name = "sha1",
  135. .cra_driver_name = "sha1-avx",
  136. .cra_priority = 160,
  137. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  138. .cra_blocksize = SHA1_BLOCK_SIZE,
  139. .cra_module = THIS_MODULE,
  140. }
  141. };
  142. static bool avx_usable(void)
  143. {
  144. if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL)) {
  145. if (cpu_has_avx)
  146. pr_info("AVX detected but unusable.\n");
  147. return false;
  148. }
  149. return true;
  150. }
  151. static int register_sha1_avx(void)
  152. {
  153. if (avx_usable())
  154. return crypto_register_shash(&sha1_avx_alg);
  155. return 0;
  156. }
  157. static void unregister_sha1_avx(void)
  158. {
  159. if (avx_usable())
  160. crypto_unregister_shash(&sha1_avx_alg);
  161. }
  162. #else /* CONFIG_AS_AVX */
  163. static inline int register_sha1_avx(void) { return 0; }
  164. static inline void unregister_sha1_avx(void) { }
  165. #endif /* CONFIG_AS_AVX */
  166. #if defined(CONFIG_AS_AVX2) && (CONFIG_AS_AVX)
  167. #define SHA1_AVX2_BLOCK_OPTSIZE 4 /* optimal 4*64 bytes of SHA1 blocks */
  168. asmlinkage void sha1_transform_avx2(u32 *digest, const char *data,
  169. unsigned int rounds);
  170. static bool avx2_usable(void)
  171. {
  172. if (avx_usable() && boot_cpu_has(X86_FEATURE_AVX2)
  173. && boot_cpu_has(X86_FEATURE_BMI1)
  174. && boot_cpu_has(X86_FEATURE_BMI2))
  175. return true;
  176. return false;
  177. }
  178. static void sha1_apply_transform_avx2(u32 *digest, const char *data,
  179. unsigned int rounds)
  180. {
  181. /* Select the optimal transform based on data block size */
  182. if (rounds >= SHA1_AVX2_BLOCK_OPTSIZE)
  183. sha1_transform_avx2(digest, data, rounds);
  184. else
  185. sha1_transform_avx(digest, data, rounds);
  186. }
  187. static int sha1_avx2_update(struct shash_desc *desc, const u8 *data,
  188. unsigned int len)
  189. {
  190. return sha1_update(desc, data, len,
  191. (sha1_transform_fn *) sha1_apply_transform_avx2);
  192. }
  193. static int sha1_avx2_finup(struct shash_desc *desc, const u8 *data,
  194. unsigned int len, u8 *out)
  195. {
  196. return sha1_finup(desc, data, len, out,
  197. (sha1_transform_fn *) sha1_apply_transform_avx2);
  198. }
  199. static int sha1_avx2_final(struct shash_desc *desc, u8 *out)
  200. {
  201. return sha1_avx2_finup(desc, NULL, 0, out);
  202. }
  203. static struct shash_alg sha1_avx2_alg = {
  204. .digestsize = SHA1_DIGEST_SIZE,
  205. .init = sha1_base_init,
  206. .update = sha1_avx2_update,
  207. .final = sha1_avx2_final,
  208. .finup = sha1_avx2_finup,
  209. .descsize = sizeof(struct sha1_state),
  210. .base = {
  211. .cra_name = "sha1",
  212. .cra_driver_name = "sha1-avx2",
  213. .cra_priority = 170,
  214. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  215. .cra_blocksize = SHA1_BLOCK_SIZE,
  216. .cra_module = THIS_MODULE,
  217. }
  218. };
  219. static int register_sha1_avx2(void)
  220. {
  221. if (avx2_usable())
  222. return crypto_register_shash(&sha1_avx2_alg);
  223. return 0;
  224. }
  225. static void unregister_sha1_avx2(void)
  226. {
  227. if (avx2_usable())
  228. crypto_unregister_shash(&sha1_avx2_alg);
  229. }
  230. #else
  231. static inline int register_sha1_avx2(void) { return 0; }
  232. static inline void unregister_sha1_avx2(void) { }
  233. #endif
  234. #ifdef CONFIG_AS_SHA1_NI
  235. asmlinkage void sha1_ni_transform(u32 *digest, const char *data,
  236. unsigned int rounds);
  237. static int sha1_ni_update(struct shash_desc *desc, const u8 *data,
  238. unsigned int len)
  239. {
  240. return sha1_update(desc, data, len,
  241. (sha1_transform_fn *) sha1_ni_transform);
  242. }
  243. static int sha1_ni_finup(struct shash_desc *desc, const u8 *data,
  244. unsigned int len, u8 *out)
  245. {
  246. return sha1_finup(desc, data, len, out,
  247. (sha1_transform_fn *) sha1_ni_transform);
  248. }
  249. static int sha1_ni_final(struct shash_desc *desc, u8 *out)
  250. {
  251. return sha1_ni_finup(desc, NULL, 0, out);
  252. }
  253. static struct shash_alg sha1_ni_alg = {
  254. .digestsize = SHA1_DIGEST_SIZE,
  255. .init = sha1_base_init,
  256. .update = sha1_ni_update,
  257. .final = sha1_ni_final,
  258. .finup = sha1_ni_finup,
  259. .descsize = sizeof(struct sha1_state),
  260. .base = {
  261. .cra_name = "sha1",
  262. .cra_driver_name = "sha1-ni",
  263. .cra_priority = 250,
  264. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  265. .cra_blocksize = SHA1_BLOCK_SIZE,
  266. .cra_module = THIS_MODULE,
  267. }
  268. };
  269. static int register_sha1_ni(void)
  270. {
  271. if (boot_cpu_has(X86_FEATURE_SHA_NI))
  272. return crypto_register_shash(&sha1_ni_alg);
  273. return 0;
  274. }
  275. static void unregister_sha1_ni(void)
  276. {
  277. if (boot_cpu_has(X86_FEATURE_SHA_NI))
  278. crypto_unregister_shash(&sha1_ni_alg);
  279. }
  280. #else
  281. static inline int register_sha1_ni(void) { return 0; }
  282. static inline void unregister_sha1_ni(void) { }
  283. #endif
  284. static int __init sha1_ssse3_mod_init(void)
  285. {
  286. if (register_sha1_ssse3())
  287. goto fail;
  288. if (register_sha1_avx()) {
  289. unregister_sha1_ssse3();
  290. goto fail;
  291. }
  292. if (register_sha1_avx2()) {
  293. unregister_sha1_avx();
  294. unregister_sha1_ssse3();
  295. goto fail;
  296. }
  297. if (register_sha1_ni()) {
  298. unregister_sha1_avx2();
  299. unregister_sha1_avx();
  300. unregister_sha1_ssse3();
  301. goto fail;
  302. }
  303. return 0;
  304. fail:
  305. return -ENODEV;
  306. }
  307. static void __exit sha1_ssse3_mod_fini(void)
  308. {
  309. unregister_sha1_ni();
  310. unregister_sha1_avx2();
  311. unregister_sha1_avx();
  312. unregister_sha1_ssse3();
  313. }
  314. module_init(sha1_ssse3_mod_init);
  315. module_exit(sha1_ssse3_mod_fini);
  316. MODULE_LICENSE("GPL");
  317. MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, Supplemental SSE3 accelerated");
  318. MODULE_ALIAS_CRYPTO("sha1");