[PATCH v2 0/5] crypto: add sha256() function
This series adds a function sha256() to the sha256 library so that users who want to compute a hash in one step can just call sha256() instead of sha256_init() + sha256_update() + sha256_final().
Patches 3-5 then convert some users to use it.
Changed v1 => v2: - Added sparc patch to fix a build breakage caused by a static variable already named "sha256". - Added Reviewed-by, Acked-by, and Tested-by tags.
Eric Biggers (5): crypto: sparc - rename sha256 to sha256_alg crypto: lib/sha256 - add sha256() function efi: use sha256() instead of open coding mptcp: use sha256() instead of open coding ASoC: cros_ec_codec: use sha256() instead of open coding
arch/sparc/crypto/sha256_glue.c | 14 ++++++------ drivers/firmware/efi/embedded-firmware.c | 9 +++----- include/crypto/sha.h | 1 + lib/crypto/sha256.c | 10 +++++++++ net/mptcp/crypto.c | 15 +++---------- sound/soc/codecs/cros_ec_codec.c | 27 ++---------------------- 6 files changed, 26 insertions(+), 50 deletions(-)
base-commit: 57c8aa43b9f272c382c253573c82be5cb68fe22d
From: Eric Biggers ebiggers@google.com
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 ardb@kernel.org Tested-by: Hans de Goede hdegoede@redhat.com Signed-off-by: Eric Biggers ebiggers@google.com --- 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: Eric Biggers ebiggers@google.com
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 tzungbi@google.com Reviewed-by: Ard Biesheuvel ardb@kernel.org Cc: alsa-devel@alsa-project.org Cc: Ard Biesheuvel ardb@kernel.org Cc: Cheng-Yi Chiang cychiang@chromium.org Cc: Enric Balletbo i Serra enric.balletbo@collabora.com Cc: Guenter Roeck groeck@chromium.org Cc: Tzung-Bi Shih tzungbi@google.com Signed-off-by: Eric Biggers ebiggers@google.com --- 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,
On Wed, Jul 08, 2020 at 09:39:38AM -0700, Eric Biggers wrote:
This series adds a function sha256() to the sha256 library so that users who want to compute a hash in one step can just call sha256() instead of sha256_init() + sha256_update() + sha256_final().
Patches 3-5 then convert some users to use it.
Changed v1 => v2:
- Added sparc patch to fix a build breakage caused by a static variable already named "sha256".
- Added Reviewed-by, Acked-by, and Tested-by tags.
Eric Biggers (5): crypto: sparc - rename sha256 to sha256_alg crypto: lib/sha256 - add sha256() function efi: use sha256() instead of open coding mptcp: use sha256() instead of open coding ASoC: cros_ec_codec: use sha256() instead of open coding
arch/sparc/crypto/sha256_glue.c | 14 ++++++------ drivers/firmware/efi/embedded-firmware.c | 9 +++----- include/crypto/sha.h | 1 + lib/crypto/sha256.c | 10 +++++++++ net/mptcp/crypto.c | 15 +++---------- sound/soc/codecs/cros_ec_codec.c | 27 ++---------------------- 6 files changed, 26 insertions(+), 50 deletions(-)
All applied. Thanks.
participants (2)
-
Eric Biggers
-
Herbert Xu