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