crc32-ce-glue.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Accelerated CRC32(C) using ARM CRC, NEON and Crypto Extensions instructions
  3. *
  4. * Copyright (C) 2016 Linaro Ltd <ard.biesheuvel@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/cpufeature.h>
  11. #include <linux/crc32.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/string.h>
  16. #include <crypto/internal/hash.h>
  17. #include <asm/hwcap.h>
  18. #include <asm/neon.h>
  19. #include <asm/simd.h>
  20. #include <asm/unaligned.h>
  21. #define PMULL_MIN_LEN 64L /* minimum size of buffer
  22. * for crc32_pmull_le_16 */
  23. #define SCALE_F 16L /* size of NEON register */
  24. asmlinkage u32 crc32_pmull_le(const u8 buf[], u32 len, u32 init_crc);
  25. asmlinkage u32 crc32_armv8_le(u32 init_crc, const u8 buf[], u32 len);
  26. asmlinkage u32 crc32c_pmull_le(const u8 buf[], u32 len, u32 init_crc);
  27. asmlinkage u32 crc32c_armv8_le(u32 init_crc, const u8 buf[], u32 len);
  28. static u32 (*fallback_crc32)(u32 init_crc, const u8 buf[], u32 len);
  29. static u32 (*fallback_crc32c)(u32 init_crc, const u8 buf[], u32 len);
  30. static int crc32_cra_init(struct crypto_tfm *tfm)
  31. {
  32. u32 *key = crypto_tfm_ctx(tfm);
  33. *key = 0;
  34. return 0;
  35. }
  36. static int crc32c_cra_init(struct crypto_tfm *tfm)
  37. {
  38. u32 *key = crypto_tfm_ctx(tfm);
  39. *key = ~0;
  40. return 0;
  41. }
  42. static int crc32_setkey(struct crypto_shash *hash, const u8 *key,
  43. unsigned int keylen)
  44. {
  45. u32 *mctx = crypto_shash_ctx(hash);
  46. if (keylen != sizeof(u32)) {
  47. crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
  48. return -EINVAL;
  49. }
  50. *mctx = le32_to_cpup((__le32 *)key);
  51. return 0;
  52. }
  53. static int crc32_init(struct shash_desc *desc)
  54. {
  55. u32 *mctx = crypto_shash_ctx(desc->tfm);
  56. u32 *crc = shash_desc_ctx(desc);
  57. *crc = *mctx;
  58. return 0;
  59. }
  60. static int crc32_update(struct shash_desc *desc, const u8 *data,
  61. unsigned int length)
  62. {
  63. u32 *crc = shash_desc_ctx(desc);
  64. *crc = crc32_armv8_le(*crc, data, length);
  65. return 0;
  66. }
  67. static int crc32c_update(struct shash_desc *desc, const u8 *data,
  68. unsigned int length)
  69. {
  70. u32 *crc = shash_desc_ctx(desc);
  71. *crc = crc32c_armv8_le(*crc, data, length);
  72. return 0;
  73. }
  74. static int crc32_final(struct shash_desc *desc, u8 *out)
  75. {
  76. u32 *crc = shash_desc_ctx(desc);
  77. put_unaligned_le32(*crc, out);
  78. return 0;
  79. }
  80. static int crc32c_final(struct shash_desc *desc, u8 *out)
  81. {
  82. u32 *crc = shash_desc_ctx(desc);
  83. put_unaligned_le32(~*crc, out);
  84. return 0;
  85. }
  86. static int crc32_pmull_update(struct shash_desc *desc, const u8 *data,
  87. unsigned int length)
  88. {
  89. u32 *crc = shash_desc_ctx(desc);
  90. unsigned int l;
  91. if (may_use_simd()) {
  92. if ((u32)data % SCALE_F) {
  93. l = min_t(u32, length, SCALE_F - ((u32)data % SCALE_F));
  94. *crc = fallback_crc32(*crc, data, l);
  95. data += l;
  96. length -= l;
  97. }
  98. if (length >= PMULL_MIN_LEN) {
  99. l = round_down(length, SCALE_F);
  100. kernel_neon_begin();
  101. *crc = crc32_pmull_le(data, l, *crc);
  102. kernel_neon_end();
  103. data += l;
  104. length -= l;
  105. }
  106. }
  107. if (length > 0)
  108. *crc = fallback_crc32(*crc, data, length);
  109. return 0;
  110. }
  111. static int crc32c_pmull_update(struct shash_desc *desc, const u8 *data,
  112. unsigned int length)
  113. {
  114. u32 *crc = shash_desc_ctx(desc);
  115. unsigned int l;
  116. if (may_use_simd()) {
  117. if ((u32)data % SCALE_F) {
  118. l = min_t(u32, length, SCALE_F - ((u32)data % SCALE_F));
  119. *crc = fallback_crc32c(*crc, data, l);
  120. data += l;
  121. length -= l;
  122. }
  123. if (length >= PMULL_MIN_LEN) {
  124. l = round_down(length, SCALE_F);
  125. kernel_neon_begin();
  126. *crc = crc32c_pmull_le(data, l, *crc);
  127. kernel_neon_end();
  128. data += l;
  129. length -= l;
  130. }
  131. }
  132. if (length > 0)
  133. *crc = fallback_crc32c(*crc, data, length);
  134. return 0;
  135. }
  136. static struct shash_alg crc32_pmull_algs[] = { {
  137. .setkey = crc32_setkey,
  138. .init = crc32_init,
  139. .update = crc32_update,
  140. .final = crc32_final,
  141. .descsize = sizeof(u32),
  142. .digestsize = sizeof(u32),
  143. .base.cra_ctxsize = sizeof(u32),
  144. .base.cra_init = crc32_cra_init,
  145. .base.cra_name = "crc32",
  146. .base.cra_driver_name = "crc32-arm-ce",
  147. .base.cra_priority = 200,
  148. .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
  149. .base.cra_blocksize = 1,
  150. .base.cra_module = THIS_MODULE,
  151. }, {
  152. .setkey = crc32_setkey,
  153. .init = crc32_init,
  154. .update = crc32c_update,
  155. .final = crc32c_final,
  156. .descsize = sizeof(u32),
  157. .digestsize = sizeof(u32),
  158. .base.cra_ctxsize = sizeof(u32),
  159. .base.cra_init = crc32c_cra_init,
  160. .base.cra_name = "crc32c",
  161. .base.cra_driver_name = "crc32c-arm-ce",
  162. .base.cra_priority = 200,
  163. .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
  164. .base.cra_blocksize = 1,
  165. .base.cra_module = THIS_MODULE,
  166. } };
  167. static int __init crc32_pmull_mod_init(void)
  168. {
  169. if (elf_hwcap2 & HWCAP2_PMULL) {
  170. crc32_pmull_algs[0].update = crc32_pmull_update;
  171. crc32_pmull_algs[1].update = crc32c_pmull_update;
  172. if (elf_hwcap2 & HWCAP2_CRC32) {
  173. fallback_crc32 = crc32_armv8_le;
  174. fallback_crc32c = crc32c_armv8_le;
  175. } else {
  176. fallback_crc32 = crc32_le;
  177. fallback_crc32c = __crc32c_le;
  178. }
  179. } else if (!(elf_hwcap2 & HWCAP2_CRC32)) {
  180. return -ENODEV;
  181. }
  182. return crypto_register_shashes(crc32_pmull_algs,
  183. ARRAY_SIZE(crc32_pmull_algs));
  184. }
  185. static void __exit crc32_pmull_mod_exit(void)
  186. {
  187. crypto_unregister_shashes(crc32_pmull_algs,
  188. ARRAY_SIZE(crc32_pmull_algs));
  189. }
  190. static const struct cpu_feature __maybe_unused crc32_cpu_feature[] = {
  191. { cpu_feature(CRC32) }, { cpu_feature(PMULL) }, { }
  192. };
  193. MODULE_DEVICE_TABLE(cpu, crc32_cpu_feature);
  194. module_init(crc32_pmull_mod_init);
  195. module_exit(crc32_pmull_mod_exit);
  196. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  197. MODULE_LICENSE("GPL v2");
  198. MODULE_ALIAS_CRYPTO("crc32");
  199. MODULE_ALIAS_CRYPTO("crc32c");