zynqmp-sha.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Xilinx ZynqMP SHA Driver.
  4. * Copyright (c) 2022 Xilinx Inc.
  5. */
  6. #include <linux/cacheflush.h>
  7. #include <crypto/hash.h>
  8. #include <crypto/internal/hash.h>
  9. #include <crypto/sha3.h>
  10. #include <linux/crypto.h>
  11. #include <linux/device.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/firmware/xlnx-zynqmp.h>
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of_device.h>
  19. #include <linux/platform_device.h>
  20. #define ZYNQMP_DMA_BIT_MASK 32U
  21. #define ZYNQMP_DMA_ALLOC_FIXED_SIZE 0x1000U
  22. enum zynqmp_sha_op {
  23. ZYNQMP_SHA3_INIT = 1,
  24. ZYNQMP_SHA3_UPDATE = 2,
  25. ZYNQMP_SHA3_FINAL = 4,
  26. };
  27. struct zynqmp_sha_drv_ctx {
  28. struct shash_alg sha3_384;
  29. struct device *dev;
  30. };
  31. struct zynqmp_sha_tfm_ctx {
  32. struct device *dev;
  33. struct crypto_shash *fbk_tfm;
  34. };
  35. struct zynqmp_sha_desc_ctx {
  36. struct shash_desc fbk_req;
  37. };
  38. static dma_addr_t update_dma_addr, final_dma_addr;
  39. static char *ubuf, *fbuf;
  40. static int zynqmp_sha_init_tfm(struct crypto_shash *hash)
  41. {
  42. const char *fallback_driver_name = crypto_shash_alg_name(hash);
  43. struct zynqmp_sha_tfm_ctx *tfm_ctx = crypto_shash_ctx(hash);
  44. struct shash_alg *alg = crypto_shash_alg(hash);
  45. struct crypto_shash *fallback_tfm;
  46. struct zynqmp_sha_drv_ctx *drv_ctx;
  47. drv_ctx = container_of(alg, struct zynqmp_sha_drv_ctx, sha3_384);
  48. tfm_ctx->dev = drv_ctx->dev;
  49. /* Allocate a fallback and abort if it failed. */
  50. fallback_tfm = crypto_alloc_shash(fallback_driver_name, 0,
  51. CRYPTO_ALG_NEED_FALLBACK);
  52. if (IS_ERR(fallback_tfm))
  53. return PTR_ERR(fallback_tfm);
  54. tfm_ctx->fbk_tfm = fallback_tfm;
  55. hash->descsize += crypto_shash_descsize(tfm_ctx->fbk_tfm);
  56. return 0;
  57. }
  58. static void zynqmp_sha_exit_tfm(struct crypto_shash *hash)
  59. {
  60. struct zynqmp_sha_tfm_ctx *tfm_ctx = crypto_shash_ctx(hash);
  61. if (tfm_ctx->fbk_tfm) {
  62. crypto_free_shash(tfm_ctx->fbk_tfm);
  63. tfm_ctx->fbk_tfm = NULL;
  64. }
  65. memzero_explicit(tfm_ctx, sizeof(struct zynqmp_sha_tfm_ctx));
  66. }
  67. static int zynqmp_sha_init(struct shash_desc *desc)
  68. {
  69. struct zynqmp_sha_desc_ctx *dctx = shash_desc_ctx(desc);
  70. struct zynqmp_sha_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
  71. dctx->fbk_req.tfm = tctx->fbk_tfm;
  72. return crypto_shash_init(&dctx->fbk_req);
  73. }
  74. static int zynqmp_sha_update(struct shash_desc *desc, const u8 *data, unsigned int length)
  75. {
  76. struct zynqmp_sha_desc_ctx *dctx = shash_desc_ctx(desc);
  77. return crypto_shash_update(&dctx->fbk_req, data, length);
  78. }
  79. static int zynqmp_sha_final(struct shash_desc *desc, u8 *out)
  80. {
  81. struct zynqmp_sha_desc_ctx *dctx = shash_desc_ctx(desc);
  82. return crypto_shash_final(&dctx->fbk_req, out);
  83. }
  84. static int zynqmp_sha_finup(struct shash_desc *desc, const u8 *data, unsigned int length, u8 *out)
  85. {
  86. struct zynqmp_sha_desc_ctx *dctx = shash_desc_ctx(desc);
  87. return crypto_shash_finup(&dctx->fbk_req, data, length, out);
  88. }
  89. static int zynqmp_sha_import(struct shash_desc *desc, const void *in)
  90. {
  91. struct zynqmp_sha_desc_ctx *dctx = shash_desc_ctx(desc);
  92. struct zynqmp_sha_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
  93. dctx->fbk_req.tfm = tctx->fbk_tfm;
  94. return crypto_shash_import(&dctx->fbk_req, in);
  95. }
  96. static int zynqmp_sha_export(struct shash_desc *desc, void *out)
  97. {
  98. struct zynqmp_sha_desc_ctx *dctx = shash_desc_ctx(desc);
  99. return crypto_shash_export(&dctx->fbk_req, out);
  100. }
  101. static int zynqmp_sha_digest(struct shash_desc *desc, const u8 *data, unsigned int len, u8 *out)
  102. {
  103. unsigned int remaining_len = len;
  104. int update_size;
  105. int ret;
  106. ret = zynqmp_pm_sha_hash(0, 0, ZYNQMP_SHA3_INIT);
  107. if (ret)
  108. return ret;
  109. while (remaining_len != 0) {
  110. memzero_explicit(ubuf, ZYNQMP_DMA_ALLOC_FIXED_SIZE);
  111. if (remaining_len >= ZYNQMP_DMA_ALLOC_FIXED_SIZE) {
  112. update_size = ZYNQMP_DMA_ALLOC_FIXED_SIZE;
  113. remaining_len -= ZYNQMP_DMA_ALLOC_FIXED_SIZE;
  114. } else {
  115. update_size = remaining_len;
  116. remaining_len = 0;
  117. }
  118. memcpy(ubuf, data, update_size);
  119. flush_icache_range((unsigned long)ubuf, (unsigned long)ubuf + update_size);
  120. ret = zynqmp_pm_sha_hash(update_dma_addr, update_size, ZYNQMP_SHA3_UPDATE);
  121. if (ret)
  122. return ret;
  123. data += update_size;
  124. }
  125. ret = zynqmp_pm_sha_hash(final_dma_addr, SHA3_384_DIGEST_SIZE, ZYNQMP_SHA3_FINAL);
  126. memcpy(out, fbuf, SHA3_384_DIGEST_SIZE);
  127. memzero_explicit(fbuf, SHA3_384_DIGEST_SIZE);
  128. return ret;
  129. }
  130. static struct zynqmp_sha_drv_ctx sha3_drv_ctx = {
  131. .sha3_384 = {
  132. .init = zynqmp_sha_init,
  133. .update = zynqmp_sha_update,
  134. .final = zynqmp_sha_final,
  135. .finup = zynqmp_sha_finup,
  136. .digest = zynqmp_sha_digest,
  137. .export = zynqmp_sha_export,
  138. .import = zynqmp_sha_import,
  139. .init_tfm = zynqmp_sha_init_tfm,
  140. .exit_tfm = zynqmp_sha_exit_tfm,
  141. .descsize = sizeof(struct zynqmp_sha_desc_ctx),
  142. .statesize = sizeof(struct sha3_state),
  143. .digestsize = SHA3_384_DIGEST_SIZE,
  144. .base = {
  145. .cra_name = "sha3-384",
  146. .cra_driver_name = "zynqmp-sha3-384",
  147. .cra_priority = 300,
  148. .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
  149. CRYPTO_ALG_ALLOCATES_MEMORY |
  150. CRYPTO_ALG_NEED_FALLBACK,
  151. .cra_blocksize = SHA3_384_BLOCK_SIZE,
  152. .cra_ctxsize = sizeof(struct zynqmp_sha_tfm_ctx),
  153. .cra_alignmask = 3,
  154. .cra_module = THIS_MODULE,
  155. }
  156. }
  157. };
  158. static int zynqmp_sha_probe(struct platform_device *pdev)
  159. {
  160. struct device *dev = &pdev->dev;
  161. int err;
  162. u32 v;
  163. /* Verify the hardware is present */
  164. err = zynqmp_pm_get_api_version(&v);
  165. if (err)
  166. return err;
  167. err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(ZYNQMP_DMA_BIT_MASK));
  168. if (err < 0) {
  169. dev_err(dev, "No usable DMA configuration\n");
  170. return err;
  171. }
  172. err = crypto_register_shash(&sha3_drv_ctx.sha3_384);
  173. if (err < 0) {
  174. dev_err(dev, "Failed to register shash alg.\n");
  175. return err;
  176. }
  177. sha3_drv_ctx.dev = dev;
  178. platform_set_drvdata(pdev, &sha3_drv_ctx);
  179. ubuf = dma_alloc_coherent(dev, ZYNQMP_DMA_ALLOC_FIXED_SIZE, &update_dma_addr, GFP_KERNEL);
  180. if (!ubuf) {
  181. err = -ENOMEM;
  182. goto err_shash;
  183. }
  184. fbuf = dma_alloc_coherent(dev, SHA3_384_DIGEST_SIZE, &final_dma_addr, GFP_KERNEL);
  185. if (!fbuf) {
  186. err = -ENOMEM;
  187. goto err_mem;
  188. }
  189. return 0;
  190. err_mem:
  191. dma_free_coherent(sha3_drv_ctx.dev, ZYNQMP_DMA_ALLOC_FIXED_SIZE, ubuf, update_dma_addr);
  192. err_shash:
  193. crypto_unregister_shash(&sha3_drv_ctx.sha3_384);
  194. return err;
  195. }
  196. static int zynqmp_sha_remove(struct platform_device *pdev)
  197. {
  198. sha3_drv_ctx.dev = platform_get_drvdata(pdev);
  199. dma_free_coherent(sha3_drv_ctx.dev, ZYNQMP_DMA_ALLOC_FIXED_SIZE, ubuf, update_dma_addr);
  200. dma_free_coherent(sha3_drv_ctx.dev, SHA3_384_DIGEST_SIZE, fbuf, final_dma_addr);
  201. crypto_unregister_shash(&sha3_drv_ctx.sha3_384);
  202. return 0;
  203. }
  204. static struct platform_driver zynqmp_sha_driver = {
  205. .probe = zynqmp_sha_probe,
  206. .remove = zynqmp_sha_remove,
  207. .driver = {
  208. .name = "zynqmp-sha3-384",
  209. },
  210. };
  211. module_platform_driver(zynqmp_sha_driver);
  212. MODULE_DESCRIPTION("ZynqMP SHA3 hardware acceleration support.");
  213. MODULE_LICENSE("GPL v2");
  214. MODULE_AUTHOR("Harsha <[email protected]>");