Back to home page

LXR

 
 

    


0001 /*
0002  * algif_hash: User-space interface for hash algorithms
0003  *
0004  * This file provides the user-space API for hash algorithms.
0005  *
0006  * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
0007  *
0008  * This program is free software; you can redistribute it and/or modify it
0009  * under the terms of the GNU General Public License as published by the Free
0010  * Software Foundation; either version 2 of the License, or (at your option)
0011  * any later version.
0012  *
0013  */
0014 
0015 #include <crypto/hash.h>
0016 #include <crypto/if_alg.h>
0017 #include <linux/init.h>
0018 #include <linux/kernel.h>
0019 #include <linux/mm.h>
0020 #include <linux/module.h>
0021 #include <linux/net.h>
0022 #include <net/sock.h>
0023 
0024 struct hash_ctx {
0025     struct af_alg_sgl sgl;
0026 
0027     u8 *result;
0028 
0029     struct af_alg_completion completion;
0030 
0031     unsigned int len;
0032     bool more;
0033 
0034     struct ahash_request req;
0035 };
0036 
0037 struct algif_hash_tfm {
0038     struct crypto_ahash *hash;
0039     bool has_key;
0040 };
0041 
0042 static int hash_alloc_result(struct sock *sk, struct hash_ctx *ctx)
0043 {
0044     unsigned ds;
0045 
0046     if (ctx->result)
0047         return 0;
0048 
0049     ds = crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req));
0050 
0051     ctx->result = sock_kmalloc(sk, ds, GFP_KERNEL);
0052     if (!ctx->result)
0053         return -ENOMEM;
0054 
0055     memset(ctx->result, 0, ds);
0056 
0057     return 0;
0058 }
0059 
0060 static void hash_free_result(struct sock *sk, struct hash_ctx *ctx)
0061 {
0062     unsigned ds;
0063 
0064     if (!ctx->result)
0065         return;
0066 
0067     ds = crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req));
0068 
0069     sock_kzfree_s(sk, ctx->result, ds);
0070     ctx->result = NULL;
0071 }
0072 
0073 static int hash_sendmsg(struct socket *sock, struct msghdr *msg,
0074             size_t ignored)
0075 {
0076     int limit = ALG_MAX_PAGES * PAGE_SIZE;
0077     struct sock *sk = sock->sk;
0078     struct alg_sock *ask = alg_sk(sk);
0079     struct hash_ctx *ctx = ask->private;
0080     long copied = 0;
0081     int err;
0082 
0083     if (limit > sk->sk_sndbuf)
0084         limit = sk->sk_sndbuf;
0085 
0086     lock_sock(sk);
0087     if (!ctx->more) {
0088         if ((msg->msg_flags & MSG_MORE))
0089             hash_free_result(sk, ctx);
0090 
0091         err = af_alg_wait_for_completion(crypto_ahash_init(&ctx->req),
0092                         &ctx->completion);
0093         if (err)
0094             goto unlock;
0095     }
0096 
0097     ctx->more = 0;
0098 
0099     while (msg_data_left(msg)) {
0100         int len = msg_data_left(msg);
0101 
0102         if (len > limit)
0103             len = limit;
0104 
0105         len = af_alg_make_sg(&ctx->sgl, &msg->msg_iter, len);
0106         if (len < 0) {
0107             err = copied ? 0 : len;
0108             goto unlock;
0109         }
0110 
0111         ahash_request_set_crypt(&ctx->req, ctx->sgl.sg, NULL, len);
0112 
0113         err = af_alg_wait_for_completion(crypto_ahash_update(&ctx->req),
0114                          &ctx->completion);
0115         af_alg_free_sg(&ctx->sgl);
0116         if (err)
0117             goto unlock;
0118 
0119         copied += len;
0120         iov_iter_advance(&msg->msg_iter, len);
0121     }
0122 
0123     err = 0;
0124 
0125     ctx->more = msg->msg_flags & MSG_MORE;
0126     if (!ctx->more) {
0127         err = hash_alloc_result(sk, ctx);
0128         if (err)
0129             goto unlock;
0130 
0131         ahash_request_set_crypt(&ctx->req, NULL, ctx->result, 0);
0132         err = af_alg_wait_for_completion(crypto_ahash_final(&ctx->req),
0133                          &ctx->completion);
0134     }
0135 
0136 unlock:
0137     release_sock(sk);
0138 
0139     return err ?: copied;
0140 }
0141 
0142 static ssize_t hash_sendpage(struct socket *sock, struct page *page,
0143                  int offset, size_t size, int flags)
0144 {
0145     struct sock *sk = sock->sk;
0146     struct alg_sock *ask = alg_sk(sk);
0147     struct hash_ctx *ctx = ask->private;
0148     int err;
0149 
0150     if (flags & MSG_SENDPAGE_NOTLAST)
0151         flags |= MSG_MORE;
0152 
0153     lock_sock(sk);
0154     sg_init_table(ctx->sgl.sg, 1);
0155     sg_set_page(ctx->sgl.sg, page, size, offset);
0156 
0157     if (!(flags & MSG_MORE)) {
0158         err = hash_alloc_result(sk, ctx);
0159         if (err)
0160             goto unlock;
0161     } else if (!ctx->more)
0162         hash_free_result(sk, ctx);
0163 
0164     ahash_request_set_crypt(&ctx->req, ctx->sgl.sg, ctx->result, size);
0165 
0166     if (!(flags & MSG_MORE)) {
0167         if (ctx->more)
0168             err = crypto_ahash_finup(&ctx->req);
0169         else
0170             err = crypto_ahash_digest(&ctx->req);
0171     } else {
0172         if (!ctx->more) {
0173             err = crypto_ahash_init(&ctx->req);
0174             err = af_alg_wait_for_completion(err, &ctx->completion);
0175             if (err)
0176                 goto unlock;
0177         }
0178 
0179         err = crypto_ahash_update(&ctx->req);
0180     }
0181 
0182     err = af_alg_wait_for_completion(err, &ctx->completion);
0183     if (err)
0184         goto unlock;
0185 
0186     ctx->more = flags & MSG_MORE;
0187 
0188 unlock:
0189     release_sock(sk);
0190 
0191     return err ?: size;
0192 }
0193 
0194 static int hash_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
0195             int flags)
0196 {
0197     struct sock *sk = sock->sk;
0198     struct alg_sock *ask = alg_sk(sk);
0199     struct hash_ctx *ctx = ask->private;
0200     unsigned ds = crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->req));
0201     bool result;
0202     int err;
0203 
0204     if (len > ds)
0205         len = ds;
0206     else if (len < ds)
0207         msg->msg_flags |= MSG_TRUNC;
0208 
0209     lock_sock(sk);
0210     result = ctx->result;
0211     err = hash_alloc_result(sk, ctx);
0212     if (err)
0213         goto unlock;
0214 
0215     ahash_request_set_crypt(&ctx->req, NULL, ctx->result, 0);
0216 
0217     if (!result && !ctx->more) {
0218         err = af_alg_wait_for_completion(
0219                 crypto_ahash_init(&ctx->req),
0220                 &ctx->completion);
0221         if (err)
0222             goto unlock;
0223     }
0224 
0225     if (!result || ctx->more) {
0226         ctx->more = 0;
0227         err = af_alg_wait_for_completion(crypto_ahash_final(&ctx->req),
0228                          &ctx->completion);
0229         if (err)
0230             goto unlock;
0231     }
0232 
0233     err = memcpy_to_msg(msg, ctx->result, len);
0234 
0235 unlock:
0236     hash_free_result(sk, ctx);
0237     release_sock(sk);
0238 
0239     return err ?: len;
0240 }
0241 
0242 static int hash_accept(struct socket *sock, struct socket *newsock, int flags)
0243 {
0244     struct sock *sk = sock->sk;
0245     struct alg_sock *ask = alg_sk(sk);
0246     struct hash_ctx *ctx = ask->private;
0247     struct ahash_request *req = &ctx->req;
0248     char state[crypto_ahash_statesize(crypto_ahash_reqtfm(req))];
0249     struct sock *sk2;
0250     struct alg_sock *ask2;
0251     struct hash_ctx *ctx2;
0252     bool more;
0253     int err;
0254 
0255     lock_sock(sk);
0256     more = ctx->more;
0257     err = more ? crypto_ahash_export(req, state) : 0;
0258     release_sock(sk);
0259 
0260     if (err)
0261         return err;
0262 
0263     err = af_alg_accept(ask->parent, newsock);
0264     if (err)
0265         return err;
0266 
0267     sk2 = newsock->sk;
0268     ask2 = alg_sk(sk2);
0269     ctx2 = ask2->private;
0270     ctx2->more = more;
0271 
0272     if (!more)
0273         return err;
0274 
0275     err = crypto_ahash_import(&ctx2->req, state);
0276     if (err) {
0277         sock_orphan(sk2);
0278         sock_put(sk2);
0279     }
0280 
0281     return err;
0282 }
0283 
0284 static struct proto_ops algif_hash_ops = {
0285     .family     =   PF_ALG,
0286 
0287     .connect    =   sock_no_connect,
0288     .socketpair =   sock_no_socketpair,
0289     .getname    =   sock_no_getname,
0290     .ioctl      =   sock_no_ioctl,
0291     .listen     =   sock_no_listen,
0292     .shutdown   =   sock_no_shutdown,
0293     .getsockopt =   sock_no_getsockopt,
0294     .mmap       =   sock_no_mmap,
0295     .bind       =   sock_no_bind,
0296     .setsockopt =   sock_no_setsockopt,
0297     .poll       =   sock_no_poll,
0298 
0299     .release    =   af_alg_release,
0300     .sendmsg    =   hash_sendmsg,
0301     .sendpage   =   hash_sendpage,
0302     .recvmsg    =   hash_recvmsg,
0303     .accept     =   hash_accept,
0304 };
0305 
0306 static int hash_check_key(struct socket *sock)
0307 {
0308     int err = 0;
0309     struct sock *psk;
0310     struct alg_sock *pask;
0311     struct algif_hash_tfm *tfm;
0312     struct sock *sk = sock->sk;
0313     struct alg_sock *ask = alg_sk(sk);
0314 
0315     lock_sock(sk);
0316     if (ask->refcnt)
0317         goto unlock_child;
0318 
0319     psk = ask->parent;
0320     pask = alg_sk(ask->parent);
0321     tfm = pask->private;
0322 
0323     err = -ENOKEY;
0324     lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
0325     if (!tfm->has_key)
0326         goto unlock;
0327 
0328     if (!pask->refcnt++)
0329         sock_hold(psk);
0330 
0331     ask->refcnt = 1;
0332     sock_put(psk);
0333 
0334     err = 0;
0335 
0336 unlock:
0337     release_sock(psk);
0338 unlock_child:
0339     release_sock(sk);
0340 
0341     return err;
0342 }
0343 
0344 static int hash_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
0345                   size_t size)
0346 {
0347     int err;
0348 
0349     err = hash_check_key(sock);
0350     if (err)
0351         return err;
0352 
0353     return hash_sendmsg(sock, msg, size);
0354 }
0355 
0356 static ssize_t hash_sendpage_nokey(struct socket *sock, struct page *page,
0357                    int offset, size_t size, int flags)
0358 {
0359     int err;
0360 
0361     err = hash_check_key(sock);
0362     if (err)
0363         return err;
0364 
0365     return hash_sendpage(sock, page, offset, size, flags);
0366 }
0367 
0368 static int hash_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
0369                   size_t ignored, int flags)
0370 {
0371     int err;
0372 
0373     err = hash_check_key(sock);
0374     if (err)
0375         return err;
0376 
0377     return hash_recvmsg(sock, msg, ignored, flags);
0378 }
0379 
0380 static int hash_accept_nokey(struct socket *sock, struct socket *newsock,
0381                  int flags)
0382 {
0383     int err;
0384 
0385     err = hash_check_key(sock);
0386     if (err)
0387         return err;
0388 
0389     return hash_accept(sock, newsock, flags);
0390 }
0391 
0392 static struct proto_ops algif_hash_ops_nokey = {
0393     .family     =   PF_ALG,
0394 
0395     .connect    =   sock_no_connect,
0396     .socketpair =   sock_no_socketpair,
0397     .getname    =   sock_no_getname,
0398     .ioctl      =   sock_no_ioctl,
0399     .listen     =   sock_no_listen,
0400     .shutdown   =   sock_no_shutdown,
0401     .getsockopt =   sock_no_getsockopt,
0402     .mmap       =   sock_no_mmap,
0403     .bind       =   sock_no_bind,
0404     .setsockopt =   sock_no_setsockopt,
0405     .poll       =   sock_no_poll,
0406 
0407     .release    =   af_alg_release,
0408     .sendmsg    =   hash_sendmsg_nokey,
0409     .sendpage   =   hash_sendpage_nokey,
0410     .recvmsg    =   hash_recvmsg_nokey,
0411     .accept     =   hash_accept_nokey,
0412 };
0413 
0414 static void *hash_bind(const char *name, u32 type, u32 mask)
0415 {
0416     struct algif_hash_tfm *tfm;
0417     struct crypto_ahash *hash;
0418 
0419     tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
0420     if (!tfm)
0421         return ERR_PTR(-ENOMEM);
0422 
0423     hash = crypto_alloc_ahash(name, type, mask);
0424     if (IS_ERR(hash)) {
0425         kfree(tfm);
0426         return ERR_CAST(hash);
0427     }
0428 
0429     tfm->hash = hash;
0430 
0431     return tfm;
0432 }
0433 
0434 static void hash_release(void *private)
0435 {
0436     struct algif_hash_tfm *tfm = private;
0437 
0438     crypto_free_ahash(tfm->hash);
0439     kfree(tfm);
0440 }
0441 
0442 static int hash_setkey(void *private, const u8 *key, unsigned int keylen)
0443 {
0444     struct algif_hash_tfm *tfm = private;
0445     int err;
0446 
0447     err = crypto_ahash_setkey(tfm->hash, key, keylen);
0448     tfm->has_key = !err;
0449 
0450     return err;
0451 }
0452 
0453 static void hash_sock_destruct(struct sock *sk)
0454 {
0455     struct alg_sock *ask = alg_sk(sk);
0456     struct hash_ctx *ctx = ask->private;
0457 
0458     hash_free_result(sk, ctx);
0459     sock_kfree_s(sk, ctx, ctx->len);
0460     af_alg_release_parent(sk);
0461 }
0462 
0463 static int hash_accept_parent_nokey(void *private, struct sock *sk)
0464 {
0465     struct hash_ctx *ctx;
0466     struct alg_sock *ask = alg_sk(sk);
0467     struct algif_hash_tfm *tfm = private;
0468     struct crypto_ahash *hash = tfm->hash;
0469     unsigned len = sizeof(*ctx) + crypto_ahash_reqsize(hash);
0470 
0471     ctx = sock_kmalloc(sk, len, GFP_KERNEL);
0472     if (!ctx)
0473         return -ENOMEM;
0474 
0475     ctx->result = NULL;
0476     ctx->len = len;
0477     ctx->more = 0;
0478     af_alg_init_completion(&ctx->completion);
0479 
0480     ask->private = ctx;
0481 
0482     ahash_request_set_tfm(&ctx->req, hash);
0483     ahash_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG,
0484                    af_alg_complete, &ctx->completion);
0485 
0486     sk->sk_destruct = hash_sock_destruct;
0487 
0488     return 0;
0489 }
0490 
0491 static int hash_accept_parent(void *private, struct sock *sk)
0492 {
0493     struct algif_hash_tfm *tfm = private;
0494 
0495     if (!tfm->has_key && crypto_ahash_has_setkey(tfm->hash))
0496         return -ENOKEY;
0497 
0498     return hash_accept_parent_nokey(private, sk);
0499 }
0500 
0501 static const struct af_alg_type algif_type_hash = {
0502     .bind       =   hash_bind,
0503     .release    =   hash_release,
0504     .setkey     =   hash_setkey,
0505     .accept     =   hash_accept_parent,
0506     .accept_nokey   =   hash_accept_parent_nokey,
0507     .ops        =   &algif_hash_ops,
0508     .ops_nokey  =   &algif_hash_ops_nokey,
0509     .name       =   "hash",
0510     .owner      =   THIS_MODULE
0511 };
0512 
0513 static int __init algif_hash_init(void)
0514 {
0515     return af_alg_register_type(&algif_type_hash);
0516 }
0517 
0518 static void __exit algif_hash_exit(void)
0519 {
0520     int err = af_alg_unregister_type(&algif_type_hash);
0521     BUG_ON(err);
0522 }
0523 
0524 module_init(algif_hash_init);
0525 module_exit(algif_hash_exit);
0526 MODULE_LICENSE("GPL");