All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stephan Mueller <smueller@chronox.de>
To: Herbert Xu <herbert@gondor.apana.org.au>
Cc: ABI/API <linux-api@vger.kernel.org>,
	linux-crypto@vger.kernel.org, LKML <linux-kernel@vger.kernel.org>
Subject: [PATCH 2/8] crypto: AF_ALG: user space interface for cipher info
Date: Wed, 12 Nov 2014 08:01:04 +0100	[thread overview]
Message-ID: <3969167.9oqJUVzqHv@tachyon.chronox.de> (raw)
In-Reply-To: <4738444.A2vZX1nNCo@tachyon.chronox.de>

The AF_ALG interface allows normal cipher (hash, encrypt, decrypt).
However, it does not allow user space to obtain the following generic
information about the currently active cipher:

	* block size of the cipher

	* IV size of the cipher

	* for AEAD, the maximum authentication tag size

The patch adds a getsockopt interface for the symmetric ciphers to
answer such information requests from user space.

The kernel crypto API function calls are used to obtain the real data.
As all data are simple integer values, the getsockopt handler function
uses put_user() to return the integer value to user space in the
*optval parameter of getsockopt.

Signed-off-by: Stephan Mueller <smueller@chronox.de>
---
 crypto/algif_skcipher.c     | 46 ++++++++++++++++++++++++++++++++++++++++++++-
 include/uapi/linux/if_alg.h |  3 +++
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/crypto/algif_skcipher.c b/crypto/algif_skcipher.c
index 83187f4..7a1656b 100644
--- a/crypto/algif_skcipher.c
+++ b/crypto/algif_skcipher.c
@@ -522,6 +522,50 @@ static unsigned int skcipher_poll(struct file *file, struct socket *sock,
 	return mask;
 }
 
+static int skcipher_getsockopt(struct socket *sock, int level, int optname,
+			       char __user *optval, int __user *optlen)
+{
+	struct sock *sk = sock->sk;
+	struct alg_sock *ask = alg_sk(sk);
+	struct skcipher_ctx *ctx = ask->private;
+	const struct af_alg_type *type;
+	int len = 0;
+	int err = -EOPNOTSUPP;
+
+	lock_sock(sk);
+	type = ask->type;
+
+	if (level != SOL_ALG || !type)
+		goto unlock;
+
+	switch (optname) {
+	case ALG_GET_BLOCKSIZE:
+		len = skcipher_crypto_blocksize(ctx);
+		err = 0;
+		break;
+	case ALG_GET_IVSIZE:
+		len = skcipher_crypto_ivsize_ctx(ctx);
+		err = 0;
+		break;
+	case ALG_GET_AEAD_AUTHSIZE:
+		if (ctx->aead) {
+			len = crypto_aead_authsize(crypto_aead_reqtfm(
+						   &ctx->u.aead_req));
+			err = 0;
+		}
+		break;
+	default:
+		break;
+	}
+
+unlock:
+	release_sock(sk);
+	if (err >= 0)
+		err = put_user(len, optlen);
+
+	return err;
+}
+
 static struct proto_ops algif_skcipher_ops = {
 	.family		=	PF_ALG,
 
@@ -531,7 +575,6 @@ static struct proto_ops algif_skcipher_ops = {
 	.ioctl		=	sock_no_ioctl,
 	.listen		=	sock_no_listen,
 	.shutdown	=	sock_no_shutdown,
-	.getsockopt	=	sock_no_getsockopt,
 	.mmap		=	sock_no_mmap,
 	.bind		=	sock_no_bind,
 	.accept		=	sock_no_accept,
@@ -542,6 +585,7 @@ static struct proto_ops algif_skcipher_ops = {
 	.sendpage	=	skcipher_sendpage,
 	.recvmsg	=	skcipher_recvmsg,
 	.poll		=	skcipher_poll,
+	.getsockopt	=	skcipher_getsockopt,
 };
 
 static void *skcipher_bind(const char *name, u32 type, u32 mask)
diff --git a/include/uapi/linux/if_alg.h b/include/uapi/linux/if_alg.h
index 64e7008..267fd73 100644
--- a/include/uapi/linux/if_alg.h
+++ b/include/uapi/linux/if_alg.h
@@ -39,6 +39,9 @@ struct af_alg_aead_assoc {
 #define ALG_SET_OP			3
 #define ALG_SET_AEAD_ASSOC		4
 #define ALG_SET_AEAD_AUTHSIZE		5
+#define ALG_GET_BLOCKSIZE		6
+#define ALG_GET_IVSIZE			7
+#define ALG_GET_AEAD_AUTHSIZE		8
 
 /* Operations */
 #define ALG_OP_DECRYPT			0
-- 
2.1.0

  parent reply	other threads:[~2014-11-12  7:01 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-12  6:59 [PATCH 0/8] crypto: AF_ALG: add AEAD and RNG support Stephan Mueller
2014-11-12  7:00 ` [PATCH 1/8] crypto: AF_ALG: add user space interface for AEAD Stephan Mueller
2014-11-12  7:01 ` Stephan Mueller [this message]
2014-11-12  7:01 ` [PATCH 3/8] crypto: AF_ALG: extend data structuers " Stephan Mueller
2014-11-12  7:03 ` [PATCH 4/8] crypto: AF_ALG: crypto API calls to inline functions Stephan Mueller
2014-11-12  7:04 ` [PATCH 5/8] crypto: AF_ALG: add AEAD support Stephan Mueller
2014-11-12  7:05 ` [PATCH 6/8] crypto: AF_ALG: make setkey optional Stephan Mueller
2014-11-12  7:05 ` [PATCH 7/8] crypto: AF_ALG: add random number generator support Stephan Mueller
2014-11-12 16:15   ` Daniel Borkmann
     [not found]     ` <546387B8.9050601-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2014-11-12 16:54       ` Stephan Mueller
2014-11-12 16:54         ` Stephan Mueller
2014-11-12 17:23         ` Daniel Borkmann
2014-11-12 17:46           ` Stephan Mueller
2014-11-12 17:51             ` Daniel Borkmann
2014-11-12 17:51               ` Daniel Borkmann
2014-11-12  7:06 ` [PATCH 8/8] crypto: AF_ALG: enable RNG interface compilation Stephan Mueller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3969167.9oqJUVzqHv@tachyon.chronox.de \
    --to=smueller@chronox.de \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.