Skip to content

Commit 9d5c0ff

Browse files
Hannes Reineckekeithbusch
authored andcommitted
nvme: add nvme_auth_derive_tls_psk()
Add a function to derive the TLS PSK as specified TP8018. Signed-off-by: Hannes Reinecke <[email protected]> Reviewed-by: Sagi Grimberg <[email protected]> Signed-off-by: Keith Busch <[email protected]>
1 parent 71972b9 commit 9d5c0ff

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

drivers/nvme/common/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ config NVME_AUTH
1212
select CRYPTO_SHA512
1313
select CRYPTO_DH
1414
select CRYPTO_DH_RFC7919_GROUPS
15+
select CRYPTO_HKDF

drivers/nvme/common/auth.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <linux/nvme.h>
1616
#include <linux/nvme-auth.h>
1717

18+
#define HKDF_MAX_HASHLEN 64
19+
1820
static u32 nvme_dhchap_seqnum;
1921
static DEFINE_MUTEX(nvme_dhchap_mutex);
2022

@@ -692,5 +694,119 @@ int nvme_auth_generate_digest(u8 hmac_id, u8 *psk, size_t psk_len,
692694
}
693695
EXPORT_SYMBOL_GPL(nvme_auth_generate_digest);
694696

697+
/**
698+
* nvme_auth_derive_tls_psk - Derive TLS PSK
699+
* @hmac_id: Hash function identifier
700+
* @psk: generated input PSK
701+
* @psk_len: size of @psk
702+
* @psk_digest: TLS PSK digest
703+
* @ret_psk: Pointer to the resulting TLS PSK
704+
*
705+
* Derive a TLS PSK as specified in TP8018 Section 3.6.1.3:
706+
* TLS PSK and PSK identity Derivation
707+
*
708+
* The TLS PSK shall be derived as follows from an input PSK
709+
* (i.e., either a retained PSK or a generated PSK) and a PSK
710+
* identity using the HKDF-Extract and HKDF-Expand-Label operations
711+
* (refer to RFC 5869 and RFC 8446) where the hash function is the
712+
* one specified by the hash specifier of the PSK identity:
713+
* 1. PRK = HKDF-Extract(0, Input PSK); and
714+
* 2. TLS PSK = HKDF-Expand-Label(PRK, "nvme-tls-psk", PskIdentityContext, L),
715+
* where PskIdentityContext is the hash identifier indicated in
716+
* the PSK identity concatenated to a space character and to the
717+
* Base64 PSK digest (i.e., "<hash> <PSK digest>") and L is the
718+
* output size in bytes of the hash function (i.e., 32 for SHA-256
719+
* and 48 for SHA-384).
720+
*
721+
* Returns 0 on success with a valid psk pointer in @ret_psk or a negative
722+
* error number otherwise.
723+
*/
724+
int nvme_auth_derive_tls_psk(int hmac_id, u8 *psk, size_t psk_len,
725+
u8 *psk_digest, u8 **ret_psk)
726+
{
727+
struct crypto_shash *hmac_tfm;
728+
const char *hmac_name;
729+
const char *psk_prefix = "tls13 nvme-tls-psk";
730+
static const char default_salt[HKDF_MAX_HASHLEN];
731+
size_t info_len, prk_len;
732+
char *info;
733+
unsigned char *prk, *tls_key;
734+
int ret;
735+
736+
hmac_name = nvme_auth_hmac_name(hmac_id);
737+
if (!hmac_name) {
738+
pr_warn("%s: invalid hash algorithm %d\n",
739+
__func__, hmac_id);
740+
return -EINVAL;
741+
}
742+
if (hmac_id == NVME_AUTH_HASH_SHA512) {
743+
pr_warn("%s: unsupported hash algorithm %s\n",
744+
__func__, hmac_name);
745+
return -EINVAL;
746+
}
747+
748+
hmac_tfm = crypto_alloc_shash(hmac_name, 0, 0);
749+
if (IS_ERR(hmac_tfm))
750+
return PTR_ERR(hmac_tfm);
751+
752+
prk_len = crypto_shash_digestsize(hmac_tfm);
753+
prk = kzalloc(prk_len, GFP_KERNEL);
754+
if (!prk) {
755+
ret = -ENOMEM;
756+
goto out_free_shash;
757+
}
758+
759+
if (WARN_ON(prk_len > HKDF_MAX_HASHLEN)) {
760+
ret = -EINVAL;
761+
goto out_free_prk;
762+
}
763+
ret = hkdf_extract(hmac_tfm, psk, psk_len,
764+
default_salt, prk_len, prk);
765+
if (ret)
766+
goto out_free_prk;
767+
768+
ret = crypto_shash_setkey(hmac_tfm, prk, prk_len);
769+
if (ret)
770+
goto out_free_prk;
771+
772+
/*
773+
* 2 addtional bytes for the length field from HDKF-Expand-Label,
774+
* 2 addtional bytes for the HMAC ID, and one byte for the space
775+
* separator.
776+
*/
777+
info_len = strlen(psk_digest) + strlen(psk_prefix) + 5;
778+
info = kzalloc(info_len + 1, GFP_KERNEL);
779+
if (!info) {
780+
ret = -ENOMEM;
781+
goto out_free_prk;
782+
}
783+
784+
put_unaligned_be16(psk_len, info);
785+
memcpy(info + 2, psk_prefix, strlen(psk_prefix));
786+
sprintf(info + 2 + strlen(psk_prefix), "%02d %s", hmac_id, psk_digest);
787+
788+
tls_key = kzalloc(psk_len, GFP_KERNEL);
789+
if (!tls_key) {
790+
ret = -ENOMEM;
791+
goto out_free_info;
792+
}
793+
ret = hkdf_expand(hmac_tfm, info, info_len, tls_key, psk_len);
794+
if (ret) {
795+
kfree(tls_key);
796+
goto out_free_info;
797+
}
798+
*ret_psk = tls_key;
799+
800+
out_free_info:
801+
kfree(info);
802+
out_free_prk:
803+
kfree(prk);
804+
out_free_shash:
805+
crypto_free_shash(hmac_tfm);
806+
807+
return ret;
808+
}
809+
EXPORT_SYMBOL_GPL(nvme_auth_derive_tls_psk);
810+
695811
MODULE_DESCRIPTION("NVMe Authentication framework");
696812
MODULE_LICENSE("GPL v2");

include/linux/nvme-auth.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,7 @@ int nvme_auth_generate_psk(u8 hmac_id, u8 *skey, size_t skey_len,
4545
u8 **ret_psk, size_t *ret_len);
4646
int nvme_auth_generate_digest(u8 hmac_id, u8 *psk, size_t psk_len,
4747
char *subsysnqn, char *hostnqn, u8 **ret_digest);
48+
int nvme_auth_derive_tls_psk(int hmac_id, u8 *psk, size_t psk_len,
49+
u8 *psk_digest, u8 **ret_psk);
4850

4951
#endif /* _NVME_AUTH_H */

0 commit comments

Comments
 (0)