0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #include <linux/init.h>
0030 #include <linux/module.h>
0031 #include <linux/string.h>
0032 #include <linux/kernel.h>
0033 #include <linux/crc32.h>
0034 #include <crypto/internal/hash.h>
0035 #include <crypto/internal/simd.h>
0036
0037 #include <asm/cpufeatures.h>
0038 #include <asm/cpu_device_id.h>
0039 #include <asm/simd.h>
0040
0041 #define CHKSUM_BLOCK_SIZE 1
0042 #define CHKSUM_DIGEST_SIZE 4
0043
0044 #define PCLMUL_MIN_LEN 64L
0045
0046 #define SCALE_F 16L
0047 #define SCALE_F_MASK (SCALE_F - 1)
0048
0049 u32 crc32_pclmul_le_16(unsigned char const *buffer, size_t len, u32 crc32);
0050
0051 static u32 __attribute__((pure))
0052 crc32_pclmul_le(u32 crc, unsigned char const *p, size_t len)
0053 {
0054 unsigned int iquotient;
0055 unsigned int iremainder;
0056 unsigned int prealign;
0057
0058 if (len < PCLMUL_MIN_LEN + SCALE_F_MASK || !crypto_simd_usable())
0059 return crc32_le(crc, p, len);
0060
0061 if ((long)p & SCALE_F_MASK) {
0062
0063 prealign = SCALE_F - ((long)p & SCALE_F_MASK);
0064
0065 crc = crc32_le(crc, p, prealign);
0066 len -= prealign;
0067 p = (unsigned char *)(((unsigned long)p + SCALE_F_MASK) &
0068 ~SCALE_F_MASK);
0069 }
0070 iquotient = len & (~SCALE_F_MASK);
0071 iremainder = len & SCALE_F_MASK;
0072
0073 kernel_fpu_begin();
0074 crc = crc32_pclmul_le_16(p, iquotient, crc);
0075 kernel_fpu_end();
0076
0077 if (iremainder)
0078 crc = crc32_le(crc, p + iquotient, iremainder);
0079
0080 return crc;
0081 }
0082
0083 static int crc32_pclmul_cra_init(struct crypto_tfm *tfm)
0084 {
0085 u32 *key = crypto_tfm_ctx(tfm);
0086
0087 *key = 0;
0088
0089 return 0;
0090 }
0091
0092 static int crc32_pclmul_setkey(struct crypto_shash *hash, const u8 *key,
0093 unsigned int keylen)
0094 {
0095 u32 *mctx = crypto_shash_ctx(hash);
0096
0097 if (keylen != sizeof(u32))
0098 return -EINVAL;
0099 *mctx = le32_to_cpup((__le32 *)key);
0100 return 0;
0101 }
0102
0103 static int crc32_pclmul_init(struct shash_desc *desc)
0104 {
0105 u32 *mctx = crypto_shash_ctx(desc->tfm);
0106 u32 *crcp = shash_desc_ctx(desc);
0107
0108 *crcp = *mctx;
0109
0110 return 0;
0111 }
0112
0113 static int crc32_pclmul_update(struct shash_desc *desc, const u8 *data,
0114 unsigned int len)
0115 {
0116 u32 *crcp = shash_desc_ctx(desc);
0117
0118 *crcp = crc32_pclmul_le(*crcp, data, len);
0119 return 0;
0120 }
0121
0122
0123 static int __crc32_pclmul_finup(u32 *crcp, const u8 *data, unsigned int len,
0124 u8 *out)
0125 {
0126 *(__le32 *)out = cpu_to_le32(crc32_pclmul_le(*crcp, data, len));
0127 return 0;
0128 }
0129
0130 static int crc32_pclmul_finup(struct shash_desc *desc, const u8 *data,
0131 unsigned int len, u8 *out)
0132 {
0133 return __crc32_pclmul_finup(shash_desc_ctx(desc), data, len, out);
0134 }
0135
0136 static int crc32_pclmul_final(struct shash_desc *desc, u8 *out)
0137 {
0138 u32 *crcp = shash_desc_ctx(desc);
0139
0140 *(__le32 *)out = cpu_to_le32p(crcp);
0141 return 0;
0142 }
0143
0144 static int crc32_pclmul_digest(struct shash_desc *desc, const u8 *data,
0145 unsigned int len, u8 *out)
0146 {
0147 return __crc32_pclmul_finup(crypto_shash_ctx(desc->tfm), data, len,
0148 out);
0149 }
0150
0151 static struct shash_alg alg = {
0152 .setkey = crc32_pclmul_setkey,
0153 .init = crc32_pclmul_init,
0154 .update = crc32_pclmul_update,
0155 .final = crc32_pclmul_final,
0156 .finup = crc32_pclmul_finup,
0157 .digest = crc32_pclmul_digest,
0158 .descsize = sizeof(u32),
0159 .digestsize = CHKSUM_DIGEST_SIZE,
0160 .base = {
0161 .cra_name = "crc32",
0162 .cra_driver_name = "crc32-pclmul",
0163 .cra_priority = 200,
0164 .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
0165 .cra_blocksize = CHKSUM_BLOCK_SIZE,
0166 .cra_ctxsize = sizeof(u32),
0167 .cra_module = THIS_MODULE,
0168 .cra_init = crc32_pclmul_cra_init,
0169 }
0170 };
0171
0172 static const struct x86_cpu_id crc32pclmul_cpu_id[] = {
0173 X86_MATCH_FEATURE(X86_FEATURE_PCLMULQDQ, NULL),
0174 {}
0175 };
0176 MODULE_DEVICE_TABLE(x86cpu, crc32pclmul_cpu_id);
0177
0178
0179 static int __init crc32_pclmul_mod_init(void)
0180 {
0181
0182 if (!x86_match_cpu(crc32pclmul_cpu_id)) {
0183 pr_info("PCLMULQDQ-NI instructions are not detected.\n");
0184 return -ENODEV;
0185 }
0186 return crypto_register_shash(&alg);
0187 }
0188
0189 static void __exit crc32_pclmul_mod_fini(void)
0190 {
0191 crypto_unregister_shash(&alg);
0192 }
0193
0194 module_init(crc32_pclmul_mod_init);
0195 module_exit(crc32_pclmul_mod_fini);
0196
0197 MODULE_AUTHOR("Alexander Boyko <alexander_boyko@xyratex.com>");
0198 MODULE_LICENSE("GPL");
0199
0200 MODULE_ALIAS_CRYPTO("crc32");
0201 MODULE_ALIAS_CRYPTO("crc32-pclmul");