blob: 585c90f9f6065200c75b082373a368828805f40b [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
Romain Perier8759fec2016-12-14 15:15:07 +0100284static void mv_cesa_ahash_dma_step(struct ahash_request *req)
285{
286 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
287 struct mv_cesa_req *base = &creq->base;
288
289 /* We must explicitly set the digest state. */
290 if (base->chain.first->flags & CESA_TDMA_SET_STATE) {
291 struct mv_cesa_engine *engine = base->engine;
292 int i;
293
294 /* Set the hash state in the IVDIG regs. */
295 for (i = 0; i < ARRAY_SIZE(creq->state); i++)
296 writel_relaxed(creq->state[i], engine->regs +
297 CESA_IVDIG(i));
298 }
299
300 mv_cesa_dma_step(base);
301}
302
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200303static void mv_cesa_ahash_step(struct crypto_async_request *req)
304{
305 struct ahash_request *ahashreq = ahash_request_cast(req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200306 struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200307
Romain Perier53da7402016-06-21 10:08:35 +0200308 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
Romain Perier8759fec2016-12-14 15:15:07 +0100309 mv_cesa_ahash_dma_step(ahashreq);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200310 else
311 mv_cesa_ahash_std_step(ahashreq);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200312}
313
314static int mv_cesa_ahash_process(struct crypto_async_request *req, u32 status)
315{
316 struct ahash_request *ahashreq = ahash_request_cast(req);
317 struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200318
Romain Perier53da7402016-06-21 10:08:35 +0200319 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
Romain Perier1bf66822016-06-21 10:08:36 +0200320 return mv_cesa_dma_process(&creq->base, status);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200321
Romain Perier1bf66822016-06-21 10:08:36 +0200322 return mv_cesa_ahash_std_process(ahashreq, status);
323}
324
325static void mv_cesa_ahash_complete(struct crypto_async_request *req)
326{
327 struct ahash_request *ahashreq = ahash_request_cast(req);
328 struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
329 struct mv_cesa_engine *engine = creq->base.engine;
330 unsigned int digsize;
331 int i;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200332
333 digsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(ahashreq));
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200334
Romain Perierf34dad12016-10-05 09:56:33 +0200335 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ &&
336 (creq->base.chain.last->flags & CESA_TDMA_TYPE_MSK) == CESA_TDMA_RESULT) {
337 __le32 *data = NULL;
338
Russell King4c2b1302015-10-18 17:23:35 +0100339 /*
Romain Perierf34dad12016-10-05 09:56:33 +0200340 * Result is already in the correct endianess when the SA is
341 * used
Russell King4c2b1302015-10-18 17:23:35 +0100342 */
Romain Perierf34dad12016-10-05 09:56:33 +0200343 data = creq->base.chain.last->op->ctx.hash.hash;
344 for (i = 0; i < digsize / 4; i++)
345 creq->state[i] = cpu_to_le32(data[i]);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200346
Romain Perierf34dad12016-10-05 09:56:33 +0200347 memcpy(ahashreq->result, data, digsize);
348 } else {
349 for (i = 0; i < digsize / 4; i++)
350 creq->state[i] = readl_relaxed(engine->regs +
351 CESA_IVDIG(i));
352 if (creq->last_req) {
353 /*
354 * Hardware's MD5 digest is in little endian format, but
355 * SHA in big endian format
356 */
357 if (creq->algo_le) {
358 __le32 *result = (void *)ahashreq->result;
Russell King4c2b1302015-10-18 17:23:35 +0100359
Romain Perierf34dad12016-10-05 09:56:33 +0200360 for (i = 0; i < digsize / 4; i++)
361 result[i] = cpu_to_le32(creq->state[i]);
362 } else {
363 __be32 *result = (void *)ahashreq->result;
364
365 for (i = 0; i < digsize / 4; i++)
366 result[i] = cpu_to_be32(creq->state[i]);
367 }
Russell King4c2b1302015-10-18 17:23:35 +0100368 }
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200369 }
Romain Perierbf8f91e2016-06-21 10:08:38 +0200370
371 atomic_sub(ahashreq->nbytes, &engine->load);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200372}
373
374static void mv_cesa_ahash_prepare(struct crypto_async_request *req,
375 struct mv_cesa_engine *engine)
376{
377 struct ahash_request *ahashreq = ahash_request_cast(req);
378 struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200379
Romain Perier53da7402016-06-21 10:08:35 +0200380 creq->base.engine = engine;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200381
Romain Perier53da7402016-06-21 10:08:35 +0200382 if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200383 mv_cesa_ahash_dma_prepare(ahashreq);
384 else
385 mv_cesa_ahash_std_prepare(ahashreq);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200386}
387
388static void mv_cesa_ahash_req_cleanup(struct crypto_async_request *req)
389{
390 struct ahash_request *ahashreq = ahash_request_cast(req);
391 struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
392
393 if (creq->last_req)
394 mv_cesa_ahash_last_cleanup(ahashreq);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200395
396 mv_cesa_ahash_cleanup(ahashreq);
Romain Perier64ec6cc2016-07-22 15:46:24 +0200397
398 if (creq->cache_ptr)
399 sg_pcopy_to_buffer(ahashreq->src, creq->src_nents,
400 creq->cache,
401 creq->cache_ptr,
402 ahashreq->nbytes - creq->cache_ptr);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200403}
404
405static const struct mv_cesa_req_ops mv_cesa_ahash_req_ops = {
406 .step = mv_cesa_ahash_step,
407 .process = mv_cesa_ahash_process,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200408 .cleanup = mv_cesa_ahash_req_cleanup,
Romain Perier1bf66822016-06-21 10:08:36 +0200409 .complete = mv_cesa_ahash_complete,
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200410};
411
Thomas Petazzoni3e5c66c2016-08-09 11:03:16 +0200412static void mv_cesa_ahash_init(struct ahash_request *req,
Russell Kinga9eb6782015-10-18 17:23:40 +0100413 struct mv_cesa_op_ctx *tmpl, bool algo_le)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200414{
415 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
416
417 memset(creq, 0, sizeof(*creq));
418 mv_cesa_update_op_cfg(tmpl,
419 CESA_SA_DESC_CFG_OP_MAC_ONLY |
420 CESA_SA_DESC_CFG_FIRST_FRAG,
421 CESA_SA_DESC_CFG_OP_MSK |
422 CESA_SA_DESC_CFG_FRAG_MSK);
423 mv_cesa_set_mac_op_total_len(tmpl, 0);
424 mv_cesa_set_mac_op_frag_len(tmpl, 0);
425 creq->op_tmpl = *tmpl;
426 creq->len = 0;
Russell Kinga9eb6782015-10-18 17:23:40 +0100427 creq->algo_le = algo_le;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200428}
429
430static inline int mv_cesa_ahash_cra_init(struct crypto_tfm *tfm)
431{
432 struct mv_cesa_hash_ctx *ctx = crypto_tfm_ctx(tfm);
433
434 ctx->base.ops = &mv_cesa_ahash_req_ops;
435
436 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
437 sizeof(struct mv_cesa_ahash_req));
438 return 0;
439}
440
Thomas Petazzoni6dc156f2016-08-09 11:03:17 +0200441static bool mv_cesa_ahash_cache_req(struct ahash_request *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200442{
443 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
Thomas Petazzoni6dc156f2016-08-09 11:03:17 +0200444 bool cached = false;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200445
Romain Perier47856202016-08-09 11:03:20 +0200446 if (creq->cache_ptr + req->nbytes < CESA_MAX_HASH_BLOCK_SIZE && !creq->last_req) {
Thomas Petazzoni6dc156f2016-08-09 11:03:17 +0200447 cached = true;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200448
449 if (!req->nbytes)
Thomas Petazzoni6dc156f2016-08-09 11:03:17 +0200450 return cached;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200451
452 sg_pcopy_to_buffer(req->src, creq->src_nents,
453 creq->cache + creq->cache_ptr,
454 req->nbytes, 0);
455
456 creq->cache_ptr += req->nbytes;
457 }
458
Thomas Petazzoni6dc156f2016-08-09 11:03:17 +0200459 return cached;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200460}
461
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200462static struct mv_cesa_op_ctx *
Russell King96212882015-10-18 17:24:06 +0100463mv_cesa_dma_add_frag(struct mv_cesa_tdma_chain *chain,
464 struct mv_cesa_op_ctx *tmpl, unsigned int frag_len,
465 gfp_t flags)
466{
467 struct mv_cesa_op_ctx *op;
468 int ret;
469
470 op = mv_cesa_dma_add_op(chain, tmpl, false, flags);
471 if (IS_ERR(op))
472 return op;
473
474 /* Set the operation block fragment length. */
475 mv_cesa_set_mac_op_frag_len(op, frag_len);
476
477 /* Append dummy desc to launch operation */
478 ret = mv_cesa_dma_add_dummy_launch(chain, flags);
479 if (ret)
480 return ERR_PTR(ret);
481
Russell King2f396a92015-10-18 17:24:11 +0100482 if (mv_cesa_mac_op_is_first_frag(tmpl))
483 mv_cesa_update_op_cfg(tmpl,
484 CESA_SA_DESC_CFG_MID_FRAG,
485 CESA_SA_DESC_CFG_FRAG_MSK);
486
Russell King96212882015-10-18 17:24:06 +0100487 return op;
488}
489
Russell King0971d092015-10-18 17:24:16 +0100490static int
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200491mv_cesa_ahash_dma_add_cache(struct mv_cesa_tdma_chain *chain,
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200492 struct mv_cesa_ahash_req *creq,
493 gfp_t flags)
494{
495 struct mv_cesa_ahash_dma_req *ahashdreq = &creq->req.dma;
Boris BREZILLON7850c912016-03-17 10:21:34 +0100496 int ret;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200497
498 if (!creq->cache_ptr)
Russell King0971d092015-10-18 17:24:16 +0100499 return 0;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200500
Boris BREZILLON7850c912016-03-17 10:21:34 +0100501 ret = mv_cesa_ahash_dma_alloc_cache(ahashdreq, flags);
502 if (ret)
503 return ret;
504
505 memcpy(ahashdreq->cache, creq->cache, creq->cache_ptr);
506
Russell King0971d092015-10-18 17:24:16 +0100507 return mv_cesa_dma_add_data_transfer(chain,
508 CESA_SA_DATA_SRAM_OFFSET,
509 ahashdreq->cache_dma,
510 creq->cache_ptr,
511 CESA_TDMA_DST_IN_SRAM,
512 flags);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200513}
514
515static struct mv_cesa_op_ctx *
516mv_cesa_ahash_dma_last_req(struct mv_cesa_tdma_chain *chain,
517 struct mv_cesa_ahash_dma_iter *dma_iter,
518 struct mv_cesa_ahash_req *creq,
Russell King58953e12015-10-18 17:24:37 +0100519 unsigned int frag_len, gfp_t flags)
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200520{
521 struct mv_cesa_ahash_dma_req *ahashdreq = &creq->req.dma;
522 unsigned int len, trailerlen, padoff = 0;
Russell King58953e12015-10-18 17:24:37 +0100523 struct mv_cesa_op_ctx *op;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200524 int ret;
525
Russell Kingaee84a72015-10-18 17:24:42 +0100526 /*
527 * If the transfer is smaller than our maximum length, and we have
528 * some data outstanding, we can ask the engine to finish the hash.
529 */
530 if (creq->len <= CESA_SA_DESC_MAC_SRC_TOTAL_LEN_MAX && frag_len) {
531 op = mv_cesa_dma_add_frag(chain, &creq->op_tmpl, frag_len,
532 flags);
533 if (IS_ERR(op))
534 return op;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200535
Russell Kingaee84a72015-10-18 17:24:42 +0100536 mv_cesa_set_mac_op_total_len(op, creq->len);
537 mv_cesa_update_op_cfg(op, mv_cesa_mac_op_is_first_frag(op) ?
538 CESA_SA_DESC_CFG_NOT_FRAG :
539 CESA_SA_DESC_CFG_LAST_FRAG,
540 CESA_SA_DESC_CFG_FRAG_MSK);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200541
Romain Perierf34dad12016-10-05 09:56:33 +0200542 ret = mv_cesa_dma_add_result_op(chain,
543 CESA_SA_CFG_SRAM_OFFSET,
544 CESA_SA_DATA_SRAM_OFFSET,
545 CESA_TDMA_SRC_IN_SRAM, flags);
546 if (ret)
547 return ERR_PTR(-ENOMEM);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200548 return op;
549 }
550
Russell Kingaee84a72015-10-18 17:24:42 +0100551 /*
552 * The request is longer than the engine can handle, or we have
553 * no data outstanding. Manually generate the padding, adding it
554 * as a "mid" fragment.
555 */
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200556 ret = mv_cesa_ahash_dma_alloc_padding(ahashdreq, flags);
557 if (ret)
558 return ERR_PTR(ret);
559
560 trailerlen = mv_cesa_ahash_pad_req(creq, ahashdreq->padding);
561
Russell Kingab270e72015-10-18 17:24:47 +0100562 len = min(CESA_SA_SRAM_PAYLOAD_SIZE - frag_len, trailerlen);
563 if (len) {
564 ret = mv_cesa_dma_add_data_transfer(chain,
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200565 CESA_SA_DATA_SRAM_OFFSET +
Russell Kingab270e72015-10-18 17:24:47 +0100566 frag_len,
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200567 ahashdreq->padding_dma,
568 len, CESA_TDMA_DST_IN_SRAM,
569 flags);
Russell Kingab270e72015-10-18 17:24:47 +0100570 if (ret)
571 return ERR_PTR(ret);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200572
Russell Kingab270e72015-10-18 17:24:47 +0100573 op = mv_cesa_dma_add_frag(chain, &creq->op_tmpl, frag_len + len,
574 flags);
575 if (IS_ERR(op))
576 return op;
577
Russell Kingab270e72015-10-18 17:24:47 +0100578 if (len == trailerlen)
579 return op;
580
581 padoff += len;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200582 }
583
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200584 ret = mv_cesa_dma_add_data_transfer(chain,
585 CESA_SA_DATA_SRAM_OFFSET,
586 ahashdreq->padding_dma +
587 padoff,
588 trailerlen - padoff,
589 CESA_TDMA_DST_IN_SRAM,
590 flags);
591 if (ret)
592 return ERR_PTR(ret);
593
Russell King96212882015-10-18 17:24:06 +0100594 return mv_cesa_dma_add_frag(chain, &creq->op_tmpl, trailerlen - padoff,
595 flags);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200596}
597
598static int mv_cesa_ahash_dma_req_init(struct ahash_request *req)
599{
600 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
601 gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
602 GFP_KERNEL : GFP_ATOMIC;
Romain Perier53da7402016-06-21 10:08:35 +0200603 struct mv_cesa_req *basereq = &creq->base;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200604 struct mv_cesa_ahash_dma_iter iter;
605 struct mv_cesa_op_ctx *op = NULL;
Russell Kinge41bbeb2015-10-18 17:24:32 +0100606 unsigned int frag_len;
Romain Perier8759fec2016-12-14 15:15:07 +0100607 bool set_state = false;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200608 int ret;
Romain Perierf34dad12016-10-05 09:56:33 +0200609 u32 type;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200610
Romain Perier53da7402016-06-21 10:08:35 +0200611 basereq->chain.first = NULL;
612 basereq->chain.last = NULL;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200613
Romain Perier8759fec2016-12-14 15:15:07 +0100614 if (!mv_cesa_mac_op_is_first_frag(&creq->op_tmpl))
615 set_state = true;
616
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200617 if (creq->src_nents) {
618 ret = dma_map_sg(cesa_dev->dev, req->src, creq->src_nents,
619 DMA_TO_DEVICE);
620 if (!ret) {
621 ret = -ENOMEM;
622 goto err;
623 }
624 }
625
Romain Perier53da7402016-06-21 10:08:35 +0200626 mv_cesa_tdma_desc_iter_init(&basereq->chain);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200627 mv_cesa_ahash_req_iter_init(&iter, req);
628
Russell King0971d092015-10-18 17:24:16 +0100629 /*
630 * Add the cache (left-over data from a previous block) first.
631 * This will never overflow the SRAM size.
632 */
Thomas Petazzoni2a8a7852016-08-09 11:03:15 +0200633 ret = mv_cesa_ahash_dma_add_cache(&basereq->chain, creq, flags);
Russell King0971d092015-10-18 17:24:16 +0100634 if (ret)
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200635 goto err_free_tdma;
Russell King0971d092015-10-18 17:24:16 +0100636
Russell Kingd9bba4c2015-10-18 17:24:21 +0100637 if (iter.src.sg) {
638 /*
639 * Add all the new data, inserting an operation block and
640 * launch command between each full SRAM block-worth of
Russell Kinge41bbeb2015-10-18 17:24:32 +0100641 * data. We intentionally do not add the final op block.
Russell Kingd9bba4c2015-10-18 17:24:21 +0100642 */
Russell Kinge41bbeb2015-10-18 17:24:32 +0100643 while (true) {
Romain Perier53da7402016-06-21 10:08:35 +0200644 ret = mv_cesa_dma_add_op_transfers(&basereq->chain,
Boris Brezillon8c07f3a2015-10-18 17:24:57 +0100645 &iter.base,
Russell Kingd9bba4c2015-10-18 17:24:21 +0100646 &iter.src, flags);
647 if (ret)
648 goto err_free_tdma;
649
Russell Kinge41bbeb2015-10-18 17:24:32 +0100650 frag_len = iter.base.op_len;
651
652 if (!mv_cesa_ahash_req_iter_next_op(&iter))
653 break;
654
Romain Perier53da7402016-06-21 10:08:35 +0200655 op = mv_cesa_dma_add_frag(&basereq->chain, &creq->op_tmpl,
Russell Kinge41bbeb2015-10-18 17:24:32 +0100656 frag_len, flags);
Russell Kingd9bba4c2015-10-18 17:24:21 +0100657 if (IS_ERR(op)) {
658 ret = PTR_ERR(op);
659 goto err_free_tdma;
660 }
Russell Kinge41bbeb2015-10-18 17:24:32 +0100661 }
662 } else {
Russell Kingd9bba4c2015-10-18 17:24:21 +0100663 /* Account for the data that was in the cache. */
Russell Kinge41bbeb2015-10-18 17:24:32 +0100664 frag_len = iter.base.op_len;
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200665 }
666
Russell King58953e12015-10-18 17:24:37 +0100667 /*
668 * At this point, frag_len indicates whether we have any data
669 * outstanding which needs an operation. Queue up the final
670 * operation, which depends whether this is the final request.
671 */
672 if (creq->last_req)
Romain Perier53da7402016-06-21 10:08:35 +0200673 op = mv_cesa_ahash_dma_last_req(&basereq->chain, &iter, creq,
Boris Brezillon8c07f3a2015-10-18 17:24:57 +0100674 frag_len, flags);
Russell King58953e12015-10-18 17:24:37 +0100675 else if (frag_len)
Romain Perier53da7402016-06-21 10:08:35 +0200676 op = mv_cesa_dma_add_frag(&basereq->chain, &creq->op_tmpl,
Boris Brezillon8c07f3a2015-10-18 17:24:57 +0100677 frag_len, flags);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200678
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200679 if (IS_ERR(op)) {
680 ret = PTR_ERR(op);
681 goto err_free_tdma;
682 }
683
Romain Perierf34dad12016-10-05 09:56:33 +0200684 /*
685 * If results are copied via DMA, this means that this
686 * request can be directly processed by the engine,
687 * without partial updates. So we can chain it at the
688 * DMA level with other requests.
689 */
690 type = basereq->chain.last->flags & CESA_TDMA_TYPE_MSK;
691
692 if (op && type != CESA_TDMA_RESULT) {
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200693 /* Add dummy desc to wait for crypto operation end */
Romain Perier53da7402016-06-21 10:08:35 +0200694 ret = mv_cesa_dma_add_dummy_end(&basereq->chain, flags);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200695 if (ret)
696 goto err_free_tdma;
697 }
698
699 if (!creq->last_req)
700 creq->cache_ptr = req->nbytes + creq->cache_ptr -
701 iter.base.len;
702 else
703 creq->cache_ptr = 0;
704
Romain Perierf34dad12016-10-05 09:56:33 +0200705 basereq->chain.last->flags |= CESA_TDMA_END_OF_REQ;
706
707 if (type != CESA_TDMA_RESULT)
708 basereq->chain.last->flags |= CESA_TDMA_BREAK_CHAIN;
Romain Perier85030c52016-06-21 10:08:39 +0200709
Romain Perier8759fec2016-12-14 15:15:07 +0100710 if (set_state) {
711 /*
712 * Put the CESA_TDMA_SET_STATE flag on the first tdma desc to
713 * let the step logic know that the IVDIG registers should be
714 * explicitly set before launching a TDMA chain.
715 */
716 basereq->chain.first->flags |= CESA_TDMA_SET_STATE;
717 }
718
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200719 return 0;
720
721err_free_tdma:
Romain Perier53da7402016-06-21 10:08:35 +0200722 mv_cesa_dma_cleanup(basereq);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200723 dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents, DMA_TO_DEVICE);
724
725err:
726 mv_cesa_ahash_last_cleanup(req);
727
728 return ret;
729}
730
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200731static int mv_cesa_ahash_req_init(struct ahash_request *req, bool *cached)
732{
733 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
734
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200735 creq->src_nents = sg_nents_for_len(req->src, req->nbytes);
LABBE Corentinc22dafb2015-11-04 21:13:33 +0100736 if (creq->src_nents < 0) {
737 dev_err(cesa_dev->dev, "Invalid number of src SG");
738 return creq->src_nents;
739 }
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200740
Thomas Petazzoni6dc156f2016-08-09 11:03:17 +0200741 *cached = mv_cesa_ahash_cache_req(req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200742
743 if (*cached)
744 return 0;
745
Romain Perier53da7402016-06-21 10:08:35 +0200746 if (cesa_dev->caps->has_tdma)
Thomas Petazzoni6dc156f2016-08-09 11:03:17 +0200747 return mv_cesa_ahash_dma_req_init(req);
748 else
749 return 0;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200750}
751
Romain Perierbf8f91e2016-06-21 10:08:38 +0200752static int mv_cesa_ahash_queue_req(struct ahash_request *req)
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200753{
754 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
Romain Perierbf8f91e2016-06-21 10:08:38 +0200755 struct mv_cesa_engine *engine;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200756 bool cached = false;
757 int ret;
758
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200759 ret = mv_cesa_ahash_req_init(req, &cached);
760 if (ret)
761 return ret;
762
763 if (cached)
764 return 0;
765
Romain Perierbf8f91e2016-06-21 10:08:38 +0200766 engine = mv_cesa_select_engine(req->nbytes);
767 mv_cesa_ahash_prepare(&req->base, engine);
768
Romain Perier53da7402016-06-21 10:08:35 +0200769 ret = mv_cesa_queue_req(&req->base, &creq->base);
Romain Perierbf8f91e2016-06-21 10:08:38 +0200770
Thomas Petazzonicfcd2272015-09-18 17:25:36 +0200771 if (mv_cesa_req_needs_cleanup(&req->base, ret))
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200772 mv_cesa_ahash_cleanup(req);
Boris BREZILLONdb509a42015-06-18 15:46:21 +0200773
774 return ret;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200775}
776
Romain Perierbf8f91e2016-06-21 10:08:38 +0200777static int mv_cesa_ahash_update(struct ahash_request *req)
778{
779 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
780
781 creq->len += req->nbytes;
782
783 return mv_cesa_ahash_queue_req(req);
784}
785
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200786static int mv_cesa_ahash_final(struct ahash_request *req)
787{
788 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
789 struct mv_cesa_op_ctx *tmpl = &creq->op_tmpl;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200790
791 mv_cesa_set_mac_op_total_len(tmpl, creq->len);
792 creq->last_req = true;
793 req->nbytes = 0;
794
Romain Perierbf8f91e2016-06-21 10:08:38 +0200795 return mv_cesa_ahash_queue_req(req);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200796}
797
798static int mv_cesa_ahash_finup(struct ahash_request *req)
799{
800 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
801 struct mv_cesa_op_ctx *tmpl = &creq->op_tmpl;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200802
803 creq->len += req->nbytes;
804 mv_cesa_set_mac_op_total_len(tmpl, creq->len);
805 creq->last_req = true;
806
Romain Perierbf8f91e2016-06-21 10:08:38 +0200807 return mv_cesa_ahash_queue_req(req);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200808}
809
Russell Kinga6479ea2015-10-09 21:14:22 +0100810static int mv_cesa_ahash_export(struct ahash_request *req, void *hash,
811 u64 *len, void *cache)
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200812{
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200813 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
814 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
815 unsigned int digsize = crypto_ahash_digestsize(ahash);
Russell Kinga6479ea2015-10-09 21:14:22 +0100816 unsigned int blocksize;
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200817
Russell King80754532015-10-18 17:23:30 +0100818 blocksize = crypto_ahash_blocksize(ahash);
Russell Kinga6479ea2015-10-09 21:14:22 +0100819
820 *len = creq->len;
821 memcpy(hash, creq->state, digsize);
822 memset(cache, 0, blocksize);
Dan Carpenter063327f2016-03-21 12:03:43 +0300823 memcpy(cache, creq->cache, creq->cache_ptr);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200824
825 return 0;
826}
827
Russell Kinga6479ea2015-10-09 21:14:22 +0100828static int mv_cesa_ahash_import(struct ahash_request *req, const void *hash,
829 u64 len, const void *cache)
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200830{
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200831 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
832 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
833 unsigned int digsize = crypto_ahash_digestsize(ahash);
Russell Kinga6479ea2015-10-09 21:14:22 +0100834 unsigned int blocksize;
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200835 unsigned int cache_ptr;
836 int ret;
837
Russell Kinga6479ea2015-10-09 21:14:22 +0100838 ret = crypto_ahash_init(req);
839 if (ret)
840 return ret;
841
Russell King80754532015-10-18 17:23:30 +0100842 blocksize = crypto_ahash_blocksize(ahash);
Russell Kinga6479ea2015-10-09 21:14:22 +0100843 if (len >= blocksize)
844 mv_cesa_update_op_cfg(&creq->op_tmpl,
845 CESA_SA_DESC_CFG_MID_FRAG,
846 CESA_SA_DESC_CFG_FRAG_MSK);
847
848 creq->len = len;
849 memcpy(creq->state, hash, digsize);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200850 creq->cache_ptr = 0;
851
Russell Kinga6479ea2015-10-09 21:14:22 +0100852 cache_ptr = do_div(len, blocksize);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200853 if (!cache_ptr)
854 return 0;
855
Russell Kinga6479ea2015-10-09 21:14:22 +0100856 memcpy(creq->cache, cache, cache_ptr);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200857 creq->cache_ptr = cache_ptr;
858
859 return 0;
860}
861
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200862static int mv_cesa_md5_init(struct ahash_request *req)
863{
Boris BREZILLONb0ef5102016-03-17 10:21:35 +0100864 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
Russell Kingd30cb2f2015-10-18 17:23:51 +0100865 struct mv_cesa_op_ctx tmpl = { };
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200866
867 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_MD5);
Romain Perier57cfda12016-08-09 11:03:19 +0200868
869 mv_cesa_ahash_init(req, &tmpl, true);
870
Boris BREZILLONb0ef5102016-03-17 10:21:35 +0100871 creq->state[0] = MD5_H0;
872 creq->state[1] = MD5_H1;
873 creq->state[2] = MD5_H2;
874 creq->state[3] = MD5_H3;
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200875
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200876 return 0;
877}
878
879static int mv_cesa_md5_export(struct ahash_request *req, void *out)
880{
881 struct md5_state *out_state = out;
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200882
Russell Kinga6479ea2015-10-09 21:14:22 +0100883 return mv_cesa_ahash_export(req, out_state->hash,
884 &out_state->byte_count, out_state->block);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200885}
886
887static int mv_cesa_md5_import(struct ahash_request *req, const void *in)
888{
889 const struct md5_state *in_state = in;
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200890
Russell Kinga6479ea2015-10-09 21:14:22 +0100891 return mv_cesa_ahash_import(req, in_state->hash, in_state->byte_count,
892 in_state->block);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200893}
894
895static int mv_cesa_md5_digest(struct ahash_request *req)
896{
897 int ret;
898
899 ret = mv_cesa_md5_init(req);
900 if (ret)
901 return ret;
902
903 return mv_cesa_ahash_finup(req);
904}
905
906struct ahash_alg mv_md5_alg = {
907 .init = mv_cesa_md5_init,
908 .update = mv_cesa_ahash_update,
909 .final = mv_cesa_ahash_final,
910 .finup = mv_cesa_ahash_finup,
911 .digest = mv_cesa_md5_digest,
912 .export = mv_cesa_md5_export,
913 .import = mv_cesa_md5_import,
914 .halg = {
915 .digestsize = MD5_DIGEST_SIZE,
Russell King9f5594c2015-10-09 20:43:38 +0100916 .statesize = sizeof(struct md5_state),
Arnaud Ebalard7aeef692015-06-18 15:46:24 +0200917 .base = {
918 .cra_name = "md5",
919 .cra_driver_name = "mv-md5",
920 .cra_priority = 300,
921 .cra_flags = CRYPTO_ALG_ASYNC |
922 CRYPTO_ALG_KERN_DRIVER_ONLY,
923 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
924 .cra_ctxsize = sizeof(struct mv_cesa_hash_ctx),
925 .cra_init = mv_cesa_ahash_cra_init,
926 .cra_module = THIS_MODULE,
927 }
928 }
929};
930
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200931static int mv_cesa_sha1_init(struct ahash_request *req)
932{
Boris BREZILLONb0ef5102016-03-17 10:21:35 +0100933 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
Russell Kingd30cb2f2015-10-18 17:23:51 +0100934 struct mv_cesa_op_ctx tmpl = { };
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200935
936 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_SHA1);
Romain Perier57cfda12016-08-09 11:03:19 +0200937
938 mv_cesa_ahash_init(req, &tmpl, false);
939
Boris BREZILLONb0ef5102016-03-17 10:21:35 +0100940 creq->state[0] = SHA1_H0;
941 creq->state[1] = SHA1_H1;
942 creq->state[2] = SHA1_H2;
943 creq->state[3] = SHA1_H3;
944 creq->state[4] = SHA1_H4;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200945
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200946 return 0;
947}
948
949static int mv_cesa_sha1_export(struct ahash_request *req, void *out)
950{
951 struct sha1_state *out_state = out;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200952
Russell Kinga6479ea2015-10-09 21:14:22 +0100953 return mv_cesa_ahash_export(req, out_state->state, &out_state->count,
954 out_state->buffer);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200955}
956
957static int mv_cesa_sha1_import(struct ahash_request *req, const void *in)
958{
959 const struct sha1_state *in_state = in;
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200960
Russell Kinga6479ea2015-10-09 21:14:22 +0100961 return mv_cesa_ahash_import(req, in_state->state, in_state->count,
962 in_state->buffer);
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200963}
964
965static int mv_cesa_sha1_digest(struct ahash_request *req)
966{
967 int ret;
968
969 ret = mv_cesa_sha1_init(req);
970 if (ret)
971 return ret;
972
973 return mv_cesa_ahash_finup(req);
974}
975
976struct ahash_alg mv_sha1_alg = {
977 .init = mv_cesa_sha1_init,
978 .update = mv_cesa_ahash_update,
979 .final = mv_cesa_ahash_final,
980 .finup = mv_cesa_ahash_finup,
981 .digest = mv_cesa_sha1_digest,
982 .export = mv_cesa_sha1_export,
983 .import = mv_cesa_sha1_import,
984 .halg = {
985 .digestsize = SHA1_DIGEST_SIZE,
Russell King9f5594c2015-10-09 20:43:38 +0100986 .statesize = sizeof(struct sha1_state),
Boris BREZILLONf63601f2015-06-18 15:46:20 +0200987 .base = {
988 .cra_name = "sha1",
989 .cra_driver_name = "mv-sha1",
990 .cra_priority = 300,
991 .cra_flags = CRYPTO_ALG_ASYNC |
992 CRYPTO_ALG_KERN_DRIVER_ONLY,
993 .cra_blocksize = SHA1_BLOCK_SIZE,
994 .cra_ctxsize = sizeof(struct mv_cesa_hash_ctx),
995 .cra_init = mv_cesa_ahash_cra_init,
996 .cra_module = THIS_MODULE,
997 }
998 }
999};
1000
Arnaud Ebalardf85a7622015-06-18 15:46:25 +02001001static int mv_cesa_sha256_init(struct ahash_request *req)
1002{
Boris BREZILLONb0ef5102016-03-17 10:21:35 +01001003 struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
Russell Kingd30cb2f2015-10-18 17:23:51 +01001004 struct mv_cesa_op_ctx tmpl = { };
Arnaud Ebalardf85a7622015-06-18 15:46:25 +02001005
1006 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_SHA256);
Romain Perier57cfda12016-08-09 11:03:19 +02001007
1008 mv_cesa_ahash_init(req, &tmpl, false);
1009
Boris BREZILLONb0ef5102016-03-17 10:21:35 +01001010 creq->state[0] = SHA256_H0;
1011 creq->state[1] = SHA256_H1;
1012 creq->state[2] = SHA256_H2;
1013 creq->state[3] = SHA256_H3;
1014 creq->state[4] = SHA256_H4;
1015 creq->state[5] = SHA256_H5;
1016 creq->state[6] = SHA256_H6;
1017 creq->state[7] = SHA256_H7;
Arnaud Ebalardf85a7622015-06-18 15:46:25 +02001018
Arnaud Ebalardf85a7622015-06-18 15:46:25 +02001019 return 0;
1020}
1021
1022static int mv_cesa_sha256_digest(struct ahash_request *req)
1023{
1024 int ret;
1025
1026 ret = mv_cesa_sha256_init(req);
1027 if (ret)
1028 return ret;
1029
1030 return mv_cesa_ahash_finup(req);
1031}
1032
1033static int mv_cesa_sha256_export(struct ahash_request *req, void *out)
1034{
1035 struct sha256_state *out_state = out;
Arnaud Ebalardf85a7622015-06-18 15:46:25 +02001036
Russell Kinga6479ea2015-10-09 21:14:22 +01001037 return mv_cesa_ahash_export(req, out_state->state, &out_state->count,
1038 out_state->buf);
Arnaud Ebalardf85a7622015-06-18 15:46:25 +02001039}
1040
1041static int mv_cesa_sha256_import(struct ahash_request *req, const void *in)
1042{
1043 const struct sha256_state *in_state = in;
Arnaud Ebalardf85a7622015-06-18 15:46:25 +02001044
Russell Kinga6479ea2015-10-09 21:14:22 +01001045 return mv_cesa_ahash_import(req, in_state->state, in_state->count,
1046 in_state->buf);
Arnaud Ebalardf85a7622015-06-18 15:46:25 +02001047}
1048
1049struct ahash_alg mv_sha256_alg = {
1050 .init = mv_cesa_sha256_init,
1051 .update = mv_cesa_ahash_update,
1052 .final = mv_cesa_ahash_final,
1053 .finup = mv_cesa_ahash_finup,
1054 .digest = mv_cesa_sha256_digest,
1055 .export = mv_cesa_sha256_export,
1056 .import = mv_cesa_sha256_import,
1057 .halg = {
1058 .digestsize = SHA256_DIGEST_SIZE,
Russell King9f5594c2015-10-09 20:43:38 +01001059 .statesize = sizeof(struct sha256_state),
Arnaud Ebalardf85a7622015-06-18 15:46:25 +02001060 .base = {
1061 .cra_name = "sha256",
1062 .cra_driver_name = "mv-sha256",
1063 .cra_priority = 300,
1064 .cra_flags = CRYPTO_ALG_ASYNC |
1065 CRYPTO_ALG_KERN_DRIVER_ONLY,
1066 .cra_blocksize = SHA256_BLOCK_SIZE,
1067 .cra_ctxsize = sizeof(struct mv_cesa_hash_ctx),
1068 .cra_init = mv_cesa_ahash_cra_init,
1069 .cra_module = THIS_MODULE,
1070 }
1071 }
1072};
1073
Boris BREZILLONf63601f2015-06-18 15:46:20 +02001074struct mv_cesa_ahash_result {
1075 struct completion completion;
1076 int error;
1077};
1078
1079static void mv_cesa_hmac_ahash_complete(struct crypto_async_request *req,
1080 int error)
1081{
1082 struct mv_cesa_ahash_result *result = req->data;
1083
1084 if (error == -EINPROGRESS)
1085 return;
1086
1087 result->error = error;
1088 complete(&result->completion);
1089}
1090
1091static int mv_cesa_ahmac_iv_state_init(struct ahash_request *req, u8 *pad,
1092 void *state, unsigned int blocksize)
1093{
1094 struct mv_cesa_ahash_result result;
1095 struct scatterlist sg;
1096 int ret;
1097
1098 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1099 mv_cesa_hmac_ahash_complete, &result);
1100 sg_init_one(&sg, pad, blocksize);
1101 ahash_request_set_crypt(req, &sg, pad, blocksize);
1102 init_completion(&result.completion);
1103
1104 ret = crypto_ahash_init(req);
1105 if (ret)
1106 return ret;
1107
1108 ret = crypto_ahash_update(req);
1109 if (ret && ret != -EINPROGRESS)
1110 return ret;
1111
1112 wait_for_completion_interruptible(&result.completion);
1113 if (result.error)
1114 return result.error;
1115
1116 ret = crypto_ahash_export(req, state);
1117 if (ret)
1118 return ret;
1119
1120 return 0;
1121}
1122
1123static int mv_cesa_ahmac_pad_init(struct ahash_request *req,
1124 const u8 *key, unsigned int keylen,
1125 u8 *ipad, u8 *opad,
1126 unsigned int blocksize)
1127{
1128 struct mv_cesa_ahash_result result;
1129 struct scatterlist sg;
1130 int ret;
1131 int i;
1132
1133 if (keylen <= blocksize) {
1134 memcpy(ipad, key, keylen);
1135 } else {
1136 u8 *keydup = kmemdup(key, keylen, GFP_KERNEL);
1137
1138 if (!keydup)
1139 return -ENOMEM;
1140
1141 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1142 mv_cesa_hmac_ahash_complete,
1143 &result);
1144 sg_init_one(&sg, keydup, keylen);
1145 ahash_request_set_crypt(req, &sg, ipad, keylen);
1146 init_completion(&result.completion);
1147
1148 ret = crypto_ahash_digest(req);
1149 if (ret == -EINPROGRESS) {
1150 wait_for_completion_interruptible(&result.completion);
1151 ret = result.error;
1152 }
1153
1154 /* Set the memory region to 0 to avoid any leak. */
1155 memset(keydup, 0, keylen);
1156 kfree(keydup);
1157
1158 if (ret)
1159 return ret;
1160
1161 keylen = crypto_ahash_digestsize(crypto_ahash_reqtfm(req));
1162 }
1163
1164 memset(ipad + keylen, 0, blocksize - keylen);
1165 memcpy(opad, ipad, blocksize);
1166
1167 for (i = 0; i < blocksize; i++) {
1168 ipad[i] ^= 0x36;
1169 opad[i] ^= 0x5c;
1170 }
1171
1172 return 0;
1173}
1174
1175static int mv_cesa_ahmac_setkey(const char *hash_alg_name,
1176 const u8 *key, unsigned int keylen,
1177 void *istate, void *ostate)
1178{
1179 struct ahash_request *req;
1180 struct crypto_ahash *tfm;
1181 unsigned int blocksize;
1182 u8 *ipad = NULL;
1183 u8 *opad;
1184 int ret;
1185
1186 tfm = crypto_alloc_ahash(hash_alg_name, CRYPTO_ALG_TYPE_AHASH,
1187 CRYPTO_ALG_TYPE_AHASH_MASK);
1188 if (IS_ERR(tfm))
1189 return PTR_ERR(tfm);
1190
1191 req = ahash_request_alloc(tfm, GFP_KERNEL);
1192 if (!req) {
1193 ret = -ENOMEM;
1194 goto free_ahash;
1195 }
1196
1197 crypto_ahash_clear_flags(tfm, ~0);
1198
1199 blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1200
1201 ipad = kzalloc(2 * blocksize, GFP_KERNEL);
1202 if (!ipad) {
1203 ret = -ENOMEM;
1204 goto free_req;
1205 }
1206
1207 opad = ipad + blocksize;
1208
1209 ret = mv_cesa_ahmac_pad_init(req, key, keylen, ipad, opad, blocksize);
1210 if (ret)
1211 goto free_ipad;
1212
1213 ret = mv_cesa_ahmac_iv_state_init(req, ipad, istate, blocksize);
1214 if (ret)
1215 goto free_ipad;
1216
1217 ret = mv_cesa_ahmac_iv_state_init(req, opad, ostate, blocksize);
1218
1219free_ipad:
1220 kfree(ipad);
1221free_req:
1222 ahash_request_free(req);
1223free_ahash:
1224 crypto_free_ahash(tfm);
1225
1226 return ret;
1227}
1228
1229static int mv_cesa_ahmac_cra_init(struct crypto_tfm *tfm)
1230{
1231 struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(tfm);
1232
1233 ctx->base.ops = &mv_cesa_ahash_req_ops;
1234
1235 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
1236 sizeof(struct mv_cesa_ahash_req));
1237 return 0;
1238}
1239
Arnaud Ebalard7aeef692015-06-18 15:46:24 +02001240static int mv_cesa_ahmac_md5_init(struct ahash_request *req)
1241{
1242 struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Russell Kingd30cb2f2015-10-18 17:23:51 +01001243 struct mv_cesa_op_ctx tmpl = { };
Arnaud Ebalard7aeef692015-06-18 15:46:24 +02001244
1245 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_HMAC_MD5);
1246 memcpy(tmpl.ctx.hash.iv, ctx->iv, sizeof(ctx->iv));
1247
Russell Kinga9eb6782015-10-18 17:23:40 +01001248 mv_cesa_ahash_init(req, &tmpl, true);
Arnaud Ebalard7aeef692015-06-18 15:46:24 +02001249
1250 return 0;
1251}
1252
1253static int mv_cesa_ahmac_md5_setkey(struct crypto_ahash *tfm, const u8 *key,
1254 unsigned int keylen)
1255{
1256 struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1257 struct md5_state istate, ostate;
1258 int ret, i;
1259
1260 ret = mv_cesa_ahmac_setkey("mv-md5", key, keylen, &istate, &ostate);
1261 if (ret)
1262 return ret;
1263
1264 for (i = 0; i < ARRAY_SIZE(istate.hash); i++)
1265 ctx->iv[i] = be32_to_cpu(istate.hash[i]);
1266
1267 for (i = 0; i < ARRAY_SIZE(ostate.hash); i++)
1268 ctx->iv[i + 8] = be32_to_cpu(ostate.hash[i]);
1269
1270 return 0;
1271}
1272
1273static int mv_cesa_ahmac_md5_digest(struct ahash_request *req)
1274{
1275 int ret;
1276
1277 ret = mv_cesa_ahmac_md5_init(req);
1278 if (ret)
1279 return ret;
1280
1281 return mv_cesa_ahash_finup(req);
1282}
1283
1284struct ahash_alg mv_ahmac_md5_alg = {
1285 .init = mv_cesa_ahmac_md5_init,
1286 .update = mv_cesa_ahash_update,
1287 .final = mv_cesa_ahash_final,
1288 .finup = mv_cesa_ahash_finup,
1289 .digest = mv_cesa_ahmac_md5_digest,
1290 .setkey = mv_cesa_ahmac_md5_setkey,
1291 .export = mv_cesa_md5_export,
1292 .import = mv_cesa_md5_import,
1293 .halg = {
1294 .digestsize = MD5_DIGEST_SIZE,
1295 .statesize = sizeof(struct md5_state),
1296 .base = {
1297 .cra_name = "hmac(md5)",
1298 .cra_driver_name = "mv-hmac-md5",
1299 .cra_priority = 300,
1300 .cra_flags = CRYPTO_ALG_ASYNC |
1301 CRYPTO_ALG_KERN_DRIVER_ONLY,
1302 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
1303 .cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx),
1304 .cra_init = mv_cesa_ahmac_cra_init,
1305 .cra_module = THIS_MODULE,
1306 }
1307 }
1308};
1309
Boris BREZILLONf63601f2015-06-18 15:46:20 +02001310static int mv_cesa_ahmac_sha1_init(struct ahash_request *req)
1311{
1312 struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Russell Kingd30cb2f2015-10-18 17:23:51 +01001313 struct mv_cesa_op_ctx tmpl = { };
Boris BREZILLONf63601f2015-06-18 15:46:20 +02001314
1315 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_HMAC_SHA1);
1316 memcpy(tmpl.ctx.hash.iv, ctx->iv, sizeof(ctx->iv));
1317
Russell Kinga9eb6782015-10-18 17:23:40 +01001318 mv_cesa_ahash_init(req, &tmpl, false);
Boris BREZILLONf63601f2015-06-18 15:46:20 +02001319
1320 return 0;
1321}
1322
1323static int mv_cesa_ahmac_sha1_setkey(struct crypto_ahash *tfm, const u8 *key,
1324 unsigned int keylen)
1325{
1326 struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1327 struct sha1_state istate, ostate;
1328 int ret, i;
1329
1330 ret = mv_cesa_ahmac_setkey("mv-sha1", key, keylen, &istate, &ostate);
1331 if (ret)
1332 return ret;
1333
1334 for (i = 0; i < ARRAY_SIZE(istate.state); i++)
1335 ctx->iv[i] = be32_to_cpu(istate.state[i]);
1336
1337 for (i = 0; i < ARRAY_SIZE(ostate.state); i++)
1338 ctx->iv[i + 8] = be32_to_cpu(ostate.state[i]);
1339
1340 return 0;
1341}
1342
1343static int mv_cesa_ahmac_sha1_digest(struct ahash_request *req)
1344{
1345 int ret;
1346
1347 ret = mv_cesa_ahmac_sha1_init(req);
1348 if (ret)
1349 return ret;
1350
1351 return mv_cesa_ahash_finup(req);
1352}
1353
1354struct ahash_alg mv_ahmac_sha1_alg = {
1355 .init = mv_cesa_ahmac_sha1_init,
1356 .update = mv_cesa_ahash_update,
1357 .final = mv_cesa_ahash_final,
1358 .finup = mv_cesa_ahash_finup,
1359 .digest = mv_cesa_ahmac_sha1_digest,
1360 .setkey = mv_cesa_ahmac_sha1_setkey,
1361 .export = mv_cesa_sha1_export,
1362 .import = mv_cesa_sha1_import,
1363 .halg = {
1364 .digestsize = SHA1_DIGEST_SIZE,
1365 .statesize = sizeof(struct sha1_state),
1366 .base = {
1367 .cra_name = "hmac(sha1)",
1368 .cra_driver_name = "mv-hmac-sha1",
1369 .cra_priority = 300,
1370 .cra_flags = CRYPTO_ALG_ASYNC |
1371 CRYPTO_ALG_KERN_DRIVER_ONLY,
1372 .cra_blocksize = SHA1_BLOCK_SIZE,
1373 .cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx),
1374 .cra_init = mv_cesa_ahmac_cra_init,
1375 .cra_module = THIS_MODULE,
1376 }
1377 }
1378};
Arnaud Ebalardf85a7622015-06-18 15:46:25 +02001379
1380static int mv_cesa_ahmac_sha256_setkey(struct crypto_ahash *tfm, const u8 *key,
1381 unsigned int keylen)
1382{
1383 struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1384 struct sha256_state istate, ostate;
1385 int ret, i;
1386
1387 ret = mv_cesa_ahmac_setkey("mv-sha256", key, keylen, &istate, &ostate);
1388 if (ret)
1389 return ret;
1390
1391 for (i = 0; i < ARRAY_SIZE(istate.state); i++)
1392 ctx->iv[i] = be32_to_cpu(istate.state[i]);
1393
1394 for (i = 0; i < ARRAY_SIZE(ostate.state); i++)
1395 ctx->iv[i + 8] = be32_to_cpu(ostate.state[i]);
1396
1397 return 0;
1398}
1399
1400static int mv_cesa_ahmac_sha256_init(struct ahash_request *req)
1401{
1402 struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Russell Kingd30cb2f2015-10-18 17:23:51 +01001403 struct mv_cesa_op_ctx tmpl = { };
Arnaud Ebalardf85a7622015-06-18 15:46:25 +02001404
1405 mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_HMAC_SHA256);
1406 memcpy(tmpl.ctx.hash.iv, ctx->iv, sizeof(ctx->iv));
1407
Russell Kinga9eb6782015-10-18 17:23:40 +01001408 mv_cesa_ahash_init(req, &tmpl, false);
Arnaud Ebalardf85a7622015-06-18 15:46:25 +02001409
1410 return 0;
1411}
1412
1413static int mv_cesa_ahmac_sha256_digest(struct ahash_request *req)
1414{
1415 int ret;
1416
1417 ret = mv_cesa_ahmac_sha256_init(req);
1418 if (ret)
1419 return ret;
1420
1421 return mv_cesa_ahash_finup(req);
1422}
1423
1424struct ahash_alg mv_ahmac_sha256_alg = {
1425 .init = mv_cesa_ahmac_sha256_init,
1426 .update = mv_cesa_ahash_update,
1427 .final = mv_cesa_ahash_final,
1428 .finup = mv_cesa_ahash_finup,
1429 .digest = mv_cesa_ahmac_sha256_digest,
1430 .setkey = mv_cesa_ahmac_sha256_setkey,
1431 .export = mv_cesa_sha256_export,
1432 .import = mv_cesa_sha256_import,
1433 .halg = {
1434 .digestsize = SHA256_DIGEST_SIZE,
1435 .statesize = sizeof(struct sha256_state),
1436 .base = {
1437 .cra_name = "hmac(sha256)",
1438 .cra_driver_name = "mv-hmac-sha256",
1439 .cra_priority = 300,
1440 .cra_flags = CRYPTO_ALG_ASYNC |
1441 CRYPTO_ALG_KERN_DRIVER_ONLY,
1442 .cra_blocksize = SHA256_BLOCK_SIZE,
1443 .cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx),
1444 .cra_init = mv_cesa_ahmac_cra_init,
1445 .cra_module = THIS_MODULE,
1446 }
1447 }
1448};