blob: 729166435a8d64f2eb0d79f6b049cec57e5c7e0d [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;
Romain Perier2786cee2016-06-21 10:08:37 +0200165 unsigned int digsize;
166 int i;
167
168 mv_cesa_adjust_op(engine, &creq->op_tmpl);
169 memcpy_toio(engine->sram, &creq->op_tmpl, sizeof(creq->op_tmpl));
170
171 digsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(req));
172 for (i = 0; i < digsize / 4; i++)
173 writel_relaxed(creq->state[i], engine->regs + CESA_IVDIG(i));
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200174
Romain Perier85030c52016-06-21 10:08:39 +0200175 mv_cesa_adjust_op(engine, &creq->op_tmpl);
176 memcpy_toio(engine->sram, &creq->op_tmpl, sizeof(creq->op_tmpl));
177
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200178 if (creq->cache_ptr)
Russell King0f3304d2015-10-18 18:31:15 +0100179 memcpy_toio(engine->sram + CESA_SA_DATA_SRAM_OFFSET,
180 creq->cache, creq->cache_ptr);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200181
182 len = min_t(size_t, req->nbytes + creq->cache_ptr - sreq->offset,
183 CESA_SA_SRAM_PAYLOAD_SIZE);
184
185 if (!creq->last_req) {
186 new_cache_ptr = len & CESA_HASH_BLOCK_SIZE_MSK;
187 len &= ~CESA_HASH_BLOCK_SIZE_MSK;
188 }
189
190 if (len - creq->cache_ptr)
191 sreq->offset += sg_pcopy_to_buffer(req->src, creq->src_nents,
192 engine->sram +
193 CESA_SA_DATA_SRAM_OFFSET +
194 creq->cache_ptr,
195 len - creq->cache_ptr,
196 sreq->offset);
197
198 op = &creq->op_tmpl;
199
200 frag_mode = mv_cesa_get_op_cfg(op) & CESA_SA_DESC_CFG_FRAG_MSK;
201
202 if (creq->last_req && sreq->offset == req->nbytes &&
203 creq->len <= CESA_SA_DESC_MAC_SRC_TOTAL_LEN_MAX) {
204 if (frag_mode == CESA_SA_DESC_CFG_FIRST_FRAG)
205 frag_mode = CESA_SA_DESC_CFG_NOT_FRAG;
206 else if (frag_mode == CESA_SA_DESC_CFG_MID_FRAG)
207 frag_mode = CESA_SA_DESC_CFG_LAST_FRAG;
208 }
209
210 if (frag_mode == CESA_SA_DESC_CFG_NOT_FRAG ||
211 frag_mode == CESA_SA_DESC_CFG_LAST_FRAG) {
212 if (len &&
213 creq->len <= CESA_SA_DESC_MAC_SRC_TOTAL_LEN_MAX) {
214 mv_cesa_set_mac_op_total_len(op, creq->len);
215 } else {
216 int trailerlen = mv_cesa_ahash_pad_len(creq) + 8;
217
218 if (len + trailerlen > CESA_SA_SRAM_PAYLOAD_SIZE) {
219 len &= CESA_HASH_BLOCK_SIZE_MSK;
220 new_cache_ptr = 64 - trailerlen;
Russell King0f3304d2015-10-18 18:31:15 +0100221 memcpy_fromio(creq->cache,
222 engine->sram +
223 CESA_SA_DATA_SRAM_OFFSET + len,
224 new_cache_ptr);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200225 } else {
226 len += mv_cesa_ahash_pad_req(creq,
227 engine->sram + len +
228 CESA_SA_DATA_SRAM_OFFSET);
229 }
230
231 if (frag_mode == CESA_SA_DESC_CFG_LAST_FRAG)
232 frag_mode = CESA_SA_DESC_CFG_MID_FRAG;
233 else
234 frag_mode = CESA_SA_DESC_CFG_FIRST_FRAG;
235 }
236 }
237
238 mv_cesa_set_mac_op_frag_len(op, len);
239 mv_cesa_update_op_cfg(op, frag_mode, CESA_SA_DESC_CFG_FRAG_MSK);
240
241 /* FIXME: only update enc_len field */
Russell King0f3304d2015-10-18 18:31:15 +0100242 memcpy_toio(engine->sram, op, sizeof(*op));
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200243
244 if (frag_mode == CESA_SA_DESC_CFG_FIRST_FRAG)
245 mv_cesa_update_op_cfg(op, CESA_SA_DESC_CFG_MID_FRAG,
246 CESA_SA_DESC_CFG_FRAG_MSK);
247
248 creq->cache_ptr = new_cache_ptr;
249
250 mv_cesa_set_int_mask(engine, CESA_SA_INT_ACCEL0_DONE);
Russell Kingb1508562015-10-18 18:31:00 +0100251 writel_relaxed(CESA_SA_CFG_PARA_DIS, engine->regs + CESA_SA_CFG);
Romain Perierf6283082016-06-21 10:08:32 +0200252 BUG_ON(readl(engine->regs + CESA_SA_CMD) &
253 CESA_SA_CMD_EN_CESA_SA_ACCL0);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200254 writel(CESA_SA_CMD_EN_CESA_SA_ACCL0, engine->regs + CESA_SA_CMD);
255}
256
257static int mv_cesa_ahash_std_process(struct ahash_request *req, u32 status)
258{
259 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
260 struct mv_cesa_ahash_std_req *sreq = &creq->req.std;
261
262 if (sreq->offset < (req->nbytes - creq->cache_ptr))
263 return -EINPROGRESS;
264
265 return 0;
266}
267
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200268static inline void mv_cesa_ahash_dma_prepare(struct ahash_request *req)
269{
270 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
Romain Perier53da7402016-06-21 10:08:35 +0200271 struct mv_cesa_req *basereq = &creq->base;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200272
Romain Perier53da7402016-06-21 10:08:35 +0200273 mv_cesa_dma_prepare(basereq, basereq->engine);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200274}
275
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200276static void mv_cesa_ahash_std_prepare(struct ahash_request *req)
277{
278 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
279 struct mv_cesa_ahash_std_req *sreq = &creq->req.std;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200280
281 sreq->offset = 0;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200282}
283
284static void mv_cesa_ahash_step(struct crypto_async_request *req)
285{
286 struct ahash_request *ahashreq = ahash_request_cast(req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200287 struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200288
Romain Perier53da7402016-06-21 10:08:35 +0200289 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
290 mv_cesa_dma_step(&creq->base);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200291 else
292 mv_cesa_ahash_std_step(ahashreq);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200293}
294
295static int mv_cesa_ahash_process(struct crypto_async_request *req, u32 status)
296{
297 struct ahash_request *ahashreq = ahash_request_cast(req);
298 struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200299
Romain Perier53da7402016-06-21 10:08:35 +0200300 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
Romain Perier1bf66822016-06-21 10:08:36 +0200301 return mv_cesa_dma_process(&creq->base, status);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200302
Romain Perier1bf66822016-06-21 10:08:36 +0200303 return mv_cesa_ahash_std_process(ahashreq, status);
304}
305
306static void mv_cesa_ahash_complete(struct crypto_async_request *req)
307{
308 struct ahash_request *ahashreq = ahash_request_cast(req);
309 struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
310 struct mv_cesa_engine *engine = creq->base.engine;
311 unsigned int digsize;
312 int i;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200313
314 digsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(ahashreq));
315 for (i = 0; i < digsize / 4; i++)
Russell Kingb1508562015-10-18 18:31:00 +0100316 creq->state[i] = readl_relaxed(engine->regs + CESA_IVDIG(i));
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200317
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200318 if (creq->last_req) {
Russell King4c2b1302015-10-18 17:23:35 +0100319 /*
320 * Hardware's MD5 digest is in little endian format, but
321 * SHA in big endian format
322 */
Russell Kinga9eb6782015-10-18 17:23:40 +0100323 if (creq->algo_le) {
Russell King4c2b1302015-10-18 17:23:35 +0100324 __le32 *result = (void *)ahashreq->result;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200325
Russell King4c2b1302015-10-18 17:23:35 +0100326 for (i = 0; i < digsize / 4; i++)
327 result[i] = cpu_to_le32(creq->state[i]);
328 } else {
329 __be32 *result = (void *)ahashreq->result;
330
331 for (i = 0; i < digsize / 4; i++)
332 result[i] = cpu_to_be32(creq->state[i]);
333 }
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200334 }
Romain Perierbf8f91e2016-06-21 10:08:38 +0200335
336 atomic_sub(ahashreq->nbytes, &engine->load);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200337}
338
339static void mv_cesa_ahash_prepare(struct crypto_async_request *req,
340 struct mv_cesa_engine *engine)
341{
342 struct ahash_request *ahashreq = ahash_request_cast(req);
343 struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200344
Romain Perier53da7402016-06-21 10:08:35 +0200345 creq->base.engine = engine;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200346
Romain Perier53da7402016-06-21 10:08:35 +0200347 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200348 mv_cesa_ahash_dma_prepare(ahashreq);
349 else
350 mv_cesa_ahash_std_prepare(ahashreq);
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);
Romain Perier64ec6cc2016-07-22 15:46:24 +0200362
363 if (creq->cache_ptr)
364 sg_pcopy_to_buffer(ahashreq->src, creq->src_nents,
365 creq->cache,
366 creq->cache_ptr,
367 ahashreq->nbytes - creq->cache_ptr);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200368}
369
370static const struct mv_cesa_req_ops mv_cesa_ahash_req_ops = {
371 .step = mv_cesa_ahash_step,
372 .process = mv_cesa_ahash_process,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200373 .cleanup = mv_cesa_ahash_req_cleanup,
Romain Perier1bf66822016-06-21 10:08:36 +0200374 .complete = mv_cesa_ahash_complete,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200375};
376
Thomas Petazzoni3e5c66c2016-08-09 11:03:16 +0200377static void mv_cesa_ahash_init(struct ahash_request *req,
Russell Kinga9eb6782015-10-18 17:23:40 +0100378 struct mv_cesa_op_ctx *tmpl, bool algo_le)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200379{
380 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
381
382 memset(creq, 0, sizeof(*creq));
383 mv_cesa_update_op_cfg(tmpl,
384 CESA_SA_DESC_CFG_OP_MAC_ONLY |
385 CESA_SA_DESC_CFG_FIRST_FRAG,
386 CESA_SA_DESC_CFG_OP_MSK |
387 CESA_SA_DESC_CFG_FRAG_MSK);
388 mv_cesa_set_mac_op_total_len(tmpl, 0);
389 mv_cesa_set_mac_op_frag_len(tmpl, 0);
390 creq->op_tmpl = *tmpl;
391 creq->len = 0;
Russell Kinga9eb6782015-10-18 17:23:40 +0100392 creq->algo_le = algo_le;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200393}
394
395static inline int mv_cesa_ahash_cra_init(struct crypto_tfm *tfm)
396{
397 struct mv_cesa_hash_ctx *ctx = crypto_tfm_ctx(tfm);
398
399 ctx->base.ops = &mv_cesa_ahash_req_ops;
400
401 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
402 sizeof(struct mv_cesa_ahash_req));
403 return 0;
404}
405
406static int mv_cesa_ahash_cache_req(struct ahash_request *req, bool *cached)
407{
408 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200409
410 if (creq->cache_ptr + req->nbytes < 64 && !creq->last_req) {
411 *cached = true;
412
413 if (!req->nbytes)
414 return 0;
415
416 sg_pcopy_to_buffer(req->src, creq->src_nents,
417 creq->cache + creq->cache_ptr,
418 req->nbytes, 0);
419
420 creq->cache_ptr += req->nbytes;
421 }
422
423 return 0;
424}
425
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200426static struct mv_cesa_op_ctx *
Russell King96212882015-10-18 17:24:06 +0100427mv_cesa_dma_add_frag(struct mv_cesa_tdma_chain *chain,
428 struct mv_cesa_op_ctx *tmpl, unsigned int frag_len,
429 gfp_t flags)
430{
431 struct mv_cesa_op_ctx *op;
432 int ret;
433
434 op = mv_cesa_dma_add_op(chain, tmpl, false, flags);
435 if (IS_ERR(op))
436 return op;
437
438 /* Set the operation block fragment length. */
439 mv_cesa_set_mac_op_frag_len(op, frag_len);
440
441 /* Append dummy desc to launch operation */
442 ret = mv_cesa_dma_add_dummy_launch(chain, flags);
443 if (ret)
444 return ERR_PTR(ret);
445
Russell King2f396a92015-10-18 17:24:11 +0100446 if (mv_cesa_mac_op_is_first_frag(tmpl))
447 mv_cesa_update_op_cfg(tmpl,
448 CESA_SA_DESC_CFG_MID_FRAG,
449 CESA_SA_DESC_CFG_FRAG_MSK);
450
Russell King96212882015-10-18 17:24:06 +0100451 return op;
452}
453
Russell King0971d092015-10-18 17:24:16 +0100454static int
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200455mv_cesa_ahash_dma_add_cache(struct mv_cesa_tdma_chain *chain,
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200456 struct mv_cesa_ahash_req *creq,
457 gfp_t flags)
458{
459 struct mv_cesa_ahash_dma_req *ahashdreq = &creq->req.dma;
Boris BREZILLON7850c912016-03-17 10:21:34 +0100460 int ret;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200461
462 if (!creq->cache_ptr)
Russell King0971d092015-10-18 17:24:16 +0100463 return 0;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200464
Boris BREZILLON7850c912016-03-17 10:21:34 +0100465 ret = mv_cesa_ahash_dma_alloc_cache(ahashdreq, flags);
466 if (ret)
467 return ret;
468
469 memcpy(ahashdreq->cache, creq->cache, creq->cache_ptr);
470
Russell King0971d092015-10-18 17:24:16 +0100471 return mv_cesa_dma_add_data_transfer(chain,
472 CESA_SA_DATA_SRAM_OFFSET,
473 ahashdreq->cache_dma,
474 creq->cache_ptr,
475 CESA_TDMA_DST_IN_SRAM,
476 flags);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200477}
478
479static struct mv_cesa_op_ctx *
480mv_cesa_ahash_dma_last_req(struct mv_cesa_tdma_chain *chain,
481 struct mv_cesa_ahash_dma_iter *dma_iter,
482 struct mv_cesa_ahash_req *creq,
Russell King58953e12015-10-18 17:24:37 +0100483 unsigned int frag_len, gfp_t flags)
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200484{
485 struct mv_cesa_ahash_dma_req *ahashdreq = &creq->req.dma;
486 unsigned int len, trailerlen, padoff = 0;
Russell King58953e12015-10-18 17:24:37 +0100487 struct mv_cesa_op_ctx *op;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200488 int ret;
489
Russell Kingaee84a72015-10-18 17:24:42 +0100490 /*
491 * If the transfer is smaller than our maximum length, and we have
492 * some data outstanding, we can ask the engine to finish the hash.
493 */
494 if (creq->len <= CESA_SA_DESC_MAC_SRC_TOTAL_LEN_MAX && frag_len) {
495 op = mv_cesa_dma_add_frag(chain, &creq->op_tmpl, frag_len,
496 flags);
497 if (IS_ERR(op))
498 return op;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200499
Russell Kingaee84a72015-10-18 17:24:42 +0100500 mv_cesa_set_mac_op_total_len(op, creq->len);
501 mv_cesa_update_op_cfg(op, mv_cesa_mac_op_is_first_frag(op) ?
502 CESA_SA_DESC_CFG_NOT_FRAG :
503 CESA_SA_DESC_CFG_LAST_FRAG,
504 CESA_SA_DESC_CFG_FRAG_MSK);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200505
506 return op;
507 }
508
Russell Kingaee84a72015-10-18 17:24:42 +0100509 /*
510 * The request is longer than the engine can handle, or we have
511 * no data outstanding. Manually generate the padding, adding it
512 * as a "mid" fragment.
513 */
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200514 ret = mv_cesa_ahash_dma_alloc_padding(ahashdreq, flags);
515 if (ret)
516 return ERR_PTR(ret);
517
518 trailerlen = mv_cesa_ahash_pad_req(creq, ahashdreq->padding);
519
Russell Kingab270e72015-10-18 17:24:47 +0100520 len = min(CESA_SA_SRAM_PAYLOAD_SIZE - frag_len, trailerlen);
521 if (len) {
522 ret = mv_cesa_dma_add_data_transfer(chain,
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200523 CESA_SA_DATA_SRAM_OFFSET +
Russell Kingab270e72015-10-18 17:24:47 +0100524 frag_len,
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200525 ahashdreq->padding_dma,
526 len, CESA_TDMA_DST_IN_SRAM,
527 flags);
Russell Kingab270e72015-10-18 17:24:47 +0100528 if (ret)
529 return ERR_PTR(ret);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200530
Russell Kingab270e72015-10-18 17:24:47 +0100531 op = mv_cesa_dma_add_frag(chain, &creq->op_tmpl, frag_len + len,
532 flags);
533 if (IS_ERR(op))
534 return op;
535
Russell Kingab270e72015-10-18 17:24:47 +0100536 if (len == trailerlen)
537 return op;
538
539 padoff += len;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200540 }
541
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200542 ret = mv_cesa_dma_add_data_transfer(chain,
543 CESA_SA_DATA_SRAM_OFFSET,
544 ahashdreq->padding_dma +
545 padoff,
546 trailerlen - padoff,
547 CESA_TDMA_DST_IN_SRAM,
548 flags);
549 if (ret)
550 return ERR_PTR(ret);
551
Russell King96212882015-10-18 17:24:06 +0100552 return mv_cesa_dma_add_frag(chain, &creq->op_tmpl, trailerlen - padoff,
553 flags);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200554}
555
556static int mv_cesa_ahash_dma_req_init(struct ahash_request *req)
557{
558 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
559 gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
560 GFP_KERNEL : GFP_ATOMIC;
Romain Perier53da7402016-06-21 10:08:35 +0200561 struct mv_cesa_req *basereq = &creq->base;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200562 struct mv_cesa_ahash_dma_iter iter;
563 struct mv_cesa_op_ctx *op = NULL;
Russell Kinge41bbeb2015-10-18 17:24:32 +0100564 unsigned int frag_len;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200565 int ret;
566
Romain Perier53da7402016-06-21 10:08:35 +0200567 basereq->chain.first = NULL;
568 basereq->chain.last = NULL;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200569
570 if (creq->src_nents) {
571 ret = dma_map_sg(cesa_dev->dev, req->src, creq->src_nents,
572 DMA_TO_DEVICE);
573 if (!ret) {
574 ret = -ENOMEM;
575 goto err;
576 }
577 }
578
Romain Perier53da7402016-06-21 10:08:35 +0200579 mv_cesa_tdma_desc_iter_init(&basereq->chain);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200580 mv_cesa_ahash_req_iter_init(&iter, req);
581
Russell King0971d092015-10-18 17:24:16 +0100582 /*
583 * Add the cache (left-over data from a previous block) first.
584 * This will never overflow the SRAM size.
585 */
Thomas Petazzoni2a8a7852016-08-09 11:03:15 +0200586 ret = mv_cesa_ahash_dma_add_cache(&basereq->chain, creq, flags);
Russell King0971d092015-10-18 17:24:16 +0100587 if (ret)
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200588 goto err_free_tdma;
Russell King0971d092015-10-18 17:24:16 +0100589
Russell Kingd9bba4c2015-10-18 17:24:21 +0100590 if (iter.src.sg) {
591 /*
592 * Add all the new data, inserting an operation block and
593 * launch command between each full SRAM block-worth of
Russell Kinge41bbeb2015-10-18 17:24:32 +0100594 * data. We intentionally do not add the final op block.
Russell Kingd9bba4c2015-10-18 17:24:21 +0100595 */
Russell Kinge41bbeb2015-10-18 17:24:32 +0100596 while (true) {
Romain Perier53da7402016-06-21 10:08:35 +0200597 ret = mv_cesa_dma_add_op_transfers(&basereq->chain,
Boris Brezillon8c07f3a2015-10-18 17:24:57 +0100598 &iter.base,
Russell Kingd9bba4c2015-10-18 17:24:21 +0100599 &iter.src, flags);
600 if (ret)
601 goto err_free_tdma;
602
Russell Kinge41bbeb2015-10-18 17:24:32 +0100603 frag_len = iter.base.op_len;
604
605 if (!mv_cesa_ahash_req_iter_next_op(&iter))
606 break;
607
Romain Perier53da7402016-06-21 10:08:35 +0200608 op = mv_cesa_dma_add_frag(&basereq->chain, &creq->op_tmpl,
Russell Kinge41bbeb2015-10-18 17:24:32 +0100609 frag_len, flags);
Russell Kingd9bba4c2015-10-18 17:24:21 +0100610 if (IS_ERR(op)) {
611 ret = PTR_ERR(op);
612 goto err_free_tdma;
613 }
Russell Kinge41bbeb2015-10-18 17:24:32 +0100614 }
615 } else {
Russell Kingd9bba4c2015-10-18 17:24:21 +0100616 /* Account for the data that was in the cache. */
Russell Kinge41bbeb2015-10-18 17:24:32 +0100617 frag_len = iter.base.op_len;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200618 }
619
Russell King58953e12015-10-18 17:24:37 +0100620 /*
621 * At this point, frag_len indicates whether we have any data
622 * outstanding which needs an operation. Queue up the final
623 * operation, which depends whether this is the final request.
624 */
625 if (creq->last_req)
Romain Perier53da7402016-06-21 10:08:35 +0200626 op = mv_cesa_ahash_dma_last_req(&basereq->chain, &iter, creq,
Boris Brezillon8c07f3a2015-10-18 17:24:57 +0100627 frag_len, flags);
Russell King58953e12015-10-18 17:24:37 +0100628 else if (frag_len)
Romain Perier53da7402016-06-21 10:08:35 +0200629 op = mv_cesa_dma_add_frag(&basereq->chain, &creq->op_tmpl,
Boris Brezillon8c07f3a2015-10-18 17:24:57 +0100630 frag_len, flags);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200631
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200632 if (IS_ERR(op)) {
633 ret = PTR_ERR(op);
634 goto err_free_tdma;
635 }
636
637 if (op) {
638 /* Add dummy desc to wait for crypto operation end */
Romain Perier53da7402016-06-21 10:08:35 +0200639 ret = mv_cesa_dma_add_dummy_end(&basereq->chain, flags);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200640 if (ret)
641 goto err_free_tdma;
642 }
643
644 if (!creq->last_req)
645 creq->cache_ptr = req->nbytes + creq->cache_ptr -
646 iter.base.len;
647 else
648 creq->cache_ptr = 0;
649
Romain Perier85030c52016-06-21 10:08:39 +0200650 basereq->chain.last->flags |= (CESA_TDMA_END_OF_REQ |
651 CESA_TDMA_BREAK_CHAIN);
652
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200653 return 0;
654
655err_free_tdma:
Romain Perier53da7402016-06-21 10:08:35 +0200656 mv_cesa_dma_cleanup(basereq);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200657 dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents, DMA_TO_DEVICE);
658
659err:
660 mv_cesa_ahash_last_cleanup(req);
661
662 return ret;
663}
664
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200665static int mv_cesa_ahash_req_init(struct ahash_request *req, bool *cached)
666{
667 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200668 int ret;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200669
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200670 creq->src_nents = sg_nents_for_len(req->src, req->nbytes);
LABBE Corentinc22dafb2015-11-04 21:13:33 +0100671 if (creq->src_nents < 0) {
672 dev_err(cesa_dev->dev, "Invalid number of src SG");
673 return creq->src_nents;
674 }
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200675
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200676 ret = mv_cesa_ahash_cache_req(req, cached);
677 if (ret)
678 return ret;
679
680 if (*cached)
681 return 0;
682
Romain Perier53da7402016-06-21 10:08:35 +0200683 if (cesa_dev->caps->has_tdma)
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200684 ret = mv_cesa_ahash_dma_req_init(req);
685
686 return ret;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200687}
688
Romain Perierbf8f91e2016-06-21 10:08:38 +0200689static int mv_cesa_ahash_queue_req(struct ahash_request *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200690{
691 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
Romain Perierbf8f91e2016-06-21 10:08:38 +0200692 struct mv_cesa_engine *engine;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200693 bool cached = false;
694 int ret;
695
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200696 ret = mv_cesa_ahash_req_init(req, &cached);
697 if (ret)
698 return ret;
699
700 if (cached)
701 return 0;
702
Romain Perierbf8f91e2016-06-21 10:08:38 +0200703 engine = mv_cesa_select_engine(req->nbytes);
704 mv_cesa_ahash_prepare(&req->base, engine);
705
Romain Perier53da7402016-06-21 10:08:35 +0200706 ret = mv_cesa_queue_req(&req->base, &creq->base);
Romain Perierbf8f91e2016-06-21 10:08:38 +0200707
Thomas Petazzonicfcd2272015-09-18 17:25:36 +0200708 if (mv_cesa_req_needs_cleanup(&req->base, ret))
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200709 mv_cesa_ahash_cleanup(req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200710
711 return ret;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200712}
713
Romain Perierbf8f91e2016-06-21 10:08:38 +0200714static int mv_cesa_ahash_update(struct ahash_request *req)
715{
716 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
717
718 creq->len += req->nbytes;
719
720 return mv_cesa_ahash_queue_req(req);
721}
722
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200723static int mv_cesa_ahash_final(struct ahash_request *req)
724{
725 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
726 struct mv_cesa_op_ctx *tmpl = &creq->op_tmpl;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200727
728 mv_cesa_set_mac_op_total_len(tmpl, creq->len);
729 creq->last_req = true;
730 req->nbytes = 0;
731
Romain Perierbf8f91e2016-06-21 10:08:38 +0200732 return mv_cesa_ahash_queue_req(req);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200733}
734
735static int mv_cesa_ahash_finup(struct ahash_request *req)
736{
737 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
738 struct mv_cesa_op_ctx *tmpl = &creq->op_tmpl;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200739
740 creq->len += req->nbytes;
741 mv_cesa_set_mac_op_total_len(tmpl, creq->len);
742 creq->last_req = true;
743
Romain Perierbf8f91e2016-06-21 10:08:38 +0200744 return mv_cesa_ahash_queue_req(req);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200745}
746
Russell Kinga6479ea2015-10-09 21:14:22 +0100747static int mv_cesa_ahash_export(struct ahash_request *req, void *hash,
748 u64 *len, void *cache)
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200749{
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200750 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
751 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
752 unsigned int digsize = crypto_ahash_digestsize(ahash);
Russell Kinga6479ea2015-10-09 21:14:22 +0100753 unsigned int blocksize;
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200754
Russell King80754532015-10-18 17:23:30 +0100755 blocksize = crypto_ahash_blocksize(ahash);
Russell Kinga6479ea2015-10-09 21:14:22 +0100756
757 *len = creq->len;
758 memcpy(hash, creq->state, digsize);
759 memset(cache, 0, blocksize);
Dan Carpenter063327f2016-03-21 12:03:43 +0300760 memcpy(cache, creq->cache, creq->cache_ptr);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200761
762 return 0;
763}
764
Russell Kinga6479ea2015-10-09 21:14:22 +0100765static int mv_cesa_ahash_import(struct ahash_request *req, const void *hash,
766 u64 len, const void *cache)
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200767{
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200768 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
769 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
770 unsigned int digsize = crypto_ahash_digestsize(ahash);
Russell Kinga6479ea2015-10-09 21:14:22 +0100771 unsigned int blocksize;
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200772 unsigned int cache_ptr;
773 int ret;
774
Russell Kinga6479ea2015-10-09 21:14:22 +0100775 ret = crypto_ahash_init(req);
776 if (ret)
777 return ret;
778
Russell King80754532015-10-18 17:23:30 +0100779 blocksize = crypto_ahash_blocksize(ahash);
Russell Kinga6479ea2015-10-09 21:14:22 +0100780 if (len >= blocksize)
781 mv_cesa_update_op_cfg(&creq->op_tmpl,
782 CESA_SA_DESC_CFG_MID_FRAG,
783 CESA_SA_DESC_CFG_FRAG_MSK);
784
785 creq->len = len;
786 memcpy(creq->state, hash, digsize);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200787 creq->cache_ptr = 0;
788
Russell Kinga6479ea2015-10-09 21:14:22 +0100789 cache_ptr = do_div(len, blocksize);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200790 if (!cache_ptr)
791 return 0;
792
Russell Kinga6479ea2015-10-09 21:14:22 +0100793 memcpy(creq->cache, cache, cache_ptr);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200794 creq->cache_ptr = cache_ptr;
795
796 return 0;
797}
798
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200799static int mv_cesa_md5_init(struct ahash_request *req)
800{
Boris BREZILLONb0ef5102016-03-17 10:21:35 +0100801 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
Russell Kingd30cb2f2015-10-18 17:23:51 +0100802 struct mv_cesa_op_ctx tmpl = { };
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200803
804 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_MD5);
Boris BREZILLONb0ef5102016-03-17 10:21:35 +0100805 creq->state[0] = MD5_H0;
806 creq->state[1] = MD5_H1;
807 creq->state[2] = MD5_H2;
808 creq->state[3] = MD5_H3;
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200809
Russell Kinga9eb6782015-10-18 17:23:40 +0100810 mv_cesa_ahash_init(req, &tmpl, true);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200811
812 return 0;
813}
814
815static int mv_cesa_md5_export(struct ahash_request *req, void *out)
816{
817 struct md5_state *out_state = out;
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200818
Russell Kinga6479ea2015-10-09 21:14:22 +0100819 return mv_cesa_ahash_export(req, out_state->hash,
820 &out_state->byte_count, out_state->block);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200821}
822
823static int mv_cesa_md5_import(struct ahash_request *req, const void *in)
824{
825 const struct md5_state *in_state = in;
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200826
Russell Kinga6479ea2015-10-09 21:14:22 +0100827 return mv_cesa_ahash_import(req, in_state->hash, in_state->byte_count,
828 in_state->block);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200829}
830
831static int mv_cesa_md5_digest(struct ahash_request *req)
832{
833 int ret;
834
835 ret = mv_cesa_md5_init(req);
836 if (ret)
837 return ret;
838
839 return mv_cesa_ahash_finup(req);
840}
841
842struct ahash_alg mv_md5_alg = {
843 .init = mv_cesa_md5_init,
844 .update = mv_cesa_ahash_update,
845 .final = mv_cesa_ahash_final,
846 .finup = mv_cesa_ahash_finup,
847 .digest = mv_cesa_md5_digest,
848 .export = mv_cesa_md5_export,
849 .import = mv_cesa_md5_import,
850 .halg = {
851 .digestsize = MD5_DIGEST_SIZE,
Russell King9f5594c2015-10-09 20:43:38 +0100852 .statesize = sizeof(struct md5_state),
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200853 .base = {
854 .cra_name = "md5",
855 .cra_driver_name = "mv-md5",
856 .cra_priority = 300,
857 .cra_flags = CRYPTO_ALG_ASYNC |
858 CRYPTO_ALG_KERN_DRIVER_ONLY,
859 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
860 .cra_ctxsize = sizeof(struct mv_cesa_hash_ctx),
861 .cra_init = mv_cesa_ahash_cra_init,
862 .cra_module = THIS_MODULE,
863 }
864 }
865};
866
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200867static int mv_cesa_sha1_init(struct ahash_request *req)
868{
Boris BREZILLONb0ef5102016-03-17 10:21:35 +0100869 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
Russell Kingd30cb2f2015-10-18 17:23:51 +0100870 struct mv_cesa_op_ctx tmpl = { };
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200871
872 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_SHA1);
Boris BREZILLONb0ef5102016-03-17 10:21:35 +0100873 creq->state[0] = SHA1_H0;
874 creq->state[1] = SHA1_H1;
875 creq->state[2] = SHA1_H2;
876 creq->state[3] = SHA1_H3;
877 creq->state[4] = SHA1_H4;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200878
Russell Kinga9eb6782015-10-18 17:23:40 +0100879 mv_cesa_ahash_init(req, &tmpl, false);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200880
881 return 0;
882}
883
884static int mv_cesa_sha1_export(struct ahash_request *req, void *out)
885{
886 struct sha1_state *out_state = out;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200887
Russell Kinga6479ea2015-10-09 21:14:22 +0100888 return mv_cesa_ahash_export(req, out_state->state, &out_state->count,
889 out_state->buffer);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200890}
891
892static int mv_cesa_sha1_import(struct ahash_request *req, const void *in)
893{
894 const struct sha1_state *in_state = in;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200895
Russell Kinga6479ea2015-10-09 21:14:22 +0100896 return mv_cesa_ahash_import(req, in_state->state, in_state->count,
897 in_state->buffer);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200898}
899
900static int mv_cesa_sha1_digest(struct ahash_request *req)
901{
902 int ret;
903
904 ret = mv_cesa_sha1_init(req);
905 if (ret)
906 return ret;
907
908 return mv_cesa_ahash_finup(req);
909}
910
911struct ahash_alg mv_sha1_alg = {
912 .init = mv_cesa_sha1_init,
913 .update = mv_cesa_ahash_update,
914 .final = mv_cesa_ahash_final,
915 .finup = mv_cesa_ahash_finup,
916 .digest = mv_cesa_sha1_digest,
917 .export = mv_cesa_sha1_export,
918 .import = mv_cesa_sha1_import,
919 .halg = {
920 .digestsize = SHA1_DIGEST_SIZE,
Russell King9f5594c2015-10-09 20:43:38 +0100921 .statesize = sizeof(struct sha1_state),
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200922 .base = {
923 .cra_name = "sha1",
924 .cra_driver_name = "mv-sha1",
925 .cra_priority = 300,
926 .cra_flags = CRYPTO_ALG_ASYNC |
927 CRYPTO_ALG_KERN_DRIVER_ONLY,
928 .cra_blocksize = SHA1_BLOCK_SIZE,
929 .cra_ctxsize = sizeof(struct mv_cesa_hash_ctx),
930 .cra_init = mv_cesa_ahash_cra_init,
931 .cra_module = THIS_MODULE,
932 }
933 }
934};
935
Arnaud Ebalardf85a7622015-06-18 15:46:25 +0200936static int mv_cesa_sha256_init(struct ahash_request *req)
937{
Boris BREZILLONb0ef5102016-03-17 10:21:35 +0100938 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
Russell Kingd30cb2f2015-10-18 17:23:51 +0100939 struct mv_cesa_op_ctx tmpl = { };
Arnaud Ebalardf85a7622015-06-18 15:46:25 +0200940
941 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_SHA256);
Boris BREZILLONb0ef5102016-03-17 10:21:35 +0100942 creq->state[0] = SHA256_H0;
943 creq->state[1] = SHA256_H1;
944 creq->state[2] = SHA256_H2;
945 creq->state[3] = SHA256_H3;
946 creq->state[4] = SHA256_H4;
947 creq->state[5] = SHA256_H5;
948 creq->state[6] = SHA256_H6;
949 creq->state[7] = SHA256_H7;
Arnaud Ebalardf85a7622015-06-18 15:46:25 +0200950
Russell Kinga9eb6782015-10-18 17:23:40 +0100951 mv_cesa_ahash_init(req, &tmpl, false);
Arnaud Ebalardf85a7622015-06-18 15:46:25 +0200952
953 return 0;
954}
955
956static int mv_cesa_sha256_digest(struct ahash_request *req)
957{
958 int ret;
959
960 ret = mv_cesa_sha256_init(req);
961 if (ret)
962 return ret;
963
964 return mv_cesa_ahash_finup(req);
965}
966
967static int mv_cesa_sha256_export(struct ahash_request *req, void *out)
968{
969 struct sha256_state *out_state = out;
Arnaud Ebalardf85a7622015-06-18 15:46:25 +0200970
Russell Kinga6479ea2015-10-09 21:14:22 +0100971 return mv_cesa_ahash_export(req, out_state->state, &out_state->count,
972 out_state->buf);
Arnaud Ebalardf85a7622015-06-18 15:46:25 +0200973}
974
975static int mv_cesa_sha256_import(struct ahash_request *req, const void *in)
976{
977 const struct sha256_state *in_state = in;
Arnaud Ebalardf85a7622015-06-18 15:46:25 +0200978
Russell Kinga6479ea2015-10-09 21:14:22 +0100979 return mv_cesa_ahash_import(req, in_state->state, in_state->count,
980 in_state->buf);
Arnaud Ebalardf85a7622015-06-18 15:46:25 +0200981}
982
983struct ahash_alg mv_sha256_alg = {
984 .init = mv_cesa_sha256_init,
985 .update = mv_cesa_ahash_update,
986 .final = mv_cesa_ahash_final,
987 .finup = mv_cesa_ahash_finup,
988 .digest = mv_cesa_sha256_digest,
989 .export = mv_cesa_sha256_export,
990 .import = mv_cesa_sha256_import,
991 .halg = {
992 .digestsize = SHA256_DIGEST_SIZE,
Russell King9f5594c2015-10-09 20:43:38 +0100993 .statesize = sizeof(struct sha256_state),
Arnaud Ebalardf85a7622015-06-18 15:46:25 +0200994 .base = {
995 .cra_name = "sha256",
996 .cra_driver_name = "mv-sha256",
997 .cra_priority = 300,
998 .cra_flags = CRYPTO_ALG_ASYNC |
999 CRYPTO_ALG_KERN_DRIVER_ONLY,
1000 .cra_blocksize = SHA256_BLOCK_SIZE,
1001 .cra_ctxsize = sizeof(struct mv_cesa_hash_ctx),
1002 .cra_init = mv_cesa_ahash_cra_init,
1003 .cra_module = THIS_MODULE,
1004 }
1005 }
1006};
1007
Boris BREZILLONf63601f2015-06-18 15:46:20 +02001008struct mv_cesa_ahash_result {
1009 struct completion completion;
1010 int error;
1011};
1012
1013static void mv_cesa_hmac_ahash_complete(struct crypto_async_request *req,
1014 int error)
1015{
1016 struct mv_cesa_ahash_result *result = req->data;
1017
1018 if (error == -EINPROGRESS)
1019 return;
1020
1021 result->error = error;
1022 complete(&result->completion);
1023}
1024
1025static int mv_cesa_ahmac_iv_state_init(struct ahash_request *req, u8 *pad,
1026 void *state, unsigned int blocksize)
1027{
1028 struct mv_cesa_ahash_result result;
1029 struct scatterlist sg;
1030 int ret;
1031
1032 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1033 mv_cesa_hmac_ahash_complete, &result);
1034 sg_init_one(&sg, pad, blocksize);
1035 ahash_request_set_crypt(req, &sg, pad, blocksize);
1036 init_completion(&result.completion);
1037
1038 ret = crypto_ahash_init(req);
1039 if (ret)
1040 return ret;
1041
1042 ret = crypto_ahash_update(req);
1043 if (ret && ret != -EINPROGRESS)
1044 return ret;
1045
1046 wait_for_completion_interruptible(&result.completion);
1047 if (result.error)
1048 return result.error;
1049
1050 ret = crypto_ahash_export(req, state);
1051 if (ret)
1052 return ret;
1053
1054 return 0;
1055}
1056
1057static int mv_cesa_ahmac_pad_init(struct ahash_request *req,
1058 const u8 *key, unsigned int keylen,
1059 u8 *ipad, u8 *opad,
1060 unsigned int blocksize)
1061{
1062 struct mv_cesa_ahash_result result;
1063 struct scatterlist sg;
1064 int ret;
1065 int i;
1066
1067 if (keylen <= blocksize) {
1068 memcpy(ipad, key, keylen);
1069 } else {
1070 u8 *keydup = kmemdup(key, keylen, GFP_KERNEL);
1071
1072 if (!keydup)
1073 return -ENOMEM;
1074
1075 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1076 mv_cesa_hmac_ahash_complete,
1077 &result);
1078 sg_init_one(&sg, keydup, keylen);
1079 ahash_request_set_crypt(req, &sg, ipad, keylen);
1080 init_completion(&result.completion);
1081
1082 ret = crypto_ahash_digest(req);
1083 if (ret == -EINPROGRESS) {
1084 wait_for_completion_interruptible(&result.completion);
1085 ret = result.error;
1086 }
1087
1088 /* Set the memory region to 0 to avoid any leak. */
1089 memset(keydup, 0, keylen);
1090 kfree(keydup);
1091
1092 if (ret)
1093 return ret;
1094
1095 keylen = crypto_ahash_digestsize(crypto_ahash_reqtfm(req));
1096 }
1097
1098 memset(ipad + keylen, 0, blocksize - keylen);
1099 memcpy(opad, ipad, blocksize);
1100
1101 for (i = 0; i < blocksize; i++) {
1102 ipad[i] ^= 0x36;
1103 opad[i] ^= 0x5c;
1104 }
1105
1106 return 0;
1107}
1108
1109static int mv_cesa_ahmac_setkey(const char *hash_alg_name,
1110 const u8 *key, unsigned int keylen,
1111 void *istate, void *ostate)
1112{
1113 struct ahash_request *req;
1114 struct crypto_ahash *tfm;
1115 unsigned int blocksize;
1116 u8 *ipad = NULL;
1117 u8 *opad;
1118 int ret;
1119
1120 tfm = crypto_alloc_ahash(hash_alg_name, CRYPTO_ALG_TYPE_AHASH,
1121 CRYPTO_ALG_TYPE_AHASH_MASK);
1122 if (IS_ERR(tfm))
1123 return PTR_ERR(tfm);
1124
1125 req = ahash_request_alloc(tfm, GFP_KERNEL);
1126 if (!req) {
1127 ret = -ENOMEM;
1128 goto free_ahash;
1129 }
1130
1131 crypto_ahash_clear_flags(tfm, ~0);
1132
1133 blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1134
1135 ipad = kzalloc(2 * blocksize, GFP_KERNEL);
1136 if (!ipad) {
1137 ret = -ENOMEM;
1138 goto free_req;
1139 }
1140
1141 opad = ipad + blocksize;
1142
1143 ret = mv_cesa_ahmac_pad_init(req, key, keylen, ipad, opad, blocksize);
1144 if (ret)
1145 goto free_ipad;
1146
1147 ret = mv_cesa_ahmac_iv_state_init(req, ipad, istate, blocksize);
1148 if (ret)
1149 goto free_ipad;
1150
1151 ret = mv_cesa_ahmac_iv_state_init(req, opad, ostate, blocksize);
1152
1153free_ipad:
1154 kfree(ipad);
1155free_req:
1156 ahash_request_free(req);
1157free_ahash:
1158 crypto_free_ahash(tfm);
1159
1160 return ret;
1161}
1162
1163static int mv_cesa_ahmac_cra_init(struct crypto_tfm *tfm)
1164{
1165 struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(tfm);
1166
1167 ctx->base.ops = &mv_cesa_ahash_req_ops;
1168
1169 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
1170 sizeof(struct mv_cesa_ahash_req));
1171 return 0;
1172}
1173
Arnaud Ebalard7aeef692015-06-18 15:46:24 +02001174static int mv_cesa_ahmac_md5_init(struct ahash_request *req)
1175{
1176 struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Russell Kingd30cb2f2015-10-18 17:23:51 +01001177 struct mv_cesa_op_ctx tmpl = { };
Arnaud Ebalard7aeef692015-06-18 15:46:24 +02001178
1179 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_HMAC_MD5);
1180 memcpy(tmpl.ctx.hash.iv, ctx->iv, sizeof(ctx->iv));
1181
Russell Kinga9eb6782015-10-18 17:23:40 +01001182 mv_cesa_ahash_init(req, &tmpl, true);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +02001183
1184 return 0;
1185}
1186
1187static int mv_cesa_ahmac_md5_setkey(struct crypto_ahash *tfm, const u8 *key,
1188 unsigned int keylen)
1189{
1190 struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1191 struct md5_state istate, ostate;
1192 int ret, i;
1193
1194 ret = mv_cesa_ahmac_setkey("mv-md5", key, keylen, &istate, &ostate);
1195 if (ret)
1196 return ret;
1197
1198 for (i = 0; i < ARRAY_SIZE(istate.hash); i++)
1199 ctx->iv[i] = be32_to_cpu(istate.hash[i]);
1200
1201 for (i = 0; i < ARRAY_SIZE(ostate.hash); i++)
1202 ctx->iv[i + 8] = be32_to_cpu(ostate.hash[i]);
1203
1204 return 0;
1205}
1206
1207static int mv_cesa_ahmac_md5_digest(struct ahash_request *req)
1208{
1209 int ret;
1210
1211 ret = mv_cesa_ahmac_md5_init(req);
1212 if (ret)
1213 return ret;
1214
1215 return mv_cesa_ahash_finup(req);
1216}
1217
1218struct ahash_alg mv_ahmac_md5_alg = {
1219 .init = mv_cesa_ahmac_md5_init,
1220 .update = mv_cesa_ahash_update,
1221 .final = mv_cesa_ahash_final,
1222 .finup = mv_cesa_ahash_finup,
1223 .digest = mv_cesa_ahmac_md5_digest,
1224 .setkey = mv_cesa_ahmac_md5_setkey,
1225 .export = mv_cesa_md5_export,
1226 .import = mv_cesa_md5_import,
1227 .halg = {
1228 .digestsize = MD5_DIGEST_SIZE,
1229 .statesize = sizeof(struct md5_state),
1230 .base = {
1231 .cra_name = "hmac(md5)",
1232 .cra_driver_name = "mv-hmac-md5",
1233 .cra_priority = 300,
1234 .cra_flags = CRYPTO_ALG_ASYNC |
1235 CRYPTO_ALG_KERN_DRIVER_ONLY,
1236 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
1237 .cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx),
1238 .cra_init = mv_cesa_ahmac_cra_init,
1239 .cra_module = THIS_MODULE,
1240 }
1241 }
1242};
1243
Boris BREZILLONf63601f2015-06-18 15:46:20 +02001244static int mv_cesa_ahmac_sha1_init(struct ahash_request *req)
1245{
1246 struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Russell Kingd30cb2f2015-10-18 17:23:51 +01001247 struct mv_cesa_op_ctx tmpl = { };
Boris BREZILLONf63601f2015-06-18 15:46:20 +02001248
1249 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_HMAC_SHA1);
1250 memcpy(tmpl.ctx.hash.iv, ctx->iv, sizeof(ctx->iv));
1251
Russell Kinga9eb6782015-10-18 17:23:40 +01001252 mv_cesa_ahash_init(req, &tmpl, false);
Boris BREZILLONf63601f2015-06-18 15:46:20 +02001253
1254 return 0;
1255}
1256
1257static int mv_cesa_ahmac_sha1_setkey(struct crypto_ahash *tfm, const u8 *key,
1258 unsigned int keylen)
1259{
1260 struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1261 struct sha1_state istate, ostate;
1262 int ret, i;
1263
1264 ret = mv_cesa_ahmac_setkey("mv-sha1", key, keylen, &istate, &ostate);
1265 if (ret)
1266 return ret;
1267
1268 for (i = 0; i < ARRAY_SIZE(istate.state); i++)
1269 ctx->iv[i] = be32_to_cpu(istate.state[i]);
1270
1271 for (i = 0; i < ARRAY_SIZE(ostate.state); i++)
1272 ctx->iv[i + 8] = be32_to_cpu(ostate.state[i]);
1273
1274 return 0;
1275}
1276
1277static int mv_cesa_ahmac_sha1_digest(struct ahash_request *req)
1278{
1279 int ret;
1280
1281 ret = mv_cesa_ahmac_sha1_init(req);
1282 if (ret)
1283 return ret;
1284
1285 return mv_cesa_ahash_finup(req);
1286}
1287
1288struct ahash_alg mv_ahmac_sha1_alg = {
1289 .init = mv_cesa_ahmac_sha1_init,
1290 .update = mv_cesa_ahash_update,
1291 .final = mv_cesa_ahash_final,
1292 .finup = mv_cesa_ahash_finup,
1293 .digest = mv_cesa_ahmac_sha1_digest,
1294 .setkey = mv_cesa_ahmac_sha1_setkey,
1295 .export = mv_cesa_sha1_export,
1296 .import = mv_cesa_sha1_import,
1297 .halg = {
1298 .digestsize = SHA1_DIGEST_SIZE,
1299 .statesize = sizeof(struct sha1_state),
1300 .base = {
1301 .cra_name = "hmac(sha1)",
1302 .cra_driver_name = "mv-hmac-sha1",
1303 .cra_priority = 300,
1304 .cra_flags = CRYPTO_ALG_ASYNC |
1305 CRYPTO_ALG_KERN_DRIVER_ONLY,
1306 .cra_blocksize = SHA1_BLOCK_SIZE,
1307 .cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx),
1308 .cra_init = mv_cesa_ahmac_cra_init,
1309 .cra_module = THIS_MODULE,
1310 }
1311 }
1312};
Arnaud Ebalardf85a7622015-06-18 15:46:25 +02001313
1314static int mv_cesa_ahmac_sha256_setkey(struct crypto_ahash *tfm, const u8 *key,
1315 unsigned int keylen)
1316{
1317 struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1318 struct sha256_state istate, ostate;
1319 int ret, i;
1320
1321 ret = mv_cesa_ahmac_setkey("mv-sha256", key, keylen, &istate, &ostate);
1322 if (ret)
1323 return ret;
1324
1325 for (i = 0; i < ARRAY_SIZE(istate.state); i++)
1326 ctx->iv[i] = be32_to_cpu(istate.state[i]);
1327
1328 for (i = 0; i < ARRAY_SIZE(ostate.state); i++)
1329 ctx->iv[i + 8] = be32_to_cpu(ostate.state[i]);
1330
1331 return 0;
1332}
1333
1334static int mv_cesa_ahmac_sha256_init(struct ahash_request *req)
1335{
1336 struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Russell Kingd30cb2f2015-10-18 17:23:51 +01001337 struct mv_cesa_op_ctx tmpl = { };
Arnaud Ebalardf85a7622015-06-18 15:46:25 +02001338
1339 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_HMAC_SHA256);
1340 memcpy(tmpl.ctx.hash.iv, ctx->iv, sizeof(ctx->iv));
1341
Russell Kinga9eb6782015-10-18 17:23:40 +01001342 mv_cesa_ahash_init(req, &tmpl, false);
Arnaud Ebalardf85a7622015-06-18 15:46:25 +02001343
1344 return 0;
1345}
1346
1347static int mv_cesa_ahmac_sha256_digest(struct ahash_request *req)
1348{
1349 int ret;
1350
1351 ret = mv_cesa_ahmac_sha256_init(req);
1352 if (ret)
1353 return ret;
1354
1355 return mv_cesa_ahash_finup(req);
1356}
1357
1358struct ahash_alg mv_ahmac_sha256_alg = {
1359 .init = mv_cesa_ahmac_sha256_init,
1360 .update = mv_cesa_ahash_update,
1361 .final = mv_cesa_ahash_final,
1362 .finup = mv_cesa_ahash_finup,
1363 .digest = mv_cesa_ahmac_sha256_digest,
1364 .setkey = mv_cesa_ahmac_sha256_setkey,
1365 .export = mv_cesa_sha256_export,
1366 .import = mv_cesa_sha256_import,
1367 .halg = {
1368 .digestsize = SHA256_DIGEST_SIZE,
1369 .statesize = sizeof(struct sha256_state),
1370 .base = {
1371 .cra_name = "hmac(sha256)",
1372 .cra_driver_name = "mv-hmac-sha256",
1373 .cra_priority = 300,
1374 .cra_flags = CRYPTO_ALG_ASYNC |
1375 CRYPTO_ALG_KERN_DRIVER_ONLY,
1376 .cra_blocksize = SHA256_BLOCK_SIZE,
1377 .cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx),
1378 .cra_init = mv_cesa_ahmac_cra_init,
1379 .cra_module = THIS_MODULE,
1380 }
1381 }
1382};