algif_hash.c 9.6 KB

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