algif_skcipher.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. unsigned used;
  35. unsigned int len;
  36. bool more;
  37. bool merge;
  38. bool enc;
  39. struct ablkcipher_request req;
  40. };
  41. #define MAX_SGL_ENTS ((PAGE_SIZE - sizeof(struct skcipher_sg_list)) / \
  42. sizeof(struct scatterlist) - 1)
  43. static inline int skcipher_sndbuf(struct sock *sk)
  44. {
  45. struct alg_sock *ask = alg_sk(sk);
  46. struct skcipher_ctx *ctx = ask->private;
  47. return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
  48. ctx->used, 0);
  49. }
  50. static inline bool skcipher_writable(struct sock *sk)
  51. {
  52. return PAGE_SIZE <= skcipher_sndbuf(sk);
  53. }
  54. static int skcipher_alloc_sgl(struct sock *sk)
  55. {
  56. struct alg_sock *ask = alg_sk(sk);
  57. struct skcipher_ctx *ctx = ask->private;
  58. struct skcipher_sg_list *sgl;
  59. struct scatterlist *sg = NULL;
  60. sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
  61. if (!list_empty(&ctx->tsgl))
  62. sg = sgl->sg;
  63. if (!sg || sgl->cur >= MAX_SGL_ENTS) {
  64. sgl = sock_kmalloc(sk, sizeof(*sgl) +
  65. sizeof(sgl->sg[0]) * (MAX_SGL_ENTS + 1),
  66. GFP_KERNEL);
  67. if (!sgl)
  68. return -ENOMEM;
  69. sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
  70. sgl->cur = 0;
  71. if (sg)
  72. scatterwalk_sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
  73. list_add_tail(&sgl->list, &ctx->tsgl);
  74. }
  75. return 0;
  76. }
  77. static void skcipher_pull_sgl(struct sock *sk, int used)
  78. {
  79. struct alg_sock *ask = alg_sk(sk);
  80. struct skcipher_ctx *ctx = ask->private;
  81. struct skcipher_sg_list *sgl;
  82. struct scatterlist *sg;
  83. int i;
  84. while (!list_empty(&ctx->tsgl)) {
  85. sgl = list_first_entry(&ctx->tsgl, struct skcipher_sg_list,
  86. list);
  87. sg = sgl->sg;
  88. for (i = 0; i < sgl->cur; i++) {
  89. int plen = min_t(int, used, sg[i].length);
  90. if (!sg_page(sg + i))
  91. continue;
  92. sg[i].length -= plen;
  93. sg[i].offset += plen;
  94. used -= plen;
  95. ctx->used -= plen;
  96. if (sg[i].length)
  97. return;
  98. put_page(sg_page(sg + i));
  99. sg_assign_page(sg + i, NULL);
  100. }
  101. list_del(&sgl->list);
  102. sock_kfree_s(sk, sgl,
  103. sizeof(*sgl) + sizeof(sgl->sg[0]) *
  104. (MAX_SGL_ENTS + 1));
  105. }
  106. if (!ctx->used)
  107. ctx->merge = 0;
  108. }
  109. static void skcipher_free_sgl(struct sock *sk)
  110. {
  111. struct alg_sock *ask = alg_sk(sk);
  112. struct skcipher_ctx *ctx = ask->private;
  113. skcipher_pull_sgl(sk, ctx->used);
  114. }
  115. static int skcipher_wait_for_wmem(struct sock *sk, unsigned flags)
  116. {
  117. long timeout;
  118. DEFINE_WAIT(wait);
  119. int err = -ERESTARTSYS;
  120. if (flags & MSG_DONTWAIT)
  121. return -EAGAIN;
  122. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  123. for (;;) {
  124. if (signal_pending(current))
  125. break;
  126. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  127. timeout = MAX_SCHEDULE_TIMEOUT;
  128. if (sk_wait_event(sk, &timeout, skcipher_writable(sk))) {
  129. err = 0;
  130. break;
  131. }
  132. }
  133. finish_wait(sk_sleep(sk), &wait);
  134. return err;
  135. }
  136. static void skcipher_wmem_wakeup(struct sock *sk)
  137. {
  138. struct socket_wq *wq;
  139. if (!skcipher_writable(sk))
  140. return;
  141. rcu_read_lock();
  142. wq = rcu_dereference(sk->sk_wq);
  143. if (wq_has_sleeper(wq))
  144. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  145. POLLRDNORM |
  146. POLLRDBAND);
  147. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
  148. rcu_read_unlock();
  149. }
  150. static int skcipher_wait_for_data(struct sock *sk, unsigned flags)
  151. {
  152. struct alg_sock *ask = alg_sk(sk);
  153. struct skcipher_ctx *ctx = ask->private;
  154. long timeout;
  155. DEFINE_WAIT(wait);
  156. int err = -ERESTARTSYS;
  157. if (flags & MSG_DONTWAIT) {
  158. return -EAGAIN;
  159. }
  160. set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  161. for (;;) {
  162. if (signal_pending(current))
  163. break;
  164. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  165. timeout = MAX_SCHEDULE_TIMEOUT;
  166. if (sk_wait_event(sk, &timeout, ctx->used)) {
  167. err = 0;
  168. break;
  169. }
  170. }
  171. finish_wait(sk_sleep(sk), &wait);
  172. clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  173. return err;
  174. }
  175. static void skcipher_data_wakeup(struct sock *sk)
  176. {
  177. struct alg_sock *ask = alg_sk(sk);
  178. struct skcipher_ctx *ctx = ask->private;
  179. struct socket_wq *wq;
  180. if (!ctx->used)
  181. return;
  182. rcu_read_lock();
  183. wq = rcu_dereference(sk->sk_wq);
  184. if (wq_has_sleeper(wq))
  185. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  186. POLLRDNORM |
  187. POLLRDBAND);
  188. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  189. rcu_read_unlock();
  190. }
  191. static int skcipher_sendmsg(struct kiocb *unused, struct socket *sock,
  192. struct msghdr *msg, size_t size)
  193. {
  194. struct sock *sk = sock->sk;
  195. struct alg_sock *ask = alg_sk(sk);
  196. struct skcipher_ctx *ctx = ask->private;
  197. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(&ctx->req);
  198. unsigned ivsize = crypto_ablkcipher_ivsize(tfm);
  199. struct skcipher_sg_list *sgl;
  200. struct af_alg_control con = {};
  201. long copied = 0;
  202. bool enc = 0;
  203. int err;
  204. int i;
  205. if (msg->msg_controllen) {
  206. err = af_alg_cmsg_send(msg, &con);
  207. if (err)
  208. return err;
  209. switch (con.op) {
  210. case ALG_OP_ENCRYPT:
  211. enc = 1;
  212. break;
  213. case ALG_OP_DECRYPT:
  214. enc = 0;
  215. break;
  216. default:
  217. return -EINVAL;
  218. }
  219. if (con.iv && con.iv->ivlen != ivsize)
  220. return -EINVAL;
  221. }
  222. err = -EINVAL;
  223. lock_sock(sk);
  224. if (!ctx->more && ctx->used)
  225. goto unlock;
  226. if (!ctx->used) {
  227. ctx->enc = enc;
  228. if (con.iv)
  229. memcpy(ctx->iv, con.iv->iv, ivsize);
  230. }
  231. while (size) {
  232. struct scatterlist *sg;
  233. unsigned long len = size;
  234. int plen;
  235. if (ctx->merge) {
  236. sgl = list_entry(ctx->tsgl.prev,
  237. struct skcipher_sg_list, list);
  238. sg = sgl->sg + sgl->cur - 1;
  239. len = min_t(unsigned long, len,
  240. PAGE_SIZE - sg->offset - sg->length);
  241. err = memcpy_fromiovec(page_address(sg_page(sg)) +
  242. sg->offset + sg->length,
  243. msg->msg_iov, len);
  244. if (err)
  245. goto unlock;
  246. sg->length += len;
  247. ctx->merge = (sg->offset + sg->length) &
  248. (PAGE_SIZE - 1);
  249. ctx->used += len;
  250. copied += len;
  251. size -= len;
  252. continue;
  253. }
  254. if (!skcipher_writable(sk)) {
  255. err = skcipher_wait_for_wmem(sk, msg->msg_flags);
  256. if (err)
  257. goto unlock;
  258. }
  259. len = min_t(unsigned long, len, skcipher_sndbuf(sk));
  260. err = skcipher_alloc_sgl(sk);
  261. if (err)
  262. goto unlock;
  263. sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
  264. sg = sgl->sg;
  265. do {
  266. i = sgl->cur;
  267. plen = min_t(int, len, PAGE_SIZE);
  268. sg_assign_page(sg + i, alloc_page(GFP_KERNEL));
  269. err = -ENOMEM;
  270. if (!sg_page(sg + i))
  271. goto unlock;
  272. err = memcpy_fromiovec(page_address(sg_page(sg + i)),
  273. msg->msg_iov, plen);
  274. if (err) {
  275. __free_page(sg_page(sg + i));
  276. sg_assign_page(sg + i, NULL);
  277. goto unlock;
  278. }
  279. sg[i].length = plen;
  280. len -= plen;
  281. ctx->used += plen;
  282. copied += plen;
  283. size -= plen;
  284. sgl->cur++;
  285. } while (len && sgl->cur < MAX_SGL_ENTS);
  286. ctx->merge = plen & (PAGE_SIZE - 1);
  287. }
  288. err = 0;
  289. ctx->more = msg->msg_flags & MSG_MORE;
  290. if (!ctx->more && !list_empty(&ctx->tsgl))
  291. sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
  292. unlock:
  293. skcipher_data_wakeup(sk);
  294. release_sock(sk);
  295. return copied ?: err;
  296. }
  297. static ssize_t skcipher_sendpage(struct socket *sock, struct page *page,
  298. int offset, size_t size, int flags)
  299. {
  300. struct sock *sk = sock->sk;
  301. struct alg_sock *ask = alg_sk(sk);
  302. struct skcipher_ctx *ctx = ask->private;
  303. struct skcipher_sg_list *sgl;
  304. int err = -EINVAL;
  305. lock_sock(sk);
  306. if (!ctx->more && ctx->used)
  307. goto unlock;
  308. if (!size)
  309. goto done;
  310. if (!skcipher_writable(sk)) {
  311. err = skcipher_wait_for_wmem(sk, flags);
  312. if (err)
  313. goto unlock;
  314. }
  315. err = skcipher_alloc_sgl(sk);
  316. if (err)
  317. goto unlock;
  318. ctx->merge = 0;
  319. sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
  320. get_page(page);
  321. sg_set_page(sgl->sg + sgl->cur, page, size, offset);
  322. sgl->cur++;
  323. ctx->used += size;
  324. done:
  325. ctx->more = flags & MSG_MORE;
  326. if (!ctx->more && !list_empty(&ctx->tsgl))
  327. sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list);
  328. unlock:
  329. skcipher_data_wakeup(sk);
  330. release_sock(sk);
  331. return err ?: size;
  332. }
  333. static int skcipher_recvmsg(struct kiocb *unused, struct socket *sock,
  334. struct msghdr *msg, size_t ignored, int flags)
  335. {
  336. struct sock *sk = sock->sk;
  337. struct alg_sock *ask = alg_sk(sk);
  338. struct skcipher_ctx *ctx = ask->private;
  339. unsigned bs = crypto_ablkcipher_blocksize(crypto_ablkcipher_reqtfm(
  340. &ctx->req));
  341. struct skcipher_sg_list *sgl;
  342. struct scatterlist *sg;
  343. unsigned long iovlen;
  344. struct iovec *iov;
  345. int err = -EAGAIN;
  346. int used;
  347. long copied = 0;
  348. lock_sock(sk);
  349. for (iov = msg->msg_iov, iovlen = msg->msg_iovlen; iovlen > 0;
  350. iovlen--, iov++) {
  351. unsigned long seglen = iov->iov_len;
  352. char __user *from = iov->iov_base;
  353. while (seglen) {
  354. sgl = list_first_entry(&ctx->tsgl,
  355. struct skcipher_sg_list, list);
  356. sg = sgl->sg;
  357. while (!sg->length)
  358. sg++;
  359. used = ctx->used;
  360. if (!used) {
  361. err = skcipher_wait_for_data(sk, flags);
  362. if (err)
  363. goto unlock;
  364. }
  365. used = min_t(unsigned long, used, seglen);
  366. used = af_alg_make_sg(&ctx->rsgl, from, used, 1);
  367. err = used;
  368. if (err < 0)
  369. goto unlock;
  370. if (ctx->more || used < ctx->used)
  371. used -= used % bs;
  372. err = -EINVAL;
  373. if (!used)
  374. goto free;
  375. ablkcipher_request_set_crypt(&ctx->req, sg,
  376. ctx->rsgl.sg, used,
  377. ctx->iv);
  378. err = af_alg_wait_for_completion(
  379. ctx->enc ?
  380. crypto_ablkcipher_encrypt(&ctx->req) :
  381. crypto_ablkcipher_decrypt(&ctx->req),
  382. &ctx->completion);
  383. free:
  384. af_alg_free_sg(&ctx->rsgl);
  385. if (err)
  386. goto unlock;
  387. copied += used;
  388. from += used;
  389. seglen -= used;
  390. skcipher_pull_sgl(sk, used);
  391. }
  392. }
  393. err = 0;
  394. unlock:
  395. skcipher_wmem_wakeup(sk);
  396. release_sock(sk);
  397. return copied ?: err;
  398. }
  399. static unsigned int skcipher_poll(struct file *file, struct socket *sock,
  400. poll_table *wait)
  401. {
  402. struct sock *sk = sock->sk;
  403. struct alg_sock *ask = alg_sk(sk);
  404. struct skcipher_ctx *ctx = ask->private;
  405. unsigned int mask;
  406. sock_poll_wait(file, sk_sleep(sk), wait);
  407. mask = 0;
  408. if (ctx->used)
  409. mask |= POLLIN | POLLRDNORM;
  410. if (skcipher_writable(sk))
  411. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  412. return mask;
  413. }
  414. static struct proto_ops algif_skcipher_ops = {
  415. .family = PF_ALG,
  416. .connect = sock_no_connect,
  417. .socketpair = sock_no_socketpair,
  418. .getname = sock_no_getname,
  419. .ioctl = sock_no_ioctl,
  420. .listen = sock_no_listen,
  421. .shutdown = sock_no_shutdown,
  422. .getsockopt = sock_no_getsockopt,
  423. .mmap = sock_no_mmap,
  424. .bind = sock_no_bind,
  425. .accept = sock_no_accept,
  426. .setsockopt = sock_no_setsockopt,
  427. .release = af_alg_release,
  428. .sendmsg = skcipher_sendmsg,
  429. .sendpage = skcipher_sendpage,
  430. .recvmsg = skcipher_recvmsg,
  431. .poll = skcipher_poll,
  432. };
  433. static void *skcipher_bind(const char *name, u32 type, u32 mask)
  434. {
  435. return crypto_alloc_ablkcipher(name, type, mask);
  436. }
  437. static void skcipher_release(void *private)
  438. {
  439. crypto_free_ablkcipher(private);
  440. }
  441. static int skcipher_setkey(void *private, const u8 *key, unsigned int keylen)
  442. {
  443. return crypto_ablkcipher_setkey(private, key, keylen);
  444. }
  445. static void skcipher_sock_destruct(struct sock *sk)
  446. {
  447. struct alg_sock *ask = alg_sk(sk);
  448. struct skcipher_ctx *ctx = ask->private;
  449. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(&ctx->req);
  450. skcipher_free_sgl(sk);
  451. sock_kfree_s(sk, ctx->iv, crypto_ablkcipher_ivsize(tfm));
  452. sock_kfree_s(sk, ctx, ctx->len);
  453. af_alg_release_parent(sk);
  454. }
  455. static int skcipher_accept_parent(void *private, struct sock *sk)
  456. {
  457. struct skcipher_ctx *ctx;
  458. struct alg_sock *ask = alg_sk(sk);
  459. unsigned int len = sizeof(*ctx) + crypto_ablkcipher_reqsize(private);
  460. ctx = sock_kmalloc(sk, len, GFP_KERNEL);
  461. if (!ctx)
  462. return -ENOMEM;
  463. ctx->iv = sock_kmalloc(sk, crypto_ablkcipher_ivsize(private),
  464. GFP_KERNEL);
  465. if (!ctx->iv) {
  466. sock_kfree_s(sk, ctx, len);
  467. return -ENOMEM;
  468. }
  469. memset(ctx->iv, 0, crypto_ablkcipher_ivsize(private));
  470. INIT_LIST_HEAD(&ctx->tsgl);
  471. ctx->len = len;
  472. ctx->used = 0;
  473. ctx->more = 0;
  474. ctx->merge = 0;
  475. ctx->enc = 0;
  476. af_alg_init_completion(&ctx->completion);
  477. ask->private = ctx;
  478. ablkcipher_request_set_tfm(&ctx->req, private);
  479. ablkcipher_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  480. af_alg_complete, &ctx->completion);
  481. sk->sk_destruct = skcipher_sock_destruct;
  482. return 0;
  483. }
  484. static const struct af_alg_type algif_type_skcipher = {
  485. .bind = skcipher_bind,
  486. .release = skcipher_release,
  487. .setkey = skcipher_setkey,
  488. .accept = skcipher_accept_parent,
  489. .ops = &algif_skcipher_ops,
  490. .name = "skcipher",
  491. .owner = THIS_MODULE
  492. };
  493. static int __init algif_skcipher_init(void)
  494. {
  495. return af_alg_register_type(&algif_type_skcipher);
  496. }
  497. static void __exit algif_skcipher_exit(void)
  498. {
  499. int err = af_alg_unregister_type(&algif_type_skcipher);
  500. BUG_ON(err);
  501. }
  502. module_init(algif_skcipher_init);
  503. module_exit(algif_skcipher_exit);
  504. MODULE_LICENSE("GPL");