Whamcloud - gitweb
LU-8767 llite: Improve proc file text in lproc_llite.c
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-crypto-crc32pclmul.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  * Copyright 2012 Xyratex Technology Limited
26  *
27  * Wrappers for kernel crypto shash api to pclmulqdq crc32 imlementation.
28  *
29  * Author:     Alexander Boyko <Alexander_Boyko@xyratex.com>
30  */
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>
37 #else
38 #include <asm/i387.h>
39 #endif
40 #include <libcfs/libcfs.h>
41
42 #define CHKSUM_BLOCK_SIZE       1
43 #define CHKSUM_DIGEST_SIZE      4
44
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)
49
50 u32 crc32_pclmul_le_16(unsigned char const *buffer, size_t len, u32 crc32);
51
52 static u32 __attribute__((pure))
53         crc32_pclmul_le(u32 crc, unsigned char const *p, size_t len)
54 {
55         unsigned int iquotient;
56         unsigned int iremainder;
57         unsigned int prealign;
58
59         if (len < PCLMUL_MIN_LEN + SCALE_F_MASK)
60                 return crc32_le(crc, p, len);
61
62         if ((long)p & SCALE_F_MASK) {
63                 /* align p to 16 byte */
64                 prealign = SCALE_F - ((long)p & SCALE_F_MASK);
65
66                 crc = crc32_le(crc, p, prealign);
67                 len -= prealign;
68                 p = (unsigned char *)(((unsigned long)p + SCALE_F_MASK) &
69                                      ~SCALE_F_MASK);
70         }
71         iquotient = len & (~SCALE_F_MASK);
72         iremainder = len & SCALE_F_MASK;
73
74         kernel_fpu_begin();
75         crc = crc32_pclmul_le_16(p, iquotient, crc);
76         kernel_fpu_end();
77
78         if (iremainder)
79                 crc = crc32_le(crc, p + iquotient, iremainder);
80
81         return crc;
82 }
83
84 static int crc32_pclmul_cra_init(struct crypto_tfm *tfm)
85 {
86         u32 *key = crypto_tfm_ctx(tfm);
87
88         *key = 0;
89
90         return 0;
91 }
92
93 /*
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
96  * the seed.
97  */
98 static int crc32_pclmul_setkey(struct crypto_shash *hash, const u8 *key,
99                         unsigned int keylen)
100 {
101         u32 *mctx = crypto_shash_ctx(hash);
102
103         if (keylen != sizeof(u32)) {
104                 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
105                 return -EINVAL;
106         }
107         *mctx = le32_to_cpup((__le32 *)key);
108         return 0;
109 }
110
111 static int crc32_pclmul_init(struct shash_desc *desc)
112 {
113         u32 *mctx = crypto_shash_ctx(desc->tfm);
114         u32 *crcp = shash_desc_ctx(desc);
115
116         *crcp = *mctx;
117
118         return 0;
119 }
120
121 static int crc32_pclmul_update(struct shash_desc *desc, const u8 *data,
122                                unsigned int len)
123 {
124         u32 *crcp = shash_desc_ctx(desc);
125
126         *crcp = crc32_pclmul_le(*crcp, data, len);
127         return 0;
128 }
129
130 /* No final XOR 0xFFFFFFFF, like crc32_le */
131 static int __crc32_pclmul_finup(u32 *crcp, const u8 *data, unsigned int len,
132                                 u8 *out)
133 {
134         *(__le32 *)out = cpu_to_le32(crc32_pclmul_le(*crcp, data, len));
135         return 0;
136 }
137
138 static int crc32_pclmul_finup(struct shash_desc *desc, const u8 *data,
139                               unsigned int len, u8 *out)
140 {
141         return __crc32_pclmul_finup(shash_desc_ctx(desc), data, len, out);
142 }
143
144 static int crc32_pclmul_final(struct shash_desc *desc, u8 *out)
145 {
146         u32 *crcp = shash_desc_ctx(desc);
147
148         *(__le32 *)out = cpu_to_le32p(crcp);
149         return 0;
150 }
151
152 static int crc32_pclmul_digest(struct shash_desc *desc, const u8 *data,
153                                unsigned int len, u8 *out)
154 {
155         return __crc32_pclmul_finup(crypto_shash_ctx(desc->tfm), data, len,
156                                     out);
157 }
158
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,
168         .base           = {
169                         .cra_name               = "crc32",
170                         .cra_driver_name        = "crc32-pclmul",
171                         .cra_priority           = 200,
172                         .cra_blocksize          = CHKSUM_BLOCK_SIZE,
173                         .cra_ctxsize            = sizeof(u32),
174                         .cra_module             = THIS_MODULE,
175                         .cra_init               = crc32_pclmul_cra_init,
176         }
177 };
178
179 #ifndef X86_FEATURE_PCLMULQDQ
180 #define X86_FEATURE_PCLMULQDQ   (4*32+1)        /* PCLMULQDQ instruction */
181 #endif
182
183 int cfs_crypto_crc32_pclmul_register(void)
184 {
185         if (!boot_cpu_has(X86_FEATURE_PCLMULQDQ)) {
186                 CDEBUG(D_INFO, "PCLMULQDQ-NI instructions are not "
187                        "detected.\n");
188                 return -ENODEV;
189         }
190         return crypto_register_shash(&alg);
191 }
192
193 void cfs_crypto_crc32_pclmul_unregister(void)
194 {
195         crypto_unregister_shash(&alg);
196 }