1
2
3
4
5
6
7
8
9
10
11
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
21#define CHKSUM_BLOCK_SIZE 32
22#define CHKSUM_DIGEST_SIZE 4
23
24struct chksum_ctx {
25 u32 crc;
26 u32 key;
27};
28
29
30
31
32
33
34static void chksum_init(struct crypto_tfm *tfm)
35{
36 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
37
38 mctx->crc = mctx->key;
39}
40
41
42
43
44
45
46static int chksum_setkey(struct crypto_tfm *tfm, const u8 *key,
47 unsigned int keylen)
48{
49 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
50
51 if (keylen != sizeof(mctx->crc)) {
52 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
53 return -EINVAL;
54 }
55 mctx->key = le32_to_cpu(*(__le32 *)key);
56 return 0;
57}
58
59static void chksum_update(struct crypto_tfm *tfm, const u8 *data,
60 unsigned int length)
61{
62 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
63
64 mctx->crc = crc32c(mctx->crc, data, length);
65}
66
67static void chksum_final(struct crypto_tfm *tfm, u8 *out)
68{
69 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
70
71 *(__le32 *)out = ~cpu_to_le32(mctx->crc);
72}
73
74static int crc32c_cra_init(struct crypto_tfm *tfm)
75{
76 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
77
78 mctx->key = ~0;
79 return 0;
80}
81
82static struct crypto_alg alg = {
83 .cra_name = "crc32c",
84 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
85 .cra_blocksize = CHKSUM_BLOCK_SIZE,
86 .cra_ctxsize = sizeof(struct chksum_ctx),
87 .cra_module = THIS_MODULE,
88 .cra_list = LIST_HEAD_INIT(alg.cra_list),
89 .cra_init = crc32c_cra_init,
90 .cra_u = {
91 .digest = {
92 .dia_digestsize= CHKSUM_DIGEST_SIZE,
93 .dia_setkey = chksum_setkey,
94 .dia_init = chksum_init,
95 .dia_update = chksum_update,
96 .dia_final = chksum_final
97 }
98 }
99};
100
101static int __init init(void)
102{
103 return crypto_register_alg(&alg);
104}
105
106static void __exit fini(void)
107{
108 crypto_unregister_alg(&alg);
109}
110
111module_init(init);
112module_exit(fini);
113
114MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
115MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
116MODULE_LICENSE("GPL");
117