2023-10-29 05:02:14

by Eric Biggers

[permalink] [raw]
Subject: [PATCH] nvme-auth: use crypto_shash_tfm_digest()

From: Eric Biggers <[email protected]>

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 <[email protected]>
---
drivers/nvme/common/auth.c | 23 ++---------------------
1 file changed, 2 insertions(+), 21 deletions(-)

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)
{

base-commit: 2af9b20dbb39f6ebf9b9b6c090271594627d818e
--
2.42.0


2023-10-30 13:28:29

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH] nvme-auth: use crypto_shash_tfm_digest()

On Sat, Oct 28, 2023 at 10:00:40PM -0700, Eric Biggers wrote:
> From: Eric Biggers <[email protected]>
>
> 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 <[email protected]>

2023-10-30 15:03:21

by Keith Busch

[permalink] [raw]
Subject: Re: [PATCH] nvme-auth: use crypto_shash_tfm_digest()

On Sat, Oct 28, 2023 at 10:00:40PM -0700, Eric Biggers wrote:
> From: Eric Biggers <[email protected]>
>
> 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 <[email protected]>

Thanks, applied to nvme-6.7.

2023-11-20 14:40:54

by Sagi Grimberg

[permalink] [raw]
Subject: Re: [PATCH] nvme-auth: use crypto_shash_tfm_digest()

>> From: Eric Biggers <[email protected]>
>>
>> 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 <[email protected]>

Indeed. fwiw:
Reviewed-by: Sagi Grimberg <[email protected]>