sha256_ssse3_glue.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 <asm/byteorder.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(const char *data, u32 *digest,
  42. u64 rounds);
  43. #ifdef CONFIG_AS_AVX
  44. asmlinkage void sha256_transform_avx(const char *data, u32 *digest,
  45. u64 rounds);
  46. #endif
  47. #ifdef CONFIG_AS_AVX2
  48. asmlinkage void sha256_transform_rorx(const char *data, u32 *digest,
  49. u64 rounds);
  50. #endif
  51. static asmlinkage void (*sha256_transform_asm)(const char *, u32 *, u64);
  52. static int sha256_ssse3_init(struct shash_desc *desc)
  53. {
  54. struct sha256_state *sctx = shash_desc_ctx(desc);
  55. sctx->state[0] = SHA256_H0;
  56. sctx->state[1] = SHA256_H1;
  57. sctx->state[2] = SHA256_H2;
  58. sctx->state[3] = SHA256_H3;
  59. sctx->state[4] = SHA256_H4;
  60. sctx->state[5] = SHA256_H5;
  61. sctx->state[6] = SHA256_H6;
  62. sctx->state[7] = SHA256_H7;
  63. sctx->count = 0;
  64. return 0;
  65. }
  66. static int __sha256_ssse3_update(struct shash_desc *desc, const u8 *data,
  67. unsigned int len, unsigned int partial)
  68. {
  69. struct sha256_state *sctx = shash_desc_ctx(desc);
  70. unsigned int done = 0;
  71. sctx->count += len;
  72. if (partial) {
  73. done = SHA256_BLOCK_SIZE - partial;
  74. memcpy(sctx->buf + partial, data, done);
  75. sha256_transform_asm(sctx->buf, sctx->state, 1);
  76. }
  77. if (len - done >= SHA256_BLOCK_SIZE) {
  78. const unsigned int rounds = (len - done) / SHA256_BLOCK_SIZE;
  79. sha256_transform_asm(data + done, sctx->state, (u64) rounds);
  80. done += rounds * SHA256_BLOCK_SIZE;
  81. }
  82. memcpy(sctx->buf, data + done, len - done);
  83. return 0;
  84. }
  85. static int sha256_ssse3_update(struct shash_desc *desc, const u8 *data,
  86. unsigned int len)
  87. {
  88. struct sha256_state *sctx = shash_desc_ctx(desc);
  89. unsigned int partial = sctx->count % SHA256_BLOCK_SIZE;
  90. int res;
  91. /* Handle the fast case right here */
  92. if (partial + len < SHA256_BLOCK_SIZE) {
  93. sctx->count += len;
  94. memcpy(sctx->buf + partial, data, len);
  95. return 0;
  96. }
  97. if (!irq_fpu_usable()) {
  98. res = crypto_sha256_update(desc, data, len);
  99. } else {
  100. kernel_fpu_begin();
  101. res = __sha256_ssse3_update(desc, data, len, partial);
  102. kernel_fpu_end();
  103. }
  104. return res;
  105. }
  106. /* Add padding and return the message digest. */
  107. static int sha256_ssse3_final(struct shash_desc *desc, u8 *out)
  108. {
  109. struct sha256_state *sctx = shash_desc_ctx(desc);
  110. unsigned int i, index, padlen;
  111. __be32 *dst = (__be32 *)out;
  112. __be64 bits;
  113. static const u8 padding[SHA256_BLOCK_SIZE] = { 0x80, };
  114. bits = cpu_to_be64(sctx->count << 3);
  115. /* Pad out to 56 mod 64 and append length */
  116. index = sctx->count % SHA256_BLOCK_SIZE;
  117. padlen = (index < 56) ? (56 - index) : ((SHA256_BLOCK_SIZE+56)-index);
  118. if (!irq_fpu_usable()) {
  119. crypto_sha256_update(desc, padding, padlen);
  120. crypto_sha256_update(desc, (const u8 *)&bits, sizeof(bits));
  121. } else {
  122. kernel_fpu_begin();
  123. /* We need to fill a whole block for __sha256_ssse3_update() */
  124. if (padlen <= 56) {
  125. sctx->count += padlen;
  126. memcpy(sctx->buf + index, padding, padlen);
  127. } else {
  128. __sha256_ssse3_update(desc, padding, padlen, index);
  129. }
  130. __sha256_ssse3_update(desc, (const u8 *)&bits,
  131. sizeof(bits), 56);
  132. kernel_fpu_end();
  133. }
  134. /* Store state in digest */
  135. for (i = 0; i < 8; i++)
  136. dst[i] = cpu_to_be32(sctx->state[i]);
  137. /* Wipe context */
  138. memset(sctx, 0, sizeof(*sctx));
  139. return 0;
  140. }
  141. static int sha256_ssse3_export(struct shash_desc *desc, void *out)
  142. {
  143. struct sha256_state *sctx = shash_desc_ctx(desc);
  144. memcpy(out, sctx, sizeof(*sctx));
  145. return 0;
  146. }
  147. static int sha256_ssse3_import(struct shash_desc *desc, const void *in)
  148. {
  149. struct sha256_state *sctx = shash_desc_ctx(desc);
  150. memcpy(sctx, in, sizeof(*sctx));
  151. return 0;
  152. }
  153. static int sha224_ssse3_init(struct shash_desc *desc)
  154. {
  155. struct sha256_state *sctx = shash_desc_ctx(desc);
  156. sctx->state[0] = SHA224_H0;
  157. sctx->state[1] = SHA224_H1;
  158. sctx->state[2] = SHA224_H2;
  159. sctx->state[3] = SHA224_H3;
  160. sctx->state[4] = SHA224_H4;
  161. sctx->state[5] = SHA224_H5;
  162. sctx->state[6] = SHA224_H6;
  163. sctx->state[7] = SHA224_H7;
  164. sctx->count = 0;
  165. return 0;
  166. }
  167. static int sha224_ssse3_final(struct shash_desc *desc, u8 *hash)
  168. {
  169. u8 D[SHA256_DIGEST_SIZE];
  170. sha256_ssse3_final(desc, D);
  171. memcpy(hash, D, SHA224_DIGEST_SIZE);
  172. memset(D, 0, SHA256_DIGEST_SIZE);
  173. return 0;
  174. }
  175. static struct shash_alg algs[] = { {
  176. .digestsize = SHA256_DIGEST_SIZE,
  177. .init = sha256_ssse3_init,
  178. .update = sha256_ssse3_update,
  179. .final = sha256_ssse3_final,
  180. .export = sha256_ssse3_export,
  181. .import = sha256_ssse3_import,
  182. .descsize = sizeof(struct sha256_state),
  183. .statesize = sizeof(struct sha256_state),
  184. .base = {
  185. .cra_name = "sha256",
  186. .cra_driver_name = "sha256-ssse3",
  187. .cra_priority = 150,
  188. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  189. .cra_blocksize = SHA256_BLOCK_SIZE,
  190. .cra_module = THIS_MODULE,
  191. }
  192. }, {
  193. .digestsize = SHA224_DIGEST_SIZE,
  194. .init = sha224_ssse3_init,
  195. .update = sha256_ssse3_update,
  196. .final = sha224_ssse3_final,
  197. .export = sha256_ssse3_export,
  198. .import = sha256_ssse3_import,
  199. .descsize = sizeof(struct sha256_state),
  200. .statesize = sizeof(struct sha256_state),
  201. .base = {
  202. .cra_name = "sha224",
  203. .cra_driver_name = "sha224-ssse3",
  204. .cra_priority = 150,
  205. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  206. .cra_blocksize = SHA224_BLOCK_SIZE,
  207. .cra_module = THIS_MODULE,
  208. }
  209. } };
  210. #ifdef CONFIG_AS_AVX
  211. static bool __init avx_usable(void)
  212. {
  213. u64 xcr0;
  214. if (!cpu_has_avx || !cpu_has_osxsave)
  215. return false;
  216. xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
  217. if ((xcr0 & (XSTATE_SSE | XSTATE_YMM)) != (XSTATE_SSE | XSTATE_YMM)) {
  218. pr_info("AVX detected but unusable.\n");
  219. return false;
  220. }
  221. return true;
  222. }
  223. #endif
  224. static int __init sha256_ssse3_mod_init(void)
  225. {
  226. /* test for SSSE3 first */
  227. if (cpu_has_ssse3)
  228. sha256_transform_asm = sha256_transform_ssse3;
  229. #ifdef CONFIG_AS_AVX
  230. /* allow AVX to override SSSE3, it's a little faster */
  231. if (avx_usable()) {
  232. #ifdef CONFIG_AS_AVX2
  233. if (boot_cpu_has(X86_FEATURE_AVX2))
  234. sha256_transform_asm = sha256_transform_rorx;
  235. else
  236. #endif
  237. sha256_transform_asm = sha256_transform_avx;
  238. }
  239. #endif
  240. if (sha256_transform_asm) {
  241. #ifdef CONFIG_AS_AVX
  242. if (sha256_transform_asm == sha256_transform_avx)
  243. pr_info("Using AVX optimized SHA-256 implementation\n");
  244. #ifdef CONFIG_AS_AVX2
  245. else if (sha256_transform_asm == sha256_transform_rorx)
  246. pr_info("Using AVX2 optimized SHA-256 implementation\n");
  247. #endif
  248. else
  249. #endif
  250. pr_info("Using SSSE3 optimized SHA-256 implementation\n");
  251. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  252. }
  253. pr_info("Neither AVX nor SSSE3 is available/usable.\n");
  254. return -ENODEV;
  255. }
  256. static void __exit sha256_ssse3_mod_fini(void)
  257. {
  258. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  259. }
  260. module_init(sha256_ssse3_mod_init);
  261. module_exit(sha256_ssse3_mod_fini);
  262. MODULE_LICENSE("GPL");
  263. MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm, Supplemental SSE3 accelerated");
  264. MODULE_ALIAS("sha256");
  265. MODULE_ALIAS("sha384");