aead.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2021, Linaro Limited. All rights reserved.
  4. */
  5. #include <linux/dma-mapping.h>
  6. #include <linux/interrupt.h>
  7. #include <crypto/gcm.h>
  8. #include <crypto/authenc.h>
  9. #include <crypto/internal/aead.h>
  10. #include <crypto/internal/des.h>
  11. #include <crypto/sha1.h>
  12. #include <crypto/sha2.h>
  13. #include <crypto/scatterwalk.h>
  14. #include "aead.h"
  15. #define CCM_NONCE_ADATA_SHIFT 6
  16. #define CCM_NONCE_AUTHSIZE_SHIFT 3
  17. #define MAX_CCM_ADATA_HEADER_LEN 6
  18. static LIST_HEAD(aead_algs);
  19. static void qce_aead_done(void *data)
  20. {
  21. struct crypto_async_request *async_req = data;
  22. struct aead_request *req = aead_request_cast(async_req);
  23. struct qce_aead_reqctx *rctx = aead_request_ctx(req);
  24. struct qce_aead_ctx *ctx = crypto_tfm_ctx(async_req->tfm);
  25. struct qce_alg_template *tmpl = to_aead_tmpl(crypto_aead_reqtfm(req));
  26. struct qce_device *qce = tmpl->qce;
  27. struct qce_result_dump *result_buf = qce->dma.result_buf;
  28. enum dma_data_direction dir_src, dir_dst;
  29. bool diff_dst;
  30. int error;
  31. u32 status;
  32. unsigned int totallen;
  33. unsigned char tag[SHA256_DIGEST_SIZE] = {0};
  34. int ret = 0;
  35. diff_dst = (req->src != req->dst) ? true : false;
  36. dir_src = diff_dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL;
  37. dir_dst = diff_dst ? DMA_FROM_DEVICE : DMA_BIDIRECTIONAL;
  38. error = qce_dma_terminate_all(&qce->dma);
  39. if (error)
  40. dev_dbg(qce->dev, "aead dma termination error (%d)\n",
  41. error);
  42. if (diff_dst)
  43. dma_unmap_sg(qce->dev, rctx->src_sg, rctx->src_nents, dir_src);
  44. dma_unmap_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
  45. if (IS_CCM(rctx->flags)) {
  46. if (req->assoclen) {
  47. sg_free_table(&rctx->src_tbl);
  48. if (diff_dst)
  49. sg_free_table(&rctx->dst_tbl);
  50. } else {
  51. if (!(IS_DECRYPT(rctx->flags) && !diff_dst))
  52. sg_free_table(&rctx->dst_tbl);
  53. }
  54. } else {
  55. sg_free_table(&rctx->dst_tbl);
  56. }
  57. error = qce_check_status(qce, &status);
  58. if (error < 0 && (error != -EBADMSG))
  59. dev_err(qce->dev, "aead operation error (%x)\n", status);
  60. if (IS_ENCRYPT(rctx->flags)) {
  61. totallen = req->cryptlen + req->assoclen;
  62. if (IS_CCM(rctx->flags))
  63. scatterwalk_map_and_copy(rctx->ccmresult_buf, req->dst,
  64. totallen, ctx->authsize, 1);
  65. else
  66. scatterwalk_map_and_copy(result_buf->auth_iv, req->dst,
  67. totallen, ctx->authsize, 1);
  68. } else if (!IS_CCM(rctx->flags)) {
  69. totallen = req->cryptlen + req->assoclen - ctx->authsize;
  70. scatterwalk_map_and_copy(tag, req->src, totallen, ctx->authsize, 0);
  71. ret = memcmp(result_buf->auth_iv, tag, ctx->authsize);
  72. if (ret) {
  73. pr_err("Bad message error\n");
  74. error = -EBADMSG;
  75. }
  76. }
  77. qce->async_req_done(qce, error);
  78. }
  79. static struct scatterlist *
  80. qce_aead_prepare_result_buf(struct sg_table *tbl, struct aead_request *req)
  81. {
  82. struct qce_aead_reqctx *rctx = aead_request_ctx(req);
  83. struct qce_alg_template *tmpl = to_aead_tmpl(crypto_aead_reqtfm(req));
  84. struct qce_device *qce = tmpl->qce;
  85. sg_init_one(&rctx->result_sg, qce->dma.result_buf, QCE_RESULT_BUF_SZ);
  86. return qce_sgtable_add(tbl, &rctx->result_sg, QCE_RESULT_BUF_SZ);
  87. }
  88. static struct scatterlist *
  89. qce_aead_prepare_ccm_result_buf(struct sg_table *tbl, struct aead_request *req)
  90. {
  91. struct qce_aead_reqctx *rctx = aead_request_ctx(req);
  92. sg_init_one(&rctx->result_sg, rctx->ccmresult_buf, QCE_BAM_BURST_SIZE);
  93. return qce_sgtable_add(tbl, &rctx->result_sg, QCE_BAM_BURST_SIZE);
  94. }
  95. static struct scatterlist *
  96. qce_aead_prepare_dst_buf(struct aead_request *req)
  97. {
  98. struct qce_aead_reqctx *rctx = aead_request_ctx(req);
  99. struct qce_alg_template *tmpl = to_aead_tmpl(crypto_aead_reqtfm(req));
  100. struct qce_device *qce = tmpl->qce;
  101. struct scatterlist *sg, *msg_sg, __sg[2];
  102. gfp_t gfp;
  103. unsigned int assoclen = req->assoclen;
  104. unsigned int totallen;
  105. int ret;
  106. totallen = rctx->cryptlen + assoclen;
  107. rctx->dst_nents = sg_nents_for_len(req->dst, totallen);
  108. if (rctx->dst_nents < 0) {
  109. dev_err(qce->dev, "Invalid numbers of dst SG.\n");
  110. return ERR_PTR(-EINVAL);
  111. }
  112. if (IS_CCM(rctx->flags))
  113. rctx->dst_nents += 2;
  114. else
  115. rctx->dst_nents += 1;
  116. gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  117. GFP_KERNEL : GFP_ATOMIC;
  118. ret = sg_alloc_table(&rctx->dst_tbl, rctx->dst_nents, gfp);
  119. if (ret)
  120. return ERR_PTR(ret);
  121. if (IS_CCM(rctx->flags) && assoclen) {
  122. /* Get the dst buffer */
  123. msg_sg = scatterwalk_ffwd(__sg, req->dst, assoclen);
  124. sg = qce_sgtable_add(&rctx->dst_tbl, &rctx->adata_sg,
  125. rctx->assoclen);
  126. if (IS_ERR(sg)) {
  127. ret = PTR_ERR(sg);
  128. goto dst_tbl_free;
  129. }
  130. /* dst buffer */
  131. sg = qce_sgtable_add(&rctx->dst_tbl, msg_sg, rctx->cryptlen);
  132. if (IS_ERR(sg)) {
  133. ret = PTR_ERR(sg);
  134. goto dst_tbl_free;
  135. }
  136. totallen = rctx->cryptlen + rctx->assoclen;
  137. } else {
  138. if (totallen) {
  139. sg = qce_sgtable_add(&rctx->dst_tbl, req->dst, totallen);
  140. if (IS_ERR(sg))
  141. goto dst_tbl_free;
  142. }
  143. }
  144. if (IS_CCM(rctx->flags))
  145. sg = qce_aead_prepare_ccm_result_buf(&rctx->dst_tbl, req);
  146. else
  147. sg = qce_aead_prepare_result_buf(&rctx->dst_tbl, req);
  148. if (IS_ERR(sg))
  149. goto dst_tbl_free;
  150. sg_mark_end(sg);
  151. rctx->dst_sg = rctx->dst_tbl.sgl;
  152. rctx->dst_nents = sg_nents_for_len(rctx->dst_sg, totallen) + 1;
  153. return sg;
  154. dst_tbl_free:
  155. sg_free_table(&rctx->dst_tbl);
  156. return sg;
  157. }
  158. static int
  159. qce_aead_ccm_prepare_buf_assoclen(struct aead_request *req)
  160. {
  161. struct scatterlist *sg, *msg_sg, __sg[2];
  162. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  163. struct qce_aead_reqctx *rctx = aead_request_ctx(req);
  164. struct qce_aead_ctx *ctx = crypto_aead_ctx(tfm);
  165. unsigned int assoclen = rctx->assoclen;
  166. unsigned int adata_header_len, cryptlen, totallen;
  167. gfp_t gfp;
  168. bool diff_dst;
  169. int ret;
  170. if (IS_DECRYPT(rctx->flags))
  171. cryptlen = rctx->cryptlen + ctx->authsize;
  172. else
  173. cryptlen = rctx->cryptlen;
  174. totallen = cryptlen + req->assoclen;
  175. /* Get the msg */
  176. msg_sg = scatterwalk_ffwd(__sg, req->src, req->assoclen);
  177. rctx->adata = kzalloc((ALIGN(assoclen, 16) + MAX_CCM_ADATA_HEADER_LEN) *
  178. sizeof(unsigned char), GFP_ATOMIC);
  179. if (!rctx->adata)
  180. return -ENOMEM;
  181. /*
  182. * Format associated data (RFC3610 and NIST 800-38C)
  183. * Even though specification allows for AAD to be up to 2^64 - 1 bytes,
  184. * the assoclen field in aead_request is unsigned int and thus limits
  185. * the AAD to be up to 2^32 - 1 bytes. So we handle only two scenarios
  186. * while forming the header for AAD.
  187. */
  188. if (assoclen < 0xff00) {
  189. adata_header_len = 2;
  190. *(__be16 *)rctx->adata = cpu_to_be16(assoclen);
  191. } else {
  192. adata_header_len = 6;
  193. *(__be16 *)rctx->adata = cpu_to_be16(0xfffe);
  194. *(__be32 *)(rctx->adata + 2) = cpu_to_be32(assoclen);
  195. }
  196. /* Copy the associated data */
  197. if (sg_copy_to_buffer(req->src, sg_nents_for_len(req->src, assoclen),
  198. rctx->adata + adata_header_len,
  199. assoclen) != assoclen)
  200. return -EINVAL;
  201. /* Pad associated data to block size */
  202. rctx->assoclen = ALIGN(assoclen + adata_header_len, 16);
  203. diff_dst = (req->src != req->dst) ? true : false;
  204. if (diff_dst)
  205. rctx->src_nents = sg_nents_for_len(req->src, totallen) + 1;
  206. else
  207. rctx->src_nents = sg_nents_for_len(req->src, totallen) + 2;
  208. gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL : GFP_ATOMIC;
  209. ret = sg_alloc_table(&rctx->src_tbl, rctx->src_nents, gfp);
  210. if (ret)
  211. return ret;
  212. /* Associated Data */
  213. sg_init_one(&rctx->adata_sg, rctx->adata, rctx->assoclen);
  214. sg = qce_sgtable_add(&rctx->src_tbl, &rctx->adata_sg,
  215. rctx->assoclen);
  216. if (IS_ERR(sg)) {
  217. ret = PTR_ERR(sg);
  218. goto err_free;
  219. }
  220. /* src msg */
  221. sg = qce_sgtable_add(&rctx->src_tbl, msg_sg, cryptlen);
  222. if (IS_ERR(sg)) {
  223. ret = PTR_ERR(sg);
  224. goto err_free;
  225. }
  226. if (!diff_dst) {
  227. /*
  228. * For decrypt, when src and dst buffers are same, there is already space
  229. * in the buffer for padded 0's which is output in lieu of
  230. * the MAC that is input. So skip the below.
  231. */
  232. if (!IS_DECRYPT(rctx->flags)) {
  233. sg = qce_aead_prepare_ccm_result_buf(&rctx->src_tbl, req);
  234. if (IS_ERR(sg)) {
  235. ret = PTR_ERR(sg);
  236. goto err_free;
  237. }
  238. }
  239. }
  240. sg_mark_end(sg);
  241. rctx->src_sg = rctx->src_tbl.sgl;
  242. totallen = cryptlen + rctx->assoclen;
  243. rctx->src_nents = sg_nents_for_len(rctx->src_sg, totallen);
  244. if (diff_dst) {
  245. sg = qce_aead_prepare_dst_buf(req);
  246. if (IS_ERR(sg)) {
  247. ret = PTR_ERR(sg);
  248. goto err_free;
  249. }
  250. } else {
  251. if (IS_ENCRYPT(rctx->flags))
  252. rctx->dst_nents = rctx->src_nents + 1;
  253. else
  254. rctx->dst_nents = rctx->src_nents;
  255. rctx->dst_sg = rctx->src_sg;
  256. }
  257. return 0;
  258. err_free:
  259. sg_free_table(&rctx->src_tbl);
  260. return ret;
  261. }
  262. static int qce_aead_prepare_buf(struct aead_request *req)
  263. {
  264. struct qce_aead_reqctx *rctx = aead_request_ctx(req);
  265. struct qce_alg_template *tmpl = to_aead_tmpl(crypto_aead_reqtfm(req));
  266. struct qce_device *qce = tmpl->qce;
  267. struct scatterlist *sg;
  268. bool diff_dst = (req->src != req->dst) ? true : false;
  269. unsigned int totallen;
  270. totallen = rctx->cryptlen + rctx->assoclen;
  271. sg = qce_aead_prepare_dst_buf(req);
  272. if (IS_ERR(sg))
  273. return PTR_ERR(sg);
  274. if (diff_dst) {
  275. rctx->src_nents = sg_nents_for_len(req->src, totallen);
  276. if (rctx->src_nents < 0) {
  277. dev_err(qce->dev, "Invalid numbers of src SG.\n");
  278. return -EINVAL;
  279. }
  280. rctx->src_sg = req->src;
  281. } else {
  282. rctx->src_nents = rctx->dst_nents - 1;
  283. rctx->src_sg = rctx->dst_sg;
  284. }
  285. return 0;
  286. }
  287. static int qce_aead_ccm_prepare_buf(struct aead_request *req)
  288. {
  289. struct qce_aead_reqctx *rctx = aead_request_ctx(req);
  290. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  291. struct qce_aead_ctx *ctx = crypto_aead_ctx(tfm);
  292. struct scatterlist *sg;
  293. bool diff_dst = (req->src != req->dst) ? true : false;
  294. unsigned int cryptlen;
  295. if (rctx->assoclen)
  296. return qce_aead_ccm_prepare_buf_assoclen(req);
  297. if (IS_ENCRYPT(rctx->flags))
  298. return qce_aead_prepare_buf(req);
  299. cryptlen = rctx->cryptlen + ctx->authsize;
  300. if (diff_dst) {
  301. rctx->src_nents = sg_nents_for_len(req->src, cryptlen);
  302. rctx->src_sg = req->src;
  303. sg = qce_aead_prepare_dst_buf(req);
  304. if (IS_ERR(sg))
  305. return PTR_ERR(sg);
  306. } else {
  307. rctx->src_nents = sg_nents_for_len(req->src, cryptlen);
  308. rctx->src_sg = req->src;
  309. rctx->dst_nents = rctx->src_nents;
  310. rctx->dst_sg = rctx->src_sg;
  311. }
  312. return 0;
  313. }
  314. static int qce_aead_create_ccm_nonce(struct qce_aead_reqctx *rctx, struct qce_aead_ctx *ctx)
  315. {
  316. unsigned int msglen_size, ivsize;
  317. u8 msg_len[4];
  318. int i;
  319. if (!rctx || !rctx->iv)
  320. return -EINVAL;
  321. msglen_size = rctx->iv[0] + 1;
  322. /* Verify that msg len size is valid */
  323. if (msglen_size < 2 || msglen_size > 8)
  324. return -EINVAL;
  325. ivsize = rctx->ivsize;
  326. /*
  327. * Clear the msglen bytes in IV.
  328. * Else the h/w engine and nonce will use any stray value pending there.
  329. */
  330. if (!IS_CCM_RFC4309(rctx->flags)) {
  331. for (i = 0; i < msglen_size; i++)
  332. rctx->iv[ivsize - i - 1] = 0;
  333. }
  334. /*
  335. * The crypto framework encodes cryptlen as unsigned int. Thus, even though
  336. * spec allows for upto 8 bytes to encode msg_len only 4 bytes are needed.
  337. */
  338. if (msglen_size > 4)
  339. msglen_size = 4;
  340. memcpy(&msg_len[0], &rctx->cryptlen, 4);
  341. memcpy(&rctx->ccm_nonce[0], rctx->iv, rctx->ivsize);
  342. if (rctx->assoclen)
  343. rctx->ccm_nonce[0] |= 1 << CCM_NONCE_ADATA_SHIFT;
  344. rctx->ccm_nonce[0] |= ((ctx->authsize - 2) / 2) <<
  345. CCM_NONCE_AUTHSIZE_SHIFT;
  346. for (i = 0; i < msglen_size; i++)
  347. rctx->ccm_nonce[QCE_MAX_NONCE - i - 1] = msg_len[i];
  348. return 0;
  349. }
  350. static int
  351. qce_aead_async_req_handle(struct crypto_async_request *async_req)
  352. {
  353. struct aead_request *req = aead_request_cast(async_req);
  354. struct qce_aead_reqctx *rctx = aead_request_ctx(req);
  355. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  356. struct qce_aead_ctx *ctx = crypto_tfm_ctx(async_req->tfm);
  357. struct qce_alg_template *tmpl = to_aead_tmpl(crypto_aead_reqtfm(req));
  358. struct qce_device *qce = tmpl->qce;
  359. enum dma_data_direction dir_src, dir_dst;
  360. bool diff_dst;
  361. int dst_nents, src_nents, ret;
  362. if (IS_CCM_RFC4309(rctx->flags)) {
  363. memset(rctx->ccm_rfc4309_iv, 0, QCE_MAX_IV_SIZE);
  364. rctx->ccm_rfc4309_iv[0] = 3;
  365. memcpy(&rctx->ccm_rfc4309_iv[1], ctx->ccm4309_salt, QCE_CCM4309_SALT_SIZE);
  366. memcpy(&rctx->ccm_rfc4309_iv[4], req->iv, 8);
  367. rctx->iv = rctx->ccm_rfc4309_iv;
  368. rctx->ivsize = AES_BLOCK_SIZE;
  369. } else {
  370. rctx->iv = req->iv;
  371. rctx->ivsize = crypto_aead_ivsize(tfm);
  372. }
  373. if (IS_CCM_RFC4309(rctx->flags))
  374. rctx->assoclen = req->assoclen - 8;
  375. else
  376. rctx->assoclen = req->assoclen;
  377. diff_dst = (req->src != req->dst) ? true : false;
  378. dir_src = diff_dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL;
  379. dir_dst = diff_dst ? DMA_FROM_DEVICE : DMA_BIDIRECTIONAL;
  380. if (IS_CCM(rctx->flags)) {
  381. ret = qce_aead_create_ccm_nonce(rctx, ctx);
  382. if (ret)
  383. return ret;
  384. }
  385. if (IS_CCM(rctx->flags))
  386. ret = qce_aead_ccm_prepare_buf(req);
  387. else
  388. ret = qce_aead_prepare_buf(req);
  389. if (ret)
  390. return ret;
  391. dst_nents = dma_map_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
  392. if (!dst_nents) {
  393. ret = -EIO;
  394. goto error_free;
  395. }
  396. if (diff_dst) {
  397. src_nents = dma_map_sg(qce->dev, rctx->src_sg, rctx->src_nents, dir_src);
  398. if (src_nents < 0) {
  399. ret = src_nents;
  400. goto error_unmap_dst;
  401. }
  402. } else {
  403. if (IS_CCM(rctx->flags) && IS_DECRYPT(rctx->flags))
  404. src_nents = dst_nents;
  405. else
  406. src_nents = dst_nents - 1;
  407. }
  408. ret = qce_dma_prep_sgs(&qce->dma, rctx->src_sg, src_nents, rctx->dst_sg, dst_nents,
  409. qce_aead_done, async_req);
  410. if (ret)
  411. goto error_unmap_src;
  412. qce_dma_issue_pending(&qce->dma);
  413. ret = qce_start(async_req, tmpl->crypto_alg_type);
  414. if (ret)
  415. goto error_terminate;
  416. return 0;
  417. error_terminate:
  418. qce_dma_terminate_all(&qce->dma);
  419. error_unmap_src:
  420. if (diff_dst)
  421. dma_unmap_sg(qce->dev, req->src, rctx->src_nents, dir_src);
  422. error_unmap_dst:
  423. dma_unmap_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
  424. error_free:
  425. if (IS_CCM(rctx->flags) && rctx->assoclen) {
  426. sg_free_table(&rctx->src_tbl);
  427. if (diff_dst)
  428. sg_free_table(&rctx->dst_tbl);
  429. } else {
  430. sg_free_table(&rctx->dst_tbl);
  431. }
  432. return ret;
  433. }
  434. static int qce_aead_crypt(struct aead_request *req, int encrypt)
  435. {
  436. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  437. struct qce_aead_reqctx *rctx = aead_request_ctx(req);
  438. struct qce_aead_ctx *ctx = crypto_aead_ctx(tfm);
  439. struct qce_alg_template *tmpl = to_aead_tmpl(tfm);
  440. unsigned int blocksize = crypto_aead_blocksize(tfm);
  441. rctx->flags = tmpl->alg_flags;
  442. rctx->flags |= encrypt ? QCE_ENCRYPT : QCE_DECRYPT;
  443. if (encrypt)
  444. rctx->cryptlen = req->cryptlen;
  445. else
  446. rctx->cryptlen = req->cryptlen - ctx->authsize;
  447. /* CE does not handle 0 length messages */
  448. if (!rctx->cryptlen) {
  449. if (!(IS_CCM(rctx->flags) && IS_DECRYPT(rctx->flags)))
  450. ctx->need_fallback = true;
  451. }
  452. /* If fallback is needed, schedule and exit */
  453. if (ctx->need_fallback) {
  454. /* Reset need_fallback in case the same ctx is used for another transaction */
  455. ctx->need_fallback = false;
  456. aead_request_set_tfm(&rctx->fallback_req, ctx->fallback);
  457. aead_request_set_callback(&rctx->fallback_req, req->base.flags,
  458. req->base.complete, req->base.data);
  459. aead_request_set_crypt(&rctx->fallback_req, req->src,
  460. req->dst, req->cryptlen, req->iv);
  461. aead_request_set_ad(&rctx->fallback_req, req->assoclen);
  462. return encrypt ? crypto_aead_encrypt(&rctx->fallback_req) :
  463. crypto_aead_decrypt(&rctx->fallback_req);
  464. }
  465. /*
  466. * CBC algorithms require message lengths to be
  467. * multiples of block size.
  468. */
  469. if (IS_CBC(rctx->flags) && !IS_ALIGNED(rctx->cryptlen, blocksize))
  470. return -EINVAL;
  471. /* RFC4309 supported AAD size 16 bytes/20 bytes */
  472. if (IS_CCM_RFC4309(rctx->flags))
  473. if (crypto_ipsec_check_assoclen(req->assoclen))
  474. return -EINVAL;
  475. return tmpl->qce->async_req_enqueue(tmpl->qce, &req->base);
  476. }
  477. static int qce_aead_encrypt(struct aead_request *req)
  478. {
  479. return qce_aead_crypt(req, 1);
  480. }
  481. static int qce_aead_decrypt(struct aead_request *req)
  482. {
  483. return qce_aead_crypt(req, 0);
  484. }
  485. static int qce_aead_ccm_setkey(struct crypto_aead *tfm, const u8 *key,
  486. unsigned int keylen)
  487. {
  488. struct qce_aead_ctx *ctx = crypto_aead_ctx(tfm);
  489. unsigned long flags = to_aead_tmpl(tfm)->alg_flags;
  490. if (IS_CCM_RFC4309(flags)) {
  491. if (keylen < QCE_CCM4309_SALT_SIZE)
  492. return -EINVAL;
  493. keylen -= QCE_CCM4309_SALT_SIZE;
  494. memcpy(ctx->ccm4309_salt, key + keylen, QCE_CCM4309_SALT_SIZE);
  495. }
  496. if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_256 && keylen != AES_KEYSIZE_192)
  497. return -EINVAL;
  498. ctx->enc_keylen = keylen;
  499. ctx->auth_keylen = keylen;
  500. memcpy(ctx->enc_key, key, keylen);
  501. memcpy(ctx->auth_key, key, keylen);
  502. if (keylen == AES_KEYSIZE_192)
  503. ctx->need_fallback = true;
  504. return IS_CCM_RFC4309(flags) ?
  505. crypto_aead_setkey(ctx->fallback, key, keylen + QCE_CCM4309_SALT_SIZE) :
  506. crypto_aead_setkey(ctx->fallback, key, keylen);
  507. }
  508. static int qce_aead_setkey(struct crypto_aead *tfm, const u8 *key, unsigned int keylen)
  509. {
  510. struct qce_aead_ctx *ctx = crypto_aead_ctx(tfm);
  511. struct crypto_authenc_keys authenc_keys;
  512. unsigned long flags = to_aead_tmpl(tfm)->alg_flags;
  513. u32 _key[6];
  514. int err;
  515. err = crypto_authenc_extractkeys(&authenc_keys, key, keylen);
  516. if (err)
  517. return err;
  518. if (authenc_keys.enckeylen > QCE_MAX_KEY_SIZE ||
  519. authenc_keys.authkeylen > QCE_MAX_KEY_SIZE)
  520. return -EINVAL;
  521. if (IS_DES(flags)) {
  522. err = verify_aead_des_key(tfm, authenc_keys.enckey, authenc_keys.enckeylen);
  523. if (err)
  524. return err;
  525. } else if (IS_3DES(flags)) {
  526. err = verify_aead_des3_key(tfm, authenc_keys.enckey, authenc_keys.enckeylen);
  527. if (err)
  528. return err;
  529. /*
  530. * The crypto engine does not support any two keys
  531. * being the same for triple des algorithms. The
  532. * verify_skcipher_des3_key does not check for all the
  533. * below conditions. Schedule fallback in this case.
  534. */
  535. memcpy(_key, authenc_keys.enckey, DES3_EDE_KEY_SIZE);
  536. if (!((_key[0] ^ _key[2]) | (_key[1] ^ _key[3])) ||
  537. !((_key[2] ^ _key[4]) | (_key[3] ^ _key[5])) ||
  538. !((_key[0] ^ _key[4]) | (_key[1] ^ _key[5])))
  539. ctx->need_fallback = true;
  540. } else if (IS_AES(flags)) {
  541. /* No random key sizes */
  542. if (authenc_keys.enckeylen != AES_KEYSIZE_128 &&
  543. authenc_keys.enckeylen != AES_KEYSIZE_192 &&
  544. authenc_keys.enckeylen != AES_KEYSIZE_256)
  545. return -EINVAL;
  546. if (authenc_keys.enckeylen == AES_KEYSIZE_192)
  547. ctx->need_fallback = true;
  548. }
  549. ctx->enc_keylen = authenc_keys.enckeylen;
  550. ctx->auth_keylen = authenc_keys.authkeylen;
  551. memcpy(ctx->enc_key, authenc_keys.enckey, authenc_keys.enckeylen);
  552. memset(ctx->auth_key, 0, sizeof(ctx->auth_key));
  553. memcpy(ctx->auth_key, authenc_keys.authkey, authenc_keys.authkeylen);
  554. return crypto_aead_setkey(ctx->fallback, key, keylen);
  555. }
  556. static int qce_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
  557. {
  558. struct qce_aead_ctx *ctx = crypto_aead_ctx(tfm);
  559. unsigned long flags = to_aead_tmpl(tfm)->alg_flags;
  560. if (IS_CCM(flags)) {
  561. if (authsize < 4 || authsize > 16 || authsize % 2)
  562. return -EINVAL;
  563. if (IS_CCM_RFC4309(flags) && (authsize < 8 || authsize % 4))
  564. return -EINVAL;
  565. }
  566. ctx->authsize = authsize;
  567. return crypto_aead_setauthsize(ctx->fallback, authsize);
  568. }
  569. static int qce_aead_init(struct crypto_aead *tfm)
  570. {
  571. struct qce_aead_ctx *ctx = crypto_aead_ctx(tfm);
  572. ctx->need_fallback = false;
  573. ctx->fallback = crypto_alloc_aead(crypto_tfm_alg_name(&tfm->base),
  574. 0, CRYPTO_ALG_NEED_FALLBACK);
  575. if (IS_ERR(ctx->fallback))
  576. return PTR_ERR(ctx->fallback);
  577. crypto_aead_set_reqsize(tfm, sizeof(struct qce_aead_reqctx) +
  578. crypto_aead_reqsize(ctx->fallback));
  579. return 0;
  580. }
  581. static void qce_aead_exit(struct crypto_aead *tfm)
  582. {
  583. struct qce_aead_ctx *ctx = crypto_aead_ctx(tfm);
  584. crypto_free_aead(ctx->fallback);
  585. }
  586. struct qce_aead_def {
  587. unsigned long flags;
  588. const char *name;
  589. const char *drv_name;
  590. unsigned int blocksize;
  591. unsigned int chunksize;
  592. unsigned int ivsize;
  593. unsigned int maxauthsize;
  594. };
  595. static const struct qce_aead_def aead_def[] = {
  596. {
  597. .flags = QCE_ALG_DES | QCE_MODE_CBC | QCE_HASH_SHA1_HMAC,
  598. .name = "authenc(hmac(sha1),cbc(des))",
  599. .drv_name = "authenc-hmac-sha1-cbc-des-qce",
  600. .blocksize = DES_BLOCK_SIZE,
  601. .ivsize = DES_BLOCK_SIZE,
  602. .maxauthsize = SHA1_DIGEST_SIZE,
  603. },
  604. {
  605. .flags = QCE_ALG_3DES | QCE_MODE_CBC | QCE_HASH_SHA1_HMAC,
  606. .name = "authenc(hmac(sha1),cbc(des3_ede))",
  607. .drv_name = "authenc-hmac-sha1-cbc-3des-qce",
  608. .blocksize = DES3_EDE_BLOCK_SIZE,
  609. .ivsize = DES3_EDE_BLOCK_SIZE,
  610. .maxauthsize = SHA1_DIGEST_SIZE,
  611. },
  612. {
  613. .flags = QCE_ALG_DES | QCE_MODE_CBC | QCE_HASH_SHA256_HMAC,
  614. .name = "authenc(hmac(sha256),cbc(des))",
  615. .drv_name = "authenc-hmac-sha256-cbc-des-qce",
  616. .blocksize = DES_BLOCK_SIZE,
  617. .ivsize = DES_BLOCK_SIZE,
  618. .maxauthsize = SHA256_DIGEST_SIZE,
  619. },
  620. {
  621. .flags = QCE_ALG_3DES | QCE_MODE_CBC | QCE_HASH_SHA256_HMAC,
  622. .name = "authenc(hmac(sha256),cbc(des3_ede))",
  623. .drv_name = "authenc-hmac-sha256-cbc-3des-qce",
  624. .blocksize = DES3_EDE_BLOCK_SIZE,
  625. .ivsize = DES3_EDE_BLOCK_SIZE,
  626. .maxauthsize = SHA256_DIGEST_SIZE,
  627. },
  628. {
  629. .flags = QCE_ALG_AES | QCE_MODE_CBC | QCE_HASH_SHA256_HMAC,
  630. .name = "authenc(hmac(sha256),cbc(aes))",
  631. .drv_name = "authenc-hmac-sha256-cbc-aes-qce",
  632. .blocksize = AES_BLOCK_SIZE,
  633. .ivsize = AES_BLOCK_SIZE,
  634. .maxauthsize = SHA256_DIGEST_SIZE,
  635. },
  636. {
  637. .flags = QCE_ALG_AES | QCE_MODE_CCM,
  638. .name = "ccm(aes)",
  639. .drv_name = "ccm-aes-qce",
  640. .blocksize = 1,
  641. .ivsize = AES_BLOCK_SIZE,
  642. .maxauthsize = AES_BLOCK_SIZE,
  643. },
  644. {
  645. .flags = QCE_ALG_AES | QCE_MODE_CCM | QCE_MODE_CCM_RFC4309,
  646. .name = "rfc4309(ccm(aes))",
  647. .drv_name = "rfc4309-ccm-aes-qce",
  648. .blocksize = 1,
  649. .ivsize = 8,
  650. .maxauthsize = AES_BLOCK_SIZE,
  651. },
  652. };
  653. static int qce_aead_register_one(const struct qce_aead_def *def, struct qce_device *qce)
  654. {
  655. struct qce_alg_template *tmpl;
  656. struct aead_alg *alg;
  657. int ret;
  658. tmpl = kzalloc(sizeof(*tmpl), GFP_KERNEL);
  659. if (!tmpl)
  660. return -ENOMEM;
  661. alg = &tmpl->alg.aead;
  662. snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  663. snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  664. def->drv_name);
  665. alg->base.cra_blocksize = def->blocksize;
  666. alg->chunksize = def->chunksize;
  667. alg->ivsize = def->ivsize;
  668. alg->maxauthsize = def->maxauthsize;
  669. if (IS_CCM(def->flags))
  670. alg->setkey = qce_aead_ccm_setkey;
  671. else
  672. alg->setkey = qce_aead_setkey;
  673. alg->setauthsize = qce_aead_setauthsize;
  674. alg->encrypt = qce_aead_encrypt;
  675. alg->decrypt = qce_aead_decrypt;
  676. alg->init = qce_aead_init;
  677. alg->exit = qce_aead_exit;
  678. alg->base.cra_priority = 300;
  679. alg->base.cra_flags = CRYPTO_ALG_ASYNC |
  680. CRYPTO_ALG_ALLOCATES_MEMORY |
  681. CRYPTO_ALG_KERN_DRIVER_ONLY |
  682. CRYPTO_ALG_NEED_FALLBACK;
  683. alg->base.cra_ctxsize = sizeof(struct qce_aead_ctx);
  684. alg->base.cra_alignmask = 0;
  685. alg->base.cra_module = THIS_MODULE;
  686. INIT_LIST_HEAD(&tmpl->entry);
  687. tmpl->crypto_alg_type = CRYPTO_ALG_TYPE_AEAD;
  688. tmpl->alg_flags = def->flags;
  689. tmpl->qce = qce;
  690. ret = crypto_register_aead(alg);
  691. if (ret) {
  692. dev_err(qce->dev, "%s registration failed\n", alg->base.cra_name);
  693. kfree(tmpl);
  694. return ret;
  695. }
  696. list_add_tail(&tmpl->entry, &aead_algs);
  697. dev_dbg(qce->dev, "%s is registered\n", alg->base.cra_name);
  698. return 0;
  699. }
  700. static void qce_aead_unregister(struct qce_device *qce)
  701. {
  702. struct qce_alg_template *tmpl, *n;
  703. list_for_each_entry_safe(tmpl, n, &aead_algs, entry) {
  704. crypto_unregister_aead(&tmpl->alg.aead);
  705. list_del(&tmpl->entry);
  706. kfree(tmpl);
  707. }
  708. }
  709. static int qce_aead_register(struct qce_device *qce)
  710. {
  711. int ret, i;
  712. for (i = 0; i < ARRAY_SIZE(aead_def); i++) {
  713. ret = qce_aead_register_one(&aead_def[i], qce);
  714. if (ret)
  715. goto err;
  716. }
  717. return 0;
  718. err:
  719. qce_aead_unregister(qce);
  720. return ret;
  721. }
  722. const struct qce_algo_ops aead_ops = {
  723. .type = CRYPTO_ALG_TYPE_AEAD,
  724. .register_algs = qce_aead_register,
  725. .unregister_algs = qce_aead_unregister,
  726. .async_req_handle = qce_aead_async_req_handle,
  727. };