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