]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
nvme-auth: host: use crypto library in nvme_auth_dhchap_setup_ctrl_response()
authorEric Biggers <ebiggers@kernel.org>
Mon, 2 Mar 2026 07:59:52 +0000 (23:59 -0800)
committerKeith Busch <kbusch@kernel.org>
Fri, 27 Mar 2026 14:35:02 +0000 (07:35 -0700)
For the HMAC computation in nvme_auth_dhchap_setup_ctrl_response(), use
the crypto library instead of crypto_shash.  This is simpler, faster,
and more reliable.

Acked-by: Ard Biesheuvel <ardb@kernel.org>
Acked-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
Signed-off-by: Keith Busch <kbusch@kernel.org>
drivers/nvme/host/auth.c

index f22f17ad7e2f4868b8485a15f6963c5229dc9698..2f27f550a7442cefdd13e856ef387245f02400e8 100644 (file)
@@ -504,7 +504,7 @@ out:
 static int nvme_auth_dhchap_setup_ctrl_response(struct nvme_ctrl *ctrl,
                struct nvme_dhchap_queue_context *chap)
 {
-       SHASH_DESC_ON_STACK(shash, chap->shash_tfm);
+       struct nvme_auth_hmac_ctx hmac;
        struct nvme_dhchap_key *transformed_key;
        u8 buf[4], *challenge = chap->c2;
        int ret;
@@ -516,10 +516,10 @@ static int nvme_auth_dhchap_setup_ctrl_response(struct nvme_ctrl *ctrl,
                return ret;
        }
 
-       ret = crypto_shash_setkey(chap->shash_tfm,
-                       transformed_key->key, transformed_key->len);
+       ret = nvme_auth_hmac_init(&hmac, chap->hash_id, transformed_key->key,
+                                 transformed_key->len);
        if (ret) {
-               dev_warn(ctrl->device, "qid %d: failed to set key, error %d\n",
+               dev_warn(ctrl->device, "qid %d: failed to init hmac, error %d\n",
                         chap->qid, ret);
                goto out;
        }
@@ -546,43 +546,29 @@ static int nvme_auth_dhchap_setup_ctrl_response(struct nvme_ctrl *ctrl,
                __func__, chap->qid, ctrl->opts->subsysnqn);
        dev_dbg(ctrl->device, "%s: qid %d hostnqn %s\n",
                __func__, chap->qid, ctrl->opts->host->nqn);
-       shash->tfm = chap->shash_tfm;
-       ret = crypto_shash_init(shash);
-       if (ret)
-               goto out;
-       ret = crypto_shash_update(shash, challenge, chap->hash_len);
-       if (ret)
-               goto out;
+
+       nvme_auth_hmac_update(&hmac, challenge, chap->hash_len);
+
        put_unaligned_le32(chap->s2, buf);
-       ret = crypto_shash_update(shash, buf, 4);
-       if (ret)
-               goto out;
+       nvme_auth_hmac_update(&hmac, buf, 4);
+
        put_unaligned_le16(chap->transaction, buf);
-       ret = crypto_shash_update(shash, buf, 2);
-       if (ret)
-               goto out;
+       nvme_auth_hmac_update(&hmac, buf, 2);
+
        memset(buf, 0, 4);
-       ret = crypto_shash_update(shash, buf, 1);
-       if (ret)
-               goto out;
-       ret = crypto_shash_update(shash, "Controller", 10);
-       if (ret)
-               goto out;
-       ret = crypto_shash_update(shash, ctrl->opts->subsysnqn,
-                                 strlen(ctrl->opts->subsysnqn));
-       if (ret)
-               goto out;
-       ret = crypto_shash_update(shash, buf, 1);
-       if (ret)
-               goto out;
-       ret = crypto_shash_update(shash, ctrl->opts->host->nqn,
-                                 strlen(ctrl->opts->host->nqn));
-       if (ret)
-               goto out;
-       ret = crypto_shash_final(shash, chap->response);
+       nvme_auth_hmac_update(&hmac, buf, 1);
+       nvme_auth_hmac_update(&hmac, "Controller", 10);
+       nvme_auth_hmac_update(&hmac, ctrl->opts->subsysnqn,
+                             strlen(ctrl->opts->subsysnqn));
+       nvme_auth_hmac_update(&hmac, buf, 1);
+       nvme_auth_hmac_update(&hmac, ctrl->opts->host->nqn,
+                             strlen(ctrl->opts->host->nqn));
+       nvme_auth_hmac_final(&hmac, chap->response);
+       ret = 0;
 out:
        if (challenge != chap->c2)
                kfree(challenge);
+       memzero_explicit(&hmac, sizeof(hmac));
        nvme_auth_free_key(transformed_key);
        return ret;
 }