algif_hash.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * algif_hash: User-space interface for hash algorithms
  3. *
  4. * This file provides the user-space API for hash algorithms.
  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. */
  14. #include <crypto/hash.h>
  15. #include <crypto/if_alg.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mm.h>
  19. #include <linux/module.h>
  20. #include <linux/net.h>
  21. #include <net/sock.h>
  22. struct hash_ctx {
  23. struct af_alg_sgl sgl;
  24. u8 *result;
  25. struct af_alg_completion completion;
  26. unsigned int len;
  27. bool more;
  28. struct ahash_request req;
  29. };
  30. static int hash_sendmsg(struct kiocb *unused, struct socket *sock,
  31. struct msghdr *msg, size_t ignored)
  32. {
  33. int limit = ALG_MAX_PAGES * PAGE_SIZE;
  34. struct sock *sk = sock->sk;
  35. struct alg_sock *ask = alg_sk(sk);
  36. struct hash_ctx *ctx = ask->private;
  37. unsigned long iovlen;
  38. struct iovec *iov;
  39. long copied = 0;
  40. int err;
  41. if (limit > sk->sk_sndbuf)
  42. limit = sk->sk_sndbuf;
  43. lock_sock(sk);
  44. if (!ctx->more) {
  45. err = crypto_ahash_init(&ctx->req);
  46. if (err)
  47. goto unlock;
  48. }
  49. ctx->more = 0;
  50. for (iov = msg->msg_iov, iovlen = msg->msg_iovlen; iovlen > 0;
  51. iovlen--, iov++) {
  52. unsigned long seglen = iov->iov_len;
  53. char __user *from = iov->iov_base;
  54. while (seglen) {
  55. int len = min_t(unsigned long, seglen, limit);
  56. int newlen;
  57. newlen = af_alg_make_sg(&ctx->sgl, from, len, 0);
  58. if (newlen < 0) {
  59. err = copied ? 0 : newlen;
  60. goto unlock;
  61. }
  62. ahash_request_set_crypt(&ctx->req, ctx->sgl.sg, NULL,
  63. newlen);
  64. err = af_alg_wait_for_completion(
  65. crypto_ahash_update(&ctx->req),
  66. &ctx->completion);
  67. af_alg_free_sg(&ctx->sgl);
  68. if (err)
  69. goto unlock;
  70. seglen -= newlen;
  71. from += newlen;
  72. copied += newlen;
  73. }
  74. }
  75. err = 0;
  76. ctx->more = msg->msg_flags & MSG_MORE;
  77. if (!ctx->more) {
  78. ahash_request_set_crypt(&ctx->req, NULL, ctx->result, 0);
  79. err = af_alg_wait_for_completion(crypto_ahash_final(&ctx->req),
  80. &ctx->completion);
  81. }
  82. unlock:
  83. release_sock(sk);
  84. return err ?: copied;
  85. }
  86. static ssize_t hash_sendpage(struct socket *sock, struct page *page,
  87. int offset, size_t size, int flags)
  88. {
  89. struct sock *sk = sock->sk;
  90. struct alg_sock *ask = alg_sk(sk);
  91. struct hash_ctx *ctx = ask->private;
  92. int err;
  93. if (flags & MSG_SENDPAGE_NOTLAST)
  94. flags |= MSG_MORE;
  95. lock_sock(sk);
  96. sg_init_table(ctx->sgl.sg, 1);
  97. sg_set_page(ctx->sgl.sg, page, size, offset);
  98. ahash_request_set_crypt(&ctx->req, ctx->sgl.sg, ctx->result, size);
  99. if (!(flags & MSG_MORE)) {
  100. if (ctx->more)
  101. err = crypto_ahash_finup(&ctx->req);
  102. else
  103. err = crypto_ahash_digest(&ctx->req);
  104. } else {
  105. if (!ctx->more) {
  106. err = crypto_ahash_init(&ctx->req);
  107. if (err)
  108. goto unlock;
  109. }
  110. err = crypto_ahash_update(&ctx->req);
  111. }
  112. err = af_alg_wait_for_completion(err, &ctx->completion);
  113. if (err)
  114. goto unlock;
  115. ctx->more = flags & MSG_MORE;
  116. unlock:
  117. release_sock(sk);
  118. return err ?: size;
  119. }
  120. static int hash_recvmsg(struct kiocb *unused, struct socket *sock,
  121. struct msghdr *msg, size_t len, int flags)
  122. {
  123. struct sock *sk = sock->sk;
  124. struct alg_sock *ask = alg_sk(sk);
  125. struct hash_ctx *ctx = ask->private;
  126. unsigned ds = crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req));
  127. int err;
  128. if (len > ds)
  129. len = ds;
  130. else if (len < ds)
  131. msg->msg_flags |= MSG_TRUNC;
  132. msg->msg_namelen = 0;
  133. lock_sock(sk);
  134. if (ctx->more) {
  135. ctx->more = 0;
  136. ahash_request_set_crypt(&ctx->req, NULL, ctx->result, 0);
  137. err = af_alg_wait_for_completion(crypto_ahash_final(&ctx->req),
  138. &ctx->completion);
  139. if (err)
  140. goto unlock;
  141. }
  142. err = memcpy_toiovec(msg->msg_iov, ctx->result, len);
  143. unlock:
  144. release_sock(sk);
  145. return err ?: len;
  146. }
  147. static int hash_accept(struct socket *sock, struct socket *newsock, int flags)
  148. {
  149. struct sock *sk = sock->sk;
  150. struct alg_sock *ask = alg_sk(sk);
  151. struct hash_ctx *ctx = ask->private;
  152. struct ahash_request *req = &ctx->req;
  153. char state[crypto_ahash_statesize(crypto_ahash_reqtfm(req))];
  154. struct sock *sk2;
  155. struct alg_sock *ask2;
  156. struct hash_ctx *ctx2;
  157. bool more;
  158. int err;
  159. lock_sock(sk);
  160. more = ctx->more;
  161. err = more ? crypto_ahash_export(req, state) : 0;
  162. release_sock(sk);
  163. if (err)
  164. return err;
  165. err = af_alg_accept(ask->parent, newsock);
  166. if (err)
  167. return err;
  168. sk2 = newsock->sk;
  169. ask2 = alg_sk(sk2);
  170. ctx2 = ask2->private;
  171. ctx2->more = more;
  172. if (!more)
  173. return err;
  174. err = crypto_ahash_import(&ctx2->req, state);
  175. if (err) {
  176. sock_orphan(sk2);
  177. sock_put(sk2);
  178. }
  179. return err;
  180. }
  181. static struct proto_ops algif_hash_ops = {
  182. .family = PF_ALG,
  183. .connect = sock_no_connect,
  184. .socketpair = sock_no_socketpair,
  185. .getname = sock_no_getname,
  186. .ioctl = sock_no_ioctl,
  187. .listen = sock_no_listen,
  188. .shutdown = sock_no_shutdown,
  189. .getsockopt = sock_no_getsockopt,
  190. .mmap = sock_no_mmap,
  191. .bind = sock_no_bind,
  192. .setsockopt = sock_no_setsockopt,
  193. .poll = sock_no_poll,
  194. .release = af_alg_release,
  195. .sendmsg = hash_sendmsg,
  196. .sendpage = hash_sendpage,
  197. .recvmsg = hash_recvmsg,
  198. .accept = hash_accept,
  199. };
  200. static void *hash_bind(const char *name, u32 type, u32 mask)
  201. {
  202. return crypto_alloc_ahash(name, type, mask);
  203. }
  204. static void hash_release(void *private)
  205. {
  206. crypto_free_ahash(private);
  207. }
  208. static int hash_setkey(void *private, const u8 *key, unsigned int keylen)
  209. {
  210. return crypto_ahash_setkey(private, key, keylen);
  211. }
  212. static void hash_sock_destruct(struct sock *sk)
  213. {
  214. struct alg_sock *ask = alg_sk(sk);
  215. struct hash_ctx *ctx = ask->private;
  216. sock_kfree_s(sk, ctx->result,
  217. crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req)));
  218. sock_kfree_s(sk, ctx, ctx->len);
  219. af_alg_release_parent(sk);
  220. }
  221. static int hash_accept_parent(void *private, struct sock *sk)
  222. {
  223. struct hash_ctx *ctx;
  224. struct alg_sock *ask = alg_sk(sk);
  225. unsigned len = sizeof(*ctx) + crypto_ahash_reqsize(private);
  226. unsigned ds = crypto_ahash_digestsize(private);
  227. ctx = sock_kmalloc(sk, len, GFP_KERNEL);
  228. if (!ctx)
  229. return -ENOMEM;
  230. ctx->result = sock_kmalloc(sk, ds, GFP_KERNEL);
  231. if (!ctx->result) {
  232. sock_kfree_s(sk, ctx, len);
  233. return -ENOMEM;
  234. }
  235. memset(ctx->result, 0, ds);
  236. ctx->len = len;
  237. ctx->more = 0;
  238. af_alg_init_completion(&ctx->completion);
  239. ask->private = ctx;
  240. ahash_request_set_tfm(&ctx->req, private);
  241. ahash_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  242. af_alg_complete, &ctx->completion);
  243. sk->sk_destruct = hash_sock_destruct;
  244. return 0;
  245. }
  246. static const struct af_alg_type algif_type_hash = {
  247. .bind = hash_bind,
  248. .release = hash_release,
  249. .setkey = hash_setkey,
  250. .accept = hash_accept_parent,
  251. .ops = &algif_hash_ops,
  252. .name = "hash",
  253. .owner = THIS_MODULE
  254. };
  255. static int __init algif_hash_init(void)
  256. {
  257. return af_alg_register_type(&algif_type_hash);
  258. }
  259. static void __exit algif_hash_exit(void)
  260. {
  261. int err = af_alg_unregister_type(&algif_type_hash);
  262. BUG_ON(err);
  263. }
  264. module_init(algif_hash_init);
  265. module_exit(algif_hash_exit);
  266. MODULE_LICENSE("GPL");