algif_skcipher.c 11 KB

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