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>
33 #ifdef HAVE_FPU_API_HEADER
34 #include <asm/fpu/api.h>
38 #include <libcfs/libcfs.h>
40 #define CHKSUM_BLOCK_SIZE 1
41 #define CHKSUM_DIGEST_SIZE 4
43 asmlinkage unsigned int crc_pcl(const u8 *buffer, int len,
44 unsigned int crc_init);
46 static int crc32c_pclmul_cra_init(struct crypto_tfm *tfm)
48 u32 *key = crypto_tfm_ctx(tfm);
55 * Setting the seed allows arbitrary accumulators and flexible XOR policy
56 * If your algorithm starts with ~0, then XOR with ~0 before you set
59 static int crc32c_pclmul_setkey(struct crypto_shash *hash, const u8 *key,
62 u32 *mctx = crypto_shash_ctx(hash);
64 if (keylen != sizeof(u32)) {
65 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
68 *mctx = le32_to_cpup((__le32 *)key);
72 static int crc32c_pclmul_init(struct shash_desc *desc)
74 u32 *mctx = crypto_shash_ctx(desc->tfm);
75 u32 *crcp = shash_desc_ctx(desc);
81 static int crc32c_pclmul_update(struct shash_desc *desc, const u8 *data,
84 u32 *crcp = shash_desc_ctx(desc);
87 *crcp = crc_pcl(data, len, *crcp);
92 /* No final XOR 0xFFFFFFFF, like crc32_le */
93 static int __crc32c_pclmul_finup(u32 *crcp, const u8 *data, unsigned int len,
97 *(__le32 *)out = ~cpu_to_le32(crc_pcl(data, len, *crcp));
102 static int crc32c_pclmul_finup(struct shash_desc *desc, const u8 *data,
103 unsigned int len, u8 *out)
105 return __crc32c_pclmul_finup(shash_desc_ctx(desc), data, len, out);
108 static int crc32c_pclmul_digest(struct shash_desc *desc, const u8 *data,
109 unsigned int len, u8 *out)
111 return __crc32c_pclmul_finup(crypto_shash_ctx(desc->tfm), data, len,
115 static int crc32c_pclmul_final(struct shash_desc *desc, u8 *out)
117 u32 *crcp = shash_desc_ctx(desc);
119 *(__le32 *)out = ~cpu_to_le32p(crcp);
123 static struct shash_alg alg = {
124 .setkey = crc32c_pclmul_setkey,
125 .init = crc32c_pclmul_init,
126 .update = crc32c_pclmul_update,
127 .final = crc32c_pclmul_final,
128 .finup = crc32c_pclmul_finup,
129 .digest = crc32c_pclmul_digest,
130 .descsize = sizeof(u32),
131 .digestsize = CHKSUM_DIGEST_SIZE,
133 .cra_name = "crc32c",
134 .cra_driver_name = "crc32c-pclmul",
136 .cra_blocksize = CHKSUM_BLOCK_SIZE,
137 .cra_ctxsize = sizeof(u32),
138 .cra_module = THIS_MODULE,
139 .cra_init = crc32c_pclmul_cra_init,
143 #ifndef X86_FEATURE_XMM4_2
144 #define X86_FEATURE_XMM4_2 (4*32+20) /* "sse4_2" SSE-4.2 */
147 int cfs_crypto_crc32c_pclmul_register(void)
149 if (!boot_cpu_has(X86_FEATURE_XMM4_2)) {
150 CDEBUG(D_INFO, "CRC32 instruction is not detected.\n");
153 return crypto_register_shash(&alg);
156 void cfs_crypto_crc32c_pclmul_unregister(void)
158 crypto_unregister_shash(&alg);