blob: 0ae84ec9e21c0d3d8dadc3b79e701266cf0535fd [file] [log] [blame]
Boris BREZILLONf63601f2015-06-18 15:46:20 +02001/*
2 * Cipher algorithms supported by the CESA: DES, 3DES and AES.
3 *
4 * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
5 * Author: Arnaud Ebalard <arno@natisbad.org>
6 *
7 * This work is based on an initial version written by
8 * Sebastian Andrzej Siewior < sebastian at breakpoint dot cc >
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
13 */
14
15#include <crypto/aes.h>
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +020016#include <crypto/des.h>
Boris BREZILLONf63601f2015-06-18 15:46:20 +020017
18#include "cesa.h"
19
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +020020struct mv_cesa_des_ctx {
21 struct mv_cesa_ctx base;
22 u8 key[DES_KEY_SIZE];
23};
24
Arnaud Ebalard4ada4832015-06-18 15:46:23 +020025struct mv_cesa_des3_ctx {
26 struct mv_cesa_ctx base;
27 u8 key[DES3_EDE_KEY_SIZE];
28};
29
Boris BREZILLONf63601f2015-06-18 15:46:20 +020030struct mv_cesa_aes_ctx {
31 struct mv_cesa_ctx base;
32 struct crypto_aes_ctx aes;
33};
34
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020035struct mv_cesa_skcipher_dma_iter {
Boris BREZILLONdb509a42015-06-18 15:46:21 +020036 struct mv_cesa_dma_iter base;
37 struct mv_cesa_sg_dma_iter src;
38 struct mv_cesa_sg_dma_iter dst;
39};
40
41static inline void
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020042mv_cesa_skcipher_req_iter_init(struct mv_cesa_skcipher_dma_iter *iter,
43 struct skcipher_request *req)
Boris BREZILLONdb509a42015-06-18 15:46:21 +020044{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020045 mv_cesa_req_dma_iter_init(&iter->base, req->cryptlen);
Boris BREZILLONdb509a42015-06-18 15:46:21 +020046 mv_cesa_sg_dma_iter_init(&iter->src, req->src, DMA_TO_DEVICE);
47 mv_cesa_sg_dma_iter_init(&iter->dst, req->dst, DMA_FROM_DEVICE);
48}
49
50static inline bool
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020051mv_cesa_skcipher_req_iter_next_op(struct mv_cesa_skcipher_dma_iter *iter)
Boris BREZILLONdb509a42015-06-18 15:46:21 +020052{
53 iter->src.op_offset = 0;
54 iter->dst.op_offset = 0;
55
56 return mv_cesa_req_dma_iter_next_op(&iter->base);
57}
58
59static inline void
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020060mv_cesa_skcipher_dma_cleanup(struct skcipher_request *req)
Boris BREZILLONdb509a42015-06-18 15:46:21 +020061{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020062 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +020063
64 if (req->dst != req->src) {
65 dma_unmap_sg(cesa_dev->dev, req->dst, creq->dst_nents,
66 DMA_FROM_DEVICE);
67 dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents,
68 DMA_TO_DEVICE);
69 } else {
70 dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents,
71 DMA_BIDIRECTIONAL);
72 }
Romain Perier53da7402016-06-21 10:08:35 +020073 mv_cesa_dma_cleanup(&creq->base);
Boris BREZILLONdb509a42015-06-18 15:46:21 +020074}
75
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020076static inline void mv_cesa_skcipher_cleanup(struct skcipher_request *req)
Boris BREZILLONdb509a42015-06-18 15:46:21 +020077{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020078 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +020079
Romain Perier53da7402016-06-21 10:08:35 +020080 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020081 mv_cesa_skcipher_dma_cleanup(req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +020082}
83
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020084static void mv_cesa_skcipher_std_step(struct skcipher_request *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +020085{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020086 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
87 struct mv_cesa_skcipher_std_req *sreq = &creq->std;
Romain Perier53da7402016-06-21 10:08:35 +020088 struct mv_cesa_engine *engine = creq->base.engine;
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020089 size_t len = min_t(size_t, req->cryptlen - sreq->offset,
Boris BREZILLONf63601f2015-06-18 15:46:20 +020090 CESA_SA_SRAM_PAYLOAD_SIZE);
91
Romain Perier2786cee2016-06-21 10:08:37 +020092 mv_cesa_adjust_op(engine, &sreq->op);
93 memcpy_toio(engine->sram, &sreq->op, sizeof(sreq->op));
94
Boris BREZILLONf63601f2015-06-18 15:46:20 +020095 len = sg_pcopy_to_buffer(req->src, creq->src_nents,
96 engine->sram + CESA_SA_DATA_SRAM_OFFSET,
97 len, sreq->offset);
98
99 sreq->size = len;
100 mv_cesa_set_crypt_op_len(&sreq->op, len);
101
102 /* FIXME: only update enc_len field */
103 if (!sreq->skip_ctx) {
Russell King0f3304d2015-10-18 18:31:15 +0100104 memcpy_toio(engine->sram, &sreq->op, sizeof(sreq->op));
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200105 sreq->skip_ctx = true;
106 } else {
Russell King0f3304d2015-10-18 18:31:15 +0100107 memcpy_toio(engine->sram, &sreq->op, sizeof(sreq->op.desc));
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200108 }
109
110 mv_cesa_set_int_mask(engine, CESA_SA_INT_ACCEL0_DONE);
Russell Kingb1508562015-10-18 18:31:00 +0100111 writel_relaxed(CESA_SA_CFG_PARA_DIS, engine->regs + CESA_SA_CFG);
Romain Perierf6283082016-06-21 10:08:32 +0200112 BUG_ON(readl(engine->regs + CESA_SA_CMD) &
113 CESA_SA_CMD_EN_CESA_SA_ACCL0);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200114 writel(CESA_SA_CMD_EN_CESA_SA_ACCL0, engine->regs + CESA_SA_CMD);
115}
116
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200117static int mv_cesa_skcipher_std_process(struct skcipher_request *req,
118 u32 status)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200119{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200120 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
121 struct mv_cesa_skcipher_std_req *sreq = &creq->std;
Romain Perier53da7402016-06-21 10:08:35 +0200122 struct mv_cesa_engine *engine = creq->base.engine;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200123 size_t len;
124
125 len = sg_pcopy_from_buffer(req->dst, creq->dst_nents,
126 engine->sram + CESA_SA_DATA_SRAM_OFFSET,
127 sreq->size, sreq->offset);
128
129 sreq->offset += len;
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200130 if (sreq->offset < req->cryptlen)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200131 return -EINPROGRESS;
132
133 return 0;
134}
135
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200136static int mv_cesa_skcipher_process(struct crypto_async_request *req,
137 u32 status)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200138{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200139 struct skcipher_request *skreq = skcipher_request_cast(req);
140 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq);
Romain Perier53da7402016-06-21 10:08:35 +0200141 struct mv_cesa_req *basereq = &creq->base;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200142
Romain Perier53da7402016-06-21 10:08:35 +0200143 if (mv_cesa_req_get_type(basereq) == CESA_STD_REQ)
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200144 return mv_cesa_skcipher_std_process(skreq, status);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200145
Romain Perier8cf740a2016-07-28 11:59:43 +0200146 return mv_cesa_dma_process(basereq, status);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200147}
148
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200149static void mv_cesa_skcipher_step(struct crypto_async_request *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200150{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200151 struct skcipher_request *skreq = skcipher_request_cast(req);
152 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200153
Romain Perier53da7402016-06-21 10:08:35 +0200154 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
155 mv_cesa_dma_step(&creq->base);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200156 else
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200157 mv_cesa_skcipher_std_step(skreq);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200158}
159
160static inline void
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200161mv_cesa_skcipher_dma_prepare(struct skcipher_request *req)
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200162{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200163 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
Romain Perier53da7402016-06-21 10:08:35 +0200164 struct mv_cesa_req *basereq = &creq->base;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200165
Romain Perier53da7402016-06-21 10:08:35 +0200166 mv_cesa_dma_prepare(basereq, basereq->engine);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200167}
168
169static inline void
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200170mv_cesa_skcipher_std_prepare(struct skcipher_request *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200171{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200172 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
173 struct mv_cesa_skcipher_std_req *sreq = &creq->std;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200174
175 sreq->size = 0;
176 sreq->offset = 0;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200177}
178
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200179static inline void mv_cesa_skcipher_prepare(struct crypto_async_request *req,
180 struct mv_cesa_engine *engine)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200181{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200182 struct skcipher_request *skreq = skcipher_request_cast(req);
183 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq);
Romain Perier53da7402016-06-21 10:08:35 +0200184 creq->base.engine = engine;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200185
Romain Perier53da7402016-06-21 10:08:35 +0200186 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200187 mv_cesa_skcipher_dma_prepare(skreq);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200188 else
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200189 mv_cesa_skcipher_std_prepare(skreq);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200190}
191
192static inline void
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200193mv_cesa_skcipher_req_cleanup(struct crypto_async_request *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200194{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200195 struct skcipher_request *skreq = skcipher_request_cast(req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200196
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200197 mv_cesa_skcipher_cleanup(skreq);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200198}
199
Romain Perier1bf66822016-06-21 10:08:36 +0200200static void
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200201mv_cesa_skcipher_complete(struct crypto_async_request *req)
Romain Perier1bf66822016-06-21 10:08:36 +0200202{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200203 struct skcipher_request *skreq = skcipher_request_cast(req);
204 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq);
Romain Perier1bf66822016-06-21 10:08:36 +0200205 struct mv_cesa_engine *engine = creq->base.engine;
206 unsigned int ivsize;
207
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200208 atomic_sub(skreq->cryptlen, &engine->load);
209 ivsize = crypto_skcipher_ivsize(crypto_skcipher_reqtfm(skreq));
Romain Perier1bf66822016-06-21 10:08:36 +0200210
211 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ) {
212 struct mv_cesa_req *basereq;
213
214 basereq = &creq->base;
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200215 memcpy(skreq->iv, basereq->chain.last->op->ctx.blkcipher.iv,
Romain Perier0c996202016-10-05 09:56:32 +0200216 ivsize);
Romain Perier1bf66822016-06-21 10:08:36 +0200217 } else {
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200218 memcpy_fromio(skreq->iv,
Romain Perier1bf66822016-06-21 10:08:36 +0200219 engine->sram + CESA_SA_CRYPT_IV_SRAM_OFFSET,
220 ivsize);
221 }
222}
223
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200224static const struct mv_cesa_req_ops mv_cesa_skcipher_req_ops = {
225 .step = mv_cesa_skcipher_step,
226 .process = mv_cesa_skcipher_process,
227 .cleanup = mv_cesa_skcipher_req_cleanup,
228 .complete = mv_cesa_skcipher_complete,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200229};
230
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200231static void mv_cesa_skcipher_cra_exit(struct crypto_tfm *tfm)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200232{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200233 void *ctx = crypto_tfm_ctx(tfm);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200234
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200235 memzero_explicit(ctx, tfm->__crt_alg->cra_ctxsize);
236}
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200237
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200238static int mv_cesa_skcipher_cra_init(struct crypto_tfm *tfm)
239{
240 struct mv_cesa_ctx *ctx = crypto_tfm_ctx(tfm);
241
242 ctx->ops = &mv_cesa_skcipher_req_ops;
243
244 crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
245 sizeof(struct mv_cesa_skcipher_req));
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200246
247 return 0;
248}
249
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200250static int mv_cesa_aes_setkey(struct crypto_skcipher *cipher, const u8 *key,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200251 unsigned int len)
252{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200253 struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200254 struct mv_cesa_aes_ctx *ctx = crypto_tfm_ctx(tfm);
255 int remaining;
256 int offset;
257 int ret;
258 int i;
259
260 ret = crypto_aes_expand_key(&ctx->aes, key, len);
261 if (ret) {
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200262 crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200263 return ret;
264 }
265
266 remaining = (ctx->aes.key_length - 16) / 4;
267 offset = ctx->aes.key_length + 24 - remaining;
268 for (i = 0; i < remaining; i++)
269 ctx->aes.key_dec[4 + i] =
270 cpu_to_le32(ctx->aes.key_enc[offset + i]);
271
272 return 0;
273}
274
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200275static int mv_cesa_des_setkey(struct crypto_skcipher *cipher, const u8 *key,
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200276 unsigned int len)
277{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200278 struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200279 struct mv_cesa_des_ctx *ctx = crypto_tfm_ctx(tfm);
280 u32 tmp[DES_EXPKEY_WORDS];
281 int ret;
282
283 if (len != DES_KEY_SIZE) {
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200284 crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200285 return -EINVAL;
286 }
287
288 ret = des_ekey(tmp, key);
289 if (!ret && (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
290 tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
291 return -EINVAL;
292 }
293
294 memcpy(ctx->key, key, DES_KEY_SIZE);
295
296 return 0;
297}
298
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200299static int mv_cesa_des3_ede_setkey(struct crypto_skcipher *cipher,
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200300 const u8 *key, unsigned int len)
301{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200302 struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200303 struct mv_cesa_des_ctx *ctx = crypto_tfm_ctx(tfm);
304
305 if (len != DES3_EDE_KEY_SIZE) {
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200306 crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200307 return -EINVAL;
308 }
309
310 memcpy(ctx->key, key, DES3_EDE_KEY_SIZE);
311
312 return 0;
313}
314
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200315static int mv_cesa_skcipher_dma_req_init(struct skcipher_request *req,
316 const struct mv_cesa_op_ctx *op_templ)
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200317{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200318 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200319 gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
320 GFP_KERNEL : GFP_ATOMIC;
Romain Perier53da7402016-06-21 10:08:35 +0200321 struct mv_cesa_req *basereq = &creq->base;
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200322 struct mv_cesa_skcipher_dma_iter iter;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200323 bool skip_ctx = false;
324 int ret;
Romain Perierbac8e802016-06-21 10:08:34 +0200325 unsigned int ivsize;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200326
Romain Perier53da7402016-06-21 10:08:35 +0200327 basereq->chain.first = NULL;
328 basereq->chain.last = NULL;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200329
330 if (req->src != req->dst) {
331 ret = dma_map_sg(cesa_dev->dev, req->src, creq->src_nents,
332 DMA_TO_DEVICE);
333 if (!ret)
334 return -ENOMEM;
335
336 ret = dma_map_sg(cesa_dev->dev, req->dst, creq->dst_nents,
337 DMA_FROM_DEVICE);
338 if (!ret) {
339 ret = -ENOMEM;
340 goto err_unmap_src;
341 }
342 } else {
343 ret = dma_map_sg(cesa_dev->dev, req->src, creq->src_nents,
344 DMA_BIDIRECTIONAL);
345 if (!ret)
346 return -ENOMEM;
347 }
348
Romain Perierec38f822016-07-22 14:40:39 +0200349 mv_cesa_tdma_desc_iter_init(&basereq->chain);
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200350 mv_cesa_skcipher_req_iter_init(&iter, req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200351
352 do {
353 struct mv_cesa_op_ctx *op;
354
Romain Perierec38f822016-07-22 14:40:39 +0200355 op = mv_cesa_dma_add_op(&basereq->chain, op_templ, skip_ctx, flags);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200356 if (IS_ERR(op)) {
357 ret = PTR_ERR(op);
358 goto err_free_tdma;
359 }
360 skip_ctx = true;
361
362 mv_cesa_set_crypt_op_len(op, iter.base.op_len);
363
364 /* Add input transfers */
Romain Perierec38f822016-07-22 14:40:39 +0200365 ret = mv_cesa_dma_add_op_transfers(&basereq->chain, &iter.base,
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200366 &iter.src, flags);
367 if (ret)
368 goto err_free_tdma;
369
370 /* Add dummy desc to launch the crypto operation */
Romain Perierec38f822016-07-22 14:40:39 +0200371 ret = mv_cesa_dma_add_dummy_launch(&basereq->chain, flags);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200372 if (ret)
373 goto err_free_tdma;
374
375 /* Add output transfers */
Romain Perierec38f822016-07-22 14:40:39 +0200376 ret = mv_cesa_dma_add_op_transfers(&basereq->chain, &iter.base,
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200377 &iter.dst, flags);
378 if (ret)
379 goto err_free_tdma;
380
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200381 } while (mv_cesa_skcipher_req_iter_next_op(&iter));
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200382
Romain Perierbac8e802016-06-21 10:08:34 +0200383 /* Add output data for IV */
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200384 ivsize = crypto_skcipher_ivsize(crypto_skcipher_reqtfm(req));
Romain Perier0c996202016-10-05 09:56:32 +0200385 ret = mv_cesa_dma_add_result_op(&basereq->chain, CESA_SA_CFG_SRAM_OFFSET,
386 CESA_SA_DATA_SRAM_OFFSET,
387 CESA_TDMA_SRC_IN_SRAM, flags);
Romain Perierbac8e802016-06-21 10:08:34 +0200388
389 if (ret)
390 goto err_free_tdma;
391
Romain Perier85030c52016-06-21 10:08:39 +0200392 basereq->chain.last->flags |= CESA_TDMA_END_OF_REQ;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200393
394 return 0;
395
396err_free_tdma:
Romain Perier53da7402016-06-21 10:08:35 +0200397 mv_cesa_dma_cleanup(basereq);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200398 if (req->dst != req->src)
399 dma_unmap_sg(cesa_dev->dev, req->dst, creq->dst_nents,
400 DMA_FROM_DEVICE);
401
402err_unmap_src:
403 dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents,
404 req->dst != req->src ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL);
405
406 return ret;
407}
408
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200409static inline int
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200410mv_cesa_skcipher_std_req_init(struct skcipher_request *req,
411 const struct mv_cesa_op_ctx *op_templ)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200412{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200413 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
414 struct mv_cesa_skcipher_std_req *sreq = &creq->std;
Romain Perier53da7402016-06-21 10:08:35 +0200415 struct mv_cesa_req *basereq = &creq->base;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200416
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200417 sreq->op = *op_templ;
418 sreq->skip_ctx = false;
Romain Perier53da7402016-06-21 10:08:35 +0200419 basereq->chain.first = NULL;
420 basereq->chain.last = NULL;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200421
422 return 0;
423}
424
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200425static int mv_cesa_skcipher_req_init(struct skcipher_request *req,
426 struct mv_cesa_op_ctx *tmpl)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200427{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200428 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
429 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
430 unsigned int blksize = crypto_skcipher_blocksize(tfm);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200431 int ret;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200432
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200433 if (!IS_ALIGNED(req->cryptlen, blksize))
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200434 return -EINVAL;
435
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200436 creq->src_nents = sg_nents_for_len(req->src, req->cryptlen);
LABBE Corentinc22dafb2015-11-04 21:13:33 +0100437 if (creq->src_nents < 0) {
438 dev_err(cesa_dev->dev, "Invalid number of src SG");
439 return creq->src_nents;
440 }
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200441 creq->dst_nents = sg_nents_for_len(req->dst, req->cryptlen);
LABBE Corentinc22dafb2015-11-04 21:13:33 +0100442 if (creq->dst_nents < 0) {
443 dev_err(cesa_dev->dev, "Invalid number of dst SG");
444 return creq->dst_nents;
445 }
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200446
447 mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_OP_CRYPT_ONLY,
448 CESA_SA_DESC_CFG_OP_MSK);
449
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200450 if (cesa_dev->caps->has_tdma)
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200451 ret = mv_cesa_skcipher_dma_req_init(req, tmpl);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200452 else
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200453 ret = mv_cesa_skcipher_std_req_init(req, tmpl);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200454
455 return ret;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200456}
457
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200458static int mv_cesa_skcipher_queue_req(struct skcipher_request *req,
459 struct mv_cesa_op_ctx *tmpl)
Romain Perierbf8f91e2016-06-21 10:08:38 +0200460{
461 int ret;
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200462 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
Romain Perierbf8f91e2016-06-21 10:08:38 +0200463 struct mv_cesa_engine *engine;
464
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200465 ret = mv_cesa_skcipher_req_init(req, tmpl);
Romain Perierbf8f91e2016-06-21 10:08:38 +0200466 if (ret)
467 return ret;
468
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200469 engine = mv_cesa_select_engine(req->cryptlen);
470 mv_cesa_skcipher_prepare(&req->base, engine);
Romain Perierbf8f91e2016-06-21 10:08:38 +0200471
472 ret = mv_cesa_queue_req(&req->base, &creq->base);
473
474 if (mv_cesa_req_needs_cleanup(&req->base, ret))
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200475 mv_cesa_skcipher_cleanup(req);
Romain Perierbf8f91e2016-06-21 10:08:38 +0200476
477 return ret;
478}
479
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200480static int mv_cesa_des_op(struct skcipher_request *req,
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200481 struct mv_cesa_op_ctx *tmpl)
482{
483 struct mv_cesa_des_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200484
485 mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTM_DES,
486 CESA_SA_DESC_CFG_CRYPTM_MSK);
487
488 memcpy(tmpl->ctx.blkcipher.key, ctx->key, DES_KEY_SIZE);
489
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200490 return mv_cesa_skcipher_queue_req(req, tmpl);
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200491}
492
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200493static int mv_cesa_ecb_des_encrypt(struct skcipher_request *req)
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200494{
495 struct mv_cesa_op_ctx tmpl;
496
497 mv_cesa_set_op_cfg(&tmpl,
498 CESA_SA_DESC_CFG_CRYPTCM_ECB |
499 CESA_SA_DESC_CFG_DIR_ENC);
500
501 return mv_cesa_des_op(req, &tmpl);
502}
503
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200504static int mv_cesa_ecb_des_decrypt(struct skcipher_request *req)
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200505{
506 struct mv_cesa_op_ctx tmpl;
507
508 mv_cesa_set_op_cfg(&tmpl,
509 CESA_SA_DESC_CFG_CRYPTCM_ECB |
510 CESA_SA_DESC_CFG_DIR_DEC);
511
512 return mv_cesa_des_op(req, &tmpl);
513}
514
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200515struct skcipher_alg mv_cesa_ecb_des_alg = {
516 .setkey = mv_cesa_des_setkey,
517 .encrypt = mv_cesa_ecb_des_encrypt,
518 .decrypt = mv_cesa_ecb_des_decrypt,
519 .min_keysize = DES_KEY_SIZE,
520 .max_keysize = DES_KEY_SIZE,
521 .base = {
522 .cra_name = "ecb(des)",
523 .cra_driver_name = "mv-ecb-des",
524 .cra_priority = 300,
525 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
526 .cra_blocksize = DES_BLOCK_SIZE,
527 .cra_ctxsize = sizeof(struct mv_cesa_des_ctx),
528 .cra_alignmask = 0,
529 .cra_module = THIS_MODULE,
530 .cra_init = mv_cesa_skcipher_cra_init,
531 .cra_exit = mv_cesa_skcipher_cra_exit,
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200532 },
533};
534
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200535static int mv_cesa_cbc_des_op(struct skcipher_request *req,
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200536 struct mv_cesa_op_ctx *tmpl)
537{
538 mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTCM_CBC,
539 CESA_SA_DESC_CFG_CRYPTCM_MSK);
540
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200541 memcpy(tmpl->ctx.blkcipher.iv, req->iv, DES_BLOCK_SIZE);
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200542
543 return mv_cesa_des_op(req, tmpl);
544}
545
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200546static int mv_cesa_cbc_des_encrypt(struct skcipher_request *req)
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200547{
548 struct mv_cesa_op_ctx tmpl;
549
550 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_ENC);
551
552 return mv_cesa_cbc_des_op(req, &tmpl);
553}
554
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200555static int mv_cesa_cbc_des_decrypt(struct skcipher_request *req)
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200556{
557 struct mv_cesa_op_ctx tmpl;
558
559 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_DEC);
560
561 return mv_cesa_cbc_des_op(req, &tmpl);
562}
563
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200564struct skcipher_alg mv_cesa_cbc_des_alg = {
565 .setkey = mv_cesa_des_setkey,
566 .encrypt = mv_cesa_cbc_des_encrypt,
567 .decrypt = mv_cesa_cbc_des_decrypt,
568 .min_keysize = DES_KEY_SIZE,
569 .max_keysize = DES_KEY_SIZE,
570 .ivsize = DES_BLOCK_SIZE,
571 .base = {
572 .cra_name = "cbc(des)",
573 .cra_driver_name = "mv-cbc-des",
574 .cra_priority = 300,
575 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
576 .cra_blocksize = DES_BLOCK_SIZE,
577 .cra_ctxsize = sizeof(struct mv_cesa_des_ctx),
578 .cra_alignmask = 0,
579 .cra_module = THIS_MODULE,
580 .cra_init = mv_cesa_skcipher_cra_init,
581 .cra_exit = mv_cesa_skcipher_cra_exit,
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200582 },
583};
584
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200585static int mv_cesa_des3_op(struct skcipher_request *req,
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200586 struct mv_cesa_op_ctx *tmpl)
587{
588 struct mv_cesa_des3_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200589
590 mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTM_3DES,
591 CESA_SA_DESC_CFG_CRYPTM_MSK);
592
593 memcpy(tmpl->ctx.blkcipher.key, ctx->key, DES3_EDE_KEY_SIZE);
594
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200595 return mv_cesa_skcipher_queue_req(req, tmpl);
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200596}
597
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200598static int mv_cesa_ecb_des3_ede_encrypt(struct skcipher_request *req)
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200599{
600 struct mv_cesa_op_ctx tmpl;
601
602 mv_cesa_set_op_cfg(&tmpl,
603 CESA_SA_DESC_CFG_CRYPTCM_ECB |
604 CESA_SA_DESC_CFG_3DES_EDE |
605 CESA_SA_DESC_CFG_DIR_ENC);
606
607 return mv_cesa_des3_op(req, &tmpl);
608}
609
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200610static int mv_cesa_ecb_des3_ede_decrypt(struct skcipher_request *req)
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200611{
612 struct mv_cesa_op_ctx tmpl;
613
614 mv_cesa_set_op_cfg(&tmpl,
615 CESA_SA_DESC_CFG_CRYPTCM_ECB |
616 CESA_SA_DESC_CFG_3DES_EDE |
617 CESA_SA_DESC_CFG_DIR_DEC);
618
619 return mv_cesa_des3_op(req, &tmpl);
620}
621
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200622struct skcipher_alg mv_cesa_ecb_des3_ede_alg = {
623 .setkey = mv_cesa_des3_ede_setkey,
624 .encrypt = mv_cesa_ecb_des3_ede_encrypt,
625 .decrypt = mv_cesa_ecb_des3_ede_decrypt,
626 .min_keysize = DES3_EDE_KEY_SIZE,
627 .max_keysize = DES3_EDE_KEY_SIZE,
628 .ivsize = DES3_EDE_BLOCK_SIZE,
629 .base = {
630 .cra_name = "ecb(des3_ede)",
631 .cra_driver_name = "mv-ecb-des3-ede",
632 .cra_priority = 300,
633 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
634 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
635 .cra_ctxsize = sizeof(struct mv_cesa_des3_ctx),
636 .cra_alignmask = 0,
637 .cra_module = THIS_MODULE,
638 .cra_init = mv_cesa_skcipher_cra_init,
639 .cra_exit = mv_cesa_skcipher_cra_exit,
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200640 },
641};
642
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200643static int mv_cesa_cbc_des3_op(struct skcipher_request *req,
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200644 struct mv_cesa_op_ctx *tmpl)
645{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200646 memcpy(tmpl->ctx.blkcipher.iv, req->iv, DES3_EDE_BLOCK_SIZE);
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200647
648 return mv_cesa_des3_op(req, tmpl);
649}
650
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200651static int mv_cesa_cbc_des3_ede_encrypt(struct skcipher_request *req)
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200652{
653 struct mv_cesa_op_ctx tmpl;
654
655 mv_cesa_set_op_cfg(&tmpl,
656 CESA_SA_DESC_CFG_CRYPTCM_CBC |
657 CESA_SA_DESC_CFG_3DES_EDE |
658 CESA_SA_DESC_CFG_DIR_ENC);
659
660 return mv_cesa_cbc_des3_op(req, &tmpl);
661}
662
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200663static int mv_cesa_cbc_des3_ede_decrypt(struct skcipher_request *req)
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200664{
665 struct mv_cesa_op_ctx tmpl;
666
667 mv_cesa_set_op_cfg(&tmpl,
668 CESA_SA_DESC_CFG_CRYPTCM_CBC |
669 CESA_SA_DESC_CFG_3DES_EDE |
670 CESA_SA_DESC_CFG_DIR_DEC);
671
672 return mv_cesa_cbc_des3_op(req, &tmpl);
673}
674
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200675struct skcipher_alg mv_cesa_cbc_des3_ede_alg = {
676 .setkey = mv_cesa_des3_ede_setkey,
677 .encrypt = mv_cesa_cbc_des3_ede_encrypt,
678 .decrypt = mv_cesa_cbc_des3_ede_decrypt,
679 .min_keysize = DES3_EDE_KEY_SIZE,
680 .max_keysize = DES3_EDE_KEY_SIZE,
681 .ivsize = DES3_EDE_BLOCK_SIZE,
682 .base = {
683 .cra_name = "cbc(des3_ede)",
684 .cra_driver_name = "mv-cbc-des3-ede",
685 .cra_priority = 300,
686 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
687 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
688 .cra_ctxsize = sizeof(struct mv_cesa_des3_ctx),
689 .cra_alignmask = 0,
690 .cra_module = THIS_MODULE,
691 .cra_init = mv_cesa_skcipher_cra_init,
692 .cra_exit = mv_cesa_skcipher_cra_exit,
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200693 },
694};
695
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200696static int mv_cesa_aes_op(struct skcipher_request *req,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200697 struct mv_cesa_op_ctx *tmpl)
698{
699 struct mv_cesa_aes_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Romain Perierbf8f91e2016-06-21 10:08:38 +0200700 int i;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200701 u32 *key;
702 u32 cfg;
703
704 cfg = CESA_SA_DESC_CFG_CRYPTM_AES;
705
706 if (mv_cesa_get_op_cfg(tmpl) & CESA_SA_DESC_CFG_DIR_DEC)
707 key = ctx->aes.key_dec;
708 else
709 key = ctx->aes.key_enc;
710
711 for (i = 0; i < ctx->aes.key_length / sizeof(u32); i++)
712 tmpl->ctx.blkcipher.key[i] = cpu_to_le32(key[i]);
713
714 if (ctx->aes.key_length == 24)
715 cfg |= CESA_SA_DESC_CFG_AES_LEN_192;
716 else if (ctx->aes.key_length == 32)
717 cfg |= CESA_SA_DESC_CFG_AES_LEN_256;
718
719 mv_cesa_update_op_cfg(tmpl, cfg,
720 CESA_SA_DESC_CFG_CRYPTM_MSK |
721 CESA_SA_DESC_CFG_AES_LEN_MSK);
722
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200723 return mv_cesa_skcipher_queue_req(req, tmpl);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200724}
725
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200726static int mv_cesa_ecb_aes_encrypt(struct skcipher_request *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200727{
728 struct mv_cesa_op_ctx tmpl;
729
730 mv_cesa_set_op_cfg(&tmpl,
731 CESA_SA_DESC_CFG_CRYPTCM_ECB |
732 CESA_SA_DESC_CFG_DIR_ENC);
733
734 return mv_cesa_aes_op(req, &tmpl);
735}
736
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200737static int mv_cesa_ecb_aes_decrypt(struct skcipher_request *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200738{
739 struct mv_cesa_op_ctx tmpl;
740
741 mv_cesa_set_op_cfg(&tmpl,
742 CESA_SA_DESC_CFG_CRYPTCM_ECB |
743 CESA_SA_DESC_CFG_DIR_DEC);
744
745 return mv_cesa_aes_op(req, &tmpl);
746}
747
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200748struct skcipher_alg mv_cesa_ecb_aes_alg = {
749 .setkey = mv_cesa_aes_setkey,
750 .encrypt = mv_cesa_ecb_aes_encrypt,
751 .decrypt = mv_cesa_ecb_aes_decrypt,
752 .min_keysize = AES_MIN_KEY_SIZE,
753 .max_keysize = AES_MAX_KEY_SIZE,
754 .base = {
755 .cra_name = "ecb(aes)",
756 .cra_driver_name = "mv-ecb-aes",
757 .cra_priority = 300,
758 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
759 .cra_blocksize = AES_BLOCK_SIZE,
760 .cra_ctxsize = sizeof(struct mv_cesa_aes_ctx),
761 .cra_alignmask = 0,
762 .cra_module = THIS_MODULE,
763 .cra_init = mv_cesa_skcipher_cra_init,
764 .cra_exit = mv_cesa_skcipher_cra_exit,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200765 },
766};
767
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200768static int mv_cesa_cbc_aes_op(struct skcipher_request *req,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200769 struct mv_cesa_op_ctx *tmpl)
770{
771 mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTCM_CBC,
772 CESA_SA_DESC_CFG_CRYPTCM_MSK);
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200773 memcpy(tmpl->ctx.blkcipher.iv, req->iv, AES_BLOCK_SIZE);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200774
775 return mv_cesa_aes_op(req, tmpl);
776}
777
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200778static int mv_cesa_cbc_aes_encrypt(struct skcipher_request *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200779{
780 struct mv_cesa_op_ctx tmpl;
781
782 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_ENC);
783
784 return mv_cesa_cbc_aes_op(req, &tmpl);
785}
786
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200787static int mv_cesa_cbc_aes_decrypt(struct skcipher_request *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200788{
789 struct mv_cesa_op_ctx tmpl;
790
791 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_DEC);
792
793 return mv_cesa_cbc_aes_op(req, &tmpl);
794}
795
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200796struct skcipher_alg mv_cesa_cbc_aes_alg = {
797 .setkey = mv_cesa_aes_setkey,
798 .encrypt = mv_cesa_cbc_aes_encrypt,
799 .decrypt = mv_cesa_cbc_aes_decrypt,
800 .min_keysize = AES_MIN_KEY_SIZE,
801 .max_keysize = AES_MAX_KEY_SIZE,
802 .ivsize = AES_BLOCK_SIZE,
803 .base = {
804 .cra_name = "cbc(aes)",
805 .cra_driver_name = "mv-cbc-aes",
806 .cra_priority = 300,
807 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
808 .cra_blocksize = AES_BLOCK_SIZE,
809 .cra_ctxsize = sizeof(struct mv_cesa_aes_ctx),
810 .cra_alignmask = 0,
811 .cra_module = THIS_MODULE,
812 .cra_init = mv_cesa_skcipher_cra_init,
813 .cra_exit = mv_cesa_skcipher_cra_exit,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200814 },
815};