Skip to contentSkip to conversation

Remove secret-size restrictions for hashes

PATCH Superseded Mark O'Donovan 2 participants 6 messages Oct 13, 2023 – Oct 16, 2023

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover Remove secret-size restrictions for hashes

This relates to the hash functions used to transform the secret. The kernel currently restricts us to using secrets equal in size to the transformation hash function they use.
e.g. 32 byte secrets with the SHA-256(32 byte) hash function.

This restriction is not required by the spec and means incompatibility with more permissive implementations.

With these patches the example secret from the spec should now be permitted with any of the following:
DHHC-1:00:ia6zGodOr4SEG0Zzaw398rpY0wqipUWj4jWjUh4HWUz6aQ2n:
DHHC-1:01:ia6zGodOr4SEG0Zzaw398rpY0wqipUWj4jWjUh4HWUz6aQ2n:
DHHC-1:02:ia6zGodOr4SEG0Zzaw398rpY0wqipUWj4jWjUh4HWUz6aQ2n:
DHHC-1:03:ia6zGodOr4SEG0Zzaw398rpY0wqipUWj4jWjUh4HWUz6aQ2n:

Note: Secrets are still restricted to 32,48 or 64 bits.

Mark O'Donovan (2):
nvme-auth: use transformed key size to create resp
nvme-auth: allow mixing of secret and hash lengths

 drivers/nvme/common/auth.c |  8 --------
 drivers/nvme/host/auth.c   | 11 ++++++++++-
 2 files changed, 10 insertions(+), 9 deletions(-)
Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/2 nvme-auth: use transformed key size to create resp

This does not change current behaviour as the driver currently verifies that the secret size is the same size as the length of the transformation hash.

---
 drivers/nvme/host/auth.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/nvme/host/auth.c +10 -1
index daf5d144a8ea..e7d478d17b06 100644
@@ -418,6 +418,14 @@ static int nvme_auth_set_dhchap_failure2_data(struct nvme_ctrl *ctrl,
 	return size;
 }
 
+static int nvme_auth_dhchap_transformed_key_len(struct nvme_dhchap_key *key)
+{
+	if (key->hash)
+		return nvme_auth_hmac_hash_len(key->hash);
+
+	return key->len;
+}
+
 static int nvme_auth_dhchap_setup_host_response(struct nvme_ctrl *ctrl,
 		struct nvme_dhchap_queue_context *chap)
 {
@@ -442,7 +450,8 @@ static int nvme_auth_dhchap_setup_host_response(struct nvme_ctrl *ctrl,
 	}
 
 	ret = crypto_shash_setkey(chap->shash_tfm,
-			chap->host_response, ctrl->host_key->len);
+			chap->host_response,
+			nvme_auth_dhchap_transformed_key_len(ctrl->host_key));
 	if (ret) {
 		dev_warn(ctrl->device, "qid %d: failed to set key, error %d\n",
 			 chap->qid, ret);
Signature · 1 line
2.39.2
maintainer
#2 lore raw

Re: [PATCH 1/2] nvme-auth: use transformed key size to create resp

Quoted 31 lines · Mark O'Donovan

This does not change current behaviour as the driver currently verifies that the secret size is the same size as the length of the transformation hash.

---
  drivers/nvme/host/auth.c | 11 ++++++++++-
  1 file changed, 10 insertions(+), 1 deletion(-)
drivers/nvme/host/auth.c +8 -0
index daf5d144a8ea..e7d478d17b06 100644
@@ -418,6 +418,14 @@ static int nvme_auth_set_dhchap_failure2_data(struct nvme_ctrl *ctrl,
  	return size;
  }
 
+static int nvme_auth_dhchap_transformed_key_len(struct nvme_dhchap_key *key)
+{
+	if (key->hash)
+		return nvme_auth_hmac_hash_len(key->hash);
+
+	return key->len;
+}
+
  static int nvme_auth_dhchap_setup_host_response(struct nvme_ctrl *ctrl,
  		struct nvme_dhchap_queue_context *chap)
  {

drivers/nvme/host/auth.c static int nvme_auth_dhchap_setup_host_response(struct nvme_ctrl *ctrl, · line 457

Quoted 9 lines
drivers/nvme/host/auth.c +2 -1
@@ -442,7 +450,8 @@ static int nvme_auth_dhchap_setup_host_response(struct nvme_ctrl *ctrl,
  	}
 
  	ret = crypto_shash_setkey(chap->shash_tfm,
-			chap->host_response, ctrl->host_key->len);
+			chap->host_response,
+			nvme_auth_dhchap_transformed_key_len(ctrl->host_key));
  	if (ret) {
  		dev_warn(ctrl->device, "qid %d: failed to set key, error %d\n",
  			 chap->qid, ret);

Hmm. Yeah, hash size vs secret size always gets me. However, wouldn't it be better to return the key size from nvme_auth_transform_key and us that directly?
(cf the attached patch)

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew
Myers, Andrew McDonald, Martje Boudien Moerman

From 14ac06a586c44ca186b5270095f551f3bdb77f18 Mon Sep 17 00:00:00 2001
From: Hannes Reinecke <[email protected]>
Date: Sat, 14 Oct 2023 13:37:31 +0200
Subject: [PATCH] nvme-auth: use length of the transformed key

The key length for generating the host response is the length of the transformed host response, which may be different from the length of host key.

---
 drivers/nvme/common/auth.c | 9 +++++++--
 drivers/nvme/host/auth.c   | 7 +++++--
 include/linux/nvme-auth.h  | 3 ++-
 3 files changed, 14 insertions(+), 5 deletions(-)
drivers/nvme/common/auth.c +7 -2
index d90e4f0c08b7..dd5f1a221f52 100644
@@ -229,7 +229,8 @@ void nvme_auth_free_key(struct nvme_dhchap_key *key)
 }
 EXPORT_SYMBOL_GPL(nvme_auth_free_key);
 
-u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn)
+u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn,
+			    size_t *transformed_len)
 {
 	const char *hmac_name;
 	struct crypto_shash *key_tfm;
@@ -243,6 +244,8 @@ u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn)
 	}
 	if (key->hash == 0) {
 		transformed_key = kmemdup(key->key, key->len, GFP_KERNEL);
+		if (transformed_key)
+			*transformed_len = key->len;
 		return transformed_key ? transformed_key : ERR_PTR(-ENOMEM);
 	}
 	hmac_name = nvme_auth_hmac_name(key->hash);
