algif_aead.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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_SLEEP,
  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_SLEEP,
  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)
  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_SLEEP |
  277. CRYPTO_TFM_REQ_MAY_BACKLOG,
  278. crypto_req_done, &ctx->wait);
  279. err = crypto_wait_req(ctx->enc ?
  280. crypto_aead_encrypt(&areq->cra_u.aead_req) :
  281. crypto_aead_decrypt(&areq->cra_u.aead_req),
  282. &ctx->wait);
  283. }
  284. free:
  285. af_alg_free_resources(areq);
  286. return err ? err : outlen;
  287. }
  288. static int aead_recvmsg(struct socket *sock, struct msghdr *msg,
  289. size_t ignored, int flags)
  290. {
  291. struct sock *sk = sock->sk;
  292. int ret = 0;
  293. lock_sock(sk);
  294. while (msg_data_left(msg)) {
  295. int err = _aead_recvmsg(sock, msg, ignored, flags);
  296. /*
  297. * This error covers -EIOCBQUEUED which implies that we can
  298. * only handle one AIO request. If the caller wants to have
  299. * multiple AIO requests in parallel, he must make multiple
  300. * separate AIO calls.
  301. *
  302. * Also return the error if no data has been processed so far.
  303. */
  304. if (err <= 0) {
  305. if (err == -EIOCBQUEUED || err == -EBADMSG || !ret)
  306. ret = err;
  307. goto out;
  308. }
  309. ret += err;
  310. }
  311. out:
  312. af_alg_wmem_wakeup(sk);
  313. release_sock(sk);
  314. return ret;
  315. }
  316. static struct proto_ops algif_aead_ops = {
  317. .family = PF_ALG,
  318. .connect = sock_no_connect,
  319. .socketpair = sock_no_socketpair,
  320. .getname = sock_no_getname,
  321. .ioctl = sock_no_ioctl,
  322. .listen = sock_no_listen,
  323. .shutdown = sock_no_shutdown,
  324. .getsockopt = sock_no_getsockopt,
  325. .mmap = sock_no_mmap,
  326. .bind = sock_no_bind,
  327. .accept = sock_no_accept,
  328. .setsockopt = sock_no_setsockopt,
  329. .release = af_alg_release,
  330. .sendmsg = aead_sendmsg,
  331. .sendpage = af_alg_sendpage,
  332. .recvmsg = aead_recvmsg,
  333. .poll = af_alg_poll,
  334. };
  335. static int aead_check_key(struct socket *sock)
  336. {
  337. int err = 0;
  338. struct sock *psk;
  339. struct alg_sock *pask;
  340. struct aead_tfm *tfm;
  341. struct sock *sk = sock->sk;
  342. struct alg_sock *ask = alg_sk(sk);
  343. lock_sock(sk);
  344. if (!atomic_read(&ask->nokey_refcnt))
  345. goto unlock_child;
  346. psk = ask->parent;
  347. pask = alg_sk(ask->parent);
  348. tfm = pask->private;
  349. err = -ENOKEY;
  350. lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
  351. if (crypto_aead_get_flags(tfm->aead) & CRYPTO_TFM_NEED_KEY)
  352. goto unlock;
  353. atomic_dec(&pask->nokey_refcnt);
  354. atomic_set(&ask->nokey_refcnt, 0);
  355. err = 0;
  356. unlock:
  357. release_sock(psk);
  358. unlock_child:
  359. release_sock(sk);
  360. return err;
  361. }
  362. static int aead_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
  363. size_t size)
  364. {
  365. int err;
  366. err = aead_check_key(sock);
  367. if (err)
  368. return err;
  369. return aead_sendmsg(sock, msg, size);
  370. }
  371. static ssize_t aead_sendpage_nokey(struct socket *sock, struct page *page,
  372. int offset, size_t size, int flags)
  373. {
  374. int err;
  375. err = aead_check_key(sock);
  376. if (err)
  377. return err;
  378. return af_alg_sendpage(sock, page, offset, size, flags);
  379. }
  380. static int aead_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
  381. size_t ignored, int flags)
  382. {
  383. int err;
  384. err = aead_check_key(sock);
  385. if (err)
  386. return err;
  387. return aead_recvmsg(sock, msg, ignored, flags);
  388. }
  389. static struct proto_ops algif_aead_ops_nokey = {
  390. .family = PF_ALG,
  391. .connect = sock_no_connect,
  392. .socketpair = sock_no_socketpair,
  393. .getname = sock_no_getname,
  394. .ioctl = sock_no_ioctl,
  395. .listen = sock_no_listen,
  396. .shutdown = sock_no_shutdown,
  397. .getsockopt = sock_no_getsockopt,
  398. .mmap = sock_no_mmap,
  399. .bind = sock_no_bind,
  400. .accept = sock_no_accept,
  401. .setsockopt = sock_no_setsockopt,
  402. .release = af_alg_release,
  403. .sendmsg = aead_sendmsg_nokey,
  404. .sendpage = aead_sendpage_nokey,
  405. .recvmsg = aead_recvmsg_nokey,
  406. .poll = af_alg_poll,
  407. };
  408. static void *aead_bind(const char *name, u32 type, u32 mask)
  409. {
  410. struct aead_tfm *tfm;
  411. struct crypto_aead *aead;
  412. struct crypto_skcipher *null_tfm;
  413. tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
  414. if (!tfm)
  415. return ERR_PTR(-ENOMEM);
  416. aead = crypto_alloc_aead(name, type, mask);
  417. if (IS_ERR(aead)) {
  418. kfree(tfm);
  419. return ERR_CAST(aead);
  420. }
  421. null_tfm = crypto_get_default_null_skcipher();
  422. if (IS_ERR(null_tfm)) {
  423. crypto_free_aead(aead);
  424. kfree(tfm);
  425. return ERR_CAST(null_tfm);
  426. }
  427. tfm->aead = aead;
  428. tfm->null_tfm = null_tfm;
  429. return tfm;
  430. }
  431. static void aead_release(void *private)
  432. {
  433. struct aead_tfm *tfm = private;
  434. crypto_free_aead(tfm->aead);
  435. crypto_put_default_null_skcipher();
  436. kfree(tfm);
  437. }
  438. static int aead_setauthsize(void *private, unsigned int authsize)
  439. {
  440. struct aead_tfm *tfm = private;
  441. return crypto_aead_setauthsize(tfm->aead, authsize);
  442. }
  443. static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
  444. {
  445. struct aead_tfm *tfm = private;
  446. return crypto_aead_setkey(tfm->aead, key, keylen);
  447. }
  448. static void aead_sock_destruct(struct sock *sk)
  449. {
  450. struct alg_sock *ask = alg_sk(sk);
  451. struct af_alg_ctx *ctx = ask->private;
  452. struct sock *psk = ask->parent;
  453. struct alg_sock *pask = alg_sk(psk);
  454. struct aead_tfm *aeadc = pask->private;
  455. struct crypto_aead *tfm = aeadc->aead;
  456. unsigned int ivlen = crypto_aead_ivsize(tfm);
  457. af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
  458. sock_kzfree_s(sk, ctx->iv, ivlen);
  459. sock_kfree_s(sk, ctx, ctx->len);
  460. af_alg_release_parent(sk);
  461. }
  462. static int aead_accept_parent_nokey(void *private, struct sock *sk)
  463. {
  464. struct af_alg_ctx *ctx;
  465. struct alg_sock *ask = alg_sk(sk);
  466. struct aead_tfm *tfm = private;
  467. struct crypto_aead *aead = tfm->aead;
  468. unsigned int len = sizeof(*ctx);
  469. unsigned int ivlen = crypto_aead_ivsize(aead);
  470. ctx = sock_kmalloc(sk, len, GFP_KERNEL);
  471. if (!ctx)
  472. return -ENOMEM;
  473. memset(ctx, 0, len);
  474. ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
  475. if (!ctx->iv) {
  476. sock_kfree_s(sk, ctx, len);
  477. return -ENOMEM;
  478. }
  479. memset(ctx->iv, 0, ivlen);
  480. INIT_LIST_HEAD(&ctx->tsgl_list);
  481. ctx->len = len;
  482. ctx->used = 0;
  483. atomic_set(&ctx->rcvused, 0);
  484. ctx->more = 0;
  485. ctx->merge = 0;
  486. ctx->enc = 0;
  487. ctx->aead_assoclen = 0;
  488. crypto_init_wait(&ctx->wait);
  489. ask->private = ctx;
  490. sk->sk_destruct = aead_sock_destruct;
  491. return 0;
  492. }
  493. static int aead_accept_parent(void *private, struct sock *sk)
  494. {
  495. struct aead_tfm *tfm = private;
  496. if (crypto_aead_get_flags(tfm->aead) & CRYPTO_TFM_NEED_KEY)
  497. return -ENOKEY;
  498. return aead_accept_parent_nokey(private, sk);
  499. }
  500. static const struct af_alg_type algif_type_aead = {
  501. .bind = aead_bind,
  502. .release = aead_release,
  503. .setkey = aead_setkey,
  504. .setauthsize = aead_setauthsize,
  505. .accept = aead_accept_parent,
  506. .accept_nokey = aead_accept_parent_nokey,
  507. .ops = &algif_aead_ops,
  508. .ops_nokey = &algif_aead_ops_nokey,
  509. .name = "aead",
  510. .owner = THIS_MODULE
  511. };
  512. static int __init algif_aead_init(void)
  513. {
  514. return af_alg_register_type(&algif_type_aead);
  515. }
  516. static void __exit algif_aead_exit(void)
  517. {
  518. int err = af_alg_unregister_type(&algif_type_aead);
  519. BUG_ON(err);
  520. }
  521. module_init(algif_aead_init);
  522. module_exit(algif_aead_exit);
  523. MODULE_LICENSE("GPL");
  524. MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
  525. MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");