algif_hash.c 9.8 KB

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