summaryrefslogtreecommitdiff
path: root/drivers/crypto/starfive/jh7110-hash.c
blob: 5064150b8a1ce40ea5067b19590304fe1c9d9fd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
// SPDX-License-Identifier: GPL-2.0
/*
 * Hash function and HMAC support for StarFive driver
 *
 * Copyright (c) 2022 StarFive Technology
 *
 */

#include <linux/clk.h>
#include <linux/crypto.h>
#include <linux/dma-direct.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/iopoll.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/reset.h>
#include <linux/amba/pl080.h>

#include <crypto/hash.h>
#include <crypto/scatterwalk.h>
#include <crypto/internal/hash.h>

#include "jh7110-cryp.h"

#define STARFIVE_HASH_REGS_OFFSET	0x300
#define STARFIVE_HASH_SHACSR		(STARFIVE_HASH_REGS_OFFSET + 0x0)
#define STARFIVE_HASH_SHAWDR		(STARFIVE_HASH_REGS_OFFSET + 0x4)
#define STARFIVE_HASH_SHARDR		(STARFIVE_HASH_REGS_OFFSET + 0x8)
#define STARFIVE_HASH_SHAWSR		(STARFIVE_HASH_REGS_OFFSET + 0xC)
#define STARFIVE_HASH_SHAWLEN3		(STARFIVE_HASH_REGS_OFFSET + 0x10)
#define STARFIVE_HASH_SHAWLEN2		(STARFIVE_HASH_REGS_OFFSET + 0x14)
#define STARFIVE_HASH_SHAWLEN1		(STARFIVE_HASH_REGS_OFFSET + 0x18)
#define STARFIVE_HASH_SHAWLEN0		(STARFIVE_HASH_REGS_OFFSET + 0x1C)
#define STARFIVE_HASH_SHAWKR		(STARFIVE_HASH_REGS_OFFSET + 0x20)
#define STARFIVE_HASH_SHAWKLEN		(STARFIVE_HASH_REGS_OFFSET + 0x24)

#define STARFIVE_HASH_BUFLEN		SHA512_BLOCK_SIZE
#define STARFIVE_HASH_RESET		0x2

static inline int starfive_hash_wait_busy(struct starfive_cryp_ctx *ctx)
{
	struct starfive_cryp_dev *cryp = ctx->cryp;
	u32 status;

	return readl_relaxed_poll_timeout(cryp->base + STARFIVE_HASH_SHACSR, status,
					  !(status & STARFIVE_HASH_BUSY), 10, 100000);
}

static inline int starfive_hash_wait_key_done(struct starfive_cryp_ctx *ctx)
{
	struct starfive_cryp_dev *cryp = ctx->cryp;
	u32 status;

	return readl_relaxed_poll_timeout(cryp->base + STARFIVE_HASH_SHACSR, status,
					  (status & STARFIVE_HASH_KEY_DONE), 10, 100000);
}

static int starfive_hash_hmac_key(struct starfive_cryp_ctx *ctx)
{
	struct starfive_cryp_request_ctx *rctx = ctx->rctx;
	struct starfive_cryp_dev *cryp = ctx->cryp;
	int klen = ctx->keylen, loop;
	unsigned int *key = (unsigned int *)ctx->key;
	unsigned char *cl;

	writel(ctx->keylen, cryp->base + STARFIVE_HASH_SHAWKLEN);

	rctx->csr.hash.hmac = 1;
	rctx->csr.hash.key_flag = 1;

	writel(rctx->csr.hash.v, cryp->base + STARFIVE_HASH_SHACSR);

	for (loop = 0; loop < klen / sizeof(unsigned int); loop++, key++)
		writel(*key, cryp->base + STARFIVE_HASH_SHAWKR);

	if (klen & 0x3) {
		cl = (unsigned char *)key;
		for (loop = 0; loop < (klen & 0x3); loop++, cl++)
			writeb(*cl, cryp->base + STARFIVE_HASH_SHAWKR);
	}

	if (starfive_hash_wait_key_done(ctx))
		return dev_err_probe(cryp->dev, -ETIMEDOUT, "starfive_hash_wait_key_done error\n");

	return 0;
}

