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 init_crc, const u8 *buf, size_t len);
37 
38 struct chksum_desc_ctx {
39 	__u16 crc;
40 };
41 
42 static int chksum_init(struct shash_desc *desc)
43 {
44 	struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
45 
46 	ctx->crc = 0;
47 
48 	return 0;
49 }
50 
51 static int chksum_update(struct shash_desc *desc, const u8 *data,
52 			 unsigned int length)
53 {
54 	struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
55 
56 	if (length >= 16 && irq_fpu_usable()) {
57 		kernel_fpu_begin();
58 		ctx->crc = crc_t10dif_pcl(ctx->crc, data, length);
59 		kernel_fpu_end();
60 	} else
61 		ctx->crc = crc_t10dif_generic(ctx->crc, data, length);
62 	return 0;
63 }
64 
65 static int chksum_final(struct shash_desc *desc, u8 *out)
66 {
67 	struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
68 
69 	*(__u16 *)out = ctx->crc;
70 	return 0;
71 }
72 
73 static int __chksum_finup(__u16 *crcp, const u8 *data, unsigned int len,
74 			u8 *out)
75 {
76 	if (len >= 16 && irq_fpu_usable()) {
77 		kernel_fpu_begin();
78 		*(__u16 *)out = crc_t10dif_pcl(*crcp, data, len);
79 		kernel_fpu_end();
80 	} else
81 		*(__u16 *)out = crc_t10dif_generic(*crcp, data, len);
82 	return 0;
83 }
84 
85 static int chksum_finup(struct shash_desc *desc, const u8 *data,
86 			unsigned int len, u8 *out)
87 {
88 	struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
89 
90 	return __chksum_finup(&ctx->crc, data, len, out);
91 }
92 
93 static int chksum_digest(struct shash_desc *desc, const u8 *data,
94 			 unsigned int length, u8 *out)
95 {
96 	struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
97 
98 	return __chksum_finup(&ctx->crc, data, length, out);
99 }
100 
101 static struct shash_alg alg = {
102 	.digestsize		=	CRC_T10DIF_DIGEST_SIZE,
103 	.init		=	chksum_init,
104 	.update		=	chksum_update,
105 	.final		=	chksum_final,
106 	.finup		=	chksum_finup,
107 	.digest		=	chksum_digest,
108 	.descsize		=	sizeof(struct chksum_desc_ctx),
109 	.base			=	{
110 		.cra_name		=	"crct10dif",
111 		.cra_driver_name	=	"crct10dif-pclmul",
112 		.cra_priority		=	200,
113 		.cra_blocksize		=	CRC_T10DIF_BLOCK_SIZE,
114 		.cra_module		=	THIS_MODULE,
115 	}
116 };
117 
118 static const struct x86_cpu_id crct10dif_cpu_id[] = {
119 	X86_FEATURE_MATCH(X86_FEATURE_PCLMULQDQ),
120 	{}
121 };
122 MODULE_DEVICE_TABLE(x86cpu, crct10dif_cpu_id);
123 
124 static int __init crct10dif_intel_mod_init(void)
125 {
126 	if (!x86_match_cpu(crct10dif_cpu_id))
127 		return -ENODEV;
128 
129 	return crypto_register_shash(&alg);
130 }
131 
132 static void __exit crct10dif_intel_mod_fini(void)
133 {
134 	crypto_unregister_shash(&alg);
135 }
136 
137 module_init(crct10dif_intel_mod_init);
138 module_exit(crct10dif_intel_mod_fini);
139 
140 MODULE_AUTHOR("Tim Chen <tim.c.chen@linux.intel.com>");
141 MODULE_DESCRIPTION("T10 DIF CRC calculation accelerated with PCLMULQDQ.");
142 MODULE_LICENSE("GPL");
143 
144 MODULE_ALIAS_CRYPTO("crct10dif");
145 MODULE_ALIAS_CRYPTO("crct10dif-pclmul");
146