algif_skcipher.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * algif_skcipher: User-space interface for skcipher algorithms
  4. *
  5. * This file provides the user-space API for symmetric key ciphers.
  6. *
  7. * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
  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 sendpage/sendmsg. Filling
  13. * up the TX SGL does not cause a crypto operation -- the data will only be
  14. * tracked by the kernel. Upon receipt of one recvmsg call, the caller must
  15. * 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/scatterwalk.h>
  26. #include <crypto/skcipher.h>
  27. #include <crypto/if_alg.h>
  28. #include <linux/init.h>
  29. #include <linux/list.h>
  30. #include <linux/kernel.h>
  31. #include <linux/mm.h>
  32. #include <linux/module.h>
  33. #include <linux/net.h>
  34. #include <net/sock.h>
  35. static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
  36. size_t size)
  37. {
  38. struct sock *sk = sock->sk;
  39. struct alg_sock *ask = alg_sk(sk);
  40. struct sock *psk = ask->parent;
  41. struct alg_sock *pask = alg_sk(psk);
  42. struct crypto_skcipher *tfm = pask->private;
  43. unsigned ivsize = crypto_skcipher_ivsize(tfm);
  44. return af_alg_sendmsg(sock, msg, size, ivsize);
  45. }
  46. static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
  47. size_t ignored, int flags)
  48. {
  49. struct sock *sk = sock->sk;
  50. struct alg_sock *ask = alg_sk(sk);
  51. struct sock *psk = ask->parent;
  52. struct alg_sock *pask = alg_sk(psk);
  53. struct af_alg_ctx *ctx = ask->private;
  54. struct crypto_skcipher *tfm = pask->private;
  55. unsigned int bs = crypto_skcipher_chunksize(tfm);
  56. struct af_alg_async_req *areq;
  57. int err = 0;
  58. size_t len = 0;
  59. if (!ctx->init || (ctx->more && ctx->used < bs)) {
  60. err = af_alg_wait_for_data(sk, flags, bs);
  61. if (err)
  62. return err;
  63. }
  64. /* Allocate cipher request for current operation. */
  65. areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) +
  66. crypto_skcipher_reqsize(tfm));
  67. if (IS_ERR(areq))
  68. return PTR_ERR(areq);
  69. /* convert iovecs of output buffers into RX SGL */
  70. err = af_alg_get_rsgl(sk, msg, flags, areq, ctx->used, &len);
  71. if (err)
  72. goto free;
  73. /*
  74. * If more buffers are to be expected to be processed, process only
  75. * full block size buffers.
  76. */
  77. if (ctx->more || len < ctx->used)
  78. len -= len % bs;
  79. /*
  80. * Create a per request TX SGL for this request which tracks the
  81. * SG entries from the global TX SGL.
  82. */
  83. areq->tsgl_entries = af_alg_count_tsgl(sk, len, 0);
  84. if (!areq->tsgl_entries)
  85. areq->tsgl_entries = 1;
  86. areq->tsgl = sock_kmalloc(sk, array_size(sizeof(*areq->tsgl),
  87. areq->tsgl_entries),
  88. GFP_KERNEL);
  89. if (!areq->tsgl) {
  90. err = -ENOMEM;
  91. goto free;
  92. }
  93. sg_init_table(areq->tsgl, areq->tsgl_entries);
  94. af_alg_pull_tsgl(sk, len, areq->tsgl, 0);
  95. /* Initialize the crypto operation */
  96. skcipher_request_set_tfm(&areq->cra_u.skcipher_req, tfm);
  97. skcipher_request_set_crypt(&areq->cra_u.skcipher_req, areq->tsgl,
  98. areq->first_rsgl.sgl.sg, len, ctx->iv);
  99. if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) {
  100. /* AIO operation */
  101. sock_hold(sk);
  102. areq->iocb = msg->msg_iocb;
  103. /* Remember output size that will be generated. */
  104. areq->outlen = len;
  105. skcipher_request_set_callback(&areq->cra_u.skcipher_req,
  106. CRYPTO_TFM_REQ_MAY_SLEEP,
  107. af_alg_async_cb, areq);
  108. err = ctx->enc ?
  109. crypto_skcipher_encrypt(&areq->cra_u.skcipher_req) :
  110. crypto_skcipher_decrypt(&areq->cra_u.skcipher_req);
  111. /* AIO operation in progress */
  112. if (err == -EINPROGRESS)
  113. return -EIOCBQUEUED;
  114. sock_put(sk);
  115. } else {
  116. /* Synchronous operation */
  117. skcipher_request_set_callback(&areq->cra_u.skcipher_req,
  118. CRYPTO_TFM_REQ_MAY_SLEEP |
  119. CRYPTO_TFM_REQ_MAY_BACKLOG,
  120. crypto_req_done, &ctx->wait);
  121. err = crypto_wait_req(ctx->enc ?
  122. crypto_skcipher_encrypt(&areq->cra_u.skcipher_req) :
  123. crypto_skcipher_decrypt(&areq->cra_u.skcipher_req),
  124. &ctx->wait);
  125. }
  126. free:
  127. af_alg_free_resources(areq);
  128. return err ? err : len;
  129. }
  130. static int skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
  131. size_t ignored, int flags)
  132. {
  133. struct sock *sk = sock->sk;
  134. int ret = 0;
  135. lock_sock(sk);
  136. while (msg_data_left(msg)) {
  137. int err = _skcipher_recvmsg(sock, msg, ignored, flags);
  138. /*
  139. * This error covers -EIOCBQUEUED which implies that we can
  140. * only handle one AIO request. If the caller wants to have
  141. * multiple AIO requests in parallel, he must make multiple
  142. * separate AIO calls.
  143. *
  144. * Also return the error if no data has been processed so far.
  145. */
  146. if (err <= 0) {
  147. if (err == -EIOCBQUEUED || !ret)
  148. ret = err;
  149. goto out;
  150. }
  151. ret += err;
  152. }
  153. out:
  154. af_alg_wmem_wakeup(sk);
  155. release_sock(sk);
  156. return ret;
  157. }
  158. static struct proto_ops algif_skcipher_ops = {
  159. .family = PF_ALG,
  160. .connect = sock_no_connect,
  161. .socketpair = sock_no_socketpair,
  162. .getname = sock_no_getname,
  163. .ioctl = sock_no_ioctl,
  164. .listen = sock_no_listen,
  165. .shutdown = sock_no_shutdown,
  166. .mmap = sock_no_mmap,
  167. .bind = sock_no_bind,
  168. .accept = sock_no_accept,
  169. .release = af_alg_release,
  170. .sendmsg = skcipher_sendmsg,
  171. .sendpage = af_alg_sendpage,
  172. .recvmsg = skcipher_recvmsg,
  173. .poll = af_alg_poll,
  174. };
  175. static int skcipher_check_key(struct socket *sock)
  176. {
  177. int err = 0;
  178. struct sock *psk;
  179. struct alg_sock *pask;
  180. struct crypto_skcipher *tfm;
  181. struct sock *sk = sock->sk;
  182. struct alg_sock *ask = alg_sk(sk);
  183. lock_sock(sk);
  184. if (!atomic_read(&ask->nokey_refcnt))
  185. goto unlock_child;
  186. psk = ask->parent;
  187. pask = alg_sk(ask->parent);
  188. tfm = pask->private;
  189. err = -ENOKEY;
  190. lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
  191. if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
  192. goto unlock;
  193. atomic_dec(&pask->nokey_refcnt);
  194. atomic_set(&ask->nokey_refcnt, 0);
  195. err = 0;
  196. unlock:
  197. release_sock(psk);
  198. unlock_child:
  199. release_sock(sk);
  200. return err;
  201. }
  202. static int skcipher_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
  203. size_t size)
  204. {
  205. int err;
  206. err = skcipher_check_key(sock);
  207. if (err)
  208. return err;
  209. return skcipher_sendmsg(sock, msg, size);
  210. }
  211. static ssize_t skcipher_sendpage_nokey(struct socket *sock, struct page *page,
  212. int offset, size_t size, int flags)
  213. {
  214. int err;
  215. err = skcipher_check_key(sock);
  216. if (err)
  217. return err;
  218. return af_alg_sendpage(sock, page, offset, size, flags);
  219. }
  220. static int skcipher_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
  221. size_t ignored, int flags)
  222. {
  223. int err;
  224. err = skcipher_check_key(sock);
  225. if (err)
  226. return err;
  227. return skcipher_recvmsg(sock, msg, ignored, flags);
  228. }
  229. static struct proto_ops algif_skcipher_ops_nokey = {
  230. .family = PF_ALG,
  231. .connect = sock_no_connect,
  232. .socketpair = sock_no_socketpair,
  233. .getname = sock_no_getname,
  234. .ioctl = sock_no_ioctl,
  235. .listen = sock_no_listen,
  236. .shutdown = sock_no_shutdown,
  237. .mmap = sock_no_mmap,
  238. .bind = sock_no_bind,
  239. .accept = sock_no_accept,
  240. .release = af_alg_release,
  241. .sendmsg = skcipher_sendmsg_nokey,
  242. .sendpage = skcipher_sendpage_nokey,
  243. .recvmsg = skcipher_recvmsg_nokey,
  244. .poll = af_alg_poll,
  245. };
  246. static void *skcipher_bind(const char *name, u32 type, u32 mask)
  247. {
  248. return crypto_alloc_skcipher(name, type, mask);
  249. }
  250. static void skcipher_release(void *private)
  251. {
  252. crypto_free_skcipher(private);
  253. }
  254. static int skcipher_setkey(void *private, const u8 *key, unsigned int keylen)
  255. {
  256. return crypto_skcipher_setkey(private, key, keylen);
  257. }
  258. static void skcipher_sock_destruct(struct sock *sk)
  259. {
  260. struct alg_sock *ask = alg_sk(sk);
  261. struct af_alg_ctx *ctx = ask->private;
  262. struct sock *psk = ask->parent;
  263. struct alg_sock *pask = alg_sk(psk);
  264. struct crypto_skcipher *tfm = pask->private;
  265. af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
  266. sock_kzfree_s(sk, ctx->iv, crypto_skcipher_ivsize(tfm));
  267. sock_kfree_s(sk, ctx, ctx->len);
  268. af_alg_release_parent(sk);
  269. }
  270. static int skcipher_accept_parent_nokey(void *private, struct sock *sk)
  271. {
  272. struct af_alg_ctx *ctx;
  273. struct alg_sock *ask = alg_sk(sk);
  274. struct crypto_skcipher *tfm = private;
  275. unsigned int len = sizeof(*ctx);
  276. ctx = sock_kmalloc(sk, len, GFP_KERNEL);
  277. if (!ctx)
  278. return -ENOMEM;
  279. memset(ctx, 0, len);
  280. ctx->iv = sock_kmalloc(sk, crypto_skcipher_ivsize(tfm),
  281. GFP_KERNEL);
  282. if (!ctx->iv) {
  283. sock_kfree_s(sk, ctx, len);
  284. return -ENOMEM;
  285. }
  286. memset(ctx->iv, 0, crypto_skcipher_ivsize(tfm));
  287. INIT_LIST_HEAD(&ctx->tsgl_list);
  288. ctx->len = len;
  289. crypto_init_wait(&ctx->wait);
  290. ask->private = ctx;
  291. sk->sk_destruct = skcipher_sock_destruct;
  292. return 0;
  293. }
  294. static int skcipher_accept_parent(void *private, struct sock *sk)
  295. {
  296. struct crypto_skcipher *tfm = private;
  297. if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
  298. return -ENOKEY;
  299. return skcipher_accept_parent_nokey(private, sk);
  300. }
  301. static const struct af_alg_type algif_type_skcipher = {
  302. .bind = skcipher_bind,
  303. .release = skcipher_release,
  304. .setkey = skcipher_setkey,
  305. .accept = skcipher_accept_parent,
  306. .accept_nokey = skcipher_accept_parent_nokey,
  307. .ops = &algif_skcipher_ops,
  308. .ops_nokey = &algif_skcipher_ops_nokey,
  309. .name = "skcipher",
  310. .owner = THIS_MODULE
  311. };
  312. static int __init algif_skcipher_init(void)
  313. {
  314. return af_alg_register_type(&algif_type_skcipher);
  315. }
  316. static void __exit algif_skcipher_exit(void)
  317. {
  318. int err = af_alg_unregister_type(&algif_type_skcipher);
  319. BUG_ON(err);
  320. }
  321. module_init(algif_skcipher_init);
  322. module_exit(algif_skcipher_exit);
  323. MODULE_LICENSE("GPL");