algif_skcipher.c 10 KB

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