algif_skcipher.c 13 KB

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