Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 1 | /* |
| 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 BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 16 | #include <crypto/des.h> |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 17 | |
| 18 | #include "cesa.h" |
| 19 | |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 20 | struct mv_cesa_des_ctx { |
| 21 | struct mv_cesa_ctx base; |
| 22 | u8 key[DES_KEY_SIZE]; |
| 23 | }; |
| 24 | |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 25 | struct mv_cesa_des3_ctx { |
| 26 | struct mv_cesa_ctx base; |
| 27 | u8 key[DES3_EDE_KEY_SIZE]; |
| 28 | }; |
| 29 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 30 | struct mv_cesa_aes_ctx { |
| 31 | struct mv_cesa_ctx base; |
| 32 | struct crypto_aes_ctx aes; |
| 33 | }; |
| 34 | |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 35 | struct mv_cesa_ablkcipher_dma_iter { |
| 36 | struct mv_cesa_dma_iter base; |
| 37 | struct mv_cesa_sg_dma_iter src; |
| 38 | struct mv_cesa_sg_dma_iter dst; |
| 39 | }; |
| 40 | |
| 41 | static inline void |
| 42 | mv_cesa_ablkcipher_req_iter_init(struct mv_cesa_ablkcipher_dma_iter *iter, |
| 43 | struct ablkcipher_request *req) |
| 44 | { |
| 45 | mv_cesa_req_dma_iter_init(&iter->base, req->nbytes); |
| 46 | 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 | |
| 50 | static inline bool |
| 51 | mv_cesa_ablkcipher_req_iter_next_op(struct mv_cesa_ablkcipher_dma_iter *iter) |
| 52 | { |
| 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 | |
| 59 | static inline void |
| 60 | mv_cesa_ablkcipher_dma_cleanup(struct ablkcipher_request *req) |
| 61 | { |
| 62 | struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req); |
| 63 | |
| 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 Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 73 | mv_cesa_dma_cleanup(&creq->base); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static inline void mv_cesa_ablkcipher_cleanup(struct ablkcipher_request *req) |
| 77 | { |
| 78 | struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req); |
| 79 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 80 | if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 81 | mv_cesa_ablkcipher_dma_cleanup(req); |
| 82 | } |
| 83 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 84 | static void mv_cesa_ablkcipher_std_step(struct ablkcipher_request *req) |
| 85 | { |
| 86 | struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req); |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 87 | struct mv_cesa_ablkcipher_std_req *sreq = &creq->std; |
| 88 | struct mv_cesa_engine *engine = creq->base.engine; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 89 | size_t len = min_t(size_t, req->nbytes - sreq->offset, |
| 90 | CESA_SA_SRAM_PAYLOAD_SIZE); |
| 91 | |
| 92 | 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 King | 0f3304d | 2015-10-18 18:31:15 +0100 | [diff] [blame] | 101 | memcpy_toio(engine->sram, &sreq->op, sizeof(sreq->op)); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 102 | sreq->skip_ctx = true; |
| 103 | } else { |
Russell King | 0f3304d | 2015-10-18 18:31:15 +0100 | [diff] [blame] | 104 | memcpy_toio(engine->sram, &sreq->op, sizeof(sreq->op.desc)); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | mv_cesa_set_int_mask(engine, CESA_SA_INT_ACCEL0_DONE); |
Russell King | b150856 | 2015-10-18 18:31:00 +0100 | [diff] [blame] | 108 | writel_relaxed(CESA_SA_CFG_PARA_DIS, engine->regs + CESA_SA_CFG); |
Romain Perier | f628308 | 2016-06-21 10:08:32 +0200 | [diff] [blame] | 109 | BUG_ON(readl(engine->regs + CESA_SA_CMD) & |
| 110 | CESA_SA_CMD_EN_CESA_SA_ACCL0); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 111 | writel(CESA_SA_CMD_EN_CESA_SA_ACCL0, engine->regs + CESA_SA_CMD); |
| 112 | } |
| 113 | |
| 114 | static int mv_cesa_ablkcipher_std_process(struct ablkcipher_request *req, |
| 115 | u32 status) |
| 116 | { |
| 117 | struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req); |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 118 | struct mv_cesa_ablkcipher_std_req *sreq = &creq->std; |
| 119 | struct mv_cesa_engine *engine = creq->base.engine; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 120 | size_t len; |
Romain Perier | bac8e80 | 2016-06-21 10:08:34 +0200 | [diff] [blame] | 121 | unsigned int ivsize; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 122 | |
| 123 | len = sg_pcopy_from_buffer(req->dst, creq->dst_nents, |
| 124 | engine->sram + CESA_SA_DATA_SRAM_OFFSET, |
| 125 | sreq->size, sreq->offset); |
| 126 | |
| 127 | sreq->offset += len; |
| 128 | if (sreq->offset < req->nbytes) |
| 129 | return -EINPROGRESS; |
| 130 | |
Romain Perier | bac8e80 | 2016-06-21 10:08:34 +0200 | [diff] [blame] | 131 | ivsize = crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(req)); |
| 132 | memcpy_fromio(req->info, |
| 133 | engine->sram + CESA_SA_CRYPT_IV_SRAM_OFFSET, ivsize); |
| 134 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static int mv_cesa_ablkcipher_process(struct crypto_async_request *req, |
| 139 | u32 status) |
| 140 | { |
| 141 | struct ablkcipher_request *ablkreq = ablkcipher_request_cast(req); |
| 142 | struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(ablkreq); |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 143 | struct mv_cesa_req *basereq = &creq->base; |
Romain Perier | bac8e80 | 2016-06-21 10:08:34 +0200 | [diff] [blame] | 144 | unsigned int ivsize; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 145 | int ret; |
| 146 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 147 | if (mv_cesa_req_get_type(basereq) == CESA_STD_REQ) |
Romain Perier | bac8e80 | 2016-06-21 10:08:34 +0200 | [diff] [blame] | 148 | return mv_cesa_ablkcipher_std_process(ablkreq, status); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 149 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 150 | ret = mv_cesa_dma_process(basereq, status); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 151 | if (ret) |
| 152 | return ret; |
| 153 | |
Romain Perier | bac8e80 | 2016-06-21 10:08:34 +0200 | [diff] [blame] | 154 | ivsize = crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(ablkreq)); |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 155 | memcpy_fromio(ablkreq->info, basereq->chain.last->data, ivsize); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static void mv_cesa_ablkcipher_step(struct crypto_async_request *req) |
| 161 | { |
| 162 | struct ablkcipher_request *ablkreq = ablkcipher_request_cast(req); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 163 | struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(ablkreq); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 164 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 165 | if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ) |
| 166 | mv_cesa_dma_step(&creq->base); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 167 | else |
| 168 | mv_cesa_ablkcipher_std_step(ablkreq); |
| 169 | } |
| 170 | |
| 171 | static inline void |
| 172 | mv_cesa_ablkcipher_dma_prepare(struct ablkcipher_request *req) |
| 173 | { |
| 174 | struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req); |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 175 | struct mv_cesa_req *basereq = &creq->base; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 176 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 177 | mv_cesa_dma_prepare(basereq, basereq->engine); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static inline void |
| 181 | mv_cesa_ablkcipher_std_prepare(struct ablkcipher_request *req) |
| 182 | { |
| 183 | struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req); |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 184 | struct mv_cesa_ablkcipher_std_req *sreq = &creq->std; |
| 185 | struct mv_cesa_engine *engine = creq->base.engine; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 186 | |
| 187 | sreq->size = 0; |
| 188 | sreq->offset = 0; |
| 189 | mv_cesa_adjust_op(engine, &sreq->op); |
Russell King | 0f3304d | 2015-10-18 18:31:15 +0100 | [diff] [blame] | 190 | memcpy_toio(engine->sram, &sreq->op, sizeof(sreq->op)); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static inline void mv_cesa_ablkcipher_prepare(struct crypto_async_request *req, |
| 194 | struct mv_cesa_engine *engine) |
| 195 | { |
| 196 | struct ablkcipher_request *ablkreq = ablkcipher_request_cast(req); |
| 197 | struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(ablkreq); |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 198 | creq->base.engine = engine; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 199 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 200 | if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 201 | mv_cesa_ablkcipher_dma_prepare(ablkreq); |
| 202 | else |
| 203 | mv_cesa_ablkcipher_std_prepare(ablkreq); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static inline void |
| 207 | mv_cesa_ablkcipher_req_cleanup(struct crypto_async_request *req) |
| 208 | { |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 209 | struct ablkcipher_request *ablkreq = ablkcipher_request_cast(req); |
| 210 | |
| 211 | mv_cesa_ablkcipher_cleanup(ablkreq); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static const struct mv_cesa_req_ops mv_cesa_ablkcipher_req_ops = { |
| 215 | .step = mv_cesa_ablkcipher_step, |
| 216 | .process = mv_cesa_ablkcipher_process, |
| 217 | .prepare = mv_cesa_ablkcipher_prepare, |
| 218 | .cleanup = mv_cesa_ablkcipher_req_cleanup, |
| 219 | }; |
| 220 | |
| 221 | static int mv_cesa_ablkcipher_cra_init(struct crypto_tfm *tfm) |
| 222 | { |
| 223 | struct mv_cesa_aes_ctx *ctx = crypto_tfm_ctx(tfm); |
| 224 | |
| 225 | ctx->base.ops = &mv_cesa_ablkcipher_req_ops; |
| 226 | |
| 227 | tfm->crt_ablkcipher.reqsize = sizeof(struct mv_cesa_ablkcipher_req); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static int mv_cesa_aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key, |
| 233 | unsigned int len) |
| 234 | { |
| 235 | struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher); |
| 236 | struct mv_cesa_aes_ctx *ctx = crypto_tfm_ctx(tfm); |
| 237 | int remaining; |
| 238 | int offset; |
| 239 | int ret; |
| 240 | int i; |
| 241 | |
| 242 | ret = crypto_aes_expand_key(&ctx->aes, key, len); |
| 243 | if (ret) { |
| 244 | crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | remaining = (ctx->aes.key_length - 16) / 4; |
| 249 | offset = ctx->aes.key_length + 24 - remaining; |
| 250 | for (i = 0; i < remaining; i++) |
| 251 | ctx->aes.key_dec[4 + i] = |
| 252 | cpu_to_le32(ctx->aes.key_enc[offset + i]); |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 257 | static int mv_cesa_des_setkey(struct crypto_ablkcipher *cipher, const u8 *key, |
| 258 | unsigned int len) |
| 259 | { |
| 260 | struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher); |
| 261 | struct mv_cesa_des_ctx *ctx = crypto_tfm_ctx(tfm); |
| 262 | u32 tmp[DES_EXPKEY_WORDS]; |
| 263 | int ret; |
| 264 | |
| 265 | if (len != DES_KEY_SIZE) { |
| 266 | crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 267 | return -EINVAL; |
| 268 | } |
| 269 | |
| 270 | ret = des_ekey(tmp, key); |
| 271 | if (!ret && (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) { |
| 272 | tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY; |
| 273 | return -EINVAL; |
| 274 | } |
| 275 | |
| 276 | memcpy(ctx->key, key, DES_KEY_SIZE); |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 281 | static int mv_cesa_des3_ede_setkey(struct crypto_ablkcipher *cipher, |
| 282 | const u8 *key, unsigned int len) |
| 283 | { |
| 284 | struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher); |
| 285 | struct mv_cesa_des_ctx *ctx = crypto_tfm_ctx(tfm); |
| 286 | |
| 287 | if (len != DES3_EDE_KEY_SIZE) { |
| 288 | crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 289 | return -EINVAL; |
| 290 | } |
| 291 | |
| 292 | memcpy(ctx->key, key, DES3_EDE_KEY_SIZE); |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 297 | static int mv_cesa_ablkcipher_dma_req_init(struct ablkcipher_request *req, |
| 298 | const struct mv_cesa_op_ctx *op_templ) |
| 299 | { |
| 300 | struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req); |
| 301 | gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? |
| 302 | GFP_KERNEL : GFP_ATOMIC; |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 303 | struct mv_cesa_req *basereq = &creq->base; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 304 | struct mv_cesa_ablkcipher_dma_iter iter; |
| 305 | struct mv_cesa_tdma_chain chain; |
| 306 | bool skip_ctx = false; |
| 307 | int ret; |
Romain Perier | bac8e80 | 2016-06-21 10:08:34 +0200 | [diff] [blame] | 308 | unsigned int ivsize; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 309 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 310 | basereq->chain.first = NULL; |
| 311 | basereq->chain.last = NULL; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 312 | |
| 313 | if (req->src != req->dst) { |
| 314 | ret = dma_map_sg(cesa_dev->dev, req->src, creq->src_nents, |
| 315 | DMA_TO_DEVICE); |
| 316 | if (!ret) |
| 317 | return -ENOMEM; |
| 318 | |
| 319 | ret = dma_map_sg(cesa_dev->dev, req->dst, creq->dst_nents, |
| 320 | DMA_FROM_DEVICE); |
| 321 | if (!ret) { |
| 322 | ret = -ENOMEM; |
| 323 | goto err_unmap_src; |
| 324 | } |
| 325 | } else { |
| 326 | ret = dma_map_sg(cesa_dev->dev, req->src, creq->src_nents, |
| 327 | DMA_BIDIRECTIONAL); |
| 328 | if (!ret) |
| 329 | return -ENOMEM; |
| 330 | } |
| 331 | |
| 332 | mv_cesa_tdma_desc_iter_init(&chain); |
| 333 | mv_cesa_ablkcipher_req_iter_init(&iter, req); |
| 334 | |
| 335 | do { |
| 336 | struct mv_cesa_op_ctx *op; |
| 337 | |
| 338 | op = mv_cesa_dma_add_op(&chain, op_templ, skip_ctx, flags); |
| 339 | if (IS_ERR(op)) { |
| 340 | ret = PTR_ERR(op); |
| 341 | goto err_free_tdma; |
| 342 | } |
| 343 | skip_ctx = true; |
| 344 | |
| 345 | mv_cesa_set_crypt_op_len(op, iter.base.op_len); |
| 346 | |
| 347 | /* Add input transfers */ |
| 348 | ret = mv_cesa_dma_add_op_transfers(&chain, &iter.base, |
| 349 | &iter.src, flags); |
| 350 | if (ret) |
| 351 | goto err_free_tdma; |
| 352 | |
| 353 | /* Add dummy desc to launch the crypto operation */ |
| 354 | ret = mv_cesa_dma_add_dummy_launch(&chain, flags); |
| 355 | if (ret) |
| 356 | goto err_free_tdma; |
| 357 | |
| 358 | /* Add output transfers */ |
| 359 | ret = mv_cesa_dma_add_op_transfers(&chain, &iter.base, |
| 360 | &iter.dst, flags); |
| 361 | if (ret) |
| 362 | goto err_free_tdma; |
| 363 | |
| 364 | } while (mv_cesa_ablkcipher_req_iter_next_op(&iter)); |
| 365 | |
Romain Perier | bac8e80 | 2016-06-21 10:08:34 +0200 | [diff] [blame] | 366 | /* Add output data for IV */ |
| 367 | ivsize = crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(req)); |
| 368 | ret = mv_cesa_dma_add_iv_op(&chain, CESA_SA_CRYPT_IV_SRAM_OFFSET, |
| 369 | ivsize, CESA_TDMA_SRC_IN_SRAM, flags); |
| 370 | |
| 371 | if (ret) |
| 372 | goto err_free_tdma; |
| 373 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 374 | basereq->chain = chain; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 375 | |
| 376 | return 0; |
| 377 | |
| 378 | err_free_tdma: |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 379 | mv_cesa_dma_cleanup(basereq); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 380 | if (req->dst != req->src) |
| 381 | dma_unmap_sg(cesa_dev->dev, req->dst, creq->dst_nents, |
| 382 | DMA_FROM_DEVICE); |
| 383 | |
| 384 | err_unmap_src: |
| 385 | dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents, |
| 386 | req->dst != req->src ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL); |
| 387 | |
| 388 | return ret; |
| 389 | } |
| 390 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 391 | static inline int |
| 392 | mv_cesa_ablkcipher_std_req_init(struct ablkcipher_request *req, |
| 393 | const struct mv_cesa_op_ctx *op_templ) |
| 394 | { |
| 395 | struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req); |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 396 | struct mv_cesa_ablkcipher_std_req *sreq = &creq->std; |
| 397 | struct mv_cesa_req *basereq = &creq->base; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 398 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 399 | sreq->op = *op_templ; |
| 400 | sreq->skip_ctx = false; |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 401 | basereq->chain.first = NULL; |
| 402 | basereq->chain.last = NULL; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static int mv_cesa_ablkcipher_req_init(struct ablkcipher_request *req, |
| 408 | struct mv_cesa_op_ctx *tmpl) |
| 409 | { |
| 410 | struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req); |
| 411 | struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req); |
| 412 | unsigned int blksize = crypto_ablkcipher_blocksize(tfm); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 413 | int ret; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 414 | |
| 415 | if (!IS_ALIGNED(req->nbytes, blksize)) |
| 416 | return -EINVAL; |
| 417 | |
| 418 | creq->src_nents = sg_nents_for_len(req->src, req->nbytes); |
LABBE Corentin | c22dafb | 2015-11-04 21:13:33 +0100 | [diff] [blame] | 419 | if (creq->src_nents < 0) { |
| 420 | dev_err(cesa_dev->dev, "Invalid number of src SG"); |
| 421 | return creq->src_nents; |
| 422 | } |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 423 | creq->dst_nents = sg_nents_for_len(req->dst, req->nbytes); |
LABBE Corentin | c22dafb | 2015-11-04 21:13:33 +0100 | [diff] [blame] | 424 | if (creq->dst_nents < 0) { |
| 425 | dev_err(cesa_dev->dev, "Invalid number of dst SG"); |
| 426 | return creq->dst_nents; |
| 427 | } |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 428 | |
| 429 | mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_OP_CRYPT_ONLY, |
| 430 | CESA_SA_DESC_CFG_OP_MSK); |
| 431 | |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 432 | /* TODO: add a threshold for DMA usage */ |
| 433 | if (cesa_dev->caps->has_tdma) |
| 434 | ret = mv_cesa_ablkcipher_dma_req_init(req, tmpl); |
| 435 | else |
| 436 | ret = mv_cesa_ablkcipher_std_req_init(req, tmpl); |
| 437 | |
| 438 | return ret; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 439 | } |
| 440 | |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 441 | static int mv_cesa_des_op(struct ablkcipher_request *req, |
| 442 | struct mv_cesa_op_ctx *tmpl) |
| 443 | { |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 444 | struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req); |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 445 | struct mv_cesa_des_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 446 | int ret; |
| 447 | |
| 448 | mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTM_DES, |
| 449 | CESA_SA_DESC_CFG_CRYPTM_MSK); |
| 450 | |
| 451 | memcpy(tmpl->ctx.blkcipher.key, ctx->key, DES_KEY_SIZE); |
| 452 | |
| 453 | ret = mv_cesa_ablkcipher_req_init(req, tmpl); |
| 454 | if (ret) |
| 455 | return ret; |
| 456 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 457 | ret = mv_cesa_queue_req(&req->base, &creq->base); |
Thomas Petazzoni | cfcd227 | 2015-09-18 17:25:36 +0200 | [diff] [blame] | 458 | if (mv_cesa_req_needs_cleanup(&req->base, ret)) |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 459 | mv_cesa_ablkcipher_cleanup(req); |
| 460 | |
| 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | static int mv_cesa_ecb_des_encrypt(struct ablkcipher_request *req) |
| 465 | { |
| 466 | struct mv_cesa_op_ctx tmpl; |
| 467 | |
| 468 | mv_cesa_set_op_cfg(&tmpl, |
| 469 | CESA_SA_DESC_CFG_CRYPTCM_ECB | |
| 470 | CESA_SA_DESC_CFG_DIR_ENC); |
| 471 | |
| 472 | return mv_cesa_des_op(req, &tmpl); |
| 473 | } |
| 474 | |
| 475 | static int mv_cesa_ecb_des_decrypt(struct ablkcipher_request *req) |
| 476 | { |
| 477 | struct mv_cesa_op_ctx tmpl; |
| 478 | |
| 479 | mv_cesa_set_op_cfg(&tmpl, |
| 480 | CESA_SA_DESC_CFG_CRYPTCM_ECB | |
| 481 | CESA_SA_DESC_CFG_DIR_DEC); |
| 482 | |
| 483 | return mv_cesa_des_op(req, &tmpl); |
| 484 | } |
| 485 | |
| 486 | struct crypto_alg mv_cesa_ecb_des_alg = { |
| 487 | .cra_name = "ecb(des)", |
| 488 | .cra_driver_name = "mv-ecb-des", |
| 489 | .cra_priority = 300, |
| 490 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
| 491 | CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC, |
| 492 | .cra_blocksize = DES_BLOCK_SIZE, |
| 493 | .cra_ctxsize = sizeof(struct mv_cesa_des_ctx), |
| 494 | .cra_alignmask = 0, |
| 495 | .cra_type = &crypto_ablkcipher_type, |
| 496 | .cra_module = THIS_MODULE, |
| 497 | .cra_init = mv_cesa_ablkcipher_cra_init, |
| 498 | .cra_u = { |
| 499 | .ablkcipher = { |
| 500 | .min_keysize = DES_KEY_SIZE, |
| 501 | .max_keysize = DES_KEY_SIZE, |
| 502 | .setkey = mv_cesa_des_setkey, |
| 503 | .encrypt = mv_cesa_ecb_des_encrypt, |
| 504 | .decrypt = mv_cesa_ecb_des_decrypt, |
| 505 | }, |
| 506 | }, |
| 507 | }; |
| 508 | |
| 509 | static int mv_cesa_cbc_des_op(struct ablkcipher_request *req, |
| 510 | struct mv_cesa_op_ctx *tmpl) |
| 511 | { |
| 512 | mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTCM_CBC, |
| 513 | CESA_SA_DESC_CFG_CRYPTCM_MSK); |
| 514 | |
| 515 | memcpy(tmpl->ctx.blkcipher.iv, req->info, DES_BLOCK_SIZE); |
| 516 | |
| 517 | return mv_cesa_des_op(req, tmpl); |
| 518 | } |
| 519 | |
| 520 | static int mv_cesa_cbc_des_encrypt(struct ablkcipher_request *req) |
| 521 | { |
| 522 | struct mv_cesa_op_ctx tmpl; |
| 523 | |
| 524 | mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_ENC); |
| 525 | |
| 526 | return mv_cesa_cbc_des_op(req, &tmpl); |
| 527 | } |
| 528 | |
| 529 | static int mv_cesa_cbc_des_decrypt(struct ablkcipher_request *req) |
| 530 | { |
| 531 | struct mv_cesa_op_ctx tmpl; |
| 532 | |
| 533 | mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_DEC); |
| 534 | |
| 535 | return mv_cesa_cbc_des_op(req, &tmpl); |
| 536 | } |
| 537 | |
| 538 | struct crypto_alg mv_cesa_cbc_des_alg = { |
| 539 | .cra_name = "cbc(des)", |
| 540 | .cra_driver_name = "mv-cbc-des", |
| 541 | .cra_priority = 300, |
| 542 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
| 543 | CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC, |
| 544 | .cra_blocksize = DES_BLOCK_SIZE, |
| 545 | .cra_ctxsize = sizeof(struct mv_cesa_des_ctx), |
| 546 | .cra_alignmask = 0, |
| 547 | .cra_type = &crypto_ablkcipher_type, |
| 548 | .cra_module = THIS_MODULE, |
| 549 | .cra_init = mv_cesa_ablkcipher_cra_init, |
| 550 | .cra_u = { |
| 551 | .ablkcipher = { |
| 552 | .min_keysize = DES_KEY_SIZE, |
| 553 | .max_keysize = DES_KEY_SIZE, |
| 554 | .ivsize = DES_BLOCK_SIZE, |
| 555 | .setkey = mv_cesa_des_setkey, |
| 556 | .encrypt = mv_cesa_cbc_des_encrypt, |
| 557 | .decrypt = mv_cesa_cbc_des_decrypt, |
| 558 | }, |
| 559 | }, |
| 560 | }; |
| 561 | |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 562 | static int mv_cesa_des3_op(struct ablkcipher_request *req, |
| 563 | struct mv_cesa_op_ctx *tmpl) |
| 564 | { |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 565 | struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req); |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 566 | struct mv_cesa_des3_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 567 | int ret; |
| 568 | |
| 569 | mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTM_3DES, |
| 570 | CESA_SA_DESC_CFG_CRYPTM_MSK); |
| 571 | |
| 572 | memcpy(tmpl->ctx.blkcipher.key, ctx->key, DES3_EDE_KEY_SIZE); |
| 573 | |
| 574 | ret = mv_cesa_ablkcipher_req_init(req, tmpl); |
| 575 | if (ret) |
| 576 | return ret; |
| 577 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 578 | ret = mv_cesa_queue_req(&req->base, &creq->base); |
Thomas Petazzoni | cfcd227 | 2015-09-18 17:25:36 +0200 | [diff] [blame] | 579 | if (mv_cesa_req_needs_cleanup(&req->base, ret)) |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 580 | mv_cesa_ablkcipher_cleanup(req); |
| 581 | |
| 582 | return ret; |
| 583 | } |
| 584 | |
| 585 | static int mv_cesa_ecb_des3_ede_encrypt(struct ablkcipher_request *req) |
| 586 | { |
| 587 | struct mv_cesa_op_ctx tmpl; |
| 588 | |
| 589 | mv_cesa_set_op_cfg(&tmpl, |
| 590 | CESA_SA_DESC_CFG_CRYPTCM_ECB | |
| 591 | CESA_SA_DESC_CFG_3DES_EDE | |
| 592 | CESA_SA_DESC_CFG_DIR_ENC); |
| 593 | |
| 594 | return mv_cesa_des3_op(req, &tmpl); |
| 595 | } |
| 596 | |
| 597 | static int mv_cesa_ecb_des3_ede_decrypt(struct ablkcipher_request *req) |
| 598 | { |
| 599 | struct mv_cesa_op_ctx tmpl; |
| 600 | |
| 601 | mv_cesa_set_op_cfg(&tmpl, |
| 602 | CESA_SA_DESC_CFG_CRYPTCM_ECB | |
| 603 | CESA_SA_DESC_CFG_3DES_EDE | |
| 604 | CESA_SA_DESC_CFG_DIR_DEC); |
| 605 | |
| 606 | return mv_cesa_des3_op(req, &tmpl); |
| 607 | } |
| 608 | |
| 609 | struct crypto_alg mv_cesa_ecb_des3_ede_alg = { |
| 610 | .cra_name = "ecb(des3_ede)", |
| 611 | .cra_driver_name = "mv-ecb-des3-ede", |
| 612 | .cra_priority = 300, |
| 613 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
| 614 | CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC, |
| 615 | .cra_blocksize = DES3_EDE_BLOCK_SIZE, |
| 616 | .cra_ctxsize = sizeof(struct mv_cesa_des3_ctx), |
| 617 | .cra_alignmask = 0, |
| 618 | .cra_type = &crypto_ablkcipher_type, |
| 619 | .cra_module = THIS_MODULE, |
| 620 | .cra_init = mv_cesa_ablkcipher_cra_init, |
| 621 | .cra_u = { |
| 622 | .ablkcipher = { |
| 623 | .min_keysize = DES3_EDE_KEY_SIZE, |
| 624 | .max_keysize = DES3_EDE_KEY_SIZE, |
| 625 | .ivsize = DES3_EDE_BLOCK_SIZE, |
| 626 | .setkey = mv_cesa_des3_ede_setkey, |
| 627 | .encrypt = mv_cesa_ecb_des3_ede_encrypt, |
| 628 | .decrypt = mv_cesa_ecb_des3_ede_decrypt, |
| 629 | }, |
| 630 | }, |
| 631 | }; |
| 632 | |
| 633 | static int mv_cesa_cbc_des3_op(struct ablkcipher_request *req, |
| 634 | struct mv_cesa_op_ctx *tmpl) |
| 635 | { |
| 636 | memcpy(tmpl->ctx.blkcipher.iv, req->info, DES3_EDE_BLOCK_SIZE); |
| 637 | |
| 638 | return mv_cesa_des3_op(req, tmpl); |
| 639 | } |
| 640 | |
| 641 | static int mv_cesa_cbc_des3_ede_encrypt(struct ablkcipher_request *req) |
| 642 | { |
| 643 | struct mv_cesa_op_ctx tmpl; |
| 644 | |
| 645 | mv_cesa_set_op_cfg(&tmpl, |
| 646 | CESA_SA_DESC_CFG_CRYPTCM_CBC | |
| 647 | CESA_SA_DESC_CFG_3DES_EDE | |
| 648 | CESA_SA_DESC_CFG_DIR_ENC); |
| 649 | |
| 650 | return mv_cesa_cbc_des3_op(req, &tmpl); |
| 651 | } |
| 652 | |
| 653 | static int mv_cesa_cbc_des3_ede_decrypt(struct ablkcipher_request *req) |
| 654 | { |
| 655 | struct mv_cesa_op_ctx tmpl; |
| 656 | |
| 657 | mv_cesa_set_op_cfg(&tmpl, |
| 658 | CESA_SA_DESC_CFG_CRYPTCM_CBC | |
| 659 | CESA_SA_DESC_CFG_3DES_EDE | |
| 660 | CESA_SA_DESC_CFG_DIR_DEC); |
| 661 | |
| 662 | return mv_cesa_cbc_des3_op(req, &tmpl); |
| 663 | } |
| 664 | |
| 665 | struct crypto_alg mv_cesa_cbc_des3_ede_alg = { |
| 666 | .cra_name = "cbc(des3_ede)", |
| 667 | .cra_driver_name = "mv-cbc-des3-ede", |
| 668 | .cra_priority = 300, |
| 669 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
| 670 | CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC, |
| 671 | .cra_blocksize = DES3_EDE_BLOCK_SIZE, |
| 672 | .cra_ctxsize = sizeof(struct mv_cesa_des3_ctx), |
| 673 | .cra_alignmask = 0, |
| 674 | .cra_type = &crypto_ablkcipher_type, |
| 675 | .cra_module = THIS_MODULE, |
| 676 | .cra_init = mv_cesa_ablkcipher_cra_init, |
| 677 | .cra_u = { |
| 678 | .ablkcipher = { |
| 679 | .min_keysize = DES3_EDE_KEY_SIZE, |
| 680 | .max_keysize = DES3_EDE_KEY_SIZE, |
| 681 | .ivsize = DES3_EDE_BLOCK_SIZE, |
| 682 | .setkey = mv_cesa_des3_ede_setkey, |
| 683 | .encrypt = mv_cesa_cbc_des3_ede_encrypt, |
| 684 | .decrypt = mv_cesa_cbc_des3_ede_decrypt, |
| 685 | }, |
| 686 | }, |
| 687 | }; |
| 688 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 689 | static int mv_cesa_aes_op(struct ablkcipher_request *req, |
| 690 | struct mv_cesa_op_ctx *tmpl) |
| 691 | { |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 692 | struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 693 | struct mv_cesa_aes_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 694 | int ret, i; |
| 695 | 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 | |
| 717 | ret = mv_cesa_ablkcipher_req_init(req, tmpl); |
| 718 | if (ret) |
| 719 | return ret; |
| 720 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame^] | 721 | ret = mv_cesa_queue_req(&req->base, &creq->base); |
Thomas Petazzoni | cfcd227 | 2015-09-18 17:25:36 +0200 | [diff] [blame] | 722 | if (mv_cesa_req_needs_cleanup(&req->base, ret)) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 723 | mv_cesa_ablkcipher_cleanup(req); |
| 724 | |
| 725 | return ret; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | static int mv_cesa_ecb_aes_encrypt(struct ablkcipher_request *req) |
| 729 | { |
| 730 | struct mv_cesa_op_ctx tmpl; |
| 731 | |
| 732 | mv_cesa_set_op_cfg(&tmpl, |
| 733 | CESA_SA_DESC_CFG_CRYPTCM_ECB | |
| 734 | CESA_SA_DESC_CFG_DIR_ENC); |
| 735 | |
| 736 | return mv_cesa_aes_op(req, &tmpl); |
| 737 | } |
| 738 | |
| 739 | static int mv_cesa_ecb_aes_decrypt(struct ablkcipher_request *req) |
| 740 | { |
| 741 | struct mv_cesa_op_ctx tmpl; |
| 742 | |
| 743 | mv_cesa_set_op_cfg(&tmpl, |
| 744 | CESA_SA_DESC_CFG_CRYPTCM_ECB | |
| 745 | CESA_SA_DESC_CFG_DIR_DEC); |
| 746 | |
| 747 | return mv_cesa_aes_op(req, &tmpl); |
| 748 | } |
| 749 | |
| 750 | struct crypto_alg mv_cesa_ecb_aes_alg = { |
| 751 | .cra_name = "ecb(aes)", |
| 752 | .cra_driver_name = "mv-ecb-aes", |
| 753 | .cra_priority = 300, |
| 754 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
| 755 | CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC, |
| 756 | .cra_blocksize = AES_BLOCK_SIZE, |
| 757 | .cra_ctxsize = sizeof(struct mv_cesa_aes_ctx), |
| 758 | .cra_alignmask = 0, |
| 759 | .cra_type = &crypto_ablkcipher_type, |
| 760 | .cra_module = THIS_MODULE, |
| 761 | .cra_init = mv_cesa_ablkcipher_cra_init, |
| 762 | .cra_u = { |
| 763 | .ablkcipher = { |
| 764 | .min_keysize = AES_MIN_KEY_SIZE, |
| 765 | .max_keysize = AES_MAX_KEY_SIZE, |
| 766 | .setkey = mv_cesa_aes_setkey, |
| 767 | .encrypt = mv_cesa_ecb_aes_encrypt, |
| 768 | .decrypt = mv_cesa_ecb_aes_decrypt, |
| 769 | }, |
| 770 | }, |
| 771 | }; |
| 772 | |
| 773 | static int mv_cesa_cbc_aes_op(struct ablkcipher_request *req, |
| 774 | struct mv_cesa_op_ctx *tmpl) |
| 775 | { |
| 776 | mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTCM_CBC, |
| 777 | CESA_SA_DESC_CFG_CRYPTCM_MSK); |
| 778 | memcpy(tmpl->ctx.blkcipher.iv, req->info, AES_BLOCK_SIZE); |
| 779 | |
| 780 | return mv_cesa_aes_op(req, tmpl); |
| 781 | } |
| 782 | |
| 783 | static int mv_cesa_cbc_aes_encrypt(struct ablkcipher_request *req) |
| 784 | { |
| 785 | struct mv_cesa_op_ctx tmpl; |
| 786 | |
| 787 | mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_ENC); |
| 788 | |
| 789 | return mv_cesa_cbc_aes_op(req, &tmpl); |
| 790 | } |
| 791 | |
| 792 | static int mv_cesa_cbc_aes_decrypt(struct ablkcipher_request *req) |
| 793 | { |
| 794 | struct mv_cesa_op_ctx tmpl; |
| 795 | |
| 796 | mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_DEC); |
| 797 | |
| 798 | return mv_cesa_cbc_aes_op(req, &tmpl); |
| 799 | } |
| 800 | |
| 801 | struct crypto_alg mv_cesa_cbc_aes_alg = { |
| 802 | .cra_name = "cbc(aes)", |
| 803 | .cra_driver_name = "mv-cbc-aes", |
| 804 | .cra_priority = 300, |
| 805 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
| 806 | CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC, |
| 807 | .cra_blocksize = AES_BLOCK_SIZE, |
| 808 | .cra_ctxsize = sizeof(struct mv_cesa_aes_ctx), |
| 809 | .cra_alignmask = 0, |
| 810 | .cra_type = &crypto_ablkcipher_type, |
| 811 | .cra_module = THIS_MODULE, |
| 812 | .cra_init = mv_cesa_ablkcipher_cra_init, |
| 813 | .cra_u = { |
| 814 | .ablkcipher = { |
| 815 | .min_keysize = AES_MIN_KEY_SIZE, |
| 816 | .max_keysize = AES_MAX_KEY_SIZE, |
| 817 | .ivsize = AES_BLOCK_SIZE, |
| 818 | .setkey = mv_cesa_aes_setkey, |
| 819 | .encrypt = mv_cesa_cbc_aes_encrypt, |
| 820 | .decrypt = mv_cesa_cbc_aes_decrypt, |
| 821 | }, |
| 822 | }, |
| 823 | }; |