algif_aead.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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/scatterwalk.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 aead_sg_list {
  25. unsigned int cur;
  26. struct scatterlist sg[ALG_MAX_PAGES];
  27. };
  28. struct aead_ctx {
  29. struct aead_sg_list tsgl;
  30. /*
  31. * RSGL_MAX_ENTRIES is an artificial limit where user space at maximum
  32. * can cause the kernel to allocate RSGL_MAX_ENTRIES * ALG_MAX_PAGES
  33. * bytes
  34. */
  35. #define RSGL_MAX_ENTRIES ALG_MAX_PAGES
  36. struct af_alg_sgl rsgl[RSGL_MAX_ENTRIES];
  37. void *iv;
  38. struct af_alg_completion completion;
  39. unsigned long used;
  40. unsigned int len;
  41. bool more;
  42. bool merge;
  43. bool enc;
  44. size_t aead_assoclen;
  45. struct aead_request aead_req;
  46. };
  47. static inline int aead_sndbuf(struct sock *sk)
  48. {
  49. struct alg_sock *ask = alg_sk(sk);
  50. struct aead_ctx *ctx = ask->private;
  51. return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
  52. ctx->used, 0);
  53. }
  54. static inline bool aead_writable(struct sock *sk)
  55. {
  56. return PAGE_SIZE <= aead_sndbuf(sk);
  57. }
  58. static inline bool aead_sufficient_data(struct aead_ctx *ctx)
  59. {
  60. unsigned as = crypto_aead_authsize(crypto_aead_reqtfm(&ctx->aead_req));
  61. return (ctx->used >= (ctx->aead_assoclen + (ctx->enc ? 0 : as)));
  62. }
  63. static void aead_put_sgl(struct sock *sk)
  64. {
  65. struct alg_sock *ask = alg_sk(sk);
  66. struct aead_ctx *ctx = ask->private;
  67. struct aead_sg_list *sgl = &ctx->tsgl;
  68. struct scatterlist *sg = sgl->sg;
  69. unsigned int i;
  70. for (i = 0; i < sgl->cur; i++) {
  71. if (!sg_page(sg + i))
  72. continue;
  73. put_page(sg_page(sg + i));
  74. sg_assign_page(sg + i, NULL);
  75. }
  76. sgl->cur = 0;
  77. ctx->used = 0;
  78. ctx->more = 0;
  79. ctx->merge = 0;
  80. }
  81. static void aead_wmem_wakeup(struct sock *sk)
  82. {
  83. struct socket_wq *wq;
  84. if (!aead_writable(sk))
  85. return;
  86. rcu_read_lock();
  87. wq = rcu_dereference(sk->sk_wq);
  88. if (wq_has_sleeper(wq))
  89. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  90. POLLRDNORM |
  91. POLLRDBAND);
  92. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
  93. rcu_read_unlock();
  94. }
  95. static int aead_wait_for_data(struct sock *sk, unsigned flags)
  96. {
  97. struct alg_sock *ask = alg_sk(sk);
  98. struct aead_ctx *ctx = ask->private;
  99. long timeout;
  100. DEFINE_WAIT(wait);
  101. int err = -ERESTARTSYS;
  102. if (flags & MSG_DONTWAIT)
  103. return -EAGAIN;
  104. set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  105. for (;;) {
  106. if (signal_pending(current))
  107. break;
  108. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  109. timeout = MAX_SCHEDULE_TIMEOUT;
  110. if (sk_wait_event(sk, &timeout, !ctx->more)) {
  111. err = 0;
  112. break;
  113. }
  114. }
  115. finish_wait(sk_sleep(sk), &wait);
  116. clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  117. return err;
  118. }
  119. static void aead_data_wakeup(struct sock *sk)
  120. {
  121. struct alg_sock *ask = alg_sk(sk);
  122. struct aead_ctx *ctx = ask->private;
  123. struct socket_wq *wq;
  124. if (ctx->more)
  125. return;
  126. if (!ctx->used)
  127. return;
  128. rcu_read_lock();
  129. wq = rcu_dereference(sk->sk_wq);
  130. if (wq_has_sleeper(wq))
  131. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  132. POLLRDNORM |
  133. POLLRDBAND);
  134. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  135. rcu_read_unlock();
  136. }
  137. static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  138. {
  139. struct sock *sk = sock->sk;
  140. struct alg_sock *ask = alg_sk(sk);
  141. struct aead_ctx *ctx = ask->private;
  142. unsigned ivsize =
  143. crypto_aead_ivsize(crypto_aead_reqtfm(&ctx->aead_req));
  144. struct aead_sg_list *sgl = &ctx->tsgl;
  145. struct af_alg_control con = {};
  146. long copied = 0;
  147. bool enc = 0;
  148. bool init = 0;
  149. int err = -EINVAL;
  150. if (msg->msg_controllen) {
  151. err = af_alg_cmsg_send(msg, &con);
  152. if (err)
  153. return err;
  154. init = 1;
  155. switch (con.op) {
  156. case ALG_OP_ENCRYPT:
  157. enc = 1;
  158. break;
  159. case ALG_OP_DECRYPT:
  160. enc = 0;
  161. break;
  162. default:
  163. return -EINVAL;
  164. }
  165. if (con.iv && con.iv->ivlen != ivsize)
  166. return -EINVAL;
  167. }
  168. lock_sock(sk);
  169. if (!ctx->more && ctx->used)
  170. goto unlock;
  171. if (init) {
  172. ctx->enc = enc;
  173. if (con.iv)
  174. memcpy(ctx->iv, con.iv->iv, ivsize);
  175. ctx->aead_assoclen = con.aead_assoclen;
  176. }
  177. while (size) {
  178. unsigned long len = size;
  179. struct scatterlist *sg = NULL;
  180. /* use the existing memory in an allocated page */
  181. if (ctx->merge) {
  182. sg = sgl->sg + sgl->cur - 1;
  183. len = min_t(unsigned long, len,
  184. PAGE_SIZE - sg->offset - sg->length);
  185. err = memcpy_from_msg(page_address(sg_page(sg)) +
  186. sg->offset + sg->length,
  187. msg, len);
  188. if (err)
  189. goto unlock;
  190. sg->length += len;
  191. ctx->merge = (sg->offset + sg->length) &
  192. (PAGE_SIZE - 1);
  193. ctx->used += len;
  194. copied += len;
  195. size -= len;
  196. continue;
  197. }
  198. if (!aead_writable(sk)) {
  199. /* user space sent too much data */
  200. aead_put_sgl(sk);
  201. err = -EMSGSIZE;
  202. goto unlock;
  203. }
  204. /* allocate a new page */
  205. len = min_t(unsigned long, size, aead_sndbuf(sk));
  206. while (len) {
  207. int plen = 0;
  208. if (sgl->cur >= ALG_MAX_PAGES) {
  209. aead_put_sgl(sk);
  210. err = -E2BIG;
  211. goto unlock;
  212. }
  213. sg = sgl->sg + sgl->cur;
  214. plen = min_t(int, len, PAGE_SIZE);
  215. sg_assign_page(sg, alloc_page(GFP_KERNEL));
  216. err = -ENOMEM;
  217. if (!sg_page(sg))
  218. goto unlock;
  219. err = memcpy_from_msg(page_address(sg_page(sg)),
  220. msg, plen);
  221. if (err) {
  222. __free_page(sg_page(sg));
  223. sg_assign_page(sg, NULL);
  224. goto unlock;
  225. }
  226. sg->offset = 0;
  227. sg->length = plen;
  228. len -= plen;
  229. ctx->used += plen;
  230. copied += plen;
  231. sgl->cur++;
  232. size -= plen;
  233. ctx->merge = plen & (PAGE_SIZE - 1);
  234. }
  235. }
  236. err = 0;
  237. ctx->more = msg->msg_flags & MSG_MORE;
  238. if (!ctx->more && !aead_sufficient_data(ctx)) {
  239. aead_put_sgl(sk);
  240. err = -EMSGSIZE;
  241. }
  242. unlock:
  243. aead_data_wakeup(sk);
  244. release_sock(sk);
  245. return err ?: copied;
  246. }
  247. static ssize_t aead_sendpage(struct socket *sock, struct page *page,
  248. int offset, size_t size, int flags)
  249. {
  250. struct sock *sk = sock->sk;
  251. struct alg_sock *ask = alg_sk(sk);
  252. struct aead_ctx *ctx = ask->private;
  253. struct aead_sg_list *sgl = &ctx->tsgl;
  254. int err = -EINVAL;
  255. if (flags & MSG_SENDPAGE_NOTLAST)
  256. flags |= MSG_MORE;
  257. if (sgl->cur >= ALG_MAX_PAGES)
  258. return -E2BIG;
  259. lock_sock(sk);
  260. if (!ctx->more && ctx->used)
  261. goto unlock;
  262. if (!size)
  263. goto done;
  264. if (!aead_writable(sk)) {
  265. /* user space sent too much data */
  266. aead_put_sgl(sk);
  267. err = -EMSGSIZE;
  268. goto unlock;
  269. }
  270. ctx->merge = 0;
  271. get_page(page);
  272. sg_set_page(sgl->sg + sgl->cur, page, size, offset);
  273. sgl->cur++;
  274. ctx->used += size;
  275. err = 0;
  276. done:
  277. ctx->more = flags & MSG_MORE;
  278. if (!ctx->more && !aead_sufficient_data(ctx)) {
  279. aead_put_sgl(sk);
  280. err = -EMSGSIZE;
  281. }
  282. unlock:
  283. aead_data_wakeup(sk);
  284. release_sock(sk);
  285. return err ?: size;
  286. }
  287. static int aead_recvmsg(struct socket *sock, struct msghdr *msg, size_t ignored, int flags)
  288. {
  289. struct sock *sk = sock->sk;
  290. struct alg_sock *ask = alg_sk(sk);
  291. struct aead_ctx *ctx = ask->private;
  292. unsigned bs = crypto_aead_blocksize(crypto_aead_reqtfm(&ctx->aead_req));
  293. unsigned as = crypto_aead_authsize(crypto_aead_reqtfm(&ctx->aead_req));
  294. struct aead_sg_list *sgl = &ctx->tsgl;
  295. struct scatterlist *sg = NULL;
  296. struct scatterlist assoc[ALG_MAX_PAGES];
  297. size_t assoclen = 0;
  298. unsigned int i = 0;
  299. int err = -EINVAL;
  300. unsigned long used = 0;
  301. size_t outlen = 0;
  302. size_t usedpages = 0;
  303. unsigned int cnt = 0;
  304. /* Limit number of IOV blocks to be accessed below */
  305. if (msg->msg_iter.nr_segs > RSGL_MAX_ENTRIES)
  306. return -ENOMSG;
  307. lock_sock(sk);
  308. /*
  309. * AEAD memory structure: For encryption, the tag is appended to the
  310. * ciphertext which implies that the memory allocated for the ciphertext
  311. * must be increased by the tag length. For decryption, the tag
  312. * is expected to be concatenated to the ciphertext. The plaintext
  313. * therefore has a memory size of the ciphertext minus the tag length.
  314. *
  315. * The memory structure for cipher operation has the following
  316. * structure:
  317. * AEAD encryption input: assoc data || plaintext
  318. * AEAD encryption output: cipherntext || auth tag
  319. * AEAD decryption input: assoc data || ciphertext || auth tag
  320. * AEAD decryption output: plaintext
  321. */
  322. if (ctx->more) {
  323. err = aead_wait_for_data(sk, flags);
  324. if (err)
  325. goto unlock;
  326. }
  327. used = ctx->used;
  328. /*
  329. * Make sure sufficient data is present -- note, the same check is
  330. * is also present in sendmsg/sendpage. The checks in sendpage/sendmsg
  331. * shall provide an information to the data sender that something is
  332. * wrong, but they are irrelevant to maintain the kernel integrity.
  333. * We need this check here too in case user space decides to not honor
  334. * the error message in sendmsg/sendpage and still call recvmsg. This
  335. * check here protects the kernel integrity.
  336. */
  337. if (!aead_sufficient_data(ctx))
  338. goto unlock;
  339. /*
  340. * The cipher operation input data is reduced by the associated data
  341. * length as this data is processed separately later on.
  342. */
  343. used -= ctx->aead_assoclen;
  344. if (ctx->enc) {
  345. /* round up output buffer to multiple of block size */
  346. outlen = ((used + bs - 1) / bs * bs);
  347. /* add the size needed for the auth tag to be created */
  348. outlen += as;
  349. } else {
  350. /* output data size is input without the authentication tag */
  351. outlen = used - as;
  352. /* round up output buffer to multiple of block size */
  353. outlen = ((outlen + bs - 1) / bs * bs);
  354. }
  355. /* convert iovecs of output buffers into scatterlists */
  356. while (iov_iter_count(&msg->msg_iter)) {
  357. size_t seglen = min_t(size_t, iov_iter_count(&msg->msg_iter),
  358. (outlen - usedpages));
  359. /* make one iovec available as scatterlist */
  360. err = af_alg_make_sg(&ctx->rsgl[cnt], &msg->msg_iter,
  361. seglen);
  362. if (err < 0)
  363. goto unlock;
  364. usedpages += err;
  365. /* chain the new scatterlist with initial list */
  366. if (cnt)
  367. scatterwalk_crypto_chain(ctx->rsgl[0].sg,
  368. ctx->rsgl[cnt].sg, 1,
  369. sg_nents(ctx->rsgl[cnt-1].sg));
  370. /* we do not need more iovecs as we have sufficient memory */
  371. if (outlen <= usedpages)
  372. break;
  373. iov_iter_advance(&msg->msg_iter, err);
  374. cnt++;
  375. }
  376. err = -EINVAL;
  377. /* ensure output buffer is sufficiently large */
  378. if (usedpages < outlen)
  379. goto unlock;
  380. sg_init_table(assoc, ALG_MAX_PAGES);
  381. assoclen = ctx->aead_assoclen;
  382. /*
  383. * Split scatterlist into two: first part becomes AD, second part
  384. * is plaintext / ciphertext. The first part is assigned to assoc
  385. * scatterlist. When this loop finishes, sg points to the start of the
  386. * plaintext / ciphertext.
  387. */
  388. for (i = 0; i < ctx->tsgl.cur; i++) {
  389. sg = sgl->sg + i;
  390. if (sg->length <= assoclen) {
  391. /* AD is larger than one page */
  392. sg_set_page(assoc + i, sg_page(sg),
  393. sg->length, sg->offset);
  394. assoclen -= sg->length;
  395. if (i >= ctx->tsgl.cur)
  396. goto unlock;
  397. } else if (!assoclen) {
  398. /* current page is to start of plaintext / ciphertext */
  399. if (i)
  400. /* AD terminates at page boundary */
  401. sg_mark_end(assoc + i - 1);
  402. else
  403. /* AD size is zero */
  404. sg_mark_end(assoc);
  405. break;
  406. } else {
  407. /* AD does not terminate at page boundary */
  408. sg_set_page(assoc + i, sg_page(sg),
  409. assoclen, sg->offset);
  410. sg_mark_end(assoc + i);
  411. /* plaintext / ciphertext starts after AD */
  412. sg->length -= assoclen;
  413. sg->offset += assoclen;
  414. break;
  415. }
  416. }
  417. aead_request_set_assoc(&ctx->aead_req, assoc, ctx->aead_assoclen);
  418. aead_request_set_crypt(&ctx->aead_req, sg, ctx->rsgl[0].sg, used,
  419. ctx->iv);
  420. err = af_alg_wait_for_completion(ctx->enc ?
  421. crypto_aead_encrypt(&ctx->aead_req) :
  422. crypto_aead_decrypt(&ctx->aead_req),
  423. &ctx->completion);
  424. if (err) {
  425. /* EBADMSG implies a valid cipher operation took place */
  426. if (err == -EBADMSG)
  427. aead_put_sgl(sk);
  428. goto unlock;
  429. }
  430. aead_put_sgl(sk);
  431. err = 0;
  432. unlock:
  433. for (i = 0; i < cnt; i++)
  434. af_alg_free_sg(&ctx->rsgl[i]);
  435. aead_wmem_wakeup(sk);
  436. release_sock(sk);
  437. return err ? err : outlen;
  438. }
  439. static unsigned int aead_poll(struct file *file, struct socket *sock,
  440. poll_table *wait)
  441. {
  442. struct sock *sk = sock->sk;
  443. struct alg_sock *ask = alg_sk(sk);
  444. struct aead_ctx *ctx = ask->private;
  445. unsigned int mask;
  446. sock_poll_wait(file, sk_sleep(sk), wait);
  447. mask = 0;
  448. if (!ctx->more)
  449. mask |= POLLIN | POLLRDNORM;
  450. if (aead_writable(sk))
  451. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  452. return mask;
  453. }
  454. static struct proto_ops algif_aead_ops = {
  455. .family = PF_ALG,
  456. .connect = sock_no_connect,
  457. .socketpair = sock_no_socketpair,
  458. .getname = sock_no_getname,
  459. .ioctl = sock_no_ioctl,
  460. .listen = sock_no_listen,
  461. .shutdown = sock_no_shutdown,
  462. .getsockopt = sock_no_getsockopt,
  463. .mmap = sock_no_mmap,
  464. .bind = sock_no_bind,
  465. .accept = sock_no_accept,
  466. .setsockopt = sock_no_setsockopt,
  467. .release = af_alg_release,
  468. .sendmsg = aead_sendmsg,
  469. .sendpage = aead_sendpage,
  470. .recvmsg = aead_recvmsg,
  471. .poll = aead_poll,
  472. };
  473. static void *aead_bind(const char *name, u32 type, u32 mask)
  474. {
  475. return crypto_alloc_aead(name, type, mask);
  476. }
  477. static void aead_release(void *private)
  478. {
  479. crypto_free_aead(private);
  480. }
  481. static int aead_setauthsize(void *private, unsigned int authsize)
  482. {
  483. return crypto_aead_setauthsize(private, authsize);
  484. }
  485. static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
  486. {
  487. return crypto_aead_setkey(private, key, keylen);
  488. }
  489. static void aead_sock_destruct(struct sock *sk)
  490. {
  491. struct alg_sock *ask = alg_sk(sk);
  492. struct aead_ctx *ctx = ask->private;
  493. unsigned int ivlen = crypto_aead_ivsize(
  494. crypto_aead_reqtfm(&ctx->aead_req));
  495. aead_put_sgl(sk);
  496. sock_kzfree_s(sk, ctx->iv, ivlen);
  497. sock_kfree_s(sk, ctx, ctx->len);
  498. af_alg_release_parent(sk);
  499. }
  500. static int aead_accept_parent(void *private, struct sock *sk)
  501. {
  502. struct aead_ctx *ctx;
  503. struct alg_sock *ask = alg_sk(sk);
  504. unsigned int len = sizeof(*ctx) + crypto_aead_reqsize(private);
  505. unsigned int ivlen = crypto_aead_ivsize(private);
  506. ctx = sock_kmalloc(sk, len, GFP_KERNEL);
  507. if (!ctx)
  508. return -ENOMEM;
  509. memset(ctx, 0, len);
  510. ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
  511. if (!ctx->iv) {
  512. sock_kfree_s(sk, ctx, len);
  513. return -ENOMEM;
  514. }
  515. memset(ctx->iv, 0, ivlen);
  516. ctx->len = len;
  517. ctx->used = 0;
  518. ctx->more = 0;
  519. ctx->merge = 0;
  520. ctx->enc = 0;
  521. ctx->tsgl.cur = 0;
  522. ctx->aead_assoclen = 0;
  523. af_alg_init_completion(&ctx->completion);
  524. sg_init_table(ctx->tsgl.sg, ALG_MAX_PAGES);
  525. ask->private = ctx;
  526. aead_request_set_tfm(&ctx->aead_req, private);
  527. aead_request_set_callback(&ctx->aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  528. af_alg_complete, &ctx->completion);
  529. sk->sk_destruct = aead_sock_destruct;
  530. return 0;
  531. }
  532. static const struct af_alg_type algif_type_aead = {
  533. .bind = aead_bind,
  534. .release = aead_release,
  535. .setkey = aead_setkey,
  536. .setauthsize = aead_setauthsize,
  537. .accept = aead_accept_parent,
  538. .ops = &algif_aead_ops,
  539. .name = "aead",
  540. .owner = THIS_MODULE
  541. };
  542. static int __init algif_aead_init(void)
  543. {
  544. return af_alg_register_type(&algif_type_aead);
  545. }
  546. static void __exit algif_aead_exit(void)
  547. {
  548. int err = af_alg_unregister_type(&algif_type_aead);
  549. BUG_ON(err);
  550. }
  551. module_init(algif_aead_init);
  552. module_exit(algif_aead_exit);
  553. MODULE_LICENSE("GPL");
  554. MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
  555. MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");