algif_hash.c 6.7 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 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. bool more;
  148. int err;
  149. lock_sock(sk);
  150. more = ctx->more;
  151. err = more ? crypto_ahash_export(req, state) : 0;
  152. release_sock(sk);
  153. if (err)
  154. return err;
  155. err = af_alg_accept(ask->parent, newsock);
  156. if (err)
  157. return err;
  158. sk2 = newsock->sk;
  159. ask2 = alg_sk(sk2);
  160. ctx2 = ask2->private;
  161. ctx2->more = more;
  162. if (!more)
  163. return err;
  164. err = crypto_ahash_import(&ctx2->req, state);
  165. if (err) {
  166. sock_orphan(sk2);
  167. sock_put(sk2);
  168. }
  169. return err;
  170. }
  171. static struct proto_ops algif_hash_ops = {
  172. .family = PF_ALG,
  173. .connect = sock_no_connect,
  174. .socketpair = sock_no_socketpair,
  175. .getname = sock_no_getname,
  176. .ioctl = sock_no_ioctl,
  177. .listen = sock_no_listen,
  178. .shutdown = sock_no_shutdown,
  179. .getsockopt = sock_no_getsockopt,
  180. .mmap = sock_no_mmap,
  181. .bind = sock_no_bind,
  182. .setsockopt = sock_no_setsockopt,
  183. .poll = sock_no_poll,
  184. .release = af_alg_release,
  185. .sendmsg = hash_sendmsg,
  186. .sendpage = hash_sendpage,
  187. .recvmsg = hash_recvmsg,
  188. .accept = hash_accept,
  189. };
  190. static void *hash_bind(const char *name, u32 type, u32 mask)
  191. {
  192. return crypto_alloc_ahash(name, type, mask);
  193. }
  194. static void hash_release(void *private)
  195. {
  196. crypto_free_ahash(private);
  197. }
  198. static int hash_setkey(void *private, const u8 *key, unsigned int keylen)
  199. {
  200. return crypto_ahash_setkey(private, key, keylen);
  201. }
  202. static void hash_sock_destruct(struct sock *sk)
  203. {
  204. struct alg_sock *ask = alg_sk(sk);
  205. struct hash_ctx *ctx = ask->private;
  206. sock_kzfree_s(sk, ctx->result,
  207. crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req)));
  208. sock_kfree_s(sk, ctx, ctx->len);
  209. af_alg_release_parent(sk);
  210. }
  211. static int hash_accept_parent(void *private, struct sock *sk)
  212. {
  213. struct hash_ctx *ctx;
  214. struct alg_sock *ask = alg_sk(sk);
  215. unsigned len = sizeof(*ctx) + crypto_ahash_reqsize(private);
  216. unsigned ds = crypto_ahash_digestsize(private);
  217. ctx = sock_kmalloc(sk, len, GFP_KERNEL);
  218. if (!ctx)
  219. return -ENOMEM;
  220. ctx->result = sock_kmalloc(sk, ds, GFP_KERNEL);
  221. if (!ctx->result) {
  222. sock_kfree_s(sk, ctx, len);
  223. return -ENOMEM;
  224. }
  225. memset(ctx->result, 0, ds);
  226. ctx->len = len;
  227. ctx->more = 0;
  228. af_alg_init_completion(&ctx->completion);
  229. ask->private = ctx;
  230. ahash_request_set_tfm(&ctx->req, private);
  231. ahash_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  232. af_alg_complete, &ctx->completion);
  233. sk->sk_destruct = hash_sock_destruct;
  234. return 0;
  235. }
  236. static const struct af_alg_type algif_type_hash = {
  237. .bind = hash_bind,
  238. .release = hash_release,
  239. .setkey = hash_setkey,
  240. .accept = hash_accept_parent,
  241. .ops = &algif_hash_ops,
  242. .name = "hash",
  243. .owner = THIS_MODULE
  244. };
  245. static int __init algif_hash_init(void)
  246. {
  247. return af_alg_register_type(&algif_type_hash);
  248. }
  249. static void __exit algif_hash_exit(void)
  250. {
  251. int err = af_alg_unregister_type(&algif_type_hash);
  252. BUG_ON(err);
  253. }
  254. module_init(algif_hash_init);
  255. module_exit(algif_hash_exit);
  256. MODULE_LICENSE("GPL");