crc32-mips.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * crc32-mips.c - CRC32 and CRC32C using optional MIPSr6 instructions
  4. *
  5. * Module based on arm64/crypto/crc32-arm.c
  6. *
  7. * Copyright (C) 2014 Linaro Ltd <[email protected]>
  8. * Copyright (C) 2018 MIPS Tech, LLC
  9. */
  10. #include <linux/cpufeature.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/string.h>
  15. #include <asm/mipsregs.h>
  16. #include <asm/unaligned.h>
  17. #include <crypto/internal/hash.h>
  18. enum crc_op_size {
  19. b, h, w, d,
  20. };
  21. enum crc_type {
  22. crc32,
  23. crc32c,
  24. };
  25. #ifndef TOOLCHAIN_SUPPORTS_CRC
  26. #define _ASM_SET_CRC(OP, SZ, TYPE) \
  27. _ASM_MACRO_3R(OP, rt, rs, rt2, \
  28. ".ifnc \\rt, \\rt2\n\t" \
  29. ".error \"invalid operands \\\"" #OP " \\rt,\\rs,\\rt2\\\"\"\n\t" \
  30. ".endif\n\t" \
  31. _ASM_INSN_IF_MIPS(0x7c00000f | (__rt << 16) | (__rs << 21) | \
  32. ((SZ) << 6) | ((TYPE) << 8)) \
  33. _ASM_INSN32_IF_MM(0x00000030 | (__rs << 16) | (__rt << 21) | \
  34. ((SZ) << 14) | ((TYPE) << 3)))
  35. #define _ASM_UNSET_CRC(op, SZ, TYPE) ".purgem " #op "\n\t"
  36. #else /* !TOOLCHAIN_SUPPORTS_CRC */
  37. #define _ASM_SET_CRC(op, SZ, TYPE) ".set\tcrc\n\t"
  38. #define _ASM_UNSET_CRC(op, SZ, TYPE)
  39. #endif
  40. #define __CRC32(crc, value, op, SZ, TYPE) \
  41. do { \
  42. __asm__ __volatile__( \
  43. ".set push\n\t" \
  44. _ASM_SET_CRC(op, SZ, TYPE) \
  45. #op " %0, %1, %0\n\t" \
  46. _ASM_UNSET_CRC(op, SZ, TYPE) \
  47. ".set pop" \
  48. : "+r" (crc) \
  49. : "r" (value)); \
  50. } while (0)
  51. #define _CRC32_crc32b(crc, value) __CRC32(crc, value, crc32b, 0, 0)
  52. #define _CRC32_crc32h(crc, value) __CRC32(crc, value, crc32h, 1, 0)
  53. #define _CRC32_crc32w(crc, value) __CRC32(crc, value, crc32w, 2, 0)
  54. #define _CRC32_crc32d(crc, value) __CRC32(crc, value, crc32d, 3, 0)
  55. #define _CRC32_crc32cb(crc, value) __CRC32(crc, value, crc32cb, 0, 1)
  56. #define _CRC32_crc32ch(crc, value) __CRC32(crc, value, crc32ch, 1, 1)
  57. #define _CRC32_crc32cw(crc, value) __CRC32(crc, value, crc32cw, 2, 1)
  58. #define _CRC32_crc32cd(crc, value) __CRC32(crc, value, crc32cd, 3, 1)
  59. #define _CRC32(crc, value, size, op) \
  60. _CRC32_##op##size(crc, value)
  61. #define CRC32(crc, value, size) \
  62. _CRC32(crc, value, size, crc32)
  63. #define CRC32C(crc, value, size) \
  64. _CRC32(crc, value, size, crc32c)
  65. static u32 crc32_mips_le_hw(u32 crc_, const u8 *p, unsigned int len)
  66. {
  67. u32 crc = crc_;
  68. #ifdef CONFIG_64BIT
  69. while (len >= sizeof(u64)) {
  70. u64 value = get_unaligned_le64(p);
  71. CRC32(crc, value, d);
  72. p += sizeof(u64);
  73. len -= sizeof(u64);
  74. }
  75. if (len & sizeof(u32)) {
  76. #else /* !CONFIG_64BIT */
  77. while (len >= sizeof(u32)) {
  78. #endif
  79. u32 value = get_unaligned_le32(p);
  80. CRC32(crc, value, w);
  81. p += sizeof(u32);
  82. len -= sizeof(u32);
  83. }
  84. if (len & sizeof(u16)) {
  85. u16 value = get_unaligned_le16(p);
  86. CRC32(crc, value, h);
  87. p += sizeof(u16);
  88. }
  89. if (len & sizeof(u8)) {
  90. u8 value = *p++;
  91. CRC32(crc, value, b);
  92. }
  93. return crc;
  94. }
  95. static u32 crc32c_mips_le_hw(u32 crc_, const u8 *p, unsigned int len)
  96. {
  97. u32 crc = crc_;
  98. #ifdef CONFIG_64BIT
  99. while (len >= sizeof(u64)) {
  100. u64 value = get_unaligned_le64(p);
  101. CRC32C(crc, value, d);
  102. p += sizeof(u64);
  103. len -= sizeof(u64);
  104. }
  105. if (len & sizeof(u32)) {
  106. #else /* !CONFIG_64BIT */
  107. while (len >= sizeof(u32)) {
  108. #endif
  109. u32 value = get_unaligned_le32(p);
  110. CRC32C(crc, value, w);
  111. p += sizeof(u32);
  112. len -= sizeof(u32);
  113. }
  114. if (len & sizeof(u16)) {
  115. u16 value = get_unaligned_le16(p);
  116. CRC32C(crc, value, h);
  117. p += sizeof(u16);
  118. }
  119. if (len & sizeof(u8)) {
  120. u8 value = *p++;
  121. CRC32C(crc, value, b);
  122. }
  123. return crc;
  124. }
  125. #define CHKSUM_BLOCK_SIZE 1
  126. #define CHKSUM_DIGEST_SIZE 4
  127. struct chksum_ctx {
  128. u32 key;
  129. };
  130. struct chksum_desc_ctx {
  131. u32 crc;
  132. };
  133. static int chksum_init(struct shash_desc *desc)
  134. {
  135. struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
  136. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  137. ctx->crc = mctx->key;
  138. return 0;
  139. }
  140. /*
  141. * Setting the seed allows arbitrary accumulators and flexible XOR policy
  142. * If your algorithm starts with ~0, then XOR with ~0 before you set
  143. * the seed.
  144. */
  145. static int chksum_setkey(struct crypto_shash *tfm, const u8 *key,
  146. unsigned int keylen)
  147. {
  148. struct chksum_ctx *mctx = crypto_shash_ctx(tfm);
  149. if (keylen != sizeof(mctx->key))
  150. return -EINVAL;
  151. mctx->key = get_unaligned_le32(key);
  152. return 0;
  153. }
  154. static int chksum_update(struct shash_desc *desc, const u8 *data,
  155. unsigned int length)
  156. {
  157. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  158. ctx->crc = crc32_mips_le_hw(ctx->crc, data, length);
  159. return 0;
  160. }
  161. static int chksumc_update(struct shash_desc *desc, const u8 *data,
  162. unsigned int length)
  163. {
  164. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  165. ctx->crc = crc32c_mips_le_hw(ctx->crc, data, length);
  166. return 0;
  167. }
  168. static int chksum_final(struct shash_desc *desc, u8 *out)
  169. {
  170. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  171. put_unaligned_le32(ctx->crc, out);
  172. return 0;
  173. }
  174. static int chksumc_final(struct shash_desc *desc, u8 *out)
  175. {
  176. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  177. put_unaligned_le32(~ctx->crc, out);
  178. return 0;
  179. }
  180. static int __chksum_finup(u32 crc, const u8 *data, unsigned int len, u8 *out)
  181. {
  182. put_unaligned_le32(crc32_mips_le_hw(crc, data, len), out);
  183. return 0;
  184. }
  185. static int __chksumc_finup(u32 crc, const u8 *data, unsigned int len, u8 *out)
  186. {
  187. put_unaligned_le32(~crc32c_mips_le_hw(crc, data, len), out);
  188. return 0;
  189. }
  190. static int chksum_finup(struct shash_desc *desc, const u8 *data,
  191. unsigned int len, u8 *out)
  192. {
  193. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  194. return __chksum_finup(ctx->crc, data, len, out);
  195. }
  196. static int chksumc_finup(struct shash_desc *desc, const u8 *data,
  197. unsigned int len, u8 *out)
  198. {
  199. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  200. return __chksumc_finup(ctx->crc, data, len, out);
  201. }
  202. static int chksum_digest(struct shash_desc *desc, const u8 *data,
  203. unsigned int length, u8 *out)
  204. {
  205. struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
  206. return __chksum_finup(mctx->key, data, length, out);
  207. }
  208. static int chksumc_digest(struct shash_desc *desc, const u8 *data,
  209. unsigned int length, u8 *out)
  210. {
  211. struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
  212. return __chksumc_finup(mctx->key, data, length, out);
  213. }
  214. static int chksum_cra_init(struct crypto_tfm *tfm)
  215. {
  216. struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
  217. mctx->key = ~0;
  218. return 0;
  219. }
  220. static struct shash_alg crc32_alg = {
  221. .digestsize = CHKSUM_DIGEST_SIZE,
  222. .setkey = chksum_setkey,
  223. .init = chksum_init,
  224. .update = chksum_update,
  225. .final = chksum_final,
  226. .finup = chksum_finup,
  227. .digest = chksum_digest,
  228. .descsize = sizeof(struct chksum_desc_ctx),
  229. .base = {
  230. .cra_name = "crc32",
  231. .cra_driver_name = "crc32-mips-hw",
  232. .cra_priority = 300,
  233. .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
  234. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  235. .cra_alignmask = 0,
  236. .cra_ctxsize = sizeof(struct chksum_ctx),
  237. .cra_module = THIS_MODULE,
  238. .cra_init = chksum_cra_init,
  239. }
  240. };
  241. static struct shash_alg crc32c_alg = {
  242. .digestsize = CHKSUM_DIGEST_SIZE,
  243. .setkey = chksum_setkey,
  244. .init = chksum_init,
  245. .update = chksumc_update,
  246. .final = chksumc_final,
  247. .finup = chksumc_finup,
  248. .digest = chksumc_digest,
  249. .descsize = sizeof(struct chksum_desc_ctx),
  250. .base = {
  251. .cra_name = "crc32c",
  252. .cra_driver_name = "crc32c-mips-hw",
  253. .cra_priority = 300,
  254. .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
  255. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  256. .cra_alignmask = 0,
  257. .cra_ctxsize = sizeof(struct chksum_ctx),
  258. .cra_module = THIS_MODULE,
  259. .cra_init = chksum_cra_init,
  260. }
  261. };
  262. static int __init crc32_mod_init(void)
  263. {
  264. int err;
  265. err = crypto_register_shash(&crc32_alg);
  266. if (err)
  267. return err;
  268. err = crypto_register_shash(&crc32c_alg);
  269. if (err) {
  270. crypto_unregister_shash(&crc32_alg);
  271. return err;
  272. }
  273. return 0;
  274. }
  275. static void __exit crc32_mod_exit(void)
  276. {
  277. crypto_unregister_shash(&crc32_alg);
  278. crypto_unregister_shash(&crc32c_alg);
  279. }
  280. MODULE_AUTHOR("Marcin Nowakowski <[email protected]");
  281. MODULE_DESCRIPTION("CRC32 and CRC32C using optional MIPS instructions");
  282. MODULE_LICENSE("GPL v2");
  283. module_cpu_feature_match(MIPS_CRC32, crc32_mod_init);
  284. module_exit(crc32_mod_exit);