sun8i-ce-hash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * sun8i-ce-hash.c - hardware cryptographic offloader for
  4. * Allwinner H3/A64/H5/H2+/H6/R40 SoC
  5. *
  6. * Copyright (C) 2015-2020 Corentin Labbe <[email protected]>
  7. *
  8. * This file add support for MD5 and SHA1/SHA224/SHA256/SHA384/SHA512.
  9. *
  10. * You could find the datasheet in Documentation/arm/sunxi.rst
  11. */
  12. #include <linux/bottom_half.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/scatterlist.h>
  16. #include <crypto/internal/hash.h>
  17. #include <crypto/sha1.h>
  18. #include <crypto/sha2.h>
  19. #include <crypto/md5.h>
  20. #include "sun8i-ce.h"
  21. int sun8i_ce_hash_crainit(struct crypto_tfm *tfm)
  22. {
  23. struct sun8i_ce_hash_tfm_ctx *op = crypto_tfm_ctx(tfm);
  24. struct ahash_alg *alg = __crypto_ahash_alg(tfm->__crt_alg);
  25. struct sun8i_ce_alg_template *algt;
  26. int err;
  27. memset(op, 0, sizeof(struct sun8i_ce_hash_tfm_ctx));
  28. algt = container_of(alg, struct sun8i_ce_alg_template, alg.hash);
  29. op->ce = algt->ce;
  30. op->enginectx.op.do_one_request = sun8i_ce_hash_run;
  31. op->enginectx.op.prepare_request = NULL;
  32. op->enginectx.op.unprepare_request = NULL;
  33. /* FALLBACK */
  34. op->fallback_tfm = crypto_alloc_ahash(crypto_tfm_alg_name(tfm), 0,
  35. CRYPTO_ALG_NEED_FALLBACK);
  36. if (IS_ERR(op->fallback_tfm)) {
  37. dev_err(algt->ce->dev, "Fallback driver could no be loaded\n");
  38. return PTR_ERR(op->fallback_tfm);
  39. }
  40. if (algt->alg.hash.halg.statesize < crypto_ahash_statesize(op->fallback_tfm))
  41. algt->alg.hash.halg.statesize = crypto_ahash_statesize(op->fallback_tfm);
  42. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  43. sizeof(struct sun8i_ce_hash_reqctx) +
  44. crypto_ahash_reqsize(op->fallback_tfm));
  45. memcpy(algt->fbname, crypto_tfm_alg_driver_name(&op->fallback_tfm->base),
  46. CRYPTO_MAX_ALG_NAME);
  47. err = pm_runtime_get_sync(op->ce->dev);
  48. if (err < 0)
  49. goto error_pm;
  50. return 0;
  51. error_pm:
  52. pm_runtime_put_noidle(op->ce->dev);
  53. crypto_free_ahash(op->fallback_tfm);
  54. return err;
  55. }
  56. void sun8i_ce_hash_craexit(struct crypto_tfm *tfm)
  57. {
  58. struct sun8i_ce_hash_tfm_ctx *tfmctx = crypto_tfm_ctx(tfm);
  59. crypto_free_ahash(tfmctx->fallback_tfm);
  60. pm_runtime_put_sync_suspend(tfmctx->ce->dev);
  61. }
  62. int sun8i_ce_hash_init(struct ahash_request *areq)
  63. {
  64. struct sun8i_ce_hash_reqctx *rctx = ahash_request_ctx(areq);
  65. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  66. struct sun8i_ce_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
  67. memset(rctx, 0, sizeof(struct sun8i_ce_hash_reqctx));
  68. ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
  69. rctx->fallback_req.base.flags = areq->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  70. return crypto_ahash_init(&rctx->fallback_req);
  71. }
  72. int sun8i_ce_hash_export(struct ahash_request *areq, void *out)
  73. {
  74. struct sun8i_ce_hash_reqctx *rctx = ahash_request_ctx(areq);
  75. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  76. struct sun8i_ce_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
  77. ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
  78. rctx->fallback_req.base.flags = areq->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  79. return crypto_ahash_export(&rctx->fallback_req, out);
  80. }
  81. int sun8i_ce_hash_import(struct ahash_request *areq, const void *in)
  82. {
  83. struct sun8i_ce_hash_reqctx *rctx = ahash_request_ctx(areq);
  84. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  85. struct sun8i_ce_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
  86. ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
  87. rctx->fallback_req.base.flags = areq->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  88. return crypto_ahash_import(&rctx->fallback_req, in);
  89. }
  90. int sun8i_ce_hash_final(struct ahash_request *areq)
  91. {
  92. struct sun8i_ce_hash_reqctx *rctx = ahash_request_ctx(areq);
  93. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  94. struct sun8i_ce_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
  95. #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
  96. struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
  97. struct sun8i_ce_alg_template *algt;
  98. #endif
  99. ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
  100. rctx->fallback_req.base.flags = areq->base.flags &
  101. CRYPTO_TFM_REQ_MAY_SLEEP;
  102. rctx->fallback_req.result = areq->result;
  103. #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
  104. algt = container_of(alg, struct sun8i_ce_alg_template, alg.hash);
  105. algt->stat_fb++;
  106. #endif
  107. return crypto_ahash_final(&rctx->fallback_req);
  108. }
  109. int sun8i_ce_hash_update(struct ahash_request *areq)
  110. {
  111. struct sun8i_ce_hash_reqctx *rctx = ahash_request_ctx(areq);
  112. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  113. struct sun8i_ce_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
  114. ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
  115. rctx->fallback_req.base.flags = areq->base.flags &
  116. CRYPTO_TFM_REQ_MAY_SLEEP;
  117. rctx->fallback_req.nbytes = areq->nbytes;
  118. rctx->fallback_req.src = areq->src;
  119. return crypto_ahash_update(&rctx->fallback_req);
  120. }
  121. int sun8i_ce_hash_finup(struct ahash_request *areq)
  122. {
  123. struct sun8i_ce_hash_reqctx *rctx = ahash_request_ctx(areq);
  124. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  125. struct sun8i_ce_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
  126. #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
  127. struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
  128. struct sun8i_ce_alg_template *algt;
  129. #endif
  130. ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
  131. rctx->fallback_req.base.flags = areq->base.flags &
  132. CRYPTO_TFM_REQ_MAY_SLEEP;
  133. rctx->fallback_req.nbytes = areq->nbytes;
  134. rctx->fallback_req.src = areq->src;
  135. rctx->fallback_req.result = areq->result;
  136. #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
  137. algt = container_of(alg, struct sun8i_ce_alg_template, alg.hash);
  138. algt->stat_fb++;
  139. #endif
  140. return crypto_ahash_finup(&rctx->fallback_req);
  141. }
  142. static int sun8i_ce_hash_digest_fb(struct ahash_request *areq)
  143. {
  144. struct sun8i_ce_hash_reqctx *rctx = ahash_request_ctx(areq);
  145. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  146. struct sun8i_ce_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
  147. #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
  148. struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
  149. struct sun8i_ce_alg_template *algt;
  150. #endif
  151. ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
  152. rctx->fallback_req.base.flags = areq->base.flags &
  153. CRYPTO_TFM_REQ_MAY_SLEEP;
  154. rctx->fallback_req.nbytes = areq->nbytes;
  155. rctx->fallback_req.src = areq->src;
  156. rctx->fallback_req.result = areq->result;
  157. #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
  158. algt = container_of(alg, struct sun8i_ce_alg_template, alg.hash);
  159. algt->stat_fb++;
  160. #endif
  161. return crypto_ahash_digest(&rctx->fallback_req);
  162. }
  163. static bool sun8i_ce_hash_need_fallback(struct ahash_request *areq)
  164. {
  165. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  166. struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
  167. struct sun8i_ce_alg_template *algt;
  168. struct scatterlist *sg;
  169. algt = container_of(alg, struct sun8i_ce_alg_template, alg.hash);
  170. if (areq->nbytes == 0) {
  171. algt->stat_fb_len0++;
  172. return true;
  173. }
  174. /* we need to reserve one SG for padding one */
  175. if (sg_nents_for_len(areq->src, areq->nbytes) > MAX_SG - 1) {
  176. algt->stat_fb_maxsg++;
  177. return true;
  178. }
  179. sg = areq->src;
  180. while (sg) {
  181. if (sg->length % 4) {
  182. algt->stat_fb_srclen++;
  183. return true;
  184. }
  185. if (!IS_ALIGNED(sg->offset, sizeof(u32))) {
  186. algt->stat_fb_srcali++;
  187. return true;
  188. }
  189. sg = sg_next(sg);
  190. }
  191. return false;
  192. }
  193. int sun8i_ce_hash_digest(struct ahash_request *areq)
  194. {
  195. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  196. struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
  197. struct sun8i_ce_hash_reqctx *rctx = ahash_request_ctx(areq);
  198. struct sun8i_ce_alg_template *algt;
  199. struct sun8i_ce_dev *ce;
  200. struct crypto_engine *engine;
  201. struct scatterlist *sg;
  202. int nr_sgs, e, i;
  203. if (sun8i_ce_hash_need_fallback(areq))
  204. return sun8i_ce_hash_digest_fb(areq);
  205. nr_sgs = sg_nents_for_len(areq->src, areq->nbytes);
  206. if (nr_sgs > MAX_SG - 1)
  207. return sun8i_ce_hash_digest_fb(areq);
  208. for_each_sg(areq->src, sg, nr_sgs, i) {
  209. if (sg->length % 4 || !IS_ALIGNED(sg->offset, sizeof(u32)))
  210. return sun8i_ce_hash_digest_fb(areq);
  211. }
  212. algt = container_of(alg, struct sun8i_ce_alg_template, alg.hash);
  213. ce = algt->ce;
  214. e = sun8i_ce_get_engine_number(ce);
  215. rctx->flow = e;
  216. engine = ce->chanlist[e].engine;
  217. return crypto_transfer_hash_request_to_engine(engine, areq);
  218. }
  219. static u64 hash_pad(__le32 *buf, unsigned int bufsize, u64 padi, u64 byte_count, bool le, int bs)
  220. {
  221. u64 fill, min_fill, j, k;
  222. __be64 *bebits;
  223. __le64 *lebits;
  224. j = padi;
  225. buf[j++] = cpu_to_le32(0x80);
  226. if (bs == 64) {
  227. fill = 64 - (byte_count % 64);
  228. min_fill = 2 * sizeof(u32) + sizeof(u32);
  229. } else {
  230. fill = 128 - (byte_count % 128);
  231. min_fill = 4 * sizeof(u32) + sizeof(u32);
  232. }
  233. if (fill < min_fill)
  234. fill += bs;
  235. k = j;
  236. j += (fill - min_fill) / sizeof(u32);
  237. if (j * 4 > bufsize) {
  238. pr_err("%s OVERFLOW %llu\n", __func__, j);
  239. return 0;
  240. }
  241. for (; k < j; k++)
  242. buf[k] = 0;
  243. if (le) {
  244. /* MD5 */
  245. lebits = (__le64 *)&buf[j];
  246. *lebits = cpu_to_le64(byte_count << 3);
  247. j += 2;
  248. } else {
  249. if (bs == 64) {
  250. /* sha1 sha224 sha256 */
  251. bebits = (__be64 *)&buf[j];
  252. *bebits = cpu_to_be64(byte_count << 3);
  253. j += 2;
  254. } else {
  255. /* sha384 sha512*/
  256. bebits = (__be64 *)&buf[j];
  257. *bebits = cpu_to_be64(byte_count >> 61);
  258. j += 2;
  259. bebits = (__be64 *)&buf[j];
  260. *bebits = cpu_to_be64(byte_count << 3);
  261. j += 2;
  262. }
  263. }
  264. if (j * 4 > bufsize) {
  265. pr_err("%s OVERFLOW %llu\n", __func__, j);
  266. return 0;
  267. }
  268. return j;
  269. }
  270. int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq)
  271. {
  272. struct ahash_request *areq = container_of(breq, struct ahash_request, base);
  273. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  274. struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
  275. struct sun8i_ce_hash_reqctx *rctx = ahash_request_ctx(areq);
  276. struct sun8i_ce_alg_template *algt;
  277. struct sun8i_ce_dev *ce;
  278. struct sun8i_ce_flow *chan;
  279. struct ce_task *cet;
  280. struct scatterlist *sg;
  281. int nr_sgs, flow, err;
  282. unsigned int len;
  283. u32 common;
  284. u64 byte_count;
  285. __le32 *bf;
  286. void *buf = NULL;
  287. int j, i, todo;
  288. void *result = NULL;
  289. u64 bs;
  290. int digestsize;
  291. dma_addr_t addr_res, addr_pad;
  292. int ns = sg_nents_for_len(areq->src, areq->nbytes);
  293. algt = container_of(alg, struct sun8i_ce_alg_template, alg.hash);
  294. ce = algt->ce;
  295. bs = algt->alg.hash.halg.base.cra_blocksize;
  296. digestsize = algt->alg.hash.halg.digestsize;
  297. if (digestsize == SHA224_DIGEST_SIZE)
  298. digestsize = SHA256_DIGEST_SIZE;
  299. if (digestsize == SHA384_DIGEST_SIZE)
  300. digestsize = SHA512_DIGEST_SIZE;
  301. /* the padding could be up to two block. */
  302. buf = kzalloc(bs * 2, GFP_KERNEL | GFP_DMA);
  303. if (!buf) {
  304. err = -ENOMEM;
  305. goto theend;
  306. }
  307. bf = (__le32 *)buf;
  308. result = kzalloc(digestsize, GFP_KERNEL | GFP_DMA);
  309. if (!result) {
  310. err = -ENOMEM;
  311. goto theend;
  312. }
  313. flow = rctx->flow;
  314. chan = &ce->chanlist[flow];
  315. #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
  316. algt->stat_req++;
  317. #endif
  318. dev_dbg(ce->dev, "%s %s len=%d\n", __func__, crypto_tfm_alg_name(areq->base.tfm), areq->nbytes);
  319. cet = chan->tl;
  320. memset(cet, 0, sizeof(struct ce_task));
  321. cet->t_id = cpu_to_le32(flow);
  322. common = ce->variant->alg_hash[algt->ce_algo_id];
  323. common |= CE_COMM_INT;
  324. cet->t_common_ctl = cpu_to_le32(common);
  325. cet->t_sym_ctl = 0;
  326. cet->t_asym_ctl = 0;
  327. nr_sgs = dma_map_sg(ce->dev, areq->src, ns, DMA_TO_DEVICE);
  328. if (nr_sgs <= 0 || nr_sgs > MAX_SG) {
  329. dev_err(ce->dev, "Invalid sg number %d\n", nr_sgs);
  330. err = -EINVAL;
  331. goto theend;
  332. }
  333. len = areq->nbytes;
  334. for_each_sg(areq->src, sg, nr_sgs, i) {
  335. cet->t_src[i].addr = cpu_to_le32(sg_dma_address(sg));
  336. todo = min(len, sg_dma_len(sg));
  337. cet->t_src[i].len = cpu_to_le32(todo / 4);
  338. len -= todo;
  339. }
  340. if (len > 0) {
  341. dev_err(ce->dev, "remaining len %d\n", len);
  342. err = -EINVAL;
  343. goto theend;
  344. }
  345. addr_res = dma_map_single(ce->dev, result, digestsize, DMA_FROM_DEVICE);
  346. cet->t_dst[0].addr = cpu_to_le32(addr_res);
  347. cet->t_dst[0].len = cpu_to_le32(digestsize / 4);
  348. if (dma_mapping_error(ce->dev, addr_res)) {
  349. dev_err(ce->dev, "DMA map dest\n");
  350. err = -EINVAL;
  351. goto theend;
  352. }
  353. byte_count = areq->nbytes;
  354. j = 0;
  355. switch (algt->ce_algo_id) {
  356. case CE_ID_HASH_MD5:
  357. j = hash_pad(bf, 2 * bs, j, byte_count, true, bs);
  358. break;
  359. case CE_ID_HASH_SHA1:
  360. case CE_ID_HASH_SHA224:
  361. case CE_ID_HASH_SHA256:
  362. j = hash_pad(bf, 2 * bs, j, byte_count, false, bs);
  363. break;
  364. case CE_ID_HASH_SHA384:
  365. case CE_ID_HASH_SHA512:
  366. j = hash_pad(bf, 2 * bs, j, byte_count, false, bs);
  367. break;
  368. }
  369. if (!j) {
  370. err = -EINVAL;
  371. goto theend;
  372. }
  373. addr_pad = dma_map_single(ce->dev, buf, j * 4, DMA_TO_DEVICE);
  374. cet->t_src[i].addr = cpu_to_le32(addr_pad);
  375. cet->t_src[i].len = cpu_to_le32(j);
  376. if (dma_mapping_error(ce->dev, addr_pad)) {
  377. dev_err(ce->dev, "DMA error on padding SG\n");
  378. err = -EINVAL;
  379. goto theend;
  380. }
  381. if (ce->variant->hash_t_dlen_in_bits)
  382. cet->t_dlen = cpu_to_le32((areq->nbytes + j * 4) * 8);
  383. else
  384. cet->t_dlen = cpu_to_le32(areq->nbytes / 4 + j);
  385. chan->timeout = areq->nbytes;
  386. err = sun8i_ce_run_task(ce, flow, crypto_tfm_alg_name(areq->base.tfm));
  387. dma_unmap_single(ce->dev, addr_pad, j * 4, DMA_TO_DEVICE);
  388. dma_unmap_sg(ce->dev, areq->src, ns, DMA_TO_DEVICE);
  389. dma_unmap_single(ce->dev, addr_res, digestsize, DMA_FROM_DEVICE);
  390. memcpy(areq->result, result, algt->alg.hash.halg.digestsize);
  391. theend:
  392. kfree(buf);
  393. kfree(result);
  394. local_bh_disable();
  395. crypto_finalize_hash_request(engine, breq, err);
  396. local_bh_enable();
  397. return 0;
  398. }