nvme: In-band authentication support
Subsystems (5)
Hannes Reinecke
maintainerRe: [PATCH 09/11] nvmet: Implement basic In-Band Authentication
In reply to Stephan MüllerQuoted 276 lines · Stephan Müller
Am Sonntag, 18. Juli 2021, 14:37:34 CEST schrieb Hannes Reinecke:
Hi Hannes,
Quoted 258 lines · Stephan Müller
Quoted 133 lines
Am Freitag, 16. Juli 2021, 13:04:26 CEST schrieb Hannes Reinecke:
Hi Hannes,
drivers/nvme/target/Makefile nvmet-y += core.o configfs.o admin-cmd.o
Quoted 48 lines
Implement support for NVMe-oF In-Band authentication. This patch adds two additional configfs entries '
dhchap_key' and 'dhchap_hash' to the 'host' configfs directory. The 'dhchap_key' needs to be specified in the format outlined in the base spec. Augmented challenge support is not implemented, and concatenation with TLS encryption is not supported.Signed-off-by Hannes Reinecke <[email protected]>---
drivers/nvme/target/Kconfig | 10 + drivers/nvme/target/Makefile | 1 + drivers/nvme/target/admin-cmd.c | 4 + drivers/nvme/target/auth.c | 352 +++++++++++++++++++ drivers/nvme/target/configfs.c | 71 +++- drivers/nvme/target/core.c | 8 + drivers/nvme/target/fabrics-cmd-auth.c | 460 +++++++++++++++++++++++++ drivers/nvme/target/fabrics-cmd.c | 30 +- drivers/nvme/target/nvmet.h | 71 ++++ 9 files changed, 1004 insertions(+), 3 deletions(-)create mode 100644
drivers/nvme/target/auth.ccreate mode 100644drivers/nvme/target/fabrics-cmd-auth.cdrivers/nvme/target/Kconfig +10
-0@@ -85,3 +85,13 @@ config NVME_TARGET_TCP devices over TCP. If unsure, say N. + +config NVME_TARGET_AUTH + bool "NVMe over Fabrics In-band Authentication support" + depends on NVME_TARGET + select CRYPTO_SHA256 + select CRYPTO_SHA512 + help + This enables support for NVMe over Fabrics In-band Authentication + + If unsure, say N.drivers/nvme/target/Makefile +0
-0@@ -13,6 +13,7 @@ nvmet-y += core.o configfs.o admin-cmd.ofabrics-cmd.o \
Quoted 78 lines
drivers/nvme/target/Makefile +1
-0discovery.o io-cmd-file.o io-cmd-bdev.o nvmet-$(CONFIG_NVME_TARGET_PASSTHRU) += passthru.o nvmet-$(CONFIG_BLK_DEV_ZONED) += zns.o +nvmet-$(CONFIG_NVME_TARGET_AUTH) += fabrics-cmd-auth.o auth.onvme-loop-y += loop.o
nvmet-rdma-y += rdma.o
nvmet-fc-y += fc.o(quoted diff) +0
-0
b/drivers/nvme/target/admin-cmd.cindex0cb98f2bbc8c..320cefc64ee0100644drivers/nvme/target/admin-cmd.c +4
-0@@ -1008,6 +1008,10 @@ u16 nvmet_parse_admin_cmd(struct nvmet_req *req)if (nvme_is_fabrics (cmd))return nvmet_parse_fabrics_cmd (req); + +if (unlikely (!nvmet_check_auth_status (req))) +return NVME_SC_AUTH_REQUIRED |NVME_SC_DNR ; +if (nvmet_req_subsys(req)->type ==
NVME_NQN_DISC)return nvmet_parse_discovery_cmd(req);drivers/nvme/target/auth.c new file +42
-0@@ -0,0 +1,352 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* NVMe over Fabrics DH-HMAC-CHAP authentication. +* Copyright (c) 2020 Hannes Reinecke, SUSE Software Solutions. +* All rights reserved. +*/ +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <linux/module.h> +#include <linux/init.h> +#include <linux/slab.h> +#include <linux/err.h> +#include <crypto/hash.h> +#include <crypto/kpp.h> +#include <crypto/dh.h> +#include <crypto/ffdhe.h> +#include <linux/crc32.h> +#include <linux/base64.h> +#include <linux/ctype.h> +#include <linux/random.h> +#include <asm/unaligned.h> + +#include "nvmet.h" +#include "../host/auth.h" + +int nvmet_auth_set_host_key (struct nvmet_host *host,const char *secret) +{ +if (sscanf (secret,"DHHC-1:%hhd:%*s" , &host->dhchap_key_hash) !=1 ) +return -EINVAL ; +if (host->dhchap_key_hash >3 ) { +pr_warn ("Invalid DH-HMAC-CHAP hash id %d\n" , + host->dhchap_key_hash); +return -EINVAL ; + } +if (host->dhchap_key_hash >0 ) { +/* Validate selected hash algorithm */ +const char *hmac =nvme_auth_hmac_name (host->dhchap_key_hash); + +if (!crypto_has_shash (hmac,0 ,0 )) { +pr_warn ("DH-HMAC-CHAP hash %s unsupported\n" , hmac); + host->dhchap_key_hash =-1 ; +return -EAGAIN ; + }Quoted 8 lines
drivers/nvme/target/auth.c line 47
Quoted 5 lines
drivers/nvme/target/auth.c new file +5
-0+/* Use this hash as default */ +if (!host->dhchap_hash_id) + host->dhchap_hash_id = host->dhchap_key_hash; + } + host->dhchap_secret =kstrdup (secret,GFP_KERNEL );Just like before - are you sure that the secret is an ASCII string and no binary blob?
That is ensured by the transport encoding (cf NVMe Base Specification version 2.0). Also, this information is being passed in via the configfs interface, so it's bounded by
PAGE_SIZE. But yes, we should be inserting a terminating 'NULL' character at the end of the page to ensure we don't incur an buffer overrun. Any other failure will be checked for during base64 decoding.Quoted 93 lines
Quoted 93 lines
drivers/nvme/target/auth.c new file +93
-0+if (!host->dhchap_secret) +return -ENOMEM ; +/* Default to SHA256 */ +if (!host->dhchap_hash_id) + host->dhchap_hash_id =NVME_AUTH_DHCHAP_HASH_SHA256 ; + +pr_debug ("Using hash %s\n" , +nvme_auth_hmac_name (host->dhchap_hash_id)); +return 0 ; +} + +int nvmet_setup_dhgroup (struct nvmet_ctrl *ctrl,int dhgroup_id) +{ +int ret = -ENOTSUPP ; + +if (dhgroup_id ==NVME_AUTH_DHCHAP_DHGROUP_NULL ) +return 0 ; + +return ret; +} + +int nvmet_setup_auth (struct nvmet_ctrl *ctrl,struct nvmet_req *req) +{ +int ret =0 ; +struct nvmet_host_link *p; +struct nvmet_host *host =NULL ; +const char *hash_name; + +down_read (&nvmet_config_sem); +if (ctrl->subsys->type ==NVME_NQN_DISC ) + goto out_unlock; + +list_for_each_entry (p, &ctrl->subsys->hosts, entry) { +pr_debug ("check %s\n" ,nvmet_host_name (p->host)); +if (strcmp (nvmet_host_name (p->host), ctrl->hostnqn)) +continue ; + host = p->host; +break ; + } +if (!host) { +pr_debug ("host %s not found\n" , ctrl->hostnqn); + ret = -EPERM ; + goto out_unlock; + } +if (!host->dhchap_secret) { +pr_debug ("No authentication provided\n" ); + goto out_unlock; + } + + hash_name =nvme_auth_hmac_name (host->dhchap_hash_id); +if (!hash_name) { +pr_debug ("Hash ID %d invalid\n" , host->dhchap_hash_id); + ret = -EINVAL ; + goto out_unlock; + } + ctrl->shash_tfm =crypto_alloc_shash (hash_name,0 , +CRYPTO_ALG_ALLOCATES_MEMORY ); +if (IS_ERR (ctrl->shash_tfm)) { +pr_debug ("failed to allocate shash %s\n" , hash_name); + ret =PTR_ERR (ctrl->shash_tfm); + ctrl->shash_tfm =NULL ; + goto out_unlock; + } + + ctrl->dhchap_key =nvme_auth_extract_secret (host->dhchap_secret, + &ctrl->dhchap_key_len); +if (IS_ERR (ctrl->dhchap_key)) { +pr_debug ("failed to extract host key, error %d\n" , ret); + ret =PTR_ERR (ctrl->dhchap_key); + ctrl->dhchap_key =NULL ; + goto out_free_hash; + } +if (host->dhchap_key_hash) { +struct crypto_shash *key_tfm; + + hash_name =nvme_auth_hmac_name (host->dhchap_key_hash); + key_tfm =crypto_alloc_shash (hash_name,0 ,0 ); +if (IS_ERR (key_tfm)) { + ret =PTR_ERR (key_tfm); + goto out_free_hash; + }else { +SHASH_DESC_ON_STACK (shash, key_tfm); + + shash->tfm = key_tfm; + ret =crypto_shash_setkey (key_tfm, ctrl->dhchap_key, + ctrl->dhchap_key_len); +crypto_shash_init (shash); +crypto_shash_update (shash, ctrl->subsys->subsysnqn, +strlen (ctrl->subsys->subsysnqn)); +crypto_shash_update (shash,"NVMe-over-Fabrics" ,17 ); +crypto_shash_final (shash, ctrl->dhchap_key); +crypto_free_shash (key_tfm); + }Quoted 8 lines
drivers/nvme/target/auth.c line 144
Quoted 4 lines
drivers/nvme/target/auth.c new file +4
-0+ } +pr_debug ("%s: using key %*ph\n" , __func__, + (int )ctrl->dhchap_key_len, ctrl->dhchap_key); + ret = crypto_shash_setkey(ctrl->shash_tfm, ctrl->dhchap_key,Is it truly necessary to keep the key around in
ctrl->dhchap_key? It looks to me that this buffer is only used here and thus could be turned into a local variable. Keys flying around in memory is not a good idea. :-)The key is also used when using the ffdhe algorithm.
Note: I _think_ that I need to use this key for the ffdhe algorithm, because the implementation I came up with is essentially plain DH with pre-defined 'p', 'q' and 'g' values. But the DH implementation also requires a 'key', and for that I'm using this key here.Quoted 4 lines
It might be that I'm completely off, and don't need to use a key for our DH implementation. In that case you are correct.
(And that's why I said I'll need a review of the FFDHE implementation). But for now I'll need the key for FFDHE.Do I understand you correctly that the
dhchap_keyis used as the input to the DH - i.e. it is the remote public key then? It looks strange that this is used for DH but then it is changed here by hashing it together with something else to form a newdhchap_key. Maybe that is what the protocol says. But it sounds strange to me, especially when you think thatdhchap_keywould be, say, 2048 bits if it is truly the remote public key and then after the hashing it is 256 or 512 bits depending on the HMAC type. This means that after the hashing, thisdhchap_keycannot be used for FFC-DH.
Quoted 4 lines
Or are you using the
dhchap_keyfor two different purposes?It seems I miss something here.
No, not entirely. It's me who buggered it up.
I got carried away by the fact that there is a crypto_dh_encode_key() function, and thought I need to use it here.
Which I don't (apparently).
Will be fixing it up.
Cheers,
Hannes
Signature · 9 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), GF: Felix Imendörffer _______________________________________________ Linux-nvme mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-nvme
Re: [PATCH 09/11] nvmet: Implement basic In-Band Authentication
Am Montag, dem 19.07.2021 um 10:15 +0200 schrieb Hannes Reinecke:
Quoted 28 lines · Stephan Müller
Quoted 27 lines
Am Sonntag, 18. Juli 2021, 14:37:34 CEST schrieb Hannes Reinecke:
Quoted 10 lines
The key is also used when using the ffdhe algorithm.
Note: I _think_ that I need to use this key for the ffdhe algorithm, because the implementation I came up with is essentially plain DH with pre-defined 'p', 'q' and 'g' values. But the DH implementation also requires a 'key', and for that I'm using this key here.It might be that I'm completely off, and don't need to use a key for our DH implementation. In that case you are correct.
(And that's why I said I'll need a review of the FFDHE implementation). But for now I'll need the key for FFDHE.Do I understand you correctly that the
dhchap_keyis used as the input to the
DH - i.e. it is the remote public key then? It looks strange that this is used
for DH but then it is changed here by hashing it together with something else
to form a newdhchap_key. Maybe that is what the protocol says. But it sounds
strange to me, especially when you think thatdhchap_keywould be, say, 2048
bits if it is truly the remote public key and then after the hashing it is 256
thisdhchap_keycannot be used for FFC-DH.
Quoted 7 lines
Quoted 4 lines
Or are you using the
dhchap_keyfor two different purposes?It seems I miss something here.
No, not entirely. It's me who buggered it up.
I got carried away by the fact that there is acrypto_dh_encode_key()function, and thought I need to use it here.
Thank you for clarifying that. It sounds to me that there is no defined protocol (or if there, I would be wondering how the code would have worked with a different implementation). Would it make sense to first specify a protocol for authentication and have it discussed? I personally think it is a bit difficult to fully understand the protocol from the code and discuss protocol-level items based on the code.
Thanks
Stephan
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Hannes Reinecke
maintainerRe: [PATCH 09/11] nvmet: Implement basic In-Band Authentication
Quoted 37 lines · Stephan Mueller
Am Montag, dem 19.07.2021 um 10:15 +0200 schrieb Hannes Reinecke:
Quoted 28 lines · Stephan Müller
Quoted 27 lines
Am Sonntag, 18. Juli 2021, 14:37:34 CEST schrieb Hannes Reinecke:
Quoted 10 lines
The key is also used when using the ffdhe algorithm.
Note: I _think_ that I need to use this key for the ffdhe algorithm, because the implementation I came up with is essentially plain DH with pre-defined 'p', 'q' and 'g' values. But the DH implementation also requires a 'key', and for that I'm using this key here.It might be that I'm completely off, and don't need to use a key for our DH implementation. In that case you are correct.
(And that's why I said I'll need a review of the FFDHE implementation). But for now I'll need the key for FFDHE.Do I understand you correctly that the
dhchap_keyis used as the input to the
DH - i.e. it is the remote public key then? It looks strange that this is used
for DH but then it is changed here by hashing it together with something else
to form a newdhchap_key. Maybe that is what the protocol says. But it sounds
strange to me, especially when you think thatdhchap_keywould be, say, 2048
bits if it is truly the remote public key and then after the hashing it is 256
thisdhchap_keycannot be used for FFC-DH.Quoted 7 lines
Quoted 4 lines
Or are you using the
dhchap_keyfor two different purposes?It seems I miss something here.
No, not entirely. It's me who buggered it up.
I got carried away by the fact that there is acrypto_dh_encode_key()function, and thought I need to use it here.
Quoted 7 lines
Thank you for clarifying that. It sounds to me that there is no defined protocol (or if there, I would be wondering how the code would have worked with a different implementation). Would it make sense to first specify a protocol for authentication and have it discussed? I personally think it is a bit difficult to fully understand the protocol from the code and discuss protocol-level items based on the code.
Oh, the protocol _is_ specified:
It's just that I have issues translating that spec onto what the kernel provides.
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), GF: Felix Imendörffer
Re: [PATCH 09/11] nvmet: Implement basic In-Band Authentication
Am Montag, dem 19.07.2021 um 11:57 +0200 schrieb Hannes Reinecke:
Quoted 54 lines · Stephan Mueller
Quoted 45 lines
Am Montag, dem 19.07.2021 um 10:15 +0200 schrieb Hannes Reinecke:
Quoted 36 lines · Stephan Müller
Quoted 35 lines
Am Sonntag, 18. Juli 2021, 14:37:34 CEST schrieb Hannes Reinecke:
Quoted 7 lines
The key is also used when using the ffdhe algorithm.
Note: I _think_ that I need to use this key for the ffdhe algorithm, because the implementation I came up with is essentially plain DH with
pre-defined 'p', 'q' and 'g' values. But the DH implementation also requires a 'key', and for that I'm using this key here.Quoted 6 lines
It might be that I'm completely off, and don't need to use a key for our
DH implementation. In that case you are correct.
(And that's why I said I'll need a review of the FFDHE implementation).
But for now I'll need the key for FFDHE.Do I understand you correctly that the
dhchap_keyis used as the input to
the
DH - i.e. it is the remote public key then? It looks strange that this is
used
for DH but then it is changed here by hashing it together with something
else
to form a newdhchap_key. Maybe that is what the protocol says. But it sounds
strange to me, especially when you think thatdhchap_keywould be, say,
2048
bits if it is truly the remote public key and then after the hashing it is
256
thisdhchap_keycannot be used for FFC-DH.Quoted 7 lines
Quoted 4 lines
Or are you using the
dhchap_keyfor two different purposes?It seems I miss something here.
No, not entirely. It's me who buggered it up.
I got carried away by the fact that there is acrypto_dh_encode_key()function, and thought I need to use it here.Quoted 8 lines
Thank you for clarifying that. It sounds to me that there is no defined protocol (or if there, I would be wondering how the code would have worked with a different implementation). Would it make sense to first specify a protocol for authentication and have it discussed? I personally think it is a
bit difficult to fully understand the protocol from the code and discuss protocol-level items based on the code.
Quoted 6 lines
Oh, the protocol _is_ specified:
It's just that I have issues translating that spec onto what the kernel provides.
according to the naming conventions there in figures 447 and following:
- x and y: DH private key (kernel calls it secret set with dh_set_secret or encoded into param.key)
- g^x mod p / g^y mod p: DH public keys from either end that is communicated over the wire (corresponding to the the DH private keys of x and y) - to set it, you initialize a dh request and set the public key to it with kpp_request_set_input. After performing the crypto_kpp_compute_shared_secret you receive the shared secret
- g^xy mod p: DH shared secret - this is the one that is to be used for the subsequent hashing /HMAC operations as this is the one that is identical on both, the host and the controller.
Ciao
Stephan
Hannes Reinecke
maintainerRe: [PATCH 09/11] nvmet: Implement basic In-Band Authentication
Quoted 59 lines · Stephan Mueller
Am Montag, dem 19.07.2021 um 11:57 +0200 schrieb Hannes Reinecke:
Quoted 58 lines · Stephan Mueller
Quoted 45 lines
Am Montag, dem 19.07.2021 um 10:15 +0200 schrieb Hannes Reinecke:
Quoted 36 lines · Stephan Müller
Quoted 35 lines
Am Sonntag, 18. Juli 2021, 14:37:34 CEST schrieb Hannes Reinecke:
Quoted 7 lines
The key is also used when using the ffdhe algorithm.
Note: I _think_ that I need to use this key for the ffdhe algorithm, because the implementation I came up with is essentially plain DH with
pre-defined 'p', 'q' and 'g' values. But the DH implementation also requires a 'key', and for that I'm using this key here.Quoted 6 lines
It might be that I'm completely off, and don't need to use a key for our
DH implementation. In that case you are correct.
(And that's why I said I'll need a review of the FFDHE implementation).
But for now I'll need the key for FFDHE.Do I understand you correctly that the
dhchap_keyis used as the input to
the
DH - i.e. it is the remote public key then? It looks strange that this is
used
for DH but then it is changed here by hashing it together with something
else
to form a newdhchap_key. Maybe that is what the protocol says. But it sounds
strange to me, especially when you think thatdhchap_keywould be, say,
2048
bits if it is truly the remote public key and then after the hashing it is
256
thisdhchap_keycannot be used for FFC-DH.Quoted 7 lines
Quoted 4 lines
Or are you using the
dhchap_keyfor two different purposes?It seems I miss something here.
No, not entirely. It's me who buggered it up.
I got carried away by the fact that there is acrypto_dh_encode_key()function, and thought I need to use it here.Quoted 8 lines
Thank you for clarifying that. It sounds to me that there is no defined protocol (or if there, I would be wondering how the code would have worked with a different implementation). Would it make sense to first specify a protocol for authentication and have it discussed? I personally think it is a
bit difficult to fully understand the protocol from the code and discuss protocol-level items based on the code.Oh, the protocol _is_ specified:
Quoted 8 lines
Quoted 2 lines
It's just that I have issues translating that spec onto what the kernel provides.
according to the naming conventions there in figures 447 and following:
- x and y: DH private key (kernel calls it secret set with
dh_set_secretor encoded into param.key)
But that's were I got confused; one needs a private key here, but there is no obvious candidate for it. But reading it more closely I guess the private key is just a random number (cf the spec: g^y mod p, where y is a random number selected by the host that shall be at least 256 bits long). So I'll fix it up with the next round.
Quoted 10 lines
- g^x mod p / g^y mod p: DH public keys from either end that is communicated over the wire (corresponding to the the DH private keys of x and y) - to set it, you initialize a dh request and set the public key to it with
kpp_request_set_input. After performing thecrypto_kpp_compute_shared_secretyou receive the shared secret- g^xy mod p: DH shared secret - this is the one that is to be used for the subsequent hashing /HMAC operations as this is the one that is identical on both, the host and the controller.
Thanks. Will be checking the code if I do it correctly.
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), GF: Felix Imendörffer
Re: [PATCH 09/11] nvmet: Implement basic In-Band Authentication
Am Montag, dem 19.07.2021 um 13:10 +0200 schrieb Hannes Reinecke:
Quoted 85 lines · Stephan Mueller
Quoted 74 lines
Am Montag, dem 19.07.2021 um 11:57 +0200 schrieb Hannes Reinecke:
Quoted 73 lines · Stephan Mueller
Quoted 54 lines
Am Montag, dem 19.07.2021 um 10:15 +0200 schrieb Hannes Reinecke:
Quoted 44 lines · Stephan Müller
Quoted 43 lines
Am Sonntag, 18. Juli 2021, 14:37:34 CEST schrieb Hannes Reinecke:
Quoted 10 lines
The key is also used when using the ffdhe algorithm.
Note: I _think_ that I need to use this key for the ffdhe algorithm,
because the implementation I came up with is essentially plain DH
with
pre-defined 'p', 'q' and 'g' values. But the DH implementation also
requires a 'key', and for that I'm using this key here.Quoted 7 lines
It might be that I'm completely off, and don't need to use a key for
our
DH implementation. In that case you are correct.
(And that's why I said I'll need a review of the FFDHE implementation).
But for now I'll need the key for FFDHE.Do I understand you correctly that the
dhchap_keyis used as the input
to
the
DH - i.e. it is the remote public key then? It looks strange that this
is
used
for DH but then it is changed here by hashing it together with something
else
to form a newdhchap_key. Maybe that is what the protocol says. But it
sounds
strange to me, especially when you think thatdhchap_keywould be, say,
2048
bits if it is truly the remote public key and then after the hashing
it is
256
thisdhchap_keycannot be used for FFC-DH.Quoted 8 lines
Quoted 4 lines
Or are you using the
dhchap_keyfor two different purposes?It seems I miss something here.
No, not entirely. It's me who buggered it up.
I got carried away by the fact that there is acrypto_dh_encode_key()
function, and thought I need to use it here.Quoted 13 lines
Thank you for clarifying that. It sounds to me that there is no defined
protocol (or if there, I would be wondering how the code would have worked
with a different implementation). Would it make sense to first specify a
protocol for authentication and have it discussed? I personally think it
is a
bit difficult to fully understand the protocol from the code and discuss
protocol-level items based on the code.Oh, the protocol _is_ specified:
Quoted 9 lines
Quoted 2 lines
It's just that I have issues translating that spec onto what the kernel provides.
according to the naming conventions there in figures 447 and following:
- x and y: DH private key (kernel calls it secret set with
dh_set_secretor
encoded into param.key)
Quoted 5 lines
But that's were I got confused; one needs a private key here, but there is no obvious candidate for it. But reading it more closely I guess the private key is just a random number (cf the spec: g^y mod p, where y is a random number selected by the host that shall be at least 256 bits long). So I'll fix it up with the next round.
Here comes the crux: the kernel has an ECC private key generation function ecdh_set_secret triggered with crypto_kpp_set_secret using a NULL key, but it has no FFC-DH counterpart.
That said, generating a random number is the most obvious choice, but not the right one.
The correct one would be following SP800-56A rev 3 and here either section 5.6.1.1.3 or 5.6.1.1.4.
Ciao
Stephan
Quoted 21 lines
Quoted 10 lines
- g^x mod p / g^y mod p: DH public keys from either end that is communicated
over the wire (corresponding to the the DH private keys of x and y) - to set
it, you initialize a dh request and set the public key to it withkpp_request_set_input. After performing thecrypto_kpp_compute_shared_secret
you receive the shared secretQuoted 6 lines
- g^xy mod p: DH shared secret - this is the one that is to be used for the
subsequent hashing /HMAC operations as this is the one that is identical on
both, the host and the controller.Thanks. Will be checking the code if I do it correctly.
Cheers,
Hannes
Hannes Reinecke
maintainerRe: [PATCH 09/11] nvmet: Implement basic In-Band Authentication
Quoted 96 lines · Stephan Mueller
Am Montag, dem 19.07.2021 um 13:10 +0200 schrieb Hannes Reinecke:
Quoted 85 lines · Stephan Mueller
Quoted 74 lines
Am Montag, dem 19.07.2021 um 11:57 +0200 schrieb Hannes Reinecke:
Quoted 73 lines · Stephan Mueller
Quoted 54 lines
Am Montag, dem 19.07.2021 um 10:15 +0200 schrieb Hannes Reinecke:
Quoted 44 lines · Stephan Müller
Quoted 43 lines
Am Sonntag, 18. Juli 2021, 14:37:34 CEST schrieb Hannes Reinecke:
Quoted 10 lines
The key is also used when using the ffdhe algorithm.
Note: I _think_ that I need to use this key for the ffdhe algorithm,
because the implementation I came up with is essentially plain DH
with
pre-defined 'p', 'q' and 'g' values. But the DH implementation also
requires a 'key', and for that I'm using this key here.Quoted 7 lines
It might be that I'm completely off, and don't need to use a key for
our
DH implementation. In that case you are correct.
(And that's why I said I'll need a review of the FFDHE implementation).
But for now I'll need the key for FFDHE.Do I understand you correctly that the
dhchap_keyis used as the input
to
the
DH - i.e. it is the remote public key then? It looks strange that this
is
used
for DH but then it is changed here by hashing it together with something
else
to form a newdhchap_key. Maybe that is what the protocol says. But it
sounds
strange to me, especially when you think thatdhchap_keywould be, say,
2048
bits if it is truly the remote public key and then after the hashing
it is
256
thisdhchap_keycannot be used for FFC-DH.Quoted 8 lines
Quoted 4 lines
Or are you using the
dhchap_keyfor two different purposes?It seems I miss something here.
No, not entirely. It's me who buggered it up.
I got carried away by the fact that there is acrypto_dh_encode_key()
function, and thought I need to use it here.Quoted 13 lines
Thank you for clarifying that. It sounds to me that there is no defined
protocol (or if there, I would be wondering how the code would have worked
with a different implementation). Would it make sense to first specify a
protocol for authentication and have it discussed? I personally think it
is a
bit difficult to fully understand the protocol from the code and discuss
protocol-level items based on the code.Oh, the protocol _is_ specified:
Quoted 9 lines
Quoted 2 lines
It's just that I have issues translating that spec onto what the kernel provides.
according to the naming conventions there in figures 447 and following:
- x and y: DH private key (kernel calls it secret set with
dh_set_secretor
encoded into param.key)Quoted 5 lines
But that's were I got confused; one needs a private key here, but there is no obvious candidate for it. But reading it more closely I guess the private key is just a random number (cf the spec: g^y mod p, where y is a random number selected by the host that shall be at least 256 bits long). So I'll fix it up with the next round.
Here comes the crux: the kernel has an ECC private key generation function
ecdh_set_secrettriggered withcrypto_kpp_set_secretusing a NULL key, but it has no FFC-DH counterpart.
Quoted 6 lines
That said, generating a random number is the most obvious choice, but not the right one.
The correct one would be following SP800-56A rev 3 and here either section 5.6.1.1.3 or 5.6.1.1.4.
Oh fsck. Of course. Would've been too easy. Well, more coding required then. Let's see how it goes. But thanks for your help!
(And I wouldn't say no if someone would step in an provide a 'real' FFDHE crypto algorithm; now that the parameters are already present :-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), GF: Felix Imendörffer
Hannes Reinecke
maintainerRe: [PATCH 09/11] nvmet: Implement basic In-Band Authentication
In reply to Stephan MuellerQuoted 4 lines · Stephan Mueller
Am Montag, dem 19.07.2021 um 13:10 +0200 schrieb Hannes Reinecke:
Quoted 3 lines · Stephan Mueller
Quoted 2 lines
Am Montag, dem 19.07.2021 um 11:57 +0200 schrieb Hannes Reinecke:
Quoted 1 line · Stephan Mueller
[ .. ]
Quoted 34 lines
Quoted 24 lines
Quoted 14 lines
Quoted 14 lines
Quoted 9 lines
Thank you for clarifying that. It sounds to me that there is no defined protocol (or if there, I would be wondering how the code would have worked
with a different implementation). Would it make sense to first specify a protocol for authentication and have it discussed? I personally think it is a bit difficult to fully understand the protocol from the code and discuss protocol-level items based on the code.Oh, the protocol _is_ specified:
Quoted 9 lines
Quoted 2 lines
It's just that I have issues translating that spec onto what the kernel provides.
according to the naming conventions there in figures 447 and following:
- x and y: DH private key (kernel calls it secret set with
dh_set_secretor
encoded into param.key)Quoted 5 lines
But that's were I got confused; one needs a private key here, but there is no obvious candidate for it. But reading it more closely I guess the private key is just a random number (cf the spec: g^y mod p, where y is a random number selected by the host that shall be at least 256 bits long). So I'll fix it up with the next round.
Here comes the crux: the kernel has an ECC private key generation function
ecdh_set_secrettriggered withcrypto_kpp_set_secretusing a NULL key, but it has no FFC-DH counterpart.
Quoted 6 lines
That said, generating a random number is the most obvious choice, but not the right one.
The correct one would be following SP800-56A rev 3 and here either section 5.6.1.1.3 or 5.6.1.1.4.
Hmm. Okay. But after having read section 5.6.1.1.4, I still do have some questions.
Assume we will be using a bit length of 512 for FFDHE, then we will trivially pass Step 2 for all supported FFDHE groups (the maximum symmetric-equivalent strength for ffdhe8192 is 192 bits).
From my understanding, the random number generator will fill out all available bytes in the string (and nothing more), so we trivially satisfy step 3 and 4.
And as q is always larger than the random number, step 6 reduces to 'if (c > 2^N - 2)', ie we just need to check if the random number is a string of 0xff characters. Which hardly is a random number at all, so it'll be impossible to get this.
Which then would mean that our 'x' is simply the random number + 1, which arguably is slightly pointless (one more than a random number is as random as the number itself), so I do feel justified with just returning a random number here.
Am I wrong with that reasoning?
Cheers,
Hannes
Signature · 9 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), GF: Felix Imendörffer _______________________________________________ Linux-nvme mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-nvme
Re: [PATCH 09/11] nvmet: Implement basic In-Band Authentication
Quoted 46 lines · Hannes Reinecke
Quoted 4 lines · Stephan Mueller
Am Montag, dem 19.07.2021 um 13:10 +0200 schrieb Hannes Reinecke:
Quoted 3 lines · Stephan Mueller
Quoted 2 lines
Am Montag, dem 19.07.2021 um 11:57 +0200 schrieb Hannes Reinecke:
Quoted 1 line · Stephan Mueller
[ .. ]
Quoted 34 lines
Quoted 24 lines
Quoted 14 lines
Quoted 14 lines
Quoted 9 lines
Thank you for clarifying that. It sounds to me that there is no defined protocol (or if there, I would be wondering how the code would have worked
with a different implementation). Would it make sense to first specify a protocol for authentication and have it discussed? I personally think it is a bit difficult to fully understand the protocol from the code and discuss protocol-level items based on the code.Oh, the protocol _is_ specified:
Quoted 9 lines
Quoted 2 lines
It's just that I have issues translating that spec onto what the kernel provides.
according to the naming conventions there in figures 447 and following:
- x and y: DH private key (kernel calls it secret set with
dh_set_secretor
encoded into param.key)Quoted 5 lines
But that's were I got confused; one needs a private key here, but there is no obvious candidate for it. But reading it more closely I guess the private key is just a random number (cf the spec: g^y mod p, where y is a random number selected by the host that shall be at least 256 bits long). So I'll fix it up with the next round.
Here comes the crux: the kernel has an ECC private key generation function
ecdh_set_secrettriggered withcrypto_kpp_set_secretusing a NULL key, but it has no FFC-DH counterpart.Quoted 6 lines
That said, generating a random number is the most obvious choice, but not the right one.
The correct one would be following SP800-56A rev 3 and here either section 5.6.1.1.3 or 5.6.1.1.4.
Quoted 6 lines
Hmm. Okay. But after having read section 5.6.1.1.4, I still do have some questions.
Assume we will be using a bit length of 512 for FFDHE, then we will trivially pass Step 2 for all supported FFDHE groups (the maximum symmetric-equivalent strength for ffdhe8192 is 192 bits).
N = 512 is not a good choice, minimum length these days for DH should be 2048 or more.
Quoted 6 lines
From my understanding, the random number generator will fill out all available bytes in the string (and nothing more), so we trivially satisfy step 3 and 4.
And as q is always larger than the random number, step 6 reduces to 'if (c > 2^N - 2)',
Where is this coming from ?
It seem you assume M = 2^N but M = min(2^N, q)
The point here is to make sure the number X you return is:
0 < X < (q-1)
Quoted 5 lines
ie we just need to check if the random number is a
string of0xffcharacters. Which hardly is a random number at all, so it'll be impossible to get this.Which then would mean that our 'x' is simply the random number + 1,
This is an artifact due to the random number being 0 <= c < 2^N - 1, therefore 1 needs to be added to make sure you never return 0.
Quoted 5 lines
which arguably is slightly pointless (one more than a random number is as random as the number itself), so I do feel justified with just returning a random number here.
Am I wrong with that reasoning?
Looks to me you are not accounting for the fact that N = 512 is too small and a random number falling in the interval (q - 2) < X < 2^N is unsuitable?
Simo.
Signature · 12 lines
Simo Sorce RHEL Crypto Team Red Hat, Inc _______________________________________________ Linux-nvme mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-nvme
Hannes Reinecke
maintainerRe: [PATCH 09/11] nvmet: Implement basic In-Band Authentication
Quoted 50 lines · Simo Sorce
Quoted 49 lines · Hannes Reinecke
Quoted 4 lines · Stephan Mueller
Am Montag, dem 19.07.2021 um 13:10 +0200 schrieb Hannes Reinecke:
Quoted 3 lines · Stephan Mueller
Quoted 2 lines
Am Montag, dem 19.07.2021 um 11:57 +0200 schrieb Hannes Reinecke:
Quoted 1 line · Stephan Mueller
[ .. ]
Quoted 34 lines
Quoted 24 lines
Quoted 14 lines
Quoted 14 lines
Quoted 9 lines
Thank you for clarifying that. It sounds to me that there is no defined protocol (or if there, I would be wondering how the code would have worked
with a different implementation). Would it make sense to first specify a protocol for authentication and have it discussed? I personally think it is a bit difficult to fully understand the protocol from the code and discuss protocol-level items based on the code.Oh, the protocol _is_ specified:
Quoted 9 lines
Quoted 2 lines
It's just that I have issues translating that spec onto what the kernel provides.
according to the naming conventions there in figures 447 and following:
- x and y: DH private key (kernel calls it secret set with
dh_set_secretor
encoded into param.key)Quoted 5 lines
But that's were I got confused; one needs a private key here, but there is no obvious candidate for it. But reading it more closely I guess the private key is just a random number (cf the spec: g^y mod p, where y is a random number selected by the host that shall be at least 256 bits long). So I'll fix it up with the next round.
Here comes the crux: the kernel has an ECC private key generation function
ecdh_set_secrettriggered withcrypto_kpp_set_secretusing a NULL key, but it has no FFC-DH counterpart.Quoted 6 lines
That said, generating a random number is the most obvious choice, but not the right one.
The correct one would be following SP800-56A rev 3 and here either section 5.6.1.1.3 or 5.6.1.1.4.
Hmm. Okay. But after having read section 5.6.1.1.4, I still do have some questions.
Quoted 7 lines
Quoted 3 lines
Assume we will be using a bit length of 512 for FFDHE, then we will trivially pass Step 2 for all supported FFDHE groups (the maximum symmetric-equivalent strength for ffdhe8192 is 192 bits).
N = 512 is not a good choice, minimum length these days for DH should be 2048 or more.
According to RFC7919:
Peers using ffdhe8192 that want to optimize their key exchange with a short exponent (Section 5.2) should choose a secret key of at least 400 bits.
So what is wrong with 512 bits?
Quoted 4 lines
Quoted 4 lines
From my understanding, the random number generator will fill out all available bytes in the string (and nothing more), so we trivially satisfy step 3 and 4.
Quoted 9 lines
Quoted 2 lines
And as q is always larger than the random number, step 6 reduces to 'if (c > 2^N - 2)',
Where is this coming from ?
It seem you assume M = 2^N but M =min(2^N, q)The point here is to make sure the number X you return is:
0 < X < (q-1)
Which is what I've tried to argue. For 512 bits private key and the smallest possible FFDHE group (which has 2048 bits, with the top bit non-zero) 2^N is always smaller than (q - 1).
As the other FFHDE groups are using even larger 'q' values, this is true for all FFHDE groups.
Quoted 9 lines
Quoted 5 lines
ie we just need to check if the random number is a
string of0xffcharacters. Which hardly is a random number at all, so it'll be impossible to get this.Which then would mean that our 'x' is simply the random number + 1,
This is an artifact due to the random number being 0 <= c < 2^N - 1, therefore 1 needs to be added to make sure you never return 0.
And my argument here is that all zeros (and all ones) are not a value I would expect from our RNG.
Quoted 10 lines
Quoted 5 lines
which arguably is slightly pointless (one more than a random number is as random as the number itself), so I do feel justified with just returning a random number here.
Am I wrong with that reasoning?
Looks to me you are not accounting for the fact that N = 512 is too small and a random number falling in the interval (q - 2) < X < 2^N is unsuitable?
Only if (q - 2) < 2^N. And my point is that it's not.
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), GF: Felix Imendörffer
Re: [PATCH 09/11] nvmet: Implement basic In-Band Authentication
Quoted 59 lines · Hannes Reinecke
Quoted 50 lines · Simo Sorce
Quoted 49 lines · Hannes Reinecke
Quoted 4 lines · Stephan Mueller
Am Montag, dem 19.07.2021 um 13:10 +0200 schrieb Hannes Reinecke:
Quoted 3 lines · Stephan Mueller
Quoted 2 lines
Am Montag, dem 19.07.2021 um 11:57 +0200 schrieb Hannes Reinecke:
Quoted 1 line · Stephan Mueller
[ .. ]
Quoted 34 lines
Quoted 24 lines
Quoted 14 lines
Quoted 14 lines
Quoted 9 lines
Thank you for clarifying that. It sounds to me that there is no defined protocol (or if there, I would be wondering how the code would have worked
with a different implementation). Would it make sense to first specify a protocol for authentication and have it discussed? I personally think it is a bit difficult to fully understand the protocol from the code and discuss protocol-level items based on the code.Oh, the protocol _is_ specified:
Quoted 9 lines
Quoted 2 lines
It's just that I have issues translating that spec onto what the kernel provides.
according to the naming conventions there in figures 447 and following:
- x and y: DH private key (kernel calls it secret set with
dh_set_secretor
encoded into param.key)Quoted 5 lines
But that's were I got confused; one needs a private key here, but there is no obvious candidate for it. But reading it more closely I guess the private key is just a random number (cf the spec: g^y mod p, where y is a random number selected by the host that shall be at least 256 bits long). So I'll fix it up with the next round.
Here comes the crux: the kernel has an ECC private key generation function
ecdh_set_secrettriggered withcrypto_kpp_set_secretusing a NULL key, but it has no FFC-DH counterpart.Quoted 6 lines
That said, generating a random number is the most obvious choice, but not the right one.
The correct one would be following SP800-56A rev 3 and here either section 5.6.1.1.3 or 5.6.1.1.4.
Hmm. Okay. But after having read section 5.6.1.1.4, I still do have some questions.
Quoted 7 lines
Quoted 3 lines
Assume we will be using a bit length of 512 for FFDHE, then we will trivially pass Step 2 for all supported FFDHE groups (the maximum symmetric-equivalent strength for ffdhe8192 is 192 bits).
N = 512 is not a good choice, minimum length these days for DH should be 2048 or more.
Quoted 6 lines
According to RFC7919:
Peers using ffdhe8192 that want to optimize their key exchange with a short exponent (Section 5.2) should choose a secret key of at least 400 bits.So what is wrong with 512 bits?
RFC7519 is TLS Specific.
I do not know if short-exponents are safe to use in all use cases.
If it is safe, your choice is fine and your arguments will follow, but then a comment that explains the choice and warns about key checks if it is changed would be a good idea.
Otherwise the default should be to use N = len(q), which implies the proper checks need to be applied.
Simo.
Quoted 49 lines
Quoted 4 lines
Quoted 4 lines
From my understanding, the random number generator will fill out all available bytes in the string (and nothing more), so we trivially satisfy step 3 and 4.
Quoted 9 lines
Quoted 2 lines
And as q is always larger than the random number, step 6 reduces to 'if (c > 2^N - 2)',
Where is this coming from ?
It seem you assume M = 2^N but M =min(2^N, q)The point here is to make sure the number X you return is:
0 < X < (q-1)Which is what I've tried to argue. For 512 bits private key and the smallest possible FFDHE group (which has 2048 bits, with the top bit non-zero) 2^N is always smaller than (q - 1).
As the other FFHDE groups are using even larger 'q' values, this is true for all FFHDE groups.Quoted 9 lines
Quoted 5 lines
ie we just need to check if the random number is a
string of0xffcharacters. Which hardly is a random number at all, so it'll be impossible to get this.Which then would mean that our 'x' is simply the random number + 1,
This is an artifact due to the random number being 0 <= c < 2^N - 1, therefore 1 needs to be added to make sure you never return 0.
And my argument here is that all zeros (and all ones) are not a value I would expect from our RNG.
Quoted 10 lines
Quoted 5 lines
which arguably is slightly pointless (one more than a random number is as random as the number itself), so I do feel justified with just returning a random number here.
Am I wrong with that reasoning?
Looks to me you are not accounting for the fact that N = 512 is too small and a random number falling in the interval (q - 2) < X < 2^N is unsuitable?
Only if (q - 2) < 2^N. And my point is that it's not.
Cheers,
Hannes
Signature · 12 lines
Simo Sorce RHEL Crypto Team Red Hat, Inc _______________________________________________ Linux-nvme mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-nvme
Re: [PATCH 09/11] nvmet: Implement basic In-Band Authentication
Am Dienstag, dem 20.07.2021 um 10:44 -0400 schrieb Simo Sorce:
Quoted 89 lines · Hannes Reinecke
Quoted 77 lines · Simo Sorce
Quoted 68 lines · Hannes Reinecke
Quoted 67 lines · Stephan Mueller
Quoted 5 lines
Am Montag, dem 19.07.2021 um 13:10 +0200 schrieb Hannes Reinecke:
Quoted 4 lines · Stephan Mueller
Quoted 3 lines
Am Montag, dem 19.07.2021 um 11:57 +0200 schrieb Hannes Reinecke:
Quoted 1 line · Stephan Mueller
[ .. ]
Quoted 48 lines
Quoted 32 lines
Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
Thank you for clarifying that. It sounds to me that there is no
defined protocol (or if there, I would be wondering how the code would have
worked
with a different implementation). Would it make sense to first specify
a protocol for authentication and have it discussed? I personally think
it is a bit difficult to fully understand the protocol from the code and
discuss protocol-level items based on the code.Quoted 8 lines
Oh, the protocol _is_ specified:
It's just that I have issues translating that spec onto what the kernel
provides.Quoted 8 lines
according to the naming conventions there in figures 447 and following:
- x and y: DH private key (kernel calls it secret set with
dh_set_secret
or
encoded into param.key)Quoted 9 lines
But that's were I got confused; one needs a private key here, but there
is no obvious candidate for it. But reading it more closely I guess the
private key is just a random number (cf the spec: g^y mod p, where y is
a random number selected by the host that shall be at least 256 bits
long). So I'll fix it up with the next round.Here comes the crux: the kernel has an ECC private key generation function
ecdh_set_secrettriggered withcrypto_kpp_set_secretusing a NULL key, but it
has no FFC-DH counterpart.Quoted 8 lines
That said, generating a random number is the most obvious choice, but not the
right one.The correct one would be following SP800-56A rev 3 and here either section
5.6.1.1.3 or 5.6.1.1.4.Hmm. Okay. But after having read section 5.6.1.1.4, I still do have some
questions.Quoted 7 lines
Quoted 3 lines
Assume we will be using a bit length of 512 for FFDHE, then we will trivially pass Step 2 for all supported FFDHE groups (the maximum symmetric-equivalent strength for ffdhe8192 is 192 bits).
N = 512 is not a good choice, minimum length these days for DH should be 2048 or more.
Quoted 6 lines
According to RFC7919:
Peers using ffdhe8192 that want to optimize their key exchange with a short exponent (Section 5.2) should choose a secret key of at least 400 bits.So what is wrong with 512 bits?
RFC7519 is TLS Specific.
I do not know if short-exponents are safe to use in all use cases.
Quoted 6 lines
If it is safe, your choice is fine and your arguments will follow, but then a comment that explains the choice and warns about key checks if it is changed would be a good idea.
Otherwise the default should be to use N = len(q), which implies the proper checks need to be applied.
Agreed.
Ciao
Stephan
Quoted 53 lines
Simo.
Quoted 49 lines
Quoted 4 lines
Quoted 4 lines
From my understanding, the random number generator will fill out all available bytes in the string (and nothing more), so we trivially satisfy step 3 and 4.
Quoted 9 lines
Quoted 2 lines
And as q is always larger than the random number, step 6 reduces to 'if (c > 2^N - 2)',
Where is this coming from ?
It seem you assume M = 2^N but M =min(2^N, q)The point here is to make sure the number X you return is:
0 < X < (q-1)Which is what I've tried to argue. For 512 bits private key and the smallest possible FFDHE group (which has 2048 bits, with the top bit non-zero) 2^N is always smaller than (q - 1).
As the other FFHDE groups are using even larger 'q' values, this is true for all FFHDE groups.Quoted 9 lines
Quoted 5 lines
ie we just need to check if the random number is a
string of0xffcharacters. Which hardly is a random number at all, so it'll be impossible to get this.Which then would mean that our 'x' is simply the random number + 1,
This is an artifact due to the random number being 0 <= c < 2^N - 1, therefore 1 needs to be added to make sure you never return 0.
And my argument here is that all zeros (and all ones) are not a value I would expect from our RNG.
Quoted 10 lines
Quoted 5 lines
which arguably is slightly pointless (one more than a random number is as random as the number itself), so I do feel justified with just returning a random number here.
Am I wrong with that reasoning?
Looks to me you are not accounting for the fact that N = 512 is too small and a random number falling in the interval (q - 2) < X < 2^N is unsuitable?
Only if (q - 2) < 2^N. And my point is that it's not.
Cheers,
Hannes
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Re: [PATCH 09/11] nvmet: Implement basic In-Band Authentication
In reply to Stephan MuellerQuoted 59 lines · Stephan Mueller
Am Montag, dem 19.07.2021 um 11:57 +0200 schrieb Hannes Reinecke:
Quoted 58 lines · Stephan Mueller
Quoted 45 lines
Am Montag, dem 19.07.2021 um 10:15 +0200 schrieb Hannes Reinecke:
Quoted 36 lines · Stephan Müller
Quoted 35 lines
Am Sonntag, 18. Juli 2021, 14:37:34 CEST schrieb Hannes Reinecke:
Quoted 7 lines
The key is also used when using the ffdhe algorithm.
Note: I _think_ that I need to use this key for the ffdhe algorithm, because the implementation I came up with is essentially plain DH with
pre-defined 'p', 'q' and 'g' values. But the DH implementation also requires a 'key', and for that I'm using this key here.Quoted 6 lines
It might be that I'm completely off, and don't need to use a key for our
DH implementation. In that case you are correct.
(And that's why I said I'll need a review of the FFDHE implementation).
But for now I'll need the key for FFDHE.Do I understand you correctly that the
dhchap_keyis used as the input to
the
DH - i.e. it is the remote public key then? It looks strange that this is
used
for DH but then it is changed here by hashing it together with something
else
to form a newdhchap_key. Maybe that is what the protocol says. But it sounds
strange to me, especially when you think thatdhchap_keywould be, say,
2048
bits if it is truly the remote public key and then after the hashing it is
256
thisdhchap_keycannot be used for FFC-DH.Quoted 7 lines
Quoted 4 lines
Or are you using the
dhchap_keyfor two different purposes?It seems I miss something here.
No, not entirely. It's me who buggered it up.
I got carried away by the fact that there is acrypto_dh_encode_key()function, and thought I need to use it here.Quoted 8 lines
Thank you for clarifying that. It sounds to me that there is no defined protocol (or if there, I would be wondering how the code would have worked with a different implementation). Would it make sense to first specify a protocol for authentication and have it discussed? I personally think it is a
bit difficult to fully understand the protocol from the code and discuss protocol-level items based on the code.Oh, the protocol _is_ specified:
Quoted 7 lines
Quoted 2 lines
It's just that I have issues translating that spec onto what the kernel provides.
according to the naming conventions there in figures 447 and following:
- x and y: DH private key (kernel calls it secret set with
dh_set_secretor encoded into param.key)
x and y are defined in Figure 444: "Random numbers used as exponents in a DH exchange". Sections 8.13.5.3 "DH-HMAC-CHAP_Challenge Message" and 8.13.5.4 "DH-HMAC-CHAP_Reply Message" additionally specify that "x is a random number selected by the controller that shall be at least 256 bits long" and "y is a random number selected by the host that shall be at least 256 bits long".
So, x and y are just random numbers, no need to overcomplicate their generation in the way that is beyond of the standard scope.
Vlad
Herbert Xu
maintainerRe: [PATCH 09/11] nvmet: Implement basic In-Band Authentication
In reply to Hannes ReineckeQuoted 8 lines · Hannes Reinecke
Quoted 4 lines
drivers/nvme/target/fabrics-cmd-auth.c line 104
Quoted 2 lines
drivers/nvme/target/fabrics-cmd-auth.c new file +1
-0+ response =kmalloc (data->hl,GFP_KERNEL );Again, align to
CRYPTO_MINALIGN_ATTR?Ah, _that_ alignment.
Wasn't aware that I need to align to anything.
But if that's required, sure, I'll be fixing it.
Memory returned by kmalloc is guaranteed to be aligned to CRYPTO_MINALIGN_ATTR, in fact that's the whole point of that attribute.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Sagi Grimberg
maintainerRe: [PATCH 09/11] nvmet: Implement basic In-Band Authentication
In reply to Hannes ReineckeQuoted 102 lines · Hannes Reinecke
Implement support for NVMe-oF In-Band authentication. This patch adds two additional configfs entries '
dhchap_key' and 'dhchap_hash' to the 'host' configfs directory. The 'dhchap_key' needs to be specified in the format outlined in the base spec. Augmented challenge support is not implemented, and concatenation with TLS encryption is not supported.Signed-off-by Hannes Reinecke <[email protected]>--- drivers/nvme/target/Kconfig | 10 + drivers/nvme/target/Makefile | 1 + drivers/nvme/target/admin-cmd.c | 4 + drivers/nvme/target/auth.c | 352 +++++++++++++++++++ drivers/nvme/target/configfs.c | 71 +++- drivers/nvme/target/core.c | 8 + drivers/nvme/target/fabrics-cmd-auth.c | 460 +++++++++++++++++++++++++ drivers/nvme/target/fabrics-cmd.c | 30 +- drivers/nvme/target/nvmet.h | 71 ++++ 9 files changed, 1004 insertions(+), 3 deletions(-)create mode 100644
drivers/nvme/target/auth.ccreate mode 100644drivers/nvme/target/fabrics-cmd-auth.cdrivers/nvme/target/Kconfig +10
-0@@ -85,3 +85,13 @@ config NVME_TARGET_TCP devices over TCP. If unsure, say N. + +config NVME_TARGET_AUTH + bool "NVMe over Fabrics In-band Authentication support" + depends on NVME_TARGET + select CRYPTO_SHA256 + select CRYPTO_SHA512 + help + This enables support for NVMe over Fabrics In-band Authentication + + If unsure, say N.drivers/nvme/target/Makefile +1
-0@@ -13,6 +13,7 @@ nvmet-y += core.o configfs.o admin-cmd.o fabrics-cmd.o \ discovery.o io-cmd-file.o io-cmd-bdev.o nvmet-$(CONFIG_NVME_TARGET_PASSTHRU) += passthru.o nvmet-$(CONFIG_BLK_DEV_ZONED) += zns.o +nvmet-$(CONFIG_NVME_TARGET_AUTH) += fabrics-cmd-auth.o auth.o nvme-loop-y += loop.o nvmet-rdma-y += rdma.o nvmet-fc-y += fc.odrivers/nvme/target/admin-cmd.c +4
-0@@ -1008,6 +1008,10 @@ u16 nvmet_parse_admin_cmd(struct nvmet_req *req)if (nvme_is_fabrics (cmd))return nvmet_parse_fabrics_cmd (req); + +if (unlikely (!nvmet_check_auth_status (req))) +return NVME_SC_AUTH_REQUIRED |NVME_SC_DNR ; +if (nvmet_req_subsys (req)->type ==NVME_NQN_DISC )return nvmet_parse_discovery_cmd (req);drivers/nvme/target/auth.c new file +29
-0@@ -0,0 +1,352 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* NVMe over Fabrics DH-HMAC-CHAP authentication. +* Copyright (c) 2020 Hannes Reinecke, SUSE Software Solutions. +* All rights reserved. +*/ +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <linux/module.h> +#include <linux/init.h> +#include <linux/slab.h> +#include <linux/err.h> +#include <crypto/hash.h> +#include <crypto/kpp.h> +#include <crypto/dh.h> +#include <crypto/ffdhe.h> +#include <linux/crc32.h> +#include <linux/base64.h> +#include <linux/ctype.h> +#include <linux/random.h> +#include <asm/unaligned.h> + +#include "nvmet.h" +#include "../host/auth.h" + +int nvmet_auth_set_host_key (struct nvmet_host *host,const char *secret) +{ +if (sscanf (secret,"DHHC-1:%hhd:%*s" , &host->dhchap_key_hash) !=1 ) +return -EINVAL ; +if (host->dhchap_key_hash >3 ) {
drivers/nvme/target/auth.c line 39
Quoted 10 lines
drivers/nvme/target/auth.c new file +10
-0+pr_warn ("Invalid DH-HMAC-CHAP hash id %d\n" , + host->dhchap_key_hash); +return -EINVAL ; + } +if (host->dhchap_key_hash >0 ) { +/* Validate selected hash algorithm */ +const char *hmac =nvme_auth_hmac_name (host->dhchap_key_hash); + +if (!crypto_has_shash (hmac,0 ,0 )) { +pr_warn ("DH-HMAC-CHAP hash %s unsupported\n" , hmac);
pr_err
drivers/nvme/target/auth.c line 41
Quoted 2 lines
drivers/nvme/target/auth.c new file +2
-0+ host->dhchap_key_hash =-1 ; +return -EAGAIN ;
Why EAGAIN?
drivers/nvme/target/auth.c line 45
Quoted 4 lines
drivers/nvme/target/auth.c new file +4
-0+ } +/* Use this hash as default */ +if (!host->dhchap_hash_id) + host->dhchap_hash_id = host->dhchap_key_hash;
Why?
drivers/nvme/target/auth.c line 52
Quoted 7 lines
drivers/nvme/target/auth.c new file +7
-0+ } + host->dhchap_secret =kstrdup (secret,GFP_KERNEL ); +if (!host->dhchap_secret) +return -ENOMEM ; +/* Default to SHA256 */ +if (!host->dhchap_hash_id) + host->dhchap_hash_id =NVME_AUTH_DHCHAP_HASH_SHA256 ;
What is the thought here?
Quoted 27 lines
drivers/nvme/target/auth.c new file +27
-0+ +pr_debug ("Using hash %s\n" , +nvme_auth_hmac_name (host->dhchap_hash_id)); +return 0 ; +} + +int nvmet_setup_dhgroup (struct nvmet_ctrl *ctrl,int dhgroup_id) +{ +int ret = -ENOTSUPP ; + +if (dhgroup_id ==NVME_AUTH_DHCHAP_DHGROUP_NULL ) +return 0 ; + +return ret; +} + +int nvmet_setup_auth (struct nvmet_ctrl *ctrl,struct nvmet_req *req) +{ +int ret =0 ; +struct nvmet_host_link *p; +struct nvmet_host *host =NULL ; +const char *hash_name; + +down_read (&nvmet_config_sem); +if (ctrl->subsys->type ==NVME_NQN_DISC ) + goto out_unlock; +
drivers/nvme/target/auth.c line 89
Quoted 10 lines
drivers/nvme/target/auth.c new file +10
-0+list_for_each_entry (p, &ctrl->subsys->hosts, entry) { +pr_debug ("check %s\n" ,nvmet_host_name (p->host)); +if (strcmp (nvmet_host_name (p->host), ctrl->hostnqn)) +continue ; + host = p->host; +break ; + } +if (!host) { +pr_debug ("host %s not found\n" , ctrl->hostnqn); + ret = -EPERM ;
I think you should propogate the nvme status code instead...
drivers/nvme/target/auth.c line 98
Quoted 9 lines
drivers/nvme/target/auth.c new file +9
-0+ goto out_unlock; + } +if (!host->dhchap_secret) { +pr_debug ("No authentication provided\n" ); + goto out_unlock; + } + + hash_name =nvme_auth_hmac_name (host->dhchap_hash_id); +if (!hash_name) {
Can this actually happen?
drivers/nvme/target/auth.c line 99
Quoted 1 line
drivers/nvme/target/auth.c new file +1
-0+pr_debug ("Hash ID %d invalid\n" , host->dhchap_hash_id);
warning, not debug.
Quoted 55 lines
drivers/nvme/target/auth.c new file +55
-0+ ret = -EINVAL ; + goto out_unlock; + } + ctrl->shash_tfm =crypto_alloc_shash (hash_name,0 , +CRYPTO_ALG_ALLOCATES_MEMORY ); +if (IS_ERR (ctrl->shash_tfm)) { +pr_debug ("failed to allocate shash %s\n" , hash_name); + ret =PTR_ERR (ctrl->shash_tfm); + ctrl->shash_tfm =NULL ; + goto out_unlock; + } + + ctrl->dhchap_key =nvme_auth_extract_secret (host->dhchap_secret, + &ctrl->dhchap_key_len); +if (IS_ERR (ctrl->dhchap_key)) { +pr_debug ("failed to extract host key, error %d\n" , ret); + ret =PTR_ERR (ctrl->dhchap_key); + ctrl->dhchap_key =NULL ; + goto out_free_hash; + } +if (host->dhchap_key_hash) { +struct crypto_shash *key_tfm; + + hash_name =nvme_auth_hmac_name (host->dhchap_key_hash); + key_tfm =crypto_alloc_shash (hash_name,0 ,0 ); +if (IS_ERR (key_tfm)) { + ret =PTR_ERR (key_tfm); + goto out_free_hash; + }else { +SHASH_DESC_ON_STACK (shash, key_tfm); + + shash->tfm = key_tfm; + ret =crypto_shash_setkey (key_tfm, ctrl->dhchap_key, + ctrl->dhchap_key_len); +crypto_shash_init (shash); +crypto_shash_update (shash, ctrl->subsys->subsysnqn, +strlen (ctrl->subsys->subsysnqn)); +crypto_shash_update (shash,"NVMe-over-Fabrics" ,17 ); +crypto_shash_final (shash, ctrl->dhchap_key); +crypto_free_shash (key_tfm); + } + } +pr_debug ("%s: using key %*ph\n" , __func__, + (int )ctrl->dhchap_key_len, ctrl->dhchap_key); + ret =crypto_shash_setkey (ctrl->shash_tfm, ctrl->dhchap_key, + ctrl->dhchap_key_len); +out_free_hash: +if (ret) { +if (ctrl->dhchap_key) { +kfree (ctrl->dhchap_key); + ctrl->dhchap_key =NULL ; + } +crypto_free_shash (ctrl->shash_tfm); + ctrl->shash_tfm =NULL ; + }
drivers/nvme/target/auth.c line 164
Quoted 10 lines
drivers/nvme/target/auth.c new file +10
-0+out_unlock: +up_read (&nvmet_config_sem); + +return ret; +} + +void nvmet_auth_sq_free (struct nvmet_sq *sq) +{ +if (sq->dhchap_c1) +kfree (sq->dhchap_c1);
just kfree, no need to if
drivers/nvme/target/auth.c line 171
Quoted 7 lines
drivers/nvme/target/auth.c new file +7
-0+if (sq->dhchap_c2) +kfree (sq->dhchap_c2); +if (sq->dhchap_skey) +kfree (sq->dhchap_skey); +} + +void nvmet_reset_auth (struct nvmet_ctrl *ctrl)
Shouldn't this be nvmet_destroy_auth? reset indicates it can be reused again...
Quoted 63 lines
drivers/nvme/target/auth.c new file +63
-0+{ +if (ctrl->shash_tfm) { +crypto_free_shash (ctrl->shash_tfm); + ctrl->shash_tfm =NULL ; + } +if (ctrl->dh_tfm) { +crypto_free_kpp (ctrl->dh_tfm); + ctrl->dh_tfm =NULL ; + } +if (ctrl->dhchap_key) { +kfree (ctrl->dhchap_key); + ctrl->dhchap_key =NULL ; + } +} + +bool nvmet_check_auth_status (struct nvmet_req *req) +{ +if (req->sq->ctrl->shash_tfm && + !req->sq->authenticated) +return false; +return true; +} + +int nvmet_auth_host_hash (struct nvmet_req *req,u8 *response, +unsigned int shash_len) +{ +struct nvmet_ctrl *ctrl = req->sq->ctrl; +SHASH_DESC_ON_STACK (shash, ctrl->shash_tfm); +u8 *challenge = req->sq->dhchap_c1; +u8 buf[4 ]; +int ret; + +if (ctrl->dh_gid !=NVME_AUTH_DHCHAP_DHGROUP_NULL ) { + ret = -ENOTSUPP ; + goto out; + } + + shash->tfm = ctrl->shash_tfm; + ret =crypto_shash_init (shash); +if (ret) + goto out; + ret =crypto_shash_update (shash, challenge, shash_len); +if (ret) + goto out; +put_unaligned_le32 (req->sq->dhchap_s1, buf); + ret =crypto_shash_update (shash, buf,4 ); +if (ret) + goto out; +put_unaligned_le16 (req->sq->dhchap_tid, buf); + ret =crypto_shash_update (shash, buf,2 ); +if (ret) + goto out; +memset (buf,0 ,4 ); + ret =crypto_shash_update (shash, buf,1 ); +if (ret) + goto out; + ret =crypto_shash_update (shash,"HostHost" ,8 ); +if (ret) + goto out; + ret =crypto_shash_update (shash, ctrl->hostnqn,strlen (ctrl->hostnqn)); +if (ret) + goto out; + ret =crypto_shash_update (shash, buf,1 );
drivers/nvme/target/auth.c line 244
Quoted 10 lines
drivers/nvme/target/auth.c new file +10
-0+if (ret) + goto out; + ret =crypto_shash_update (shash, ctrl->subsysnqn, +strlen (ctrl->subsysnqn)); +if (ret) + goto out; + ret =crypto_shash_final (shash, response); +out: +if (challenge != req->sq->dhchap_c1) +kfree (challenge);
What about actually failing?
Quoted 185 lines
drivers/nvme/target/auth.c new file +108
-0+return 0 ; +} + +int nvmet_auth_ctrl_hash (struct nvmet_req *req,u8 *response, +unsigned int shash_len) +{ +struct nvmet_ctrl *ctrl = req->sq->ctrl; +SHASH_DESC_ON_STACK (shash, ctrl->shash_tfm); +u8 *challenge = req->sq->dhchap_c2; +u8 buf[4 ]; +int ret; + +pr_debug ("%s: ctrl %d hash seq %d transaction %u\n" , __func__, + ctrl->cntlid, req->sq->dhchap_s2, req->sq->dhchap_tid); +pr_debug ("%s: ctrl %d challenge %*ph\n" , __func__, + ctrl->cntlid, shash_len, req->sq->dhchap_c2); +pr_debug ("%s: ctrl %d subsysnqn %s\n" , __func__, + ctrl->cntlid, ctrl->subsysnqn); +pr_debug ("%s: ctrl %d hostnqn %s\n" , __func__, + ctrl->cntlid, ctrl->hostnqn); + +if (ctrl->dh_gid !=NVME_AUTH_DHCHAP_DHGROUP_NULL ) { + ret = -ENOTSUPP ; + goto out; + } + + shash->tfm = ctrl->shash_tfm; + ret =crypto_shash_init (shash); +if (ret) + goto out; + ret =crypto_shash_update (shash, challenge, shash_len); +if (ret) + goto out; +put_unaligned_le32 (req->sq->dhchap_s2, buf); + ret =crypto_shash_update (shash, buf,4 ); +if (ret) + goto out; +put_unaligned_le16 (req->sq->dhchap_tid, buf); + ret =crypto_shash_update (shash, buf,2 ); +if (ret) + goto out; +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->subsysnqn, +strlen (ctrl->subsysnqn)); +if (ret) + goto out; + ret =crypto_shash_update (shash, buf,1 ); +if (ret) + goto out; + ret =crypto_shash_update (shash, ctrl->hostnqn,strlen (ctrl->hostnqn)); +if (ret) + goto out; + ret =crypto_shash_final (shash, response); +out: +if (challenge != req->sq->dhchap_c2) +kfree (challenge); +return 0 ; +} + +int nvmet_auth_ctrl_sesskey (struct nvmet_req *req, +u8 *pkey,int pkey_size) +{ +struct nvmet_ctrl *ctrl = req->sq->ctrl; +struct kpp_request *kpp_req; +struct crypto_wait wait; +struct scatterlist src, dst; +int ret; + + req->sq->dhchap_skey_len = +nvme_auth_dhgroup_privkey_size (ctrl->dh_gid); + req->sq->dhchap_skey =kzalloc (req->sq->dhchap_skey_len,GFP_KERNEL ); +if (!req->sq->dhchap_skey) +return -ENOMEM ; + kpp_req =kpp_request_alloc (ctrl->dh_tfm,GFP_KERNEL ); +if (!kpp_req) { +kfree (req->sq->dhchap_skey); + req->sq->dhchap_skey =NULL ; +return -ENOMEM ; + } + +pr_debug ("%s: host public key %*ph\n" , __func__, + (int )pkey_size, pkey); +crypto_init_wait (&wait); +sg_init_one (&src, pkey, pkey_size); +kpp_request_set_input (kpp_req, &src, pkey_size); +sg_init_one (&dst, req->sq->dhchap_skey, + req->sq->dhchap_skey_len); +kpp_request_set_output (kpp_req, &dst, req->sq->dhchap_skey_len); +kpp_request_set_callback (kpp_req,CRYPTO_TFM_REQ_MAY_BACKLOG , + crypto_req_done, &wait); + + ret =crypto_wait_req (crypto_kpp_compute_shared_secret (kpp_req), &wait); +kpp_request_free (kpp_req); +if (ret) +pr_debug ("failed to compute shared secred, err %d\n" , ret); +else +pr_debug ("%s: shared secret %*ph\n" , __func__, + (int )req->sq->dhchap_skey_len, + req->sq->dhchap_skey); + +return ret; +}drivers/nvme/target/configfs.c +56
-1@@ -11,8 +11,13 @@#include <linux/ctype.h> #include <linux/pci.h> #include <linux/pci-p2pdma.h> +#include <crypto/hash.h> +#include <crypto/kpp.h> #include "nvmet.h" +#ifdef CONFIG_NVME_TARGET_AUTH +#include "../host/auth.h" +#endif static const struct config_item_type nvmet_host_type;static const struct config_item_type nvmet_subsys_type;@@ -1656,10 +1661,71 @@ static const struct config_item_type nvmet_ports_type = {static struct config_group nvmet_subsystems_group;static struct config_group nvmet_ports_group; -static void nvmet_host_release (struct config_item *item) +#ifdef CONFIG_NVME_TARGET_AUTH +static ssize_t nvmet_host_dhchap_key_show (struct config_item *item, +char *page) +{ +u8 *dhchap_secret =to_host (item)->dhchap_secret; + +if (!dhchap_secret) +return sprintf (page,"\n" ); +return sprintf (page,"%s\n" , dhchap_secret); +} + +static ssize_t nvmet_host_dhchap_key_store (struct config_item *item, +const char *page,size_t count) {struct nvmet_host *host =to_host (item); +int ret; + ret =nvmet_auth_set_host_key (host, page); +if (ret <0 ) +return ret; +return count; +} + +CONFIGFS_ATTR (nvmet_host_, dhchap_key); + +static ssize_t nvmet_host_dhchap_hash_show (struct config_item *item, +char *page) +{ +struct nvmet_host *host =to_host (item); +const char *hash_name =nvme_auth_hmac_name (host->dhchap_hash_id); + +return sprintf (page,"%s\n" , hash_name ? hash_name :"none" ); +} + +static ssize_t nvmet_host_dhchap_hash_store (struct config_item *item, +const char *page,size_t count) +{ +struct nvmet_host *host =to_host (item); +int hmac_id; + + hmac_id =nvme_auth_hmac_id (page); +if (hmac_id <0 ) +return -EINVAL ; +if (!crypto_has_shash (nvme_auth_hmac_name (hmac_id),0 ,0 )) +return -ENOTSUPP ; + host->dhchap_hash_id = hmac_id; +return count; +} + +CONFIGFS_ATTR (nvmet_host_, dhchap_hash); + +static struct configfs_attribute *nvmet_host_attrs[] = { + &nvmet_host_attr_dhchap_key, + &nvmet_host_attr_dhchap_hash,
drivers/nvme/target/configfs.c static const struct config_item_type nvmet_ports_type = { · line 1727
Quoted 10 lines
drivers/nvme/target/configfs.c +10
-0+ NULL, +}; +#endif /* CONFIG_NVME_TARGET_AUTH */ + +static void nvmet_host_release (struct config_item *item) +{ +struct nvmet_host *host =to_host (item); +#ifdef CONFIG_NVME_TARGET_AUTH +if (host->dhchap_secret) +kfree (host->dhchap_secret);
No need for if condition.
Quoted 63 lines
drivers/nvme/target/configfs.c +4
-0+#endif kfree (host); }@@ -1669,6 +1735,9 @@ static struct configfs_item_operations nvmet_host_item_ops = {static const struct config_item_type nvmet_host_type = { .ct_item_ops = &nvmet_host_item_ops, +#ifdef CONFIG_NVME_TARGET_AUTH + .ct_attrs = nvmet_host_attrs, +#endif .ct_owner =THIS_MODULE , };drivers/nvme/target/core.c +8
-0@@ -793,6 +793,7 @@ void nvmet_sq_destroy(struct nvmet_sq *sq)wait_for_completion (&sq->confirm_done);wait_for_completion (&sq->free_done);percpu_ref_exit (&sq->ref); +nvmet_auth_sq_free (sq);if (ctrl) {/* @@ -1264,6 +1265,11 @@ u16 nvmet_check_ctrl_status(struct nvmet_req *req) req->cmd->common.opcode, req->sq->qid);return NVME_SC_CMD_SEQ_ERROR |NVME_SC_DNR ; } + +if (unlikely (!nvmet_check_auth_status (req))) { +pr_warn ("qid %d not authenticated\n" , req->sq->qid); +return NVME_SC_AUTH_REQUIRED |NVME_SC_DNR ; + }return 0 ; }@@ -1456,6 +1462,8 @@ static void nvmet_ctrl_free(struct kref *ref)flush_work (&ctrl->async_event_work);cancel_work_sync (&ctrl->fatal_err_work); +nvmet_reset_auth (ctrl); +ida_simple_remove (&cntlid_ida, ctrl->cntlid);nvmet_async_events_free (ctrl);drivers/nvme/target/fabrics-cmd-auth.c new file +10
-0@@ -0,0 +1,460 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* NVMe over Fabrics DH-HMAC-CHAP authentication command handling. +* Copyright (c) 2020 Hannes Reinecke, SUSE Software Solutions. +* All rights reserved. +*/ +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <linux/blkdev.h> +#include <linux/random.h> +#include <crypto/hash.h>
drivers/nvme/target/fabrics-cmd-auth.c line 20
Quoted 10 lines
drivers/nvme/target/fabrics-cmd-auth.c new file +10
-0+#include <crypto/kpp.h> +#include "nvmet.h" +#include "../host/auth.h" + +void nvmet_init_auth (struct nvmet_ctrl *ctrl,struct nvmet_req *req) +{ +/* Initialize in-band authentication */ + req->sq->authenticated = false; + req->sq->dhchap_step =NVME_AUTH_DHCHAP_MESSAGE_NEGOTIATE ; + req->cqe->result.u32 |=0x2 <<16 ;
Can you add a define for this: NVME_CONNECT_AUTHREQ_INBAND
Quoted 166 lines
drivers/nvme/target/fabrics-cmd-auth.c new file +166
-0+} + +static u16 nvmet_auth_negotiate (struct nvmet_req *req,void *d) +{ +struct nvmet_ctrl *ctrl = req->sq->ctrl; +struct nvmf_auth_dhchap_negotiate_data *data = d; +int i, hash_id, null_dh =-1 ; + +pr_debug ("%s: ctrl %d qid %d: data sc_d %d napd %d authid %d halen %d dhlen %d\n" , + __func__, ctrl->cntlid, req->sq->qid, + data->sc_c, data->napd, data->auth_protocol[0 ].dhchap.authid, + data->auth_protocol[0 ].dhchap.halen, + data->auth_protocol[0 ].dhchap.dhlen); + req->sq->dhchap_tid =le16_to_cpu (data->t_id); +if (data->sc_c) +return NVME_AUTH_DHCHAP_FAILURE_CONCAT_MISMATCH ; + +if (data->napd !=1 ) +return NVME_AUTH_DHCHAP_FAILURE_HASH_UNUSABLE ; + +if (data->auth_protocol[0 ].dhchap.authid !=0x01 ) +return NVME_AUTH_DHCHAP_FAILURE_INVALID_PAYLOAD ; + + hash_id =nvme_auth_hmac_id (crypto_shash_alg_name (ctrl->shash_tfm)); +for (i =0 ; i < data->auth_protocol[0 ].dhchap.halen; i++) { +pr_debug ("%s: ctrl %d qid %d checking hash %d for %d\n" , + __func__, ctrl->cntlid, req->sq->qid, + data->auth_protocol[0 ].dhchap.idlist[i], hash_id); +if (hash_id != data->auth_protocol[0 ].dhchap.idlist[i]) +continue ; + req->sq->dhchap_hash_id = hash_id; + req->sq->dhchap_hash_len =crypto_shash_digestsize (ctrl->shash_tfm); +break ; + } +if (req->sq->dhchap_hash_id ==0 ) { +pr_debug ("%s: ctrl %d qid %d: no usable hash found\n" , + __func__, ctrl->cntlid, req->sq->qid); +return NVME_AUTH_DHCHAP_FAILURE_HASH_UNUSABLE ; + } + +for (i = data->auth_protocol[0 ].dhchap.halen; + i < data->auth_protocol[0 ].dhchap.halen + + data->auth_protocol[0 ].dhchap.dhlen; i++) { +int dhgid = data->auth_protocol[0 ].dhchap.idlist[i]; + +if (dhgid ==NVME_AUTH_DHCHAP_DHGROUP_NULL ) { + null_dh = dhgid; +continue ; + } +if (nvmet_setup_dhgroup (ctrl, dhgid) ==0 ) +break ; + } +if (!ctrl->dh_tfm && null_dh <0 ) { +pr_debug ("%s: ctrl %d qid %d: no DH group selected\n" , + __func__, ctrl->cntlid, req->sq->qid); +return NVME_AUTH_DHCHAP_FAILURE_DHGROUP_UNUSABLE ; + } +if (ctrl->dh_gid ==-1 ) { + ctrl->dh_gid = null_dh; + ctrl->dh_tfm =NULL ; + } +pr_debug ("%s: ctrl %d qid %d: DH group %s (%d)\n" , + __func__, ctrl->cntlid, req->sq->qid, +nvme_auth_dhgroup_name (ctrl->dh_gid), ctrl->dh_gid); +return 0 ; +} + +static u16 nvmet_auth_reply (struct nvmet_req *req,void *d) +{ +struct nvmet_ctrl *ctrl = req->sq->ctrl; +struct nvmf_auth_dhchap_reply_data *data = d; +u8 *response; + +pr_debug ("%s: ctrl %d qid %d: data hl %d cvalid %d dhvlen %d\n" , + __func__, ctrl->cntlid, req->sq->qid, + data->hl, data->cvalid, data->dhvlen); +if (data->hl != req->sq->dhchap_hash_len) +return NVME_AUTH_DHCHAP_FAILURE_INVALID_PAYLOAD ; + +if (data->dhvlen) { +return NVME_AUTH_DHCHAP_FAILURE_INVALID_PAYLOAD ; + } + + response =kmalloc (data->hl,GFP_KERNEL ); +if (!response) +return NVME_AUTH_DHCHAP_FAILURE_FAILED ; + +if (nvmet_auth_host_hash (req, response, data->hl) <0 ) { +pr_debug ("ctrl %d qid %d DH-HMAC-CHAP hash failed\n" , + ctrl->cntlid, req->sq->qid); +kfree (response); +return NVME_AUTH_DHCHAP_FAILURE_FAILED ; + } + +if (memcmp (data->rval, response, data->hl)) { +pr_info ("ctrl %d qid %d DH-HMAC-CHAP response mismatch\n" , + ctrl->cntlid, req->sq->qid); +kfree (response); +return NVME_AUTH_DHCHAP_FAILURE_FAILED ; + } +kfree (response); +pr_info ("ctrl %d qid %d DH-HMAC-CHAP host authenticated\n" , + ctrl->cntlid, req->sq->qid); +if (data->cvalid) { + req->sq->dhchap_c2 =kmalloc (data->hl,GFP_KERNEL ); +if (!req->sq->dhchap_c2) +return NVME_AUTH_DHCHAP_FAILURE_FAILED ; +memcpy (req->sq->dhchap_c2, data->rval + data->hl, data->hl); + +pr_debug ("ctrl %d qid %d challenge %*ph\n" , + ctrl->cntlid, req->sq->qid, data->hl, + req->sq->dhchap_c2); + req->sq->dhchap_s2 =le32_to_cpu (data->seqnum); + }else + req->sq->dhchap_c2 =NULL ; + +return 0 ; +} + +static u16 nvmet_auth_failure2 (struct nvmet_req *req,void *d) +{ +struct nvmf_auth_dhchap_failure_data *data = d; + +return data->reason_code_explanation; +} + +void nvmet_execute_auth_send (struct nvmet_req *req) +{ +struct nvmet_ctrl *ctrl = req->sq->ctrl; +struct nvmf_auth_dhchap_success2_data *data; +void *d; +u32 tl; +u16 status =0 ; + +if (req->cmd->auth_send.secp !=NVME_AUTH_DHCHAP_PROTOCOL_IDENTIFIER ) { + status =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + req->error_loc = + offsetof(struct nvmf_auth_send_command , secp); + goto done; + } +if (req->cmd->auth_send.spsp0 !=0x01 ) { + status =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + req->error_loc = + offsetof(struct nvmf_auth_send_command , spsp0); + goto done; + } +if (req->cmd->auth_send.spsp1 !=0x01 ) { + status =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + req->error_loc = + offsetof(struct nvmf_auth_send_command , spsp1); + goto done; + } + tl =le32_to_cpu (req->cmd->auth_send.tl); +if (!tl) { + status =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + req->error_loc = + offsetof(struct nvmf_auth_send_command , tl); + goto done; + } +if (!nvmet_check_transfer_len (req, tl)) { +pr_debug ("%s: transfer length mismatch (%u)\n" , __func__, tl); +return ; + } + + d =kmalloc (tl,GFP_KERNEL ); +if (!d) {
drivers/nvme/target/fabrics-cmd-auth.c line 196
Quoted 10 lines
drivers/nvme/target/fabrics-cmd-auth.c new file +10
-0+ status =NVME_SC_INTERNAL ; + goto done; + } + + status =nvmet_copy_from_sgl (req,0 , d, tl); +if (status) { +kfree (d); + goto done; + } +
This whole block below should move to something like nvmet_process_auth_send_data()
Quoted 89 lines
drivers/nvme/target/fabrics-cmd-auth.c new file +89
-0+ data = d; +pr_debug ("%s: ctrl %d qid %d type %d id %d step %x\n" , __func__, + ctrl->cntlid, req->sq->qid, data->auth_type, data->auth_id, + req->sq->dhchap_step); +if (data->auth_type !=NVME_AUTH_COMMON_MESSAGES && + data->auth_type !=NVME_AUTH_DHCHAP_MESSAGES ) { + req->sq->dhchap_step =NVME_AUTH_DHCHAP_MESSAGE_FAILURE1 ; + req->sq->dhchap_status =NVME_AUTH_DHCHAP_FAILURE_INVALID_MESSAGE ; + }else if (data->auth_type ==NVME_AUTH_COMMON_MESSAGES ) { +if (data->auth_id != req->sq->dhchap_step) { + req->sq->dhchap_step =NVME_AUTH_DHCHAP_MESSAGE_FAILURE1 ; + req->sq->dhchap_status =NVME_AUTH_DHCHAP_FAILURE_INVALID_MESSAGE ; + }else if (data->auth_id !=NVME_AUTH_DHCHAP_MESSAGE_NEGOTIATE ) { + req->sq->dhchap_step =NVME_AUTH_DHCHAP_MESSAGE_FAILURE1 ; + req->sq->dhchap_status =NVME_AUTH_DHCHAP_FAILURE_INVALID_MESSAGE ; + }else { +/* Validate negotiation parameters */ + status =nvmet_auth_negotiate (req, d); +if (status ==0 ) + req->sq->dhchap_step =NVME_AUTH_DHCHAP_MESSAGE_CHALLENGE ; +else { + req->sq->dhchap_step =NVME_AUTH_DHCHAP_MESSAGE_FAILURE1 ; + req->sq->dhchap_status = status; + status =0 ; + } + } + }else if (data->auth_type ==NVME_AUTH_DHCHAP_MESSAGES ) { +if (data->auth_id != req->sq->dhchap_step) { +pr_debug ("%s: ctrl %d qid %d step mismatch (%d != %d)\n" , + __func__, ctrl->cntlid, req->sq->qid, + data->auth_id, req->sq->dhchap_step); + req->sq->dhchap_step =NVME_AUTH_DHCHAP_MESSAGE_FAILURE1 ; + req->sq->dhchap_status =NVME_AUTH_DHCHAP_FAILURE_INVALID_MESSAGE ; + }else if (le16_to_cpu (data->t_id) != req->sq->dhchap_tid) { +pr_debug ("%s: ctrl %d qid %d invalid transaction %d (expected %d)\n" , + __func__, ctrl->cntlid, req->sq->qid, +le16_to_cpu (data->t_id), + req->sq->dhchap_tid); + req->sq->dhchap_step =NVME_AUTH_DHCHAP_MESSAGE_FAILURE1 ; + req->sq->dhchap_status =NVME_AUTH_DHCHAP_FAILURE_INVALID_PAYLOAD ; + }else { +switch (data->auth_id) { +case NVME_AUTH_DHCHAP_MESSAGE_REPLY : + status =nvmet_auth_reply (req, d); +if (status ==0 ) + req->sq->dhchap_step =NVME_AUTH_DHCHAP_MESSAGE_SUCCESS1 ; +else { + req->sq->dhchap_step =NVME_AUTH_DHCHAP_MESSAGE_FAILURE1 ; + req->sq->dhchap_status = status; + status =0 ; + } +break ; +case NVME_AUTH_DHCHAP_MESSAGE_SUCCESS2 : + req->sq->authenticated = true; +pr_debug ("%s: ctrl %d qid %d authenticated\n" , + __func__, ctrl->cntlid, req->sq->qid); +break ; +case NVME_AUTH_DHCHAP_MESSAGE_FAILURE2 : + status =nvmet_auth_failure2 (req, d); +if (status) { +pr_warn ("ctrl %d qid %d: DH-HMAC-CHAP negotiation failed (%d)\n" , + ctrl->cntlid, req->sq->qid, + status); + req->sq->dhchap_status = status; + status =0 ; + } +break ; +default : + req->sq->dhchap_status =NVME_AUTH_DHCHAP_FAILURE_INVALID_MESSAGE ; + req->sq->dhchap_step =NVME_AUTH_DHCHAP_MESSAGE_FAILURE2 ; +break ; + } + } + }else { + req->sq->dhchap_status =NVME_AUTH_DHCHAP_FAILURE_INVALID_MESSAGE ; + req->sq->dhchap_step =NVME_AUTH_DHCHAP_MESSAGE_FAILURE2 ; + } +kfree (d); +done: +pr_debug ("%s: ctrl %d qid %d dhchap status %x step %x\n" , __func__, + ctrl->cntlid, req->sq->qid, + req->sq->dhchap_status, req->sq->dhchap_step); +if (status) +pr_debug ("%s: ctrl %d qid %d nvme status %x error loc %d\n" , + __func__, ctrl->cntlid, req->sq->qid, + status, req->error_loc); + req->cqe->result.u64 =0 ; +nvmet_req_complete (req, status); + if (req->sq->dhchap_step != NVME_AUTH_DHCHAP_MESSAGE_SUCCESS2 &&
drivers/nvme/target/fabrics-cmd-auth.c line 295
Quoted 10 lines
drivers/nvme/target/fabrics-cmd-auth.c new file +10
-0+ req->sq->dhchap_step != NVME_AUTH_DHCHAP_MESSAGE_FAILURE2) + return; +/* Final states, clear up variables */ +kfree (req->sq->dhchap_c1); +kfree (req->sq->dhchap_c2); +if (req->sq->dhchap_step ==NVME_AUTH_DHCHAP_MESSAGE_FAILURE2 ) +nvmet_ctrl_fatal_error (ctrl); +} + +static int nvmet_auth_challenge (struct nvmet_req *req,void *d,int al)
nvmet_auth_set_challange
Quoted 227 lines
drivers/nvme/target/fabrics-cmd-auth.c new file +165
-0+{ +struct nvmf_auth_dhchap_challenge_data *data = d; +struct nvmet_ctrl *ctrl = req->sq->ctrl; +int ret =0 ; +int data_size =sizeof (*d) + req->sq->dhchap_hash_len; + +if (al < data_size) { +pr_debug ("%s: buffer too small (al %d need %d)\n" , __func__, + al, data_size); +return -EINVAL ; + } +memset (data,0 , data_size); + req->sq->dhchap_s1 = ctrl->dhchap_seqnum++; + data->auth_type =NVME_AUTH_DHCHAP_MESSAGES ; + data->auth_id =NVME_AUTH_DHCHAP_MESSAGE_CHALLENGE ; + data->t_id =cpu_to_le16 (req->sq->dhchap_tid); + data->hashid = req->sq->dhchap_hash_id; + data->hl = req->sq->dhchap_hash_len; + data->seqnum =cpu_to_le32 (req->sq->dhchap_s1); + req->sq->dhchap_c1 =kmalloc (data->hl,GFP_KERNEL ); +if (!req->sq->dhchap_c1) +return -ENOMEM ; +get_random_bytes (req->sq->dhchap_c1, data->hl); +memcpy (data->cval, req->sq->dhchap_c1, data->hl); +pr_debug ("%s: ctrl %d qid %d seq %d transaction %d hl %d dhvlen %d\n" , + __func__, ctrl->cntlid, req->sq->qid, req->sq->dhchap_s1, + req->sq->dhchap_tid, data->hl, data->dhvlen); +return ret; +} + +static int nvmet_auth_success1 (struct nvmet_req *req,void *d,int al) +{ +struct nvmf_auth_dhchap_success1_data *data = d; +struct nvmet_ctrl *ctrl = req->sq->ctrl; + +WARN_ON (al <sizeof (*data)); +memset (data,0 ,sizeof (*data)); + data->auth_type =NVME_AUTH_DHCHAP_MESSAGES ; + data->auth_id =NVME_AUTH_DHCHAP_MESSAGE_SUCCESS1 ; + data->t_id =cpu_to_le16 (req->sq->dhchap_tid); + data->hl = req->sq->dhchap_hash_len; +if (req->sq->dhchap_c2) { +if (nvmet_auth_ctrl_hash (req, data->rval, data->hl)) +return NVME_AUTH_DHCHAP_FAILURE_HASH_UNUSABLE ; + data->rvalid =1 ; +pr_debug ("ctrl %d qid %d response %*ph\n" , + ctrl->cntlid, req->sq->qid, data->hl, data->rval); + } +return 0 ; +} + +static void nvmet_auth_failure1 (struct nvmet_req *req,void *d,int al) +{ +struct nvmf_auth_dhchap_failure_data *data = d; + +WARN_ON (al <sizeof (*data)); + data->auth_type =NVME_AUTH_COMMON_MESSAGES ; + data->auth_id =NVME_AUTH_DHCHAP_MESSAGE_FAILURE1 ; + data->t_id =cpu_to_le32 (req->sq->dhchap_tid); + data->reason_code =NVME_AUTH_DHCHAP_FAILURE_REASON_FAILED ; + data->reason_code_explanation = req->sq->dhchap_status; +} + +void nvmet_execute_auth_receive (struct nvmet_req *req) +{ +struct nvmet_ctrl *ctrl = req->sq->ctrl; +void *d; +u32 al; +u16 status =0 ; + +if (req->cmd->auth_receive.secp !=NVME_AUTH_DHCHAP_PROTOCOL_IDENTIFIER ) { + status =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + req->error_loc = + offsetof(struct nvmf_auth_receive_command , secp); + goto done; + } +if (req->cmd->auth_receive.spsp0 !=0x01 ) { + status =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + req->error_loc = + offsetof(struct nvmf_auth_receive_command , spsp0); + goto done; + } +if (req->cmd->auth_receive.spsp1 !=0x01 ) { + status =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + req->error_loc = + offsetof(struct nvmf_auth_receive_command , spsp1); + goto done; + } + al =le32_to_cpu (req->cmd->auth_receive.al); +if (!al) { + status =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + req->error_loc = + offsetof(struct nvmf_auth_receive_command , al); + goto done; + } +if (!nvmet_check_transfer_len (req, al)) { +pr_debug ("%s: transfer length mismatch (%u)\n" , __func__, al); +return ; + } + + d =kmalloc (al,GFP_KERNEL ); +if (!d) { + status =NVME_SC_INTERNAL ; + goto done; + } +pr_debug ("%s: ctrl %d qid %d step %x\n" , __func__, + ctrl->cntlid, req->sq->qid, req->sq->dhchap_step); +switch (req->sq->dhchap_step) { +case NVME_AUTH_DHCHAP_MESSAGE_CHALLENGE : + status =nvmet_auth_challenge (req, d, al); +if (status <0 ) { +pr_warn ("ctrl %d qid %d: challenge error (%d)\n" , + ctrl->cntlid, req->sq->qid, status); + status =NVME_SC_INTERNAL ; +break ; + } +if (status) { + req->sq->dhchap_status = status; +nvmet_auth_failure1 (req, d, al); +pr_warn ("ctrl %d qid %d: challenge status (%x)\n" , + ctrl->cntlid, req->sq->qid, + req->sq->dhchap_status); + status =0 ; +break ; + } + req->sq->dhchap_step =NVME_AUTH_DHCHAP_MESSAGE_REPLY ; +break ; +case NVME_AUTH_DHCHAP_MESSAGE_SUCCESS1 : + status =nvmet_auth_success1 (req, d, al); +if (status) { + req->sq->dhchap_status = status; +nvmet_auth_failure1 (req, d, al); +pr_warn ("ctrl %d qid %d: success1 status (%x)\n" , + ctrl->cntlid, req->sq->qid, + req->sq->dhchap_status); +break ; + } + req->sq->dhchap_step =NVME_AUTH_DHCHAP_MESSAGE_SUCCESS2 ; +break ; +case NVME_AUTH_DHCHAP_MESSAGE_FAILURE1 : +nvmet_auth_failure1 (req, d, al); +pr_warn ("ctrl %d qid %d failure1 (%x)\n" , + ctrl->cntlid, req->sq->qid, req->sq->dhchap_status); +break ; +default : +pr_warn ("ctrl %d qid %d unhandled step (%d)\n" , + ctrl->cntlid, req->sq->qid, req->sq->dhchap_step); + req->sq->dhchap_step =NVME_AUTH_DHCHAP_MESSAGE_FAILURE1 ; + req->sq->dhchap_status =NVME_AUTH_DHCHAP_FAILURE_FAILED ; +nvmet_auth_failure1 (req, d, al); + status =0 ; +break ; + } + + status =nvmet_copy_to_sgl (req,0 , d, al); +kfree (d); +done: + req->cqe->result.u64 =0 ; +nvmet_req_complete (req, status); +if (req->sq->dhchap_step ==NVME_AUTH_DHCHAP_MESSAGE_FAILURE1 ) { +kfree (req->sq->dhchap_c1); +kfree (req->sq->dhchap_c2); +nvmet_ctrl_fatal_error (ctrl); + } +}drivers/nvme/target/fabrics-cmd.c +25
-2@@ -93,6 +93,14 @@ u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req)case nvme_fabrics_type_property_get: req->execute = nvmet_execute_prop_get;break ; +#ifdef CONFIG_NVME_TARGET_AUTH +case nvme_fabrics_type_auth_send: + req->execute = nvmet_execute_auth_send; +break ; +case nvme_fabrics_type_auth_receive: + req->execute = nvmet_execute_auth_receive; +break ; +#endif default :pr_debug ("received unknown capsule type 0x%x\n" , cmd->fabrics.fctype);@@ -155,6 +163,7 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req)struct nvmf_connect_data *d;struct nvmet_ctrl *ctrl =NULL ;u16 status =0 ; +int ret;if (!nvmet_check_transfer_len (req,sizeof (struct nvmf_connect_data )))return ;@@ -197,17 +206,31 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req)uuid_copy (&ctrl->hostid, &d->hostid); + ret =nvmet_setup_auth (ctrl, req); +if (ret <0 ) { +pr_err ("Failed to setup authentication, error %d\n" , ret); +nvmet_ctrl_put (ctrl); +if (ret == -EPERM ) + status = (NVME_SC_CONNECT_INVALID_HOST |NVME_SC_DNR ); +else + status =NVME_SC_INTERNAL ; + goto out; + } + status =nvmet_install_queue (ctrl, req);if (status) {nvmet_ctrl_put (ctrl); goto out; } -pr_info ("creating controller %d for subsystem %s for NQN %s%s.\n" , +pr_info ("creating controller %d for subsystem %s for NQN %s%s%s.\n" , ctrl->cntlid, ctrl->subsys->subsysnqn, ctrl->hostnqn, - ctrl->pi_support ?" T10-PI is enabled" :"" ); + ctrl->pi_support ?" T10-PI is enabled" :"" , +nvmet_has_auth (ctrl) ?" with DH-HMAC-CHAP" :"" ); req->cqe->result.u16 =cpu_to_le16 (ctrl->cntlid); +if (nvmet_has_auth (ctrl)) +nvmet_init_auth (ctrl, req); out:kfree (d); complete:
drivers/nvme/target/fabrics-cmd.c static void nvmet_execute_io_connect(struct nvmet_req *req) · line 293
Quoted 4 lines
drivers/nvme/target/fabrics-cmd.c +1
-0@@ -267,6 +290,9 @@ static void nvmet_execute_io_connect(struct nvmet_req *req) }pr_debug ("adding queue %d to ctrl %d.\n" , qid, ctrl->cntlid); + req->cqe->result.u16 =cpu_to_le16 (ctrl->cntlid);
Is this related to the patch?
Quoted 97 lines
drivers/nvme/target/fabrics-cmd.c +2
-0+if (nvmet_has_auth (ctrl)) +nvmet_init_auth (ctrl, req); out:kfree (d);drivers/nvme/target/nvmet.h +62
-0@@ -108,6 +108,20 @@ struct nvmet_sq {u16 size;u32 sqhd;bool sqhd_disabled; +#ifdef CONFIG_NVME_TARGET_AUTH +bool authenticated; +u16 dhchap_tid; +u16 dhchap_status; +int dhchap_step; +u8 dhchap_hash_id; +u8 dhchap_hash_len; +u8 *dhchap_c1; +u8 *dhchap_c2; +u32 dhchap_s1; +u32 dhchap_s2; +u8 *dhchap_skey; +int dhchap_skey_len; +#endif struct completion free_done;struct completion confirm_done; };@@ -209,6 +223,15 @@ struct nvmet_ctrl {u64 err_counter;struct nvme_error_slot slots[NVMET_ERROR_LOG_SLOTS ];bool pi_support; +#ifdef CONFIG_NVME_TARGET_AUTH +u32 dhchap_seqnum; +u8 *dhchap_key; +size_t dhchap_key_len; +struct crypto_shash *shash_tfm; +struct crypto_kpp *dh_tfm; +u32 dh_gid; +u32 dh_keysize; +#endif };struct nvmet_subsys {@@ -270,6 +293,10 @@ static inline struct nvmet_subsys *namespaces_to_subsys(struct nvmet_host {struct config_group group; +u8 *dhchap_secret; +u8 dhchap_key_hash; +u8 dhchap_hash_id; +u8 dhchap_dhgroup_id; };static inline struct nvmet_host *to_host (struct config_item *item)@@ -659,4 +686,48 @@ static inline void nvmet_req_bio_put(struct nvmet_req *req, struct bio *bio)bio_put (bio); } +#ifdef CONFIG_NVME_TARGET_AUTH +void nvmet_execute_auth_send (struct nvmet_req *req); +void nvmet_execute_auth_receive (struct nvmet_req *req); +int nvmet_auth_set_host_key (struct nvmet_host *host,const char *secret); +int nvmet_auth_set_host_hash (struct nvmet_host *host,const char *hash); +int nvmet_setup_auth (struct nvmet_ctrl *ctrl,struct nvmet_req *req); +void nvmet_init_auth (struct nvmet_ctrl *ctrl,struct nvmet_req *req); +void nvmet_reset_auth (struct nvmet_ctrl *ctrl); +void nvmet_auth_sq_free (struct nvmet_sq *sq); +int nvmet_setup_dhgroup (struct nvmet_ctrl *ctrl,int dhgroup_id); +bool nvmet_check_auth_status (struct nvmet_req *req); +int nvmet_auth_host_hash (struct nvmet_req *req,u8 *response, +unsigned int hash_len); +int nvmet_auth_ctrl_hash (struct nvmet_req *req,u8 *response, +unsigned int hash_len); +static inline bool nvmet_has_auth (struct nvmet_ctrl *ctrl) +{ +return ctrl->shash_tfm !=NULL ; +} +int nvmet_auth_ctrl_exponential (struct nvmet_req *req, +u8 *buf,int buf_size); +int nvmet_auth_ctrl_sesskey (struct nvmet_req *req, +u8 *buf,int buf_size); +#else +static inline int nvmet_setup_auth (struct nvmet_ctrl *ctrl, +struct nvmet_req *req) +{ +return 0 ; +} +static inline void nvmet_init_auth (struct nvmet_ctrl *ctrl, +struct nvmet_req *req) {}; +static inline void nvmet_reset_auth (struct nvmet_ctrl *ctrl) {}; +static inline void nvmet_auth_sq_free (struct nvmet_sq *sq) {}; +static inline bool nvmet_check_auth_status (struct nvmet_req *req) +{
drivers/nvme/target/nvmet.h static inline void nvmet_req_bio_put(struct nvmet_req *req, struct bio *bio) · line 733
Quoted 10 lines
drivers/nvme/target/nvmet.h +9
-0+return true; +} +static inline bool nvmet_has_auth (struct nvmet_ctrl *ctrl) +{ +return false; +} +static inline const char *nvmet_dhchap_dhgroup_name (int dhgid) {return NULL ; } +#endif +#endif /* _NVMET_H */
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Hannes Reinecke
maintainerRe: [PATCH 09/11] nvmet: Implement basic In-Band Authentication
Quoted 117 lines · Sagi Grimberg
Quoted 101 lines · Hannes Reinecke
Implement support for NVMe-oF In-Band authentication. This patch adds two additional configfs entries '
dhchap_key' and 'dhchap_hash' to the 'host' configfs directory. The 'dhchap_key' needs to be specified in the format outlined in the base spec. Augmented challenge support is not implemented, and concatenation with TLS encryption is not supported.Signed-off-by Hannes Reinecke <[email protected]>---
drivers/nvme/target/Kconfig | 10 + drivers/nvme/target/Makefile | 1 + drivers/nvme/target/admin-cmd.c | 4 + drivers/nvme/target/auth.c | 352 +++++++++++++++++++ drivers/nvme/target/configfs.c | 71 +++- drivers/nvme/target/core.c | 8 +drivers/nvme/target/fabrics-cmd-auth.c| 460 +++++++++++++++++++++++++ drivers/nvme/target/fabrics-cmd.c | 30 +- drivers/nvme/target/nvmet.h | 71 ++++9 files changed, 1004 insertions(+), 3 deletions(-)create mode 100644
drivers/nvme/target/auth.ccreate mode 100644drivers/nvme/target/fabrics-cmd-auth.cdrivers/nvme/target/Kconfig +0
-0@@ -85,3 +85,13 @@ config NVME_TARGET_TCPdevices over TCP.
If unsure, say N.drivers/nvme/target/Kconfig +10
-0+ +config NVME_TARGET_AUTH + bool "NVMe over Fabrics In-band Authentication support" + depends on NVME_TARGET + select CRYPTO_SHA256 + select CRYPTO_SHA512 + help + This enables support for NVMe over Fabrics In-band Authentication + + If unsure, say N.drivers/nvme/target/Makefile +0
-0@@ -13,6 +13,7 @@ nvmet-y += core.o configfs.o admin-cmd.o fabrics-cmd.o \discovery.o io-cmd-file.o io-cmd-bdev.o nvmet-$(CONFIG_NVME_TARGET_PASSTHRU) += passthru.o nvmet-$(CONFIG_BLK_DEV_ZONED) += zns.o +nvmet-$(CONFIG_NVME_TARGET_AUTH) += fabrics-cmd-auth.o auth.o nvme-loop-y += loop.o
nvmet-rdma-y += rdma.o
nvmet-fc-y += fc.odrivers/nvme/target/admin-cmd.c +4
-0@@ -1008,6 +1008,10 @@ u16 nvmet_parse_admin_cmd(struct nvmet_req *req) if (nvme_is_fabrics(cmd)) return nvmet_parse_fabrics_cmd(req); + + if (unlikely(!nvmet_check_auth_status(req))) + return NVME_SC_AUTH_REQUIRED | NVME_SC_DNR; + if (nvmet_req_subsys(req)->type == NVME_NQN_DISC) return nvmet_parse_discovery_cmd(req);diff --git
a/drivers/nvme/target/auth.cb/drivers/nvme/target/auth.cnew file mode 100644
index 000000000000..00c7d051dfb1drivers/nvme/target/auth.c new file +29
-0@@ -0,0 +1,352 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* NVMe over Fabrics DH-HMAC-CHAP authentication. +* Copyright (c) 2020 Hannes Reinecke, SUSE Software Solutions. +* All rights reserved. +*/ +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <linux/module.h> +#include <linux/init.h> +#include <linux/slab.h> +#include <linux/err.h> +#include <crypto/hash.h> +#include <crypto/kpp.h> +#include <crypto/dh.h> +#include <crypto/ffdhe.h> +#include <linux/crc32.h> +#include <linux/base64.h> +#include <linux/ctype.h> +#include <linux/random.h> +#include <asm/unaligned.h> + +#include "nvmet.h" +#include "../host/auth.h" + +int nvmet_auth_set_host_key (struct nvmet_host *host,const char *secret) +{ + if (sscanf(secret, "DHHC-1:%hhd:%*s", &host->dhchap_key_hash) != 1) + return -EINVAL; + if (host->dhchap_key_hash > 3) {drivers/nvme/target/auth.c line 39
Quoted 10 lines
drivers/nvme/target/auth.c new file +10
-0+ pr_warn("Invalid DH-HMAC-CHAP hash id %d\n", + host->dhchap_key_hash); + return -EINVAL; + } + if (host->dhchap_key_hash > 0) { + /* Validate selected hash algorithm */ + const char *hmac = nvme_auth_hmac_name(host->dhchap_key_hash); + + if (!crypto_has_shash(hmac, 0, 0)) { + pr_warn("DH-HMAC-CHAP hash %s unsupported\n", hmac);
pr_err
Quoted 5 lines
drivers/nvme/target/auth.c line 41
Quoted 2 lines
drivers/nvme/target/auth.c new file +2
-0+ host->dhchap_key_hash = -1; + return -EAGAIN;Why EAGAIN?
What else? ENOTSUPP?
Quoted 7 lines
drivers/nvme/target/auth.c line 45
Quoted 4 lines
drivers/nvme/target/auth.c new file +4
-0+ } + /* Use this hash as default */ + if (!host->dhchap_hash_id) + host->dhchap_hash_id = host->dhchap_key_hash;Why?
Because there is no mechanism how the controller selects the DHCHAP hmac algorithm.
The host will send a list of supported hmac algorithms, and the controller has to pick one of them.
And as we are sure that the hmac algorithm from the PSK will be supported on the controller I set that as default (if nothing was specified otherwise).
Quoted 10 lines
drivers/nvme/target/auth.c line 52
Quoted 7 lines
drivers/nvme/target/auth.c new file +7
-0+ } + host->dhchap_secret = kstrdup(secret, GFP_KERNEL); + if (!host->dhchap_secret) + return -ENOMEM; + /* Default to SHA256 */ + if (!host->dhchap_hash_id) + host->dhchap_hash_id = NVME_AUTH_DHCHAP_HASH_SHA256;What is the thought here?
This case is triggered when the user specifies a PSK without a hmac algorithm (ie DHHC-1:00:XXXXX).
Then the above mechanism doesn't work, but we still have to specify a default HMAC algorithm such that the selection mechanism can work.
Quoted 31 lines
Quoted 31 lines
drivers/nvme/target/auth.c new file +31
-0+ + pr_debug("Using hash %s\n", + nvme_auth_hmac_name(host->dhchap_hash_id)); + return 0; +} + +int nvmet_setup_dhgroup (struct nvmet_ctrl *ctrl,int dhgroup_id) +{ + int ret = -ENOTSUPP; + + if (dhgroup_id == NVME_AUTH_DHCHAP_DHGROUP_NULL) + return 0; + + return ret; +} + +int nvmet_setup_auth (struct nvmet_ctrl *ctrl,struct nvmet_req *req) +{ + int ret = 0; + struct nvmet_host_link *p; + struct nvmet_host *host = NULL; + const char *hash_name; + + down_read(&nvmet_config_sem); + if (ctrl->subsys->type == NVME_NQN_DISC) + goto out_unlock; + + list_for_each_entry(p, &ctrl->subsys->hosts, entry) { + pr_debug("check %s\n", nvmet_host_name(p->host)); + if (strcmp(nvmet_host_name(p->host), ctrl->hostnqn)) + continue;
Quoted 9 lines
drivers/nvme/target/auth.c line 89
Quoted 6 lines
drivers/nvme/target/auth.c new file +6
-0+ host = p->host; + break; + } + if (!host) { + pr_debug("host %s not found\n", ctrl->hostnqn); + ret = -EPERM;I think you should propogate the nvme status code instead...
I _thought_ it got translated into one; but yeah, can do.
Quoted 10 lines
drivers/nvme/target/auth.c line 98
Quoted 9 lines
drivers/nvme/target/auth.c new file +9
-0+ goto out_unlock; + } + if (!host->dhchap_secret) { + pr_debug("No authentication provided\n"); + goto out_unlock; + } + + hash_name = nvme_auth_hmac_name(host->dhchap_hash_id); + if (!hash_name) {
Quoted 2 lines
Can this actually happen?
Good question. I don't think so; will be changing it into a WARN_ON().
Quoted 4 lines
drivers/nvme/target/auth.c line 99
Quoted 1 line
drivers/nvme/target/auth.c new file +1
-0+ pr_debug("Hash ID %d invalid\n", host->dhchap_hash_id);warning, not debug.
See above. Yes.
Quoted 59 lines
Quoted 59 lines
drivers/nvme/target/auth.c new file +59
-0+ ret = -EINVAL; + goto out_unlock; + } + ctrl->shash_tfm = crypto_alloc_shash(hash_name, 0, + CRYPTO_ALG_ALLOCATES_MEMORY); + if (IS_ERR(ctrl->shash_tfm)) { + pr_debug("failed to allocate shash %s\n", hash_name); + ret = PTR_ERR(ctrl->shash_tfm); + ctrl->shash_tfm = NULL; + goto out_unlock; + } + + ctrl->dhchap_key = nvme_auth_extract_secret(host->dhchap_secret, + &ctrl->dhchap_key_len); + if (IS_ERR(ctrl->dhchap_key)) { + pr_debug("failed to extract host key, error %d\n", ret); + ret = PTR_ERR(ctrl->dhchap_key); + ctrl->dhchap_key = NULL; + goto out_free_hash; + } + if (host->dhchap_key_hash) { + struct crypto_shash *key_tfm; + + hash_name = nvme_auth_hmac_name(host->dhchap_key_hash); + key_tfm = crypto_alloc_shash(hash_name, 0, 0); + if (IS_ERR(key_tfm)) { + ret = PTR_ERR(key_tfm); + goto out_free_hash; + } else { + SHASH_DESC_ON_STACK(shash, key_tfm); + + shash->tfm = key_tfm; + ret = crypto_shash_setkey(key_tfm, ctrl->dhchap_key, + ctrl->dhchap_key_len); + crypto_shash_init(shash); + crypto_shash_update(shash, ctrl->subsys->subsysnqn, + strlen(ctrl->subsys->subsysnqn)); + crypto_shash_update(shash, "NVMe-over-Fabrics", 17); + crypto_shash_final(shash, ctrl->dhchap_key); + crypto_free_shash(key_tfm); + } + } + pr_debug("%s: using key %*ph\n", __func__, + (int)ctrl->dhchap_key_len, ctrl->dhchap_key); + ret = crypto_shash_setkey(ctrl->shash_tfm, ctrl->dhchap_key, + ctrl->dhchap_key_len); +out_free_hash: + if (ret) { + if (ctrl->dhchap_key) { + kfree(ctrl->dhchap_key); + ctrl->dhchap_key = NULL; + } + crypto_free_shash(ctrl->shash_tfm); + ctrl->shash_tfm = NULL; + } +out_unlock: + up_read(&nvmet_config_sem); + + return ret;
Quoted 9 lines
drivers/nvme/target/auth.c line 164
Quoted 6 lines
drivers/nvme/target/auth.c new file +6
-0+} + +void nvmet_auth_sq_free (struct nvmet_sq *sq) +{ + if (sq->dhchap_c1) + kfree(sq->dhchap_c1);just kfree, no need to if
Yeah.
Quoted 11 lines
drivers/nvme/target/auth.c line 171
Quoted 7 lines
drivers/nvme/target/auth.c new file +7
-0+ if (sq->dhchap_c2) + kfree(sq->dhchap_c2); + if (sq->dhchap_skey) + kfree(sq->dhchap_skey); +} + +void nvmet_reset_auth (struct nvmet_ctrl *ctrl)Shouldn't this be
nvmet_destroy_auth? reset indicates it can be reused again...
Oh, it should. I've coded nvmet_destroy_auth() pretty late in the game, so I missed that one.
Quoted 68 lines
Quoted 68 lines
drivers/nvme/target/auth.c new file +60
-0+{ + if (ctrl->shash_tfm) { + crypto_free_shash(ctrl->shash_tfm); + ctrl->shash_tfm = NULL; + } + if (ctrl->dh_tfm) { + crypto_free_kpp(ctrl->dh_tfm); + ctrl->dh_tfm = NULL; + } + if (ctrl->dhchap_key) { + kfree(ctrl->dhchap_key); + ctrl->dhchap_key = NULL; + } +} + +bool nvmet_check_auth_status (struct nvmet_req *req) +{ + if (req->sq->ctrl->shash_tfm && + !req->sq->authenticated) + return false; + return true; +} + +int nvmet_auth_host_hash (struct nvmet_req *req,u8 *response, + unsigned int shash_len) +{ + struct nvmet_ctrl *ctrl = req->sq->ctrl; + SHASH_DESC_ON_STACK(shash, ctrl->shash_tfm); + u8 *challenge = req->sq->dhchap_c1; + u8 buf[4]; + int ret; + + if (ctrl->dh_gid != NVME_AUTH_DHCHAP_DHGROUP_NULL) { + ret = -ENOTSUPP; + goto out; + } + + shash->tfm = ctrl->shash_tfm; + ret = crypto_shash_init(shash); + if (ret) + goto out; + ret = crypto_shash_update(shash, challenge, shash_len); + if (ret) + goto out; + put_unaligned_le32(req->sq->dhchap_s1, buf); + ret = crypto_shash_update(shash, buf, 4); + if (ret) + goto out; + put_unaligned_le16(req->sq->dhchap_tid, buf); + ret = crypto_shash_update(shash, buf, 2); + if (ret) + goto out; + memset(buf, 0, 4); + ret = crypto_shash_update(shash, buf, 1); + if (ret) + goto out; + ret = crypto_shash_update(shash, "HostHost", 8); + if (ret) + goto out; + ret = crypto_shash_update(shash, ctrl->hostnqn,
strlen(ctrl->hostnqn));
+ if (ret)
+ goto out;
+ ret =crypto_shash_update(shash, buf, 1); + if (ret)
+ goto out;
+ ret =crypto_shash_update(shash, ctrl->subsysnqn, + strlen(ctrl->subsysnqn));
Quoted 9 lines
Quoted 6 lines
+ if (ret)
+ goto out;
+ ret =crypto_shash_final(shash, response);
+out:
+ if (challenge !=req->sq->dhchap_c1)
+ kfree(challenge);What about actually failing?
Ho-hum. Of course.
Quoted 190 lines
Quoted 190 lines
+ return 0;
drivers/nvme/target/auth.c new file +55
-0+} + +int nvmet_auth_ctrl_hash (struct nvmet_req *req,u8 *response, + unsigned int shash_len) +{ + struct nvmet_ctrl *ctrl = req->sq->ctrl; + SHASH_DESC_ON_STACK(shash, ctrl->shash_tfm); + u8 *challenge = req->sq->dhchap_c2; + u8 buf[4]; + int ret; + + pr_debug("%s: ctrl %d hash seq %d transaction %u\n", __func__, + ctrl->cntlid, req->sq->dhchap_s2, req->sq->dhchap_tid); + pr_debug("%s: ctrl %d challenge %*ph\n", __func__, + ctrl->cntlid, shash_len, req->sq->dhchap_c2); + pr_debug("%s: ctrl %d subsysnqn %s\n", __func__, + ctrl->cntlid, ctrl->subsysnqn); + pr_debug("%s: ctrl %d hostnqn %s\n", __func__, + ctrl->cntlid, ctrl->hostnqn); + + if (ctrl->dh_gid != NVME_AUTH_DHCHAP_DHGROUP_NULL) { + ret = -ENOTSUPP; + goto out; + } + + shash->tfm = ctrl->shash_tfm; + ret = crypto_shash_init(shash); + if (ret) + goto out; + ret = crypto_shash_update(shash, challenge, shash_len); + if (ret) + goto out; + put_unaligned_le32(req->sq->dhchap_s2, buf); + ret = crypto_shash_update(shash, buf, 4); + if (ret) + goto out; + put_unaligned_le16(req->sq->dhchap_tid, buf); + ret = crypto_shash_update(shash, buf, 2); + if (ret) + goto out; + 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->subsysnqn, + strlen(ctrl->subsysnqn)); + if (ret) + goto out; + ret = crypto_shash_update(shash, buf, 1); + if (ret) + goto out; + ret = crypto_shash_update(shash, ctrl->hostnqn,
strlen(ctrl->hostnqn));
+ if (ret)
+ goto out;
+ ret =crypto_shash_final(shash, response);drivers/nvme/target/auth.c new file +18
-0+out: + if (challenge != req->sq->dhchap_c2) + kfree(challenge); + return 0; +} + +int nvmet_auth_ctrl_sesskey (struct nvmet_req *req, + u8 *pkey, int pkey_size) +{ + struct nvmet_ctrl *ctrl = req->sq->ctrl; + struct kpp_request *kpp_req; + struct crypto_wait wait; + struct scatterlist src, dst; + int ret; + + req->sq->dhchap_skey_len = + nvme_auth_dhgroup_privkey_size(ctrl->dh_gid); + req->sq->dhchap_skey = kzalloc(req->sq->dhchap_skey_len,
GFP_KERNEL);
+ if (!req->sq->dhchap_skey)
+ return-ENOMEM;
+kpp_req=kpp_request_alloc(ctrl->dh_tfm, GFP_KERNEL); + if (!kpp_req) {
+kfree(req->sq->dhchap_skey);
+req->sq->dhchap_skey= NULL;
+ return-ENOMEM;
+ }drivers/nvme/target/auth.c new file +13
-0+ + pr_debug("%s: host public key %*ph\n", __func__, + (int)pkey_size, pkey); + crypto_init_wait(&wait); + sg_init_one(&src, pkey, pkey_size); + kpp_request_set_input(kpp_req, &src, pkey_size); + sg_init_one(&dst, req->sq->dhchap_skey, + req->sq->dhchap_skey_len); + kpp_request_set_output(kpp_req, &dst, req->sq->dhchap_skey_len); + kpp_request_set_callback(kpp_req, CRYPTO_TFM_REQ_MAY_BACKLOG, + crypto_req_done, &wait); + + ret = crypto_wait_req(crypto_kpp_compute_shared_secret(kpp_req),&wait);
+kpp_request_free(kpp_req);
+ if (ret)
+pr_debug("failed to compute shared secred, err %d\n", ret); + else
+ pr_debug("%s: shared secret %*ph\n",__func__,
+ (int)req->sq->dhchap_skey_len,
+req->sq->dhchap_skey);drivers/nvme/target/auth.c new file +3
-0+ + return ret; +}drivers/nvme/target/configfs.c +59
-0@@ -11,8 +11,13 @@ #include <linux/ctype.h> #include <linux/pci.h> #include <linux/pci-p2pdma.h> +#include <crypto/hash.h> +#include <crypto/kpp.h> #include "nvmet.h" +#ifdef CONFIG_NVME_TARGET_AUTH +#include "../host/auth.h" +#endif static const struct config_item_type nvmet_host_type; static const struct config_item_type nvmet_subsys_type;@@ -1656,10 +1661,71 @@ static const struct config_item_type nvmet_ports_type = { static struct config_group nvmet_subsystems_group; static struct config_group nvmet_ports_group; -static void nvmet_host_release(struct config_item *item) +#ifdef CONFIG_NVME_TARGET_AUTH +static ssize_t nvmet_host_dhchap_key_show (struct config_item *item, + char *page) +{ + u8 *dhchap_secret = to_host(item)->dhchap_secret; + + if (!dhchap_secret) + return sprintf(page, "\n"); + return sprintf(page, "%s\n", dhchap_secret); +} + +static ssize_t nvmet_host_dhchap_key_store (struct config_item *item, + const char *page, size_t count) { struct nvmet_host *host = to_host(item); + int ret; + ret = nvmet_auth_set_host_key(host, page); + if (ret < 0) + return ret; + return count; +} + +CONFIGFS_ATTR (nvmet_host_, dhchap_key); + +static ssize_t nvmet_host_dhchap_hash_show (struct config_item *item, + char *page) +{ + struct nvmet_host *host = to_host(item); + const char *hash_name = nvme_auth_hmac_name(host->dhchap_hash_id); + + return sprintf(page, "%s\n", hash_name ? hash_name : "none"); +} + +static ssize_t nvmet_host_dhchap_hash_store (struct config_item *item, + const char *page, size_t count) +{ + struct nvmet_host *host = to_host(item); + int hmac_id; + + hmac_id = nvme_auth_hmac_id(page); + if (hmac_id < 0) + return -EINVAL; + if (!crypto_has_shash(nvme_auth_hmac_name(hmac_id), 0, 0)) + return -ENOTSUPP; + host->dhchap_hash_id = hmac_id; + return count; +} + +CONFIGFS_ATTR (nvmet_host_, dhchap_hash); + +static struct configfs_attribute *nvmet_host_attrs[] = { + &nvmet_host_attr_dhchap_key, + &nvmet_host_attr_dhchap_hash, + NULL, +}; +#endif /* CONFIG_NVME_TARGET_AUTH */ +
Quoted 9 lines
drivers/nvme/target/configfs.c static const struct config_item_type nvmet_ports_type = { · line 1727
Quoted 6 lines
drivers/nvme/target/configfs.c +6
-0+static void nvmet_host_release (struct config_item *item) +{ + struct nvmet_host *host = to_host(item); +#ifdef CONFIG_NVME_TARGET_AUTH + if (host->dhchap_secret) + kfree(host->dhchap_secret);No need for if condition.
Ok.
Quoted 62 lines
Quoted 62 lines
drivers/nvme/target/configfs.c +1
-0+#endif kfree(host); }@@ -1669,6 +1735,9 @@ static struct
configfs_item_operationsnvmet_host_item_ops= {
static const structconfig_item_typenvmet_host_type= { .ct_item_ops =&nvmet_host_item_ops,drivers/nvme/target/configfs.c +3
-0+#ifdef CONFIG_NVME_TARGET_AUTH + .ct_attrs = nvmet_host_attrs, +#endif .ct_owner =
THIS_MODULE,
};
diff --gita/drivers/nvme/target/core.cb/drivers/nvme/target/core.cindex163f7dc1a929..b5d7971f566b100644drivers/nvme/target/core.c +6
-0@@ -793,6 +793,7 @@ void nvmet_sq_destroy(struct nvmet_sq *sq) wait_for_completion(&sq->confirm_done); wait_for_completion(&sq->free_done); percpu_ref_exit(&sq->ref); + nvmet_auth_sq_free(sq); if (ctrl) { /*@@ -1264,6 +1265,11 @@ u16 nvmet_check_ctrl_status(struct nvmet_req *req) req->cmd->common.opcode, req->sq->qid); return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR; } + + if (unlikely(!nvmet_check_auth_status(req))) { + pr_warn("qid %d not authenticated\n", req->sq->qid); + return NVME_SC_AUTH_REQUIRED | NVME_SC_DNR; + } return 0; } @@ -1456,6 +1462,8 @@ static void nvmet_ctrl_free(struct kref *ref)
flush_work(&ctrl->async_event_work);cancel_work_sync(&ctrl->fatal_err_work); +nvmet_reset_auth(ctrl);
+
ida_simple_remove(&cntlid_ida, ctrl->cntlid);nvmet_async_events_free(ctrl);drivers/nvme/target/fabrics-cmd-auth.c new file +14
-0@@ -0,0 +1,460 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* NVMe over Fabrics DH-HMAC-CHAP authentication command handling. +* Copyright (c) 2020 Hannes Reinecke, SUSE Software Solutions. +* All rights reserved. +*/ +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <linux/blkdev.h> +#include <linux/random.h> +#include <crypto/hash.h> +#include <crypto/kpp.h> +#include "nvmet.h" +#include "../host/auth.h" +
Quoted 9 lines
drivers/nvme/target/fabrics-cmd-auth.c line 20
Quoted 6 lines
drivers/nvme/target/fabrics-cmd-auth.c new file +6
-0+void nvmet_init_auth (struct nvmet_ctrl *ctrl,struct nvmet_req *req) +{ + /* Initialize in-band authentication */ + req->sq->authenticated = false; + req->sq->dhchap_step = NVME_AUTH_DHCHAP_MESSAGE_NEGOTIATE; + req->cqe->result.u32 |= 0x2 << 16;Can you add a define for this:
NVME_CONNECT_AUTHREQ_INBAND
Sure.
Quoted 174 lines
Quoted 174 lines
drivers/nvme/target/fabrics-cmd-auth.c new file +9
-0+} + +static u16 nvmet_auth_negotiate (struct nvmet_req *req,void *d) +{ + struct nvmet_ctrl *ctrl = req->sq->ctrl; + struct nvmf_auth_dhchap_negotiate_data *data = d; + int i, hash_id, null_dh = -1; + + pr_debug("%s: ctrl %d qid %d: data sc_d %d napd %d authid %dhalen %d dhlen %d\n",
+__func__,ctrl->cntlid,req->sq->qid,
+data->sc_c,data->napd, data->auth_protocol[0].dhchap.authid, + data->auth_protocol[0].dhchap.halen,
+ data->auth_protocol[0].dhchap.dhlen);
+req->sq->dhchap_tid=le16_to_cpu(data->t_id);
+ if (data->sc_c)
+ returnNVME_AUTH_DHCHAP_FAILURE_CONCAT_MISMATCH;drivers/nvme/target/fabrics-cmd-auth.c new file +16
-0+ + if (data->napd != 1) + return NVME_AUTH_DHCHAP_FAILURE_HASH_UNUSABLE; + + if (data->auth_protocol[0].dhchap.authid != 0x01) + return NVME_AUTH_DHCHAP_FAILURE_INVALID_PAYLOAD; + + hash_id = nvme_auth_hmac_id(crypto_shash_alg_name(ctrl->shash_tfm)); + for (i = 0; i < data->auth_protocol[0].dhchap.halen; i++) { + pr_debug("%s: ctrl %d qid %d checking hash %d for %d\n", + __func__, ctrl->cntlid, req->sq->qid, + data->auth_protocol[0].dhchap.idlist[i], hash_id); + if (hash_id != data->auth_protocol[0].dhchap.idlist[i]) + continue; + req->sq->dhchap_hash_id = hash_id; + req->sq->dhchap_hash_len =
crypto_shash_digestsize(ctrl->shash_tfm);
+ break;
+ }
+ if (req->sq->dhchap_hash_id== 0) {
+ pr_debug("%s: ctrl %d qid %d: no usable hash found\n", +__func__,ctrl->cntlid,req->sq->qid); + returnNVME_AUTH_DHCHAP_FAILURE_HASH_UNUSABLE; + }drivers/nvme/target/fabrics-cmd-auth.c new file +96
-0+ + for (i = data->auth_protocol[0].dhchap.halen; + i < data->auth_protocol[0].dhchap.halen + + data->auth_protocol[0].dhchap.dhlen; i++) { + int dhgid = data->auth_protocol[0].dhchap.idlist[i]; + + if (dhgid == NVME_AUTH_DHCHAP_DHGROUP_NULL) { + null_dh = dhgid; + continue; + } + if (nvmet_setup_dhgroup(ctrl, dhgid) == 0) + break; + } + if (!ctrl->dh_tfm && null_dh < 0) { + pr_debug("%s: ctrl %d qid %d: no DH group selected\n", + __func__, ctrl->cntlid, req->sq->qid); + return NVME_AUTH_DHCHAP_FAILURE_DHGROUP_UNUSABLE; + } + if (ctrl->dh_gid == -1) { + ctrl->dh_gid = null_dh; + ctrl->dh_tfm = NULL; + } + pr_debug("%s: ctrl %d qid %d: DH group %s (%d)\n", + __func__, ctrl->cntlid, req->sq->qid, + nvme_auth_dhgroup_name(ctrl->dh_gid), ctrl->dh_gid); + return 0; +} + +static u16 nvmet_auth_reply (struct nvmet_req *req,void *d) +{ + struct nvmet_ctrl *ctrl = req->sq->ctrl; + struct nvmf_auth_dhchap_reply_data *data = d; + u8 *response; + + pr_debug("%s: ctrl %d qid %d: data hl %d cvalid %d dhvlen %d\n", + __func__, ctrl->cntlid, req->sq->qid, + data->hl, data->cvalid, data->dhvlen); + if (data->hl != req->sq->dhchap_hash_len) + return NVME_AUTH_DHCHAP_FAILURE_INVALID_PAYLOAD; + + if (data->dhvlen) { + return NVME_AUTH_DHCHAP_FAILURE_INVALID_PAYLOAD; + } + + response = kmalloc(data->hl, GFP_KERNEL); + if (!response) + return NVME_AUTH_DHCHAP_FAILURE_FAILED; + + if (nvmet_auth_host_hash(req, response, data->hl) < 0) { + pr_debug("ctrl %d qid %d DH-HMAC-CHAP hash failed\n", + ctrl->cntlid, req->sq->qid); + kfree(response); + return NVME_AUTH_DHCHAP_FAILURE_FAILED; + } + + if (memcmp(data->rval, response, data->hl)) { + pr_info("ctrl %d qid %d DH-HMAC-CHAP response mismatch\n", + ctrl->cntlid, req->sq->qid); + kfree(response); + return NVME_AUTH_DHCHAP_FAILURE_FAILED; + } + kfree(response); + pr_info("ctrl %d qid %d DH-HMAC-CHAP host authenticated\n", + ctrl->cntlid, req->sq->qid); + if (data->cvalid) { + req->sq->dhchap_c2 = kmalloc(data->hl, GFP_KERNEL); + if (!req->sq->dhchap_c2) + return NVME_AUTH_DHCHAP_FAILURE_FAILED; + memcpy(req->sq->dhchap_c2, data->rval + data->hl, data->hl); + + pr_debug("ctrl %d qid %d challenge %*ph\n", + ctrl->cntlid, req->sq->qid, data->hl, + req->sq->dhchap_c2); + req->sq->dhchap_s2 = le32_to_cpu(data->seqnum); + } else + req->sq->dhchap_c2 = NULL; + + return 0; +} + +static u16 nvmet_auth_failure2 (struct nvmet_req *req,void *d) +{ + struct nvmf_auth_dhchap_failure_data *data = d; + + return data->reason_code_explanation; +} + +void nvmet_execute_auth_send (struct nvmet_req *req) +{ + struct nvmet_ctrl *ctrl = req->sq->ctrl; + struct nvmf_auth_dhchap_success2_data *data; + void *d; + u32 tl; + u16 status = 0; + + if (req->cmd->auth_send.secp !=
NVME_AUTH_DHCHAP_PROTOCOL_IDENTIFIER) {
+ status =NVME_SC_INVALID_FIELD|NVME_SC_DNR;
+req->error_loc=
+offsetof(struct nvmf_auth_send_command, secp); + goto done;
+ }
+ if (req->cmd->auth_send.spsp0!=0x01) {
+ status =NVME_SC_INVALID_FIELD|NVME_SC_DNR;
+req->error_loc=
+offsetof(struct nvmf_auth_send_command, spsp0); + goto done;
+ }
+ if (req->cmd->auth_send.spsp1!=0x01) {
+ status =NVME_SC_INVALID_FIELD|NVME_SC_DNR;
+req->error_loc=
+offsetof(struct nvmf_auth_send_command, spsp1); + goto done;
+ }
+ tl =le32_to_cpu(req->cmd->auth_send.tl);
+ if (!tl) {
+ status =NVME_SC_INVALID_FIELD|NVME_SC_DNR;
+req->error_loc=
+offsetof(struct nvmf_auth_send_command, tl);
+ goto done;
+ }
+ if (!nvmet_check_transfer_len(req, tl)) {
+pr_debug("%s: transfer length mismatch (%u)\n", __func__, tl); + return;
+ }drivers/nvme/target/fabrics-cmd-auth.c new file +8
-0+ + d = kmalloc(tl, GFP_KERNEL); + if (!d) { + status = NVME_SC_INTERNAL; + goto done; + } + + status = nvmet_copy_from_sgl(req, 0, d, tl);
Quoted 9 lines
Quoted 5 lines
+ if (status) {
+ kfree(d);
+ goto done;
+ }
+This whole block below should move to something like
nvmet_process_auth_send_data()
Ok.
Quoted 108 lines
Quoted 108 lines
+ data = d;
+ pr_debug("%s: ctrl %d qid %d type %d id %d step %x\n",__func__, +ctrl->cntlid,req->sq->qid,data->auth_type,data->auth_id, +req->sq->dhchap_step);
+ if (data->auth_type!=NVME_AUTH_COMMON_MESSAGES&&
+data->auth_type!=NVME_AUTH_DHCHAP_MESSAGES) {
+req->sq->dhchap_step=NVME_AUTH_DHCHAP_MESSAGE_FAILURE1; +req->sq->dhchap_status=NVME_AUTH_DHCHAP_FAILURE_INVALID_MESSAGE;
+ } else if (data->auth_type==NVME_AUTH_COMMON_MESSAGES) { + if (data->auth_id!=req->sq->dhchap_step) {
+req->sq->dhchap_step=NVME_AUTH_DHCHAP_MESSAGE_FAILURE1; +req->sq->dhchap_status=NVME_AUTH_DHCHAP_FAILURE_INVALID_MESSAGE;
+ } else if (data->auth_id!=NVME_AUTH_DHCHAP_MESSAGE_NEGOTIATE) {
+req->sq->dhchap_step=NVME_AUTH_DHCHAP_MESSAGE_FAILURE1; +req->sq->dhchap_status=NVME_AUTH_DHCHAP_FAILURE_INVALID_MESSAGE;
+ } else {
+ /* Validate negotiation parameters */
+ status =nvmet_auth_negotiate(req, d);
+ if (status == 0)
+req->sq->dhchap_step=NVME_AUTH_DHCHAP_MESSAGE_CHALLENGE;
+ else {
+req->sq->dhchap_step=NVME_AUTH_DHCHAP_MESSAGE_FAILURE1;
+req->sq->dhchap_status= status;
+ status = 0;
+ }
+ }
+ } else if (data->auth_type==NVME_AUTH_DHCHAP_MESSAGES) { + if (data->auth_id!=req->sq->dhchap_step) {
+ pr_debug("%s: ctrl %d qid %d step mismatch (%d != %d)\n", +__func__,ctrl->cntlid,req->sq->qid,
+data->auth_id,req->sq->dhchap_step);
+req->sq->dhchap_step=NVME_AUTH_DHCHAP_MESSAGE_FAILURE1; +req->sq->dhchap_status=NVME_AUTH_DHCHAP_FAILURE_INVALID_MESSAGE;
+ } else if (le16_to_cpu(data->t_id)!=req->sq->dhchap_tid) { + pr_debug("%s: ctrl %d qid %d invalid transaction %d (expected %d)\n",
+__func__,ctrl->cntlid,req->sq->qid,
+le16_to_cpu(data->t_id),
+req->sq->dhchap_tid);
+req->sq->dhchap_step=NVME_AUTH_DHCHAP_MESSAGE_FAILURE1; +req->sq->dhchap_status=NVME_AUTH_DHCHAP_FAILURE_INVALID_PAYLOAD;
+ } else {
+ switch (data->auth_id) {
+ caseNVME_AUTH_DHCHAP_MESSAGE_REPLY:
+ status =nvmet_auth_reply(req, d);
+ if (status == 0)
+req->sq->dhchap_step=NVME_AUTH_DHCHAP_MESSAGE_SUCCESS1;
+ else {
+req->sq->dhchap_step=NVME_AUTH_DHCHAP_MESSAGE_FAILURE1;
+req->sq->dhchap_status= status;
+ status = 0;
+ }
+ break;
+ caseNVME_AUTH_DHCHAP_MESSAGE_SUCCESS2:
+req->sq->authenticated= true;
+ pr_debug("%s: ctrl %d qid %d authenticated\n", +__func__,ctrl->cntlid,req->sq->qid); + break;
+ caseNVME_AUTH_DHCHAP_MESSAGE_FAILURE2:
+ status =nvmet_auth_failure2(req, d);
+ if (status) {
+ pr_warn("ctrl %d qid %d: DH-HMAC-CHAP negotiation failed (%d)\n",
+ctrl->cntlid,req->sq->qid,
+ status);
+req->sq->dhchap_status= status;
+ status = 0;
+ }
+ break;
+ default:
+req->sq->dhchap_status=NVME_AUTH_DHCHAP_FAILURE_INVALID_MESSAGE;
+req->sq->dhchap_step=NVME_AUTH_DHCHAP_MESSAGE_FAILURE2;
+ break;
+ }
+ }
+ } else {
+req->sq->dhchap_status=NVME_AUTH_DHCHAP_FAILURE_INVALID_MESSAGE;
+req->sq->dhchap_step=NVME_AUTH_DHCHAP_MESSAGE_FAILURE2; + }
+ kfree(d);
+done:
+ pr_debug("%s: ctrl %d qid %d dhchap status %x step %x\n",__func__, +ctrl->cntlid,req->sq->qid,
+req->sq->dhchap_status,req->sq->dhchap_step);
+ if (status)
+ pr_debug("%s: ctrl %d qid %d nvme status %x error loc %d\n", +__func__,ctrl->cntlid,req->sq->qid,
+ status,req->error_loc);
+req->cqe->result.u64= 0;
+nvmet_req_complete(req, status);
+ if (req->sq->dhchap_step!=NVME_AUTH_DHCHAP_MESSAGE_SUCCESS2&& +req->sq->dhchap_step!=NVME_AUTH_DHCHAP_MESSAGE_FAILURE2) + return;
+ /* Final states, clear up variables */
+kfree(req->sq->dhchap_c1);
Quoted 9 lines
drivers/nvme/target/fabrics-cmd-auth.c line 295
Quoted 6 lines
+
kfree(req->sq->dhchap_c2);
+ if (req->sq->dhchap_step==NVME_AUTH_DHCHAP_MESSAGE_FAILURE2) +nvmet_ctrl_fatal_error(ctrl);drivers/nvme/target/fabrics-cmd-auth.c new file +3
-0+} + +static int nvmet_auth_challenge (struct nvmet_req *req,void *d,int al)
nvmet_auth_set_challenge
Ok.
Quoted 226 lines
Quoted 226 lines
drivers/nvme/target/fabrics-cmd-auth.c new file +25
-0+{ + struct nvmf_auth_dhchap_challenge_data *data = d; + struct nvmet_ctrl *ctrl = req->sq->ctrl; + int ret = 0; + int data_size = sizeof(*d) + req->sq->dhchap_hash_len; + + if (al < data_size) { + pr_debug("%s: buffer too small (al %d need %d)\n", __func__, + al, data_size); + return -EINVAL; + } + memset(data, 0, data_size); + req->sq->dhchap_s1 = ctrl->dhchap_seqnum++; + data->auth_type = NVME_AUTH_DHCHAP_MESSAGES; + data->auth_id = NVME_AUTH_DHCHAP_MESSAGE_CHALLENGE; + data->t_id = cpu_to_le16(req->sq->dhchap_tid); + data->hashid = req->sq->dhchap_hash_id; + data->hl = req->sq->dhchap_hash_len; + data->seqnum = cpu_to_le32(req->sq->dhchap_s1); + req->sq->dhchap_c1 = kmalloc(data->hl, GFP_KERNEL); + if (!req->sq->dhchap_c1) + return -ENOMEM; + get_random_bytes(req->sq->dhchap_c1, data->hl); + memcpy(data->cval, req->sq->dhchap_c1, data->hl); + pr_debug("%s: ctrl %d qid %d seq %d transaction %d hl %d dhvlen%d\n",
+__func__,ctrl->cntlid,req->sq->qid,req->sq->dhchap_s1, +req->sq->dhchap_tid,data->hl,data->dhvlen); + return ret;drivers/nvme/target/fabrics-cmd-auth.c new file +43
-0+} + +static int nvmet_auth_success1 (struct nvmet_req *req,void *d,int al) +{ + struct nvmf_auth_dhchap_success1_data *data = d; + struct nvmet_ctrl *ctrl = req->sq->ctrl; + + WARN_ON(al < sizeof(*data)); + memset(data, 0, sizeof(*data)); + data->auth_type = NVME_AUTH_DHCHAP_MESSAGES; + data->auth_id = NVME_AUTH_DHCHAP_MESSAGE_SUCCESS1; + data->t_id = cpu_to_le16(req->sq->dhchap_tid); + data->hl = req->sq->dhchap_hash_len; + if (req->sq->dhchap_c2) { + if (nvmet_auth_ctrl_hash(req, data->rval, data->hl)) + return NVME_AUTH_DHCHAP_FAILURE_HASH_UNUSABLE; + data->rvalid = 1; + pr_debug("ctrl %d qid %d response %*ph\n", + ctrl->cntlid, req->sq->qid, data->hl, data->rval); + } + return 0; +} + +static void nvmet_auth_failure1 (struct nvmet_req *req,void *d,int al) +{ + struct nvmf_auth_dhchap_failure_data *data = d; + + WARN_ON(al < sizeof(*data)); + data->auth_type = NVME_AUTH_COMMON_MESSAGES; + data->auth_id = NVME_AUTH_DHCHAP_MESSAGE_FAILURE1; + data->t_id = cpu_to_le32(req->sq->dhchap_tid); + data->reason_code = NVME_AUTH_DHCHAP_FAILURE_REASON_FAILED; + data->reason_code_explanation = req->sq->dhchap_status; +} + +void nvmet_execute_auth_receive (struct nvmet_req *req) +{ + struct nvmet_ctrl *ctrl = req->sq->ctrl; + void *d; + u32 al; + u16 status = 0; + + if (req->cmd->auth_receive.secp !=
NVME_AUTH_DHCHAP_PROTOCOL_IDENTIFIER) {
+ status =NVME_SC_INVALID_FIELD|NVME_SC_DNR;
+req->error_loc=
+offsetof(struct nvmf_auth_receive_command, secp); + goto done;
+ }
+ if (req->cmd->auth_receive.spsp0!=0x01) {
+ status =NVME_SC_INVALID_FIELD|NVME_SC_DNR;
+req->error_loc=
+offsetof(struct nvmf_auth_receive_command, spsp0); + goto done;
+ }
+ if (req->cmd->auth_receive.spsp1!=0x01) {
+ status =NVME_SC_INVALID_FIELD|NVME_SC_DNR;
+req->error_loc=
+offsetof(struct nvmf_auth_receive_command, spsp1); + goto done;
+ }
+ al =le32_to_cpu(req->cmd->auth_receive.al);
+ if (!al) {
+ status =NVME_SC_INVALID_FIELD|NVME_SC_DNR;
+req->error_loc=
+offsetof(struct nvmf_auth_receive_command, al); + goto done;
+ }
+ if (!nvmet_check_transfer_len(req, al)) {
+pr_debug("%s: transfer length mismatch (%u)\n", __func__, al); + return;
+ }drivers/nvme/target/fabrics-cmd-auth.c new file +66
-0+ + d = kmalloc(al, GFP_KERNEL); + if (!d) { + status = NVME_SC_INTERNAL; + goto done; + } + pr_debug("%s: ctrl %d qid %d step %x\n", __func__, + ctrl->cntlid, req->sq->qid, req->sq->dhchap_step); + switch (req->sq->dhchap_step) { + case NVME_AUTH_DHCHAP_MESSAGE_CHALLENGE: + status = nvmet_auth_challenge(req, d, al); + if (status < 0) { + pr_warn("ctrl %d qid %d: challenge error (%d)\n", + ctrl->cntlid, req->sq->qid, status); + status = NVME_SC_INTERNAL; + break; + } + if (status) { + req->sq->dhchap_status = status; + nvmet_auth_failure1(req, d, al); + pr_warn("ctrl %d qid %d: challenge status (%x)\n", + ctrl->cntlid, req->sq->qid, + req->sq->dhchap_status); + status = 0; + break; + } + req->sq->dhchap_step = NVME_AUTH_DHCHAP_MESSAGE_REPLY; + break; + case NVME_AUTH_DHCHAP_MESSAGE_SUCCESS1: + status = nvmet_auth_success1(req, d, al); + if (status) { + req->sq->dhchap_status = status; + nvmet_auth_failure1(req, d, al); + pr_warn("ctrl %d qid %d: success1 status (%x)\n", + ctrl->cntlid, req->sq->qid, + req->sq->dhchap_status); + break; + } + req->sq->dhchap_step = NVME_AUTH_DHCHAP_MESSAGE_SUCCESS2; + break; + case NVME_AUTH_DHCHAP_MESSAGE_FAILURE1: + nvmet_auth_failure1(req, d, al); + pr_warn("ctrl %d qid %d failure1 (%x)\n", + ctrl->cntlid, req->sq->qid, req->sq->dhchap_status); + break; + default: + pr_warn("ctrl %d qid %d unhandled step (%d)\n", + ctrl->cntlid, req->sq->qid, req->sq->dhchap_step); + req->sq->dhchap_step = NVME_AUTH_DHCHAP_MESSAGE_FAILURE1; + req->sq->dhchap_status = NVME_AUTH_DHCHAP_FAILURE_FAILED; + nvmet_auth_failure1(req, d, al); + status = 0; + break; + } + + status = nvmet_copy_to_sgl(req, 0, d, al); + kfree(d); +done: + req->cqe->result.u64 = 0; + nvmet_req_complete(req, status); + if (req->sq->dhchap_step == NVME_AUTH_DHCHAP_MESSAGE_FAILURE1) { + kfree(req->sq->dhchap_c1); + kfree(req->sq->dhchap_c2); + nvmet_ctrl_fatal_error(ctrl); + } +}drivers/nvme/target/fabrics-cmd.c +0
-0@@ -93,6 +93,14 @@ u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req)case
nvme_fabrics_type_property_get:
req->execute=nvmet_execute_prop_get;
break;drivers/nvme/target/fabrics-cmd.c +8
-0+#ifdef CONFIG_NVME_TARGET_AUTH + case nvme_fabrics_type_auth_send: + req->execute = nvmet_execute_auth_send; + break; + case nvme_fabrics_type_auth_receive: + req->execute = nvmet_execute_auth_receive; + break; +#endif default:
pr_debug("received unknown capsule type 0x%x\n", cmd->fabrics.fctype);drivers/nvme/target/fabrics-cmd.c +1
-0@@ -155,6 +163,7 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req) struct nvmf_connect_data *d; struct nvmet_ctrl *ctrl = NULL; u16 status = 0; + int ret;if (!nvmet_check_transfer_len(req,
sizeof(struct nvmf_connect_data)))
return;drivers/nvme/target/fabrics-cmd.c +14
-1@@ -197,17 +206,31 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req) uuid_copy(&ctrl->hostid, &d->hostid); + ret = nvmet_setup_auth(ctrl, req); + if (ret < 0) { + pr_err("Failed to setup authentication, error %d\n", ret); + nvmet_ctrl_put(ctrl); + if (ret == -EPERM) + status = (NVME_SC_CONNECT_INVALID_HOST | NVME_SC_DNR); + else + status = NVME_SC_INTERNAL; + goto out; + } + status = nvmet_install_queue(ctrl, req); if (status) { nvmet_ctrl_put(ctrl); goto out; } - pr_info("creating controller %d for subsystem %s for NQN %s%s.\n", + pr_info("creating controller %d for subsystem %s for NQN %s%s%s.\n", ctrl->cntlid, ctrl->subsys->subsysnqn, ctrl->hostnqn, - ctrl->pi_support ? " T10-PI is enabled" : ""); + ctrl->pi_support ? " T10-PI is enabled" : "", + nvmet_has_auth(ctrl) ? " with DH-HMAC-CHAP" : ""); req->cqe->result.u16 = cpu_to_le16(ctrl->cntlid); + if (nvmet_has_auth(ctrl)) + nvmet_init_auth(ctrl, req);out:
kfree(d);
Quoted 9 lines
drivers/nvme/target/fabrics-cmd.c static void nvmet_execute_io_connect(struct · line 293
Quoted 6 lines
complete:
drivers/nvme/target/fabrics-cmd.c +1
-0@@ -267,6 +290,9 @@ static void nvmet_execute_io_connect(struct nvmet_req *req) } pr_debug("adding queue %d to ctrl %d.\n", qid, ctrl->cntlid); + req->cqe->result.u16 = cpu_to_le16(ctrl->cntlid);Is this related to the patch?
Have to check. I thought it did, but now that you mention it...
Quoted 98 lines
Quoted 98 lines
drivers/nvme/target/fabrics-cmd.c +2
-0+ if (nvmet_has_auth(ctrl)) + nvmet_init_auth(ctrl, req);out:
kfree(d);drivers/nvme/target/nvmet.h +27
-0@@ -108,6 +108,20 @@ struct nvmet_sq { u16 size; u32 sqhd; bool sqhd_disabled; +#ifdef CONFIG_NVME_TARGET_AUTH + bool authenticated; + u16 dhchap_tid; + u16 dhchap_status; + int dhchap_step; + u8 dhchap_hash_id; + u8 dhchap_hash_len; + u8 *dhchap_c1; + u8 *dhchap_c2; + u32 dhchap_s1; + u32 dhchap_s2; + u8 *dhchap_skey; + int dhchap_skey_len; +#endif struct completion free_done; struct completion confirm_done; };@@ -209,6 +223,15 @@ struct nvmet_ctrl { u64 err_counter; struct nvme_error_slot slots[NVMET_ERROR_LOG_SLOTS]; bool pi_support; +#ifdef CONFIG_NVME_TARGET_AUTH + u32 dhchap_seqnum; + u8 *dhchap_key; + size_t dhchap_key_len; + struct crypto_shash *shash_tfm; + struct crypto_kpp *dh_tfm; + u32 dh_gid; + u32 dh_keysize; +#endif }; struct nvmet_subsys {@@ -270,6 +293,10 @@ static inline struct nvmet_subsys *namespaces_to_subsys( struct nvmet_host { struct config_group group; + u8 *dhchap_secret; + u8 dhchap_key_hash; + u8 dhchap_hash_id; + u8 dhchap_dhgroup_id; }; static inline struct nvmet_host *to_host(struct config_item *item)@@ -659,4 +686,48 @@ static inline void nvmet_req_bio_put(struct nvmet_req *req, struct bio *bio) bio_put(bio); }+#ifdef
CONFIG_NVME_TARGET_AUTHdrivers/nvme/target/nvmet.h +3
-0+void nvmet_execute_auth_send (struct nvmet_req *req); +void nvmet_execute_auth_receive (struct nvmet_req *req); +int nvmet_auth_set_host_key(struct nvmet_host *host, const char*secret);
drivers/nvme/target/nvmet.h +35
-0+int nvmet_auth_set_host_hash (struct nvmet_host *host,const char *hash); +int nvmet_setup_auth (struct nvmet_ctrl *ctrl,struct nvmet_req *req); +void nvmet_init_auth (struct nvmet_ctrl *ctrl,struct nvmet_req *req); +void nvmet_reset_auth (struct nvmet_ctrl *ctrl); +void nvmet_auth_sq_free (struct nvmet_sq *sq); +int nvmet_setup_dhgroup (struct nvmet_ctrl *ctrl,int dhgroup_id); +bool nvmet_check_auth_status (struct nvmet_req *req); +int nvmet_auth_host_hash (struct nvmet_req *req,u8 *response, + unsigned int hash_len); +int nvmet_auth_ctrl_hash (struct nvmet_req *req,u8 *response, + unsigned int hash_len); +static inline bool nvmet_has_auth (struct nvmet_ctrl *ctrl) +{ + return ctrl->shash_tfm != NULL; +} +int nvmet_auth_ctrl_exponential (struct nvmet_req *req, + u8 *buf, int buf_size); +int nvmet_auth_ctrl_sesskey (struct nvmet_req *req, + u8 *buf, int buf_size); +#else +static inline int nvmet_setup_auth (struct nvmet_ctrl *ctrl, + struct nvmet_req *req) +{ + return 0; +} +static inline void nvmet_init_auth (struct nvmet_ctrl *ctrl, + struct nvmet_req *req) {}; +static inline void nvmet_reset_auth (struct nvmet_ctrl *ctrl) {}; +static inline void nvmet_auth_sq_free (struct nvmet_sq *sq) {}; +static inline bool nvmet_check_auth_status (struct nvmet_req *req) +{ + return true; +} +static inline bool nvmet_has_auth (struct nvmet_ctrl *ctrl) +{
Quoted 9 lines
Quoted 8 lines
+ return false;
drivers/nvme/target/nvmet.h +2
-0+} +static inline const char *nvmet_dhchap_dhgroup_name (int dhgid) {return NULL; }
drivers/nvme/target/nvmet.h +2
-0+#endif +#endif /*
_NVMET_H*/
Thanks for the review!
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), GF: Felix Imendörffer
Hannes Reinecke
maintainerPATCH 10/11 nvmet-auth: implement support for augmented challenge
In reply to Hannes ReineckeImplement support for augmented challenge with FFDHE groups. This patch adds a new configfs attribute 'dhchap_dhgroup' to select the DH group to use.
--- drivers/nvme/target/auth.c | 241 ++++++++++++++++++++++++- drivers/nvme/target/configfs.c | 31 ++++ drivers/nvme/target/fabrics-cmd-auth.c | 14 +- 3 files changed, 281 insertions(+), 5 deletions(-)
drivers/nvme/target/auth.c +237 -4
@@ -58,11 +58,56 @@ int nvmet_auth_set_host_key(struct nvmet_host *host, const char *secret)int nvmet_setup_dhgroup (struct nvmet_ctrl *ctrl,int dhgroup_id) { +struct nvmet_host_link *p; +struct nvmet_host *host =NULL ; +const char *dhgroup_kpp;int ret = -ENOTSUPP ;if (dhgroup_id ==NVME_AUTH_DHCHAP_DHGROUP_NULL )return 0 ; +down_read (&nvmet_config_sem); +if (ctrl->subsys->type ==NVME_NQN_DISC ) + goto out_unlock; + +list_for_each_entry (p, &ctrl->subsys->hosts, entry) { +if (strcmp (nvmet_host_name (p->host), ctrl->hostnqn)) +continue ; + host = p->host; +break ; + } +if (!host) { +pr_debug ("host %s not found\n" , ctrl->hostnqn); + ret = -ENXIO ; + goto out_unlock; + } + +if (host->dhchap_dhgroup_id != dhgroup_id) { + ret = -EINVAL ; + goto out_unlock; + } + dhgroup_kpp =nvme_auth_dhgroup_kpp (dhgroup_id); +if (!dhgroup_kpp) { + ret = -EINVAL ; + goto out_unlock; + } + ctrl->dh_tfm =crypto_alloc_kpp (dhgroup_kpp,0 ,0 ); +if (IS_ERR (ctrl->dh_tfm)) { +pr_debug ("failed to setup DH group %d, err %ld\n" , + dhgroup_id,PTR_ERR (ctrl->dh_tfm)); + ret =PTR_ERR (ctrl->dh_tfm); + ctrl->dh_tfm =NULL ; + }else { + ctrl->dh_gid = dhgroup_id; + ctrl->dh_keysize =nvme_auth_dhgroup_pubkey_size (dhgroup_id); +pr_debug ("select DH group %d keysize %d\n" , + ctrl->dh_gid, ctrl->dh_keysize); + ret =0 ; + } + +out_unlock: +up_read (&nvmet_config_sem); +return ret; }
@@ -192,6 +237,101 @@ bool nvmet_check_auth_status(struct nvmet_req *req)return true; } +static int nvmet_auth_hash_sesskey (struct nvmet_req *req,u8 *hashed_key) +{ +struct nvmet_ctrl *ctrl = req->sq->ctrl; +const char *hmac_name, *digest_name; +struct crypto_shash *tfm; +int hmac_id, ret; + +if (!ctrl->shash_tfm) { +pr_debug ("%s: hash alg not set\n" , __func__); +return -EINVAL ; + } + hmac_name =crypto_shash_alg_name (ctrl->shash_tfm); + hmac_id =nvme_auth_hmac_id (hmac_name); +if (hmac_id <0 ) { +pr_debug ("%s: unsupported hmac %s\n" , __func__, + hmac_name); +return -EINVAL ; + } + digest_name =nvme_auth_digest_name (hmac_id); +if (!digest_name) { +pr_debug ("%s: failed to get digest for %s\n" , __func__, + hmac_name); +return -EINVAL ; + } + tfm =crypto_alloc_shash (digest_name,0 ,0 ); +if (IS_ERR (tfm)) +return -ENOMEM ; + + ret =crypto_shash_tfm_digest (tfm, req->sq->dhchap_skey, + req->sq->dhchap_skey_len, hashed_key); +if (ret <0 ) +pr_debug ("%s: Failed to hash digest len %d\n" , __func__, + req->sq->dhchap_skey_len); + +crypto_free_shash (tfm); +return ret; +} + +static int nvmet_auth_augmented_challenge (struct nvmet_req *req, +u8 *challenge,u8 *aug) +{ +struct nvmet_ctrl *ctrl = req->sq->ctrl; +struct crypto_shash *tfm; +struct shash_desc *desc; +u8 *hashed_key; +const char *hash_name; +int hash_len = req->sq->dhchap_hash_len; +int ret; + + hashed_key =kmalloc (hash_len,GFP_KERNEL ); +if (!hashed_key) +return -ENOMEM ; + + ret =nvmet_auth_hash_sesskey (req, hashed_key); +if (ret <0 ) { +pr_debug ("failed to hash session key, err %d\n" , ret); +kfree (hashed_key); +return ret; + } + hash_name =crypto_shash_alg_name (ctrl->shash_tfm); +if (!hash_name) { +pr_debug ("Invalid hash algoritm\n" ); +return -EINVAL ; + } + tfm =crypto_alloc_shash (hash_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, hash_len); +if (ret) + goto out_free_desc; + ret =crypto_shash_init (desc); +if (ret) + goto out_free_desc; +crypto_shash_update (desc, challenge, hash_len); +crypto_shash_final (desc, aug); + +out_free_desc: +kfree_sensitive (desc); +out_free_hash: +crypto_free_shash (tfm); +out_free_key: +kfree (hashed_key); +return ret; +} +int nvmet_auth_host_hash (struct nvmet_req *req,u8 *response,unsigned int shash_len) {
@@ -202,8 +342,15 @@ int nvmet_auth_host_hash(struct nvmet_req *req, u8 *response,int ret;if (ctrl->dh_gid !=NVME_AUTH_DHCHAP_DHGROUP_NULL ) { - ret = -ENOTSUPP ; - goto out; + challenge =kmalloc (shash_len,GFP_KERNEL ); +if (!challenge) { + ret = -ENOMEM ; + goto out; + } + ret =nvmet_auth_augmented_challenge (req, req->sq->dhchap_c1, + challenge); +if (ret) + goto out; } shash->tfm = ctrl->shash_tfm;
@@ -264,8 +411,15 @@ int nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 *response, ctrl->cntlid, ctrl->hostnqn);if (ctrl->dh_gid !=NVME_AUTH_DHCHAP_DHGROUP_NULL ) { - ret = -ENOTSUPP ; - goto out; + challenge =kmalloc (shash_len,GFP_KERNEL ); +if (!challenge) { + ret = -ENOMEM ; + goto out; + } + ret =nvmet_auth_augmented_challenge (req, req->sq->dhchap_c2, + challenge); +if (ret) + goto out; } shash->tfm = ctrl->shash_tfm;
@@ -307,6 +461,85 @@ int nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 *response,return 0 ; } +int nvmet_auth_ctrl_exponential (struct nvmet_req *req, +u8 *buf,int buf_size) +{ +struct nvmet_ctrl *ctrl = req->sq->ctrl; +struct kpp_request *kpp_req; +struct crypto_wait wait; +char *pkey; +struct scatterlist dst; +int ret, pkey_len; + +if (ctrl->dh_gid ==NVME_AUTH_DHCHAP_DHGROUP_2048 || + ctrl->dh_gid ==NVME_AUTH_DHCHAP_DHGROUP_3072 || + ctrl->dh_gid ==NVME_AUTH_DHCHAP_DHGROUP_4096 || + ctrl->dh_gid ==NVME_AUTH_DHCHAP_DHGROUP_6144 || + ctrl->dh_gid ==NVME_AUTH_DHCHAP_DHGROUP_8192 ) { +struct dh p = {0 }; +int bits =nvme_auth_dhgroup_pubkey_size (ctrl->dh_gid) <<3 ; + + ret =crypto_ffdhe_params (&p, bits); +if (ret) +return ret; + + p.key = ctrl->dhchap_key; + p.key_size = ctrl->dhchap_key_len; + + pkey_len =crypto_dh_key_len (&p); + pkey =kmalloc (pkey_len,GFP_KERNEL ); +if (!pkey) +return -ENOMEM ; + +get_random_bytes (pkey, pkey_len); + ret =crypto_dh_encode_key (pkey, pkey_len, &p); +if (ret) { +pr_debug ("failed to encode private key, error %d\n" , + ret); + goto out; + } + }else { +pr_warn ("invalid dh group %d\n" , ctrl->dh_gid); +return -EINVAL ; + } + ret =crypto_kpp_set_secret (ctrl->dh_tfm, pkey, pkey_len); +if (ret) { +pr_debug ("failed to set private key, error %d\n" , ret); + goto out; + } + + kpp_req =kpp_request_alloc (ctrl->dh_tfm,GFP_KERNEL ); +if (!kpp_req) { +pr_debug ("cannot allocate kpp request\n" ); + ret = -ENOMEM ; + goto out; + } + +crypto_init_wait (&wait); +kpp_request_set_input (kpp_req,NULL ,0 ); +sg_init_one (&dst, buf, buf_size); +kpp_request_set_output (kpp_req, &dst, buf_size); +kpp_request_set_callback (kpp_req,CRYPTO_TFM_REQ_MAY_BACKLOG , + crypto_req_done, &wait); + + ret =crypto_wait_req (crypto_kpp_generate_public_key (kpp_req), &wait); +kpp_request_free (kpp_req); +if (ret == -EOVERFLOW ) { +pr_debug ("public key buffer too small, need %d is %d\n" , +crypto_kpp_maxsize (ctrl->dh_tfm), buf_size); + ret = -ENOKEY ; + }else if (ret) { +pr_debug ("failed to generate public key, err %d\n" , ret); + ret = -ENOKEY ; + }else +pr_debug ("%s: ctrl public key %*ph\n" , __func__, + (int )buf_size, buf); + +out: +kfree_sensitive (pkey); +return ret; +} +int nvmet_auth_ctrl_sesskey (struct nvmet_req *req,u8 *pkey,int pkey_size) {
drivers/nvme/target/configfs.c +31 -0
@@ -1712,9 +1712,40 @@ static ssize_t nvmet_host_dhchap_hash_store(struct config_item *item,CONFIGFS_ATTR (nvmet_host_, dhchap_hash); +static ssize_t nvmet_host_dhchap_dhgroup_show (struct config_item *item, +char *page) +{ +struct nvmet_host *host =to_host (item); +const char *dhgroup =nvme_auth_dhgroup_name (host->dhchap_dhgroup_id); + +return sprintf (page,"%s\n" , dhgroup ? dhgroup :"none" ); +} + +static ssize_t nvmet_host_dhchap_dhgroup_store (struct config_item *item, +const char *page,size_t count) +{ +struct nvmet_host *host =to_host (item); +int dhgroup_id; + + dhgroup_id =nvme_auth_dhgroup_id (page); +if (dhgroup_id <0 ) +return -EINVAL ; +if (dhgroup_id !=NVME_AUTH_DHCHAP_DHGROUP_NULL ) { +const char *kpp =nvme_auth_dhgroup_kpp (dhgroup_id); + +if (!crypto_has_kpp (kpp,0 ,0 )) +return -EINVAL ; + } + host->dhchap_dhgroup_id = dhgroup_id; +return count; +} + +CONFIGFS_ATTR (nvmet_host_, dhchap_dhgroup); +static struct configfs_attribute *nvmet_host_attrs[] = { &nvmet_host_attr_dhchap_key, &nvmet_host_attr_dhchap_hash, + &nvmet_host_attr_dhchap_dhgroup,NULL , };#endif /* CONFIG_NVME_TARGET_AUTH */
drivers/nvme/target/fabrics-cmd-auth.c +13 -1
@@ -98,7 +98,11 @@ static u16 nvmet_auth_reply(struct nvmet_req *req, void *d)return NVME_AUTH_DHCHAP_FAILURE_INVALID_PAYLOAD ;if (data->dhvlen) { -return NVME_AUTH_DHCHAP_FAILURE_INVALID_PAYLOAD ; +if (!ctrl->dh_tfm) +return NVME_AUTH_DHCHAP_FAILURE_INVALID_PAYLOAD ; +if (nvmet_auth_ctrl_sesskey (req, data->rval +2 * data->hl, + data->dhvlen) <0 ) +return NVME_AUTH_DHCHAP_FAILURE_DHGROUP_UNUSABLE ; } response =kmalloc (data->hl,GFP_KERNEL );
@@ -299,6 +303,8 @@ static int nvmet_auth_challenge(struct nvmet_req *req, void *d, int al)int ret =0 ;int data_size =sizeof (*d) + req->sq->dhchap_hash_len; +if (ctrl->dh_tfm) + data_size += ctrl->dh_keysize;if (al < data_size) {pr_debug ("%s: buffer too small (al %d need %d)\n" , __func__, al, data_size);
@@ -317,6 +323,12 @@ static int nvmet_auth_challenge(struct nvmet_req *req, void *d, int al)return -ENOMEM ;get_random_bytes (req->sq->dhchap_c1, data->hl);memcpy (data->cval, req->sq->dhchap_c1, data->hl); +if (ctrl->dh_tfm) { + data->dhgid = ctrl->dh_gid; + data->dhvlen = ctrl->dh_keysize; + ret =nvmet_auth_ctrl_exponential (req, data->cval + data->hl, + data->dhvlen); + }pr_debug ("%s: ctrl %d qid %d seq %d transaction %d hl %d dhvlen %d\n" , __func__, ctrl->cntlid, req->sq->qid, req->sq->dhchap_s1, req->sq->dhchap_tid, data->hl, data->dhvlen);
Signature · 1 line
2.29.2
Re: [PATCH 10/11] nvmet-auth: implement support for augmented challenge
Am Freitag, 16. Juli 2021, 13:04:27 CEST schrieb Hannes Reinecke:
Hi Hannes,
Quoted 176 lines
Implement support for augmented challenge with FFDHE groups. This patch adds a new configfs attribute '
dhchap_dhgroup' to select the DH group to use.Signed-off-by Hannes Reinecke <[email protected]>--- drivers/nvme/target/auth.c | 241 ++++++++++++++++++++++++- drivers/nvme/target/configfs.c | 31 ++++ drivers/nvme/target/fabrics-cmd-auth.c | 14 +- 3 files changed, 281 insertions(+), 5 deletions(-)drivers/nvme/target/auth.c +140
-0@@ -58,11 +58,56 @@ int nvmet_auth_set_host_key(struct nvmet_host *host, const char *secret) int nvmet_setup_dhgroup(struct nvmet_ctrl *ctrl, int dhgroup_id) { +struct nvmet_host_link *p; +struct nvmet_host *host =NULL ; +const char *dhgroup_kpp;int ret = -ENOTSUPP ;if (dhgroup_id ==NVME_AUTH_DHCHAP_DHGROUP_NULL )return 0 ; +down_read (&nvmet_config_sem); +if (ctrl->subsys->type ==NVME_NQN_DISC ) + goto out_unlock; + +list_for_each_entry (p, &ctrl->subsys->hosts, entry) { +if (strcmp (nvmet_host_name (p->host), ctrl->hostnqn)) +continue ; + host = p->host; +break ; + } +if (!host) { +pr_debug ("host %s not found\n" , ctrl->hostnqn); + ret = -ENXIO ; + goto out_unlock; + } + +if (host->dhchap_dhgroup_id != dhgroup_id) { + ret = -EINVAL ; + goto out_unlock; + } + dhgroup_kpp =nvme_auth_dhgroup_kpp (dhgroup_id); +if (!dhgroup_kpp) { + ret = -EINVAL ; + goto out_unlock; + } + ctrl->dh_tfm =crypto_alloc_kpp (dhgroup_kpp,0 ,0 ); +if (IS_ERR (ctrl->dh_tfm)) { +pr_debug ("failed to setup DH group %d, err %ld\n" , + dhgroup_id,PTR_ERR (ctrl->dh_tfm)); + ret =PTR_ERR (ctrl->dh_tfm); + ctrl->dh_tfm =NULL ; + }else { + ctrl->dh_gid = dhgroup_id; + ctrl->dh_keysize =nvme_auth_dhgroup_pubkey_size (dhgroup_id); +pr_debug ("select DH group %d keysize %d\n" , + ctrl->dh_gid, ctrl->dh_keysize); + ret =0 ; + } + +out_unlock: +up_read (&nvmet_config_sem); +return ret; }@@ -192,6 +237,101 @@ bool nvmet_check_auth_status(struct nvmet_req *req)return true; } +static int nvmet_auth_hash_sesskey (struct nvmet_req *req,u8 *hashed_key) +{ +struct nvmet_ctrl *ctrl = req->sq->ctrl; +const char *hmac_name, *digest_name; +struct crypto_shash *tfm; +int hmac_id, ret; + +if (!ctrl->shash_tfm) { +pr_debug ("%s: hash alg not set\n" , __func__); +return -EINVAL ; + } + hmac_name =crypto_shash_alg_name (ctrl->shash_tfm); + hmac_id =nvme_auth_hmac_id (hmac_name); +if (hmac_id <0 ) { +pr_debug ("%s: unsupported hmac %s\n" , __func__, + hmac_name); +return -EINVAL ; + } + digest_name =nvme_auth_digest_name (hmac_id); +if (!digest_name) { +pr_debug ("%s: failed to get digest for %s\n" , __func__, + hmac_name); +return -EINVAL ; + } + tfm =crypto_alloc_shash (digest_name,0 ,0 ); +if (IS_ERR (tfm)) +return -ENOMEM ; + + ret =crypto_shash_tfm_digest (tfm, req->sq->dhchap_skey, + req->sq->dhchap_skey_len, hashed_key); +if (ret <0 ) +pr_debug ("%s: Failed to hash digest len %d\n" , __func__, + req->sq->dhchap_skey_len); + +crypto_free_shash (tfm); +return ret; +} + +static int nvmet_auth_augmented_challenge (struct nvmet_req *req, +u8 *challenge,u8 *aug) +{ +struct nvmet_ctrl *ctrl = req->sq->ctrl; +struct crypto_shash *tfm; +struct shash_desc *desc; +u8 *hashed_key; +const char *hash_name; +int hash_len = req->sq->dhchap_hash_len; +int ret; + + hashed_key =kmalloc (hash_len,GFP_KERNEL ); +if (!hashed_key) +return -ENOMEM ; + + ret =nvmet_auth_hash_sesskey (req, hashed_key); +if (ret <0 ) { +pr_debug ("failed to hash session key, err %d\n" , ret); +kfree (hashed_key); +return ret; + } + hash_name =crypto_shash_alg_name (ctrl->shash_tfm); +if (!hash_name) { +pr_debug ("Invalid hash algoritm\n" ); +return -EINVAL ; + } + tfm =crypto_alloc_shash (hash_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, hash_len); +if (ret) + goto out_free_desc; + ret =crypto_shash_init (desc); +if (ret) + goto out_free_desc; +crypto_shash_update (desc, challenge, hash_len); +crypto_shash_final (desc, aug); + +out_free_desc: +kfree_sensitive (desc); +out_free_hash: +crypto_free_shash (tfm); +out_free_key: +kfree (hashed_key); +return ret; +} +int nvmet_auth_host_hash (struct nvmet_req *req,u8 *response,unsigned int shash_len) {
drivers/nvme/target/auth.c int nvmet_auth_host_hash(struct nvmet_req *req, u8 · line 345
Quoted 6 lines
drivers/nvme/target/auth.c +1
-2@@ -202,8 +342,15 @@ int nvmet_auth_host_hash(struct nvmet_req *req, u8 *response, int ret;if (ctrl->dh_gid !=NVME_AUTH_DHCHAP_DHGROUP_NULL ) { - ret = -ENOTSUPP ; - goto out; + challenge =kmalloc (shash_len,GFP_KERNEL );
Alignment?
Quoted 12 lines
drivers/nvme/target/auth.c +8
-0+if (!challenge) { + ret = -ENOMEM ; + goto out; + } + ret =nvmet_auth_augmented_challenge (req, req->sq->dhchap_c1, + challenge); +if (ret) + goto out; } shash->tfm = ctrl->shash_tfm;
drivers/nvme/target/auth.c int nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 · line 414
Quoted 6 lines
drivers/nvme/target/auth.c +1
-2@@ -264,8 +411,15 @@ int nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 *response, ctrl->cntlid, ctrl->hostnqn);if (ctrl->dh_gid !=NVME_AUTH_DHCHAP_DHGROUP_NULL ) { - ret = -ENOTSUPP ; - goto out; + challenge =kmalloc (shash_len,GFP_KERNEL );
dto.
Quoted 83 lines
drivers/nvme/target/auth.c +76
-0+if (!challenge) { + ret = -ENOMEM ; + goto out; + } + ret =nvmet_auth_augmented_challenge (req, req->sq->dhchap_c2, + challenge); +if (ret) + goto out; } shash->tfm = ctrl->shash_tfm;@@ -307,6 +461,85 @@ int nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 *response, return 0; } +int nvmet_auth_ctrl_exponential (struct nvmet_req *req, +u8 *buf,int buf_size) +{ +struct nvmet_ctrl *ctrl = req->sq->ctrl; +struct kpp_request *kpp_req; +struct crypto_wait wait; +char *pkey; +struct scatterlist dst; +int ret, pkey_len; + +if (ctrl->dh_gid ==NVME_AUTH_DHCHAP_DHGROUP_2048 || + ctrl->dh_gid ==NVME_AUTH_DHCHAP_DHGROUP_3072 || + ctrl->dh_gid ==NVME_AUTH_DHCHAP_DHGROUP_4096 || + ctrl->dh_gid ==NVME_AUTH_DHCHAP_DHGROUP_6144 || + ctrl->dh_gid ==NVME_AUTH_DHCHAP_DHGROUP_8192 ) { +struct dh p = {0 }; +int bits =nvme_auth_dhgroup_pubkey_size (ctrl->dh_gid) <<3 ; + + ret =crypto_ffdhe_params (&p, bits); +if (ret) +return ret; + + p.key = ctrl->dhchap_key; + p.key_size = ctrl->dhchap_key_len; + + pkey_len =crypto_dh_key_len (&p); + pkey =kmalloc (pkey_len,GFP_KERNEL ); +if (!pkey) +return -ENOMEM ; + +get_random_bytes (pkey, pkey_len); + ret =crypto_dh_encode_key (pkey, pkey_len, &p); +if (ret) { +pr_debug ("failed to encode private key, error %d\n" , + ret); + goto out; + } + }else { +pr_warn ("invalid dh group %d\n" , ctrl->dh_gid); +return -EINVAL ; + } + ret =crypto_kpp_set_secret (ctrl->dh_tfm, pkey, pkey_len); +if (ret) { +pr_debug ("failed to set private key, error %d\n" , ret); + goto out; + } + + kpp_req =kpp_request_alloc (ctrl->dh_tfm,GFP_KERNEL ); +if (!kpp_req) { +pr_debug ("cannot allocate kpp request\n" ); + ret = -ENOMEM ; + goto out; + } + +crypto_init_wait (&wait); +kpp_request_set_input (kpp_req,NULL ,0 ); +sg_init_one (&dst, buf, buf_size); +kpp_request_set_output (kpp_req, &dst, buf_size); +kpp_request_set_callback (kpp_req,CRYPTO_TFM_REQ_MAY_BACKLOG , + crypto_req_done, &wait); + + ret =crypto_wait_req (crypto_kpp_generate_public_key (kpp_req), &wait); +kpp_request_free (kpp_req); +if (ret == -EOVERFLOW ) { +pr_debug ("public key buffer too small, need %d is %d\n" , +crypto_kpp_maxsize (ctrl->dh_tfm), buf_size); + ret = -ENOKEY ; + }else if (ret) {
drivers/nvme/target/auth.c int nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 · line 541
Quoted 10 lines
drivers/nvme/target/auth.c +10
-0+pr_debug ("failed to generate public key, err %d\n" , ret); + ret = -ENOKEY ; + }else +pr_debug ("%s: ctrl public key %*ph\n" , __func__, + (int )buf_size, buf); + +out: +kfree_sensitive (pkey); +return ret; +}
In general: the target/host authentication code looks very similar. Is there no way to have a common code base?
Quoted 80 lines
drivers/nvme/target/auth.c +1
-0+int nvmet_auth_ctrl_sesskey (struct nvmet_req *req,u8 *pkey,int pkey_size) {drivers/nvme/target/configfs.c +31
-0@@ -1712,9 +1712,40 @@ static ssize_t nvmet_host_dhchap_hash_store(structconfig_item *item,CONFIGFS_ATTR (nvmet_host_ ,dhchap_hash ); +static ssize_t nvmet_host_dhchap_dhgroup_show (struct config_item *item, +char *page) +{ +struct nvmet_host *host =to_host (item); +const char *dhgroup =nvme_auth_dhgroup_name (host->dhchap_dhgroup_id); + +return sprintf (page,"%s\n" , dhgroup ? dhgroup :"none" ); +} + +static ssize_t nvmet_host_dhchap_dhgroup_store (struct config_item *item, +const char *page,size_t count) +{ +struct nvmet_host *host =to_host (item); +int dhgroup_id; + + dhgroup_id =nvme_auth_dhgroup_id (page); +if (dhgroup_id <0 ) +return -EINVAL ; +if (dhgroup_id !=NVME_AUTH_DHCHAP_DHGROUP_NULL ) { +const char *kpp =nvme_auth_dhgroup_kpp (dhgroup_id); + +if (!crypto_has_kpp (kpp,0 ,0 )) +return -EINVAL ; + } + host->dhchap_dhgroup_id = dhgroup_id; +return count; +} + +CONFIGFS_ATTR (nvmet_host_, dhchap_dhgroup); +static struct configfs_attribute *nvmet_host_attrs[] = { &nvmet_host_attr_dhchap_key, &nvmet_host_attr_dhchap_hash, + &nvmet_host_attr_dhchap_dhgroup,NULL , };#endif /*
CONFIG_NVME_TARGET_AUTH*/(quoted diff) +0
-0
b/drivers/nvme/target/fabrics-cmd-auth.cindex962f9f5e9d89..478ac351c645100644drivers/nvme/target/fabrics-cmd-auth.c +7
-1@@ -98,7 +98,11 @@ static u16 nvmet_auth_reply(struct nvmet_req *req, void *d) return NVME_AUTH_DHCHAP_FAILURE_INVALID_PAYLOAD;if (data->dhvlen) { -return NVME_AUTH_DHCHAP_FAILURE_INVALID_PAYLOAD ; +if (!ctrl->dh_tfm) +return NVME_AUTH_DHCHAP_FAILURE_INVALID_PAYLOAD ; +if (nvmet_auth_ctrl_sesskey (req, data->rval +2 * data->hl, + data->dhvlen) <0 ) +return NVME_AUTH_DHCHAP_FAILURE_DHGROUP_UNUSABLE ; } response =kmalloc (data->hl,GFP_KERNEL );@@ -299,6 +303,8 @@ static int nvmet_auth_challenge(struct nvmet_req *req, void *d, int al) int ret = 0;int data_size =sizeof (*d) + req->sq->dhchap_hash_len; +if (ctrl->dh_tfm) + data_size += ctrl->dh_keysize;if (al < data_size) {pr_debug ("%s: buffer too small (al %d need %d)\n" , __func__, al, data_size);@@ -317,6 +323,12 @@ static int nvmet_auth_challenge(struct nvmet_req *req, void *d, int al) return -ENOMEM;get_random_bytes (req->sq->dhchap_c1, data->hl);
drivers/nvme/target/fabrics-cmd-auth.c static int nvmet_auth_challenge(struct nvmet_req *req, · line 334
Quoted 10 lines
drivers/nvme/target/fabrics-cmd-auth.c +6
-0memcpy (data->cval, req->sq->dhchap_c1, data->hl); +if (ctrl->dh_tfm) { + data->dhgid = ctrl->dh_gid; + data->dhvlen = ctrl->dh_keysize; + ret =nvmet_auth_ctrl_exponential (req, data->cval + data->hl, + data->dhvlen); + }pr_debug ("%s: ctrl %d qid %d seq %d transaction %d hl %d dhvlen %d\n" , __func__, ctrl->cntlid, req->sq->qid, req->sq->dhchap_s1, req->sq->dhchap_tid, data->hl, data->dhvlen);
Ciao
Stephan
Hannes Reinecke
maintainerRe: [PATCH 10/11] nvmet-auth: implement support for augmented challenge
Quoted 182 lines · Stephan Müller
Am Freitag, 16. Juli 2021, 13:04:27 CEST schrieb Hannes Reinecke:
Hi Hannes,
Quoted 178 lines
Implement support for augmented challenge with FFDHE groups. This patch adds a new configfs attribute '
dhchap_dhgroup' to select the DH group to use.Signed-off-by Hannes Reinecke <[email protected]>--- drivers/nvme/target/auth.c | 241 ++++++++++++++++++++++++- drivers/nvme/target/configfs.c | 31 ++++ drivers/nvme/target/fabrics-cmd-auth.c | 14 +- 3 files changed, 281 insertions(+), 5 deletions(-)drivers/nvme/target/auth.c +140
-0@@ -58,11 +58,56 @@ int nvmet_auth_set_host_key(struct nvmet_host *host, const char *secret) int nvmet_setup_dhgroup(struct nvmet_ctrl *ctrl, int dhgroup_id) { +struct nvmet_host_link *p; +struct nvmet_host *host =NULL ; +const char *dhgroup_kpp;int ret = -ENOTSUPP ;if (dhgroup_id ==NVME_AUTH_DHCHAP_DHGROUP_NULL )return 0 ; +down_read (&nvmet_config_sem); +if (ctrl->subsys->type ==NVME_NQN_DISC ) + goto out_unlock; + +list_for_each_entry (p, &ctrl->subsys->hosts, entry) { +if (strcmp (nvmet_host_name (p->host), ctrl->hostnqn)) +continue ; + host = p->host; +break ; + } +if (!host) { +pr_debug ("host %s not found\n" , ctrl->hostnqn); + ret = -ENXIO ; + goto out_unlock; + } + +if (host->dhchap_dhgroup_id != dhgroup_id) { + ret = -EINVAL ; + goto out_unlock; + } + dhgroup_kpp =nvme_auth_dhgroup_kpp (dhgroup_id); +if (!dhgroup_kpp) { + ret = -EINVAL ; + goto out_unlock; + } + ctrl->dh_tfm =crypto_alloc_kpp (dhgroup_kpp,0 ,0 ); +if (IS_ERR (ctrl->dh_tfm)) { +pr_debug ("failed to setup DH group %d, err %ld\n" , + dhgroup_id,PTR_ERR (ctrl->dh_tfm)); + ret =PTR_ERR (ctrl->dh_tfm); + ctrl->dh_tfm =NULL ; + }else { + ctrl->dh_gid = dhgroup_id; + ctrl->dh_keysize =nvme_auth_dhgroup_pubkey_size (dhgroup_id); +pr_debug ("select DH group %d keysize %d\n" , + ctrl->dh_gid, ctrl->dh_keysize); + ret =0 ; + } + +out_unlock: +up_read (&nvmet_config_sem); +return ret; }@@ -192,6 +237,101 @@ bool nvmet_check_auth_status(struct nvmet_req *req)return true; } +static int nvmet_auth_hash_sesskey (struct nvmet_req *req,u8 *hashed_key) +{ +struct nvmet_ctrl *ctrl = req->sq->ctrl; +const char *hmac_name, *digest_name; +struct crypto_shash *tfm; +int hmac_id, ret; + +if (!ctrl->shash_tfm) { +pr_debug ("%s: hash alg not set\n" , __func__); +return -EINVAL ; + } + hmac_name =crypto_shash_alg_name (ctrl->shash_tfm); + hmac_id =nvme_auth_hmac_id (hmac_name); +if (hmac_id <0 ) { +pr_debug ("%s: unsupported hmac %s\n" , __func__, + hmac_name); +return -EINVAL ; + } + digest_name =nvme_auth_digest_name (hmac_id); +if (!digest_name) { +pr_debug ("%s: failed to get digest for %s\n" , __func__, + hmac_name); +return -EINVAL ; + } + tfm =crypto_alloc_shash (digest_name,0 ,0 ); +if (IS_ERR (tfm)) +return -ENOMEM ; + + ret =crypto_shash_tfm_digest (tfm, req->sq->dhchap_skey, + req->sq->dhchap_skey_len, hashed_key); +if (ret <0 ) +pr_debug ("%s: Failed to hash digest len %d\n" , __func__, + req->sq->dhchap_skey_len); + +crypto_free_shash (tfm); +return ret; +} + +static int nvmet_auth_augmented_challenge (struct nvmet_req *req, +u8 *challenge,u8 *aug) +{ +struct nvmet_ctrl *ctrl = req->sq->ctrl; +struct crypto_shash *tfm; +struct shash_desc *desc; +u8 *hashed_key; +const char *hash_name; +int hash_len = req->sq->dhchap_hash_len; +int ret; + + hashed_key =kmalloc (hash_len,GFP_KERNEL ); +if (!hashed_key) +return -ENOMEM ; + + ret =nvmet_auth_hash_sesskey (req, hashed_key); +if (ret <0 ) { +pr_debug ("failed to hash session key, err %d\n" , ret); +kfree (hashed_key); +return ret; + } + hash_name =crypto_shash_alg_name (ctrl->shash_tfm); +if (!hash_name) { +pr_debug ("Invalid hash algoritm\n" ); +return -EINVAL ; + } + tfm =crypto_alloc_shash (hash_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, hash_len); +if (ret) + goto out_free_desc; + ret =crypto_shash_init (desc); +if (ret) + goto out_free_desc; +crypto_shash_update (desc, challenge, hash_len); +crypto_shash_final (desc, aug); + +out_free_desc: +kfree_sensitive (desc); +out_free_hash: +crypto_free_shash (tfm); +out_free_key: +kfree (hashed_key); +return ret; +} +int nvmet_auth_host_hash (struct nvmet_req *req,u8 *response,unsigned int shash_len) {@@ -202,8 +342,15 @@ int nvmet_auth_host_hash(struct nvmet_req *req, u8 *response, int ret;
Quoted 7 lines
drivers/nvme/target/auth.c int nvmet_auth_host_hash(struct nvmet_req *req, u8 *response, · line 345
Quoted 4 lines
drivers/nvme/target/auth.c +1
-2if (ctrl->dh_gid !=NVME_AUTH_DHCHAP_DHGROUP_NULL ) { - ret = -ENOTSUPP ; - goto out; + challenge =kmalloc (shash_len,GFP_KERNEL );Alignment?
With what?
And why?
Quoted 14 lines
Quoted 14 lines
drivers/nvme/target/auth.c +8
-0+if (!challenge) { + ret = -ENOMEM ; + goto out; + } + ret =nvmet_auth_augmented_challenge (req, req->sq->dhchap_c1, + challenge); +if (ret) + goto out; } shash->tfm = ctrl->shash_tfm;@@ -264,8 +411,15 @@ int nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 *response, ctrl->cntlid, ctrl->hostnqn);
Quoted 6 lines
drivers/nvme/target/auth.c int nvmet_auth_host_hash(struct nvmet_req *req, u8 *response, · line 345
Quoted 4 lines
drivers/nvme/target/auth.c +1
-2if (ctrl->dh_gid !=NVME_AUTH_DHCHAP_DHGROUP_NULL ) { - ret = -ENOTSUPP ; - goto out; + challenge =kmalloc (shash_len,GFP_KERNEL );dto.
dto.
Quoted 88 lines
Quoted 88 lines
drivers/nvme/target/auth.c +81
-0+if (!challenge) { + ret = -ENOMEM ; + goto out; + } + ret =nvmet_auth_augmented_challenge (req, req->sq->dhchap_c2, + challenge); +if (ret) + goto out; } shash->tfm = ctrl->shash_tfm;@@ -307,6 +461,85 @@ int nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 *response, return 0; } +int nvmet_auth_ctrl_exponential (struct nvmet_req *req, +u8 *buf,int buf_size) +{ +struct nvmet_ctrl *ctrl = req->sq->ctrl; +struct kpp_request *kpp_req; +struct crypto_wait wait; +char *pkey; +struct scatterlist dst; +int ret, pkey_len; + +if (ctrl->dh_gid ==NVME_AUTH_DHCHAP_DHGROUP_2048 || + ctrl->dh_gid ==NVME_AUTH_DHCHAP_DHGROUP_3072 || + ctrl->dh_gid ==NVME_AUTH_DHCHAP_DHGROUP_4096 || + ctrl->dh_gid ==NVME_AUTH_DHCHAP_DHGROUP_6144 || + ctrl->dh_gid ==NVME_AUTH_DHCHAP_DHGROUP_8192 ) { +struct dh p = {0 }; +int bits =nvme_auth_dhgroup_pubkey_size (ctrl->dh_gid) <<3 ; + + ret =crypto_ffdhe_params (&p, bits); +if (ret) +return ret; + + p.key = ctrl->dhchap_key; + p.key_size = ctrl->dhchap_key_len; + + pkey_len =crypto_dh_key_len (&p); + pkey =kmalloc (pkey_len,GFP_KERNEL ); +if (!pkey) +return -ENOMEM ; + +get_random_bytes (pkey, pkey_len); + ret =crypto_dh_encode_key (pkey, pkey_len, &p); +if (ret) { +pr_debug ("failed to encode private key, error %d\n" , + ret); + goto out; + } + }else { +pr_warn ("invalid dh group %d\n" , ctrl->dh_gid); +return -EINVAL ; + } + ret =crypto_kpp_set_secret (ctrl->dh_tfm, pkey, pkey_len); +if (ret) { +pr_debug ("failed to set private key, error %d\n" , ret); + goto out; + } + + kpp_req =kpp_request_alloc (ctrl->dh_tfm,GFP_KERNEL ); +if (!kpp_req) { +pr_debug ("cannot allocate kpp request\n" ); + ret = -ENOMEM ; + goto out; + } + +crypto_init_wait (&wait); +kpp_request_set_input (kpp_req,NULL ,0 ); +sg_init_one (&dst, buf, buf_size); +kpp_request_set_output (kpp_req, &dst, buf_size); +kpp_request_set_callback (kpp_req,CRYPTO_TFM_REQ_MAY_BACKLOG , + crypto_req_done, &wait); + + ret =crypto_wait_req (crypto_kpp_generate_public_key (kpp_req), &wait); +kpp_request_free (kpp_req); +if (ret == -EOVERFLOW ) { +pr_debug ("public key buffer too small, need %d is %d\n" , +crypto_kpp_maxsize (ctrl->dh_tfm), buf_size); + ret = -ENOKEY ; + }else if (ret) { +pr_debug ("failed to generate public key, err %d\n" , ret); + ret = -ENOKEY ; + }else +pr_debug ("%s: ctrl public key %*ph\n" , __func__, + (int )buf_size, buf);
Quoted 8 lines
drivers/nvme/target/auth.c int nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 *response, · line 541
Quoted 5 lines
drivers/nvme/target/auth.c +5
-0+ +out: +kfree_sensitive (pkey); +return ret; +}In general: the target/host authentication code looks very similar. Is there no way to have a common code base?
That is the plan, but I would need to rework the parameter passing for the target code to also use a 'dhchap' authentication block like the host code does. But Sagi is not entirely happy with it, so I'll rework the code once we have consensus there.
Cheers,
Hannes
Signature · 4 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: Felix Imendörffer
Hannes Reinecke
maintainerPATCH 11/11 nvme: add non-standard ECDH and curve25517 algorithms
In reply to Hannes ReineckeTLS 1.3 specifies ECDH and curve25517 in addition to the FFDHE groups, and these are already implemented in the kernel. So add support for these non-standard groups for NVMe in-band authentication to validate the augmented challenge implementation.
--- drivers/nvme/host/auth.c | 38 +++++++++++++++++++++++++++++++++++++- drivers/nvme/target/auth.c | 23 +++++++++++++++++++++++ include/linux/nvme.h | 2 ++ 3 files changed, 62 insertions(+), 1 deletion(-)
drivers/nvme/host/auth.c +37 -1
@@ -10,6 +10,8 @@#include <crypto/kpp.h> #include <crypto/dh.h> #include <crypto/ffdhe.h> +#include <crypto/ecdh.h> +#include <crypto/curve25519.h> #include "nvme.h" #include "fabrics.h" #include "auth.h"
@@ -67,6 +69,13 @@ struct nvme_auth_dhgroup_map { { .id = NVME_AUTH_DHCHAP_DHGROUP_8192, .name = "ffdhe8192", .kpp = "dh", .privkey_size = 1024, .pubkey_size = 1024 }, + { .id = NVME_AUTH_DHCHAP_DHGROUP_ECDH, + .name = "ecdh", .kpp = "ecdh-nist-p256", + .privkey_size = 32, .pubkey_size = 64 }, + { .id = NVME_AUTH_DHCHAP_DHGROUP_25519, + .name = "curve25519", .kpp = "curve25519", + .privkey_size = CURVE25519_KEY_SIZE, + .pubkey_size = CURVE25519_KEY_SIZE }, };const char *nvme_auth_dhgroup_name (int dhgroup_id)
@@ -337,7 +346,7 @@ static int nvme_auth_dhchap_negotiate(struct nvme_ctrl *ctrl, data->napd =1 ; data->auth_protocol[0 ].dhchap.authid =NVME_AUTH_DHCHAP_AUTH_ID ; data->auth_protocol[0 ].dhchap.halen =3 ; - data->auth_protocol[0 ].dhchap.dhlen =6 ; + data->auth_protocol[0 ].dhchap.dhlen =8 ; data->auth_protocol[0 ].dhchap.idlist[0 ] =NVME_AUTH_DHCHAP_HASH_SHA256 ; data->auth_protocol[0 ].dhchap.idlist[1 ] =NVME_AUTH_DHCHAP_HASH_SHA384 ; data->auth_protocol[0 ].dhchap.idlist[2 ] =NVME_AUTH_DHCHAP_HASH_SHA512 ;
@@ -347,6 +356,8 @@ static int nvme_auth_dhchap_negotiate(struct nvme_ctrl *ctrl, data->auth_protocol[0 ].dhchap.idlist[6 ] =NVME_AUTH_DHCHAP_DHGROUP_4096 ; data->auth_protocol[0 ].dhchap.idlist[7 ] =NVME_AUTH_DHCHAP_DHGROUP_6144 ; data->auth_protocol[0 ].dhchap.idlist[8 ] =NVME_AUTH_DHCHAP_DHGROUP_8192 ; + data->auth_protocol[0 ].dhchap.idlist[9 ] =NVME_AUTH_DHCHAP_DHGROUP_ECDH ; + data->auth_protocol[0 ].dhchap.idlist[10 ] =NVME_AUTH_DHCHAP_DHGROUP_25519 ;return size; }
@@ -889,6 +900,31 @@ static int nvme_auth_dhchap_exponential(struct nvme_ctrl *ctrl, } chap->host_key_len = pubkey_size; chap->sess_key_len = pubkey_size; + }else if (chap->dhgroup_id ==NVME_AUTH_DHCHAP_DHGROUP_ECDH ) { +struct ecdh p = {0 }; + + pkey_len =crypto_ecdh_key_len (&p); + pkey =kzalloc (pkey_len,GFP_KERNEL ); +if (!pkey) +return -ENOMEM ; + +get_random_bytes (pkey, pkey_len); + ret =crypto_ecdh_encode_key (pkey, pkey_len, &p); +if (ret) { +dev_dbg (ctrl->device, +"failed to encode pkey, error %d\n" , ret); +kfree (pkey); +return ret; + } + chap->host_key_len =64 ; + chap->sess_key_len =32 ; + }else if (chap->dhgroup_id ==NVME_AUTH_DHCHAP_DHGROUP_25519 ) { + pkey_len =CURVE25519_KEY_SIZE ; + pkey =kzalloc (pkey_len,GFP_KERNEL ); +if (!pkey) +return -ENOMEM ; +get_random_bytes (pkey, pkey_len); + chap->host_key_len = chap->sess_key_len =CURVE25519_KEY_SIZE ; }else {dev_warn (ctrl->device,"Invalid DH group id %d\n" , chap->dhgroup_id);
drivers/nvme/target/auth.c +23 -0
@@ -13,6 +13,8 @@#include <crypto/kpp.h> #include <crypto/dh.h> #include <crypto/ffdhe.h> +#include <crypto/ecdh.h> +#include <crypto/curve25519.h> #include <linux/crc32.h> #include <linux/base64.h> #include <linux/ctype.h>
@@ -498,6 +500,27 @@ int nvmet_auth_ctrl_exponential(struct nvmet_req *req, ret); goto out; } + }else if (ctrl->dh_gid ==NVME_AUTH_DHCHAP_DHGROUP_ECDH ) { +struct ecdh p = {0 }; + + pkey_len =crypto_ecdh_key_len (&p); + pkey =kmalloc (pkey_len,GFP_KERNEL ); +if (!pkey) +return -ENOMEM ; + +get_random_bytes (pkey, pkey_len); + ret =crypto_ecdh_encode_key (pkey, pkey_len, &p); +if (ret) { +pr_debug ("failed to encode private key, error %d\n" , + ret); + goto out; + } + }else if (ctrl->dh_gid ==NVME_AUTH_DHCHAP_DHGROUP_25519 ) { + pkey_len =CURVE25519_KEY_SIZE ; + pkey =kmalloc (pkey_len,GFP_KERNEL ); +if (!pkey) +return -ENOMEM ; +get_random_bytes (pkey, pkey_len); }else {pr_warn ("invalid dh group %d\n" , ctrl->dh_gid);return -EINVAL ;
include/linux/nvme.h +2 -0
@@ -1476,6 +1476,8 @@ enum {NVME_AUTH_DHCHAP_DHGROUP_4096 =0x03 ,NVME_AUTH_DHCHAP_DHGROUP_6144 =0x04 ,NVME_AUTH_DHCHAP_DHGROUP_8192 =0x05 , +NVME_AUTH_DHCHAP_DHGROUP_ECDH =0x0e , + NVME_AUTH_DHCHAP_DHGROUP_25519 = 0x0f, }; union nvmf_auth_protocol {
Signature · 1 line
2.29.2
Re: [PATCH 11/11] nvme: add non-standard ECDH and curve25517 algorithms
Am Freitag, 16. Juli 2021, 13:04:28 CEST schrieb Hannes Reinecke:
Hi Hannes,
Quoted 1 line
TLS 1.3 specifies ECDH and curve25517 in addition to the FFDHE
curve25519?
Quoted 49 lines
groups, and these are already implemented in the kernel. So add support for these non-standard groups for NVMe in-band authentication to validate the augmented challenge implementation.
Signed-off-by Hannes Reinecke <[email protected]>--- drivers/nvme/host/auth.c | 38 +++++++++++++++++++++++++++++++++++++- drivers/nvme/target/auth.c | 23 +++++++++++++++++++++++ include/linux/nvme.h | 2 ++ 3 files changed, 62 insertions(+), 1 deletion(-)drivers/nvme/host/auth.c +10
-1@@ -10,6 +10,8 @@#include <crypto/kpp.h> #include <crypto/dh.h> #include <crypto/ffdhe.h> +#include <crypto/ecdh.h> +#include <crypto/curve25519.h> #include "nvme.h" #include "fabrics.h" #include "auth.h" @@ -67,6 +69,13 @@ struct nvme_auth_dhgroup_map { { .id = NVME_AUTH_DHCHAP_DHGROUP_8192, .name = "ffdhe8192", .kpp = "dh", .privkey_size = 1024, .pubkey_size = 1024 }, + { .id = NVME_AUTH_DHCHAP_DHGROUP_ECDH, + .name = "ecdh", .kpp = "ecdh-nist-p256", + .privkey_size = 32, .pubkey_size = 64 }, + { .id = NVME_AUTH_DHCHAP_DHGROUP_25519, + .name = "curve25519", .kpp = "curve25519", + .privkey_size = CURVE25519_KEY_SIZE, + .pubkey_size = CURVE25519_KEY_SIZE }, };const char *nvme_auth_dhgroup_name (int dhgroup_id)@@ -337,7 +346,7 @@ static int nvme_auth_dhchap_negotiate(struct nvme_ctrl *ctrl, data->napd =1 ; data->auth_protocol[0 ].dhchap.authid =NVME_AUTH_DHCHAP_AUTH_ID ; data->auth_protocol[0 ].dhchap.halen =3 ; - data->auth_protocol[0 ].dhchap.dhlen =6 ; + data->auth_protocol[0 ].dhchap.dhlen =8 ; data->auth_protocol[0 ].dhchap.idlist[0 ] =NVME_AUTH_DHCHAP_HASH_SHA256 ; data->auth_protocol[0 ].dhchap.idlist[1 ] =NVME_AUTH_DHCHAP_HASH_SHA384 ; data->auth_protocol[0 ].dhchap.idlist[2 ] =NVME_AUTH_DHCHAP_HASH_SHA512 ;@@ -347,6 +356,8 @@ static int nvme_auth_dhchap_negotiate(struct nvme_ctrl *ctrl, data->auth_protocol[0].dhchap.idlist[6] =
Quoted 3 lines
NVME_AUTH_DHCHAP_DHGROUP_4096; data->auth_protocol[0].dhchap.idlist[7] =NVME_AUTH_DHCHAP_DHGROUP_6144; data->auth_protocol[0].dhchap.idlist[8] =NVME_AUTH_DHCHAP_DHGROUP_8192; + data->auth_protocol[0].dhchap.idlist[9]
=
Quoted 1 line
NVME_AUTH_DHCHAP_DHGROUP_ECDH; + data->auth_protocol[0].dhchap.idlist[10]
=
Quoted 82 lines
NVME_AUTH_DHCHAP_DHGROUP_25519;drivers/nvme/host/auth.c +25
-0return size; }@@ -889,6 +900,31 @@ static int nvme_auth_dhchap_exponential(struct nvme_ctrl *ctrl, } chap->host_key_len = pubkey_size; chap->sess_key_len = pubkey_size; + }else if (chap->dhgroup_id ==NVME_AUTH_DHCHAP_DHGROUP_ECDH ) { +struct ecdh p = {0 }; + + pkey_len =crypto_ecdh_key_len (&p); + pkey =kzalloc (pkey_len,GFP_KERNEL ); +if (!pkey) +return -ENOMEM ; + +get_random_bytes (pkey, pkey_len); + ret =crypto_ecdh_encode_key (pkey, pkey_len, &p); +if (ret) { +dev_dbg (ctrl->device, +"failed to encode pkey, error %d\n" , ret); +kfree (pkey); +return ret; + } + chap->host_key_len =64 ; + chap->sess_key_len =32 ; + }else if (chap->dhgroup_id ==NVME_AUTH_DHCHAP_DHGROUP_25519 ) { + pkey_len =CURVE25519_KEY_SIZE ; + pkey =kzalloc (pkey_len,GFP_KERNEL ); +if (!pkey) +return -ENOMEM ; +get_random_bytes (pkey, pkey_len); + chap->host_key_len = chap->sess_key_len =CURVE25519_KEY_SIZE ; }else {dev_warn (ctrl->device,"Invalid DH group id %d\n" , chap->dhgroup_id);drivers/nvme/target/auth.c +23
-0@@ -13,6 +13,8 @@#include <crypto/kpp.h> #include <crypto/dh.h> #include <crypto/ffdhe.h> +#include <crypto/ecdh.h> +#include <crypto/curve25519.h> #include <linux/crc32.h> #include <linux/base64.h> #include <linux/ctype.h> @@ -498,6 +500,27 @@ int nvmet_auth_ctrl_exponential(struct nvmet_req *req, ret); goto out; } + }else if (ctrl->dh_gid ==NVME_AUTH_DHCHAP_DHGROUP_ECDH ) { +struct ecdh p = {0 }; + + pkey_len =crypto_ecdh_key_len (&p); + pkey =kmalloc (pkey_len,GFP_KERNEL ); +if (!pkey) +return -ENOMEM ; + +get_random_bytes (pkey, pkey_len); + ret =crypto_ecdh_encode_key (pkey, pkey_len, &p); +if (ret) { +pr_debug ("failed to encode private key, error %d\n" , + ret); + goto out; + } + }else if (ctrl->dh_gid ==NVME_AUTH_DHCHAP_DHGROUP_25519 ) { + pkey_len =CURVE25519_KEY_SIZE ; + pkey =kmalloc (pkey_len,GFP_KERNEL ); +if (!pkey) +return -ENOMEM ; +get_random_bytes (pkey, pkey_len); }else {pr_warn ("invalid dh group %d\n" , ctrl->dh_gid);return -EINVAL ;include/linux/nvme.h +0
-0
include/linux/nvme.h enum { · line 1483
Quoted 8 lines
include/linux/nvme.h +2
-0@@ -1476,6 +1476,8 @@ enum {NVME_AUTH_DHCHAP_DHGROUP_4096 =0x03 ,NVME_AUTH_DHCHAP_DHGROUP_6144 =0x04 ,NVME_AUTH_DHCHAP_DHGROUP_8192 =0x05 , +NVME_AUTH_DHCHAP_DHGROUP_ECDH =0x0e , + NVME_AUTH_DHCHAP_DHGROUP_25519 = 0x0f, }; union nvmf_auth_protocol {
Ciao
Stephan
Hannes Reinecke
maintainerRe: [PATCH 11/11] nvme: add non-standard ECDH and curve25517 algorithms
Quoted 8 lines · Stephan Müller
Am Freitag, 16. Juli 2021, 13:04:28 CEST schrieb Hannes Reinecke:
Hi Hannes,
Quoted 1 line
TLS 1.3 specifies ECDH and curve25517 in addition to the FFDHE
curve25519?
Of course.
Cheers,
Hannes
Signature · 4 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: Felix Imendörffer
Sagi Grimberg
maintainerRe: [PATCH 11/11] nvme: add non-standard ECDH and curve25517 algorithms
In reply to Hannes ReineckeQuoted 3 lines
TLS 1.3 specifies ECDH and curve25517 in addition to the FFDHE groups, and these are already implemented in the kernel.
So?
Quoted 2 lines
So add support for these non-standard groups for NVMe in-band authentication to validate the augmented challenge implementation.
Why? why should users come to expect controllers to support it?
Hannes Reinecke
maintainerRe: [PATCH 11/11] nvme: add non-standard ECDH and curve25517 algorithms
Quoted 10 lines · Sagi Grimberg
Quoted 3 lines
TLS 1.3 specifies ECDH and curve25517 in addition to the FFDHE groups, and these are already implemented in the kernel.
So?
Quoted 2 lines
So add support for these non-standard groups for NVMe in-band authentication to validate the augmented challenge implementation.
Why? why should users come to expect controllers to support it?
Having ECDH and curve25517 algorithms (which are known-good implementations) allows one to validate the ffdhe implementation, ie to ensure that the remainder of the protocol works as designed, even if the ffdhe implementation might not.
And one could argue that TLS1.3 specifies all of these algorithms, so NVMe with it's explicit reference to TLS should do so, too.
But I don't insist on it; it's just nice for debugging, that's all.
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), GF: Felix Imendörffer
Sagi Grimberg
maintainerQuoted 2 lines
Hi all,
Hey Hannes, nice progress. This is definitely
a step in the right direction.
Quoted 3 lines
recent updates to the NVMe spec have added definitions for in-band authentication, and seeing that it provides some real benefit especially for NVMe-TCP here's an attempt to implement it.
Please call out the TP 8006 specifically so people can look into it.
Quoted 5 lines
Tricky bit here is that the specification orients itself on TLS 1.3, but supports only the FFDHE groups. Which of course the kernel doesn't support. I've been able to come up with a patch for this, but as this is my first attempt to fix anything in the crypto area I would invite people more familiar with these matters to have a look.
Glad to see this turned out to be very simple!
Quoted 4 lines
Also note that this is just for in-band authentication. Secure concatenation (ie starting TLS with the negotiated parameters) is not implemented; one would need to update the kernel TLS implementation for this, which at this time is beyond scope.
TLS is an additional effort, as discussed, inband auth alone has merits and we should not lock it down to NVMe/TCP-TLS.
Quoted 1 line
As usual, comments and reviews are welcome.
Having another look into this now...
Quoted 12 lines · Hannes Reinecke
Hi all,
recent updates to the NVMe spec have added definitions for in-band authentication, and seeing that it provides some real benefit especially for NVMe-TCP here's an attempt to implement it.
Tricky bit here is that the specification orients itself on TLS 1.3, but supports only the FFDHE groups. Which of course the kernel doesn't support. I've been able to come up with a patch for this, but as this is my first attempt to fix anything in the crypto area I would invite people more familiar with these matters to have a look.
Quoted 6 lines
Also note that this is just for in-band authentication. Secure concatenation (ie starting TLS with the negotiated parameters) is not implemented; one would need to update the kernel TLS implementation for this, which at this time is beyond scope.
As usual, comments and reviews are welcome.
Hi Hannes,
could you please reference the specific standards that describe the NVMe authentication protocols?
Thanks,
Simo.
Signature · 3 lines
Simo Sorce RHEL Crypto Team Red Hat, Inc
Hannes Reinecke
maintainerQuoted 18 lines · Simo Sorce
Quoted 17 lines · Hannes Reinecke
Hi all,
recent updates to the NVMe spec have added definitions for in-band authentication, and seeing that it provides some real benefit especially for NVMe-TCP here's an attempt to implement it.
Tricky bit here is that the specification orients itself on TLS 1.3, but supports only the FFDHE groups. Which of course the kernel doesn't support. I've been able to come up with a patch for this, but as this is my first attempt to fix anything in the crypto area I would invite people more familiar with these matters to have a look.
Also note that this is just for in-band authentication. Secure concatenation (ie starting TLS with the negotiated parameters) is not implemented; one would need to update the kernel TLS implementation for this, which at this time is beyond scope.
Quoted 6 lines
Quoted 1 line
As usual, comments and reviews are welcome.
Hi Hannes,
could you please reference the specific standards that describe the NVMe authentication protocols?
Section '8.13 NVMe-over-Fabrics In-band authentication'
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), GF: Felix Imendörffer
Hi,
Great to see those patches coming! After some review, they look to be very well done. Some comments/suggestions below.
1. I strongly recommend to implement DH exponentials reuse (g x mod p / g y mod p as well as g xy mod p) as specified in section 8.13.5.7 "DH-HMAC-CHAP Security Requirements". When I was working on TP 8006 I had a prototype that demonstrated that DH math has quite significant latency, something like (as far as I remember) 30ms for 4K group and few hundreds of ms for 8K group. For single connection it is not a big deal, but imagine AMD EPYC with 128 cores. Since all connections are created sequentially, even with 30 ms per connection time to complete full remote device connection would become 128*30 => almost 4 seconds. With 8K group it might be more than 10 seconds. Users are unlikely going to be happy with this, especially in cases, when connecting multiple of NVMe-oF devices is a part of a server or VM boot sequence.
If DH exponential reuse implemented, for all subsequent connections the DH math is excluded, so authentication overhead becomes pretty much negligible.
In my prototype I implemented DH exponential reuse as a simple per-host/target cache that keeps DH exponentials (including g xy mod p) for up to 10 seconds. Simple and sufficient.
Another, might be ever more significant reason why DH exponential reuse is important is that without it x (or y on the host side) must always be randomly generated each time a new connection is established. Which means, for instance, for 8K groups for each connection 1KB of random bytes must be taken from the random pool. With 128 connections it is now 128KB. Quite a big pressure on the random pool that DH exponential reuse mostly avoids.
Those are the 2 reasons why we added this DH exponential reuse sentence in the spec. In the original TP 8006 there was a small informative piece explaining reasonings behind that, but for some reasons it was removed from the final version.
2. What is the status of this code from perspective of stability in face of malicious host behavior? Seems implementation is carefully done, but, for instance, at the first look I was not able to find a code to clean up if host in not acting for too long in the middle of exchange. Other observation is that in nvmet_execute_auth_send() nvmet_check_transfer_len() does not check if tl size is reasonable, i.e., for instance, not 1GB.
For sure, we don't want to allow remote hosts to hang or crash target. For instance, because of OOM conditions that happened, because malicious host asked target to allocate too much memory or open to many being authenticated connections in which the host is not going to reply in the middle of exchange.
Asking, because don't want to go in my review too far ahead from the author ;)
In this regard, it would be great if you add in your test application ability to perform authentication with random parameters and randomly stop responding. Overnight running of such test would give us good degree of confidence that it will always work as expected.
Vlad
Quoted 63 lines · Hannes Reinecke
Hi all,
recent updates to the NVMe spec have added definitions for in-band authentication, and seeing that it provides some real benefit especially for NVMe-TCP here's an attempt to implement it.
Tricky bit here is that the specification orients itself on TLS 1.3, but supports only the FFDHE groups. Which of course the kernel doesn't support. I've been able to come up with a patch for this, but as this is my first attempt to fix anything in the crypto area I would invite people more familiar with these matters to have a look.
Also note that this is just for in-band authentication. Secure concatenation (ie starting TLS with the negotiated parameters) is not implemented; one would need to update the kernel TLS implementation for this, which at this time is beyond scope.
As usual, comments and reviews are welcome.
Hannes Reinecke (11):
crypto: addcrypto_has_shash()
crypto: addcrypto_has_kpp()
crypto/ffdhe: Finite Field DH Ephemeral Parameters lib/base64: RFC4648-compliant base64 encoding
nvme: add definitions for NVMe In-Band authentication
nvme: Implement In-Band authentication
nvme-auth: augmented challenge support
nvmet: Parse fabrics commands on all queues
nvmet: Implement basic In-Band Authentication
nvmet-auth: implement support for augmented challenge
nvme: add non-standard ECDH and curve25517 algorithmscrypto/Kconfig | 8 + crypto/Makefile | 1 + crypto/ffdhe_helper.c | 877 +++++++++++++++++ crypto/kpp.c | 6 + crypto/shash.c | 6 + drivers/nvme/host/Kconfig | 11 + drivers/nvme/host/Makefile | 1 + drivers/nvme/host/auth.c | 1188 ++++++++++++++++++++++++ drivers/nvme/host/auth.h | 23 + drivers/nvme/host/core.c | 77 +- drivers/nvme/host/fabrics.c | 65 +- drivers/nvme/host/fabrics.h | 8 + drivers/nvme/host/nvme.h | 15 + drivers/nvme/host/trace.c | 32 + drivers/nvme/target/Kconfig | 10 + drivers/nvme/target/Makefile | 1 + drivers/nvme/target/admin-cmd.c | 4 + drivers/nvme/target/auth.c | 608 ++++++++++++ drivers/nvme/target/configfs.c | 102 +- drivers/nvme/target/core.c | 10 + drivers/nvme/target/fabrics-cmd-auth.c | 472 ++++++++++ drivers/nvme/target/fabrics-cmd.c | 30 +- drivers/nvme/target/nvmet.h | 71 ++ include/crypto/ffdhe.h | 24 + include/crypto/hash.h | 2 + include/crypto/kpp.h | 2 + include/linux/base64.h | 16 + include/linux/nvme.h | 187 +++- lib/Makefile | 2 +- lib/base64.c | 111 +++ 30 files changed, 3961 insertions(+), 9 deletions(-)
Quoted 9 lines
create mode 100644 crypto/ffdhe_helper.c create mode 100644 drivers/nvme/host/auth.c create mode 100644 drivers/nvme/host/auth.h create mode 100644 drivers/nvme/target/auth.c create mode 100644 drivers/nvme/target/fabrics-cmd-auth.c create mode 100644 include/crypto/ffdhe.h create mode 100644 include/linux/base64.h create mode 100644 lib/base64.c
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Hannes Reinecke
maintainerQuoted 5 lines · Vladislav Bolkhovitin
Hi,
Great to see those patches coming! After some review, they look to be very well done. Some comments/suggestions below.
Quoted 13 lines
1. I strongly recommend to implement DH exponentials reuse (g x mod p / g y mod p as well as g xy mod p) as specified in section 8.13.5.7 "DH-HMAC-CHAP Security Requirements". When I was working on TP 8006 I had a prototype that demonstrated that DH math has quite significant latency, something like (as far as I remember) 30ms for 4K group and few hundreds of ms for 8K group. For single connection it is not a big deal, but imagine AMD EPYC with 128 cores. Since all connections are created sequentially, even with 30 ms per connection time to complete full remote device connection would become 128*30 => almost 4 seconds. With 8K group it might be more than 10 seconds. Users are unlikely going to be happy with this, especially in cases, when connecting multiple of NVMe-oF devices is a part of a server or VM boot sequence.
Oh, indeed, I can confirm that. FFDHE calculations are quite time-consuming. But incidentally, ECDH and curve25519 are reasonably fast, so maybe there _is_ a value in having a TPAR asking for them to be specified, too ...
Quoted 8 lines
If DH exponential reuse implemented, for all subsequent connections the DH math is excluded, so authentication overhead becomes pretty much negligible.
In my prototype I implemented DH exponential reuse as a simple per-host/target cache that keeps DH exponentials (including g xy mod p) for up to 10 seconds. Simple and sufficient.
Frankly, I hadn't looked at exponential reuse; this implementation really is just a first step to get feedback from people if this is a direction they want to go.
Quoted 8 lines
Another, might be ever more significant reason why DH exponential reuse is important is that without it x (or y on the host side) must always be randomly generated each time a new connection is established. Which means, for instance, for 8K groups for each connection 1KB of random bytes must be taken from the random pool. With 128 connections it is now 128KB. Quite a big pressure on the random pool that DH exponential reuse mostly avoids.
Quoted 5 lines
Those are the 2 reasons why we added this DH exponential reuse sentence in the spec. In the original TP 8006 there was a small informative piece explaining reasonings behind that, but for some reasons it was removed from the final version.
Thanks for the hint. I'll be adding exponential reuse to the code.
Quoted 8 lines
2. What is the status of this code from perspective of stability in face of malicious host behavior? Seems implementation is carefully done, but, for instance, at the first look I was not able to find a code to clean up if host in not acting for too long in the middle of exchange. Other observation is that in
nvmet_execute_auth_send()nvmet_check_transfer_len()does not check if tl size is reasonable, i.e., for instance, not 1GB.
That is true; exchange timeouts are missing. Will be adding them, of course. And haven't thought of checking for tl size overflows; will be adding them, too.
Quoted 6 lines
For sure, we don't want to allow remote hosts to hang or crash target. For instance, because of OOM conditions that happened, because malicious host asked target to allocate too much memory or open to many being authenticated connections in which the host is not going to reply in the middle of exchange.
This is something I'll need to look at, anyway. What we do not want is a userspace application chipping in and send a 'negotiate' command without any subsequent steps, thereby invalidating the existing authentication.
Quoted 8 lines
Asking, because don't want to go in my review too far ahead from the author ;)
In this regard, it would be great if you add in your test application ability to perform authentication with random parameters and randomly stop responding. Overnight running of such test would give us good degree of confidence that it will always work as expected.
That indeed would be good; let me think on how something like that can be implemented.
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), GF: Felix Imendörffer
Quoted 19 lines · Hannes Reinecke
Quoted 5 lines · Vladislav Bolkhovitin
Hi,
Great to see those patches coming! After some review, they look to be very well done. Some comments/suggestions below.
Quoted 13 lines
1. I strongly recommend to implement DH exponentials reuse (g x mod p / g y mod p as well as g xy mod p) as specified in section 8.13.5.7 "DH-HMAC-CHAP Security Requirements". When I was working on TP 8006 I had a prototype that demonstrated that DH math has quite significant latency, something like (as far as I remember) 30ms for 4K group and few hundreds of ms for 8K group. For single connection it is not a big deal, but imagine AMD EPYC with 128 cores. Since all connections are created sequentially, even with 30 ms per connection time to complete full remote device connection would become 128*30 => almost 4 seconds. With 8K group it might be more than 10 seconds. Users are unlikely going to be happy with this, especially in cases, when connecting multiple of NVMe-oF devices is a part of a server or VM boot sequence.
Quoted 2 lines
Oh, indeed, I can confirm that. FFDHE calculations are quite time-consuming. But incidentally, ECDH and curve25519 are reasonably fast,
Yes, EC calculations are very fast, this is why EC cryptography is gaining more and more popularity.
Quoted 2 lines
so maybe
there _is_ a value in having a TPAR asking for them to be specified, too ...
There's too much politics and procedures involved here. Even in the current scope it took more, than 2 years to get the spec officially done (I started proposing it early 2018). Maybe, in future, if someone comes in the the committee with the corresponding proposal and value justification.
Although, frankly speaking, with DH exponentials reuse I personally don't see much value in ECDH in this application. Maybe, only for very small embedded devices with really limited computational capabilities.
Quoted 4 lines
Quoted 4 lines
If DH exponential reuse implemented, for all subsequent connections the DH math is excluded, so authentication overhead becomes pretty much negligible.
Quoted 8 lines
Quoted 4 lines
In my prototype I implemented DH exponential reuse as a simple per-host/target cache that keeps DH exponentials (including g xy mod p) for up to 10 seconds. Simple and sufficient.
Frankly, I hadn't looked at exponential reuse; this implementation really is just a first step to get feedback from people if this is a direction they want to go.
Sure, I understand.
Quoted 8 lines
Quoted 8 lines
Another, might be ever more significant reason why DH exponential reuse is important is that without it x (or y on the host side) must always be randomly generated each time a new connection is established. Which means, for instance, for 8K groups for each connection 1KB of random bytes must be taken from the random pool. With 128 connections it is now 128KB. Quite a big pressure on the random pool that DH exponential reuse mostly avoids.
Quoted 7 lines
Quoted 5 lines
Those are the 2 reasons why we added this DH exponential reuse sentence in the spec. In the original TP 8006 there was a small informative piece explaining reasonings behind that, but for some reasons it was removed from the final version.
Thanks for the hint. I'll be adding exponential reuse to the code.
Yes, please. Otherwise, people might start talking that Linux NVMe-oF authentication is too bad and slow.
Vlad
Another comment is that better to perform CRC check of dhchap_secret and generation of dhchap_key right where the secret was specified (e.g., nvmet_auth_set_host_key() on the target side).
No need to do it every time for every connection and by that increase authentication latency. As I wrote in the other comment, it might be 128 or more connections established during connecting to a single NVMe-oF device.
Vlad
Hannes Reinecke
maintainerQuoted 9 lines · Vladislav Bolkhovitin
Another comment is that better to perform CRC check of
dhchap_secretand generation ofdhchap_keyright where the secret was specified (e.g.,nvmet_auth_set_host_key()on the target side).No need to do it every time for every connection and by that increase authentication latency. As I wrote in the other comment, it might be 128 or more connections established during connecting to a single NVMe-oF device.
And this is something I did deliberately.
The primary issue here is that the user might want/need to change the PSK for re-authentication. But for that he might need to check what the original/current PSK is, so I think we need to keep the original PSK as passed in from the user. And I found it a waste of space to store the decoded secret in addition to the original one, seeing that it can be derived from the original one.
But your argument about the many connections required for a single NVMe association is certainly true, to it would make sense to keep the decode one around, too, just to speed up computation.
Will be updating the patchset.
Cheers,
Hannes
Signature · 4 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: Felix Imendörffer