algif_hash.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. goto unlock;
  60. ahash_request_set_crypt(&ctx->req, ctx->sgl.sg, NULL,
  61. newlen);
  62. err = af_alg_wait_for_completion(
  63. crypto_ahash_update(&ctx->req),
  64. &ctx->completion);
  65. af_alg_free_sg(&ctx->sgl);
  66. if (err)
  67. goto unlock;
  68. seglen -= newlen;
  69. from += newlen;
  70. copied += newlen;
  71. }
  72. }
  73. err = 0;
  74. ctx->more = msg->msg_flags & MSG_MORE;
  75. if (!ctx->more) {
  76. ahash_request_set_crypt(&ctx->req, NULL, ctx->result, 0);
  77. err = af_alg_wait_for_completion(crypto_ahash_final(&ctx->req),
  78. &ctx->completion);
  79. }
  80. unlock:
  81. release_sock(sk);
  82. return err ?: copied;
  83. }
  84. static ssize_t hash_sendpage(struct socket *sock, struct page *page,
  85. int offset, size_t size, int flags)
  86. {
  87. struct sock *sk = sock->sk;
  88. struct alg_sock *ask = alg_sk(sk);
  89. struct hash_ctx *ctx = ask->private;
  90. int err;
  91. lock_sock(sk);
  92. sg_init_table(ctx->sgl.sg, 1);
  93. sg_set_page(ctx->sgl.sg, page, size, offset);
  94. ahash_request_set_crypt(&ctx->req, ctx->sgl.sg, ctx->result, size);
  95. if (!(flags & MSG_MORE)) {
  96. if (ctx->more)
  97. err = crypto_ahash_finup(&ctx->req);
  98. else
  99. err = crypto_ahash_digest(&ctx->req);
  100. } else {
  101. if (!ctx->more) {
  102. err = crypto_ahash_init(&ctx->req);
  103. if (err)
  104. goto unlock;
  105. }
  106. err = crypto_ahash_update(&ctx->req);
  107. }
  108. err = af_alg_wait_for_completion(err, &ctx->completion);
  109. if (err)
  110. goto unlock;
  111. ctx->more = flags & MSG_MORE;
  112. unlock:
  113. release_sock(sk);
  114. return err ?: size;
  115. }
  116. static int hash_recvmsg(struct kiocb *unused, struct socket *sock,
  117. struct msghdr *msg, size_t len, int flags)
  118. {
  119. struct sock *sk = sock->sk;
  120. struct alg_sock *ask = alg_sk(sk);
  121. struct hash_ctx *ctx = ask->private;
  122. unsigned ds = crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req));
  123. int err;
  124. if (len > ds)
  125. len = ds;
  126. else if (len < ds)
  127. msg->msg_flags |= MSG_TRUNC;
  128. lock_sock(sk);
  129. if (ctx->more) {
  130. ctx->more = 0;
  131. ahash_request_set_crypt(&ctx->req, NULL, ctx->result, 0);
  132. err = af_alg_wait_for_completion(crypto_ahash_final(&ctx->req),
  133. &ctx->completion);
  134. if (err)
  135. goto unlock;
  136. }
  137. err = memcpy_toiovec(msg->msg_iov, ctx->result, len);
  138. unlock:
  139. release_sock(sk);
  140. return err ?: len;
  141. }
  142. static int hash_accept(struct socket *sock, struct socket *newsock, int flags)
  143. {
  144. struct sock *sk = sock->sk;
  145. struct alg_sock *ask = alg_sk(sk);
  146. struct hash_ctx *ctx = ask->private;
  147. struct ahash_request *req = &ctx->req;
  148. char state[crypto_ahash_statesize(crypto_ahash_reqtfm(req))];
  149. struct sock *sk2;
  150. struct alg_sock *ask2;
  151. struct hash_ctx *ctx2;
  152. int err;
  153. err = crypto_ahash_export(req, state);
  154. if (err)
  155. return err;
  156. err = af_alg_accept(ask->parent, newsock);
  157. if (err)
  158. return err;
  159. sk2 = newsock->sk;
  160. ask2 = alg_sk(sk2);
  161. ctx2 = ask2->private;
  162. ctx2->more = 1;
  163. err = crypto_ahash_import(&ctx2->req, state);
  164. if (err) {
  165. sock_orphan(sk2);
  166. sock_put(sk2);
  167. }
  168. return err;
  169. }
  170. static struct proto_ops algif_hash_ops = {
  171. .family = PF_ALG,
  172. .connect = sock_no_connect,
  173. .socketpair = sock_no_socketpair,
  174. .getname = sock_no_getname,
  175. .ioctl = sock_no_ioctl,
  176. .listen = sock_no_listen,
  177. .shutdown = sock_no_shutdown,
  178. .getsockopt = sock_no_getsockopt,
  179. .mmap = sock_no_mmap,
  180. .bind = sock_no_bind,
  181. .setsockopt = sock_no_setsockopt,
  182. .poll = sock_no_poll,
  183. .release = af_alg_release,
  184. .sendmsg = hash_sendmsg,
  185. .sendpage = hash_sendpage,
  186. .recvmsg = hash_recvmsg,
  187. .accept = hash_accept,
  188. };
  189. static void *hash_bind(const char *name, u32 type, u32 mask)
  190. {
  191. return crypto_alloc_ahash(name, type, mask);
  192. }
  193. static void hash_release(void *private)
  194. {
  195. crypto_free_ahash(private);
  196. }
  197. static int hash_setkey(void *private, const u8 *key, unsigned int keylen)
  198. {
  199. return crypto_ahash_setkey(private, key, keylen);
  200. }
  201. static void hash_sock_destruct(struct sock *sk)
  202. {
  203. struct alg_sock *ask = alg_sk(sk);
  204. struct hash_ctx *ctx = ask->private;
  205. sock_kfree_s(sk, ctx->result,
  206. crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req)));
  207. sock_kfree_s(sk, ctx, ctx->len);
  208. af_alg_release_parent(sk);
  209. }
  210. static int hash_accept_parent(void *private, struct sock *sk)
  211. {
  212. struct hash_ctx *ctx;
  213. struct alg_sock *ask = alg_sk(sk);
  214. unsigned len = sizeof(*ctx) + crypto_ahash_reqsize(private);
  215. unsigned ds = crypto_ahash_digestsize(private);
  216. ctx = sock_kmalloc(sk, len, GFP_KERNEL);
  217. if (!ctx)
  218. return -ENOMEM;
  219. ctx->result = sock_kmalloc(sk, ds, GFP_KERNEL);
  220. if (!ctx->result) {
  221. sock_kfree_s(sk, ctx, len);
  222. return -ENOMEM;
  223. }
  224. memset(ctx->result, 0, ds);
  225. ctx->len = len;
  226. ctx->more = 0;
  227. af_alg_init_completion(&ctx->completion);
  228. ask->private = ctx;
  229. ahash_request_set_tfm(&ctx->req, private);
  230. ahash_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  231. af_alg_complete, &ctx->completion);
  232. sk->sk_destruct = hash_sock_destruct;
  233. return 0;
  234. }
  235. static const struct af_alg_type algif_type_hash = {
  236. .bind = hash_bind,
  237. .release = hash_release,
  238. .setkey = hash_setkey,
  239. .accept = hash_accept_parent,
  240. .ops = &algif_hash_ops,
  241. .name = "hash",
  242. .owner = THIS_MODULE
  243. };
  244. static int __init algif_hash_init(void)
  245. {
  246. return af_alg_register_type(&algif_type_hash);
  247. }
  248. static void __exit algif_hash_exit(void)
  249. {
  250. int err = af_alg_unregister_type(&algif_type_hash);
  251. BUG_ON(err);
  252. }
  253. module_init(algif_hash_init);
  254. module_exit(algif_hash_exit);
  255. MODULE_LICENSE("GPL");