@@ -263,7 +266,8 @@ u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn)
 		goto out_free_key;
 	}
 
-	transformed_key = kzalloc(crypto_shash_digestsize(key_tfm), GFP_KERNEL);
+	*transformed_len = crypto_shash_digestsize(key_tfm);
+	transformed_key = kzalloc(*transformed_len, GFP_KERNEL);
 	if (!transformed_key) {
 		ret = -ENOMEM;
 		goto out_free_shash;
@@ -294,6 +298,7 @@ u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn)
 out_free_transformed_key:
 	kfree_sensitive(transformed_key);
 out_free_shash:
+	*transformed_len = 0;
 	kfree(shash);
 out_free_key:
 	crypto_free_shash(key_tfm);
drivers/nvme/host/auth.c +5 -2
index daf5d144a8ea..929b37502f12 100644
@@ -40,6 +40,7 @@ struct nvme_dhchap_queue_context {
 	u8 *ctrl_key;
 	u8 *host_key;
 	u8 *sess_key;
+	int host_response_len;
 	int ctrl_key_len;
 	int host_key_len;
 	int sess_key_len;
@@ -430,10 +431,12 @@ static int nvme_auth_dhchap_setup_host_response(struct nvme_ctrl *ctrl,
 
 	if (!chap->host_response) {
 		chap->host_response = nvme_auth_transform_key(ctrl->host_key,
-						ctrl->opts->host->nqn);
+						ctrl->opts->host->nqn,
+						&chap->host_response_len);
 		if (IS_ERR(chap->host_response)) {
 			ret = PTR_ERR(chap->host_response);
 			chap->host_response = NULL;
+			chap->host_response_len = 0;
 			return ret;
 		}
 	} else {
@@ -442,7 +445,7 @@ static int nvme_auth_dhchap_setup_host_response(struct nvme_ctrl *ctrl,
 	}
 
 	ret = crypto_shash_setkey(chap->shash_tfm,
-			chap->host_response, ctrl->host_key->len);
+			chap->host_response, chap->host_response_len);
 	if (ret) {
 		dev_warn(ctrl->device, "qid %d: failed to set key, error %d\n",
 			 chap->qid, ret);
include/linux/nvme-auth.h +2 -1
index dcb8030062dd..72f59632bf38 100644
@@ -27,7 +27,8 @@ u8 nvme_auth_hmac_id(const char *hmac_name);
 struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret,
 					      u8 key_hash);
 void nvme_auth_free_key(struct nvme_dhchap_key *key);
-u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn);
+u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn,
+			    size_t *transformed_len);
 int nvme_auth_generate_key(u8 *secret, struct nvme_dhchap_key **ret_key);
 int nvme_auth_augmented_challenge(u8 hmac_id, u8 *skey, size_t skey_len,
 				  u8 *challenge, u8 *aug, size_t hlen);
Signature · 1 line
2.35.3
#3 lore raw

Re: [PATCH 1/2] nvme-auth: use transformed key size to create resp

Quoted 45 lines · Hannes Reinecke
Quoted 30 lines · Mark O'Donovan

This does not change current behaviour as the driver currently verifies that the secret size is the same size as the length of the transformation hash.

---
  drivers/nvme/host/auth.c | 11 ++++++++++-

  1 file changed, 10 insertions(+), 1 deletion(-)
drivers/nvme/host/auth.c +8 -0
index daf5d144a8ea..e7d478d17b06 100644
@@ -418,6 +418,14 @@ static int nvme_auth_set_dhchap_failure2_data(struct nvme_ctrl *ctrl,
       return size;
   }
