crypto: add sha256() function
Subsystems (10)
Eric Biggers
maintainerv2 cover 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
Signature · 1 line
2.27.0
Eric Biggers
maintainerPATCH v2 1/5 crypto: sparc - rename sha256 to sha256_alg
From: Eric Biggers <[email protected]>
To avoid a naming collision when we add a sha256() library function, rename the "sha256" static variable in sha256_glue.c to "sha256_alg". For consistency, also rename "sha224" to "sha224_alg".
--- arch/sparc/crypto/sha256_glue.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
arch/sparc/crypto/sha256_glue.c +7 -7
@@ -156,7 +156,7 @@ static int sha256_sparc64_import(struct shash_desc *desc, const void *in)return 0 ; } -static struct shash_alg sha256 = { +static struct shash_alg sha256_alg = { .digestsize =SHA256_DIGEST_SIZE , .init = sha256_sparc64_init, .update = sha256_sparc64_update,
@@ -174,7 +174,7 @@ static struct shash_alg sha256 = { } }; -static struct shash_alg sha224 = { +static struct shash_alg sha224_alg = { .digestsize =SHA224_DIGEST_SIZE , .init = sha224_sparc64_init, .update = sha256_sparc64_update,
@@ -206,13 +206,13 @@ static bool __init sparc64_has_sha256_opcode(void)static int __init sha256_sparc64_mod_init (void ) {if (sparc64_has_sha256_opcode ()) { -int ret =crypto_register_shash (&sha224); +int ret =crypto_register_shash (&sha224_alg);if (ret <0 )return ret; - ret =crypto_register_shash (&sha256); + ret =crypto_register_shash (&sha256_alg);if (ret <0 ) { -crypto_unregister_shash (&sha224); +crypto_unregister_shash (&sha224_alg);return ret; }
@@ -225,8 +225,8 @@ static int __init sha256_sparc64_mod_init(void) static void __exit sha256_sparc64_mod_fini(void) { -crypto_unregister_shash (&sha224); -crypto_unregister_shash (&sha256); +crypto_unregister_shash (&sha224_alg); +crypto_unregister_shash (&sha256_alg); }module_init (sha256_sparc64_mod_init);
Signature · 1 line
2.27.0
Eric Biggers
maintainerPATCH v2 2/5 crypto: lib/sha256 - add sha256() function
In reply to Eric BiggersFrom: Eric Biggers <ebiggers(a)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().
--- include/crypto/sha.h | 1 + lib/crypto/sha256.c | 10 ++++++++++ 2 files changed, 11 insertions(+)
include/crypto/sha.h +1 -0
@@ -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) {
lib/crypto/sha256.c +10 -0
@@ -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" );
Signature · 1 line
2.27.0
Eric Biggers
maintainerPATCH v2 3/5 efi: use sha256() instead of open coding
In reply to Eric BiggersFrom: 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().
--- drivers/firmware/efi/embedded-firmware.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/firmware/efi/embedded-firmware.c +3 -6
@@ -37,9 +37,8 @@ static const struct dmi_system_id * const embedded_fw_table[] = {static int __init efi_check_md_for_embedded_firmware (efi_memory_desc_t *md,const struct efi_embedded_fw_desc *desc) { -struct sha256_state sctx;struct efi_embedded_fw *fw; -u8 sha256[32 ]; +u8 hash[32 ];u64 i, size;u8 *map;
@@ -54,10 +53,8 @@ static int __init efi_check_md_for_embedded_firmware(if (memcmp (map + i, desc->prefix,EFI_EMBEDDED_FW_PREFIX_LEN ))continue ; -sha256_init (&sctx); -sha256_update (&sctx, map + i, desc->length); -sha256_final (&sctx, sha256); -if (memcmp (sha256, desc->sha256,32 ) ==0 ) +sha256 (map + i, desc->length, hash); +if (memcmp (hash, desc->sha256,32 ) ==0 )break ; }if ((i + desc->length) > size) {
Signature · 1 line
2.27.0
Eric Biggers
maintainerPATCH v2 4/5 mptcp: use sha256() instead of open coding
In reply to Eric BiggersFrom: Eric Biggers <ebiggers(a)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().
--- net/mptcp/crypto.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-)
net/mptcp/crypto.c +3 -12
@@ -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
Signature · 1 line
2.27.0
Eric Biggers
maintainerPATCH v2 5/5 ASoC: cros_ec_codec: use sha256() instead of open coding
In reply to Eric BiggersFrom: 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().
--- sound/soc/codecs/cros_ec_codec.c | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-)
sound/soc/codecs/cros_ec_codec.c +2 -25
@@ -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,
Signature · 1 line
2.27.0
Herbert Xu
maintainerQuoted 18 lines · Eric Biggers
This series adds a function
sha256()to the sha256 library so that users who want to compute a hash in one step can just callsha256()instead ofsha256_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 tosha256_alg
crypto: lib/sha256 - addsha256()function
efi: usesha256()instead of open coding
mptcp: usesha256()instead of open coding
ASoC:cros_ec_codec: usesha256()instead of open coding
Quoted 7 lines
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.
Signature · 3 lines
Email: Herbert Xu <herbert(a)gondor.apana.org.au> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt