3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
18 * Please visit http://www.xyratex.com/contact if you need additional
19 * information or have any questions.
25 * Copyright 2012 Xyratex Technology Limited
29 * This is crypto api shash wrappers to crc32_le.
32 #include <linux/module.h>
33 #include <linux/crc32.h>
34 #ifdef HAVE_STRUCT_SHASH_ALG
35 #include <crypto/internal/hash.h>
37 #include <linux/crypto.h>
40 #define CHKSUM_BLOCK_SIZE 1
41 #define CHKSUM_DIGEST_SIZE 4
43 static u32 __crc32_le(u32 crc, unsigned char const *p, size_t len)
45 return crc32_le(crc, p, len);
48 /** No default init with ~0 */
49 static int crc32_cra_init(struct crypto_tfm *tfm)
51 u32 *key = crypto_tfm_ctx(tfm);
59 #ifdef HAVE_STRUCT_SHASH_ALG
61 * Setting the seed allows arbitrary accumulators and flexible XOR policy
62 * If your algorithm starts with ~0, then XOR with ~0 before you set
65 static int crc32_setkey(struct crypto_shash *hash, const u8 *key,
68 u32 *mctx = crypto_shash_ctx(hash);
70 if (keylen != sizeof(u32)) {
71 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
74 *mctx = le32_to_cpup((__le32 *)key);
78 static int crc32_init(struct shash_desc *desc)
80 u32 *mctx = crypto_shash_ctx(desc->tfm);
81 u32 *crcp = shash_desc_ctx(desc);
88 static int crc32_update(struct shash_desc *desc, const u8 *data,
91 u32 *crcp = shash_desc_ctx(desc);
93 *crcp = __crc32_le(*crcp, data, len);
96 /* No final XOR 0xFFFFFFFF, like crc32_le */
97 static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len,
100 *(__le32 *)out = cpu_to_le32(__crc32_le(*crcp, data, len));
104 static int crc32_finup(struct shash_desc *desc, const u8 *data,
105 unsigned int len, u8 *out)
107 return __crc32_finup(shash_desc_ctx(desc), data, len, out);
110 static int crc32_final(struct shash_desc *desc, u8 *out)
112 u32 *crcp = shash_desc_ctx(desc);
114 *(__le32 *)out = cpu_to_le32p(crcp);
118 static int crc32_digest(struct shash_desc *desc, const u8 *data,
119 unsigned int len, u8 *out)
121 return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len,
124 static struct shash_alg alg = {
125 .setkey = crc32_setkey,
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,
135 .cra_driver_name = "crc32-table",
137 .cra_blocksize = CHKSUM_BLOCK_SIZE,
138 .cra_ctxsize = sizeof(u32),
139 .cra_module = THIS_MODULE,
140 .cra_init = crc32_cra_init,
143 #else /* HAVE_STRUCT_SHASH_ALG */
144 #ifdef HAVE_DIGEST_SETKEY_FLAGS
145 static int crc32_digest_setkey(struct crypto_tfm *tfm, const u8 *key,
146 unsigned int keylen, unsigned int *flags)
148 static int crc32_digest_setkey(struct crypto_tfm *tfm, const u8 *key,
152 u32 *mctx = crypto_tfm_ctx(tfm);
154 if (keylen != sizeof(u32)) {
155 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
158 *mctx = le32_to_cpup((__le32 *)key);
162 static void crc32_digest_init(struct crypto_tfm *tfm)
164 u32 *mctx = crypto_tfm_ctx(tfm);
169 static void crc32_digest_update(struct crypto_tfm *tfm, const u8 *data,
172 u32 *crcp = crypto_tfm_ctx(tfm);
174 *crcp = __crc32_le(*crcp, data, len);
177 static void crc32_digest_final(struct crypto_tfm *tfm, u8 *out)
179 u32 *crcp = crypto_tfm_ctx(tfm);
181 *(__le32 *)out = cpu_to_le32p(crcp);
184 static struct crypto_alg alg = {
186 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
187 .cra_driver_name = "crc32-table",
189 .cra_blocksize = CHKSUM_BLOCK_SIZE,
190 .cra_ctxsize = sizeof(u32),
191 .cra_module = THIS_MODULE,
192 .cra_init = crc32_cra_init,
193 .cra_list = LIST_HEAD_INIT(alg.cra_list),
196 .dia_digestsize = CHKSUM_DIGEST_SIZE,
197 .dia_setkey = crc32_digest_setkey,
198 .dia_init = crc32_digest_init,
199 .dia_update = crc32_digest_update,
200 .dia_final = crc32_digest_final
204 #endif /* HAVE_STRUCT_SHASH_ALG */
206 int cfs_crypto_crc32_register(void)
208 #ifdef HAVE_STRUCT_SHASH_ALG
209 return crypto_register_shash(&alg);
211 return crypto_register_alg(&alg);
214 EXPORT_SYMBOL(cfs_crypto_crc32_register);
216 void cfs_crypto_crc32_unregister(void)
218 #ifdef HAVE_STRUCT_SHASH_ALG
219 crypto_unregister_shash(&alg);
221 crypto_unregister_alg(&alg);
224 EXPORT_SYMBOL(cfs_crypto_crc32_unregister);