Add SP800-108 KDF implementation to crypto API
Subsystems (3)
v4 cover Add SP800-108 KDF implementation to crypto API
Hi,
The key derviation functions are considered to be a cryptographic operation. As cryptographic operations are provided via the kernel crypto API, this patch set consolidates the SP800-108 KDF implementation into the crypto API.
If this patch is accepted, another patch set will be published attempting to move the HKDF implementation from the crypto file system code base to the kernel crypto API.
The KDF implementation is provided as service functions. Yet, the interface to the the provided KDF is modeled such, that additional KDF implementation can use the same API style. The goal is to allow the transformation from a service function into a crypto API template eventually.
The KDF executes a power-on self test with test vectors from commonly known sources.
Tbe SP800-108 KDF implementation is used to replace the implementation in the keys subsystem. The implementation was verified using the keyutils command line test code provided in tests/keyctl/dh_compute/valid. All tests show that the expected values are calculated with the new code.
Changes v3 to v4:
* SP800-108 KDF kernel configuration parameter is not user selectable as suggested by Eric Biggers
* update the error code path for the self test handling to mirror testmgr.c as suggested by Eric Biggers
* further cleanup in kdf_alloc as suggested by Mat Martineau
Changes v2 to v3:
* port to kernel 5.16-rc1
* remove the HKDF patch to only leave the SP800-108 patch
Stephan Mueller (4):
crypto: Add key derivation self-test support code
crypto: add SP800-108 counter key derivation function
security: DH - remove dead code for zero padding
security: DH - use KDF implementation from crypto API
crypto/Kconfig | 4 + crypto/Makefile | 5 + crypto/kdf_sp800108.c | 153 +++++++++++++++++++++++++ include/crypto/internal/kdf_selftest.h | 71 ++++++++++++ include/crypto/kdf_sp800108.h | 61 ++++++++++ security/keys/Kconfig | 2 +- security/keys/dh.c | 130 ++++----------------- 7 files changed, 315 insertions(+), 111 deletions(-) create mode 100644 crypto/kdf_sp800108.c create mode 100644 include/crypto/internal/kdf_selftest.h create mode 100644 include/crypto/kdf_sp800108.h
Signature · 1 line
2.33.1
PATCH v4 1/4 crypto: Add key derivation self-test support code
As a preparation to add the key derivation implementations, the self-test data structure definition and the common test code is made available.
The test framework follows the testing applied by the NIST CAVP test approach.
The structure of the test code follows the implementations found in crypto/testmgr.c|h. In case the KDF implementations will be made available via a kernel crypto API templates, the test code is intended to be merged into testmgr.c|h.
--- include/crypto/internal/kdf_selftest.h | 71 ++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 include/crypto/internal/kdf_selftest.h
include/crypto/internal/kdf_selftest.h new file +71 -0
@@ -0,0 +1,71 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Copyright (C) 2021, Stephan Mueller <[email protected]> +*/ + +#ifndef _CRYPTO_KDF_SELFTEST_H +#define _CRYPTO_KDF_SELFTEST_H + +#include <crypto/hash.h> +#include <linux/uio.h> + +struct kdf_testvec { +unsigned char *key; +size_t keylen; +unsigned char *ikm; +size_t ikmlen; +struct kvec info; +unsigned char *expected; +size_t expectedlen; +}; + +static inline int +kdf_test (const struct kdf_testvec *test,const char *name, +int (*crypto_kdf_setkey)(struct crypto_shash *kmd, +const u8 *key,size_t keylen, +const u8 *ikm,size_t ikmlen), +int (*crypto_kdf_generate)(struct crypto_shash *kmd, +const struct kvec *info, +unsigned int info_nvec, +u8 *dst,unsigned int dlen)) +{ +struct crypto_shash *kmd; +int ret; +u8 *buf =kzalloc (test->expectedlen,GFP_KERNEL ); + +if (!buf) +return -ENOMEM ; + + kmd =crypto_alloc_shash (name,0 ,0 ); +if (IS_ERR (kmd)) { +pr_err ("alg: kdf: could not allocate hash handle for %s\n" , + name); +kfree (buf); +return -ENOMEM ; + } + + ret =crypto_kdf_setkey (kmd, test->key, test->keylen, + test->ikm, test->ikmlen); +if (ret) { +pr_err ("alg: kdf: could not set key derivation key\n" ); + goto err; + } + + ret =crypto_kdf_generate (kmd, &test->info,1 , buf, test->expectedlen); +if (ret) { +pr_err ("alg: kdf: could not obtain key data\n" ); + goto err; + } + + ret =memcmp (test->expected, buf, test->expectedlen); +if (ret) + ret = -EINVAL ; + +err: +crypto_free_shash (kmd); +kfree (buf); +return ret; +} + +#endif /* _CRYPTO_KDF_SELFTEST_H */
Signature · 1 line
2.33.1
PATCH v4 2/4 crypto: add SP800-108 counter key derivation function
In reply to Stephan MüllerSP800-108 defines three KDFs - this patch provides the counter KDF implementation.
The KDF is implemented as a service function where the caller has to maintain the hash / HMAC state. Apart from this hash/HMAC state, no additional state is required to be maintained by either the caller or the KDF implementation.
The key for the KDF is set with the crypto_kdf108_setkey function which is intended to be invoked before the caller requests a key derivation operation via crypto_kdf108_ctr_generate.
SP800-108 allows the use of either a HMAC or a hash as crypto primitive for the KDF. When a HMAC primtive is intended to be used, crypto_kdf108_setkey must be used to set the HMAC key. Otherwise, for a hash crypto primitve crypto_kdf108_ctr_generate can be used immediately after allocating the hash handle.
--- crypto/Kconfig | 4 + crypto/Makefile | 5 ++ crypto/kdf_sp800108.c | 153 ++++++++++++++++++++++++++++++++++ include/crypto/kdf_sp800108.h | 61 ++++++++++++++ 4 files changed, 223 insertions(+) create mode 100644 crypto/kdf_sp800108.c create mode 100644 include/crypto/kdf_sp800108.h
crypto/Kconfig +4 -0
@@ -1845,6 +1845,10 @@ config CRYPTO_JITTERENTROPY random numbers. This Jitterentropy RNG registers with the kernel crypto API and can be used by any caller. +config CRYPTO_KDF800108_CTR + tristate + select CRYPTO_HASH + config CRYPTO_USER_API tristate
crypto/Makefile +5 -0
@@ -200,3 +200,8 @@ obj-$(CONFIG_ASYMMETRIC_KEY_TYPE) += asymmetric_keys/ obj-$(CONFIG_CRYPTO_HASH_INFO) += hash_info.o crypto_simd-y := simd.o obj-$(CONFIG_CRYPTO_SIMD) += crypto_simd.o + +# +# Key derivation function +# +obj-$(CONFIG_CRYPTO_KDF800108_CTR) += kdf_sp800108.o
crypto/kdf_sp800108.c new file +153 -0
153 lines not on this page · raw message
include/crypto/kdf_sp800108.h new file +61 -0
61 lines not on this page · raw message
Signature · 1 line
2.33.1
PATCH v4 3/4 security: DH - remove dead code for zero padding
In reply to Stephan MüllerRemove the specific code that adds a zero padding that was intended to be invoked when the DH operation result was smaller than the modulus. However, this cannot occur any more these days because the function mpi_write_to_sgl is used in the code path that calculates the shared secret in dh_compute_value. This MPI service function guarantees that leading zeros are introduced as needed to ensure the resulting data is exactly as long as the modulus. This implies that the specific code to add zero padding is dead code which can be safely removed.
--- security/keys/dh.c | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-)
security/keys/dh.c +4 -21
@@ -141,7 +141,7 @@ static void kdf_dealloc(struct kdf_sdesc *sdesc)* 'dlen' must be a multiple of the digest size. */ static int kdf_ctr (struct kdf_sdesc *sdesc,const u8 *src,unsigned int slen, -u8 *dst,unsigned int dlen,unsigned int zlen) +u8 *dst,unsigned int dlen) {struct shash_desc *desc = &sdesc->shash;unsigned int h =crypto_shash_digestsize (desc->tfm);
@@ -158,22 +158,6 @@ static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen,if (err) goto err; -if (zlen && h) { -u8 tmpbuffer[32 ]; -size_t chunk =min_t (size_t, zlen,sizeof (tmpbuffer)); -memset (tmpbuffer,0 , chunk); - -do { - err =crypto_shash_update (desc, tmpbuffer, - chunk); -if (err) - goto err; - - zlen -= chunk; - chunk =min_t (size_t, zlen,sizeof (tmpbuffer)); - }while (zlen); - } -if (src && slen) { err =crypto_shash_update (desc, src, slen);if (err)
@@ -198,7 +182,7 @@ static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen,static int keyctl_dh_compute_kdf (struct kdf_sdesc *sdesc,char __user *buffer,size_t buflen, -uint8_t *kbuf,size_t kbuflen,size_t lzero) +uint8_t *kbuf,size_t kbuflen) {uint8_t *outbuf =NULL ;int ret;
@@ -211,7 +195,7 @@ static int keyctl_dh_compute_kdf(struct kdf_sdesc *sdesc, goto err; } - ret =kdf_ctr (sdesc, kbuf, kbuflen, outbuf, outbuf_len, lzero); + ret =kdf_ctr (sdesc, kbuf, kbuflen, outbuf, outbuf_len);if (ret) goto err;
@@ -384,8 +368,7 @@ long __keyctl_dh_compute(struct keyctl_dh_params __user *params, } ret =keyctl_dh_compute_kdf (sdesc, buffer, buflen, outbuf, - req->dst_len + kdfcopy->otherinfolen, - outlen - req->dst_len); + req->dst_len + kdfcopy->otherinfolen); }else if (copy_to_user (buffer, outbuf, req->dst_len) ==0 ) { ret = req->dst_len; }else {
Signature · 1 line
2.33.1
PATCH v4 4/4 security: DH - use KDF implementation from crypto API
In reply to Stephan MüllerThe kernel crypto API provides the SP800-108 counter KDF implementation. Thus, the separate implementation provided as part of the keys subsystem can be replaced with calls to the KDF offered by the kernel crypto API.
The keys subsystem uses the counter KDF with a hash primitive. Thus, it only uses the call to crypto_kdf108_ctr_generate.
--- security/keys/Kconfig | 2 +- security/keys/dh.c | 109 +++++++----------------------------------- 2 files changed, 19 insertions(+), 92 deletions(-)
security/keys/Kconfig +1 -1
@@ -109,7 +109,7 @@ config KEY_DH_OPERATIONS bool "Diffie-Hellman operations on retained keys" depends on KEYS select CRYPTO - select CRYPTO_HASH + select CRYPTO_KDF800108_CTR select CRYPTO_DH help This option provides support for calculating Diffie-Hellman
security/keys/dh.c +18 -91
172 lines not on this page · raw message
Signature · 1 line
2.33.1
Re: [PATCH v4 4/4] security: DH - use KDF implementation from crypto API
Quoted 4 lines · Stephan Müller
The kernel crypto API provides the SP800-108 counter KDF implementation. Thus, the separate implementation provided as part of the keys subsystem can be replaced with calls to the KDF offered by the kernel crypto API.
Quoted 8 lines
The keys subsystem uses the counter KDF with a hash primitive. Thus, it only uses the call to
crypto_kdf108_ctr_generate.Signed-off-by Stephan Mueller <[email protected]>---
security/keys/Kconfig| 2 +-security/keys/dh.c| 109 +++++++-----------------------------------2 files changed, 19 insertions(+), 92 deletions(-)
--
Mat Martineau
Intel
Herbert Xu
maintainerQuoted 44 lines · Stephan Müller
Hi,
The key derviation functions are considered to be a cryptographic operation. As cryptographic operations are provided via the kernel crypto API, this patch set consolidates the SP800-108 KDF implementation into the crypto API.
If this patch is accepted, another patch set will be published attempting to move the HKDF implementation from the crypto file system code base to the kernel crypto API.
The KDF implementation is provided as service functions. Yet, the interface to the the provided KDF is modeled such, that additional KDF implementation can use the same API style. The goal is to allow the transformation from a service function into a crypto API template eventually.
The KDF executes a power-on self test with test vectors from commonly known sources.
Tbe SP800-108 KDF implementation is used to replace the implementation in the keys subsystem. The implementation was verified using the keyutils command line test code provided in tests/keyctl/dh_compute/valid. All tests show that the expected values are calculated with the new code.
Changes v3 to v4:
* SP800-108 KDF kernel configuration parameter is not user selectable as suggested by Eric Biggers
* update the error code path for the self test handling to mirrortestmgr.cas suggested by Eric Biggers
* further cleanup inkdf_allocas suggested by Mat MartineauChanges v2 to v3:
* port to kernel 5.16-rc1
* remove the HKDF patch to only leave the SP800-108 patchStephan Mueller (4):
crypto: Add key derivation self-test support code
crypto: add SP800-108 counter key derivation function
security: DH - remove dead code for zero padding
security: DH - use KDF implementation from crypto API
Quoted 11 lines
crypto/Kconfig | 4 + crypto/Makefile | 5 + crypto/kdf_sp800108.c | 153 +++++++++++++++++++++++++ include/crypto/internal/kdf_selftest.h | 71 ++++++++++++ include/crypto/kdf_sp800108.h | 61 ++++++++++ security/keys/Kconfig | 2 +- security/keys/dh.c | 130 ++++----------------- 7 files changed, 315 insertions(+), 111 deletions(-) create mode 100644 crypto/kdf_sp800108.c create mode 100644 include/crypto/internal/kdf_selftest.h create mode 100644 include/crypto/kdf_sp800108.h
All applied. Thanks.
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