crc32c.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Cryptographic API.
  3. *
  4. * CRC32C chksum
  5. *
  6. * This module file is a wrapper to invoke the lib/crc32c routines.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/string.h>
  17. #include <linux/crypto.h>
  18. #include <linux/crc32c.h>
  19. #include <linux/kernel.h>
  20. #define CHKSUM_BLOCK_SIZE 32
  21. #define CHKSUM_DIGEST_SIZE 4
  22. struct chksum_ctx {
  23. u32 crc;
  24. u32 key;
  25. };
  26. /*
  27. * Steps through buffer one byte at at time, calculates reflected
  28. * crc using table.
  29. */
  30. static void chksum_init(struct crypto_tfm *tfm)
  31. {
  32. struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
  33. mctx->crc = mctx->key;
  34. }
  35. /*
  36. * Setting the seed allows arbitrary accumulators and flexible XOR policy
  37. * If your algorithm starts with ~0, then XOR with ~0 before you set
  38. * the seed.
  39. */
  40. static int chksum_setkey(struct crypto_tfm *tfm, const u8 *key,
  41. unsigned int keylen)
  42. {
  43. struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
  44. if (keylen != sizeof(mctx->crc)) {
  45. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  46. return -EINVAL;
  47. }
  48. mctx->key = le32_to_cpu(*(__le32 *)key);
  49. return 0;
  50. }
  51. static void chksum_update(struct crypto_tfm *tfm, const u8 *data,
  52. unsigned int length)
  53. {
  54. struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
  55. mctx->crc = crc32c(mctx->crc, data, length);
  56. }
  57. static void chksum_final(struct crypto_tfm *tfm, u8 *out)
  58. {
  59. struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
  60. *(__le32 *)out = ~cpu_to_le32(mctx->crc);
  61. }
  62. static int crc32c_cra_init(struct crypto_tfm *tfm)
  63. {
  64. struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
  65. mctx->key = ~0;
  66. return 0;
  67. }
  68. static struct crypto_alg alg = {
  69. .cra_name = "crc32c",
  70. .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
  71. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  72. .cra_ctxsize = sizeof(struct chksum_ctx),
  73. .cra_module = THIS_MODULE,
  74. .cra_list = LIST_HEAD_INIT(alg.cra_list),
  75. .cra_init = crc32c_cra_init,
  76. .cra_u = {
  77. .digest = {
  78. .dia_digestsize= CHKSUM_DIGEST_SIZE,
  79. .dia_setkey = chksum_setkey,
  80. .dia_init = chksum_init,
  81. .dia_update = chksum_update,
  82. .dia_final = chksum_final
  83. }
  84. }
  85. };
  86. static int __init init(void)
  87. {
  88. return crypto_register_alg(&alg);
  89. }
  90. static void __exit fini(void)
  91. {
  92. crypto_unregister_alg(&alg);
  93. }
  94. module_init(init);
  95. module_exit(fini);
  96. MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
  97. MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
  98. MODULE_LICENSE("GPL");