2020-07-07 19:00:23

by Eric Biggers

[permalink] [raw]
Subject: [PATCH 0/4] 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 2-4 then convert some users to use it.

Eric Biggers (4):
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

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 ++----------------------
5 files changed, 19 insertions(+), 43 deletions(-)


base-commit: 57c8aa43b9f272c382c253573c82be5cb68fe22d
--
2.27.0


2020-07-07 19:00:24

by Eric Biggers

[permalink] [raw]
Subject: [PATCH 3/4] mptcp: use sha256() instead of open coding

From: Eric Biggers <[email protected]>

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().

Cc: [email protected]
Cc: Mat Martineau <[email protected]>
Cc: Matthieu Baerts <[email protected]>
Signed-off-by: Eric Biggers <[email protected]>
---
net/mptcp/crypto.c | 15 +++------------
1 file changed, 3 insertions(+), 12 deletions(-)

diff --git a/net/mptcp/crypto.c b/net/mptcp/crypto.c
index 3d980713a9e2..82bd2b54d741 100644
--- a/net/mptcp/crypto.c
+++ b/net/mptcp/crypto.c
@@ -32,11 +32,8 @@ void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn)
{
__be32 mptcp_hashed_key[SHA256_DIGEST_WORDS];
__be64 input = cpu_to_be64(key);
- struct sha256_state state;

- sha256_init(&state);
- sha256_update(&state, (__force u8 *)&input, sizeof(input));
- sha256_final(&state, (u8 *)mptcp_hashed_key);
+ sha256((__force u8 *)&input, sizeof(input), (u8 *)mptcp_hashed_key);

if (token)
*token = be32_to_cpu(mptcp_hashed_key[0]);
@@ -47,7 +44,6 @@ void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn)
void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac)
{
u8 input[SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE];
- struct sha256_state state;
u8 key1be[8];
u8 key2be[8];
int i;
@@ -67,13 +63,10 @@ void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac)

memcpy(&input[SHA256_BLOCK_SIZE], msg, len);

- sha256_init(&state);
- sha256_update(&state, input, SHA256_BLOCK_SIZE + len);
-
/* emit sha256(K1 || msg) on the second input block, so we can
* reuse 'input' for the last hashing
*/
- sha256_final(&state, &input[SHA256_BLOCK_SIZE]);
+ sha256(input, SHA256_BLOCK_SIZE + len, &input[SHA256_BLOCK_SIZE]);

/* Prepare second part of hmac */
memset(input, 0x5C, SHA256_BLOCK_SIZE);
@@ -82,9 +75,7 @@ void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac)
for (i = 0; i < 8; i++)
input[i + 8] ^= key2be[i];

- sha256_init(&state);
- sha256_update(&state, input, SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE);
- sha256_final(&state, (u8 *)hmac);
+ sha256(input, SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE, hmac);
}

#ifdef CONFIG_MPTCP_HMAC_TEST
--
2.27.0

2020-07-07 19:00:25

by Eric Biggers

[permalink] [raw]
Subject: [PATCH 1/4] crypto: lib/sha256 - add sha256() function

From: Eric Biggers <[email protected]>

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().

Signed-off-by: Eric Biggers <[email protected]>
---
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");
--
2.27.0

2020-07-07 19:00:26

by Eric Biggers

[permalink] [raw]
Subject: [PATCH 4/4] ASoC: cros_ec_codec: use sha256() instead of open coding

From: Eric Biggers <[email protected]>

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().

Cc: [email protected]
Cc: Ard Biesheuvel <[email protected]>
Cc: Cheng-Yi Chiang <[email protected]>
Cc: Enric Balletbo i Serra <[email protected]>
Cc: Guenter Roeck <[email protected]>
Cc: Tzung-Bi Shih <[email protected]>
Signed-off-by: Eric Biggers <[email protected]>
---
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,
--
2.27.0

2020-07-08 03:10:28

by Tzung-Bi Shih

[permalink] [raw]
Subject: Re: [PATCH 4/4] ASoC: cros_ec_codec: use sha256() instead of open coding

On Wed, Jul 8, 2020 at 2:59 AM Eric Biggers <[email protected]> wrote:
>
> From: Eric Biggers <[email protected]>
>
> 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().
>
> Cc: [email protected]
> Cc: Ard Biesheuvel <[email protected]>
> Cc: Cheng-Yi Chiang <[email protected]>
> Cc: Enric Balletbo i Serra <[email protected]>
> Cc: Guenter Roeck <[email protected]>
> Cc: Tzung-Bi Shih <[email protected]>
> Signed-off-by: Eric Biggers <[email protected]>

Acked-by: Tzung-Bi Shih <[email protected]>

2020-07-08 05:49:46

by Ard Biesheuvel

[permalink] [raw]
Subject: Re: [PATCH 0/4] crypto: add sha256() function

On Tue, 7 Jul 2020 at 21:59, Eric Biggers <[email protected]> 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 2-4 then convert some users to use it.
>
> Eric Biggers (4):
> 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
>

For the series,

Reviewed-by: Ard Biesheuvel <[email protected]>

Feel free to take the EFI patch through the crypto tree.


> 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 ++----------------------
> 5 files changed, 19 insertions(+), 43 deletions(-)
>
>
> base-commit: 57c8aa43b9f272c382c253573c82be5cb68fe22d
> --
> 2.27.0
>

2020-07-08 11:02:44

by Hans de Goede

[permalink] [raw]
Subject: Re: [PATCH 0/4] crypto: add sha256() function

Hi,

On 7/7/20 8:58 PM, 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 2-4 then convert some users to use it.
>
> Eric Biggers (4):
> 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
>
> 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 ++----------------------
> 5 files changed, 19 insertions(+), 43 deletions(-)
>
>
> base-commit: 57c8aa43b9f272c382c253573c82be5cb68fe22d

I've done some quick tests on this series to make sure that
the efi embedded-firmware support did not regress.
That still works fine, so this series is;

Tested-by: Hans de Goede <[email protected]>

Regards,

Hans

2020-07-08 11:24:19

by Matthieu Baerts

[permalink] [raw]
Subject: Re: [PATCH 3/4] mptcp: use sha256() instead of open coding

Hi Eric,

On 07/07/2020 20:58, Eric Biggers wrote:
> From: Eric Biggers <[email protected]>
>
> 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().
>
> Cc: [email protected]
> Cc: Mat Martineau <[email protected]>
> Cc: Matthieu Baerts <[email protected]>
> Signed-off-by: Eric Biggers <[email protected]>

Thank you for this simplification and update MPTCP code, it's clearer!

Acked-by: Matthieu Baerts <[email protected]>

Cheers,
Matt
--
Tessares | Belgium | Hybrid Access Solutions
http://www.tessares.net