PageRenderTime 62ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/crypto/cavium/nitrox/nitrox_skcipher.c

https://gitlab.com/Skylake/Staging
C | 494 lines | 414 code | 62 blank | 18 comment | 17 complexity | 5134ad1068a8cc6be0f34d227ff73634 MD5 | raw file
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/crypto.h>
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/printk.h>
  6. #include <crypto/aes.h>
  7. #include <crypto/skcipher.h>
  8. #include <crypto/ctr.h>
  9. #include <crypto/des.h>
  10. #include <crypto/xts.h>
  11. #include "nitrox_dev.h"
  12. #include "nitrox_common.h"
  13. #include "nitrox_req.h"
  14. struct nitrox_cipher {
  15. const char *name;
  16. enum flexi_cipher value;
  17. };
  18. /**
  19. * supported cipher list
  20. */
  21. static const struct nitrox_cipher flexi_cipher_table[] = {
  22. { "null", CIPHER_NULL },
  23. { "cbc(des3_ede)", CIPHER_3DES_CBC },
  24. { "ecb(des3_ede)", CIPHER_3DES_ECB },
  25. { "cbc(aes)", CIPHER_AES_CBC },
  26. { "ecb(aes)", CIPHER_AES_ECB },
  27. { "cfb(aes)", CIPHER_AES_CFB },
  28. { "rfc3686(ctr(aes))", CIPHER_AES_CTR },
  29. { "xts(aes)", CIPHER_AES_XTS },
  30. { "cts(cbc(aes))", CIPHER_AES_CBC_CTS },
  31. { NULL, CIPHER_INVALID }
  32. };
  33. static enum flexi_cipher flexi_cipher_type(const char *name)
  34. {
  35. const struct nitrox_cipher *cipher = flexi_cipher_table;
  36. while (cipher->name) {
  37. if (!strcmp(cipher->name, name))
  38. break;
  39. cipher++;
  40. }
  41. return cipher->value;
  42. }
  43. static int nitrox_skcipher_init(struct crypto_skcipher *tfm)
  44. {
  45. struct nitrox_crypto_ctx *nctx = crypto_skcipher_ctx(tfm);
  46. struct crypto_ctx_hdr *chdr;
  47. /* get the first device */
  48. nctx->ndev = nitrox_get_first_device();
  49. if (!nctx->ndev)
  50. return -ENODEV;
  51. /* allocate nitrox crypto context */
  52. chdr = crypto_alloc_context(nctx->ndev);
  53. if (!chdr) {
  54. nitrox_put_device(nctx->ndev);
  55. return -ENOMEM;
  56. }
  57. nctx->chdr = chdr;
  58. nctx->u.ctx_handle = (uintptr_t)((u8 *)chdr->vaddr +
  59. sizeof(struct ctx_hdr));
  60. crypto_skcipher_set_reqsize(tfm, crypto_skcipher_reqsize(tfm) +
  61. sizeof(struct nitrox_kcrypt_request));
  62. return 0;
  63. }
  64. static void nitrox_skcipher_exit(struct crypto_skcipher *tfm)
  65. {
  66. struct nitrox_crypto_ctx *nctx = crypto_skcipher_ctx(tfm);
  67. /* free the nitrox crypto context */
  68. if (nctx->u.ctx_handle) {
  69. struct flexi_crypto_context *fctx = nctx->u.fctx;
  70. memzero_explicit(&fctx->crypto, sizeof(struct crypto_keys));
  71. memzero_explicit(&fctx->auth, sizeof(struct auth_keys));
  72. crypto_free_context((void *)nctx->chdr);
  73. }
  74. nitrox_put_device(nctx->ndev);
  75. nctx->u.ctx_handle = 0;
  76. nctx->ndev = NULL;
  77. }
  78. static inline int nitrox_skcipher_setkey(struct crypto_skcipher *cipher,
  79. int aes_keylen, const u8 *key,
  80. unsigned int keylen)
  81. {
  82. struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
  83. struct nitrox_crypto_ctx *nctx = crypto_tfm_ctx(tfm);
  84. struct flexi_crypto_context *fctx;
  85. union fc_ctx_flags *flags;
  86. enum flexi_cipher cipher_type;
  87. const char *name;
  88. name = crypto_tfm_alg_name(tfm);
  89. cipher_type = flexi_cipher_type(name);
  90. if (unlikely(cipher_type == CIPHER_INVALID)) {
  91. pr_err("unsupported cipher: %s\n", name);
  92. return -EINVAL;
  93. }
  94. /* fill crypto context */
  95. fctx = nctx->u.fctx;
  96. flags = &fctx->flags;
  97. flags->f = 0;
  98. flags->w0.cipher_type = cipher_type;
  99. flags->w0.aes_keylen = aes_keylen;
  100. flags->w0.iv_source = IV_FROM_DPTR;
  101. flags->f = cpu_to_be64(*(u64 *)&flags->w0);
  102. /* copy the key to context */
  103. memcpy(fctx->crypto.u.key, key, keylen);
  104. return 0;
  105. }
  106. static int nitrox_aes_setkey(struct crypto_skcipher *cipher, const u8 *key,
  107. unsigned int keylen)
  108. {
  109. int aes_keylen;
  110. aes_keylen = flexi_aes_keylen(keylen);
  111. if (aes_keylen < 0) {
  112. crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  113. return -EINVAL;
  114. }
  115. return nitrox_skcipher_setkey(cipher, aes_keylen, key, keylen);
  116. }
  117. static int alloc_src_sglist(struct skcipher_request *skreq, int ivsize)
  118. {
  119. struct nitrox_kcrypt_request *nkreq = skcipher_request_ctx(skreq);
  120. int nents = sg_nents(skreq->src) + 1;
  121. int ret;
  122. /* Allocate buffer to hold IV and input scatterlist array */
  123. ret = alloc_src_req_buf(nkreq, nents, ivsize);
  124. if (ret)
  125. return ret;
  126. nitrox_creq_copy_iv(nkreq->src, skreq->iv, ivsize);
  127. nitrox_creq_set_src_sg(nkreq, nents, ivsize, skreq->src,
  128. skreq->cryptlen);
  129. return 0;
  130. }
  131. static int alloc_dst_sglist(struct skcipher_request *skreq, int ivsize)
  132. {
  133. struct nitrox_kcrypt_request *nkreq = skcipher_request_ctx(skreq);
  134. int nents = sg_nents(skreq->dst) + 3;
  135. int ret;
  136. /* Allocate buffer to hold ORH, COMPLETION and output scatterlist
  137. * array
  138. */
  139. ret = alloc_dst_req_buf(nkreq, nents);
  140. if (ret)
  141. return ret;
  142. nitrox_creq_set_orh(nkreq);
  143. nitrox_creq_set_comp(nkreq);
  144. nitrox_creq_set_dst_sg(nkreq, nents, ivsize, skreq->dst,
  145. skreq->cryptlen);
  146. return 0;
  147. }
  148. static void free_src_sglist(struct skcipher_request *skreq)
  149. {
  150. struct nitrox_kcrypt_request *nkreq = skcipher_request_ctx(skreq);
  151. kfree(nkreq->src);
  152. }
  153. static void free_dst_sglist(struct skcipher_request *skreq)
  154. {
  155. struct nitrox_kcrypt_request *nkreq = skcipher_request_ctx(skreq);
  156. kfree(nkreq->dst);
  157. }
  158. static void nitrox_skcipher_callback(void *arg, int err)
  159. {
  160. struct skcipher_request *skreq = arg;
  161. free_src_sglist(skreq);
  162. free_dst_sglist(skreq);
  163. if (err) {
  164. pr_err_ratelimited("request failed status 0x%0x\n", err);
  165. err = -EINVAL;
  166. }
  167. skcipher_request_complete(skreq, err);
  168. }
  169. static int nitrox_skcipher_crypt(struct skcipher_request *skreq, bool enc)
  170. {
  171. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(skreq);
  172. struct nitrox_crypto_ctx *nctx = crypto_skcipher_ctx(cipher);
  173. struct nitrox_kcrypt_request *nkreq = skcipher_request_ctx(skreq);
  174. int ivsize = crypto_skcipher_ivsize(cipher);
  175. struct se_crypto_request *creq;
  176. int ret;
  177. creq = &nkreq->creq;
  178. creq->flags = skreq->base.flags;
  179. creq->gfp = (skreq->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  180. GFP_KERNEL : GFP_ATOMIC;
  181. /* fill the request */
  182. creq->ctrl.value = 0;
  183. creq->opcode = FLEXI_CRYPTO_ENCRYPT_HMAC;
  184. creq->ctrl.s.arg = (enc ? ENCRYPT : DECRYPT);
  185. /* param0: length of the data to be encrypted */
  186. creq->gph.param0 = cpu_to_be16(skreq->cryptlen);
  187. creq->gph.param1 = 0;
  188. /* param2: encryption data offset */
  189. creq->gph.param2 = cpu_to_be16(ivsize);
  190. creq->gph.param3 = 0;
  191. creq->ctx_handle = nctx->u.ctx_handle;
  192. creq->ctrl.s.ctxl = sizeof(struct flexi_crypto_context);
  193. ret = alloc_src_sglist(skreq, ivsize);
  194. if (ret)
  195. return ret;
  196. ret = alloc_dst_sglist(skreq, ivsize);
  197. if (ret) {
  198. free_src_sglist(skreq);
  199. return ret;
  200. }
  201. /* send the crypto request */
  202. return nitrox_process_se_request(nctx->ndev, creq,
  203. nitrox_skcipher_callback, skreq);
  204. }
  205. static int nitrox_aes_encrypt(struct skcipher_request *skreq)
  206. {
  207. return nitrox_skcipher_crypt(skreq, true);
  208. }
  209. static int nitrox_aes_decrypt(struct skcipher_request *skreq)
  210. {
  211. return nitrox_skcipher_crypt(skreq, false);
  212. }
  213. static int nitrox_3des_setkey(struct crypto_skcipher *cipher,
  214. const u8 *key, unsigned int keylen)
  215. {
  216. return unlikely(des3_verify_key(cipher, key)) ?:
  217. nitrox_skcipher_setkey(cipher, 0, key, keylen);
  218. }
  219. static int nitrox_3des_encrypt(struct skcipher_request *skreq)
  220. {
  221. return nitrox_skcipher_crypt(skreq, true);
  222. }
  223. static int nitrox_3des_decrypt(struct skcipher_request *skreq)
  224. {
  225. return nitrox_skcipher_crypt(skreq, false);
  226. }
  227. static int nitrox_aes_xts_setkey(struct crypto_skcipher *cipher,
  228. const u8 *key, unsigned int keylen)
  229. {
  230. struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
  231. struct nitrox_crypto_ctx *nctx = crypto_tfm_ctx(tfm);
  232. struct flexi_crypto_context *fctx;
  233. int aes_keylen, ret;
  234. ret = xts_check_key(tfm, key, keylen);
  235. if (ret)
  236. return ret;
  237. keylen /= 2;
  238. aes_keylen = flexi_aes_keylen(keylen);
  239. if (aes_keylen < 0) {
  240. crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  241. return -EINVAL;
  242. }
  243. fctx = nctx->u.fctx;
  244. /* copy KEY2 */
  245. memcpy(fctx->auth.u.key2, (key + keylen), keylen);
  246. return nitrox_skcipher_setkey(cipher, aes_keylen, key, keylen);
  247. }
  248. static int nitrox_aes_ctr_rfc3686_setkey(struct crypto_skcipher *cipher,
  249. const u8 *key, unsigned int keylen)
  250. {
  251. struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
  252. struct nitrox_crypto_ctx *nctx = crypto_tfm_ctx(tfm);
  253. struct flexi_crypto_context *fctx;
  254. int aes_keylen;
  255. if (keylen < CTR_RFC3686_NONCE_SIZE)
  256. return -EINVAL;
  257. fctx = nctx->u.fctx;
  258. memcpy(fctx->crypto.iv, key + (keylen - CTR_RFC3686_NONCE_SIZE),
  259. CTR_RFC3686_NONCE_SIZE);
  260. keylen -= CTR_RFC3686_NONCE_SIZE;
  261. aes_keylen = flexi_aes_keylen(keylen);
  262. if (aes_keylen < 0) {
  263. crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  264. return -EINVAL;
  265. }
  266. return nitrox_skcipher_setkey(cipher, aes_keylen, key, keylen);
  267. }
  268. static struct skcipher_alg nitrox_skciphers[] = { {
  269. .base = {
  270. .cra_name = "cbc(aes)",
  271. .cra_driver_name = "n5_cbc(aes)",
  272. .cra_priority = PRIO,
  273. .cra_flags = CRYPTO_ALG_ASYNC,
  274. .cra_blocksize = AES_BLOCK_SIZE,
  275. .cra_ctxsize = sizeof(struct nitrox_crypto_ctx),
  276. .cra_alignmask = 0,
  277. .cra_module = THIS_MODULE,
  278. },
  279. .min_keysize = AES_MIN_KEY_SIZE,
  280. .max_keysize = AES_MAX_KEY_SIZE,
  281. .ivsize = AES_BLOCK_SIZE,
  282. .setkey = nitrox_aes_setkey,
  283. .encrypt = nitrox_aes_encrypt,
  284. .decrypt = nitrox_aes_decrypt,
  285. .init = nitrox_skcipher_init,
  286. .exit = nitrox_skcipher_exit,
  287. }, {
  288. .base = {
  289. .cra_name = "ecb(aes)",
  290. .cra_driver_name = "n5_ecb(aes)",
  291. .cra_priority = PRIO,
  292. .cra_flags = CRYPTO_ALG_ASYNC,
  293. .cra_blocksize = AES_BLOCK_SIZE,
  294. .cra_ctxsize = sizeof(struct nitrox_crypto_ctx),
  295. .cra_alignmask = 0,
  296. .cra_module = THIS_MODULE,
  297. },
  298. .min_keysize = AES_MIN_KEY_SIZE,
  299. .max_keysize = AES_MAX_KEY_SIZE,
  300. .ivsize = AES_BLOCK_SIZE,
  301. .setkey = nitrox_aes_setkey,
  302. .encrypt = nitrox_aes_encrypt,
  303. .decrypt = nitrox_aes_decrypt,
  304. .init = nitrox_skcipher_init,
  305. .exit = nitrox_skcipher_exit,
  306. }, {
  307. .base = {
  308. .cra_name = "cfb(aes)",
  309. .cra_driver_name = "n5_cfb(aes)",
  310. .cra_priority = PRIO,
  311. .cra_flags = CRYPTO_ALG_ASYNC,
  312. .cra_blocksize = AES_BLOCK_SIZE,
  313. .cra_ctxsize = sizeof(struct nitrox_crypto_ctx),
  314. .cra_alignmask = 0,
  315. .cra_module = THIS_MODULE,
  316. },
  317. .min_keysize = AES_MIN_KEY_SIZE,
  318. .max_keysize = AES_MAX_KEY_SIZE,
  319. .ivsize = AES_BLOCK_SIZE,
  320. .setkey = nitrox_aes_setkey,
  321. .encrypt = nitrox_aes_encrypt,
  322. .decrypt = nitrox_aes_decrypt,
  323. .init = nitrox_skcipher_init,
  324. .exit = nitrox_skcipher_exit,
  325. }, {
  326. .base = {
  327. .cra_name = "xts(aes)",
  328. .cra_driver_name = "n5_xts(aes)",
  329. .cra_priority = PRIO,
  330. .cra_flags = CRYPTO_ALG_ASYNC,
  331. .cra_blocksize = AES_BLOCK_SIZE,
  332. .cra_ctxsize = sizeof(struct nitrox_crypto_ctx),
  333. .cra_alignmask = 0,
  334. .cra_module = THIS_MODULE,
  335. },
  336. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  337. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  338. .ivsize = AES_BLOCK_SIZE,
  339. .setkey = nitrox_aes_xts_setkey,
  340. .encrypt = nitrox_aes_encrypt,
  341. .decrypt = nitrox_aes_decrypt,
  342. .init = nitrox_skcipher_init,
  343. .exit = nitrox_skcipher_exit,
  344. }, {
  345. .base = {
  346. .cra_name = "rfc3686(ctr(aes))",
  347. .cra_driver_name = "n5_rfc3686(ctr(aes))",
  348. .cra_priority = PRIO,
  349. .cra_flags = CRYPTO_ALG_ASYNC,
  350. .cra_blocksize = 1,
  351. .cra_ctxsize = sizeof(struct nitrox_crypto_ctx),
  352. .cra_alignmask = 0,
  353. .cra_module = THIS_MODULE,
  354. },
  355. .min_keysize = AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
  356. .max_keysize = AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
  357. .ivsize = CTR_RFC3686_IV_SIZE,
  358. .init = nitrox_skcipher_init,
  359. .exit = nitrox_skcipher_exit,
  360. .setkey = nitrox_aes_ctr_rfc3686_setkey,
  361. .encrypt = nitrox_aes_encrypt,
  362. .decrypt = nitrox_aes_decrypt,
  363. }, {
  364. .base = {
  365. .cra_name = "cts(cbc(aes))",
  366. .cra_driver_name = "n5_cts(cbc(aes))",
  367. .cra_priority = PRIO,
  368. .cra_flags = CRYPTO_ALG_ASYNC,
  369. .cra_blocksize = AES_BLOCK_SIZE,
  370. .cra_ctxsize = sizeof(struct nitrox_crypto_ctx),
  371. .cra_alignmask = 0,
  372. .cra_type = &crypto_ablkcipher_type,
  373. .cra_module = THIS_MODULE,
  374. },
  375. .min_keysize = AES_MIN_KEY_SIZE,
  376. .max_keysize = AES_MAX_KEY_SIZE,
  377. .ivsize = AES_BLOCK_SIZE,
  378. .setkey = nitrox_aes_setkey,
  379. .encrypt = nitrox_aes_encrypt,
  380. .decrypt = nitrox_aes_decrypt,
  381. .init = nitrox_skcipher_init,
  382. .exit = nitrox_skcipher_exit,
  383. }, {
  384. .base = {
  385. .cra_name = "cbc(des3_ede)",
  386. .cra_driver_name = "n5_cbc(des3_ede)",
  387. .cra_priority = PRIO,
  388. .cra_flags = CRYPTO_ALG_ASYNC,
  389. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  390. .cra_ctxsize = sizeof(struct nitrox_crypto_ctx),
  391. .cra_alignmask = 0,
  392. .cra_module = THIS_MODULE,
  393. },
  394. .min_keysize = DES3_EDE_KEY_SIZE,
  395. .max_keysize = DES3_EDE_KEY_SIZE,
  396. .ivsize = DES3_EDE_BLOCK_SIZE,
  397. .setkey = nitrox_3des_setkey,
  398. .encrypt = nitrox_3des_encrypt,
  399. .decrypt = nitrox_3des_decrypt,
  400. .init = nitrox_skcipher_init,
  401. .exit = nitrox_skcipher_exit,
  402. }, {
  403. .base = {
  404. .cra_name = "ecb(des3_ede)",
  405. .cra_driver_name = "n5_ecb(des3_ede)",
  406. .cra_priority = PRIO,
  407. .cra_flags = CRYPTO_ALG_ASYNC,
  408. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  409. .cra_ctxsize = sizeof(struct nitrox_crypto_ctx),
  410. .cra_alignmask = 0,
  411. .cra_module = THIS_MODULE,
  412. },
  413. .min_keysize = DES3_EDE_KEY_SIZE,
  414. .max_keysize = DES3_EDE_KEY_SIZE,
  415. .ivsize = DES3_EDE_BLOCK_SIZE,
  416. .setkey = nitrox_3des_setkey,
  417. .encrypt = nitrox_3des_encrypt,
  418. .decrypt = nitrox_3des_decrypt,
  419. .init = nitrox_skcipher_init,
  420. .exit = nitrox_skcipher_exit,
  421. }
  422. };
  423. int nitrox_register_skciphers(void)
  424. {
  425. return crypto_register_skciphers(nitrox_skciphers,
  426. ARRAY_SIZE(nitrox_skciphers));
  427. }
  428. void nitrox_unregister_skciphers(void)
  429. {
  430. crypto_unregister_skciphers(nitrox_skciphers,
  431. ARRAY_SIZE(nitrox_skciphers));
  432. }