skcipher.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/device.h>
  6. #include <linux/dma-mapping.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/moduleparam.h>
  9. #include <linux/types.h>
  10. #include <linux/errno.h>
  11. #include <crypto/aes.h>
  12. #include <crypto/internal/des.h>
  13. #include <crypto/internal/skcipher.h>
  14. #include "cipher.h"
  15. static unsigned int aes_sw_max_len = CONFIG_CRYPTO_DEV_QCE_SW_MAX_LEN;
  16. module_param(aes_sw_max_len, uint, 0644);
  17. MODULE_PARM_DESC(aes_sw_max_len,
  18. "Only use hardware for AES requests larger than this "
  19. "[0=always use hardware; anything <16 breaks AES-GCM; default="
  20. __stringify(CONFIG_CRYPTO_DEV_QCE_SW_MAX_LEN)"]");
  21. static LIST_HEAD(skcipher_algs);
  22. static void qce_skcipher_done(void *data)
  23. {
  24. struct crypto_async_request *async_req = data;
  25. struct skcipher_request *req = skcipher_request_cast(async_req);
  26. struct qce_cipher_reqctx *rctx = skcipher_request_ctx(req);
  27. struct qce_alg_template *tmpl = to_cipher_tmpl(crypto_skcipher_reqtfm(req));
  28. struct qce_device *qce = tmpl->qce;
  29. struct qce_result_dump *result_buf = qce->dma.result_buf;
  30. enum dma_data_direction dir_src, dir_dst;
  31. u32 status;
  32. int error;
  33. bool diff_dst;
  34. diff_dst = (req->src != req->dst) ? true : false;
  35. dir_src = diff_dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL;
  36. dir_dst = diff_dst ? DMA_FROM_DEVICE : DMA_BIDIRECTIONAL;
  37. error = qce_dma_terminate_all(&qce->dma);
  38. if (error)
  39. dev_dbg(qce->dev, "skcipher dma termination error (%d)\n",
  40. error);
  41. if (diff_dst)
  42. dma_unmap_sg(qce->dev, rctx->src_sg, rctx->src_nents, dir_src);
  43. dma_unmap_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
  44. sg_free_table(&rctx->dst_tbl);
  45. error = qce_check_status(qce, &status);
  46. if (error < 0)
  47. dev_dbg(qce->dev, "skcipher operation error (%x)\n", status);
  48. memcpy(rctx->iv, result_buf->encr_cntr_iv, rctx->ivsize);
  49. qce->async_req_done(tmpl->qce, error);
  50. }
  51. static int
  52. qce_skcipher_async_req_handle(struct crypto_async_request *async_req)
  53. {
  54. struct skcipher_request *req = skcipher_request_cast(async_req);
  55. struct qce_cipher_reqctx *rctx = skcipher_request_ctx(req);
  56. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  57. struct qce_alg_template *tmpl = to_cipher_tmpl(crypto_skcipher_reqtfm(req));
  58. struct qce_device *qce = tmpl->qce;
  59. enum dma_data_direction dir_src, dir_dst;
  60. struct scatterlist *sg;
  61. bool diff_dst;
  62. gfp_t gfp;
  63. int dst_nents, src_nents, ret;
  64. rctx->iv = req->iv;
  65. rctx->ivsize = crypto_skcipher_ivsize(skcipher);
  66. rctx->cryptlen = req->cryptlen;
  67. diff_dst = (req->src != req->dst) ? true : false;
  68. dir_src = diff_dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL;
  69. dir_dst = diff_dst ? DMA_FROM_DEVICE : DMA_BIDIRECTIONAL;
  70. rctx->src_nents = sg_nents_for_len(req->src, req->cryptlen);
  71. if (diff_dst)
  72. rctx->dst_nents = sg_nents_for_len(req->dst, req->cryptlen);
  73. else
  74. rctx->dst_nents = rctx->src_nents;
  75. if (rctx->src_nents < 0) {
  76. dev_err(qce->dev, "Invalid numbers of src SG.\n");
  77. return rctx->src_nents;
  78. }
  79. if (rctx->dst_nents < 0) {
  80. dev_err(qce->dev, "Invalid numbers of dst SG.\n");
  81. return -rctx->dst_nents;
  82. }
  83. rctx->dst_nents += 1;
  84. gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  85. GFP_KERNEL : GFP_ATOMIC;
  86. ret = sg_alloc_table(&rctx->dst_tbl, rctx->dst_nents, gfp);
  87. if (ret)
  88. return ret;
  89. sg_init_one(&rctx->result_sg, qce->dma.result_buf, QCE_RESULT_BUF_SZ);
  90. sg = qce_sgtable_add(&rctx->dst_tbl, req->dst, req->cryptlen);
  91. if (IS_ERR(sg)) {
  92. ret = PTR_ERR(sg);
  93. goto error_free;
  94. }
  95. sg = qce_sgtable_add(&rctx->dst_tbl, &rctx->result_sg,
  96. QCE_RESULT_BUF_SZ);
  97. if (IS_ERR(sg)) {
  98. ret = PTR_ERR(sg);
  99. goto error_free;
  100. }
  101. sg_mark_end(sg);
  102. rctx->dst_sg = rctx->dst_tbl.sgl;
  103. dst_nents = dma_map_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
  104. if (!dst_nents) {
  105. ret = -EIO;
  106. goto error_free;
  107. }
  108. if (diff_dst) {
  109. src_nents = dma_map_sg(qce->dev, req->src, rctx->src_nents, dir_src);
  110. if (!src_nents) {
  111. ret = -EIO;
  112. goto error_unmap_dst;
  113. }
  114. rctx->src_sg = req->src;
  115. } else {
  116. rctx->src_sg = rctx->dst_sg;
  117. src_nents = dst_nents - 1;
  118. }
  119. ret = qce_dma_prep_sgs(&qce->dma, rctx->src_sg, src_nents,
  120. rctx->dst_sg, dst_nents,
  121. qce_skcipher_done, async_req);
  122. if (ret)
  123. goto error_unmap_src;
  124. qce_dma_issue_pending(&qce->dma);
  125. ret = qce_start(async_req, tmpl->crypto_alg_type);
  126. if (ret)
  127. goto error_terminate;
  128. return 0;
  129. error_terminate:
  130. qce_dma_terminate_all(&qce->dma);
  131. error_unmap_src:
  132. if (diff_dst)
  133. dma_unmap_sg(qce->dev, req->src, rctx->src_nents, dir_src);
  134. error_unmap_dst:
  135. dma_unmap_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
  136. error_free:
  137. sg_free_table(&rctx->dst_tbl);
  138. return ret;
  139. }
  140. static int qce_skcipher_setkey(struct crypto_skcipher *ablk, const u8 *key,
  141. unsigned int keylen)
  142. {
  143. struct crypto_tfm *tfm = crypto_skcipher_tfm(ablk);
  144. struct qce_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
  145. unsigned long flags = to_cipher_tmpl(ablk)->alg_flags;
  146. unsigned int __keylen;
  147. int ret;
  148. if (!key || !keylen)
  149. return -EINVAL;
  150. /*
  151. * AES XTS key1 = key2 not supported by crypto engine.
  152. * Revisit to request a fallback cipher in this case.
  153. */
  154. if (IS_XTS(flags)) {
  155. __keylen = keylen >> 1;
  156. if (!memcmp(key, key + __keylen, __keylen))
  157. return -ENOKEY;
  158. } else {
  159. __keylen = keylen;
  160. }
  161. switch (__keylen) {
  162. case AES_KEYSIZE_128:
  163. case AES_KEYSIZE_256:
  164. memcpy(ctx->enc_key, key, keylen);
  165. break;
  166. case AES_KEYSIZE_192:
  167. break;
  168. default:
  169. return -EINVAL;
  170. }
  171. ret = crypto_skcipher_setkey(ctx->fallback, key, keylen);
  172. if (!ret)
  173. ctx->enc_keylen = keylen;
  174. return ret;
  175. }
  176. static int qce_des_setkey(struct crypto_skcipher *ablk, const u8 *key,
  177. unsigned int keylen)
  178. {
  179. struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(ablk);
  180. int err;
  181. err = verify_skcipher_des_key(ablk, key);
  182. if (err)
  183. return err;
  184. ctx->enc_keylen = keylen;
  185. memcpy(ctx->enc_key, key, keylen);
  186. return 0;
  187. }
  188. static int qce_des3_setkey(struct crypto_skcipher *ablk, const u8 *key,
  189. unsigned int keylen)
  190. {
  191. struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(ablk);
  192. u32 _key[6];
  193. int err;
  194. err = verify_skcipher_des3_key(ablk, key);
  195. if (err)
  196. return err;
  197. /*
  198. * The crypto engine does not support any two keys
  199. * being the same for triple des algorithms. The
  200. * verify_skcipher_des3_key does not check for all the
  201. * below conditions. Return -ENOKEY in case any two keys
  202. * are the same. Revisit to see if a fallback cipher
  203. * is needed to handle this condition.
  204. */
  205. memcpy(_key, key, DES3_EDE_KEY_SIZE);
  206. if (!((_key[0] ^ _key[2]) | (_key[1] ^ _key[3])) ||
  207. !((_key[2] ^ _key[4]) | (_key[3] ^ _key[5])) ||
  208. !((_key[0] ^ _key[4]) | (_key[1] ^ _key[5])))
  209. return -ENOKEY;
  210. ctx->enc_keylen = keylen;
  211. memcpy(ctx->enc_key, key, keylen);
  212. return 0;
  213. }
  214. static int qce_skcipher_crypt(struct skcipher_request *req, int encrypt)
  215. {
  216. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  217. struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  218. struct qce_cipher_reqctx *rctx = skcipher_request_ctx(req);
  219. struct qce_alg_template *tmpl = to_cipher_tmpl(tfm);
  220. unsigned int blocksize = crypto_skcipher_blocksize(tfm);
  221. int keylen;
  222. int ret;
  223. rctx->flags = tmpl->alg_flags;
  224. rctx->flags |= encrypt ? QCE_ENCRYPT : QCE_DECRYPT;
  225. keylen = IS_XTS(rctx->flags) ? ctx->enc_keylen >> 1 : ctx->enc_keylen;
  226. /* CE does not handle 0 length messages */
  227. if (!req->cryptlen)
  228. return 0;
  229. /*
  230. * ECB and CBC algorithms require message lengths to be
  231. * multiples of block size.
  232. */
  233. if (IS_ECB(rctx->flags) || IS_CBC(rctx->flags))
  234. if (!IS_ALIGNED(req->cryptlen, blocksize))
  235. return -EINVAL;
  236. /*
  237. * Conditions for requesting a fallback cipher
  238. * AES-192 (not supported by crypto engine (CE))
  239. * AES-XTS request with len <= 512 byte (not recommended to use CE)
  240. * AES-XTS request with len > QCE_SECTOR_SIZE and
  241. * is not a multiple of it.(Revisit this condition to check if it is
  242. * needed in all versions of CE)
  243. */
  244. if (IS_AES(rctx->flags) &&
  245. ((keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_256) ||
  246. (IS_XTS(rctx->flags) && ((req->cryptlen <= aes_sw_max_len) ||
  247. (req->cryptlen > QCE_SECTOR_SIZE &&
  248. req->cryptlen % QCE_SECTOR_SIZE))))) {
  249. skcipher_request_set_tfm(&rctx->fallback_req, ctx->fallback);
  250. skcipher_request_set_callback(&rctx->fallback_req,
  251. req->base.flags,
  252. req->base.complete,
  253. req->base.data);
  254. skcipher_request_set_crypt(&rctx->fallback_req, req->src,
  255. req->dst, req->cryptlen, req->iv);
  256. ret = encrypt ? crypto_skcipher_encrypt(&rctx->fallback_req) :
  257. crypto_skcipher_decrypt(&rctx->fallback_req);
  258. return ret;
  259. }
  260. return tmpl->qce->async_req_enqueue(tmpl->qce, &req->base);
  261. }
  262. static int qce_skcipher_encrypt(struct skcipher_request *req)
  263. {
  264. return qce_skcipher_crypt(req, 1);
  265. }
  266. static int qce_skcipher_decrypt(struct skcipher_request *req)
  267. {
  268. return qce_skcipher_crypt(req, 0);
  269. }
  270. static int qce_skcipher_init(struct crypto_skcipher *tfm)
  271. {
  272. /* take the size without the fallback skcipher_request at the end */
  273. crypto_skcipher_set_reqsize(tfm, offsetof(struct qce_cipher_reqctx,
  274. fallback_req));
  275. return 0;
  276. }
  277. static int qce_skcipher_init_fallback(struct crypto_skcipher *tfm)
  278. {
  279. struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  280. ctx->fallback = crypto_alloc_skcipher(crypto_tfm_alg_name(&tfm->base),
  281. 0, CRYPTO_ALG_NEED_FALLBACK);
  282. if (IS_ERR(ctx->fallback))
  283. return PTR_ERR(ctx->fallback);
  284. crypto_skcipher_set_reqsize(tfm, sizeof(struct qce_cipher_reqctx) +
  285. crypto_skcipher_reqsize(ctx->fallback));
  286. return 0;
  287. }
  288. static void qce_skcipher_exit(struct crypto_skcipher *tfm)
  289. {
  290. struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  291. crypto_free_skcipher(ctx->fallback);
  292. }
  293. struct qce_skcipher_def {
  294. unsigned long flags;
  295. const char *name;
  296. const char *drv_name;
  297. unsigned int blocksize;
  298. unsigned int chunksize;
  299. unsigned int ivsize;
  300. unsigned int min_keysize;
  301. unsigned int max_keysize;
  302. };
  303. static const struct qce_skcipher_def skcipher_def[] = {
  304. {
  305. .flags = QCE_ALG_AES | QCE_MODE_ECB,
  306. .name = "ecb(aes)",
  307. .drv_name = "ecb-aes-qce",
  308. .blocksize = AES_BLOCK_SIZE,
  309. .ivsize = 0,
  310. .min_keysize = AES_MIN_KEY_SIZE,
  311. .max_keysize = AES_MAX_KEY_SIZE,
  312. },
  313. {
  314. .flags = QCE_ALG_AES | QCE_MODE_CBC,
  315. .name = "cbc(aes)",
  316. .drv_name = "cbc-aes-qce",
  317. .blocksize = AES_BLOCK_SIZE,
  318. .ivsize = AES_BLOCK_SIZE,
  319. .min_keysize = AES_MIN_KEY_SIZE,
  320. .max_keysize = AES_MAX_KEY_SIZE,
  321. },
  322. {
  323. .flags = QCE_ALG_AES | QCE_MODE_CTR,
  324. .name = "ctr(aes)",
  325. .drv_name = "ctr-aes-qce",
  326. .blocksize = 1,
  327. .chunksize = AES_BLOCK_SIZE,
  328. .ivsize = AES_BLOCK_SIZE,
  329. .min_keysize = AES_MIN_KEY_SIZE,
  330. .max_keysize = AES_MAX_KEY_SIZE,
  331. },
  332. {
  333. .flags = QCE_ALG_AES | QCE_MODE_XTS,
  334. .name = "xts(aes)",
  335. .drv_name = "xts-aes-qce",
  336. .blocksize = AES_BLOCK_SIZE,
  337. .ivsize = AES_BLOCK_SIZE,
  338. .min_keysize = AES_MIN_KEY_SIZE * 2,
  339. .max_keysize = AES_MAX_KEY_SIZE * 2,
  340. },
  341. {
  342. .flags = QCE_ALG_DES | QCE_MODE_ECB,
  343. .name = "ecb(des)",
  344. .drv_name = "ecb-des-qce",
  345. .blocksize = DES_BLOCK_SIZE,
  346. .ivsize = 0,
  347. .min_keysize = DES_KEY_SIZE,
  348. .max_keysize = DES_KEY_SIZE,
  349. },
  350. {
  351. .flags = QCE_ALG_DES | QCE_MODE_CBC,
  352. .name = "cbc(des)",
  353. .drv_name = "cbc-des-qce",
  354. .blocksize = DES_BLOCK_SIZE,
  355. .ivsize = DES_BLOCK_SIZE,
  356. .min_keysize = DES_KEY_SIZE,
  357. .max_keysize = DES_KEY_SIZE,
  358. },
  359. {
  360. .flags = QCE_ALG_3DES | QCE_MODE_ECB,
  361. .name = "ecb(des3_ede)",
  362. .drv_name = "ecb-3des-qce",
  363. .blocksize = DES3_EDE_BLOCK_SIZE,
  364. .ivsize = 0,
  365. .min_keysize = DES3_EDE_KEY_SIZE,
  366. .max_keysize = DES3_EDE_KEY_SIZE,
  367. },
  368. {
  369. .flags = QCE_ALG_3DES | QCE_MODE_CBC,
  370. .name = "cbc(des3_ede)",
  371. .drv_name = "cbc-3des-qce",
  372. .blocksize = DES3_EDE_BLOCK_SIZE,
  373. .ivsize = DES3_EDE_BLOCK_SIZE,
  374. .min_keysize = DES3_EDE_KEY_SIZE,
  375. .max_keysize = DES3_EDE_KEY_SIZE,
  376. },
  377. };
  378. static int qce_skcipher_register_one(const struct qce_skcipher_def *def,
  379. struct qce_device *qce)
  380. {
  381. struct qce_alg_template *tmpl;
  382. struct skcipher_alg *alg;
  383. int ret;
  384. tmpl = kzalloc(sizeof(*tmpl), GFP_KERNEL);
  385. if (!tmpl)
  386. return -ENOMEM;
  387. alg = &tmpl->alg.skcipher;
  388. snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  389. snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  390. def->drv_name);
  391. alg->base.cra_blocksize = def->blocksize;
  392. alg->chunksize = def->chunksize;
  393. alg->ivsize = def->ivsize;
  394. alg->min_keysize = def->min_keysize;
  395. alg->max_keysize = def->max_keysize;
  396. alg->setkey = IS_3DES(def->flags) ? qce_des3_setkey :
  397. IS_DES(def->flags) ? qce_des_setkey :
  398. qce_skcipher_setkey;
  399. alg->encrypt = qce_skcipher_encrypt;
  400. alg->decrypt = qce_skcipher_decrypt;
  401. alg->base.cra_priority = 275;
  402. alg->base.cra_flags = CRYPTO_ALG_ASYNC |
  403. CRYPTO_ALG_ALLOCATES_MEMORY |
  404. CRYPTO_ALG_KERN_DRIVER_ONLY;
  405. alg->base.cra_ctxsize = sizeof(struct qce_cipher_ctx);
  406. alg->base.cra_alignmask = 0;
  407. alg->base.cra_module = THIS_MODULE;
  408. if (IS_AES(def->flags)) {
  409. alg->base.cra_flags |= CRYPTO_ALG_NEED_FALLBACK;
  410. alg->init = qce_skcipher_init_fallback;
  411. alg->exit = qce_skcipher_exit;
  412. } else {
  413. alg->init = qce_skcipher_init;
  414. }
  415. INIT_LIST_HEAD(&tmpl->entry);
  416. tmpl->crypto_alg_type = CRYPTO_ALG_TYPE_SKCIPHER;
  417. tmpl->alg_flags = def->flags;
  418. tmpl->qce = qce;
  419. ret = crypto_register_skcipher(alg);
  420. if (ret) {
  421. dev_err(qce->dev, "%s registration failed\n", alg->base.cra_name);
  422. kfree(tmpl);
  423. return ret;
  424. }
  425. list_add_tail(&tmpl->entry, &skcipher_algs);
  426. dev_dbg(qce->dev, "%s is registered\n", alg->base.cra_name);
  427. return 0;
  428. }
  429. static void qce_skcipher_unregister(struct qce_device *qce)
  430. {
  431. struct qce_alg_template *tmpl, *n;
  432. list_for_each_entry_safe(tmpl, n, &skcipher_algs, entry) {
  433. crypto_unregister_skcipher(&tmpl->alg.skcipher);
  434. list_del(&tmpl->entry);
  435. kfree(tmpl);
  436. }
  437. }
  438. static int qce_skcipher_register(struct qce_device *qce)
  439. {
  440. int ret, i;
  441. for (i = 0; i < ARRAY_SIZE(skcipher_def); i++) {
  442. ret = qce_skcipher_register_one(&skcipher_def[i], qce);
  443. if (ret)
  444. goto err;
  445. }
  446. return 0;
  447. err:
  448. qce_skcipher_unregister(qce);
  449. return ret;
  450. }
  451. const struct qce_algo_ops skcipher_ops = {
  452. .type = CRYPTO_ALG_TYPE_SKCIPHER,
  453. .register_algs = qce_skcipher_register,
  454. .unregister_algs = qce_skcipher_unregister,
  455. .async_req_handle = qce_skcipher_async_req_handle,
  456. };