static void starfive_hash_start(void *param)
{
	struct starfive_cryp_ctx *ctx = param;
	struct starfive_cryp_request_ctx *rctx = ctx->rctx;
	struct starfive_cryp_dev *cryp = ctx->cryp;
	union starfive_alg_cr alg_cr;
	union starfive_hash_csr csr;
	u32 stat;

	dma_unmap_sg(cryp->dev, rctx->in_sg, rctx->in_sg_len, DMA_TO_DEVICE);

	alg_cr.v = 0;
	alg_cr.clear = 1;

	writel(alg_cr.v, cryp->base + STARFIVE_ALG_CR_OFFSET);

	csr.v = readl(cryp->base + STARFIVE_HASH_SHACSR);
	csr.firstb = 0;
	csr.final = 1;

	stat = readl(cryp->base + STARFIVE_IE_MASK_OFFSET);
	stat &= ~STARFIVE_IE_MASK_HASH_DONE;
	writel(stat, cryp->base + STARFIVE_IE_MASK_OFFSET);
	writel(csr.v, cryp->base + STARFIVE_HASH_SHACSR);
}

static int starfive_hash_xmit_dma(struct starfive_cryp_ctx *ctx)
{
	struct starfive_cryp_request_ctx *rctx = ctx->rctx;
	struct starfive_cryp_dev *cryp = ctx->cryp;
	struct dma_async_tx_descriptor	*in_desc;
	union  starfive_alg_cr alg_cr;
	int total_len;
	int ret;

	if (!rctx->total) {
		starfive_hash_start(ctx);
		return 0;
	}

	writel(rctx->total, cryp->base + STARFIVE_DMA_IN_LEN_OFFSET);

	total_len = rctx->total;
	total_len = (total_len & 0x3) ? (((total_len >> 2) + 1) << 2) : total_len;
	sg_dma_len(rctx->in_sg) = total_len;

	alg_cr.v = 0;
	alg_cr.start = 1;
	alg_cr.hash_dma_en = 1;

	writel(alg_cr.v, cryp->base + STARFIVE_ALG_CR_OFFSET);

	ret = dma_map_sg(cryp->dev, rctx->in_sg, rctx->in_sg_len, DMA_TO_DEVICE);
	if (!ret)
		return dev_err_probe(cryp->dev, -EINVAL, "dma_map_sg() error\n");

	cryp->cfg_in.direction = DMA_MEM_TO_DEV;
	cryp->cfg_in.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
	cryp->cfg_in.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
	cryp->cfg_in.src_maxburst = cryp->dma_maxburst;
	cryp->cfg_in.dst_maxburst = cryp->dma_maxburst;
	cryp->cfg_in.dst_addr = cryp->phys_base + STARFIVE_ALG_FIFO_OFFSET;

	dmaengine_slave_config(cryp->tx, &cryp->cfg_in);

	in_desc = dmaengine_prep_slave_sg(cryp->tx, rctx->in_sg,
					  ret, DMA_MEM_TO_DEV,
					  DMA_PREP_INTERRUPT  |  DMA_CTRL_ACK);

	if (!in_desc)
		return -EINVAL;

	in_desc->callback = starfive_hash_start;
	in_desc->callback_param = ctx;

	dmaengine_submit(in_desc);
	dma_async_issue_pending(cryp->tx);

	return 0;
}

static int starfive_hash_xmit(struct starfive_cryp_ctx *ctx)
{
	struct starfive_cryp_request_ctx *rctx = ctx->rctx;
	struct starfive_cryp_dev *cryp = ctx->cryp;
	int ret = 0;

	rctx->csr.hash.v = 0;
	rctx->csr.hash.reset = 1;
	writel(rctx->csr.hash.v, cryp->base + STARFIVE_HASH_SHACSR);

	if (starfive_hash_wait_busy(ctx))
		return dev_err_probe(cryp->dev, -ETIMEDOUT, "Error resetting engine.\n");

	rctx->csr.hash.v = 0;
	rctx->csr.hash.mode = ctx->hash_mode;
	rctx->csr.hash.ie = 1;

	if (ctx->is_hmac) {
		ret = starfive_hash_hmac_key(ctx);
		if (ret)
			return ret;
	} else {
		rctx->csr.hash.start = 1;
		rctx->csr.hash.firstb = 1;
		writel(rctx->csr.hash.v, cryp->base + STARFIVE_HASH_SHACSR);
	}

	return starfive_hash_xmit_dma(ctx);
}

