algif_hash.c 6.8 KB

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