algif_hash.c 6.6 KB

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