~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/x86/crypto/crc32-pclmul_glue.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 implementation.
 28  */
 29 #include <linux/init.h>
 30 #include <linux/module.h>
 31 #include <linux/string.h>
 32 #include <linux/kernel.h>
 33 #include <linux/crc32.h>
 34 #include <crypto/internal/hash.h>
 35 #include <crypto/internal/simd.h>
 36 
 37 #include <asm/cpufeatures.h>
 38 #include <asm/cpu_device_id.h>
 39 #include <asm/simd.h>
 40 
 41 #define CHKSUM_BLOCK_SIZE       1
 42 #define CHKSUM_DIGEST_SIZE      4
 43 
 44 #define PCLMUL_MIN_LEN          64L     /* minimum size of buffer
 45                                          * for crc32_pclmul_le_16 */
 46 #define SCALE_F                 16L     /* size of xmm register */
 47 #define SCALE_F_MASK            (SCALE_F - 1)
 48 
 49 u32 crc32_pclmul_le_16(unsigned char const *buffer, size_t len, u32 crc32);
 50 
 51 static u32 __attribute__((pure))
 52         crc32_pclmul_le(u32 crc, unsigned char const *p, size_t len)
 53 {
 54         unsigned int iquotient;
 55         unsigned int iremainder;
 56         unsigned int prealign;
 57 
 58         if (len < PCLMUL_MIN_LEN + SCALE_F_MASK || !crypto_simd_usable())
 59                 return crc32_le(crc, p, len);
 60 
 61         if ((long)p & SCALE_F_MASK) {
 62                 /* align p to 16 byte */
 63                 prealign = SCALE_F - ((long)p & SCALE_F_MASK);
 64 
 65                 crc = crc32_le(crc, p, prealign);
 66                 len -= prealign;
 67                 p = (unsigned char *)(((unsigned long)p + SCALE_F_MASK) &
 68                                      ~SCALE_F_MASK);
 69         }
 70         iquotient = len & (~SCALE_F_MASK);
 71         iremainder = len & SCALE_F_MASK;
 72 
 73         kernel_fpu_begin();
 74         crc = crc32_pclmul_le_16(p, iquotient, crc);
 75         kernel_fpu_end();
 76 
 77         if (iremainder)
 78                 crc = crc32_le(crc, p + iquotient, iremainder);
 79 
 80         return crc;
 81 }
 82 
 83 static int crc32_pclmul_cra_init(struct crypto_tfm *tfm)
 84 {
 85         u32 *key = crypto_tfm_ctx(tfm);
 86 
 87         *key = 0;
 88 
 89         return 0;
 90 }
 91 
 92 static int crc32_pclmul_setkey(struct crypto_shash *hash, const u8 *key,
 93                         unsigned int keylen)
 94 {
 95         u32 *mctx = crypto_shash_ctx(hash);
 96 
 97         if (keylen != sizeof(u32))
 98                 return -EINVAL;
 99         *mctx = le32_to_cpup((__le32 *)key);
100         return 0;
101 }
102 
103 static int crc32_pclmul_init(struct shash_desc *desc)
104 {
105         u32 *mctx = crypto_shash_ctx(desc->tfm);
106         u32 *crcp = shash_desc_ctx(desc);
107 
108         *crcp = *mctx;
109 
110         return 0;
111 }
112 
113 static int crc32_pclmul_update(struct shash_desc *desc, const u8 *data,
114                                unsigned int len)
115 {
116         u32 *crcp = shash_desc_ctx(desc);
117 
118         *crcp = crc32_pclmul_le(*crcp, data, len);
119         return 0;
120 }
121 
122 /* No final XOR 0xFFFFFFFF, like crc32_le */
123 static int __crc32_pclmul_finup(u32 *crcp, const u8 *data, unsigned int len,
124                                 u8 *out)
125 {
126         *(__le32 *)out = cpu_to_le32(crc32_pclmul_le(*crcp, data, len));
127         return 0;
128 }
129 
130 static int crc32_pclmul_finup(struct shash_desc *desc, const u8 *data,
131                               unsigned int len, u8 *out)
132 {
133         return __crc32_pclmul_finup(shash_desc_ctx(desc), data, len, out);
134 }
135 
136 static int crc32_pclmul_final(struct shash_desc *desc, u8 *out)
137 {
138         u32 *crcp = shash_desc_ctx(desc);
139 
140         *(__le32 *)out = cpu_to_le32p(crcp);
141         return 0;
142 }
143 
144 static int crc32_pclmul_digest(struct shash_desc *desc, const u8 *data,
145                                unsigned int len, u8 *out)
146 {
147         return __crc32_pclmul_finup(crypto_shash_ctx(desc->tfm), data, len,
148                                     out);
149 }
150 
151 static struct shash_alg alg = {
152         .setkey         = crc32_pclmul_setkey,
153         .init           = crc32_pclmul_init,
154         .update         = crc32_pclmul_update,
155         .final          = crc32_pclmul_final,
156         .finup          = crc32_pclmul_finup,
157         .digest         = crc32_pclmul_digest,
158         .descsize       = sizeof(u32),
159         .digestsize     = CHKSUM_DIGEST_SIZE,
160         .base           = {
161                         .cra_name               = "crc32",
162                         .cra_driver_name        = "crc32-pclmul",
163                         .cra_priority           = 200,
164                         .cra_flags              = CRYPTO_ALG_OPTIONAL_KEY,
165                         .cra_blocksize          = CHKSUM_BLOCK_SIZE,
166                         .cra_ctxsize            = sizeof(u32),
167                         .cra_module             = THIS_MODULE,
168                         .cra_init               = crc32_pclmul_cra_init,
169         }
170 };
171 
172 static const struct x86_cpu_id crc32pclmul_cpu_id[] = {
173         X86_MATCH_FEATURE(X86_FEATURE_PCLMULQDQ, NULL),
174         {}
175 };
176 MODULE_DEVICE_TABLE(x86cpu, crc32pclmul_cpu_id);
177 
178 
179 static int __init crc32_pclmul_mod_init(void)
180 {
181 
182         if (!x86_match_cpu(crc32pclmul_cpu_id)) {
183                 pr_info("PCLMULQDQ-NI instructions are not detected.\n");
184                 return -ENODEV;
185         }
186         return crypto_register_shash(&alg);
187 }
188 
189 static void __exit crc32_pclmul_mod_fini(void)
190 {
191         crypto_unregister_shash(&alg);
192 }
193 
194 module_init(crc32_pclmul_mod_init);
195 module_exit(crc32_pclmul_mod_fini);
196 
197 MODULE_AUTHOR("Alexander Boyko <alexander_boyko@xyratex.com>");
198 MODULE_DESCRIPTION("CRC32 algorithm (IEEE 802.3) accelerated with PCLMULQDQ");
199 MODULE_LICENSE("GPL");
200 
201 MODULE_ALIAS_CRYPTO("crc32");
202 MODULE_ALIAS_CRYPTO("crc32-pclmul");
203 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php