algif_hash.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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_alloc_result(struct sock *sk, struct hash_ctx *ctx)
  31. {
  32. unsigned ds;
  33. if (ctx->result)
  34. return 0;
  35. ds = crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req));
  36. ctx->result = sock_kmalloc(sk, ds, GFP_KERNEL);
  37. if (!ctx->result)
  38. return -ENOMEM;
  39. memset(ctx->result, 0, ds);
  40. return 0;
  41. }
  42. static void hash_free_result(struct sock *sk, struct hash_ctx *ctx)
  43. {
  44. unsigned ds;
  45. if (!ctx->result)
  46. return;
  47. ds = crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req));
  48. sock_kzfree_s(sk, ctx->result, ds);
  49. ctx->result = NULL;
  50. }
  51. static int hash_sendmsg(struct socket *sock, struct msghdr *msg,
  52. size_t ignored)
  53. {
  54. int limit = ALG_MAX_PAGES * PAGE_SIZE;
  55. struct sock *sk = sock->sk;
  56. struct alg_sock *ask = alg_sk(sk);
  57. struct hash_ctx *ctx = ask->private;
  58. long copied = 0;
  59. int err;
  60. if (limit > sk->sk_sndbuf)
  61. limit = sk->sk_sndbuf;
  62. lock_sock(sk);
  63. if (!ctx->more) {
  64. if ((msg->msg_flags & MSG_MORE))
  65. hash_free_result(sk, ctx);
  66. err = af_alg_wait_for_completion(crypto_ahash_init(&ctx->req),
  67. &ctx->completion);
  68. if (err)
  69. goto unlock;
  70. }
  71. ctx->more = 0;
  72. while (msg_data_left(msg)) {
  73. int len = msg_data_left(msg);
  74. if (len > limit)
  75. len = limit;
  76. len = af_alg_make_sg(&ctx->sgl, &msg->msg_iter, len);
  77. if (len < 0) {
  78. err = copied ? 0 : len;
  79. goto unlock;
  80. }
  81. ahash_request_set_crypt(&ctx->req, ctx->sgl.sg, NULL, len);
  82. err = af_alg_wait_for_completion(crypto_ahash_update(&ctx->req),
  83. &ctx->completion);
  84. af_alg_free_sg(&ctx->sgl);
  85. if (err)
  86. goto unlock;
  87. copied += len;
  88. iov_iter_advance(&msg->msg_iter, len);
  89. }
  90. err = 0;
  91. ctx->more = msg->msg_flags & MSG_MORE;
  92. if (!ctx->more) {
  93. err = hash_alloc_result(sk, ctx);
  94. if (err)
  95. goto unlock;
  96. ahash_request_set_crypt(&ctx->req, NULL, ctx->result, 0);
  97. err = af_alg_wait_for_completion(crypto_ahash_final(&ctx->req),
  98. &ctx->completion);
  99. }
  100. unlock:
  101. release_sock(sk);
  102. return err ?: copied;
  103. }
  104. static ssize_t hash_sendpage(struct socket *sock, struct page *page,
  105. int offset, size_t size, int flags)
  106. {
  107. struct sock *sk = sock->sk;
  108. struct alg_sock *ask = alg_sk(sk);
  109. struct hash_ctx *ctx = ask->private;
  110. int err;
  111. if (flags & MSG_SENDPAGE_NOTLAST)
  112. flags |= MSG_MORE;
  113. lock_sock(sk);
  114. sg_init_table(ctx->sgl.sg, 1);
  115. sg_set_page(ctx->sgl.sg, page, size, offset);
  116. if (!(flags & MSG_MORE)) {
  117. err = hash_alloc_result(sk, ctx);
  118. if (err)
  119. goto unlock;
  120. } else if (!ctx->more)
  121. hash_free_result(sk, ctx);
  122. ahash_request_set_crypt(&ctx->req, ctx->sgl.sg, ctx->result, size);
  123. if (!(flags & MSG_MORE)) {
  124. if (ctx->more)
  125. err = crypto_ahash_finup(&ctx->req);
  126. else
  127. err = crypto_ahash_digest(&ctx->req);
  128. } else {
  129. if (!ctx->more) {
  130. err = crypto_ahash_init(&ctx->req);
  131. err = af_alg_wait_for_completion(err, &ctx->completion);
  132. if (err)
  133. goto unlock;
  134. }
  135. err = crypto_ahash_update(&ctx->req);
  136. }
  137. err = af_alg_wait_for_completion(err, &ctx->completion);
  138. if (err)
  139. goto unlock;
  140. ctx->more = flags & MSG_MORE;
  141. unlock:
  142. release_sock(sk);
  143. return err ?: size;
  144. }
  145. static int hash_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  146. int flags)
  147. {
  148. struct sock *sk = sock->sk;
  149. struct alg_sock *ask = alg_sk(sk);
  150. struct hash_ctx *ctx = ask->private;
  151. unsigned ds = crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req));
  152. bool result;
  153. int err;
  154. if (len > ds)
  155. len = ds;
  156. else if (len < ds)
  157. msg->msg_flags |= MSG_TRUNC;
  158. lock_sock(sk);
  159. result = ctx->result;
  160. err = hash_alloc_result(sk, ctx);
  161. if (err)
  162. goto unlock;
  163. ahash_request_set_crypt(&ctx->req, NULL, ctx->result, 0);
  164. if (!result && !ctx->more) {
  165. err = af_alg_wait_for_completion(
  166. crypto_ahash_init(&ctx->req),
  167. &ctx->completion);
  168. if (err)
  169. goto unlock;
  170. }
  171. if (!result || ctx->more) {
  172. ctx->more = 0;
  173. err = af_alg_wait_for_completion(crypto_ahash_final(&ctx->req),
  174. &ctx->completion);
  175. if (err)
  176. goto unlock;
  177. }
  178. err = memcpy_to_msg(msg, ctx->result, len);
  179. unlock:
  180. hash_free_result(sk, ctx);
  181. release_sock(sk);
  182. return err ?: len;
  183. }
  184. static int hash_accept(struct socket *sock, struct socket *newsock, int flags)
  185. {
  186. struct sock *sk = sock->sk;
  187. struct alg_sock *ask = alg_sk(sk);
  188. struct hash_ctx *ctx = ask->private;
  189. struct ahash_request *req = &ctx->req;
  190. char state[crypto_ahash_statesize(crypto_ahash_reqtfm(req)) ? : 1];
  191. struct sock *sk2;
  192. struct alg_sock *ask2;
  193. struct hash_ctx *ctx2;
  194. bool more;
  195. int err;
  196. lock_sock(sk);
  197. more = ctx->more;
  198. err = more ? crypto_ahash_export(req, state) : 0;
  199. release_sock(sk);
  200. if (err)
  201. return err;
  202. err = af_alg_accept(ask->parent, newsock);
  203. if (err)
  204. return err;
  205. sk2 = newsock->sk;
  206. ask2 = alg_sk(sk2);
  207. ctx2 = ask2->private;
  208. ctx2->more = more;
  209. if (!more)
  210. return err;
  211. err = crypto_ahash_import(&ctx2->req, state);
  212. if (err) {
  213. sock_orphan(sk2);
  214. sock_put(sk2);
  215. }
  216. return err;
  217. }
  218. static struct proto_ops algif_hash_ops = {
  219. .family = PF_ALG,
  220. .connect = sock_no_connect,
  221. .socketpair = sock_no_socketpair,
  222. .getname = sock_no_getname,
  223. .ioctl = sock_no_ioctl,
  224. .listen = sock_no_listen,
  225. .shutdown = sock_no_shutdown,
  226. .getsockopt = sock_no_getsockopt,
  227. .mmap = sock_no_mmap,
  228. .bind = sock_no_bind,
  229. .setsockopt = sock_no_setsockopt,
  230. .poll = sock_no_poll,
  231. .release = af_alg_release,
  232. .sendmsg = hash_sendmsg,
  233. .sendpage = hash_sendpage,
  234. .recvmsg = hash_recvmsg,
  235. .accept = hash_accept,
  236. };
  237. static int hash_check_key(struct socket *sock)
  238. {
  239. int err = 0;
  240. struct sock *psk;
  241. struct alg_sock *pask;
  242. struct crypto_ahash *tfm;
  243. struct sock *sk = sock->sk;
  244. struct alg_sock *ask = alg_sk(sk);
  245. lock_sock(sk);
  246. if (ask->refcnt)
  247. goto unlock_child;
  248. psk = ask->parent;
  249. pask = alg_sk(ask->parent);
  250. tfm = pask->private;
  251. err = -ENOKEY;
  252. lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
  253. if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
  254. goto unlock;
  255. if (!pask->refcnt++)
  256. sock_hold(psk);
  257. ask->refcnt = 1;
  258. sock_put(psk);
  259. err = 0;
  260. unlock:
  261. release_sock(psk);
  262. unlock_child:
  263. release_sock(sk);
  264. return err;
  265. }
  266. static int hash_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
  267. size_t size)
  268. {
  269. int err;
  270. err = hash_check_key(sock);
  271. if (err)
  272. return err;
  273. return hash_sendmsg(sock, msg, size);
  274. }
  275. static ssize_t hash_sendpage_nokey(struct socket *sock, struct page *page,
  276. int offset, size_t size, int flags)
  277. {
  278. int err;
  279. err = hash_check_key(sock);
  280. if (err)
  281. return err;
  282. return hash_sendpage(sock, page, offset, size, flags);
  283. }
  284. static int hash_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
  285. size_t ignored, int flags)
  286. {
  287. int err;
  288. err = hash_check_key(sock);
  289. if (err)
  290. return err;
  291. return hash_recvmsg(sock, msg, ignored, flags);
  292. }
  293. static int hash_accept_nokey(struct socket *sock, struct socket *newsock,
  294. int flags)
  295. {
  296. int err;
  297. err = hash_check_key(sock);
  298. if (err)
  299. return err;
  300. return hash_accept(sock, newsock, flags);
  301. }
  302. static struct proto_ops algif_hash_ops_nokey = {
  303. .family = PF_ALG,
  304. .connect = sock_no_connect,
  305. .socketpair = sock_no_socketpair,
  306. .getname = sock_no_getname,
  307. .ioctl = sock_no_ioctl,
  308. .listen = sock_no_listen,
  309. .shutdown = sock_no_shutdown,
  310. .getsockopt = sock_no_getsockopt,
  311. .mmap = sock_no_mmap,
  312. .bind = sock_no_bind,
  313. .setsockopt = sock_no_setsockopt,
  314. .poll = sock_no_poll,
  315. .release = af_alg_release,
  316. .sendmsg = hash_sendmsg_nokey,
  317. .sendpage = hash_sendpage_nokey,
  318. .recvmsg = hash_recvmsg_nokey,
  319. .accept = hash_accept_nokey,
  320. };
  321. static void *hash_bind(const char *name, u32 type, u32 mask)
  322. {
  323. return crypto_alloc_ahash(name, type, mask);
  324. }
  325. static void hash_release(void *private)
  326. {
  327. crypto_free_ahash(private);
  328. }
  329. static int hash_setkey(void *private, const u8 *key, unsigned int keylen)
  330. {
  331. return crypto_ahash_setkey(private, key, keylen);
  332. }
  333. static void hash_sock_destruct(struct sock *sk)
  334. {
  335. struct alg_sock *ask = alg_sk(sk);
  336. struct hash_ctx *ctx = ask->private;
  337. hash_free_result(sk, ctx);
  338. sock_kfree_s(sk, ctx, ctx->len);
  339. af_alg_release_parent(sk);
  340. }
  341. static int hash_accept_parent_nokey(void *private, struct sock *sk)
  342. {
  343. struct crypto_ahash *tfm = private;
  344. struct alg_sock *ask = alg_sk(sk);
  345. struct hash_ctx *ctx;
  346. unsigned int len = sizeof(*ctx) + crypto_ahash_reqsize(tfm);
  347. ctx = sock_kmalloc(sk, len, GFP_KERNEL);
  348. if (!ctx)
  349. return -ENOMEM;
  350. ctx->result = NULL;
  351. ctx->len = len;
  352. ctx->more = 0;
  353. af_alg_init_completion(&ctx->completion);
  354. ask->private = ctx;
  355. ahash_request_set_tfm(&ctx->req, tfm);
  356. ahash_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  357. af_alg_complete, &ctx->completion);
  358. sk->sk_destruct = hash_sock_destruct;
  359. return 0;
  360. }
  361. static int hash_accept_parent(void *private, struct sock *sk)
  362. {
  363. struct crypto_ahash *tfm = private;
  364. if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
  365. return -ENOKEY;
  366. return hash_accept_parent_nokey(private, sk);
  367. }
  368. static const struct af_alg_type algif_type_hash = {
  369. .bind = hash_bind,
  370. .release = hash_release,
  371. .setkey = hash_setkey,
  372. .accept = hash_accept_parent,
  373. .accept_nokey = hash_accept_parent_nokey,
  374. .ops = &algif_hash_ops,
  375. .ops_nokey = &algif_hash_ops_nokey,
  376. .name = "hash",
  377. .owner = THIS_MODULE
  378. };
  379. static int __init algif_hash_init(void)
  380. {
  381. return af_alg_register_type(&algif_type_hash);
  382. }
  383. static void __exit algif_hash_exit(void)
  384. {
  385. int err = af_alg_unregister_type(&algif_type_hash);
  386. BUG_ON(err);
  387. }
  388. module_init(algif_hash_init);
  389. module_exit(algif_hash_exit);
  390. MODULE_LICENSE("GPL");