Skip to content

Commit

Permalink
libnvme: Implement 'nvme_generate_tls_key_identity()'
Browse files Browse the repository at this point in the history
Implement a function to generate the TLS key identity.

Signed-off-by: Hannes Reinecke <hare@suse.de>
  • Loading branch information
hreinecke committed Nov 17, 2023
1 parent 7837064 commit 9f0850d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/libnvme.map
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ LIBNVME_1_7 {
nvme_init_copy_range_f2;
nvme_init_copy_range_f3;
nvme_insert_tls_key_versioned;
nvme_generate_tls_key_identity;
};

LIBNVME_1_6 {
Expand Down
42 changes: 42 additions & 0 deletions src/nvme/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,38 @@ long nvme_insert_tls_key_versioned(const char *keyring, const char *key_type,
return key;
}

char *nvme_generate_tls_key_identity(const char *hostnqn, const char *subsysnqn,
int version, int hmac,
unsigned char *configured_key, int key_len)
{
char *identity;
size_t identity_len;
unsigned char *psk;
int ret;

identity_len = nvme_identity_len(hmac, version, hostnqn, subsysnqn);
if (identity_len < 0)
return NULL;

identity = malloc(identity_len);
if (!identity)
return NULL;

psk = malloc(key_len);
if (!psk)
goto out_free_identity;

memset(psk, 0, key_len);
ret = derive_nvme_keys(hostnqn, subsysnqn, identity, version, hmac,
configured_key, psk, key_len);
free(psk);
out_free_identity:
if (ret < 0) {
free(identity);
identity = NULL;
}
return identity;
}
#else
long nvme_lookup_keyring(const char *keyring)
{
Expand Down Expand Up @@ -1274,6 +1306,16 @@ long nvme_insert_tls_key_versioned(const char *keyring, const char *key_type,
errno = ENOTSUP;
return -1;
}

char *nvme_generate_tls_key_identity(const char *hostnqn, const char *subsysnqn,
int version, int hmac,
unsigned char *configured_key, int key_len)
{
nvme_msg(NULL, LOG_ERR, "key operations not supported; " \
"recompile with keyutils support.\n");
errno = ENOTSUP;
return -1;
}
#endif

long nvme_insert_tls_key(const char *keyring, const char *key_type,
Expand Down
19 changes: 19 additions & 0 deletions src/nvme/linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,23 @@ long nvme_insert_tls_key_versioned(const char *keyring, const char *key_type,
int version, int hmac,
unsigned char *configured_key, int key_len);

/**
* nvme_generate_tls_key_identity() - Generate the TLS key identity
* @hostnqn: Host NVMe Qualified Name
* @subsysnqn: Subsystem NVMe Qualified Name
* @version: Key version to use
* @hmac: HMAC algorithm
* @configured_key: Configured key data to derive the key from
* @key_len: Length of @configured_key
*
* Derives a 'retained' TLS key as specified in NVMe TCP and
* generate the corresponding TLs identity.
*
* Return: The string containing the TLS identity. It is the responsibility
* of the caller to free the returned string.
*/
char *nvme_generate_tls_key_identity(const char *hostnqn, const char *subsysnqn,
int version, int hmac,
unsigned char *configured_key, int key_len);

#endif /* _LIBNVME_LINUX_H */

0 comments on commit 9f0850d

Please sign in to comment.