static int starfive_hash_copy_hash(struct ahash_request *req)
{
	struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
	int count, *data;
	int mlen;

	if (!req->result)
		return 0;

	mlen = rctx->digsize / sizeof(u32);
	data = (u32 *)req->result;

	for (count = 0; count < mlen; count++)
		data[count] = readl(ctx->cryp->base + STARFIVE_HASH_SHARDR);

	return 0;
}

void starfive_hash_done_task(unsigned long param)
{
	struct starfive_cryp_dev *cryp = (struct starfive_cryp_dev *)param;
	int err = cryp->err;

	if (!err)
		err = starfive_hash_copy_hash(cryp->req.hreq);

	/* Reset to clear hash_done in irq register*/
	writel(STARFIVE_HASH_RESET, cryp->base + STARFIVE_HASH_SHACSR);

	crypto_finalize_hash_request(cryp->engine, cryp->req.hreq, err);
}

static int starfive_hash_check_aligned(struct scatterlist *sg, size_t total, size_t align)
{
	int len = 0;

	if (!total)
		return 0;

	if (!IS_ALIGNED(total, align))
		return -EINVAL;

	while (sg) {
		if (!IS_ALIGNED(sg->offset, sizeof(u32)))
			return -EINVAL;

		if (!IS_ALIGNED(sg->length, align))
			return -EINVAL;

		len += sg->length;
		sg = sg_next(sg);
	}

	if (len != total)
		return -EINVAL;

	return 0;
}

static int starfive_hash_one_request(struct crypto_engine *engine, void *areq)
{
	struct ahash_request *req = container_of(areq, struct ahash_request,
						 base);
	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
	struct starfive_cryp_dev *cryp = ctx->cryp;

	if (!cryp)
		return -ENODEV;

	return starfive_hash_xmit(ctx);
}

static int starfive_hash_init(struct ahash_request *req)
{
	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
	struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);

	ahash_request_set_tfm(&rctx->ahash_fbk_req, ctx->ahash_fbk);
	ahash_request_set_callback(&rctx->ahash_fbk_req,
				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
				   req->base.complete, req->base.data);

	ahash_request_set_crypt(&rctx->ahash_fbk_req, req->src,
				req->result, req->nbytes);

	return crypto_ahash_init(&rctx->ahash_fbk_req);
}

static int starfive_hash_update(struct ahash_request *req)
{
	struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);

	ahash_request_set_tfm(&rctx->ahash_fbk_req, ctx->ahash_fbk);
	ahash_request_set_callback(&rctx->ahash_fbk_req,
				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
				   req->base.complete, req->base.data);

	ahash_request_set_crypt(&rctx->ahash_fbk_req, req->src,
				req->result, req->nbytes);

	return crypto_ahash_update(&rctx->ahash_fbk_req);
}

static int starfive_hash_final(struct ahash_request *req)
{
	struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);

	ahash_request_set_tfm(&rctx->ahash_fbk_req, ctx->ahash_fbk);
	ahash_request_set_callback(&rctx->ahash_fbk_req,
				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
				   req->base.complete, req->base.data);

	ahash_request_set_crypt(&rctx->ahash_fbk_req, req->src,
				req->result, req->nbytes);

	return crypto_ahash_final(&rctx->ahash_fbk_req);
}

