algif_aead.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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 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. * The following concept of the memory management is used:
  14. *
  15. * The kernel maintains two SGLs, the TX SGL and the RX SGL. The TX SGL is
  16. * filled by user space with the data submitted via sendpage/sendmsg. Filling
  17. * up the TX SGL does not cause a crypto operation -- the data will only be
  18. * tracked by the kernel. Upon receipt of one recvmsg call, the caller must
  19. * provide a buffer which is tracked with the RX SGL.
  20. *
  21. * During the processing of the recvmsg operation, the cipher request is
  22. * allocated and prepared. As part of the recvmsg operation, the processed
  23. * TX buffers are extracted from the TX SGL into a separate SGL.
  24. *
  25. * After the completion of the crypto operation, the RX SGL and the cipher
  26. * request is released. The extracted TX SGL parts are released together with
  27. * the RX SGL release.
  28. */
  29. #include <crypto/internal/aead.h>
  30. #include <crypto/scatterwalk.h>
  31. #include <crypto/if_alg.h>
  32. #include <crypto/skcipher.h>
  33. #include <crypto/null.h>
  34. #include <linux/init.h>
  35. #include <linux/list.h>
  36. #include <linux/kernel.h>
  37. #include <linux/mm.h>
  38. #include <linux/module.h>
  39. #include <linux/net.h>
  40. #include <net/sock.h>
  41. struct aead_tfm {
  42. struct crypto_aead *aead;
  43. struct crypto_skcipher *null_tfm;
  44. };
  45. static inline bool aead_sufficient_data(struct sock *sk)
  46. {
  47. struct alg_sock *ask = alg_sk(sk);
  48. struct sock *psk = ask->parent;
  49. struct alg_sock *pask = alg_sk(psk);
  50. struct af_alg_ctx *ctx = ask->private;
  51. struct aead_tfm *aeadc = pask->private;
  52. struct crypto_aead *tfm = aeadc->aead;
  53. unsigned int as = crypto_aead_authsize(tfm);
  54. /*
  55. * The minimum amount of memory needed for an AEAD cipher is
  56. * the AAD and in case of decryption the tag.
  57. */
  58. return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
  59. }
  60. static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  61. {
  62. struct sock *sk = sock->sk;
  63. struct alg_sock *ask = alg_sk(sk);
  64. struct sock *psk = ask->parent;
  65. struct alg_sock *pask = alg_sk(psk);
  66. struct aead_tfm *aeadc = pask->private;
  67. struct crypto_aead *tfm = aeadc->aead;
  68. unsigned int ivsize = crypto_aead_ivsize(tfm);
  69. return af_alg_sendmsg(sock, msg, size, ivsize);
  70. }
  71. static int crypto_aead_copy_sgl(struct crypto_skcipher *null_tfm,
  72. struct scatterlist *src,
  73. struct scatterlist *dst, unsigned int len)
  74. {
  75. SKCIPHER_REQUEST_ON_STACK(skreq, null_tfm);
  76. skcipher_request_set_tfm(skreq, null_tfm);
  77. skcipher_request_set_callback(skreq, CRYPTO_TFM_REQ_MAY_BACKLOG,
  78. NULL, NULL);
  79. skcipher_request_set_crypt(skreq, src, dst, len, NULL);
  80. return crypto_skcipher_encrypt(skreq);
  81. }
  82. static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
  83. size_t ignored, int flags)
  84. {
  85. struct sock *sk = sock->sk;
  86. struct alg_sock *ask = alg_sk(sk);
  87. struct sock *psk = ask->parent;
  88. struct alg_sock *pask = alg_sk(psk);
  89. struct af_alg_ctx *ctx = ask->private;
  90. struct aead_tfm *aeadc = pask->private;
  91. struct crypto_aead *tfm = aeadc->aead;
  92. struct crypto_skcipher *null_tfm = aeadc->null_tfm;
  93. unsigned int i, as = crypto_aead_authsize(tfm);
  94. struct af_alg_async_req *areq;
  95. struct af_alg_tsgl *tsgl, *tmp;
  96. struct scatterlist *rsgl_src, *tsgl_src = NULL;
  97. int err = 0;
  98. size_t used = 0; /* [in] TX bufs to be en/decrypted */
  99. size_t outlen = 0; /* [out] RX bufs produced by kernel */
  100. size_t usedpages = 0; /* [in] RX bufs to be used from user */
  101. size_t processed = 0; /* [in] TX bufs to be consumed */
  102. if (!ctx->used) {
  103. err = af_alg_wait_for_data(sk, flags);
  104. if (err)
  105. return err;
  106. }
  107. /*
  108. * Data length provided by caller via sendmsg/sendpage that has not
  109. * yet been processed.
  110. */
  111. used = ctx->used;
  112. /*
  113. * Make sure sufficient data is present -- note, the same check is
  114. * is also present in sendmsg/sendpage. The checks in sendpage/sendmsg
  115. * shall provide an information to the data sender that something is
  116. * wrong, but they are irrelevant to maintain the kernel integrity.
  117. * We need this check here too in case user space decides to not honor
  118. * the error message in sendmsg/sendpage and still call recvmsg. This
  119. * check here protects the kernel integrity.
  120. */
  121. if (!aead_sufficient_data(sk))
  122. return -EINVAL;
  123. /*
  124. * Calculate the minimum output buffer size holding the result of the
  125. * cipher operation. When encrypting data, the receiving buffer is
  126. * larger by the tag length compared to the input buffer as the
  127. * encryption operation generates the tag. For decryption, the input
  128. * buffer provides the tag which is consumed resulting in only the
  129. * plaintext without a buffer for the tag returned to the caller.
  130. */
  131. if (ctx->enc)
  132. outlen = used + as;
  133. else
  134. outlen = used - as;
  135. /*
  136. * The cipher operation input data is reduced by the associated data
  137. * length as this data is processed separately later on.
  138. */
  139. used -= ctx->aead_assoclen;
  140. /* Allocate cipher request for current operation. */
  141. areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) +
  142. crypto_aead_reqsize(tfm));
  143. if (IS_ERR(areq))
  144. return PTR_ERR(areq);
  145. /* convert iovecs of output buffers into RX SGL */
  146. err = af_alg_get_rsgl(sk, msg, flags, areq, outlen, &usedpages);
  147. if (err)
  148. goto free;
  149. /*
  150. * Ensure output buffer is sufficiently large. If the caller provides
  151. * less buffer space, only use the relative required input size. This
  152. * allows AIO operation where the caller sent all data to be processed
  153. * and the AIO operation performs the operation on the different chunks
  154. * of the input data.
  155. */
  156. if (usedpages < outlen) {
  157. size_t less = outlen - usedpages;
  158. if (used < less) {
  159. err = -EINVAL;
  160. goto free;
  161. }
  162. used -= less;
  163. outlen -= less;
  164. }
  165. processed = used + ctx->aead_assoclen;
  166. list_for_each_entry_safe(tsgl, tmp, &ctx->tsgl_list, list) {
  167. for (i = 0; i < tsgl->cur; i++) {
  168. struct scatterlist *process_sg = tsgl->sg + i;
  169. if (!(process_sg->length) || !sg_page(process_sg))
  170. continue;
  171. tsgl_src = process_sg;
  172. break;
  173. }
  174. if (tsgl_src)
  175. break;
  176. }
  177. if (processed && !tsgl_src) {
  178. err = -EFAULT;
  179. goto free;
  180. }
  181. /*
  182. * Copy of AAD from source to destination
  183. *
  184. * The AAD is copied to the destination buffer without change. Even
  185. * when user space uses an in-place cipher operation, the kernel
  186. * will copy the data as it does not see whether such in-place operation
  187. * is initiated.
  188. *
  189. * To ensure efficiency, the following implementation ensure that the
  190. * ciphers are invoked to perform a crypto operation in-place. This
  191. * is achieved by memory management specified as follows.
  192. */
  193. /* Use the RX SGL as source (and destination) for crypto op. */
  194. rsgl_src = areq->first_rsgl.sgl.sg;
  195. if (ctx->enc) {
  196. /*
  197. * Encryption operation - The in-place cipher operation is
  198. * achieved by the following operation:
  199. *
  200. * TX SGL: AAD || PT
  201. * | |
  202. * | copy |
  203. * v v
  204. * RX SGL: AAD || PT || Tag
  205. */
  206. err = crypto_aead_copy_sgl(null_tfm, tsgl_src,
  207. areq->first_rsgl.sgl.sg, processed);
  208. if (err)
  209. goto free;
  210. af_alg_pull_tsgl(sk, processed, NULL, 0);
  211. } else {
  212. /*
  213. * Decryption operation - To achieve an in-place cipher
  214. * operation, the following SGL structure is used:
  215. *
  216. * TX SGL: AAD || CT || Tag
  217. * | | ^
  218. * | copy | | Create SGL link.
  219. * v v |
  220. * RX SGL: AAD || CT ----+
  221. */
  222. /* Copy AAD || CT to RX SGL buffer for in-place operation. */
  223. err = crypto_aead_copy_sgl(null_tfm, tsgl_src,
  224. areq->first_rsgl.sgl.sg, outlen);
  225. if (err)
  226. goto free;
  227. /* Create TX SGL for tag and chain it to RX SGL. */
  228. areq->tsgl_entries = af_alg_count_tsgl(sk, processed,
  229. processed - as);
  230. if (!areq->tsgl_entries)
  231. areq->tsgl_entries = 1;
  232. areq->tsgl = sock_kmalloc(sk, array_size(sizeof(*areq->tsgl),
  233. areq->tsgl_entries),
  234. GFP_KERNEL);
  235. if (!areq->tsgl) {
  236. err = -ENOMEM;
  237. goto free;
  238. }
  239. sg_init_table(areq->tsgl, areq->tsgl_entries);
  240. /* Release TX SGL, except for tag data and reassign tag data. */
  241. af_alg_pull_tsgl(sk, processed, areq->tsgl, processed - as);
  242. /* chain the areq TX SGL holding the tag with RX SGL */
  243. if (usedpages) {
  244. /* RX SGL present */
  245. struct af_alg_sgl *sgl_prev = &areq->last_rsgl->sgl;
  246. sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1);
  247. sg_chain(sgl_prev->sg, sgl_prev->npages + 1,
  248. areq->tsgl);
  249. } else
  250. /* no RX SGL present (e.g. authentication only) */
  251. rsgl_src = areq->tsgl;
  252. }
  253. /* Initialize the crypto operation */
  254. aead_request_set_crypt(&areq->cra_u.aead_req, rsgl_src,
  255. areq->first_rsgl.sgl.sg, used, ctx->iv);
  256. aead_request_set_ad(&areq->cra_u.aead_req, ctx->aead_assoclen);
  257. aead_request_set_tfm(&areq->cra_u.aead_req, tfm);
  258. if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) {
  259. /* AIO operation */
  260. sock_hold(sk);
  261. areq->iocb = msg->msg_iocb;
  262. /* Remember output size that will be generated. */
  263. areq->outlen = outlen;
  264. aead_request_set_callback(&areq->cra_u.aead_req,
  265. CRYPTO_TFM_REQ_MAY_BACKLOG,
  266. af_alg_async_cb, areq);
  267. err = ctx->enc ? crypto_aead_encrypt(&areq->cra_u.aead_req) :
  268. crypto_aead_decrypt(&areq->cra_u.aead_req);
  269. /* AIO operation in progress */
  270. if (err == -EINPROGRESS || err == -EBUSY)
  271. return -EIOCBQUEUED;
  272. sock_put(sk);
  273. } else {
  274. /* Synchronous operation */
  275. aead_request_set_callback(&areq->cra_u.aead_req,
  276. CRYPTO_TFM_REQ_MAY_BACKLOG,
  277. crypto_req_done, &ctx->wait);
  278. err = crypto_wait_req(ctx->enc ?
  279. crypto_aead_encrypt(&areq->cra_u.aead_req) :
  280. crypto_aead_decrypt(&areq->cra_u.aead_req),
  281. &ctx->wait);
  282. }
  283. free:
  284. af_alg_free_resources(areq);
  285. return err ? err : outlen;
  286. }
  287. static int aead_recvmsg(struct socket *sock, struct msghdr *msg,
  288. size_t ignored, int flags)
  289. {
  290. struct sock *sk = sock->sk;
  291. int ret = 0;
  292. lock_sock(sk);
  293. while (msg_data_left(msg)) {
  294. int err = _aead_recvmsg(sock, msg, ignored, flags);
  295. /*
  296. * This error covers -EIOCBQUEUED which implies that we can
  297. * only handle one AIO request. If the caller wants to have
  298. * multiple AIO requests in parallel, he must make multiple
  299. * separate AIO calls.
  300. *
  301. * Also return the error if no data has been processed so far.
  302. */
  303. if (err <= 0) {
  304. if (err == -EIOCBQUEUED || err == -EBADMSG || !ret)
  305. ret = err;
  306. goto out;
  307. }
  308. ret += err;
  309. }
  310. out:
  311. af_alg_wmem_wakeup(sk);
  312. release_sock(sk);
  313. return ret;
  314. }
  315. static struct proto_ops algif_aead_ops = {
  316. .family = PF_ALG,
  317. .connect = sock_no_connect,
  318. .socketpair = sock_no_socketpair,
  319. .getname = sock_no_getname,
  320. .ioctl = sock_no_ioctl,
  321. .listen = sock_no_listen,
  322. .shutdown = sock_no_shutdown,
  323. .getsockopt = sock_no_getsockopt,
  324. .mmap = sock_no_mmap,
  325. .bind = sock_no_bind,
  326. .accept = sock_no_accept,
  327. .setsockopt = sock_no_setsockopt,
  328. .release = af_alg_release,
  329. .sendmsg = aead_sendmsg,
  330. .sendpage = af_alg_sendpage,
  331. .recvmsg = aead_recvmsg,
  332. .poll = af_alg_poll,
  333. };
  334. static int aead_check_key(struct socket *sock)
  335. {
  336. int err = 0;
  337. struct sock *psk;
  338. struct alg_sock *pask;
  339. struct aead_tfm *tfm;
  340. struct sock *sk = sock->sk;
  341. struct alg_sock *ask = alg_sk(sk);
  342. lock_sock(sk);
  343. if (ask->refcnt)
  344. goto unlock_child;
  345. psk = ask->parent;
  346. pask = alg_sk(ask->parent);
  347. tfm = pask->private;
  348. err = -ENOKEY;
  349. lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
  350. if (crypto_aead_get_flags(tfm->aead) & CRYPTO_TFM_NEED_KEY)
  351. goto unlock;
  352. if (!pask->refcnt++)
  353. sock_hold(psk);
  354. ask->refcnt = 1;
  355. sock_put(psk);
  356. err = 0;
  357. unlock:
  358. release_sock(psk);
  359. unlock_child:
  360. release_sock(sk);
  361. return err;
  362. }
  363. static int aead_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
  364. size_t size)
  365. {
  366. int err;
  367. err = aead_check_key(sock);
  368. if (err)
  369. return err;
  370. return aead_sendmsg(sock, msg, size);
  371. }
  372. static ssize_t aead_sendpage_nokey(struct socket *sock, struct page *page,
  373. int offset, size_t size, int flags)
  374. {
  375. int err;
  376. err = aead_check_key(sock);
  377. if (err)
  378. return err;
  379. return af_alg_sendpage(sock, page, offset, size, flags);
  380. }
  381. static int aead_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
  382. size_t ignored, int flags)
  383. {
  384. int err;
  385. err = aead_check_key(sock);
  386. if (err)
  387. return err;
  388. return aead_recvmsg(sock, msg, ignored, flags);
  389. }
  390. static struct proto_ops algif_aead_ops_nokey = {
  391. .family = PF_ALG,
  392. .connect = sock_no_connect,
  393. .socketpair = sock_no_socketpair,
  394. .getname = sock_no_getname,
  395. .ioctl = sock_no_ioctl,
  396. .listen = sock_no_listen,
  397. .shutdown = sock_no_shutdown,
  398. .getsockopt = sock_no_getsockopt,
  399. .mmap = sock_no_mmap,
  400. .bind = sock_no_bind,
  401. .accept = sock_no_accept,
  402. .setsockopt = sock_no_setsockopt,
  403. .release = af_alg_release,
  404. .sendmsg = aead_sendmsg_nokey,
  405. .sendpage = aead_sendpage_nokey,
  406. .recvmsg = aead_recvmsg_nokey,
  407. .poll = af_alg_poll,
  408. };
  409. static void *aead_bind(const char *name, u32 type, u32 mask)
  410. {
  411. struct aead_tfm *tfm;
  412. struct crypto_aead *aead;
  413. struct crypto_skcipher *null_tfm;
  414. tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
  415. if (!tfm)
  416. return ERR_PTR(-ENOMEM);
  417. aead = crypto_alloc_aead(name, type, mask);
  418. if (IS_ERR(aead)) {
  419. kfree(tfm);
  420. return ERR_CAST(aead);
  421. }
  422. null_tfm = crypto_get_default_null_skcipher();
  423. if (IS_ERR(null_tfm)) {
  424. crypto_free_aead(aead);
  425. kfree(tfm);
  426. return ERR_CAST(null_tfm);
  427. }
  428. tfm->aead = aead;
  429. tfm->null_tfm = null_tfm;
  430. return tfm;
  431. }
  432. static void aead_release(void *private)
  433. {
  434. struct aead_tfm *tfm = private;
  435. crypto_free_aead(tfm->aead);
  436. crypto_put_default_null_skcipher();
  437. kfree(tfm);
  438. }
  439. static int aead_setauthsize(void *private, unsigned int authsize)
  440. {
  441. struct aead_tfm *tfm = private;
  442. return crypto_aead_setauthsize(tfm->aead, authsize);
  443. }
  444. static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
  445. {
  446. struct aead_tfm *tfm = private;
  447. return crypto_aead_setkey(tfm->aead, key, keylen);
  448. }
  449. static void aead_sock_destruct(struct sock *sk)
  450. {
  451. struct alg_sock *ask = alg_sk(sk);
  452. struct af_alg_ctx *ctx = ask->private;
  453. struct sock *psk = ask->parent;
  454. struct alg_sock *pask = alg_sk(psk);
  455. struct aead_tfm *aeadc = pask->private;
  456. struct crypto_aead *tfm = aeadc->aead;
  457. unsigned int ivlen = crypto_aead_ivsize(tfm);
  458. af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
  459. sock_kzfree_s(sk, ctx->iv, ivlen);
  460. sock_kfree_s(sk, ctx, ctx->len);
  461. af_alg_release_parent(sk);
  462. }
  463. static int aead_accept_parent_nokey(void *private, struct sock *sk)
  464. {
  465. struct af_alg_ctx *ctx;
  466. struct alg_sock *ask = alg_sk(sk);
  467. struct aead_tfm *tfm = private;
  468. struct crypto_aead *aead = tfm->aead;
  469. unsigned int len = sizeof(*ctx);
  470. unsigned int ivlen = crypto_aead_ivsize(aead);
  471. ctx = sock_kmalloc(sk, len, GFP_KERNEL);
  472. if (!ctx)
  473. return -ENOMEM;
  474. memset(ctx, 0, len);
  475. ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
  476. if (!ctx->iv) {
  477. sock_kfree_s(sk, ctx, len);
  478. return -ENOMEM;
  479. }
  480. memset(ctx->iv, 0, ivlen);
  481. INIT_LIST_HEAD(&ctx->tsgl_list);
  482. ctx->len = len;
  483. ctx->used = 0;
  484. atomic_set(&ctx->rcvused, 0);
  485. ctx->more = 0;
  486. ctx->merge = 0;
  487. ctx->enc = 0;
  488. ctx->aead_assoclen = 0;
  489. crypto_init_wait(&ctx->wait);
  490. ask->private = ctx;
  491. sk->sk_destruct = aead_sock_destruct;
  492. return 0;
  493. }
  494. static int aead_accept_parent(void *private, struct sock *sk)
  495. {
  496. struct aead_tfm *tfm = private;
  497. if (crypto_aead_get_flags(tfm->aead) & CRYPTO_TFM_NEED_KEY)
  498. return -ENOKEY;
  499. return aead_accept_parent_nokey(private, sk);
  500. }
  501. static const struct af_alg_type algif_type_aead = {
  502. .bind = aead_bind,
  503. .release = aead_release,
  504. .setkey = aead_setkey,
  505. .setauthsize = aead_setauthsize,
  506. .accept = aead_accept_parent,
  507. .accept_nokey = aead_accept_parent_nokey,
  508. .ops = &algif_aead_ops,
  509. .ops_nokey = &algif_aead_ops_nokey,
  510. .name = "aead",
  511. .owner = THIS_MODULE
  512. };
  513. static int __init algif_aead_init(void)
  514. {
  515. return af_alg_register_type(&algif_type_aead);
  516. }
  517. static void __exit algif_aead_exit(void)
  518. {
  519. int err = af_alg_unregister_type(&algif_type_aead);
  520. BUG_ON(err);
  521. }
  522. module_init(algif_aead_init);
  523. module_exit(algif_aead_exit);
  524. MODULE_LICENSE("GPL");
  525. MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
  526. MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");