algif_aead.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * algif_aead: User-space interface for AEAD algorithms
  4. *
  5. * Copyright (C) 2014, Stephan Mueller <smueller@chronox.de>
  6. *
  7. * This file provides the user-space API for AEAD ciphers.
  8. *
  9. * The following concept of the memory management is used:
  10. *
  11. * The kernel maintains two SGLs, the TX SGL and the RX SGL. The TX SGL is
  12. * filled by user space with the data submitted via sendpage/sendmsg. Filling
  13. * up the TX SGL does not cause a crypto operation -- the data will only be
  14. * tracked by the kernel. Upon receipt of one recvmsg call, the caller must
  15. * provide a buffer which is tracked with the RX SGL.
  16. *
  17. * During the processing of the recvmsg operation, the cipher request is
  18. * allocated and prepared. As part of the recvmsg operation, the processed
  19. * TX buffers are extracted from the TX SGL into a separate SGL.
  20. *
  21. * After the completion of the crypto operation, the RX SGL and the cipher
  22. * request is released. The extracted TX SGL parts are released together with
  23. * the RX SGL release.
  24. */
  25. #include <crypto/internal/aead.h>
  26. #include <crypto/scatterwalk.h>
  27. #include <crypto/if_alg.h>
  28. #include <crypto/skcipher.h>
  29. #include <crypto/null.h>
  30. #include <linux/init.h>
  31. #include <linux/list.h>
  32. #include <linux/kernel.h>
  33. #include <linux/mm.h>
  34. #include <linux/module.h>
  35. #include <linux/net.h>
  36. #include <net/sock.h>
  37. struct aead_tfm {
  38. struct crypto_aead *aead;
  39. struct crypto_sync_skcipher *null_tfm;
  40. };
  41. static inline bool aead_sufficient_data(struct sock *sk)
  42. {
  43. struct alg_sock *ask = alg_sk(sk);
  44. struct sock *psk = ask->parent;
  45. struct alg_sock *pask = alg_sk(psk);
  46. struct af_alg_ctx *ctx = ask->private;
  47. struct aead_tfm *aeadc = pask->private;
  48. struct crypto_aead *tfm = aeadc->aead;
  49. unsigned int as = crypto_aead_authsize(tfm);
  50. /*
  51. * The minimum amount of memory needed for an AEAD cipher is
  52. * the AAD and in case of decryption the tag.
  53. */
  54. return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
  55. }
  56. static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  57. {
  58. struct sock *sk = sock->sk;
  59. struct alg_sock *ask = alg_sk(sk);
  60. struct sock *psk = ask->parent;
  61. struct alg_sock *pask = alg_sk(psk);
  62. struct aead_tfm *aeadc = pask->private;
  63. struct crypto_aead *tfm = aeadc->aead;
  64. unsigned int ivsize = crypto_aead_ivsize(tfm);
  65. return af_alg_sendmsg(sock, msg, size, ivsize);
  66. }
  67. static int crypto_aead_copy_sgl(struct crypto_sync_skcipher *null_tfm,
  68. struct scatterlist *src,
  69. struct scatterlist *dst, unsigned int len)
  70. {
  71. SYNC_SKCIPHER_REQUEST_ON_STACK(skreq, null_tfm);
  72. skcipher_request_set_sync_tfm(skreq, null_tfm);
  73. skcipher_request_set_callback(skreq, CRYPTO_TFM_REQ_MAY_SLEEP,
  74. NULL, NULL);
  75. skcipher_request_set_crypt(skreq, src, dst, len, NULL);
  76. return crypto_skcipher_encrypt(skreq);
  77. }
  78. static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
  79. size_t ignored, int flags)
  80. {
  81. struct sock *sk = sock->sk;
  82. struct alg_sock *ask = alg_sk(sk);
  83. struct sock *psk = ask->parent;
  84. struct alg_sock *pask = alg_sk(psk);
  85. struct af_alg_ctx *ctx = ask->private;
  86. struct aead_tfm *aeadc = pask->private;
  87. struct crypto_aead *tfm = aeadc->aead;
  88. struct crypto_sync_skcipher *null_tfm = aeadc->null_tfm;
  89. unsigned int i, as = crypto_aead_authsize(tfm);
  90. struct af_alg_async_req *areq;
  91. struct af_alg_tsgl *tsgl, *tmp;
  92. struct scatterlist *rsgl_src, *tsgl_src = NULL;
  93. int err = 0;
  94. size_t used = 0; /* [in] TX bufs to be en/decrypted */
  95. size_t outlen = 0; /* [out] RX bufs produced by kernel */
  96. size_t usedpages = 0; /* [in] RX bufs to be used from user */
  97. size_t processed = 0; /* [in] TX bufs to be consumed */
  98. if (!ctx->init || ctx->more) {
  99. err = af_alg_wait_for_data(sk, flags, 0);
  100. if (err)
  101. return err;
  102. }
  103. /*
  104. * Data length provided by caller via sendmsg/sendpage that has not
  105. * yet been processed.
  106. */
  107. used = ctx->used;
  108. /*
  109. * Make sure sufficient data is present -- note, the same check is
  110. * is also present in sendmsg/sendpage. The checks in sendpage/sendmsg
  111. * shall provide an information to the data sender that something is
  112. * wrong, but they are irrelevant to maintain the kernel integrity.
  113. * We need this check here too in case user space decides to not honor
  114. * the error message in sendmsg/sendpage and still call recvmsg. This
  115. * check here protects the kernel integrity.
  116. */
  117. if (!aead_sufficient_data(sk))
  118. return -EINVAL;
  119. /*
  120. * Calculate the minimum output buffer size holding the result of the
  121. * cipher operation. When encrypting data, the receiving buffer is
  122. * larger by the tag length compared to the input buffer as the
  123. * encryption operation generates the tag. For decryption, the input
  124. * buffer provides the tag which is consumed resulting in only the
  125. * plaintext without a buffer for the tag returned to the caller.
  126. */
  127. if (ctx->enc)
  128. outlen = used + as;
  129. else
  130. outlen = used - as;
  131. /*
  132. * The cipher operation input data is reduced by the associated data
  133. * length as this data is processed separately later on.
  134. */
  135. used -= ctx->aead_assoclen;
  136. /* Allocate cipher request for current operation. */
  137. areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) +
  138. crypto_aead_reqsize(tfm));
  139. if (IS_ERR(areq))
  140. return PTR_ERR(areq);
  141. /* convert iovecs of output buffers into RX SGL */
  142. err = af_alg_get_rsgl(sk, msg, flags, areq, outlen, &usedpages);
  143. if (err)
  144. goto free;
  145. /*
  146. * Ensure output buffer is sufficiently large. If the caller provides
  147. * less buffer space, only use the relative required input size. This
  148. * allows AIO operation where the caller sent all data to be processed
  149. * and the AIO operation performs the operation on the different chunks
  150. * of the input data.
  151. */
  152. if (usedpages < outlen) {
  153. size_t less = outlen - usedpages;
  154. if (used < less) {
  155. err = -EINVAL;
  156. goto free;
  157. }
  158. used -= less;
  159. outlen -= less;
  160. }
  161. processed = used + ctx->aead_assoclen;
  162. list_for_each_entry_safe(tsgl, tmp, &ctx->tsgl_list, list) {
  163. for (i = 0; i < tsgl->cur; i++) {
  164. struct scatterlist *process_sg = tsgl->sg + i;
  165. if (!(process_sg->length) || !sg_page(process_sg))
  166. continue;
  167. tsgl_src = process_sg;
  168. break;
  169. }
  170. if (tsgl_src)
  171. break;
  172. }
  173. if (processed && !tsgl_src) {
  174. err = -EFAULT;
  175. goto free;
  176. }
  177. /*
  178. * Copy of AAD from source to destination
  179. *
  180. * The AAD is copied to the destination buffer without change. Even
  181. * when user space uses an in-place cipher operation, the kernel
  182. * will copy the data as it does not see whether such in-place operation
  183. * is initiated.
  184. *
  185. * To ensure efficiency, the following implementation ensure that the
  186. * ciphers are invoked to perform a crypto operation in-place. This
  187. * is achieved by memory management specified as follows.
  188. */
  189. /* Use the RX SGL as source (and destination) for crypto op. */
  190. rsgl_src = areq->first_rsgl.sgl.sg;
  191. if (ctx->enc) {
  192. /*
  193. * Encryption operation - The in-place cipher operation is
  194. * achieved by the following operation:
  195. *
  196. * TX SGL: AAD || PT
  197. * | |
  198. * | copy |
  199. * v v
  200. * RX SGL: AAD || PT || Tag
  201. */
  202. err = crypto_aead_copy_sgl(null_tfm, tsgl_src,
  203. areq->first_rsgl.sgl.sg, processed);
  204. if (err)
  205. goto free;
  206. af_alg_pull_tsgl(sk, processed, NULL, 0);
  207. } else {
  208. /*
  209. * Decryption operation - To achieve an in-place cipher
  210. * operation, the following SGL structure is used:
  211. *
  212. * TX SGL: AAD || CT || Tag
  213. * | | ^
  214. * | copy | | Create SGL link.
  215. * v v |
  216. * RX SGL: AAD || CT ----+
  217. */
  218. /* Copy AAD || CT to RX SGL buffer for in-place operation. */
  219. err = crypto_aead_copy_sgl(null_tfm, tsgl_src,
  220. areq->first_rsgl.sgl.sg, outlen);
  221. if (err)
  222. goto free;
  223. /* Create TX SGL for tag and chain it to RX SGL. */
  224. areq->tsgl_entries = af_alg_count_tsgl(sk, processed,
  225. processed - as);
  226. if (!areq->tsgl_entries)
  227. areq->tsgl_entries = 1;
  228. areq->tsgl = sock_kmalloc(sk, array_size(sizeof(*areq->tsgl),
  229. areq->tsgl_entries),
  230. GFP_KERNEL);
  231. if (!areq->tsgl) {
  232. err = -ENOMEM;
  233. goto free;
  234. }
  235. sg_init_table(areq->tsgl, areq->tsgl_entries);
  236. /* Release TX SGL, except for tag data and reassign tag data. */
  237. af_alg_pull_tsgl(sk, processed, areq->tsgl, processed - as);
  238. /* chain the areq TX SGL holding the tag with RX SGL */
  239. if (usedpages) {
  240. /* RX SGL present */
  241. struct af_alg_sgl *sgl_prev = &areq->last_rsgl->sgl;
  242. sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1);
  243. sg_chain(sgl_prev->sg, sgl_prev->npages + 1,
  244. areq->tsgl);
  245. } else
  246. /* no RX SGL present (e.g. authentication only) */
  247. rsgl_src = areq->tsgl;
  248. }
  249. /* Initialize the crypto operation */
  250. aead_request_set_crypt(&areq->cra_u.aead_req, rsgl_src,
  251. areq->first_rsgl.sgl.sg, used, ctx->iv);
  252. aead_request_set_ad(&areq->cra_u.aead_req, ctx->aead_assoclen);
  253. aead_request_set_tfm(&areq->cra_u.aead_req, tfm);
  254. if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) {
  255. /* AIO operation */
  256. sock_hold(sk);
  257. areq->iocb = msg->msg_iocb;
  258. /* Remember output size that will be generated. */
  259. areq->outlen = outlen;
  260. aead_request_set_callback(&areq->cra_u.aead_req,
  261. CRYPTO_TFM_REQ_MAY_SLEEP,
  262. af_alg_async_cb, areq);
  263. err = ctx->enc ? crypto_aead_encrypt(&areq->cra_u.aead_req) :
  264. crypto_aead_decrypt(&areq->cra_u.aead_req);
  265. /* AIO operation in progress */
  266. if (err == -EINPROGRESS)
  267. return -EIOCBQUEUED;
  268. sock_put(sk);
  269. } else {
  270. /* Synchronous operation */
  271. aead_request_set_callback(&areq->cra_u.aead_req,
  272. CRYPTO_TFM_REQ_MAY_SLEEP |
  273. CRYPTO_TFM_REQ_MAY_BACKLOG,
  274. crypto_req_done, &ctx->wait);
  275. err = crypto_wait_req(ctx->enc ?
  276. crypto_aead_encrypt(&areq->cra_u.aead_req) :
  277. crypto_aead_decrypt(&areq->cra_u.aead_req),
  278. &ctx->wait);
  279. }
  280. free:
  281. af_alg_free_resources(areq);
  282. return err ? err : outlen;
  283. }
  284. static int aead_recvmsg(struct socket *sock, struct msghdr *msg,
  285. size_t ignored, int flags)
  286. {
  287. struct sock *sk = sock->sk;
  288. int ret = 0;
  289. lock_sock(sk);
  290. while (msg_data_left(msg)) {
  291. int err = _aead_recvmsg(sock, msg, ignored, flags);
  292. /*
  293. * This error covers -EIOCBQUEUED which implies that we can
  294. * only handle one AIO request. If the caller wants to have
  295. * multiple AIO requests in parallel, he must make multiple
  296. * separate AIO calls.
  297. *
  298. * Also return the error if no data has been processed so far.
  299. */
  300. if (err <= 0) {
  301. if (err == -EIOCBQUEUED || err == -EBADMSG || !ret)
  302. ret = err;
  303. goto out;
  304. }
  305. ret += err;
  306. }
  307. out:
  308. af_alg_wmem_wakeup(sk);
  309. release_sock(sk);
  310. return ret;
  311. }
  312. static struct proto_ops algif_aead_ops = {
  313. .family = PF_ALG,
  314. .connect = sock_no_connect,
  315. .socketpair = sock_no_socketpair,
  316. .getname = sock_no_getname,
  317. .ioctl = sock_no_ioctl,
  318. .listen = sock_no_listen,
  319. .shutdown = sock_no_shutdown,
  320. .getsockopt = sock_no_getsockopt,
  321. .mmap = sock_no_mmap,
  322. .bind = sock_no_bind,
  323. .accept = sock_no_accept,
  324. .setsockopt = sock_no_setsockopt,
  325. .release = af_alg_release,
  326. .sendmsg = aead_sendmsg,
  327. .sendpage = af_alg_sendpage,
  328. .recvmsg = aead_recvmsg,
  329. .poll = af_alg_poll,
  330. };
  331. static int aead_check_key(struct socket *sock)
  332. {
  333. int err = 0;
  334. struct sock *psk;
  335. struct alg_sock *pask;
  336. struct aead_tfm *tfm;
  337. struct sock *sk = sock->sk;
  338. struct alg_sock *ask = alg_sk(sk);
  339. lock_sock(sk);
  340. if (!atomic_read(&ask->nokey_refcnt))
  341. goto unlock_child;
  342. psk = ask->parent;
  343. pask = alg_sk(ask->parent);
  344. tfm = pask->private;
  345. err = -ENOKEY;
  346. lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
  347. if (crypto_aead_get_flags(tfm->aead) & CRYPTO_TFM_NEED_KEY)
  348. goto unlock;
  349. atomic_dec(&pask->nokey_refcnt);
  350. atomic_set(&ask->nokey_refcnt, 0);
  351. err = 0;
  352. unlock:
  353. release_sock(psk);
  354. unlock_child:
  355. release_sock(sk);
  356. return err;
  357. }
  358. static int aead_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
  359. size_t size)
  360. {
  361. int err;
  362. err = aead_check_key(sock);
  363. if (err)
  364. return err;
  365. return aead_sendmsg(sock, msg, size);
  366. }
  367. static ssize_t aead_sendpage_nokey(struct socket *sock, struct page *page,
  368. int offset, size_t size, int flags)
  369. {
  370. int err;
  371. err = aead_check_key(sock);
  372. if (err)
  373. return err;
  374. return af_alg_sendpage(sock, page, offset, size, flags);
  375. }
  376. static int aead_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
  377. size_t ignored, int flags)
  378. {
  379. int err;
  380. err = aead_check_key(sock);
  381. if (err)
  382. return err;
  383. return aead_recvmsg(sock, msg, ignored, flags);
  384. }
  385. static struct proto_ops algif_aead_ops_nokey = {
  386. .family = PF_ALG,
  387. .connect = sock_no_connect,
  388. .socketpair = sock_no_socketpair,
  389. .getname = sock_no_getname,
  390. .ioctl = sock_no_ioctl,
  391. .listen = sock_no_listen,
  392. .shutdown = sock_no_shutdown,
  393. .getsockopt = sock_no_getsockopt,
  394. .mmap = sock_no_mmap,
  395. .bind = sock_no_bind,
  396. .accept = sock_no_accept,
  397. .setsockopt = sock_no_setsockopt,
  398. .release = af_alg_release,
  399. .sendmsg = aead_sendmsg_nokey,
  400. .sendpage = aead_sendpage_nokey,
  401. .recvmsg = aead_recvmsg_nokey,
  402. .poll = af_alg_poll,
  403. };
  404. static void *aead_bind(const char *name, u32 type, u32 mask)
  405. {
  406. struct aead_tfm *tfm;
  407. struct crypto_aead *aead;
  408. struct crypto_sync_skcipher *null_tfm;
  409. tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
  410. if (!tfm)
  411. return ERR_PTR(-ENOMEM);
  412. aead = crypto_alloc_aead(name, type, mask);
  413. if (IS_ERR(aead)) {
  414. kfree(tfm);
  415. return ERR_CAST(aead);
  416. }
  417. null_tfm = crypto_get_default_null_skcipher();
  418. if (IS_ERR(null_tfm)) {
  419. crypto_free_aead(aead);
  420. kfree(tfm);
  421. return ERR_CAST(null_tfm);
  422. }
  423. tfm->aead = aead;
  424. tfm->null_tfm = null_tfm;
  425. return tfm;
  426. }
  427. static void aead_release(void *private)
  428. {
  429. struct aead_tfm *tfm = private;
  430. crypto_free_aead(tfm->aead);
  431. crypto_put_default_null_skcipher();
  432. kfree(tfm);
  433. }
  434. static int aead_setauthsize(void *private, unsigned int authsize)
  435. {
  436. struct aead_tfm *tfm = private;
  437. return crypto_aead_setauthsize(tfm->aead, authsize);
  438. }
  439. static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
  440. {
  441. struct aead_tfm *tfm = private;
  442. return crypto_aead_setkey(tfm->aead, key, keylen);
  443. }
  444. static void aead_sock_destruct(struct sock *sk)
  445. {
  446. struct alg_sock *ask = alg_sk(sk);
  447. struct af_alg_ctx *ctx = ask->private;
  448. struct sock *psk = ask->parent;
  449. struct alg_sock *pask = alg_sk(psk);
  450. struct aead_tfm *aeadc = pask->private;
  451. struct crypto_aead *tfm = aeadc->aead;
  452. unsigned int ivlen = crypto_aead_ivsize(tfm);
  453. af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
  454. sock_kzfree_s(sk, ctx->iv, ivlen);
  455. sock_kfree_s(sk, ctx, ctx->len);
  456. af_alg_release_parent(sk);
  457. }
  458. static int aead_accept_parent_nokey(void *private, struct sock *sk)
  459. {
  460. struct af_alg_ctx *ctx;
  461. struct alg_sock *ask = alg_sk(sk);
  462. struct aead_tfm *tfm = private;
  463. struct crypto_aead *aead = tfm->aead;
  464. unsigned int len = sizeof(*ctx);
  465. unsigned int ivlen = crypto_aead_ivsize(aead);
  466. ctx = sock_kmalloc(sk, len, GFP_KERNEL);
  467. if (!ctx)
  468. return -ENOMEM;
  469. memset(ctx, 0, len);
  470. ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
  471. if (!ctx->iv) {
  472. sock_kfree_s(sk, ctx, len);
  473. return -ENOMEM;
  474. }
  475. memset(ctx->iv, 0, ivlen);
  476. INIT_LIST_HEAD(&ctx->tsgl_list);
  477. ctx->len = len;
  478. crypto_init_wait(&ctx->wait);
  479. ask->private = ctx;
  480. sk->sk_destruct = aead_sock_destruct;
  481. return 0;
  482. }
  483. static int aead_accept_parent(void *private, struct sock *sk)
  484. {
  485. struct aead_tfm *tfm = private;
  486. if (crypto_aead_get_flags(tfm->aead) & CRYPTO_TFM_NEED_KEY)
  487. return -ENOKEY;
  488. return aead_accept_parent_nokey(private, sk);
  489. }
  490. static const struct af_alg_type algif_type_aead = {
  491. .bind = aead_bind,
  492. .release = aead_release,
  493. .setkey = aead_setkey,
  494. .setauthsize = aead_setauthsize,
  495. .accept = aead_accept_parent,
  496. .accept_nokey = aead_accept_parent_nokey,
  497. .ops = &algif_aead_ops,
  498. .ops_nokey = &algif_aead_ops_nokey,
  499. .name = "aead",
  500. .owner = THIS_MODULE
  501. };
  502. static int __init algif_aead_init(void)
  503. {
  504. return af_alg_register_type(&algif_type_aead);
  505. }
  506. static void __exit algif_aead_exit(void)
  507. {
  508. int err = af_alg_unregister_type(&algif_type_aead);
  509. BUG_ON(err);
  510. }
  511. module_init(algif_aead_init);
  512. module_exit(algif_aead_exit);
  513. MODULE_LICENSE("GPL");
  514. MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
  515. MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");