algif_hash.c 9.5 KB

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