crc32c.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 crc;
  26. u32 key;
  27. };
  28. /*
  29. * Steps through buffer one byte at at time, calculates reflected
  30. * crc using table.
  31. */
  32. static void chksum_init(struct crypto_tfm *tfm)
  33. {
  34. struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
  35. mctx->crc = mctx->key;
  36. }
  37. /*
  38. * Setting the seed allows arbitrary accumulators and flexible XOR policy
  39. * If your algorithm starts with ~0, then XOR with ~0 before you set
  40. * the seed.
  41. */
  42. static int chksum_setkey(struct crypto_tfm *tfm, const u8 *key,
  43. unsigned int keylen)
  44. {
  45. struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
  46. if (keylen != sizeof(mctx->crc)) {
  47. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  48. return -EINVAL;
  49. }
  50. mctx->key = le32_to_cpu(*(__le32 *)key);
  51. return 0;
  52. }
  53. static void chksum_update(struct crypto_tfm *tfm, const u8 *data,
  54. unsigned int length)
  55. {
  56. struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
  57. mctx->crc = crc32c(mctx->crc, data, length);
  58. }
  59. static void chksum_final(struct crypto_tfm *tfm, u8 *out)
  60. {
  61. struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
  62. *(__le32 *)out = ~cpu_to_le32(mctx->crc);
  63. }
  64. static int crc32c_cra_init_old(struct crypto_tfm *tfm)
  65. {
  66. struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
  67. mctx->key = ~0;
  68. return 0;
  69. }
  70. static struct crypto_alg old_alg = {
  71. .cra_name = "crc32c",
  72. .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
  73. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  74. .cra_ctxsize = sizeof(struct chksum_ctx),
  75. .cra_module = THIS_MODULE,
  76. .cra_list = LIST_HEAD_INIT(old_alg.cra_list),
  77. .cra_init = crc32c_cra_init_old,
  78. .cra_u = {
  79. .digest = {
  80. .dia_digestsize= CHKSUM_DIGEST_SIZE,
  81. .dia_setkey = chksum_setkey,
  82. .dia_init = chksum_init,
  83. .dia_update = chksum_update,
  84. .dia_final = chksum_final
  85. }
  86. }
  87. };
  88. /*
  89. * Setting the seed allows arbitrary accumulators and flexible XOR policy
  90. * If your algorithm starts with ~0, then XOR with ~0 before you set
  91. * the seed.
  92. */
  93. static int crc32c_setkey(struct crypto_ahash *hash, const u8 *key,
  94. unsigned int keylen)
  95. {
  96. u32 *mctx = crypto_ahash_ctx(hash);
  97. if (keylen != sizeof(u32)) {
  98. crypto_ahash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
  99. return -EINVAL;
  100. }
  101. *mctx = le32_to_cpup((__le32 *)key);
  102. return 0;
  103. }
  104. static int crc32c_init(struct ahash_request *req)
  105. {
  106. u32 *mctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  107. u32 *crcp = ahash_request_ctx(req);
  108. *crcp = *mctx;
  109. return 0;
  110. }
  111. static int crc32c_update(struct ahash_request *req)
  112. {
  113. struct crypto_hash_walk walk;
  114. u32 *crcp = ahash_request_ctx(req);
  115. u32 crc = *crcp;
  116. int nbytes;
  117. for (nbytes = crypto_hash_walk_first(req, &walk); nbytes;
  118. nbytes = crypto_hash_walk_done(&walk, 0))
  119. crc = crc32c(crc, walk.data, nbytes);
  120. *crcp = crc;
  121. return 0;
  122. }
  123. static int crc32c_final(struct ahash_request *req)
  124. {
  125. u32 *crcp = ahash_request_ctx(req);
  126. *(__le32 *)req->result = ~cpu_to_le32p(crcp);
  127. return 0;
  128. }
  129. static int crc32c_digest(struct ahash_request *req)
  130. {
  131. struct crypto_hash_walk walk;
  132. u32 *mctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  133. u32 crc = *mctx;
  134. int nbytes;
  135. for (nbytes = crypto_hash_walk_first(req, &walk); nbytes;
  136. nbytes = crypto_hash_walk_done(&walk, 0))
  137. crc = crc32c(crc, walk.data, nbytes);
  138. *(__le32 *)req->result = ~cpu_to_le32(crc);
  139. return 0;
  140. }
  141. static int crc32c_cra_init(struct crypto_tfm *tfm)
  142. {
  143. u32 *key = crypto_tfm_ctx(tfm);
  144. *key = ~0;
  145. tfm->crt_ahash.reqsize = sizeof(u32);
  146. return 0;
  147. }
  148. static struct crypto_alg alg = {
  149. .cra_name = "crc32c",
  150. .cra_driver_name = "crc32c-generic",
  151. .cra_priority = 100,
  152. .cra_flags = CRYPTO_ALG_TYPE_AHASH,
  153. .cra_blocksize = CHKSUM_BLOCK_SIZE,
  154. .cra_alignmask = 3,
  155. .cra_ctxsize = sizeof(u32),
  156. .cra_module = THIS_MODULE,
  157. .cra_list = LIST_HEAD_INIT(alg.cra_list),
  158. .cra_init = crc32c_cra_init,
  159. .cra_type = &crypto_ahash_type,
  160. .cra_u = {
  161. .ahash = {
  162. .digestsize = CHKSUM_DIGEST_SIZE,
  163. .setkey = crc32c_setkey,
  164. .init = crc32c_init,
  165. .update = crc32c_update,
  166. .final = crc32c_final,
  167. .digest = crc32c_digest,
  168. }
  169. }
  170. };
  171. static int __init crc32c_mod_init(void)
  172. {
  173. int err;
  174. err = crypto_register_alg(&old_alg);
  175. if (err)
  176. return err;
  177. err = crypto_register_alg(&alg);
  178. if (err)
  179. crypto_unregister_alg(&old_alg);
  180. return err;
  181. }
  182. static void __exit crc32c_mod_fini(void)
  183. {
  184. crypto_unregister_alg(&alg);
  185. crypto_unregister_alg(&old_alg);
  186. }
  187. module_init(crc32c_mod_init);
  188. module_exit(crc32c_mod_fini);
  189. MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
  190. MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
  191. MODULE_LICENSE("GPL");