Merge branch 'pm-opp'
[deliverable/linux.git] / drivers / crypto / qce / ablkcipher.c
1 /*
2 * Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14 #include <linux/device.h>
15 #include <linux/interrupt.h>
16 #include <linux/types.h>
17 #include <crypto/aes.h>
18 #include <crypto/algapi.h>
19 #include <crypto/des.h>
20
21 #include "cipher.h"
22
23 static LIST_HEAD(ablkcipher_algs);
24
25 static void qce_ablkcipher_done(void *data)
26 {
27 struct crypto_async_request *async_req = data;
28 struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
29 struct qce_cipher_reqctx *rctx = ablkcipher_request_ctx(req);
30 struct qce_alg_template *tmpl = to_cipher_tmpl(async_req->tfm);
31 struct qce_device *qce = tmpl->qce;
32 enum dma_data_direction dir_src, dir_dst;
33 u32 status;
34 int error;
35 bool diff_dst;
36
37 diff_dst = (req->src != req->dst) ? true : false;
38 dir_src = diff_dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL;
39 dir_dst = diff_dst ? DMA_FROM_DEVICE : DMA_BIDIRECTIONAL;
40
41 error = qce_dma_terminate_all(&qce->dma);
42 if (error)
43 dev_dbg(qce->dev, "ablkcipher dma termination error (%d)\n",
44 error);
45
46 if (diff_dst)
47 dma_unmap_sg(qce->dev, rctx->src_sg, rctx->src_nents, dir_src);
48 dma_unmap_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
49
50 sg_free_table(&rctx->dst_tbl);
51
52 error = qce_check_status(qce, &status);
53 if (error < 0)
54 dev_dbg(qce->dev, "ablkcipher operation error (%x)\n", status);
55
56 qce->async_req_done(tmpl->qce, error);
57 }
58
59 static int
60 qce_ablkcipher_async_req_handle(struct crypto_async_request *async_req)
61 {
62 struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
63 struct qce_cipher_reqctx *rctx = ablkcipher_request_ctx(req);
64 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
65 struct qce_alg_template *tmpl = to_cipher_tmpl(async_req->tfm);
66 struct qce_device *qce = tmpl->qce;
67 enum dma_data_direction dir_src, dir_dst;
68 struct scatterlist *sg;
69 bool diff_dst;
70 gfp_t gfp;
71 int ret;
72
73 rctx->iv = req->info;
74 rctx->ivsize = crypto_ablkcipher_ivsize(ablkcipher);
75 rctx->cryptlen = req->nbytes;
76
77 diff_dst = (req->src != req->dst) ? true : false;
78 dir_src = diff_dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL;
79 dir_dst = diff_dst ? DMA_FROM_DEVICE : DMA_BIDIRECTIONAL;
80
81 rctx->src_nents = sg_nents_for_len(req->src, req->nbytes);
82 if (diff_dst)
83 rctx->dst_nents = sg_nents_for_len(req->dst, req->nbytes);
84 else
85 rctx->dst_nents = rctx->src_nents;
86
87 rctx->dst_nents += 1;
88
89 gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
90 GFP_KERNEL : GFP_ATOMIC;
91
92 ret = sg_alloc_table(&rctx->dst_tbl, rctx->dst_nents, gfp);
93 if (ret)
94 return ret;
95
96 sg_init_one(&rctx->result_sg, qce->dma.result_buf, QCE_RESULT_BUF_SZ);
97
98 sg = qce_sgtable_add(&rctx->dst_tbl, req->dst);
99 if (IS_ERR(sg)) {
100 ret = PTR_ERR(sg);
101 goto error_free;
102 }
103
104 sg = qce_sgtable_add(&rctx->dst_tbl, &rctx->result_sg);
105 if (IS_ERR(sg)) {
106 ret = PTR_ERR(sg);
107 goto error_free;
108 }
109
110 sg_mark_end(sg);
111 rctx->dst_sg = rctx->dst_tbl.sgl;
112
113 ret = dma_map_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
114 if (ret < 0)
115 goto error_free;
116
117 if (diff_dst) {
118 ret = dma_map_sg(qce->dev, req->src, rctx->src_nents, dir_src);
119 if (ret < 0)
120 goto error_unmap_dst;
121 rctx->src_sg = req->src;
122 } else {
123 rctx->src_sg = rctx->dst_sg;
124 }
125
126 ret = qce_dma_prep_sgs(&qce->dma, rctx->src_sg, rctx->src_nents,
127 rctx->dst_sg, rctx->dst_nents,
128 qce_ablkcipher_done, async_req);
129 if (ret)
130 goto error_unmap_src;
131
132 qce_dma_issue_pending(&qce->dma);
133
134 ret = qce_start(async_req, tmpl->crypto_alg_type, req->nbytes, 0);
135 if (ret)
136 goto error_terminate;
137
138 return 0;
139
140 error_terminate:
141 qce_dma_terminate_all(&qce->dma);
142 error_unmap_src:
143 if (diff_dst)
144 dma_unmap_sg(qce->dev, req->src, rctx->src_nents, dir_src);
145 error_unmap_dst:
146 dma_unmap_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
147 error_free:
148 sg_free_table(&rctx->dst_tbl);
149 return ret;
150 }
151
152 static int qce_ablkcipher_setkey(struct crypto_ablkcipher *ablk, const u8 *key,
153 unsigned int keylen)
154 {
155 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(ablk);
156 struct qce_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
157 unsigned long flags = to_cipher_tmpl(tfm)->alg_flags;
158 int ret;
159
160 if (!key || !keylen)
161 return -EINVAL;
162
163 if (IS_AES(flags)) {
164 switch (keylen) {
165 case AES_KEYSIZE_128:
166 case AES_KEYSIZE_256:
167 break;
168 default:
169 goto fallback;
170 }
171 } else if (IS_DES(flags)) {
172 u32 tmp[DES_EXPKEY_WORDS];
173
174 ret = des_ekey(tmp, key);
175 if (!ret && crypto_ablkcipher_get_flags(ablk) &
176 CRYPTO_TFM_REQ_WEAK_KEY)
177 goto weakkey;
178 }
179
180 ctx->enc_keylen = keylen;
181 memcpy(ctx->enc_key, key, keylen);
182 return 0;
183 fallback:
184 ret = crypto_ablkcipher_setkey(ctx->fallback, key, keylen);
185 if (!ret)
186 ctx->enc_keylen = keylen;
187 return ret;
188 weakkey:
189 crypto_ablkcipher_set_flags(ablk, CRYPTO_TFM_RES_WEAK_KEY);
190 return -EINVAL;
191 }
192
193 static int qce_ablkcipher_crypt(struct ablkcipher_request *req, int encrypt)
194 {
195 struct crypto_tfm *tfm =
196 crypto_ablkcipher_tfm(crypto_ablkcipher_reqtfm(req));
197 struct qce_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
198 struct qce_cipher_reqctx *rctx = ablkcipher_request_ctx(req);
199 struct qce_alg_template *tmpl = to_cipher_tmpl(tfm);
200 int ret;
201
202 rctx->flags = tmpl->alg_flags;
203 rctx->flags |= encrypt ? QCE_ENCRYPT : QCE_DECRYPT;
204
205 if (IS_AES(rctx->flags) && ctx->enc_keylen != AES_KEYSIZE_128 &&
206 ctx->enc_keylen != AES_KEYSIZE_256) {
207 ablkcipher_request_set_tfm(req, ctx->fallback);
208 ret = encrypt ? crypto_ablkcipher_encrypt(req) :
209 crypto_ablkcipher_decrypt(req);
210 ablkcipher_request_set_tfm(req, __crypto_ablkcipher_cast(tfm));
211 return ret;
212 }
213
214 return tmpl->qce->async_req_enqueue(tmpl->qce, &req->base);
215 }
216
217 static int qce_ablkcipher_encrypt(struct ablkcipher_request *req)
218 {
219 return qce_ablkcipher_crypt(req, 1);
220 }
221
222 static int qce_ablkcipher_decrypt(struct ablkcipher_request *req)
223 {
224 return qce_ablkcipher_crypt(req, 0);
225 }
226
227 static int qce_ablkcipher_init(struct crypto_tfm *tfm)
228 {
229 struct qce_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
230
231 memset(ctx, 0, sizeof(*ctx));
232 tfm->crt_ablkcipher.reqsize = sizeof(struct qce_cipher_reqctx);
233
234 ctx->fallback = crypto_alloc_ablkcipher(crypto_tfm_alg_name(tfm),
235 CRYPTO_ALG_TYPE_ABLKCIPHER,
236 CRYPTO_ALG_ASYNC |
237 CRYPTO_ALG_NEED_FALLBACK);
238 if (IS_ERR(ctx->fallback))
239 return PTR_ERR(ctx->fallback);
240
241 return 0;
242 }
243
244 static void qce_ablkcipher_exit(struct crypto_tfm *tfm)
245 {
246 struct qce_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
247
248 crypto_free_ablkcipher(ctx->fallback);
249 }
250
251 struct qce_ablkcipher_def {
252 unsigned long flags;
253 const char *name;
254 const char *drv_name;
255 unsigned int blocksize;
256 unsigned int ivsize;
257 unsigned int min_keysize;
258 unsigned int max_keysize;
259 };
260
261 static const struct qce_ablkcipher_def ablkcipher_def[] = {
262 {
263 .flags = QCE_ALG_AES | QCE_MODE_ECB,
264 .name = "ecb(aes)",
265 .drv_name = "ecb-aes-qce",
266 .blocksize = AES_BLOCK_SIZE,
267 .ivsize = AES_BLOCK_SIZE,
268 .min_keysize = AES_MIN_KEY_SIZE,
269 .max_keysize = AES_MAX_KEY_SIZE,
270 },
271 {
272 .flags = QCE_ALG_AES | QCE_MODE_CBC,
273 .name = "cbc(aes)",
274 .drv_name = "cbc-aes-qce",
275 .blocksize = AES_BLOCK_SIZE,
276 .ivsize = AES_BLOCK_SIZE,
277 .min_keysize = AES_MIN_KEY_SIZE,
278 .max_keysize = AES_MAX_KEY_SIZE,
279 },
280 {
281 .flags = QCE_ALG_AES | QCE_MODE_CTR,
282 .name = "ctr(aes)",
283 .drv_name = "ctr-aes-qce",
284 .blocksize = AES_BLOCK_SIZE,
285 .ivsize = AES_BLOCK_SIZE,
286 .min_keysize = AES_MIN_KEY_SIZE,
287 .max_keysize = AES_MAX_KEY_SIZE,
288 },
289 {
290 .flags = QCE_ALG_AES | QCE_MODE_XTS,
291 .name = "xts(aes)",
292 .drv_name = "xts-aes-qce",
293 .blocksize = AES_BLOCK_SIZE,
294 .ivsize = AES_BLOCK_SIZE,
295 .min_keysize = AES_MIN_KEY_SIZE,
296 .max_keysize = AES_MAX_KEY_SIZE,
297 },
298 {
299 .flags = QCE_ALG_DES | QCE_MODE_ECB,
300 .name = "ecb(des)",
301 .drv_name = "ecb-des-qce",
302 .blocksize = DES_BLOCK_SIZE,
303 .ivsize = 0,
304 .min_keysize = DES_KEY_SIZE,
305 .max_keysize = DES_KEY_SIZE,
306 },
307 {
308 .flags = QCE_ALG_DES | QCE_MODE_CBC,
309 .name = "cbc(des)",
310 .drv_name = "cbc-des-qce",
311 .blocksize = DES_BLOCK_SIZE,
312 .ivsize = DES_BLOCK_SIZE,
313 .min_keysize = DES_KEY_SIZE,
314 .max_keysize = DES_KEY_SIZE,
315 },
316 {
317 .flags = QCE_ALG_3DES | QCE_MODE_ECB,
318 .name = "ecb(des3_ede)",
319 .drv_name = "ecb-3des-qce",
320 .blocksize = DES3_EDE_BLOCK_SIZE,
321 .ivsize = 0,
322 .min_keysize = DES3_EDE_KEY_SIZE,
323 .max_keysize = DES3_EDE_KEY_SIZE,
324 },
325 {
326 .flags = QCE_ALG_3DES | QCE_MODE_CBC,
327 .name = "cbc(des3_ede)",
328 .drv_name = "cbc-3des-qce",
329 .blocksize = DES3_EDE_BLOCK_SIZE,
330 .ivsize = DES3_EDE_BLOCK_SIZE,
331 .min_keysize = DES3_EDE_KEY_SIZE,
332 .max_keysize = DES3_EDE_KEY_SIZE,
333 },
334 };
335
336 static int qce_ablkcipher_register_one(const struct qce_ablkcipher_def *def,
337 struct qce_device *qce)
338 {
339 struct qce_alg_template *tmpl;
340 struct crypto_alg *alg;
341 int ret;
342
343 tmpl = kzalloc(sizeof(*tmpl), GFP_KERNEL);
344 if (!tmpl)
345 return -ENOMEM;
346
347 alg = &tmpl->alg.crypto;
348
349 snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
350 snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
351 def->drv_name);
352
353 alg->cra_blocksize = def->blocksize;
354 alg->cra_ablkcipher.ivsize = def->ivsize;
355 alg->cra_ablkcipher.min_keysize = def->min_keysize;
356 alg->cra_ablkcipher.max_keysize = def->max_keysize;
357 alg->cra_ablkcipher.setkey = qce_ablkcipher_setkey;
358 alg->cra_ablkcipher.encrypt = qce_ablkcipher_encrypt;
359 alg->cra_ablkcipher.decrypt = qce_ablkcipher_decrypt;
360
361 alg->cra_priority = 300;
362 alg->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
363 CRYPTO_ALG_NEED_FALLBACK;
364 alg->cra_ctxsize = sizeof(struct qce_cipher_ctx);
365 alg->cra_alignmask = 0;
366 alg->cra_type = &crypto_ablkcipher_type;
367 alg->cra_module = THIS_MODULE;
368 alg->cra_init = qce_ablkcipher_init;
369 alg->cra_exit = qce_ablkcipher_exit;
370 INIT_LIST_HEAD(&alg->cra_list);
371
372 INIT_LIST_HEAD(&tmpl->entry);
373 tmpl->crypto_alg_type = CRYPTO_ALG_TYPE_ABLKCIPHER;
374 tmpl->alg_flags = def->flags;
375 tmpl->qce = qce;
376
377 ret = crypto_register_alg(alg);
378 if (ret) {
379 kfree(tmpl);
380 dev_err(qce->dev, "%s registration failed\n", alg->cra_name);
381 return ret;
382 }
383
384 list_add_tail(&tmpl->entry, &ablkcipher_algs);
385 dev_dbg(qce->dev, "%s is registered\n", alg->cra_name);
386 return 0;
387 }
388
389 static void qce_ablkcipher_unregister(struct qce_device *qce)
390 {
391 struct qce_alg_template *tmpl, *n;
392
393 list_for_each_entry_safe(tmpl, n, &ablkcipher_algs, entry) {
394 crypto_unregister_alg(&tmpl->alg.crypto);
395 list_del(&tmpl->entry);
396 kfree(tmpl);
397 }
398 }
399
400 static int qce_ablkcipher_register(struct qce_device *qce)
401 {
402 int ret, i;
403
404 for (i = 0; i < ARRAY_SIZE(ablkcipher_def); i++) {
405 ret = qce_ablkcipher_register_one(&ablkcipher_def[i], qce);
406 if (ret)
407 goto err;
408 }
409
410 return 0;
411 err:
412 qce_ablkcipher_unregister(qce);
413 return ret;
414 }
415
416 const struct qce_algo_ops ablkcipher_ops = {
417 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
418 .register_algs = qce_ablkcipher_register,
419 .unregister_algs = qce_ablkcipher_unregister,
420 .async_req_handle = qce_ablkcipher_async_req_handle,
421 };
This page took 0.043886 seconds and 5 git commands to generate.