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#include <linux/crc32.h>
33#include <crypto/internal/hash.h>
34#include <linux/init.h>
35#include <linux/module.h>
36#include <linux/string.h>
37#include <linux/kernel.h>
38
39#define CHKSUM_BLOCK_SIZE 1
40#define CHKSUM_DIGEST_SIZE 4
41
42static u32 __crc32_le(u32 crc, unsigned char const *p, size_t len)
43{
44 return crc32_le(crc, p, len);
45}
46
47
48static int crc32_cra_init(struct crypto_tfm *tfm)
49{
50 u32 *key = crypto_tfm_ctx(tfm);
51
52 *key = 0;
53
54 return 0;
55}
56
57
58
59
60
61
62
63static int crc32_setkey(struct crypto_shash *hash, const u8 *key,
64 unsigned int keylen)
65{
66 u32 *mctx = crypto_shash_ctx(hash);
67
68 if (keylen != sizeof(u32)) {
69 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
70 return -EINVAL;
71 }
72 *mctx = le32_to_cpup((__le32 *)key);
73 return 0;
74}
75
76static int crc32_init(struct shash_desc *desc)
77{
78 u32 *mctx = crypto_shash_ctx(desc->tfm);
79 u32 *crcp = shash_desc_ctx(desc);
80
81 *crcp = *mctx;
82
83 return 0;
84}
85
86static int crc32_update(struct shash_desc *desc, const u8 *data,
87 unsigned int len)
88{
89 u32 *crcp = shash_desc_ctx(desc);
90
91 *crcp = __crc32_le(*crcp, data, len);
92 return 0;
93}
94
95
96static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len,
97 u8 *out)
98{
99 *(__le32 *)out = cpu_to_le32(__crc32_le(*crcp, data, len));
100 return 0;
101}
102
103static int crc32_finup(struct shash_desc *desc, const u8 *data,
104 unsigned int len, u8 *out)
105{
106 return __crc32_finup(shash_desc_ctx(desc), data, len, out);
107}
108
109static int crc32_final(struct shash_desc *desc, u8 *out)
110{
111 u32 *crcp = shash_desc_ctx(desc);
112
113 *(__le32 *)out = cpu_to_le32p(crcp);
114 return 0;
115}
116
117static int crc32_digest(struct shash_desc *desc, const u8 *data,
118 unsigned int len, u8 *out)
119{
120 return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len,
121 out);
122}
123static struct shash_alg alg = {
124 .setkey = crc32_setkey,
125 .init = crc32_init,
126 .update = crc32_update,
127 .final = crc32_final,
128 .finup = crc32_finup,
129 .digest = crc32_digest,
130 .descsize = sizeof(u32),
131 .digestsize = CHKSUM_DIGEST_SIZE,
132 .base = {
133 .cra_name = "crc32",
134 .cra_driver_name = "crc32-table",
135 .cra_priority = 100,
136 .cra_blocksize = CHKSUM_BLOCK_SIZE,
137 .cra_ctxsize = sizeof(u32),
138 .cra_module = THIS_MODULE,
139 .cra_init = crc32_cra_init,
140 }
141};
142
143static int __init crc32_mod_init(void)
144{
145 return crypto_register_shash(&alg);
146}
147
148static void __exit crc32_mod_fini(void)
149{
150 crypto_unregister_shash(&alg);
151}
152
153module_init(crc32_mod_init);
154module_exit(crc32_mod_fini);
155
156MODULE_AUTHOR("Alexander Boyko <alexander_boyko@xyratex.com>");
157MODULE_DESCRIPTION("CRC32 calculations wrapper for lib/crc32");
158MODULE_LICENSE("GPL");
159MODULE_ALIAS_CRYPTO("crc32");
160