algif_aead.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * algif_aead: User-space interface for AEAD algorithms
  4. *
  5. * Copyright (C) 2014, Stephan Mueller <smueller@chronox.de>
  6. *
  7. * This file provides the user-space API for AEAD ciphers.
  8. *
  9. * The following concept of the memory management is used:
  10. *
  11. * The kernel maintains two SGLs, the TX SGL and the RX SGL. The TX SGL is
  12. * filled by user space with the data submitted via sendmsg (maybe with
  13. * MSG_SPLICE_PAGES). Filling up the TX SGL does not cause a crypto operation
  14. * -- the data will only be tracked by the kernel. Upon receipt of one recvmsg
  15. * call, the caller must provide a buffer which is tracked with the RX SGL.
  16. *
  17. * During the processing of the recvmsg operation, the cipher request is
  18. * allocated and prepared. As part of the recvmsg operation, the processed
  19. * TX buffers are extracted from the TX SGL into a separate SGL.
  20. *
  21. * After the completion of the crypto operation, the RX SGL and the cipher
  22. * request is released. The extracted TX SGL parts are released together with
  23. * the RX SGL release.
  24. */
  25. #include <crypto/internal/aead.h>
  26. #include <crypto/scatterwalk.h>
  27. #include <crypto/if_alg.h>
  28. #include <crypto/skcipher.h>
  29. #include <crypto/null.h>
  30. #include <linux/init.h>
  31. #include <linux/list.h>
  32. #include <linux/kernel.h>
  33. #include <linux/mm.h>
  34. #include <linux/module.h>
  35. #include <linux/net.h>
  36. #include <net/sock.h>
  37. struct aead_tfm {
  38. struct crypto_aead *aead;
  39. struct crypto_sync_skcipher *null_tfm;
  40. };
  41. static inline bool aead_sufficient_data(struct sock *sk)
  42. {
  43. struct alg_sock *ask = alg_sk(sk);
  44. struct sock *psk = ask->parent;
  45. struct alg_sock *pask = alg_sk(psk);
  46. struct af_alg_ctx *ctx = ask->private;
  47. struct aead_tfm *aeadc = pask->private;
  48. struct crypto_aead *tfm = aeadc->aead;
  49. unsigned int as = crypto_aead_authsize(tfm);
  50. /*
  51. * The minimum amount of memory needed for an AEAD cipher is
  52. * the AAD and in case of decryption the tag.
  53. */
  54. return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
  55. }
  56. static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  57. {
  58. struct sock *sk = sock->sk;
  59. struct alg_sock *ask = alg_sk(sk);
  60. struct sock *psk = ask->parent;
  61. struct alg_sock *pask = alg_sk(psk);
  62. struct aead_tfm *aeadc = pask->private;
  63. struct crypto_aead *tfm = aeadc->aead;
  64. unsigned int ivsize = crypto_aead_ivsize(tfm);
  65. return af_alg_sendmsg(sock, msg, size, ivsize);
  66. }
  67. static int crypto_aead_copy_sgl(struct crypto_sync_skcipher *null_tfm,
  68. struct scatterlist *src,
  69. struct scatterlist *dst, unsigned int len)
  70. {
  71. SYNC_SKCIPHER_REQUEST_ON_STACK(skreq, null_tfm);
  72. skcipher_request_set_sync_tfm(skreq, null_tfm);
  73. skcipher_request_set_callback(skreq, CRYPTO_TFM_REQ_MAY_SLEEP,
  74. NULL, NULL);
  75. skcipher_request_set_crypt(skreq, src, dst, len, NULL);
  76. return crypto_skcipher_encrypt(skreq);
  77. }
  78. static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
  79. size_t ignored, int flags)
  80. {
  81. struct sock *sk = sock->sk;
  82. struct alg_sock *ask = alg_sk(sk);
  83. struct sock *psk = ask->parent;
  84. struct alg_sock *pask = alg_sk(psk);
  85. struct af_alg_ctx *ctx = ask->private;
  86. struct aead_tfm *aeadc = pask->private;
  87. struct crypto_aead *tfm = aeadc->aead;
  88. struct crypto_sync_skcipher *null_tfm = aeadc->null_tfm;
  89. unsigned int i, as = crypto_aead_authsize(tfm);
  90. struct af_alg_async_req *areq;
  91. struct af_alg_tsgl *tsgl, *tmp;
  92. struct scatterlist *rsgl_src, *tsgl_src = NULL;
  93. int err = 0;
  94. size_t used = 0; /* [in] TX bufs to be en/decrypted */
  95. size_t outlen = 0; /* [out] RX bufs produced by kernel */
  96. size_t usedpages = 0; /* [in] RX bufs to be used from user */
  97. size_t processed = 0; /* [in] TX bufs to be consumed */
  98. if (!ctx->init || ctx->more) {
  99. err = af_alg_wait_for_data(sk, flags, 0);
  100. if (err)
  101. return err;
  102. }
  103. /*
  104. * Data length provided by caller via sendmsg that has not yet been
  105. * processed.
  106. */
  107. used = ctx->used;
  108. /*
  109. * Make sure sufficient data is present -- note, the same check is also
  110. * present in sendmsg. The checks in sendmsg shall provide an
  111. * information to the data sender that something is wrong, but they are
  112. * irrelevant to maintain the kernel integrity. We need this check
  113. * here too in case user space decides to not honor the error message
  114. * in sendmsg and still call recvmsg. This check here protects the
  115. * 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.sgt.sgl;
  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.sgt.sgl,
  204. processed);
  205. if (err)
  206. goto free;
  207. af_alg_pull_tsgl(sk, processed, NULL, 0);
  208. } else {
  209. /*
  210. * Decryption operation - To achieve an in-place cipher
  211. * operation, the following SGL structure is used:
  212. *
  213. * TX SGL: AAD || CT || Tag
  214. * | | ^
  215. * | copy | | Create SGL link.
  216. * v v |
  217. * RX SGL: AAD || CT ----+
  218. */
  219. /* Copy AAD || CT to RX SGL buffer for in-place operation. */
  220. err = crypto_aead_copy_sgl(null_tfm, tsgl_src,
  221. areq->first_rsgl.sgl.sgt.sgl,
  222. outlen);
  223. if (err)
  224. goto free;
  225. /* Create TX SGL for tag and chain it to RX SGL. */
  226. areq->tsgl_entries = af_alg_count_tsgl(sk, processed,
  227. processed - as);
  228. if (!areq->tsgl_entries)
  229. areq->tsgl_entries = 1;
  230. areq->tsgl = sock_kmalloc(sk, array_size(sizeof(*areq->tsgl),
  231. areq->tsgl_entries),
  232. GFP_KERNEL);
  233. if (!areq->tsgl) {
  234. err = -ENOMEM;
  235. goto free;
  236. }
  237. sg_init_table(areq->tsgl, areq->tsgl_entries);
  238. /* Release TX SGL, except for tag data and reassign tag data. */
  239. af_alg_pull_tsgl(sk, processed, areq->tsgl, processed - as);
  240. /* chain the areq TX SGL holding the tag with RX SGL */
  241. if (usedpages) {
  242. /* RX SGL present */
  243. struct af_alg_sgl *sgl_prev = &areq->last_rsgl->sgl;
  244. struct scatterlist *sg = sgl_prev->sgt.sgl;
  245. sg_unmark_end(sg + sgl_prev->sgt.nents - 1);
  246. sg_chain(sg, sgl_prev->sgt.nents + 1, areq->tsgl);
  247. } else
  248. /* no RX SGL present (e.g. authentication only) */
  249. rsgl_src = areq->tsgl;
  250. }
  251. /* Initialize the crypto operation */
  252. aead_request_set_crypt(&areq->cra_u.aead_req, rsgl_src,
  253. areq->first_rsgl.sgl.sgt.sgl, used, ctx->iv);
  254. aead_request_set_ad(&areq->cra_u.aead_req, ctx->aead_assoclen);
  255. aead_request_set_tfm(&areq->cra_u.aead_req, tfm);
  256. if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) {
  257. /* AIO operation */
  258. sock_hold(sk);
  259. areq->iocb = msg->msg_iocb;
  260. /* Remember output size that will be generated. */
  261. areq->outlen = outlen;
  262. aead_request_set_callback(&areq->cra_u.aead_req,
  263. CRYPTO_TFM_REQ_MAY_SLEEP,
  264. af_alg_async_cb, areq);
  265. err = ctx->enc ? crypto_aead_encrypt(&areq->cra_u.aead_req) :
  266. crypto_aead_decrypt(&areq->cra_u.aead_req);
  267. /* AIO operation in progress */
  268. if (err == -EINPROGRESS)
  269. return -EIOCBQUEUED;
  270. sock_put(sk);
  271. } else {
  272. /* Synchronous operation */
  273. aead_request_set_callback(&areq->cra_u.aead_req,
  274. CRYPTO_TFM_REQ_MAY_SLEEP |
  275. CRYPTO_TFM_REQ_MAY_BACKLOG,
  276. crypto_req_done, &ctx->wait);
  277. err = crypto_wait_req(ctx->enc ?
  278. crypto_aead_encrypt(&areq->cra_u.aead_req) :
  279. crypto_aead_decrypt(&areq->cra_u.aead_req),
  280. &ctx->wait);
  281. }
  282. free:
  283. af_alg_free_resources(areq);
  284. return err ? err : outlen;
  285. }
  286. static int aead_recvmsg(struct socket *sock, struct msghdr *msg,
  287. size_t ignored, int flags)
  288. {
  289. struct sock *sk = sock->sk;
  290. int ret = 0;
  291. lock_sock(sk);
  292. while (msg_data_left(msg)) {
  293. int err = _aead_recvmsg(sock, msg, ignored, flags);
  294. /*
  295. * This error covers -EIOCBQUEUED which implies that we can
  296. * only handle one AIO request. If the caller wants to have
  297. * multiple AIO requests in parallel, he must make multiple
  298. * separate AIO calls.
  299. *
  300. * Also return the error if no data has been processed so far.
  301. */
  302. if (err <= 0) {
  303. if (err == -EIOCBQUEUED || err == -EBADMSG || !ret)
  304. ret = err;
  305. goto out;
  306. }
  307. ret += err;
  308. }
  309. out:
  310. af_alg_wmem_wakeup(sk);
  311. release_sock(sk);
  312. return ret;
  313. }
  314. static struct proto_ops algif_aead_ops = {
  315. .family = PF_ALG,
  316. .connect = sock_no_connect,
  317. .socketpair = sock_no_socketpair,
  318. .getname = sock_no_getname,
  319. .ioctl = sock_no_ioctl,
  320. .listen = sock_no_listen,
  321. .shutdown = sock_no_shutdown,
  322. .mmap = sock_no_mmap,
  323. .bind = sock_no_bind,
  324. .accept = sock_no_accept,
  325. .release = af_alg_release,
  326. .sendmsg = aead_sendmsg,
  327. .recvmsg = aead_recvmsg,
  328. .poll = af_alg_poll,
  329. };
  330. static int aead_check_key(struct socket *sock)
  331. {
  332. int err = 0;
  333. struct sock *psk;
  334. struct alg_sock *pask;
  335. struct aead_tfm *tfm;
  336. struct sock *sk = sock->sk;
  337. struct alg_sock *ask = alg_sk(sk);
  338. lock_sock(sk);
  339. if (!atomic_read(&ask->nokey_refcnt))
  340. goto unlock_child;
  341. psk = ask->parent;
  342. pask = alg_sk(ask->parent);
  343. tfm = pask->private;
  344. err = -ENOKEY;
  345. lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
  346. if (crypto_aead_get_flags(tfm->aead) & CRYPTO_TFM_NEED_KEY)
  347. goto unlock;
  348. atomic_dec(&pask->nokey_refcnt);
  349. atomic_set(&ask->nokey_refcnt, 0);
  350. err = 0;
  351. unlock:
  352. release_sock(psk);
  353. unlock_child:
  354. release_sock(sk);
  355. return err;
  356. }
  357. static int aead_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
  358. size_t size)
  359. {
  360. int err;
  361. err = aead_check_key(sock);
  362. if (err)
  363. return err;
  364. return aead_sendmsg(sock, msg, size);
  365. }
  366. static int aead_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
  367. size_t ignored, int flags)
  368. {
  369. int err;
  370. err = aead_check_key(sock);
  371. if (err)
  372. return err;
  373. return aead_recvmsg(sock, msg, ignored, flags);
  374. }
  375. static struct proto_ops algif_aead_ops_nokey = {
  376. .family = PF_ALG,
  377. .connect = sock_no_connect,
  378. .socketpair = sock_no_socketpair,
  379. .getname = sock_no_getname,
  380. .ioctl = sock_no_ioctl,
  381. .listen = sock_no_listen,
  382. .shutdown = sock_no_shutdown,
  383. .mmap = sock_no_mmap,
  384. .bind = sock_no_bind,
  385. .accept = sock_no_accept,
  386. .release = af_alg_release,
  387. .sendmsg = aead_sendmsg_nokey,
  388. .recvmsg = aead_recvmsg_nokey,
  389. .poll = af_alg_poll,
  390. };
  391. static void *aead_bind(const char *name, u32 type, u32 mask)
  392. {
  393. struct aead_tfm *tfm;
  394. struct crypto_aead *aead;
  395. struct crypto_sync_skcipher *null_tfm;
  396. tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
  397. if (!tfm)
  398. return ERR_PTR(-ENOMEM);
  399. aead = crypto_alloc_aead(name, type, mask);
  400. if (IS_ERR(aead)) {
  401. kfree(tfm);
  402. return ERR_CAST(aead);
  403. }
  404. null_tfm = crypto_get_default_null_skcipher();
  405. if (IS_ERR(null_tfm)) {
  406. crypto_free_aead(aead);
  407. kfree(tfm);
  408. return ERR_CAST(null_tfm);
  409. }
  410. tfm->aead = aead;
  411. tfm->null_tfm = null_tfm;
  412. return tfm;
  413. }
  414. static void aead_release(void *private)
  415. {
  416. struct aead_tfm *tfm = private;
  417. crypto_free_aead(tfm->aead);
  418. crypto_put_default_null_skcipher();
  419. kfree(tfm);
  420. }
  421. static int aead_setauthsize(void *private, unsigned int authsize)
  422. {
  423. struct aead_tfm *tfm = private;
  424. return crypto_aead_setauthsize(tfm->aead, authsize);
  425. }
  426. static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
  427. {
  428. struct aead_tfm *tfm = private;
  429. return crypto_aead_setkey(tfm->aead, key, keylen);
  430. }
  431. static void aead_sock_destruct(struct sock *sk)
  432. {
  433. struct alg_sock *ask = alg_sk(sk);
  434. struct af_alg_ctx *ctx = ask->private;
  435. struct sock *psk = ask->parent;
  436. struct alg_sock *pask = alg_sk(psk);
  437. struct aead_tfm *aeadc = pask->private;
  438. struct crypto_aead *tfm = aeadc->aead;
  439. unsigned int ivlen = crypto_aead_ivsize(tfm);
  440. af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
  441. sock_kzfree_s(sk, ctx->iv, ivlen);
  442. sock_kfree_s(sk, ctx, ctx->len);
  443. af_alg_release_parent(sk);
  444. }
  445. static int aead_accept_parent_nokey(void *private, struct sock *sk)
  446. {
  447. struct af_alg_ctx *ctx;
  448. struct alg_sock *ask = alg_sk(sk);
  449. struct aead_tfm *tfm = private;
  450. struct crypto_aead *aead = tfm->aead;
  451. unsigned int len = sizeof(*ctx);
  452. unsigned int ivlen = crypto_aead_ivsize(aead);
  453. ctx = sock_kmalloc(sk, len, GFP_KERNEL);
  454. if (!ctx)
  455. return -ENOMEM;
  456. memset(ctx, 0, len);
  457. ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
  458. if (!ctx->iv) {
  459. sock_kfree_s(sk, ctx, len);
  460. return -ENOMEM;
  461. }
  462. memset(ctx->iv, 0, ivlen);
  463. INIT_LIST_HEAD(&ctx->tsgl_list);
  464. ctx->len = len;
  465. crypto_init_wait(&ctx->wait);
  466. ask->private = ctx;
  467. sk->sk_destruct = aead_sock_destruct;
  468. return 0;
  469. }
  470. static int aead_accept_parent(void *private, struct sock *sk)
  471. {
  472. struct aead_tfm *tfm = private;
  473. if (crypto_aead_get_flags(tfm->aead) & CRYPTO_TFM_NEED_KEY)
  474. return -ENOKEY;
  475. return aead_accept_parent_nokey(private, sk);
  476. }
  477. static const struct af_alg_type algif_type_aead = {
  478. .bind = aead_bind,
  479. .release = aead_release,
  480. .setkey = aead_setkey,
  481. .setauthsize = aead_setauthsize,
  482. .accept = aead_accept_parent,
  483. .accept_nokey = aead_accept_parent_nokey,
  484. .ops = &algif_aead_ops,
  485. .ops_nokey = &algif_aead_ops_nokey,
  486. .name = "aead",
  487. .owner = THIS_MODULE
  488. };
  489. static int __init algif_aead_init(void)
  490. {
  491. return af_alg_register_type(&algif_type_aead);
  492. }
  493. static void __exit algif_aead_exit(void)
  494. {
  495. int err = af_alg_unregister_type(&algif_type_aead);
  496. BUG_ON(err);
  497. }
  498. module_init(algif_aead_init);
  499. module_exit(algif_aead_exit);
  500. MODULE_LICENSE("GPL");
  501. MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
  502. MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");