algif_skcipher.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*
  2. * algif_skcipher: User-space interface for skcipher algorithms
  3. *
  4. * This file provides the user-space API for symmetric key ciphers.
  5. *
  6. * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. */
  14. #include <crypto/scatterwalk.h>
  15. #include <crypto/skcipher.h>
  16. #include <crypto/if_alg.h>
  17. #include <linux/init.h>
  18. #include <linux/list.h>
  19. #include <linux/kernel.h>
  20. #include <linux/mm.h>
  21. #include <linux/module.h>
  22. #include <linux/net.h>
  23. #include <net/sock.h>
  24. struct skcipher_sg_list {
  25. struct list_head list;
  26. int cur;
  27. struct scatterlist sg[0];
  28. };
  29. struct skcipher_ctx {
  30. struct list_head tsgl;
  31. struct af_alg_sgl rsgl;
  32. void *iv;
  33. struct af_alg_completion completion;
  34. atomic_t inflight;
  35. unsigned used;
  36. unsigned int len;
  37. bool more;
  38. bool merge;
  39. bool enc;
  40. struct ablkcipher_request req;
  41. };
  42. struct skcipher_async_rsgl {
  43. struct af_alg_sgl sgl;
  44. struct list_head list;
  45. };
  46. struct skcipher_async_req {
  47. struct kiocb *iocb;
  48. struct skcipher_async_rsgl first_sgl;
  49. struct list_head list;
  50. struct scatterlist *tsg;
  51. char iv[];
  52. };
  53. #define GET_SREQ(areq, ctx) (struct skcipher_async_req *)((char *)areq + \
  54. crypto_ablkcipher_reqsize(crypto_ablkcipher_reqtfm(&ctx->req)))
  55. #define GET_REQ_SIZE(ctx) \
  56. crypto_ablkcipher_reqsize(crypto_ablkcipher_reqtfm(&ctx->req))
  57. #define GET_IV_SIZE(ctx) \
  58. crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(&ctx->req))
  59. #define MAX_SGL_ENTS ((4096 - sizeof(struct skcipher_sg_list)) / \
  60. sizeof(struct scatterlist) - 1)
  61. static void skcipher_free_async_sgls(struct skcipher_async_req *sreq)
  62. {
  63. struct skcipher_async_rsgl *rsgl, *tmp;
  64. struct scatterlist *sgl;
  65. struct scatterlist *sg;
  66. int i, n;
  67. list_for_each_entry_safe(rsgl, tmp, &sreq->list, list) {
  68. af_alg_free_sg(&rsgl->sgl);
  69. if (rsgl != &sreq->first_sgl)
  70. kfree(rsgl);
  71. }
  72. sgl = sreq->tsg;
  73. n = sg_nents(sgl);
  74. for_each_sg(sgl, sg, n, i)
  75. put_page(sg_page(sg));
  76. kfree(sreq->tsg);
  77. }
  78. static void skcipher_async_cb(struct crypto_async_request *req, int err)
  79. {
  80. struct sock *sk = req->data;
  81. struct alg_sock *ask = alg_sk(sk);
  82. struct skcipher_ctx *ctx = ask->private;
  83. struct skcipher_async_req *sreq = GET_SREQ(req, ctx);
  84. struct kiocb *iocb = sreq->iocb;
  85. atomic_dec(&ctx->inflight);
  86. skcipher_free_async_sgls(sreq);
  87. kfree(req);
  88. iocb->ki_complete(iocb, err, err);
  89. }
  90. static inline int skcipher_sndbuf(struct sock *sk)
  91. {
  92. struct alg_sock *ask = alg_sk(sk);
  93. struct skcipher_ctx *ctx = ask->private;
  94. return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
  95. ctx->used, 0);
  96. }
  97. static inline bool skcipher_writable(struct sock *sk)
  98. {
  99. return PAGE_SIZE <= skcipher_sndbuf(sk);
  100. }
  101. static int skcipher_alloc_sgl(struct sock *sk)
  102. {
  103. struct alg_sock *ask = alg_sk(sk);
  104. struct skcipher_ctx *ctx = ask->private;
  105. struct skcipher_sg_list *sgl;
  106. struct scatterlist *sg = NULL;
  107. sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
  108. if (!list_empty(&ctx->tsgl))
  109. sg = sgl->sg;
  110. if (!sg || sgl->cur >= MAX_SGL_ENTS) {
  111. sgl = sock_kmalloc(sk, sizeof(*sgl) +
  112. sizeof(sgl->sg[0]) * (MAX_SGL_ENTS + 1),
  113. GFP_KERNEL);
  114. if (!sgl)
  115. return -ENOMEM;
  116. sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
  117. sgl->cur = 0;
  118. if (sg)
  119. scatterwalk_sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
  120. list_add_tail(&sgl->list, &ctx->tsgl);
  121. }
  122. return 0;
  123. }
  124. static void skcipher_pull_sgl(struct sock *sk, int used, int put)
  125. {
  126. struct alg_sock *ask = alg_sk(sk);
  127. struct skcipher_ctx *ctx = ask->private;
  128. struct skcipher_sg_list *sgl;
  129. struct scatterlist *sg;
  130. int i;
  131. while (!list_empty(&ctx->tsgl)) {
  132. sgl = list_first_entry(&ctx->tsgl, struct skcipher_sg_list,
  133. list);
  134. sg = sgl->sg;
  135. for (i = 0; i < sgl->cur; i++) {
  136. int plen = min_t(int, used, sg[i].length);
  137. if (!sg_page(sg + i))
  138. continue;
  139. sg[i].length -= plen;
  140. sg[i].offset += plen;
  141. used -= plen;
  142. ctx->used -= plen;
  143. if (sg[i].length)
  144. return;
  145. if (put)
  146. put_page(sg_page(sg + i));
  147. sg_assign_page(sg + i, NULL);
  148. }
  149. list_del(&sgl->list);
  150. sock_kfree_s(sk, sgl,
  151. sizeof(*sgl) + sizeof(sgl->sg[0]) *
  152. (MAX_SGL_ENTS + 1));
  153. }
  154. if (!ctx->used)
  155. ctx->merge = 0;
  156. }
  157. static void skcipher_free_sgl(struct sock *sk)
  158. {
  159. struct alg_sock *ask = alg_sk(sk);
  160. struct skcipher_ctx *ctx = ask->private;
  161. skcipher_pull_sgl(sk, ctx->used, 1);
  162. }
  163. static int skcipher_wait_for_wmem(struct sock *sk, unsigned flags)
  164. {
  165. long timeout;
  166. DEFINE_WAIT(wait);
  167. int err = -ERESTARTSYS;
  168. if (flags & MSG_DONTWAIT)
  169. return -EAGAIN;
  170. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  171. for (;;) {
  172. if (signal_pending(current))
  173. break;
  174. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  175. timeout = MAX_SCHEDULE_TIMEOUT;
  176. if (sk_wait_event(sk, &timeout, skcipher_writable(sk))) {
  177. err = 0;
  178. break;
  179. }
  180. }
  181. finish_wait(sk_sleep(sk), &wait);
  182. return err;
  183. }
  184. static void skcipher_wmem_wakeup(struct sock *sk)
  185. {
  186. struct socket_wq *wq;
  187. if (!skcipher_writable(sk))
  188. return;
  189. rcu_read_lock();
  190. wq = rcu_dereference(sk->sk_wq);
  191. if (wq_has_sleeper(wq))
  192. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  193. POLLRDNORM |
  194. POLLRDBAND);
  195. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
  196. rcu_read_unlock();
  197. }
  198. static int skcipher_wait_for_data(struct sock *sk, unsigned flags)
  199. {
  200. struct alg_sock *ask = alg_sk(sk);
  201. struct skcipher_ctx *ctx = ask->private;
  202. long timeout;
  203. DEFINE_WAIT(wait);
  204. int err = -ERESTARTSYS;
  205. if (flags & MSG_DONTWAIT) {
  206. return -EAGAIN;
  207. }
  208. set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  209. for (;;) {
  210. if (signal_pending(current))
  211. break;
  212. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  213. timeout = MAX_SCHEDULE_TIMEOUT;
  214. if (sk_wait_event(sk, &timeout, ctx->used)) {
  215. err = 0;
  216. break;
  217. }
  218. }
  219. finish_wait(sk_sleep(sk), &wait);
  220. clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  221. return err;
  222. }
  223. static void skcipher_data_wakeup(struct sock *sk)
  224. {
  225. struct alg_sock *ask = alg_sk(sk);
  226. struct skcipher_ctx *ctx = ask->private;
  227. struct socket_wq *wq;
  228. if (!ctx->used)
  229. return;
  230. rcu_read_lock();
  231. wq = rcu_dereference(sk->sk_wq);
  232. if (wq_has_sleeper(wq))
  233. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  234. POLLRDNORM |
  235. POLLRDBAND);
  236. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  237. rcu_read_unlock();
  238. }
  239. static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
  240. size_t size)
  241. {
  242. struct sock *sk = sock->sk;
  243. struct alg_sock *ask = alg_sk(sk);
  244. struct skcipher_ctx *ctx = ask->private;
  245. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(&ctx->req);
  246. unsigned ivsize = crypto_ablkcipher_ivsize(tfm);
  247. struct skcipher_sg_list *sgl;
  248. struct af_alg_control con = {};
  249. long copied = 0;
  250. bool enc = 0;
  251. bool init = 0;
  252. int err;
  253. int i;
  254. if (msg->msg_controllen) {
  255. err = af_alg_cmsg_send(msg, &con);
  256. if (err)
  257. return err;
  258. init = 1;
  259. switch (con.op) {
  260. case ALG_OP_ENCRYPT:
  261. enc = 1;
  262. break;
  263. case ALG_OP_DECRYPT:
  264. enc = 0;
  265. break;
  266. default:
  267. return -EINVAL;
  268. }
  269. if (con.iv && con.iv->ivlen != ivsize)
  270. return -EINVAL;
  271. }
  272. err = -EINVAL;
  273. lock_sock(sk);
  274. if (!ctx->more && ctx->used)
  275. goto unlock;
  276. if (init) {
  277. ctx->enc = enc;
  278. if (con.iv)
  279. memcpy(ctx->iv, con.iv->iv, ivsize);
  280. }
  281. while (size) {
  282. struct scatterlist *sg;
  283. unsigned long len = size;
  284. int plen;
  285. if (ctx->merge) {
  286. sgl = list_entry(ctx->tsgl.prev,
  287. struct skcipher_sg_list, list);
  288. sg = sgl->sg + sgl->cur - 1;
  289. len = min_t(unsigned long, len,
  290. PAGE_SIZE - sg->offset - sg->length);
  291. err = memcpy_from_msg(page_address(sg_page(sg)) +
  292. sg->offset + sg->length,
  293. msg, len);
  294. if (err)
  295. goto unlock;
  296. sg->length += len;
  297. ctx->merge = (sg->offset + sg->length) &
  298. (PAGE_SIZE - 1);
  299. ctx->used += len;
  300. copied += len;
  301. size -= len;
  302. continue;
  303. }
  304. if (!skcipher_writable(sk)) {
  305. err = skcipher_wait_for_wmem(sk, msg->msg_flags);
  306. if (err)
  307. goto unlock;
  308. }
  309. len = min_t(unsigned long, len, skcipher_sndbuf(sk));
  310. err = skcipher_alloc_sgl(sk);
  311. if (err)
  312. goto unlock;
  313. sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
  314. sg = sgl->sg;
  315. sg_unmark_end(sg + sgl->cur);
  316. do {
  317. i = sgl->cur;
  318. plen = min_t(int, len, PAGE_SIZE);
  319. sg_assign_page(sg + i, alloc_page(GFP_KERNEL));
  320. err = -ENOMEM;
  321. if (!sg_page(sg + i))
  322. goto unlock;
  323. err = memcpy_from_msg(page_address(sg_page(sg + i)),
  324. msg, plen);
  325. if (err) {
  326. __free_page(sg_page(sg + i));
  327. sg_assign_page(sg + i, NULL);
  328. goto unlock;
  329. }
  330. sg[i].length = plen;
  331. len -= plen;
  332. ctx->used += plen;
  333. copied += plen;
  334. size -= plen;
  335. sgl->cur++;
  336. } while (len && sgl->cur < MAX_SGL_ENTS);
  337. if (!size)
  338. sg_mark_end(sg + sgl->cur - 1);
  339. ctx->merge = plen & (PAGE_SIZE - 1);
  340. }
  341. err = 0;
  342. ctx->more = msg->msg_flags & MSG_MORE;
  343. unlock:
  344. skcipher_data_wakeup(sk);
  345. release_sock(sk);
  346. return copied ?: err;
  347. }
  348. static ssize_t skcipher_sendpage(struct socket *sock, struct page *page,
  349. int offset, size_t size, int flags)
  350. {
  351. struct sock *sk = sock->sk;
  352. struct alg_sock *ask = alg_sk(sk);
  353. struct skcipher_ctx *ctx = ask->private;
  354. struct skcipher_sg_list *sgl;
  355. int err = -EINVAL;
  356. if (flags & MSG_SENDPAGE_NOTLAST)
  357. flags |= MSG_MORE;
  358. lock_sock(sk);
  359. if (!ctx->more && ctx->used)
  360. goto unlock;
  361. if (!size)
  362. goto done;
  363. if (!skcipher_writable(sk)) {
  364. err = skcipher_wait_for_wmem(sk, flags);
  365. if (err)
  366. goto unlock;
  367. }
  368. err = skcipher_alloc_sgl(sk);
  369. if (err)
  370. goto unlock;
  371. ctx->merge = 0;
  372. sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
  373. if (sgl->cur)
  374. sg_unmark_end(sgl->sg + sgl->cur - 1);
  375. sg_mark_end(sgl->sg + sgl->cur);
  376. get_page(page);
  377. sg_set_page(sgl->sg + sgl->cur, page, size, offset);
  378. sgl->cur++;
  379. ctx->used += size;
  380. done:
  381. ctx->more = flags & MSG_MORE;
  382. unlock:
  383. skcipher_data_wakeup(sk);
  384. release_sock(sk);
  385. return err ?: size;
  386. }
  387. static int skcipher_all_sg_nents(struct skcipher_ctx *ctx)
  388. {
  389. struct skcipher_sg_list *sgl;
  390. struct scatterlist *sg;
  391. int nents = 0;
  392. list_for_each_entry(sgl, &ctx->tsgl, list) {
  393. sg = sgl->sg;
  394. while (!sg->length)
  395. sg++;
  396. nents += sg_nents(sg);
  397. }
  398. return nents;
  399. }
  400. static int skcipher_recvmsg_async(struct socket *sock, struct msghdr *msg,
  401. int flags)
  402. {
  403. struct sock *sk = sock->sk;
  404. struct alg_sock *ask = alg_sk(sk);
  405. struct skcipher_ctx *ctx = ask->private;
  406. struct skcipher_sg_list *sgl;
  407. struct scatterlist *sg;
  408. struct skcipher_async_req *sreq;
  409. struct ablkcipher_request *req;
  410. struct skcipher_async_rsgl *last_rsgl = NULL;
  411. unsigned int txbufs = 0, len = 0, tx_nents = skcipher_all_sg_nents(ctx);
  412. unsigned int reqlen = sizeof(struct skcipher_async_req) +
  413. GET_REQ_SIZE(ctx) + GET_IV_SIZE(ctx);
  414. int err = -ENOMEM;
  415. bool mark = false;
  416. lock_sock(sk);
  417. req = kmalloc(reqlen, GFP_KERNEL);
  418. if (unlikely(!req))
  419. goto unlock;
  420. sreq = GET_SREQ(req, ctx);
  421. sreq->iocb = msg->msg_iocb;
  422. memset(&sreq->first_sgl, '\0', sizeof(struct skcipher_async_rsgl));
  423. INIT_LIST_HEAD(&sreq->list);
  424. sreq->tsg = kcalloc(tx_nents, sizeof(*sg), GFP_KERNEL);
  425. if (unlikely(!sreq->tsg)) {
  426. kfree(req);
  427. goto unlock;
  428. }
  429. sg_init_table(sreq->tsg, tx_nents);
  430. memcpy(sreq->iv, ctx->iv, GET_IV_SIZE(ctx));
  431. ablkcipher_request_set_tfm(req, crypto_ablkcipher_reqtfm(&ctx->req));
  432. ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  433. skcipher_async_cb, sk);
  434. while (iov_iter_count(&msg->msg_iter)) {
  435. struct skcipher_async_rsgl *rsgl;
  436. int used;
  437. if (!ctx->used) {
  438. err = skcipher_wait_for_data(sk, flags);
  439. if (err)
  440. goto free;
  441. }
  442. sgl = list_first_entry(&ctx->tsgl,
  443. struct skcipher_sg_list, list);
  444. sg = sgl->sg;
  445. while (!sg->length)
  446. sg++;
  447. used = min_t(unsigned long, ctx->used,
  448. iov_iter_count(&msg->msg_iter));
  449. used = min_t(unsigned long, used, sg->length);
  450. if (txbufs == tx_nents) {
  451. struct scatterlist *tmp;
  452. int x;
  453. /* Ran out of tx slots in async request
  454. * need to expand */
  455. tmp = kcalloc(tx_nents * 2, sizeof(*tmp),
  456. GFP_KERNEL);
  457. if (!tmp)
  458. goto free;
  459. sg_init_table(tmp, tx_nents * 2);
  460. for (x = 0; x < tx_nents; x++)
  461. sg_set_page(&tmp[x], sg_page(&sreq->tsg[x]),
  462. sreq->tsg[x].length,
  463. sreq->tsg[x].offset);
  464. kfree(sreq->tsg);
  465. sreq->tsg = tmp;
  466. tx_nents *= 2;
  467. mark = true;
  468. }
  469. /* Need to take over the tx sgl from ctx
  470. * to the asynch req - these sgls will be freed later */
  471. sg_set_page(sreq->tsg + txbufs++, sg_page(sg), sg->length,
  472. sg->offset);
  473. if (list_empty(&sreq->list)) {
  474. rsgl = &sreq->first_sgl;
  475. list_add_tail(&rsgl->list, &sreq->list);
  476. } else {
  477. rsgl = kmalloc(sizeof(*rsgl), GFP_KERNEL);
  478. if (!rsgl) {
  479. err = -ENOMEM;
  480. goto free;
  481. }
  482. list_add_tail(&rsgl->list, &sreq->list);
  483. }
  484. used = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, used);
  485. err = used;
  486. if (used < 0)
  487. goto free;
  488. if (last_rsgl)
  489. af_alg_link_sg(&last_rsgl->sgl, &rsgl->sgl);
  490. last_rsgl = rsgl;
  491. len += used;
  492. skcipher_pull_sgl(sk, used, 0);
  493. iov_iter_advance(&msg->msg_iter, used);
  494. }
  495. if (mark)
  496. sg_mark_end(sreq->tsg + txbufs - 1);
  497. ablkcipher_request_set_crypt(req, sreq->tsg, sreq->first_sgl.sgl.sg,
  498. len, sreq->iv);
  499. err = ctx->enc ? crypto_ablkcipher_encrypt(req) :
  500. crypto_ablkcipher_decrypt(req);
  501. if (err == -EINPROGRESS) {
  502. atomic_inc(&ctx->inflight);
  503. err = -EIOCBQUEUED;
  504. goto unlock;
  505. }
  506. free:
  507. skcipher_free_async_sgls(sreq);
  508. kfree(req);
  509. unlock:
  510. skcipher_wmem_wakeup(sk);
  511. release_sock(sk);
  512. return err;
  513. }
  514. static int skcipher_recvmsg_sync(struct socket *sock, struct msghdr *msg,
  515. int flags)
  516. {
  517. struct sock *sk = sock->sk;
  518. struct alg_sock *ask = alg_sk(sk);
  519. struct skcipher_ctx *ctx = ask->private;
  520. unsigned bs = crypto_ablkcipher_blocksize(crypto_ablkcipher_reqtfm(
  521. &ctx->req));
  522. struct skcipher_sg_list *sgl;
  523. struct scatterlist *sg;
  524. int err = -EAGAIN;
  525. int used;
  526. long copied = 0;
  527. lock_sock(sk);
  528. while (msg_data_left(msg)) {
  529. sgl = list_first_entry(&ctx->tsgl,
  530. struct skcipher_sg_list, list);
  531. sg = sgl->sg;
  532. while (!sg->length)
  533. sg++;
  534. if (!ctx->used) {
  535. err = skcipher_wait_for_data(sk, flags);
  536. if (err)
  537. goto unlock;
  538. }
  539. used = min_t(unsigned long, ctx->used, msg_data_left(msg));
  540. used = af_alg_make_sg(&ctx->rsgl, &msg->msg_iter, used);
  541. err = used;
  542. if (err < 0)
  543. goto unlock;
  544. if (ctx->more || used < ctx->used)
  545. used -= used % bs;
  546. err = -EINVAL;
  547. if (!used)
  548. goto free;
  549. ablkcipher_request_set_crypt(&ctx->req, sg,
  550. ctx->rsgl.sg, used,
  551. ctx->iv);
  552. err = af_alg_wait_for_completion(
  553. ctx->enc ?
  554. crypto_ablkcipher_encrypt(&ctx->req) :
  555. crypto_ablkcipher_decrypt(&ctx->req),
  556. &ctx->completion);
  557. free:
  558. af_alg_free_sg(&ctx->rsgl);
  559. if (err)
  560. goto unlock;
  561. copied += used;
  562. skcipher_pull_sgl(sk, used, 1);
  563. iov_iter_advance(&msg->msg_iter, used);
  564. }
  565. err = 0;
  566. unlock:
  567. skcipher_wmem_wakeup(sk);
  568. release_sock(sk);
  569. return copied ?: err;
  570. }
  571. static int skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
  572. size_t ignored, int flags)
  573. {
  574. return (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) ?
  575. skcipher_recvmsg_async(sock, msg, flags) :
  576. skcipher_recvmsg_sync(sock, msg, flags);
  577. }
  578. static unsigned int skcipher_poll(struct file *file, struct socket *sock,
  579. poll_table *wait)
  580. {
  581. struct sock *sk = sock->sk;
  582. struct alg_sock *ask = alg_sk(sk);
  583. struct skcipher_ctx *ctx = ask->private;
  584. unsigned int mask;
  585. sock_poll_wait(file, sk_sleep(sk), wait);
  586. mask = 0;
  587. if (ctx->used)
  588. mask |= POLLIN | POLLRDNORM;
  589. if (skcipher_writable(sk))
  590. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  591. return mask;
  592. }
  593. static struct proto_ops algif_skcipher_ops = {
  594. .family = PF_ALG,
  595. .connect = sock_no_connect,
  596. .socketpair = sock_no_socketpair,
  597. .getname = sock_no_getname,
  598. .ioctl = sock_no_ioctl,
  599. .listen = sock_no_listen,
  600. .shutdown = sock_no_shutdown,
  601. .getsockopt = sock_no_getsockopt,
  602. .mmap = sock_no_mmap,
  603. .bind = sock_no_bind,
  604. .accept = sock_no_accept,
  605. .setsockopt = sock_no_setsockopt,
  606. .release = af_alg_release,
  607. .sendmsg = skcipher_sendmsg,
  608. .sendpage = skcipher_sendpage,
  609. .recvmsg = skcipher_recvmsg,
  610. .poll = skcipher_poll,
  611. };
  612. static void *skcipher_bind(const char *name, u32 type, u32 mask)
  613. {
  614. return crypto_alloc_ablkcipher(name, type, mask);
  615. }
  616. static void skcipher_release(void *private)
  617. {
  618. crypto_free_ablkcipher(private);
  619. }
  620. static int skcipher_setkey(void *private, const u8 *key, unsigned int keylen)
  621. {
  622. return crypto_ablkcipher_setkey(private, key, keylen);
  623. }
  624. static void skcipher_wait(struct sock *sk)
  625. {
  626. struct alg_sock *ask = alg_sk(sk);
  627. struct skcipher_ctx *ctx = ask->private;
  628. int ctr = 0;
  629. while (atomic_read(&ctx->inflight) && ctr++ < 100)
  630. msleep(100);
  631. }
  632. static void skcipher_sock_destruct(struct sock *sk)
  633. {
  634. struct alg_sock *ask = alg_sk(sk);
  635. struct skcipher_ctx *ctx = ask->private;
  636. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(&ctx->req);
  637. if (atomic_read(&ctx->inflight))
  638. skcipher_wait(sk);
  639. skcipher_free_sgl(sk);
  640. sock_kzfree_s(sk, ctx->iv, crypto_ablkcipher_ivsize(tfm));
  641. sock_kfree_s(sk, ctx, ctx->len);
  642. af_alg_release_parent(sk);
  643. }
  644. static int skcipher_accept_parent(void *private, struct sock *sk)
  645. {
  646. struct skcipher_ctx *ctx;
  647. struct alg_sock *ask = alg_sk(sk);
  648. unsigned int len = sizeof(*ctx) + crypto_ablkcipher_reqsize(private);
  649. ctx = sock_kmalloc(sk, len, GFP_KERNEL);
  650. if (!ctx)
  651. return -ENOMEM;
  652. ctx->iv = sock_kmalloc(sk, crypto_ablkcipher_ivsize(private),
  653. GFP_KERNEL);
  654. if (!ctx->iv) {
  655. sock_kfree_s(sk, ctx, len);
  656. return -ENOMEM;
  657. }
  658. memset(ctx->iv, 0, crypto_ablkcipher_ivsize(private));
  659. INIT_LIST_HEAD(&ctx->tsgl);
  660. ctx->len = len;
  661. ctx->used = 0;
  662. ctx->more = 0;
  663. ctx->merge = 0;
  664. ctx->enc = 0;
  665. atomic_set(&ctx->inflight, 0);
  666. af_alg_init_completion(&ctx->completion);
  667. ask->private = ctx;
  668. ablkcipher_request_set_tfm(&ctx->req, private);
  669. ablkcipher_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  670. af_alg_complete, &ctx->completion);
  671. sk->sk_destruct = skcipher_sock_destruct;
  672. return 0;
  673. }
  674. static const struct af_alg_type algif_type_skcipher = {
  675. .bind = skcipher_bind,
  676. .release = skcipher_release,
  677. .setkey = skcipher_setkey,
  678. .accept = skcipher_accept_parent,
  679. .ops = &algif_skcipher_ops,
  680. .name = "skcipher",
  681. .owner = THIS_MODULE
  682. };
  683. static int __init algif_skcipher_init(void)
  684. {
  685. return af_alg_register_type(&algif_type_skcipher);
  686. }
  687. static void __exit algif_skcipher_exit(void)
  688. {
  689. int err = af_alg_unregister_type(&algif_type_skcipher);
  690. BUG_ON(err);
  691. }
  692. module_init(algif_skcipher_init);
  693. module_exit(algif_skcipher_exit);
  694. MODULE_LICENSE("GPL");