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
27 * Wrappers for kernel crypto shash api to pclmulqdq crc32 imlementation.
29 * Author: Alexander Boyko <Alexander_Boyko@xyratex.com>
31 #include <linux/crc32.h>
32 #include <crypto/internal/hash.h>
33 #include <linux/crc32.h>
34 #include <asm/cpufeature.h>
35 #ifdef HAVE_FPU_API_HEADER
36 #include <asm/fpu/api.h>
40 #include <libcfs/libcfs.h>
42 #define CHKSUM_BLOCK_SIZE 1
43 #define CHKSUM_DIGEST_SIZE 4
45 #define PCLMUL_MIN_LEN 64L /* minimum size of buffer
46 * for crc32_pclmul_le_16 */
47 #define SCALE_F 16L /* size of xmm register */
48 #define SCALE_F_MASK (SCALE_F - 1)
50 u32 crc32_pclmul_le_16(unsigned char const *buffer, size_t len, u32 crc32);
52 static u32 __attribute__((pure))
53 crc32_pclmul_le(u32 crc, unsigned char const *p, size_t len)
55 unsigned int iquotient;
56 unsigned int iremainder;
57 unsigned int prealign;
59 if (len < PCLMUL_MIN_LEN + SCALE_F_MASK)
60 return crc32_le(crc, p, len);
62 if ((long)p & SCALE_F_MASK) {
63 /* align p to 16 byte */
64 prealign = SCALE_F - ((long)p & SCALE_F_MASK);
66 crc = crc32_le(crc, p, prealign);
68 p = (unsigned char *)(((unsigned long)p + SCALE_F_MASK) &
71 iquotient = len & (~SCALE_F_MASK);
72 iremainder = len & SCALE_F_MASK;
75 crc = crc32_pclmul_le_16(p, iquotient, crc);
79 crc = crc32_le(crc, p + iquotient, iremainder);
84 static int crc32_pclmul_cra_init(struct crypto_tfm *tfm)
86 u32 *key = crypto_tfm_ctx(tfm);
94 * Setting the seed allows arbitrary accumulators and flexible XOR policy
95 * If your algorithm starts with ~0, then XOR with ~0 before you set
98 static int crc32_pclmul_setkey(struct crypto_shash *hash, const u8 *key,
101 u32 *mctx = crypto_shash_ctx(hash);
103 if (keylen != sizeof(u32)) {
104 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
107 *mctx = le32_to_cpup((__le32 *)key);
111 static int crc32_pclmul_init(struct shash_desc *desc)
113 u32 *mctx = crypto_shash_ctx(desc->tfm);
114 u32 *crcp = shash_desc_ctx(desc);
121 static int crc32_pclmul_update(struct shash_desc *desc, const u8 *data,
124 u32 *crcp = shash_desc_ctx(desc);
126 *crcp = crc32_pclmul_le(*crcp, data, len);
130 /* No final XOR 0xFFFFFFFF, like crc32_le */
131 static int __crc32_pclmul_finup(u32 *crcp, const u8 *data, unsigned int len,
134 *(__le32 *)out = cpu_to_le32(crc32_pclmul_le(*crcp, data, len));
138 static int crc32_pclmul_finup(struct shash_desc *desc, const u8 *data,
139 unsigned int len, u8 *out)
141 return __crc32_pclmul_finup(shash_desc_ctx(desc), data, len, out);
144 static int crc32_pclmul_final(struct shash_desc *desc, u8 *out)
146 u32 *crcp = shash_desc_ctx(desc);
148 *(__le32 *)out = cpu_to_le32p(crcp);
152 static int crc32_pclmul_digest(struct shash_desc *desc, const u8 *data,
153 unsigned int len, u8 *out)
155 return __crc32_pclmul_finup(crypto_shash_ctx(desc->tfm), data, len,
159 static struct shash_alg alg = {
160 .setkey = crc32_pclmul_setkey,
161 .init = crc32_pclmul_init,
162 .update = crc32_pclmul_update,
163 .final = crc32_pclmul_final,
164 .finup = crc32_pclmul_finup,
165 .digest = crc32_pclmul_digest,
166 .descsize = sizeof(u32),
167 .digestsize = CHKSUM_DIGEST_SIZE,
170 .cra_driver_name = "crc32-pclmul",
172 .cra_blocksize = CHKSUM_BLOCK_SIZE,
173 .cra_ctxsize = sizeof(u32),
174 .cra_module = THIS_MODULE,
175 .cra_init = crc32_pclmul_cra_init,
179 #ifndef X86_FEATURE_PCLMULQDQ
180 #define X86_FEATURE_PCLMULQDQ (4*32+1) /* PCLMULQDQ instruction */
183 int cfs_crypto_crc32_pclmul_register(void)
185 if (!boot_cpu_has(X86_FEATURE_PCLMULQDQ)) {
186 CDEBUG(D_INFO, "PCLMULQDQ-NI instructions are not "
190 return crypto_register_shash(&alg);
193 void cfs_crypto_crc32_pclmul_unregister(void)
195 crypto_unregister_shash(&alg);