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