sha512_ssse3_glue.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 <asm/byteorder.h>
  36. #include <asm/i387.h>
  37. #include <asm/xcr.h>
  38. #include <asm/xsave.h>
  39. #include <linux/string.h>
  40. asmlinkage void sha512_transform_ssse3(const char *data, u64 *digest,
  41. u64 rounds);
  42. #ifdef CONFIG_AS_AVX
  43. asmlinkage void sha512_transform_avx(const char *data, u64 *digest,
  44. u64 rounds);
  45. #endif
  46. #ifdef CONFIG_AS_AVX2
  47. asmlinkage void sha512_transform_rorx(const char *data, u64 *digest,
  48. u64 rounds);
  49. #endif
  50. static asmlinkage void (*sha512_transform_asm)(const char *, u64 *, u64);
  51. static int sha512_ssse3_init(struct shash_desc *desc)
  52. {
  53. struct sha512_state *sctx = shash_desc_ctx(desc);
  54. sctx->state[0] = SHA512_H0;
  55. sctx->state[1] = SHA512_H1;
  56. sctx->state[2] = SHA512_H2;
  57. sctx->state[3] = SHA512_H3;
  58. sctx->state[4] = SHA512_H4;
  59. sctx->state[5] = SHA512_H5;
  60. sctx->state[6] = SHA512_H6;
  61. sctx->state[7] = SHA512_H7;
  62. sctx->count[0] = sctx->count[1] = 0;
  63. return 0;
  64. }
  65. static int __sha512_ssse3_update(struct shash_desc *desc, const u8 *data,
  66. unsigned int len, unsigned int partial)
  67. {
  68. struct sha512_state *sctx = shash_desc_ctx(desc);
  69. unsigned int done = 0;
  70. sctx->count[0] += len;
  71. if (sctx->count[0] < len)
  72. sctx->count[1]++;
  73. if (partial) {
  74. done = SHA512_BLOCK_SIZE - partial;
  75. memcpy(sctx->buf + partial, data, done);
  76. sha512_transform_asm(sctx->buf, sctx->state, 1);
  77. }
  78. if (len - done >= SHA512_BLOCK_SIZE) {
  79. const unsigned int rounds = (len - done) / SHA512_BLOCK_SIZE;
  80. sha512_transform_asm(data + done, sctx->state, (u64) rounds);
  81. done += rounds * SHA512_BLOCK_SIZE;
  82. }
  83. memcpy(sctx->buf, data + done, len - done);
  84. return 0;
  85. }
  86. static int sha512_ssse3_update(struct shash_desc *desc, const u8 *data,
  87. unsigned int len)
  88. {
  89. struct sha512_state *sctx = shash_desc_ctx(desc);
  90. unsigned int partial = sctx->count[0] % SHA512_BLOCK_SIZE;
  91. int res;
  92. /* Handle the fast case right here */
  93. if (partial + len < SHA512_BLOCK_SIZE) {
  94. sctx->count[0] += len;
  95. if (sctx->count[0] < len)
  96. sctx->count[1]++;
  97. memcpy(sctx->buf + partial, data, len);
  98. return 0;
  99. }
  100. if (!irq_fpu_usable()) {
  101. res = crypto_sha512_update(desc, data, len);
  102. } else {
  103. kernel_fpu_begin();
  104. res = __sha512_ssse3_update(desc, data, len, partial);
  105. kernel_fpu_end();
  106. }
  107. return res;
  108. }
  109. /* Add padding and return the message digest. */
  110. static int sha512_ssse3_final(struct shash_desc *desc, u8 *out)
  111. {
  112. struct sha512_state *sctx = shash_desc_ctx(desc);
  113. unsigned int i, index, padlen;
  114. __be64 *dst = (__be64 *)out;
  115. __be64 bits[2];
  116. static const u8 padding[SHA512_BLOCK_SIZE] = { 0x80, };
  117. /* save number of bits */
  118. bits[1] = cpu_to_be64(sctx->count[0] << 3);
  119. bits[0] = cpu_to_be64(sctx->count[1] << 3) | sctx->count[0] >> 61;
  120. /* Pad out to 112 mod 128 and append length */
  121. index = sctx->count[0] & 0x7f;
  122. padlen = (index < 112) ? (112 - index) : ((128+112) - index);
  123. if (!irq_fpu_usable()) {
  124. crypto_sha512_update(desc, padding, padlen);
  125. crypto_sha512_update(desc, (const u8 *)&bits, sizeof(bits));
  126. } else {
  127. kernel_fpu_begin();
  128. /* We need to fill a whole block for __sha512_ssse3_update() */
  129. if (padlen <= 112) {
  130. sctx->count[0] += padlen;
  131. if (sctx->count[0] < padlen)
  132. sctx->count[1]++;
  133. memcpy(sctx->buf + index, padding, padlen);
  134. } else {
  135. __sha512_ssse3_update(desc, padding, padlen, index);
  136. }
  137. __sha512_ssse3_update(desc, (const u8 *)&bits,
  138. sizeof(bits), 112);
  139. kernel_fpu_end();
  140. }
  141. /* Store state in digest */
  142. for (i = 0; i < 8; i++)
  143. dst[i] = cpu_to_be64(sctx->state[i]);
  144. /* Wipe context */
  145. memset(sctx, 0, sizeof(*sctx));
  146. return 0;
  147. }
  148. static int sha512_ssse3_export(struct shash_desc *desc, void *out)
  149. {
  150. struct sha512_state *sctx = shash_desc_ctx(desc);
  151. memcpy(out, sctx, sizeof(*sctx));
  152. return 0;
  153. }
  154. static int sha512_ssse3_import(struct shash_desc *desc, const void *in)
  155. {
  156. struct sha512_state *sctx = shash_desc_ctx(desc);
  157. memcpy(sctx, in, sizeof(*sctx));
  158. return 0;
  159. }
  160. static int sha384_ssse3_init(struct shash_desc *desc)
  161. {
  162. struct sha512_state *sctx = shash_desc_ctx(desc);
  163. sctx->state[0] = SHA384_H0;
  164. sctx->state[1] = SHA384_H1;
  165. sctx->state[2] = SHA384_H2;
  166. sctx->state[3] = SHA384_H3;
  167. sctx->state[4] = SHA384_H4;
  168. sctx->state[5] = SHA384_H5;
  169. sctx->state[6] = SHA384_H6;
  170. sctx->state[7] = SHA384_H7;
  171. sctx->count[0] = sctx->count[1] = 0;
  172. return 0;
  173. }
  174. static int sha384_ssse3_final(struct shash_desc *desc, u8 *hash)
  175. {
  176. u8 D[SHA512_DIGEST_SIZE];
  177. sha512_ssse3_final(desc, D);
  178. memcpy(hash, D, SHA384_DIGEST_SIZE);
  179. memset(D, 0, SHA512_DIGEST_SIZE);
  180. return 0;
  181. }
  182. static struct shash_alg algs[] = { {
  183. .digestsize = SHA512_DIGEST_SIZE,
  184. .init = sha512_ssse3_init,
  185. .update = sha512_ssse3_update,
  186. .final = sha512_ssse3_final,
  187. .export = sha512_ssse3_export,
  188. .import = sha512_ssse3_import,
  189. .descsize = sizeof(struct sha512_state),
  190. .statesize = sizeof(struct sha512_state),
  191. .base = {
  192. .cra_name = "sha512",
  193. .cra_driver_name = "sha512-ssse3",
  194. .cra_priority = 150,
  195. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  196. .cra_blocksize = SHA512_BLOCK_SIZE,
  197. .cra_module = THIS_MODULE,
  198. }
  199. }, {
  200. .digestsize = SHA384_DIGEST_SIZE,
  201. .init = sha384_ssse3_init,
  202. .update = sha512_ssse3_update,
  203. .final = sha384_ssse3_final,
  204. .export = sha512_ssse3_export,
  205. .import = sha512_ssse3_import,
  206. .descsize = sizeof(struct sha512_state),
  207. .statesize = sizeof(struct sha512_state),
  208. .base = {
  209. .cra_name = "sha384",
  210. .cra_driver_name = "sha384-ssse3",
  211. .cra_priority = 150,
  212. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  213. .cra_blocksize = SHA384_BLOCK_SIZE,
  214. .cra_module = THIS_MODULE,
  215. }
  216. } };
  217. #ifdef CONFIG_AS_AVX
  218. static bool __init avx_usable(void)
  219. {
  220. u64 xcr0;
  221. if (!cpu_has_avx || !cpu_has_osxsave)
  222. return false;
  223. xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
  224. if ((xcr0 & (XSTATE_SSE | XSTATE_YMM)) != (XSTATE_SSE | XSTATE_YMM)) {
  225. pr_info("AVX detected but unusable.\n");
  226. return false;
  227. }
  228. return true;
  229. }
  230. #endif
  231. static int __init sha512_ssse3_mod_init(void)
  232. {
  233. /* test for SSSE3 first */
  234. if (cpu_has_ssse3)
  235. sha512_transform_asm = sha512_transform_ssse3;
  236. #ifdef CONFIG_AS_AVX
  237. /* allow AVX to override SSSE3, it's a little faster */
  238. if (avx_usable()) {
  239. #ifdef CONFIG_AS_AVX2
  240. if (boot_cpu_has(X86_FEATURE_AVX2))
  241. sha512_transform_asm = sha512_transform_rorx;
  242. else
  243. #endif
  244. sha512_transform_asm = sha512_transform_avx;
  245. }
  246. #endif
  247. if (sha512_transform_asm) {
  248. #ifdef CONFIG_AS_AVX
  249. if (sha512_transform_asm == sha512_transform_avx)
  250. pr_info("Using AVX optimized SHA-512 implementation\n");
  251. #ifdef CONFIG_AS_AVX2
  252. else if (sha512_transform_asm == sha512_transform_rorx)
  253. pr_info("Using AVX2 optimized SHA-512 implementation\n");
  254. #endif
  255. else
  256. #endif
  257. pr_info("Using SSSE3 optimized SHA-512 implementation\n");
  258. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  259. }
  260. pr_info("Neither AVX nor SSSE3 is available/usable.\n");
  261. return -ENODEV;
  262. }
  263. static void __exit sha512_ssse3_mod_fini(void)
  264. {
  265. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  266. }
  267. module_init(sha512_ssse3_mod_init);
  268. module_exit(sha512_ssse3_mod_fini);
  269. MODULE_LICENSE("GPL");
  270. MODULE_DESCRIPTION("SHA512 Secure Hash Algorithm, Supplemental SSE3 accelerated");
  271. MODULE_ALIAS("sha512");
  272. MODULE_ALIAS("sha384");