Whamcloud - gitweb
LU-8056 libcfs: Support for linux 4.2 kernels
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-crypto-crc32c-pclmul.c
1 /* GPL HEADER START
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
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.
8  *
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).
14  *
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
17  *
18  * Please  visit http://www.xyratex.com/contact if you need additional
19  * information or have any questions.
20  *
21  * GPL HEADER END
22  */
23
24 /*
25  * Wrappers for kernel crypto shash api to pclmulqdq crc32c imlementation.
26  *
27  * Author:     James Simmons <jsimmons@infradead.org>
28  */
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>
35 #else
36 #include <asm/i387.h>
37 #endif
38 #include <libcfs/libcfs.h>
39
40 #define CHKSUM_BLOCK_SIZE       1
41 #define CHKSUM_DIGEST_SIZE      4
42
43 asmlinkage unsigned int crc_pcl(const u8 *buffer, int len,
44                                 unsigned int crc_init);
45
46 static int crc32c_pclmul_cra_init(struct crypto_tfm *tfm)
47 {
48         u32 *key = crypto_tfm_ctx(tfm);
49
50         *key = ~0;
51         return 0;
52 }
53
54 /*
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
57  * the seed.
58  */
59 static int crc32c_pclmul_setkey(struct crypto_shash *hash, const u8 *key,
60                         unsigned int keylen)
61 {
62         u32 *mctx = crypto_shash_ctx(hash);
63
64         if (keylen != sizeof(u32)) {
65                 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
66                 return -EINVAL;
67         }
68         *mctx = le32_to_cpup((__le32 *)key);
69         return 0;
70 }
71
72 static int crc32c_pclmul_init(struct shash_desc *desc)
73 {
74         u32 *mctx = crypto_shash_ctx(desc->tfm);
75         u32 *crcp = shash_desc_ctx(desc);
76
77         *crcp = *mctx;
78         return 0;
79 }
80
81 static int crc32c_pclmul_update(struct shash_desc *desc, const u8 *data,
82                                unsigned int len)
83 {
84         u32 *crcp = shash_desc_ctx(desc);
85
86         kernel_fpu_begin();
87         *crcp = crc_pcl(data, len, *crcp);
88         kernel_fpu_end();
89         return 0;
90 }
91
92 /* No final XOR 0xFFFFFFFF, like crc32_le */
93 static int __crc32c_pclmul_finup(u32 *crcp, const u8 *data, unsigned int len,
94                                 u8 *out)
95 {
96         kernel_fpu_begin();
97         *(__le32 *)out = ~cpu_to_le32(crc_pcl(data, len, *crcp));
98         kernel_fpu_end();
99         return 0;
100 }
101
102 static int crc32c_pclmul_finup(struct shash_desc *desc, const u8 *data,
103                               unsigned int len, u8 *out)
104 {
105         return __crc32c_pclmul_finup(shash_desc_ctx(desc), data, len, out);
106 }
107
108 static int crc32c_pclmul_digest(struct shash_desc *desc, const u8 *data,
109                                unsigned int len, u8 *out)
110 {
111         return __crc32c_pclmul_finup(crypto_shash_ctx(desc->tfm), data, len,
112                                     out);
113 }
114
115 static int crc32c_pclmul_final(struct shash_desc *desc, u8 *out)
116 {
117         u32 *crcp = shash_desc_ctx(desc);
118
119         *(__le32 *)out = ~cpu_to_le32p(crcp);
120         return 0;
121 }
122
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,
132         .base           = {
133                         .cra_name               = "crc32c",
134                         .cra_driver_name        = "crc32c-pclmul",
135                         .cra_priority           = 150,
136                         .cra_blocksize          = CHKSUM_BLOCK_SIZE,
137                         .cra_ctxsize            = sizeof(u32),
138                         .cra_module             = THIS_MODULE,
139                         .cra_init               = crc32c_pclmul_cra_init,
140         }
141 };
142
143 #ifndef X86_FEATURE_XMM4_2
144 #define X86_FEATURE_XMM4_2      (4*32+20)       /* "sse4_2" SSE-4.2 */
145 #endif
146
147 int cfs_crypto_crc32c_pclmul_register(void)
148 {
149         if (!boot_cpu_has(X86_FEATURE_XMM4_2)) {
150                 CDEBUG(D_INFO, "CRC32 instruction is not detected.\n");
151                 return -ENODEV;
152         }
153         return crypto_register_shash(&alg);
154 }
155
156 void cfs_crypto_crc32c_pclmul_unregister(void)
157 {
158         crypto_unregister_shash(&alg);
159 }