static int starfive_hash_finup(struct ahash_request *req)
{
	struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);

	ahash_request_set_tfm(&rctx->ahash_fbk_req, ctx->ahash_fbk);
	ahash_request_set_callback(&rctx->ahash_fbk_req,
				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
				   req->base.complete, req->base.data);

	ahash_request_set_crypt(&rctx->ahash_fbk_req, req->src,
				req->result, req->nbytes);

	return crypto_ahash_finup(&rctx->ahash_fbk_req);
}

static int starfive_hash_digest_fb(struct ahash_request *req)
{
	struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);

	ahash_request_set_tfm(&rctx->ahash_fbk_req, ctx->ahash_fbk);
	ahash_request_set_callback(&rctx->ahash_fbk_req, req->base.flags,
				   req->base.complete, req->base.data);

	ahash_request_set_crypt(&rctx->ahash_fbk_req, req->src,
				req->result, req->nbytes);

	return crypto_ahash_digest(&rctx->ahash_fbk_req);
}

static int starfive_hash_digest(struct ahash_request *req)
{
	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);
	struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
	struct starfive_cryp_dev *cryp = ctx->cryp;

	memset(rctx, 0, sizeof(struct starfive_cryp_request_ctx));

	cryp->req.hreq = req;
	rctx->total = req->nbytes;
	rctx->in_sg = req->src;
	rctx->blksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
	rctx->digsize = crypto_ahash_digestsize(tfm);
	rctx->in_sg_len = sg_nents_for_len(rctx->in_sg, rctx->total);
	ctx->rctx = rctx;

	if (starfive_hash_check_aligned(rctx->in_sg, rctx->total, rctx->blksize))
		return starfive_hash_digest_fb(req);

	return crypto_transfer_hash_request_to_engine(cryp->engine, req);
}

static int starfive_hash_export(struct ahash_request *req, void *out)
{
	struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);

	ahash_request_set_tfm(&rctx->ahash_fbk_req, ctx->ahash_fbk);
	ahash_request_set_callback(&rctx->ahash_fbk_req,
				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
				   req->base.complete, req->base.data);

	return crypto_ahash_export(&rctx->ahash_fbk_req, out);
}

static int starfive_hash_import(struct ahash_request *req, const void *in)
{
	struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);

	ahash_request_set_tfm(&rctx->ahash_fbk_req, ctx->ahash_fbk);
	ahash_request_set_callback(&rctx->ahash_fbk_req,
				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
				   req->base.complete, req->base.data);

	return crypto_ahash_import(&rctx->ahash_fbk_req, in);
}

static int starfive_hash_init_tfm(struct crypto_ahash *hash,
				  const char *alg_name,
				  unsigned int mode)
{
	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(hash);

	ctx->cryp = starfive_cryp_find_dev(ctx);

	if (!ctx->cryp)
		return -ENODEV;

	ctx->ahash_fbk = crypto_alloc_ahash(alg_name, 0,
					    CRYPTO_ALG_NEED_FALLBACK);

	if (IS_ERR(ctx->ahash_fbk))
		return dev_err_probe(ctx->cryp->dev, PTR_ERR(ctx->ahash_fbk),
				     "starfive_hash: Could not load fallback driver.\n");

	crypto_ahash_set_statesize(hash, crypto_ahash_statesize(ctx->ahash_fbk));
	crypto_ahash_set_reqsize(hash, sizeof(struct starfive_cryp_request_ctx) +
				 crypto_ahash_reqsize(ctx->ahash_fbk));

	ctx->keylen = 0;
	ctx->hash_mode = mode;

	ctx->enginectx.op.do_one_request = starfive_hash_one_request;
	ctx->enginectx.op.prepare_request = NULL;
	ctx->enginectx.op.unprepare_request = NULL;

	return 0;
}

static void starfive_hash_exit_tfm(struct crypto_ahash *hash)
{
	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(hash);

	crypto_free_ahash(ctx->ahash_fbk);

	ctx->ahash_fbk = NULL;
	ctx->enginectx.op.do_one_request = NULL;
	ctx->enginectx.op.prepare_request = NULL;
	ctx->enginectx.op.unprepare_request = NULL;
}

