crc32-ce-glue.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Accelerated CRC32(C) using arm64 NEON and Crypto Extensions instructions
  3. *
  4. * Copyright (C) 2016 - 2017 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[], u64 len, u32 init_crc);
  25. asmlinkage u32 crc32_armv8_le(u32 init_crc, const u8 buf[], size_t len);
  26. asmlinkage u32 crc32c_pmull_le(const u8 buf[], u64 len, u32 init_crc);
  27. asmlinkage u32 crc32c_armv8_le(u32 init_crc, const u8 buf[], size_t len);
  28. static u32 (*fallback_crc32)(u32 init_crc, const u8 buf[], size_t len);
  29. static u32 (*fallback_crc32c)(u32 init_crc, const u8 buf[], size_t len);
  30. static int crc32_pmull_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_pmull_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_pmull_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_pmull_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_pmull_update(struct shash_desc *desc, const u8 *data,
  75. unsigned int length)
  76. {
  77. u32 *crc = shash_desc_ctx(desc);
  78. unsigned int l;
  79. if ((u64)data % SCALE_F) {
  80. l = min_t(u32, length, SCALE_F - ((u64)data % SCALE_F));
  81. *crc = fallback_crc32(*crc, data, l);
  82. data += l;
  83. length -= l;
  84. }
  85. if (length >= PMULL_MIN_LEN && may_use_simd()) {
  86. l = round_down(length, SCALE_F);
  87. kernel_neon_begin();
  88. *crc = crc32_pmull_le(data, l, *crc);
  89. kernel_neon_end();
  90. data += l;
  91. length -= l;
  92. }
  93. if (length > 0)
  94. *crc = fallback_crc32(*crc, data, length);
  95. return 0;
  96. }
  97. static int crc32c_pmull_update(struct shash_desc *desc, const u8 *data,
  98. unsigned int length)
  99. {
  100. u32 *crc = shash_desc_ctx(desc);
  101. unsigned int l;
  102. if ((u64)data % SCALE_F) {
  103. l = min_t(u32, length, SCALE_F - ((u64)data % SCALE_F));
  104. *crc = fallback_crc32c(*crc, data, l);
  105. data += l;
  106. length -= l;
  107. }
  108. if (length >= PMULL_MIN_LEN && may_use_simd()) {
  109. l = round_down(length, SCALE_F);
  110. kernel_neon_begin();
  111. *crc = crc32c_pmull_le(data, l, *crc);
  112. kernel_neon_end();
  113. data += l;
  114. length -= l;
  115. }
  116. if (length > 0) {
  117. *crc = fallback_crc32c(*crc, data, length);
  118. }
  119. return 0;
  120. }
  121. static int crc32_pmull_final(struct shash_desc *desc, u8 *out)
  122. {
  123. u32 *crc = shash_desc_ctx(desc);
  124. put_unaligned_le32(*crc, out);
  125. return 0;
  126. }
  127. static int crc32c_pmull_final(struct shash_desc *desc, u8 *out)
  128. {
  129. u32 *crc = shash_desc_ctx(desc);
  130. put_unaligned_le32(~*crc, out);
  131. return 0;
  132. }
  133. static struct shash_alg crc32_pmull_algs[] = { {
  134. .setkey = crc32_pmull_setkey,
  135. .init = crc32_pmull_init,
  136. .update = crc32_update,
  137. .final = crc32_pmull_final,
  138. .descsize = sizeof(u32),
  139. .digestsize = sizeof(u32),
  140. .base.cra_ctxsize = sizeof(u32),
  141. .base.cra_init = crc32_pmull_cra_init,
  142. .base.cra_name = "crc32",
  143. .base.cra_driver_name = "crc32-arm64-ce",
  144. .base.cra_priority = 200,
  145. .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
  146. .base.cra_blocksize = 1,
  147. .base.cra_module = THIS_MODULE,
  148. }, {
  149. .setkey = crc32_pmull_setkey,
  150. .init = crc32_pmull_init,
  151. .update = crc32c_update,
  152. .final = crc32c_pmull_final,
  153. .descsize = sizeof(u32),
  154. .digestsize = sizeof(u32),
  155. .base.cra_ctxsize = sizeof(u32),
  156. .base.cra_init = crc32c_pmull_cra_init,
  157. .base.cra_name = "crc32c",
  158. .base.cra_driver_name = "crc32c-arm64-ce",
  159. .base.cra_priority = 200,
  160. .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
  161. .base.cra_blocksize = 1,
  162. .base.cra_module = THIS_MODULE,
  163. } };
  164. static int __init crc32_pmull_mod_init(void)
  165. {
  166. if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) && (elf_hwcap & HWCAP_PMULL)) {
  167. crc32_pmull_algs[0].update = crc32_pmull_update;
  168. crc32_pmull_algs[1].update = crc32c_pmull_update;
  169. if (elf_hwcap & HWCAP_CRC32) {
  170. fallback_crc32 = crc32_armv8_le;
  171. fallback_crc32c = crc32c_armv8_le;
  172. } else {
  173. fallback_crc32 = crc32_le;
  174. fallback_crc32c = __crc32c_le;
  175. }
  176. } else if (!(elf_hwcap & HWCAP_CRC32)) {
  177. return -ENODEV;
  178. }
  179. return crypto_register_shashes(crc32_pmull_algs,
  180. ARRAY_SIZE(crc32_pmull_algs));
  181. }
  182. static void __exit crc32_pmull_mod_exit(void)
  183. {
  184. crypto_unregister_shashes(crc32_pmull_algs,
  185. ARRAY_SIZE(crc32_pmull_algs));
  186. }
  187. static const struct cpu_feature crc32_cpu_feature[] = {
  188. { cpu_feature(CRC32) }, { cpu_feature(PMULL) }, { }
  189. };
  190. MODULE_DEVICE_TABLE(cpu, crc32_cpu_feature);
  191. module_init(crc32_pmull_mod_init);
  192. module_exit(crc32_pmull_mod_exit);
  193. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  194. MODULE_LICENSE("GPL v2");