Whamcloud - gitweb
9858db42437ec0c2605939c2c7f9c59d480f523d
[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 #include <asm/i387.h>
34 #include <libcfs/libcfs.h>
35
36 #define CHKSUM_BLOCK_SIZE       1
37 #define CHKSUM_DIGEST_SIZE      4
38
39 asmlinkage unsigned int crc_pcl(const u8 *buffer, int len,
40                                 unsigned int crc_init);
41
42 static int crc32c_pclmul_cra_init(struct crypto_tfm *tfm)
43 {
44         u32 *key = crypto_tfm_ctx(tfm);
45
46         *key = ~0;
47         return 0;
48 }
49
50 /*
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
53  * the seed.
54  */
55 static int crc32c_pclmul_setkey(struct crypto_shash *hash, const u8 *key,
56                         unsigned int keylen)
57 {
58         u32 *mctx = crypto_shash_ctx(hash);
59
60         if (keylen != sizeof(u32)) {
61                 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
62                 return -EINVAL;
63         }
64         *mctx = le32_to_cpup((__le32 *)key);
65         return 0;
66 }
67
68 static int crc32c_pclmul_init(struct shash_desc *desc)
69 {
70         u32 *mctx = crypto_shash_ctx(desc->tfm);
71         u32 *crcp = shash_desc_ctx(desc);
72
73         *crcp = *mctx;
74         return 0;
75 }
76
77 static int crc32c_pclmul_update(struct shash_desc *desc, const u8 *data,
78                                unsigned int len)
79 {
80         u32 *crcp = shash_desc_ctx(desc);
81
82         kernel_fpu_begin();
83         *crcp = crc_pcl(data, len, *crcp);
84         kernel_fpu_end();
85         return 0;
86 }
87
88 /* No final XOR 0xFFFFFFFF, like crc32_le */
89 static int __crc32c_pclmul_finup(u32 *crcp, const u8 *data, unsigned int len,
90                                 u8 *out)
91 {
92         kernel_fpu_begin();
93         *(__le32 *)out = ~cpu_to_le32(crc_pcl(data, len, *crcp));
94         kernel_fpu_end();
95         return 0;
96 }
97
98 static int crc32c_pclmul_finup(struct shash_desc *desc, const u8 *data,
99                               unsigned int len, u8 *out)
100 {
101         return __crc32c_pclmul_finup(shash_desc_ctx(desc), data, len, out);
102 }
103
104 static int crc32c_pclmul_digest(struct shash_desc *desc, const u8 *data,
105                                unsigned int len, u8 *out)
106 {
107         return __crc32c_pclmul_finup(crypto_shash_ctx(desc->tfm), data, len,
108                                     out);
109 }
110
111 static int crc32c_pclmul_final(struct shash_desc *desc, u8 *out)
112 {
113         u32 *crcp = shash_desc_ctx(desc);
114
115         *(__le32 *)out = ~cpu_to_le32p(crcp);
116         return 0;
117 }
118
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,
128         .base           = {
129                         .cra_name               = "crc32c",
130                         .cra_driver_name        = "crc32c-pclmul",
131                         .cra_priority           = 150,
132                         .cra_blocksize          = CHKSUM_BLOCK_SIZE,
133                         .cra_ctxsize            = sizeof(u32),
134                         .cra_module             = THIS_MODULE,
135                         .cra_init               = crc32c_pclmul_cra_init,
136         }
137 };
138
139 #ifndef X86_FEATURE_XMM4_2
140 #define X86_FEATURE_XMM4_2      (4*32+20)       /* "sse4_2" SSE-4.2 */
141 #endif
142
143 int cfs_crypto_crc32c_pclmul_register(void)
144 {
145         if (!boot_cpu_has(X86_FEATURE_XMM4_2)) {
146                 CDEBUG(D_INFO, "CRC32 instruction is not detected.\n");
147                 return -ENODEV;
148         }
149         return crypto_register_shash(&alg);
150 }
151
152 void cfs_crypto_crc32c_pclmul_unregister(void)
153 {
154         crypto_unregister_shash(&alg);
155 }