cipher.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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_ablkcipher_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_ablkcipher_req_iter_init(struct mv_cesa_ablkcipher_dma_iter *iter,
  36. struct ablkcipher_request *req)
  37. {
  38. mv_cesa_req_dma_iter_init(&iter->base, req->nbytes);
  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_ablkcipher_req_iter_next_op(struct mv_cesa_ablkcipher_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_ablkcipher_dma_cleanup(struct ablkcipher_request *req)
  51. {
  52. struct mv_cesa_ablkcipher_req *creq = ablkcipher_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_ablkcipher_cleanup(struct ablkcipher_request *req)
  65. {
  66. struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req);
  67. if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
  68. mv_cesa_ablkcipher_dma_cleanup(req);
  69. }
  70. static void mv_cesa_ablkcipher_std_step(struct ablkcipher_request *req)
  71. {
  72. struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req);
  73. struct mv_cesa_ablkcipher_std_req *sreq = &creq->std;
  74. struct mv_cesa_engine *engine = creq->base.engine;
  75. size_t len = min_t(size_t, req->nbytes - 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_ablkcipher_std_process(struct ablkcipher_request *req,
  98. u32 status)
  99. {
  100. struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req);
  101. struct mv_cesa_ablkcipher_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->nbytes)
  109. return -EINPROGRESS;
  110. return 0;
  111. }
  112. static int mv_cesa_ablkcipher_process(struct crypto_async_request *req,
  113. u32 status)
  114. {
  115. struct ablkcipher_request *ablkreq = ablkcipher_request_cast(req);
  116. struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(ablkreq);
  117. struct mv_cesa_req *basereq = &creq->base;
  118. if (mv_cesa_req_get_type(basereq) == CESA_STD_REQ)
  119. return mv_cesa_ablkcipher_std_process(ablkreq, status);
  120. return mv_cesa_dma_process(basereq, status);
  121. }
  122. static void mv_cesa_ablkcipher_step(struct crypto_async_request *req)
  123. {
  124. struct ablkcipher_request *ablkreq = ablkcipher_request_cast(req);
  125. struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(ablkreq);
  126. if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
  127. mv_cesa_dma_step(&creq->base);
  128. else
  129. mv_cesa_ablkcipher_std_step(ablkreq);
  130. }
  131. static inline void
  132. mv_cesa_ablkcipher_dma_prepare(struct ablkcipher_request *req)
  133. {
  134. struct mv_cesa_ablkcipher_req *creq = ablkcipher_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_ablkcipher_std_prepare(struct ablkcipher_request *req)
  140. {
  141. struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req);
  142. struct mv_cesa_ablkcipher_std_req *sreq = &creq->std;
  143. sreq->size = 0;
  144. sreq->offset = 0;
  145. }
  146. static inline void mv_cesa_ablkcipher_prepare(struct crypto_async_request *req,
  147. struct mv_cesa_engine *engine)
  148. {
  149. struct ablkcipher_request *ablkreq = ablkcipher_request_cast(req);
  150. struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(ablkreq);
  151. creq->base.engine = engine;
  152. if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
  153. mv_cesa_ablkcipher_dma_prepare(ablkreq);
  154. else
  155. mv_cesa_ablkcipher_std_prepare(ablkreq);
  156. }
  157. static inline void
  158. mv_cesa_ablkcipher_req_cleanup(struct crypto_async_request *req)
  159. {
  160. struct ablkcipher_request *ablkreq = ablkcipher_request_cast(req);
  161. mv_cesa_ablkcipher_cleanup(ablkreq);
  162. }
  163. static void
  164. mv_cesa_ablkcipher_complete(struct crypto_async_request *req)
  165. {
  166. struct ablkcipher_request *ablkreq = ablkcipher_request_cast(req);
  167. struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(ablkreq);
  168. struct mv_cesa_engine *engine = creq->base.engine;
  169. unsigned int ivsize;
  170. atomic_sub(ablkreq->nbytes, &engine->load);
  171. ivsize = crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(ablkreq));
  172. if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ) {
  173. struct mv_cesa_req *basereq;
  174. basereq = &creq->base;
  175. memcpy(ablkreq->info, basereq->chain.last->op->ctx.blkcipher.iv,
  176. ivsize);
  177. } else {
  178. memcpy_fromio(ablkreq->info,
  179. engine->sram + CESA_SA_CRYPT_IV_SRAM_OFFSET,
  180. ivsize);
  181. }
  182. }
  183. static const struct mv_cesa_req_ops mv_cesa_ablkcipher_req_ops = {
  184. .step = mv_cesa_ablkcipher_step,
  185. .process = mv_cesa_ablkcipher_process,
  186. .cleanup = mv_cesa_ablkcipher_req_cleanup,
  187. .complete = mv_cesa_ablkcipher_complete,
  188. };
  189. static int mv_cesa_ablkcipher_cra_init(struct crypto_tfm *tfm)
  190. {
  191. struct mv_cesa_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  192. ctx->base.ops = &mv_cesa_ablkcipher_req_ops;
  193. tfm->crt_ablkcipher.reqsize = sizeof(struct mv_cesa_ablkcipher_req);
  194. return 0;
  195. }
  196. static int mv_cesa_aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  197. unsigned int len)
  198. {
  199. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  200. struct mv_cesa_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  201. int remaining;
  202. int offset;
  203. int ret;
  204. int i;
  205. ret = crypto_aes_expand_key(&ctx->aes, key, len);
  206. if (ret) {
  207. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  208. return ret;
  209. }
  210. remaining = (ctx->aes.key_length - 16) / 4;
  211. offset = ctx->aes.key_length + 24 - remaining;
  212. for (i = 0; i < remaining; i++)
  213. ctx->aes.key_dec[4 + i] =
  214. cpu_to_le32(ctx->aes.key_enc[offset + i]);
  215. return 0;
  216. }
  217. static int mv_cesa_des_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  218. unsigned int len)
  219. {
  220. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  221. struct mv_cesa_des_ctx *ctx = crypto_tfm_ctx(tfm);
  222. u32 tmp[DES_EXPKEY_WORDS];
  223. int ret;
  224. if (len != DES_KEY_SIZE) {
  225. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  226. return -EINVAL;
  227. }
  228. ret = des_ekey(tmp, key);
  229. if (!ret && (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  230. tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
  231. return -EINVAL;
  232. }
  233. memcpy(ctx->key, key, DES_KEY_SIZE);
  234. return 0;
  235. }
  236. static int mv_cesa_des3_ede_setkey(struct crypto_ablkcipher *cipher,
  237. const u8 *key, unsigned int len)
  238. {
  239. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  240. struct mv_cesa_des_ctx *ctx = crypto_tfm_ctx(tfm);
  241. if (len != DES3_EDE_KEY_SIZE) {
  242. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  243. return -EINVAL;
  244. }
  245. memcpy(ctx->key, key, DES3_EDE_KEY_SIZE);
  246. return 0;
  247. }
  248. static int mv_cesa_ablkcipher_dma_req_init(struct ablkcipher_request *req,
  249. const struct mv_cesa_op_ctx *op_templ)
  250. {
  251. struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req);
  252. gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  253. GFP_KERNEL : GFP_ATOMIC;
  254. struct mv_cesa_req *basereq = &creq->base;
  255. struct mv_cesa_ablkcipher_dma_iter iter;
  256. bool skip_ctx = false;
  257. int ret;
  258. unsigned int ivsize;
  259. basereq->chain.first = NULL;
  260. basereq->chain.last = NULL;
  261. if (req->src != req->dst) {
  262. ret = dma_map_sg(cesa_dev->dev, req->src, creq->src_nents,
  263. DMA_TO_DEVICE);
  264. if (!ret)
  265. return -ENOMEM;
  266. ret = dma_map_sg(cesa_dev->dev, req->dst, creq->dst_nents,
  267. DMA_FROM_DEVICE);
  268. if (!ret) {
  269. ret = -ENOMEM;
  270. goto err_unmap_src;
  271. }
  272. } else {
  273. ret = dma_map_sg(cesa_dev->dev, req->src, creq->src_nents,
  274. DMA_BIDIRECTIONAL);
  275. if (!ret)
  276. return -ENOMEM;
  277. }
  278. mv_cesa_tdma_desc_iter_init(&basereq->chain);
  279. mv_cesa_ablkcipher_req_iter_init(&iter, req);
  280. do {
  281. struct mv_cesa_op_ctx *op;
  282. op = mv_cesa_dma_add_op(&basereq->chain, op_templ, skip_ctx, flags);
  283. if (IS_ERR(op)) {
  284. ret = PTR_ERR(op);
  285. goto err_free_tdma;
  286. }
  287. skip_ctx = true;
  288. mv_cesa_set_crypt_op_len(op, iter.base.op_len);
  289. /* Add input transfers */
  290. ret = mv_cesa_dma_add_op_transfers(&basereq->chain, &iter.base,
  291. &iter.src, flags);
  292. if (ret)
  293. goto err_free_tdma;
  294. /* Add dummy desc to launch the crypto operation */
  295. ret = mv_cesa_dma_add_dummy_launch(&basereq->chain, flags);
  296. if (ret)
  297. goto err_free_tdma;
  298. /* Add output transfers */
  299. ret = mv_cesa_dma_add_op_transfers(&basereq->chain, &iter.base,
  300. &iter.dst, flags);
  301. if (ret)
  302. goto err_free_tdma;
  303. } while (mv_cesa_ablkcipher_req_iter_next_op(&iter));
  304. /* Add output data for IV */
  305. ivsize = crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(req));
  306. ret = mv_cesa_dma_add_result_op(&basereq->chain, CESA_SA_CFG_SRAM_OFFSET,
  307. CESA_SA_DATA_SRAM_OFFSET,
  308. CESA_TDMA_SRC_IN_SRAM, flags);
  309. if (ret)
  310. goto err_free_tdma;
  311. basereq->chain.last->flags |= CESA_TDMA_END_OF_REQ;
  312. return 0;
  313. err_free_tdma:
  314. mv_cesa_dma_cleanup(basereq);
  315. if (req->dst != req->src)
  316. dma_unmap_sg(cesa_dev->dev, req->dst, creq->dst_nents,
  317. DMA_FROM_DEVICE);
  318. err_unmap_src:
  319. dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents,
  320. req->dst != req->src ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL);
  321. return ret;
  322. }
  323. static inline int
  324. mv_cesa_ablkcipher_std_req_init(struct ablkcipher_request *req,
  325. const struct mv_cesa_op_ctx *op_templ)
  326. {
  327. struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req);
  328. struct mv_cesa_ablkcipher_std_req *sreq = &creq->std;
  329. struct mv_cesa_req *basereq = &creq->base;
  330. sreq->op = *op_templ;
  331. sreq->skip_ctx = false;
  332. basereq->chain.first = NULL;
  333. basereq->chain.last = NULL;
  334. return 0;
  335. }
  336. static int mv_cesa_ablkcipher_req_init(struct ablkcipher_request *req,
  337. struct mv_cesa_op_ctx *tmpl)
  338. {
  339. struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req);
  340. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  341. unsigned int blksize = crypto_ablkcipher_blocksize(tfm);
  342. int ret;
  343. if (!IS_ALIGNED(req->nbytes, blksize))
  344. return -EINVAL;
  345. creq->src_nents = sg_nents_for_len(req->src, req->nbytes);
  346. if (creq->src_nents < 0) {
  347. dev_err(cesa_dev->dev, "Invalid number of src SG");
  348. return creq->src_nents;
  349. }
  350. creq->dst_nents = sg_nents_for_len(req->dst, req->nbytes);
  351. if (creq->dst_nents < 0) {
  352. dev_err(cesa_dev->dev, "Invalid number of dst SG");
  353. return creq->dst_nents;
  354. }
  355. mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_OP_CRYPT_ONLY,
  356. CESA_SA_DESC_CFG_OP_MSK);
  357. if (cesa_dev->caps->has_tdma)
  358. ret = mv_cesa_ablkcipher_dma_req_init(req, tmpl);
  359. else
  360. ret = mv_cesa_ablkcipher_std_req_init(req, tmpl);
  361. return ret;
  362. }
  363. static int mv_cesa_ablkcipher_queue_req(struct ablkcipher_request *req,
  364. struct mv_cesa_op_ctx *tmpl)
  365. {
  366. int ret;
  367. struct mv_cesa_ablkcipher_req *creq = ablkcipher_request_ctx(req);
  368. struct mv_cesa_engine *engine;
  369. ret = mv_cesa_ablkcipher_req_init(req, tmpl);
  370. if (ret)
  371. return ret;
  372. engine = mv_cesa_select_engine(req->nbytes);
  373. mv_cesa_ablkcipher_prepare(&req->base, engine);
  374. ret = mv_cesa_queue_req(&req->base, &creq->base);
  375. if (mv_cesa_req_needs_cleanup(&req->base, ret))
  376. mv_cesa_ablkcipher_cleanup(req);
  377. return ret;
  378. }
  379. static int mv_cesa_des_op(struct ablkcipher_request *req,
  380. struct mv_cesa_op_ctx *tmpl)
  381. {
  382. struct mv_cesa_des_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  383. mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTM_DES,
  384. CESA_SA_DESC_CFG_CRYPTM_MSK);
  385. memcpy(tmpl->ctx.blkcipher.key, ctx->key, DES_KEY_SIZE);
  386. return mv_cesa_ablkcipher_queue_req(req, tmpl);
  387. }
  388. static int mv_cesa_ecb_des_encrypt(struct ablkcipher_request *req)
  389. {
  390. struct mv_cesa_op_ctx tmpl;
  391. mv_cesa_set_op_cfg(&tmpl,
  392. CESA_SA_DESC_CFG_CRYPTCM_ECB |
  393. CESA_SA_DESC_CFG_DIR_ENC);
  394. return mv_cesa_des_op(req, &tmpl);
  395. }
  396. static int mv_cesa_ecb_des_decrypt(struct ablkcipher_request *req)
  397. {
  398. struct mv_cesa_op_ctx tmpl;
  399. mv_cesa_set_op_cfg(&tmpl,
  400. CESA_SA_DESC_CFG_CRYPTCM_ECB |
  401. CESA_SA_DESC_CFG_DIR_DEC);
  402. return mv_cesa_des_op(req, &tmpl);
  403. }
  404. struct crypto_alg mv_cesa_ecb_des_alg = {
  405. .cra_name = "ecb(des)",
  406. .cra_driver_name = "mv-ecb-des",
  407. .cra_priority = 300,
  408. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  409. CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
  410. .cra_blocksize = DES_BLOCK_SIZE,
  411. .cra_ctxsize = sizeof(struct mv_cesa_des_ctx),
  412. .cra_alignmask = 0,
  413. .cra_type = &crypto_ablkcipher_type,
  414. .cra_module = THIS_MODULE,
  415. .cra_init = mv_cesa_ablkcipher_cra_init,
  416. .cra_u = {
  417. .ablkcipher = {
  418. .min_keysize = DES_KEY_SIZE,
  419. .max_keysize = DES_KEY_SIZE,
  420. .setkey = mv_cesa_des_setkey,
  421. .encrypt = mv_cesa_ecb_des_encrypt,
  422. .decrypt = mv_cesa_ecb_des_decrypt,
  423. },
  424. },
  425. };
  426. static int mv_cesa_cbc_des_op(struct ablkcipher_request *req,
  427. struct mv_cesa_op_ctx *tmpl)
  428. {
  429. mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTCM_CBC,
  430. CESA_SA_DESC_CFG_CRYPTCM_MSK);
  431. memcpy(tmpl->ctx.blkcipher.iv, req->info, DES_BLOCK_SIZE);
  432. return mv_cesa_des_op(req, tmpl);
  433. }
  434. static int mv_cesa_cbc_des_encrypt(struct ablkcipher_request *req)
  435. {
  436. struct mv_cesa_op_ctx tmpl;
  437. mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_ENC);
  438. return mv_cesa_cbc_des_op(req, &tmpl);
  439. }
  440. static int mv_cesa_cbc_des_decrypt(struct ablkcipher_request *req)
  441. {
  442. struct mv_cesa_op_ctx tmpl;
  443. mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_DEC);
  444. return mv_cesa_cbc_des_op(req, &tmpl);
  445. }
  446. struct crypto_alg mv_cesa_cbc_des_alg = {
  447. .cra_name = "cbc(des)",
  448. .cra_driver_name = "mv-cbc-des",
  449. .cra_priority = 300,
  450. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  451. CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
  452. .cra_blocksize = DES_BLOCK_SIZE,
  453. .cra_ctxsize = sizeof(struct mv_cesa_des_ctx),
  454. .cra_alignmask = 0,
  455. .cra_type = &crypto_ablkcipher_type,
  456. .cra_module = THIS_MODULE,
  457. .cra_init = mv_cesa_ablkcipher_cra_init,
  458. .cra_u = {
  459. .ablkcipher = {
  460. .min_keysize = DES_KEY_SIZE,
  461. .max_keysize = DES_KEY_SIZE,
  462. .ivsize = DES_BLOCK_SIZE,
  463. .setkey = mv_cesa_des_setkey,
  464. .encrypt = mv_cesa_cbc_des_encrypt,
  465. .decrypt = mv_cesa_cbc_des_decrypt,
  466. },
  467. },
  468. };
  469. static int mv_cesa_des3_op(struct ablkcipher_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_ablkcipher_queue_req(req, tmpl);
  477. }
  478. static int mv_cesa_ecb_des3_ede_encrypt(struct ablkcipher_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 ablkcipher_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 crypto_alg mv_cesa_ecb_des3_ede_alg = {
  497. .cra_name = "ecb(des3_ede)",
  498. .cra_driver_name = "mv-ecb-des3-ede",
  499. .cra_priority = 300,
  500. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  501. CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
  502. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  503. .cra_ctxsize = sizeof(struct mv_cesa_des3_ctx),
  504. .cra_alignmask = 0,
  505. .cra_type = &crypto_ablkcipher_type,
  506. .cra_module = THIS_MODULE,
  507. .cra_init = mv_cesa_ablkcipher_cra_init,
  508. .cra_u = {
  509. .ablkcipher = {
  510. .min_keysize = DES3_EDE_KEY_SIZE,
  511. .max_keysize = DES3_EDE_KEY_SIZE,
  512. .ivsize = DES3_EDE_BLOCK_SIZE,
  513. .setkey = mv_cesa_des3_ede_setkey,
  514. .encrypt = mv_cesa_ecb_des3_ede_encrypt,
  515. .decrypt = mv_cesa_ecb_des3_ede_decrypt,
  516. },
  517. },
  518. };
  519. static int mv_cesa_cbc_des3_op(struct ablkcipher_request *req,
  520. struct mv_cesa_op_ctx *tmpl)
  521. {
  522. memcpy(tmpl->ctx.blkcipher.iv, req->info, DES3_EDE_BLOCK_SIZE);
  523. return mv_cesa_des3_op(req, tmpl);
  524. }
  525. static int mv_cesa_cbc_des3_ede_encrypt(struct ablkcipher_request *req)
  526. {
  527. struct mv_cesa_op_ctx tmpl;
  528. mv_cesa_set_op_cfg(&tmpl,
  529. CESA_SA_DESC_CFG_CRYPTCM_CBC |
  530. CESA_SA_DESC_CFG_3DES_EDE |
  531. CESA_SA_DESC_CFG_DIR_ENC);
  532. return mv_cesa_cbc_des3_op(req, &tmpl);
  533. }
  534. static int mv_cesa_cbc_des3_ede_decrypt(struct ablkcipher_request *req)
  535. {
  536. struct mv_cesa_op_ctx tmpl;
  537. mv_cesa_set_op_cfg(&tmpl,
  538. CESA_SA_DESC_CFG_CRYPTCM_CBC |
  539. CESA_SA_DESC_CFG_3DES_EDE |
  540. CESA_SA_DESC_CFG_DIR_DEC);
  541. return mv_cesa_cbc_des3_op(req, &tmpl);
  542. }
  543. struct crypto_alg mv_cesa_cbc_des3_ede_alg = {
  544. .cra_name = "cbc(des3_ede)",
  545. .cra_driver_name = "mv-cbc-des3-ede",
  546. .cra_priority = 300,
  547. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  548. CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
  549. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  550. .cra_ctxsize = sizeof(struct mv_cesa_des3_ctx),
  551. .cra_alignmask = 0,
  552. .cra_type = &crypto_ablkcipher_type,
  553. .cra_module = THIS_MODULE,
  554. .cra_init = mv_cesa_ablkcipher_cra_init,
  555. .cra_u = {
  556. .ablkcipher = {
  557. .min_keysize = DES3_EDE_KEY_SIZE,
  558. .max_keysize = DES3_EDE_KEY_SIZE,
  559. .ivsize = DES3_EDE_BLOCK_SIZE,
  560. .setkey = mv_cesa_des3_ede_setkey,
  561. .encrypt = mv_cesa_cbc_des3_ede_encrypt,
  562. .decrypt = mv_cesa_cbc_des3_ede_decrypt,
  563. },
  564. },
  565. };
  566. static int mv_cesa_aes_op(struct ablkcipher_request *req,
  567. struct mv_cesa_op_ctx *tmpl)
  568. {
  569. struct mv_cesa_aes_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  570. int i;
  571. u32 *key;
  572. u32 cfg;
  573. cfg = CESA_SA_DESC_CFG_CRYPTM_AES;
  574. if (mv_cesa_get_op_cfg(tmpl) & CESA_SA_DESC_CFG_DIR_DEC)
  575. key = ctx->aes.key_dec;
  576. else
  577. key = ctx->aes.key_enc;
  578. for (i = 0; i < ctx->aes.key_length / sizeof(u32); i++)
  579. tmpl->ctx.blkcipher.key[i] = cpu_to_le32(key[i]);
  580. if (ctx->aes.key_length == 24)
  581. cfg |= CESA_SA_DESC_CFG_AES_LEN_192;
  582. else if (ctx->aes.key_length == 32)
  583. cfg |= CESA_SA_DESC_CFG_AES_LEN_256;
  584. mv_cesa_update_op_cfg(tmpl, cfg,
  585. CESA_SA_DESC_CFG_CRYPTM_MSK |
  586. CESA_SA_DESC_CFG_AES_LEN_MSK);
  587. return mv_cesa_ablkcipher_queue_req(req, tmpl);
  588. }
  589. static int mv_cesa_ecb_aes_encrypt(struct ablkcipher_request *req)
  590. {
  591. struct mv_cesa_op_ctx tmpl;
  592. mv_cesa_set_op_cfg(&tmpl,
  593. CESA_SA_DESC_CFG_CRYPTCM_ECB |
  594. CESA_SA_DESC_CFG_DIR_ENC);
  595. return mv_cesa_aes_op(req, &tmpl);
  596. }
  597. static int mv_cesa_ecb_aes_decrypt(struct ablkcipher_request *req)
  598. {
  599. struct mv_cesa_op_ctx tmpl;
  600. mv_cesa_set_op_cfg(&tmpl,
  601. CESA_SA_DESC_CFG_CRYPTCM_ECB |
  602. CESA_SA_DESC_CFG_DIR_DEC);
  603. return mv_cesa_aes_op(req, &tmpl);
  604. }
  605. struct crypto_alg mv_cesa_ecb_aes_alg = {
  606. .cra_name = "ecb(aes)",
  607. .cra_driver_name = "mv-ecb-aes",
  608. .cra_priority = 300,
  609. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  610. CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
  611. .cra_blocksize = AES_BLOCK_SIZE,
  612. .cra_ctxsize = sizeof(struct mv_cesa_aes_ctx),
  613. .cra_alignmask = 0,
  614. .cra_type = &crypto_ablkcipher_type,
  615. .cra_module = THIS_MODULE,
  616. .cra_init = mv_cesa_ablkcipher_cra_init,
  617. .cra_u = {
  618. .ablkcipher = {
  619. .min_keysize = AES_MIN_KEY_SIZE,
  620. .max_keysize = AES_MAX_KEY_SIZE,
  621. .setkey = mv_cesa_aes_setkey,
  622. .encrypt = mv_cesa_ecb_aes_encrypt,
  623. .decrypt = mv_cesa_ecb_aes_decrypt,
  624. },
  625. },
  626. };
  627. static int mv_cesa_cbc_aes_op(struct ablkcipher_request *req,
  628. struct mv_cesa_op_ctx *tmpl)
  629. {
  630. mv_cesa_update_op_cfg(tmpl, CESA_SA_DESC_CFG_CRYPTCM_CBC,
  631. CESA_SA_DESC_CFG_CRYPTCM_MSK);
  632. memcpy(tmpl->ctx.blkcipher.iv, req->info, AES_BLOCK_SIZE);
  633. return mv_cesa_aes_op(req, tmpl);
  634. }
  635. static int mv_cesa_cbc_aes_encrypt(struct ablkcipher_request *req)
  636. {
  637. struct mv_cesa_op_ctx tmpl;
  638. mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_ENC);
  639. return mv_cesa_cbc_aes_op(req, &tmpl);
  640. }
  641. static int mv_cesa_cbc_aes_decrypt(struct ablkcipher_request *req)
  642. {
  643. struct mv_cesa_op_ctx tmpl;
  644. mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_DIR_DEC);
  645. return mv_cesa_cbc_aes_op(req, &tmpl);
  646. }
  647. struct crypto_alg mv_cesa_cbc_aes_alg = {
  648. .cra_name = "cbc(aes)",
  649. .cra_driver_name = "mv-cbc-aes",
  650. .cra_priority = 300,
  651. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  652. CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
  653. .cra_blocksize = AES_BLOCK_SIZE,
  654. .cra_ctxsize = sizeof(struct mv_cesa_aes_ctx),
  655. .cra_alignmask = 0,
  656. .cra_type = &crypto_ablkcipher_type,
  657. .cra_module = THIS_MODULE,
  658. .cra_init = mv_cesa_ablkcipher_cra_init,
  659. .cra_u = {
  660. .ablkcipher = {
  661. .min_keysize = AES_MIN_KEY_SIZE,
  662. .max_keysize = AES_MAX_KEY_SIZE,
  663. .ivsize = AES_BLOCK_SIZE,
  664. .setkey = mv_cesa_aes_setkey,
  665. .encrypt = mv_cesa_cbc_aes_encrypt,
  666. .decrypt = mv_cesa_cbc_aes_decrypt,
  667. },
  668. },
  669. };