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