1 /*
2 * Cryptographic API.
3 *
4 * T10 Data Integrity Field CRC16 Crypto Transform
5 *
6 * Copyright (c) 2007 Oracle Corporation. All rights reserved.
7 * Written by Martin K. Petersen <martin.petersen@oracle.com>
8 * Copyright (C) 2013 Intel Corporation
9 * Author: Tim Chen <tim.c.chen@linux.intel.com>
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 */
26
27 #include <linux/module.h>
28 #include <linux/crc-t10dif.h>
29 #include <crypto/internal/hash.h>
30 #include <linux/init.h>
31 #include <linux/kernel.h>
32
33 struct chksum_desc_ctx {
34 __u16 crc;
35 };
36
37 /*
38 * Steps through buffer one byte at a time, calculates reflected
39 * crc using table.
40 */
41
chksum_init(struct shash_desc * desc)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
chksum_update(struct shash_desc * desc,const u8 * data,unsigned int length)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 ctx->crc = crc_t10dif_generic(ctx->crc, data, length);
57 return 0;
58 }
59
chksum_update_arch(struct shash_desc * desc,const u8 * data,unsigned int length)60 static int chksum_update_arch(struct shash_desc *desc, const u8 *data,
61 unsigned int length)
62 {
63 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
64
65 ctx->crc = crc_t10dif_update(ctx->crc, data, length);
66 return 0;
67 }
68
chksum_final(struct shash_desc * desc,u8 * out)69 static int chksum_final(struct shash_desc *desc, u8 *out)
70 {
71 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
72
73 *(__u16 *)out = ctx->crc;
74 return 0;
75 }
76
__chksum_finup(__u16 crc,const u8 * data,unsigned int len,u8 * out)77 static int __chksum_finup(__u16 crc, const u8 *data, unsigned int len, u8 *out)
78 {
79 *(__u16 *)out = crc_t10dif_generic(crc, data, len);
80 return 0;
81 }
82
__chksum_finup_arch(__u16 crc,const u8 * data,unsigned int len,u8 * out)83 static int __chksum_finup_arch(__u16 crc, const u8 *data, unsigned int len,
84 u8 *out)
85 {
86 *(__u16 *)out = crc_t10dif_update(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_finup_arch(struct shash_desc * desc,const u8 * data,unsigned int len,u8 * out)98 static int chksum_finup_arch(struct shash_desc *desc, const u8 *data,
99 unsigned int len, u8 *out)
100 {
101 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
102
103 return __chksum_finup_arch(ctx->crc, data, len, out);
104 }
105
chksum_digest(struct shash_desc * desc,const u8 * data,unsigned int length,u8 * out)106 static int chksum_digest(struct shash_desc *desc, const u8 *data,
107 unsigned int length, u8 *out)
108 {
109 return __chksum_finup(0, data, length, out);
110 }
111
chksum_digest_arch(struct shash_desc * desc,const u8 * data,unsigned int length,u8 * out)112 static int chksum_digest_arch(struct shash_desc *desc, const u8 *data,
113 unsigned int length, u8 *out)
114 {
115 return __chksum_finup_arch(0, data, length, out);
116 }
117
118 static struct shash_alg algs[] = {{
119 .digestsize = CRC_T10DIF_DIGEST_SIZE,
120 .init = chksum_init,
121 .update = chksum_update,
122 .final = chksum_final,
123 .finup = chksum_finup,
124 .digest = chksum_digest,
125 .descsize = sizeof(struct chksum_desc_ctx),
126 .base.cra_name = "crct10dif",
127 .base.cra_driver_name = "crct10dif-generic",
128 .base.cra_priority = 100,
129 .base.cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
130 .base.cra_module = THIS_MODULE,
131 }, {
132 .digestsize = CRC_T10DIF_DIGEST_SIZE,
133 .init = chksum_init,
134 .update = chksum_update_arch,
135 .final = chksum_final,
136 .finup = chksum_finup_arch,
137 .digest = chksum_digest_arch,
138 .descsize = sizeof(struct chksum_desc_ctx),
139 .base.cra_name = "crct10dif",
140 .base.cra_driver_name = "crct10dif-" __stringify(ARCH),
141 .base.cra_priority = 150,
142 .base.cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
143 .base.cra_module = THIS_MODULE,
144 }};
145
146 static int num_algs;
147
crct10dif_mod_init(void)148 static int __init crct10dif_mod_init(void)
149 {
150 /* register the arch flavor only if it differs from the generic one */
151 num_algs = 1 + crc_t10dif_is_optimized();
152
153 return crypto_register_shashes(algs, num_algs);
154 }
155
crct10dif_mod_fini(void)156 static void __exit crct10dif_mod_fini(void)
157 {
158 crypto_unregister_shashes(algs, num_algs);
159 }
160
161 subsys_initcall(crct10dif_mod_init);
162 module_exit(crct10dif_mod_fini);
163
164 MODULE_AUTHOR("Tim Chen <tim.c.chen@linux.intel.com>");
165 MODULE_DESCRIPTION("T10 DIF CRC calculation.");
166 MODULE_LICENSE("GPL");
167 MODULE_ALIAS_CRYPTO("crct10dif");
168 MODULE_ALIAS_CRYPTO("crct10dif-generic");
169