crc32-ce-glue.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Accelerated CRC32(C) using arm64 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/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[], u64 len, u32 init_crc);
  24. asmlinkage u32 crc32_armv8_le(u32 init_crc, const u8 buf[], size_t len);
  25. asmlinkage u32 crc32c_pmull_le(const u8 buf[], u64 len, u32 init_crc);
  26. asmlinkage u32 crc32c_armv8_le(u32 init_crc, const u8 buf[], size_t len);
  27. static u32 (*fallback_crc32)(u32 init_crc, const u8 buf[], size_t len);
  28. static u32 (*fallback_crc32c)(u32 init_crc, const u8 buf[], size_t len);
  29. static int crc32_pmull_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_pmull_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_pmull_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_pmull_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_pmull_update(struct shash_desc *desc, const u8 *data,
  60. unsigned int length)
  61. {
  62. u32 *crc = shash_desc_ctx(desc);
  63. unsigned int l;
  64. if ((u64)data % SCALE_F) {
  65. l = min_t(u32, length, SCALE_F - ((u64)data % SCALE_F));
  66. *crc = fallback_crc32(*crc, data, l);
  67. data += l;
  68. length -= l;
  69. }
  70. if (length >= PMULL_MIN_LEN) {
  71. l = round_down(length, SCALE_F);
  72. kernel_neon_begin_partial(10);
  73. *crc = crc32_pmull_le(data, l, *crc);
  74. kernel_neon_end();
  75. data += l;
  76. length -= l;
  77. }
  78. if (length > 0)
  79. *crc = fallback_crc32(*crc, data, length);
  80. return 0;
  81. }
  82. static int crc32c_pmull_update(struct shash_desc *desc, const u8 *data,
  83. unsigned int length)
  84. {
  85. u32 *crc = shash_desc_ctx(desc);
  86. unsigned int l;
  87. if ((u64)data % SCALE_F) {
  88. l = min_t(u32, length, SCALE_F - ((u64)data % SCALE_F));
  89. *crc = fallback_crc32c(*crc, data, l);
  90. data += l;
  91. length -= l;
  92. }
  93. if (length >= PMULL_MIN_LEN) {
  94. l = round_down(length, SCALE_F);
  95. kernel_neon_begin_partial(10);
  96. *crc = crc32c_pmull_le(data, l, *crc);
  97. kernel_neon_end();
  98. data += l;
  99. length -= l;
  100. }
  101. if (length > 0) {
  102. *crc = fallback_crc32c(*crc, data, length);
  103. }
  104. return 0;
  105. }
  106. static int crc32_pmull_final(struct shash_desc *desc, u8 *out)
  107. {
  108. u32 *crc = shash_desc_ctx(desc);
  109. put_unaligned_le32(*crc, out);
  110. return 0;
  111. }
  112. static int crc32c_pmull_final(struct shash_desc *desc, u8 *out)
  113. {
  114. u32 *crc = shash_desc_ctx(desc);
  115. put_unaligned_le32(~*crc, out);
  116. return 0;
  117. }
  118. static struct shash_alg crc32_pmull_algs[] = { {
  119. .setkey = crc32_pmull_setkey,
  120. .init = crc32_pmull_init,
  121. .update = crc32_pmull_update,
  122. .final = crc32_pmull_final,
  123. .descsize = sizeof(u32),
  124. .digestsize = sizeof(u32),
  125. .base.cra_ctxsize = sizeof(u32),
  126. .base.cra_init = crc32_pmull_cra_init,
  127. .base.cra_name = "crc32",
  128. .base.cra_driver_name = "crc32-arm64-ce",
  129. .base.cra_priority = 200,
  130. .base.cra_blocksize = 1,
  131. .base.cra_module = THIS_MODULE,
  132. }, {
  133. .setkey = crc32_pmull_setkey,
  134. .init = crc32_pmull_init,
  135. .update = crc32c_pmull_update,
  136. .final = crc32c_pmull_final,
  137. .descsize = sizeof(u32),
  138. .digestsize = sizeof(u32),
  139. .base.cra_ctxsize = sizeof(u32),
  140. .base.cra_init = crc32c_pmull_cra_init,
  141. .base.cra_name = "crc32c",
  142. .base.cra_driver_name = "crc32c-arm64-ce",
  143. .base.cra_priority = 200,
  144. .base.cra_blocksize = 1,
  145. .base.cra_module = THIS_MODULE,
  146. } };
  147. static int __init crc32_pmull_mod_init(void)
  148. {
  149. if (elf_hwcap & HWCAP_CRC32) {
  150. fallback_crc32 = crc32_armv8_le;
  151. fallback_crc32c = crc32c_armv8_le;
  152. } else {
  153. fallback_crc32 = crc32_le;
  154. fallback_crc32c = __crc32c_le;
  155. }
  156. return crypto_register_shashes(crc32_pmull_algs,
  157. ARRAY_SIZE(crc32_pmull_algs));
  158. }
  159. static void __exit crc32_pmull_mod_exit(void)
  160. {
  161. crypto_unregister_shashes(crc32_pmull_algs,
  162. ARRAY_SIZE(crc32_pmull_algs));
  163. }
  164. module_cpu_feature_match(PMULL, crc32_pmull_mod_init);
  165. module_exit(crc32_pmull_mod_exit);
  166. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  167. MODULE_LICENSE("GPL v2");