diff mbox series

nvme-auth: use crypto_shash_tfm_digest()

Message ID 20231029050040.154563-1-ebiggers@kernel.org (mailing list archive)
State Not Applicable
Delegated to: Herbert Xu
Headers show
Series nvme-auth: use crypto_shash_tfm_digest() | expand

Commit Message

Eric Biggers Oct. 29, 2023, 5 a.m. UTC
From: Eric Biggers <ebiggers@google.com>

Simplify nvme_auth_augmented_challenge() by using
crypto_shash_tfm_digest() instead of an alloc+init+update+final
sequence.  This should also improve performance.

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 drivers/nvme/common/auth.c | 23 ++---------------------
 1 file changed, 2 insertions(+), 21 deletions(-)


base-commit: 2af9b20dbb39f6ebf9b9b6c090271594627d818e

Comments

Christoph Hellwig Oct. 30, 2023, 1:28 p.m. UTC | #1
On Sat, Oct 28, 2023 at 10:00:40PM -0700, Eric Biggers wrote:
> From: Eric Biggers <ebiggers@google.com>
> 
> Simplify nvme_auth_augmented_challenge() by using
> crypto_shash_tfm_digest() instead of an alloc+init+update+final
> sequence.  This should also improve performance.

Nice:

Reviewed-by: Christoph Hellwig <hch@lst.de>
Keith Busch Oct. 30, 2023, 3:03 p.m. UTC | #2
On Sat, Oct 28, 2023 at 10:00:40PM -0700, Eric Biggers wrote:
> From: Eric Biggers <ebiggers@google.com>
> 
> Simplify nvme_auth_augmented_challenge() by using
> crypto_shash_tfm_digest() instead of an alloc+init+update+final
> sequence.  This should also improve performance.
> 
> Signed-off-by: Eric Biggers <ebiggers@google.com>

Thanks, applied to nvme-6.7.
Sagi Grimberg Nov. 20, 2023, 1:51 p.m. UTC | #3
>> From: Eric Biggers <ebiggers@google.com>
>>
>> Simplify nvme_auth_augmented_challenge() by using
>> crypto_shash_tfm_digest() instead of an alloc+init+update+final
>> sequence.  This should also improve performance.
> 
> Nice:
> 
> Reviewed-by: Christoph Hellwig <hch@lst.de>

Indeed. fwiw:
Reviewed-by: Sagi Grimberg <sagi@grimberg.me>
diff mbox series

Patch

diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c
index d90e4f0c08b7..54cffbc24b4a 100644
--- a/drivers/nvme/common/auth.c
+++ b/drivers/nvme/common/auth.c
@@ -324,21 +324,20 @@  static int nvme_auth_hash_skey(int hmac_id, u8 *skey, size_t skey_len, u8 *hkey)
 			 skey_len);
 
 	crypto_free_shash(tfm);
 	return ret;
 }
 
 int nvme_auth_augmented_challenge(u8 hmac_id, u8 *skey, size_t skey_len,
 		u8 *challenge, u8 *aug, size_t hlen)
 {
 	struct crypto_shash *tfm;
-	struct shash_desc *desc;
 	u8 *hashed_key;
 	const char *hmac_name;
 	int ret;
 
 	hashed_key = kmalloc(hlen, GFP_KERNEL);
 	if (!hashed_key)
 		return -ENOMEM;
 
 	ret = nvme_auth_hash_skey(hmac_id, skey,
 				  skey_len, hashed_key);
@@ -352,43 +351,25 @@  int nvme_auth_augmented_challenge(u8 hmac_id, u8 *skey, size_t skey_len,
 		ret = -EINVAL;
 		goto out_free_key;
 	}
 
 	tfm = crypto_alloc_shash(hmac_name, 0, 0);
 	if (IS_ERR(tfm)) {
 		ret = PTR_ERR(tfm);
 		goto out_free_key;
 	}
 
-	desc = kmalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
-		       GFP_KERNEL);
-	if (!desc) {
-		ret = -ENOMEM;
-		goto out_free_hash;
-	}
-	desc->tfm = tfm;
-
 	ret = crypto_shash_setkey(tfm, hashed_key, hlen);
 	if (ret)
-		goto out_free_desc;
-
-	ret = crypto_shash_init(desc);
-	if (ret)
-		goto out_free_desc;
-
-	ret = crypto_shash_update(desc, challenge, hlen);
-	if (ret)
-		goto out_free_desc;
+		goto out_free_hash;
 
-	ret = crypto_shash_final(desc, aug);
-out_free_desc:
-	kfree_sensitive(desc);
+	ret = crypto_shash_tfm_digest(tfm, challenge, hlen, aug);
 out_free_hash:
 	crypto_free_shash(tfm);
 out_free_key:
 	kfree_sensitive(hashed_key);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(nvme_auth_augmented_challenge);
 
 int nvme_auth_gen_privkey(struct crypto_kpp *dh_tfm, u8 dh_gid)
 {