crc32-arm64.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * crc32-arm64.c - CRC32 and CRC32C using optional ARMv8 instructions
  3. *
  4. * Module based on crypto/crc32c_generic.c
  5. *
  6. * CRC32 loop taken from Ed Nevill's Hadoop CRC patch
  7. * http://mail-archives.apache.org/mod_mbox/hadoop-common-dev/201406.mbox/%3C1403687030.3355.19.camel%40localhost.localdomain%3E
  8. *
  9. * Using inline assembly instead of intrinsics in order to be backwards
  10. * compatible with older compilers.
  11. *
  12. * Copyright (C) 2014 Linaro Ltd <yazen.ghannam@linaro.org>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/unaligned/access_ok.h>
  19. #include <linux/cpufeature.h>
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/string.h>
  24. #include <crypto/internal/hash.h>
  25. MODULE_AUTHOR("Yazen Ghannam <yazen.ghannam@linaro.org>");
  26. MODULE_DESCRIPTION("CRC32 and CRC32C using optional ARMv8 instructions");
  27. MODULE_LICENSE("GPL v2");
  28. #define CRC32X(crc, value) __asm__("crc32x %w[c], %w[c], %x[v]":[c]"+r"(crc):[v]"r"(value))
  29. #define CRC32W(crc, value) __asm__("crc32w %w[c], %w[c], %w[v]":[c]"+r"(crc):[v]"r"(value))
  30. #define CRC32H(crc, value) __asm__("crc32h %w[c], %w[c], %w[v]":[c]"+r"(crc):[v]"r"(value))
  31. #define CRC32B(crc, value) __asm__("crc32b %w[c], %w[c], %w[v]":[c]"+r"(crc):[v]"r"(value))
  32. #define CRC32CX(crc, value) __asm__("crc32cx %w[c], %w[c], %x[v]":[c]"+r"(crc):[v]"r"(value))
  33. #define CRC32CW(crc, value) __asm__("crc32cw %w[c], %w[c], %w[v]":[c]"+r"(crc):[v]"r"(value))
  34. #define CRC32CH(crc, value) __asm__("crc32ch %w[c], %w[c], %w[v]":[c]"+r"(crc):[v]"r"(value))
  35. #define CRC32CB(crc, value) __asm__("crc32cb %w[c], %w[c], %w[v]":[c]"+r"(crc):[v]"r"(value))
  36. static u32 crc32_arm64_le_hw(u32 crc, const u8 *p, unsigned int len)
  37. {
  38. s64 length = len;
  39. while ((length -= sizeof(u64)) >= 0) {
  40. CRC32X(crc, get_unaligned_le64(p));
  41. p += sizeof(u64);
  42. }
  43. /* The following is more efficient than the straight loop */
  44. if (length & sizeof(u32)) {
  45. CRC32W(crc, get_unaligned_le32(p));
  46. p += sizeof(u32);
  47. }
  48. if (length & sizeof(u16)) {
  49. CRC32H(crc, get_unaligned_le16(p));
  50. p += sizeof(u16);
  51. }
  52. if (length & sizeof(u8))
  53. CRC32B(crc, *p);
  54. return crc;
  55. }
  56. static u32 crc32c_arm64_le_hw(u32 crc, const u8 *p, unsigned int len)
  57. {
  58. s64 length = len;
  59. while ((length -= sizeof(u64)) >= 0) {
  60. CRC32CX(crc, get_unaligned_le64(p));
  61. p += sizeof(u64);
  62. }
  63. /* The following is more efficient than the straight loop */
  64. if (length & sizeof(u32)) {
  65. CRC32CW(crc, get_unaligned_le32(p));
  66. p += sizeof(u32);
  67. }
  68. if (length & sizeof(u16)) {
  69. CRC32CH(crc, get_unaligned_le16(p));
  70. p += sizeof(u16);
  71. }
  72. if (length & sizeof(u8))
  73. CRC32CB(crc, *p);
  74. return crc;
  75. }
  76. #define CHKSUM_BLOCK_SIZE 1
  77. #define CHKSUM_DIGEST_SIZE 4
  78. struct chksum_ctx {
  79. u32 key;
  80. };
  81. struct chksum_desc_ctx {
  82. u32 crc;
  83. };
  84. static int chksum_init(struct shash_desc *desc)
  85. {
  86. struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
  87. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  88. ctx->crc = mctx->key;
  89. return 0;
  90. }
  91. /*
  92. * Setting the seed allows arbitrary accumulators and flexible XOR policy
  93. * If your algorithm starts with ~0, then XOR with ~0 before you set
  94. * the seed.
  95. */
  96. static int chksum_setkey(struct crypto_shash *tfm, const u8 *key,
  97. unsigned int keylen)
  98. {
  99. struct chksum_ctx *mctx = crypto_shash_ctx(tfm);
  100. if (keylen != sizeof(mctx->key)) {
  101. crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  102. return -EINVAL;
  103. }
  104. mctx->key = get_unaligned_le32(key);
  105. return 0;
  106. }
  107. static int chksum_update(struct shash_desc *desc, const u8 *data,
  108. unsigned int length)
  109. {
  110. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  111. ctx->crc = crc32_arm64_le_hw(ctx->crc, data, length);
  112. return 0;
  113. }
  114. static int chksumc_update(struct shash_desc *desc, const u8 *data,
  115. unsigned int length)
  116. {
  117. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  118. ctx->crc = crc32c_arm64_le_hw(ctx->crc, data, length);
  119. return 0;
  120. }
  121. static int chksum_final(struct shash_desc *desc, u8 *out)
  122. {
  123. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  124. put_unaligned_le32(ctx->crc, out);
  125. return 0;
  126. }
  127. static int chksumc_final(struct shash_desc *desc, u8 *out)
  128. {
  129. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  130. put_unaligned_le32(~ctx->crc, out);
  131. return 0;
  132. }
  133. static int __chksum_finup(u32 crc, const u8 *data, unsigned int len, u8 *out)
  134. {
  135. put_unaligned_le32(crc32_arm64_le_hw(crc, data, len), out);
  136. return 0;
  137. }
  138. static int __chksumc_finup(u32 crc, const u8 *data, unsigned int len, u8 *out)
  139. {
  140. put_unaligned_le32(~crc32c_arm64_le_hw(crc, data, len), out);
  141. return 0;
  142. }
  143. static int chksum_finup(struct shash_desc *desc, const u8 *data,
  144. unsigned int len, u8 *out)
  145. {
  146. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  147. return __chksum_finup(ctx->crc, data, len, out);
  148. }
  149. static int chksumc_finup(struct shash_desc *desc, const u8 *data,
  150. unsigned int len, u8 *out)
  151. {
  152. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  153. return __chksumc_finup(ctx->crc, data, len, out);
  154. }
  155. static int chksum_digest(struct shash_desc *desc, const u8 *data,
  156. unsigned int length, u8 *out)
  157. {
  158. struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
  159. return __chksum_finup(mctx->key, data, length, out);
  160. }
  161. static int chksumc_digest(struct shash_desc *desc, const u8 *data,
  162. unsigned int length, u8 *out)
  163. {
  164. struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
  165. return __chksumc_finup(mctx->key, data, length, out);
  166. }
  167. static int crc32_cra_init(struct crypto_tfm *tfm)
  168. {
  169. struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
  170. mctx->key = 0;
  171. return 0;
  172. }
  173. static int crc32c_cra_init(struct crypto_tfm *tfm)
  174. {
  175. struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
  176. mctx->key = ~0;
  177. return 0;
  178. }
  179. static struct shash_alg crc32_alg = {
  180. .digestsize = CHKSUM_DIGEST_SIZE,
  181. .setkey = chksum_setkey,
  182. .init = chksum_init,
  183. .update = chksum_update,
  184. .final = chksum_final,
  185. .finup = chksum_finup,
  186. .digest = chksum_digest,
  187. .descsize = sizeof(struct chksum_desc_ctx),
  188. .base = {
  189. .cra_name = "crc32",
  190. .cra_driver_name = "crc32-arm64-hw",
  191. .cra_priority = 300,
  192. .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
  193. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  194. .cra_alignmask = 0,
  195. .cra_ctxsize = sizeof(struct chksum_ctx),
  196. .cra_module = THIS_MODULE,
  197. .cra_init = crc32_cra_init,
  198. }
  199. };
  200. static struct shash_alg crc32c_alg = {
  201. .digestsize = CHKSUM_DIGEST_SIZE,
  202. .setkey = chksum_setkey,
  203. .init = chksum_init,
  204. .update = chksumc_update,
  205. .final = chksumc_final,
  206. .finup = chksumc_finup,
  207. .digest = chksumc_digest,
  208. .descsize = sizeof(struct chksum_desc_ctx),
  209. .base = {
  210. .cra_name = "crc32c",
  211. .cra_driver_name = "crc32c-arm64-hw",
  212. .cra_priority = 300,
  213. .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
  214. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  215. .cra_alignmask = 0,
  216. .cra_ctxsize = sizeof(struct chksum_ctx),
  217. .cra_module = THIS_MODULE,
  218. .cra_init = crc32c_cra_init,
  219. }
  220. };
  221. static int __init crc32_mod_init(void)
  222. {
  223. int err;
  224. err = crypto_register_shash(&crc32_alg);
  225. if (err)
  226. return err;
  227. err = crypto_register_shash(&crc32c_alg);
  228. if (err) {
  229. crypto_unregister_shash(&crc32_alg);
  230. return err;
  231. }
  232. return 0;
  233. }
  234. static void __exit crc32_mod_exit(void)
  235. {
  236. crypto_unregister_shash(&crc32_alg);
  237. crypto_unregister_shash(&crc32c_alg);
  238. }
  239. module_cpu_feature_match(CRC32, crc32_mod_init);
  240. module_exit(crc32_mod_exit);