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>
36 #include <libcfs/libcfs.h>
38 #define CHKSUM_BLOCK_SIZE 1
39 #define CHKSUM_DIGEST_SIZE 4
41 #define PCLMUL_MIN_LEN 64L /* minimum size of buffer
42 * for crc32_pclmul_le_16 */
43 #define SCALE_F 16L /* size of xmm register */
44 #define SCALE_F_MASK (SCALE_F - 1)
46 u32 crc32_pclmul_le_16(unsigned char const *buffer, size_t len, u32 crc32);
48 static u32 __attribute__((pure))
49 crc32_pclmul_le(u32 crc, unsigned char const *p, size_t len)
51 unsigned int iquotient;
52 unsigned int iremainder;
53 unsigned int prealign;
55 if (len < PCLMUL_MIN_LEN + SCALE_F_MASK)
56 return crc32_le(crc, p, len);
58 if ((long)p & SCALE_F_MASK) {
59 /* align p to 16 byte */
60 prealign = SCALE_F - ((long)p & SCALE_F_MASK);
62 crc = crc32_le(crc, p, prealign);
64 p = (unsigned char *)(((unsigned long)p + SCALE_F_MASK) &
67 iquotient = len & (~SCALE_F_MASK);
68 iremainder = len & SCALE_F_MASK;
71 crc = crc32_pclmul_le_16(p, iquotient, crc);
75 crc = crc32_le(crc, p + iquotient, iremainder);
80 static int crc32_pclmul_cra_init(struct crypto_tfm *tfm)
82 u32 *key = crypto_tfm_ctx(tfm);
90 * Setting the seed allows arbitrary accumulators and flexible XOR policy
91 * If your algorithm starts with ~0, then XOR with ~0 before you set
94 static int crc32_pclmul_setkey(struct crypto_shash *hash, const u8 *key,
97 u32 *mctx = crypto_shash_ctx(hash);
99 if (keylen != sizeof(u32)) {
100 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
103 *mctx = le32_to_cpup((__le32 *)key);
107 static int crc32_pclmul_init(struct shash_desc *desc)
109 u32 *mctx = crypto_shash_ctx(desc->tfm);
110 u32 *crcp = shash_desc_ctx(desc);
117 static int crc32_pclmul_update(struct shash_desc *desc, const u8 *data,
120 u32 *crcp = shash_desc_ctx(desc);
122 *crcp = crc32_pclmul_le(*crcp, data, len);
126 /* No final XOR 0xFFFFFFFF, like crc32_le */
127 static int __crc32_pclmul_finup(u32 *crcp, const u8 *data, unsigned int len,
130 *(__le32 *)out = cpu_to_le32(crc32_pclmul_le(*crcp, data, len));
134 static int crc32_pclmul_finup(struct shash_desc *desc, const u8 *data,
135 unsigned int len, u8 *out)
137 return __crc32_pclmul_finup(shash_desc_ctx(desc), data, len, out);
140 static int crc32_pclmul_final(struct shash_desc *desc, u8 *out)
142 u32 *crcp = shash_desc_ctx(desc);
144 *(__le32 *)out = cpu_to_le32p(crcp);
148 static int crc32_pclmul_digest(struct shash_desc *desc, const u8 *data,
149 unsigned int len, u8 *out)
151 return __crc32_pclmul_finup(crypto_shash_ctx(desc->tfm), data, len,
155 static struct shash_alg alg = {
156 .setkey = crc32_pclmul_setkey,
157 .init = crc32_pclmul_init,
158 .update = crc32_pclmul_update,
159 .final = crc32_pclmul_final,
160 .finup = crc32_pclmul_finup,
161 .digest = crc32_pclmul_digest,
162 .descsize = sizeof(u32),
163 .digestsize = CHKSUM_DIGEST_SIZE,
166 .cra_driver_name = "crc32-pclmul",
168 .cra_blocksize = CHKSUM_BLOCK_SIZE,
169 .cra_ctxsize = sizeof(u32),
170 .cra_module = THIS_MODULE,
171 .cra_init = crc32_pclmul_cra_init,
175 #ifndef X86_FEATURE_PCLMULQDQ
176 #define X86_FEATURE_PCLMULQDQ (4*32+1) /* PCLMULQDQ instruction */
179 int cfs_crypto_crc32_pclmul_register(void)
181 if (!boot_cpu_has(X86_FEATURE_PCLMULQDQ)) {
182 CDEBUG(D_INFO, "PCLMULQDQ-NI instructions are not "
186 return crypto_register_shash(&alg);
189 void cfs_crypto_crc32_pclmul_unregister(void)
191 crypto_unregister_shash(&alg);