This source file includes following definitions.
- ccp_aes_xts_complete
- ccp_aes_xts_setkey
- ccp_aes_xts_crypt
- ccp_aes_xts_encrypt
- ccp_aes_xts_decrypt
- ccp_aes_xts_cra_init
- ccp_aes_xts_cra_exit
- ccp_register_aes_xts_alg
- ccp_register_aes_xts_algs
1
2
3
4
5
6
7
8
9
10
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/delay.h>
14 #include <linux/scatterlist.h>
15 #include <crypto/aes.h>
16 #include <crypto/xts.h>
17 #include <crypto/internal/skcipher.h>
18 #include <crypto/scatterwalk.h>
19
20 #include "ccp-crypto.h"
21
22 struct ccp_aes_xts_def {
23 const char *name;
24 const char *drv_name;
25 };
26
27 static struct ccp_aes_xts_def aes_xts_algs[] = {
28 {
29 .name = "xts(aes)",
30 .drv_name = "xts-aes-ccp",
31 },
32 };
33
34 struct ccp_unit_size_map {
35 unsigned int size;
36 u32 value;
37 };
38
39 static struct ccp_unit_size_map xts_unit_sizes[] = {
40 {
41 .size = 16,
42 .value = CCP_XTS_AES_UNIT_SIZE_16,
43 },
44 {
45 .size = 512,
46 .value = CCP_XTS_AES_UNIT_SIZE_512,
47 },
48 {
49 .size = 1024,
50 .value = CCP_XTS_AES_UNIT_SIZE_1024,
51 },
52 {
53 .size = 2048,
54 .value = CCP_XTS_AES_UNIT_SIZE_2048,
55 },
56 {
57 .size = 4096,
58 .value = CCP_XTS_AES_UNIT_SIZE_4096,
59 },
60 };
61
62 static int ccp_aes_xts_complete(struct crypto_async_request *async_req, int ret)
63 {
64 struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
65 struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
66
67 if (ret)
68 return ret;
69
70 memcpy(req->info, rctx->iv, AES_BLOCK_SIZE);
71
72 return 0;
73 }
74
75 static int ccp_aes_xts_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
76 unsigned int key_len)
77 {
78 struct crypto_tfm *xfm = crypto_ablkcipher_tfm(tfm);
79 struct ccp_ctx *ctx = crypto_tfm_ctx(xfm);
80 unsigned int ccpversion = ccp_version();
81 int ret;
82
83 ret = xts_check_key(xfm, key, key_len);
84 if (ret)
85 return ret;
86
87
88
89
90 switch (key_len) {
91 case AES_KEYSIZE_128 * 2:
92 memcpy(ctx->u.aes.key, key, key_len);
93 break;
94 case AES_KEYSIZE_256 * 2:
95 if (ccpversion > CCP_VERSION(3, 0))
96 memcpy(ctx->u.aes.key, key, key_len);
97 break;
98 }
99 ctx->u.aes.key_len = key_len / 2;
100 sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
101
102 return crypto_sync_skcipher_setkey(ctx->u.aes.tfm_skcipher, key, key_len);
103 }
104
105 static int ccp_aes_xts_crypt(struct ablkcipher_request *req,
106 unsigned int encrypt)
107 {
108 struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
109 struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
110 unsigned int ccpversion = ccp_version();
111 unsigned int fallback = 0;
112 unsigned int unit;
113 u32 unit_size;
114 int ret;
115
116 if (!ctx->u.aes.key_len)
117 return -EINVAL;
118
119 if (!req->info)
120 return -EINVAL;
121
122
123
124
125
126
127
128 unit_size = CCP_XTS_AES_UNIT_SIZE__LAST;
129 for (unit = 0; unit < ARRAY_SIZE(xts_unit_sizes); unit++) {
130 if (req->nbytes == xts_unit_sizes[unit].size) {
131 unit_size = unit;
132 break;
133 }
134 }
135
136
137
138
139 if (unit_size == CCP_XTS_AES_UNIT_SIZE__LAST)
140 fallback = 1;
141 if ((ccpversion < CCP_VERSION(5, 0)) &&
142 (ctx->u.aes.key_len != AES_KEYSIZE_128))
143 fallback = 1;
144 if ((ctx->u.aes.key_len != AES_KEYSIZE_128) &&
145 (ctx->u.aes.key_len != AES_KEYSIZE_256))
146 fallback = 1;
147 if (fallback) {
148 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq,
149 ctx->u.aes.tfm_skcipher);
150
151
152
153
154 skcipher_request_set_sync_tfm(subreq, ctx->u.aes.tfm_skcipher);
155 skcipher_request_set_callback(subreq, req->base.flags,
156 NULL, NULL);
157 skcipher_request_set_crypt(subreq, req->src, req->dst,
158 req->nbytes, req->info);
159 ret = encrypt ? crypto_skcipher_encrypt(subreq) :
160 crypto_skcipher_decrypt(subreq);
161 skcipher_request_zero(subreq);
162 return ret;
163 }
164
165 memcpy(rctx->iv, req->info, AES_BLOCK_SIZE);
166 sg_init_one(&rctx->iv_sg, rctx->iv, AES_BLOCK_SIZE);
167
168 memset(&rctx->cmd, 0, sizeof(rctx->cmd));
169 INIT_LIST_HEAD(&rctx->cmd.entry);
170 rctx->cmd.engine = CCP_ENGINE_XTS_AES_128;
171 rctx->cmd.u.xts.type = CCP_AES_TYPE_128;
172 rctx->cmd.u.xts.action = (encrypt) ? CCP_AES_ACTION_ENCRYPT
173 : CCP_AES_ACTION_DECRYPT;
174 rctx->cmd.u.xts.unit_size = unit_size;
175 rctx->cmd.u.xts.key = &ctx->u.aes.key_sg;
176 rctx->cmd.u.xts.key_len = ctx->u.aes.key_len;
177 rctx->cmd.u.xts.iv = &rctx->iv_sg;
178 rctx->cmd.u.xts.iv_len = AES_BLOCK_SIZE;
179 rctx->cmd.u.xts.src = req->src;
180 rctx->cmd.u.xts.src_len = req->nbytes;
181 rctx->cmd.u.xts.dst = req->dst;
182
183 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
184
185 return ret;
186 }
187
188 static int ccp_aes_xts_encrypt(struct ablkcipher_request *req)
189 {
190 return ccp_aes_xts_crypt(req, 1);
191 }
192
193 static int ccp_aes_xts_decrypt(struct ablkcipher_request *req)
194 {
195 return ccp_aes_xts_crypt(req, 0);
196 }
197
198 static int ccp_aes_xts_cra_init(struct crypto_tfm *tfm)
199 {
200 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
201 struct crypto_sync_skcipher *fallback_tfm;
202
203 ctx->complete = ccp_aes_xts_complete;
204 ctx->u.aes.key_len = 0;
205
206 fallback_tfm = crypto_alloc_sync_skcipher("xts(aes)", 0,
207 CRYPTO_ALG_ASYNC |
208 CRYPTO_ALG_NEED_FALLBACK);
209 if (IS_ERR(fallback_tfm)) {
210 pr_warn("could not load fallback driver xts(aes)\n");
211 return PTR_ERR(fallback_tfm);
212 }
213 ctx->u.aes.tfm_skcipher = fallback_tfm;
214
215 tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_aes_req_ctx);
216
217 return 0;
218 }
219
220 static void ccp_aes_xts_cra_exit(struct crypto_tfm *tfm)
221 {
222 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
223
224 crypto_free_sync_skcipher(ctx->u.aes.tfm_skcipher);
225 }
226
227 static int ccp_register_aes_xts_alg(struct list_head *head,
228 const struct ccp_aes_xts_def *def)
229 {
230 struct ccp_crypto_ablkcipher_alg *ccp_alg;
231 struct crypto_alg *alg;
232 int ret;
233
234 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
235 if (!ccp_alg)
236 return -ENOMEM;
237
238 INIT_LIST_HEAD(&ccp_alg->entry);
239
240 alg = &ccp_alg->alg;
241
242 snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
243 snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
244 def->drv_name);
245 alg->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
246 CRYPTO_ALG_KERN_DRIVER_ONLY |
247 CRYPTO_ALG_NEED_FALLBACK;
248 alg->cra_blocksize = AES_BLOCK_SIZE;
249 alg->cra_ctxsize = sizeof(struct ccp_ctx);
250 alg->cra_priority = CCP_CRA_PRIORITY;
251 alg->cra_type = &crypto_ablkcipher_type;
252 alg->cra_ablkcipher.setkey = ccp_aes_xts_setkey;
253 alg->cra_ablkcipher.encrypt = ccp_aes_xts_encrypt;
254 alg->cra_ablkcipher.decrypt = ccp_aes_xts_decrypt;
255 alg->cra_ablkcipher.min_keysize = AES_MIN_KEY_SIZE * 2;
256 alg->cra_ablkcipher.max_keysize = AES_MAX_KEY_SIZE * 2;
257 alg->cra_ablkcipher.ivsize = AES_BLOCK_SIZE;
258 alg->cra_init = ccp_aes_xts_cra_init;
259 alg->cra_exit = ccp_aes_xts_cra_exit;
260 alg->cra_module = THIS_MODULE;
261
262 ret = crypto_register_alg(alg);
263 if (ret) {
264 pr_err("%s ablkcipher algorithm registration error (%d)\n",
265 alg->cra_name, ret);
266 kfree(ccp_alg);
267 return ret;
268 }
269
270 list_add(&ccp_alg->entry, head);
271
272 return 0;
273 }
274
275 int ccp_register_aes_xts_algs(struct list_head *head)
276 {
277 int i, ret;
278
279 for (i = 0; i < ARRAY_SIZE(aes_xts_algs); i++) {
280 ret = ccp_register_aes_xts_alg(head, &aes_xts_algs[i]);
281 if (ret)
282 return ret;
283 }
284
285 return 0;
286 }