jh7110-hash.c 24 KB

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