+static int nvme_auth_dhchap_transformed_key_len(struct nvme_dhchap_key *key)
+{
+    if (key->hash)
+        return nvme_auth_hmac_hash_len(key->hash);
+
+    return key->len;
+}
+
   static int nvme_auth_dhchap_setup_host_response(struct nvme_ctrl *ctrl,
           struct nvme_dhchap_queue_context *chap)
   {

drivers/nvme/host/auth.c static int nvme_auth_dhchap_setup_host_response(struct nvme_ctrl *ctrl, · line 456

Quoted 8 lines
drivers/nvme/host/auth.c +2 -1
@@ -442,7 +450,8 @@ static int nvme_auth_dhchap_setup_host_response(struct nvme_ctrl *ctrl,
       }
       ret = crypto_shash_setkey(chap->shash_tfm,
-            chap->host_response, ctrl->host_key->len);
+            chap->host_response,
+            nvme_auth_dhchap_transformed_key_len(ctrl->host_key));
       if (ret) {
           dev_warn(ctrl->device, "qid %d: failed to set key, error %d\n",
                chap->qid, ret);

Hmm. Yeah, hash size vs secret size always gets me. However, wouldn't it be better to return the key size from nvme_auth_transform_key and us that directly?
(cf the attached patch)

Quoted 3 lines

Cheers,

Hannes

Hi Hannes,

I gave this a try and it ended up being easier to put it in struct nvme_dhchap_key. V2 also does the nvme target code, and this means the length is stored in the same place. Let me know if this works for you.

Thanks,
Mark

#4 lore raw

PATCH 2/2 nvme-auth: allow mixing of secret and hash lengths

In reply to Mark O'Donovan

We can now use any of the secret transformation hashes with a secret, regardless of the secret size.
e.g. a 32 byte key with the SHA-512(64 byte) hash.

The example secret from the spec should now be permitted with any of the following:
DHHC-1:00:ia6zGodOr4SEG0Zzaw398rpY0wqipUWj4jWjUh4HWUz6aQ2n:
DHHC-1:01:ia6zGodOr4SEG0Zzaw398rpY0wqipUWj4jWjUh4HWUz6aQ2n:
DHHC-1:02:ia6zGodOr4SEG0Zzaw398rpY0wqipUWj4jWjUh4HWUz6aQ2n:
DHHC-1:03:ia6zGodOr4SEG0Zzaw398rpY0wqipUWj4jWjUh4HWUz6aQ2n:

Note: Secrets are still restricted to 32,48 or 64 bits.

---
 drivers/nvme/common/auth.c | 8 --------
 1 file changed, 8 deletions(-)
drivers/nvme/common/auth.c +0 -8
index d90e4f0c08b7..176855f86f0d 100644
@@ -187,14 +187,6 @@ struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret,
 		goto out_free_secret;
 	}
 
-	if (key_hash > 0 &&
-	    (key_len - 4) != nvme_auth_hmac_hash_len(key_hash)) {
-		pr_err("Mismatched key len %d for %s\n", key_len,
-		       nvme_auth_hmac_name(key_hash));
-		ret = -EINVAL;
-		goto out_free_secret;
-	}
-
 	/* The last four bytes is the CRC in little-endian format */
 	key_len -= 4;
 	/*
Signature · 1 line
2.39.2
maintainer Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] nvme-auth: allow mixing of secret and hash lengths

Quoted 29 lines · Mark O'Donovan

We can now use any of the secret transformation hashes with a secret, regardless of the secret size.
e.g. a 32 byte key with the SHA-512(64 byte) hash.

The example secret from the spec should now be permitted with any of the following:
DHHC-1:00:ia6zGodOr4SEG0Zzaw398rpY0wqipUWj4jWjUh4HWUz6aQ2n:
DHHC-1:01:ia6zGodOr4SEG0Zzaw398rpY0wqipUWj4jWjUh4HWUz6aQ2n:
DHHC-1:02:ia6zGodOr4SEG0Zzaw398rpY0wqipUWj4jWjUh4HWUz6aQ2n:
DHHC-1:03:ia6zGodOr4SEG0Zzaw398rpY0wqipUWj4jWjUh4HWUz6aQ2n:

Note: Secrets are still restricted to 32,48 or 64 bits.

---
  drivers/nvme/common/auth.c | 8 --------
  1 file changed, 8 deletions(-)
drivers/nvme/common/auth.c +0 -1
index d90e4f0c08b7..176855f86f0d 100644
@@ -187,14 +187,6 @@ struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret,
  		goto out_free_secret;
  	}
 
-	if (key_hash > 0 &&

drivers/nvme/common/auth.c struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret, · line 192

Quoted 10 lines
drivers/nvme/common/auth.c +0 -7
-	    (key_len - 4) != nvme_auth_hmac_hash_len(key_hash)) {
-		pr_err("Mismatched key len %d for %s\n", key_len,
-		       nvme_auth_hmac_name(key_hash));
-		ret = -EINVAL;
-		goto out_free_secret;
-	}
-
  	/* The last four bytes is the CRC in little-endian format */
  	key_len -= 4;
  	/*

That, however, looks good.

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew
Myers, Andrew McDonald, Martje Boudien Moerman