algif_aead.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. /*
  2. * algif_aead: User-space interface for AEAD algorithms
  3. *
  4. * Copyright (C) 2014, Stephan Mueller <smueller@chronox.de>
  5. *
  6. * This file provides the user-space API for AEAD ciphers.
  7. *
  8. * This file is derived from algif_skcipher.c.
  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 as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. */
  15. #include <crypto/internal/aead.h>
  16. #include <crypto/scatterwalk.h>
  17. #include <crypto/if_alg.h>
  18. #include <linux/init.h>
  19. #include <linux/list.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/module.h>
  23. #include <linux/net.h>
  24. #include <net/sock.h>
  25. struct aead_sg_list {
  26. unsigned int cur;
  27. struct scatterlist sg[ALG_MAX_PAGES];
  28. };
  29. struct aead_async_rsgl {
  30. struct af_alg_sgl sgl;
  31. struct list_head list;
  32. };
  33. struct aead_async_req {
  34. struct scatterlist *tsgl;
  35. struct aead_async_rsgl first_rsgl;
  36. struct list_head list;
  37. struct kiocb *iocb;
  38. struct sock *sk;
  39. unsigned int tsgls;
  40. char iv[];
  41. };
  42. struct aead_tfm {
  43. struct crypto_aead *aead;
  44. bool has_key;
  45. };
  46. struct aead_ctx {
  47. struct aead_sg_list tsgl;
  48. struct aead_async_rsgl first_rsgl;
  49. struct list_head list;
  50. void *iv;
  51. struct af_alg_completion completion;
  52. unsigned long used;
  53. unsigned int len;
  54. bool more;
  55. bool merge;
  56. bool enc;
  57. size_t aead_assoclen;
  58. struct aead_request aead_req;
  59. };
  60. static inline int aead_sndbuf(struct sock *sk)
  61. {
  62. struct alg_sock *ask = alg_sk(sk);
  63. struct aead_ctx *ctx = ask->private;
  64. return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
  65. ctx->used, 0);
  66. }
  67. static inline bool aead_writable(struct sock *sk)
  68. {
  69. return PAGE_SIZE <= aead_sndbuf(sk);
  70. }
  71. static inline bool aead_sufficient_data(struct aead_ctx *ctx)
  72. {
  73. unsigned as = crypto_aead_authsize(crypto_aead_reqtfm(&ctx->aead_req));
  74. /*
  75. * The minimum amount of memory needed for an AEAD cipher is
  76. * the AAD and in case of decryption the tag.
  77. */
  78. return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
  79. }
  80. static void aead_reset_ctx(struct aead_ctx *ctx)
  81. {
  82. struct aead_sg_list *sgl = &ctx->tsgl;
  83. sg_init_table(sgl->sg, ALG_MAX_PAGES);
  84. sgl->cur = 0;
  85. ctx->used = 0;
  86. ctx->more = 0;
  87. ctx->merge = 0;
  88. }
  89. static void aead_put_sgl(struct sock *sk)
  90. {
  91. struct alg_sock *ask = alg_sk(sk);
  92. struct aead_ctx *ctx = ask->private;
  93. struct aead_sg_list *sgl = &ctx->tsgl;
  94. struct scatterlist *sg = sgl->sg;
  95. unsigned int i;
  96. for (i = 0; i < sgl->cur; i++) {
  97. if (!sg_page(sg + i))
  98. continue;
  99. put_page(sg_page(sg + i));
  100. sg_assign_page(sg + i, NULL);
  101. }
  102. aead_reset_ctx(ctx);
  103. }
  104. static void aead_wmem_wakeup(struct sock *sk)
  105. {
  106. struct socket_wq *wq;
  107. if (!aead_writable(sk))
  108. return;
  109. rcu_read_lock();
  110. wq = rcu_dereference(sk->sk_wq);
  111. if (skwq_has_sleeper(wq))
  112. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  113. POLLRDNORM |
  114. POLLRDBAND);
  115. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
  116. rcu_read_unlock();
  117. }
  118. static int aead_wait_for_data(struct sock *sk, unsigned flags)
  119. {
  120. struct alg_sock *ask = alg_sk(sk);
  121. struct aead_ctx *ctx = ask->private;
  122. long timeout;
  123. DEFINE_WAIT(wait);
  124. int err = -ERESTARTSYS;
  125. if (flags & MSG_DONTWAIT)
  126. return -EAGAIN;
  127. sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  128. for (;;) {
  129. if (signal_pending(current))
  130. break;
  131. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  132. timeout = MAX_SCHEDULE_TIMEOUT;
  133. if (sk_wait_event(sk, &timeout, !ctx->more)) {
  134. err = 0;
  135. break;
  136. }
  137. }
  138. finish_wait(sk_sleep(sk), &wait);
  139. sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  140. return err;
  141. }
  142. static void aead_data_wakeup(struct sock *sk)
  143. {
  144. struct alg_sock *ask = alg_sk(sk);
  145. struct aead_ctx *ctx = ask->private;
  146. struct socket_wq *wq;
  147. if (ctx->more)
  148. return;
  149. if (!ctx->used)
  150. return;
  151. rcu_read_lock();
  152. wq = rcu_dereference(sk->sk_wq);
  153. if (skwq_has_sleeper(wq))
  154. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  155. POLLRDNORM |
  156. POLLRDBAND);
  157. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  158. rcu_read_unlock();
  159. }
  160. static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  161. {
  162. struct sock *sk = sock->sk;
  163. struct alg_sock *ask = alg_sk(sk);
  164. struct aead_ctx *ctx = ask->private;
  165. unsigned ivsize =
  166. crypto_aead_ivsize(crypto_aead_reqtfm(&ctx->aead_req));
  167. struct aead_sg_list *sgl = &ctx->tsgl;
  168. struct af_alg_control con = {};
  169. long copied = 0;
  170. bool enc = 0;
  171. bool init = 0;
  172. int err = -EINVAL;
  173. if (msg->msg_controllen) {
  174. err = af_alg_cmsg_send(msg, &con);
  175. if (err)
  176. return err;
  177. init = 1;
  178. switch (con.op) {
  179. case ALG_OP_ENCRYPT:
  180. enc = 1;
  181. break;
  182. case ALG_OP_DECRYPT:
  183. enc = 0;
  184. break;
  185. default:
  186. return -EINVAL;
  187. }
  188. if (con.iv && con.iv->ivlen != ivsize)
  189. return -EINVAL;
  190. }
  191. lock_sock(sk);
  192. if (!ctx->more && ctx->used)
  193. goto unlock;
  194. if (init) {
  195. ctx->enc = enc;
  196. if (con.iv)
  197. memcpy(ctx->iv, con.iv->iv, ivsize);
  198. ctx->aead_assoclen = con.aead_assoclen;
  199. }
  200. while (size) {
  201. size_t len = size;
  202. struct scatterlist *sg = NULL;
  203. /* use the existing memory in an allocated page */
  204. if (ctx->merge) {
  205. sg = sgl->sg + sgl->cur - 1;
  206. len = min_t(unsigned long, len,
  207. PAGE_SIZE - sg->offset - sg->length);
  208. err = memcpy_from_msg(page_address(sg_page(sg)) +
  209. sg->offset + sg->length,
  210. msg, len);
  211. if (err)
  212. goto unlock;
  213. sg->length += len;
  214. ctx->merge = (sg->offset + sg->length) &
  215. (PAGE_SIZE - 1);
  216. ctx->used += len;
  217. copied += len;
  218. size -= len;
  219. continue;
  220. }
  221. if (!aead_writable(sk)) {
  222. /* user space sent too much data */
  223. aead_put_sgl(sk);
  224. err = -EMSGSIZE;
  225. goto unlock;
  226. }
  227. /* allocate a new page */
  228. len = min_t(unsigned long, size, aead_sndbuf(sk));
  229. while (len) {
  230. size_t plen = 0;
  231. if (sgl->cur >= ALG_MAX_PAGES) {
  232. aead_put_sgl(sk);
  233. err = -E2BIG;
  234. goto unlock;
  235. }
  236. sg = sgl->sg + sgl->cur;
  237. plen = min_t(size_t, len, PAGE_SIZE);
  238. sg_assign_page(sg, alloc_page(GFP_KERNEL));
  239. err = -ENOMEM;
  240. if (!sg_page(sg))
  241. goto unlock;
  242. err = memcpy_from_msg(page_address(sg_page(sg)),
  243. msg, plen);
  244. if (err) {
  245. __free_page(sg_page(sg));
  246. sg_assign_page(sg, NULL);
  247. goto unlock;
  248. }
  249. sg->offset = 0;
  250. sg->length = plen;
  251. len -= plen;
  252. ctx->used += plen;
  253. copied += plen;
  254. sgl->cur++;
  255. size -= plen;
  256. ctx->merge = plen & (PAGE_SIZE - 1);
  257. }
  258. }
  259. err = 0;
  260. ctx->more = msg->msg_flags & MSG_MORE;
  261. if (!ctx->more && !aead_sufficient_data(ctx)) {
  262. aead_put_sgl(sk);
  263. err = -EMSGSIZE;
  264. }
  265. unlock:
  266. aead_data_wakeup(sk);
  267. release_sock(sk);
  268. return err ?: copied;
  269. }
  270. static ssize_t aead_sendpage(struct socket *sock, struct page *page,
  271. int offset, size_t size, int flags)
  272. {
  273. struct sock *sk = sock->sk;
  274. struct alg_sock *ask = alg_sk(sk);
  275. struct aead_ctx *ctx = ask->private;
  276. struct aead_sg_list *sgl = &ctx->tsgl;
  277. int err = -EINVAL;
  278. if (flags & MSG_SENDPAGE_NOTLAST)
  279. flags |= MSG_MORE;
  280. if (sgl->cur >= ALG_MAX_PAGES)
  281. return -E2BIG;
  282. lock_sock(sk);
  283. if (!ctx->more && ctx->used)
  284. goto unlock;
  285. if (!size)
  286. goto done;
  287. if (!aead_writable(sk)) {
  288. /* user space sent too much data */
  289. aead_put_sgl(sk);
  290. err = -EMSGSIZE;
  291. goto unlock;
  292. }
  293. ctx->merge = 0;
  294. get_page(page);
  295. sg_set_page(sgl->sg + sgl->cur, page, size, offset);
  296. sgl->cur++;
  297. ctx->used += size;
  298. err = 0;
  299. done:
  300. ctx->more = flags & MSG_MORE;
  301. if (!ctx->more && !aead_sufficient_data(ctx)) {
  302. aead_put_sgl(sk);
  303. err = -EMSGSIZE;
  304. }
  305. unlock:
  306. aead_data_wakeup(sk);
  307. release_sock(sk);
  308. return err ?: size;
  309. }
  310. #define GET_ASYM_REQ(req, tfm) (struct aead_async_req *) \
  311. ((char *)req + sizeof(struct aead_request) + \
  312. crypto_aead_reqsize(tfm))
  313. #define GET_REQ_SIZE(tfm) sizeof(struct aead_async_req) + \
  314. crypto_aead_reqsize(tfm) + crypto_aead_ivsize(tfm) + \
  315. sizeof(struct aead_request)
  316. static void aead_async_cb(struct crypto_async_request *_req, int err)
  317. {
  318. struct aead_request *req = _req->data;
  319. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  320. struct aead_async_req *areq = GET_ASYM_REQ(req, tfm);
  321. struct sock *sk = areq->sk;
  322. struct scatterlist *sg = areq->tsgl;
  323. struct aead_async_rsgl *rsgl;
  324. struct kiocb *iocb = areq->iocb;
  325. unsigned int i, reqlen = GET_REQ_SIZE(tfm);
  326. list_for_each_entry(rsgl, &areq->list, list) {
  327. af_alg_free_sg(&rsgl->sgl);
  328. if (rsgl != &areq->first_rsgl)
  329. sock_kfree_s(sk, rsgl, sizeof(*rsgl));
  330. }
  331. for (i = 0; i < areq->tsgls; i++)
  332. put_page(sg_page(sg + i));
  333. sock_kfree_s(sk, areq->tsgl, sizeof(*areq->tsgl) * areq->tsgls);
  334. sock_kfree_s(sk, req, reqlen);
  335. __sock_put(sk);
  336. iocb->ki_complete(iocb, err, err);
  337. }
  338. static int aead_recvmsg_async(struct socket *sock, struct msghdr *msg,
  339. int flags)
  340. {
  341. struct sock *sk = sock->sk;
  342. struct alg_sock *ask = alg_sk(sk);
  343. struct aead_ctx *ctx = ask->private;
  344. struct crypto_aead *tfm = crypto_aead_reqtfm(&ctx->aead_req);
  345. struct aead_async_req *areq;
  346. struct aead_request *req = NULL;
  347. struct aead_sg_list *sgl = &ctx->tsgl;
  348. struct aead_async_rsgl *last_rsgl = NULL, *rsgl;
  349. unsigned int as = crypto_aead_authsize(tfm);
  350. unsigned int i, reqlen = GET_REQ_SIZE(tfm);
  351. int err = -ENOMEM;
  352. unsigned long used;
  353. size_t outlen = 0;
  354. size_t usedpages = 0;
  355. lock_sock(sk);
  356. if (ctx->more) {
  357. err = aead_wait_for_data(sk, flags);
  358. if (err)
  359. goto unlock;
  360. }
  361. if (!aead_sufficient_data(ctx))
  362. goto unlock;
  363. used = ctx->used;
  364. if (ctx->enc)
  365. outlen = used + as;
  366. else
  367. outlen = used - as;
  368. req = sock_kmalloc(sk, reqlen, GFP_KERNEL);
  369. if (unlikely(!req))
  370. goto unlock;
  371. areq = GET_ASYM_REQ(req, tfm);
  372. memset(&areq->first_rsgl, '\0', sizeof(areq->first_rsgl));
  373. INIT_LIST_HEAD(&areq->list);
  374. areq->iocb = msg->msg_iocb;
  375. areq->sk = sk;
  376. memcpy(areq->iv, ctx->iv, crypto_aead_ivsize(tfm));
  377. aead_request_set_tfm(req, tfm);
  378. aead_request_set_ad(req, ctx->aead_assoclen);
  379. aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  380. aead_async_cb, req);
  381. used -= ctx->aead_assoclen;
  382. /* take over all tx sgls from ctx */
  383. areq->tsgl = sock_kmalloc(sk, sizeof(*areq->tsgl) * sgl->cur,
  384. GFP_KERNEL);
  385. if (unlikely(!areq->tsgl))
  386. goto free;
  387. sg_init_table(areq->tsgl, sgl->cur);
  388. for (i = 0; i < sgl->cur; i++)
  389. sg_set_page(&areq->tsgl[i], sg_page(&sgl->sg[i]),
  390. sgl->sg[i].length, sgl->sg[i].offset);
  391. areq->tsgls = sgl->cur;
  392. /* create rx sgls */
  393. while (outlen > usedpages && iov_iter_count(&msg->msg_iter)) {
  394. size_t seglen = min_t(size_t, iov_iter_count(&msg->msg_iter),
  395. (outlen - usedpages));
  396. if (list_empty(&areq->list)) {
  397. rsgl = &areq->first_rsgl;
  398. } else {
  399. rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
  400. if (unlikely(!rsgl)) {
  401. err = -ENOMEM;
  402. goto free;
  403. }
  404. }
  405. rsgl->sgl.npages = 0;
  406. list_add_tail(&rsgl->list, &areq->list);
  407. /* make one iovec available as scatterlist */
  408. err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen);
  409. if (err < 0)
  410. goto free;
  411. usedpages += err;
  412. /* chain the new scatterlist with previous one */
  413. if (last_rsgl)
  414. af_alg_link_sg(&last_rsgl->sgl, &rsgl->sgl);
  415. last_rsgl = rsgl;
  416. iov_iter_advance(&msg->msg_iter, err);
  417. }
  418. /* ensure output buffer is sufficiently large */
  419. if (usedpages < outlen) {
  420. err = -EINVAL;
  421. goto unlock;
  422. }
  423. aead_request_set_crypt(req, areq->tsgl, areq->first_rsgl.sgl.sg, used,
  424. areq->iv);
  425. err = ctx->enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
  426. if (err) {
  427. if (err == -EINPROGRESS) {
  428. sock_hold(sk);
  429. err = -EIOCBQUEUED;
  430. aead_reset_ctx(ctx);
  431. goto unlock;
  432. } else if (err == -EBADMSG) {
  433. aead_put_sgl(sk);
  434. }
  435. goto free;
  436. }
  437. aead_put_sgl(sk);
  438. free:
  439. list_for_each_entry(rsgl, &areq->list, list) {
  440. af_alg_free_sg(&rsgl->sgl);
  441. if (rsgl != &areq->first_rsgl)
  442. sock_kfree_s(sk, rsgl, sizeof(*rsgl));
  443. }
  444. if (areq->tsgl)
  445. sock_kfree_s(sk, areq->tsgl, sizeof(*areq->tsgl) * areq->tsgls);
  446. if (req)
  447. sock_kfree_s(sk, req, reqlen);
  448. unlock:
  449. aead_wmem_wakeup(sk);
  450. release_sock(sk);
  451. return err ? err : outlen;
  452. }
  453. static int aead_recvmsg_sync(struct socket *sock, struct msghdr *msg, int flags)
  454. {
  455. struct sock *sk = sock->sk;
  456. struct alg_sock *ask = alg_sk(sk);
  457. struct aead_ctx *ctx = ask->private;
  458. unsigned as = crypto_aead_authsize(crypto_aead_reqtfm(&ctx->aead_req));
  459. struct aead_sg_list *sgl = &ctx->tsgl;
  460. struct aead_async_rsgl *last_rsgl = NULL;
  461. struct aead_async_rsgl *rsgl, *tmp;
  462. int err = -EINVAL;
  463. unsigned long used = 0;
  464. size_t outlen = 0;
  465. size_t usedpages = 0;
  466. lock_sock(sk);
  467. /*
  468. * AEAD memory structure: For encryption, the tag is appended to the
  469. * ciphertext which implies that the memory allocated for the ciphertext
  470. * must be increased by the tag length. For decryption, the tag
  471. * is expected to be concatenated to the ciphertext. The plaintext
  472. * therefore has a memory size of the ciphertext minus the tag length.
  473. *
  474. * The memory structure for cipher operation has the following
  475. * structure:
  476. * AEAD encryption input: assoc data || plaintext
  477. * AEAD encryption output: cipherntext || auth tag
  478. * AEAD decryption input: assoc data || ciphertext || auth tag
  479. * AEAD decryption output: plaintext
  480. */
  481. if (ctx->more) {
  482. err = aead_wait_for_data(sk, flags);
  483. if (err)
  484. goto unlock;
  485. }
  486. /* data length provided by caller via sendmsg/sendpage */
  487. used = ctx->used;
  488. /*
  489. * Make sure sufficient data is present -- note, the same check is
  490. * is also present in sendmsg/sendpage. The checks in sendpage/sendmsg
  491. * shall provide an information to the data sender that something is
  492. * wrong, but they are irrelevant to maintain the kernel integrity.
  493. * We need this check here too in case user space decides to not honor
  494. * the error message in sendmsg/sendpage and still call recvmsg. This
  495. * check here protects the kernel integrity.
  496. */
  497. if (!aead_sufficient_data(ctx))
  498. goto unlock;
  499. /*
  500. * Calculate the minimum output buffer size holding the result of the
  501. * cipher operation. When encrypting data, the receiving buffer is
  502. * larger by the tag length compared to the input buffer as the
  503. * encryption operation generates the tag. For decryption, the input
  504. * buffer provides the tag which is consumed resulting in only the
  505. * plaintext without a buffer for the tag returned to the caller.
  506. */
  507. if (ctx->enc)
  508. outlen = used + as;
  509. else
  510. outlen = used - as;
  511. /*
  512. * The cipher operation input data is reduced by the associated data
  513. * length as this data is processed separately later on.
  514. */
  515. used -= ctx->aead_assoclen;
  516. /* convert iovecs of output buffers into scatterlists */
  517. while (outlen > usedpages && iov_iter_count(&msg->msg_iter)) {
  518. size_t seglen = min_t(size_t, iov_iter_count(&msg->msg_iter),
  519. (outlen - usedpages));
  520. if (list_empty(&ctx->list)) {
  521. rsgl = &ctx->first_rsgl;
  522. } else {
  523. rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
  524. if (unlikely(!rsgl)) {
  525. err = -ENOMEM;
  526. goto unlock;
  527. }
  528. }
  529. rsgl->sgl.npages = 0;
  530. list_add_tail(&rsgl->list, &ctx->list);
  531. /* make one iovec available as scatterlist */
  532. err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen);
  533. if (err < 0)
  534. goto unlock;
  535. usedpages += err;
  536. /* chain the new scatterlist with previous one */
  537. if (last_rsgl)
  538. af_alg_link_sg(&last_rsgl->sgl, &rsgl->sgl);
  539. last_rsgl = rsgl;
  540. iov_iter_advance(&msg->msg_iter, err);
  541. }
  542. /* ensure output buffer is sufficiently large */
  543. if (usedpages < outlen) {
  544. err = -EINVAL;
  545. goto unlock;
  546. }
  547. sg_mark_end(sgl->sg + sgl->cur - 1);
  548. aead_request_set_crypt(&ctx->aead_req, sgl->sg, ctx->first_rsgl.sgl.sg,
  549. used, ctx->iv);
  550. aead_request_set_ad(&ctx->aead_req, ctx->aead_assoclen);
  551. err = af_alg_wait_for_completion(ctx->enc ?
  552. crypto_aead_encrypt(&ctx->aead_req) :
  553. crypto_aead_decrypt(&ctx->aead_req),
  554. &ctx->completion);
  555. if (err) {
  556. /* EBADMSG implies a valid cipher operation took place */
  557. if (err == -EBADMSG)
  558. aead_put_sgl(sk);
  559. goto unlock;
  560. }
  561. aead_put_sgl(sk);
  562. err = 0;
  563. unlock:
  564. list_for_each_entry_safe(rsgl, tmp, &ctx->list, list) {
  565. af_alg_free_sg(&rsgl->sgl);
  566. list_del(&rsgl->list);
  567. if (rsgl != &ctx->first_rsgl)
  568. sock_kfree_s(sk, rsgl, sizeof(*rsgl));
  569. }
  570. INIT_LIST_HEAD(&ctx->list);
  571. aead_wmem_wakeup(sk);
  572. release_sock(sk);
  573. return err ? err : outlen;
  574. }
  575. static int aead_recvmsg(struct socket *sock, struct msghdr *msg, size_t ignored,
  576. int flags)
  577. {
  578. return (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) ?
  579. aead_recvmsg_async(sock, msg, flags) :
  580. aead_recvmsg_sync(sock, msg, flags);
  581. }
  582. static unsigned int aead_poll(struct file *file, struct socket *sock,
  583. poll_table *wait)
  584. {
  585. struct sock *sk = sock->sk;
  586. struct alg_sock *ask = alg_sk(sk);
  587. struct aead_ctx *ctx = ask->private;
  588. unsigned int mask;
  589. sock_poll_wait(file, sk_sleep(sk), wait);
  590. mask = 0;
  591. if (!ctx->more)
  592. mask |= POLLIN | POLLRDNORM;
  593. if (aead_writable(sk))
  594. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  595. return mask;
  596. }
  597. static struct proto_ops algif_aead_ops = {
  598. .family = PF_ALG,
  599. .connect = sock_no_connect,
  600. .socketpair = sock_no_socketpair,
  601. .getname = sock_no_getname,
  602. .ioctl = sock_no_ioctl,
  603. .listen = sock_no_listen,
  604. .shutdown = sock_no_shutdown,
  605. .getsockopt = sock_no_getsockopt,
  606. .mmap = sock_no_mmap,
  607. .bind = sock_no_bind,
  608. .accept = sock_no_accept,
  609. .setsockopt = sock_no_setsockopt,
  610. .release = af_alg_release,
  611. .sendmsg = aead_sendmsg,
  612. .sendpage = aead_sendpage,
  613. .recvmsg = aead_recvmsg,
  614. .poll = aead_poll,
  615. };
  616. static int aead_check_key(struct socket *sock)
  617. {
  618. int err = 0;
  619. struct sock *psk;
  620. struct alg_sock *pask;
  621. struct aead_tfm *tfm;
  622. struct sock *sk = sock->sk;
  623. struct alg_sock *ask = alg_sk(sk);
  624. lock_sock(sk);
  625. if (ask->refcnt)
  626. goto unlock_child;
  627. psk = ask->parent;
  628. pask = alg_sk(ask->parent);
  629. tfm = pask->private;
  630. err = -ENOKEY;
  631. lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
  632. if (!tfm->has_key)
  633. goto unlock;
  634. if (!pask->refcnt++)
  635. sock_hold(psk);
  636. ask->refcnt = 1;
  637. sock_put(psk);
  638. err = 0;
  639. unlock:
  640. release_sock(psk);
  641. unlock_child:
  642. release_sock(sk);
  643. return err;
  644. }
  645. static int aead_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
  646. size_t size)
  647. {
  648. int err;
  649. err = aead_check_key(sock);
  650. if (err)
  651. return err;
  652. return aead_sendmsg(sock, msg, size);
  653. }
  654. static ssize_t aead_sendpage_nokey(struct socket *sock, struct page *page,
  655. int offset, size_t size, int flags)
  656. {
  657. int err;
  658. err = aead_check_key(sock);
  659. if (err)
  660. return err;
  661. return aead_sendpage(sock, page, offset, size, flags);
  662. }
  663. static int aead_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
  664. size_t ignored, int flags)
  665. {
  666. int err;
  667. err = aead_check_key(sock);
  668. if (err)
  669. return err;
  670. return aead_recvmsg(sock, msg, ignored, flags);
  671. }
  672. static struct proto_ops algif_aead_ops_nokey = {
  673. .family = PF_ALG,
  674. .connect = sock_no_connect,
  675. .socketpair = sock_no_socketpair,
  676. .getname = sock_no_getname,
  677. .ioctl = sock_no_ioctl,
  678. .listen = sock_no_listen,
  679. .shutdown = sock_no_shutdown,
  680. .getsockopt = sock_no_getsockopt,
  681. .mmap = sock_no_mmap,
  682. .bind = sock_no_bind,
  683. .accept = sock_no_accept,
  684. .setsockopt = sock_no_setsockopt,
  685. .release = af_alg_release,
  686. .sendmsg = aead_sendmsg_nokey,
  687. .sendpage = aead_sendpage_nokey,
  688. .recvmsg = aead_recvmsg_nokey,
  689. .poll = aead_poll,
  690. };
  691. static void *aead_bind(const char *name, u32 type, u32 mask)
  692. {
  693. struct aead_tfm *tfm;
  694. struct crypto_aead *aead;
  695. tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
  696. if (!tfm)
  697. return ERR_PTR(-ENOMEM);
  698. aead = crypto_alloc_aead(name, type, mask);
  699. if (IS_ERR(aead)) {
  700. kfree(tfm);
  701. return ERR_CAST(aead);
  702. }
  703. tfm->aead = aead;
  704. return tfm;
  705. }
  706. static void aead_release(void *private)
  707. {
  708. struct aead_tfm *tfm = private;
  709. crypto_free_aead(tfm->aead);
  710. kfree(tfm);
  711. }
  712. static int aead_setauthsize(void *private, unsigned int authsize)
  713. {
  714. struct aead_tfm *tfm = private;
  715. return crypto_aead_setauthsize(tfm->aead, authsize);
  716. }
  717. static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
  718. {
  719. struct aead_tfm *tfm = private;
  720. int err;
  721. err = crypto_aead_setkey(tfm->aead, key, keylen);
  722. tfm->has_key = !err;
  723. return err;
  724. }
  725. static void aead_sock_destruct(struct sock *sk)
  726. {
  727. struct alg_sock *ask = alg_sk(sk);
  728. struct aead_ctx *ctx = ask->private;
  729. unsigned int ivlen = crypto_aead_ivsize(
  730. crypto_aead_reqtfm(&ctx->aead_req));
  731. WARN_ON(atomic_read(&sk->sk_refcnt) != 0);
  732. aead_put_sgl(sk);
  733. sock_kzfree_s(sk, ctx->iv, ivlen);
  734. sock_kfree_s(sk, ctx, ctx->len);
  735. af_alg_release_parent(sk);
  736. }
  737. static int aead_accept_parent_nokey(void *private, struct sock *sk)
  738. {
  739. struct aead_ctx *ctx;
  740. struct alg_sock *ask = alg_sk(sk);
  741. struct aead_tfm *tfm = private;
  742. struct crypto_aead *aead = tfm->aead;
  743. unsigned int len = sizeof(*ctx) + crypto_aead_reqsize(aead);
  744. unsigned int ivlen = crypto_aead_ivsize(aead);
  745. ctx = sock_kmalloc(sk, len, GFP_KERNEL);
  746. if (!ctx)
  747. return -ENOMEM;
  748. memset(ctx, 0, len);
  749. ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
  750. if (!ctx->iv) {
  751. sock_kfree_s(sk, ctx, len);
  752. return -ENOMEM;
  753. }
  754. memset(ctx->iv, 0, ivlen);
  755. ctx->len = len;
  756. ctx->used = 0;
  757. ctx->more = 0;
  758. ctx->merge = 0;
  759. ctx->enc = 0;
  760. ctx->tsgl.cur = 0;
  761. ctx->aead_assoclen = 0;
  762. af_alg_init_completion(&ctx->completion);
  763. sg_init_table(ctx->tsgl.sg, ALG_MAX_PAGES);
  764. INIT_LIST_HEAD(&ctx->list);
  765. ask->private = ctx;
  766. aead_request_set_tfm(&ctx->aead_req, aead);
  767. aead_request_set_callback(&ctx->aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  768. af_alg_complete, &ctx->completion);
  769. sk->sk_destruct = aead_sock_destruct;
  770. return 0;
  771. }
  772. static int aead_accept_parent(void *private, struct sock *sk)
  773. {
  774. struct aead_tfm *tfm = private;
  775. if (!tfm->has_key)
  776. return -ENOKEY;
  777. return aead_accept_parent_nokey(private, sk);
  778. }
  779. static const struct af_alg_type algif_type_aead = {
  780. .bind = aead_bind,
  781. .release = aead_release,
  782. .setkey = aead_setkey,
  783. .setauthsize = aead_setauthsize,
  784. .accept = aead_accept_parent,
  785. .accept_nokey = aead_accept_parent_nokey,
  786. .ops = &algif_aead_ops,
  787. .ops_nokey = &algif_aead_ops_nokey,
  788. .name = "aead",
  789. .owner = THIS_MODULE
  790. };
  791. static int __init algif_aead_init(void)
  792. {
  793. return af_alg_register_type(&algif_type_aead);
  794. }
  795. static void __exit algif_aead_exit(void)
  796. {
  797. int err = af_alg_unregister_type(&algif_type_aead);
  798. BUG_ON(err);
  799. }
  800. module_init(algif_aead_init);
  801. module_exit(algif_aead_exit);
  802. MODULE_LICENSE("GPL");
  803. MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
  804. MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");