algif_aead.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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_SLEEP,
  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. if (!ctx->used) {
  104. err = af_alg_wait_for_data(sk, flags);
  105. if (err)
  106. return err;
  107. }
  108. /*
  109. * Data length provided by caller via sendmsg/sendpage that has not
  110. * yet been processed.
  111. */
  112. used = ctx->used;
  113. /*
  114. * Make sure sufficient data is present -- note, the same check is
  115. * is also present in sendmsg/sendpage. The checks in sendpage/sendmsg
  116. * shall provide an information to the data sender that something is
  117. * wrong, but they are irrelevant to maintain the kernel integrity.
  118. * We need this check here too in case user space decides to not honor
  119. * the error message in sendmsg/sendpage and still call recvmsg. This
  120. * check here protects the kernel integrity.
  121. */
  122. if (!aead_sufficient_data(sk))
  123. return -EINVAL;
  124. /*
  125. * Calculate the minimum output buffer size holding the result of the
  126. * cipher operation. When encrypting data, the receiving buffer is
  127. * larger by the tag length compared to the input buffer as the
  128. * encryption operation generates the tag. For decryption, the input
  129. * buffer provides the tag which is consumed resulting in only the
  130. * plaintext without a buffer for the tag returned to the caller.
  131. */
  132. if (ctx->enc)
  133. outlen = used + as;
  134. else
  135. outlen = used - as;
  136. /*
  137. * The cipher operation input data is reduced by the associated data
  138. * length as this data is processed separately later on.
  139. */
  140. used -= ctx->aead_assoclen;
  141. /* Allocate cipher request for current operation. */
  142. areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) +
  143. crypto_aead_reqsize(tfm));
  144. if (IS_ERR(areq))
  145. return PTR_ERR(areq);
  146. /* convert iovecs of output buffers into RX SGL */
  147. err = af_alg_get_rsgl(sk, msg, flags, areq, outlen, &usedpages);
  148. if (err)
  149. goto free;
  150. /*
  151. * Ensure output buffer is sufficiently large. If the caller provides
  152. * less buffer space, only use the relative required input size. This
  153. * allows AIO operation where the caller sent all data to be processed
  154. * and the AIO operation performs the operation on the different chunks
  155. * of the input data.
  156. */
  157. if (usedpages < outlen) {
  158. size_t less = outlen - usedpages;
  159. if (used < less) {
  160. err = -EINVAL;
  161. goto free;
  162. }
  163. used -= less;
  164. outlen -= less;
  165. }
  166. processed = used + ctx->aead_assoclen;
  167. list_for_each_entry_safe(tsgl, tmp, &ctx->tsgl_list, list) {
  168. for (i = 0; i < tsgl->cur; i++) {
  169. struct scatterlist *process_sg = tsgl->sg + i;
  170. if (!(process_sg->length) || !sg_page(process_sg))
  171. continue;
  172. tsgl_src = process_sg;
  173. break;
  174. }
  175. if (tsgl_src)
  176. break;
  177. }
  178. if (processed && !tsgl_src) {
  179. err = -EFAULT;
  180. goto free;
  181. }
  182. /*
  183. * Copy of AAD from source to destination
  184. *
  185. * The AAD is copied to the destination buffer without change. Even
  186. * when user space uses an in-place cipher operation, the kernel
  187. * will copy the data as it does not see whether such in-place operation
  188. * is initiated.
  189. *
  190. * To ensure efficiency, the following implementation ensure that the
  191. * ciphers are invoked to perform a crypto operation in-place. This
  192. * is achieved by memory management specified as follows.
  193. */
  194. /* Use the RX SGL as source (and destination) for crypto op. */
  195. rsgl_src = areq->first_rsgl.sgl.sg;
  196. if (ctx->enc) {
  197. /*
  198. * Encryption operation - The in-place cipher operation is
  199. * achieved by the following operation:
  200. *
  201. * TX SGL: AAD || PT
  202. * | |
  203. * | copy |
  204. * v v
  205. * RX SGL: AAD || PT || Tag
  206. */
  207. err = crypto_aead_copy_sgl(null_tfm, tsgl_src,
  208. areq->first_rsgl.sgl.sg, processed);
  209. if (err)
  210. goto free;
  211. af_alg_pull_tsgl(sk, processed, NULL, 0);
  212. } else {
  213. /*
  214. * Decryption operation - To achieve an in-place cipher
  215. * operation, the following SGL structure is used:
  216. *
  217. * TX SGL: AAD || CT || Tag
  218. * | | ^
  219. * | copy | | Create SGL link.
  220. * v v |
  221. * RX SGL: AAD || CT ----+
  222. */
  223. /* Copy AAD || CT to RX SGL buffer for in-place operation. */
  224. err = crypto_aead_copy_sgl(null_tfm, tsgl_src,
  225. areq->first_rsgl.sgl.sg, outlen);
  226. if (err)
  227. goto free;
  228. /* Create TX SGL for tag and chain it to RX SGL. */
  229. areq->tsgl_entries = af_alg_count_tsgl(sk, processed,
  230. processed - as);
  231. if (!areq->tsgl_entries)
  232. areq->tsgl_entries = 1;
  233. areq->tsgl = sock_kmalloc(sk, sizeof(*areq->tsgl) *
  234. areq->tsgl_entries,
  235. GFP_KERNEL);
  236. if (!areq->tsgl) {
  237. err = -ENOMEM;
  238. goto free;
  239. }
  240. sg_init_table(areq->tsgl, areq->tsgl_entries);
  241. /* Release TX SGL, except for tag data and reassign tag data. */
  242. af_alg_pull_tsgl(sk, processed, areq->tsgl, processed - as);
  243. /* chain the areq TX SGL holding the tag with RX SGL */
  244. if (usedpages) {
  245. /* RX SGL present */
  246. struct af_alg_sgl *sgl_prev = &areq->last_rsgl->sgl;
  247. sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1);
  248. sg_chain(sgl_prev->sg, sgl_prev->npages + 1,
  249. areq->tsgl);
  250. } else
  251. /* no RX SGL present (e.g. authentication only) */
  252. rsgl_src = areq->tsgl;
  253. }
  254. /* Initialize the crypto operation */
  255. aead_request_set_crypt(&areq->cra_u.aead_req, rsgl_src,
  256. areq->first_rsgl.sgl.sg, used, ctx->iv);
  257. aead_request_set_ad(&areq->cra_u.aead_req, ctx->aead_assoclen);
  258. aead_request_set_tfm(&areq->cra_u.aead_req, tfm);
  259. if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) {
  260. /* AIO operation */
  261. sock_hold(sk);
  262. areq->iocb = msg->msg_iocb;
  263. /* Remember output size that will be generated. */
  264. areq->outlen = outlen;
  265. aead_request_set_callback(&areq->cra_u.aead_req,
  266. CRYPTO_TFM_REQ_MAY_SLEEP,
  267. af_alg_async_cb, areq);
  268. err = ctx->enc ? crypto_aead_encrypt(&areq->cra_u.aead_req) :
  269. crypto_aead_decrypt(&areq->cra_u.aead_req);
  270. /* AIO operation in progress */
  271. if (err == -EINPROGRESS)
  272. return -EIOCBQUEUED;
  273. sock_put(sk);
  274. } else {
  275. /* Synchronous operation */
  276. aead_request_set_callback(&areq->cra_u.aead_req,
  277. CRYPTO_TFM_REQ_MAY_SLEEP |
  278. CRYPTO_TFM_REQ_MAY_BACKLOG,
  279. af_alg_complete, &ctx->completion);
  280. err = af_alg_wait_for_completion(ctx->enc ?
  281. crypto_aead_encrypt(&areq->cra_u.aead_req) :
  282. crypto_aead_decrypt(&areq->cra_u.aead_req),
  283. &ctx->completion);
  284. }
  285. free:
  286. af_alg_free_resources(areq);
  287. return err ? err : outlen;
  288. }
  289. static int aead_recvmsg(struct socket *sock, struct msghdr *msg,
  290. size_t ignored, int flags)
  291. {
  292. struct sock *sk = sock->sk;
  293. int ret = 0;
  294. lock_sock(sk);
  295. while (msg_data_left(msg)) {
  296. int err = _aead_recvmsg(sock, msg, ignored, flags);
  297. /*
  298. * This error covers -EIOCBQUEUED which implies that we can
  299. * only handle one AIO request. If the caller wants to have
  300. * multiple AIO requests in parallel, he must make multiple
  301. * separate AIO calls.
  302. *
  303. * Also return the error if no data has been processed so far.
  304. */
  305. if (err <= 0) {
  306. if (err == -EIOCBQUEUED || err == -EBADMSG || !ret)
  307. ret = err;
  308. goto out;
  309. }
  310. ret += err;
  311. }
  312. out:
  313. af_alg_wmem_wakeup(sk);
  314. release_sock(sk);
  315. return ret;
  316. }
  317. static struct proto_ops algif_aead_ops = {
  318. .family = PF_ALG,
  319. .connect = sock_no_connect,
  320. .socketpair = sock_no_socketpair,
  321. .getname = sock_no_getname,
  322. .ioctl = sock_no_ioctl,
  323. .listen = sock_no_listen,
  324. .shutdown = sock_no_shutdown,
  325. .getsockopt = sock_no_getsockopt,
  326. .mmap = sock_no_mmap,
  327. .bind = sock_no_bind,
  328. .accept = sock_no_accept,
  329. .setsockopt = sock_no_setsockopt,
  330. .release = af_alg_release,
  331. .sendmsg = aead_sendmsg,
  332. .sendpage = af_alg_sendpage,
  333. .recvmsg = aead_recvmsg,
  334. .poll = af_alg_poll,
  335. };
  336. static int aead_check_key(struct socket *sock)
  337. {
  338. int err = 0;
  339. struct sock *psk;
  340. struct alg_sock *pask;
  341. struct aead_tfm *tfm;
  342. struct sock *sk = sock->sk;
  343. struct alg_sock *ask = alg_sk(sk);
  344. lock_sock(sk);
  345. if (!atomic_read(&ask->nokey_refcnt))
  346. goto unlock_child;
  347. psk = ask->parent;
  348. pask = alg_sk(ask->parent);
  349. tfm = pask->private;
  350. err = -ENOKEY;
  351. lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
  352. if (!tfm->has_key)
  353. goto unlock;
  354. atomic_dec(&pask->nokey_refcnt);
  355. atomic_set(&ask->nokey_refcnt, 0);
  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_skcipher2();
  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_skcipher2();
  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. int err;
  448. err = crypto_aead_setkey(tfm->aead, key, keylen);
  449. tfm->has_key = !err;
  450. return err;
  451. }
  452. static void aead_sock_destruct(struct sock *sk)
  453. {
  454. struct alg_sock *ask = alg_sk(sk);
  455. struct af_alg_ctx *ctx = ask->private;
  456. struct sock *psk = ask->parent;
  457. struct alg_sock *pask = alg_sk(psk);
  458. struct aead_tfm *aeadc = pask->private;
  459. struct crypto_aead *tfm = aeadc->aead;
  460. unsigned int ivlen = crypto_aead_ivsize(tfm);
  461. af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
  462. sock_kzfree_s(sk, ctx->iv, ivlen);
  463. sock_kfree_s(sk, ctx, ctx->len);
  464. af_alg_release_parent(sk);
  465. }
  466. static int aead_accept_parent_nokey(void *private, struct sock *sk)
  467. {
  468. struct af_alg_ctx *ctx;
  469. struct alg_sock *ask = alg_sk(sk);
  470. struct aead_tfm *tfm = private;
  471. struct crypto_aead *aead = tfm->aead;
  472. unsigned int len = sizeof(*ctx);
  473. unsigned int ivlen = crypto_aead_ivsize(aead);
  474. ctx = sock_kmalloc(sk, len, GFP_KERNEL);
  475. if (!ctx)
  476. return -ENOMEM;
  477. memset(ctx, 0, len);
  478. ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
  479. if (!ctx->iv) {
  480. sock_kfree_s(sk, ctx, len);
  481. return -ENOMEM;
  482. }
  483. memset(ctx->iv, 0, ivlen);
  484. INIT_LIST_HEAD(&ctx->tsgl_list);
  485. ctx->len = len;
  486. ctx->used = 0;
  487. atomic_set(&ctx->rcvused, 0);
  488. ctx->more = 0;
  489. ctx->merge = 0;
  490. ctx->enc = 0;
  491. ctx->aead_assoclen = 0;
  492. af_alg_init_completion(&ctx->completion);
  493. ask->private = ctx;
  494. sk->sk_destruct = aead_sock_destruct;
  495. return 0;
  496. }
  497. static int aead_accept_parent(void *private, struct sock *sk)
  498. {
  499. struct aead_tfm *tfm = private;
  500. if (!tfm->has_key)
  501. return -ENOKEY;
  502. return aead_accept_parent_nokey(private, sk);
  503. }
  504. static const struct af_alg_type algif_type_aead = {
  505. .bind = aead_bind,
  506. .release = aead_release,
  507. .setkey = aead_setkey,
  508. .setauthsize = aead_setauthsize,
  509. .accept = aead_accept_parent,
  510. .accept_nokey = aead_accept_parent_nokey,
  511. .ops = &algif_aead_ops,
  512. .ops_nokey = &algif_aead_ops_nokey,
  513. .name = "aead",
  514. .owner = THIS_MODULE
  515. };
  516. static int __init algif_aead_init(void)
  517. {
  518. return af_alg_register_type(&algif_type_aead);
  519. }
  520. static void __exit algif_aead_exit(void)
  521. {
  522. int err = af_alg_unregister_type(&algif_type_aead);
  523. BUG_ON(err);
  524. }
  525. module_init(algif_aead_init);
  526. module_exit(algif_aead_exit);
  527. MODULE_LICENSE("GPL");
  528. MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
  529. MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");