xref: /linux/drivers/crypto/marvell/cesa/cipher.c (revision efbc7764)
1655ff1a1SSrujanaChalla // SPDX-License-Identifier: GPL-2.0-only
2655ff1a1SSrujanaChalla /*
3655ff1a1SSrujanaChalla  * Cipher algorithms supported by the CESA: DES, 3DES and AES.
4655ff1a1SSrujanaChalla  *
5655ff1a1SSrujanaChalla  * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
6655ff1a1SSrujanaChalla  * Author: Arnaud Ebalard <arno@natisbad.org>
7655ff1a1SSrujanaChalla  *
8655ff1a1SSrujanaChalla  * This work is based on an initial version written by
9655ff1a1SSrujanaChalla  * Sebastian Andrzej Siewior < sebastian at breakpoint dot cc >
10655ff1a1SSrujanaChalla  */
11655ff1a1SSrujanaChalla 
12655ff1a1SSrujanaChalla #include <crypto/aes.h>
13655ff1a1SSrujanaChalla #include <crypto/internal/des.h>
140c3dc787SHerbert Xu #include <linux/device.h>
150c3dc787SHerbert Xu #include <linux/dma-mapping.h>
16655ff1a1SSrujanaChalla 
17655ff1a1SSrujanaChalla #include "cesa.h"
18655ff1a1SSrujanaChalla 
19655ff1a1SSrujanaChalla struct mv_cesa_des_ctx {
20655ff1a1SSrujanaChalla 	struct mv_cesa_ctx base;
21655ff1a1SSrujanaChalla 	u8 key[DES_KEY_SIZE];
22655ff1a1SSrujanaChalla };
23655ff1a1SSrujanaChalla 
24655ff1a1SSrujanaChalla struct mv_cesa_des3_ctx {
25655ff1a1SSrujanaChalla 	struct mv_cesa_ctx base;
26655ff1a1SSrujanaChalla 	u8 key[DES3_EDE_KEY_SIZE];
27655ff1a1SSrujanaChalla };
28655ff1a1SSrujanaChalla 
29655ff1a1SSrujanaChalla struct mv_cesa_aes_ctx {
30655ff1a1SSrujanaChalla 	struct mv_cesa_ctx base;
31655ff1a1SSrujanaChalla 	struct crypto_aes_ctx aes;
32655ff1a1SSrujanaChalla };
33655ff1a1SSrujanaChalla 
34655ff1a1SSrujanaChalla struct mv_cesa_skcipher_dma_iter {
35655ff1a1SSrujanaChalla 	struct mv_cesa_dma_iter base;
36655ff1a1SSrujanaChalla 	struct mv_cesa_sg_dma_iter src;
37655ff1a1SSrujanaChalla 	struct mv_cesa_sg_dma_iter dst;
38655ff1a1SSrujanaChalla };
39655ff1a1SSrujanaChalla 
40655ff1a1SSrujanaChalla static inline void
mv_cesa_skcipher_req_iter_init(struct mv_cesa_skcipher_dma_iter * iter,struct skcipher_request * req)41655ff1a1SSrujanaChalla mv_cesa_skcipher_req_iter_init(struct mv_cesa_skcipher_dma_iter *iter,
42655ff1a1SSrujanaChalla 			       struct skcipher_request *req)
43655ff1a1SSrujanaChalla {
44655ff1a1SSrujanaChalla 	mv_cesa_req_dma_iter_init(&iter->base, req->cryptlen);
45655ff1a1SSrujanaChalla 	mv_cesa_sg_dma_iter_init(&iter->src, req->src, DMA_TO_DEVICE);
46655ff1a1SSrujanaChalla 	mv_cesa_sg_dma_iter_init(&iter->dst, req->dst, DMA_FROM_DEVICE);
47655ff1a1SSrujanaChalla }
48655ff1a1SSrujanaChalla 
49655ff1a1SSrujanaChalla static inline bool
mv_cesa_skcipher_req_iter_next_op(struct mv_cesa_skcipher_dma_iter * iter)50655ff1a1SSrujanaChalla mv_cesa_skcipher_req_iter_next_op(struct mv_cesa_skcipher_dma_iter *iter)
51655ff1a1SSrujanaChalla {
52655ff1a1SSrujanaChalla 	iter->src.op_offset = 0;
53655ff1a1SSrujanaChalla 	iter->dst.op_offset = 0;
54655ff1a1SSrujanaChalla 
55655ff1a1SSrujanaChalla 	return mv_cesa_req_dma_iter_next_op(&iter->base);
56655ff1a1SSrujanaChalla }
57655ff1a1SSrujanaChalla 
58655ff1a1SSrujanaChalla static inline void
mv_cesa_skcipher_dma_cleanup(struct skcipher_request * req)59655ff1a1SSrujanaChalla mv_cesa_skcipher_dma_cleanup(struct skcipher_request *req)
60655ff1a1SSrujanaChalla {
61655ff1a1SSrujanaChalla 	struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
62655ff1a1SSrujanaChalla 
63655ff1a1SSrujanaChalla 	if (req->dst != req->src) {
64655ff1a1SSrujanaChalla 		dma_unmap_sg(cesa_dev->dev, req->dst, creq->dst_nents,
65655ff1a1SSrujanaChalla 			     DMA_FROM_DEVICE);
66655ff1a1SSrujanaChalla 		dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents,
67655ff1a1SSrujanaChalla 			     DMA_TO_DEVICE);
68655ff1a1SSrujanaChalla 	} else {
69655ff1a1SSrujanaChalla 		dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents,
70655ff1a1SSrujanaChalla 			     DMA_BIDIRECTIONAL);
71655ff1a1SSrujanaChalla 	}
72655ff1a1SSrujanaChalla 	mv_cesa_dma_cleanup(&creq->base);
73655ff1a1SSrujanaChalla }
74655ff1a1SSrujanaChalla 
mv_cesa_skcipher_cleanup(struct skcipher_request * req)75655ff1a1SSrujanaChalla static inline void mv_cesa_skcipher_cleanup(struct skcipher_request *req)
76655ff1a1SSrujanaChalla {
77655ff1a1SSrujanaChalla 	struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
78655ff1a1SSrujanaChalla 
79655ff1a1SSrujanaChalla 	if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
80655ff1a1SSrujanaChalla 		mv_cesa_skcipher_dma_cleanup(req);
81655ff1a1SSrujanaChalla }
82655ff1a1SSrujanaChalla 
mv_cesa_skcipher_std_step(struct skcipher_request * req)83655ff1a1SSrujanaChalla static void mv_cesa_skcipher_std_step(struct skcipher_request *req)
84655ff1a1SSrujanaChalla {
85655ff1a1SSrujanaChalla 	struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
86655ff1a1SSrujanaChalla 	struct mv_cesa_skcipher_std_req *sreq = &creq->std;
87655ff1a1SSrujanaChalla 	struct mv_cesa_engine *engine = creq->base.engine;
88655ff1a1SSrujanaChalla 	size_t  len = min_t(size_t, req->cryptlen - sreq->offset,
89655ff1a1SSrujanaChalla 			    CESA_SA_SRAM_PAYLOAD_SIZE);
90655ff1a1SSrujanaChalla 
91655ff1a1SSrujanaChalla 	mv_cesa_adjust_op(engine, &sreq->op);
92c114cf7fSHerbert Xu 	if (engine->pool)
93c114cf7fSHerbert Xu 		memcpy(engine->sram_pool, &sreq->op, sizeof(sreq->op));
94c114cf7fSHerbert Xu 	else
95655ff1a1SSrujanaChalla 		memcpy_toio(engine->sram, &sreq->op, sizeof(sreq->op));
96655ff1a1SSrujanaChalla 
97c114cf7fSHerbert Xu 	len = mv_cesa_sg_copy_to_sram(engine, req->src, creq->src_nents,
98c114cf7fSHerbert Xu 				      CESA_SA_DATA_SRAM_OFFSET, len,
99c114cf7fSHerbert Xu 				      sreq->offset);
100655ff1a1SSrujanaChalla 
101655ff1a1SSrujanaChalla 	sreq->size = len;
102655ff1a1SSrujanaChalla 	mv_cesa_set_crypt_op_len(&sreq->op, len);
103655ff1a1SSrujanaChalla 
104655ff1a1SSrujanaChalla 	/* FIXME: only update enc_len field */
105655ff1a1SSrujanaChalla 	if (!sreq->skip_ctx) {
106c114cf7fSHerbert Xu 		if (engine->pool)
107c114cf7fSHerbert Xu 			memcpy(engine->sram_pool, &sreq->op, sizeof(sreq->op));
108c114cf7fSHerbert Xu 		else
109655ff1a1SSrujanaChalla 			memcpy_toio(engine->sram, &sreq->op, sizeof(sreq->op));
110655ff1a1SSrujanaChalla 		sreq->skip_ctx = true;
111c114cf7fSHerbert Xu 	} else if (engine->pool)
112c114cf7fSHerbert Xu 		memcpy(engine->sram_pool, &sreq->op, sizeof(sreq->op.desc));
113c114cf7fSHerbert Xu 	else
114655ff1a1SSrujanaChalla 		memcpy_toio(engine->sram, &sreq->op, sizeof(sreq->op.desc));
115655ff1a1SSrujanaChalla 
116655ff1a1SSrujanaChalla 	mv_cesa_set_int_mask(engine, CESA_SA_INT_ACCEL0_DONE);
117655ff1a1SSrujanaChalla 	writel_relaxed(CESA_SA_CFG_PARA_DIS, engine->regs + CESA_SA_CFG);
118655ff1a1SSrujanaChalla 	WARN_ON(readl(engine->regs + CESA_SA_CMD) &
119655ff1a1SSrujanaChalla 		CESA_SA_CMD_EN_CESA_SA_ACCL0);
120655ff1a1SSrujanaChalla 	writel(CESA_SA_CMD_EN_CESA_SA_ACCL0, engine->regs + CESA_SA_CMD);
121655ff1a1SSrujanaChalla }
122655ff1a1SSrujanaChalla 
mv_cesa_skcipher_std_process(struct skcipher_request * req,u32 status)123655ff1a1SSrujanaChalla static int mv_cesa_skcipher_std_process(struct skcipher_request *req,
124655ff1a1SSrujanaChalla 					u32 status)
125655ff1a1SSrujanaChalla {
126655ff1a1SSrujanaChalla 	struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
127655ff1a1SSrujanaChalla 	struct mv_cesa_skcipher_std_req *sreq = &creq->std;
128655ff1a1SSrujanaChalla 	struct mv_cesa_engine *engine = creq->base.engine;
129655ff1a1SSrujanaChalla 	size_t len;
130655ff1a1SSrujanaChalla 
131c114cf7fSHerbert Xu 	len = mv_cesa_sg_copy_from_sram(engine, req->dst, creq->dst_nents,
132c114cf7fSHerbert Xu 					CESA_SA_DATA_SRAM_OFFSET, sreq->size,
133c114cf7fSHerbert Xu 					sreq->offset);
134655ff1a1SSrujanaChalla 
135655ff1a1SSrujanaChalla 	sreq->offset += len;
136655ff1a1SSrujanaChalla 	if (sreq->offset < req->cryptlen)
137655ff1a1SSrujanaChalla 		return -EINPROGRESS;
138655ff1a1SSrujanaChalla 
139655ff1a1SSrujanaChalla 	return 0;
140655ff1a1SSrujanaChalla }
141655ff1a1SSrujanaChalla 
mv_cesa_skcipher_process(struct crypto_async_request * req,u32 status)142655ff1a1SSrujanaChalla static int mv_cesa_skcipher_process(struct crypto_async_request *req,
143655ff1a1SSrujanaChalla 				    u32 status)
144655ff1a1SSrujanaChalla {
145655ff1a1SSrujanaChalla 	struct skcipher_request *skreq = skcipher_request_cast(req);
146655ff1a1SSrujanaChalla 	struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq);
147655ff1a1SSrujanaChalla 	struct mv_cesa_req *basereq = &creq->base;
148655ff1a1SSrujanaChalla 
149655ff1a1SSrujanaChalla 	if (mv_cesa_req_get_type(basereq) == CESA_STD_REQ)
150655ff1a1SSrujanaChalla 		return mv_cesa_skcipher_std_process(skreq, status);
151655ff1a1SSrujanaChalla 
152655ff1a1SSrujanaChalla 	return mv_cesa_dma_process(basereq, status);
153655ff1a1SSrujanaChalla }
154655ff1a1SSrujanaChalla 
mv_cesa_skcipher_step(struct crypto_async_request * req)155655ff1a1SSrujanaChalla static void mv_cesa_skcipher_step(struct crypto_async_request *req)
156655ff1a1SSrujanaChalla {
157655ff1a1SSrujanaChalla 	struct skcipher_request *skreq = skcipher_request_cast(req);
158655ff1a1SSrujanaChalla 	struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq);
159655ff1a1SSrujanaChalla 
160655ff1a1SSrujanaChalla 	if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
161655ff1a1SSrujanaChalla 		mv_cesa_dma_step(&creq->base);
162655ff1a1SSrujanaChalla 	else
163655ff1a1SSrujanaChalla 		mv_cesa_skcipher_std_step(skreq);
164655ff1a1SSrujanaChalla }
165655ff1a1SSrujanaChalla 
166655ff1a1SSrujanaChalla static inline void
mv_cesa_skcipher_dma_prepare(struct skcipher_request * req)167655ff1a1SSrujanaChalla mv_cesa_skcipher_dma_prepare(struct skcipher_request *req)
168655ff1a1SSrujanaChalla {
169655ff1a1SSrujanaChalla 	struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
170655ff1a1SSrujanaChalla 	struct mv_cesa_req *basereq = &creq->base;
171655ff1a1SSrujanaChalla 
172655ff1a1SSrujanaChalla 	mv_cesa_dma_prepare(basereq, basereq->engine);
173655ff1a1SSrujanaChalla }
174655ff1a1SSrujanaChalla 
175655ff1a1SSrujanaChalla static inline void
mv_cesa_skcipher_std_prepare(struct skcipher_request * req)176655ff1a1SSrujanaChalla mv_cesa_skcipher_std_prepare(struct skcipher_request *req)
177655ff1a1SSrujanaChalla {
178655ff1a1SSrujanaChalla 	struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
179655ff1a1SSrujanaChalla 	struct mv_cesa_skcipher_std_req *sreq = &creq->std;
180655ff1a1SSrujanaChalla 
181655ff1a1SSrujanaChalla 	sreq->size = 0;
182655ff1a1SSrujanaChalla 	sreq->offset = 0;
183655ff1a1SSrujanaChalla }
184655ff1a1SSrujanaChalla 
mv_cesa_skcipher_prepare(struct crypto_async_request * req,struct mv_cesa_engine * engine)185655ff1a1SSrujanaChalla static inline void mv_cesa_skcipher_prepare(struct crypto_async_request *req,
186655ff1a1SSrujanaChalla 					    struct mv_cesa_engine *engine)
187655ff1a1SSrujanaChalla {
188655ff1a1SSrujanaChalla 	struct skcipher_request *skreq = skcipher_request_cast(req);
189655ff1a1SSrujanaChalla 	struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq);
190655ff1a1SSrujanaChalla 
191655ff1a1SSrujanaChalla 	creq->base.engine = engine;
192655ff1a1SSrujanaChalla 
193655ff1a1SSrujanaChalla 	if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
194655ff1a1SSrujanaChalla 		mv_cesa_skcipher_dma_prepare(skreq);
195655ff1a1SSrujanaChalla 	else
196655ff1a1SSrujanaChalla 		mv_cesa_skcipher_std_prepare(skreq);
197655ff1a1SSrujanaChalla }
198655ff1a1SSrujanaChalla 
199655ff1a1SSrujanaChalla static inline void
mv_cesa_skcipher_req_cleanup(struct crypto_async_request * req)200655ff1a1SSrujanaChalla mv_cesa_skcipher_req_cleanup(struct crypto_async_request *req)
201655ff1a1SSrujanaChalla {
202655ff1a1SSrujanaChalla 	struct skcipher_request *skreq = skcipher_request_cast(req);
203655ff1a1SSrujanaChalla 
204655ff1a1SSrujanaChalla 	mv_cesa_skcipher_cleanup(skreq);
205655ff1a1SSrujanaChalla }
206655ff1a1SSrujanaChalla 
207655ff1a1SSrujanaChalla static void
mv_cesa_skcipher_complete(struct crypto_async_request * req)208655ff1a1SSrujanaChalla mv_cesa_skcipher_complete(struct crypto_async_request *req)
209655ff1a1SSrujanaChalla {
210655ff1a1SSrujanaChalla 	struct skcipher_request *skreq = skcipher_request_cast(req);
211655ff1a1SSrujanaChalla 	struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq);
212655ff1a1SSrujanaChalla 	struct mv_cesa_engine *engine = creq->base.engine;
213655ff1a1SSrujanaChalla 	unsigned int ivsize;
214655ff1a1SSrujanaChalla 
215655ff1a1SSrujanaChalla 	atomic_sub(skreq->cryptlen, &engine->load);
216655ff1a1SSrujanaChalla 	ivsize = crypto_skcipher_ivsize(crypto_skcipher_reqtfm(skreq));
217655ff1a1SSrujanaChalla 
218655ff1a1SSrujanaChalla 	if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ) {
219655ff1a1SSrujanaChalla 		struct mv_cesa_req *basereq;
220655ff1a1SSrujanaChalla 
221655ff1a1SSrujanaChalla 		basereq = &creq->base;
222655ff1a1SSrujanaChalla 		memcpy(skreq->iv, basereq->chain.last->op->ctx.skcipher.iv,
223655ff1a1SSrujanaChalla 		       ivsize);
224c114cf7fSHerbert Xu 	} else if (engine->pool)
225c114cf7fSHerbert Xu 		memcpy(skreq->iv,
226c114cf7fSHerbert Xu 		       engine->sram_pool + CESA_SA_CRYPT_IV_SRAM_OFFSET,
227c114cf7fSHerbert Xu 		       ivsize);
228c114cf7fSHerbert Xu 	else
229655ff1a1SSrujanaChalla 		memcpy_fromio(skreq->iv,
230655ff1a1SSrujanaChalla 			      engine->sram + CESA_SA_CRYPT_IV_SRAM_OFFSET,
231655ff1a1SSrujanaChalla 			      ivsize);
232655ff1a1SSrujanaChalla }
233655ff1a1SSrujanaChalla 
234655ff1a1SSrujanaChalla static const struct mv_cesa_req_ops mv_cesa_skcipher_req_ops = {
235655ff1a1SSrujanaChalla 	.step = mv_cesa_skcipher_step,
236655ff1a1SSrujanaChalla 	.process = mv_cesa_skcipher_process,
237655ff1a1SSrujanaChalla 	.cleanup = mv_cesa_skcipher_req_cleanup,
238655ff1a1SSrujanaChalla 	.complete = mv_cesa_skcipher_complete,
239655ff1a1SSrujanaChalla };
240655ff1a1SSrujanaChalla 
mv_cesa_skcipher_cra_exit(struct crypto_tfm * tfm)241655ff1a1SSrujanaChalla static void mv_cesa_skcipher_cra_exit(struct crypto_tfm *tfm)
242655ff1a1SSrujanaChalla {
243655ff1a1SSrujanaChalla 	void *ctx = crypto_tfm_ctx(tfm);
244655ff1a1SSrujanaChalla 
245655ff1a1SSrujanaChalla 	memzero_explicit(ctx, tfm->__crt_alg->cra_ctxsize);
246655ff1a1SSrujanaChalla }
247655ff1a1SSrujanaChalla 
mv_cesa_skcipher_cra_init(struct crypto_tfm * tfm)248655ff1a1SSrujanaChalla static int mv_cesa_skcipher_cra_init(struct crypto_tfm *tfm)
249655ff1a1SSrujanaChalla {
250655ff1a1SSrujanaChalla 	struct mv_cesa_ctx *ctx = crypto_tfm_ctx(tfm);
251655ff1a1SSrujanaChalla 
252655ff1a1SSrujanaChalla 	ctx->ops = &mv_cesa_skcipher_req_ops;
253655ff1a1SSrujanaChalla 
254655ff1a1SSrujanaChalla 	crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
255655ff1a1SSrujanaChalla 				    sizeof(struct mv_cesa_skcipher_req));
256655ff1a1SSrujanaChalla 
257655ff1a1SSrujanaChalla 	return 0;
258655ff1a1SSrujanaChalla }
259655ff1a1SSrujanaChalla 
mv_cesa_aes_setkey(struct crypto_skcipher * cipher,const u8 * key,unsigned int len)260655ff1a1SSrujanaChalla static int mv_cesa_aes_setkey(struct crypto_skcipher *cipher, const u8 *key,
261655ff1a1SSrujanaChalla 			      unsigned int len)
262655ff1a1SSrujanaChalla {
263655ff1a1SSrujanaChalla 	struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
264655ff1a1SSrujanaChalla 	struct mv_cesa_aes_ctx *ctx = crypto_tfm_ctx(tfm);
265655ff1a1SSrujanaChalla 	int remaining;
266655ff1a1SSrujanaChalla 	int offset;
267655ff1a1SSrujanaChalla 	int ret;
268655ff1a1SSrujanaChalla 	int i;
269655ff1a1SSrujanaChalla 
270655ff1a1SSrujanaChalla 	ret = aes_expandkey(&ctx->aes, key, len);
271655ff1a1SSrujanaChalla 	if (ret)
272655ff1a1SSrujanaChalla 		return ret;
273655ff1a1SSrujanaChalla 
274655ff1a1SSrujanaChalla 	remaining = (ctx->aes.key_length - 16) / 4;
275655ff1a1SSrujanaChalla 	offset = ctx->aes.key_length + 24 - remaining;
276655ff1a1SSrujanaChalla 	for (i = 0; i < remaining; i++)
277e62291c1SHerbert Xu 		ctx->aes.key_dec[4 + i] = ctx->aes.key_enc[offset + i];
278655ff1a1SSrujanaChalla 
279655ff1a1SSrujanaChalla 	return 0;
280655ff1a1SSrujanaChalla }
281655ff1a1SSrujanaChalla 
mv_cesa_des_setkey(struct crypto_skcipher * cipher,const u8 * key,unsigned int len)282655ff1a1SSrujanaChalla static int mv_cesa_des_setkey(struct crypto_skcipher *cipher, const u8 *key,
283655ff1a1SSrujanaChalla 			      unsigned int len)
284655ff1a1SSrujanaChalla {
285655ff1a1SSrujanaChalla 	struct mv_cesa_des_ctx *ctx = crypto_skcipher_ctx(cipher);
286655ff1a1SSrujanaChalla 	int err;
287655ff1a1SSrujanaChalla 
288655ff1a1SSrujanaChalla 	err = verify_skcipher_des_key(cipher, key);
289655ff1a1SSrujanaChalla 	if (err)
290655ff1a1SSrujanaChalla 		return err;
291655ff1a1SSrujanaChalla 
292655ff1a1SSrujanaChalla 	memcpy(ctx->key, key, DES_KEY_SIZE);
293655ff1a1SSrujanaChalla 
294655ff1a1SSrujanaChalla 	return 0;
295655ff1a1SSrujanaChalla }
296655ff1a1SSrujanaChalla 
mv_cesa_des3_ede_setkey(struct crypto_skcipher * cipher,const u8 * key,unsigned int len)297655ff1a1SSrujanaChalla static int mv_cesa_des3_ede_setkey(struct crypto_skcipher *cipher,
298655ff1a1SSrujanaChalla 				   const u8 *key, unsigned int len)
299655ff1a1SSrujanaChalla {
300*efbc7764SArnd Bergmann 	struct mv_cesa_des3_ctx *ctx = crypto_skcipher_ctx(cipher);
301655ff1a1SSrujanaChalla 	int err;
302655ff1a1SSrujanaChalla 
303655ff1a1SSrujanaChalla 	err = verify_skcipher_des3_key(cipher, key);
304655ff1a1SSrujanaChalla 	if (err)
305655ff1a1SSrujanaChalla 		return err;
306655ff1a1SSrujanaChalla 
307655ff1a1SSrujanaChalla 	memcpy(ctx->key, key, DES3_EDE_KEY_SIZE);
308655ff1a1SSrujanaChalla 
309655ff1a1SSrujanaChalla 	return 0;
310655ff1a1SSrujanaChalla }
311655ff1a1SSrujanaChalla 
mv_cesa_skcipher_dma_req_init(struct skcipher_request * req,const struct mv_cesa_op_ctx * op_templ)312655ff1a1SSrujanaChalla static int mv_cesa_skcipher_dma_req_init(struct skcipher_request *req,
313655ff1a1SSrujanaChalla 					 const struct mv_cesa_op_ctx *op_templ)
314655ff1a1SSrujanaChalla {
315655ff1a1SSrujanaChalla 	struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
316655ff1a1SSrujanaChalla 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
317655ff1a1SSrujanaChalla 		      GFP_KERNEL : GFP_ATOMIC;
318655ff1a1SSrujanaChalla 	struct mv_cesa_req *basereq = &creq->base;
319655ff1a1SSrujanaChalla 	struct mv_cesa_skcipher_dma_iter iter;
320655ff1a1SSrujanaChalla 	bool skip_ctx = false;
321655ff1a1SSrujanaChalla 	int ret;
322655ff1a1SSrujanaChalla 
323655ff1a1SSrujanaChalla 	basereq->chain.first = NULL;
324655ff1a1SSrujanaChalla 	basereq->chain.last = NULL;
325655ff1a1SSrujanaChalla 
326655ff1a1SSrujanaChalla 	if (req->src != req->dst) {
327655ff1a1SSrujanaChalla 		ret = dma_map_sg(cesa_dev->dev, req->src, creq->src_nents,
328655ff1a1SSrujanaChalla 				 DMA_TO_DEVICE);
329655ff1a1SSrujanaChalla 		if (!ret)
330655ff1a1SSrujanaChalla 			return -ENOMEM;
331655ff1a1SSrujanaChalla 
332655ff1a1SSrujanaChalla 		ret = dma_map_sg(cesa_dev->dev, req->dst, creq->dst_nents,
333655ff1a1SSrujanaChalla 				 DMA_FROM_DEVICE);
334655ff1a1SSrujanaChalla 		if (!ret) {
335655ff1a1SSrujanaChalla 			ret = -ENOMEM;
336655ff1a1SSrujanaChalla 			goto err_unmap_src;
337655ff1a1SSrujanaChalla 		}
338655ff1a1SSrujanaChalla 	} else {
339655ff1a1SSrujanaChalla 		ret = dma_map_sg(cesa_dev->dev, req->src, creq->src_nents,
340655ff1a1SSrujanaChalla 				 DMA_BIDIRECTIONAL);
341655ff1a1SSrujanaChalla 		if (!ret)
342655ff1a1SSrujanaChalla 			return -ENOMEM;
343655ff1a1SSrujanaChalla 	}
344655ff1a1SSrujanaChalla 
345655ff1a1SSrujanaChalla 	mv_cesa_tdma_desc_iter_init(&basereq->chain);
346655ff1a1SSrujanaChalla 	mv_cesa_skcipher_req_iter_init(&iter, req);
347655ff1a1SSrujanaChalla 
348655ff1a1SSrujanaChalla 	do {
349655ff1a1SSrujanaChalla 		struct mv_cesa_op_ctx *op;
350655ff1a1SSrujanaChalla 
351655ff1a1SSrujanaChalla 		op = mv_cesa_dma_add_op(&basereq->chain, op_templ, skip_ctx,
352655ff1a1SSrujanaChalla 					flags);
353655ff1a1SSrujanaChalla 		if (IS_ERR(op)) {
354655ff1a1SSrujanaChalla 			ret = PTR_ERR(op);
355655ff1a1SSrujanaChalla 			goto err_free_tdma;
356655ff1a1SSrujanaChalla 		}
357655ff1a1SSrujanaChalla 		skip_ctx = true;
358655ff1a1SSrujanaChalla 
359655ff1a1SSrujanaChalla 		mv_cesa_set_crypt_op_len(op, iter.base.op_len);
360655ff1a1SSrujanaChalla 
361655ff1a1SSrujanaChalla 		/* Add input transfers */
362655ff1a1SSrujanaChalla 		ret = mv_cesa_dma_add_op_transfers(&basereq->chain, &iter.base,
363655ff1a1SSrujanaChalla 						   &iter.src, flags);
364655ff1a1SSrujanaChalla 		if (ret)
365655ff1a1SSrujanaChalla 			goto err_free_tdma;
366655ff1a1SSrujanaChalla 
367655ff1a1SSrujanaChalla 		/* Add dummy desc to launch the crypto operation */
368655ff1a1SSrujanaChalla 		ret = mv_cesa_dma_add_dummy_launch(&basereq->chain, flags);
369655ff1a1SSrujanaChalla 		if (ret)
370655ff1a1SSrujanaChalla 			goto err_free_tdma;
371655ff1a1SSrujanaChalla 
372655ff1a1SSrujanaChalla 		/* Add output transfers */
373655ff1a1SSrujanaChalla 		ret = mv_cesa_dma_add_op_transfers(&basereq->chain, &iter.base,
374655ff1a1SSrujanaChalla 						   &iter.dst, flags);
375655ff1a1SSrujanaChalla 		if (ret)
376655ff1a1SSrujanaChalla 			goto err_free_tdma;
377655ff1a1SSrujanaChalla 
378655ff1a1SSrujanaChalla 	} while (mv_cesa_skcipher_req_iter_next_op(&iter));
379655ff1a1SSrujanaChalla 
380655ff1a1SSrujanaChalla 	/* Add output data for IV */
381655ff1a1SSrujanaChalla 	ret = mv_cesa_dma_add_result_op(&basereq->chain,
382655ff1a1SSrujanaChalla 					CESA_SA_CFG_SRAM_OFFSET,
383655ff1a1SSrujanaChalla 					CESA_SA_DATA_SRAM_OFFSET,
384655ff1a1SSrujanaChalla 					CESA_TDMA_SRC_IN_SRAM, flags);
385655ff1a1SSrujanaChalla 
386655ff1a1SSrujanaChalla 	if (ret)
387655ff1a1SSrujanaChalla 		goto err_free_tdma;
388655ff1a1SSrujanaChalla 
389655ff1a1SSrujanaChalla 	basereq->chain.last->flags |= CESA_TDMA_END_OF_REQ;
390655ff1a1SSrujanaChalla 
391655ff1a1SSrujanaChalla 	return 0;
392655ff1a1SSrujanaChalla 
393655ff1a1SSrujanaChalla err_free_tdma:
394655ff1a1SSrujanaChalla 	mv_cesa_dma_cleanup(basereq);
395655ff1a1SSrujanaChalla 	if (req->dst != req->src)
396655ff1a1SSrujanaChalla 		dma_unmap_sg(cesa_dev->dev, req->dst, creq->dst_nents,
397655ff1a1SSrujanaChalla 			     DMA_FROM_DEVICE);
398655ff1a1SSrujanaChalla 
399655ff1a1SSrujanaChalla err_unmap_src:
400655ff1a1SSrujanaChalla 	dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents,
401655ff1a1SSrujanaChalla 		     req->dst != req->src ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL);
402655ff1a1SSrujanaChalla 
403655ff1a1SSrujanaChalla 	return ret;
404655ff1a1SSrujanaChalla }
405655ff1a1SSrujanaChalla 
406655ff1a1SSrujanaChalla static inline int
mv_cesa_skcipher_std_req_init(struct skcipher_request * req,const struct mv_cesa_op_ctx * op_templ)407655ff1a1SSrujanaChalla mv_cesa_skcipher_std_req_init(struct skcipher_request *req,
408655ff1a1SSrujanaChalla 			      const struct mv_cesa_op_ctx *op_templ)
409655ff1a1SSrujanaChalla {
410655ff1a1SSrujanaChalla 	struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
411655ff1a1SSrujanaChalla 	struct mv_cesa_skcipher_std_req *sreq = &creq->std;
412655ff1a1SSrujanaChalla 	struct mv_cesa_req *basereq = &creq->base;
413655ff1a1SSrujanaChalla 
414655ff1a1SSrujanaChalla 	sreq->op = *op_templ;
415655ff1a1SSrujanaChalla 	sreq->skip_ctx = false;
416655ff1a1SSrujanaChalla 	basereq->chain.first = NULL;
417655ff1a1SSrujanaChalla 	basereq->chain.last = NULL;
418655ff1a1SSrujanaChalla 
419655ff1a1SSrujanaChalla 	return 0;
420655ff1a1SSrujanaChalla }
421655ff1a1SSrujanaChalla 
mv_cesa_skcipher_req_init(struct skcipher_request * req,struct mv_cesa_op_ctx * tmpl)422655ff1a1SSrujanaChalla static int mv_cesa_skcipher_req_init(struct skcipher_request *req,
423655ff1a1SSrujanaChalla 				     struct mv_cesa_op_ctx *tmpl)
424655ff1a1SSrujanaChalla {
425655ff1a1SSrujanaChalla 	struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
426655ff1a1SSrujanaChalla 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
427655ff1a1SSrujanaChalla 	unsigned int blksize = crypto_skcipher_blocksize(tfm);
428655ff1a1SSrujanaChalla 	int ret;
429655ff1a1SSrujanaChalla 
430655ff1a1SSrujanaChalla 	if (!IS_ALIGNED(req->cryptlen, blksize))
431655ff1a1SSrujanaChalla 		return -EINVAL;
432655ff1a1SSrujanaChalla 
433655ff1a1SSrujanaChalla 	creq->src_nents = sg_nents_for_len(req->src, req->cryptlen);
434655ff1a1SSrujanaChalla 	if (creq->src_nents < 0) {
435655ff1a1SSrujanaChalla 		dev_err(cesa_dev->dev, "Invalid number of src SG");
436655ff1a1SSrujanaChalla 		return creq->src_nents;
437655ff1a1SSrujanaChalla 	}
438655ff1a1SSrujanaChalla 	creq->dst_nents = sg_nents_for_len(req->dst, req->cryptlen);
439655ff1a1SSrujanaChalla 	if (creq->dst_nents < 0) {
440655ff1a1SSrujanaChalla 		dev_err(cesa_dev->dev, "Invalid number of dst SG");
441655ff1a1SSrujanaChalla 		return creq->dst_nents;
442655ff1a1SSrujanaChalla 	}
443655ff1a1SSrujanaChalla 
444655ff1a1SSrujanaChalla 	mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_OP_CRYPT_ONLY,
445655ff1a1SSrujanaChalla 			      CESA_SA_DESC_CFG_OP_MSK);
446655ff1a1SSrujanaChalla 
447655ff1a1SSrujanaChalla 	if (cesa_dev->caps->has_tdma)
448655ff1a1SSrujanaChalla 		ret = mv_cesa_skcipher_dma_req_init(req, tmpl);
449655ff1a1SSrujanaChalla 	else
450655ff1a1SSrujanaChalla 		ret = mv_cesa_skcipher_std_req_init(req, tmpl);
451655ff1a1SSrujanaChalla 
452655ff1a1SSrujanaChalla 	return ret;
453655ff1a1SSrujanaChalla }
454655ff1a1SSrujanaChalla 
mv_cesa_skcipher_queue_req(struct skcipher_request * req,struct mv_cesa_op_ctx * tmpl)455655ff1a1SSrujanaChalla static int mv_cesa_skcipher_queue_req(struct skcipher_request *req,
456655ff1a1SSrujanaChalla 				      struct mv_cesa_op_ctx *tmpl)
457655ff1a1SSrujanaChalla {
458655ff1a1SSrujanaChalla 	int ret;
459655ff1a1SSrujanaChalla 	struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
460655ff1a1SSrujanaChalla 	struct mv_cesa_engine *engine;
461655ff1a1SSrujanaChalla 
462655ff1a1SSrujanaChalla 	ret = mv_cesa_skcipher_req_init(req, tmpl);
463655ff1a1SSrujanaChalla 	if (ret)
464655ff1a1SSrujanaChalla 		return ret;
465655ff1a1SSrujanaChalla 
466655ff1a1SSrujanaChalla 	engine = mv_cesa_select_engine(req->cryptlen);
467655ff1a1SSrujanaChalla 	mv_cesa_skcipher_prepare(&req->base, engine);
468655ff1a1SSrujanaChalla 
469655ff1a1SSrujanaChalla 	ret = mv_cesa_queue_req(&req->base, &creq->base);
470655ff1a1SSrujanaChalla 
471655ff1a1SSrujanaChalla 	if (mv_cesa_req_needs_cleanup(&req->base, ret))
472655ff1a1SSrujanaChalla 		mv_cesa_skcipher_cleanup(req);
473655ff1a1SSrujanaChalla 
474655ff1a1SSrujanaChalla 	return ret;
475655ff1a1SSrujanaChalla }
476655ff1a1SSrujanaChalla 
mv_cesa_des_op(struct skcipher_request * req,struct mv_cesa_op_ctx * tmpl)477655ff1a1SSrujanaChalla static int mv_cesa_des_op(struct skcipher_request *req,
478655ff1a1SSrujanaChalla 			  struct mv_cesa_op_ctx *tmpl)
479655ff1a1SSrujanaChalla {
480655ff1a1SSrujanaChalla 	struct mv_cesa_des_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
481655ff1a1SSrujanaChalla 
482655ff1a1SSrujanaChalla 	mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTM_DES,
483655ff1a1SSrujanaChalla 			      CESA_SA_DESC_CFG_CRYPTM_MSK);
484655ff1a1SSrujanaChalla 
485655ff1a1SSrujanaChalla 	memcpy(tmpl->ctx.skcipher.key, ctx->key, DES_KEY_SIZE);
486655ff1a1SSrujanaChalla 
487655ff1a1SSrujanaChalla 	return mv_cesa_skcipher_queue_req(req, tmpl);
488655ff1a1SSrujanaChalla }
489655ff1a1SSrujanaChalla 
mv_cesa_ecb_des_encrypt(struct skcipher_request * req)490655ff1a1SSrujanaChalla static int mv_cesa_ecb_des_encrypt(struct skcipher_request *req)
491655ff1a1SSrujanaChalla {
492655ff1a1SSrujanaChalla 	struct mv_cesa_op_ctx tmpl;
493655ff1a1SSrujanaChalla 
494655ff1a1SSrujanaChalla 	mv_cesa_set_op_cfg(&tmpl,
495655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_CRYPTCM_ECB |
496655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_DIR_ENC);
497655ff1a1SSrujanaChalla 
498655ff1a1SSrujanaChalla 	return mv_cesa_des_op(req, &tmpl);
499655ff1a1SSrujanaChalla }
500655ff1a1SSrujanaChalla 
mv_cesa_ecb_des_decrypt(struct skcipher_request * req)501655ff1a1SSrujanaChalla static int mv_cesa_ecb_des_decrypt(struct skcipher_request *req)
502655ff1a1SSrujanaChalla {
503655ff1a1SSrujanaChalla 	struct mv_cesa_op_ctx tmpl;
504655ff1a1SSrujanaChalla 
505655ff1a1SSrujanaChalla 	mv_cesa_set_op_cfg(&tmpl,
506655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_CRYPTCM_ECB |
507655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_DIR_DEC);
508655ff1a1SSrujanaChalla 
509655ff1a1SSrujanaChalla 	return mv_cesa_des_op(req, &tmpl);
510655ff1a1SSrujanaChalla }
511655ff1a1SSrujanaChalla 
512655ff1a1SSrujanaChalla struct skcipher_alg mv_cesa_ecb_des_alg = {
513655ff1a1SSrujanaChalla 	.setkey = mv_cesa_des_setkey,
514655ff1a1SSrujanaChalla 	.encrypt = mv_cesa_ecb_des_encrypt,
515655ff1a1SSrujanaChalla 	.decrypt = mv_cesa_ecb_des_decrypt,
516655ff1a1SSrujanaChalla 	.min_keysize = DES_KEY_SIZE,
517655ff1a1SSrujanaChalla 	.max_keysize = DES_KEY_SIZE,
518655ff1a1SSrujanaChalla 	.base = {
519655ff1a1SSrujanaChalla 		.cra_name = "ecb(des)",
520655ff1a1SSrujanaChalla 		.cra_driver_name = "mv-ecb-des",
521655ff1a1SSrujanaChalla 		.cra_priority = 300,
522b8aa7dc5SMikulas Patocka 		.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC |
523b8aa7dc5SMikulas Patocka 			     CRYPTO_ALG_ALLOCATES_MEMORY,
524655ff1a1SSrujanaChalla 		.cra_blocksize = DES_BLOCK_SIZE,
525655ff1a1SSrujanaChalla 		.cra_ctxsize = sizeof(struct mv_cesa_des_ctx),
526655ff1a1SSrujanaChalla 		.cra_alignmask = 0,
527655ff1a1SSrujanaChalla 		.cra_module = THIS_MODULE,
528655ff1a1SSrujanaChalla 		.cra_init = mv_cesa_skcipher_cra_init,
529655ff1a1SSrujanaChalla 		.cra_exit = mv_cesa_skcipher_cra_exit,
530655ff1a1SSrujanaChalla 	},
531655ff1a1SSrujanaChalla };
532655ff1a1SSrujanaChalla 
mv_cesa_cbc_des_op(struct skcipher_request * req,struct mv_cesa_op_ctx * tmpl)533655ff1a1SSrujanaChalla static int mv_cesa_cbc_des_op(struct skcipher_request *req,
534655ff1a1SSrujanaChalla 			      struct mv_cesa_op_ctx *tmpl)
535655ff1a1SSrujanaChalla {
536655ff1a1SSrujanaChalla 	mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTCM_CBC,
537655ff1a1SSrujanaChalla 			      CESA_SA_DESC_CFG_CRYPTCM_MSK);
538655ff1a1SSrujanaChalla 
539655ff1a1SSrujanaChalla 	memcpy(tmpl->ctx.skcipher.iv, req->iv, DES_BLOCK_SIZE);
540655ff1a1SSrujanaChalla 
541655ff1a1SSrujanaChalla 	return mv_cesa_des_op(req, tmpl);
542655ff1a1SSrujanaChalla }
543655ff1a1SSrujanaChalla 
mv_cesa_cbc_des_encrypt(struct skcipher_request * req)544655ff1a1SSrujanaChalla static int mv_cesa_cbc_des_encrypt(struct skcipher_request *req)
545655ff1a1SSrujanaChalla {
546655ff1a1SSrujanaChalla 	struct mv_cesa_op_ctx tmpl;
547655ff1a1SSrujanaChalla 
548655ff1a1SSrujanaChalla 	mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_ENC);
549655ff1a1SSrujanaChalla 
550655ff1a1SSrujanaChalla 	return mv_cesa_cbc_des_op(req, &tmpl);
551655ff1a1SSrujanaChalla }
552655ff1a1SSrujanaChalla 
mv_cesa_cbc_des_decrypt(struct skcipher_request * req)553655ff1a1SSrujanaChalla static int mv_cesa_cbc_des_decrypt(struct skcipher_request *req)
554655ff1a1SSrujanaChalla {
555655ff1a1SSrujanaChalla 	struct mv_cesa_op_ctx tmpl;
556655ff1a1SSrujanaChalla 
557655ff1a1SSrujanaChalla 	mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_DEC);
558655ff1a1SSrujanaChalla 
559655ff1a1SSrujanaChalla 	return mv_cesa_cbc_des_op(req, &tmpl);
560655ff1a1SSrujanaChalla }
561655ff1a1SSrujanaChalla 
562655ff1a1SSrujanaChalla struct skcipher_alg mv_cesa_cbc_des_alg = {
563655ff1a1SSrujanaChalla 	.setkey = mv_cesa_des_setkey,
564655ff1a1SSrujanaChalla 	.encrypt = mv_cesa_cbc_des_encrypt,
565655ff1a1SSrujanaChalla 	.decrypt = mv_cesa_cbc_des_decrypt,
566655ff1a1SSrujanaChalla 	.min_keysize = DES_KEY_SIZE,
567655ff1a1SSrujanaChalla 	.max_keysize = DES_KEY_SIZE,
568655ff1a1SSrujanaChalla 	.ivsize = DES_BLOCK_SIZE,
569655ff1a1SSrujanaChalla 	.base = {
570655ff1a1SSrujanaChalla 		.cra_name = "cbc(des)",
571655ff1a1SSrujanaChalla 		.cra_driver_name = "mv-cbc-des",
572655ff1a1SSrujanaChalla 		.cra_priority = 300,
573b8aa7dc5SMikulas Patocka 		.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC |
574b8aa7dc5SMikulas Patocka 			     CRYPTO_ALG_ALLOCATES_MEMORY,
575655ff1a1SSrujanaChalla 		.cra_blocksize = DES_BLOCK_SIZE,
576655ff1a1SSrujanaChalla 		.cra_ctxsize = sizeof(struct mv_cesa_des_ctx),
577655ff1a1SSrujanaChalla 		.cra_alignmask = 0,
578655ff1a1SSrujanaChalla 		.cra_module = THIS_MODULE,
579655ff1a1SSrujanaChalla 		.cra_init = mv_cesa_skcipher_cra_init,
580655ff1a1SSrujanaChalla 		.cra_exit = mv_cesa_skcipher_cra_exit,
581655ff1a1SSrujanaChalla 	},
582655ff1a1SSrujanaChalla };
583655ff1a1SSrujanaChalla 
mv_cesa_des3_op(struct skcipher_request * req,struct mv_cesa_op_ctx * tmpl)584655ff1a1SSrujanaChalla static int mv_cesa_des3_op(struct skcipher_request *req,
585655ff1a1SSrujanaChalla 			   struct mv_cesa_op_ctx *tmpl)
586655ff1a1SSrujanaChalla {
587655ff1a1SSrujanaChalla 	struct mv_cesa_des3_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
588655ff1a1SSrujanaChalla 
589655ff1a1SSrujanaChalla 	mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTM_3DES,
590655ff1a1SSrujanaChalla 			      CESA_SA_DESC_CFG_CRYPTM_MSK);
591655ff1a1SSrujanaChalla 
592655ff1a1SSrujanaChalla 	memcpy(tmpl->ctx.skcipher.key, ctx->key, DES3_EDE_KEY_SIZE);
593655ff1a1SSrujanaChalla 
594655ff1a1SSrujanaChalla 	return mv_cesa_skcipher_queue_req(req, tmpl);
595655ff1a1SSrujanaChalla }
596655ff1a1SSrujanaChalla 
mv_cesa_ecb_des3_ede_encrypt(struct skcipher_request * req)597655ff1a1SSrujanaChalla static int mv_cesa_ecb_des3_ede_encrypt(struct skcipher_request *req)
598655ff1a1SSrujanaChalla {
599655ff1a1SSrujanaChalla 	struct mv_cesa_op_ctx tmpl;
600655ff1a1SSrujanaChalla 
601655ff1a1SSrujanaChalla 	mv_cesa_set_op_cfg(&tmpl,
602655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_CRYPTCM_ECB |
603655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_3DES_EDE |
604655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_DIR_ENC);
605655ff1a1SSrujanaChalla 
606655ff1a1SSrujanaChalla 	return mv_cesa_des3_op(req, &tmpl);
607655ff1a1SSrujanaChalla }
608655ff1a1SSrujanaChalla 
mv_cesa_ecb_des3_ede_decrypt(struct skcipher_request * req)609655ff1a1SSrujanaChalla static int mv_cesa_ecb_des3_ede_decrypt(struct skcipher_request *req)
610655ff1a1SSrujanaChalla {
611655ff1a1SSrujanaChalla 	struct mv_cesa_op_ctx tmpl;
612655ff1a1SSrujanaChalla 
613655ff1a1SSrujanaChalla 	mv_cesa_set_op_cfg(&tmpl,
614655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_CRYPTCM_ECB |
615655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_3DES_EDE |
616655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_DIR_DEC);
617655ff1a1SSrujanaChalla 
618655ff1a1SSrujanaChalla 	return mv_cesa_des3_op(req, &tmpl);
619655ff1a1SSrujanaChalla }
620655ff1a1SSrujanaChalla 
621655ff1a1SSrujanaChalla struct skcipher_alg mv_cesa_ecb_des3_ede_alg = {
622655ff1a1SSrujanaChalla 	.setkey = mv_cesa_des3_ede_setkey,
623655ff1a1SSrujanaChalla 	.encrypt = mv_cesa_ecb_des3_ede_encrypt,
624655ff1a1SSrujanaChalla 	.decrypt = mv_cesa_ecb_des3_ede_decrypt,
625655ff1a1SSrujanaChalla 	.min_keysize = DES3_EDE_KEY_SIZE,
626655ff1a1SSrujanaChalla 	.max_keysize = DES3_EDE_KEY_SIZE,
627655ff1a1SSrujanaChalla 	.base = {
628655ff1a1SSrujanaChalla 		.cra_name = "ecb(des3_ede)",
629655ff1a1SSrujanaChalla 		.cra_driver_name = "mv-ecb-des3-ede",
630655ff1a1SSrujanaChalla 		.cra_priority = 300,
631b8aa7dc5SMikulas Patocka 		.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC |
632b8aa7dc5SMikulas Patocka 			     CRYPTO_ALG_ALLOCATES_MEMORY,
633655ff1a1SSrujanaChalla 		.cra_blocksize = DES3_EDE_BLOCK_SIZE,
634655ff1a1SSrujanaChalla 		.cra_ctxsize = sizeof(struct mv_cesa_des3_ctx),
635655ff1a1SSrujanaChalla 		.cra_alignmask = 0,
636655ff1a1SSrujanaChalla 		.cra_module = THIS_MODULE,
637655ff1a1SSrujanaChalla 		.cra_init = mv_cesa_skcipher_cra_init,
638655ff1a1SSrujanaChalla 		.cra_exit = mv_cesa_skcipher_cra_exit,
639655ff1a1SSrujanaChalla 	},
640655ff1a1SSrujanaChalla };
641655ff1a1SSrujanaChalla 
mv_cesa_cbc_des3_op(struct skcipher_request * req,struct mv_cesa_op_ctx * tmpl)642655ff1a1SSrujanaChalla static int mv_cesa_cbc_des3_op(struct skcipher_request *req,
643655ff1a1SSrujanaChalla 			       struct mv_cesa_op_ctx *tmpl)
644655ff1a1SSrujanaChalla {
645655ff1a1SSrujanaChalla 	memcpy(tmpl->ctx.skcipher.iv, req->iv, DES3_EDE_BLOCK_SIZE);
646655ff1a1SSrujanaChalla 
647655ff1a1SSrujanaChalla 	return mv_cesa_des3_op(req, tmpl);
648655ff1a1SSrujanaChalla }
649655ff1a1SSrujanaChalla 
mv_cesa_cbc_des3_ede_encrypt(struct skcipher_request * req)650655ff1a1SSrujanaChalla static int mv_cesa_cbc_des3_ede_encrypt(struct skcipher_request *req)
651655ff1a1SSrujanaChalla {
652655ff1a1SSrujanaChalla 	struct mv_cesa_op_ctx tmpl;
653655ff1a1SSrujanaChalla 
654655ff1a1SSrujanaChalla 	mv_cesa_set_op_cfg(&tmpl,
655655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_CRYPTCM_CBC |
656655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_3DES_EDE |
657655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_DIR_ENC);
658655ff1a1SSrujanaChalla 
659655ff1a1SSrujanaChalla 	return mv_cesa_cbc_des3_op(req, &tmpl);
660655ff1a1SSrujanaChalla }
661655ff1a1SSrujanaChalla 
mv_cesa_cbc_des3_ede_decrypt(struct skcipher_request * req)662655ff1a1SSrujanaChalla static int mv_cesa_cbc_des3_ede_decrypt(struct skcipher_request *req)
663655ff1a1SSrujanaChalla {
664655ff1a1SSrujanaChalla 	struct mv_cesa_op_ctx tmpl;
665655ff1a1SSrujanaChalla 
666655ff1a1SSrujanaChalla 	mv_cesa_set_op_cfg(&tmpl,
667655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_CRYPTCM_CBC |
668655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_3DES_EDE |
669655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_DIR_DEC);
670655ff1a1SSrujanaChalla 
671655ff1a1SSrujanaChalla 	return mv_cesa_cbc_des3_op(req, &tmpl);
672655ff1a1SSrujanaChalla }
673655ff1a1SSrujanaChalla 
674655ff1a1SSrujanaChalla struct skcipher_alg mv_cesa_cbc_des3_ede_alg = {
675655ff1a1SSrujanaChalla 	.setkey = mv_cesa_des3_ede_setkey,
676655ff1a1SSrujanaChalla 	.encrypt = mv_cesa_cbc_des3_ede_encrypt,
677655ff1a1SSrujanaChalla 	.decrypt = mv_cesa_cbc_des3_ede_decrypt,
678655ff1a1SSrujanaChalla 	.min_keysize = DES3_EDE_KEY_SIZE,
679655ff1a1SSrujanaChalla 	.max_keysize = DES3_EDE_KEY_SIZE,
680655ff1a1SSrujanaChalla 	.ivsize = DES3_EDE_BLOCK_SIZE,
681655ff1a1SSrujanaChalla 	.base = {
682655ff1a1SSrujanaChalla 		.cra_name = "cbc(des3_ede)",
683655ff1a1SSrujanaChalla 		.cra_driver_name = "mv-cbc-des3-ede",
684655ff1a1SSrujanaChalla 		.cra_priority = 300,
685b8aa7dc5SMikulas Patocka 		.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC |
686b8aa7dc5SMikulas Patocka 			     CRYPTO_ALG_ALLOCATES_MEMORY,
687655ff1a1SSrujanaChalla 		.cra_blocksize = DES3_EDE_BLOCK_SIZE,
688655ff1a1SSrujanaChalla 		.cra_ctxsize = sizeof(struct mv_cesa_des3_ctx),
689655ff1a1SSrujanaChalla 		.cra_alignmask = 0,
690655ff1a1SSrujanaChalla 		.cra_module = THIS_MODULE,
691655ff1a1SSrujanaChalla 		.cra_init = mv_cesa_skcipher_cra_init,
692655ff1a1SSrujanaChalla 		.cra_exit = mv_cesa_skcipher_cra_exit,
693655ff1a1SSrujanaChalla 	},
694655ff1a1SSrujanaChalla };
695655ff1a1SSrujanaChalla 
mv_cesa_aes_op(struct skcipher_request * req,struct mv_cesa_op_ctx * tmpl)696655ff1a1SSrujanaChalla static int mv_cesa_aes_op(struct skcipher_request *req,
697655ff1a1SSrujanaChalla 			  struct mv_cesa_op_ctx *tmpl)
698655ff1a1SSrujanaChalla {
699655ff1a1SSrujanaChalla 	struct mv_cesa_aes_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
700655ff1a1SSrujanaChalla 	int i;
701655ff1a1SSrujanaChalla 	u32 *key;
702655ff1a1SSrujanaChalla 	u32 cfg;
703655ff1a1SSrujanaChalla 
704655ff1a1SSrujanaChalla 	cfg = CESA_SA_DESC_CFG_CRYPTM_AES;
705655ff1a1SSrujanaChalla 
706655ff1a1SSrujanaChalla 	if (mv_cesa_get_op_cfg(tmpl) & CESA_SA_DESC_CFG_DIR_DEC)
707655ff1a1SSrujanaChalla 		key = ctx->aes.key_dec;
708655ff1a1SSrujanaChalla 	else
709655ff1a1SSrujanaChalla 		key = ctx->aes.key_enc;
710655ff1a1SSrujanaChalla 
711655ff1a1SSrujanaChalla 	for (i = 0; i < ctx->aes.key_length / sizeof(u32); i++)
712655ff1a1SSrujanaChalla 		tmpl->ctx.skcipher.key[i] = cpu_to_le32(key[i]);
713655ff1a1SSrujanaChalla 
714655ff1a1SSrujanaChalla 	if (ctx->aes.key_length == 24)
715655ff1a1SSrujanaChalla 		cfg |= CESA_SA_DESC_CFG_AES_LEN_192;
716655ff1a1SSrujanaChalla 	else if (ctx->aes.key_length == 32)
717655ff1a1SSrujanaChalla 		cfg |= CESA_SA_DESC_CFG_AES_LEN_256;
718655ff1a1SSrujanaChalla 
719655ff1a1SSrujanaChalla 	mv_cesa_update_op_cfg(tmpl, cfg,
720655ff1a1SSrujanaChalla 			      CESA_SA_DESC_CFG_CRYPTM_MSK |
721655ff1a1SSrujanaChalla 			      CESA_SA_DESC_CFG_AES_LEN_MSK);
722655ff1a1SSrujanaChalla 
723655ff1a1SSrujanaChalla 	return mv_cesa_skcipher_queue_req(req, tmpl);
724655ff1a1SSrujanaChalla }
725655ff1a1SSrujanaChalla 
mv_cesa_ecb_aes_encrypt(struct skcipher_request * req)726655ff1a1SSrujanaChalla static int mv_cesa_ecb_aes_encrypt(struct skcipher_request *req)
727655ff1a1SSrujanaChalla {
728655ff1a1SSrujanaChalla 	struct mv_cesa_op_ctx tmpl;
729655ff1a1SSrujanaChalla 
730655ff1a1SSrujanaChalla 	mv_cesa_set_op_cfg(&tmpl,
731655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_CRYPTCM_ECB |
732655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_DIR_ENC);
733655ff1a1SSrujanaChalla 
734655ff1a1SSrujanaChalla 	return mv_cesa_aes_op(req, &tmpl);
735655ff1a1SSrujanaChalla }
736655ff1a1SSrujanaChalla 
mv_cesa_ecb_aes_decrypt(struct skcipher_request * req)737655ff1a1SSrujanaChalla static int mv_cesa_ecb_aes_decrypt(struct skcipher_request *req)
738655ff1a1SSrujanaChalla {
739655ff1a1SSrujanaChalla 	struct mv_cesa_op_ctx tmpl;
740655ff1a1SSrujanaChalla 
741655ff1a1SSrujanaChalla 	mv_cesa_set_op_cfg(&tmpl,
742655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_CRYPTCM_ECB |
743655ff1a1SSrujanaChalla 			   CESA_SA_DESC_CFG_DIR_DEC);
744655ff1a1SSrujanaChalla 
745655ff1a1SSrujanaChalla 	return mv_cesa_aes_op(req, &tmpl);
746655ff1a1SSrujanaChalla }
747655ff1a1SSrujanaChalla 
748655ff1a1SSrujanaChalla struct skcipher_alg mv_cesa_ecb_aes_alg = {
749655ff1a1SSrujanaChalla 	.setkey = mv_cesa_aes_setkey,
750655ff1a1SSrujanaChalla 	.encrypt = mv_cesa_ecb_aes_encrypt,
751655ff1a1SSrujanaChalla 	.decrypt = mv_cesa_ecb_aes_decrypt,
752655ff1a1SSrujanaChalla 	.min_keysize = AES_MIN_KEY_SIZE,
753655ff1a1SSrujanaChalla 	.max_keysize = AES_MAX_KEY_SIZE,
754655ff1a1SSrujanaChalla 	.base = {
755655ff1a1SSrujanaChalla 		.cra_name = "ecb(aes)",
756655ff1a1SSrujanaChalla 		.cra_driver_name = "mv-ecb-aes",
757655ff1a1SSrujanaChalla 		.cra_priority = 300,
758b8aa7dc5SMikulas Patocka 		.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC |
759b8aa7dc5SMikulas Patocka 			     CRYPTO_ALG_ALLOCATES_MEMORY,
760655ff1a1SSrujanaChalla 		.cra_blocksize = AES_BLOCK_SIZE,
761655ff1a1SSrujanaChalla 		.cra_ctxsize = sizeof(struct mv_cesa_aes_ctx),
762655ff1a1SSrujanaChalla 		.cra_alignmask = 0,
763655ff1a1SSrujanaChalla 		.cra_module = THIS_MODULE,
764655ff1a1SSrujanaChalla 		.cra_init = mv_cesa_skcipher_cra_init,
765655ff1a1SSrujanaChalla 		.cra_exit = mv_cesa_skcipher_cra_exit,
766655ff1a1SSrujanaChalla 	},
767655ff1a1SSrujanaChalla };
768655ff1a1SSrujanaChalla 
mv_cesa_cbc_aes_op(struct skcipher_request * req,struct mv_cesa_op_ctx * tmpl)769655ff1a1SSrujanaChalla static int mv_cesa_cbc_aes_op(struct skcipher_request *req,
770655ff1a1SSrujanaChalla 			      struct mv_cesa_op_ctx *tmpl)
771655ff1a1SSrujanaChalla {
772655ff1a1SSrujanaChalla 	mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTCM_CBC,
773655ff1a1SSrujanaChalla 			      CESA_SA_DESC_CFG_CRYPTCM_MSK);
774655ff1a1SSrujanaChalla 	memcpy(tmpl->ctx.skcipher.iv, req->iv, AES_BLOCK_SIZE);
775655ff1a1SSrujanaChalla 
776655ff1a1SSrujanaChalla 	return mv_cesa_aes_op(req, tmpl);
777655ff1a1SSrujanaChalla }
778655ff1a1SSrujanaChalla 
mv_cesa_cbc_aes_encrypt(struct skcipher_request * req)779655ff1a1SSrujanaChalla static int mv_cesa_cbc_aes_encrypt(struct skcipher_request *req)
780655ff1a1SSrujanaChalla {
781655ff1a1SSrujanaChalla 	struct mv_cesa_op_ctx tmpl;
782655ff1a1SSrujanaChalla 
783655ff1a1SSrujanaChalla 	mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_ENC);
784655ff1a1SSrujanaChalla 
785655ff1a1SSrujanaChalla 	return mv_cesa_cbc_aes_op(req, &tmpl);
786655ff1a1SSrujanaChalla }
787655ff1a1SSrujanaChalla 
mv_cesa_cbc_aes_decrypt(struct skcipher_request * req)788655ff1a1SSrujanaChalla static int mv_cesa_cbc_aes_decrypt(struct skcipher_request *req)
789655ff1a1SSrujanaChalla {
790655ff1a1SSrujanaChalla 	struct mv_cesa_op_ctx tmpl;
791655ff1a1SSrujanaChalla 
792655ff1a1SSrujanaChalla 	mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_DEC);
793655ff1a1SSrujanaChalla 
794655ff1a1SSrujanaChalla 	return mv_cesa_cbc_aes_op(req, &tmpl);
795655ff1a1SSrujanaChalla }
796655ff1a1SSrujanaChalla 
797655ff1a1SSrujanaChalla struct skcipher_alg mv_cesa_cbc_aes_alg = {
798655ff1a1SSrujanaChalla 	.setkey = mv_cesa_aes_setkey,
799655ff1a1SSrujanaChalla 	.encrypt = mv_cesa_cbc_aes_encrypt,
800655ff1a1SSrujanaChalla 	.decrypt = mv_cesa_cbc_aes_decrypt,
801655ff1a1SSrujanaChalla 	.min_keysize = AES_MIN_KEY_SIZE,
802655ff1a1SSrujanaChalla 	.max_keysize = AES_MAX_KEY_SIZE,
803655ff1a1SSrujanaChalla 	.ivsize = AES_BLOCK_SIZE,
804655ff1a1SSrujanaChalla 	.base = {
805655ff1a1SSrujanaChalla 		.cra_name = "cbc(aes)",
806655ff1a1SSrujanaChalla 		.cra_driver_name = "mv-cbc-aes",
807655ff1a1SSrujanaChalla 		.cra_priority = 300,
808b8aa7dc5SMikulas Patocka 		.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC |
809b8aa7dc5SMikulas Patocka 			     CRYPTO_ALG_ALLOCATES_MEMORY,
810655ff1a1SSrujanaChalla 		.cra_blocksize = AES_BLOCK_SIZE,
811655ff1a1SSrujanaChalla 		.cra_ctxsize = sizeof(struct mv_cesa_aes_ctx),
812655ff1a1SSrujanaChalla 		.cra_alignmask = 0,
813655ff1a1SSrujanaChalla 		.cra_module = THIS_MODULE,
814655ff1a1SSrujanaChalla 		.cra_init = mv_cesa_skcipher_cra_init,
815655ff1a1SSrujanaChalla 		.cra_exit = mv_cesa_skcipher_cra_exit,
816655ff1a1SSrujanaChalla 	},
817655ff1a1SSrujanaChalla };
818