Skip to content

Commit c5931d5

Browse files
cleechkeithbusch
authored andcommitted
nvme-auth: use hkdf_expand_label()
When generating keying material during an authentication transaction (secure channel concatenation), the HKDF-Expand-Label function is part of the specified key derivation process. The current open-coded implementation misses the length prefix requirements on the HkdfLabel label and context variable-length vectors (RFC 8446 Section 3.4). Instead, use the hkdf_expand_label() function. Signed-off-by: Chris Leech <cleech@redhat.com> Signed-off-by: Hannes Reinecke <hare@kernel.org> Signed-off-by: Keith Busch <kbusch@kernel.org>
1 parent 1cab50d commit c5931d5

1 file changed

Lines changed: 13 additions & 20 deletions

File tree

drivers/nvme/common/auth.c

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -768,10 +768,10 @@ int nvme_auth_derive_tls_psk(int hmac_id, u8 *psk, size_t psk_len,
768768
{
769769
struct crypto_shash *hmac_tfm;
770770
const char *hmac_name;
771-
const char *psk_prefix = "tls13 nvme-tls-psk";
771+
const char *label = "nvme-tls-psk";
772772
static const char default_salt[HKDF_MAX_HASHLEN];
773-
size_t info_len, prk_len;
774-
char *info;
773+
size_t prk_len;
774+
const char *ctx;
775775
unsigned char *prk, *tls_key;
776776
int ret;
777777

@@ -811,36 +811,29 @@ int nvme_auth_derive_tls_psk(int hmac_id, u8 *psk, size_t psk_len,
811811
if (ret)
812812
goto out_free_prk;
813813

814-
/*
815-
* 2 additional bytes for the length field from HDKF-Expand-Label,
816-
* 2 additional bytes for the HMAC ID, and one byte for the space
817-
* separator.
818-
*/
819-
info_len = strlen(psk_digest) + strlen(psk_prefix) + 5;
820-
info = kzalloc(info_len + 1, GFP_KERNEL);
821-
if (!info) {
814+
ctx = kasprintf(GFP_KERNEL, "%02d %s", hmac_id, psk_digest);
815+
if (!ctx) {
822816
ret = -ENOMEM;
823817
goto out_free_prk;
824818
}
825819

826-
put_unaligned_be16(psk_len, info);
827-
memcpy(info + 2, psk_prefix, strlen(psk_prefix));
828-
sprintf(info + 2 + strlen(psk_prefix), "%02d %s", hmac_id, psk_digest);
829-
830820
tls_key = kzalloc(psk_len, GFP_KERNEL);
831821
if (!tls_key) {
832822
ret = -ENOMEM;
833-
goto out_free_info;
823+
goto out_free_ctx;
834824
}
835-
ret = hkdf_expand(hmac_tfm, info, info_len, tls_key, psk_len);
825+
ret = hkdf_expand_label(hmac_tfm,
826+
label, strlen(label),
827+
ctx, strlen(ctx),
828+
tls_key, psk_len);
836829
if (ret) {
837830
kfree(tls_key);
838-
goto out_free_info;
831+
goto out_free_ctx;
839832
}
840833
*ret_psk = tls_key;
841834

842-
out_free_info:
843-
kfree(info);
835+
out_free_ctx:
836+
kfree(ctx);
844837
out_free_prk:
845838
kfree(prk);
846839
out_free_shash:

0 commit comments

Comments
 (0)