crc32-ce-glue.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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/crc32.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/string.h>
  15. #include <crypto/internal/hash.h>
  16. #include <asm/hwcap.h>
  17. #include <asm/neon.h>
  18. #include <asm/simd.h>
  19. #include <asm/unaligned.h>
  20. #define PMULL_MIN_LEN 64L /* minimum size of buffer
  21. * for crc32_pmull_le_16 */
  22. #define SCALE_F 16L /* size of NEON register */
  23. asmlinkage u32 crc32_pmull_le(const u8 buf[], u32 len, u32 init_crc);
  24. asmlinkage u32 crc32_armv8_le(u32 init_crc, const u8 buf[], u32 len);
  25. asmlinkage u32 crc32c_pmull_le(const u8 buf[], u32 len, u32 init_crc);
  26. asmlinkage u32 crc32c_armv8_le(u32 init_crc, const u8 buf[], u32 len);
  27. static u32 (*fallback_crc32)(u32 init_crc, const u8 buf[], u32 len);
  28. static u32 (*fallback_crc32c)(u32 init_crc, const u8 buf[], u32 len);
  29. static int crc32_cra_init(struct crypto_tfm *tfm)
  30. {
  31. u32 *key = crypto_tfm_ctx(tfm);
  32. *key = 0;
  33. return 0;
  34. }
  35. static int crc32c_cra_init(struct crypto_tfm *tfm)
  36. {
  37. u32 *key = crypto_tfm_ctx(tfm);
  38. *key = ~0;
  39. return 0;
  40. }
  41. static int crc32_setkey(struct crypto_shash *hash, const u8 *key,
  42. unsigned int keylen)
  43. {
  44. u32 *mctx = crypto_shash_ctx(hash);
  45. if (keylen != sizeof(u32)) {
  46. crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
  47. return -EINVAL;
  48. }
  49. *mctx = le32_to_cpup((__le32 *)key);
  50. return 0;
  51. }
  52. static int crc32_init(struct shash_desc *desc)
  53. {
  54. u32 *mctx = crypto_shash_ctx(desc->tfm);
  55. u32 *crc = shash_desc_ctx(desc);
  56. *crc = *mctx;
  57. return 0;
  58. }
  59. static int crc32_update(struct shash_desc *desc, const u8 *data,
  60. unsigned int length)
  61. {
  62. u32 *crc = shash_desc_ctx(desc);
  63. *crc = crc32_armv8_le(*crc, data, length);
  64. return 0;
  65. }
  66. static int crc32c_update(struct shash_desc *desc, const u8 *data,
  67. unsigned int length)
  68. {
  69. u32 *crc = shash_desc_ctx(desc);
  70. *crc = crc32c_armv8_le(*crc, data, length);
  71. return 0;
  72. }
  73. static int crc32_final(struct shash_desc *desc, u8 *out)
  74. {
  75. u32 *crc = shash_desc_ctx(desc);
  76. put_unaligned_le32(*crc, out);
  77. return 0;
  78. }
  79. static int crc32c_final(struct shash_desc *desc, u8 *out)
  80. {
  81. u32 *crc = shash_desc_ctx(desc);
  82. put_unaligned_le32(~*crc, out);
  83. return 0;
  84. }
  85. static int crc32_pmull_update(struct shash_desc *desc, const u8 *data,
  86. unsigned int length)
  87. {
  88. u32 *crc = shash_desc_ctx(desc);
  89. unsigned int l;
  90. if (may_use_simd()) {
  91. if ((u32)data % SCALE_F) {
  92. l = min_t(u32, length, SCALE_F - ((u32)data % SCALE_F));
  93. *crc = fallback_crc32(*crc, data, l);
  94. data += l;
  95. length -= l;
  96. }
  97. if (length >= PMULL_MIN_LEN) {
  98. l = round_down(length, SCALE_F);
  99. kernel_neon_begin();
  100. *crc = crc32_pmull_le(data, l, *crc);
  101. kernel_neon_end();
  102. data += l;
  103. length -= l;
  104. }
  105. }
  106. if (length > 0)
  107. *crc = fallback_crc32(*crc, data, length);
  108. return 0;
  109. }
  110. static int crc32c_pmull_update(struct shash_desc *desc, const u8 *data,
  111. unsigned int length)
  112. {
  113. u32 *crc = shash_desc_ctx(desc);
  114. unsigned int l;
  115. if (may_use_simd()) {
  116. if ((u32)data % SCALE_F) {
  117. l = min_t(u32, length, SCALE_F - ((u32)data % SCALE_F));
  118. *crc = fallback_crc32c(*crc, data, l);
  119. data += l;
  120. length -= l;
  121. }
  122. if (length >= PMULL_MIN_LEN) {
  123. l = round_down(length, SCALE_F);
  124. kernel_neon_begin();
  125. *crc = crc32c_pmull_le(data, l, *crc);
  126. kernel_neon_end();
  127. data += l;
  128. length -= l;
  129. }
  130. }
  131. if (length > 0)
  132. *crc = fallback_crc32c(*crc, data, length);
  133. return 0;
  134. }
  135. static struct shash_alg crc32_pmull_algs[] = { {
  136. .setkey = crc32_setkey,
  137. .init = crc32_init,
  138. .update = crc32_update,
  139. .final = crc32_final,
  140. .descsize = sizeof(u32),
  141. .digestsize = sizeof(u32),
  142. .base.cra_ctxsize = sizeof(u32),
  143. .base.cra_init = crc32_cra_init,
  144. .base.cra_name = "crc32",
  145. .base.cra_driver_name = "crc32-arm-ce",
  146. .base.cra_priority = 200,
  147. .base.cra_blocksize = 1,
  148. .base.cra_module = THIS_MODULE,
  149. }, {
  150. .setkey = crc32_setkey,
  151. .init = crc32_init,
  152. .update = crc32c_update,
  153. .final = crc32c_final,
  154. .descsize = sizeof(u32),
  155. .digestsize = sizeof(u32),
  156. .base.cra_ctxsize = sizeof(u32),
  157. .base.cra_init = crc32c_cra_init,
  158. .base.cra_name = "crc32c",
  159. .base.cra_driver_name = "crc32c-arm-ce",
  160. .base.cra_priority = 200,
  161. .base.cra_blocksize = 1,
  162. .base.cra_module = THIS_MODULE,
  163. } };
  164. static int __init crc32_pmull_mod_init(void)
  165. {
  166. if (elf_hwcap2 & HWCAP2_PMULL) {
  167. crc32_pmull_algs[0].update = crc32_pmull_update;
  168. crc32_pmull_algs[1].update = crc32c_pmull_update;
  169. if (elf_hwcap2 & HWCAP2_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_hwcap2 & HWCAP2_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. module_init(crc32_pmull_mod_init);
  188. module_exit(crc32_pmull_mod_exit);
  189. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  190. MODULE_LICENSE("GPL v2");
  191. MODULE_ALIAS_CRYPTO("crc32");
  192. MODULE_ALIAS_CRYPTO("crc32c");