crc32c.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Cryptographic API.
  3. *
  4. * CRC32C chksum
  5. *
  6. * This module file is a wrapper to invoke the lib/crc32c routines.
  7. *
  8. * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. *
  15. */
  16. #include <crypto/internal/hash.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/string.h>
  20. #include <linux/crc32c.h>
  21. #include <linux/kernel.h>
  22. #define CHKSUM_BLOCK_SIZE 1
  23. #define CHKSUM_DIGEST_SIZE 4
  24. struct chksum_ctx {
  25. u32 key;
  26. };
  27. struct chksum_desc_ctx {
  28. u32 crc;
  29. };
  30. /*
  31. * Steps through buffer one byte at at time, calculates reflected
  32. * crc using table.
  33. */
  34. static int chksum_init(struct shash_desc *desc)
  35. {
  36. struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
  37. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  38. ctx->crc = mctx->key;
  39. return 0;
  40. }
  41. /*
  42. * Setting the seed allows arbitrary accumulators and flexible XOR policy
  43. * If your algorithm starts with ~0, then XOR with ~0 before you set
  44. * the seed.
  45. */
  46. static int chksum_setkey(struct crypto_shash *tfm, const u8 *key,
  47. unsigned int keylen)
  48. {
  49. struct chksum_ctx *mctx = crypto_shash_ctx(tfm);
  50. if (keylen != sizeof(mctx->key)) {
  51. crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  52. return -EINVAL;
  53. }
  54. mctx->key = le32_to_cpu(*(__le32 *)key);
  55. return 0;
  56. }
  57. static int chksum_update(struct shash_desc *desc, const u8 *data,
  58. unsigned int length)
  59. {
  60. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  61. ctx->crc = crc32c(ctx->crc, data, length);
  62. return 0;
  63. }
  64. static int chksum_final(struct shash_desc *desc, u8 *out)
  65. {
  66. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  67. *(__le32 *)out = ~cpu_to_le32p(&ctx->crc);
  68. return 0;
  69. }
  70. static int __chksum_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out)
  71. {
  72. *(__le32 *)out = ~cpu_to_le32(crc32c(*crcp, data, len));
  73. return 0;
  74. }
  75. static int chksum_finup(struct shash_desc *desc, const u8 *data,
  76. unsigned int len, u8 *out)
  77. {
  78. struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
  79. return __chksum_finup(&ctx->crc, data, len, out);
  80. }
  81. static int chksum_digest(struct shash_desc *desc, const u8 *data,
  82. unsigned int length, u8 *out)
  83. {
  84. struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
  85. return __chksum_finup(&mctx->key, data, length, out);
  86. }
  87. static int crc32c_cra_init(struct crypto_tfm *tfm)
  88. {
  89. struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
  90. mctx->key = ~0;
  91. return 0;
  92. }
  93. static struct shash_alg alg = {
  94. .digestsize = CHKSUM_DIGEST_SIZE,
  95. .setkey = chksum_setkey,
  96. .init = chksum_init,
  97. .update = chksum_update,
  98. .final = chksum_final,
  99. .finup = chksum_finup,
  100. .digest = chksum_digest,
  101. .descsize = sizeof(struct chksum_desc_ctx),
  102. .base = {
  103. .cra_name = "crc32c",
  104. .cra_driver_name = "crc32c-generic",
  105. .cra_priority = 100,
  106. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  107. .cra_alignmask = 3,
  108. .cra_ctxsize = sizeof(struct chksum_ctx),
  109. .cra_module = THIS_MODULE,
  110. .cra_init = crc32c_cra_init,
  111. }
  112. };
  113. static int __init crc32c_mod_init(void)
  114. {
  115. return crypto_register_shash(&alg);
  116. }
  117. static void __exit crc32c_mod_fini(void)
  118. {
  119. crypto_unregister_shash(&alg);
  120. }
  121. module_init(crc32c_mod_init);
  122. module_exit(crc32c_mod_fini);
  123. MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
  124. MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
  125. MODULE_LICENSE("GPL");