tegra-se-hash.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
  3. /*
  4. * Crypto driver to handle HASH algorithms using NVIDIA Security Engine.
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/module.h>
  9. #include <linux/of_device.h>
  10. #include <linux/platform_device.h>
  11. #include <crypto/aes.h>
  12. #include <crypto/sha1.h>
  13. #include <crypto/sha2.h>
  14. #include <crypto/sha3.h>
  15. #include <crypto/internal/des.h>
  16. #include <crypto/engine.h>
  17. #include <crypto/scatterwalk.h>
  18. #include <crypto/internal/hash.h>
  19. #include "tegra-se.h"
  20. struct tegra_sha_ctx {
  21. struct tegra_se *se;
  22. unsigned int alg;
  23. bool fallback;
  24. u32 key_id;
  25. struct crypto_ahash *fallback_tfm;
  26. };
  27. struct tegra_sha_reqctx {
  28. struct scatterlist *src_sg;
  29. struct tegra_se_datbuf datbuf;
  30. struct tegra_se_datbuf residue;
  31. struct tegra_se_datbuf digest;
  32. unsigned int alg;
  33. unsigned int config;
  34. unsigned int total_len;
  35. unsigned int blk_size;
  36. unsigned int task;
  37. u32 key_id;
  38. u32 result[HASH_RESULT_REG_COUNT];
  39. struct ahash_request fallback_req;
  40. };
  41. static int tegra_sha_get_config(u32 alg)
  42. {
  43. int cfg = 0;
  44. switch (alg) {
  45. case SE_ALG_SHA1:
  46. cfg |= SE_SHA_ENC_ALG_SHA;
  47. cfg |= SE_SHA_ENC_MODE_SHA1;
  48. break;
  49. case SE_ALG_HMAC_SHA224:
  50. cfg |= SE_SHA_ENC_ALG_HMAC;
  51. fallthrough;
  52. case SE_ALG_SHA224:
  53. cfg |= SE_SHA_ENC_ALG_SHA;
  54. cfg |= SE_SHA_ENC_MODE_SHA224;
  55. break;
  56. case SE_ALG_HMAC_SHA256:
  57. cfg |= SE_SHA_ENC_ALG_HMAC;
  58. fallthrough;
  59. case SE_ALG_SHA256:
  60. cfg |= SE_SHA_ENC_ALG_SHA;
  61. cfg |= SE_SHA_ENC_MODE_SHA256;
  62. break;
  63. case SE_ALG_HMAC_SHA384:
  64. cfg |= SE_SHA_ENC_ALG_HMAC;
  65. fallthrough;
  66. case SE_ALG_SHA384:
  67. cfg |= SE_SHA_ENC_ALG_SHA;
  68. cfg |= SE_SHA_ENC_MODE_SHA384;
  69. break;
  70. case SE_ALG_HMAC_SHA512:
  71. cfg |= SE_SHA_ENC_ALG_HMAC;
  72. fallthrough;
  73. case SE_ALG_SHA512:
  74. cfg |= SE_SHA_ENC_ALG_SHA;
  75. cfg |= SE_SHA_ENC_MODE_SHA512;
  76. break;
  77. case SE_ALG_SHA3_224:
  78. cfg |= SE_SHA_ENC_ALG_SHA;
  79. cfg |= SE_SHA_ENC_MODE_SHA3_224;
  80. break;
  81. case SE_ALG_SHA3_256:
  82. cfg |= SE_SHA_ENC_ALG_SHA;
  83. cfg |= SE_SHA_ENC_MODE_SHA3_256;
  84. break;
  85. case SE_ALG_SHA3_384:
  86. cfg |= SE_SHA_ENC_ALG_SHA;
  87. cfg |= SE_SHA_ENC_MODE_SHA3_384;
  88. break;
  89. case SE_ALG_SHA3_512:
  90. cfg |= SE_SHA_ENC_ALG_SHA;
  91. cfg |= SE_SHA_ENC_MODE_SHA3_512;
  92. break;
  93. default:
  94. return -EINVAL;
  95. }
  96. return cfg;
  97. }
  98. static int tegra_sha_fallback_init(struct ahash_request *req)
  99. {
  100. struct tegra_sha_reqctx *rctx = ahash_request_ctx(req);
  101. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  102. struct tegra_sha_ctx *ctx = crypto_ahash_ctx(tfm);
  103. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
  104. rctx->fallback_req.base.flags = req->base.flags &
  105. CRYPTO_TFM_REQ_MAY_SLEEP;
  106. return crypto_ahash_init(&rctx->fallback_req);
  107. }
  108. static int tegra_sha_fallback_update(struct ahash_request *req)
  109. {
  110. struct tegra_sha_reqctx *rctx = ahash_request_ctx(req);
  111. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  112. struct tegra_sha_ctx *ctx = crypto_ahash_ctx(tfm);
  113. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
  114. rctx->fallback_req.base.flags = req->base.flags &
  115. CRYPTO_TFM_REQ_MAY_SLEEP;
  116. rctx->fallback_req.nbytes = req->nbytes;
  117. rctx->fallback_req.src = req->src;
  118. return crypto_ahash_update(&rctx->fallback_req);
  119. }
  120. static int tegra_sha_fallback_final(struct ahash_request *req)
  121. {
  122. struct tegra_sha_reqctx *rctx = ahash_request_ctx(req);
  123. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  124. struct tegra_sha_ctx *ctx = crypto_ahash_ctx(tfm);
  125. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
  126. rctx->fallback_req.base.flags = req->base.flags &
  127. CRYPTO_TFM_REQ_MAY_SLEEP;
  128. rctx->fallback_req.result = req->result;
  129. return crypto_ahash_final(&rctx->fallback_req);
  130. }
  131. static int tegra_sha_fallback_finup(struct ahash_request *req)
  132. {
  133. struct tegra_sha_reqctx *rctx = ahash_request_ctx(req);
  134. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  135. struct tegra_sha_ctx *ctx = crypto_ahash_ctx(tfm);
  136. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
  137. rctx->fallback_req.base.flags = req->base.flags &
  138. CRYPTO_TFM_REQ_MAY_SLEEP;
  139. rctx->fallback_req.nbytes = req->nbytes;
  140. rctx->fallback_req.src = req->src;
  141. rctx->fallback_req.result = req->result;
  142. return crypto_ahash_finup(&rctx->fallback_req);
  143. }
  144. static int tegra_sha_fallback_digest(struct ahash_request *req)
  145. {
  146. struct tegra_sha_reqctx *rctx = ahash_request_ctx(req);
  147. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  148. struct tegra_sha_ctx *ctx = crypto_ahash_ctx(tfm);
  149. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
  150. rctx->fallback_req.base.flags = req->base.flags &
  151. CRYPTO_TFM_REQ_MAY_SLEEP;
  152. rctx->fallback_req.nbytes = req->nbytes;
  153. rctx->fallback_req.src = req->src;
  154. rctx->fallback_req.result = req->result;
  155. return crypto_ahash_digest(&rctx->fallback_req);
  156. }
  157. static int tegra_sha_fallback_import(struct ahash_request *req, const void *in)
  158. {
  159. struct tegra_sha_reqctx *rctx = ahash_request_ctx(req);
  160. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  161. struct tegra_sha_ctx *ctx = crypto_ahash_ctx(tfm);
  162. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
  163. rctx->fallback_req.base.flags = req->base.flags &
  164. CRYPTO_TFM_REQ_MAY_SLEEP;
  165. return crypto_ahash_import(&rctx->fallback_req, in);
  166. }
  167. static int tegra_sha_fallback_export(struct ahash_request *req, void *out)
  168. {
  169. struct tegra_sha_reqctx *rctx = ahash_request_ctx(req);
  170. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  171. struct tegra_sha_ctx *ctx = crypto_ahash_ctx(tfm);
  172. ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
  173. rctx->fallback_req.base.flags = req->base.flags &
  174. CRYPTO_TFM_REQ_MAY_SLEEP;
  175. return crypto_ahash_export(&rctx->fallback_req, out);
  176. }
  177. static int tegra_sha_prep_cmd(struct tegra_se *se, u32 *cpuvaddr,
  178. struct tegra_sha_reqctx *rctx)
  179. {
  180. u64 msg_len, msg_left;
  181. int i = 0;
  182. msg_len = rctx->total_len * 8;
  183. msg_left = rctx->datbuf.size * 8;
  184. /*
  185. * If IN_ADDR_HI_0.SZ > SHA_MSG_LEFT_[0-3] to the HASH engine,
  186. * HW treats it as the last buffer and process the data.
  187. * Therefore, add an extra byte to msg_left if it is not the
  188. * last buffer.
  189. */
  190. if (rctx->task & SHA_UPDATE) {
  191. msg_left += 8;
  192. msg_len += 8;
  193. }
  194. cpuvaddr[i++] = host1x_opcode_setpayload(8);
  195. cpuvaddr[i++] = se_host1x_opcode_incr_w(SE_SHA_MSG_LENGTH);
  196. cpuvaddr[i++] = lower_32_bits(msg_len);
  197. cpuvaddr[i++] = upper_32_bits(msg_len);
  198. cpuvaddr[i++] = 0;
  199. cpuvaddr[i++] = 0;
  200. cpuvaddr[i++] = lower_32_bits(msg_left);
  201. cpuvaddr[i++] = upper_32_bits(msg_left);
  202. cpuvaddr[i++] = 0;
  203. cpuvaddr[i++] = 0;
  204. cpuvaddr[i++] = host1x_opcode_setpayload(6);
  205. cpuvaddr[i++] = se_host1x_opcode_incr_w(SE_SHA_CFG);
  206. cpuvaddr[i++] = rctx->config;
  207. if (rctx->task & SHA_FIRST) {
  208. cpuvaddr[i++] = SE_SHA_TASK_HASH_INIT;
  209. rctx->task &= ~SHA_FIRST;
  210. } else {
  211. cpuvaddr[i++] = 0;
  212. }
  213. cpuvaddr[i++] = rctx->datbuf.addr;
  214. cpuvaddr[i++] = (u32)(SE_ADDR_HI_MSB(upper_32_bits(rctx->datbuf.addr)) |
  215. SE_ADDR_HI_SZ(rctx->datbuf.size));
  216. cpuvaddr[i++] = rctx->digest.addr;
  217. cpuvaddr[i++] = (u32)(SE_ADDR_HI_MSB(upper_32_bits(rctx->digest.addr)) |
  218. SE_ADDR_HI_SZ(rctx->digest.size));
  219. if (rctx->key_id) {
  220. cpuvaddr[i++] = host1x_opcode_setpayload(1);
  221. cpuvaddr[i++] = se_host1x_opcode_nonincr_w(SE_SHA_CRYPTO_CFG);
  222. cpuvaddr[i++] = SE_AES_KEY_INDEX(rctx->key_id);
  223. }
  224. cpuvaddr[i++] = host1x_opcode_setpayload(1);
  225. cpuvaddr[i++] = se_host1x_opcode_nonincr_w(SE_SHA_OPERATION);
  226. cpuvaddr[i++] = SE_SHA_OP_WRSTALL |
  227. SE_SHA_OP_START |
  228. SE_SHA_OP_LASTBUF;
  229. cpuvaddr[i++] = se_host1x_opcode_nonincr(host1x_uclass_incr_syncpt_r(), 1);
  230. cpuvaddr[i++] = host1x_uclass_incr_syncpt_cond_f(1) |
  231. host1x_uclass_incr_syncpt_indx_f(se->syncpt_id);
  232. dev_dbg(se->dev, "msg len %llu msg left %llu cfg %#x",
  233. msg_len, msg_left, rctx->config);
  234. return i;
  235. }
  236. static void tegra_sha_copy_hash_result(struct tegra_se *se, struct tegra_sha_reqctx *rctx)
  237. {
  238. int i;
  239. for (i = 0; i < HASH_RESULT_REG_COUNT; i++)
  240. rctx->result[i] = readl(se->base + se->hw->regs->result + (i * 4));
  241. }
  242. static void tegra_sha_paste_hash_result(struct tegra_se *se, struct tegra_sha_reqctx *rctx)
  243. {
  244. int i;
  245. for (i = 0; i < HASH_RESULT_REG_COUNT; i++)
  246. writel(rctx->result[i],
  247. se->base + se->hw->regs->result + (i * 4));
  248. }
  249. static int tegra_sha_do_update(struct ahash_request *req)
  250. {
  251. struct tegra_sha_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  252. struct tegra_sha_reqctx *rctx = ahash_request_ctx(req);
  253. struct tegra_se *se = ctx->se;
  254. unsigned int nblks, nresidue, size, ret;
  255. u32 *cpuvaddr = se->cmdbuf->addr;
  256. nresidue = (req->nbytes + rctx->residue.size) % rctx->blk_size;
  257. nblks = (req->nbytes + rctx->residue.size) / rctx->blk_size;
  258. /*
  259. * If nbytes is a multiple of block size and there is no residue,
  260. * then reserve the last block as residue during final() to process.
  261. */
  262. if (!nresidue && nblks) {
  263. nresidue = rctx->blk_size;
  264. nblks--;
  265. }
  266. rctx->src_sg = req->src;
  267. rctx->datbuf.size = (req->nbytes + rctx->residue.size) - nresidue;
  268. rctx->total_len += rctx->datbuf.size;
  269. /*
  270. * If nbytes are less than a block size, copy it residue and
  271. * return. The bytes will be processed in final()
  272. */
  273. if (nblks < 1) {
  274. scatterwalk_map_and_copy(rctx->residue.buf + rctx->residue.size,
  275. rctx->src_sg, 0, req->nbytes, 0);
  276. rctx->residue.size += req->nbytes;
  277. return 0;
  278. }
  279. rctx->datbuf.buf = dma_alloc_coherent(ctx->se->dev, rctx->datbuf.size,
  280. &rctx->datbuf.addr, GFP_KERNEL);
  281. if (!rctx->datbuf.buf)
  282. return -ENOMEM;
  283. /* Copy the previous residue first */
  284. if (rctx->residue.size)
  285. memcpy(rctx->datbuf.buf, rctx->residue.buf, rctx->residue.size);
  286. scatterwalk_map_and_copy(rctx->datbuf.buf + rctx->residue.size,
  287. rctx->src_sg, 0, req->nbytes - nresidue, 0);
  288. scatterwalk_map_and_copy(rctx->residue.buf, rctx->src_sg,
  289. req->nbytes - nresidue, nresidue, 0);
  290. /* Update residue value with the residue after current block */
  291. rctx->residue.size = nresidue;
  292. rctx->config = tegra_sha_get_config(rctx->alg) |
  293. SE_SHA_DST_HASH_REG;
  294. /*
  295. * If this is not the first 'update' call, paste the previous copied
  296. * intermediate results to the registers so that it gets picked up.
  297. * This is to support the import/export functionality.
  298. */
  299. if (!(rctx->task & SHA_FIRST))
  300. tegra_sha_paste_hash_result(se, rctx);
  301. size = tegra_sha_prep_cmd(se, cpuvaddr, rctx);
  302. ret = tegra_se_host1x_submit(se, se->cmdbuf, size);
  303. /*
  304. * If this is not the final update, copy the intermediate results
  305. * from the registers so that it can be used in the next 'update'
  306. * call. This is to support the import/export functionality.
  307. */
  308. if (!(rctx->task & SHA_FINAL))
  309. tegra_sha_copy_hash_result(se, rctx);
  310. dma_free_coherent(ctx->se->dev, rctx->datbuf.size,
  311. rctx->datbuf.buf, rctx->datbuf.addr);
  312. return ret;
  313. }
  314. static int tegra_sha_do_final(struct ahash_request *req)
  315. {
  316. struct tegra_sha_reqctx *rctx = ahash_request_ctx(req);
  317. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  318. struct tegra_sha_ctx *ctx = crypto_ahash_ctx(tfm);
  319. struct tegra_se *se = ctx->se;
  320. u32 *cpuvaddr = se->cmdbuf->addr;
  321. int size, ret = 0;
  322. if (rctx->residue.size) {
  323. rctx->datbuf.buf = dma_alloc_coherent(se->dev, rctx->residue.size,
  324. &rctx->datbuf.addr, GFP_KERNEL);
  325. if (!rctx->datbuf.buf) {
  326. ret = -ENOMEM;
  327. goto out_free;
  328. }
  329. memcpy(rctx->datbuf.buf, rctx->residue.buf, rctx->residue.size);
  330. }
  331. rctx->datbuf.size = rctx->residue.size;
  332. rctx->total_len += rctx->residue.size;
  333. rctx->config = tegra_sha_get_config(rctx->alg) |
  334. SE_SHA_DST_MEMORY;
  335. size = tegra_sha_prep_cmd(se, cpuvaddr, rctx);
  336. ret = tegra_se_host1x_submit(se, se->cmdbuf, size);
  337. if (ret)
  338. goto out;
  339. /* Copy result */
  340. memcpy(req->result, rctx->digest.buf, rctx->digest.size);
  341. out:
  342. if (rctx->residue.size)
  343. dma_free_coherent(se->dev, rctx->datbuf.size,
  344. rctx->datbuf.buf, rctx->datbuf.addr);
  345. out_free:
  346. dma_free_coherent(se->dev, crypto_ahash_blocksize(tfm),
  347. rctx->residue.buf, rctx->residue.addr);
  348. dma_free_coherent(se->dev, rctx->digest.size, rctx->digest.buf,
  349. rctx->digest.addr);
  350. return ret;
  351. }
  352. static int tegra_sha_do_one_req(struct crypto_engine *engine, void *areq)
  353. {
  354. struct ahash_request *req = ahash_request_cast(areq);
  355. struct tegra_sha_reqctx *rctx = ahash_request_ctx(req);
  356. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  357. struct tegra_sha_ctx *ctx = crypto_ahash_ctx(tfm);
  358. struct tegra_se *se = ctx->se;
  359. int ret = 0;
  360. if (rctx->task & SHA_UPDATE) {
  361. ret = tegra_sha_do_update(req);
  362. if (ret)
  363. goto out;
  364. rctx->task &= ~SHA_UPDATE;
  365. }
  366. if (rctx->task & SHA_FINAL) {
  367. ret = tegra_sha_do_final(req);
  368. if (ret)
  369. goto out;
  370. rctx->task &= ~SHA_FINAL;
  371. }
  372. out:
  373. crypto_finalize_hash_request(se->engine, req, ret);
  374. return 0;
  375. }
  376. static void tegra_sha_init_fallback(struct crypto_ahash *tfm, struct tegra_sha_ctx *ctx,
  377. const char *algname)
  378. {
  379. unsigned int statesize;
  380. ctx->fallback_tfm = crypto_alloc_ahash(algname, 0, CRYPTO_ALG_ASYNC |
  381. CRYPTO_ALG_NEED_FALLBACK);
  382. if (IS_ERR(ctx->fallback_tfm)) {
  383. dev_warn(ctx->se->dev,
  384. "failed to allocate fallback for %s\n", algname);
  385. ctx->fallback_tfm = NULL;
  386. return;
  387. }
  388. statesize = crypto_ahash_statesize(ctx->fallback_tfm);
  389. if (statesize > sizeof(struct tegra_sha_reqctx))
  390. crypto_ahash_set_statesize(tfm, statesize);
  391. /* Update reqsize if fallback is added */
  392. crypto_ahash_set_reqsize(tfm,
  393. sizeof(struct tegra_sha_reqctx) +
  394. crypto_ahash_reqsize(ctx->fallback_tfm));
  395. }
  396. static int tegra_sha_cra_init(struct crypto_tfm *tfm)
  397. {
  398. struct tegra_sha_ctx *ctx = crypto_tfm_ctx(tfm);
  399. struct crypto_ahash *ahash_tfm = __crypto_ahash_cast(tfm);
  400. struct ahash_alg *alg = __crypto_ahash_alg(tfm->__crt_alg);
  401. struct tegra_se_alg *se_alg;
  402. const char *algname;
  403. int ret;
  404. algname = crypto_tfm_alg_name(tfm);
  405. se_alg = container_of(alg, struct tegra_se_alg, alg.ahash.base);
  406. crypto_ahash_set_reqsize(ahash_tfm, sizeof(struct tegra_sha_reqctx));
  407. ctx->se = se_alg->se_dev;
  408. ctx->fallback = false;
  409. ctx->key_id = 0;
  410. ret = se_algname_to_algid(algname);
  411. if (ret < 0) {
  412. dev_err(ctx->se->dev, "invalid algorithm\n");
  413. return ret;
  414. }
  415. if (se_alg->alg_base)
  416. tegra_sha_init_fallback(ahash_tfm, ctx, algname);
  417. ctx->alg = ret;
  418. return 0;
  419. }
  420. static void tegra_sha_cra_exit(struct crypto_tfm *tfm)
  421. {
  422. struct tegra_sha_ctx *ctx = crypto_tfm_ctx(tfm);
  423. if (ctx->fallback_tfm)
  424. crypto_free_ahash(ctx->fallback_tfm);
  425. tegra_key_invalidate(ctx->se, ctx->key_id, ctx->alg);
  426. }
  427. static int tegra_sha_init(struct ahash_request *req)
  428. {
  429. struct tegra_sha_reqctx *rctx = ahash_request_ctx(req);
  430. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  431. struct tegra_sha_ctx *ctx = crypto_ahash_ctx(tfm);
  432. struct tegra_se *se = ctx->se;
  433. if (ctx->fallback)
  434. return tegra_sha_fallback_init(req);
  435. rctx->total_len = 0;
  436. rctx->datbuf.size = 0;
  437. rctx->residue.size = 0;
  438. rctx->key_id = ctx->key_id;
  439. rctx->task = SHA_FIRST;
  440. rctx->alg = ctx->alg;
  441. rctx->blk_size = crypto_ahash_blocksize(tfm);
  442. rctx->digest.size = crypto_ahash_digestsize(tfm);
  443. rctx->digest.buf = dma_alloc_coherent(se->dev, rctx->digest.size,
  444. &rctx->digest.addr, GFP_KERNEL);
  445. if (!rctx->digest.buf)
  446. goto digbuf_fail;
  447. rctx->residue.buf = dma_alloc_coherent(se->dev, rctx->blk_size,
  448. &rctx->residue.addr, GFP_KERNEL);
  449. if (!rctx->residue.buf)
  450. goto resbuf_fail;
  451. return 0;
  452. resbuf_fail:
  453. dma_free_coherent(se->dev, rctx->digest.size, rctx->digest.buf,
  454. rctx->digest.addr);
  455. digbuf_fail:
  456. return -ENOMEM;
  457. }
  458. static int tegra_hmac_fallback_setkey(struct tegra_sha_ctx *ctx, const u8 *key,
  459. unsigned int keylen)
  460. {
  461. if (!ctx->fallback_tfm) {
  462. dev_dbg(ctx->se->dev, "invalid key length (%d)\n", keylen);
  463. return -EINVAL;
  464. }
  465. ctx->fallback = true;
  466. return crypto_ahash_setkey(ctx->fallback_tfm, key, keylen);
  467. }
  468. static int tegra_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
  469. unsigned int keylen)
  470. {
  471. struct tegra_sha_ctx *ctx = crypto_ahash_ctx(tfm);
  472. int ret;
  473. if (aes_check_keylen(keylen))
  474. return tegra_hmac_fallback_setkey(ctx, key, keylen);
  475. ret = tegra_key_submit(ctx->se, key, keylen, ctx->alg, &ctx->key_id);
  476. if (ret)
  477. return tegra_hmac_fallback_setkey(ctx, key, keylen);
  478. ctx->fallback = false;
  479. return 0;
  480. }
  481. static int tegra_sha_update(struct ahash_request *req)
  482. {
  483. struct tegra_sha_reqctx *rctx = ahash_request_ctx(req);
  484. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  485. struct tegra_sha_ctx *ctx = crypto_ahash_ctx(tfm);
  486. if (ctx->fallback)
  487. return tegra_sha_fallback_update(req);
  488. rctx->task |= SHA_UPDATE;
  489. return crypto_transfer_hash_request_to_engine(ctx->se->engine, req);
  490. }
  491. static int tegra_sha_final(struct ahash_request *req)
  492. {
  493. struct tegra_sha_reqctx *rctx = ahash_request_ctx(req);
  494. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  495. struct tegra_sha_ctx *ctx = crypto_ahash_ctx(tfm);
  496. if (ctx->fallback)
  497. return tegra_sha_fallback_final(req);
  498. rctx->task |= SHA_FINAL;
  499. return crypto_transfer_hash_request_to_engine(ctx->se->engine, req);
  500. }
  501. static int tegra_sha_finup(struct ahash_request *req)
  502. {
  503. struct tegra_sha_reqctx *rctx = ahash_request_ctx(req);
  504. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  505. struct tegra_sha_ctx *ctx = crypto_ahash_ctx(tfm);
  506. if (ctx->fallback)
  507. return tegra_sha_fallback_finup(req);
  508. rctx->task |= SHA_UPDATE | SHA_FINAL;
  509. return crypto_transfer_hash_request_to_engine(ctx->se->engine, req);
  510. }
  511. static int tegra_sha_digest(struct ahash_request *req)
  512. {
  513. struct tegra_sha_reqctx *rctx = ahash_request_ctx(req);
  514. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  515. struct tegra_sha_ctx *ctx = crypto_ahash_ctx(tfm);
  516. int ret;
  517. if (ctx->fallback)
  518. return tegra_sha_fallback_digest(req);
  519. ret = tegra_sha_init(req);
  520. if (ret)
  521. return ret;
  522. rctx->task |= SHA_UPDATE | SHA_FINAL;
  523. return crypto_transfer_hash_request_to_engine(ctx->se->engine, req);
  524. }
  525. static int tegra_sha_export(struct ahash_request *req, void *out)
  526. {
  527. struct tegra_sha_reqctx *rctx = ahash_request_ctx(req);
  528. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  529. struct tegra_sha_ctx *ctx = crypto_ahash_ctx(tfm);
  530. if (ctx->fallback)
  531. return tegra_sha_fallback_export(req, out);
  532. memcpy(out, rctx, sizeof(*rctx));
  533. return 0;
  534. }
  535. static int tegra_sha_import(struct ahash_request *req, const void *in)
  536. {
  537. struct tegra_sha_reqctx *rctx = ahash_request_ctx(req);
  538. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  539. struct tegra_sha_ctx *ctx = crypto_ahash_ctx(tfm);
  540. if (ctx->fallback)
  541. return tegra_sha_fallback_import(req, in);
  542. memcpy(rctx, in, sizeof(*rctx));
  543. return 0;
  544. }
  545. static struct tegra_se_alg tegra_hash_algs[] = {
  546. {
  547. .alg.ahash.op.do_one_request = tegra_sha_do_one_req,
  548. .alg.ahash.base = {
  549. .init = tegra_sha_init,
  550. .update = tegra_sha_update,
  551. .final = tegra_sha_final,
  552. .finup = tegra_sha_finup,
  553. .digest = tegra_sha_digest,
  554. .export = tegra_sha_export,
  555. .import = tegra_sha_import,
  556. .halg.digestsize = SHA1_DIGEST_SIZE,
  557. .halg.statesize = sizeof(struct tegra_sha_reqctx),
  558. .halg.base = {
  559. .cra_name = "sha1",
  560. .cra_driver_name = "tegra-se-sha1",
  561. .cra_priority = 300,
  562. .cra_flags = CRYPTO_ALG_TYPE_AHASH,
  563. .cra_blocksize = SHA1_BLOCK_SIZE,
  564. .cra_ctxsize = sizeof(struct tegra_sha_ctx),
  565. .cra_alignmask = 0,
  566. .cra_module = THIS_MODULE,
  567. .cra_init = tegra_sha_cra_init,
  568. .cra_exit = tegra_sha_cra_exit,
  569. }
  570. }
  571. }, {
  572. .alg.ahash.op.do_one_request = tegra_sha_do_one_req,
  573. .alg.ahash.base = {
  574. .init = tegra_sha_init,
  575. .update = tegra_sha_update,
  576. .final = tegra_sha_final,
  577. .finup = tegra_sha_finup,
  578. .digest = tegra_sha_digest,
  579. .export = tegra_sha_export,
  580. .import = tegra_sha_import,
  581. .halg.digestsize = SHA224_DIGEST_SIZE,
  582. .halg.statesize = sizeof(struct tegra_sha_reqctx),
  583. .halg.base = {
  584. .cra_name = "sha224",
  585. .cra_driver_name = "tegra-se-sha224",
  586. .cra_priority = 300,
  587. .cra_flags = CRYPTO_ALG_TYPE_AHASH,
  588. .cra_blocksize = SHA224_BLOCK_SIZE,
  589. .cra_ctxsize = sizeof(struct tegra_sha_ctx),
  590. .cra_alignmask = 0,
  591. .cra_module = THIS_MODULE,
  592. .cra_init = tegra_sha_cra_init,
  593. .cra_exit = tegra_sha_cra_exit,
  594. }
  595. }
  596. }, {
  597. .alg.ahash.op.do_one_request = tegra_sha_do_one_req,
  598. .alg.ahash.base = {
  599. .init = tegra_sha_init,
  600. .update = tegra_sha_update,
  601. .final = tegra_sha_final,
  602. .finup = tegra_sha_finup,
  603. .digest = tegra_sha_digest,
  604. .export = tegra_sha_export,
  605. .import = tegra_sha_import,
  606. .halg.digestsize = SHA256_DIGEST_SIZE,
  607. .halg.statesize = sizeof(struct tegra_sha_reqctx),
  608. .halg.base = {
  609. .cra_name = "sha256",
  610. .cra_driver_name = "tegra-se-sha256",
  611. .cra_priority = 300,
  612. .cra_flags = CRYPTO_ALG_TYPE_AHASH,
  613. .cra_blocksize = SHA256_BLOCK_SIZE,
  614. .cra_ctxsize = sizeof(struct tegra_sha_ctx),
  615. .cra_alignmask = 0,
  616. .cra_module = THIS_MODULE,
  617. .cra_init = tegra_sha_cra_init,
  618. .cra_exit = tegra_sha_cra_exit,
  619. }
  620. }
  621. }, {
  622. .alg.ahash.op.do_one_request = tegra_sha_do_one_req,
  623. .alg.ahash.base = {
  624. .init = tegra_sha_init,
  625. .update = tegra_sha_update,
  626. .final = tegra_sha_final,
  627. .finup = tegra_sha_finup,
  628. .digest = tegra_sha_digest,
  629. .export = tegra_sha_export,
  630. .import = tegra_sha_import,
  631. .halg.digestsize = SHA384_DIGEST_SIZE,
  632. .halg.statesize = sizeof(struct tegra_sha_reqctx),
  633. .halg.base = {
  634. .cra_name = "sha384",
  635. .cra_driver_name = "tegra-se-sha384",
  636. .cra_priority = 300,
  637. .cra_flags = CRYPTO_ALG_TYPE_AHASH,
  638. .cra_blocksize = SHA384_BLOCK_SIZE,
  639. .cra_ctxsize = sizeof(struct tegra_sha_ctx),
  640. .cra_alignmask = 0,
  641. .cra_module = THIS_MODULE,
  642. .cra_init = tegra_sha_cra_init,
  643. .cra_exit = tegra_sha_cra_exit,
  644. }
  645. }
  646. }, {
  647. .alg.ahash.op.do_one_request = tegra_sha_do_one_req,
  648. .alg.ahash.base = {
  649. .init = tegra_sha_init,
  650. .update = tegra_sha_update,
  651. .final = tegra_sha_final,
  652. .finup = tegra_sha_finup,
  653. .digest = tegra_sha_digest,
  654. .export = tegra_sha_export,
  655. .import = tegra_sha_import,
  656. .halg.digestsize = SHA512_DIGEST_SIZE,
  657. .halg.statesize = sizeof(struct tegra_sha_reqctx),
  658. .halg.base = {
  659. .cra_name = "sha512",
  660. .cra_driver_name = "tegra-se-sha512",
  661. .cra_priority = 300,
  662. .cra_flags = CRYPTO_ALG_TYPE_AHASH,
  663. .cra_blocksize = SHA512_BLOCK_SIZE,
  664. .cra_ctxsize = sizeof(struct tegra_sha_ctx),
  665. .cra_alignmask = 0,
  666. .cra_module = THIS_MODULE,
  667. .cra_init = tegra_sha_cra_init,
  668. .cra_exit = tegra_sha_cra_exit,
  669. }
  670. }
  671. }, {
  672. .alg.ahash.op.do_one_request = tegra_sha_do_one_req,
  673. .alg.ahash.base = {
  674. .init = tegra_sha_init,
  675. .update = tegra_sha_update,
  676. .final = tegra_sha_final,
  677. .finup = tegra_sha_finup,
  678. .digest = tegra_sha_digest,
  679. .export = tegra_sha_export,
  680. .import = tegra_sha_import,
  681. .halg.digestsize = SHA3_224_DIGEST_SIZE,
  682. .halg.statesize = sizeof(struct tegra_sha_reqctx),
  683. .halg.base = {
  684. .cra_name = "sha3-224",
  685. .cra_driver_name = "tegra-se-sha3-224",
  686. .cra_priority = 300,
  687. .cra_flags = CRYPTO_ALG_TYPE_AHASH,
  688. .cra_blocksize = SHA3_224_BLOCK_SIZE,
  689. .cra_ctxsize = sizeof(struct tegra_sha_ctx),
  690. .cra_alignmask = 0,
  691. .cra_module = THIS_MODULE,
  692. .cra_init = tegra_sha_cra_init,
  693. .cra_exit = tegra_sha_cra_exit,
  694. }
  695. }
  696. }, {
  697. .alg.ahash.op.do_one_request = tegra_sha_do_one_req,
  698. .alg.ahash.base = {
  699. .init = tegra_sha_init,
  700. .update = tegra_sha_update,
  701. .final = tegra_sha_final,
  702. .finup = tegra_sha_finup,
  703. .digest = tegra_sha_digest,
  704. .export = tegra_sha_export,
  705. .import = tegra_sha_import,
  706. .halg.digestsize = SHA3_256_DIGEST_SIZE,
  707. .halg.statesize = sizeof(struct tegra_sha_reqctx),
  708. .halg.base = {
  709. .cra_name = "sha3-256",
  710. .cra_driver_name = "tegra-se-sha3-256",
  711. .cra_priority = 300,
  712. .cra_flags = CRYPTO_ALG_TYPE_AHASH,
  713. .cra_blocksize = SHA3_256_BLOCK_SIZE,
  714. .cra_ctxsize = sizeof(struct tegra_sha_ctx),
  715. .cra_alignmask = 0,
  716. .cra_module = THIS_MODULE,
  717. .cra_init = tegra_sha_cra_init,
  718. .cra_exit = tegra_sha_cra_exit,
  719. }
  720. }
  721. }, {
  722. .alg.ahash.op.do_one_request = tegra_sha_do_one_req,
  723. .alg.ahash.base = {
  724. .init = tegra_sha_init,
  725. .update = tegra_sha_update,
  726. .final = tegra_sha_final,
  727. .finup = tegra_sha_finup,
  728. .digest = tegra_sha_digest,
  729. .export = tegra_sha_export,
  730. .import = tegra_sha_import,
  731. .halg.digestsize = SHA3_384_DIGEST_SIZE,
  732. .halg.statesize = sizeof(struct tegra_sha_reqctx),
  733. .halg.base = {
  734. .cra_name = "sha3-384",
  735. .cra_driver_name = "tegra-se-sha3-384",
  736. .cra_priority = 300,
  737. .cra_flags = CRYPTO_ALG_TYPE_AHASH,
  738. .cra_blocksize = SHA3_384_BLOCK_SIZE,
  739. .cra_ctxsize = sizeof(struct tegra_sha_ctx),
  740. .cra_alignmask = 0,
  741. .cra_module = THIS_MODULE,
  742. .cra_init = tegra_sha_cra_init,
  743. .cra_exit = tegra_sha_cra_exit,
  744. }
  745. }
  746. }, {
  747. .alg.ahash.op.do_one_request = tegra_sha_do_one_req,
  748. .alg.ahash.base = {
  749. .init = tegra_sha_init,
  750. .update = tegra_sha_update,
  751. .final = tegra_sha_final,
  752. .finup = tegra_sha_finup,
  753. .digest = tegra_sha_digest,
  754. .export = tegra_sha_export,
  755. .import = tegra_sha_import,
  756. .halg.digestsize = SHA3_512_DIGEST_SIZE,
  757. .halg.statesize = sizeof(struct tegra_sha_reqctx),
  758. .halg.base = {
  759. .cra_name = "sha3-512",
  760. .cra_driver_name = "tegra-se-sha3-512",
  761. .cra_priority = 300,
  762. .cra_flags = CRYPTO_ALG_TYPE_AHASH,
  763. .cra_blocksize = SHA3_512_BLOCK_SIZE,
  764. .cra_ctxsize = sizeof(struct tegra_sha_ctx),
  765. .cra_alignmask = 0,
  766. .cra_module = THIS_MODULE,
  767. .cra_init = tegra_sha_cra_init,
  768. .cra_exit = tegra_sha_cra_exit,
  769. }
  770. }
  771. }, {
  772. .alg_base = "sha224",
  773. .alg.ahash.op.do_one_request = tegra_sha_do_one_req,
  774. .alg.ahash.base = {
  775. .init = tegra_sha_init,
  776. .update = tegra_sha_update,
  777. .final = tegra_sha_final,
  778. .finup = tegra_sha_finup,
  779. .digest = tegra_sha_digest,
  780. .export = tegra_sha_export,
  781. .import = tegra_sha_import,
  782. .setkey = tegra_hmac_setkey,
  783. .halg.digestsize = SHA224_DIGEST_SIZE,
  784. .halg.statesize = sizeof(struct tegra_sha_reqctx),
  785. .halg.base = {
  786. .cra_name = "hmac(sha224)",
  787. .cra_driver_name = "tegra-se-hmac-sha224",
  788. .cra_priority = 300,
  789. .cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_NEED_FALLBACK,
  790. .cra_blocksize = SHA224_BLOCK_SIZE,
  791. .cra_ctxsize = sizeof(struct tegra_sha_ctx),
  792. .cra_alignmask = 0,
  793. .cra_module = THIS_MODULE,
  794. .cra_init = tegra_sha_cra_init,
  795. .cra_exit = tegra_sha_cra_exit,
  796. }
  797. }
  798. }, {
  799. .alg_base = "sha256",
  800. .alg.ahash.op.do_one_request = tegra_sha_do_one_req,
  801. .alg.ahash.base = {
  802. .init = tegra_sha_init,
  803. .update = tegra_sha_update,
  804. .final = tegra_sha_final,
  805. .finup = tegra_sha_finup,
  806. .digest = tegra_sha_digest,
  807. .export = tegra_sha_export,
  808. .import = tegra_sha_import,
  809. .setkey = tegra_hmac_setkey,
  810. .halg.digestsize = SHA256_DIGEST_SIZE,
  811. .halg.statesize = sizeof(struct tegra_sha_reqctx),
  812. .halg.base = {
  813. .cra_name = "hmac(sha256)",
  814. .cra_driver_name = "tegra-se-hmac-sha256",
  815. .cra_priority = 300,
  816. .cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_NEED_FALLBACK,
  817. .cra_blocksize = SHA256_BLOCK_SIZE,
  818. .cra_ctxsize = sizeof(struct tegra_sha_ctx),
  819. .cra_alignmask = 0,
  820. .cra_module = THIS_MODULE,
  821. .cra_init = tegra_sha_cra_init,
  822. .cra_exit = tegra_sha_cra_exit,
  823. }
  824. }
  825. }, {
  826. .alg_base = "sha384",
  827. .alg.ahash.op.do_one_request = tegra_sha_do_one_req,
  828. .alg.ahash.base = {
  829. .init = tegra_sha_init,
  830. .update = tegra_sha_update,
  831. .final = tegra_sha_final,
  832. .finup = tegra_sha_finup,
  833. .digest = tegra_sha_digest,
  834. .export = tegra_sha_export,
  835. .import = tegra_sha_import,
  836. .setkey = tegra_hmac_setkey,
  837. .halg.digestsize = SHA384_DIGEST_SIZE,
  838. .halg.statesize = sizeof(struct tegra_sha_reqctx),
  839. .halg.base = {
  840. .cra_name = "hmac(sha384)",
  841. .cra_driver_name = "tegra-se-hmac-sha384",
  842. .cra_priority = 300,
  843. .cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_NEED_FALLBACK,
  844. .cra_blocksize = SHA384_BLOCK_SIZE,
  845. .cra_ctxsize = sizeof(struct tegra_sha_ctx),
  846. .cra_alignmask = 0,
  847. .cra_module = THIS_MODULE,
  848. .cra_init = tegra_sha_cra_init,
  849. .cra_exit = tegra_sha_cra_exit,
  850. }
  851. }
  852. }, {
  853. .alg_base = "sha512",
  854. .alg.ahash.op.do_one_request = tegra_sha_do_one_req,
  855. .alg.ahash.base = {
  856. .init = tegra_sha_init,
  857. .update = tegra_sha_update,
  858. .final = tegra_sha_final,
  859. .finup = tegra_sha_finup,
  860. .digest = tegra_sha_digest,
  861. .export = tegra_sha_export,
  862. .import = tegra_sha_import,
  863. .setkey = tegra_hmac_setkey,
  864. .halg.digestsize = SHA512_DIGEST_SIZE,
  865. .halg.statesize = sizeof(struct tegra_sha_reqctx),
  866. .halg.base = {
  867. .cra_name = "hmac(sha512)",
  868. .cra_driver_name = "tegra-se-hmac-sha512",
  869. .cra_priority = 300,
  870. .cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_NEED_FALLBACK,
  871. .cra_blocksize = SHA512_BLOCK_SIZE,
  872. .cra_ctxsize = sizeof(struct tegra_sha_ctx),
  873. .cra_alignmask = 0,
  874. .cra_module = THIS_MODULE,
  875. .cra_init = tegra_sha_cra_init,
  876. .cra_exit = tegra_sha_cra_exit,
  877. }
  878. }
  879. }
  880. };
  881. static int tegra_hash_kac_manifest(u32 user, u32 alg, u32 keylen)
  882. {
  883. int manifest;
  884. manifest = SE_KAC_USER_NS;
  885. switch (alg) {
  886. case SE_ALG_HMAC_SHA224:
  887. case SE_ALG_HMAC_SHA256:
  888. case SE_ALG_HMAC_SHA384:
  889. case SE_ALG_HMAC_SHA512:
  890. manifest |= SE_KAC_HMAC;
  891. break;
  892. default:
  893. return -EINVAL;
  894. }
  895. switch (keylen) {
  896. case AES_KEYSIZE_128:
  897. manifest |= SE_KAC_SIZE_128;
  898. break;
  899. case AES_KEYSIZE_192:
  900. manifest |= SE_KAC_SIZE_192;
  901. break;
  902. case AES_KEYSIZE_256:
  903. default:
  904. manifest |= SE_KAC_SIZE_256;
  905. break;
  906. }
  907. return manifest;
  908. }
  909. int tegra_init_hash(struct tegra_se *se)
  910. {
  911. struct ahash_engine_alg *alg;
  912. int i, ret;
  913. se->manifest = tegra_hash_kac_manifest;
  914. for (i = 0; i < ARRAY_SIZE(tegra_hash_algs); i++) {
  915. tegra_hash_algs[i].se_dev = se;
  916. alg = &tegra_hash_algs[i].alg.ahash;
  917. ret = crypto_engine_register_ahash(alg);
  918. if (ret) {
  919. dev_err(se->dev, "failed to register %s\n",
  920. alg->base.halg.base.cra_name);
  921. goto sha_err;
  922. }
  923. }
  924. return 0;
  925. sha_err:
  926. while (i--)
  927. crypto_engine_unregister_ahash(&tegra_hash_algs[i].alg.ahash);
  928. return ret;
  929. }
  930. void tegra_deinit_hash(struct tegra_se *se)
  931. {
  932. int i;
  933. for (i = 0; i < ARRAY_SIZE(tegra_hash_algs); i++)
  934. crypto_engine_unregister_ahash(&tegra_hash_algs[i].alg.ahash);
  935. }