• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Cryptographic API.
3  *
4  * T10 Data Integrity Field CRC16 Crypto Transform using PCLMULQDQ Instructions
5  *
6  * Copyright (C) 2013 Intel Corporation
7  * Author: Tim Chen <tim.c.chen@linux.intel.com>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the Free
11  * Software Foundation; either version 2 of the License, or (at your option)
12  * any later version.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  */
24 
25 #include <linux/types.h>
26 #include <linux/module.h>
27 #include <linux/crc-t10dif.h>
28 #include <crypto/internal/hash.h>
29 #include <linux/init.h>
30 #include <linux/string.h>
31 #include <linux/kernel.h>
32 #include <asm/fpu/api.h>
33 #include <asm/cpufeatures.h>
34 #include <asm/cpu_device_id.h>
35 
36 asmlinkage __u16 crc_t10dif_pcl(__u16 crc, const unsigned char *buf,
37 				size_t len);
38 
39 struct chksum_desc_ctx {
40 	__u16 crc;
41 };
42 
43 /*
44  * Steps through buffer one byte at at time, calculates reflected
45  * crc using table.
46  */
47 
chksum_init(struct shash_desc * desc)48 static int chksum_init(struct shash_desc *desc)
49 {
50 	struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
51 
52 	ctx->crc = 0;
53 
54 	return 0;
55 }
56 
chksum_update(struct shash_desc * desc,const u8 * data,unsigned int length)57 static int chksum_update(struct shash_desc *desc, const u8 *data,
58 			 unsigned int length)
59 {
60 	struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
61 
62 	if (irq_fpu_usable()) {
63 		kernel_fpu_begin();
64 		ctx->crc = crc_t10dif_pcl(ctx->crc, data, length);
65 		kernel_fpu_end();
66 	} else
67 		ctx->crc = crc_t10dif_generic(ctx->crc, data, length);
68 	return 0;
69 }
70 
chksum_final(struct shash_desc * desc,u8 * out)71 static int chksum_final(struct shash_desc *desc, u8 *out)
72 {
73 	struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
74 
75 	*(__u16 *)out = ctx->crc;
76 	return 0;
77 }
78 
__chksum_finup(__u16 crc,const u8 * data,unsigned int len,u8 * out)79 static int __chksum_finup(__u16 crc, const u8 *data, unsigned int len, u8 *out)
80 {
81 	if (irq_fpu_usable()) {
82 		kernel_fpu_begin();
83 		*(__u16 *)out = crc_t10dif_pcl(crc, data, len);
84 		kernel_fpu_end();
85 	} else
86 		*(__u16 *)out = crc_t10dif_generic(crc, data, len);
87 	return 0;
88 }
89 
chksum_finup(struct shash_desc * desc,const u8 * data,unsigned int len,u8 * out)90 static int chksum_finup(struct shash_desc *desc, const u8 *data,
91 			unsigned int len, u8 *out)
92 {
93 	struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
94 
95 	return __chksum_finup(ctx->crc, data, len, out);
96 }
97 
chksum_digest(struct shash_desc * desc,const u8 * data,unsigned int length,u8 * out)98 static int chksum_digest(struct shash_desc *desc, const u8 *data,
99 			 unsigned int length, u8 *out)
100 {
101 	return __chksum_finup(0, data, length, out);
102 }
103 
104 static struct shash_alg alg = {
105 	.digestsize		=	CRC_T10DIF_DIGEST_SIZE,
106 	.init		=	chksum_init,
107 	.update		=	chksum_update,
108 	.final		=	chksum_final,
109 	.finup		=	chksum_finup,
110 	.digest		=	chksum_digest,
111 	.descsize		=	sizeof(struct chksum_desc_ctx),
112 	.base			=	{
113 		.cra_name		=	"crct10dif",
114 		.cra_driver_name	=	"crct10dif-pclmul",
115 		.cra_priority		=	200,
116 		.cra_blocksize		=	CRC_T10DIF_BLOCK_SIZE,
117 		.cra_module		=	THIS_MODULE,
118 	}
119 };
120 
121 static const struct x86_cpu_id crct10dif_cpu_id[] = {
122 	X86_FEATURE_MATCH(X86_FEATURE_PCLMULQDQ),
123 	{}
124 };
125 MODULE_DEVICE_TABLE(x86cpu, crct10dif_cpu_id);
126 
crct10dif_intel_mod_init(void)127 static int __init crct10dif_intel_mod_init(void)
128 {
129 	if (!x86_match_cpu(crct10dif_cpu_id))
130 		return -ENODEV;
131 
132 	return crypto_register_shash(&alg);
133 }
134 
crct10dif_intel_mod_fini(void)135 static void __exit crct10dif_intel_mod_fini(void)
136 {
137 	crypto_unregister_shash(&alg);
138 }
139 
140 module_init(crct10dif_intel_mod_init);
141 module_exit(crct10dif_intel_mod_fini);
142 
143 MODULE_AUTHOR("Tim Chen <tim.c.chen@linux.intel.com>");
144 MODULE_DESCRIPTION("T10 DIF CRC calculation accelerated with PCLMULQDQ.");
145 MODULE_LICENSE("GPL");
146 
147 MODULE_ALIAS_CRYPTO("crct10dif");
148 MODULE_ALIAS_CRYPTO("crct10dif-pclmul");
149