algif_skcipher.c 10 KB

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