From patchwork Sat May 2 05:31:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11523689 X-Patchwork-Delegate: marcel@holtmann.org Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 54C8B1864 for ; Sat, 2 May 2020 05:33:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3DD9020643 for ; Sat, 2 May 2020 05:33:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588397614; bh=Xy09gJgOkkbJ0ZYrb6dZNClW7tkDv1nBUg2GeKdk1cQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=wHr6Y81U4X97C4U3k1Sj1MSpWF+lo82xLCnKRVfhfxkKJu/HSqvXs5iLgx+zlNKnb pD7yu5GrhewoMPEPjBPQ+v9FXM2oEkPA0GDyLoAzCVUpURajiTfwX51WvAaFYoQc96 7sM2DPAC6UblJvj6KRmUigIfeR9KBDELGpyEa3Uk= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727088AbgEBFdc (ORCPT ); Sat, 2 May 2020 01:33:32 -0400 Received: from mail.kernel.org ([198.145.29.99]:39030 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726058AbgEBFdb (ORCPT ); Sat, 2 May 2020 01:33:31 -0400 Received: from sol.hsd1.ca.comcast.net (c-107-3-166-239.hsd1.ca.comcast.net [107.3.166.239]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 40D182184D; Sat, 2 May 2020 05:33:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588397610; bh=Xy09gJgOkkbJ0ZYrb6dZNClW7tkDv1nBUg2GeKdk1cQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lAXfGF92DERufghwz00F/QIgqNab18uegXgVsfl+gbihI7P/p7oBkSo2b7SRYemMo leI+9L+LpjxypzBwDoEDHo1kF1Vsv7aU4ikYmzDLr8A4ucNa1VtsvN0WpnFizpRcYI 5y9cBCWx62jX8rIidTrAwvjSxsV+xDpN0LwZDndU= From: Eric Biggers To: linux-crypto@vger.kernel.org Cc: Cheng-Yi Chiang , ecryptfs@vger.kernel.org, Enric Balletbo i Serra , Gilad Ben-Yossef , Guenter Roeck , Jesper Nilsson , Kamil Konieczny , keyrings@vger.kernel.org, Krzysztof Kozlowski , Krzysztof Opasiak , Lars Persson , linux-bluetooth@vger.kernel.org, linux-mtd@lists.infradead.org, linux-nfs@vger.kernel.org, linux-sctp@vger.kernel.org, Robert Baldyga , Tom Lendacky , Vladimir Zapolskiy , Zaibo Xu Subject: [PATCH 01/20] crypto: hash - introduce crypto_shash_tfm_digest() Date: Fri, 1 May 2020 22:31:03 -0700 Message-Id: <20200502053122.995648-2-ebiggers@kernel.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200502053122.995648-1-ebiggers@kernel.org> References: <20200502053122.995648-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Eric Biggers Currently the simplest use of the shash API is to use crypto_shash_digest() to digest a whole buffer. However, this still requires allocating a hash descriptor (struct shash_desc). Many users don't really want to preallocate one and instead just use a one-off descriptor on the stack like the following: { SHASH_DESC_ON_STACK(desc, tfm); int err; desc->tfm = tfm; err = crypto_shash_digest(desc, data, len, out); shash_desc_zero(desc); } Wrap this in a new helper function crypto_shash_tfm_digest() that can be used instead of the above. Signed-off-by: Eric Biggers --- crypto/shash.c | 16 ++++++++++++++++ include/crypto/hash.h | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/crypto/shash.c b/crypto/shash.c index c075b26c2a1d9f..e6a4b5f39b8c64 100644 --- a/crypto/shash.c +++ b/crypto/shash.c @@ -206,6 +206,22 @@ int crypto_shash_digest(struct shash_desc *desc, const u8 *data, } EXPORT_SYMBOL_GPL(crypto_shash_digest); +int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data, + unsigned int len, u8 *out) +{ + SHASH_DESC_ON_STACK(desc, tfm); + int err; + + desc->tfm = tfm; + + err = crypto_shash_digest(desc, data, len, out); + + shash_desc_zero(desc); + + return err; +} +EXPORT_SYMBOL_GPL(crypto_shash_tfm_digest); + static int shash_default_export(struct shash_desc *desc, void *out) { memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(desc->tfm)); diff --git a/include/crypto/hash.h b/include/crypto/hash.h index cee446c59497c6..4829d2367eda87 100644 --- a/include/crypto/hash.h +++ b/include/crypto/hash.h @@ -855,6 +855,25 @@ int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key, int crypto_shash_digest(struct shash_desc *desc, const u8 *data, unsigned int len, u8 *out); +/** + * crypto_shash_tfm_digest() - calculate message digest for buffer + * @tfm: hash transformation object + * @data: see crypto_shash_update() + * @len: see crypto_shash_update() + * @out: see crypto_shash_final() + * + * This is a simplified version of crypto_shash_digest() for users who don't + * want to allocate their own hash descriptor (shash_desc). Instead, + * crypto_shash_tfm_digest() takes a hash transformation object (crypto_shash) + * directly, and it allocates a hash descriptor on the stack internally. + * Note that this stack allocation may be fairly large. + * + * Context: Any context. + * Return: 0 on success; < 0 if an error occurred. + */ +int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data, + unsigned int len, u8 *out); + /** * crypto_shash_export() - extract operational state for message digest * @desc: reference to the operational state handle whose state is exported From patchwork Sat May 2 05:31:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11523727 X-Patchwork-Delegate: marcel@holtmann.org Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 985221862 for ; Sat, 2 May 2020 05:33:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7598E2184D for ; Sat, 2 May 2020 05:33:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588397630; bh=aRnhsnypAWsWXKVWbib/6Chk6PgcC0D+Q1jEbEW+EEA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=RLvEdfwI4m8NSkwE94Y9KpMyyoiE0WwwDmcAqLgbCN06PiFecaZu1ovhPj7Cx/zrA 3frFJqGB3a9NAeBqpKgLv4juC837mcrPXt1Jh9tRJQyF4gy9ScFd/PHHpSuSTUvbOV 3lhUe8eiXHpqx5EhSz5HhTcyt+RhXTqRWVPSyTpg= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727850AbgEBFdt (ORCPT ); Sat, 2 May 2020 01:33:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:39294 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727828AbgEBFdq (ORCPT ); Sat, 2 May 2020 01:33:46 -0400 Received: from sol.hsd1.ca.comcast.net (c-107-3-166-239.hsd1.ca.comcast.net [107.3.166.239]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A0C6924956; Sat, 2 May 2020 05:33:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588397625; bh=aRnhsnypAWsWXKVWbib/6Chk6PgcC0D+Q1jEbEW+EEA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PTGKbB6CEORZGYYUIt9JxkJSWjc4AMdJy/Ebcqg1ZEvcjNV7zjU8yAoCxLfvgP6p/ GpQs4ja/Otvda6MMGOV78JogJg2t6gr+50RB3vkM0+xAL3oIxIeqJdhj0Hb1ot5mo0 /x72PV9ExK8jtq2rF5WYPlnNqYV+DnwY/s5p22iA= From: Eric Biggers To: linux-crypto@vger.kernel.org Cc: linux-bluetooth@vger.kernel.org Subject: [PATCH 17/20] Bluetooth: use crypto_shash_tfm_digest() Date: Fri, 1 May 2020 22:31:19 -0700 Message-Id: <20200502053122.995648-18-ebiggers@kernel.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200502053122.995648-1-ebiggers@kernel.org> References: <20200502053122.995648-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Eric Biggers Instead of manually allocating a 'struct shash_desc' on the stack and calling crypto_shash_digest(), switch to using the new helper function crypto_shash_tfm_digest() which does this for us. Cc: linux-bluetooth@vger.kernel.org Signed-off-by: Eric Biggers --- net/bluetooth/smp.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c index 1476a91ce93572..d022f126eb026b 100644 --- a/net/bluetooth/smp.c +++ b/net/bluetooth/smp.c @@ -170,7 +170,6 @@ static int aes_cmac(struct crypto_shash *tfm, const u8 k[16], const u8 *m, size_t len, u8 mac[16]) { uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX]; - SHASH_DESC_ON_STACK(desc, tfm); int err; if (len > CMAC_MSG_MAX) @@ -181,8 +180,6 @@ static int aes_cmac(struct crypto_shash *tfm, const u8 k[16], const u8 *m, return -EINVAL; } - desc->tfm = tfm; - /* Swap key and message from LSB to MSB */ swap_buf(k, tmp, 16); swap_buf(m, msg_msb, len); @@ -196,8 +193,7 @@ static int aes_cmac(struct crypto_shash *tfm, const u8 k[16], const u8 *m, return err; } - err = crypto_shash_digest(desc, msg_msb, len, mac_msb); - shash_desc_zero(desc); + err = crypto_shash_tfm_digest(tfm, msg_msb, len, mac_msb); if (err) { BT_ERR("Hash computation error %d", err); return err;