algif_aead.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 as = crypto_aead_authsize(tfm);
  95. struct af_alg_async_req *areq;
  96. struct af_alg_tsgl *tsgl;
  97. struct scatterlist *src;
  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. tsgl = list_first_entry(&ctx->tsgl_list, struct af_alg_tsgl, list);
  163. /*
  164. * Copy of AAD from source to destination
  165. *
  166. * The AAD is copied to the destination buffer without change. Even
  167. * when user space uses an in-place cipher operation, the kernel
  168. * will copy the data as it does not see whether such in-place operation
  169. * is initiated.
  170. *
  171. * To ensure efficiency, the following implementation ensure that the
  172. * ciphers are invoked to perform a crypto operation in-place. This
  173. * is achieved by memory management specified as follows.
  174. */
  175. /* Use the RX SGL as source (and destination) for crypto op. */
  176. src = areq->first_rsgl.sgl.sg;
  177. if (ctx->enc) {
  178. /*
  179. * Encryption operation - The in-place cipher operation is
  180. * achieved by the following operation:
  181. *
  182. * TX SGL: AAD || PT
  183. * | |
  184. * | copy |
  185. * v v
  186. * RX SGL: AAD || PT || Tag
  187. */
  188. err = crypto_aead_copy_sgl(null_tfm, tsgl->sg,
  189. areq->first_rsgl.sgl.sg, processed);
  190. if (err)
  191. goto free;
  192. af_alg_pull_tsgl(sk, processed, NULL, 0);
  193. } else {
  194. /*
  195. * Decryption operation - To achieve an in-place cipher
  196. * operation, the following SGL structure is used:
  197. *
  198. * TX SGL: AAD || CT || Tag
  199. * | | ^
  200. * | copy | | Create SGL link.
  201. * v v |
  202. * RX SGL: AAD || CT ----+
  203. */
  204. /* Copy AAD || CT to RX SGL buffer for in-place operation. */
  205. err = crypto_aead_copy_sgl(null_tfm, tsgl->sg,
  206. areq->first_rsgl.sgl.sg, outlen);
  207. if (err)
  208. goto free;
  209. /* Create TX SGL for tag and chain it to RX SGL. */
  210. areq->tsgl_entries = af_alg_count_tsgl(sk, processed,
  211. processed - as);
  212. if (!areq->tsgl_entries)
  213. areq->tsgl_entries = 1;
  214. areq->tsgl = sock_kmalloc(sk, sizeof(*areq->tsgl) *
  215. areq->tsgl_entries,
  216. GFP_KERNEL);
  217. if (!areq->tsgl) {
  218. err = -ENOMEM;
  219. goto free;
  220. }
  221. sg_init_table(areq->tsgl, areq->tsgl_entries);
  222. /* Release TX SGL, except for tag data and reassign tag data. */
  223. af_alg_pull_tsgl(sk, processed, areq->tsgl, processed - as);
  224. /* chain the areq TX SGL holding the tag with RX SGL */
  225. if (usedpages) {
  226. /* RX SGL present */
  227. struct af_alg_sgl *sgl_prev = &areq->last_rsgl->sgl;
  228. sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1);
  229. sg_chain(sgl_prev->sg, sgl_prev->npages + 1,
  230. areq->tsgl);
  231. } else
  232. /* no RX SGL present (e.g. authentication only) */
  233. src = areq->tsgl;
  234. }
  235. /* Initialize the crypto operation */
  236. aead_request_set_crypt(&areq->cra_u.aead_req, src,
  237. areq->first_rsgl.sgl.sg, used, ctx->iv);
  238. aead_request_set_ad(&areq->cra_u.aead_req, ctx->aead_assoclen);
  239. aead_request_set_tfm(&areq->cra_u.aead_req, tfm);
  240. if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) {
  241. /* AIO operation */
  242. areq->iocb = msg->msg_iocb;
  243. aead_request_set_callback(&areq->cra_u.aead_req,
  244. CRYPTO_TFM_REQ_MAY_BACKLOG,
  245. af_alg_async_cb, areq);
  246. err = ctx->enc ? crypto_aead_encrypt(&areq->cra_u.aead_req) :
  247. crypto_aead_decrypt(&areq->cra_u.aead_req);
  248. } else {
  249. /* Synchronous operation */
  250. aead_request_set_callback(&areq->cra_u.aead_req,
  251. CRYPTO_TFM_REQ_MAY_BACKLOG,
  252. crypto_req_done, &ctx->wait);
  253. err = crypto_wait_req(ctx->enc ?
  254. crypto_aead_encrypt(&areq->cra_u.aead_req) :
  255. crypto_aead_decrypt(&areq->cra_u.aead_req),
  256. &ctx->wait);
  257. }
  258. /* AIO operation in progress */
  259. if (err == -EINPROGRESS) {
  260. sock_hold(sk);
  261. /* Remember output size that will be generated. */
  262. areq->outlen = outlen;
  263. return -EIOCBQUEUED;
  264. }
  265. free:
  266. af_alg_free_areq_sgls(areq);
  267. sock_kfree_s(sk, areq, areq->areqlen);
  268. return err ? err : outlen;
  269. }
  270. static int aead_recvmsg(struct socket *sock, struct msghdr *msg,
  271. size_t ignored, int flags)
  272. {
  273. struct sock *sk = sock->sk;
  274. int ret = 0;
  275. lock_sock(sk);
  276. while (msg_data_left(msg)) {
  277. int err = _aead_recvmsg(sock, msg, ignored, flags);
  278. /*
  279. * This error covers -EIOCBQUEUED which implies that we can
  280. * only handle one AIO request. If the caller wants to have
  281. * multiple AIO requests in parallel, he must make multiple
  282. * separate AIO calls.
  283. *
  284. * Also return the error if no data has been processed so far.
  285. */
  286. if (err <= 0) {
  287. if (err == -EIOCBQUEUED || err == -EBADMSG || !ret)
  288. ret = err;
  289. goto out;
  290. }
  291. ret += err;
  292. }
  293. out:
  294. af_alg_wmem_wakeup(sk);
  295. release_sock(sk);
  296. return ret;
  297. }
  298. static struct proto_ops algif_aead_ops = {
  299. .family = PF_ALG,
  300. .connect = sock_no_connect,
  301. .socketpair = sock_no_socketpair,
  302. .getname = sock_no_getname,
  303. .ioctl = sock_no_ioctl,
  304. .listen = sock_no_listen,
  305. .shutdown = sock_no_shutdown,
  306. .getsockopt = sock_no_getsockopt,
  307. .mmap = sock_no_mmap,
  308. .bind = sock_no_bind,
  309. .accept = sock_no_accept,
  310. .setsockopt = sock_no_setsockopt,
  311. .release = af_alg_release,
  312. .sendmsg = aead_sendmsg,
  313. .sendpage = af_alg_sendpage,
  314. .recvmsg = aead_recvmsg,
  315. .poll = af_alg_poll,
  316. };
  317. static int aead_check_key(struct socket *sock)
  318. {
  319. int err = 0;
  320. struct sock *psk;
  321. struct alg_sock *pask;
  322. struct aead_tfm *tfm;
  323. struct sock *sk = sock->sk;
  324. struct alg_sock *ask = alg_sk(sk);
  325. lock_sock(sk);
  326. if (ask->refcnt)
  327. goto unlock_child;
  328. psk = ask->parent;
  329. pask = alg_sk(ask->parent);
  330. tfm = pask->private;
  331. err = -ENOKEY;
  332. lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
  333. if (!tfm->has_key)
  334. goto unlock;
  335. if (!pask->refcnt++)
  336. sock_hold(psk);
  337. ask->refcnt = 1;
  338. sock_put(psk);
  339. err = 0;
  340. unlock:
  341. release_sock(psk);
  342. unlock_child:
  343. release_sock(sk);
  344. return err;
  345. }
  346. static int aead_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
  347. size_t size)
  348. {
  349. int err;
  350. err = aead_check_key(sock);
  351. if (err)
  352. return err;
  353. return aead_sendmsg(sock, msg, size);
  354. }
  355. static ssize_t aead_sendpage_nokey(struct socket *sock, struct page *page,
  356. int offset, size_t size, int flags)
  357. {
  358. int err;
  359. err = aead_check_key(sock);
  360. if (err)
  361. return err;
  362. return af_alg_sendpage(sock, page, offset, size, flags);
  363. }
  364. static int aead_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
  365. size_t ignored, int flags)
  366. {
  367. int err;
  368. err = aead_check_key(sock);
  369. if (err)
  370. return err;
  371. return aead_recvmsg(sock, msg, ignored, flags);
  372. }
  373. static struct proto_ops algif_aead_ops_nokey = {
  374. .family = PF_ALG,
  375. .connect = sock_no_connect,
  376. .socketpair = sock_no_socketpair,
  377. .getname = sock_no_getname,
  378. .ioctl = sock_no_ioctl,
  379. .listen = sock_no_listen,
  380. .shutdown = sock_no_shutdown,
  381. .getsockopt = sock_no_getsockopt,
  382. .mmap = sock_no_mmap,
  383. .bind = sock_no_bind,
  384. .accept = sock_no_accept,
  385. .setsockopt = sock_no_setsockopt,
  386. .release = af_alg_release,
  387. .sendmsg = aead_sendmsg_nokey,
  388. .sendpage = aead_sendpage_nokey,
  389. .recvmsg = aead_recvmsg_nokey,
  390. .poll = af_alg_poll,
  391. };
  392. static void *aead_bind(const char *name, u32 type, u32 mask)
  393. {
  394. struct aead_tfm *tfm;
  395. struct crypto_aead *aead;
  396. struct crypto_skcipher *null_tfm;
  397. tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
  398. if (!tfm)
  399. return ERR_PTR(-ENOMEM);
  400. aead = crypto_alloc_aead(name, type, mask);
  401. if (IS_ERR(aead)) {
  402. kfree(tfm);
  403. return ERR_CAST(aead);
  404. }
  405. null_tfm = crypto_get_default_null_skcipher2();
  406. if (IS_ERR(null_tfm)) {
  407. crypto_free_aead(aead);
  408. kfree(tfm);
  409. return ERR_CAST(null_tfm);
  410. }
  411. tfm->aead = aead;
  412. tfm->null_tfm = null_tfm;
  413. return tfm;
  414. }
  415. static void aead_release(void *private)
  416. {
  417. struct aead_tfm *tfm = private;
  418. crypto_free_aead(tfm->aead);
  419. crypto_put_default_null_skcipher2();
  420. kfree(tfm);
  421. }
  422. static int aead_setauthsize(void *private, unsigned int authsize)
  423. {
  424. struct aead_tfm *tfm = private;
  425. return crypto_aead_setauthsize(tfm->aead, authsize);
  426. }
  427. static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
  428. {
  429. struct aead_tfm *tfm = private;
  430. int err;
  431. err = crypto_aead_setkey(tfm->aead, key, keylen);
  432. tfm->has_key = !err;
  433. return err;
  434. }
  435. static void aead_sock_destruct(struct sock *sk)
  436. {
  437. struct alg_sock *ask = alg_sk(sk);
  438. struct af_alg_ctx *ctx = ask->private;
  439. struct sock *psk = ask->parent;
  440. struct alg_sock *pask = alg_sk(psk);
  441. struct aead_tfm *aeadc = pask->private;
  442. struct crypto_aead *tfm = aeadc->aead;
  443. unsigned int ivlen = crypto_aead_ivsize(tfm);
  444. af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
  445. sock_kzfree_s(sk, ctx->iv, ivlen);
  446. sock_kfree_s(sk, ctx, ctx->len);
  447. af_alg_release_parent(sk);
  448. }
  449. static int aead_accept_parent_nokey(void *private, struct sock *sk)
  450. {
  451. struct af_alg_ctx *ctx;
  452. struct alg_sock *ask = alg_sk(sk);
  453. struct aead_tfm *tfm = private;
  454. struct crypto_aead *aead = tfm->aead;
  455. unsigned int len = sizeof(*ctx);
  456. unsigned int ivlen = crypto_aead_ivsize(aead);
  457. ctx = sock_kmalloc(sk, len, GFP_KERNEL);
  458. if (!ctx)
  459. return -ENOMEM;
  460. memset(ctx, 0, len);
  461. ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
  462. if (!ctx->iv) {
  463. sock_kfree_s(sk, ctx, len);
  464. return -ENOMEM;
  465. }
  466. memset(ctx->iv, 0, ivlen);
  467. INIT_LIST_HEAD(&ctx->tsgl_list);
  468. ctx->len = len;
  469. ctx->used = 0;
  470. ctx->rcvused = 0;
  471. ctx->more = 0;
  472. ctx->merge = 0;
  473. ctx->enc = 0;
  474. ctx->aead_assoclen = 0;
  475. crypto_init_wait(&ctx->wait);
  476. ask->private = ctx;
  477. sk->sk_destruct = aead_sock_destruct;
  478. return 0;
  479. }
  480. static int aead_accept_parent(void *private, struct sock *sk)
  481. {
  482. struct aead_tfm *tfm = private;
  483. if (!tfm->has_key)
  484. return -ENOKEY;
  485. return aead_accept_parent_nokey(private, sk);
  486. }
  487. static const struct af_alg_type algif_type_aead = {
  488. .bind = aead_bind,
  489. .release = aead_release,
  490. .setkey = aead_setkey,
  491. .setauthsize = aead_setauthsize,
  492. .accept = aead_accept_parent,
  493. .accept_nokey = aead_accept_parent_nokey,
  494. .ops = &algif_aead_ops,
  495. .ops_nokey = &algif_aead_ops_nokey,
  496. .name = "aead",
  497. .owner = THIS_MODULE
  498. };
  499. static int __init algif_aead_init(void)
  500. {
  501. return af_alg_register_type(&algif_type_aead);
  502. }
  503. static void __exit algif_aead_exit(void)
  504. {
  505. int err = af_alg_unregister_type(&algif_type_aead);
  506. BUG_ON(err);
  507. }
  508. module_init(algif_aead_init);
  509. module_exit(algif_aead_exit);
  510. MODULE_LICENSE("GPL");
  511. MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
  512. MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");