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 | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 35 | struct mv_cesa_skcipher_dma_iter { |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 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 |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 42 | mv_cesa_skcipher_req_iter_init(struct mv_cesa_skcipher_dma_iter *iter, |
| 43 | struct skcipher_request *req) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 44 | { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 45 | mv_cesa_req_dma_iter_init(&iter->base, req->cryptlen); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 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 |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 51 | mv_cesa_skcipher_req_iter_next_op(struct mv_cesa_skcipher_dma_iter *iter) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 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 |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 60 | mv_cesa_skcipher_dma_cleanup(struct skcipher_request *req) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 61 | { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 62 | struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 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 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 76 | static inline void mv_cesa_skcipher_cleanup(struct skcipher_request *req) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 77 | { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 78 | struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 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 | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 81 | mv_cesa_skcipher_dma_cleanup(req); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 84 | static void mv_cesa_skcipher_std_step(struct skcipher_request *req) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 85 | { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 86 | struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req); |
| 87 | struct mv_cesa_skcipher_std_req *sreq = &creq->std; |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 88 | struct mv_cesa_engine *engine = creq->base.engine; |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 89 | size_t len = min_t(size_t, req->cryptlen - sreq->offset, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 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 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 117 | static int mv_cesa_skcipher_std_process(struct skcipher_request *req, |
| 118 | u32 status) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 119 | { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 120 | struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req); |
| 121 | struct mv_cesa_skcipher_std_req *sreq = &creq->std; |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 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; |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 130 | if (sreq->offset < req->cryptlen) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 131 | return -EINPROGRESS; |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 136 | static int mv_cesa_skcipher_process(struct crypto_async_request *req, |
| 137 | u32 status) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 138 | { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 139 | struct skcipher_request *skreq = skcipher_request_cast(req); |
| 140 | struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq); |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 141 | struct mv_cesa_req *basereq = &creq->base; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 142 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 143 | if (mv_cesa_req_get_type(basereq) == CESA_STD_REQ) |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 144 | return mv_cesa_skcipher_std_process(skreq, status); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 145 | |
Romain Perier | 8cf740a | 2016-07-28 11:59:43 +0200 | [diff] [blame] | 146 | return mv_cesa_dma_process(basereq, status); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 149 | static void mv_cesa_skcipher_step(struct crypto_async_request *req) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 150 | { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 151 | struct skcipher_request *skreq = skcipher_request_cast(req); |
| 152 | struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 153 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 154 | if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ) |
| 155 | mv_cesa_dma_step(&creq->base); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 156 | else |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 157 | mv_cesa_skcipher_std_step(skreq); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static inline void |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 161 | mv_cesa_skcipher_dma_prepare(struct skcipher_request *req) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 162 | { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 163 | struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req); |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 164 | struct mv_cesa_req *basereq = &creq->base; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 165 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 166 | mv_cesa_dma_prepare(basereq, basereq->engine); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static inline void |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 170 | mv_cesa_skcipher_std_prepare(struct skcipher_request *req) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 171 | { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 172 | struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req); |
| 173 | struct mv_cesa_skcipher_std_req *sreq = &creq->std; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 174 | |
| 175 | sreq->size = 0; |
| 176 | sreq->offset = 0; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 179 | static inline void mv_cesa_skcipher_prepare(struct crypto_async_request *req, |
| 180 | struct mv_cesa_engine *engine) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 181 | { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 182 | struct skcipher_request *skreq = skcipher_request_cast(req); |
| 183 | struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq); |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 184 | creq->base.engine = engine; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 185 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 186 | if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ) |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 187 | mv_cesa_skcipher_dma_prepare(skreq); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 188 | else |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 189 | mv_cesa_skcipher_std_prepare(skreq); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static inline void |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 193 | mv_cesa_skcipher_req_cleanup(struct crypto_async_request *req) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 194 | { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 195 | struct skcipher_request *skreq = skcipher_request_cast(req); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 196 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 197 | mv_cesa_skcipher_cleanup(skreq); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Romain Perier | 1bf6682 | 2016-06-21 10:08:36 +0200 | [diff] [blame] | 200 | static void |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 201 | mv_cesa_skcipher_complete(struct crypto_async_request *req) |
Romain Perier | 1bf6682 | 2016-06-21 10:08:36 +0200 | [diff] [blame] | 202 | { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 203 | struct skcipher_request *skreq = skcipher_request_cast(req); |
| 204 | struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq); |
Romain Perier | 1bf6682 | 2016-06-21 10:08:36 +0200 | [diff] [blame] | 205 | struct mv_cesa_engine *engine = creq->base.engine; |
| 206 | unsigned int ivsize; |
| 207 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 208 | atomic_sub(skreq->cryptlen, &engine->load); |
| 209 | ivsize = crypto_skcipher_ivsize(crypto_skcipher_reqtfm(skreq)); |
Romain Perier | 1bf6682 | 2016-06-21 10:08:36 +0200 | [diff] [blame] | 210 | |
| 211 | if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ) { |
| 212 | struct mv_cesa_req *basereq; |
| 213 | |
| 214 | basereq = &creq->base; |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 215 | memcpy(skreq->iv, basereq->chain.last->op->ctx.blkcipher.iv, |
Romain Perier | 0c99620 | 2016-10-05 09:56:32 +0200 | [diff] [blame] | 216 | ivsize); |
Romain Perier | 1bf6682 | 2016-06-21 10:08:36 +0200 | [diff] [blame] | 217 | } else { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 218 | memcpy_fromio(skreq->iv, |
Romain Perier | 1bf6682 | 2016-06-21 10:08:36 +0200 | [diff] [blame] | 219 | engine->sram + CESA_SA_CRYPT_IV_SRAM_OFFSET, |
| 220 | ivsize); |
| 221 | } |
| 222 | } |
| 223 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 224 | static const struct mv_cesa_req_ops mv_cesa_skcipher_req_ops = { |
| 225 | .step = mv_cesa_skcipher_step, |
| 226 | .process = mv_cesa_skcipher_process, |
| 227 | .cleanup = mv_cesa_skcipher_req_cleanup, |
| 228 | .complete = mv_cesa_skcipher_complete, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 229 | }; |
| 230 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 231 | static void mv_cesa_skcipher_cra_exit(struct crypto_tfm *tfm) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 232 | { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 233 | void *ctx = crypto_tfm_ctx(tfm); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 234 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 235 | memzero_explicit(ctx, tfm->__crt_alg->cra_ctxsize); |
| 236 | } |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 237 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 238 | static int mv_cesa_skcipher_cra_init(struct crypto_tfm *tfm) |
| 239 | { |
| 240 | struct mv_cesa_ctx *ctx = crypto_tfm_ctx(tfm); |
| 241 | |
| 242 | ctx->ops = &mv_cesa_skcipher_req_ops; |
| 243 | |
| 244 | crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm), |
| 245 | sizeof(struct mv_cesa_skcipher_req)); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 250 | static int mv_cesa_aes_setkey(struct crypto_skcipher *cipher, const u8 *key, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 251 | unsigned int len) |
| 252 | { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 253 | struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 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) { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 262 | crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 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 | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 275 | static int mv_cesa_des_setkey(struct crypto_skcipher *cipher, const u8 *key, |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 276 | unsigned int len) |
| 277 | { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 278 | struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher); |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 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) { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 284 | crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN); |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 285 | return -EINVAL; |
| 286 | } |
| 287 | |
| 288 | ret = des_ekey(tmp, key); |
Eric Biggers | 231baec | 2019-01-18 22:48:00 -0800 | [diff] [blame] | 289 | if (!ret && (tfm->crt_flags & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)) { |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 290 | tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY; |
| 291 | return -EINVAL; |
| 292 | } |
| 293 | |
| 294 | memcpy(ctx->key, key, DES_KEY_SIZE); |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 299 | static int mv_cesa_des3_ede_setkey(struct crypto_skcipher *cipher, |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 300 | const u8 *key, unsigned int len) |
| 301 | { |
Herbert Xu | cc4bd9f | 2019-04-11 16:51:12 +0800 | [diff] [blame] | 302 | struct mv_cesa_des_ctx *ctx = crypto_skcipher_ctx(cipher); |
| 303 | int err; |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 304 | |
Herbert Xu | cc4bd9f | 2019-04-11 16:51:12 +0800 | [diff] [blame] | 305 | err = des3_verify_key(cipher, key); |
| 306 | if (unlikely(err)) |
| 307 | return err; |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 308 | |
| 309 | memcpy(ctx->key, key, DES3_EDE_KEY_SIZE); |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 314 | static int mv_cesa_skcipher_dma_req_init(struct skcipher_request *req, |
| 315 | const struct mv_cesa_op_ctx *op_templ) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 316 | { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 317 | struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 318 | gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? |
| 319 | GFP_KERNEL : GFP_ATOMIC; |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 320 | struct mv_cesa_req *basereq = &creq->base; |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 321 | struct mv_cesa_skcipher_dma_iter iter; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 322 | bool skip_ctx = false; |
| 323 | int ret; |
| 324 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 325 | basereq->chain.first = NULL; |
| 326 | basereq->chain.last = NULL; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 327 | |
| 328 | if (req->src != req->dst) { |
| 329 | ret = dma_map_sg(cesa_dev->dev, req->src, creq->src_nents, |
| 330 | DMA_TO_DEVICE); |
| 331 | if (!ret) |
| 332 | return -ENOMEM; |
| 333 | |
| 334 | ret = dma_map_sg(cesa_dev->dev, req->dst, creq->dst_nents, |
| 335 | DMA_FROM_DEVICE); |
| 336 | if (!ret) { |
| 337 | ret = -ENOMEM; |
| 338 | goto err_unmap_src; |
| 339 | } |
| 340 | } else { |
| 341 | ret = dma_map_sg(cesa_dev->dev, req->src, creq->src_nents, |
| 342 | DMA_BIDIRECTIONAL); |
| 343 | if (!ret) |
| 344 | return -ENOMEM; |
| 345 | } |
| 346 | |
Romain Perier | ec38f82 | 2016-07-22 14:40:39 +0200 | [diff] [blame] | 347 | mv_cesa_tdma_desc_iter_init(&basereq->chain); |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 348 | mv_cesa_skcipher_req_iter_init(&iter, req); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 349 | |
| 350 | do { |
| 351 | struct mv_cesa_op_ctx *op; |
| 352 | |
Romain Perier | ec38f82 | 2016-07-22 14:40:39 +0200 | [diff] [blame] | 353 | 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] | 354 | if (IS_ERR(op)) { |
| 355 | ret = PTR_ERR(op); |
| 356 | goto err_free_tdma; |
| 357 | } |
| 358 | skip_ctx = true; |
| 359 | |
| 360 | mv_cesa_set_crypt_op_len(op, iter.base.op_len); |
| 361 | |
| 362 | /* Add input transfers */ |
Romain Perier | ec38f82 | 2016-07-22 14:40:39 +0200 | [diff] [blame] | 363 | ret = mv_cesa_dma_add_op_transfers(&basereq->chain, &iter.base, |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 364 | &iter.src, flags); |
| 365 | if (ret) |
| 366 | goto err_free_tdma; |
| 367 | |
| 368 | /* Add dummy desc to launch the crypto operation */ |
Romain Perier | ec38f82 | 2016-07-22 14:40:39 +0200 | [diff] [blame] | 369 | ret = mv_cesa_dma_add_dummy_launch(&basereq->chain, flags); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 370 | if (ret) |
| 371 | goto err_free_tdma; |
| 372 | |
| 373 | /* Add output transfers */ |
Romain Perier | ec38f82 | 2016-07-22 14:40:39 +0200 | [diff] [blame] | 374 | ret = mv_cesa_dma_add_op_transfers(&basereq->chain, &iter.base, |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 375 | &iter.dst, flags); |
| 376 | if (ret) |
| 377 | goto err_free_tdma; |
| 378 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 379 | } while (mv_cesa_skcipher_req_iter_next_op(&iter)); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 380 | |
Romain Perier | bac8e80 | 2016-06-21 10:08:34 +0200 | [diff] [blame] | 381 | /* Add output data for IV */ |
Romain Perier | 0c99620 | 2016-10-05 09:56:32 +0200 | [diff] [blame] | 382 | ret = mv_cesa_dma_add_result_op(&basereq->chain, CESA_SA_CFG_SRAM_OFFSET, |
| 383 | CESA_SA_DATA_SRAM_OFFSET, |
| 384 | CESA_TDMA_SRC_IN_SRAM, flags); |
Romain Perier | bac8e80 | 2016-06-21 10:08:34 +0200 | [diff] [blame] | 385 | |
| 386 | if (ret) |
| 387 | goto err_free_tdma; |
| 388 | |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 389 | basereq->chain.last->flags |= CESA_TDMA_END_OF_REQ; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 390 | |
| 391 | return 0; |
| 392 | |
| 393 | err_free_tdma: |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 394 | mv_cesa_dma_cleanup(basereq); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 395 | if (req->dst != req->src) |
| 396 | dma_unmap_sg(cesa_dev->dev, req->dst, creq->dst_nents, |
| 397 | DMA_FROM_DEVICE); |
| 398 | |
| 399 | err_unmap_src: |
| 400 | dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents, |
| 401 | req->dst != req->src ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL); |
| 402 | |
| 403 | return ret; |
| 404 | } |
| 405 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 406 | static inline int |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 407 | mv_cesa_skcipher_std_req_init(struct skcipher_request *req, |
| 408 | const struct mv_cesa_op_ctx *op_templ) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 409 | { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 410 | struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req); |
| 411 | struct mv_cesa_skcipher_std_req *sreq = &creq->std; |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 412 | struct mv_cesa_req *basereq = &creq->base; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 413 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 414 | sreq->op = *op_templ; |
| 415 | sreq->skip_ctx = false; |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 416 | basereq->chain.first = NULL; |
| 417 | basereq->chain.last = NULL; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 422 | static int mv_cesa_skcipher_req_init(struct skcipher_request *req, |
| 423 | struct mv_cesa_op_ctx *tmpl) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 424 | { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 425 | struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req); |
| 426 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 427 | unsigned int blksize = crypto_skcipher_blocksize(tfm); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 428 | int ret; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 429 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 430 | if (!IS_ALIGNED(req->cryptlen, blksize)) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 431 | return -EINVAL; |
| 432 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 433 | creq->src_nents = sg_nents_for_len(req->src, req->cryptlen); |
LABBE Corentin | c22dafb | 2015-11-04 21:13:33 +0100 | [diff] [blame] | 434 | if (creq->src_nents < 0) { |
| 435 | dev_err(cesa_dev->dev, "Invalid number of src SG"); |
| 436 | return creq->src_nents; |
| 437 | } |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 438 | creq->dst_nents = sg_nents_for_len(req->dst, req->cryptlen); |
LABBE Corentin | c22dafb | 2015-11-04 21:13:33 +0100 | [diff] [blame] | 439 | if (creq->dst_nents < 0) { |
| 440 | dev_err(cesa_dev->dev, "Invalid number of dst SG"); |
| 441 | return creq->dst_nents; |
| 442 | } |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 443 | |
| 444 | mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_OP_CRYPT_ONLY, |
| 445 | CESA_SA_DESC_CFG_OP_MSK); |
| 446 | |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 447 | if (cesa_dev->caps->has_tdma) |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 448 | ret = mv_cesa_skcipher_dma_req_init(req, tmpl); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 449 | else |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 450 | ret = mv_cesa_skcipher_std_req_init(req, tmpl); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 451 | |
| 452 | return ret; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 453 | } |
| 454 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 455 | static int mv_cesa_skcipher_queue_req(struct skcipher_request *req, |
| 456 | struct mv_cesa_op_ctx *tmpl) |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 457 | { |
| 458 | int ret; |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 459 | struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req); |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 460 | struct mv_cesa_engine *engine; |
| 461 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 462 | ret = mv_cesa_skcipher_req_init(req, tmpl); |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 463 | if (ret) |
| 464 | return ret; |
| 465 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 466 | engine = mv_cesa_select_engine(req->cryptlen); |
| 467 | mv_cesa_skcipher_prepare(&req->base, engine); |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 468 | |
| 469 | ret = mv_cesa_queue_req(&req->base, &creq->base); |
| 470 | |
| 471 | if (mv_cesa_req_needs_cleanup(&req->base, ret)) |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 472 | mv_cesa_skcipher_cleanup(req); |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 473 | |
| 474 | return ret; |
| 475 | } |
| 476 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 477 | static int mv_cesa_des_op(struct skcipher_request *req, |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 478 | struct mv_cesa_op_ctx *tmpl) |
| 479 | { |
| 480 | struct mv_cesa_des_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 481 | |
| 482 | mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTM_DES, |
| 483 | CESA_SA_DESC_CFG_CRYPTM_MSK); |
| 484 | |
| 485 | memcpy(tmpl->ctx.blkcipher.key, ctx->key, DES_KEY_SIZE); |
| 486 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 487 | return mv_cesa_skcipher_queue_req(req, tmpl); |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 488 | } |
| 489 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 490 | static int mv_cesa_ecb_des_encrypt(struct skcipher_request *req) |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 491 | { |
| 492 | struct mv_cesa_op_ctx tmpl; |
| 493 | |
| 494 | mv_cesa_set_op_cfg(&tmpl, |
| 495 | CESA_SA_DESC_CFG_CRYPTCM_ECB | |
| 496 | CESA_SA_DESC_CFG_DIR_ENC); |
| 497 | |
| 498 | return mv_cesa_des_op(req, &tmpl); |
| 499 | } |
| 500 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 501 | static int mv_cesa_ecb_des_decrypt(struct skcipher_request *req) |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 502 | { |
| 503 | struct mv_cesa_op_ctx tmpl; |
| 504 | |
| 505 | mv_cesa_set_op_cfg(&tmpl, |
| 506 | CESA_SA_DESC_CFG_CRYPTCM_ECB | |
| 507 | CESA_SA_DESC_CFG_DIR_DEC); |
| 508 | |
| 509 | return mv_cesa_des_op(req, &tmpl); |
| 510 | } |
| 511 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 512 | struct skcipher_alg mv_cesa_ecb_des_alg = { |
| 513 | .setkey = mv_cesa_des_setkey, |
| 514 | .encrypt = mv_cesa_ecb_des_encrypt, |
| 515 | .decrypt = mv_cesa_ecb_des_decrypt, |
| 516 | .min_keysize = DES_KEY_SIZE, |
| 517 | .max_keysize = DES_KEY_SIZE, |
| 518 | .base = { |
| 519 | .cra_name = "ecb(des)", |
| 520 | .cra_driver_name = "mv-ecb-des", |
| 521 | .cra_priority = 300, |
| 522 | .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC, |
| 523 | .cra_blocksize = DES_BLOCK_SIZE, |
| 524 | .cra_ctxsize = sizeof(struct mv_cesa_des_ctx), |
| 525 | .cra_alignmask = 0, |
| 526 | .cra_module = THIS_MODULE, |
| 527 | .cra_init = mv_cesa_skcipher_cra_init, |
| 528 | .cra_exit = mv_cesa_skcipher_cra_exit, |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 529 | }, |
| 530 | }; |
| 531 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 532 | static int mv_cesa_cbc_des_op(struct skcipher_request *req, |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 533 | struct mv_cesa_op_ctx *tmpl) |
| 534 | { |
| 535 | mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTCM_CBC, |
| 536 | CESA_SA_DESC_CFG_CRYPTCM_MSK); |
| 537 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 538 | memcpy(tmpl->ctx.blkcipher.iv, req->iv, DES_BLOCK_SIZE); |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 539 | |
| 540 | return mv_cesa_des_op(req, tmpl); |
| 541 | } |
| 542 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 543 | static int mv_cesa_cbc_des_encrypt(struct skcipher_request *req) |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 544 | { |
| 545 | struct mv_cesa_op_ctx tmpl; |
| 546 | |
| 547 | mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_ENC); |
| 548 | |
| 549 | return mv_cesa_cbc_des_op(req, &tmpl); |
| 550 | } |
| 551 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 552 | static int mv_cesa_cbc_des_decrypt(struct skcipher_request *req) |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 553 | { |
| 554 | struct mv_cesa_op_ctx tmpl; |
| 555 | |
| 556 | mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_DEC); |
| 557 | |
| 558 | return mv_cesa_cbc_des_op(req, &tmpl); |
| 559 | } |
| 560 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 561 | struct skcipher_alg mv_cesa_cbc_des_alg = { |
| 562 | .setkey = mv_cesa_des_setkey, |
| 563 | .encrypt = mv_cesa_cbc_des_encrypt, |
| 564 | .decrypt = mv_cesa_cbc_des_decrypt, |
| 565 | .min_keysize = DES_KEY_SIZE, |
| 566 | .max_keysize = DES_KEY_SIZE, |
| 567 | .ivsize = DES_BLOCK_SIZE, |
| 568 | .base = { |
| 569 | .cra_name = "cbc(des)", |
| 570 | .cra_driver_name = "mv-cbc-des", |
| 571 | .cra_priority = 300, |
| 572 | .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC, |
| 573 | .cra_blocksize = DES_BLOCK_SIZE, |
| 574 | .cra_ctxsize = sizeof(struct mv_cesa_des_ctx), |
| 575 | .cra_alignmask = 0, |
| 576 | .cra_module = THIS_MODULE, |
| 577 | .cra_init = mv_cesa_skcipher_cra_init, |
| 578 | .cra_exit = mv_cesa_skcipher_cra_exit, |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 579 | }, |
| 580 | }; |
| 581 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 582 | static int mv_cesa_des3_op(struct skcipher_request *req, |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 583 | struct mv_cesa_op_ctx *tmpl) |
| 584 | { |
| 585 | struct mv_cesa_des3_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 586 | |
| 587 | mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTM_3DES, |
| 588 | CESA_SA_DESC_CFG_CRYPTM_MSK); |
| 589 | |
| 590 | memcpy(tmpl->ctx.blkcipher.key, ctx->key, DES3_EDE_KEY_SIZE); |
| 591 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 592 | return mv_cesa_skcipher_queue_req(req, tmpl); |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 593 | } |
| 594 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 595 | static int mv_cesa_ecb_des3_ede_encrypt(struct skcipher_request *req) |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 596 | { |
| 597 | struct mv_cesa_op_ctx tmpl; |
| 598 | |
| 599 | mv_cesa_set_op_cfg(&tmpl, |
| 600 | CESA_SA_DESC_CFG_CRYPTCM_ECB | |
| 601 | CESA_SA_DESC_CFG_3DES_EDE | |
| 602 | CESA_SA_DESC_CFG_DIR_ENC); |
| 603 | |
| 604 | return mv_cesa_des3_op(req, &tmpl); |
| 605 | } |
| 606 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 607 | static int mv_cesa_ecb_des3_ede_decrypt(struct skcipher_request *req) |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 608 | { |
| 609 | struct mv_cesa_op_ctx tmpl; |
| 610 | |
| 611 | mv_cesa_set_op_cfg(&tmpl, |
| 612 | CESA_SA_DESC_CFG_CRYPTCM_ECB | |
| 613 | CESA_SA_DESC_CFG_3DES_EDE | |
| 614 | CESA_SA_DESC_CFG_DIR_DEC); |
| 615 | |
| 616 | return mv_cesa_des3_op(req, &tmpl); |
| 617 | } |
| 618 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 619 | struct skcipher_alg mv_cesa_ecb_des3_ede_alg = { |
| 620 | .setkey = mv_cesa_des3_ede_setkey, |
| 621 | .encrypt = mv_cesa_ecb_des3_ede_encrypt, |
| 622 | .decrypt = mv_cesa_ecb_des3_ede_decrypt, |
| 623 | .min_keysize = DES3_EDE_KEY_SIZE, |
| 624 | .max_keysize = DES3_EDE_KEY_SIZE, |
| 625 | .ivsize = DES3_EDE_BLOCK_SIZE, |
| 626 | .base = { |
| 627 | .cra_name = "ecb(des3_ede)", |
| 628 | .cra_driver_name = "mv-ecb-des3-ede", |
| 629 | .cra_priority = 300, |
| 630 | .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC, |
| 631 | .cra_blocksize = DES3_EDE_BLOCK_SIZE, |
| 632 | .cra_ctxsize = sizeof(struct mv_cesa_des3_ctx), |
| 633 | .cra_alignmask = 0, |
| 634 | .cra_module = THIS_MODULE, |
| 635 | .cra_init = mv_cesa_skcipher_cra_init, |
| 636 | .cra_exit = mv_cesa_skcipher_cra_exit, |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 637 | }, |
| 638 | }; |
| 639 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 640 | static int mv_cesa_cbc_des3_op(struct skcipher_request *req, |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 641 | struct mv_cesa_op_ctx *tmpl) |
| 642 | { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 643 | memcpy(tmpl->ctx.blkcipher.iv, req->iv, DES3_EDE_BLOCK_SIZE); |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 644 | |
| 645 | return mv_cesa_des3_op(req, tmpl); |
| 646 | } |
| 647 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 648 | static int mv_cesa_cbc_des3_ede_encrypt(struct skcipher_request *req) |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 649 | { |
| 650 | struct mv_cesa_op_ctx tmpl; |
| 651 | |
| 652 | mv_cesa_set_op_cfg(&tmpl, |
| 653 | CESA_SA_DESC_CFG_CRYPTCM_CBC | |
| 654 | CESA_SA_DESC_CFG_3DES_EDE | |
| 655 | CESA_SA_DESC_CFG_DIR_ENC); |
| 656 | |
| 657 | return mv_cesa_cbc_des3_op(req, &tmpl); |
| 658 | } |
| 659 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 660 | static int mv_cesa_cbc_des3_ede_decrypt(struct skcipher_request *req) |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 661 | { |
| 662 | struct mv_cesa_op_ctx tmpl; |
| 663 | |
| 664 | mv_cesa_set_op_cfg(&tmpl, |
| 665 | CESA_SA_DESC_CFG_CRYPTCM_CBC | |
| 666 | CESA_SA_DESC_CFG_3DES_EDE | |
| 667 | CESA_SA_DESC_CFG_DIR_DEC); |
| 668 | |
| 669 | return mv_cesa_cbc_des3_op(req, &tmpl); |
| 670 | } |
| 671 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 672 | struct skcipher_alg mv_cesa_cbc_des3_ede_alg = { |
| 673 | .setkey = mv_cesa_des3_ede_setkey, |
| 674 | .encrypt = mv_cesa_cbc_des3_ede_encrypt, |
| 675 | .decrypt = mv_cesa_cbc_des3_ede_decrypt, |
| 676 | .min_keysize = DES3_EDE_KEY_SIZE, |
| 677 | .max_keysize = DES3_EDE_KEY_SIZE, |
| 678 | .ivsize = DES3_EDE_BLOCK_SIZE, |
| 679 | .base = { |
| 680 | .cra_name = "cbc(des3_ede)", |
| 681 | .cra_driver_name = "mv-cbc-des3-ede", |
| 682 | .cra_priority = 300, |
| 683 | .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC, |
| 684 | .cra_blocksize = DES3_EDE_BLOCK_SIZE, |
| 685 | .cra_ctxsize = sizeof(struct mv_cesa_des3_ctx), |
| 686 | .cra_alignmask = 0, |
| 687 | .cra_module = THIS_MODULE, |
| 688 | .cra_init = mv_cesa_skcipher_cra_init, |
| 689 | .cra_exit = mv_cesa_skcipher_cra_exit, |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 690 | }, |
| 691 | }; |
| 692 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 693 | static int mv_cesa_aes_op(struct skcipher_request *req, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 694 | struct mv_cesa_op_ctx *tmpl) |
| 695 | { |
| 696 | struct mv_cesa_aes_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 697 | int i; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 698 | u32 *key; |
| 699 | u32 cfg; |
| 700 | |
| 701 | cfg = CESA_SA_DESC_CFG_CRYPTM_AES; |
| 702 | |
| 703 | if (mv_cesa_get_op_cfg(tmpl) & CESA_SA_DESC_CFG_DIR_DEC) |
| 704 | key = ctx->aes.key_dec; |
| 705 | else |
| 706 | key = ctx->aes.key_enc; |
| 707 | |
| 708 | for (i = 0; i < ctx->aes.key_length / sizeof(u32); i++) |
| 709 | tmpl->ctx.blkcipher.key[i] = cpu_to_le32(key[i]); |
| 710 | |
| 711 | if (ctx->aes.key_length == 24) |
| 712 | cfg |= CESA_SA_DESC_CFG_AES_LEN_192; |
| 713 | else if (ctx->aes.key_length == 32) |
| 714 | cfg |= CESA_SA_DESC_CFG_AES_LEN_256; |
| 715 | |
| 716 | mv_cesa_update_op_cfg(tmpl, cfg, |
| 717 | CESA_SA_DESC_CFG_CRYPTM_MSK | |
| 718 | CESA_SA_DESC_CFG_AES_LEN_MSK); |
| 719 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 720 | return mv_cesa_skcipher_queue_req(req, tmpl); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 721 | } |
| 722 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 723 | static int mv_cesa_ecb_aes_encrypt(struct skcipher_request *req) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 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_ENC); |
| 730 | |
| 731 | return mv_cesa_aes_op(req, &tmpl); |
| 732 | } |
| 733 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 734 | static int mv_cesa_ecb_aes_decrypt(struct skcipher_request *req) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 735 | { |
| 736 | struct mv_cesa_op_ctx tmpl; |
| 737 | |
| 738 | mv_cesa_set_op_cfg(&tmpl, |
| 739 | CESA_SA_DESC_CFG_CRYPTCM_ECB | |
| 740 | CESA_SA_DESC_CFG_DIR_DEC); |
| 741 | |
| 742 | return mv_cesa_aes_op(req, &tmpl); |
| 743 | } |
| 744 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 745 | struct skcipher_alg mv_cesa_ecb_aes_alg = { |
| 746 | .setkey = mv_cesa_aes_setkey, |
| 747 | .encrypt = mv_cesa_ecb_aes_encrypt, |
| 748 | .decrypt = mv_cesa_ecb_aes_decrypt, |
| 749 | .min_keysize = AES_MIN_KEY_SIZE, |
| 750 | .max_keysize = AES_MAX_KEY_SIZE, |
| 751 | .base = { |
| 752 | .cra_name = "ecb(aes)", |
| 753 | .cra_driver_name = "mv-ecb-aes", |
| 754 | .cra_priority = 300, |
| 755 | .cra_flags = 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_module = THIS_MODULE, |
| 760 | .cra_init = mv_cesa_skcipher_cra_init, |
| 761 | .cra_exit = mv_cesa_skcipher_cra_exit, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 762 | }, |
| 763 | }; |
| 764 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 765 | static int mv_cesa_cbc_aes_op(struct skcipher_request *req, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 766 | struct mv_cesa_op_ctx *tmpl) |
| 767 | { |
| 768 | mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTCM_CBC, |
| 769 | CESA_SA_DESC_CFG_CRYPTCM_MSK); |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 770 | memcpy(tmpl->ctx.blkcipher.iv, req->iv, AES_BLOCK_SIZE); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 771 | |
| 772 | return mv_cesa_aes_op(req, tmpl); |
| 773 | } |
| 774 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 775 | static int mv_cesa_cbc_aes_encrypt(struct skcipher_request *req) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 776 | { |
| 777 | struct mv_cesa_op_ctx tmpl; |
| 778 | |
| 779 | mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_ENC); |
| 780 | |
| 781 | return mv_cesa_cbc_aes_op(req, &tmpl); |
| 782 | } |
| 783 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 784 | static int mv_cesa_cbc_aes_decrypt(struct skcipher_request *req) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 785 | { |
| 786 | struct mv_cesa_op_ctx tmpl; |
| 787 | |
| 788 | mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_DEC); |
| 789 | |
| 790 | return mv_cesa_cbc_aes_op(req, &tmpl); |
| 791 | } |
| 792 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 793 | struct skcipher_alg mv_cesa_cbc_aes_alg = { |
| 794 | .setkey = mv_cesa_aes_setkey, |
| 795 | .encrypt = mv_cesa_cbc_aes_encrypt, |
| 796 | .decrypt = mv_cesa_cbc_aes_decrypt, |
| 797 | .min_keysize = AES_MIN_KEY_SIZE, |
| 798 | .max_keysize = AES_MAX_KEY_SIZE, |
| 799 | .ivsize = AES_BLOCK_SIZE, |
| 800 | .base = { |
| 801 | .cra_name = "cbc(aes)", |
| 802 | .cra_driver_name = "mv-cbc-aes", |
| 803 | .cra_priority = 300, |
| 804 | .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC, |
| 805 | .cra_blocksize = AES_BLOCK_SIZE, |
| 806 | .cra_ctxsize = sizeof(struct mv_cesa_aes_ctx), |
| 807 | .cra_alignmask = 0, |
| 808 | .cra_module = THIS_MODULE, |
| 809 | .cra_init = mv_cesa_skcipher_cra_init, |
| 810 | .cra_exit = mv_cesa_skcipher_cra_exit, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 811 | }, |
| 812 | }; |