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 #include <libcfs/linux/linux-crypto.h>
35 #include <crypto/internal/hash.h>
37 #define CHKSUM_BLOCK_SIZE 1
38 #define CHKSUM_DIGEST_SIZE 4
40 static u32 __crc32_le(u32 crc, unsigned char const *p, size_t len)
42 return crc32_le(crc, p, len);
45 /** No default init with ~0 */
46 static int crc32_cra_init(struct crypto_tfm *tfm)
48 u32 *key = crypto_tfm_ctx(tfm);
57 * Setting the seed allows arbitrary accumulators and flexible XOR policy
58 * If your algorithm starts with ~0, then XOR with ~0 before you set
61 static int crc32_setkey(struct crypto_shash *hash, const u8 *key,
64 u32 *mctx = crypto_shash_ctx(hash);
66 if (keylen != sizeof(u32)) {
67 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
70 *mctx = le32_to_cpup((__le32 *)key);
74 static int crc32_init(struct shash_desc *desc)
76 u32 *mctx = crypto_shash_ctx(desc->tfm);
77 u32 *crcp = shash_desc_ctx(desc);
84 static int crc32_update(struct shash_desc *desc, const u8 *data,
87 u32 *crcp = shash_desc_ctx(desc);
89 *crcp = __crc32_le(*crcp, data, len);
92 /* No final XOR 0xFFFFFFFF, like crc32_le */
93 static int __crc32_finup(u32 *crcp, const u8 *data, unsigned int len,
96 *(__le32 *)out = cpu_to_le32(__crc32_le(*crcp, data, len));
100 static int crc32_finup(struct shash_desc *desc, const u8 *data,
101 unsigned int len, u8 *out)
103 return __crc32_finup(shash_desc_ctx(desc), data, len, out);
106 static int crc32_final(struct shash_desc *desc, u8 *out)
108 u32 *crcp = shash_desc_ctx(desc);
110 *(__le32 *)out = cpu_to_le32p(crcp);
114 static int crc32_digest(struct shash_desc *desc, const u8 *data,
115 unsigned int len, u8 *out)
117 return __crc32_finup(crypto_shash_ctx(desc->tfm), data, len,
120 static struct shash_alg alg = {
121 .setkey = crc32_setkey,
123 .update = crc32_update,
124 .final = crc32_final,
125 .finup = crc32_finup,
126 .digest = crc32_digest,
127 .descsize = sizeof(u32),
128 .digestsize = CHKSUM_DIGEST_SIZE,
131 .cra_driver_name = "crc32-table",
133 .cra_blocksize = CHKSUM_BLOCK_SIZE,
134 .cra_ctxsize = sizeof(u32),
135 .cra_module = THIS_MODULE,
136 .cra_init = crc32_cra_init,
140 int cfs_crypto_crc32_register(void)
142 return crypto_register_shash(&alg);
145 void cfs_crypto_crc32_unregister(void)
147 crypto_unregister_shash(&alg);