static int starfive_hash_long_setkey(struct starfive_cryp_ctx *ctx,
				     const u8 *key, unsigned int keylen,
				     const char *alg_name)
{
	struct crypto_wait wait;
	struct ahash_request *req;
	struct scatterlist sg;
	struct crypto_ahash *ahash_tfm;
	u8 *buf;
	int ret;

	ahash_tfm = crypto_alloc_ahash(alg_name, 0, 0);
	if (IS_ERR(ahash_tfm))
		return PTR_ERR(ahash_tfm);

	req = ahash_request_alloc(ahash_tfm, GFP_KERNEL);
	if (!req) {
		ret = -ENOMEM;
		goto err_free_ahash;
	}

	crypto_init_wait(&wait);
	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
				   crypto_req_done, &wait);
	crypto_ahash_clear_flags(ahash_tfm, ~0);

	buf = kzalloc(keylen + STARFIVE_HASH_BUFLEN, GFP_KERNEL);
	if (!buf) {
		ret = -ENOMEM;
		goto err_free_req;
	}

	memcpy(buf, key, keylen);
	sg_init_one(&sg, buf, keylen);
	ahash_request_set_crypt(req, &sg, ctx->key, keylen);

	ret = crypto_wait_req(crypto_ahash_digest(req), &wait);

	kfree(buf);
err_free_req:
	ahash_request_free(req);
err_free_ahash:
	crypto_free_ahash(ahash_tfm);
	return ret;
}

static int starfive_hash_setkey(struct crypto_ahash *hash,
				const u8 *key, unsigned int keylen)
{
	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(hash);
	unsigned int digestsize = crypto_ahash_digestsize(hash);
	unsigned int blocksize = crypto_ahash_blocksize(hash);
	const char *alg_name;

	crypto_ahash_setkey(ctx->ahash_fbk, key, keylen);

	if (keylen <= blocksize) {
		memcpy(ctx->key, key, keylen);
		ctx->keylen = keylen;
		return 0;
	}

	ctx->keylen = digestsize;

	switch (digestsize) {
	case SHA224_DIGEST_SIZE:
		alg_name = "sha224-starfive";
		break;
	case SHA256_DIGEST_SIZE:
		if (ctx->hash_mode == STARFIVE_HASH_SM3)
			alg_name = "sm3-starfive";
		else
			alg_name = "sha256-starfive";
		break;
	case SHA384_DIGEST_SIZE:
		alg_name = "sha384-starfive";
		break;
	case SHA512_DIGEST_SIZE:
		alg_name = "sha512-starfive";
		break;
	default:
		return -EINVAL;
	}

	return starfive_hash_long_setkey(ctx, key, keylen, alg_name);
}

static int starfive_sha224_init_tfm(struct crypto_ahash *hash)
{
	return starfive_hash_init_tfm(hash, "sha224-generic",
				      STARFIVE_HASH_SHA224);
}

static int starfive_sha256_init_tfm(struct crypto_ahash *hash)
{
	return starfive_hash_init_tfm(hash, "sha256-generic",
				      STARFIVE_HASH_SHA256);
}

static int starfive_sha384_init_tfm(struct crypto_ahash *hash)
{
	return starfive_hash_init_tfm(hash, "sha384-generic",
				      STARFIVE_HASH_SHA384);
}

static int starfive_sha512_init_tfm(struct crypto_ahash *hash)
{
	return starfive_hash_init_tfm(hash, "sha512-generic",
				      STARFIVE_HASH_SHA512);
}

static int starfive_sm3_init_tfm(struct crypto_ahash *hash)
{
	return starfive_hash_init_tfm(hash, "sm3-generic",
				      STARFIVE_HASH_SM3);
}

static int starfive_hmac_sha224_init_tfm(struct crypto_ahash *hash)
{
	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(hash);

	ctx->is_hmac = true;

	return starfive_hash_init_tfm(hash, "hmac(sha224-generic)",
				      STARFIVE_HASH_SHA224);
}

static int starfive_hmac_sha256_init_tfm(struct crypto_ahash *hash)
{
	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(hash);

	ctx->is_hmac = true;

	return starfive_hash_init_tfm(hash, "hmac(sha256-generic)",
				      STARFIVE_HASH_SHA256);
}

