Commit | Line | Data |
---|---|---|
78c37d19 AB |
1 | /* GPL HEADER START |
2 | * | |
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 only, | |
7 | * as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, but | |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * General Public License version 2 for more details (a copy is included | |
13 | * in the LICENSE file that accompanied this code). | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * version 2 along with this program; If not, see http://www.gnu.org/licenses | |
17 | * | |
18 | * Please visit http://www.xyratex.com/contact if you need additional | |
19 | * information or have any questions. | |
20 | * | |
21 | * GPL HEADER END | |
22 | */ | |
23 | ||
24 | /* | |
25 | * Copyright 2012 Xyratex Technology Limited | |
26 | */ | |
27 | ||
28 | /* | |
29 | * This is crypto api shash wrappers to crc32_le. | |
30 | */ | |
31 | ||
fffe7d92 | 32 | #include <asm/unaligned.h> |
78c37d19 AB |
33 | #include <linux/crc32.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 | ||
40 | #define CHKSUM_BLOCK_SIZE 1 | |
41 | #define CHKSUM_DIGEST_SIZE 4 | |
42 | ||
43 | static u32 __crc32_le(u32 crc, unsigned char const *p, size_t len) | |
44 | { | |
45 | return crc32_le(crc, p, len); | |
46 | } | |
47 | ||
48 | /** No default init with ~0 */ | |
49 | static int crc32_cra_init(struct crypto_tfm *tfm) | |
50 | { | |
51 | u32 *key = crypto_tfm_ctx(tfm); | |
52 | ||
53 | *key = 0; | |
54 | ||
55 | return 0; | |
56 | } | |
57 | ||
58 | ||
59 | /* | |
60 | * Setting the seed allows arbitrary accumulators and flexible XOR policy | |
61 | * If your algorithm starts with ~0, then XOR with ~0 before you set | |
62 | * the seed. | |
63 | */ | |
64 | static int crc32_setkey(struct crypto_shash *hash, const u8 *key, | |
65 | unsigned int keylen) | |
66 | { | |
67 | u32 *mctx = crypto_shash_ctx(hash); | |
68 | ||
69 | if (keylen != sizeof(u32)) { | |
70 | crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN); | |
71 | return -EINVAL; | |
72 | } | |
fffe7d92 | 73 | *mctx = get_unaligned_le32(key); |
78c37d19 AB |
74 | return 0; |
75 | } | |
76 | ||
77 | static int crc32_init(struct shash_desc *desc) | |
78 | { | |
79 | u32 *mctx = crypto_shash_ctx(desc->tfm); | |
80 | u32 *crcp = shash_desc_ctx(desc); | |
81 | ||
82 | *crcp = *mctx; | |
83 | ||
84 | return 0; | |
85 | } | |
86 | ||
87 | static int crc32_update(struct shash_desc *desc, const u8 *data, | |
88 | unsigned int len) | |
89 | { | |
90 | u32 *crcp = shash_desc_ctx(desc); | |
91 | ||
92 | *crcp = __crc32_le(*crcp, data, len); | |
93 | return 0; | |
94 | } | |
95 | ||
96 | /* No final XOR 0xFFFFFFFF, like crc32_le */ | |
97 | static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len, | |
98 | u8 *out) | |
99 | { | |
fffe7d92 | 100 | put_unaligned_le32(__crc32_le(*crcp, data, len), out); |
78c37d19 AB |
101 | return 0; |
102 | } | |
103 | ||
104 | static int crc32_finup(struct shash_desc *desc, const u8 *data, | |
105 | unsigned int len, u8 *out) | |
106 | { | |
107 | return __crc32_finup(shash_desc_ctx(desc), data, len, out); | |
108 | } | |
109 | ||
110 | static int crc32_final(struct shash_desc *desc, u8 *out) | |
111 | { | |
112 | u32 *crcp = shash_desc_ctx(desc); | |
113 | ||
fffe7d92 | 114 | put_unaligned_le32(*crcp, out); |
78c37d19 AB |
115 | return 0; |
116 | } | |
117 | ||
118 | static int crc32_digest(struct shash_desc *desc, const u8 *data, | |
119 | unsigned int len, u8 *out) | |
120 | { | |
121 | return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len, | |
122 | out); | |
123 | } | |
124 | static struct shash_alg alg = { | |
125 | .setkey = crc32_setkey, | |
126 | .init = crc32_init, | |
127 | .update = crc32_update, | |
128 | .final = crc32_final, | |
129 | .finup = crc32_finup, | |
130 | .digest = crc32_digest, | |
131 | .descsize = sizeof(u32), | |
132 | .digestsize = CHKSUM_DIGEST_SIZE, | |
133 | .base = { | |
134 | .cra_name = "crc32", | |
a7c58ac0 | 135 | .cra_driver_name = "crc32-generic", |
78c37d19 | 136 | .cra_priority = 100, |
a208fa8f | 137 | .cra_flags = CRYPTO_ALG_OPTIONAL_KEY, |
78c37d19 AB |
138 | .cra_blocksize = CHKSUM_BLOCK_SIZE, |
139 | .cra_ctxsize = sizeof(u32), | |
140 | .cra_module = THIS_MODULE, | |
141 | .cra_init = crc32_cra_init, | |
142 | } | |
143 | }; | |
144 | ||
145 | static int __init crc32_mod_init(void) | |
146 | { | |
147 | return crypto_register_shash(&alg); | |
148 | } | |
149 | ||
150 | static void __exit crc32_mod_fini(void) | |
151 | { | |
152 | crypto_unregister_shash(&alg); | |
153 | } | |
154 | ||
155 | module_init(crc32_mod_init); | |
156 | module_exit(crc32_mod_fini); | |
157 | ||
158 | MODULE_AUTHOR("Alexander Boyko <alexander_boyko@xyratex.com>"); | |
159 | MODULE_DESCRIPTION("CRC32 calculations wrapper for lib/crc32"); | |
160 | MODULE_LICENSE("GPL"); | |
5d26a105 | 161 | MODULE_ALIAS_CRYPTO("crc32"); |
a7c58ac0 | 162 | MODULE_ALIAS_CRYPTO("crc32-generic"); |