cptvf_algs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * Copyright (C) 2016 Cavium, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License
  6. * as published by the Free Software Foundation.
  7. */
  8. #include <crypto/aes.h>
  9. #include <crypto/algapi.h>
  10. #include <crypto/authenc.h>
  11. #include <crypto/cryptd.h>
  12. #include <crypto/crypto_wq.h>
  13. #include <crypto/des.h>
  14. #include <crypto/xts.h>
  15. #include <linux/crypto.h>
  16. #include <linux/err.h>
  17. #include <linux/list.h>
  18. #include <linux/scatterlist.h>
  19. #include "cptvf.h"
  20. #include "cptvf_algs.h"
  21. struct cpt_device_handle {
  22. void *cdev[MAX_DEVICES];
  23. u32 dev_count;
  24. };
  25. static struct cpt_device_handle dev_handle;
  26. static void cvm_callback(u32 status, void *arg)
  27. {
  28. struct crypto_async_request *req = (struct crypto_async_request *)arg;
  29. req->complete(req, !status);
  30. }
  31. static inline void update_input_iv(struct cpt_request_info *req_info,
  32. u8 *iv, u32 enc_iv_len,
  33. u32 *argcnt)
  34. {
  35. /* Setting the iv information */
  36. req_info->in[*argcnt].vptr = (void *)iv;
  37. req_info->in[*argcnt].size = enc_iv_len;
  38. req_info->req.dlen += enc_iv_len;
  39. ++(*argcnt);
  40. }
  41. static inline void update_output_iv(struct cpt_request_info *req_info,
  42. u8 *iv, u32 enc_iv_len,
  43. u32 *argcnt)
  44. {
  45. /* Setting the iv information */
  46. req_info->out[*argcnt].vptr = (void *)iv;
  47. req_info->out[*argcnt].size = enc_iv_len;
  48. req_info->rlen += enc_iv_len;
  49. ++(*argcnt);
  50. }
  51. static inline void update_input_data(struct cpt_request_info *req_info,
  52. struct scatterlist *inp_sg,
  53. u32 nbytes, u32 *argcnt)
  54. {
  55. req_info->req.dlen += nbytes;
  56. while (nbytes) {
  57. u32 len = min(nbytes, inp_sg->length);
  58. u8 *ptr = sg_virt(inp_sg);
  59. req_info->in[*argcnt].vptr = (void *)ptr;
  60. req_info->in[*argcnt].size = len;
  61. nbytes -= len;
  62. ++(*argcnt);
  63. ++inp_sg;
  64. }
  65. }
  66. static inline void update_output_data(struct cpt_request_info *req_info,
  67. struct scatterlist *outp_sg,
  68. u32 nbytes, u32 *argcnt)
  69. {
  70. req_info->rlen += nbytes;
  71. while (nbytes) {
  72. u32 len = min(nbytes, outp_sg->length);
  73. u8 *ptr = sg_virt(outp_sg);
  74. req_info->out[*argcnt].vptr = (void *)ptr;
  75. req_info->out[*argcnt].size = len;
  76. nbytes -= len;
  77. ++(*argcnt);
  78. ++outp_sg;
  79. }
  80. }
  81. static inline u32 create_ctx_hdr(struct ablkcipher_request *req, u32 enc,
  82. u32 *argcnt)
  83. {
  84. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  85. struct cvm_enc_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  86. struct cvm_req_ctx *rctx = ablkcipher_request_ctx(req);
  87. struct fc_context *fctx = &rctx->fctx;
  88. u64 *offset_control = &rctx->control_word;
  89. u32 enc_iv_len = crypto_ablkcipher_ivsize(tfm);
  90. struct cpt_request_info *req_info = &rctx->cpt_req;
  91. u64 *ctrl_flags = NULL;
  92. req_info->ctrl.s.grp = 0;
  93. req_info->ctrl.s.dma_mode = DMA_GATHER_SCATTER;
  94. req_info->ctrl.s.se_req = SE_CORE_REQ;
  95. req_info->req.opcode.s.major = MAJOR_OP_FC |
  96. DMA_MODE_FLAG(DMA_GATHER_SCATTER);
  97. if (enc)
  98. req_info->req.opcode.s.minor = 2;
  99. else
  100. req_info->req.opcode.s.minor = 3;
  101. req_info->req.param1 = req->nbytes; /* Encryption Data length */
  102. req_info->req.param2 = 0; /*Auth data length */
  103. fctx->enc.enc_ctrl.e.enc_cipher = ctx->cipher_type;
  104. fctx->enc.enc_ctrl.e.aes_key = ctx->key_type;
  105. fctx->enc.enc_ctrl.e.iv_source = FROM_DPTR;
  106. if (ctx->cipher_type == AES_XTS)
  107. memcpy(fctx->enc.encr_key, ctx->enc_key, ctx->key_len * 2);
  108. else
  109. memcpy(fctx->enc.encr_key, ctx->enc_key, ctx->key_len);
  110. ctrl_flags = (u64 *)&fctx->enc.enc_ctrl.flags;
  111. *ctrl_flags = cpu_to_be64(*ctrl_flags);
  112. *offset_control = cpu_to_be64(((u64)(enc_iv_len) << 16));
  113. /* Storing Packet Data Information in offset
  114. * Control Word First 8 bytes
  115. */
  116. req_info->in[*argcnt].vptr = (u8 *)offset_control;
  117. req_info->in[*argcnt].size = CONTROL_WORD_LEN;
  118. req_info->req.dlen += CONTROL_WORD_LEN;
  119. ++(*argcnt);
  120. req_info->in[*argcnt].vptr = (u8 *)fctx;
  121. req_info->in[*argcnt].size = sizeof(struct fc_context);
  122. req_info->req.dlen += sizeof(struct fc_context);
  123. ++(*argcnt);
  124. return 0;
  125. }
  126. static inline u32 create_input_list(struct ablkcipher_request *req, u32 enc,
  127. u32 enc_iv_len)
  128. {
  129. struct cvm_req_ctx *rctx = ablkcipher_request_ctx(req);
  130. struct cpt_request_info *req_info = &rctx->cpt_req;
  131. u32 argcnt = 0;
  132. create_ctx_hdr(req, enc, &argcnt);
  133. update_input_iv(req_info, req->info, enc_iv_len, &argcnt);
  134. update_input_data(req_info, req->src, req->nbytes, &argcnt);
  135. req_info->incnt = argcnt;
  136. return 0;
  137. }
  138. static inline void store_cb_info(struct ablkcipher_request *req,
  139. struct cpt_request_info *req_info)
  140. {
  141. req_info->callback = (void *)cvm_callback;
  142. req_info->callback_arg = (void *)&req->base;
  143. }
  144. static inline void create_output_list(struct ablkcipher_request *req,
  145. u32 enc_iv_len)
  146. {
  147. struct cvm_req_ctx *rctx = ablkcipher_request_ctx(req);
  148. struct cpt_request_info *req_info = &rctx->cpt_req;
  149. u32 argcnt = 0;
  150. /* OUTPUT Buffer Processing
  151. * AES encryption/decryption output would be
  152. * received in the following format
  153. *
  154. * ------IV--------|------ENCRYPTED/DECRYPTED DATA-----|
  155. * [ 16 Bytes/ [ Request Enc/Dec/ DATA Len AES CBC ]
  156. */
  157. /* Reading IV information */
  158. update_output_iv(req_info, req->info, enc_iv_len, &argcnt);
  159. update_output_data(req_info, req->dst, req->nbytes, &argcnt);
  160. req_info->outcnt = argcnt;
  161. }
  162. static inline int cvm_enc_dec(struct ablkcipher_request *req, u32 enc)
  163. {
  164. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  165. struct cvm_req_ctx *rctx = ablkcipher_request_ctx(req);
  166. u32 enc_iv_len = crypto_ablkcipher_ivsize(tfm);
  167. struct fc_context *fctx = &rctx->fctx;
  168. struct cpt_request_info *req_info = &rctx->cpt_req;
  169. void *cdev = NULL;
  170. int status;
  171. memset(req_info, 0, sizeof(struct cpt_request_info));
  172. memset(fctx, 0, sizeof(struct fc_context));
  173. create_input_list(req, enc, enc_iv_len);
  174. create_output_list(req, enc_iv_len);
  175. store_cb_info(req, req_info);
  176. cdev = dev_handle.cdev[smp_processor_id()];
  177. status = cptvf_do_request(cdev, req_info);
  178. /* We perform an asynchronous send and once
  179. * the request is completed the driver would
  180. * intimate through registered call back functions
  181. */
  182. if (status)
  183. return status;
  184. else
  185. return -EINPROGRESS;
  186. }
  187. static int cvm_encrypt(struct ablkcipher_request *req)
  188. {
  189. return cvm_enc_dec(req, true);
  190. }
  191. static int cvm_decrypt(struct ablkcipher_request *req)
  192. {
  193. return cvm_enc_dec(req, false);
  194. }
  195. static int cvm_xts_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  196. u32 keylen)
  197. {
  198. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  199. struct cvm_enc_ctx *ctx = crypto_tfm_ctx(tfm);
  200. int err;
  201. const u8 *key1 = key;
  202. const u8 *key2 = key + (keylen / 2);
  203. err = xts_check_key(tfm, key, keylen);
  204. if (err)
  205. return err;
  206. ctx->key_len = keylen;
  207. memcpy(ctx->enc_key, key1, keylen / 2);
  208. memcpy(ctx->enc_key + KEY2_OFFSET, key2, keylen / 2);
  209. ctx->cipher_type = AES_XTS;
  210. switch (ctx->key_len) {
  211. case 32:
  212. ctx->key_type = AES_128_BIT;
  213. break;
  214. case 64:
  215. ctx->key_type = AES_256_BIT;
  216. break;
  217. default:
  218. return -EINVAL;
  219. }
  220. return 0;
  221. }
  222. static int cvm_validate_keylen(struct cvm_enc_ctx *ctx, u32 keylen)
  223. {
  224. if ((keylen == 16) || (keylen == 24) || (keylen == 32)) {
  225. ctx->key_len = keylen;
  226. switch (ctx->key_len) {
  227. case 16:
  228. ctx->key_type = AES_128_BIT;
  229. break;
  230. case 24:
  231. ctx->key_type = AES_192_BIT;
  232. break;
  233. case 32:
  234. ctx->key_type = AES_256_BIT;
  235. break;
  236. default:
  237. return -EINVAL;
  238. }
  239. if (ctx->cipher_type == DES3_CBC)
  240. ctx->key_type = 0;
  241. return 0;
  242. }
  243. return -EINVAL;
  244. }
  245. static int cvm_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  246. u32 keylen, u8 cipher_type)
  247. {
  248. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  249. struct cvm_enc_ctx *ctx = crypto_tfm_ctx(tfm);
  250. ctx->cipher_type = cipher_type;
  251. if (!cvm_validate_keylen(ctx, keylen)) {
  252. memcpy(ctx->enc_key, key, keylen);
  253. return 0;
  254. } else {
  255. crypto_ablkcipher_set_flags(cipher,
  256. CRYPTO_TFM_RES_BAD_KEY_LEN);
  257. return -EINVAL;
  258. }
  259. }
  260. static int cvm_cbc_aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  261. u32 keylen)
  262. {
  263. return cvm_setkey(cipher, key, keylen, AES_CBC);
  264. }
  265. static int cvm_ecb_aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  266. u32 keylen)
  267. {
  268. return cvm_setkey(cipher, key, keylen, AES_ECB);
  269. }
  270. static int cvm_cfb_aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  271. u32 keylen)
  272. {
  273. return cvm_setkey(cipher, key, keylen, AES_CFB);
  274. }
  275. static int cvm_cbc_des3_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  276. u32 keylen)
  277. {
  278. return cvm_setkey(cipher, key, keylen, DES3_CBC);
  279. }
  280. static int cvm_ecb_des3_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  281. u32 keylen)
  282. {
  283. return cvm_setkey(cipher, key, keylen, DES3_ECB);
  284. }
  285. static int cvm_enc_dec_init(struct crypto_tfm *tfm)
  286. {
  287. struct cvm_enc_ctx *ctx = crypto_tfm_ctx(tfm);
  288. memset(ctx, 0, sizeof(*ctx));
  289. tfm->crt_ablkcipher.reqsize = sizeof(struct cvm_req_ctx) +
  290. sizeof(struct ablkcipher_request);
  291. /* Additional memory for ablkcipher_request is
  292. * allocated since the cryptd daemon uses
  293. * this memory for request_ctx information
  294. */
  295. return 0;
  296. }
  297. static struct crypto_alg algs[] = { {
  298. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  299. .cra_blocksize = AES_BLOCK_SIZE,
  300. .cra_ctxsize = sizeof(struct cvm_enc_ctx),
  301. .cra_alignmask = 7,
  302. .cra_priority = 4001,
  303. .cra_name = "xts(aes)",
  304. .cra_driver_name = "cavium-xts-aes",
  305. .cra_type = &crypto_ablkcipher_type,
  306. .cra_u = {
  307. .ablkcipher = {
  308. .ivsize = AES_BLOCK_SIZE,
  309. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  310. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  311. .setkey = cvm_xts_setkey,
  312. .encrypt = cvm_encrypt,
  313. .decrypt = cvm_decrypt,
  314. },
  315. },
  316. .cra_init = cvm_enc_dec_init,
  317. .cra_module = THIS_MODULE,
  318. }, {
  319. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  320. .cra_blocksize = AES_BLOCK_SIZE,
  321. .cra_ctxsize = sizeof(struct cvm_enc_ctx),
  322. .cra_alignmask = 7,
  323. .cra_priority = 4001,
  324. .cra_name = "cbc(aes)",
  325. .cra_driver_name = "cavium-cbc-aes",
  326. .cra_type = &crypto_ablkcipher_type,
  327. .cra_u = {
  328. .ablkcipher = {
  329. .ivsize = AES_BLOCK_SIZE,
  330. .min_keysize = AES_MIN_KEY_SIZE,
  331. .max_keysize = AES_MAX_KEY_SIZE,
  332. .setkey = cvm_cbc_aes_setkey,
  333. .encrypt = cvm_encrypt,
  334. .decrypt = cvm_decrypt,
  335. },
  336. },
  337. .cra_init = cvm_enc_dec_init,
  338. .cra_module = THIS_MODULE,
  339. }, {
  340. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  341. .cra_blocksize = AES_BLOCK_SIZE,
  342. .cra_ctxsize = sizeof(struct cvm_enc_ctx),
  343. .cra_alignmask = 7,
  344. .cra_priority = 4001,
  345. .cra_name = "ecb(aes)",
  346. .cra_driver_name = "cavium-ecb-aes",
  347. .cra_type = &crypto_ablkcipher_type,
  348. .cra_u = {
  349. .ablkcipher = {
  350. .ivsize = AES_BLOCK_SIZE,
  351. .min_keysize = AES_MIN_KEY_SIZE,
  352. .max_keysize = AES_MAX_KEY_SIZE,
  353. .setkey = cvm_ecb_aes_setkey,
  354. .encrypt = cvm_encrypt,
  355. .decrypt = cvm_decrypt,
  356. },
  357. },
  358. .cra_init = cvm_enc_dec_init,
  359. .cra_module = THIS_MODULE,
  360. }, {
  361. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  362. .cra_blocksize = AES_BLOCK_SIZE,
  363. .cra_ctxsize = sizeof(struct cvm_enc_ctx),
  364. .cra_alignmask = 7,
  365. .cra_priority = 4001,
  366. .cra_name = "cfb(aes)",
  367. .cra_driver_name = "cavium-cfb-aes",
  368. .cra_type = &crypto_ablkcipher_type,
  369. .cra_u = {
  370. .ablkcipher = {
  371. .ivsize = AES_BLOCK_SIZE,
  372. .min_keysize = AES_MIN_KEY_SIZE,
  373. .max_keysize = AES_MAX_KEY_SIZE,
  374. .setkey = cvm_cfb_aes_setkey,
  375. .encrypt = cvm_encrypt,
  376. .decrypt = cvm_decrypt,
  377. },
  378. },
  379. .cra_init = cvm_enc_dec_init,
  380. .cra_module = THIS_MODULE,
  381. }, {
  382. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  383. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  384. .cra_ctxsize = sizeof(struct cvm_des3_ctx),
  385. .cra_alignmask = 7,
  386. .cra_priority = 4001,
  387. .cra_name = "cbc(des3_ede)",
  388. .cra_driver_name = "cavium-cbc-des3_ede",
  389. .cra_type = &crypto_ablkcipher_type,
  390. .cra_u = {
  391. .ablkcipher = {
  392. .min_keysize = DES3_EDE_KEY_SIZE,
  393. .max_keysize = DES3_EDE_KEY_SIZE,
  394. .ivsize = DES_BLOCK_SIZE,
  395. .setkey = cvm_cbc_des3_setkey,
  396. .encrypt = cvm_encrypt,
  397. .decrypt = cvm_decrypt,
  398. },
  399. },
  400. .cra_init = cvm_enc_dec_init,
  401. .cra_module = THIS_MODULE,
  402. }, {
  403. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  404. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  405. .cra_ctxsize = sizeof(struct cvm_des3_ctx),
  406. .cra_alignmask = 7,
  407. .cra_priority = 4001,
  408. .cra_name = "ecb(des3_ede)",
  409. .cra_driver_name = "cavium-ecb-des3_ede",
  410. .cra_type = &crypto_ablkcipher_type,
  411. .cra_u = {
  412. .ablkcipher = {
  413. .min_keysize = DES3_EDE_KEY_SIZE,
  414. .max_keysize = DES3_EDE_KEY_SIZE,
  415. .ivsize = DES_BLOCK_SIZE,
  416. .setkey = cvm_ecb_des3_setkey,
  417. .encrypt = cvm_encrypt,
  418. .decrypt = cvm_decrypt,
  419. },
  420. },
  421. .cra_init = cvm_enc_dec_init,
  422. .cra_module = THIS_MODULE,
  423. } };
  424. static inline int cav_register_algs(void)
  425. {
  426. int err = 0;
  427. err = crypto_register_algs(algs, ARRAY_SIZE(algs));
  428. if (err)
  429. return err;
  430. return 0;
  431. }
  432. static inline void cav_unregister_algs(void)
  433. {
  434. crypto_unregister_algs(algs, ARRAY_SIZE(algs));
  435. }
  436. int cvm_crypto_init(struct cpt_vf *cptvf)
  437. {
  438. struct pci_dev *pdev = cptvf->pdev;
  439. u32 dev_count;
  440. dev_count = dev_handle.dev_count;
  441. dev_handle.cdev[dev_count] = cptvf;
  442. dev_handle.dev_count++;
  443. if (dev_count == 3) {
  444. if (cav_register_algs()) {
  445. dev_err(&pdev->dev, "Error in registering crypto algorithms\n");
  446. return -EINVAL;
  447. }
  448. }
  449. return 0;
  450. }
  451. void cvm_crypto_exit(void)
  452. {
  453. u32 dev_count;
  454. dev_count = --dev_handle.dev_count;
  455. if (!dev_count)
  456. cav_unregister_algs();
  457. }