From patchwork Wed Jul 8 16:39:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11651915 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 3B14713B4 for ; Wed, 8 Jul 2020 16:42:36 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5FCBE20663 for ; Wed, 8 Jul 2020 16:42:35 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alsa-project.org header.i=@alsa-project.org header.b="i8rpqzRI"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="2AdOv2oj" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5FCBE20663 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=alsa-devel-bounces@alsa-project.org Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id A45A515F9; Wed, 8 Jul 2020 18:41:43 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz A45A515F9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1594226551; bh=rE+uvyPJsx8rFIs6khWUJUiQ/J5nt2exnTaDySam3F4=; h=From:To:Subject:Date:In-Reply-To:References:Cc:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=i8rpqzRI5XwFn7vtshTzFZzvrwFtDPxKDhLhR9sKsI+PpIvu34Rf7ddSQ7gYvR1ve uGWW1YD0EqyERBreWZZm3tp8k8Nsev5vbThdwosfiNVTcY6WSAuuLjLFgwgA87KREw pdOVhzPWQ1O2UAgGTH7re9R0XZM31hFOl66kGzOg= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 95AE5F80258; Wed, 8 Jul 2020 18:40:47 +0200 (CEST) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa1.perex.cz (Postfix, from userid 50401) id B5657F8015C; Wed, 8 Jul 2020 18:40:44 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on alsa1.perex.cz X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 6239AF800AE for ; Wed, 8 Jul 2020 18:40:34 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 6239AF800AE Authentication-Results: alsa1.perex.cz; dkim=pass (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="2AdOv2oj" 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 3FC932078D; Wed, 8 Jul 2020 16:40:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1594226432; bh=rE+uvyPJsx8rFIs6khWUJUiQ/J5nt2exnTaDySam3F4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2AdOv2ojkr5ZbL0vrz8iP22badDVJXNqokKsVwP6sOfodzMgiWpzAoAlmr5rO5RZm Prmrsra9KPKdnKtIEc726U5NOchmXKi17+4uSRRoXnAfJcKiU0pEJF50TLhG1xgFJd GYqnMHph6bl0BxWv6K4yr1q0HIsXrYa5cFF0t1b8= From: Eric Biggers To: linux-crypto@vger.kernel.org, Herbert Xu Subject: [PATCH v2 2/5] crypto: lib/sha256 - add sha256() function Date: Wed, 8 Jul 2020 09:39:40 -0700 Message-Id: <20200708163943.52071-3-ebiggers@kernel.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200708163943.52071-1-ebiggers@kernel.org> References: <20200708163943.52071-1-ebiggers@kernel.org> MIME-Version: 1.0 Cc: Enric Balletbo i Serra , alsa-devel@alsa-project.org, linux-efi@vger.kernel.org, Tzung-Bi Shih , Mat Martineau , Hans de Goede , sparclinux@vger.kernel.org, Matthieu Baerts , Guenter Roeck , mptcp@lists.01.org, Ard Biesheuvel , Cheng-Yi Chiang X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" From: Eric Biggers Add a function sha256() which computes a SHA-256 digest in one step, combining sha256_init() + sha256_update() + sha256_final(). This is similar to how we also have blake2s(). Reviewed-by: Ard Biesheuvel Tested-by: Hans de Goede Signed-off-by: Eric Biggers --- include/crypto/sha.h | 1 + lib/crypto/sha256.c | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/include/crypto/sha.h b/include/crypto/sha.h index 10753ff71d46..4ff3da816630 100644 --- a/include/crypto/sha.h +++ b/include/crypto/sha.h @@ -147,6 +147,7 @@ static inline void sha256_init(struct sha256_state *sctx) } void sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len); void sha256_final(struct sha256_state *sctx, u8 *out); +void sha256(const u8 *data, unsigned int len, u8 *out); static inline void sha224_init(struct sha256_state *sctx) { diff --git a/lib/crypto/sha256.c b/lib/crypto/sha256.c index 2e621697c5c3..2321f6cb322f 100644 --- a/lib/crypto/sha256.c +++ b/lib/crypto/sha256.c @@ -280,4 +280,14 @@ void sha224_final(struct sha256_state *sctx, u8 *out) } EXPORT_SYMBOL(sha224_final); +void sha256(const u8 *data, unsigned int len, u8 *out) +{ + struct sha256_state sctx; + + sha256_init(&sctx); + sha256_update(&sctx, data, len); + sha256_final(&sctx, out); +} +EXPORT_SYMBOL(sha256); + MODULE_LICENSE("GPL"); From patchwork Wed Jul 8 16:39:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 11651917 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 559B613B4 for ; Wed, 8 Jul 2020 16:43:24 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id DB6BD20663 for ; Wed, 8 Jul 2020 16:43:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alsa-project.org header.i=@alsa-project.org header.b="UOaL7sWT"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="qJN66Ndy" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DB6BD20663 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=alsa-devel-bounces@alsa-project.org Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 4A37A165D; Wed, 8 Jul 2020 18:42:32 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 4A37A165D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1594226599; bh=PtXTYUvhYp1TuGbsTeUDAcDwsA77DUzWQdDtmeDTULs=; h=From:To:Subject:Date:In-Reply-To:References:Cc:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=UOaL7sWTAi3QrX04yy63gH7i6JvyV31FMMZElaeEed5S2YcbxHi9IfROvjyy+hequ FvkTf3G6KWReyj1Uy5YVJZAiwYU41ezxq88Y/+5o2qHX7cj9zcvud1cPDeQwtv8jZL T/k99devNwgLmPu/cz1NCko+l0z/x0oDw6ZM7/ko= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id A4967F80269; Wed, 8 Jul 2020 18:40:51 +0200 (CEST) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa1.perex.cz (Postfix, from userid 50401) id D19EDF802A9; Wed, 8 Jul 2020 18:40:49 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on alsa1.perex.cz X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id D47D9F80150 for ; Wed, 8 Jul 2020 18:40:35 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz D47D9F80150 Authentication-Results: alsa1.perex.cz; dkim=pass (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="qJN66Ndy" 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 87BD820857; Wed, 8 Jul 2020 16:40:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1594226433; bh=PtXTYUvhYp1TuGbsTeUDAcDwsA77DUzWQdDtmeDTULs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qJN66Ndye2qFIHyiJrkgTdH/ifIhiwbe6VTAXwwXDZSots17O7ZP+1npYona5La6I E1LdabHsFPyNR8BUdckP4hJKQyONX/UDQSyqJXwe9Xnv6Y+u8NmH/n20ylLt/+KQD/ 5Sdkno5gfMnXsS5w+S5mBxoiReJ9IRpAZBydLEEk= From: Eric Biggers To: linux-crypto@vger.kernel.org, Herbert Xu Subject: [PATCH v2 5/5] ASoC: cros_ec_codec: use sha256() instead of open coding Date: Wed, 8 Jul 2020 09:39:43 -0700 Message-Id: <20200708163943.52071-6-ebiggers@kernel.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200708163943.52071-1-ebiggers@kernel.org> References: <20200708163943.52071-1-ebiggers@kernel.org> MIME-Version: 1.0 Cc: alsa-devel@alsa-project.org, Tzung-Bi Shih , Guenter Roeck , Enric Balletbo i Serra , Ard Biesheuvel , Cheng-Yi Chiang X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" From: Eric Biggers Now that there's a function that calculates the SHA-256 digest of a buffer in one step, use it instead of sha256_init() + sha256_update() + sha256_final(). Also simplify the code by inlining calculate_sha256() into its caller and switching a debug log statement to use %*phN instead of bin2hex(). Acked-by: Tzung-Bi Shih Reviewed-by: Ard Biesheuvel Cc: alsa-devel@alsa-project.org Cc: Ard Biesheuvel Cc: Cheng-Yi Chiang Cc: Enric Balletbo i Serra Cc: Guenter Roeck Cc: Tzung-Bi Shih Signed-off-by: Eric Biggers --- sound/soc/codecs/cros_ec_codec.c | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/sound/soc/codecs/cros_ec_codec.c b/sound/soc/codecs/cros_ec_codec.c index 8d45c628e988..ab009c7dfdf4 100644 --- a/sound/soc/codecs/cros_ec_codec.c +++ b/sound/soc/codecs/cros_ec_codec.c @@ -103,28 +103,6 @@ static int send_ec_host_command(struct cros_ec_device *ec_dev, uint32_t cmd, return ret; } -static int calculate_sha256(struct cros_ec_codec_priv *priv, - uint8_t *buf, uint32_t size, uint8_t *digest) -{ - struct sha256_state sctx; - - sha256_init(&sctx); - sha256_update(&sctx, buf, size); - sha256_final(&sctx, digest); - -#ifdef DEBUG - { - char digest_str[65]; - - bin2hex(digest_str, digest, 32); - digest_str[64] = 0; - dev_dbg(priv->dev, "hash=%s\n", digest_str); - } -#endif - - return 0; -} - static int dmic_get_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { @@ -782,9 +760,8 @@ static int wov_hotword_model_put(struct snd_kcontrol *kcontrol, if (IS_ERR(buf)) return PTR_ERR(buf); - ret = calculate_sha256(priv, buf, size, digest); - if (ret) - goto leave; + sha256(buf, size, digest); + dev_dbg(priv->dev, "hash=%*phN\n", SHA256_DIGEST_SIZE, digest); p.cmd = EC_CODEC_WOV_GET_LANG; ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_WOV,