1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Hash function and HMAC support for StarFive driver
4 *
5 * Copyright (c) 2022 StarFive Technology
6 *
7 */
8
9 #include <crypto/engine.h>
10 #include <crypto/internal/hash.h>
11 #include <crypto/scatterwalk.h>
12 #include "jh7110-cryp.h"
13 #include <linux/amba/pl080.h>
14 #include <linux/clk.h>
15 #include <linux/dma-direct.h>
16 #include <linux/interrupt.h>
17 #include <linux/iopoll.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/reset.h>
23
24 #define STARFIVE_HASH_REGS_OFFSET 0x300
25 #define STARFIVE_HASH_SHACSR (STARFIVE_HASH_REGS_OFFSET + 0x0)
26 #define STARFIVE_HASH_SHAWDR (STARFIVE_HASH_REGS_OFFSET + 0x4)
27 #define STARFIVE_HASH_SHARDR (STARFIVE_HASH_REGS_OFFSET + 0x8)
28 #define STARFIVE_HASH_SHAWSR (STARFIVE_HASH_REGS_OFFSET + 0xC)
29 #define STARFIVE_HASH_SHAWLEN3 (STARFIVE_HASH_REGS_OFFSET + 0x10)
30 #define STARFIVE_HASH_SHAWLEN2 (STARFIVE_HASH_REGS_OFFSET + 0x14)
31 #define STARFIVE_HASH_SHAWLEN1 (STARFIVE_HASH_REGS_OFFSET + 0x18)
32 #define STARFIVE_HASH_SHAWLEN0 (STARFIVE_HASH_REGS_OFFSET + 0x1C)
33 #define STARFIVE_HASH_SHAWKR (STARFIVE_HASH_REGS_OFFSET + 0x20)
34 #define STARFIVE_HASH_SHAWKLEN (STARFIVE_HASH_REGS_OFFSET + 0x24)
35
36 #define STARFIVE_HASH_BUFLEN SHA512_BLOCK_SIZE
37 #define STARFIVE_HASH_RESET 0x2
38
starfive_hash_wait_busy(struct starfive_cryp_dev * cryp)39 static inline int starfive_hash_wait_busy(struct starfive_cryp_dev *cryp)
40 {
41 u32 status;
42
43 return readl_relaxed_poll_timeout(cryp->base + STARFIVE_HASH_SHACSR, status,
44 !(status & STARFIVE_HASH_BUSY), 10, 100000);
45 }
46
starfive_hash_wait_hmac_done(struct starfive_cryp_dev * cryp)47 static inline int starfive_hash_wait_hmac_done(struct starfive_cryp_dev *cryp)
48 {
49 u32 status;
50
51 return readl_relaxed_poll_timeout(cryp->base + STARFIVE_HASH_SHACSR, status,
52 (status & STARFIVE_HASH_HMAC_DONE), 10, 100000);
53 }
54
starfive_hash_wait_key_done(struct starfive_cryp_ctx * ctx)55 static inline int starfive_hash_wait_key_done(struct starfive_cryp_ctx *ctx)
56 {
57 struct starfive_cryp_dev *cryp = ctx->cryp;
58 u32 status;
59
60 return readl_relaxed_poll_timeout(cryp->base + STARFIVE_HASH_SHACSR, status,
61 (status & STARFIVE_HASH_KEY_DONE), 10, 100000);
62 }
63
starfive_hash_hmac_key(struct starfive_cryp_ctx * ctx)64 static int starfive_hash_hmac_key(struct starfive_cryp_ctx *ctx)
65 {
66 struct starfive_cryp_request_ctx *rctx = ctx->rctx;
67 struct starfive_cryp_dev *cryp = ctx->cryp;
68 int klen = ctx->keylen, loop;
69 unsigned int *key = (unsigned int *)ctx->key;
70 unsigned char *cl;
71
72 writel(ctx->keylen, cryp->base + STARFIVE_HASH_SHAWKLEN);
73
74 rctx->csr.hash.hmac = 1;
75 rctx->csr.hash.key_flag = 1;
76
77 writel(rctx->csr.hash.v, cryp->base + STARFIVE_HASH_SHACSR);
78
79 for (loop = 0; loop < klen / sizeof(unsigned int); loop++, key++)
80 writel(*key, cryp->base + STARFIVE_HASH_SHAWKR);
81
82 if (klen & 0x3) {
83 cl = (unsigned char *)key;
84 for (loop = 0; loop < (klen & 0x3); loop++, cl++)
85 writeb(*cl, cryp->base + STARFIVE_HASH_SHAWKR);
86 }
87
88 if (starfive_hash_wait_key_done(ctx))
89 return dev_err_probe(cryp->dev, -ETIMEDOUT, "starfive_hash_wait_key_done error\n");
90
91 return 0;
92 }
93
starfive_hash_start(struct starfive_cryp_dev * cryp)94 static void starfive_hash_start(struct starfive_cryp_dev *cryp)
95 {
96 union starfive_hash_csr csr;
97
98 csr.v = readl(cryp->base + STARFIVE_HASH_SHACSR);
99 csr.firstb = 0;
100 csr.final = 1;
101 writel(csr.v, cryp->base + STARFIVE_HASH_SHACSR);
102 }
103
starfive_hash_dma_callback(void * param)104 static void starfive_hash_dma_callback(void *param)
105 {
106 struct starfive_cryp_dev *cryp = param;
107
108 complete(&cryp->dma_done);
109 }
110
starfive_hash_dma_init(struct starfive_cryp_dev * cryp)111 static void starfive_hash_dma_init(struct starfive_cryp_dev *cryp)
112 {
113 cryp->cfg_in.src_addr_width = DMA_SLAVE_BUSWIDTH_16_BYTES;
114 cryp->cfg_in.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
115 cryp->cfg_in.src_maxburst = cryp->dma_maxburst;
116 cryp->cfg_in.dst_maxburst = cryp->dma_maxburst;
117 cryp->cfg_in.dst_addr = cryp->phys_base + STARFIVE_ALG_FIFO_OFFSET;
118
119 dmaengine_slave_config(cryp->tx, &cryp->cfg_in);
120
121 init_completion(&cryp->dma_done);
122 }
123
starfive_hash_dma_xfer(struct starfive_cryp_dev * cryp,struct scatterlist * sg)124 static int starfive_hash_dma_xfer(struct starfive_cryp_dev *cryp,
125 struct scatterlist *sg)
126 {
127 struct dma_async_tx_descriptor *in_desc;
128 union starfive_alg_cr alg_cr;
129 int ret = 0;
130
131 alg_cr.v = 0;
132 alg_cr.start = 1;
133 alg_cr.hash_dma_en = 1;
134 writel(alg_cr.v, cryp->base + STARFIVE_ALG_CR_OFFSET);
135
136 writel(sg_dma_len(sg), cryp->base + STARFIVE_DMA_IN_LEN_OFFSET);
137 sg_dma_len(sg) = ALIGN(sg_dma_len(sg), sizeof(u32));
138
139 in_desc = dmaengine_prep_slave_sg(cryp->tx, sg, 1, DMA_MEM_TO_DEV,
140 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
141 if (!in_desc) {
142 ret = -EINVAL;
143 goto end;
144 }
145
146 reinit_completion(&cryp->dma_done);
147 in_desc->callback = starfive_hash_dma_callback;
148 in_desc->callback_param = cryp;
149
150 dmaengine_submit(in_desc);
151 dma_async_issue_pending(cryp->tx);
152
153 if (!wait_for_completion_timeout(&cryp->dma_done,
154 msecs_to_jiffies(1000)))
155 ret = -ETIMEDOUT;
156
157 end:
158 alg_cr.v = 0;
159 alg_cr.clear = 1;
160 writel(alg_cr.v, cryp->base + STARFIVE_ALG_CR_OFFSET);
161
162 return ret;
163 }
164
starfive_hash_copy_hash(struct ahash_request * req)165 static int starfive_hash_copy_hash(struct ahash_request *req)
166 {
167 struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
168 struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
169 int count, *data;
170 int mlen;
171
172 if (!req->result)
173 return 0;
174
175 mlen = rctx->digsize / sizeof(u32);
176 data = (u32 *)req->result;
177
178 for (count = 0; count < mlen; count++)
179 put_unaligned(readl(ctx->cryp->base + STARFIVE_HASH_SHARDR),
180 &data[count]);
181
182 return 0;
183 }
184
starfive_hash_done_task(struct starfive_cryp_dev * cryp)185 static void starfive_hash_done_task(struct starfive_cryp_dev *cryp)
186 {
187 int err = cryp->err;
188
189 if (!err)
190 err = starfive_hash_copy_hash(cryp->req.hreq);
191
192 crypto_finalize_hash_request(cryp->engine, cryp->req.hreq, err);
193 }
194
starfive_hash_one_request(struct crypto_engine * engine,void * areq)195 static int starfive_hash_one_request(struct crypto_engine *engine, void *areq)
196 {
197 struct ahash_request *req = container_of(areq, struct ahash_request,
198 base);
199 struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
200 struct starfive_cryp_request_ctx *rctx = ctx->rctx;
201 struct starfive_cryp_dev *cryp = ctx->cryp;
202 struct scatterlist *tsg;
203 int ret, src_nents, i;
204
205 writel(STARFIVE_HASH_RESET, cryp->base + STARFIVE_HASH_SHACSR);
206
207 if (starfive_hash_wait_busy(cryp))
208 return dev_err_probe(cryp->dev, -ETIMEDOUT, "Error resetting hardware\n");
209
210 rctx->csr.hash.v = 0;
211 rctx->csr.hash.mode = ctx->hash_mode;
212
213 if (ctx->is_hmac) {
214 ret = starfive_hash_hmac_key(ctx);
215 if (ret)
216 return ret;
217 } else {
218 rctx->csr.hash.start = 1;
219 rctx->csr.hash.firstb = 1;
220 writel(rctx->csr.hash.v, cryp->base + STARFIVE_HASH_SHACSR);
221 }
222
223 /* No input message, get digest and end. */
224 if (!rctx->total)
225 goto hash_start;
226
227 starfive_hash_dma_init(cryp);
228
229 for_each_sg(rctx->in_sg, tsg, rctx->in_sg_len, i) {
230 src_nents = dma_map_sg(cryp->dev, tsg, 1, DMA_TO_DEVICE);
231 if (src_nents == 0)
232 return dev_err_probe(cryp->dev, -ENOMEM,
233 "dma_map_sg error\n");
234
235 ret = starfive_hash_dma_xfer(cryp, tsg);
236 dma_unmap_sg(cryp->dev, tsg, 1, DMA_TO_DEVICE);
237 if (ret)
238 return ret;
239 }
240
241 hash_start:
242 starfive_hash_start(cryp);
243
244 if (starfive_hash_wait_busy(cryp))
245 return dev_err_probe(cryp->dev, -ETIMEDOUT, "Error generating digest\n");
246
247 if (ctx->is_hmac)
248 cryp->err = starfive_hash_wait_hmac_done(cryp);
249
250 starfive_hash_done_task(cryp);
251
252 return 0;
253 }
254
starfive_hash_init(struct ahash_request * req)255 static int starfive_hash_init(struct ahash_request *req)
256 {
257 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
258 struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
259 struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);
260
261 ahash_request_set_tfm(&rctx->ahash_fbk_req, ctx->ahash_fbk);
262 ahash_request_set_callback(&rctx->ahash_fbk_req,
263 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
264 req->base.complete, req->base.data);
265
266 ahash_request_set_crypt(&rctx->ahash_fbk_req, req->src,
267 req->result, req->nbytes);
268
269 return crypto_ahash_init(&rctx->ahash_fbk_req);
270 }
271
starfive_hash_update(struct ahash_request * req)272 static int starfive_hash_update(struct ahash_request *req)
273 {
274 struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
275 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
276 struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);
277
278 ahash_request_set_tfm(&rctx->ahash_fbk_req, ctx->ahash_fbk);
279 ahash_request_set_callback(&rctx->ahash_fbk_req,
280 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
281 req->base.complete, req->base.data);
282
283 ahash_request_set_crypt(&rctx->ahash_fbk_req, req->src,
284 req->result, req->nbytes);
285
286 return crypto_ahash_update(&rctx->ahash_fbk_req);
287 }
288
starfive_hash_final(struct ahash_request * req)289 static int starfive_hash_final(struct ahash_request *req)
290 {
291 struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
292 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
293 struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);
294
295 ahash_request_set_tfm(&rctx->ahash_fbk_req, ctx->ahash_fbk);
296 ahash_request_set_callback(&rctx->ahash_fbk_req,
297 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
298 req->base.complete, req->base.data);
299
300 ahash_request_set_crypt(&rctx->ahash_fbk_req, req->src,
301 req->result, req->nbytes);
302
303 return crypto_ahash_final(&rctx->ahash_fbk_req);
304 }
305
starfive_hash_finup(struct ahash_request * req)306 static int starfive_hash_finup(struct ahash_request *req)
307 {
308 struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
309 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
310 struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);
311
312 ahash_request_set_tfm(&rctx->ahash_fbk_req, ctx->ahash_fbk);
313 ahash_request_set_callback(&rctx->ahash_fbk_req,
314 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
315 req->base.complete, req->base.data);
316
317 ahash_request_set_crypt(&rctx->ahash_fbk_req, req->src,
318 req->result, req->nbytes);
319
320 return crypto_ahash_finup(&rctx->ahash_fbk_req);
321 }
322
starfive_hash_digest(struct ahash_request * req)323 static int starfive_hash_digest(struct ahash_request *req)
324 {
325 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
326 struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);
327 struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
328 struct starfive_cryp_dev *cryp = ctx->cryp;
329
330 memset(rctx, 0, sizeof(struct starfive_cryp_request_ctx));
331
332 cryp->req.hreq = req;
333 rctx->total = req->nbytes;
334 rctx->in_sg = req->src;
335 rctx->blksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
336 rctx->digsize = crypto_ahash_digestsize(tfm);
337 rctx->in_sg_len = sg_nents_for_len(rctx->in_sg, rctx->total);
338 ctx->rctx = rctx;
339
340 return crypto_transfer_hash_request_to_engine(cryp->engine, req);
341 }
342
starfive_hash_export(struct ahash_request * req,void * out)343 static int starfive_hash_export(struct ahash_request *req, void *out)
344 {
345 struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
346 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
347 struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);
348
349 ahash_request_set_tfm(&rctx->ahash_fbk_req, ctx->ahash_fbk);
350 ahash_request_set_callback(&rctx->ahash_fbk_req,
351 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
352 req->base.complete, req->base.data);
353
354 return crypto_ahash_export(&rctx->ahash_fbk_req, out);
355 }
356
starfive_hash_import(struct ahash_request * req,const void * in)357 static int starfive_hash_import(struct ahash_request *req, const void *in)
358 {
359 struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
360 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
361 struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);
362
363 ahash_request_set_tfm(&rctx->ahash_fbk_req, ctx->ahash_fbk);
364 ahash_request_set_callback(&rctx->ahash_fbk_req,
365 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
366 req->base.complete, req->base.data);
367
368 return crypto_ahash_import(&rctx->ahash_fbk_req, in);
369 }
370
starfive_hash_init_tfm(struct crypto_ahash * hash,const char * alg_name,unsigned int mode,bool is_hmac)371 static int starfive_hash_init_tfm(struct crypto_ahash *hash,
372 const char *alg_name,
373 unsigned int mode,
374 bool is_hmac)
375 {
376 struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(hash);
377
378 ctx->cryp = starfive_cryp_find_dev(ctx);
379
380 if (!ctx->cryp)
381 return -ENODEV;
382
383 ctx->ahash_fbk = crypto_alloc_ahash(alg_name, 0,
384 CRYPTO_ALG_NEED_FALLBACK);
385
386 if (IS_ERR(ctx->ahash_fbk))
387 return dev_err_probe(ctx->cryp->dev, PTR_ERR(ctx->ahash_fbk),
388 "starfive_hash: Could not load fallback driver.\n");
389
390 crypto_ahash_set_statesize(hash, crypto_ahash_statesize(ctx->ahash_fbk));
391 crypto_ahash_set_reqsize(hash, sizeof(struct starfive_cryp_request_ctx) +
392 crypto_ahash_reqsize(ctx->ahash_fbk));
393
394 ctx->is_hmac = is_hmac;
395 ctx->hash_mode = mode;
396
397 return 0;
398 }
399
starfive_hash_exit_tfm(struct crypto_ahash * hash)400 static void starfive_hash_exit_tfm(struct crypto_ahash *hash)
401 {
402 struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(hash);
403
404 crypto_free_ahash(ctx->ahash_fbk);
405 }
406
starfive_hash_long_setkey(struct starfive_cryp_ctx * ctx,const u8 * key,unsigned int keylen,const char * alg_name)407 static int starfive_hash_long_setkey(struct starfive_cryp_ctx *ctx,
408 const u8 *key, unsigned int keylen,
409 const char *alg_name)
410 {
411 struct crypto_wait wait;
412 struct ahash_request *req;
413 struct scatterlist sg;
414 struct crypto_ahash *ahash_tfm;
415 u8 *buf;
416 int ret;
417
418 ahash_tfm = crypto_alloc_ahash(alg_name, 0, 0);
419 if (IS_ERR(ahash_tfm))
420 return PTR_ERR(ahash_tfm);
421
422 req = ahash_request_alloc(ahash_tfm, GFP_KERNEL);
423 if (!req) {
424 ret = -ENOMEM;
425 goto err_free_ahash;
426 }
427
428 crypto_init_wait(&wait);
429 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
430 crypto_req_done, &wait);
431 crypto_ahash_clear_flags(ahash_tfm, ~0);
432
433 buf = kzalloc(keylen + STARFIVE_HASH_BUFLEN, GFP_KERNEL);
434 if (!buf) {
435 ret = -ENOMEM;
436 goto err_free_req;
437 }
438
439 memcpy(buf, key, keylen);
440 sg_init_one(&sg, buf, keylen);
441 ahash_request_set_crypt(req, &sg, ctx->key, keylen);
442
443 ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
444
445 kfree(buf);
446 err_free_req:
447 ahash_request_free(req);
448 err_free_ahash:
449 crypto_free_ahash(ahash_tfm);
450 return ret;
451 }
452
starfive_hash_setkey(struct crypto_ahash * hash,const u8 * key,unsigned int keylen)453 static int starfive_hash_setkey(struct crypto_ahash *hash,
454 const u8 *key, unsigned int keylen)
455 {
456 struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(hash);
457 unsigned int digestsize = crypto_ahash_digestsize(hash);
458 unsigned int blocksize = crypto_ahash_blocksize(hash);
459 const char *alg_name;
460
461 crypto_ahash_setkey(ctx->ahash_fbk, key, keylen);
462
463 if (keylen <= blocksize) {
464 memcpy(ctx->key, key, keylen);
465 ctx->keylen = keylen;
466 return 0;
467 }
468
469 ctx->keylen = digestsize;
470
471 switch (digestsize) {
472 case SHA224_DIGEST_SIZE:
473 alg_name = "sha224-starfive";
474 break;
475 case SHA256_DIGEST_SIZE:
476 if (ctx->hash_mode == STARFIVE_HASH_SM3)
477 alg_name = "sm3-starfive";
478 else
479 alg_name = "sha256-starfive";
480 break;
481 case SHA384_DIGEST_SIZE:
482 alg_name = "sha384-starfive";
483 break;
484 case SHA512_DIGEST_SIZE:
485 alg_name = "sha512-starfive";
486 break;
487 default:
488 return -EINVAL;
489 }
490
491 return starfive_hash_long_setkey(ctx, key, keylen, alg_name);
492 }
493
starfive_sha224_init_tfm(struct crypto_ahash * hash)494 static int starfive_sha224_init_tfm(struct crypto_ahash *hash)
495 {
496 return starfive_hash_init_tfm(hash, "sha224-generic",
497 STARFIVE_HASH_SHA224, 0);
498 }
499
starfive_sha256_init_tfm(struct crypto_ahash * hash)500 static int starfive_sha256_init_tfm(struct crypto_ahash *hash)
501 {
502 return starfive_hash_init_tfm(hash, "sha256-generic",
503 STARFIVE_HASH_SHA256, 0);
504 }
505
starfive_sha384_init_tfm(struct crypto_ahash * hash)506 static int starfive_sha384_init_tfm(struct crypto_ahash *hash)
507 {
508 return starfive_hash_init_tfm(hash, "sha384-generic",
509 STARFIVE_HASH_SHA384, 0);
510 }
511
starfive_sha512_init_tfm(struct crypto_ahash * hash)512 static int starfive_sha512_init_tfm(struct crypto_ahash *hash)
513 {
514 return starfive_hash_init_tfm(hash, "sha512-generic",
515 STARFIVE_HASH_SHA512, 0);
516 }
517
starfive_sm3_init_tfm(struct crypto_ahash * hash)518 static int starfive_sm3_init_tfm(struct crypto_ahash *hash)
519 {
520 return starfive_hash_init_tfm(hash, "sm3-generic",
521 STARFIVE_HASH_SM3, 0);
522 }
523
starfive_hmac_sha224_init_tfm(struct crypto_ahash * hash)524 static int starfive_hmac_sha224_init_tfm(struct crypto_ahash *hash)
525 {
526 return starfive_hash_init_tfm(hash, "hmac(sha224-generic)",
527 STARFIVE_HASH_SHA224, 1);
528 }
529
starfive_hmac_sha256_init_tfm(struct crypto_ahash * hash)530 static int starfive_hmac_sha256_init_tfm(struct crypto_ahash *hash)
531 {
532 return starfive_hash_init_tfm(hash, "hmac(sha256-generic)",
533 STARFIVE_HASH_SHA256, 1);
534 }
535
starfive_hmac_sha384_init_tfm(struct crypto_ahash * hash)536 static int starfive_hmac_sha384_init_tfm(struct crypto_ahash *hash)
537 {
538 return starfive_hash_init_tfm(hash, "hmac(sha384-generic)",
539 STARFIVE_HASH_SHA384, 1);
540 }
541
starfive_hmac_sha512_init_tfm(struct crypto_ahash * hash)542 static int starfive_hmac_sha512_init_tfm(struct crypto_ahash *hash)
543 {
544 return starfive_hash_init_tfm(hash, "hmac(sha512-generic)",
545 STARFIVE_HASH_SHA512, 1);
546 }
547
starfive_hmac_sm3_init_tfm(struct crypto_ahash * hash)548 static int starfive_hmac_sm3_init_tfm(struct crypto_ahash *hash)
549 {
550 return starfive_hash_init_tfm(hash, "hmac(sm3-generic)",
551 STARFIVE_HASH_SM3, 1);
552 }
553
554 static struct ahash_engine_alg algs_sha2_sm3[] = {
555 {
556 .base.init = starfive_hash_init,
557 .base.update = starfive_hash_update,
558 .base.final = starfive_hash_final,
559 .base.finup = starfive_hash_finup,
560 .base.digest = starfive_hash_digest,
561 .base.export = starfive_hash_export,
562 .base.import = starfive_hash_import,
563 .base.init_tfm = starfive_sha224_init_tfm,
564 .base.exit_tfm = starfive_hash_exit_tfm,
565 .base.halg = {
566 .digestsize = SHA224_DIGEST_SIZE,
567 .statesize = sizeof(struct sha256_state),
568 .base = {
569 .cra_name = "sha224",
570 .cra_driver_name = "sha224-starfive",
571 .cra_priority = 200,
572 .cra_flags = CRYPTO_ALG_ASYNC |
573 CRYPTO_ALG_TYPE_AHASH |
574 CRYPTO_ALG_NEED_FALLBACK,
575 .cra_blocksize = SHA224_BLOCK_SIZE,
576 .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
577 .cra_module = THIS_MODULE,
578 }
579 },
580 .op = {
581 .do_one_request = starfive_hash_one_request,
582 },
583 }, {
584 .base.init = starfive_hash_init,
585 .base.update = starfive_hash_update,
586 .base.final = starfive_hash_final,
587 .base.finup = starfive_hash_finup,
588 .base.digest = starfive_hash_digest,
589 .base.export = starfive_hash_export,
590 .base.import = starfive_hash_import,
591 .base.init_tfm = starfive_hmac_sha224_init_tfm,
592 .base.exit_tfm = starfive_hash_exit_tfm,
593 .base.setkey = starfive_hash_setkey,
594 .base.halg = {
595 .digestsize = SHA224_DIGEST_SIZE,
596 .statesize = sizeof(struct sha256_state),
597 .base = {
598 .cra_name = "hmac(sha224)",
599 .cra_driver_name = "sha224-hmac-starfive",
600 .cra_priority = 200,
601 .cra_flags = CRYPTO_ALG_ASYNC |
602 CRYPTO_ALG_TYPE_AHASH |
603 CRYPTO_ALG_NEED_FALLBACK,
604 .cra_blocksize = SHA224_BLOCK_SIZE,
605 .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
606 .cra_module = THIS_MODULE,
607 }
608 },
609 .op = {
610 .do_one_request = starfive_hash_one_request,
611 },
612 }, {
613 .base.init = starfive_hash_init,
614 .base.update = starfive_hash_update,
615 .base.final = starfive_hash_final,
616 .base.finup = starfive_hash_finup,
617 .base.digest = starfive_hash_digest,
618 .base.export = starfive_hash_export,
619 .base.import = starfive_hash_import,
620 .base.init_tfm = starfive_sha256_init_tfm,
621 .base.exit_tfm = starfive_hash_exit_tfm,
622 .base.halg = {
623 .digestsize = SHA256_DIGEST_SIZE,
624 .statesize = sizeof(struct sha256_state),
625 .base = {
626 .cra_name = "sha256",
627 .cra_driver_name = "sha256-starfive",
628 .cra_priority = 200,
629 .cra_flags = CRYPTO_ALG_ASYNC |
630 CRYPTO_ALG_TYPE_AHASH |
631 CRYPTO_ALG_NEED_FALLBACK,
632 .cra_blocksize = SHA256_BLOCK_SIZE,
633 .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
634 .cra_module = THIS_MODULE,
635 }
636 },
637 .op = {
638 .do_one_request = starfive_hash_one_request,
639 },
640 }, {
641 .base.init = starfive_hash_init,
642 .base.update = starfive_hash_update,
643 .base.final = starfive_hash_final,
644 .base.finup = starfive_hash_finup,
645 .base.digest = starfive_hash_digest,
646 .base.export = starfive_hash_export,
647 .base.import = starfive_hash_import,
648 .base.init_tfm = starfive_hmac_sha256_init_tfm,
649 .base.exit_tfm = starfive_hash_exit_tfm,
650 .base.setkey = starfive_hash_setkey,
651 .base.halg = {
652 .digestsize = SHA256_DIGEST_SIZE,
653 .statesize = sizeof(struct sha256_state),
654 .base = {
655 .cra_name = "hmac(sha256)",
656 .cra_driver_name = "sha256-hmac-starfive",
657 .cra_priority = 200,
658 .cra_flags = CRYPTO_ALG_ASYNC |
659 CRYPTO_ALG_TYPE_AHASH |
660 CRYPTO_ALG_NEED_FALLBACK,
661 .cra_blocksize = SHA256_BLOCK_SIZE,
662 .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
663 .cra_module = THIS_MODULE,
664 }
665 },
666 .op = {
667 .do_one_request = starfive_hash_one_request,
668 },
669 }, {
670 .base.init = starfive_hash_init,
671 .base.update = starfive_hash_update,
672 .base.final = starfive_hash_final,
673 .base.finup = starfive_hash_finup,
674 .base.digest = starfive_hash_digest,
675 .base.export = starfive_hash_export,
676 .base.import = starfive_hash_import,
677 .base.init_tfm = starfive_sha384_init_tfm,
678 .base.exit_tfm = starfive_hash_exit_tfm,
679 .base.halg = {
680 .digestsize = SHA384_DIGEST_SIZE,
681 .statesize = sizeof(struct sha512_state),
682 .base = {
683 .cra_name = "sha384",
684 .cra_driver_name = "sha384-starfive",
685 .cra_priority = 200,
686 .cra_flags = CRYPTO_ALG_ASYNC |
687 CRYPTO_ALG_TYPE_AHASH |
688 CRYPTO_ALG_NEED_FALLBACK,
689 .cra_blocksize = SHA384_BLOCK_SIZE,
690 .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
691 .cra_module = THIS_MODULE,
692 }
693 },
694 .op = {
695 .do_one_request = starfive_hash_one_request,
696 },
697 }, {
698 .base.init = starfive_hash_init,
699 .base.update = starfive_hash_update,
700 .base.final = starfive_hash_final,
701 .base.finup = starfive_hash_finup,
702 .base.digest = starfive_hash_digest,
703 .base.export = starfive_hash_export,
704 .base.import = starfive_hash_import,
705 .base.init_tfm = starfive_hmac_sha384_init_tfm,
706 .base.exit_tfm = starfive_hash_exit_tfm,
707 .base.setkey = starfive_hash_setkey,
708 .base.halg = {
709 .digestsize = SHA384_DIGEST_SIZE,
710 .statesize = sizeof(struct sha512_state),
711 .base = {
712 .cra_name = "hmac(sha384)",
713 .cra_driver_name = "sha384-hmac-starfive",
714 .cra_priority = 200,
715 .cra_flags = CRYPTO_ALG_ASYNC |
716 CRYPTO_ALG_TYPE_AHASH |
717 CRYPTO_ALG_NEED_FALLBACK,
718 .cra_blocksize = SHA384_BLOCK_SIZE,
719 .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
720 .cra_module = THIS_MODULE,
721 }
722 },
723 .op = {
724 .do_one_request = starfive_hash_one_request,
725 },
726 }, {
727 .base.init = starfive_hash_init,
728 .base.update = starfive_hash_update,
729 .base.final = starfive_hash_final,
730 .base.finup = starfive_hash_finup,
731 .base.digest = starfive_hash_digest,
732 .base.export = starfive_hash_export,
733 .base.import = starfive_hash_import,
734 .base.init_tfm = starfive_sha512_init_tfm,
735 .base.exit_tfm = starfive_hash_exit_tfm,
736 .base.halg = {
737 .digestsize = SHA512_DIGEST_SIZE,
738 .statesize = sizeof(struct sha512_state),
739 .base = {
740 .cra_name = "sha512",
741 .cra_driver_name = "sha512-starfive",
742 .cra_priority = 200,
743 .cra_flags = CRYPTO_ALG_ASYNC |
744 CRYPTO_ALG_TYPE_AHASH |
745 CRYPTO_ALG_NEED_FALLBACK,
746 .cra_blocksize = SHA512_BLOCK_SIZE,
747 .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
748 .cra_module = THIS_MODULE,
749 }
750 },
751 .op = {
752 .do_one_request = starfive_hash_one_request,
753 },
754 }, {
755 .base.init = starfive_hash_init,
756 .base.update = starfive_hash_update,
757 .base.final = starfive_hash_final,
758 .base.finup = starfive_hash_finup,
759 .base.digest = starfive_hash_digest,
760 .base.export = starfive_hash_export,
761 .base.import = starfive_hash_import,
762 .base.init_tfm = starfive_hmac_sha512_init_tfm,
763 .base.exit_tfm = starfive_hash_exit_tfm,
764 .base.setkey = starfive_hash_setkey,
765 .base.halg = {
766 .digestsize = SHA512_DIGEST_SIZE,
767 .statesize = sizeof(struct sha512_state),
768 .base = {
769 .cra_name = "hmac(sha512)",
770 .cra_driver_name = "sha512-hmac-starfive",
771 .cra_priority = 200,
772 .cra_flags = CRYPTO_ALG_ASYNC |
773 CRYPTO_ALG_TYPE_AHASH |
774 CRYPTO_ALG_NEED_FALLBACK,
775 .cra_blocksize = SHA512_BLOCK_SIZE,
776 .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
777 .cra_module = THIS_MODULE,
778 }
779 },
780 .op = {
781 .do_one_request = starfive_hash_one_request,
782 },
783 }, {
784 .base.init = starfive_hash_init,
785 .base.update = starfive_hash_update,
786 .base.final = starfive_hash_final,
787 .base.finup = starfive_hash_finup,
788 .base.digest = starfive_hash_digest,
789 .base.export = starfive_hash_export,
790 .base.import = starfive_hash_import,
791 .base.init_tfm = starfive_sm3_init_tfm,
792 .base.exit_tfm = starfive_hash_exit_tfm,
793 .base.halg = {
794 .digestsize = SM3_DIGEST_SIZE,
795 .statesize = sizeof(struct sm3_state),
796 .base = {
797 .cra_name = "sm3",
798 .cra_driver_name = "sm3-starfive",
799 .cra_priority = 200,
800 .cra_flags = CRYPTO_ALG_ASYNC |
801 CRYPTO_ALG_TYPE_AHASH |
802 CRYPTO_ALG_NEED_FALLBACK,
803 .cra_blocksize = SM3_BLOCK_SIZE,
804 .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
805 .cra_module = THIS_MODULE,
806 }
807 },
808 .op = {
809 .do_one_request = starfive_hash_one_request,
810 },
811 }, {
812 .base.init = starfive_hash_init,
813 .base.update = starfive_hash_update,
814 .base.final = starfive_hash_final,
815 .base.finup = starfive_hash_finup,
816 .base.digest = starfive_hash_digest,
817 .base.export = starfive_hash_export,
818 .base.import = starfive_hash_import,
819 .base.init_tfm = starfive_hmac_sm3_init_tfm,
820 .base.exit_tfm = starfive_hash_exit_tfm,
821 .base.setkey = starfive_hash_setkey,
822 .base.halg = {
823 .digestsize = SM3_DIGEST_SIZE,
824 .statesize = sizeof(struct sm3_state),
825 .base = {
826 .cra_name = "hmac(sm3)",
827 .cra_driver_name = "sm3-hmac-starfive",
828 .cra_priority = 200,
829 .cra_flags = CRYPTO_ALG_ASYNC |
830 CRYPTO_ALG_TYPE_AHASH |
831 CRYPTO_ALG_NEED_FALLBACK,
832 .cra_blocksize = SM3_BLOCK_SIZE,
833 .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
834 .cra_module = THIS_MODULE,
835 }
836 },
837 .op = {
838 .do_one_request = starfive_hash_one_request,
839 },
840 },
841 };
842
starfive_hash_register_algs(void)843 int starfive_hash_register_algs(void)
844 {
845 return crypto_engine_register_ahashes(algs_sha2_sm3, ARRAY_SIZE(algs_sha2_sm3));
846 }
847
starfive_hash_unregister_algs(void)848 void starfive_hash_unregister_algs(void)
849 {
850 crypto_engine_unregister_ahashes(algs_sha2_sm3, ARRAY_SIZE(algs_sha2_sm3));
851 }
852