algif_skcipher.c 10.0 KB

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