This source file includes following definitions.
- chksum_init
- chksum_setkey
- chksum_update
- chksum_final
- __chksum_finup
- chksum_finup
- chksum_digest
- crc32c_cra_init
- crc32c_mod_init
- crc32c_mod_fini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 #include <asm/unaligned.h>
34 #include <crypto/internal/hash.h>
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
38 #include <linux/kernel.h>
39 #include <linux/crc32.h>
40
41 #define CHKSUM_BLOCK_SIZE 1
42 #define CHKSUM_DIGEST_SIZE 4
43
44 struct chksum_ctx {
45 u32 key;
46 };
47
48 struct chksum_desc_ctx {
49 u32 crc;
50 };
51
52
53
54
55
56
57 static int chksum_init(struct shash_desc *desc)
58 {
59 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
60 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
61
62 ctx->crc = mctx->key;
63
64 return 0;
65 }
66
67
68
69
70
71
72 static int chksum_setkey(struct crypto_shash *tfm, const u8 *key,
73 unsigned int keylen)
74 {
75 struct chksum_ctx *mctx = crypto_shash_ctx(tfm);
76
77 if (keylen != sizeof(mctx->key)) {
78 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
79 return -EINVAL;
80 }
81 mctx->key = get_unaligned_le32(key);
82 return 0;
83 }
84
85 static int chksum_update(struct shash_desc *desc, const u8 *data,
86 unsigned int length)
87 {
88 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
89
90 ctx->crc = __crc32c_le(ctx->crc, data, length);
91 return 0;
92 }
93
94 static int chksum_final(struct shash_desc *desc, u8 *out)
95 {
96 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
97
98 put_unaligned_le32(~ctx->crc, out);
99 return 0;
100 }
101
102 static int __chksum_finup(u32 *crcp, const u8 *data, unsigned int len, u8 *out)
103 {
104 put_unaligned_le32(~__crc32c_le(*crcp, data, len), out);
105 return 0;
106 }
107
108 static int chksum_finup(struct shash_desc *desc, const u8 *data,
109 unsigned int len, u8 *out)
110 {
111 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
112
113 return __chksum_finup(&ctx->crc, data, len, out);
114 }
115
116 static int chksum_digest(struct shash_desc *desc, const u8 *data,
117 unsigned int length, u8 *out)
118 {
119 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
120
121 return __chksum_finup(&mctx->key, data, length, out);
122 }
123
124 static int crc32c_cra_init(struct crypto_tfm *tfm)
125 {
126 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
127
128 mctx->key = ~0;
129 return 0;
130 }
131
132 static struct shash_alg alg = {
133 .digestsize = CHKSUM_DIGEST_SIZE,
134 .setkey = chksum_setkey,
135 .init = chksum_init,
136 .update = chksum_update,
137 .final = chksum_final,
138 .finup = chksum_finup,
139 .digest = chksum_digest,
140 .descsize = sizeof(struct chksum_desc_ctx),
141 .base = {
142 .cra_name = "crc32c",
143 .cra_driver_name = "crc32c-generic",
144 .cra_priority = 100,
145 .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
146 .cra_blocksize = CHKSUM_BLOCK_SIZE,
147 .cra_ctxsize = sizeof(struct chksum_ctx),
148 .cra_module = THIS_MODULE,
149 .cra_init = crc32c_cra_init,
150 }
151 };
152
153 static int __init crc32c_mod_init(void)
154 {
155 return crypto_register_shash(&alg);
156 }
157
158 static void __exit crc32c_mod_fini(void)
159 {
160 crypto_unregister_shash(&alg);
161 }
162
163 subsys_initcall(crc32c_mod_init);
164 module_exit(crc32c_mod_fini);
165
166 MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
167 MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
168 MODULE_LICENSE("GPL");
169 MODULE_ALIAS_CRYPTO("crc32c");
170 MODULE_ALIAS_CRYPTO("crc32c-generic");