Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Hash algorithms supported by the CESA: MD5, SHA1 and SHA256. |
| 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 | |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 15 | #include <crypto/md5.h> |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 16 | #include <crypto/sha.h> |
| 17 | |
| 18 | #include "cesa.h" |
| 19 | |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 20 | struct mv_cesa_ahash_dma_iter { |
| 21 | struct mv_cesa_dma_iter base; |
| 22 | struct mv_cesa_sg_dma_iter src; |
| 23 | }; |
| 24 | |
| 25 | static inline void |
| 26 | mv_cesa_ahash_req_iter_init(struct mv_cesa_ahash_dma_iter *iter, |
| 27 | struct ahash_request *req) |
| 28 | { |
| 29 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
Russell King | bd274b1 | 2015-10-18 17:24:26 +0100 | [diff] [blame] | 30 | unsigned int len = req->nbytes + creq->cache_ptr; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 31 | |
| 32 | if (!creq->last_req) |
Russell King | bd274b1 | 2015-10-18 17:24:26 +0100 | [diff] [blame] | 33 | len &= ~CESA_HASH_BLOCK_SIZE_MSK; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 34 | |
| 35 | mv_cesa_req_dma_iter_init(&iter->base, len); |
| 36 | mv_cesa_sg_dma_iter_init(&iter->src, req->src, DMA_TO_DEVICE); |
| 37 | iter->src.op_offset = creq->cache_ptr; |
| 38 | } |
| 39 | |
| 40 | static inline bool |
| 41 | mv_cesa_ahash_req_iter_next_op(struct mv_cesa_ahash_dma_iter *iter) |
| 42 | { |
| 43 | iter->src.op_offset = 0; |
| 44 | |
| 45 | return mv_cesa_req_dma_iter_next_op(&iter->base); |
| 46 | } |
| 47 | |
Boris BREZILLON | 7850c91 | 2016-03-17 10:21:34 +0100 | [diff] [blame] | 48 | static inline int |
| 49 | mv_cesa_ahash_dma_alloc_cache(struct mv_cesa_ahash_dma_req *req, gfp_t flags) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 50 | { |
Boris BREZILLON | 7850c91 | 2016-03-17 10:21:34 +0100 | [diff] [blame] | 51 | req->cache = dma_pool_alloc(cesa_dev->dma->cache_pool, flags, |
| 52 | &req->cache_dma); |
| 53 | if (!req->cache) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 54 | return -ENOMEM; |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
Boris BREZILLON | 7850c91 | 2016-03-17 10:21:34 +0100 | [diff] [blame] | 59 | static inline void |
| 60 | mv_cesa_ahash_dma_free_cache(struct mv_cesa_ahash_dma_req *req) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 61 | { |
Boris BREZILLON | 7850c91 | 2016-03-17 10:21:34 +0100 | [diff] [blame] | 62 | if (!req->cache) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 63 | return; |
| 64 | |
Boris BREZILLON | 7850c91 | 2016-03-17 10:21:34 +0100 | [diff] [blame] | 65 | dma_pool_free(cesa_dev->dma->cache_pool, req->cache, |
| 66 | req->cache_dma); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 69 | static int mv_cesa_ahash_dma_alloc_padding(struct mv_cesa_ahash_dma_req *req, |
| 70 | gfp_t flags) |
| 71 | { |
| 72 | if (req->padding) |
| 73 | return 0; |
| 74 | |
| 75 | req->padding = dma_pool_alloc(cesa_dev->dma->padding_pool, flags, |
| 76 | &req->padding_dma); |
| 77 | if (!req->padding) |
| 78 | return -ENOMEM; |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static void mv_cesa_ahash_dma_free_padding(struct mv_cesa_ahash_dma_req *req) |
| 84 | { |
| 85 | if (!req->padding) |
| 86 | return; |
| 87 | |
| 88 | dma_pool_free(cesa_dev->dma->padding_pool, req->padding, |
| 89 | req->padding_dma); |
| 90 | req->padding = NULL; |
| 91 | } |
| 92 | |
| 93 | static inline void mv_cesa_ahash_dma_last_cleanup(struct ahash_request *req) |
| 94 | { |
| 95 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
| 96 | |
| 97 | mv_cesa_ahash_dma_free_padding(&creq->req.dma); |
| 98 | } |
| 99 | |
| 100 | static inline void mv_cesa_ahash_dma_cleanup(struct ahash_request *req) |
| 101 | { |
| 102 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
| 103 | |
| 104 | dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents, DMA_TO_DEVICE); |
Boris BREZILLON | 7850c91 | 2016-03-17 10:21:34 +0100 | [diff] [blame] | 105 | mv_cesa_ahash_dma_free_cache(&creq->req.dma); |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 106 | mv_cesa_dma_cleanup(&creq->base); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static inline void mv_cesa_ahash_cleanup(struct ahash_request *req) |
| 110 | { |
| 111 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
| 112 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 113 | if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 114 | mv_cesa_ahash_dma_cleanup(req); |
| 115 | } |
| 116 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 117 | static void mv_cesa_ahash_last_cleanup(struct ahash_request *req) |
| 118 | { |
| 119 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
| 120 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 121 | if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 122 | mv_cesa_ahash_dma_last_cleanup(req); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static int mv_cesa_ahash_pad_len(struct mv_cesa_ahash_req *creq) |
| 126 | { |
| 127 | unsigned int index, padlen; |
| 128 | |
| 129 | index = creq->len & CESA_HASH_BLOCK_SIZE_MSK; |
| 130 | padlen = (index < 56) ? (56 - index) : (64 + 56 - index); |
| 131 | |
| 132 | return padlen; |
| 133 | } |
| 134 | |
| 135 | static int mv_cesa_ahash_pad_req(struct mv_cesa_ahash_req *creq, u8 *buf) |
| 136 | { |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 137 | unsigned int index, padlen; |
| 138 | |
| 139 | buf[0] = 0x80; |
| 140 | /* Pad out to 56 mod 64 */ |
| 141 | index = creq->len & CESA_HASH_BLOCK_SIZE_MSK; |
| 142 | padlen = mv_cesa_ahash_pad_len(creq); |
| 143 | memset(buf + 1, 0, padlen - 1); |
Russell King | 51954a9 | 2015-10-18 17:23:46 +0100 | [diff] [blame] | 144 | |
| 145 | if (creq->algo_le) { |
| 146 | __le64 bits = cpu_to_le64(creq->len << 3); |
| 147 | memcpy(buf + padlen, &bits, sizeof(bits)); |
| 148 | } else { |
| 149 | __be64 bits = cpu_to_be64(creq->len << 3); |
| 150 | memcpy(buf + padlen, &bits, sizeof(bits)); |
| 151 | } |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 152 | |
| 153 | return padlen + 8; |
| 154 | } |
| 155 | |
| 156 | static void mv_cesa_ahash_std_step(struct ahash_request *req) |
| 157 | { |
| 158 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
| 159 | struct mv_cesa_ahash_std_req *sreq = &creq->req.std; |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 160 | struct mv_cesa_engine *engine = creq->base.engine; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 161 | struct mv_cesa_op_ctx *op; |
| 162 | unsigned int new_cache_ptr = 0; |
| 163 | u32 frag_mode; |
| 164 | size_t len; |
Romain Perier | 2786cee | 2016-06-21 10:08:37 +0200 | [diff] [blame] | 165 | unsigned int digsize; |
| 166 | int i; |
| 167 | |
| 168 | mv_cesa_adjust_op(engine, &creq->op_tmpl); |
| 169 | memcpy_toio(engine->sram, &creq->op_tmpl, sizeof(creq->op_tmpl)); |
| 170 | |
| 171 | digsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(req)); |
| 172 | for (i = 0; i < digsize / 4; i++) |
| 173 | writel_relaxed(creq->state[i], engine->regs + CESA_IVDIG(i)); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 174 | |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 175 | mv_cesa_adjust_op(engine, &creq->op_tmpl); |
| 176 | memcpy_toio(engine->sram, &creq->op_tmpl, sizeof(creq->op_tmpl)); |
| 177 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 178 | if (creq->cache_ptr) |
Russell King | 0f3304d | 2015-10-18 18:31:15 +0100 | [diff] [blame] | 179 | memcpy_toio(engine->sram + CESA_SA_DATA_SRAM_OFFSET, |
| 180 | creq->cache, creq->cache_ptr); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 181 | |
| 182 | len = min_t(size_t, req->nbytes + creq->cache_ptr - sreq->offset, |
| 183 | CESA_SA_SRAM_PAYLOAD_SIZE); |
| 184 | |
| 185 | if (!creq->last_req) { |
| 186 | new_cache_ptr = len & CESA_HASH_BLOCK_SIZE_MSK; |
| 187 | len &= ~CESA_HASH_BLOCK_SIZE_MSK; |
| 188 | } |
| 189 | |
| 190 | if (len - creq->cache_ptr) |
| 191 | sreq->offset += sg_pcopy_to_buffer(req->src, creq->src_nents, |
| 192 | engine->sram + |
| 193 | CESA_SA_DATA_SRAM_OFFSET + |
| 194 | creq->cache_ptr, |
| 195 | len - creq->cache_ptr, |
| 196 | sreq->offset); |
| 197 | |
| 198 | op = &creq->op_tmpl; |
| 199 | |
| 200 | frag_mode = mv_cesa_get_op_cfg(op) & CESA_SA_DESC_CFG_FRAG_MSK; |
| 201 | |
| 202 | if (creq->last_req && sreq->offset == req->nbytes && |
| 203 | creq->len <= CESA_SA_DESC_MAC_SRC_TOTAL_LEN_MAX) { |
| 204 | if (frag_mode == CESA_SA_DESC_CFG_FIRST_FRAG) |
| 205 | frag_mode = CESA_SA_DESC_CFG_NOT_FRAG; |
| 206 | else if (frag_mode == CESA_SA_DESC_CFG_MID_FRAG) |
| 207 | frag_mode = CESA_SA_DESC_CFG_LAST_FRAG; |
| 208 | } |
| 209 | |
| 210 | if (frag_mode == CESA_SA_DESC_CFG_NOT_FRAG || |
| 211 | frag_mode == CESA_SA_DESC_CFG_LAST_FRAG) { |
| 212 | if (len && |
| 213 | creq->len <= CESA_SA_DESC_MAC_SRC_TOTAL_LEN_MAX) { |
| 214 | mv_cesa_set_mac_op_total_len(op, creq->len); |
| 215 | } else { |
| 216 | int trailerlen = mv_cesa_ahash_pad_len(creq) + 8; |
| 217 | |
| 218 | if (len + trailerlen > CESA_SA_SRAM_PAYLOAD_SIZE) { |
| 219 | len &= CESA_HASH_BLOCK_SIZE_MSK; |
| 220 | new_cache_ptr = 64 - trailerlen; |
Russell King | 0f3304d | 2015-10-18 18:31:15 +0100 | [diff] [blame] | 221 | memcpy_fromio(creq->cache, |
| 222 | engine->sram + |
| 223 | CESA_SA_DATA_SRAM_OFFSET + len, |
| 224 | new_cache_ptr); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 225 | } else { |
| 226 | len += mv_cesa_ahash_pad_req(creq, |
| 227 | engine->sram + len + |
| 228 | CESA_SA_DATA_SRAM_OFFSET); |
| 229 | } |
| 230 | |
| 231 | if (frag_mode == CESA_SA_DESC_CFG_LAST_FRAG) |
| 232 | frag_mode = CESA_SA_DESC_CFG_MID_FRAG; |
| 233 | else |
| 234 | frag_mode = CESA_SA_DESC_CFG_FIRST_FRAG; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | mv_cesa_set_mac_op_frag_len(op, len); |
| 239 | mv_cesa_update_op_cfg(op, frag_mode, CESA_SA_DESC_CFG_FRAG_MSK); |
| 240 | |
| 241 | /* FIXME: only update enc_len field */ |
Russell King | 0f3304d | 2015-10-18 18:31:15 +0100 | [diff] [blame] | 242 | memcpy_toio(engine->sram, op, sizeof(*op)); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 243 | |
| 244 | if (frag_mode == CESA_SA_DESC_CFG_FIRST_FRAG) |
| 245 | mv_cesa_update_op_cfg(op, CESA_SA_DESC_CFG_MID_FRAG, |
| 246 | CESA_SA_DESC_CFG_FRAG_MSK); |
| 247 | |
| 248 | creq->cache_ptr = new_cache_ptr; |
| 249 | |
| 250 | mv_cesa_set_int_mask(engine, CESA_SA_INT_ACCEL0_DONE); |
Russell King | b150856 | 2015-10-18 18:31:00 +0100 | [diff] [blame] | 251 | writel_relaxed(CESA_SA_CFG_PARA_DIS, engine->regs + CESA_SA_CFG); |
Romain Perier | f628308 | 2016-06-21 10:08:32 +0200 | [diff] [blame] | 252 | BUG_ON(readl(engine->regs + CESA_SA_CMD) & |
| 253 | CESA_SA_CMD_EN_CESA_SA_ACCL0); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 254 | writel(CESA_SA_CMD_EN_CESA_SA_ACCL0, engine->regs + CESA_SA_CMD); |
| 255 | } |
| 256 | |
| 257 | static int mv_cesa_ahash_std_process(struct ahash_request *req, u32 status) |
| 258 | { |
| 259 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
| 260 | struct mv_cesa_ahash_std_req *sreq = &creq->req.std; |
| 261 | |
| 262 | if (sreq->offset < (req->nbytes - creq->cache_ptr)) |
| 263 | return -EINPROGRESS; |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 268 | static inline void mv_cesa_ahash_dma_prepare(struct ahash_request *req) |
| 269 | { |
| 270 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 271 | struct mv_cesa_req *basereq = &creq->base; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 272 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 273 | mv_cesa_dma_prepare(basereq, basereq->engine); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 276 | static void mv_cesa_ahash_std_prepare(struct ahash_request *req) |
| 277 | { |
| 278 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
| 279 | struct mv_cesa_ahash_std_req *sreq = &creq->req.std; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 280 | |
| 281 | sreq->offset = 0; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static void mv_cesa_ahash_step(struct crypto_async_request *req) |
| 285 | { |
| 286 | struct ahash_request *ahashreq = ahash_request_cast(req); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 287 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 288 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 289 | if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ) |
| 290 | mv_cesa_dma_step(&creq->base); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 291 | else |
| 292 | mv_cesa_ahash_std_step(ahashreq); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static int mv_cesa_ahash_process(struct crypto_async_request *req, u32 status) |
| 296 | { |
| 297 | struct ahash_request *ahashreq = ahash_request_cast(req); |
| 298 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 299 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 300 | if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ) |
Romain Perier | 1bf6682 | 2016-06-21 10:08:36 +0200 | [diff] [blame] | 301 | return mv_cesa_dma_process(&creq->base, status); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 302 | |
Romain Perier | 1bf6682 | 2016-06-21 10:08:36 +0200 | [diff] [blame] | 303 | return mv_cesa_ahash_std_process(ahashreq, status); |
| 304 | } |
| 305 | |
| 306 | static void mv_cesa_ahash_complete(struct crypto_async_request *req) |
| 307 | { |
| 308 | struct ahash_request *ahashreq = ahash_request_cast(req); |
| 309 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq); |
| 310 | struct mv_cesa_engine *engine = creq->base.engine; |
| 311 | unsigned int digsize; |
| 312 | int i; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 313 | |
| 314 | digsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(ahashreq)); |
| 315 | for (i = 0; i < digsize / 4; i++) |
Russell King | b150856 | 2015-10-18 18:31:00 +0100 | [diff] [blame] | 316 | creq->state[i] = readl_relaxed(engine->regs + CESA_IVDIG(i)); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 317 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 318 | if (creq->last_req) { |
Russell King | 4c2b130 | 2015-10-18 17:23:35 +0100 | [diff] [blame] | 319 | /* |
| 320 | * Hardware's MD5 digest is in little endian format, but |
| 321 | * SHA in big endian format |
| 322 | */ |
Russell King | a9eb678 | 2015-10-18 17:23:40 +0100 | [diff] [blame] | 323 | if (creq->algo_le) { |
Russell King | 4c2b130 | 2015-10-18 17:23:35 +0100 | [diff] [blame] | 324 | __le32 *result = (void *)ahashreq->result; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 325 | |
Russell King | 4c2b130 | 2015-10-18 17:23:35 +0100 | [diff] [blame] | 326 | for (i = 0; i < digsize / 4; i++) |
| 327 | result[i] = cpu_to_le32(creq->state[i]); |
| 328 | } else { |
| 329 | __be32 *result = (void *)ahashreq->result; |
| 330 | |
| 331 | for (i = 0; i < digsize / 4; i++) |
| 332 | result[i] = cpu_to_be32(creq->state[i]); |
| 333 | } |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 334 | } |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 335 | |
| 336 | atomic_sub(ahashreq->nbytes, &engine->load); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static void mv_cesa_ahash_prepare(struct crypto_async_request *req, |
| 340 | struct mv_cesa_engine *engine) |
| 341 | { |
| 342 | struct ahash_request *ahashreq = ahash_request_cast(req); |
| 343 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 344 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 345 | creq->base.engine = engine; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 346 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 347 | if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 348 | mv_cesa_ahash_dma_prepare(ahashreq); |
| 349 | else |
| 350 | mv_cesa_ahash_std_prepare(ahashreq); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static void mv_cesa_ahash_req_cleanup(struct crypto_async_request *req) |
| 354 | { |
| 355 | struct ahash_request *ahashreq = ahash_request_cast(req); |
| 356 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq); |
| 357 | |
| 358 | if (creq->last_req) |
| 359 | mv_cesa_ahash_last_cleanup(ahashreq); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 360 | |
| 361 | mv_cesa_ahash_cleanup(ahashreq); |
Romain Perier | 64ec6cc | 2016-07-22 15:46:24 +0200 | [diff] [blame] | 362 | |
| 363 | if (creq->cache_ptr) |
| 364 | sg_pcopy_to_buffer(ahashreq->src, creq->src_nents, |
| 365 | creq->cache, |
| 366 | creq->cache_ptr, |
| 367 | ahashreq->nbytes - creq->cache_ptr); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static const struct mv_cesa_req_ops mv_cesa_ahash_req_ops = { |
| 371 | .step = mv_cesa_ahash_step, |
| 372 | .process = mv_cesa_ahash_process, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 373 | .cleanup = mv_cesa_ahash_req_cleanup, |
Romain Perier | 1bf6682 | 2016-06-21 10:08:36 +0200 | [diff] [blame] | 374 | .complete = mv_cesa_ahash_complete, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 375 | }; |
| 376 | |
Thomas Petazzoni | 3e5c66c | 2016-08-09 11:03:16 +0200 | [diff] [blame^] | 377 | static void mv_cesa_ahash_init(struct ahash_request *req, |
Russell King | a9eb678 | 2015-10-18 17:23:40 +0100 | [diff] [blame] | 378 | struct mv_cesa_op_ctx *tmpl, bool algo_le) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 379 | { |
| 380 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
| 381 | |
| 382 | memset(creq, 0, sizeof(*creq)); |
| 383 | mv_cesa_update_op_cfg(tmpl, |
| 384 | CESA_SA_DESC_CFG_OP_MAC_ONLY | |
| 385 | CESA_SA_DESC_CFG_FIRST_FRAG, |
| 386 | CESA_SA_DESC_CFG_OP_MSK | |
| 387 | CESA_SA_DESC_CFG_FRAG_MSK); |
| 388 | mv_cesa_set_mac_op_total_len(tmpl, 0); |
| 389 | mv_cesa_set_mac_op_frag_len(tmpl, 0); |
| 390 | creq->op_tmpl = *tmpl; |
| 391 | creq->len = 0; |
Russell King | a9eb678 | 2015-10-18 17:23:40 +0100 | [diff] [blame] | 392 | creq->algo_le = algo_le; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static inline int mv_cesa_ahash_cra_init(struct crypto_tfm *tfm) |
| 396 | { |
| 397 | struct mv_cesa_hash_ctx *ctx = crypto_tfm_ctx(tfm); |
| 398 | |
| 399 | ctx->base.ops = &mv_cesa_ahash_req_ops; |
| 400 | |
| 401 | crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), |
| 402 | sizeof(struct mv_cesa_ahash_req)); |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static int mv_cesa_ahash_cache_req(struct ahash_request *req, bool *cached) |
| 407 | { |
| 408 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 409 | |
| 410 | if (creq->cache_ptr + req->nbytes < 64 && !creq->last_req) { |
| 411 | *cached = true; |
| 412 | |
| 413 | if (!req->nbytes) |
| 414 | return 0; |
| 415 | |
| 416 | sg_pcopy_to_buffer(req->src, creq->src_nents, |
| 417 | creq->cache + creq->cache_ptr, |
| 418 | req->nbytes, 0); |
| 419 | |
| 420 | creq->cache_ptr += req->nbytes; |
| 421 | } |
| 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 426 | static struct mv_cesa_op_ctx * |
Russell King | 9621288 | 2015-10-18 17:24:06 +0100 | [diff] [blame] | 427 | mv_cesa_dma_add_frag(struct mv_cesa_tdma_chain *chain, |
| 428 | struct mv_cesa_op_ctx *tmpl, unsigned int frag_len, |
| 429 | gfp_t flags) |
| 430 | { |
| 431 | struct mv_cesa_op_ctx *op; |
| 432 | int ret; |
| 433 | |
| 434 | op = mv_cesa_dma_add_op(chain, tmpl, false, flags); |
| 435 | if (IS_ERR(op)) |
| 436 | return op; |
| 437 | |
| 438 | /* Set the operation block fragment length. */ |
| 439 | mv_cesa_set_mac_op_frag_len(op, frag_len); |
| 440 | |
| 441 | /* Append dummy desc to launch operation */ |
| 442 | ret = mv_cesa_dma_add_dummy_launch(chain, flags); |
| 443 | if (ret) |
| 444 | return ERR_PTR(ret); |
| 445 | |
Russell King | 2f396a9 | 2015-10-18 17:24:11 +0100 | [diff] [blame] | 446 | if (mv_cesa_mac_op_is_first_frag(tmpl)) |
| 447 | mv_cesa_update_op_cfg(tmpl, |
| 448 | CESA_SA_DESC_CFG_MID_FRAG, |
| 449 | CESA_SA_DESC_CFG_FRAG_MSK); |
| 450 | |
Russell King | 9621288 | 2015-10-18 17:24:06 +0100 | [diff] [blame] | 451 | return op; |
| 452 | } |
| 453 | |
Russell King | 0971d09 | 2015-10-18 17:24:16 +0100 | [diff] [blame] | 454 | static int |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 455 | mv_cesa_ahash_dma_add_cache(struct mv_cesa_tdma_chain *chain, |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 456 | struct mv_cesa_ahash_req *creq, |
| 457 | gfp_t flags) |
| 458 | { |
| 459 | struct mv_cesa_ahash_dma_req *ahashdreq = &creq->req.dma; |
Boris BREZILLON | 7850c91 | 2016-03-17 10:21:34 +0100 | [diff] [blame] | 460 | int ret; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 461 | |
| 462 | if (!creq->cache_ptr) |
Russell King | 0971d09 | 2015-10-18 17:24:16 +0100 | [diff] [blame] | 463 | return 0; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 464 | |
Boris BREZILLON | 7850c91 | 2016-03-17 10:21:34 +0100 | [diff] [blame] | 465 | ret = mv_cesa_ahash_dma_alloc_cache(ahashdreq, flags); |
| 466 | if (ret) |
| 467 | return ret; |
| 468 | |
| 469 | memcpy(ahashdreq->cache, creq->cache, creq->cache_ptr); |
| 470 | |
Russell King | 0971d09 | 2015-10-18 17:24:16 +0100 | [diff] [blame] | 471 | return mv_cesa_dma_add_data_transfer(chain, |
| 472 | CESA_SA_DATA_SRAM_OFFSET, |
| 473 | ahashdreq->cache_dma, |
| 474 | creq->cache_ptr, |
| 475 | CESA_TDMA_DST_IN_SRAM, |
| 476 | flags); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static struct mv_cesa_op_ctx * |
| 480 | mv_cesa_ahash_dma_last_req(struct mv_cesa_tdma_chain *chain, |
| 481 | struct mv_cesa_ahash_dma_iter *dma_iter, |
| 482 | struct mv_cesa_ahash_req *creq, |
Russell King | 58953e1 | 2015-10-18 17:24:37 +0100 | [diff] [blame] | 483 | unsigned int frag_len, gfp_t flags) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 484 | { |
| 485 | struct mv_cesa_ahash_dma_req *ahashdreq = &creq->req.dma; |
| 486 | unsigned int len, trailerlen, padoff = 0; |
Russell King | 58953e1 | 2015-10-18 17:24:37 +0100 | [diff] [blame] | 487 | struct mv_cesa_op_ctx *op; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 488 | int ret; |
| 489 | |
Russell King | aee84a7 | 2015-10-18 17:24:42 +0100 | [diff] [blame] | 490 | /* |
| 491 | * If the transfer is smaller than our maximum length, and we have |
| 492 | * some data outstanding, we can ask the engine to finish the hash. |
| 493 | */ |
| 494 | if (creq->len <= CESA_SA_DESC_MAC_SRC_TOTAL_LEN_MAX && frag_len) { |
| 495 | op = mv_cesa_dma_add_frag(chain, &creq->op_tmpl, frag_len, |
| 496 | flags); |
| 497 | if (IS_ERR(op)) |
| 498 | return op; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 499 | |
Russell King | aee84a7 | 2015-10-18 17:24:42 +0100 | [diff] [blame] | 500 | mv_cesa_set_mac_op_total_len(op, creq->len); |
| 501 | mv_cesa_update_op_cfg(op, mv_cesa_mac_op_is_first_frag(op) ? |
| 502 | CESA_SA_DESC_CFG_NOT_FRAG : |
| 503 | CESA_SA_DESC_CFG_LAST_FRAG, |
| 504 | CESA_SA_DESC_CFG_FRAG_MSK); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 505 | |
| 506 | return op; |
| 507 | } |
| 508 | |
Russell King | aee84a7 | 2015-10-18 17:24:42 +0100 | [diff] [blame] | 509 | /* |
| 510 | * The request is longer than the engine can handle, or we have |
| 511 | * no data outstanding. Manually generate the padding, adding it |
| 512 | * as a "mid" fragment. |
| 513 | */ |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 514 | ret = mv_cesa_ahash_dma_alloc_padding(ahashdreq, flags); |
| 515 | if (ret) |
| 516 | return ERR_PTR(ret); |
| 517 | |
| 518 | trailerlen = mv_cesa_ahash_pad_req(creq, ahashdreq->padding); |
| 519 | |
Russell King | ab270e7 | 2015-10-18 17:24:47 +0100 | [diff] [blame] | 520 | len = min(CESA_SA_SRAM_PAYLOAD_SIZE - frag_len, trailerlen); |
| 521 | if (len) { |
| 522 | ret = mv_cesa_dma_add_data_transfer(chain, |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 523 | CESA_SA_DATA_SRAM_OFFSET + |
Russell King | ab270e7 | 2015-10-18 17:24:47 +0100 | [diff] [blame] | 524 | frag_len, |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 525 | ahashdreq->padding_dma, |
| 526 | len, CESA_TDMA_DST_IN_SRAM, |
| 527 | flags); |
Russell King | ab270e7 | 2015-10-18 17:24:47 +0100 | [diff] [blame] | 528 | if (ret) |
| 529 | return ERR_PTR(ret); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 530 | |
Russell King | ab270e7 | 2015-10-18 17:24:47 +0100 | [diff] [blame] | 531 | op = mv_cesa_dma_add_frag(chain, &creq->op_tmpl, frag_len + len, |
| 532 | flags); |
| 533 | if (IS_ERR(op)) |
| 534 | return op; |
| 535 | |
Russell King | ab270e7 | 2015-10-18 17:24:47 +0100 | [diff] [blame] | 536 | if (len == trailerlen) |
| 537 | return op; |
| 538 | |
| 539 | padoff += len; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 540 | } |
| 541 | |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 542 | ret = mv_cesa_dma_add_data_transfer(chain, |
| 543 | CESA_SA_DATA_SRAM_OFFSET, |
| 544 | ahashdreq->padding_dma + |
| 545 | padoff, |
| 546 | trailerlen - padoff, |
| 547 | CESA_TDMA_DST_IN_SRAM, |
| 548 | flags); |
| 549 | if (ret) |
| 550 | return ERR_PTR(ret); |
| 551 | |
Russell King | 9621288 | 2015-10-18 17:24:06 +0100 | [diff] [blame] | 552 | return mv_cesa_dma_add_frag(chain, &creq->op_tmpl, trailerlen - padoff, |
| 553 | flags); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | static int mv_cesa_ahash_dma_req_init(struct ahash_request *req) |
| 557 | { |
| 558 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
| 559 | gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? |
| 560 | GFP_KERNEL : GFP_ATOMIC; |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 561 | struct mv_cesa_req *basereq = &creq->base; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 562 | struct mv_cesa_ahash_dma_iter iter; |
| 563 | struct mv_cesa_op_ctx *op = NULL; |
Russell King | e41bbeb | 2015-10-18 17:24:32 +0100 | [diff] [blame] | 564 | unsigned int frag_len; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 565 | int ret; |
| 566 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 567 | basereq->chain.first = NULL; |
| 568 | basereq->chain.last = NULL; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 569 | |
| 570 | if (creq->src_nents) { |
| 571 | ret = dma_map_sg(cesa_dev->dev, req->src, creq->src_nents, |
| 572 | DMA_TO_DEVICE); |
| 573 | if (!ret) { |
| 574 | ret = -ENOMEM; |
| 575 | goto err; |
| 576 | } |
| 577 | } |
| 578 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 579 | mv_cesa_tdma_desc_iter_init(&basereq->chain); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 580 | mv_cesa_ahash_req_iter_init(&iter, req); |
| 581 | |
Russell King | 0971d09 | 2015-10-18 17:24:16 +0100 | [diff] [blame] | 582 | /* |
| 583 | * Add the cache (left-over data from a previous block) first. |
| 584 | * This will never overflow the SRAM size. |
| 585 | */ |
Thomas Petazzoni | 2a8a785 | 2016-08-09 11:03:15 +0200 | [diff] [blame] | 586 | ret = mv_cesa_ahash_dma_add_cache(&basereq->chain, creq, flags); |
Russell King | 0971d09 | 2015-10-18 17:24:16 +0100 | [diff] [blame] | 587 | if (ret) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 588 | goto err_free_tdma; |
Russell King | 0971d09 | 2015-10-18 17:24:16 +0100 | [diff] [blame] | 589 | |
Russell King | d9bba4c | 2015-10-18 17:24:21 +0100 | [diff] [blame] | 590 | if (iter.src.sg) { |
| 591 | /* |
| 592 | * Add all the new data, inserting an operation block and |
| 593 | * launch command between each full SRAM block-worth of |
Russell King | e41bbeb | 2015-10-18 17:24:32 +0100 | [diff] [blame] | 594 | * data. We intentionally do not add the final op block. |
Russell King | d9bba4c | 2015-10-18 17:24:21 +0100 | [diff] [blame] | 595 | */ |
Russell King | e41bbeb | 2015-10-18 17:24:32 +0100 | [diff] [blame] | 596 | while (true) { |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 597 | ret = mv_cesa_dma_add_op_transfers(&basereq->chain, |
Boris Brezillon | 8c07f3a | 2015-10-18 17:24:57 +0100 | [diff] [blame] | 598 | &iter.base, |
Russell King | d9bba4c | 2015-10-18 17:24:21 +0100 | [diff] [blame] | 599 | &iter.src, flags); |
| 600 | if (ret) |
| 601 | goto err_free_tdma; |
| 602 | |
Russell King | e41bbeb | 2015-10-18 17:24:32 +0100 | [diff] [blame] | 603 | frag_len = iter.base.op_len; |
| 604 | |
| 605 | if (!mv_cesa_ahash_req_iter_next_op(&iter)) |
| 606 | break; |
| 607 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 608 | op = mv_cesa_dma_add_frag(&basereq->chain, &creq->op_tmpl, |
Russell King | e41bbeb | 2015-10-18 17:24:32 +0100 | [diff] [blame] | 609 | frag_len, flags); |
Russell King | d9bba4c | 2015-10-18 17:24:21 +0100 | [diff] [blame] | 610 | if (IS_ERR(op)) { |
| 611 | ret = PTR_ERR(op); |
| 612 | goto err_free_tdma; |
| 613 | } |
Russell King | e41bbeb | 2015-10-18 17:24:32 +0100 | [diff] [blame] | 614 | } |
| 615 | } else { |
Russell King | d9bba4c | 2015-10-18 17:24:21 +0100 | [diff] [blame] | 616 | /* Account for the data that was in the cache. */ |
Russell King | e41bbeb | 2015-10-18 17:24:32 +0100 | [diff] [blame] | 617 | frag_len = iter.base.op_len; |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Russell King | 58953e1 | 2015-10-18 17:24:37 +0100 | [diff] [blame] | 620 | /* |
| 621 | * At this point, frag_len indicates whether we have any data |
| 622 | * outstanding which needs an operation. Queue up the final |
| 623 | * operation, which depends whether this is the final request. |
| 624 | */ |
| 625 | if (creq->last_req) |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 626 | op = mv_cesa_ahash_dma_last_req(&basereq->chain, &iter, creq, |
Boris Brezillon | 8c07f3a | 2015-10-18 17:24:57 +0100 | [diff] [blame] | 627 | frag_len, flags); |
Russell King | 58953e1 | 2015-10-18 17:24:37 +0100 | [diff] [blame] | 628 | else if (frag_len) |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 629 | op = mv_cesa_dma_add_frag(&basereq->chain, &creq->op_tmpl, |
Boris Brezillon | 8c07f3a | 2015-10-18 17:24:57 +0100 | [diff] [blame] | 630 | frag_len, flags); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 631 | |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 632 | if (IS_ERR(op)) { |
| 633 | ret = PTR_ERR(op); |
| 634 | goto err_free_tdma; |
| 635 | } |
| 636 | |
| 637 | if (op) { |
| 638 | /* Add dummy desc to wait for crypto operation end */ |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 639 | ret = mv_cesa_dma_add_dummy_end(&basereq->chain, flags); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 640 | if (ret) |
| 641 | goto err_free_tdma; |
| 642 | } |
| 643 | |
| 644 | if (!creq->last_req) |
| 645 | creq->cache_ptr = req->nbytes + creq->cache_ptr - |
| 646 | iter.base.len; |
| 647 | else |
| 648 | creq->cache_ptr = 0; |
| 649 | |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 650 | basereq->chain.last->flags |= (CESA_TDMA_END_OF_REQ | |
| 651 | CESA_TDMA_BREAK_CHAIN); |
| 652 | |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 653 | return 0; |
| 654 | |
| 655 | err_free_tdma: |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 656 | mv_cesa_dma_cleanup(basereq); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 657 | dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents, DMA_TO_DEVICE); |
| 658 | |
| 659 | err: |
| 660 | mv_cesa_ahash_last_cleanup(req); |
| 661 | |
| 662 | return ret; |
| 663 | } |
| 664 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 665 | static int mv_cesa_ahash_req_init(struct ahash_request *req, bool *cached) |
| 666 | { |
| 667 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 668 | int ret; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 669 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 670 | creq->src_nents = sg_nents_for_len(req->src, req->nbytes); |
LABBE Corentin | c22dafb | 2015-11-04 21:13:33 +0100 | [diff] [blame] | 671 | if (creq->src_nents < 0) { |
| 672 | dev_err(cesa_dev->dev, "Invalid number of src SG"); |
| 673 | return creq->src_nents; |
| 674 | } |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 675 | |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 676 | ret = mv_cesa_ahash_cache_req(req, cached); |
| 677 | if (ret) |
| 678 | return ret; |
| 679 | |
| 680 | if (*cached) |
| 681 | return 0; |
| 682 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 683 | if (cesa_dev->caps->has_tdma) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 684 | ret = mv_cesa_ahash_dma_req_init(req); |
| 685 | |
| 686 | return ret; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 687 | } |
| 688 | |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 689 | static int mv_cesa_ahash_queue_req(struct ahash_request *req) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 690 | { |
| 691 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 692 | struct mv_cesa_engine *engine; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 693 | bool cached = false; |
| 694 | int ret; |
| 695 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 696 | ret = mv_cesa_ahash_req_init(req, &cached); |
| 697 | if (ret) |
| 698 | return ret; |
| 699 | |
| 700 | if (cached) |
| 701 | return 0; |
| 702 | |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 703 | engine = mv_cesa_select_engine(req->nbytes); |
| 704 | mv_cesa_ahash_prepare(&req->base, engine); |
| 705 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 706 | ret = mv_cesa_queue_req(&req->base, &creq->base); |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 707 | |
Thomas Petazzoni | cfcd227 | 2015-09-18 17:25:36 +0200 | [diff] [blame] | 708 | if (mv_cesa_req_needs_cleanup(&req->base, ret)) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 709 | mv_cesa_ahash_cleanup(req); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 710 | |
| 711 | return ret; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 712 | } |
| 713 | |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 714 | static int mv_cesa_ahash_update(struct ahash_request *req) |
| 715 | { |
| 716 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
| 717 | |
| 718 | creq->len += req->nbytes; |
| 719 | |
| 720 | return mv_cesa_ahash_queue_req(req); |
| 721 | } |
| 722 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 723 | static int mv_cesa_ahash_final(struct ahash_request *req) |
| 724 | { |
| 725 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
| 726 | struct mv_cesa_op_ctx *tmpl = &creq->op_tmpl; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 727 | |
| 728 | mv_cesa_set_mac_op_total_len(tmpl, creq->len); |
| 729 | creq->last_req = true; |
| 730 | req->nbytes = 0; |
| 731 | |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 732 | return mv_cesa_ahash_queue_req(req); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | static int mv_cesa_ahash_finup(struct ahash_request *req) |
| 736 | { |
| 737 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
| 738 | struct mv_cesa_op_ctx *tmpl = &creq->op_tmpl; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 739 | |
| 740 | creq->len += req->nbytes; |
| 741 | mv_cesa_set_mac_op_total_len(tmpl, creq->len); |
| 742 | creq->last_req = true; |
| 743 | |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 744 | return mv_cesa_ahash_queue_req(req); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 745 | } |
| 746 | |
Russell King | a6479ea | 2015-10-09 21:14:22 +0100 | [diff] [blame] | 747 | static int mv_cesa_ahash_export(struct ahash_request *req, void *hash, |
| 748 | u64 *len, void *cache) |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 749 | { |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 750 | struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); |
| 751 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
| 752 | unsigned int digsize = crypto_ahash_digestsize(ahash); |
Russell King | a6479ea | 2015-10-09 21:14:22 +0100 | [diff] [blame] | 753 | unsigned int blocksize; |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 754 | |
Russell King | 8075453 | 2015-10-18 17:23:30 +0100 | [diff] [blame] | 755 | blocksize = crypto_ahash_blocksize(ahash); |
Russell King | a6479ea | 2015-10-09 21:14:22 +0100 | [diff] [blame] | 756 | |
| 757 | *len = creq->len; |
| 758 | memcpy(hash, creq->state, digsize); |
| 759 | memset(cache, 0, blocksize); |
Dan Carpenter | 063327f | 2016-03-21 12:03:43 +0300 | [diff] [blame] | 760 | memcpy(cache, creq->cache, creq->cache_ptr); |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 761 | |
| 762 | return 0; |
| 763 | } |
| 764 | |
Russell King | a6479ea | 2015-10-09 21:14:22 +0100 | [diff] [blame] | 765 | static int mv_cesa_ahash_import(struct ahash_request *req, const void *hash, |
| 766 | u64 len, const void *cache) |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 767 | { |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 768 | struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); |
| 769 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
| 770 | unsigned int digsize = crypto_ahash_digestsize(ahash); |
Russell King | a6479ea | 2015-10-09 21:14:22 +0100 | [diff] [blame] | 771 | unsigned int blocksize; |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 772 | unsigned int cache_ptr; |
| 773 | int ret; |
| 774 | |
Russell King | a6479ea | 2015-10-09 21:14:22 +0100 | [diff] [blame] | 775 | ret = crypto_ahash_init(req); |
| 776 | if (ret) |
| 777 | return ret; |
| 778 | |
Russell King | 8075453 | 2015-10-18 17:23:30 +0100 | [diff] [blame] | 779 | blocksize = crypto_ahash_blocksize(ahash); |
Russell King | a6479ea | 2015-10-09 21:14:22 +0100 | [diff] [blame] | 780 | if (len >= blocksize) |
| 781 | mv_cesa_update_op_cfg(&creq->op_tmpl, |
| 782 | CESA_SA_DESC_CFG_MID_FRAG, |
| 783 | CESA_SA_DESC_CFG_FRAG_MSK); |
| 784 | |
| 785 | creq->len = len; |
| 786 | memcpy(creq->state, hash, digsize); |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 787 | creq->cache_ptr = 0; |
| 788 | |
Russell King | a6479ea | 2015-10-09 21:14:22 +0100 | [diff] [blame] | 789 | cache_ptr = do_div(len, blocksize); |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 790 | if (!cache_ptr) |
| 791 | return 0; |
| 792 | |
Russell King | a6479ea | 2015-10-09 21:14:22 +0100 | [diff] [blame] | 793 | memcpy(creq->cache, cache, cache_ptr); |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 794 | creq->cache_ptr = cache_ptr; |
| 795 | |
| 796 | return 0; |
| 797 | } |
| 798 | |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 799 | static int mv_cesa_md5_init(struct ahash_request *req) |
| 800 | { |
Boris BREZILLON | b0ef510 | 2016-03-17 10:21:35 +0100 | [diff] [blame] | 801 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
Russell King | d30cb2f | 2015-10-18 17:23:51 +0100 | [diff] [blame] | 802 | struct mv_cesa_op_ctx tmpl = { }; |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 803 | |
| 804 | mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_MD5); |
Boris BREZILLON | b0ef510 | 2016-03-17 10:21:35 +0100 | [diff] [blame] | 805 | creq->state[0] = MD5_H0; |
| 806 | creq->state[1] = MD5_H1; |
| 807 | creq->state[2] = MD5_H2; |
| 808 | creq->state[3] = MD5_H3; |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 809 | |
Russell King | a9eb678 | 2015-10-18 17:23:40 +0100 | [diff] [blame] | 810 | mv_cesa_ahash_init(req, &tmpl, true); |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 811 | |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | static int mv_cesa_md5_export(struct ahash_request *req, void *out) |
| 816 | { |
| 817 | struct md5_state *out_state = out; |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 818 | |
Russell King | a6479ea | 2015-10-09 21:14:22 +0100 | [diff] [blame] | 819 | return mv_cesa_ahash_export(req, out_state->hash, |
| 820 | &out_state->byte_count, out_state->block); |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | static int mv_cesa_md5_import(struct ahash_request *req, const void *in) |
| 824 | { |
| 825 | const struct md5_state *in_state = in; |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 826 | |
Russell King | a6479ea | 2015-10-09 21:14:22 +0100 | [diff] [blame] | 827 | return mv_cesa_ahash_import(req, in_state->hash, in_state->byte_count, |
| 828 | in_state->block); |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | static int mv_cesa_md5_digest(struct ahash_request *req) |
| 832 | { |
| 833 | int ret; |
| 834 | |
| 835 | ret = mv_cesa_md5_init(req); |
| 836 | if (ret) |
| 837 | return ret; |
| 838 | |
| 839 | return mv_cesa_ahash_finup(req); |
| 840 | } |
| 841 | |
| 842 | struct ahash_alg mv_md5_alg = { |
| 843 | .init = mv_cesa_md5_init, |
| 844 | .update = mv_cesa_ahash_update, |
| 845 | .final = mv_cesa_ahash_final, |
| 846 | .finup = mv_cesa_ahash_finup, |
| 847 | .digest = mv_cesa_md5_digest, |
| 848 | .export = mv_cesa_md5_export, |
| 849 | .import = mv_cesa_md5_import, |
| 850 | .halg = { |
| 851 | .digestsize = MD5_DIGEST_SIZE, |
Russell King | 9f5594c | 2015-10-09 20:43:38 +0100 | [diff] [blame] | 852 | .statesize = sizeof(struct md5_state), |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 853 | .base = { |
| 854 | .cra_name = "md5", |
| 855 | .cra_driver_name = "mv-md5", |
| 856 | .cra_priority = 300, |
| 857 | .cra_flags = CRYPTO_ALG_ASYNC | |
| 858 | CRYPTO_ALG_KERN_DRIVER_ONLY, |
| 859 | .cra_blocksize = MD5_HMAC_BLOCK_SIZE, |
| 860 | .cra_ctxsize = sizeof(struct mv_cesa_hash_ctx), |
| 861 | .cra_init = mv_cesa_ahash_cra_init, |
| 862 | .cra_module = THIS_MODULE, |
| 863 | } |
| 864 | } |
| 865 | }; |
| 866 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 867 | static int mv_cesa_sha1_init(struct ahash_request *req) |
| 868 | { |
Boris BREZILLON | b0ef510 | 2016-03-17 10:21:35 +0100 | [diff] [blame] | 869 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
Russell King | d30cb2f | 2015-10-18 17:23:51 +0100 | [diff] [blame] | 870 | struct mv_cesa_op_ctx tmpl = { }; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 871 | |
| 872 | mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_SHA1); |
Boris BREZILLON | b0ef510 | 2016-03-17 10:21:35 +0100 | [diff] [blame] | 873 | creq->state[0] = SHA1_H0; |
| 874 | creq->state[1] = SHA1_H1; |
| 875 | creq->state[2] = SHA1_H2; |
| 876 | creq->state[3] = SHA1_H3; |
| 877 | creq->state[4] = SHA1_H4; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 878 | |
Russell King | a9eb678 | 2015-10-18 17:23:40 +0100 | [diff] [blame] | 879 | mv_cesa_ahash_init(req, &tmpl, false); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 880 | |
| 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | static int mv_cesa_sha1_export(struct ahash_request *req, void *out) |
| 885 | { |
| 886 | struct sha1_state *out_state = out; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 887 | |
Russell King | a6479ea | 2015-10-09 21:14:22 +0100 | [diff] [blame] | 888 | return mv_cesa_ahash_export(req, out_state->state, &out_state->count, |
| 889 | out_state->buffer); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | static int mv_cesa_sha1_import(struct ahash_request *req, const void *in) |
| 893 | { |
| 894 | const struct sha1_state *in_state = in; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 895 | |
Russell King | a6479ea | 2015-10-09 21:14:22 +0100 | [diff] [blame] | 896 | return mv_cesa_ahash_import(req, in_state->state, in_state->count, |
| 897 | in_state->buffer); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | static int mv_cesa_sha1_digest(struct ahash_request *req) |
| 901 | { |
| 902 | int ret; |
| 903 | |
| 904 | ret = mv_cesa_sha1_init(req); |
| 905 | if (ret) |
| 906 | return ret; |
| 907 | |
| 908 | return mv_cesa_ahash_finup(req); |
| 909 | } |
| 910 | |
| 911 | struct ahash_alg mv_sha1_alg = { |
| 912 | .init = mv_cesa_sha1_init, |
| 913 | .update = mv_cesa_ahash_update, |
| 914 | .final = mv_cesa_ahash_final, |
| 915 | .finup = mv_cesa_ahash_finup, |
| 916 | .digest = mv_cesa_sha1_digest, |
| 917 | .export = mv_cesa_sha1_export, |
| 918 | .import = mv_cesa_sha1_import, |
| 919 | .halg = { |
| 920 | .digestsize = SHA1_DIGEST_SIZE, |
Russell King | 9f5594c | 2015-10-09 20:43:38 +0100 | [diff] [blame] | 921 | .statesize = sizeof(struct sha1_state), |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 922 | .base = { |
| 923 | .cra_name = "sha1", |
| 924 | .cra_driver_name = "mv-sha1", |
| 925 | .cra_priority = 300, |
| 926 | .cra_flags = CRYPTO_ALG_ASYNC | |
| 927 | CRYPTO_ALG_KERN_DRIVER_ONLY, |
| 928 | .cra_blocksize = SHA1_BLOCK_SIZE, |
| 929 | .cra_ctxsize = sizeof(struct mv_cesa_hash_ctx), |
| 930 | .cra_init = mv_cesa_ahash_cra_init, |
| 931 | .cra_module = THIS_MODULE, |
| 932 | } |
| 933 | } |
| 934 | }; |
| 935 | |
Arnaud Ebalard | f85a762 | 2015-06-18 15:46:25 +0200 | [diff] [blame] | 936 | static int mv_cesa_sha256_init(struct ahash_request *req) |
| 937 | { |
Boris BREZILLON | b0ef510 | 2016-03-17 10:21:35 +0100 | [diff] [blame] | 938 | struct mv_cesa_ahash_req *creq = ahash_request_ctx(req); |
Russell King | d30cb2f | 2015-10-18 17:23:51 +0100 | [diff] [blame] | 939 | struct mv_cesa_op_ctx tmpl = { }; |
Arnaud Ebalard | f85a762 | 2015-06-18 15:46:25 +0200 | [diff] [blame] | 940 | |
| 941 | mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_SHA256); |
Boris BREZILLON | b0ef510 | 2016-03-17 10:21:35 +0100 | [diff] [blame] | 942 | creq->state[0] = SHA256_H0; |
| 943 | creq->state[1] = SHA256_H1; |
| 944 | creq->state[2] = SHA256_H2; |
| 945 | creq->state[3] = SHA256_H3; |
| 946 | creq->state[4] = SHA256_H4; |
| 947 | creq->state[5] = SHA256_H5; |
| 948 | creq->state[6] = SHA256_H6; |
| 949 | creq->state[7] = SHA256_H7; |
Arnaud Ebalard | f85a762 | 2015-06-18 15:46:25 +0200 | [diff] [blame] | 950 | |
Russell King | a9eb678 | 2015-10-18 17:23:40 +0100 | [diff] [blame] | 951 | mv_cesa_ahash_init(req, &tmpl, false); |
Arnaud Ebalard | f85a762 | 2015-06-18 15:46:25 +0200 | [diff] [blame] | 952 | |
| 953 | return 0; |
| 954 | } |
| 955 | |
| 956 | static int mv_cesa_sha256_digest(struct ahash_request *req) |
| 957 | { |
| 958 | int ret; |
| 959 | |
| 960 | ret = mv_cesa_sha256_init(req); |
| 961 | if (ret) |
| 962 | return ret; |
| 963 | |
| 964 | return mv_cesa_ahash_finup(req); |
| 965 | } |
| 966 | |
| 967 | static int mv_cesa_sha256_export(struct ahash_request *req, void *out) |
| 968 | { |
| 969 | struct sha256_state *out_state = out; |
Arnaud Ebalard | f85a762 | 2015-06-18 15:46:25 +0200 | [diff] [blame] | 970 | |
Russell King | a6479ea | 2015-10-09 21:14:22 +0100 | [diff] [blame] | 971 | return mv_cesa_ahash_export(req, out_state->state, &out_state->count, |
| 972 | out_state->buf); |
Arnaud Ebalard | f85a762 | 2015-06-18 15:46:25 +0200 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | static int mv_cesa_sha256_import(struct ahash_request *req, const void *in) |
| 976 | { |
| 977 | const struct sha256_state *in_state = in; |
Arnaud Ebalard | f85a762 | 2015-06-18 15:46:25 +0200 | [diff] [blame] | 978 | |
Russell King | a6479ea | 2015-10-09 21:14:22 +0100 | [diff] [blame] | 979 | return mv_cesa_ahash_import(req, in_state->state, in_state->count, |
| 980 | in_state->buf); |
Arnaud Ebalard | f85a762 | 2015-06-18 15:46:25 +0200 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | struct ahash_alg mv_sha256_alg = { |
| 984 | .init = mv_cesa_sha256_init, |
| 985 | .update = mv_cesa_ahash_update, |
| 986 | .final = mv_cesa_ahash_final, |
| 987 | .finup = mv_cesa_ahash_finup, |
| 988 | .digest = mv_cesa_sha256_digest, |
| 989 | .export = mv_cesa_sha256_export, |
| 990 | .import = mv_cesa_sha256_import, |
| 991 | .halg = { |
| 992 | .digestsize = SHA256_DIGEST_SIZE, |
Russell King | 9f5594c | 2015-10-09 20:43:38 +0100 | [diff] [blame] | 993 | .statesize = sizeof(struct sha256_state), |
Arnaud Ebalard | f85a762 | 2015-06-18 15:46:25 +0200 | [diff] [blame] | 994 | .base = { |
| 995 | .cra_name = "sha256", |
| 996 | .cra_driver_name = "mv-sha256", |
| 997 | .cra_priority = 300, |
| 998 | .cra_flags = CRYPTO_ALG_ASYNC | |
| 999 | CRYPTO_ALG_KERN_DRIVER_ONLY, |
| 1000 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 1001 | .cra_ctxsize = sizeof(struct mv_cesa_hash_ctx), |
| 1002 | .cra_init = mv_cesa_ahash_cra_init, |
| 1003 | .cra_module = THIS_MODULE, |
| 1004 | } |
| 1005 | } |
| 1006 | }; |
| 1007 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 1008 | struct mv_cesa_ahash_result { |
| 1009 | struct completion completion; |
| 1010 | int error; |
| 1011 | }; |
| 1012 | |
| 1013 | static void mv_cesa_hmac_ahash_complete(struct crypto_async_request *req, |
| 1014 | int error) |
| 1015 | { |
| 1016 | struct mv_cesa_ahash_result *result = req->data; |
| 1017 | |
| 1018 | if (error == -EINPROGRESS) |
| 1019 | return; |
| 1020 | |
| 1021 | result->error = error; |
| 1022 | complete(&result->completion); |
| 1023 | } |
| 1024 | |
| 1025 | static int mv_cesa_ahmac_iv_state_init(struct ahash_request *req, u8 *pad, |
| 1026 | void *state, unsigned int blocksize) |
| 1027 | { |
| 1028 | struct mv_cesa_ahash_result result; |
| 1029 | struct scatterlist sg; |
| 1030 | int ret; |
| 1031 | |
| 1032 | ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 1033 | mv_cesa_hmac_ahash_complete, &result); |
| 1034 | sg_init_one(&sg, pad, blocksize); |
| 1035 | ahash_request_set_crypt(req, &sg, pad, blocksize); |
| 1036 | init_completion(&result.completion); |
| 1037 | |
| 1038 | ret = crypto_ahash_init(req); |
| 1039 | if (ret) |
| 1040 | return ret; |
| 1041 | |
| 1042 | ret = crypto_ahash_update(req); |
| 1043 | if (ret && ret != -EINPROGRESS) |
| 1044 | return ret; |
| 1045 | |
| 1046 | wait_for_completion_interruptible(&result.completion); |
| 1047 | if (result.error) |
| 1048 | return result.error; |
| 1049 | |
| 1050 | ret = crypto_ahash_export(req, state); |
| 1051 | if (ret) |
| 1052 | return ret; |
| 1053 | |
| 1054 | return 0; |
| 1055 | } |
| 1056 | |
| 1057 | static int mv_cesa_ahmac_pad_init(struct ahash_request *req, |
| 1058 | const u8 *key, unsigned int keylen, |
| 1059 | u8 *ipad, u8 *opad, |
| 1060 | unsigned int blocksize) |
| 1061 | { |
| 1062 | struct mv_cesa_ahash_result result; |
| 1063 | struct scatterlist sg; |
| 1064 | int ret; |
| 1065 | int i; |
| 1066 | |
| 1067 | if (keylen <= blocksize) { |
| 1068 | memcpy(ipad, key, keylen); |
| 1069 | } else { |
| 1070 | u8 *keydup = kmemdup(key, keylen, GFP_KERNEL); |
| 1071 | |
| 1072 | if (!keydup) |
| 1073 | return -ENOMEM; |
| 1074 | |
| 1075 | ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 1076 | mv_cesa_hmac_ahash_complete, |
| 1077 | &result); |
| 1078 | sg_init_one(&sg, keydup, keylen); |
| 1079 | ahash_request_set_crypt(req, &sg, ipad, keylen); |
| 1080 | init_completion(&result.completion); |
| 1081 | |
| 1082 | ret = crypto_ahash_digest(req); |
| 1083 | if (ret == -EINPROGRESS) { |
| 1084 | wait_for_completion_interruptible(&result.completion); |
| 1085 | ret = result.error; |
| 1086 | } |
| 1087 | |
| 1088 | /* Set the memory region to 0 to avoid any leak. */ |
| 1089 | memset(keydup, 0, keylen); |
| 1090 | kfree(keydup); |
| 1091 | |
| 1092 | if (ret) |
| 1093 | return ret; |
| 1094 | |
| 1095 | keylen = crypto_ahash_digestsize(crypto_ahash_reqtfm(req)); |
| 1096 | } |
| 1097 | |
| 1098 | memset(ipad + keylen, 0, blocksize - keylen); |
| 1099 | memcpy(opad, ipad, blocksize); |
| 1100 | |
| 1101 | for (i = 0; i < blocksize; i++) { |
| 1102 | ipad[i] ^= 0x36; |
| 1103 | opad[i] ^= 0x5c; |
| 1104 | } |
| 1105 | |
| 1106 | return 0; |
| 1107 | } |
| 1108 | |
| 1109 | static int mv_cesa_ahmac_setkey(const char *hash_alg_name, |
| 1110 | const u8 *key, unsigned int keylen, |
| 1111 | void *istate, void *ostate) |
| 1112 | { |
| 1113 | struct ahash_request *req; |
| 1114 | struct crypto_ahash *tfm; |
| 1115 | unsigned int blocksize; |
| 1116 | u8 *ipad = NULL; |
| 1117 | u8 *opad; |
| 1118 | int ret; |
| 1119 | |
| 1120 | tfm = crypto_alloc_ahash(hash_alg_name, CRYPTO_ALG_TYPE_AHASH, |
| 1121 | CRYPTO_ALG_TYPE_AHASH_MASK); |
| 1122 | if (IS_ERR(tfm)) |
| 1123 | return PTR_ERR(tfm); |
| 1124 | |
| 1125 | req = ahash_request_alloc(tfm, GFP_KERNEL); |
| 1126 | if (!req) { |
| 1127 | ret = -ENOMEM; |
| 1128 | goto free_ahash; |
| 1129 | } |
| 1130 | |
| 1131 | crypto_ahash_clear_flags(tfm, ~0); |
| 1132 | |
| 1133 | blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm)); |
| 1134 | |
| 1135 | ipad = kzalloc(2 * blocksize, GFP_KERNEL); |
| 1136 | if (!ipad) { |
| 1137 | ret = -ENOMEM; |
| 1138 | goto free_req; |
| 1139 | } |
| 1140 | |
| 1141 | opad = ipad + blocksize; |
| 1142 | |
| 1143 | ret = mv_cesa_ahmac_pad_init(req, key, keylen, ipad, opad, blocksize); |
| 1144 | if (ret) |
| 1145 | goto free_ipad; |
| 1146 | |
| 1147 | ret = mv_cesa_ahmac_iv_state_init(req, ipad, istate, blocksize); |
| 1148 | if (ret) |
| 1149 | goto free_ipad; |
| 1150 | |
| 1151 | ret = mv_cesa_ahmac_iv_state_init(req, opad, ostate, blocksize); |
| 1152 | |
| 1153 | free_ipad: |
| 1154 | kfree(ipad); |
| 1155 | free_req: |
| 1156 | ahash_request_free(req); |
| 1157 | free_ahash: |
| 1158 | crypto_free_ahash(tfm); |
| 1159 | |
| 1160 | return ret; |
| 1161 | } |
| 1162 | |
| 1163 | static int mv_cesa_ahmac_cra_init(struct crypto_tfm *tfm) |
| 1164 | { |
| 1165 | struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(tfm); |
| 1166 | |
| 1167 | ctx->base.ops = &mv_cesa_ahash_req_ops; |
| 1168 | |
| 1169 | crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), |
| 1170 | sizeof(struct mv_cesa_ahash_req)); |
| 1171 | return 0; |
| 1172 | } |
| 1173 | |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 1174 | static int mv_cesa_ahmac_md5_init(struct ahash_request *req) |
| 1175 | { |
| 1176 | struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
Russell King | d30cb2f | 2015-10-18 17:23:51 +0100 | [diff] [blame] | 1177 | struct mv_cesa_op_ctx tmpl = { }; |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 1178 | |
| 1179 | mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_HMAC_MD5); |
| 1180 | memcpy(tmpl.ctx.hash.iv, ctx->iv, sizeof(ctx->iv)); |
| 1181 | |
Russell King | a9eb678 | 2015-10-18 17:23:40 +0100 | [diff] [blame] | 1182 | mv_cesa_ahash_init(req, &tmpl, true); |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 1183 | |
| 1184 | return 0; |
| 1185 | } |
| 1186 | |
| 1187 | static int mv_cesa_ahmac_md5_setkey(struct crypto_ahash *tfm, const u8 *key, |
| 1188 | unsigned int keylen) |
| 1189 | { |
| 1190 | struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm)); |
| 1191 | struct md5_state istate, ostate; |
| 1192 | int ret, i; |
| 1193 | |
| 1194 | ret = mv_cesa_ahmac_setkey("mv-md5", key, keylen, &istate, &ostate); |
| 1195 | if (ret) |
| 1196 | return ret; |
| 1197 | |
| 1198 | for (i = 0; i < ARRAY_SIZE(istate.hash); i++) |
| 1199 | ctx->iv[i] = be32_to_cpu(istate.hash[i]); |
| 1200 | |
| 1201 | for (i = 0; i < ARRAY_SIZE(ostate.hash); i++) |
| 1202 | ctx->iv[i + 8] = be32_to_cpu(ostate.hash[i]); |
| 1203 | |
| 1204 | return 0; |
| 1205 | } |
| 1206 | |
| 1207 | static int mv_cesa_ahmac_md5_digest(struct ahash_request *req) |
| 1208 | { |
| 1209 | int ret; |
| 1210 | |
| 1211 | ret = mv_cesa_ahmac_md5_init(req); |
| 1212 | if (ret) |
| 1213 | return ret; |
| 1214 | |
| 1215 | return mv_cesa_ahash_finup(req); |
| 1216 | } |
| 1217 | |
| 1218 | struct ahash_alg mv_ahmac_md5_alg = { |
| 1219 | .init = mv_cesa_ahmac_md5_init, |
| 1220 | .update = mv_cesa_ahash_update, |
| 1221 | .final = mv_cesa_ahash_final, |
| 1222 | .finup = mv_cesa_ahash_finup, |
| 1223 | .digest = mv_cesa_ahmac_md5_digest, |
| 1224 | .setkey = mv_cesa_ahmac_md5_setkey, |
| 1225 | .export = mv_cesa_md5_export, |
| 1226 | .import = mv_cesa_md5_import, |
| 1227 | .halg = { |
| 1228 | .digestsize = MD5_DIGEST_SIZE, |
| 1229 | .statesize = sizeof(struct md5_state), |
| 1230 | .base = { |
| 1231 | .cra_name = "hmac(md5)", |
| 1232 | .cra_driver_name = "mv-hmac-md5", |
| 1233 | .cra_priority = 300, |
| 1234 | .cra_flags = CRYPTO_ALG_ASYNC | |
| 1235 | CRYPTO_ALG_KERN_DRIVER_ONLY, |
| 1236 | .cra_blocksize = MD5_HMAC_BLOCK_SIZE, |
| 1237 | .cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx), |
| 1238 | .cra_init = mv_cesa_ahmac_cra_init, |
| 1239 | .cra_module = THIS_MODULE, |
| 1240 | } |
| 1241 | } |
| 1242 | }; |
| 1243 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 1244 | static int mv_cesa_ahmac_sha1_init(struct ahash_request *req) |
| 1245 | { |
| 1246 | struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
Russell King | d30cb2f | 2015-10-18 17:23:51 +0100 | [diff] [blame] | 1247 | struct mv_cesa_op_ctx tmpl = { }; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 1248 | |
| 1249 | mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_HMAC_SHA1); |
| 1250 | memcpy(tmpl.ctx.hash.iv, ctx->iv, sizeof(ctx->iv)); |
| 1251 | |
Russell King | a9eb678 | 2015-10-18 17:23:40 +0100 | [diff] [blame] | 1252 | mv_cesa_ahash_init(req, &tmpl, false); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 1253 | |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | static int mv_cesa_ahmac_sha1_setkey(struct crypto_ahash *tfm, const u8 *key, |
| 1258 | unsigned int keylen) |
| 1259 | { |
| 1260 | struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm)); |
| 1261 | struct sha1_state istate, ostate; |
| 1262 | int ret, i; |
| 1263 | |
| 1264 | ret = mv_cesa_ahmac_setkey("mv-sha1", key, keylen, &istate, &ostate); |
| 1265 | if (ret) |
| 1266 | return ret; |
| 1267 | |
| 1268 | for (i = 0; i < ARRAY_SIZE(istate.state); i++) |
| 1269 | ctx->iv[i] = be32_to_cpu(istate.state[i]); |
| 1270 | |
| 1271 | for (i = 0; i < ARRAY_SIZE(ostate.state); i++) |
| 1272 | ctx->iv[i + 8] = be32_to_cpu(ostate.state[i]); |
| 1273 | |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
| 1277 | static int mv_cesa_ahmac_sha1_digest(struct ahash_request *req) |
| 1278 | { |
| 1279 | int ret; |
| 1280 | |
| 1281 | ret = mv_cesa_ahmac_sha1_init(req); |
| 1282 | if (ret) |
| 1283 | return ret; |
| 1284 | |
| 1285 | return mv_cesa_ahash_finup(req); |
| 1286 | } |
| 1287 | |
| 1288 | struct ahash_alg mv_ahmac_sha1_alg = { |
| 1289 | .init = mv_cesa_ahmac_sha1_init, |
| 1290 | .update = mv_cesa_ahash_update, |
| 1291 | .final = mv_cesa_ahash_final, |
| 1292 | .finup = mv_cesa_ahash_finup, |
| 1293 | .digest = mv_cesa_ahmac_sha1_digest, |
| 1294 | .setkey = mv_cesa_ahmac_sha1_setkey, |
| 1295 | .export = mv_cesa_sha1_export, |
| 1296 | .import = mv_cesa_sha1_import, |
| 1297 | .halg = { |
| 1298 | .digestsize = SHA1_DIGEST_SIZE, |
| 1299 | .statesize = sizeof(struct sha1_state), |
| 1300 | .base = { |
| 1301 | .cra_name = "hmac(sha1)", |
| 1302 | .cra_driver_name = "mv-hmac-sha1", |
| 1303 | .cra_priority = 300, |
| 1304 | .cra_flags = CRYPTO_ALG_ASYNC | |
| 1305 | CRYPTO_ALG_KERN_DRIVER_ONLY, |
| 1306 | .cra_blocksize = SHA1_BLOCK_SIZE, |
| 1307 | .cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx), |
| 1308 | .cra_init = mv_cesa_ahmac_cra_init, |
| 1309 | .cra_module = THIS_MODULE, |
| 1310 | } |
| 1311 | } |
| 1312 | }; |
Arnaud Ebalard | f85a762 | 2015-06-18 15:46:25 +0200 | [diff] [blame] | 1313 | |
| 1314 | static int mv_cesa_ahmac_sha256_setkey(struct crypto_ahash *tfm, const u8 *key, |
| 1315 | unsigned int keylen) |
| 1316 | { |
| 1317 | struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm)); |
| 1318 | struct sha256_state istate, ostate; |
| 1319 | int ret, i; |
| 1320 | |
| 1321 | ret = mv_cesa_ahmac_setkey("mv-sha256", key, keylen, &istate, &ostate); |
| 1322 | if (ret) |
| 1323 | return ret; |
| 1324 | |
| 1325 | for (i = 0; i < ARRAY_SIZE(istate.state); i++) |
| 1326 | ctx->iv[i] = be32_to_cpu(istate.state[i]); |
| 1327 | |
| 1328 | for (i = 0; i < ARRAY_SIZE(ostate.state); i++) |
| 1329 | ctx->iv[i + 8] = be32_to_cpu(ostate.state[i]); |
| 1330 | |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
| 1334 | static int mv_cesa_ahmac_sha256_init(struct ahash_request *req) |
| 1335 | { |
| 1336 | struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
Russell King | d30cb2f | 2015-10-18 17:23:51 +0100 | [diff] [blame] | 1337 | struct mv_cesa_op_ctx tmpl = { }; |
Arnaud Ebalard | f85a762 | 2015-06-18 15:46:25 +0200 | [diff] [blame] | 1338 | |
| 1339 | mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_HMAC_SHA256); |
| 1340 | memcpy(tmpl.ctx.hash.iv, ctx->iv, sizeof(ctx->iv)); |
| 1341 | |
Russell King | a9eb678 | 2015-10-18 17:23:40 +0100 | [diff] [blame] | 1342 | mv_cesa_ahash_init(req, &tmpl, false); |
Arnaud Ebalard | f85a762 | 2015-06-18 15:46:25 +0200 | [diff] [blame] | 1343 | |
| 1344 | return 0; |
| 1345 | } |
| 1346 | |
| 1347 | static int mv_cesa_ahmac_sha256_digest(struct ahash_request *req) |
| 1348 | { |
| 1349 | int ret; |
| 1350 | |
| 1351 | ret = mv_cesa_ahmac_sha256_init(req); |
| 1352 | if (ret) |
| 1353 | return ret; |
| 1354 | |
| 1355 | return mv_cesa_ahash_finup(req); |
| 1356 | } |
| 1357 | |
| 1358 | struct ahash_alg mv_ahmac_sha256_alg = { |
| 1359 | .init = mv_cesa_ahmac_sha256_init, |
| 1360 | .update = mv_cesa_ahash_update, |
| 1361 | .final = mv_cesa_ahash_final, |
| 1362 | .finup = mv_cesa_ahash_finup, |
| 1363 | .digest = mv_cesa_ahmac_sha256_digest, |
| 1364 | .setkey = mv_cesa_ahmac_sha256_setkey, |
| 1365 | .export = mv_cesa_sha256_export, |
| 1366 | .import = mv_cesa_sha256_import, |
| 1367 | .halg = { |
| 1368 | .digestsize = SHA256_DIGEST_SIZE, |
| 1369 | .statesize = sizeof(struct sha256_state), |
| 1370 | .base = { |
| 1371 | .cra_name = "hmac(sha256)", |
| 1372 | .cra_driver_name = "mv-hmac-sha256", |
| 1373 | .cra_priority = 300, |
| 1374 | .cra_flags = CRYPTO_ALG_ASYNC | |
| 1375 | CRYPTO_ALG_KERN_DRIVER_ONLY, |
| 1376 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 1377 | .cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx), |
| 1378 | .cra_init = mv_cesa_ahmac_cra_init, |
| 1379 | .cra_module = THIS_MODULE, |
| 1380 | } |
| 1381 | } |
| 1382 | }; |