lib/crypto: add HKDF and convert fscrypt and NVMe
Subsystems (6)
cover lib/crypto: add HKDF and convert fscrypt and NVMe
fscrypt and NVMe authentication each carry private HKDF implementations. Add HKDF-SHA256, HKDF-SHA384, and HKDF-SHA512 functions to lib/crypto and convert both users.
Patches 1-4 are intended for review and inclusion.
Patch 5 is RFC only and is not intended to be applied. It shows how the OVPN epoch-key code currently under development can use the new HKDF API. That patch has an additional dependency on the OVPN epoch series. It is included here to allow the API to be reviewed against a third user. The OVPN conversion will be submitted separately once its prerequisite code is ready.
HKDF-Expand accepts info as an array of segments and processes them as if concatenated. This preserves the byte ordering used by fscrypt and allows NVMe to encode HkdfLabel without allocating a temporary buffer.
HKDF-Extract produces a prepared HMAC key, avoiding repeated key setup when the same PRK is expanded more than once. The implementation uses the direct HMAC library functions and neither allocates memory nor returns errors.
The series is intended for the libcrypto tree. Patches 3 and 4 need review from the fscrypt and NVMe maintainers, respectively.
Tested with:
- HKDF KUnit tests
- NVMe authentication KUnit tests
- kvm-xfstests smoketest on ext4
- kvm-xfstests generic/582, generic/583, and generic/584 on ext4 and f2fs
- kvm-xfstests encrypt group on ext4 and f2fs
The following shortlog and diffstat refers only to the first 4 patches.
Marco Baffo (4):
lib/crypto: add HKDF-SHA{256,384,512}
lib/crypto: tests: add HKDF KUnit tests
fscrypt: use HKDF library functions
nvme-auth: use HKDF library functions for TLS PSK derivation
drivers/nvme/common/auth.c | 120 ++++++++------- fs/crypto/hkdf.c | 56 ++----- include/crypto/hkdf.h | 131 ++++++++++++++++ lib/crypto/.kunitconfig | 1 + lib/crypto/Makefile | 4 +- lib/crypto/hkdf-sha256.c | 22 +++ lib/crypto/hkdf-sha512.c | 34 +++++ lib/crypto/hkdf-template.h | 97 ++++++++++++ lib/crypto/tests/Kconfig | 8 + lib/crypto/tests/Makefile | 1 + lib/crypto/tests/hkdf_kunit.c | 276 ++++++++++++++++++++++++++++++++++ 11 files changed, 653 insertions(+), 97 deletions(-) create mode 100644 include/crypto/hkdf.h create mode 100644 lib/crypto/hkdf-sha256.c create mode 100644 lib/crypto/hkdf-sha512.c create mode 100644 lib/crypto/hkdf-template.h create mode 100644 lib/crypto/tests/hkdf_kunit.c
base-commit: e4f23159c8d09b6b15d47f2c95e0a3e7c62f52a2
Signature · 1 line
2.43.0
PATCH 1/5 lib/crypto: add HKDF-SHA{256,384,512}
fscrypt and NVMe authentication each carry private HKDF (RFC 5869) code. Add common HKDF-SHA{256,384,512} helpers based on fscrypt's implementation to lib/crypto, so both users can share the code.
Represent info as an array of segments processed as if concatenated. This avoids temporary concatenation buffers.
Implement the helpers using the direct HMAC library API, so they cannot fail, perform no allocations, and make no indirect calls. HKDF-Extract stores the PRK as a prepared HMAC key, so key preparation is not repeated for each HKDF-Expand call.
Use a template to instantiate the code for SHA-256, SHA-384, and SHA-512. Add the resulting objects to libsha256 and libsha512.
--- include/crypto/hkdf.h | 131 +++++++++++++++++++++++++++++++++++++ lib/crypto/Makefile | 4 +- lib/crypto/hkdf-sha256.c | 22 +++++++ lib/crypto/hkdf-sha512.c | 34 ++++++++++ lib/crypto/hkdf-template.h | 97 +++++++++++++++++++++++++++ 5 files changed, 286 insertions(+), 2 deletions(-) create mode 100644 include/crypto/hkdf.h create mode 100644 lib/crypto/hkdf-sha256.c create mode 100644 lib/crypto/hkdf-sha512.c create mode 100644 lib/crypto/hkdf-template.h
include/crypto/hkdf.h new file +131 -0
@@ -0,0 +1,131 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* HKDF: HMAC-based Key Derivation Function (RFC 5869) +* +* Derived from fs/crypto/hkdf.c +* Copyright 2019 Google LLC +*/ + +#ifndef _CRYPTO_HKDF_H +#define _CRYPTO_HKDF_H + +#include <crypto/sha2.h> + +/* +* HKDF has two steps: +* +* 1. Extract: derive a pseudorandom key from input keying material and salt. +* 2. Expand: derive output keying material from the pseudorandom key and info. +*/ + +/** +* struct hkdf_seg - one segment of the HKDF-Expand 'info' parameter +* @data: the segment data +* @len: length of @data in bytes +* +* The segments are processed as if concatenated, so a composite info can be +* passed without assembling it in a temporary buffer first. +*/ +struct hkdf_seg { +const void *data; +size_t len; +}; + +/** +* hkdf_sha256_extract() - HKDF-Extract using HMAC-SHA256 +* @prk: (output) the pseudorandom key, prepared for hkdf_sha256_expand() +* @salt: optional non-secret salt, or NULL for the RFC 5869 default +* (all-zero) salt +* @salt_len: length of @salt in bytes (must be 0 if @salt is NULL) +* @ikm: input keying material +* @ikm_len: length of @ikm in bytes +* +* Extract a SHA256_DIGEST_SIZE-byte pseudorandom key from @ikm. If @ikm is +* already a pseudorandom key of that size, prepare @prk with +* hmac_sha256_preparekey() instead. +*/ +void hkdf_sha256_extract (struct hmac_sha256_key *prk, +const u8 *salt,size_t salt_len, +const u8 *ikm,size_t ikm_len); + +/** +* hkdf_sha256_expand() - HKDF-Expand using HMAC-SHA256 +* @prk: pseudorandom key from hkdf_sha256_extract() or +* hmac_sha256_preparekey() +* @info: info segments, processed as if concatenated. Use different info for +* different purposes. +* @info_nsegs: number of segments in @info +* @okm: (output) the output keying material +* @okm_len: length of @okm in bytes, at most 255 * SHA256_DIGEST_SIZE +* +* Expand @prk into @okm_len bytes of output keying material (RFC 5869 +* section 2.3). The same (@prk, @info) always yields the same output. +* Different @info values yield independent outputs, but different @okm_len do +* not: a shorter output is a truncation of a longer one. This may be called many +* times per @prk and is thread-safe. +*/ +void hkdf_sha256_expand (const struct hmac_sha256_key *prk, +const struct hkdf_seg *info,size_t info_nsegs, +u8 *okm,size_t okm_len); + +/** +* hkdf_sha384_extract() - HKDF-Extract using HMAC-SHA384 +* @prk: (output) the pseudorandom key, prepared for hkdf_sha384_expand() +* @salt: optional non-secret salt, or NULL for the RFC 5869 default +* (all-zero) salt +* @salt_len: length of @salt in bytes (must be 0 if @salt is NULL) +* @ikm: input keying material +* @ikm_len: length of @ikm in bytes +* +* SHA-384 variant of hkdf_sha256_extract(). +*/ +void hkdf_sha384_extract (struct hmac_sha384_key *prk, +const u8 *salt,size_t salt_len, +const u8 *ikm,size_t ikm_len); + +/** +* hkdf_sha384_expand() - HKDF-Expand using HMAC-SHA384 +* @prk: pseudorandom key from hkdf_sha384_extract() or +* hmac_sha384_preparekey() +* @info: info segments, processed as if concatenated +* @info_nsegs: number of segments in @info +* @okm: (output) the output keying material +* @okm_len: length of @okm in bytes, at most 255 * SHA384_DIGEST_SIZE +* +* SHA-384 variant of hkdf_sha256_expand(). +*/ +void hkdf_sha384_expand (const struct hmac_sha384_key *prk, +const struct hkdf_seg *info,size_t info_nsegs, +u8 *okm,size_t okm_len); + +/** +* hkdf_sha512_extract() - HKDF-Extract using HMAC-SHA512 +* @prk: (output) the pseudorandom key, prepared for hkdf_sha512_expand() +* @salt: optional non-secret salt, or NULL for the RFC 5869 default +* (all-zero) salt +* @salt_len: length of @salt in bytes (must be 0 if @salt is NULL) +* @ikm: input keying material +* @ikm_len: length of @ikm in bytes +* +* SHA-512 variant of hkdf_sha256_extract(). +*/ +void hkdf_sha512_extract (struct hmac_sha512_key *prk, +const u8 *salt,size_t salt_len, +const u8 *ikm,size_t ikm_len); + +/** +* hkdf_sha512_expand() - HKDF-Expand using HMAC-SHA512 +* @prk: pseudorandom key from hkdf_sha512_extract() or +* hmac_sha512_preparekey() +* @info: info segments, processed as if concatenated +* @info_nsegs: number of segments in @info +* @okm: (output) the output keying material +* @okm_len: length of @okm in bytes, at most 255 * SHA512_DIGEST_SIZE +* +* SHA-512 variant of hkdf_sha256_expand(). +*/ +void hkdf_sha512_expand (const struct hmac_sha512_key *prk, +const struct hkdf_seg *info,size_t info_nsegs, +u8 *okm,size_t okm_len); + +#endif /* _CRYPTO_HKDF_H */
lib/crypto/Makefile +2 -2
@@ -300,7 +300,7 @@ endif # CONFIG_CRYPTO_LIB_SHA1_ARCH ################################################################################ obj-$(CONFIG_CRYPTO_LIB_SHA256) += libsha256.o -libsha256-y := sha256.o +libsha256-y := sha256.o hkdf-sha256.o ifeq ($(CONFIG_CRYPTO_LIB_SHA256_ARCH),y) CFLAGS_sha256.o += -I$(src)/$(SRCARCH)
@@ -329,7 +329,7 @@ endif # CONFIG_CRYPTO_LIB_SHA256_ARCH ################################################################################ obj-$(CONFIG_CRYPTO_LIB_SHA512) += libsha512.o -libsha512-y := sha512.o +libsha512-y := sha512.o hkdf-sha512.o ifeq ($(CONFIG_CRYPTO_LIB_SHA512_ARCH),y) CFLAGS_sha512.o += -I$(src)/$(SRCARCH)
lib/crypto/hkdf-sha256.c new file +22 -0
@@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* HKDF-SHA256 library functions (RFC 5869) +* +* Derived from fs/crypto/hkdf.c +* Copyright 2019 Google LLC +*/ + +#include <crypto/hkdf.h> +#include <linux/string.h> + +#define HKDF_EXTRACT hkdf_sha256_extract +#define HKDF_EXPAND hkdf_sha256_expand +#define HMAC_KEY hmac_sha256_key +#define HMAC_CTX hmac_sha256_ctx +#define HMAC_PREPAREKEY hmac_sha256_preparekey +#define HMAC_INIT hmac_sha256_init +#define HMAC_UPDATE hmac_sha256_update +#define HMAC_FINAL hmac_sha256_final +#define HMAC_USINGRAWKEY hmac_sha256_usingrawkey +#define HKDF_HASHLEN SHA256_DIGEST_SIZE +#include "hkdf-template.h"
lib/crypto/hkdf-sha512.c new file +34 -0
@@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* HKDF-SHA384 and HKDF-SHA512 library functions (RFC 5869) +* +* Derived from fs/crypto/hkdf.c +* Copyright 2019 Google LLC +*/ + +#include <crypto/hkdf.h> +#include <linux/string.h> + +#define HKDF_EXTRACT hkdf_sha384_extract +#define HKDF_EXPAND hkdf_sha384_expand +#define HMAC_KEY hmac_sha384_key +#define HMAC_CTX hmac_sha384_ctx +#define HMAC_PREPAREKEY hmac_sha384_preparekey +#define HMAC_INIT hmac_sha384_init +#define HMAC_UPDATE hmac_sha384_update +#define HMAC_FINAL hmac_sha384_final +#define HMAC_USINGRAWKEY hmac_sha384_usingrawkey +#define HKDF_HASHLEN SHA384_DIGEST_SIZE +#include "hkdf-template.h" + +#define HKDF_EXTRACT hkdf_sha512_extract +#define HKDF_EXPAND hkdf_sha512_expand +#define HMAC_KEY hmac_sha512_key +#define HMAC_CTX hmac_sha512_ctx +#define HMAC_PREPAREKEY hmac_sha512_preparekey +#define HMAC_INIT hmac_sha512_init +#define HMAC_UPDATE hmac_sha512_update +#define HMAC_FINAL hmac_sha512_final +#define HMAC_USINGRAWKEY hmac_sha512_usingrawkey +#define HKDF_HASHLEN SHA512_DIGEST_SIZE +#include "hkdf-template.h"
lib/crypto/hkdf-template.h new file +97 -0
97 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 2/5 lib/crypto: tests: add HKDF KUnit tests
In reply to Marco BaffoAdd KUnit tests for the HKDF-SHA{256,384,512} helpers using the RFC 5869 SHA-256 vectors and SHA-384 and SHA-512 vectors generated with "openssl kdf" from the same inputs. Also test empty salt and info, multi-block output, and segmented info.
--- lib/crypto/.kunitconfig | 1 + lib/crypto/tests/Kconfig | 8 + lib/crypto/tests/Makefile | 1 + lib/crypto/tests/hkdf_kunit.c | 276 ++++++++++++++++++++++++++++++++++ 4 files changed, 286 insertions(+) create mode 100644 lib/crypto/tests/hkdf_kunit.c
lib/crypto/.kunitconfig +1 -0
@@ -8,6 +8,7 @@ CONFIG_CRYPTO_LIB_BLAKE2S_KUNIT_TEST=y CONFIG_CRYPTO_LIB_CHACHA20POLY1305_KUNIT_TEST=y CONFIG_CRYPTO_LIB_CURVE25519_KUNIT_TEST=y CONFIG_CRYPTO_LIB_GHASH_KUNIT_TEST=y +CONFIG_CRYPTO_LIB_HKDF_KUNIT_TEST=y CONFIG_CRYPTO_LIB_MD5_KUNIT_TEST=y CONFIG_CRYPTO_LIB_MLDSA_KUNIT_TEST=y CONFIG_CRYPTO_LIB_NH_KUNIT_TEST=y
lib/crypto/tests/Kconfig +8 -0
@@ -52,6 +52,14 @@ config CRYPTO_LIB_GHASH_KUNIT_TEST help KUnit tests for the GHASH library functions. +config CRYPTO_LIB_HKDF_KUNIT_TEST + tristate "KUnit tests for HKDF" if !KUNIT_ALL_TESTS + depends on KUNIT && CRYPTO_LIB_SHA256 && CRYPTO_LIB_SHA512 + default KUNIT_ALL_TESTS + help + KUnit tests for the HKDF-SHA256, HKDF-SHA384, and HKDF-SHA512 key + derivation functions. + config CRYPTO_LIB_MD5_KUNIT_TEST tristate "KUnit tests for MD5" if !KUNIT_ALL_TESTS depends on KUNIT && CRYPTO_LIB_MD5
lib/crypto/tests/Makefile +1 -0
@@ -6,6 +6,7 @@ obj-$(CONFIG_CRYPTO_LIB_BLAKE2S_KUNIT_TEST) += blake2s_kunit.o obj-$(CONFIG_CRYPTO_LIB_CHACHA20POLY1305_KUNIT_TEST) += chacha20poly1305_kunit.o obj-$(CONFIG_CRYPTO_LIB_CURVE25519_KUNIT_TEST) += curve25519_kunit.o obj-$(CONFIG_CRYPTO_LIB_GHASH_KUNIT_TEST) += ghash_kunit.o +obj-$(CONFIG_CRYPTO_LIB_HKDF_KUNIT_TEST) += hkdf_kunit.o obj-$(CONFIG_CRYPTO_LIB_MD5_KUNIT_TEST) += md5_kunit.o obj-$(CONFIG_CRYPTO_LIB_MLDSA_KUNIT_TEST) += mldsa_kunit.o obj-$(CONFIG_CRYPTO_LIB_NH_KUNIT_TEST) += nh_kunit.o
lib/crypto/tests/hkdf_kunit.c new file +276 -0
276 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 3/5 fscrypt: use HKDF library functions
In reply to Marco BaffoConvert fscrypt to the common HKDF-SHA512 helpers without changing its derived keys. The info bytes are processed in the same order as before. An empty salt is equivalent to the previous 64-byte all-zero salt after HMAC key padding.
--- fs/crypto/hkdf.c | 56 +++++++++++------------------------------------- 1 file changed, 13 insertions(+), 43 deletions(-)
fs/crypto/hkdf.c +13 -43
@@ -1,8 +1,6 @@// SPDX-License-Identifier: GPL-2.0 /* -* Implementation of HKDF ("HMAC-based Extract-and-Expand Key Derivation -* Function"), aka RFC 5869. See also the original paper (Krawczyk 2010): -* "Cryptographic Extraction and Key Derivation: The HKDF Scheme". +* Key derivation using HKDF-SHA512. * * This is used to derive keys from the fscrypt master keys (or from the * "software secrets" which hardware derives from the fscrypt master keys, in
@@ -11,6 +9,8 @@* Copyright 2019 Google LLC */ +#include <crypto/hkdf.h> +#include "fscrypt_private.h" /*
@@ -24,19 +24,11 @@* HKDF-SHA512 being much faster than HKDF-SHA256, as the longer digest size of * SHA-512 causes HKDF-Expand to only need to do one iteration rather than two. */ -#define HKDF_HASHLEN SHA512_DIGEST_SIZE/* -* HKDF consists of two steps: -* -* 1. HKDF-Extract: extract a pseudorandom key of length HKDF_HASHLEN bytes from -* the input keying material and optional salt. -* 2. HKDF-Expand: expand the pseudorandom key into output keying material of -* any length, parameterized by an application-specific info string. -* * HKDF-Extract can be skipped if the input is already a pseudorandom key of -* length HKDF_HASHLEN bytes. However, cipher modes other than AES-256-XTS take -* shorter keys, and we don't want to force users of those modes to provide +* length SHA512_DIGEST_SIZE bytes. However, cipher modes other than AES-256-XTS +* take shorter keys, and we don't want to force users of those modes to provide * unnecessarily long master keys. Thus fscrypt still does HKDF-Extract. No * salt is used, since fscrypt master keys should already be pseudorandom and * there's no way to persist a random salt per master key from kernel mode.
@@ -50,13 +42,7 @@void fscrypt_init_hkdf (struct hmac_sha512_key *hkdf,const u8 *master_key,unsigned int master_key_size) { -static const u8 default_salt[HKDF_HASHLEN ]; -u8 prk[HKDF_HASHLEN ]; - -hmac_sha512_usingrawkey (default_salt,sizeof (default_salt), - master_key, master_key_size, prk); -hmac_sha512_preparekey (hkdf, prk,sizeof (prk)); -memzero_explicit (prk,sizeof (prk)); +hkdf_sha512_extract (hkdf,NULL ,0 , master_key, master_key_size); }/*
@@ -73,28 +59,12 @@ void fscrypt_hkdf_expand(const struct hmac_sha512_key *hkdf, u8 context, const u8 *info, unsigned int infolen, u8 *okm, unsigned int okmlen) { - struct hmac_sha512_ctx ctx; -u8 counter =1 ; -u8 tmp[HKDF_HASHLEN ]; - -WARN_ON_ONCE (okmlen >255 *HKDF_HASHLEN ); + const struct hkdf_seg info_segs[] = { + { .data ="fscrypt\0" , .len =8 }, + { .data = &context, .len =1 }, + { .data = info, .len = infolen }, + }; -for (unsigned int i =0 ; i < okmlen; i +=HKDF_HASHLEN ) { -hmac_sha512_init (&ctx, hkdf); -if (i !=0 ) -hmac_sha512_update (&ctx, &okm[i -HKDF_HASHLEN ], -HKDF_HASHLEN ); -hmac_sha512_update (&ctx,"fscrypt\0" ,8 ); -hmac_sha512_update (&ctx, &context,1 ); -hmac_sha512_update (&ctx, info, infolen); -hmac_sha512_update (&ctx, &counter,1 ); -if (okmlen - i <HKDF_HASHLEN ) { -hmac_sha512_final (&ctx, tmp); -memcpy (&okm[i], tmp, okmlen - i); -memzero_explicit (tmp,sizeof (tmp)); - }else { -hmac_sha512_final (&ctx, &okm[i]); - } - counter++; - } +hkdf_sha512_expand (hkdf, info_segs,ARRAY_SIZE (info_segs), + okm, okmlen); }
Signature · 1 line
2.43.0
PATCH 4/5 nvme-auth: use HKDF library functions for TLS PSK derivation
In reply to Marco BaffoNVMe currently implements HKDF locally in nvme_auth_derive_tls_psk() and allocates a temporary buffer for the HKDF-Expand input. Use the common SHA-256 and SHA-384 HKDF helpers instead.
Pass the HkdfLabel fields as separate info segments, eliminating the temporary allocation and its failure path.
The derived keys are unchanged. The segments are processed as if concatenated, the expand counter byte is appended by the helper, and the empty salt is equivalent to the previous digest-sized all-zero salt after HMAC key padding.
--- drivers/nvme/common/auth.c | 120 +++++++++++++++++++++---------------- 1 file changed, 68 insertions(+), 52 deletions(-)
drivers/nvme/common/auth.c +68 -52
@@ -10,6 +10,7 @@#include <linux/scatterlist.h> #include <linux/unaligned.h> #include <crypto/dh.h> +#include <crypto/hkdf.h> #include <crypto/sha2.h> #include <linux/nvme.h> #include <linux/nvme-auth.h>
@@ -691,14 +692,19 @@ EXPORT_SYMBOL_GPL(nvme_auth_generate_digest);int nvme_auth_derive_tls_psk (int hmac_id,const u8 *psk,size_t psk_len,const char *psk_digest,u8 **ret_psk) { -static const u8 default_salt[NVME_AUTH_MAX_DIGEST_SIZE ];static const char label[] ="tls13 nvme-tls-psk" ; -const size_t label_len =sizeof (label) -1 ; -u8 prk[NVME_AUTH_MAX_DIGEST_SIZE ]; -size_t hash_len, ctx_len; -u8 *hmac_data =NULL , *tls_key; -size_t i; -int ret; +const u8 label_len =sizeof (label) -1 ; +union { +struct hmac_sha256_key sha256; +struct hmac_sha384_key sha384; + } prk; +struct hkdf_seg info[6 ]; +size_t hash_len; +u8 output_len[2 ]; +u8 ctx_len; +char ctx_prefix[4 ]; +size_t digest_len; +u8 *tls_key; hash_len =nvme_auth_hmac_hash_len (hmac_id);if (hash_len ==0 ) {
@@ -717,64 +723,74 @@ int nvme_auth_derive_tls_psk(int hmac_id, const u8 *psk, size_t psk_len,return -EINVAL ; } -/* HKDF-Extract */ - ret =nvme_auth_hmac (hmac_id, default_salt, hash_len, psk, psk_len, - prk); -if (ret) - goto out; -/* -* HKDF-Expand-Label (RFC 8446 section 7.1), with output length equal to -* the hash length (so only a single HMAC operation is needed) +* HKDF-Expand-Label (RFC 8446 section 7.1). The HKDF-Expand info +* parameter is an encoded HkdfLabel containing the output length, +* label, and context. */ - hmac_data =kmalloc (/* output length */ 2 + -/* label */ 1 + label_len + -/* context (max) */ 1 +3 +1 +strlen (psk_digest) + -/* counter */ 1 , -GFP_KERNEL ); -if (!hmac_data) { - ret = -ENOMEM ; - goto out; - }/* output length */ - i =0 ; - hmac_data[i++] = hash_len >>8 ; - hmac_data[i++] = hash_len; + output_len[0 ] = hash_len >>8 ; + output_len[1 ] = hash_len; + info[0 ] = (struct hkdf_seg ){ + .data = output_len, + .len =sizeof (output_len), + };/* label */ -static_assert (label_len <=255 ); - hmac_data[i] = label_len; -memcpy (&hmac_data[i +1 ], label, label_len); - i +=1 + label_len; +static_assert (sizeof (label) -1 <=255 ); + info[1 ] = (struct hkdf_seg ){ + .data = &label_len, + .len =sizeof (label_len), + }; + info[2 ] = (struct hkdf_seg ){ + .data = label, + .len = label_len, + };/* context */ - ctx_len =sprintf (&hmac_data[i +1 ],"%02d %s" , hmac_id, psk_digest); -if (ctx_len >255 ) { - ret = -EINVAL ; - goto out; - } - hmac_data[i] = ctx_len; - i +=1 + ctx_len; + digest_len =strlen (psk_digest); +if (digest_len >255 - (sizeof (ctx_prefix) -1 )) +return -EINVAL ; -/* counter (this overwrites the NUL terminator written by sprintf) */ - hmac_data[i++] =1 ; +snprintf (ctx_prefix,sizeof (ctx_prefix),"%02d " , hmac_id); + ctx_len =sizeof (ctx_prefix) -1 + digest_len; + info[3 ] = (struct hkdf_seg ){ + .data = &ctx_len, + .len =sizeof (ctx_len), + }; + info[4 ] = (struct hkdf_seg ){ + .data = ctx_prefix, + .len =sizeof (ctx_prefix) -1 , + }; + info[5 ] = (struct hkdf_seg ){ + .data = psk_digest, + .len = digest_len, + }; + + tls_key =kmalloc (psk_len,GFP_KERNEL ); +if (!tls_key) +return -ENOMEM ; - tls_key =kzalloc (psk_len,GFP_KERNEL ); -if (!tls_key) { - ret = -ENOMEM ; - goto out; - } - ret =nvme_auth_hmac (hmac_id, prk, hash_len, hmac_data, i, tls_key); -if (ret) { +switch (hmac_id) { +case NVME_AUTH_HASH_SHA256 : +hkdf_sha256_extract (&prk.sha256,NULL ,0 , psk, psk_len); +hkdf_sha256_expand (&prk.sha256, info,ARRAY_SIZE (info), + tls_key, psk_len); +break ; +case NVME_AUTH_HASH_SHA384 : +hkdf_sha384_extract (&prk.sha384,NULL ,0 , psk, psk_len); +hkdf_sha384_expand (&prk.sha384, info,ARRAY_SIZE (info), + tls_key, psk_len); +break ; +default : +WARN_ON_ONCE (1 );kfree_sensitive (tls_key); - goto out; +return -EINVAL ; } +memzero_explicit (&prk,sizeof (prk)); *ret_psk = tls_key; -out: -kfree_sensitive (hmac_data); -memzero_explicit (prk,sizeof (prk)); -return ret; +return 0 ; }EXPORT_SYMBOL_GPL (nvme_auth_derive_tls_psk);
Signature · 1 line
2.43.0
RFC PATCH 5/5 ovpn: use HKDF library functions for epoch key derivation
In reply to Marco Baffoovpn carries a local shash-based HKDF-Expand implementation for deriving epoch PRKs, data keys, and implicit IVs. Replace it with hkdf_sha256_expand() and feed the HkdfLabel bytes as an array of segments processed as if concatenated. The derived keys are unchanged.
Store PRKs as prepared HMAC-SHA256 keys embedded in struct ovpn_epoch_key instead of allocated shash transforms. ovpn_epoch_set_prk() now handles initial setup as well as epoch updates via hmac_sha256_preparekey(), replacing ovpn_epoch_init_key(). This removes the shash allocation and setkey failure paths. The embedded keys are wiped in place where the transforms were previously freed. The labels are compile-time constants, so check their encoded sizes at build time and remove the corresponding runtime error paths.
Select CRYPTO_LIB_SHA256 instead of CRYPTO_HMAC and CRYPTO_SHA256, as the HKDF code was ovpn's only shash user.
--- drivers/net/Kconfig | 3 +- drivers/net/ovpn/crypto_epoch.c | 168 ++++++++------------------------ drivers/net/ovpn/crypto_epoch.h | 11 +-- drivers/net/ovpn/crypto_key.c | 50 +++------- 4 files changed, 60 insertions(+), 172 deletions(-)
drivers/net/Kconfig +1 -2
@@ -109,8 +109,7 @@ config OVPN select CRYPTO_AES select CRYPTO_GCM select CRYPTO_CHACHA20POLY1305 - select CRYPTO_HMAC - select CRYPTO_SHA256 + select CRYPTO_LIB_SHA256 select STREAM_PARSER help This module enhances the performance of the OpenVPN userspace software
drivers/net/ovpn/crypto_epoch.c +41 -127
214 lines not on this page · raw message
drivers/net/ovpn/crypto_epoch.h +5 -6
@@ -10,7 +10,7 @@#ifndef _NET_OVPN_OVPNEPOCH_H_#define _NET_OVPN_OVPNEPOCH_H_ -#include <crypto/hash.h> +#include <crypto/sha2.h> #include <linux/limits.h> #include <linux/rcupdate.h> #include <linux/types.h>
@@ -21,11 +21,11 @@struct ovpn_key_ctx ; -/* crypto handle used for key derivation through HKDF-Expand-Label */ +/* key derivation state for HKDF-Expand-Label */ struct ovpn_epoch_key {u16 epoch;unsigned int cipher_key_len; -struct crypto_shash *shash; +struct hmac_sha256_key prk; };/* ring buffer of prederived future epoch data keys */
@@ -46,11 +46,10 @@ ovpn_epoch_future_keys_count(const struct ovpn_future_keys *fk)OVPN_EPOCH_FUTURE_KEYS_COUNT ; } -struct crypto_shash *ovpn_epoch_init_key (const u8 *key,size_t key_size);int ovpn_epoch_derive_next_prk (const struct ovpn_epoch_key *epoch_key,u8 next_prk[]); -int ovpn_epoch_set_prk (struct ovpn_epoch_key *epoch_key,const u8 prk[], -u16 epoch); +void ovpn_epoch_set_prk (struct ovpn_epoch_key *epoch_key,const u8 prk[], +u16 epoch);int ovpn_epoch_iterate (struct ovpn_epoch_key *epoch_key);int ovpn_epoch_derive_key (const struct ovpn_epoch_key *epoch_key,u8 cipher_key[],u8 implicit_iv[]);
drivers/net/ovpn/crypto_key.c +13 -37
104 lines not on this page · raw message
Signature · 1 line
2.43.0
Eric Biggers
maintainerHi Marco,
Quoted 50 lines · Marco Baffo
fscrypt and NVMe authentication each carry private HKDF implementations. Add HKDF-SHA256, HKDF-SHA384, and HKDF-SHA512 functions to lib/crypto and convert both users.
Patches 1-4 are intended for review and inclusion.
Patch 5 is RFC only and is not intended to be applied. It shows how the OVPN epoch-key code currently under development can use the new HKDF API. That patch has an additional dependency on the OVPN epoch series. It is included here to allow the API to be reviewed against a third user. The OVPN conversion will be submitted separately once its prerequisite code is ready.
HKDF-Expand accepts info as an array of segments and processes them as if concatenated. This preserves the byte ordering used by fscrypt and allows NVMe to encode HkdfLabel without allocating a temporary buffer.
HKDF-Extract produces a prepared HMAC key, avoiding repeated key setup when the same PRK is expanded more than once. The implementation uses the direct HMAC library functions and neither allocates memory nor returns errors.
The series is intended for the libcrypto tree. Patches 3 and 4 need review from the fscrypt and NVMe maintainers, respectively.
Tested with:
- HKDF KUnit tests
- NVMe authentication KUnit tests
- kvm-xfstests smoketest on ext4
- kvm-xfstests generic/582, generic/583, and generic/584 on ext4 and f2fs
- kvm-xfstests encrypt group on ext4 and f2fsThe following shortlog and diffstat refers only to the first 4 patches.
Marco Baffo (4):
lib/crypto: add HKDF-SHA{256,384,512}
lib/crypto: tests: add HKDF KUnit tests
fscrypt: use HKDF library functions
nvme-auth: use HKDF library functions for TLS PSK derivationdrivers/nvme/common/auth.c | 120 ++++++++------- fs/crypto/hkdf.c | 56 ++----- include/crypto/hkdf.h | 131 ++++++++++++++++ lib/crypto/.kunitconfig | 1 + lib/crypto/Makefile | 4 +- lib/crypto/hkdf-sha256.c | 22 +++ lib/crypto/hkdf-sha512.c | 34 +++++ lib/crypto/hkdf-template.h | 97 ++++++++++++ lib/crypto/tests/Kconfig | 8 +
Quoted 8 lines
lib/crypto/tests/Makefile | 1 + lib/crypto/tests/hkdf_kunit.c | 276 ++++++++++++++++++++++++++++++++++ 11 files changed, 653 insertions(+), 97 deletions(-) create mode 100644 include/crypto/hkdf.h create mode 100644 lib/crypto/hkdf-sha256.c create mode 100644 lib/crypto/hkdf-sha512.c create mode 100644 lib/crypto/hkdf-template.h create mode 100644 lib/crypto/tests/hkdf_kunit.c
If we're going to reintroduce an HKDF library, this is probably the way to do it (no crypto_shash dependency).
But I think the positive diffstat makes having an HKDF library at all in the kernel still kind of questionable. HKDF is just a wrapper around HMAC and it can "unroll" to as little as 1-2 HMAC calls. It's not really like the other crypto algorithms where the implementations can be very complex and we often have multiple arch-specific implementations.
In drivers/nvme/common/auth.c, using the library actually ended up as more code than not using it.
And in fs/crypto/hkdf.c this patchset only saves a bit (with some of the savings coming from deleting helpful comments).
In the ovpn case, the extraction step isn't actually used. Nor are multiple expansion iterations needed, since all the keys needing to be derived are at most the hash size. So vs. using hmac_sha256_* directly it saves maybe 10-15 lines? (Note that the diffstat in patch 5 isn't directly relevant, as the starting point was crypto_shash.)
So then we end up with quite a bit of additional code in lib/crypto/, to support users where the diffstat is basically neutral.
And this is only HKDF, so this hardly solves key derivation in general. Plenty of KDFs don't use HKDF at all, either ones based on block ciphers or stream ciphers, or newer hash functions that aren't vulnerable to length extension attacks so don't require the HMAC layer. There are lots of other KDFs implemented in the kernel in various places. Just to pick a couple random examples, extract_entropy() in drivers/char/random.c is doing a KDF, tcp_ao_calc_traffic_key() in net/ipv4/tcp_ao.c is doing a KDF. Do those need to be libraries too?
So overall I would suggest it might be best to hold off on this for now and just call the hmac_sha256_* functions directly from ovpn_expand_label(). (And again, only one iteration is actually needed there, so it doesn't even need to be the full HKDF-Expand.)
- Eric
Thanks for the detailed explanation, and sorry for the slow reply.
I see your point now, and I agree that adding a generic HKDF implementation probably would not buy us much here. I can still see some value in having a shared, well-tested implementation, but I agree it’s probably not worth it in this case.
Thanks again for taking the time to review this.
Quoted 104 lines · CEST Eric Biggers
Hi Marco,
Quoted 50 lines · Marco Baffo
fscrypt and NVMe authentication each carry private HKDF implementations. Add HKDF-SHA256, HKDF-SHA384, and HKDF-SHA512 functions to lib/crypto and convert both users.
Patches 1-4 are intended for review and inclusion.
Patch 5 is RFC only and is not intended to be applied. It shows how the OVPN epoch-key code currently under development can use the new HKDF API. That patch has an additional dependency on the OVPN epoch series. It is included here to allow the API to be reviewed against a third user. The OVPN conversion will be submitted separately once its prerequisite code is ready.
HKDF-Expand accepts info as an array of segments and processes them as if concatenated. This preserves the byte ordering used by fscrypt and allows NVMe to encode HkdfLabel without allocating a temporary buffer.
HKDF-Extract produces a prepared HMAC key, avoiding repeated key setup when the same PRK is expanded more than once. The implementation uses the direct HMAC library functions and neither allocates memory nor returns errors.
The series is intended for the libcrypto tree. Patches 3 and 4 need review from the fscrypt and NVMe maintainers, respectively.
Tested with:
- HKDF KUnit tests
- NVMe authentication KUnit tests
- kvm-xfstests smoketest on ext4
- kvm-xfstests generic/582, generic/583, and generic/584 on ext4 and f2fs
- kvm-xfstests encrypt group on ext4 and f2fsThe following shortlog and diffstat refers only to the first 4 patches.
Marco Baffo (4):
lib/crypto: add HKDF-SHA{256,384,512}
lib/crypto: tests: add HKDF KUnit tests
fscrypt: use HKDF library functions
nvme-auth: use HKDF library functions for TLS PSK derivationdrivers/nvme/common/auth.c | 120 ++++++++------- fs/crypto/hkdf.c | 56 ++----- include/crypto/hkdf.h | 131 ++++++++++++++++ lib/crypto/.kunitconfig | 1 + lib/crypto/Makefile | 4 +- lib/crypto/hkdf-sha256.c | 22 +++ lib/crypto/hkdf-sha512.c | 34 +++++ lib/crypto/hkdf-template.h | 97 ++++++++++++ lib/crypto/tests/Kconfig | 8 +Quoted 8 lines
lib/crypto/tests/Makefile | 1 + lib/crypto/tests/hkdf_kunit.c | 276 ++++++++++++++++++++++++++++++++++ 11 files changed, 653 insertions(+), 97 deletions(-) create mode 100644 include/crypto/hkdf.h create mode 100644 lib/crypto/hkdf-sha256.c create mode 100644 lib/crypto/hkdf-sha512.c create mode 100644 lib/crypto/hkdf-template.h create mode 100644 lib/crypto/tests/hkdf_kunit.cIf we're going to reintroduce an HKDF library, this is probably the way to do it (no
crypto_shashdependency).But I think the positive diffstat makes having an HKDF library at all in the kernel still kind of questionable. HKDF is just a wrapper around HMAC and it can "unroll" to as little as 1-2 HMAC calls. It's not really like the other crypto algorithms where the implementations can be very complex and we often have multiple arch-specific implementations.
In
drivers/nvme/common/auth.c, using the library actually ended up as more code than not using it.And in
fs/crypto/hkdf.cthis patchset only saves a bit (with some of the savings coming from deleting helpful comments).In the ovpn case, the extraction step isn't actually used. Nor are multiple expansion iterations needed, since all the keys needing to be derived are at most the hash size. So vs. using hmac_sha256_* directly it saves maybe 10-15 lines? (Note that the diffstat in patch 5 isn't directly relevant, as the starting point was
crypto_shash.)So then we end up with quite a bit of additional code in
lib/crypto/, to support users where the diffstat is basically neutral.And this is only HKDF, so this hardly solves key derivation in general. Plenty of KDFs don't use HKDF at all, either ones based on block ciphers or stream ciphers, or newer hash functions that aren't vulnerable to length extension attacks so don't require the HMAC layer. There are lots of other KDFs implemented in the kernel in various places. Just to pick a couple random examples,
extract_entropy()indrivers/char/random.cis doing a KDF,tcp_ao_calc_traffic_key()innet/ipv4/tcp_ao.cis doing a KDF. Do those need to be libraries too?So overall I would suggest it might be best to hold off on this for now and just call the hmac_sha256_* functions directly from
ovpn_expand_label(). (And again, only one iteration is actually needed there, so it doesn't even need to be the full HKDF-Expand.)- Eric