From patchwork Sun Nov 16 02:28:52 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephan Mueller X-Patchwork-Id: 5312761 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Original-To: patchwork-linux-crypto@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id B5D3C9F440 for ; Sun, 16 Nov 2014 02:48:20 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 9CF3B201DD for ; Sun, 16 Nov 2014 02:48:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A9868201ED for ; Sun, 16 Nov 2014 02:48:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755148AbaKPCsD (ORCPT ); Sat, 15 Nov 2014 21:48:03 -0500 Received: from mail.eperm.de ([89.247.134.16]:54564 "EHLO mail.eperm.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755082AbaKPCsB (ORCPT ); Sat, 15 Nov 2014 21:48:01 -0500 X-AuthUser: sm@eperm.de Received: from tachyon.chronox.de by mail.eperm.de with [XMail 1.27 ESMTP Server] id for from ; Sun, 16 Nov 2014 03:47:58 +0100 From: Stephan Mueller To: Herbert Xu Cc: Daniel Borkmann , quentin.gouchet@gmail.com, LKML , linux-crypto@vger.kernel.org, ABI/API Subject: [PATCH v2 09/10] crypto: AF_ALG: user space interface for hash info Date: Sun, 16 Nov 2014 03:28:52 +0100 Message-ID: <1907032.lnybFlp3cC@tachyon.chronox.de> User-Agent: KMail/4.14.2 (Linux/3.17.2-300.fc21.x86_64; KDE/4.14.2; x86_64; ; ) In-Reply-To: <5365136.g8vbXlhRyC@tachyon.chronox.de> References: <5365136.g8vbXlhRyC@tachyon.chronox.de> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 hash: * digestsize The patch adds a getsockopt interface for the hash 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. A fully working example using the digestsize interface is provided at http://www.chronox.de/libkcapi.html Signed-off-by: Stephan Mueller --- crypto/algif_hash.c | 35 ++++++++++++++++++++++++++++++++++- include/uapi/linux/if_alg.h | 1 + 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/crypto/algif_hash.c b/crypto/algif_hash.c index f75db4c..68ae34c 100644 --- a/crypto/algif_hash.c +++ b/crypto/algif_hash.c @@ -216,6 +216,39 @@ static int hash_accept(struct socket *sock, struct socket *newsock, int flags) return err; } +static int hash_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 hash_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_DIGESTSIZE: + len = crypto_ahash_digestsize(crypto_ahash_reqtfm(&ctx->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_hash_ops = { .family = PF_ALG, @@ -225,7 +258,6 @@ static struct proto_ops algif_hash_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, .setsockopt = sock_no_setsockopt, @@ -236,6 +268,7 @@ static struct proto_ops algif_hash_ops = { .sendpage = hash_sendpage, .recvmsg = hash_recvmsg, .accept = hash_accept, + .getsockopt = hash_getsockopt, }; static void *hash_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 b8fb714..3759d3c 100644 --- a/include/uapi/linux/if_alg.h +++ b/include/uapi/linux/if_alg.h @@ -44,6 +44,7 @@ struct af_alg_aead_assoc { #define ALG_GET_BLOCKSIZE 1 #define ALG_GET_IVSIZE 2 #define ALG_GET_AEAD_AUTHSIZE 3 +#define ALG_GET_DIGESTSIZE 4 /* Operations */ #define ALG_OP_DECRYPT 0