xref: /linux/drivers/crypto/starfive/jh7110-hash.c (revision 8a7c601e14576a22c2bbf7f67455ccf3f3d2737f)
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 -ENOMEM;
233 
234 		ret = starfive_hash_dma_xfer(cryp, tsg);
235 		dma_unmap_sg(cryp->dev, tsg, 1, DMA_TO_DEVICE);
236 		if (ret)
237 			return ret;
238 	}
239 
240 hash_start:
241 	starfive_hash_start(cryp);
242 
243 	if (starfive_hash_wait_busy(cryp))
244 		return dev_err_probe(cryp->dev, -ETIMEDOUT, "Error generating digest\n");
245 
246 	if (ctx->is_hmac)
247 		cryp->err = starfive_hash_wait_hmac_done(cryp);
248 
249 	starfive_hash_done_task(cryp);
250 
251 	return 0;
252 }
253 
254 static int starfive_hash_init(struct ahash_request *req)
255 {
256 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
257 	struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
258 	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);
259 
260 	ahash_request_set_tfm(&rctx->ahash_fbk_req, ctx->ahash_fbk);
261 	ahash_request_set_callback(&rctx->ahash_fbk_req,
262 				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
263 				   req->base.complete, req->base.data);
264 
265 	ahash_request_set_crypt(&rctx->ahash_fbk_req, req->src,
266 				req->result, req->nbytes);
267 
268 	return crypto_ahash_init(&rctx->ahash_fbk_req);
269 }
270 
271 static int starfive_hash_update(struct ahash_request *req)
272 {
273 	struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
274 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
275 	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);
276 
277 	ahash_request_set_tfm(&rctx->ahash_fbk_req, ctx->ahash_fbk);
278 	ahash_request_set_callback(&rctx->ahash_fbk_req,
279 				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
280 				   req->base.complete, req->base.data);
281 
282 	ahash_request_set_crypt(&rctx->ahash_fbk_req, req->src,
283 				req->result, req->nbytes);
284 
285 	return crypto_ahash_update(&rctx->ahash_fbk_req);
286 }
287 
288 static int starfive_hash_final(struct ahash_request *req)
289 {
290 	struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
291 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
292 	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);
293 
294 	ahash_request_set_tfm(&rctx->ahash_fbk_req, ctx->ahash_fbk);
295 	ahash_request_set_callback(&rctx->ahash_fbk_req,
296 				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
297 				   req->base.complete, req->base.data);
298 
299 	ahash_request_set_crypt(&rctx->ahash_fbk_req, req->src,
300 				req->result, req->nbytes);
301 
302 	return crypto_ahash_final(&rctx->ahash_fbk_req);
303 }
304 
305 static int starfive_hash_finup(struct ahash_request *req)
306 {
307 	struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
308 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
309 	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);
310 
311 	ahash_request_set_tfm(&rctx->ahash_fbk_req, ctx->ahash_fbk);
312 	ahash_request_set_callback(&rctx->ahash_fbk_req,
313 				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
314 				   req->base.complete, req->base.data);
315 
316 	ahash_request_set_crypt(&rctx->ahash_fbk_req, req->src,
317 				req->result, req->nbytes);
318 
319 	return crypto_ahash_finup(&rctx->ahash_fbk_req);
320 }
321 
322 static int starfive_hash_digest(struct ahash_request *req)
323 {
324 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
325 	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);
326 	struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
327 	struct starfive_cryp_dev *cryp = ctx->cryp;
328 
329 	memset(rctx, 0, sizeof(struct starfive_cryp_request_ctx));
330 
331 	cryp->req.hreq = req;
332 	rctx->total = req->nbytes;
333 	rctx->in_sg = req->src;
334 	rctx->blksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
335 	rctx->digsize = crypto_ahash_digestsize(tfm);
336 	rctx->in_sg_len = sg_nents_for_len(rctx->in_sg, rctx->total);
337 	ctx->rctx = rctx;
338 
339 	return crypto_transfer_hash_request_to_engine(cryp->engine, req);
340 }
341 
342 static int starfive_hash_export(struct ahash_request *req, void *out)
343 {
344 	struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
345 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
346 	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);
347 
348 	ahash_request_set_tfm(&rctx->ahash_fbk_req, ctx->ahash_fbk);
349 	ahash_request_set_callback(&rctx->ahash_fbk_req,
350 				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
351 				   req->base.complete, req->base.data);
352 
353 	return crypto_ahash_export(&rctx->ahash_fbk_req, out);
354 }
355 
356 static int starfive_hash_import(struct ahash_request *req, const void *in)
357 {
358 	struct starfive_cryp_request_ctx *rctx = ahash_request_ctx(req);
359 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
360 	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(tfm);
361 
362 	ahash_request_set_tfm(&rctx->ahash_fbk_req, ctx->ahash_fbk);
363 	ahash_request_set_callback(&rctx->ahash_fbk_req,
364 				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP,
365 				   req->base.complete, req->base.data);
366 
367 	return crypto_ahash_import(&rctx->ahash_fbk_req, in);
368 }
369 
370 static int starfive_hash_init_tfm(struct crypto_ahash *hash,
371 				  const char *alg_name,
372 				  unsigned int mode,
373 				  bool is_hmac)
374 {
375 	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(hash);
376 
377 	ctx->cryp = starfive_cryp_find_dev(ctx);
378 
379 	if (!ctx->cryp)
380 		return -ENODEV;
381 
382 	ctx->ahash_fbk = crypto_alloc_ahash(alg_name, 0,
383 					    CRYPTO_ALG_NEED_FALLBACK);
384 
385 	if (IS_ERR(ctx->ahash_fbk))
386 		return dev_err_probe(ctx->cryp->dev, PTR_ERR(ctx->ahash_fbk),
387 				     "starfive_hash: Could not load fallback driver.\n");
388 
389 	crypto_ahash_set_statesize(hash, crypto_ahash_statesize(ctx->ahash_fbk));
390 	crypto_ahash_set_reqsize(hash, sizeof(struct starfive_cryp_request_ctx) +
391 				 crypto_ahash_reqsize(ctx->ahash_fbk));
392 
393 	ctx->is_hmac = is_hmac;
394 	ctx->hash_mode = mode;
395 
396 	return 0;
397 }
398 
399 static void starfive_hash_exit_tfm(struct crypto_ahash *hash)
400 {
401 	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(hash);
402 
403 	crypto_free_ahash(ctx->ahash_fbk);
404 }
405 
406 static int starfive_hash_long_setkey(struct starfive_cryp_ctx *ctx,
407 				     const u8 *key, unsigned int keylen,
408 				     const char *alg_name)
409 {
410 	struct crypto_wait wait;
411 	struct ahash_request *req;
412 	struct scatterlist sg;
413 	struct crypto_ahash *ahash_tfm;
414 	u8 *buf;
415 	int ret;
416 
417 	ahash_tfm = crypto_alloc_ahash(alg_name, 0, 0);
418 	if (IS_ERR(ahash_tfm))
419 		return PTR_ERR(ahash_tfm);
420 
421 	req = ahash_request_alloc(ahash_tfm, GFP_KERNEL);
422 	if (!req) {
423 		ret = -ENOMEM;
424 		goto err_free_ahash;
425 	}
426 
427 	crypto_init_wait(&wait);
428 	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
429 				   crypto_req_done, &wait);
430 	crypto_ahash_clear_flags(ahash_tfm, ~0);
431 
432 	buf = kzalloc(keylen + STARFIVE_HASH_BUFLEN, GFP_KERNEL);
433 	if (!buf) {
434 		ret = -ENOMEM;
435 		goto err_free_req;
436 	}
437 
438 	memcpy(buf, key, keylen);
439 	sg_init_one(&sg, buf, keylen);
440 	ahash_request_set_crypt(req, &sg, ctx->key, keylen);
441 
442 	ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
443 
444 	kfree(buf);
445 err_free_req:
446 	ahash_request_free(req);
447 err_free_ahash:
448 	crypto_free_ahash(ahash_tfm);
449 	return ret;
450 }
451 
452 static int starfive_hash_setkey(struct crypto_ahash *hash,
453 				const u8 *key, unsigned int keylen)
454 {
455 	struct starfive_cryp_ctx *ctx = crypto_ahash_ctx(hash);
456 	unsigned int digestsize = crypto_ahash_digestsize(hash);
457 	unsigned int blocksize = crypto_ahash_blocksize(hash);
458 	const char *alg_name;
459 
460 	crypto_ahash_setkey(ctx->ahash_fbk, key, keylen);
461 
462 	if (keylen <= blocksize) {
463 		memcpy(ctx->key, key, keylen);
464 		ctx->keylen = keylen;
465 		return 0;
466 	}
467 
468 	ctx->keylen = digestsize;
469 
470 	switch (digestsize) {
471 	case SHA224_DIGEST_SIZE:
472 		alg_name = "sha224-starfive";
473 		break;
474 	case SHA256_DIGEST_SIZE:
475 		if (ctx->hash_mode == STARFIVE_HASH_SM3)
476 			alg_name = "sm3-starfive";
477 		else
478 			alg_name = "sha256-starfive";
479 		break;
480 	case SHA384_DIGEST_SIZE:
481 		alg_name = "sha384-starfive";
482 		break;
483 	case SHA512_DIGEST_SIZE:
484 		alg_name = "sha512-starfive";
485 		break;
486 	default:
487 		return -EINVAL;
488 	}
489 
490 	return starfive_hash_long_setkey(ctx, key, keylen, alg_name);
491 }
492 
493 static int starfive_sha224_init_tfm(struct crypto_ahash *hash)
494 {
495 	return starfive_hash_init_tfm(hash, "sha224-lib",
496 				      STARFIVE_HASH_SHA224, 0);
497 }
498 
499 static int starfive_sha256_init_tfm(struct crypto_ahash *hash)
500 {
501 	return starfive_hash_init_tfm(hash, "sha256-lib",
502 				      STARFIVE_HASH_SHA256, 0);
503 }
504 
505 static int starfive_sha384_init_tfm(struct crypto_ahash *hash)
506 {
507 	return starfive_hash_init_tfm(hash, "sha384-lib",
508 				      STARFIVE_HASH_SHA384, 0);
509 }
510 
511 static int starfive_sha512_init_tfm(struct crypto_ahash *hash)
512 {
513 	return starfive_hash_init_tfm(hash, "sha512-lib",
514 				      STARFIVE_HASH_SHA512, 0);
515 }
516 
517 static int starfive_sm3_init_tfm(struct crypto_ahash *hash)
518 {
519 	return starfive_hash_init_tfm(hash, "sm3-generic",
520 				      STARFIVE_HASH_SM3, 0);
521 }
522 
523 static int starfive_hmac_sha224_init_tfm(struct crypto_ahash *hash)
524 {
525 	return starfive_hash_init_tfm(hash, "hmac-sha224-lib",
526 				      STARFIVE_HASH_SHA224, 1);
527 }
528 
529 static int starfive_hmac_sha256_init_tfm(struct crypto_ahash *hash)
530 {
531 	return starfive_hash_init_tfm(hash, "hmac-sha256-lib",
532 				      STARFIVE_HASH_SHA256, 1);
533 }
534 
535 static int starfive_hmac_sha384_init_tfm(struct crypto_ahash *hash)
536 {
537 	return starfive_hash_init_tfm(hash, "hmac-sha384-lib",
538 				      STARFIVE_HASH_SHA384, 1);
539 }
540 
541 static int starfive_hmac_sha512_init_tfm(struct crypto_ahash *hash)
542 {
543 	return starfive_hash_init_tfm(hash, "hmac-sha512-lib",
544 				      STARFIVE_HASH_SHA512, 1);
545 }
546 
547 static int starfive_hmac_sm3_init_tfm(struct crypto_ahash *hash)
548 {
549 	return starfive_hash_init_tfm(hash, "hmac(sm3-generic)",
550 				      STARFIVE_HASH_SM3, 1);
551 }
552 
553 static struct ahash_engine_alg algs_sha2_sm3[] = {
554 {
555 	.base.init     = starfive_hash_init,
556 	.base.update   = starfive_hash_update,
557 	.base.final    = starfive_hash_final,
558 	.base.finup    = starfive_hash_finup,
559 	.base.digest   = starfive_hash_digest,
560 	.base.export   = starfive_hash_export,
561 	.base.import   = starfive_hash_import,
562 	.base.init_tfm = starfive_sha224_init_tfm,
563 	.base.exit_tfm = starfive_hash_exit_tfm,
564 	.base.halg = {
565 		.digestsize = SHA224_DIGEST_SIZE,
566 		.statesize  = sizeof(struct sha256_state),
567 		.base = {
568 			.cra_name		= "sha224",
569 			.cra_driver_name	= "sha224-starfive",
570 			.cra_priority		= 200,
571 			.cra_flags		= CRYPTO_ALG_ASYNC |
572 						  CRYPTO_ALG_TYPE_AHASH |
573 						  CRYPTO_ALG_NEED_FALLBACK,
574 			.cra_blocksize		= SHA224_BLOCK_SIZE,
575 			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
576 			.cra_module		= THIS_MODULE,
577 		}
578 	},
579 	.op = {
580 		.do_one_request = starfive_hash_one_request,
581 	},
582 }, {
583 	.base.init     = starfive_hash_init,
584 	.base.update   = starfive_hash_update,
585 	.base.final    = starfive_hash_final,
586 	.base.finup    = starfive_hash_finup,
587 	.base.digest   = starfive_hash_digest,
588 	.base.export   = starfive_hash_export,
589 	.base.import   = starfive_hash_import,
590 	.base.init_tfm = starfive_hmac_sha224_init_tfm,
591 	.base.exit_tfm = starfive_hash_exit_tfm,
592 	.base.setkey   = starfive_hash_setkey,
593 	.base.halg = {
594 		.digestsize = SHA224_DIGEST_SIZE,
595 		.statesize  = sizeof(struct sha256_state),
596 		.base = {
597 			.cra_name		= "hmac(sha224)",
598 			.cra_driver_name	= "sha224-hmac-starfive",
599 			.cra_priority		= 200,
600 			.cra_flags		= CRYPTO_ALG_ASYNC |
601 						  CRYPTO_ALG_TYPE_AHASH |
602 						  CRYPTO_ALG_NEED_FALLBACK,
603 			.cra_blocksize		= SHA224_BLOCK_SIZE,
604 			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
605 			.cra_module		= THIS_MODULE,
606 		}
607 	},
608 	.op = {
609 		.do_one_request = starfive_hash_one_request,
610 	},
611 }, {
612 	.base.init     = starfive_hash_init,
613 	.base.update   = starfive_hash_update,
614 	.base.final    = starfive_hash_final,
615 	.base.finup    = starfive_hash_finup,
616 	.base.digest   = starfive_hash_digest,
617 	.base.export   = starfive_hash_export,
618 	.base.import   = starfive_hash_import,
619 	.base.init_tfm = starfive_sha256_init_tfm,
620 	.base.exit_tfm = starfive_hash_exit_tfm,
621 	.base.halg = {
622 		.digestsize = SHA256_DIGEST_SIZE,
623 		.statesize  = sizeof(struct sha256_state),
624 		.base = {
625 			.cra_name		= "sha256",
626 			.cra_driver_name	= "sha256-starfive",
627 			.cra_priority		= 200,
628 			.cra_flags		= CRYPTO_ALG_ASYNC |
629 						  CRYPTO_ALG_TYPE_AHASH |
630 						  CRYPTO_ALG_NEED_FALLBACK,
631 			.cra_blocksize		= SHA256_BLOCK_SIZE,
632 			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
633 			.cra_module		= THIS_MODULE,
634 		}
635 	},
636 	.op = {
637 		.do_one_request = starfive_hash_one_request,
638 	},
639 }, {
640 	.base.init     = starfive_hash_init,
641 	.base.update   = starfive_hash_update,
642 	.base.final    = starfive_hash_final,
643 	.base.finup    = starfive_hash_finup,
644 	.base.digest   = starfive_hash_digest,
645 	.base.export   = starfive_hash_export,
646 	.base.import   = starfive_hash_import,
647 	.base.init_tfm = starfive_hmac_sha256_init_tfm,
648 	.base.exit_tfm = starfive_hash_exit_tfm,
649 	.base.setkey   = starfive_hash_setkey,
650 	.base.halg = {
651 		.digestsize = SHA256_DIGEST_SIZE,
652 		.statesize  = sizeof(struct sha256_state),
653 		.base = {
654 			.cra_name		= "hmac(sha256)",
655 			.cra_driver_name	= "sha256-hmac-starfive",
656 			.cra_priority		= 200,
657 			.cra_flags		= CRYPTO_ALG_ASYNC |
658 						  CRYPTO_ALG_TYPE_AHASH |
659 						  CRYPTO_ALG_NEED_FALLBACK,
660 			.cra_blocksize		= SHA256_BLOCK_SIZE,
661 			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
662 			.cra_module		= THIS_MODULE,
663 		}
664 	},
665 	.op = {
666 		.do_one_request = starfive_hash_one_request,
667 	},
668 }, {
669 	.base.init     = starfive_hash_init,
670 	.base.update   = starfive_hash_update,
671 	.base.final    = starfive_hash_final,
672 	.base.finup    = starfive_hash_finup,
673 	.base.digest   = starfive_hash_digest,
674 	.base.export   = starfive_hash_export,
675 	.base.import   = starfive_hash_import,
676 	.base.init_tfm = starfive_sha384_init_tfm,
677 	.base.exit_tfm = starfive_hash_exit_tfm,
678 	.base.halg = {
679 		.digestsize = SHA384_DIGEST_SIZE,
680 		.statesize  = sizeof(struct sha512_state),
681 		.base = {
682 			.cra_name		= "sha384",
683 			.cra_driver_name	= "sha384-starfive",
684 			.cra_priority		= 200,
685 			.cra_flags		= CRYPTO_ALG_ASYNC |
686 						  CRYPTO_ALG_TYPE_AHASH |
687 						  CRYPTO_ALG_NEED_FALLBACK,
688 			.cra_blocksize		= SHA384_BLOCK_SIZE,
689 			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
690 			.cra_module		= THIS_MODULE,
691 		}
692 	},
693 	.op = {
694 		.do_one_request = starfive_hash_one_request,
695 	},
696 }, {
697 	.base.init     = starfive_hash_init,
698 	.base.update   = starfive_hash_update,
699 	.base.final    = starfive_hash_final,
700 	.base.finup    = starfive_hash_finup,
701 	.base.digest   = starfive_hash_digest,
702 	.base.export   = starfive_hash_export,
703 	.base.import   = starfive_hash_import,
704 	.base.init_tfm = starfive_hmac_sha384_init_tfm,
705 	.base.exit_tfm = starfive_hash_exit_tfm,
706 	.base.setkey   = starfive_hash_setkey,
707 	.base.halg = {
708 		.digestsize = SHA384_DIGEST_SIZE,
709 		.statesize  = sizeof(struct sha512_state),
710 		.base = {
711 			.cra_name		= "hmac(sha384)",
712 			.cra_driver_name	= "sha384-hmac-starfive",
713 			.cra_priority		= 200,
714 			.cra_flags		= CRYPTO_ALG_ASYNC |
715 						  CRYPTO_ALG_TYPE_AHASH |
716 						  CRYPTO_ALG_NEED_FALLBACK,
717 			.cra_blocksize		= SHA384_BLOCK_SIZE,
718 			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
719 			.cra_module		= THIS_MODULE,
720 		}
721 	},
722 	.op = {
723 		.do_one_request = starfive_hash_one_request,
724 	},
725 }, {
726 	.base.init     = starfive_hash_init,
727 	.base.update   = starfive_hash_update,
728 	.base.final    = starfive_hash_final,
729 	.base.finup    = starfive_hash_finup,
730 	.base.digest   = starfive_hash_digest,
731 	.base.export   = starfive_hash_export,
732 	.base.import   = starfive_hash_import,
733 	.base.init_tfm = starfive_sha512_init_tfm,
734 	.base.exit_tfm = starfive_hash_exit_tfm,
735 	.base.halg = {
736 		.digestsize = SHA512_DIGEST_SIZE,
737 		.statesize  = sizeof(struct sha512_state),
738 		.base = {
739 			.cra_name		= "sha512",
740 			.cra_driver_name	= "sha512-starfive",
741 			.cra_priority		= 200,
742 			.cra_flags		= CRYPTO_ALG_ASYNC |
743 						  CRYPTO_ALG_TYPE_AHASH |
744 						  CRYPTO_ALG_NEED_FALLBACK,
745 			.cra_blocksize		= SHA512_BLOCK_SIZE,
746 			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
747 			.cra_module		= THIS_MODULE,
748 		}
749 	},
750 	.op = {
751 		.do_one_request = starfive_hash_one_request,
752 	},
753 }, {
754 	.base.init     = starfive_hash_init,
755 	.base.update   = starfive_hash_update,
756 	.base.final    = starfive_hash_final,
757 	.base.finup    = starfive_hash_finup,
758 	.base.digest   = starfive_hash_digest,
759 	.base.export   = starfive_hash_export,
760 	.base.import   = starfive_hash_import,
761 	.base.init_tfm = starfive_hmac_sha512_init_tfm,
762 	.base.exit_tfm = starfive_hash_exit_tfm,
763 	.base.setkey   = starfive_hash_setkey,
764 	.base.halg = {
765 		.digestsize = SHA512_DIGEST_SIZE,
766 		.statesize  = sizeof(struct sha512_state),
767 		.base = {
768 			.cra_name		= "hmac(sha512)",
769 			.cra_driver_name	= "sha512-hmac-starfive",
770 			.cra_priority		= 200,
771 			.cra_flags		= CRYPTO_ALG_ASYNC |
772 						  CRYPTO_ALG_TYPE_AHASH |
773 						  CRYPTO_ALG_NEED_FALLBACK,
774 			.cra_blocksize		= SHA512_BLOCK_SIZE,
775 			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
776 			.cra_module		= THIS_MODULE,
777 		}
778 	},
779 	.op = {
780 		.do_one_request = starfive_hash_one_request,
781 	},
782 }, {
783 	.base.init     = starfive_hash_init,
784 	.base.update   = starfive_hash_update,
785 	.base.final    = starfive_hash_final,
786 	.base.finup    = starfive_hash_finup,
787 	.base.digest   = starfive_hash_digest,
788 	.base.export   = starfive_hash_export,
789 	.base.import   = starfive_hash_import,
790 	.base.init_tfm = starfive_sm3_init_tfm,
791 	.base.exit_tfm = starfive_hash_exit_tfm,
792 	.base.halg = {
793 		.digestsize = SM3_DIGEST_SIZE,
794 		.statesize  = sizeof(struct sm3_state),
795 		.base = {
796 			.cra_name		= "sm3",
797 			.cra_driver_name	= "sm3-starfive",
798 			.cra_priority		= 200,
799 			.cra_flags		= CRYPTO_ALG_ASYNC |
800 						  CRYPTO_ALG_TYPE_AHASH |
801 						  CRYPTO_ALG_NEED_FALLBACK,
802 			.cra_blocksize		= SM3_BLOCK_SIZE,
803 			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
804 			.cra_module		= THIS_MODULE,
805 		}
806 	},
807 	.op = {
808 		.do_one_request = starfive_hash_one_request,
809 	},
810 }, {
811 	.base.init	  = starfive_hash_init,
812 	.base.update	  = starfive_hash_update,
813 	.base.final	  = starfive_hash_final,
814 	.base.finup	  = starfive_hash_finup,
815 	.base.digest	  = starfive_hash_digest,
816 	.base.export	  = starfive_hash_export,
817 	.base.import	  = starfive_hash_import,
818 	.base.init_tfm = starfive_hmac_sm3_init_tfm,
819 	.base.exit_tfm = starfive_hash_exit_tfm,
820 	.base.setkey	  = starfive_hash_setkey,
821 	.base.halg = {
822 		.digestsize = SM3_DIGEST_SIZE,
823 		.statesize  = sizeof(struct sm3_state),
824 		.base = {
825 			.cra_name		= "hmac(sm3)",
826 			.cra_driver_name	= "sm3-hmac-starfive",
827 			.cra_priority		= 200,
828 			.cra_flags		= CRYPTO_ALG_ASYNC |
829 						  CRYPTO_ALG_TYPE_AHASH |
830 						  CRYPTO_ALG_NEED_FALLBACK,
831 			.cra_blocksize		= SM3_BLOCK_SIZE,
832 			.cra_ctxsize		= sizeof(struct starfive_cryp_ctx),
833 			.cra_module		= THIS_MODULE,
834 		}
835 	},
836 	.op = {
837 		.do_one_request = starfive_hash_one_request,
838 	},
839 },
840 };
841 
842 int starfive_hash_register_algs(void)
843 {
844 	return crypto_engine_register_ahashes(algs_sha2_sm3, ARRAY_SIZE(algs_sha2_sm3));
845 }
846 
847 void starfive_hash_unregister_algs(void)
848 {
849 	crypto_engine_unregister_ahashes(algs_sha2_sm3, ARRAY_SIZE(algs_sha2_sm3));
850 }
851