static int starfive_hmac_sha384_init_tfm(struct crypto_ahash *hash)
{
	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(hash);

	ctx->is_hmac = true;

	return starfive_hash_init_tfm(hash, "hmac(sha384-generic)",
				      STARFIVE_HASH_SHA384);
}

static int starfive_hmac_sha512_init_tfm(struct crypto_ahash *hash)
{
	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(hash);

	ctx->is_hmac = true;

	return starfive_hash_init_tfm(hash, "hmac(sha512-generic)",
				      STARFIVE_HASH_SHA512);
}

static int starfive_hmac_sm3_init_tfm(struct crypto_ahash *hash)
{
	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(hash);

	ctx->is_hmac = true;

	return starfive_hash_init_tfm(hash, "hmac(sm3-generic)",
				      STARFIVE_HASH_SM3);
}

static struct ahash_alg algs_sha2_sm3[] = {
{
	.init     = starfive_hash_init,
	.update   = starfive_hash_update,
	.final    = starfive_hash_final,
	.finup    = starfive_hash_finup,
	.digest   = starfive_hash_digest,
	.export   = starfive_hash_export,
	.import   = starfive_hash_import,
	.init_tfm = starfive_sha224_init_tfm,
	.exit_tfm = starfive_hash_exit_tfm,
	.halg = {
		.digestsize = SHA224_DIGEST_SIZE,
		.statesize  = sizeof(struct sha256_state),
		.base = {
			.cra_name		= "sha224",
			.cra_driver_name	= "sha224-starfive",
			.cra_priority		= 200,
			.cra_flags		= CRYPTO_ALG_ASYNC |
						  CRYPTO_ALG_TYPE_AHASH |
						  CRYPTO_ALG_NEED_FALLBACK,
			.cra_blocksize		= SHA224_BLOCK_SIZE,
			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
			.cra_alignmask		= 3,
			.cra_module		= THIS_MODULE,
		}
	}
}, {
	.init     = starfive_hash_init,
	.update   = starfive_hash_update,
	.final    = starfive_hash_final,
	.finup    = starfive_hash_finup,
	.digest   = starfive_hash_digest,
	.export   = starfive_hash_export,
	.import   = starfive_hash_import,
	.init_tfm = starfive_hmac_sha224_init_tfm,
	.exit_tfm = starfive_hash_exit_tfm,
	.setkey   = starfive_hash_setkey,
	.halg = {
		.digestsize = SHA224_DIGEST_SIZE,
		.statesize  = sizeof(struct sha256_state),
		.base = {
			.cra_name		= "hmac(sha224)",
			.cra_driver_name	= "sha224-hmac-starfive",
			.cra_priority		= 200,
			.cra_flags		= CRYPTO_ALG_ASYNC |
						  CRYPTO_ALG_TYPE_AHASH |
						  CRYPTO_ALG_NEED_FALLBACK,
			.cra_blocksize		= SHA224_BLOCK_SIZE,
			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
			.cra_alignmask		= 3,
			.cra_module		= THIS_MODULE,
		}
	}
}, {
	.init     = starfive_hash_init,
	.update   = starfive_hash_update,
	.final    = starfive_hash_final,
	.finup    = starfive_hash_finup,
	.digest   = starfive_hash_digest,
	.export   = starfive_hash_export,
	.import   = starfive_hash_import,
	.init_tfm = starfive_sha256_init_tfm,
	.exit_tfm = starfive_hash_exit_tfm,
	.halg = {
		.digestsize = SHA256_DIGEST_SIZE,
		.statesize  = sizeof(struct sha256_state),
		.base = {
			.cra_name		= "sha256",
			.cra_driver_name	= "sha256-starfive",
			.cra_priority		= 200,
			.cra_flags		= CRYPTO_ALG_ASYNC |
						  CRYPTO_ALG_TYPE_AHASH |
						  CRYPTO_ALG_NEED_FALLBACK,
			.cra_blocksize		= SHA256_BLOCK_SIZE,
			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
			.cra_alignmask		= 3,
			.cra_module		= THIS_MODULE,
		}
	}
}, {
	.init     = starfive_hash_init,
	.update   = starfive_hash_update,
	.final    = starfive_hash_final,
	.finup    = starfive_hash_finup,
	.digest   = starfive_hash_digest,
	.export   = starfive_hash_export,
	.import   = starfive_hash_import,
	.init_tfm = starfive_hmac_sha256_init_tfm,
	.exit_tfm = starfive_hash_exit_tfm,
	.setkey   = starfive_hash_setkey,
	.halg = {
		.digestsize = SHA256_DIGEST_SIZE,
		.statesize  = sizeof(struct sha256_state),
		.base = {
			.cra_name		= "hmac(sha256)",
			.cra_driver_name	= "sha256-hmac-starfive",
			.cra_priority		= 200,
			.cra_flags		= CRYPTO_ALG_ASYNC |
						  CRYPTO_ALG_TYPE_AHASH |
						  CRYPTO_ALG_NEED_FALLBACK,
			.cra_blocksize		= SHA256_BLOCK_SIZE,
			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
			.cra_alignmask		= 3,
			.cra_module		= THIS_MODULE,
		}
	}
}, {
	.init     = starfive_hash_init,
	.update   = starfive_hash_update,
	.final    = starfive_hash_final,
	.finup    = starfive_hash_finup,
	.digest   = starfive_hash_digest,
	.export   = starfive_hash_export,
	.import   = starfive_hash_import,
	.init_tfm = starfive_sha384_init_tfm,
	.exit_tfm = starfive_hash_exit_tfm,
	.halg = {
		.digestsize = SHA384_DIGEST_SIZE,
		.statesize  = sizeof(struct sha512_state),
		.base = {
			.cra_name		= "sha384",
			.cra_driver_name	= "sha384-starfive",
			.cra_priority		= 200,
			.cra_flags		= CRYPTO_ALG_ASYNC |
						  CRYPTO_ALG_TYPE_AHASH |
						  CRYPTO_ALG_NEED_FALLBACK,
			.cra_blocksize		= SHA384_BLOCK_SIZE,
			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
			.cra_alignmask		= 3,
			.cra_module		= THIS_MODULE,
		}
	}
}, {
	.init     = starfive_hash_init,
	.update   = starfive_hash_update,
	.final    = starfive_hash_final,
	.finup    = starfive_hash_finup,
	.digest   = starfive_hash_digest,
	.export   = starfive_hash_export,
	.import   = starfive_hash_import,
	.init_tfm = starfive_hmac_sha384_init_tfm,
	.exit_tfm = starfive_hash_exit_tfm,
	.setkey   = starfive_hash_setkey,
	.halg = {
		.digestsize = SHA384_DIGEST_SIZE,
		.statesize  = sizeof(struct sha512_state),
		.base = {
			.cra_name		= "hmac(sha384)",
			.cra_driver_name	= "sha384-hmac-starfive",
			.cra_priority		= 200,
			.cra_flags		= CRYPTO_ALG_ASYNC |
						  CRYPTO_ALG_TYPE_AHASH |
						  CRYPTO_ALG_NEED_FALLBACK,
			.cra_blocksize		= SHA384_BLOCK_SIZE,
			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
			.cra_alignmask		= 3,
			.cra_module		= THIS_MODULE,
		}
	}
}, {
	.init     = starfive_hash_init,
	.update   = starfive_hash_update,
	.final    = starfive_hash_final,
	.finup    = starfive_hash_finup,
	.digest   = starfive_hash_digest,
	.export   = starfive_hash_export,
	.import   = starfive_hash_import,
	.init_tfm = starfive_sha512_init_tfm,
	.exit_tfm = starfive_hash_exit_tfm,
	.halg = {
		.digestsize = SHA512_DIGEST_SIZE,
		.statesize  = sizeof(struct sha512_state),
		.base = {
			.cra_name		= "sha512",
			.cra_driver_name	= "sha512-starfive",
			.cra_priority		= 200,
			.cra_flags		= CRYPTO_ALG_ASYNC |
						  CRYPTO_ALG_TYPE_AHASH |
						  CRYPTO_ALG_NEED_FALLBACK,
			.cra_blocksize		= SHA512_BLOCK_SIZE,
			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
			.cra_alignmask		= 3,
			.cra_module		= THIS_MODULE,
		}
	}
}, {
	.init     = starfive_hash_init,
	.update   = starfive_hash_update,
	.final    = starfive_hash_final,
	.finup    = starfive_hash_finup,
	.digest   = starfive_hash_digest,
	.export   = starfive_hash_export,
	.import   = starfive_hash_import,
	.init_tfm = starfive_hmac_sha512_init_tfm,
	.exit_tfm = starfive_hash_exit_tfm,
	.setkey   = starfive_hash_setkey,
	.halg = {
		.digestsize = SHA512_DIGEST_SIZE,
		.statesize  = sizeof(struct sha512_state),
		.base = {
			.cra_name		= "hmac(sha512)",
			.cra_driver_name	= "sha512-hmac-starfive",
			.cra_priority		= 200,
			.cra_flags		= CRYPTO_ALG_ASYNC |
						  CRYPTO_ALG_TYPE_AHASH |
						  CRYPTO_ALG_NEED_FALLBACK,
			.cra_blocksize		= SHA512_BLOCK_SIZE,
			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
			.cra_alignmask		= 3,
			.cra_module		= THIS_MODULE,
		}
	}
}, {
	.init     = starfive_hash_init,
	.update   = starfive_hash_update,
	.final    = starfive_hash_final,
	.finup    = starfive_hash_finup,
	.digest   = starfive_hash_digest,
	.export   = starfive_hash_export,
	.import   = starfive_hash_import,
	.init_tfm = starfive_sm3_init_tfm,
	.exit_tfm = starfive_hash_exit_tfm,
	.halg = {
		.digestsize = SM3_DIGEST_SIZE,
		.statesize  = sizeof(struct sm3_state),
		.base = {
			.cra_name		= "sm3",
			.cra_driver_name	= "sm3-starfive",
			.cra_priority		= 200,
			.cra_flags		= CRYPTO_ALG_ASYNC |
						  CRYPTO_ALG_TYPE_AHASH |
						  CRYPTO_ALG_NEED_FALLBACK,
			.cra_blocksize		= SM3_BLOCK_SIZE,
			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
			.cra_alignmask		= 3,
			.cra_module		= THIS_MODULE,
		}
	}
}, {
	.init	  = starfive_hash_init,
	.update	  = starfive_hash_update,
	.final	  = starfive_hash_final,
	.finup	  = starfive_hash_finup,
	.digest	  = starfive_hash_digest,
	.export	  = starfive_hash_export,
	.import	  = starfive_hash_import,
	.init_tfm = starfive_hmac_sm3_init_tfm,
	.exit_tfm = starfive_hash_exit_tfm,
	.setkey	  = starfive_hash_setkey,
	.halg = {
		.digestsize = SM3_DIGEST_SIZE,
		.statesize  = sizeof(struct sm3_state),
		.base = {
			.cra_name		= "hmac(sm3)",
			.cra_driver_name	= "sm3-hmac-starfive",
			.cra_priority		= 200,
			.cra_flags		= CRYPTO_ALG_ASYNC |
						  CRYPTO_ALG_TYPE_AHASH |
						  CRYPTO_ALG_NEED_FALLBACK,
			.cra_blocksize		= SM3_BLOCK_SIZE,
			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
			.cra_alignmask		= 3,
			.cra_module		= THIS_MODULE,
		}
	}
},
};

int starfive_hash_register_algs(void)
{
	return crypto_register_ahashes(algs_sha2_sm3, ARRAY_SIZE(algs_sha2_sm3));
}

void starfive_hash_unregister_algs(void)
{
	crypto_unregister_ahashes(algs_sha2_sm3, ARRAY_SIZE(algs_sha2_sm3));
}