blob: f4321f3c077727370b7b6a2e2b09ef52b5b501cd [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Boris BREZILLONf63601f2015-06-18 15:46:20 +02002/*
3 * Cipher algorithms supported by the CESA: DES, 3DES and AES.
4 *
5 * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
6 * Author: Arnaud Ebalard <arno@natisbad.org>
7 *
8 * This work is based on an initial version written by
9 * Sebastian Andrzej Siewior < sebastian at breakpoint dot cc >
Boris BREZILLONf63601f2015-06-18 15:46:20 +020010 */
11
12#include <crypto/aes.h>
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +020013#include <crypto/des.h>
Boris BREZILLONf63601f2015-06-18 15:46:20 +020014
15#include "cesa.h"
16
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +020017struct mv_cesa_des_ctx {
18 struct mv_cesa_ctx base;
19 u8 key[DES_KEY_SIZE];
20};
21
Arnaud Ebalard4ada4832015-06-18 15:46:23 +020022struct mv_cesa_des3_ctx {
23 struct mv_cesa_ctx base;
24 u8 key[DES3_EDE_KEY_SIZE];
25};
26
Boris BREZILLONf63601f2015-06-18 15:46:20 +020027struct mv_cesa_aes_ctx {
28 struct mv_cesa_ctx base;
29 struct crypto_aes_ctx aes;
30};
31
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020032struct mv_cesa_skcipher_dma_iter {
Boris BREZILLONdb509a42015-06-18 15:46:21 +020033 struct mv_cesa_dma_iter base;
34 struct mv_cesa_sg_dma_iter src;
35 struct mv_cesa_sg_dma_iter dst;
36};
37
38static inline void
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020039mv_cesa_skcipher_req_iter_init(struct mv_cesa_skcipher_dma_iter *iter,
40 struct skcipher_request *req)
Boris BREZILLONdb509a42015-06-18 15:46:21 +020041{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020042 mv_cesa_req_dma_iter_init(&iter->base, req->cryptlen);
Boris BREZILLONdb509a42015-06-18 15:46:21 +020043 mv_cesa_sg_dma_iter_init(&iter->src, req->src, DMA_TO_DEVICE);
44 mv_cesa_sg_dma_iter_init(&iter->dst, req->dst, DMA_FROM_DEVICE);
45}
46
47static inline bool
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020048mv_cesa_skcipher_req_iter_next_op(struct mv_cesa_skcipher_dma_iter *iter)
Boris BREZILLONdb509a42015-06-18 15:46:21 +020049{
50 iter->src.op_offset = 0;
51 iter->dst.op_offset = 0;
52
53 return mv_cesa_req_dma_iter_next_op(&iter->base);
54}
55
56static inline void
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020057mv_cesa_skcipher_dma_cleanup(struct skcipher_request *req)
Boris BREZILLONdb509a42015-06-18 15:46:21 +020058{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020059 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +020060
61 if (req->dst != req->src) {
62 dma_unmap_sg(cesa_dev->dev, req->dst, creq->dst_nents,
63 DMA_FROM_DEVICE);
64 dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents,
65 DMA_TO_DEVICE);
66 } else {
67 dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents,
68 DMA_BIDIRECTIONAL);
69 }
Romain Perier53da7402016-06-21 10:08:35 +020070 mv_cesa_dma_cleanup(&creq->base);
Boris BREZILLONdb509a42015-06-18 15:46:21 +020071}
72
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020073static inline void mv_cesa_skcipher_cleanup(struct skcipher_request *req)
Boris BREZILLONdb509a42015-06-18 15:46:21 +020074{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020075 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +020076
Romain Perier53da7402016-06-21 10:08:35 +020077 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020078 mv_cesa_skcipher_dma_cleanup(req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +020079}
80
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020081static void mv_cesa_skcipher_std_step(struct skcipher_request *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +020082{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020083 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
84 struct mv_cesa_skcipher_std_req *sreq = &creq->std;
Romain Perier53da7402016-06-21 10:08:35 +020085 struct mv_cesa_engine *engine = creq->base.engine;
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +020086 size_t len = min_t(size_t, req->cryptlen - sreq->offset,
Boris BREZILLONf63601f2015-06-18 15:46:20 +020087 CESA_SA_SRAM_PAYLOAD_SIZE);
88
Romain Perier2786cee2016-06-21 10:08:37 +020089 mv_cesa_adjust_op(engine, &sreq->op);
90 memcpy_toio(engine->sram, &sreq->op, sizeof(sreq->op));
91
Boris BREZILLONf63601f2015-06-18 15:46:20 +020092 len = sg_pcopy_to_buffer(req->src, creq->src_nents,
93 engine->sram + CESA_SA_DATA_SRAM_OFFSET,
94 len, sreq->offset);
95
96 sreq->size = len;
97 mv_cesa_set_crypt_op_len(&sreq->op, len);
98
99 /* FIXME: only update enc_len field */
100 if (!sreq->skip_ctx) {
Russell King0f3304d2015-10-18 18:31:15 +0100101 memcpy_toio(engine->sram, &sreq->op, sizeof(sreq->op));
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200102 sreq->skip_ctx = true;
103 } else {
Russell King0f3304d2015-10-18 18:31:15 +0100104 memcpy_toio(engine->sram, &sreq->op, sizeof(sreq->op.desc));
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200105 }
106
107 mv_cesa_set_int_mask(engine, CESA_SA_INT_ACCEL0_DONE);
Russell Kingb1508562015-10-18 18:31:00 +0100108 writel_relaxed(CESA_SA_CFG_PARA_DIS, engine->regs + CESA_SA_CFG);
Romain Perierf6283082016-06-21 10:08:32 +0200109 BUG_ON(readl(engine->regs + CESA_SA_CMD) &
110 CESA_SA_CMD_EN_CESA_SA_ACCL0);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200111 writel(CESA_SA_CMD_EN_CESA_SA_ACCL0, engine->regs + CESA_SA_CMD);
112}
113
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200114static int mv_cesa_skcipher_std_process(struct skcipher_request *req,
115 u32 status)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200116{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200117 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
118 struct mv_cesa_skcipher_std_req *sreq = &creq->std;
Romain Perier53da7402016-06-21 10:08:35 +0200119 struct mv_cesa_engine *engine = creq->base.engine;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200120 size_t len;
121
122 len = sg_pcopy_from_buffer(req->dst, creq->dst_nents,
123 engine->sram + CESA_SA_DATA_SRAM_OFFSET,
124 sreq->size, sreq->offset);
125
126 sreq->offset += len;
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200127 if (sreq->offset < req->cryptlen)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200128 return -EINPROGRESS;
129
130 return 0;
131}
132
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200133static int mv_cesa_skcipher_process(struct crypto_async_request *req,
134 u32 status)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200135{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200136 struct skcipher_request *skreq = skcipher_request_cast(req);
137 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq);
Romain Perier53da7402016-06-21 10:08:35 +0200138 struct mv_cesa_req *basereq = &creq->base;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200139
Romain Perier53da7402016-06-21 10:08:35 +0200140 if (mv_cesa_req_get_type(basereq) == CESA_STD_REQ)
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200141 return mv_cesa_skcipher_std_process(skreq, status);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200142
Romain Perier8cf740a2016-07-28 11:59:43 +0200143 return mv_cesa_dma_process(basereq, status);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200144}
145
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200146static void mv_cesa_skcipher_step(struct crypto_async_request *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200147{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200148 struct skcipher_request *skreq = skcipher_request_cast(req);
149 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200150
Romain Perier53da7402016-06-21 10:08:35 +0200151 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
152 mv_cesa_dma_step(&creq->base);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200153 else
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200154 mv_cesa_skcipher_std_step(skreq);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200155}
156
157static inline void
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200158mv_cesa_skcipher_dma_prepare(struct skcipher_request *req)
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200159{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200160 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
Romain Perier53da7402016-06-21 10:08:35 +0200161 struct mv_cesa_req *basereq = &creq->base;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200162
Romain Perier53da7402016-06-21 10:08:35 +0200163 mv_cesa_dma_prepare(basereq, basereq->engine);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200164}
165
166static inline void
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200167mv_cesa_skcipher_std_prepare(struct skcipher_request *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200168{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200169 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
170 struct mv_cesa_skcipher_std_req *sreq = &creq->std;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200171
172 sreq->size = 0;
173 sreq->offset = 0;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200174}
175
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200176static inline void mv_cesa_skcipher_prepare(struct crypto_async_request *req,
177 struct mv_cesa_engine *engine)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200178{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200179 struct skcipher_request *skreq = skcipher_request_cast(req);
180 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq);
Romain Perier53da7402016-06-21 10:08:35 +0200181 creq->base.engine = engine;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200182
Romain Perier53da7402016-06-21 10:08:35 +0200183 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200184 mv_cesa_skcipher_dma_prepare(skreq);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200185 else
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200186 mv_cesa_skcipher_std_prepare(skreq);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200187}
188
189static inline void
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200190mv_cesa_skcipher_req_cleanup(struct crypto_async_request *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200191{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200192 struct skcipher_request *skreq = skcipher_request_cast(req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200193
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200194 mv_cesa_skcipher_cleanup(skreq);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200195}
196
Romain Perier1bf66822016-06-21 10:08:36 +0200197static void
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200198mv_cesa_skcipher_complete(struct crypto_async_request *req)
Romain Perier1bf66822016-06-21 10:08:36 +0200199{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200200 struct skcipher_request *skreq = skcipher_request_cast(req);
201 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq);
Romain Perier1bf66822016-06-21 10:08:36 +0200202 struct mv_cesa_engine *engine = creq->base.engine;
203 unsigned int ivsize;
204
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200205 atomic_sub(skreq->cryptlen, &engine->load);
206 ivsize = crypto_skcipher_ivsize(crypto_skcipher_reqtfm(skreq));
Romain Perier1bf66822016-06-21 10:08:36 +0200207
208 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ) {
209 struct mv_cesa_req *basereq;
210
211 basereq = &creq->base;
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200212 memcpy(skreq->iv, basereq->chain.last->op->ctx.blkcipher.iv,
Romain Perier0c996202016-10-05 09:56:32 +0200213 ivsize);
Romain Perier1bf66822016-06-21 10:08:36 +0200214 } else {
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200215 memcpy_fromio(skreq->iv,
Romain Perier1bf66822016-06-21 10:08:36 +0200216 engine->sram + CESA_SA_CRYPT_IV_SRAM_OFFSET,
217 ivsize);
218 }
219}
220
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200221static const struct mv_cesa_req_ops mv_cesa_skcipher_req_ops = {
222 .step = mv_cesa_skcipher_step,
223 .process = mv_cesa_skcipher_process,
224 .cleanup = mv_cesa_skcipher_req_cleanup,
225 .complete = mv_cesa_skcipher_complete,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200226};
227
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200228static void mv_cesa_skcipher_cra_exit(struct crypto_tfm *tfm)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200229{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200230 void *ctx = crypto_tfm_ctx(tfm);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200231
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200232 memzero_explicit(ctx, tfm->__crt_alg->cra_ctxsize);
233}
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200234
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200235static int mv_cesa_skcipher_cra_init(struct crypto_tfm *tfm)
236{
237 struct mv_cesa_ctx *ctx = crypto_tfm_ctx(tfm);
238
239 ctx->ops = &mv_cesa_skcipher_req_ops;
240
241 crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
242 sizeof(struct mv_cesa_skcipher_req));
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200243
244 return 0;
245}
246
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200247static int mv_cesa_aes_setkey(struct crypto_skcipher *cipher, const u8 *key,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200248 unsigned int len)
249{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200250 struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200251 struct mv_cesa_aes_ctx *ctx = crypto_tfm_ctx(tfm);
252 int remaining;
253 int offset;
254 int ret;
255 int i;
256
257 ret = crypto_aes_expand_key(&ctx->aes, key, len);
258 if (ret) {
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200259 crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200260 return ret;
261 }
262
263 remaining = (ctx->aes.key_length - 16) / 4;
264 offset = ctx->aes.key_length + 24 - remaining;
265 for (i = 0; i < remaining; i++)
266 ctx->aes.key_dec[4 + i] =
267 cpu_to_le32(ctx->aes.key_enc[offset + i]);
268
269 return 0;
270}
271
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200272static int mv_cesa_des_setkey(struct crypto_skcipher *cipher, const u8 *key,
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200273 unsigned int len)
274{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200275 struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200276 struct mv_cesa_des_ctx *ctx = crypto_tfm_ctx(tfm);
277 u32 tmp[DES_EXPKEY_WORDS];
278 int ret;
279
280 if (len != DES_KEY_SIZE) {
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200281 crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200282 return -EINVAL;
283 }
284
285 ret = des_ekey(tmp, key);
Eric Biggers231baec2019-01-18 22:48:00 -0800286 if (!ret && (tfm->crt_flags & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)) {
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200287 tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
288 return -EINVAL;
289 }
290
291 memcpy(ctx->key, key, DES_KEY_SIZE);
292
293 return 0;
294}
295
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200296static int mv_cesa_des3_ede_setkey(struct crypto_skcipher *cipher,
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200297 const u8 *key, unsigned int len)
298{
Herbert Xucc4bd9f2019-04-11 16:51:12 +0800299 struct mv_cesa_des_ctx *ctx = crypto_skcipher_ctx(cipher);
300 int err;
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200301
Herbert Xucc4bd9f2019-04-11 16:51:12 +0800302 err = des3_verify_key(cipher, key);
303 if (unlikely(err))
304 return err;
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200305
306 memcpy(ctx->key, key, DES3_EDE_KEY_SIZE);
307
308 return 0;
309}
310
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200311static int mv_cesa_skcipher_dma_req_init(struct skcipher_request *req,
312 const struct mv_cesa_op_ctx *op_templ)
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200313{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200314 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200315 gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
316 GFP_KERNEL : GFP_ATOMIC;
Romain Perier53da7402016-06-21 10:08:35 +0200317 struct mv_cesa_req *basereq = &creq->base;
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200318 struct mv_cesa_skcipher_dma_iter iter;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200319 bool skip_ctx = false;
320 int ret;
321
Romain Perier53da7402016-06-21 10:08:35 +0200322 basereq->chain.first = NULL;
323 basereq->chain.last = NULL;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200324
325 if (req->src != req->dst) {
326 ret = dma_map_sg(cesa_dev->dev, req->src, creq->src_nents,
327 DMA_TO_DEVICE);
328 if (!ret)
329 return -ENOMEM;
330
331 ret = dma_map_sg(cesa_dev->dev, req->dst, creq->dst_nents,
332 DMA_FROM_DEVICE);
333 if (!ret) {
334 ret = -ENOMEM;
335 goto err_unmap_src;
336 }
337 } else {
338 ret = dma_map_sg(cesa_dev->dev, req->src, creq->src_nents,
339 DMA_BIDIRECTIONAL);
340 if (!ret)
341 return -ENOMEM;
342 }
343
Romain Perierec38f822016-07-22 14:40:39 +0200344 mv_cesa_tdma_desc_iter_init(&basereq->chain);
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200345 mv_cesa_skcipher_req_iter_init(&iter, req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200346
347 do {
348 struct mv_cesa_op_ctx *op;
349
Romain Perierec38f822016-07-22 14:40:39 +0200350 op = mv_cesa_dma_add_op(&basereq->chain, op_templ, skip_ctx, flags);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200351 if (IS_ERR(op)) {
352 ret = PTR_ERR(op);
353 goto err_free_tdma;
354 }
355 skip_ctx = true;
356
357 mv_cesa_set_crypt_op_len(op, iter.base.op_len);
358
359 /* Add input transfers */
Romain Perierec38f822016-07-22 14:40:39 +0200360 ret = mv_cesa_dma_add_op_transfers(&basereq->chain, &iter.base,
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200361 &iter.src, flags);
362 if (ret)
363 goto err_free_tdma;
364
365 /* Add dummy desc to launch the crypto operation */
Romain Perierec38f822016-07-22 14:40:39 +0200366 ret = mv_cesa_dma_add_dummy_launch(&basereq->chain, flags);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200367 if (ret)
368 goto err_free_tdma;
369
370 /* Add output transfers */
Romain Perierec38f822016-07-22 14:40:39 +0200371 ret = mv_cesa_dma_add_op_transfers(&basereq->chain, &iter.base,
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200372 &iter.dst, flags);
373 if (ret)
374 goto err_free_tdma;
375
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200376 } while (mv_cesa_skcipher_req_iter_next_op(&iter));
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200377
Romain Perierbac8e802016-06-21 10:08:34 +0200378 /* Add output data for IV */
Romain Perier0c996202016-10-05 09:56:32 +0200379 ret = mv_cesa_dma_add_result_op(&basereq->chain, CESA_SA_CFG_SRAM_OFFSET,
380 CESA_SA_DATA_SRAM_OFFSET,
381 CESA_TDMA_SRC_IN_SRAM, flags);
Romain Perierbac8e802016-06-21 10:08:34 +0200382
383 if (ret)
384 goto err_free_tdma;
385
Romain Perier85030c52016-06-21 10:08:39 +0200386 basereq->chain.last->flags |= CESA_TDMA_END_OF_REQ;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200387
388 return 0;
389
390err_free_tdma:
Romain Perier53da7402016-06-21 10:08:35 +0200391 mv_cesa_dma_cleanup(basereq);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200392 if (req->dst != req->src)
393 dma_unmap_sg(cesa_dev->dev, req->dst, creq->dst_nents,
394 DMA_FROM_DEVICE);
395
396err_unmap_src:
397 dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents,
398 req->dst != req->src ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL);
399
400 return ret;
401}
402
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200403static inline int
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200404mv_cesa_skcipher_std_req_init(struct skcipher_request *req,
405 const struct mv_cesa_op_ctx *op_templ)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200406{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200407 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
408 struct mv_cesa_skcipher_std_req *sreq = &creq->std;
Romain Perier53da7402016-06-21 10:08:35 +0200409 struct mv_cesa_req *basereq = &creq->base;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200410
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200411 sreq->op = *op_templ;
412 sreq->skip_ctx = false;
Romain Perier53da7402016-06-21 10:08:35 +0200413 basereq->chain.first = NULL;
414 basereq->chain.last = NULL;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200415
416 return 0;
417}
418
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200419static int mv_cesa_skcipher_req_init(struct skcipher_request *req,
420 struct mv_cesa_op_ctx *tmpl)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200421{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200422 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
423 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
424 unsigned int blksize = crypto_skcipher_blocksize(tfm);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200425 int ret;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200426
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200427 if (!IS_ALIGNED(req->cryptlen, blksize))
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200428 return -EINVAL;
429
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200430 creq->src_nents = sg_nents_for_len(req->src, req->cryptlen);
LABBE Corentinc22dafb2015-11-04 21:13:33 +0100431 if (creq->src_nents < 0) {
432 dev_err(cesa_dev->dev, "Invalid number of src SG");
433 return creq->src_nents;
434 }
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200435 creq->dst_nents = sg_nents_for_len(req->dst, req->cryptlen);
LABBE Corentinc22dafb2015-11-04 21:13:33 +0100436 if (creq->dst_nents < 0) {
437 dev_err(cesa_dev->dev, "Invalid number of dst SG");
438 return creq->dst_nents;
439 }
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200440
441 mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_OP_CRYPT_ONLY,
442 CESA_SA_DESC_CFG_OP_MSK);
443
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200444 if (cesa_dev->caps->has_tdma)
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200445 ret = mv_cesa_skcipher_dma_req_init(req, tmpl);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200446 else
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200447 ret = mv_cesa_skcipher_std_req_init(req, tmpl);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200448
449 return ret;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200450}
451
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200452static int mv_cesa_skcipher_queue_req(struct skcipher_request *req,
453 struct mv_cesa_op_ctx *tmpl)
Romain Perierbf8f91e2016-06-21 10:08:38 +0200454{
455 int ret;
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200456 struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
Romain Perierbf8f91e2016-06-21 10:08:38 +0200457 struct mv_cesa_engine *engine;
458
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200459 ret = mv_cesa_skcipher_req_init(req, tmpl);
Romain Perierbf8f91e2016-06-21 10:08:38 +0200460 if (ret)
461 return ret;
462
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200463 engine = mv_cesa_select_engine(req->cryptlen);
464 mv_cesa_skcipher_prepare(&req->base, engine);
Romain Perierbf8f91e2016-06-21 10:08:38 +0200465
466 ret = mv_cesa_queue_req(&req->base, &creq->base);
467
468 if (mv_cesa_req_needs_cleanup(&req->base, ret))
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200469 mv_cesa_skcipher_cleanup(req);
Romain Perierbf8f91e2016-06-21 10:08:38 +0200470
471 return ret;
472}
473
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200474static int mv_cesa_des_op(struct skcipher_request *req,
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200475 struct mv_cesa_op_ctx *tmpl)
476{
477 struct mv_cesa_des_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200478
479 mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTM_DES,
480 CESA_SA_DESC_CFG_CRYPTM_MSK);
481
482 memcpy(tmpl->ctx.blkcipher.key, ctx->key, DES_KEY_SIZE);
483
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200484 return mv_cesa_skcipher_queue_req(req, tmpl);
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200485}
486
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200487static int mv_cesa_ecb_des_encrypt(struct skcipher_request *req)
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200488{
489 struct mv_cesa_op_ctx tmpl;
490
491 mv_cesa_set_op_cfg(&tmpl,
492 CESA_SA_DESC_CFG_CRYPTCM_ECB |
493 CESA_SA_DESC_CFG_DIR_ENC);
494
495 return mv_cesa_des_op(req, &tmpl);
496}
497
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200498static int mv_cesa_ecb_des_decrypt(struct skcipher_request *req)
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200499{
500 struct mv_cesa_op_ctx tmpl;
501
502 mv_cesa_set_op_cfg(&tmpl,
503 CESA_SA_DESC_CFG_CRYPTCM_ECB |
504 CESA_SA_DESC_CFG_DIR_DEC);
505
506 return mv_cesa_des_op(req, &tmpl);
507}
508
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200509struct skcipher_alg mv_cesa_ecb_des_alg = {
510 .setkey = mv_cesa_des_setkey,
511 .encrypt = mv_cesa_ecb_des_encrypt,
512 .decrypt = mv_cesa_ecb_des_decrypt,
513 .min_keysize = DES_KEY_SIZE,
514 .max_keysize = DES_KEY_SIZE,
515 .base = {
516 .cra_name = "ecb(des)",
517 .cra_driver_name = "mv-ecb-des",
518 .cra_priority = 300,
519 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
520 .cra_blocksize = DES_BLOCK_SIZE,
521 .cra_ctxsize = sizeof(struct mv_cesa_des_ctx),
522 .cra_alignmask = 0,
523 .cra_module = THIS_MODULE,
524 .cra_init = mv_cesa_skcipher_cra_init,
525 .cra_exit = mv_cesa_skcipher_cra_exit,
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200526 },
527};
528
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200529static int mv_cesa_cbc_des_op(struct skcipher_request *req,
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200530 struct mv_cesa_op_ctx *tmpl)
531{
532 mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTCM_CBC,
533 CESA_SA_DESC_CFG_CRYPTCM_MSK);
534
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200535 memcpy(tmpl->ctx.blkcipher.iv, req->iv, DES_BLOCK_SIZE);
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200536
537 return mv_cesa_des_op(req, tmpl);
538}
539
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200540static int mv_cesa_cbc_des_encrypt(struct skcipher_request *req)
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200541{
542 struct mv_cesa_op_ctx tmpl;
543
544 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_ENC);
545
546 return mv_cesa_cbc_des_op(req, &tmpl);
547}
548
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200549static int mv_cesa_cbc_des_decrypt(struct skcipher_request *req)
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200550{
551 struct mv_cesa_op_ctx tmpl;
552
553 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_DEC);
554
555 return mv_cesa_cbc_des_op(req, &tmpl);
556}
557
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200558struct skcipher_alg mv_cesa_cbc_des_alg = {
559 .setkey = mv_cesa_des_setkey,
560 .encrypt = mv_cesa_cbc_des_encrypt,
561 .decrypt = mv_cesa_cbc_des_decrypt,
562 .min_keysize = DES_KEY_SIZE,
563 .max_keysize = DES_KEY_SIZE,
564 .ivsize = DES_BLOCK_SIZE,
565 .base = {
566 .cra_name = "cbc(des)",
567 .cra_driver_name = "mv-cbc-des",
568 .cra_priority = 300,
569 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
570 .cra_blocksize = DES_BLOCK_SIZE,
571 .cra_ctxsize = sizeof(struct mv_cesa_des_ctx),
572 .cra_alignmask = 0,
573 .cra_module = THIS_MODULE,
574 .cra_init = mv_cesa_skcipher_cra_init,
575 .cra_exit = mv_cesa_skcipher_cra_exit,
Boris BREZILLON7b3aaaa2015-06-18 15:46:22 +0200576 },
577};
578
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200579static int mv_cesa_des3_op(struct skcipher_request *req,
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200580 struct mv_cesa_op_ctx *tmpl)
581{
582 struct mv_cesa_des3_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200583
584 mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTM_3DES,
585 CESA_SA_DESC_CFG_CRYPTM_MSK);
586
587 memcpy(tmpl->ctx.blkcipher.key, ctx->key, DES3_EDE_KEY_SIZE);
588
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200589 return mv_cesa_skcipher_queue_req(req, tmpl);
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200590}
591
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200592static int mv_cesa_ecb_des3_ede_encrypt(struct skcipher_request *req)
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200593{
594 struct mv_cesa_op_ctx tmpl;
595
596 mv_cesa_set_op_cfg(&tmpl,
597 CESA_SA_DESC_CFG_CRYPTCM_ECB |
598 CESA_SA_DESC_CFG_3DES_EDE |
599 CESA_SA_DESC_CFG_DIR_ENC);
600
601 return mv_cesa_des3_op(req, &tmpl);
602}
603
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200604static int mv_cesa_ecb_des3_ede_decrypt(struct skcipher_request *req)
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200605{
606 struct mv_cesa_op_ctx tmpl;
607
608 mv_cesa_set_op_cfg(&tmpl,
609 CESA_SA_DESC_CFG_CRYPTCM_ECB |
610 CESA_SA_DESC_CFG_3DES_EDE |
611 CESA_SA_DESC_CFG_DIR_DEC);
612
613 return mv_cesa_des3_op(req, &tmpl);
614}
615
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200616struct skcipher_alg mv_cesa_ecb_des3_ede_alg = {
617 .setkey = mv_cesa_des3_ede_setkey,
618 .encrypt = mv_cesa_ecb_des3_ede_encrypt,
619 .decrypt = mv_cesa_ecb_des3_ede_decrypt,
620 .min_keysize = DES3_EDE_KEY_SIZE,
621 .max_keysize = DES3_EDE_KEY_SIZE,
622 .ivsize = DES3_EDE_BLOCK_SIZE,
623 .base = {
624 .cra_name = "ecb(des3_ede)",
625 .cra_driver_name = "mv-ecb-des3-ede",
626 .cra_priority = 300,
627 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
628 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
629 .cra_ctxsize = sizeof(struct mv_cesa_des3_ctx),
630 .cra_alignmask = 0,
631 .cra_module = THIS_MODULE,
632 .cra_init = mv_cesa_skcipher_cra_init,
633 .cra_exit = mv_cesa_skcipher_cra_exit,
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200634 },
635};
636
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200637static int mv_cesa_cbc_des3_op(struct skcipher_request *req,
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200638 struct mv_cesa_op_ctx *tmpl)
639{
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200640 memcpy(tmpl->ctx.blkcipher.iv, req->iv, DES3_EDE_BLOCK_SIZE);
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200641
642 return mv_cesa_des3_op(req, tmpl);
643}
644
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200645static int mv_cesa_cbc_des3_ede_encrypt(struct skcipher_request *req)
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200646{
647 struct mv_cesa_op_ctx tmpl;
648
649 mv_cesa_set_op_cfg(&tmpl,
650 CESA_SA_DESC_CFG_CRYPTCM_CBC |
651 CESA_SA_DESC_CFG_3DES_EDE |
652 CESA_SA_DESC_CFG_DIR_ENC);
653
654 return mv_cesa_cbc_des3_op(req, &tmpl);
655}
656
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200657static int mv_cesa_cbc_des3_ede_decrypt(struct skcipher_request *req)
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200658{
659 struct mv_cesa_op_ctx tmpl;
660
661 mv_cesa_set_op_cfg(&tmpl,
662 CESA_SA_DESC_CFG_CRYPTCM_CBC |
663 CESA_SA_DESC_CFG_3DES_EDE |
664 CESA_SA_DESC_CFG_DIR_DEC);
665
666 return mv_cesa_cbc_des3_op(req, &tmpl);
667}
668
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200669struct skcipher_alg mv_cesa_cbc_des3_ede_alg = {
670 .setkey = mv_cesa_des3_ede_setkey,
671 .encrypt = mv_cesa_cbc_des3_ede_encrypt,
672 .decrypt = mv_cesa_cbc_des3_ede_decrypt,
673 .min_keysize = DES3_EDE_KEY_SIZE,
674 .max_keysize = DES3_EDE_KEY_SIZE,
675 .ivsize = DES3_EDE_BLOCK_SIZE,
676 .base = {
677 .cra_name = "cbc(des3_ede)",
678 .cra_driver_name = "mv-cbc-des3-ede",
679 .cra_priority = 300,
680 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
681 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
682 .cra_ctxsize = sizeof(struct mv_cesa_des3_ctx),
683 .cra_alignmask = 0,
684 .cra_module = THIS_MODULE,
685 .cra_init = mv_cesa_skcipher_cra_init,
686 .cra_exit = mv_cesa_skcipher_cra_exit,
Arnaud Ebalard4ada4832015-06-18 15:46:23 +0200687 },
688};
689
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200690static int mv_cesa_aes_op(struct skcipher_request *req,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200691 struct mv_cesa_op_ctx *tmpl)
692{
693 struct mv_cesa_aes_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Romain Perierbf8f91e2016-06-21 10:08:38 +0200694 int i;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200695 u32 *key;
696 u32 cfg;
697
698 cfg = CESA_SA_DESC_CFG_CRYPTM_AES;
699
700 if (mv_cesa_get_op_cfg(tmpl) & CESA_SA_DESC_CFG_DIR_DEC)
701 key = ctx->aes.key_dec;
702 else
703 key = ctx->aes.key_enc;
704
705 for (i = 0; i < ctx->aes.key_length / sizeof(u32); i++)
706 tmpl->ctx.blkcipher.key[i] = cpu_to_le32(key[i]);
707
708 if (ctx->aes.key_length == 24)
709 cfg |= CESA_SA_DESC_CFG_AES_LEN_192;
710 else if (ctx->aes.key_length == 32)
711 cfg |= CESA_SA_DESC_CFG_AES_LEN_256;
712
713 mv_cesa_update_op_cfg(tmpl, cfg,
714 CESA_SA_DESC_CFG_CRYPTM_MSK |
715 CESA_SA_DESC_CFG_AES_LEN_MSK);
716
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200717 return mv_cesa_skcipher_queue_req(req, tmpl);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200718}
719
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200720static int mv_cesa_ecb_aes_encrypt(struct skcipher_request *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200721{
722 struct mv_cesa_op_ctx tmpl;
723
724 mv_cesa_set_op_cfg(&tmpl,
725 CESA_SA_DESC_CFG_CRYPTCM_ECB |
726 CESA_SA_DESC_CFG_DIR_ENC);
727
728 return mv_cesa_aes_op(req, &tmpl);
729}
730
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200731static int mv_cesa_ecb_aes_decrypt(struct skcipher_request *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200732{
733 struct mv_cesa_op_ctx tmpl;
734
735 mv_cesa_set_op_cfg(&tmpl,
736 CESA_SA_DESC_CFG_CRYPTCM_ECB |
737 CESA_SA_DESC_CFG_DIR_DEC);
738
739 return mv_cesa_aes_op(req, &tmpl);
740}
741
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200742struct skcipher_alg mv_cesa_ecb_aes_alg = {
743 .setkey = mv_cesa_aes_setkey,
744 .encrypt = mv_cesa_ecb_aes_encrypt,
745 .decrypt = mv_cesa_ecb_aes_decrypt,
746 .min_keysize = AES_MIN_KEY_SIZE,
747 .max_keysize = AES_MAX_KEY_SIZE,
748 .base = {
749 .cra_name = "ecb(aes)",
750 .cra_driver_name = "mv-ecb-aes",
751 .cra_priority = 300,
752 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
753 .cra_blocksize = AES_BLOCK_SIZE,
754 .cra_ctxsize = sizeof(struct mv_cesa_aes_ctx),
755 .cra_alignmask = 0,
756 .cra_module = THIS_MODULE,
757 .cra_init = mv_cesa_skcipher_cra_init,
758 .cra_exit = mv_cesa_skcipher_cra_exit,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200759 },
760};
761
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200762static int mv_cesa_cbc_aes_op(struct skcipher_request *req,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200763 struct mv_cesa_op_ctx *tmpl)
764{
765 mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTCM_CBC,
766 CESA_SA_DESC_CFG_CRYPTCM_MSK);
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200767 memcpy(tmpl->ctx.blkcipher.iv, req->iv, AES_BLOCK_SIZE);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200768
769 return mv_cesa_aes_op(req, tmpl);
770}
771
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200772static int mv_cesa_cbc_aes_encrypt(struct skcipher_request *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200773{
774 struct mv_cesa_op_ctx tmpl;
775
776 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_ENC);
777
778 return mv_cesa_cbc_aes_op(req, &tmpl);
779}
780
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200781static int mv_cesa_cbc_aes_decrypt(struct skcipher_request *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200782{
783 struct mv_cesa_op_ctx tmpl;
784
785 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_DEC);
786
787 return mv_cesa_cbc_aes_op(req, &tmpl);
788}
789
Boris BREZILLONe6cd5bf2017-10-13 15:30:32 +0200790struct skcipher_alg mv_cesa_cbc_aes_alg = {
791 .setkey = mv_cesa_aes_setkey,
792 .encrypt = mv_cesa_cbc_aes_encrypt,
793 .decrypt = mv_cesa_cbc_aes_decrypt,
794 .min_keysize = AES_MIN_KEY_SIZE,
795 .max_keysize = AES_MAX_KEY_SIZE,
796 .ivsize = AES_BLOCK_SIZE,
797 .base = {
798 .cra_name = "cbc(aes)",
799 .cra_driver_name = "mv-cbc-aes",
800 .cra_priority = 300,
801 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
802 .cra_blocksize = AES_BLOCK_SIZE,
803 .cra_ctxsize = sizeof(struct mv_cesa_aes_ctx),
804 .cra_alignmask = 0,
805 .cra_module = THIS_MODULE,
806 .cra_init = mv_cesa_skcipher_cra_init,
807 .cra_exit = mv_cesa_skcipher_cra_exit,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200808 },
809};