nitrox_algs.c 12 KB

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