blob: 03aaa39d6fd60b1a350575a84a453a7bc6d04c73 [file] [log] [blame]
Boris BREZILLONf63601f2015-06-18 15:46:20 +02001/*
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 Ebalard7aeef692015-06-18 15:46:24 +020015#include <crypto/md5.h>
Boris BREZILLONf63601f2015-06-18 15:46:20 +020016#include <crypto/sha.h>
17
18#include "cesa.h"
19
Boris BREZILLONdb509a42015-06-18 15:46:21 +020020struct mv_cesa_ahash_dma_iter {
21 struct mv_cesa_dma_iter base;
22 struct mv_cesa_sg_dma_iter src;
23};
24
25static inline void
26mv_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 Kingbd274b12015-10-18 17:24:26 +010030 unsigned int len = req->nbytes + creq->cache_ptr;
Boris BREZILLONdb509a42015-06-18 15:46:21 +020031
32 if (!creq->last_req)
Russell Kingbd274b12015-10-18 17:24:26 +010033 len &= ~CESA_HASH_BLOCK_SIZE_MSK;
Boris BREZILLONdb509a42015-06-18 15:46:21 +020034
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
40static inline bool
41mv_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 BREZILLON7850c912016-03-17 10:21:34 +010048static inline int
49mv_cesa_ahash_dma_alloc_cache(struct mv_cesa_ahash_dma_req *req, gfp_t flags)
Boris BREZILLONdb509a42015-06-18 15:46:21 +020050{
Boris BREZILLON7850c912016-03-17 10:21:34 +010051 req->cache = dma_pool_alloc(cesa_dev->dma->cache_pool, flags,
52 &req->cache_dma);
53 if (!req->cache)
Boris BREZILLONdb509a42015-06-18 15:46:21 +020054 return -ENOMEM;
55
56 return 0;
57}
58
Boris BREZILLON7850c912016-03-17 10:21:34 +010059static inline void
60mv_cesa_ahash_dma_free_cache(struct mv_cesa_ahash_dma_req *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +020061{
Boris BREZILLON7850c912016-03-17 10:21:34 +010062 if (!req->cache)
Boris BREZILLONf63601f2015-06-18 15:46:20 +020063 return;
64
Boris BREZILLON7850c912016-03-17 10:21:34 +010065 dma_pool_free(cesa_dev->dma->cache_pool, req->cache,
66 req->cache_dma);
Boris BREZILLONf63601f2015-06-18 15:46:20 +020067}
68
Boris BREZILLONdb509a42015-06-18 15:46:21 +020069static 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
83static 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
93static 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
100static 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 BREZILLON7850c912016-03-17 10:21:34 +0100105 mv_cesa_ahash_dma_free_cache(&creq->req.dma);
Romain Perier53da7402016-06-21 10:08:35 +0200106 mv_cesa_dma_cleanup(&creq->base);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200107}
108
109static inline void mv_cesa_ahash_cleanup(struct ahash_request *req)
110{
111 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
112
Romain Perier53da7402016-06-21 10:08:35 +0200113 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200114 mv_cesa_ahash_dma_cleanup(req);
115}
116
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200117static void mv_cesa_ahash_last_cleanup(struct ahash_request *req)
118{
119 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
120
Romain Perier53da7402016-06-21 10:08:35 +0200121 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200122 mv_cesa_ahash_dma_last_cleanup(req);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200123}
124
125static 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
135static int mv_cesa_ahash_pad_req(struct mv_cesa_ahash_req *creq, u8 *buf)
136{
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200137 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 King51954a92015-10-18 17:23:46 +0100144
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 BREZILLONf63601f2015-06-18 15:46:20 +0200152
153 return padlen + 8;
154}
155
156static 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 Perier53da7402016-06-21 10:08:35 +0200160 struct mv_cesa_engine *engine = creq->base.engine;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200161 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 King0f3304d2015-10-18 18:31:15 +0100167 memcpy_toio(engine->sram + CESA_SA_DATA_SRAM_OFFSET,
168 creq->cache, creq->cache_ptr);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200169
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 King0f3304d2015-10-18 18:31:15 +0100209 memcpy_fromio(creq->cache,
210 engine->sram +
211 CESA_SA_DATA_SRAM_OFFSET + len,
212 new_cache_ptr);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200213 } 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 King0f3304d2015-10-18 18:31:15 +0100230 memcpy_toio(engine->sram, op, sizeof(*op));
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200231
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 Kingb1508562015-10-18 18:31:00 +0100239 writel_relaxed(CESA_SA_CFG_PARA_DIS, engine->regs + CESA_SA_CFG);
Romain Perierf6283082016-06-21 10:08:32 +0200240 BUG_ON(readl(engine->regs + CESA_SA_CMD) &
241 CESA_SA_CMD_EN_CESA_SA_ACCL0);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200242 writel(CESA_SA_CMD_EN_CESA_SA_ACCL0, engine->regs + CESA_SA_CMD);
243}
244
245static 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 BREZILLONdb509a42015-06-18 15:46:21 +0200256static inline void mv_cesa_ahash_dma_prepare(struct ahash_request *req)
257{
258 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
Romain Perier53da7402016-06-21 10:08:35 +0200259 struct mv_cesa_req *basereq = &creq->base;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200260
Romain Perier53da7402016-06-21 10:08:35 +0200261 mv_cesa_dma_prepare(basereq, basereq->engine);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200262}
263
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200264static 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 Perier53da7402016-06-21 10:08:35 +0200268 struct mv_cesa_engine *engine = creq->base.engine;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200269
270 sreq->offset = 0;
271 mv_cesa_adjust_op(engine, &creq->op_tmpl);
Russell King0f3304d2015-10-18 18:31:15 +0100272 memcpy_toio(engine->sram, &creq->op_tmpl, sizeof(creq->op_tmpl));
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200273}
274
275static void mv_cesa_ahash_step(struct crypto_async_request *req)
276{
277 struct ahash_request *ahashreq = ahash_request_cast(req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200278 struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200279
Romain Perier53da7402016-06-21 10:08:35 +0200280 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
281 mv_cesa_dma_step(&creq->base);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200282 else
283 mv_cesa_ahash_std_step(ahashreq);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200284}
285
286static 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 Perier53da7402016-06-21 10:08:35 +0200290 struct mv_cesa_engine *engine = creq->base.engine;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200291 unsigned int digsize;
292 int ret, i;
293
Romain Perier53da7402016-06-21 10:08:35 +0200294 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
295 ret = mv_cesa_dma_process(&creq->base, status);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200296 else
297 ret = mv_cesa_ahash_std_process(ahashreq, status);
298
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200299 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 Kingb1508562015-10-18 18:31:00 +0100304 creq->state[i] = readl_relaxed(engine->regs + CESA_IVDIG(i));
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200305
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 King4c2b1302015-10-18 17:23:35 +0100313 /*
314 * Hardware's MD5 digest is in little endian format, but
315 * SHA in big endian format
316 */
Russell Kinga9eb6782015-10-18 17:23:40 +0100317 if (creq->algo_le) {
Russell King4c2b1302015-10-18 17:23:35 +0100318 __le32 *result = (void *)ahashreq->result;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200319
Russell King4c2b1302015-10-18 17:23:35 +0100320 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 BREZILLONf63601f2015-06-18 15:46:20 +0200328 }
329
330 return ret;
331}
332
333static 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 Perier53da7402016-06-21 10:08:35 +0200341 creq->base.engine = engine;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200342
Romain Perier53da7402016-06-21 10:08:35 +0200343 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200344 mv_cesa_ahash_dma_prepare(ahashreq);
345 else
346 mv_cesa_ahash_std_prepare(ahashreq);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200347
348 digsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(ahashreq));
349 for (i = 0; i < digsize / 4; i++)
Russell Kingb1508562015-10-18 18:31:00 +0100350 writel_relaxed(creq->state[i], engine->regs + CESA_IVDIG(i));
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200351}
352
353static 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 BREZILLONdb509a42015-06-18 15:46:21 +0200360
361 mv_cesa_ahash_cleanup(ahashreq);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200362}
363
364static 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
371static int mv_cesa_ahash_init(struct ahash_request *req,
Russell Kinga9eb6782015-10-18 17:23:40 +0100372 struct mv_cesa_op_ctx *tmpl, bool algo_le)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200373{
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 Kinga9eb6782015-10-18 17:23:40 +0100386 creq->algo_le = algo_le;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200387
388 return 0;
389}
390
391static 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
402static 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 BREZILLONf63601f2015-06-18 15:46:20 +0200405
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 BREZILLONdb509a42015-06-18 15:46:21 +0200422static struct mv_cesa_op_ctx *
Russell King96212882015-10-18 17:24:06 +0100423mv_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 King2f396a92015-10-18 17:24:11 +0100442 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 King96212882015-10-18 17:24:06 +0100447 return op;
448}
449
Russell King0971d092015-10-18 17:24:16 +0100450static int
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200451mv_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 BREZILLON7850c912016-03-17 10:21:34 +0100457 int ret;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200458
459 if (!creq->cache_ptr)
Russell King0971d092015-10-18 17:24:16 +0100460 return 0;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200461
Boris BREZILLON7850c912016-03-17 10:21:34 +0100462 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 King0971d092015-10-18 17:24:16 +0100468 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 BREZILLONdb509a42015-06-18 15:46:21 +0200474}
475
476static struct mv_cesa_op_ctx *
477mv_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 King58953e12015-10-18 17:24:37 +0100480 unsigned int frag_len, gfp_t flags)
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200481{
482 struct mv_cesa_ahash_dma_req *ahashdreq = &creq->req.dma;
483 unsigned int len, trailerlen, padoff = 0;
Russell King58953e12015-10-18 17:24:37 +0100484 struct mv_cesa_op_ctx *op;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200485 int ret;
486
Russell Kingaee84a72015-10-18 17:24:42 +0100487 /*
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 BREZILLONdb509a42015-06-18 15:46:21 +0200496
Russell Kingaee84a72015-10-18 17:24:42 +0100497 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 BREZILLONdb509a42015-06-18 15:46:21 +0200502
503 return op;
504 }
505
Russell Kingaee84a72015-10-18 17:24:42 +0100506 /*
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 BREZILLONdb509a42015-06-18 15:46:21 +0200511 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 Kingab270e72015-10-18 17:24:47 +0100517 len = min(CESA_SA_SRAM_PAYLOAD_SIZE - frag_len, trailerlen);
518 if (len) {
519 ret = mv_cesa_dma_add_data_transfer(chain,
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200520 CESA_SA_DATA_SRAM_OFFSET +
Russell Kingab270e72015-10-18 17:24:47 +0100521 frag_len,
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200522 ahashdreq->padding_dma,
523 len, CESA_TDMA_DST_IN_SRAM,
524 flags);
Russell Kingab270e72015-10-18 17:24:47 +0100525 if (ret)
526 return ERR_PTR(ret);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200527
Russell Kingab270e72015-10-18 17:24:47 +0100528 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 Kingab270e72015-10-18 17:24:47 +0100533 if (len == trailerlen)
534 return op;
535
536 padoff += len;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200537 }
538
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200539 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 King96212882015-10-18 17:24:06 +0100549 return mv_cesa_dma_add_frag(chain, &creq->op_tmpl, trailerlen - padoff,
550 flags);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200551}
552
553static 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 Perier53da7402016-06-21 10:08:35 +0200558 struct mv_cesa_req *basereq = &creq->base;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200559 struct mv_cesa_ahash_dma_iter iter;
560 struct mv_cesa_op_ctx *op = NULL;
Russell Kinge41bbeb2015-10-18 17:24:32 +0100561 unsigned int frag_len;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200562 int ret;
563
Romain Perier53da7402016-06-21 10:08:35 +0200564 basereq->chain.first = NULL;
565 basereq->chain.last = NULL;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200566
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 Perier53da7402016-06-21 10:08:35 +0200576 mv_cesa_tdma_desc_iter_init(&basereq->chain);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200577 mv_cesa_ahash_req_iter_init(&iter, req);
578
Russell King0971d092015-10-18 17:24:16 +0100579 /*
580 * Add the cache (left-over data from a previous block) first.
581 * This will never overflow the SRAM size.
582 */
Romain Perier53da7402016-06-21 10:08:35 +0200583 ret = mv_cesa_ahash_dma_add_cache(&basereq->chain, &iter, creq, flags);
Russell King0971d092015-10-18 17:24:16 +0100584 if (ret)
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200585 goto err_free_tdma;
Russell King0971d092015-10-18 17:24:16 +0100586
Russell Kingd9bba4c2015-10-18 17:24:21 +0100587 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 Kinge41bbeb2015-10-18 17:24:32 +0100591 * data. We intentionally do not add the final op block.
Russell Kingd9bba4c2015-10-18 17:24:21 +0100592 */
Russell Kinge41bbeb2015-10-18 17:24:32 +0100593 while (true) {
Romain Perier53da7402016-06-21 10:08:35 +0200594 ret = mv_cesa_dma_add_op_transfers(&basereq->chain,
Boris Brezillon8c07f3a2015-10-18 17:24:57 +0100595 &iter.base,
Russell Kingd9bba4c2015-10-18 17:24:21 +0100596 &iter.src, flags);
597 if (ret)
598 goto err_free_tdma;
599
Russell Kinge41bbeb2015-10-18 17:24:32 +0100600 frag_len = iter.base.op_len;
601
602 if (!mv_cesa_ahash_req_iter_next_op(&iter))
603 break;
604
Romain Perier53da7402016-06-21 10:08:35 +0200605 op = mv_cesa_dma_add_frag(&basereq->chain, &creq->op_tmpl,
Russell Kinge41bbeb2015-10-18 17:24:32 +0100606 frag_len, flags);
Russell Kingd9bba4c2015-10-18 17:24:21 +0100607 if (IS_ERR(op)) {
608 ret = PTR_ERR(op);
609 goto err_free_tdma;
610 }
Russell Kinge41bbeb2015-10-18 17:24:32 +0100611 }
612 } else {
Russell Kingd9bba4c2015-10-18 17:24:21 +0100613 /* Account for the data that was in the cache. */
Russell Kinge41bbeb2015-10-18 17:24:32 +0100614 frag_len = iter.base.op_len;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200615 }
616
Russell King58953e12015-10-18 17:24:37 +0100617 /*
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 Perier53da7402016-06-21 10:08:35 +0200623 op = mv_cesa_ahash_dma_last_req(&basereq->chain, &iter, creq,
Boris Brezillon8c07f3a2015-10-18 17:24:57 +0100624 frag_len, flags);
Russell King58953e12015-10-18 17:24:37 +0100625 else if (frag_len)
Romain Perier53da7402016-06-21 10:08:35 +0200626 op = mv_cesa_dma_add_frag(&basereq->chain, &creq->op_tmpl,
Boris Brezillon8c07f3a2015-10-18 17:24:57 +0100627 frag_len, flags);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200628
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200629 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 Perier53da7402016-06-21 10:08:35 +0200636 ret = mv_cesa_dma_add_dummy_end(&basereq->chain, flags);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200637 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 BREZILLONdb509a42015-06-18 15:46:21 +0200647 return 0;
648
649err_free_tdma:
Romain Perier53da7402016-06-21 10:08:35 +0200650 mv_cesa_dma_cleanup(basereq);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200651 dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents, DMA_TO_DEVICE);
652
653err:
654 mv_cesa_ahash_last_cleanup(req);
655
656 return ret;
657}
658
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200659static 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 BREZILLONdb509a42015-06-18 15:46:21 +0200662 int ret;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200663
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200664 creq->src_nents = sg_nents_for_len(req->src, req->nbytes);
LABBE Corentinc22dafb2015-11-04 21:13:33 +0100665 if (creq->src_nents < 0) {
666 dev_err(cesa_dev->dev, "Invalid number of src SG");
667 return creq->src_nents;
668 }
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200669
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200670 ret = mv_cesa_ahash_cache_req(req, cached);
671 if (ret)
672 return ret;
673
674 if (*cached)
675 return 0;
676
Romain Perier53da7402016-06-21 10:08:35 +0200677 if (cesa_dev->caps->has_tdma)
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200678 ret = mv_cesa_ahash_dma_req_init(req);
679
680 return ret;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200681}
682
683static 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 Perier53da7402016-06-21 10:08:35 +0200697 ret = mv_cesa_queue_req(&req->base, &creq->base);
Thomas Petazzonicfcd2272015-09-18 17:25:36 +0200698 if (mv_cesa_req_needs_cleanup(&req->base, ret))
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200699 mv_cesa_ahash_cleanup(req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200700
701 return ret;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200702}
703
704static 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 Perier53da7402016-06-21 10:08:35 +0200722 ret = mv_cesa_queue_req(&req->base, &creq->base);
Thomas Petazzonicfcd2272015-09-18 17:25:36 +0200723 if (mv_cesa_req_needs_cleanup(&req->base, ret))
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200724 mv_cesa_ahash_cleanup(req);
725
726 return ret;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200727}
728
729static 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 Perier53da7402016-06-21 10:08:35 +0200747 ret = mv_cesa_queue_req(&req->base, &creq->base);
Thomas Petazzonicfcd2272015-09-18 17:25:36 +0200748 if (mv_cesa_req_needs_cleanup(&req->base, ret))
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200749 mv_cesa_ahash_cleanup(req);
750
751 return ret;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200752}
753
Russell Kinga6479ea2015-10-09 21:14:22 +0100754static int mv_cesa_ahash_export(struct ahash_request *req, void *hash,
755 u64 *len, void *cache)
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200756{
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200757 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 Kinga6479ea2015-10-09 21:14:22 +0100760 unsigned int blocksize;
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200761
Russell King80754532015-10-18 17:23:30 +0100762 blocksize = crypto_ahash_blocksize(ahash);
Russell Kinga6479ea2015-10-09 21:14:22 +0100763
764 *len = creq->len;
765 memcpy(hash, creq->state, digsize);
766 memset(cache, 0, blocksize);
Dan Carpenter063327f2016-03-21 12:03:43 +0300767 memcpy(cache, creq->cache, creq->cache_ptr);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200768
769 return 0;
770}
771
Russell Kinga6479ea2015-10-09 21:14:22 +0100772static int mv_cesa_ahash_import(struct ahash_request *req, const void *hash,
773 u64 len, const void *cache)
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200774{
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200775 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 Kinga6479ea2015-10-09 21:14:22 +0100778 unsigned int blocksize;
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200779 unsigned int cache_ptr;
780 int ret;
781
Russell Kinga6479ea2015-10-09 21:14:22 +0100782 ret = crypto_ahash_init(req);
783 if (ret)
784 return ret;
785
Russell King80754532015-10-18 17:23:30 +0100786 blocksize = crypto_ahash_blocksize(ahash);
Russell Kinga6479ea2015-10-09 21:14:22 +0100787 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 Ebalard7aeef692015-06-18 15:46:24 +0200794 creq->cache_ptr = 0;
795
Russell Kinga6479ea2015-10-09 21:14:22 +0100796 cache_ptr = do_div(len, blocksize);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200797 if (!cache_ptr)
798 return 0;
799
Russell Kinga6479ea2015-10-09 21:14:22 +0100800 memcpy(creq->cache, cache, cache_ptr);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200801 creq->cache_ptr = cache_ptr;
802
803 return 0;
804}
805
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200806static int mv_cesa_md5_init(struct ahash_request *req)
807{
Boris BREZILLONb0ef5102016-03-17 10:21:35 +0100808 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
Russell Kingd30cb2f2015-10-18 17:23:51 +0100809 struct mv_cesa_op_ctx tmpl = { };
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200810
811 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_MD5);
Boris BREZILLONb0ef5102016-03-17 10:21:35 +0100812 creq->state[0] = MD5_H0;
813 creq->state[1] = MD5_H1;
814 creq->state[2] = MD5_H2;
815 creq->state[3] = MD5_H3;
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200816
Russell Kinga9eb6782015-10-18 17:23:40 +0100817 mv_cesa_ahash_init(req, &tmpl, true);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200818
819 return 0;
820}
821
822static int mv_cesa_md5_export(struct ahash_request *req, void *out)
823{
824 struct md5_state *out_state = out;
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200825
Russell Kinga6479ea2015-10-09 21:14:22 +0100826 return mv_cesa_ahash_export(req, out_state->hash,
827 &out_state->byte_count, out_state->block);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200828}
829
830static int mv_cesa_md5_import(struct ahash_request *req, const void *in)
831{
832 const struct md5_state *in_state = in;
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200833
Russell Kinga6479ea2015-10-09 21:14:22 +0100834 return mv_cesa_ahash_import(req, in_state->hash, in_state->byte_count,
835 in_state->block);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200836}
837
838static 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
849struct 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 King9f5594c2015-10-09 20:43:38 +0100859 .statesize = sizeof(struct md5_state),
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200860 .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 BREZILLONf63601f2015-06-18 15:46:20 +0200874static int mv_cesa_sha1_init(struct ahash_request *req)
875{
Boris BREZILLONb0ef5102016-03-17 10:21:35 +0100876 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
Russell Kingd30cb2f2015-10-18 17:23:51 +0100877 struct mv_cesa_op_ctx tmpl = { };
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200878
879 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_SHA1);
Boris BREZILLONb0ef5102016-03-17 10:21:35 +0100880 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 BREZILLONf63601f2015-06-18 15:46:20 +0200885
Russell Kinga9eb6782015-10-18 17:23:40 +0100886 mv_cesa_ahash_init(req, &tmpl, false);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200887
888 return 0;
889}
890
891static int mv_cesa_sha1_export(struct ahash_request *req, void *out)
892{
893 struct sha1_state *out_state = out;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200894
Russell Kinga6479ea2015-10-09 21:14:22 +0100895 return mv_cesa_ahash_export(req, out_state->state, &out_state->count,
896 out_state->buffer);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200897}
898
899static int mv_cesa_sha1_import(struct ahash_request *req, const void *in)
900{
901 const struct sha1_state *in_state = in;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200902
Russell Kinga6479ea2015-10-09 21:14:22 +0100903 return mv_cesa_ahash_import(req, in_state->state, in_state->count,
904 in_state->buffer);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200905}
906
907static 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
918struct 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 King9f5594c2015-10-09 20:43:38 +0100928 .statesize = sizeof(struct sha1_state),
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200929 .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 Ebalardf85a7622015-06-18 15:46:25 +0200943static int mv_cesa_sha256_init(struct ahash_request *req)
944{
Boris BREZILLONb0ef5102016-03-17 10:21:35 +0100945 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
Russell Kingd30cb2f2015-10-18 17:23:51 +0100946 struct mv_cesa_op_ctx tmpl = { };
Arnaud Ebalardf85a7622015-06-18 15:46:25 +0200947
948 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_SHA256);
Boris BREZILLONb0ef5102016-03-17 10:21:35 +0100949 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 Ebalardf85a7622015-06-18 15:46:25 +0200957
Russell Kinga9eb6782015-10-18 17:23:40 +0100958 mv_cesa_ahash_init(req, &tmpl, false);
Arnaud Ebalardf85a7622015-06-18 15:46:25 +0200959
960 return 0;
961}
962
963static 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
974static int mv_cesa_sha256_export(struct ahash_request *req, void *out)
975{
976 struct sha256_state *out_state = out;
Arnaud Ebalardf85a7622015-06-18 15:46:25 +0200977
Russell Kinga6479ea2015-10-09 21:14:22 +0100978 return mv_cesa_ahash_export(req, out_state->state, &out_state->count,
979 out_state->buf);
Arnaud Ebalardf85a7622015-06-18 15:46:25 +0200980}
981
982static int mv_cesa_sha256_import(struct ahash_request *req, const void *in)
983{
984 const struct sha256_state *in_state = in;
Arnaud Ebalardf85a7622015-06-18 15:46:25 +0200985
Russell Kinga6479ea2015-10-09 21:14:22 +0100986 return mv_cesa_ahash_import(req, in_state->state, in_state->count,
987 in_state->buf);
Arnaud Ebalardf85a7622015-06-18 15:46:25 +0200988}
989
990struct 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 King9f5594c2015-10-09 20:43:38 +01001000 .statesize = sizeof(struct sha256_state),
Arnaud Ebalardf85a7622015-06-18 15:46:25 +02001001 .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 BREZILLONf63601f2015-06-18 15:46:20 +02001015struct mv_cesa_ahash_result {
1016 struct completion completion;
1017 int error;
1018};
1019
1020static 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
1032static 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
1064static 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
1116static 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
1160free_ipad:
1161 kfree(ipad);
1162free_req:
1163 ahash_request_free(req);
1164free_ahash:
1165 crypto_free_ahash(tfm);
1166
1167 return ret;
1168}
1169
1170static 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 Ebalard7aeef692015-06-18 15:46:24 +02001181static 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 Kingd30cb2f2015-10-18 17:23:51 +01001184 struct mv_cesa_op_ctx tmpl = { };
Arnaud Ebalard7aeef692015-06-18 15:46:24 +02001185
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 Kinga9eb6782015-10-18 17:23:40 +01001189 mv_cesa_ahash_init(req, &tmpl, true);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +02001190
1191 return 0;
1192}
1193
1194static 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
1214static 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
1225struct 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 BREZILLONf63601f2015-06-18 15:46:20 +02001251static 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 Kingd30cb2f2015-10-18 17:23:51 +01001254 struct mv_cesa_op_ctx tmpl = { };
Boris BREZILLONf63601f2015-06-18 15:46:20 +02001255
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 Kinga9eb6782015-10-18 17:23:40 +01001259 mv_cesa_ahash_init(req, &tmpl, false);
Boris BREZILLONf63601f2015-06-18 15:46:20 +02001260
1261 return 0;
1262}
1263
1264static 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
1284static 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
1295struct 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 Ebalardf85a7622015-06-18 15:46:25 +02001320
1321static 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
1341static 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 Kingd30cb2f2015-10-18 17:23:51 +01001344 struct mv_cesa_op_ctx tmpl = { };
Arnaud Ebalardf85a7622015-06-18 15:46:25 +02001345
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 Kinga9eb6782015-10-18 17:23:40 +01001349 mv_cesa_ahash_init(req, &tmpl, false);
Arnaud Ebalardf85a7622015-06-18 15:46:25 +02001350
1351 return 0;
1352}
1353
1354static 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
1365struct 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};