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 * Wrappers for kernel crypto shash api to pclmulqdq crc32c imlementation.
27 * Author: James Simmons <jsimmons@infradead.org>
29 #include <linux/crc32.h>
30 #include <crypto/internal/hash.h>
31 #include <linux/crc32.h>
32 #include <asm/cpufeature.h>
34 #include <libcfs/libcfs.h>
36 #define CHKSUM_BLOCK_SIZE 1
37 #define CHKSUM_DIGEST_SIZE 4
39 asmlinkage unsigned int crc_pcl(const u8 *buffer, int len,
40 unsigned int crc_init);
42 static int crc32c_pclmul_cra_init(struct crypto_tfm *tfm)
44 u32 *key = crypto_tfm_ctx(tfm);
51 * Setting the seed allows arbitrary accumulators and flexible XOR policy
52 * If your algorithm starts with ~0, then XOR with ~0 before you set
55 static int crc32c_pclmul_setkey(struct crypto_shash *hash, const u8 *key,
58 u32 *mctx = crypto_shash_ctx(hash);
60 if (keylen != sizeof(u32)) {
61 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
64 *mctx = le32_to_cpup((__le32 *)key);
68 static int crc32c_pclmul_init(struct shash_desc *desc)
70 u32 *mctx = crypto_shash_ctx(desc->tfm);
71 u32 *crcp = shash_desc_ctx(desc);
77 static int crc32c_pclmul_update(struct shash_desc *desc, const u8 *data,
80 u32 *crcp = shash_desc_ctx(desc);
83 *crcp = crc_pcl(data, len, *crcp);
88 /* No final XOR 0xFFFFFFFF, like crc32_le */
89 static int __crc32c_pclmul_finup(u32 *crcp, const u8 *data, unsigned int len,
93 *(__le32 *)out = ~cpu_to_le32(crc_pcl(data, len, *crcp));
98 static int crc32c_pclmul_finup(struct shash_desc *desc, const u8 *data,
99 unsigned int len, u8 *out)
101 return __crc32c_pclmul_finup(shash_desc_ctx(desc), data, len, out);
104 static int crc32c_pclmul_digest(struct shash_desc *desc, const u8 *data,
105 unsigned int len, u8 *out)
107 return __crc32c_pclmul_finup(crypto_shash_ctx(desc->tfm), data, len,
111 static int crc32c_pclmul_final(struct shash_desc *desc, u8 *out)
113 u32 *crcp = shash_desc_ctx(desc);
115 *(__le32 *)out = ~cpu_to_le32p(crcp);
119 static struct shash_alg alg = {
120 .setkey = crc32c_pclmul_setkey,
121 .init = crc32c_pclmul_init,
122 .update = crc32c_pclmul_update,
123 .final = crc32c_pclmul_final,
124 .finup = crc32c_pclmul_finup,
125 .digest = crc32c_pclmul_digest,
126 .descsize = sizeof(u32),
127 .digestsize = CHKSUM_DIGEST_SIZE,
129 .cra_name = "crc32c",
130 .cra_driver_name = "crc32c-pclmul",
132 .cra_blocksize = CHKSUM_BLOCK_SIZE,
133 .cra_ctxsize = sizeof(u32),
134 .cra_module = THIS_MODULE,
135 .cra_init = crc32c_pclmul_cra_init,
139 #ifndef X86_FEATURE_XMM4_2
140 #define X86_FEATURE_XMM4_2 (4*32+20) /* "sse4_2" SSE-4.2 */
143 int cfs_crypto_crc32c_pclmul_register(void)
145 if (!boot_cpu_has(X86_FEATURE_XMM4_2)) {
146 CDEBUG(D_INFO, "CRC32 instruction is not detected.\n");
149 return crypto_register_shash(&alg);
152 void cfs_crypto_crc32c_pclmul_unregister(void)
154 crypto_unregister_shash(&alg);