cipher.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*
  2. * Cipher algorithms supported by the CESA: DES, 3DES and AES.
  3. *
  4. * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
  5. * Author: Arnaud Ebalard <arno@natisbad.org>
  6. *
  7. * This work is based on an initial version written by
  8. * Sebastian Andrzej Siewior < sebastian at breakpoint dot cc >
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published
  12. * by the Free Software Foundation.
  13. */
  14. #include <crypto/aes.h>
  15. #include <crypto/des.h>
  16. #include "cesa.h"
  17. struct mv_cesa_des_ctx {
  18. struct mv_cesa_ctx base;
  19. u8 key[DES_KEY_SIZE];
  20. };
  21. struct mv_cesa_des3_ctx {
  22. struct mv_cesa_ctx base;
  23. u8 key[DES3_EDE_KEY_SIZE];
  24. };
  25. struct mv_cesa_aes_ctx {
  26. struct mv_cesa_ctx base;
  27. struct crypto_aes_ctx aes;
  28. };
  29. struct mv_cesa_skcipher_dma_iter {
  30. struct mv_cesa_dma_iter base;
  31. struct mv_cesa_sg_dma_iter src;
  32. struct mv_cesa_sg_dma_iter dst;
  33. };
  34. static inline void
  35. mv_cesa_skcipher_req_iter_init(struct mv_cesa_skcipher_dma_iter *iter,
  36. struct skcipher_request *req)
  37. {
  38. mv_cesa_req_dma_iter_init(&iter->base, req->cryptlen);
  39. mv_cesa_sg_dma_iter_init(&iter->src, req->src, DMA_TO_DEVICE);
  40. mv_cesa_sg_dma_iter_init(&iter->dst, req->dst, DMA_FROM_DEVICE);
  41. }
  42. static inline bool
  43. mv_cesa_skcipher_req_iter_next_op(struct mv_cesa_skcipher_dma_iter *iter)
  44. {
  45. iter->src.op_offset = 0;
  46. iter->dst.op_offset = 0;
  47. return mv_cesa_req_dma_iter_next_op(&iter->base);
  48. }
  49. static inline void
  50. mv_cesa_skcipher_dma_cleanup(struct skcipher_request *req)
  51. {
  52. struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
  53. if (req->dst != req->src) {
  54. dma_unmap_sg(cesa_dev->dev, req->dst, creq->dst_nents,
  55. DMA_FROM_DEVICE);
  56. dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents,
  57. DMA_TO_DEVICE);
  58. } else {
  59. dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents,
  60. DMA_BIDIRECTIONAL);
  61. }
  62. mv_cesa_dma_cleanup(&creq->base);
  63. }
  64. static inline void mv_cesa_skcipher_cleanup(struct skcipher_request *req)
  65. {
  66. struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
  67. if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
  68. mv_cesa_skcipher_dma_cleanup(req);
  69. }
  70. static void mv_cesa_skcipher_std_step(struct skcipher_request *req)
  71. {
  72. struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
  73. struct mv_cesa_skcipher_std_req *sreq = &creq->std;
  74. struct mv_cesa_engine *engine = creq->base.engine;
  75. size_t len = min_t(size_t, req->cryptlen - sreq->offset,
  76. CESA_SA_SRAM_PAYLOAD_SIZE);
  77. mv_cesa_adjust_op(engine, &sreq->op);
  78. memcpy_toio(engine->sram, &sreq->op, sizeof(sreq->op));
  79. len = sg_pcopy_to_buffer(req->src, creq->src_nents,
  80. engine->sram + CESA_SA_DATA_SRAM_OFFSET,
  81. len, sreq->offset);
  82. sreq->size = len;
  83. mv_cesa_set_crypt_op_len(&sreq->op, len);
  84. /* FIXME: only update enc_len field */
  85. if (!sreq->skip_ctx) {
  86. memcpy_toio(engine->sram, &sreq->op, sizeof(sreq->op));
  87. sreq->skip_ctx = true;
  88. } else {
  89. memcpy_toio(engine->sram, &sreq->op, sizeof(sreq->op.desc));
  90. }
  91. mv_cesa_set_int_mask(engine, CESA_SA_INT_ACCEL0_DONE);
  92. writel_relaxed(CESA_SA_CFG_PARA_DIS, engine->regs + CESA_SA_CFG);
  93. BUG_ON(readl(engine->regs + CESA_SA_CMD) &
  94. CESA_SA_CMD_EN_CESA_SA_ACCL0);
  95. writel(CESA_SA_CMD_EN_CESA_SA_ACCL0, engine->regs + CESA_SA_CMD);
  96. }
  97. static int mv_cesa_skcipher_std_process(struct skcipher_request *req,
  98. u32 status)
  99. {
  100. struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
  101. struct mv_cesa_skcipher_std_req *sreq = &creq->std;
  102. struct mv_cesa_engine *engine = creq->base.engine;
  103. size_t len;
  104. len = sg_pcopy_from_buffer(req->dst, creq->dst_nents,
  105. engine->sram + CESA_SA_DATA_SRAM_OFFSET,
  106. sreq->size, sreq->offset);
  107. sreq->offset += len;
  108. if (sreq->offset < req->cryptlen)
  109. return -EINPROGRESS;
  110. return 0;
  111. }
  112. static int mv_cesa_skcipher_process(struct crypto_async_request *req,
  113. u32 status)
  114. {
  115. struct skcipher_request *skreq = skcipher_request_cast(req);
  116. struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq);
  117. struct mv_cesa_req *basereq = &creq->base;
  118. if (mv_cesa_req_get_type(basereq) == CESA_STD_REQ)
  119. return mv_cesa_skcipher_std_process(skreq, status);
  120. return mv_cesa_dma_process(basereq, status);
  121. }
  122. static void mv_cesa_skcipher_step(struct crypto_async_request *req)
  123. {
  124. struct skcipher_request *skreq = skcipher_request_cast(req);
  125. struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq);
  126. if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
  127. mv_cesa_dma_step(&creq->base);
  128. else
  129. mv_cesa_skcipher_std_step(skreq);
  130. }
  131. static inline void
  132. mv_cesa_skcipher_dma_prepare(struct skcipher_request *req)
  133. {
  134. struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
  135. struct mv_cesa_req *basereq = &creq->base;
  136. mv_cesa_dma_prepare(basereq, basereq->engine);
  137. }
  138. static inline void
  139. mv_cesa_skcipher_std_prepare(struct skcipher_request *req)
  140. {
  141. struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
  142. struct mv_cesa_skcipher_std_req *sreq = &creq->std;
  143. sreq->size = 0;
  144. sreq->offset = 0;
  145. }
  146. static inline void mv_cesa_skcipher_prepare(struct crypto_async_request *req,
  147. struct mv_cesa_engine *engine)
  148. {
  149. struct skcipher_request *skreq = skcipher_request_cast(req);
  150. struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq);
  151. creq->base.engine = engine;
  152. if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
  153. mv_cesa_skcipher_dma_prepare(skreq);
  154. else
  155. mv_cesa_skcipher_std_prepare(skreq);
  156. }
  157. static inline void
  158. mv_cesa_skcipher_req_cleanup(struct crypto_async_request *req)
  159. {
  160. struct skcipher_request *skreq = skcipher_request_cast(req);
  161. mv_cesa_skcipher_cleanup(skreq);
  162. }
  163. static void
  164. mv_cesa_skcipher_complete(struct crypto_async_request *req)
  165. {
  166. struct skcipher_request *skreq = skcipher_request_cast(req);
  167. struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(skreq);
  168. struct mv_cesa_engine *engine = creq->base.engine;
  169. unsigned int ivsize;
  170. atomic_sub(skreq->cryptlen, &engine->load);
  171. ivsize = crypto_skcipher_ivsize(crypto_skcipher_reqtfm(skreq));
  172. if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ) {
  173. struct mv_cesa_req *basereq;
  174. basereq = &creq->base;
  175. memcpy(skreq->iv, basereq->chain.last->op->ctx.blkcipher.iv,
  176. ivsize);
  177. } else {
  178. memcpy_fromio(skreq->iv,
  179. engine->sram + CESA_SA_CRYPT_IV_SRAM_OFFSET,
  180. ivsize);
  181. }
  182. }
  183. static const struct mv_cesa_req_ops mv_cesa_skcipher_req_ops = {
  184. .step = mv_cesa_skcipher_step,
  185. .process = mv_cesa_skcipher_process,
  186. .cleanup = mv_cesa_skcipher_req_cleanup,
  187. .complete = mv_cesa_skcipher_complete,
  188. };
  189. static void mv_cesa_skcipher_cra_exit(struct crypto_tfm *tfm)
  190. {
  191. void *ctx = crypto_tfm_ctx(tfm);
  192. memzero_explicit(ctx, tfm->__crt_alg->cra_ctxsize);
  193. }
  194. static int mv_cesa_skcipher_cra_init(struct crypto_tfm *tfm)
  195. {
  196. struct mv_cesa_ctx *ctx = crypto_tfm_ctx(tfm);
  197. ctx->ops = &mv_cesa_skcipher_req_ops;
  198. crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
  199. sizeof(struct mv_cesa_skcipher_req));
  200. return 0;
  201. }
  202. static int mv_cesa_aes_setkey(struct crypto_skcipher *cipher, const u8 *key,
  203. unsigned int len)
  204. {
  205. struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
  206. struct mv_cesa_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  207. int remaining;
  208. int offset;
  209. int ret;
  210. int i;
  211. ret = crypto_aes_expand_key(&ctx->aes, key, len);
  212. if (ret) {
  213. crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  214. return ret;
  215. }
  216. remaining = (ctx->aes.key_length - 16) / 4;
  217. offset = ctx->aes.key_length + 24 - remaining;
  218. for (i = 0; i < remaining; i++)
  219. ctx->aes.key_dec[4 + i] =
  220. cpu_to_le32(ctx->aes.key_enc[offset + i]);
  221. return 0;
  222. }
  223. static int mv_cesa_des_setkey(struct crypto_skcipher *cipher, const u8 *key,
  224. unsigned int len)
  225. {
  226. struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
  227. struct mv_cesa_des_ctx *ctx = crypto_tfm_ctx(tfm);
  228. u32 tmp[DES_EXPKEY_WORDS];
  229. int ret;
  230. if (len != DES_KEY_SIZE) {
  231. crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  232. return -EINVAL;
  233. }
  234. ret = des_ekey(tmp, key);
  235. if (!ret && (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  236. tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
  237. return -EINVAL;
  238. }
  239. memcpy(ctx->key, key, DES_KEY_SIZE);
  240. return 0;
  241. }
  242. static int mv_cesa_des3_ede_setkey(struct crypto_skcipher *cipher,
  243. const u8 *key, unsigned int len)
  244. {
  245. struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
  246. struct mv_cesa_des_ctx *ctx = crypto_tfm_ctx(tfm);
  247. if (len != DES3_EDE_KEY_SIZE) {
  248. crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  249. return -EINVAL;
  250. }
  251. memcpy(ctx->key, key, DES3_EDE_KEY_SIZE);
  252. return 0;
  253. }
  254. static int mv_cesa_skcipher_dma_req_init(struct skcipher_request *req,
  255. const struct mv_cesa_op_ctx *op_templ)
  256. {
  257. struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
  258. gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  259. GFP_KERNEL : GFP_ATOMIC;
  260. struct mv_cesa_req *basereq = &creq->base;
  261. struct mv_cesa_skcipher_dma_iter iter;
  262. bool skip_ctx = false;
  263. int ret;
  264. unsigned int ivsize;
  265. basereq->chain.first = NULL;
  266. basereq->chain.last = NULL;
  267. if (req->src != req->dst) {
  268. ret = dma_map_sg(cesa_dev->dev, req->src, creq->src_nents,
  269. DMA_TO_DEVICE);
  270. if (!ret)
  271. return -ENOMEM;
  272. ret = dma_map_sg(cesa_dev->dev, req->dst, creq->dst_nents,
  273. DMA_FROM_DEVICE);
  274. if (!ret) {
  275. ret = -ENOMEM;
  276. goto err_unmap_src;
  277. }
  278. } else {
  279. ret = dma_map_sg(cesa_dev->dev, req->src, creq->src_nents,
  280. DMA_BIDIRECTIONAL);
  281. if (!ret)
  282. return -ENOMEM;
  283. }
  284. mv_cesa_tdma_desc_iter_init(&basereq->chain);
  285. mv_cesa_skcipher_req_iter_init(&iter, req);
  286. do {
  287. struct mv_cesa_op_ctx *op;
  288. op = mv_cesa_dma_add_op(&basereq->chain, op_templ, skip_ctx, flags);
  289. if (IS_ERR(op)) {
  290. ret = PTR_ERR(op);
  291. goto err_free_tdma;
  292. }
  293. skip_ctx = true;
  294. mv_cesa_set_crypt_op_len(op, iter.base.op_len);
  295. /* Add input transfers */
  296. ret = mv_cesa_dma_add_op_transfers(&basereq->chain, &iter.base,
  297. &iter.src, flags);
  298. if (ret)
  299. goto err_free_tdma;
  300. /* Add dummy desc to launch the crypto operation */
  301. ret = mv_cesa_dma_add_dummy_launch(&basereq->chain, flags);
  302. if (ret)
  303. goto err_free_tdma;
  304. /* Add output transfers */
  305. ret = mv_cesa_dma_add_op_transfers(&basereq->chain, &iter.base,
  306. &iter.dst, flags);
  307. if (ret)
  308. goto err_free_tdma;
  309. } while (mv_cesa_skcipher_req_iter_next_op(&iter));
  310. /* Add output data for IV */
  311. ivsize = crypto_skcipher_ivsize(crypto_skcipher_reqtfm(req));
  312. ret = mv_cesa_dma_add_result_op(&basereq->chain, CESA_SA_CFG_SRAM_OFFSET,
  313. CESA_SA_DATA_SRAM_OFFSET,
  314. CESA_TDMA_SRC_IN_SRAM, flags);
  315. if (ret)
  316. goto err_free_tdma;
  317. basereq->chain.last->flags |= CESA_TDMA_END_OF_REQ;
  318. return 0;
  319. err_free_tdma:
  320. mv_cesa_dma_cleanup(basereq);
  321. if (req->dst != req->src)
  322. dma_unmap_sg(cesa_dev->dev, req->dst, creq->dst_nents,
  323. DMA_FROM_DEVICE);
  324. err_unmap_src:
  325. dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents,
  326. req->dst != req->src ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL);
  327. return ret;
  328. }
  329. static inline int
  330. mv_cesa_skcipher_std_req_init(struct skcipher_request *req,
  331. const struct mv_cesa_op_ctx *op_templ)
  332. {
  333. struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
  334. struct mv_cesa_skcipher_std_req *sreq = &creq->std;
  335. struct mv_cesa_req *basereq = &creq->base;
  336. sreq->op = *op_templ;
  337. sreq->skip_ctx = false;
  338. basereq->chain.first = NULL;
  339. basereq->chain.last = NULL;
  340. return 0;
  341. }
  342. static int mv_cesa_skcipher_req_init(struct skcipher_request *req,
  343. struct mv_cesa_op_ctx *tmpl)
  344. {
  345. struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
  346. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  347. unsigned int blksize = crypto_skcipher_blocksize(tfm);
  348. int ret;
  349. if (!IS_ALIGNED(req->cryptlen, blksize))
  350. return -EINVAL;
  351. creq->src_nents = sg_nents_for_len(req->src, req->cryptlen);
  352. if (creq->src_nents < 0) {
  353. dev_err(cesa_dev->dev, "Invalid number of src SG");
  354. return creq->src_nents;
  355. }
  356. creq->dst_nents = sg_nents_for_len(req->dst, req->cryptlen);
  357. if (creq->dst_nents < 0) {
  358. dev_err(cesa_dev->dev, "Invalid number of dst SG");
  359. return creq->dst_nents;
  360. }
  361. mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_OP_CRYPT_ONLY,
  362. CESA_SA_DESC_CFG_OP_MSK);
  363. if (cesa_dev->caps->has_tdma)
  364. ret = mv_cesa_skcipher_dma_req_init(req, tmpl);
  365. else
  366. ret = mv_cesa_skcipher_std_req_init(req, tmpl);
  367. return ret;
  368. }
  369. static int mv_cesa_skcipher_queue_req(struct skcipher_request *req,
  370. struct mv_cesa_op_ctx *tmpl)
  371. {
  372. int ret;
  373. struct mv_cesa_skcipher_req *creq = skcipher_request_ctx(req);
  374. struct mv_cesa_engine *engine;
  375. ret = mv_cesa_skcipher_req_init(req, tmpl);
  376. if (ret)
  377. return ret;
  378. engine = mv_cesa_select_engine(req->cryptlen);
  379. mv_cesa_skcipher_prepare(&req->base, engine);
  380. ret = mv_cesa_queue_req(&req->base, &creq->base);
  381. if (mv_cesa_req_needs_cleanup(&req->base, ret))
  382. mv_cesa_skcipher_cleanup(req);
  383. return ret;
  384. }
  385. static int mv_cesa_des_op(struct skcipher_request *req,
  386. struct mv_cesa_op_ctx *tmpl)
  387. {
  388. struct mv_cesa_des_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  389. mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTM_DES,
  390. CESA_SA_DESC_CFG_CRYPTM_MSK);
  391. memcpy(tmpl->ctx.blkcipher.key, ctx->key, DES_KEY_SIZE);
  392. return mv_cesa_skcipher_queue_req(req, tmpl);
  393. }
  394. static int mv_cesa_ecb_des_encrypt(struct skcipher_request *req)
  395. {
  396. struct mv_cesa_op_ctx tmpl;
  397. mv_cesa_set_op_cfg(&tmpl,
  398. CESA_SA_DESC_CFG_CRYPTCM_ECB |
  399. CESA_SA_DESC_CFG_DIR_ENC);
  400. return mv_cesa_des_op(req, &tmpl);
  401. }
  402. static int mv_cesa_ecb_des_decrypt(struct skcipher_request *req)
  403. {
  404. struct mv_cesa_op_ctx tmpl;
  405. mv_cesa_set_op_cfg(&tmpl,
  406. CESA_SA_DESC_CFG_CRYPTCM_ECB |
  407. CESA_SA_DESC_CFG_DIR_DEC);
  408. return mv_cesa_des_op(req, &tmpl);
  409. }
  410. struct skcipher_alg mv_cesa_ecb_des_alg = {
  411. .setkey = mv_cesa_des_setkey,
  412. .encrypt = mv_cesa_ecb_des_encrypt,
  413. .decrypt = mv_cesa_ecb_des_decrypt,
  414. .min_keysize = DES_KEY_SIZE,
  415. .max_keysize = DES_KEY_SIZE,
  416. .base = {
  417. .cra_name = "ecb(des)",
  418. .cra_driver_name = "mv-ecb-des",
  419. .cra_priority = 300,
  420. .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
  421. .cra_blocksize = DES_BLOCK_SIZE,
  422. .cra_ctxsize = sizeof(struct mv_cesa_des_ctx),
  423. .cra_alignmask = 0,
  424. .cra_module = THIS_MODULE,
  425. .cra_init = mv_cesa_skcipher_cra_init,
  426. .cra_exit = mv_cesa_skcipher_cra_exit,
  427. },
  428. };
  429. static int mv_cesa_cbc_des_op(struct skcipher_request *req,
  430. struct mv_cesa_op_ctx *tmpl)
  431. {
  432. mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTCM_CBC,
  433. CESA_SA_DESC_CFG_CRYPTCM_MSK);
  434. memcpy(tmpl->ctx.blkcipher.iv, req->iv, DES_BLOCK_SIZE);
  435. return mv_cesa_des_op(req, tmpl);
  436. }
  437. static int mv_cesa_cbc_des_encrypt(struct skcipher_request *req)
  438. {
  439. struct mv_cesa_op_ctx tmpl;
  440. mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_ENC);
  441. return mv_cesa_cbc_des_op(req, &tmpl);
  442. }
  443. static int mv_cesa_cbc_des_decrypt(struct skcipher_request *req)
  444. {
  445. struct mv_cesa_op_ctx tmpl;
  446. mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_DEC);
  447. return mv_cesa_cbc_des_op(req, &tmpl);
  448. }
  449. struct skcipher_alg mv_cesa_cbc_des_alg = {
  450. .setkey = mv_cesa_des_setkey,
  451. .encrypt = mv_cesa_cbc_des_encrypt,
  452. .decrypt = mv_cesa_cbc_des_decrypt,
  453. .min_keysize = DES_KEY_SIZE,
  454. .max_keysize = DES_KEY_SIZE,
  455. .ivsize = DES_BLOCK_SIZE,
  456. .base = {
  457. .cra_name = "cbc(des)",
  458. .cra_driver_name = "mv-cbc-des",
  459. .cra_priority = 300,
  460. .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
  461. .cra_blocksize = DES_BLOCK_SIZE,
  462. .cra_ctxsize = sizeof(struct mv_cesa_des_ctx),
  463. .cra_alignmask = 0,
  464. .cra_module = THIS_MODULE,
  465. .cra_init = mv_cesa_skcipher_cra_init,
  466. .cra_exit = mv_cesa_skcipher_cra_exit,
  467. },
  468. };
  469. static int mv_cesa_des3_op(struct skcipher_request *req,
  470. struct mv_cesa_op_ctx *tmpl)
  471. {
  472. struct mv_cesa_des3_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  473. mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTM_3DES,
  474. CESA_SA_DESC_CFG_CRYPTM_MSK);
  475. memcpy(tmpl->ctx.blkcipher.key, ctx->key, DES3_EDE_KEY_SIZE);
  476. return mv_cesa_skcipher_queue_req(req, tmpl);
  477. }
  478. static int mv_cesa_ecb_des3_ede_encrypt(struct skcipher_request *req)
  479. {
  480. struct mv_cesa_op_ctx tmpl;
  481. mv_cesa_set_op_cfg(&tmpl,
  482. CESA_SA_DESC_CFG_CRYPTCM_ECB |
  483. CESA_SA_DESC_CFG_3DES_EDE |
  484. CESA_SA_DESC_CFG_DIR_ENC);
  485. return mv_cesa_des3_op(req, &tmpl);
  486. }
  487. static int mv_cesa_ecb_des3_ede_decrypt(struct skcipher_request *req)
  488. {
  489. struct mv_cesa_op_ctx tmpl;
  490. mv_cesa_set_op_cfg(&tmpl,
  491. CESA_SA_DESC_CFG_CRYPTCM_ECB |
  492. CESA_SA_DESC_CFG_3DES_EDE |
  493. CESA_SA_DESC_CFG_DIR_DEC);
  494. return mv_cesa_des3_op(req, &tmpl);
  495. }
  496. struct skcipher_alg mv_cesa_ecb_des3_ede_alg = {
  497. .setkey = mv_cesa_des3_ede_setkey,
  498. .encrypt = mv_cesa_ecb_des3_ede_encrypt,
  499. .decrypt = mv_cesa_ecb_des3_ede_decrypt,
  500. .min_keysize = DES3_EDE_KEY_SIZE,
  501. .max_keysize = DES3_EDE_KEY_SIZE,
  502. .ivsize = DES3_EDE_BLOCK_SIZE,
  503. .base = {
  504. .cra_name = "ecb(des3_ede)",
  505. .cra_driver_name = "mv-ecb-des3-ede",
  506. .cra_priority = 300,
  507. .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
  508. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  509. .cra_ctxsize = sizeof(struct mv_cesa_des3_ctx),
  510. .cra_alignmask = 0,
  511. .cra_module = THIS_MODULE,
  512. .cra_init = mv_cesa_skcipher_cra_init,
  513. .cra_exit = mv_cesa_skcipher_cra_exit,
  514. },
  515. };
  516. static int mv_cesa_cbc_des3_op(struct skcipher_request *req,
  517. struct mv_cesa_op_ctx *tmpl)
  518. {
  519. memcpy(tmpl->ctx.blkcipher.iv, req->iv, DES3_EDE_BLOCK_SIZE);
  520. return mv_cesa_des3_op(req, tmpl);
  521. }
  522. static int mv_cesa_cbc_des3_ede_encrypt(struct skcipher_request *req)
  523. {
  524. struct mv_cesa_op_ctx tmpl;
  525. mv_cesa_set_op_cfg(&tmpl,
  526. CESA_SA_DESC_CFG_CRYPTCM_CBC |
  527. CESA_SA_DESC_CFG_3DES_EDE |
  528. CESA_SA_DESC_CFG_DIR_ENC);
  529. return mv_cesa_cbc_des3_op(req, &tmpl);
  530. }
  531. static int mv_cesa_cbc_des3_ede_decrypt(struct skcipher_request *req)
  532. {
  533. struct mv_cesa_op_ctx tmpl;
  534. mv_cesa_set_op_cfg(&tmpl,
  535. CESA_SA_DESC_CFG_CRYPTCM_CBC |
  536. CESA_SA_DESC_CFG_3DES_EDE |
  537. CESA_SA_DESC_CFG_DIR_DEC);
  538. return mv_cesa_cbc_des3_op(req, &tmpl);
  539. }
  540. struct skcipher_alg mv_cesa_cbc_des3_ede_alg = {
  541. .setkey = mv_cesa_des3_ede_setkey,
  542. .encrypt = mv_cesa_cbc_des3_ede_encrypt,
  543. .decrypt = mv_cesa_cbc_des3_ede_decrypt,
  544. .min_keysize = DES3_EDE_KEY_SIZE,
  545. .max_keysize = DES3_EDE_KEY_SIZE,
  546. .ivsize = DES3_EDE_BLOCK_SIZE,
  547. .base = {
  548. .cra_name = "cbc(des3_ede)",
  549. .cra_driver_name = "mv-cbc-des3-ede",
  550. .cra_priority = 300,
  551. .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
  552. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  553. .cra_ctxsize = sizeof(struct mv_cesa_des3_ctx),
  554. .cra_alignmask = 0,
  555. .cra_module = THIS_MODULE,
  556. .cra_init = mv_cesa_skcipher_cra_init,
  557. .cra_exit = mv_cesa_skcipher_cra_exit,
  558. },
  559. };
  560. static int mv_cesa_aes_op(struct skcipher_request *req,
  561. struct mv_cesa_op_ctx *tmpl)
  562. {
  563. struct mv_cesa_aes_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  564. int i;
  565. u32 *key;
  566. u32 cfg;
  567. cfg = CESA_SA_DESC_CFG_CRYPTM_AES;
  568. if (mv_cesa_get_op_cfg(tmpl) & CESA_SA_DESC_CFG_DIR_DEC)
  569. key = ctx->aes.key_dec;
  570. else
  571. key = ctx->aes.key_enc;
  572. for (i = 0; i < ctx->aes.key_length / sizeof(u32); i++)
  573. tmpl->ctx.blkcipher.key[i] = cpu_to_le32(key[i]);
  574. if (ctx->aes.key_length == 24)
  575. cfg |= CESA_SA_DESC_CFG_AES_LEN_192;
  576. else if (ctx->aes.key_length == 32)
  577. cfg |= CESA_SA_DESC_CFG_AES_LEN_256;
  578. mv_cesa_update_op_cfg(tmpl, cfg,
  579. CESA_SA_DESC_CFG_CRYPTM_MSK |
  580. CESA_SA_DESC_CFG_AES_LEN_MSK);
  581. return mv_cesa_skcipher_queue_req(req, tmpl);
  582. }
  583. static int mv_cesa_ecb_aes_encrypt(struct skcipher_request *req)
  584. {
  585. struct mv_cesa_op_ctx tmpl;
  586. mv_cesa_set_op_cfg(&tmpl,
  587. CESA_SA_DESC_CFG_CRYPTCM_ECB |
  588. CESA_SA_DESC_CFG_DIR_ENC);
  589. return mv_cesa_aes_op(req, &tmpl);
  590. }
  591. static int mv_cesa_ecb_aes_decrypt(struct skcipher_request *req)
  592. {
  593. struct mv_cesa_op_ctx tmpl;
  594. mv_cesa_set_op_cfg(&tmpl,
  595. CESA_SA_DESC_CFG_CRYPTCM_ECB |
  596. CESA_SA_DESC_CFG_DIR_DEC);
  597. return mv_cesa_aes_op(req, &tmpl);
  598. }
  599. struct skcipher_alg mv_cesa_ecb_aes_alg = {
  600. .setkey = mv_cesa_aes_setkey,
  601. .encrypt = mv_cesa_ecb_aes_encrypt,
  602. .decrypt = mv_cesa_ecb_aes_decrypt,
  603. .min_keysize = AES_MIN_KEY_SIZE,
  604. .max_keysize = AES_MAX_KEY_SIZE,
  605. .base = {
  606. .cra_name = "ecb(aes)",
  607. .cra_driver_name = "mv-ecb-aes",
  608. .cra_priority = 300,
  609. .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
  610. .cra_blocksize = AES_BLOCK_SIZE,
  611. .cra_ctxsize = sizeof(struct mv_cesa_aes_ctx),
  612. .cra_alignmask = 0,
  613. .cra_module = THIS_MODULE,
  614. .cra_init = mv_cesa_skcipher_cra_init,
  615. .cra_exit = mv_cesa_skcipher_cra_exit,
  616. },
  617. };
  618. static int mv_cesa_cbc_aes_op(struct skcipher_request *req,
  619. struct mv_cesa_op_ctx *tmpl)
  620. {
  621. mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTCM_CBC,
  622. CESA_SA_DESC_CFG_CRYPTCM_MSK);
  623. memcpy(tmpl->ctx.blkcipher.iv, req->iv, AES_BLOCK_SIZE);
  624. return mv_cesa_aes_op(req, tmpl);
  625. }
  626. static int mv_cesa_cbc_aes_encrypt(struct skcipher_request *req)
  627. {
  628. struct mv_cesa_op_ctx tmpl;
  629. mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_ENC);
  630. return mv_cesa_cbc_aes_op(req, &tmpl);
  631. }
  632. static int mv_cesa_cbc_aes_decrypt(struct skcipher_request *req)
  633. {
  634. struct mv_cesa_op_ctx tmpl;
  635. mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_DEC);
  636. return mv_cesa_cbc_aes_op(req, &tmpl);
  637. }
  638. struct skcipher_alg mv_cesa_cbc_aes_alg = {
  639. .setkey = mv_cesa_aes_setkey,
  640. .encrypt = mv_cesa_cbc_aes_encrypt,
  641. .decrypt = mv_cesa_cbc_aes_decrypt,
  642. .min_keysize = AES_MIN_KEY_SIZE,
  643. .max_keysize = AES_MAX_KEY_SIZE,
  644. .ivsize = AES_BLOCK_SIZE,
  645. .base = {
  646. .cra_name = "cbc(aes)",
  647. .cra_driver_name = "mv-cbc-aes",
  648. .cra_priority = 300,
  649. .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
  650. .cra_blocksize = AES_BLOCK_SIZE,
  651. .cra_ctxsize = sizeof(struct mv_cesa_aes_ctx),
  652. .cra_alignmask = 0,
  653. .cra_module = THIS_MODULE,
  654. .cra_init = mv_cesa_skcipher_cra_init,
  655. .cra_exit = mv_cesa_skcipher_cra_exit,
  656. },
  657. };