KEYS: add SP800-56A KDF support for DH
Subsystems (6)
PATCH v2 1/1 KEYS: add SP800-56A KDF support for DH
Hi Mat,
the following code addresses all your comments as discussed.
In addition, this code is now complete: it contains an extension to the documentation as well as the compat code. The compat code was tested successfully with compiling the user space tool with -m32 after applying the patch from David ensuring the invocation of the keyctl compat syscall.
Ciao
Stephan
---8<----
SP800-56A defines the use of DH with key derivation function based on a counter. The input to the KDF is defined as (DH shared secret || other information). The value for the "other information" is to be provided by the caller.
The KDF is provided by the kernel crypto API. The SP800-56A KDF is equal to the SP800-108 counter KDF. However, the caller is allowed to specify the KDF type that he wants to use to derive the key material allowing the use of the other KDFs provided with the kernel crypto API.
As the KDF implements the proper truncation of the DH shared secret to the requested size, this patch fills the caller buffer up to its size.
The patch is tested with a new test added to the keyutils user space code which uses a CAVS test vector testing the compliance with SP800-56A.
--- Documentation/security/keys.txt | 35 +++++++++--- include/linux/compat.h | 7 +++ include/uapi/linux/keyctl.h | 7 +++ security/keys/Kconfig | 1 + security/keys/compat.c | 34 +++++++++++- security/keys/dh.c | 119 ++++++++++++++++++++++++++++++++++++---- security/keys/internal.h | 9 ++- security/keys/keyctl.c | 2 +- 8 files changed, 189 insertions(+), 25 deletions(-)
Documentation/security/keys.txt +27 -8
@@ -827,7 +827,7 @@ The keyctl syscall functions are: long keyctl(KEYCTL_DH_COMPUTE, struct keyctl_dh_params *params, char *buffer, size_t buflen, - void *reserved); + struct keyctl_kdf_params *kdf); The params struct contains serial numbers for three keys:
@@ -844,18 +844,37 @@ The keyctl syscall functions are: public key. If the base is the remote public key, the result is the shared secret. - The reserved argument must be set to NULL. + If the parameter kdf is NULL, the following applies: - The buffer length must be at least the length of the prime, or zero. + - The buffer length must be at least the length of the prime, or zero. - If the buffer length is nonzero, the length of the result is - returned when it is successfully calculated and copied in to the - buffer. When the buffer length is zero, the minimum required - buffer length is returned. + - If the buffer length is nonzero, the length of the result is + returned when it is successfully calculated and copied in to the + buffer. When the buffer length is zero, the minimum required + buffer length is returned. + + The kdf parameter allows the caller to apply a key derivation function + (KDF) on the Diffie-Hellman computation where only the result + of the KDF is returned to the caller. The KDF is characterized with + struct keyctl_kdf_params as follows: + + - char *kdfname specifies the NULL terminated string identifying + the KDF function used from the kernel crypto API. As of now, + only non-keyed KDFs are supported, such as kdf_ctr(sha256), + kdf_fb(sha1) or kdf_dpi(sha512). The use of kdf_ctr() complies + with SP800-56A. + + - char *otherinfo specifies the OtherInfo string as documented in + SP800-56A section 5.8.1.2. The length of the buffer is given with + otherinfolen. The format of OtherInfo is defined by the caller. + The otherinfo pointer may be NULL if no OtherInfo shall be used. This function will return error EOPNOTSUPP if the key type is not supported, error ENOKEY if the key could not be found, or error - EACCES if the key is not readable by the caller. + EACCES if the key is not readable by the caller. In addition, the + function will return EMSGSIZE when the parameter kdf is non-NULL + and either the buffer length or the OtherInfo length exceeds the + allowed length. =============== KERNEL SERVICES
include/linux/compat.h +7 -0
@@ -295,6 +295,13 @@ struct compat_old_sigaction { };#endif +struct compat_keyctl_kdf_params { +compat_uptr_t kdfname; +compat_uptr_t otherinfo; +__u32 otherinfolen; +__u32 __spare[8 ]; +}; +struct compat_statfs ;struct compat_statfs64 ;struct compat_old_linux_dirent ;
include/uapi/linux/keyctl.h +7 -0
@@ -68,4 +68,11 @@ struct keyctl_dh_params { __s32 base; }; +struct keyctl_kdf_params { +char *kdfname; +char *otherinfo; +__u32 otherinfolen; +__u32 __spare[8 ]; +}; +#endif /* _LINUX_KEYCTL_H */
security/keys/Kconfig +1 -0
@@ -90,6 +90,7 @@ config KEY_DH_OPERATIONS bool "Diffie-Hellman operations on retained keys" depends on KEYS select MPILIB + select CRYPTO_KDF help This option provides support for calculating Diffie-Hellman public keys and shared secrets using values stored as keys
security/keys/compat.c +32 -2
@@ -48,6 +48,35 @@ static long compat_keyctl_instantiate_key_iov(return ret; } +#ifdef CONFIG_KEY_DH_OPERATIONS +static long compat_keyctl_dh_compute (struct keyctl_dh_params __user *params, +char __user *buffer,size_t buflen, +struct compat_keyctl_kdf_params __user *kdf) +{ +struct keyctl_kdf_params kdfcopy; +struct compat_keyctl_kdf_params compat_kdfcopy; + +if (!kdf) +return __keyctl_dh_compute (params, buffer, buflen,NULL ); + +if (copy_from_user (&compat_kdfcopy, kdf,sizeof (compat_kdfcopy)) !=0 ) +return -EFAULT ; + + kdfcopy.kdfname =compat_ptr (compat_kdfcopy.kdfname); + kdfcopy.otherinfo =compat_ptr (compat_kdfcopy.otherinfo); + kdfcopy.otherinfolen = compat_kdfcopy.otherinfolen; + +return __keyctl_dh_compute (params, buffer, buflen, &kdfcopy); +} +#else +static long compat_keyctl_dh_compute (struct keyctl_dh_params __user *params, +char __user *buffer,size_t buflen, +struct keyctl_kdf_params __user *kdf) +{ + return -EOPNOTSUPP +} +#endif +/* * The key control system call, 32-bit compatibility version for 64-bit archs *
@@ -133,8 +162,9 @@ COMPAT_SYSCALL_DEFINE5(keyctl, u32, option,return keyctl_get_persistent (arg2, arg3);case KEYCTL_DH_COMPUTE : -return keyctl_dh_compute (compat_ptr (arg2),compat_ptr (arg3), - arg4,compat_ptr (arg5)); +return compat_keyctl_dh_compute (compat_ptr (arg2), +compat_ptr (arg3), + arg4,compat_ptr (arg5));default :return -EOPNOTSUPP ;
security/keys/dh.c +107 -12
163 lines not on this page · raw message
security/keys/internal.h +7 -2
@@ -259,12 +259,17 @@ static inline long keyctl_get_persistent(uid_t uid, key_serial_t destring)#endif #ifdef CONFIG_KEY_DH_OPERATIONS +#include <crypto/rng.h> extern long keyctl_dh_compute (struct keyctl_dh_params __user *,char __user *, -size_t ,void __user *); +size_t ,struct keyctl_kdf_params __user *); +extern long __keyctl_dh_compute (struct keyctl_dh_params __user *,char __user *, +size_t ,struct keyctl_kdf_params *); +#define KEYCTL_KDF_MAX_OUTPUTLEN 1024/* max length of KDF output */ +#define KEYCTL_KDF_MAX_STRING_LEN 64/* maximum length of strings */ #else static inline long keyctl_dh_compute (struct keyctl_dh_params __user *params,char __user *buffer,size_t buflen, -void __user *reserved) +struct keyctl_kdf_params __user *kdf) {return -EOPNOTSUPP ; }
security/keys/keyctl.c +1 -1
@@ -1689,7 +1689,7 @@ SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,case KEYCTL_DH_COMPUTE :return keyctl_dh_compute ((struct keyctl_dh_params __user *) arg2, (char __user *) arg3, (size_t ) arg4, - (void __user *) arg5); + (struct keyctl_kdf_params __user *) arg5);default :return -EOPNOTSUPP ;
Signature · 1 line
2.7.4
v4 cover crypto: Key Derivation Function (SP800-108)
Hi,
this patch set implements all three key derivation functions defined in SP800-108.
The implementation is provided as a template for random number generators, since a KDF can be considered a form of deterministic RNG where the key material is used as a seed.
With the KDF implemented as a template, all types of keyed and non-keyed hashes can be utilized, including HMAC and CMAC. The testmgr tests are derived from publicly available test vectors from NIST.
The KDF are all tested with a complete round of CAVS testing on 32 and 64 bit.
The patch set introduces an extension to the kernel crypto API in the first patch by adding a template handling for random number generators based on the same logic as for keyed hashes.
Changes v4:
* removal of the check that src and dst buffers are not the same from the KDF implementations as requested by Herbert
* implement and use new free API in the RNG instance handling as requested by Herbert
* move the instance handling code from include/crypto/rng.h to include/crypto/internal/rng.h
Changes v3:
* port testmgr patch to current cryptodev-2.6 tree
* add non-keyed KDF references to testmgr.c
Changes v2:
* port to 4.7-rc1
Stephan Mueller (4):
crypto: add template handling for RNGs
crypto: kdf - add known answer tests
crypto: kdf - SP800-108 Key Derivation Function
crypto: kdf - enable compilation
crypto/Kconfig | 7 + crypto/Makefile | 1 + crypto/kdf.c | 508 ++++++++++++++++++++++++++++++++++++++++++ crypto/rng.c | 32 +++ crypto/testmgr.c | 226 +++++++++++++++++++ crypto/testmgr.h | 110 +++++++++ include/crypto/internal/rng.h | 38 ++++ 7 files changed, 922 insertions(+) create mode 100644 crypto/kdf.c
Signature · 1 line
2.7.4
PATCH v4 1/4 crypto: add template handling for RNGs
This patch adds the ability to register templates for RNGs. RNGs are "meta" mechanisms using raw cipher primitives. Thus, RNGs can now be implemented as templates to allow the complete flexibility the kernel crypto API provides.
--- crypto/rng.c | 32 ++++++++++++++++++++++++++++++++ include/crypto/internal/rng.h | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+)
crypto/rng.c +32 -0
@@ -63,6 +63,13 @@ static int crypto_rng_init_tfm(struct crypto_tfm *tfm)return 0 ; } +static void crypto_rng_free_instance (struct crypto_instance *inst) +{ +struct rng_instance *rng =rng_instance (inst); + + rng->free (rng); +} +static unsigned int seedsize (struct crypto_alg *alg) { struct rng_alg *ralg = container_of(alg, struct rng_alg, base);
@@ -105,6 +112,7 @@ static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)static const struct crypto_type crypto_rng_type = { .extsize = crypto_alg_extsize, .init_tfm = crypto_rng_init_tfm, + .free = crypto_rng_free_instance, #ifdef CONFIG_PROC_FS .show = crypto_rng_show, #endif
@@ -232,5 +240,29 @@ void crypto_unregister_rngs(struct rng_alg *algs, int count) }EXPORT_SYMBOL_GPL (crypto_unregister_rngs); +static int rng_prepare_alg (struct rng_alg *alg) +{ +struct crypto_alg *base = &alg->base; + + base->cra_type = &crypto_rng_type; + base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK ; + base->cra_flags |=CRYPTO_ALG_TYPE_RNG ; + +return 0 ; +} + +int rng_register_instance (struct crypto_template *tmpl, +struct rng_instance *inst) +{ +int err; + + err =rng_prepare_alg (&inst->alg); +if (err) +return err; + +return crypto_register_instance (tmpl,rng_crypto_instance (inst)); +} +EXPORT_SYMBOL_GPL (rng_register_instance); +MODULE_LICENSE ("GPL" );MODULE_DESCRIPTION ("Random Number Generator" );
include/crypto/internal/rng.h +38 -0
@@ -42,4 +42,42 @@ static inline void crypto_rng_set_entropy(struct crypto_rng *tfm,crypto_rng_alg (tfm)->set_ent (tfm, data, len); } +struct rng_instance { +void (*free)(struct rng_instance *inst); +struct rng_alg alg; +}; + +static inline struct rng_instance *rng_alloc_instance ( +const char *name,struct crypto_alg *alg) +{ +return crypto_alloc_instance2 (name, alg, +sizeof (struct rng_instance ) -sizeof (*alg)); +} + +static inline struct crypto_instance *rng_crypto_instance ( +struct rng_instance *inst) +{ + return container_of(&inst->alg.base, struct crypto_instance, alg); +} + +static inline void *rng_instance_ctx (struct rng_instance *inst) +{ +return crypto_instance_ctx (rng_crypto_instance (inst)); +} + +static inline struct rng_alg *__crypto_rng_alg (struct crypto_alg *alg) +{ + return container_of(alg, struct rng_alg, base); +} + +static inline struct rng_instance *rng_instance ( +struct crypto_instance *inst) +{ +return container_of (__crypto_rng_alg (&inst->alg), + struct rng_instance, alg); +} + +int rng_register_instance (struct crypto_template *tmpl, +struct rng_instance *inst); +#endif
Signature · 1 line
2.7.4
Herbert Xu
maintainerRe: [PATCH v4 1/4] crypto: add template handling for RNGs
include/crypto/internal/rng.h static inline void crypto_rng_set_entropy(struct crypto_rng *tfm, · line 78
Quoted 12 lines · Stephan Mueller
include/crypto/internal/rng.h +12
-0+ +static inline struct rng_alg *__crypto_rng_alg (struct crypto_alg *alg) +{ + return container_of(alg, struct rng_alg, base); +} + +static inline struct rng_instance *rng_instance ( +struct crypto_instance *inst) +{ +return container_of (__crypto_rng_alg (&inst->alg), + struct rng_instance, alg); +}
Please move these functions into rng.c.
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
PATCH v4 2/4 crypto: kdf - add known answer tests
In reply to Stephan MuellerAdd known answer tests to the testmgr for the KDF (SP800-108) cipher.
--- crypto/testmgr.c | 226 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ crypto/testmgr.h | 110 +++++++++++++++++++++++++++ 2 files changed, 336 insertions(+)
crypto/testmgr.c +226 -0
@@ -116,6 +116,11 @@ struct drbg_test_suite {unsigned int count; }; +struct kdf_test_suite { +struct kdf_testvec *vecs; +unsigned int count; +}; +struct akcipher_test_suite {struct akcipher_testvec *vecs;unsigned int count;
@@ -139,6 +144,7 @@ struct alg_test_desc {struct hash_test_suite hash;struct cprng_test_suite cprng;struct drbg_test_suite drbg; +struct kdf_test_suite kdf;struct akcipher_test_suite akcipher;struct kpp_test_suite kpp; } suite;
@@ -1758,6 +1764,64 @@ outbuf:return ret; } +static int kdf_cavs_test (struct kdf_testvec *test, +const char *driver,u32 type,u32 mask) +{ +int ret = -EAGAIN ; +struct crypto_rng *drng; +unsigned char *buf =kzalloc (test->expectedlen,GFP_KERNEL ); + +if (!buf) +return -ENOMEM ; + + drng =crypto_alloc_rng (driver, type |CRYPTO_ALG_INTERNAL , mask); +if (IS_ERR (drng)) { +printk (KERN_ERR "alg: kdf: could not allocate cipher handle " +"for %s\n" , driver); +kzfree (buf); +return -ENOMEM ; + } + + ret =crypto_rng_reset (drng, test->K1, test->K1len); +if (ret) { +printk (KERN_ERR "alg: kdf: could not set key derivation key\n" ); + goto err; + } + + ret =crypto_rng_generate (drng, test->context, test->contextlen, + buf, test->expectedlen); +if (ret) { +printk (KERN_ERR "alg: kdf: could not obtain key data\n" ); + goto err; + } + + ret =memcmp (test->expected, buf, test->expectedlen); + +err: +crypto_free_rng (drng); +kzfree (buf); +return ret; +} + +static int alg_test_kdf (const struct alg_test_desc *desc,const char *driver, +u32 type,u32 mask) +{ +int err =0 ; +unsigned int i =0 ; +struct kdf_testvec *template = desc->suite.kdf.vecs; +unsigned int tcount = desc->suite.kdf.count; + +for (i =0 ; i < tcount; i++) { + err =kdf_cavs_test (&template[i], driver, type, mask); +if (err) { +printk (KERN_ERR "alg: kdf: Test %d failed for %s\n" , + i, driver); + err = -EINVAL ; +break ; + } + } +return err; +}static int alg_test_drbg (const struct alg_test_desc *desc,const char *driver,u32 type,u32 mask)
@@ -3467,6 +3531,168 @@ static const struct alg_test_desc alg_test_descs[] = { .fips_allowed = 1, .test = alg_test_null, }, { + .alg = "kdf_ctr(cmac(aes))", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_ctr(cmac(des3_ede))", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_ctr(hmac(sha1))", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_ctr(hmac(sha224))", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_ctr(hmac(sha256))", + .test = alg_test_kdf, + .fips_allowed = 1, + .suite = { + .kdf = { + .vecs = kdf_ctr_hmac_sha256_tv_template, + .count = ARRAY_SIZE(kdf_ctr_hmac_sha256_tv_template) + } + } + }, { + .alg = "kdf_ctr(hmac(sha384))", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_ctr(hmac(sha512))", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_ctr(sha1)", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_ctr(sha224)", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_ctr(sha256)", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_ctr(sha384)", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_ctr(sha512)", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_dpi(cmac(aes))", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_dpi(cmac(des3_ede))", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_dpi(hmac(sha1))", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_dpi(hmac(sha224))", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_dpi(hmac(sha256))", + .test = alg_test_kdf, + .fips_allowed = 1, + .suite = { + .kdf = { + .vecs = kdf_dpi_hmac_sha256_tv_template, + .count = ARRAY_SIZE(kdf_dpi_hmac_sha256_tv_template) + } + } + }, { + .alg = "kdf_dpi(hmac(sha384))", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_dpi(hmac(sha512))", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_dpi(sha1)", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_dpi(sha224)", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_dpi(sha256)", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_dpi(sha384)", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_dpi(sha512)", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_fb(cmac(aes))", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_fb(cmac(des3_ede))", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_fb(hmac(sha1))", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_fb(hmac(sha224))", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_fb(hmac(sha256))", + .test = alg_test_kdf, + .fips_allowed = 1, + .suite = { + .kdf = { + .vecs = kdf_fb_hmac_sha256_tv_template, + .count = ARRAY_SIZE(kdf_fb_hmac_sha256_tv_template) + } + } + }, { + .alg = "kdf_fb(hmac(sha384))", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_fb(hmac(sha512))", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_fb(sha1)", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_fb(sha224)", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_fb(sha256)", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_fb(sha384)", + .test = alg_test_null, + .fips_allowed = 1, + }, { + .alg = "kdf_fb(sha512)", + .test = alg_test_null, + .fips_allowed = 1, + }, { .alg = "kw(aes)", .test = alg_test_skcipher, .fips_allowed = 1,
crypto/testmgr.h +110 -0
122 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v4 3/4 crypto: kdf - SP800-108 Key Derivation Function
In reply to Stephan MuellerThe SP800-108 compliant Key Derivation Function is implemented as a random number generator considering that it behaves like a deterministic RNG.
All three KDF types specified in SP800-108 are implemented.
The code comments provide details about how to invoke the different KDF types.
--- crypto/kdf.c | 508 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 508 insertions(+) create mode 100644 crypto/kdf.c
crypto/kdf.c new file +508 -0
@@ -0,0 +1,508 @@ +/* +* Copyright (C) 2016, Stephan Mueller <[email protected]> +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* 1. Redistributions of source code must retain the above copyright +* notice, and the entire permission notice in its entirety, +* including the disclaimer of warranties. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. The name of the author may not be used to endorse or promote +* products derived from this software without specific prior +* written permission. +* +* ALTERNATIVELY, this product may be distributed under the terms of +* the GNU General Public License, in which case the provisions of the GPL2 +* are required INSTEAD OF the above restrictions. (This clause is +* necessary due to a potential bad interaction between the GPL and +* the restrictions contained in a BSD-style copyright.) +* +* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF +* WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE +* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT +* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE +* USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH +* DAMAGE. +*/ + +/* +* For performing a KDF operation, the following input is required +* from the caller: +* +* * Keying material to be used to derive the new keys from +* (denoted as Ko in SP800-108) +* * Label -- a free form binary string +* * Context -- a free form binary string +* +* The KDF is implemented as a random number generator. +* +* The Ko keying material is to be provided with the initialization of the KDF +* "random number generator", i.e. with the crypto_rng_reset function. +* +* The Label and Context concatenated string is provided when obtaining random +* numbers, i.e. with the crypto_rng_generate function. The caller must format +* the free-form Label || Context input as deemed necessary for the given +* purpose. Note, SP800-108 mandates that the Label and Context are separated +* by a 0x00 byte, i.e. the caller shall provide the input as +* Label || 0x00 || Context when trying to be compliant to SP800-108. For +* the feedback KDF, an IV is required as documented below. +* +* Example without proper error handling: +* char *keying_material = "\x00\x11\x22\x33\x44\x55\x66\x77"; +* char *label_context = "\xde\xad\xbe\xef\x00\xde\xad\xbe\xef"; +* kdf = crypto_alloc_rng(name, 0, 0); +* crypto_rng_reset(kdf, keying_material, 8); +* crypto_rng_generate(kdf, label_context, 9, outbuf, outbuflen); +* +* NOTE: In-place cipher operations are not supported. +*/ + +#include <linux/module.h> +#include <crypto/rng.h> +#include <crypto/internal/rng.h> +#include <crypto/hash.h> +#include <crypto/internal/hash.h> + +struct crypto_kdf_ctx { +struct shash_desc shash; +char ctx[]; +}; + +/* convert 32 bit integer into its string representation */ +static inline void crypto_kw_cpu_to_be32 (u32 val,u8 *buf) +{ +__be32 *a = (__be32 *)buf; + + *a =cpu_to_be32 (val); +} + +/* +* Implementation of the KDF in double pipeline iteration mode according with +* counter to SP800-108 section 5.3. +* +* The caller must provide Label || 0x00 || Context in src. This src pointer +* may also be NULL if the caller wishes not to provide anything. +*/ +static int crypto_kdf_dpi_random (struct crypto_rng *rng, +const u8 *src,unsigned int slen, +u8 *dst,unsigned int dlen) +{ +struct crypto_kdf_ctx *ctx =crypto_tfm_ctx (crypto_rng_tfm (rng)); +struct shash_desc *desc = &ctx->shash; +unsigned int h =crypto_shash_digestsize (desc->tfm); +unsigned int alignmask =crypto_shash_alignmask (desc->tfm); +int err =0 ; +u8 *dst_orig = dst; +u8 Aiblock[h + alignmask]; +u8 *Ai =PTR_ALIGN ((u8 *)Aiblock, alignmask +1 ); +u32 i =1 ; +u8 iteration[sizeof (u32)]; + +memset (Ai,0 , h); + +while (dlen) { +/* Calculate A(i) */ +if (dst == dst_orig && src && slen) +/* 5.3 step 4 and 5.a */ + err =crypto_shash_digest (desc, src, slen, Ai); +else +/* 5.3 step 5.a */ + err =crypto_shash_digest (desc, Ai, h, Ai); +if (err) + goto err; + +/* Calculate K(i) -- step 5.b */ + err =crypto_shash_init (desc); +if (err) + goto err; + + err =crypto_shash_update (desc, Ai, h); +if (err) + goto err; + +crypto_kw_cpu_to_be32 (i, iteration); + err =crypto_shash_update (desc, iteration,sizeof (u32)); +if (err) + goto err; +if (src && slen) { + err =crypto_shash_update (desc, src, slen); +if (err) + goto err; + } + +if (dlen < h) { +u8 tmpbuffer[h]; + + err =crypto_shash_final (desc, tmpbuffer); +if (err) + goto err; +memcpy (dst, tmpbuffer, dlen); +memzero_explicit (tmpbuffer, h); + goto ret; + }else { + err =crypto_shash_final (desc, dst); +if (err) + goto err; + dlen -= h; + dst += h; + i++; + } + } + +err: +memzero_explicit (dst_orig, dlen); +ret: +memzero_explicit (Ai, h); +return err; +} + +/* +* Implementation of the KDF in feedback mode with a non-NULL IV and with +* counter according to SP800-108 section 5.2. The IV is supplied with src +* and must be equal to the digestsize of the used cipher. +* +* In addition, the caller must provide Label || 0x00 || Context in src. This +* src pointer must not be NULL as the IV is required. The ultimate format of +* the src pointer is IV || Label || 0x00 || Context where the length of the +* IV is equal to the output size of the PRF. +*/ +static int crypto_kdf_fb_random (struct crypto_rng *rng, +const u8 *src,unsigned int slen, +u8 *dst,unsigned int dlen) +{ +struct crypto_kdf_ctx *ctx =crypto_tfm_ctx (crypto_rng_tfm (rng)); +struct shash_desc *desc = &ctx->shash; +unsigned int h =crypto_shash_digestsize (desc->tfm); +int err =0 ; +u8 *dst_orig = dst; +const u8 *label; +unsigned int labellen =0 ; +u32 i =1 ; +u8 iteration[sizeof (u32)]; + +/* require the presence of an IV */ +if (!src || slen < h) +return -EINVAL ; + +/* calculate the offset of the label / context data */ + label = src + h; + labellen = slen - h; + +while (dlen) { + err =crypto_shash_init (desc); +if (err) + goto err; + +/* +* Feedback mode applies to all rounds except first which uses +* the IV. +*/ +if (dst_orig == dst) + err =crypto_shash_update (desc, src, h); +else + err =crypto_shash_update (desc, dst - h, h); +if (err) + goto err; + +crypto_kw_cpu_to_be32 (i, iteration); + err =crypto_shash_update (desc, iteration,sizeof (u32)); +if (err) + goto err; +if (labellen) { + err =crypto_shash_update (desc, label, labellen); +if (err) + goto err; + } + +if (dlen < h) { +u8 tmpbuffer[h]; + + err =crypto_shash_final (desc, tmpbuffer); +if (err) + goto err; +memcpy (dst, tmpbuffer, dlen); +memzero_explicit (tmpbuffer, h); +return 0 ; + }else { + err =crypto_shash_final (desc, dst); +if (err) + goto err; + dlen -= h; + dst += h; + i++; + } + } + +return 0 ; + +err: +memzero_explicit (dst_orig, dlen); +return err; +} + +/* +* Implementation of the KDF in counter mode according to SP800-108 section 5.1 +* as well as SP800-56A section 5.8.1 (Single-step KDF). +* +* SP800-108: +* The caller must provide Label || 0x00 || Context in src. This src pointer +* may also be NULL if the caller wishes not to provide anything. +* +* SP800-56A: +* The key provided for the HMAC during the crypto_rng_reset shall NOT be the +* shared secret from the DH operation, but an independently generated key. +* The src pointer is defined as Z || other info where Z is the shared secret +* from DH and other info is an arbitrary string (see SP800-56A section +* 5.8.1.2). +*/ +static int crypto_kdf_ctr_random (struct crypto_rng *rng, +const u8 *src,unsigned int slen, +u8 *dst,unsigned int dlen) +{ +struct crypto_kdf_ctx *ctx =crypto_tfm_ctx (crypto_rng_tfm (rng)); +struct shash_desc *desc = &ctx->shash; +unsigned int h =crypto_shash_digestsize (desc->tfm); +int err =0 ; +u8 *dst_orig = dst; +u32 i =1 ; +u8 iteration[sizeof (u32)]; + +while (dlen) { + err =crypto_shash_init (desc); +if (err) + goto err; + +crypto_kw_cpu_to_be32 (i, iteration); + err =crypto_shash_update (desc, iteration,sizeof (u32)); +if (err) + goto err; + +if (src && slen) { + err =crypto_shash_update (desc, src, slen); +if (err) + goto err; + } + +if (dlen < h) { +u8 tmpbuffer[h]; + + err =crypto_shash_final (desc, tmpbuffer); +if (err) + goto err; +memcpy (dst, tmpbuffer, dlen); +memzero_explicit (tmpbuffer, h); +return 0 ; + }else { + err =crypto_shash_final (desc, dst); +if (err) + goto err; + + dlen -= h; + dst += h; + i++; + } + } + +return 0 ; + +err: +memzero_explicit (dst_orig, dlen); +return err; +} + +/* +* The seeding of the KDF allows to set a key which must be at least +* digestsize long. +*/ +static int crypto_kdf_seed (struct crypto_rng *rng, +const u8 *seed,unsigned int slen) +{ +struct crypto_kdf_ctx *ctx =crypto_tfm_ctx (crypto_rng_tfm (rng)); +unsigned int ds =crypto_shash_digestsize (ctx->shash.tfm); + +/* Check according to SP800-108 section 7.2 */ +if (ds > slen) +return -EINVAL ; + +/* +* We require that we operate on a MAC -- if we do not operate on a +* MAC, this function returns an error. +*/ +return crypto_shash_setkey (ctx->shash.tfm, seed, slen); +} + +static int crypto_kdf_init_tfm (struct crypto_tfm *tfm) +{ +struct crypto_instance *inst =crypto_tfm_alg_instance (tfm); +struct crypto_shash_spawn *spawn =crypto_instance_ctx (inst); +struct crypto_kdf_ctx *ctx =crypto_tfm_ctx (tfm); +struct crypto_shash *hash; + + hash =crypto_spawn_shash (spawn); +if (IS_ERR (hash)) +return PTR_ERR (hash); + + ctx->shash.tfm = hash; + +return 0 ; +} + +static void crypto_kdf_exit_tfm (struct crypto_tfm *tfm) +{ +struct crypto_kdf_ctx *ctx =crypto_tfm_ctx (tfm); + +crypto_free_shash (ctx->shash.tfm); +} + +static void crypto_kdf_free (struct rng_instance *inst) +{ +crypto_drop_spawn (rng_instance_ctx (inst)); +kfree (inst); +} + +static int crypto_kdf_alloc_common (struct crypto_template *tmpl, +struct rtattr **tb, +const u8 *name, +int (*generate)(struct crypto_rng *tfm, +const u8 *src, +unsigned int slen, +u8 *dst,unsigned int dlen)) +{ +struct rng_instance *inst; +struct crypto_alg *alg; +struct shash_alg *salg; +int err; +unsigned int ds, ss; + + err =crypto_check_attr_type (tb,CRYPTO_ALG_TYPE_RNG ); +if (err) +return err; + + salg =shash_attr_alg (tb[1 ],0 ,0 ); +if (IS_ERR (salg)) +return PTR_ERR (salg); + + ds = salg->digestsize; + ss = salg->statesize; + alg = &salg->base; + + inst =rng_alloc_instance (name, alg); + err =PTR_ERR (inst); +if (IS_ERR (inst)) + goto out_put_alg; + + err =crypto_init_shash_spawn (rng_instance_ctx (inst), salg, +rng_crypto_instance (inst)); +if (err) + goto out_free_inst; + + inst->alg.base.cra_priority = alg->cra_priority; + inst->alg.base.cra_blocksize = alg->cra_blocksize; + inst->alg.base.cra_alignmask = alg->cra_alignmask; + + inst->alg.generate = generate; + inst->alg.seed = crypto_kdf_seed; + inst->alg.seedsize = ds; + + inst->alg.base.cra_init = crypto_kdf_init_tfm; + inst->alg.base.cra_exit = crypto_kdf_exit_tfm; + inst->alg.base.cra_ctxsize =ALIGN (sizeof (struct crypto_kdf_ctx ) + + ss *2 ,crypto_tfm_ctx_alignment ()); + + inst->free = crypto_kdf_free; + + err =rng_register_instance (tmpl, inst); + +if (err) { +out_free_inst: +crypto_kdf_free (inst); + } + +out_put_alg: +crypto_mod_put (alg); +return err; +} + +static int crypto_kdf_ctr_create (struct crypto_template *tmpl, +struct rtattr **tb) +{ +return crypto_kdf_alloc_common (tmpl, tb,"kdf_ctr" , + crypto_kdf_ctr_random); +} + +static struct crypto_template crypto_kdf_ctr_tmpl = { + .name ="kdf_ctr" , + .create = crypto_kdf_ctr_create, + .module =THIS_MODULE , +}; + +static int crypto_kdf_fb_create (struct crypto_template *tmpl, +struct rtattr **tb) { +return crypto_kdf_alloc_common (tmpl, tb,"kdf_fb" , + crypto_kdf_fb_random); +} + +static struct crypto_template crypto_kdf_fb_tmpl = { + .name ="kdf_fb" , + .create = crypto_kdf_fb_create, + .module =THIS_MODULE , +}; + +static int crypto_kdf_dpi_create (struct crypto_template *tmpl, +struct rtattr **tb) { +return crypto_kdf_alloc_common (tmpl, tb,"kdf_dpi" , + crypto_kdf_dpi_random); +} + +static struct crypto_template crypto_kdf_dpi_tmpl = { + .name ="kdf_dpi" , + .create = crypto_kdf_dpi_create, + .module =THIS_MODULE , +}; + +static int __init crypto_kdf_init (void ) +{ +int err =crypto_register_template (&crypto_kdf_ctr_tmpl); + +if (err) +return err; + + err =crypto_register_template (&crypto_kdf_fb_tmpl); +if (err) { +crypto_unregister_template (&crypto_kdf_ctr_tmpl); +return err; + } + + err =crypto_register_template (&crypto_kdf_dpi_tmpl); +if (err) { +crypto_unregister_template (&crypto_kdf_ctr_tmpl); +crypto_unregister_template (&crypto_kdf_fb_tmpl); + } +return err; +} + +static void __exit crypto_kdf_exit(void) +{ +crypto_unregister_template (&crypto_kdf_ctr_tmpl); +crypto_unregister_template (&crypto_kdf_fb_tmpl); +crypto_unregister_template (&crypto_kdf_dpi_tmpl); +} + +module_init (crypto_kdf_init); +module_exit (crypto_kdf_exit); + +MODULE_LICENSE ("Dual BSD/GPL" ); +MODULE_AUTHOR ("Stephan Mueller <[email protected]>" ); +MODULE_DESCRIPTION ("Key Derivation Function according to SP800-108 and SP800-56A" ); +MODULE_ALIAS_CRYPTO ("kdf_ctr" ); +MODULE_ALIAS_CRYPTO ("kdf_fb" ); +MODULE_ALIAS_CRYPTO ("kdf_dpi" );
Signature · 1 line
2.7.4
PATCH v4 4/4 crypto: kdf - enable compilation
In reply to Stephan MuellerInclude KDF into Kconfig and Makefile for compilation.
--- crypto/Kconfig | 7 +++++++ crypto/Makefile | 1 + 2 files changed, 8 insertions(+)
crypto/Kconfig +7 -0
@@ -372,6 +372,13 @@ config CRYPTO_KEYWRAP Support for key wrapping (NIST SP800-38F / RFC3394) without padding. +config CRYPTO_KDF + tristate "Key Derivation Function (SP800-108)" + select CRYPTO_RNG + help + Support for KDF compliant to SP800-108. All three types of + KDF specified in SP800-108 are implemented. + comment "Hash modes" config CRYPTO_CMAC
crypto/Makefile +1 -0
@@ -80,6 +80,7 @@ obj-$(CONFIG_CRYPTO_LRW) += lrw.o obj-$(CONFIG_CRYPTO_XTS) += xts.o obj-$(CONFIG_CRYPTO_CTR) += ctr.o obj-$(CONFIG_CRYPTO_KEYWRAP) += keywrap.o +obj-$(CONFIG_CRYPTO_KDF) += kdf.o obj-$(CONFIG_CRYPTO_GCM) += gcm.o obj-$(CONFIG_CRYPTO_CCM) += ccm.o obj-$(CONFIG_CRYPTO_CHACHA20POLY1305) += chacha20poly1305.o
Signature · 1 line
2.7.4
PATCH v2 1/1 DH support: add KDF handling support
In reply to Stephan MuellerHi Mat, David,
this patch covers all comments you raised. I also added a man page for the new API calls.
---8<---
Add the interface logic to support DH with KDF handling support.
The dh_compute code now allows the following options:
- no KDF support / output of raw DH shared secret:
dh_compute <private> <prime> <base>
- KDF support without "other information" string:
dh_compute_kdf <private> <prime> <base> <output length> <KDF type>
- KDF support with "other information string:
dh_compute_kdf_oi <private> <prime> <base> <output length> <KDF type>
where the OI string is provided on STDIN.
The test to verify the code is based on a test vector used for the CAVS testing of SP800-56A.
--- Makefile | 1 + keyctl.c | 125 +++++++++++++++++++++++ keyutils.c | 44 ++++++++ keyutils.h | 15 +++ man/keyctl_dh_compute_kdf.3 | 143 ++++++++++++++++++++++++++ tests/keyctl/dh_compute/valid/runtest.sh | 168 +++++++++++++++++++++++++++++++ tests/toolbox.inc.sh | 44 ++++++++ version.lds | 2 + 8 files changed, 542 insertions(+) create mode 100644 man/keyctl_dh_compute_kdf.3
Makefile +1 -0
@@ -195,6 +195,7 @@ endif $(LNS) keyctl_read.3 $(DESTDIR)$(MAN3)/keyctl_read_alloc.3 $(LNS) recursive_key_scan.3 $(DESTDIR)$(MAN3)/recursive_session_key_scan.3 $(LNS) keyctl_dh_compute.3 $(DESTDIR)$(MAN3)/keyctl_dh_compute_alloc.3 + $(LNS) keyctl_dh_compute_kdf.3 $(DESTDIR)$(MAN3)/keyctl_dh_compute_kdf_alloc.3 $(INSTALL) -D -m 0644 keyutils.h $(DESTDIR)$(INCLUDEDIR)/keyutils.h ###############################################################################
keyctl.c +125 -0
155 lines not on this page · raw message
keyutils.c +44 -0
56 lines not on this page · raw message
keyutils.h +15 -0
@@ -108,6 +108,13 @@ struct keyctl_dh_params {key_serial_t base; }; +struct keyctl_kdf_params { +char *kdfname; +char *otherinfo; +uint32_t otherinfolen; +uint32_t __spare[8 ]; +}; +/* * syscall wrappers */
@@ -163,6 +170,10 @@ extern long keyctl_invalidate(key_serial_t id);extern long keyctl_get_persistent (uid_t uid,key_serial_t id);extern long keyctl_dh_compute (key_serial_t private,key_serial_t prime,key_serial_t base,char *buffer,size_t buflen); +extern long keyctl_dh_compute_kdf (key_serial_t private,key_serial_t prime, +key_serial_t base,char *kdfname, +char *otherinfo,size_t otherinfolen, +char *buffer,size_t buflen);/* * utilities
@@ -172,6 +183,10 @@ extern int keyctl_read_alloc(key_serial_t id, void **_buffer);extern int keyctl_get_security_alloc (key_serial_t id,char **_buffer);extern int keyctl_dh_compute_alloc (key_serial_t private,key_serial_t prime,key_serial_t base,void **_buffer); +extern int keyctl_dh_compute_kdf_alloc (key_serial_t private,key_serial_t prime, +key_serial_t base,size_t genlen, +char *kdfname,char *otherinfo, +size_t otherinfolen,void **_buffer);typedef int (*recursive_key_scanner_t )(key_serial_t parent,key_serial_t key,char *desc,int desc_len,void *data);
man/keyctl_dh_compute_kdf.3 new file +143 -0
143 lines not on this page · raw message
tests/keyctl/dh_compute/valid/runtest.sh +168 -0
173 lines not on this page · raw message
tests/toolbox.inc.sh +44 -0
50 lines not on this page · raw message
version.lds +2 -0
@@ -66,5 +66,7 @@ KEYUTILS_1.6 { /* management functions */ keyctl_dh_compute; keyctl_dh_compute_alloc; + keyctl_dh_compute_kdf; + keyctl_dh_compute_kdf_alloc; } KEYUTILS_1.5;
Signature · 1 line
2.7.4
Re: [PATCH v2] DH support: add KDF handling support
Am Donnerstag, 4. August 2016, 20:38:59 CEST schrieb Stephan Mueller:
Hi Mat,
Quoted 9 lines
keyctl.c +1
-0@@ -1663,6 +1668,7 @@ static void act_keyctl_dh_compute(int argc, char *argv[]) } printf("%02hhx", *p); + *p =0x00 ;/* zeroize buffer */ p++; col++;
keyctl.c static void act_keyctl_dh_compute(int argc, char · line 1684
Quoted 5 lines
keyctl.c +2
-0@@ -1674,6 +1680,125 @@ static void act_keyctl_dh_compute(int argc, char *argv[]) } while (--ret > 0); printf("\n"); + + free(buffer);
I hope it is ok that I add a bugfix in that patch set.
Ciao
Stephan
Re: [PATCH v2] DH support: add KDF handling support
In reply to Stephan MuellerStephan,
Quoted 63 lines · Stephan Mueller
Hi Mat, David,
this patch covers all comments you raised. I also added a man page for the new API calls.
---8<---
Add the interface logic to support DH with KDF handling support.
The
dh_computecode now allows the following options:- no KDF support / output of raw DH shared secret:
dh_compute<private> <prime> <base>- KDF support without "other information" string:
dh_compute_kdf<private> <prime> <base> <output length> <KDF type>- KDF support with "other information string:
dh_compute_kdf_oi<private> <prime> <base> <output length> <KDF type>
where the OI string is provided on STDIN.The test to verify the code is based on a test vector used for the CAVS testing of SP800-56A.
Signed-off-by Stephan Mueller <[email protected]>---
Makefile | 1 + keyctl.c | 125 +++++++++++++++++++++++ keyutils.c | 44 ++++++++ keyutils.h | 15 +++ man/keyctl_dh_compute_kdf.3 | 143 ++++++++++++++++++++++++++ tests/keyctl/dh_compute/valid/runtest.sh | 168 +++++++++++++++++++++++++++++++ tests/toolbox.inc.sh | 44 ++++++++ version.lds | 2 + 8 files changed, 542 insertions(+)create mode 100644 man/keyctl_dh_compute_kdf.3
keyutils.c +14
-0@@ -244,6 +244,20 @@ long keyctl_dh_compute(key_serial_t private, key_serial_t prime,return keyctl (KEYCTL_DH_COMPUTE , ¶ms, buffer, buflen,0 ); } +long keyctl_dh_compute_kdf (key_serial_t private,key_serial_t prime, +key_serial_t base,char *kdfname,char *otherinfo, +size_t otherinfolen,char *buffer,size_t buflen) +{ +struct keyctl_dh_params params = { .private = private, + .prime = prime, + .base = base }; +struct keyctl_kdf_params kdfparams = { .kdfname = kdfname, + .otherinfo = otherinfo, + .otherinfolen = otherinfolen }; + +return keyctl (KEYCTL_DH_COMPUTE , ¶ms, buffer, buflen, &kdfparams); +} +/*****************************************************************************/ /* * fetch key description into an allocated buffer
keyutils.c int keyctl_dh_compute_alloc(key_serial_t private, key_serial_t prime, · line 407
Quoted 8 lines
keyutils.c +5
-0@@ -386,6 +400,36 @@ int keyctl_dh_compute_alloc(key_serial_t private, key_serial_t prime, }/* +* fetch DH computation results processed by a KDF into an +* allocated buffer +* - resulting buffer has an extra NUL added to the end +* - returns count (not including extraneous NUL) +*/
I don't think this function should be added. Since genlen is known, the caller can handle all of the memory management and call keyctl_dh_compute_kdf itself. Other _alloc functions do something extra, like ask the kernel how big of a buffer is needed.
man/keyctl_dh_compute_kdf.3
Quoted 73 lines
keyutils.c +25
-0+int keyctl_dh_compute_kdf_alloc (key_serial_t private,key_serial_t prime, +key_serial_t base,size_t genlen,char *kdfname, +char *otherinfo,size_t otherinfolen, +void **_buffer) +{ +char *buf; +int ret; + + buf =malloc (genlen +1 ); +if (!buf) +return -1 ; + + ret =keyctl_dh_compute_kdf (private, prime, base, kdfname, otherinfo, + otherinfolen, buf, genlen); +if (ret <0 ) { +free (buf); +return -1 ; + } + + buf[ret] =0 ; + *_buffer = buf; +return ret; +} + +/* * Depth-first recursively apply a function over a keyring tree */ static int recursive_key_scan_aux(key_serial_t parent, key_serial_t key,keyutils.h +15
-0@@ -108,6 +108,13 @@ struct keyctl_dh_params {key_serial_t base; }; +struct keyctl_kdf_params { +char *kdfname; +char *otherinfo; +uint32_t otherinfolen; +uint32_t __spare[8 ]; +}; +/* * syscall wrappers */ @@ -163,6 +170,10 @@ extern long keyctl_invalidate(key_serial_t id);extern long keyctl_get_persistent (uid_t uid,key_serial_t id);extern long keyctl_dh_compute (key_serial_t private,key_serial_t prime,key_serial_t base,char *buffer,size_t buflen); +extern long keyctl_dh_compute_kdf (key_serial_t private,key_serial_t prime, +key_serial_t base,char *kdfname, +char *otherinfo,size_t otherinfolen, +char *buffer,size_t buflen);/* * utilities @@ -172,6 +183,10 @@ extern int keyctl_read_alloc(key_serial_t id, void **_buffer);extern int keyctl_get_security_alloc (key_serial_t id,char **_buffer);extern int keyctl_dh_compute_alloc (key_serial_t private,key_serial_t prime,key_serial_t base,void **_buffer); +extern int keyctl_dh_compute_kdf_alloc (key_serial_t private,key_serial_t prime, +key_serial_t base,size_t genlen, +char *kdfname,char *otherinfo, +size_t otherinfolen,void **_buffer);typedef int (*recursive_key_scanner_t )(key_serial_t parent,key_serial_t key,char *desc,int desc_len,void *data);man/keyctl_dh_compute_kdf.3 new file +0
-0
My vote is to include this content in keyctl_dh_compute.3 instead of adding a separate man page.
Quoted 134 lines
man/keyctl_dh_compute_kdf.3 new file +133
-0@@ -0,0 +1,143 @@ +.\" +.\" Copyright (C) 2006 Red Hat, Inc. All Rights Reserved. +.\" Copyright (C) 2016 Intel Corporation. All rights reserved. +.\" +.\" This program is free software; you can redistribute it and/or +.\" modify it under the terms of the GNU General Public License +.\" as published by the Free Software Foundation; either version +.\" 2 of the License, or (at your option) any later version. +.\" +.TH KEYCTL_DH_COMPUTE 3 "26 Jul 2016" Linux "Linux Key Management Calls" +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.SH NAME +keyctl_dh_compute_kdf \- Derive key from a Diffie-Hellman shared secret +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.SH SYNOPSIS +.nf +.B #include <keyutils.h> +.sp +.BI "long keyctl_dh_compute_kdf(key_serial_t " private ", key_serial_t " prime , +.BI "key_serial_t " base ", char *" kdfname ", char *" otherinfo ", +.BI "size_t " otherinfolen ", char *" buffer ", size_t " buflen ");" +.sp +.BI "long keyctl_dh_compute_kdf_alloc(key_serial_t " private, +.BI "key_serial_t " prime ", key_serial_t " base ", size_t " genlen ", +.BI "char *" kdfname ", "char *" otherinfo ", size_t " otherinfolen ", +.BI "void **" _buffer ");" +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.SH DESCRIPTION +.BR keyctl_dh_compute_kdf () +derives a key from a Diffie-Hellman shared secret according to the protocol +specified in SP800-56A. +.P +For the Diffie-Hellman computation, the following algorithm is used:: +.IP +.I base +^ +.I private +( mod +.I prime +) +.P +To implement the protocol of SP800-56A +.I base +is a key containing the remote public key to compute the Diffie-Hellman +shared secret. That shared secret is post-processed with a key derivation +function. +.P +.IR base ", " private ", and " prime +must all refer to +.BR user -type +keys containing the parameters for the computation. Each of these keys must +grant the caller +.B read +permission in order for them to be used. +.P +The +.I kdfname +specifies the Linux kernel crypto API name for a key derivation function +using a non-keyed hash, such as kdf_ctr(sha256). Using the counter KDF function +specified with kdf_ctr() makes the key derivation compliant to SP800-56A. +The +.I kdfname +must be a NULL terminated string. +.P +Following the specification of SP800-56A section 5.8.1.2 the +.I otherinfo +parameter may be provided. The format of the OtherInfo field is defined +by the caller. The caller may also specify NULL as a valid argument when +no OtherInfo data shall be processed. The length of the +.I otherinfo +parameter is specified with +.I otherinfolen +and is restricted to a maximum length by the kernel. +.P +The KDF returns the requested number of bytes specified with the +.I genlen +or the +.I buflen +parameter depending on the invoked function. +.P +.I buffer +and +.I buflen +specify the buffer into which the computed result will be placed. +.P +.BR keyctl_dh_compute_kdf_alloc () +is similar to +.BR keyctl_dh_compute_kdf () +except that it allocates a buffer with the size of +.I genlen +to hold the payload data and places the data in it. If successful, a pointer +to the buffer is placed in +.IR *_buffer . +The caller must free the buffer. +.P +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.SH RETURN VALUE +On success +.BR keyctl_dh_compute_kdf () +returns the amount of data placed into the buffer when +.I buflen +is non-zero. +.P +On success +.BR keyctl_dh_compute_kdf_alloc () +returns the amount of data in the buffer. +.P +On error, both functions set errno to an appropriate code and return the value +.BR -1 . +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.SH ERRORS +.TP +.B ENOKEY +One of the keys specified is invalid or not readable. +.TP +.B EINVAL +The buffer pointer is invalid or buflen is too small. +.TP +.B EOPNOTSUPP +One of the keys was not a valid user key. +.TP +.B EMSGSIZE +The size of either +.I otherinfolen +or +.I buflen +is too big. +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.SH LINKING +This is a library function that can be found in +.IR libkeyutils . +When linking, +.B -lkeyutils
man/keyctl_dh_compute_kdf.3 line 143
Quoted 10 lines
man/keyctl_dh_compute_kdf.3 new file +10
-0+should be specified to the linker. +.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +.SH SEE ALSO +.BR keyctl (1), +.br +.BR keyctl (2), +.br +.BR keyctl (3), +.br +.BR keyutils (7)
--
Mat Martineau
Intel OTC
Re: [PATCH v2] DH support: add KDF handling support
Am Donnerstag, 4. August 2016, 13:57:27 CEST schrieb Mat Martineau:
Hi Mat,
Quoted 74 lines
Stephan,
Quoted 71 lines · Stephan Mueller
Hi Mat, David,
this patch covers all comments you raised. I also added a man page for the new API calls.
---8<---
Add the interface logic to support DH with KDF handling support.
The
dh_computecode now allows the following options:- no KDF support / output of raw DH shared secret:
dh_compute<private> <prime> <base>- KDF support without "other information" string:
dh_compute_kdf<private> <prime> <base> <output length> <KDF type>- KDF support with "other information string:
dh_compute_kdf_oi<private> <prime> <base> <output length> <KDF type>where the OI string is provided on STDIN.
The test to verify the code is based on a test vector used for the CAVS testing of SP800-56A.
Signed-off-by Stephan Mueller <[email protected]>---
Makefile | 1 + keyctl.c | 125 +++++++++++++++++++++++ keyutils.c | 44 ++++++++ keyutils.h | 15 +++ man/keyctl_dh_compute_kdf.3 | 143 ++++++++++++++++++++++++++ tests/keyctl/dh_compute/valid/runtest.sh | 168 +++++++++++++++++++++++++++++++ tests/toolbox.inc.sh | 44 ++++++++ version.lds | 2 + 8 files changed, 542 insertions(+)create mode 100644 man/keyctl_dh_compute_kdf.3
keyutils.c +0
-0@@ -244,6 +244,20 @@ long keyctl_dh_compute(key_serial_t private,
key_serial_tprime,>return keyctl(KEYCTL_DH_COMPUTE, ¶ms, buffer, buflen, 0);}
keyutils.c +14
-0+long keyctl_dh_compute_kdf (key_serial_t private,key_serial_t prime, +key_serial_t base,char *kdfname,char *otherinfo, +size_t otherinfolen,char *buffer,size_t buflen) +{ +struct keyctl_dh_params params = { .private = private, + .prime = prime, + .base = base }; +struct keyctl_kdf_params kdfparams = { .kdfname = kdfname, + .otherinfo = otherinfo, + .otherinfolen = otherinfolen }; + +return keyctl (KEYCTL_DH_COMPUTE , ¶ms, buffer, buflen, &kdfparams); +} +/************************************************************************* ****/ /*
* fetch key description into an allocated buffer
keyutils.c +0
-0@@ -386,6 +400,36 @@ int keyctl_dh_compute_alloc(key_serial_t private, key_serial_t prime, }
Quoted 11 lines
keyutils.c int keyctl_dh_compute_alloc(key_serial_t private, key_serial_t prime, · line 407
Quoted 6 lines
keyutils.c +5
-0/* +* fetch DH computation results processed by a KDF into an +* allocated buffer +* - resulting buffer has an extra NUL added to the end +* - returns count (not including extraneous NUL) +*/ I don't think this function should be added. Since genlen is known, the caller can handle all of the memory management and call
keyctl_dh_compute_kdfitself. Other_allocfunctions do something extra, like ask the kernel how big of a buffer is needed.
Ok, I will remove this one.
Quoted 83 lines
Quoted 83 lines
keyutils.c +25
-0+int keyctl_dh_compute_kdf_alloc (key_serial_t private,key_serial_t prime, +key_serial_t base,size_t genlen,char *kdfname, +char *otherinfo,size_t otherinfolen, +void **_buffer) +{ +char *buf; +int ret; + + buf =malloc (genlen +1 ); +if (!buf) +return -1 ; + + ret =keyctl_dh_compute_kdf (private, prime, base, kdfname, otherinfo, + otherinfolen, buf, genlen); +if (ret <0 ) { +free (buf); +return -1 ; + } + + buf[ret] =0 ; + *_buffer = buf; +return ret; +} + +/* * Depth-first recursively apply a function over a keyring tree */ static int recursive_key_scan_aux(key_serial_t parent,
key_serial_tkey,keyutils.h +7
-0@@ -108,6 +108,13 @@ struct keyctl_dh_params {key_serial_t base; }; +struct keyctl_kdf_params { +char *kdfname; +char *otherinfo; +uint32_t otherinfolen; +uint32_t __spare[8 ]; +}; +/* * syscall wrappers
*/keyutils.h +3
-0@@ -163,6 +170,10 @@ extern long keyctl_invalidate(key_serial_t id);extern long keyctl_get_persistent (uid_t uid,key_serial_t id);extern long keyctl_dh_compute (key_serial_t private,key_serial_t prime,key_serial_t base,char *buffer,size_t buflen); +extern long keyctl_dh_compute_kdf (key_serial_t private,key_serial_t prime, + key_serial_t base, char *kdfname, +char *otherinfo,size_t otherinfolen, +char *buffer,size_t buflen);/*
* utilities
keyutils.h +0
-0@@ -172,6 +183,10 @@ extern int keyctl_read_alloc(key_serial_t id, void **_buffer); extern int keyctl_get_security_alloc(key_serial_t id, char **_buffer); extern int keyctl_dh_compute_alloc(key_serial_t private,
key_serial_tprime,>key_serial_t base, void **_buffer);+extern int keyctl_dh_compute_kdf_alloc(key_serial_t private,
key_serial_t
prime, +key_serial_tbase,size_tgenlen,keyutils.h +2
-0+ char *kdfname, char *otherinfo, + size_t otherinfolen, void **_buffer);typedef int (*recursive_key_scanner_t)(key_serial_t parent,
key_serial_tkey,>char *desc, int desc_len, void *data);
Quoted 8 lines
man/keyctl_dh_compute_kdf.3
Quoted 5 lines
man/keyctl_dh_compute_kdf.3 new file +0
-0My vote is to include this content in keyctl_dh_compute.3 instead of adding a separate man page.
Ok, I will move that into the existing man page.
Ciao
Stephan
PATCH v3 1/1 DH support: add KDF handling support
In reply to Stephan MuellerAdd the interface logic to support DH with KDF handling support.
The dh_compute code now allows the following options:
- no KDF support / output of raw DH shared secret:
dh_compute <private> <prime> <base>
- KDF support without "other information" string:
dh_compute_kdf <private> <prime> <base> <output length> <KDF type>
- KDF support with "other information string:
dh_compute_kdf_oi <private> <prime> <base> <output length> <KDF type>
where the OI string is provided on STDIN.
The test to verify the code is based on a test vector used for the CAVS testing of SP800-56A.
--- Makefile | 1 + keyctl.c | 133 ++++++++++++++++++++++++ keyutils.c | 14 +++ keyutils.h | 11 ++ man/keyctl_dh_compute.3 | 58 +++++++++++ tests/keyctl/dh_compute/valid/runtest.sh | 168 +++++++++++++++++++++++++++++++ tests/toolbox.inc.sh | 44 ++++++++ version.lds | 2 + 8 files changed, 431 insertions(+)
Makefile +1 -0
@@ -195,6 +195,7 @@ endif $(LNS) keyctl_read.3 $(DESTDIR)$(MAN3)/keyctl_read_alloc.3 $(LNS) recursive_key_scan.3 $(DESTDIR)$(MAN3)/recursive_session_key_scan.3 $(LNS) keyctl_dh_compute.3 $(DESTDIR)$(MAN3)/keyctl_dh_compute_alloc.3 + $(LNS) keyctl_dh_compute.3 $(DESTDIR)$(MAN3)/keyctl_dh_compute_kdf.3 $(INSTALL) -D -m 0644 keyutils.h $(DESTDIR)$(INCLUDEDIR)/keyutils.h ###############################################################################
keyctl.c +133 -0
163 lines not on this page · raw message
keyutils.c +14 -0
@@ -244,6 +244,20 @@ long keyctl_dh_compute(key_serial_t private, key_serial_t prime,return keyctl (KEYCTL_DH_COMPUTE , ¶ms, buffer, buflen,0 ); } +long keyctl_dh_compute_kdf (key_serial_t private,key_serial_t prime, +key_serial_t base,char *kdfname,char *otherinfo, +size_t otherinfolen,char *buffer,size_t buflen) +{ +struct keyctl_dh_params params = { .private = private, + .prime = prime, + .base = base }; +struct keyctl_kdf_params kdfparams = { .kdfname = kdfname, + .otherinfo = otherinfo, + .otherinfolen = otherinfolen }; + +return keyctl (KEYCTL_DH_COMPUTE , ¶ms, buffer, buflen, &kdfparams); +} +/*****************************************************************************/ /* * fetch key description into an allocated buffer
keyutils.h +11 -0
@@ -108,6 +108,13 @@ struct keyctl_dh_params {key_serial_t base; }; +struct keyctl_kdf_params { +char *kdfname; +char *otherinfo; +uint32_t otherinfolen; +uint32_t __spare[8 ]; +}; +/* * syscall wrappers */
@@ -163,6 +170,10 @@ extern long keyctl_invalidate(key_serial_t id);extern long keyctl_get_persistent (uid_t uid,key_serial_t id);extern long keyctl_dh_compute (key_serial_t private,key_serial_t prime,key_serial_t base,char *buffer,size_t buflen); +extern long keyctl_dh_compute_kdf (key_serial_t private,key_serial_t prime, +key_serial_t base,char *kdfname, +char *otherinfo,size_t otherinfolen, +char *buffer,size_t buflen);/* * utilities
man/keyctl_dh_compute.3 +58 -0
82 lines not on this page · raw message
tests/keyctl/dh_compute/valid/runtest.sh +168 -0
173 lines not on this page · raw message
tests/toolbox.inc.sh +44 -0
50 lines not on this page · raw message
version.lds +2 -0
@@ -66,5 +66,7 @@ KEYUTILS_1.6 { /* management functions */ keyctl_dh_compute; keyctl_dh_compute_alloc; + keyctl_dh_compute_kdf; + keyctl_dh_compute_kdf_alloc; } KEYUTILS_1.5;
Signature · 1 line
2.7.4
Re: [PATCH v3] DH support: add KDF handling support
Quoted 29 lines · Stephan Mueller
man/keyctl_dh_compute.3 +0
-0@@ -11,6 +11,8 @@.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" .SH NAME
keyctl_dh_compute\- Compute a Diffie-Hellman shared secret or public key +.br
+keyctl_dh_compute_kdf \- Derive key from a Diffie-Hellman shared secret .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" .SH SYNOPSIS
.nfman/keyctl_dh_compute.3 +0
-0@@ -21,6 +23,10 @@ keyctl_dh_compute \- Compute a Diffie-Hellman shared secret or public key.sp
.BI "long keyctl_dh_compute_alloc(key_serial_t " private,
.BI "key_serial_t" prime ",key_serial_t" base ", void **"_buffer");" +.sp
+.BI "long keyctl_dh_compute_kdf(key_serial_t " private ",key_serial_t" prime , +.BI "key_serial_t" base ", char *" kdfname ", char *" otherinfo ", +.BI "size_t" otherinfolen ", char *" buffer ",size_t" buflen ");" .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" .SH DESCRIPTION
.BRkeyctl_dh_compute()man/keyctl_dh_compute.3 +0
-0@@ -64,6 +70,49 @@ places the data in it. If successful, a pointer to the buffer is placed in.IR
*_buffer.
The caller must free the buffer.
.P
+.BRkeyctl_dh_compute_kdf()
man/keyctl_dh_compute.3 places the data in it. If successful, a pointer to the buffer is placed in · line 78
Quoted 5 lines
man/keyctl_dh_compute.3 +5
-0+derives a key from a Diffie-Hellman shared secret according to the protocol +specified in SP800-56A. The Diffie-Hellman computation is based on the same +primitives as discussed +for +.BR keyctl_dy_compute ().
Minor typo: dy->dh
Regards,
--
Mat Martineau
Intel OTC
PATCH v4 1/1 DH support: add KDF handling support
Add the interface logic to support DH with KDF handling support.
The dh_compute code now allows the following options:
- no KDF support / output of raw DH shared secret:
dh_compute <private> <prime> <base>
- KDF support without "other information" string:
dh_compute_kdf <private> <prime> <base> <output length> <KDF type>
- KDF support with "other information string:
dh_compute_kdf_oi <private> <prime> <base> <output length> <KDF type>
where the OI string is provided on STDIN.
The test to verify the code is based on a test vector used for the CAVS testing of SP800-56A.
--- Makefile | 1 + keyctl.c | 133 ++++++++++++++++++++++++ keyutils.c | 14 +++ keyutils.h | 11 ++ man/keyctl_dh_compute.3 | 58 +++++++++++ tests/keyctl/dh_compute/valid/runtest.sh | 168 +++++++++++++++++++++++++++++++ tests/toolbox.inc.sh | 44 ++++++++ version.lds | 2 + 8 files changed, 431 insertions(+)
Makefile +1 -0
@@ -195,6 +195,7 @@ endif $(LNS) keyctl_read.3 $(DESTDIR)$(MAN3)/keyctl_read_alloc.3 $(LNS) recursive_key_scan.3 $(DESTDIR)$(MAN3)/recursive_session_key_scan.3 $(LNS) keyctl_dh_compute.3 $(DESTDIR)$(MAN3)/keyctl_dh_compute_alloc.3 + $(LNS) keyctl_dh_compute.3 $(DESTDIR)$(MAN3)/keyctl_dh_compute_kdf.3 $(INSTALL) -D -m 0644 keyutils.h $(DESTDIR)$(INCLUDEDIR)/keyutils.h ###############################################################################
keyctl.c +133 -0
163 lines not on this page · raw message
keyutils.c +14 -0
@@ -244,6 +244,20 @@ long keyctl_dh_compute(key_serial_t private, key_serial_t prime,return keyctl (KEYCTL_DH_COMPUTE , ¶ms, buffer, buflen,0 ); } +long keyctl_dh_compute_kdf (key_serial_t private,key_serial_t prime, +key_serial_t base,char *kdfname,char *otherinfo, +size_t otherinfolen,char *buffer,size_t buflen) +{ +struct keyctl_dh_params params = { .private = private, + .prime = prime, + .base = base }; +struct keyctl_kdf_params kdfparams = { .kdfname = kdfname, + .otherinfo = otherinfo, + .otherinfolen = otherinfolen }; + +return keyctl (KEYCTL_DH_COMPUTE , ¶ms, buffer, buflen, &kdfparams); +} +/*****************************************************************************/ /* * fetch key description into an allocated buffer
keyutils.h +11 -0
@@ -108,6 +108,13 @@ struct keyctl_dh_params {key_serial_t base; }; +struct keyctl_kdf_params { +char *kdfname; +char *otherinfo; +uint32_t otherinfolen; +uint32_t __spare[8 ]; +}; +/* * syscall wrappers */
@@ -163,6 +170,10 @@ extern long keyctl_invalidate(key_serial_t id);extern long keyctl_get_persistent (uid_t uid,key_serial_t id);extern long keyctl_dh_compute (key_serial_t private,key_serial_t prime,key_serial_t base,char *buffer,size_t buflen); +extern long keyctl_dh_compute_kdf (key_serial_t private,key_serial_t prime, +key_serial_t base,char *kdfname, +char *otherinfo,size_t otherinfolen, +char *buffer,size_t buflen);/* * utilities
man/keyctl_dh_compute.3 +58 -0
82 lines not on this page · raw message
tests/keyctl/dh_compute/valid/runtest.sh +168 -0
173 lines not on this page · raw message
tests/toolbox.inc.sh +44 -0
50 lines not on this page · raw message
version.lds +2 -0
@@ -66,5 +66,7 @@ KEYUTILS_1.6 { /* management functions */ keyctl_dh_compute; keyctl_dh_compute_alloc; + keyctl_dh_compute_kdf; + keyctl_dh_compute_kdf_alloc; } KEYUTILS_1.5;
Signature · 1 line
2.7.4
Stephan,
Quoted 72 lines · Stephan Mueller
Hi Mat,
the following code addresses all your comments as discussed.
In addition, this code is now complete: it contains an extension to the documentation as well as the compat code. The compat code was tested successfully with compiling the user space tool with -m32 after applying the patch from David ensuring the invocation of the keyctl compat syscall.
Ciao
Stephan---8<----
SP800-56A defines the use of DH with key derivation function based on a counter. The input to the KDF is defined as (DH shared secret || other information). The value for the "other information" is to be provided by the caller.
The KDF is provided by the kernel crypto API. The SP800-56A KDF is equal to the SP800-108 counter KDF. However, the caller is allowed to specify the KDF type that he wants to use to derive the key material allowing the use of the other KDFs provided with the kernel crypto API.
As the KDF implements the proper truncation of the DH shared secret to the requested size, this patch fills the caller buffer up to its size.
The patch is tested with a new test added to the keyutils user space code which uses a CAVS test vector testing the compliance with SP800-56A.
Signed-off-by Stephan Mueller <[email protected]>---
Documentation/security/keys.txt | 35 +++++++++--- include/linux/compat.h | 7 +++ include/uapi/linux/keyctl.h | 7 +++ security/keys/Kconfig | 1 + security/keys/compat.c | 34 +++++++++++- security/keys/dh.c | 119 ++++++++++++++++++++++++++++++++++++---- security/keys/internal.h | 9 ++- security/keys/keyctl.c | 2 +- 8 files changed, 189 insertions(+), 25 deletions(-)Documentation/security/keys.txt +4
-7@@ -827,7 +827,7 @@ The keyctl syscall functions are: long keyctl(KEYCTL_DH_COMPUTE, struct keyctl_dh_params *params, char *buffer, size_t buflen, - void *reserved); + struct keyctl_kdf_params *kdf); The params struct contains serial numbers for three keys:@@ -844,18 +844,37 @@ The keyctl syscall functions are: public key. If the base is the remote public key, the result is the shared secret. - The reserved argument must be set to NULL. + If the parameter kdf is NULL, the following applies: - The buffer length must be at least the length of the prime, or zero. + - The buffer length must be at least the length of the prime, or zero. - If the buffer length is nonzero, the length of the result is - returned when it is successfully calculated and copied in to the - buffer. When the buffer length is zero, the minimum required - buffer length is returned. + - If the buffer length is nonzero, the length of the result is
Documentation/security/keys.txt The keyctl syscall functions are: · line 861
Quoted 10 lines
Documentation/security/keys.txt +10
-0+ returned when it is successfully calculated and copied in to the + buffer. When the buffer length is zero, the minimum required + buffer length is returned. + + The kdf parameter allows the caller to apply a key derivation function + (KDF) on the Diffie-Hellman computation where only the result + of the KDF is returned to the caller. The KDF is characterized with + struct keyctl_kdf_params as follows: + + - char *kdfname specifies the NULL terminated string identifying
Change "NULL" to "NUL". NULL refers to a zero pointer, NUL refers to the '\0' character.
Documentation/security/keys.txt The keyctl syscall functions are: · line 867
Quoted 6 lines
Documentation/security/keys.txt +6
-0+ the KDF function used from the kernel crypto API. As of now, + only non-keyed KDFs are supported, such as kdf_ctr(sha256), + kdf_fb(sha1) or kdf_dpi(sha512). The use of kdf_ctr() complies + with SP800-56A. + + - char *otherinfo specifies the OtherInfo string as documented in
I suggest calling it "OtherInfo data" rather than a "string", which implies a NUL-terminated C string.
Quoted 65 lines
Documentation/security/keys.txt +7
-1+ SP800-56A section 5.8.1.2. The length of the buffer is given with + otherinfolen. The format of OtherInfo is defined by the caller. + The otherinfo pointer may be NULL if no OtherInfo shall be used. This function will return error EOPNOTSUPP if the key type is not supported, error ENOKEY if the key could not be found, or error - EACCES if the key is not readable by the caller. + EACCES if the key is not readable by the caller. In addition, the + function will return EMSGSIZE when the parameter kdf is non-NULL + and either the buffer length or the OtherInfo length exceeds the + allowed length.===============
KERNEL SERVICESinclude/linux/compat.h +7
-0@@ -295,6 +295,13 @@ struct compat_old_sigaction { };#endif +struct compat_keyctl_kdf_params { +compat_uptr_t kdfname; +compat_uptr_t otherinfo; +__u32 otherinfolen; +__u32 __spare[8 ]; +}; +struct compat_statfs ;struct compat_statfs64 ;struct compat_old_linux_dirent ;include/uapi/linux/keyctl.h +7
-0@@ -68,4 +68,11 @@ struct keyctl_dh_params { __s32 base; }; +struct keyctl_kdf_params { +char *kdfname; +char *otherinfo; +__u32 otherinfolen; +__u32 __spare[8 ]; +}; +#endif /* _LINUX_KEYCTL_H */ security/keys/Kconfig +1
-0@@ -90,6 +90,7 @@ config KEY_DH_OPERATIONS bool "Diffie-Hellman operations on retained keys" depends on KEYS select MPILIB + select CRYPTO_KDF help This option provides support for calculating Diffie-Hellman public keys and shared secrets using values stored as keyssecurity/keys/compat.c +0
-0
security/keys/compat.c static long compat_keyctl_instantiate_key_iov( · line 51
Quoted 4 lines
security/keys/compat.c +1
-0@@ -48,6 +48,35 @@ static long compat_keyctl_instantiate_key_iov(return ret; } +#ifdef CONFIG_KEY_DH_OPERATIONS
See chapter 20 of https://www.kernel.org/doc/Documentation/CodingStyle, preprocessor conditionals are advised against in .c files.
You can put the function prototype and stub implementation in internal.h, and move the function to compat_dh.c. Then the Makefile should only build compat_dh.c if both relevant config options are set (look for "compat-obj" in other Makefiles).
Quoted 52 lines
security/keys/compat.c +31
-2+static long compat_keyctl_dh_compute (struct keyctl_dh_params __user *params, +char __user *buffer,size_t buflen, +struct compat_keyctl_kdf_params __user *kdf) +{ +struct keyctl_kdf_params kdfcopy; +struct compat_keyctl_kdf_params compat_kdfcopy; + +if (!kdf) +return __keyctl_dh_compute (params, buffer, buflen,NULL ); + +if (copy_from_user (&compat_kdfcopy, kdf,sizeof (compat_kdfcopy)) !=0 ) +return -EFAULT ; + + kdfcopy.kdfname =compat_ptr (compat_kdfcopy.kdfname); + kdfcopy.otherinfo =compat_ptr (compat_kdfcopy.otherinfo); + kdfcopy.otherinfolen = compat_kdfcopy.otherinfolen; + +return __keyctl_dh_compute (params, buffer, buflen, &kdfcopy); +} +#else +static long compat_keyctl_dh_compute (struct keyctl_dh_params __user *params, +char __user *buffer,size_t buflen, +struct keyctl_kdf_params __user *kdf) +{ + return -EOPNOTSUPP +} +#endif +/* * The key control system call, 32-bit compatibility version for 64-bit archs * @@ -133,8 +162,9 @@ COMPAT_SYSCALL_DEFINE5(keyctl, u32, option,return keyctl_get_persistent (arg2, arg3);case KEYCTL_DH_COMPUTE : -return keyctl_dh_compute (compat_ptr (arg2),compat_ptr (arg3), - arg4,compat_ptr (arg5)); +return compat_keyctl_dh_compute (compat_ptr (arg2), +compat_ptr (arg3), + arg4,compat_ptr (arg5));default :return -EOPNOTSUPP ;security/keys/dh.c +0
-1@@ -77,9 +77,44 @@ error:return ret; } -long keyctl_dh_compute(struct keyctl_dh_params __user *params,
security/keys/dh.c error: · line 87
Quoted 10 lines
security/keys/dh.c +8
-2- char __user *buffer, size_t buflen, - void __user *reserved) +static int keyctl_dh_compute_kdf (struct crypto_rng *tfm, +char __user *buffer,size_t buflen, +uint8_t *kbuf,size_t kbuflen) +{ +uint8_t *outbuf =NULL ; +int ret; + +#if 0W
Need to remove these conditional lines.
Quoted 143 lines
security/keys/dh.c +81
-8+/* we do not support HMAC currently */ + ret =crypto_rng_reset (tfm, xx, xxlen); +if (ret) { +crypto_free_rng (tfm); + goto error5; + } +#endif + + outbuf =kmalloc (buflen,GFP_KERNEL ); +if (!outbuf) { + ret = -ENOMEM ; + goto err; + } + + ret =crypto_rng_generate (tfm, kbuf, kbuflen, outbuf, buflen); +if (ret) + goto err; + + ret = buflen; +if (copy_to_user (buffer, outbuf, buflen) !=0 ) + ret = -EFAULT ; + +err: +kzfree (outbuf); +return ret; +} + +long __keyctl_dh_compute (struct keyctl_dh_params __user *params, +char __user *buffer,size_t buflen, +struct keyctl_kdf_params *kdfcopy) {long ret;MPI base, private, prime, result;@@ -88,6 +123,7 @@ long keyctl_dh_compute(struct keyctl_dh_params __user *params,uint8_t *kbuf;ssize_t keylen;size_t resultlen; +struct crypto_rng *tfm =NULL ;if (!params || (!buffer && buflen)) { ret = -EINVAL ;@@ -98,12 +134,36 @@ long keyctl_dh_compute(struct keyctl_dh_params __user *params, goto out; } -if (reserved) { - ret = -EINVAL ; - goto out; +if (kdfcopy) { +char *kdfname; + +if (buflen >KEYCTL_KDF_MAX_OUTPUTLEN || + kdfcopy->otherinfolen >KEYCTL_KDF_MAX_STRING_LEN ) { + ret = -EMSGSIZE ; + goto out; + } + +/* get KDF name string */ + kdfname =strndup_user (kdfcopy->kdfname,CRYPTO_MAX_ALG_NAME ); +if (IS_ERR (kdfname)) { + ret =PTR_ERR (kdfname); + goto out; + } + +/* allocate KDF from the kernel crypto API */ + tfm =crypto_alloc_rng (kdfname,0 ,0 ); +kfree (kdfname); +if (IS_ERR (tfm)) { + ret =PTR_ERR (tfm); + goto out; + } } - keylen =mpi_from_key (pcopy.prime, buflen, &prime); +/* +* If the caller requests postprocessing with a KDF, allow an +* arbitrary output buffer size since the KDF ensures proper truncation. +*/ + keylen =mpi_from_key (pcopy.prime, kdfcopy ?SIZE_MAX : buflen, &prime);if (keylen <0 || !prime) {/* buflen == 0 may be used to query the required buffer size, * which is the prime key length. @@ -133,12 +193,25 @@ long keyctl_dh_compute(struct keyctl_dh_params __user *params, goto error3; } - kbuf =kmalloc (resultlen,GFP_KERNEL ); +/* allocate space for DH shared secret and SP800-56A otherinfo */ + kbuf =kmalloc (kdfcopy ? (resultlen + kdfcopy->otherinfolen) : resultlen, +GFP_KERNEL );if (!kbuf) { ret = -ENOMEM ; goto error4; } +/* +* Concatenate SP800-56A otherinfo past DH shared secret -- the +* input to the KDF is (DH shared secret || otherinfo) +*/ +if (kdfcopy && kdfcopy->otherinfo && +copy_from_user (kbuf + resultlen, kdfcopy->otherinfo, + kdfcopy->otherinfolen) !=0 ) { + ret = -EFAULT ; + goto error5; + } + ret =do_dh (result, base, private, prime);if (ret) goto error5;@@ -147,12 +220,17 @@ long keyctl_dh_compute(struct keyctl_dh_params __user *params,if (ret !=0 ) goto error5; - ret = nbytes; -if (copy_to_user (buffer, kbuf, nbytes) !=0 ) - ret = -EFAULT ; +if (kdfcopy) { + ret =keyctl_dh_compute_kdf (tfm, buffer, buflen, kbuf, + resultlen + kdfcopy->otherinfolen); + }else { + ret = nbytes; +if (copy_to_user (buffer, kbuf, nbytes) !=0 ) + ret = -EFAULT ; + }error5:
security/keys/dh.c +1
-1-kfree (kbuf); +kzfree (kbuf);error4:
mpi_free(result);error3:
security/keys/dh.c +0
-0@@ -162,5 +240,22 @@ error2:error1:
mpi_free(prime);out:
security/keys/dh.c +2
-0+if (tfm) +crypto_free_rng (tfm);return ret; }security/keys/dh.c +4
-0+ +long keyctl_dh_compute (struct keyctl_dh_params __user *params, +char __user *buffer,size_t buflen, +struct keyctl_kdf_params __user *kdf)
security/keys/dh.c error2: · line 260
Quoted 10 lines
security/keys/dh.c +10
-0+{ +struct keyctl_kdf_params kdfcopy; + +if (!kdf) +return __keyctl_dh_compute (params, buffer, buflen,NULL ); + +if (copy_from_user (&kdfcopy, kdf,sizeof (kdfcopy)) !=0 ) +return -EFAULT ; + +return __keyctl_dh_compute (params, buffer, buflen, &kdfcopy);
I'd find this more readable if there was one call to __keyctl_dh_compute.
Quoted 6 lines
security/keys/dh.c +1
-0+}security/keys/internal.h +0
-0
security/keys/internal.h static inline long keyctl_get_persistent(uid_t uid, key_serial_t destring) · line 267
Quoted 10 lines
security/keys/internal.h +5
-1@@ -259,12 +259,17 @@ static inline long keyctl_get_persistent(uid_t uid, key_serial_t destring)#endif #ifdef CONFIG_KEY_DH_OPERATIONS +#include <crypto/rng.h> extern long keyctl_dh_compute (struct keyctl_dh_params __user *,char __user *, -size_t ,void __user *); +size_t ,struct keyctl_kdf_params __user *); +extern long __keyctl_dh_compute (struct keyctl_dh_params __user *,char __user *, +size_t ,struct keyctl_kdf_params *); +#define KEYCTL_KDF_MAX_OUTPUTLEN 1024/* max length of KDF output */
KEYCTL_KDF_MAX_OUTPUT_LEN
^
for consistency?
security/keys/internal.h static inline long keyctl_get_persistent(uid_t uid, key_serial_t destring) · line 268
Quoted 1 line
security/keys/internal.h +1
-0+#define KEYCTL_KDF_MAX_STRING_LEN 64/* maximum length of strings */
Since this applies only to otherinfo (binary bytes, not a C string), avoid "STRING"/"strings".
Quoted 13 lines
security/keys/internal.h +1
-1#else static inline long keyctl_dh_compute (struct keyctl_dh_params __user *params,char __user *buffer,size_t buflen, -void __user *reserved) +struct keyctl_kdf_params __user *kdf) {return -EOPNOTSUPP ; }security/keys/keyctl.c +0
-0
security/keys/keyctl.c SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3, · line 1695
Quoted 8 lines
security/keys/keyctl.c +1
-1@@ -1689,7 +1689,7 @@ SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,case KEYCTL_DH_COMPUTE :return keyctl_dh_compute ((struct keyctl_dh_params __user *) arg2, (char __user *) arg3, (size_t ) arg4, - (void __user *) arg5); + (struct keyctl_kdf_params __user *) arg5);default :return -EOPNOTSUPP ;
Regards,
--
Mat Martineau
Intel OTC
Am Donnerstag, 4. August 2016, 13:41:58 CEST schrieb Mat Martineau:
Hi Mat,
I will take care of your comments in the next iteration.
Ciao
Stephan
Am Donnerstag, 4. August 2016, 13:41:58 CEST schrieb Mat Martineau:
Hi Mat,
Quoted 8 lines
Quoted 8 lines
security/keys/dh.c +8
-0+ +long keyctl_dh_compute (struct keyctl_dh_params __user *params, +char __user *buffer,size_t buflen, +struct keyctl_kdf_params __user *kdf) +{ +struct keyctl_kdf_params kdfcopy; + +if (!kdf)
Quoted 8 lines
security/keys/dh.c error2: · line 260
Quoted 6 lines
security/keys/dh.c +6
-0+return __keyctl_dh_compute (params, buffer, buflen,NULL ); + +if (copy_from_user (&kdfcopy, kdf,sizeof (kdfcopy)) !=0 ) +return -EFAULT ; + +return __keyctl_dh_compute (params, buffer, buflen, &kdfcopy);I'd find this more readable if there was one call to
__keyctl_dh_compute.
When trying to implement it, I think that it would even be more confusing, because we need a conditional in any case. Do you like
return __keyctl_dh_compute(params, buffer, buflen, (kdf) ? &kdfcopy : NULL);
better than the already presented code? Besides, this would now imply that we have two conditionals instead of one.
Ciao
Stephan
Quoted 21 lines · Stephan Mueller
Am Donnerstag, 4. August 2016, 13:41:58 CEST schrieb Mat Martineau:
Hi Mat,
Quoted 8 lines
Quoted 8 lines
security/keys/dh.c +8
-0+ +long keyctl_dh_compute (struct keyctl_dh_params __user *params, +char __user *buffer,size_t buflen, +struct keyctl_kdf_params __user *kdf) +{ +struct keyctl_kdf_params kdfcopy; + +if (!kdf)Quoted 8 lines
security/keys/dh.c error2: · line 260
Quoted 6 lines
security/keys/dh.c +6
-0+return __keyctl_dh_compute (params, buffer, buflen,NULL ); + +if (copy_from_user (&kdfcopy, kdf,sizeof (kdfcopy)) !=0 ) +return -EFAULT ; + +return __keyctl_dh_compute (params, buffer, buflen, &kdfcopy);I'd find this more readable if there was one call to
__keyctl_dh_compute.
Quoted 7 lines
When trying to implement it, I think that it would even be more confusing, because we need a conditional in any case. Do you like
return
__keyctl_dh_compute(params, buffer, buflen, (kdf) ? &kdfcopy : NULL);better than the already presented code? Besides, this would now imply that we have two conditionals instead of one.
Another approach that uses one conditional is to add a struct keyctl_kdf_params* variable and assign it in the conditional block. Maybe that's not much of a win, it's up to you.
--
Mat Martineau
Intel OTC
Am Freitag, 5. August 2016, 09:08:59 CEST schrieb Mat Martineau:
Hi Mat,
Quoted 7 lines
Quoted 7 lines
When trying to implement it, I think that it would even be more confusing, because we need a conditional in any case. Do you like
return __keyctl_dh_compute(params, buffer, buflen, (kdf) ? &kdfcopy :
NULL);
Quoted 6 lines
Quoted 2 lines
better than the already presented code? Besides, this would now imply that we have two conditionals instead of one.
Another approach that uses one conditional is to add a struct keyctl_kdf_params* variable and assign it in the conditional block. Maybe that's not much of a win, it's up to you.
To be honest, I like the code as is, because although you have two invocations, it is yet just a few lines in size and thus readable and second either my suggestion or yours with the additional variable implies one or more additional instructions (yes, I know, this should not matter too much, but still ... ).
Ciao
Stephan
PATCH v3 1/1 KEYS: add SP800-56A KDF support for DH
In reply to Stephan MuellerSP800-56A defines the use of DH with key derivation function based on a counter. The input to the KDF is defined as (DH shared secret || other information). The value for the "other information" is to be provided by the caller.
The KDF is provided by the kernel crypto API. The SP800-56A KDF is equal to the SP800-108 counter KDF. However, the caller is allowed to specify the KDF type that he wants to use to derive the key material allowing the use of the other KDFs provided with the kernel crypto API.
As the KDF implements the proper truncation of the DH shared secret to the requested size, this patch fills the caller buffer up to its size.
The patch is tested with a new test added to the keyutils user space code which uses a CAVS test vector testing the compliance with SP800-56A.
--- Documentation/security/keys.txt | 35 ++++++++++--- include/linux/compat.h | 7 +++ include/uapi/linux/keyctl.h | 7 +++ security/keys/Kconfig | 1 + security/keys/Makefile | 3 +- security/keys/compat.c | 5 +- security/keys/compat_dh.c | 36 +++++++++++++ security/keys/dh.c | 110 +++++++++++++++++++++++++++++++++++----- security/keys/internal.h | 21 +++++++- security/keys/keyctl.c | 2 +- 10 files changed, 201 insertions(+), 26 deletions(-) create mode 100644 security/keys/compat_dh.c
Documentation/security/keys.txt +27 -8
@@ -827,7 +827,7 @@ The keyctl syscall functions are: long keyctl(KEYCTL_DH_COMPUTE, struct keyctl_dh_params *params, char *buffer, size_t buflen, - void *reserved); + struct keyctl_kdf_params *kdf); The params struct contains serial numbers for three keys:
@@ -844,18 +844,37 @@ The keyctl syscall functions are: public key. If the base is the remote public key, the result is the shared secret. - The reserved argument must be set to NULL. + If the parameter kdf is NULL, the following applies: - The buffer length must be at least the length of the prime, or zero. + - The buffer length must be at least the length of the prime, or zero. - If the buffer length is nonzero, the length of the result is - returned when it is successfully calculated and copied in to the - buffer. When the buffer length is zero, the minimum required - buffer length is returned. + - If the buffer length is nonzero, the length of the result is + returned when it is successfully calculated and copied in to the + buffer. When the buffer length is zero, the minimum required + buffer length is returned. + + The kdf parameter allows the caller to apply a key derivation function + (KDF) on the Diffie-Hellman computation where only the result + of the KDF is returned to the caller. The KDF is characterized with + struct keyctl_kdf_params as follows: + + - char *kdfname specifies the NUL terminated string identifying + the KDF function used from the kernel crypto API. As of now, + only non-keyed KDFs are supported, such as kdf_ctr(sha256), + kdf_fb(sha1) or kdf_dpi(sha512). The use of kdf_ctr() complies + with SP800-56A. + + - char *otherinfo specifies the OtherInfo data as documented in + SP800-56A section 5.8.1.2. The length of the buffer is given with + otherinfolen. The format of OtherInfo is defined by the caller. + The otherinfo pointer may be NULL if no OtherInfo shall be used. This function will return error EOPNOTSUPP if the key type is not supported, error ENOKEY if the key could not be found, or error - EACCES if the key is not readable by the caller. + EACCES if the key is not readable by the caller. In addition, the + function will return EMSGSIZE when the parameter kdf is non-NULL + and either the buffer length or the OtherInfo length exceeds the + allowed length. =============== KERNEL SERVICES
include/linux/compat.h +7 -0
@@ -295,6 +295,13 @@ struct compat_old_sigaction { };#endif +struct compat_keyctl_kdf_params { +compat_uptr_t kdfname; +compat_uptr_t otherinfo; +__u32 otherinfolen; +__u32 __spare[8 ]; +}; +struct compat_statfs ;struct compat_statfs64 ;struct compat_old_linux_dirent ;
include/uapi/linux/keyctl.h +7 -0
@@ -68,4 +68,11 @@ struct keyctl_dh_params { __s32 base; }; +struct keyctl_kdf_params { +char *kdfname; +char *otherinfo; +__u32 otherinfolen; +__u32 __spare[8 ]; +}; +#endif /* _LINUX_KEYCTL_H */
security/keys/Kconfig +1 -0
@@ -90,6 +90,7 @@ config KEY_DH_OPERATIONS bool "Diffie-Hellman operations on retained keys" depends on KEYS select MPILIB + select CRYPTO_KDF help This option provides support for calculating Diffie-Hellman public keys and shared secrets using values stored as keys
security/keys/Makefile +2 -1
@@ -15,7 +15,8 @@ obj-y := \ request_key.o \ request_key_auth.o \ user_defined.o -obj-$(CONFIG_KEYS_COMPAT) += compat.o +compat-obj-$(CONFIG_KEY_DH_OPERATIONS) += compat_dh.o +obj-$(CONFIG_KEYS_COMPAT) += compat.o $(compat-obj-y) obj-$(CONFIG_PROC_FS) += proc.o obj-$(CONFIG_SYSCTL) += sysctl.o obj-$(CONFIG_PERSISTENT_KEYRINGS) += persistent.o
security/keys/compat.c +3 -2
@@ -133,8 +133,9 @@ COMPAT_SYSCALL_DEFINE5(keyctl, u32, option,return keyctl_get_persistent (arg2, arg3);case KEYCTL_DH_COMPUTE : -return keyctl_dh_compute (compat_ptr (arg2),compat_ptr (arg3), - arg4,compat_ptr (arg5)); +return compat_keyctl_dh_compute (compat_ptr (arg2), +compat_ptr (arg3), + arg4,compat_ptr (arg5));default :return -EOPNOTSUPP ;
security/keys/compat_dh.c new file +36 -0
@@ -0,0 +1,36 @@ +/* 32-bit compatibility syscall for 64-bit systems for DH operations +* +* Copyright (C) 2016 Stephan Mueller <[email protected]> +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version +* 2 of the License, or (at your option) any later version. +*/ + +#include "internal.h" + +/* +* Perform the DH computation or DH based key derivation. +* +* If successful, 0 will be returned. +*/ +long compat_keyctl_dh_compute (struct keyctl_dh_params __user *params, +char __user *buffer,size_t buflen, +struct compat_keyctl_kdf_params __user *kdf) +{ +struct keyctl_kdf_params kdfcopy; +struct compat_keyctl_kdf_params compat_kdfcopy; + +if (!kdf) +return __keyctl_dh_compute (params, buffer, buflen,NULL ); + +if (copy_from_user (&compat_kdfcopy, kdf,sizeof (compat_kdfcopy)) !=0 ) +return -EFAULT ; + + kdfcopy.kdfname =compat_ptr (compat_kdfcopy.kdfname); + kdfcopy.otherinfo =compat_ptr (compat_kdfcopy.otherinfo); + kdfcopy.otherinfolen = compat_kdfcopy.otherinfolen; + +return __keyctl_dh_compute (params, buffer, buflen, &kdfcopy); +}
security/keys/dh.c +98 -12
154 lines not on this page · raw message
security/keys/internal.h +19 -2
@@ -259,15 +259,32 @@ static inline long keyctl_get_persistent(uid_t uid, key_serial_t destring)#endif #ifdef CONFIG_KEY_DH_OPERATIONS +#include <crypto/rng.h> +#include <linux/compat.h> extern long keyctl_dh_compute (struct keyctl_dh_params __user *,char __user *, -size_t ,void __user *); +size_t ,struct keyctl_kdf_params __user *); +extern long __keyctl_dh_compute (struct keyctl_dh_params __user *,char __user *, +size_t ,struct keyctl_kdf_params *); +extern long compat_keyctl_dh_compute (struct keyctl_dh_params __user *params, +char __user *buffer,size_t buflen, +struct compat_keyctl_kdf_params __user *kdf); +#define KEYCTL_KDF_MAX_OUTPUT_LEN 1024/* max length of KDF output */ +#define KEYCTL_KDF_MAX_OI_LEN 64/* max length of otherinfo */ #else static inline long keyctl_dh_compute (struct keyctl_dh_params __user *params,char __user *buffer,size_t buflen, -void __user *reserved) +struct keyctl_kdf_params __user *kdf) {return -EOPNOTSUPP ; } + +static inline long compat_keyctl_dh_compute ( +struct keyctl_dh_params __user *params, +char __user *buffer,size_t buflen, +struct keyctl_kdf_params __user *kdf) +{ + return -EOPNOTSUPP +}#endif /*
security/keys/keyctl.c +1 -1
@@ -1689,7 +1689,7 @@ SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,case KEYCTL_DH_COMPUTE :return keyctl_dh_compute ((struct keyctl_dh_params __user *) arg2, (char __user *) arg3, (size_t ) arg4, - (void __user *) arg5); + (struct keyctl_kdf_params __user *) arg5);default :return -EOPNOTSUPP ;
Signature · 1 line
2.7.4
security/keys/internal.h static inline long keyctl_get_persistent(uid_t uid, key_serial_t destring) · line 263
Quoted 10 lines · Stephan Mueller
security/keys/internal.h +2
-0@@ -259,15 +259,32 @@ static inline long keyctl_get_persistent(uid_t uid, key_serial_t destring)#endif #ifdef CONFIG_KEY_DH_OPERATIONS +#include <crypto/rng.h> +#include <linux/compat.h>
These may belong at the top of the file, even if they are only used when CONFIG_KEY_DH_OPERATIONS is defined.
Quoted 34 lines
security/keys/internal.h +17
-2extern long keyctl_dh_compute (struct keyctl_dh_params __user *,char __user *, -size_t ,void __user *); +size_t ,struct keyctl_kdf_params __user *); +extern long __keyctl_dh_compute (struct keyctl_dh_params __user *,char __user *, +size_t ,struct keyctl_kdf_params *); +extern long compat_keyctl_dh_compute (struct keyctl_dh_params __user *params, +char __user *buffer,size_t buflen, +struct compat_keyctl_kdf_params __user *kdf); +#define KEYCTL_KDF_MAX_OUTPUT_LEN 1024/* max length of KDF output */ +#define KEYCTL_KDF_MAX_OI_LEN 64/* max length of otherinfo */ #else static inline long keyctl_dh_compute (struct keyctl_dh_params __user *params,char __user *buffer,size_t buflen, -void __user *reserved) +struct keyctl_kdf_params __user *kdf) {return -EOPNOTSUPP ; } + +static inline long compat_keyctl_dh_compute ( +struct keyctl_dh_params __user *params, +char __user *buffer,size_t buflen, +struct keyctl_kdf_params __user *kdf) +{ + return -EOPNOTSUPP +}#endif /* security/keys/keyctl.c +0
-0
security/keys/keyctl.c SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3, · line 1695
Quoted 8 lines
security/keys/keyctl.c +1
-1@@ -1689,7 +1689,7 @@ SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,case KEYCTL_DH_COMPUTE :return keyctl_dh_compute ((struct keyctl_dh_params __user *) arg2, (char __user *) arg3, (size_t ) arg4, - (void __user *) arg5); + (struct keyctl_kdf_params __user *) arg5);default :return -EOPNOTSUPP ;
Regards,
--
Mat Martineau
Intel OTC
Am Dienstag, 9. August 2016, 15:48:00 CEST schrieb Mat Martineau:
Hi Mat,
Quoted 8 lines · Stephan Mueller
Quoted 7 lines
security/keys/internal.h +0
-0@@ -259,15 +259,32 @@ static inline long keyctl_get_persistent(uid_t uid,
key_serial_tdestring) #endif
Quoted 6 lines
security/keys/internal.h static inline long keyctl_get_persistent(uid_t uid, key_serial_t destring) · line 263
Quoted 3 lines
#ifdef
CONFIG_KEY_DH_OPERATIONSsecurity/keys/internal.h +2
-0+#include <crypto/rng.h> +#include <linux/compat.h> These may belong at the top of the file, even if they are only used when
CONFIG_KEY_DH_OPERATIONSis defined.
Sure. As I have seen also this coding style in the kernel, I thought it would make sense here too. But I will move it.
Quoted 48 lines
Quoted 48 lines
extern long keyctl_dh_compute(struct
keyctl_dh_params__user*, char__user*, -size_t, void__user*);security/keys/internal.h +2
-0+ size_t, struct keyctl_kdf_params __user *); +extern long __keyctl_dh_compute(struct keyctl_dh_params __user *, char
__user*, +size_t, structkeyctl_kdf_params*);
+extern long compat_keyctl_dh_compute(structkeyctl_dh_params__user
*params, + char__user*buffer,size_tbuflen,security/keys/internal.h +3
-0+ struct compat_keyctl_kdf_params __user *kdf); +#define KEYCTL_KDF_MAX_OUTPUT_LEN 1024/* max length of KDF output */ +#define KEYCTL_KDF_MAX_OI_LEN 64/* max length of otherinfo */ #else
static inline long keyctl_dh_compute(structkeyctl_dh_params__user*params,>
char__user*buffer,size_tbuflen,security/keys/internal.h +1
-1- void __user *reserved) + struct keyctl_kdf_params __user *kdf){
return -EOPNOTSUPP;}
security/keys/internal.h +8
-0+ +static inline long compat_keyctl_dh_compute ( +struct keyctl_dh_params __user *params, +char __user *buffer,size_t buflen, +struct keyctl_kdf_params __user *kdf) +{ + return -EOPNOTSUPP +}#endif
/*
security/keys/keyctl.c +0
-0@@ -1689,7 +1689,7 @@ SYSCALL_DEFINE5(keyctl, int, option, unsigned long,arg2, unsigned long, arg3,>
case KEYCTL_DH_COMPUTE: return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2, (char __user *) arg3, (size_t) arg4,security/keys/keyctl.c +1
-1- (void __user *) arg5); + (struct keyctl_kdf_params __user *) arg5);
Quoted 7 lines
Quoted 2 lines
default: return -EOPNOTSUPP;Regards,
--
Mat Martineau
Intel OTC
Ciao
Stephan
PATCH v4 1/1 KEYS: add SP800-56A KDF support for DH
In reply to Mat MartineauSP800-56A defines the use of DH with key derivation function based on a counter. The input to the KDF is defined as (DH shared secret || other information). The value for the "other information" is to be provided by the caller.
The KDF is provided by the kernel crypto API. The SP800-56A KDF is equal to the SP800-108 counter KDF. However, the caller is allowed to specify the KDF type that he wants to use to derive the key material allowing the use of the other KDFs provided with the kernel crypto API.
As the KDF implements the proper truncation of the DH shared secret to the requested size, this patch fills the caller buffer up to its size.
The patch is tested with a new test added to the keyutils user space code which uses a CAVS test vector testing the compliance with SP800-56A.
--- Documentation/security/keys.txt | 35 ++++++++++--- include/linux/compat.h | 7 +++ include/uapi/linux/keyctl.h | 7 +++ security/keys/Kconfig | 1 + security/keys/Makefile | 3 +- security/keys/compat.c | 5 +- security/keys/compat_dh.c | 36 +++++++++++++ security/keys/dh.c | 110 +++++++++++++++++++++++++++++++++++----- security/keys/internal.h | 21 +++++++- security/keys/keyctl.c | 2 +- 10 files changed, 201 insertions(+), 26 deletions(-) create mode 100644 security/keys/compat_dh.c
Documentation/security/keys.txt +27 -8
@@ -827,7 +827,7 @@ The keyctl syscall functions are: long keyctl(KEYCTL_DH_COMPUTE, struct keyctl_dh_params *params, char *buffer, size_t buflen, - void *reserved); + struct keyctl_kdf_params *kdf); The params struct contains serial numbers for three keys:
@@ -844,18 +844,37 @@ The keyctl syscall functions are: public key. If the base is the remote public key, the result is the shared secret. - The reserved argument must be set to NULL. + If the parameter kdf is NULL, the following applies: - The buffer length must be at least the length of the prime, or zero. + - The buffer length must be at least the length of the prime, or zero. - If the buffer length is nonzero, the length of the result is - returned when it is successfully calculated and copied in to the - buffer. When the buffer length is zero, the minimum required - buffer length is returned. + - If the buffer length is nonzero, the length of the result is + returned when it is successfully calculated and copied in to the + buffer. When the buffer length is zero, the minimum required + buffer length is returned. + + The kdf parameter allows the caller to apply a key derivation function + (KDF) on the Diffie-Hellman computation where only the result + of the KDF is returned to the caller. The KDF is characterized with + struct keyctl_kdf_params as follows: + + - char *kdfname specifies the NUL terminated string identifying + the KDF function used from the kernel crypto API. As of now, + only non-keyed KDFs are supported, such as kdf_ctr(sha256), + kdf_fb(sha1) or kdf_dpi(sha512). The use of kdf_ctr() complies + with SP800-56A. + + - char *otherinfo specifies the OtherInfo data as documented in + SP800-56A section 5.8.1.2. The length of the buffer is given with + otherinfolen. The format of OtherInfo is defined by the caller. + The otherinfo pointer may be NULL if no OtherInfo shall be used. This function will return error EOPNOTSUPP if the key type is not supported, error ENOKEY if the key could not be found, or error - EACCES if the key is not readable by the caller. + EACCES if the key is not readable by the caller. In addition, the + function will return EMSGSIZE when the parameter kdf is non-NULL + and either the buffer length or the OtherInfo length exceeds the + allowed length. =============== KERNEL SERVICES
include/linux/compat.h +7 -0
@@ -295,6 +295,13 @@ struct compat_old_sigaction { };#endif +struct compat_keyctl_kdf_params { +compat_uptr_t kdfname; +compat_uptr_t otherinfo; +__u32 otherinfolen; +__u32 __spare[8 ]; +}; +struct compat_statfs ;struct compat_statfs64 ;struct compat_old_linux_dirent ;
include/uapi/linux/keyctl.h +7 -0
@@ -68,4 +68,11 @@ struct keyctl_dh_params { __s32 base; }; +struct keyctl_kdf_params { +char *kdfname; +char *otherinfo; +__u32 otherinfolen; +__u32 __spare[8 ]; +}; +#endif /* _LINUX_KEYCTL_H */
security/keys/Kconfig +1 -0
@@ -90,6 +90,7 @@ config KEY_DH_OPERATIONS bool "Diffie-Hellman operations on retained keys" depends on KEYS select MPILIB + select CRYPTO_KDF help This option provides support for calculating Diffie-Hellman public keys and shared secrets using values stored as keys
security/keys/Makefile +2 -1
@@ -15,7 +15,8 @@ obj-y := \ request_key.o \ request_key_auth.o \ user_defined.o -obj-$(CONFIG_KEYS_COMPAT) += compat.o +compat-obj-$(CONFIG_KEY_DH_OPERATIONS) += compat_dh.o +obj-$(CONFIG_KEYS_COMPAT) += compat.o $(compat-obj-y) obj-$(CONFIG_PROC_FS) += proc.o obj-$(CONFIG_SYSCTL) += sysctl.o obj-$(CONFIG_PERSISTENT_KEYRINGS) += persistent.o
security/keys/compat.c +3 -2
@@ -133,8 +133,9 @@ COMPAT_SYSCALL_DEFINE5(keyctl, u32, option,return keyctl_get_persistent (arg2, arg3);case KEYCTL_DH_COMPUTE : -return keyctl_dh_compute (compat_ptr (arg2),compat_ptr (arg3), - arg4,compat_ptr (arg5)); +return compat_keyctl_dh_compute (compat_ptr (arg2), +compat_ptr (arg3), + arg4,compat_ptr (arg5));default :return -EOPNOTSUPP ;
security/keys/compat_dh.c new file +36 -0
@@ -0,0 +1,36 @@ +/* 32-bit compatibility syscall for 64-bit systems for DH operations +* +* Copyright (C) 2016 Stephan Mueller <[email protected]> +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version +* 2 of the License, or (at your option) any later version. +*/ + +#include "internal.h" + +/* +* Perform the DH computation or DH based key derivation. +* +* If successful, 0 will be returned. +*/ +long compat_keyctl_dh_compute (struct keyctl_dh_params __user *params, +char __user *buffer,size_t buflen, +struct compat_keyctl_kdf_params __user *kdf) +{ +struct keyctl_kdf_params kdfcopy; +struct compat_keyctl_kdf_params compat_kdfcopy; + +if (!kdf) +return __keyctl_dh_compute (params, buffer, buflen,NULL ); + +if (copy_from_user (&compat_kdfcopy, kdf,sizeof (compat_kdfcopy)) !=0 ) +return -EFAULT ; + + kdfcopy.kdfname =compat_ptr (compat_kdfcopy.kdfname); + kdfcopy.otherinfo =compat_ptr (compat_kdfcopy.otherinfo); + kdfcopy.otherinfolen = compat_kdfcopy.otherinfolen; + +return __keyctl_dh_compute (params, buffer, buflen, &kdfcopy); +}
security/keys/dh.c +98 -12
154 lines not on this page · raw message
security/keys/internal.h +19 -2
@@ -16,6 +16,8 @@#include <linux/key-type.h> #include <linux/task_work.h> #include <linux/keyctl.h> +#include <crypto/rng.h> +#include <linux/compat.h> struct iovec ;
@@ -260,14 +262,29 @@ static inline long keyctl_get_persistent(uid_t uid, key_serial_t destring)#ifdef CONFIG_KEY_DH_OPERATIONS extern long keyctl_dh_compute (struct keyctl_dh_params __user *,char __user *, -size_t ,void __user *); +size_t ,struct keyctl_kdf_params __user *); +extern long __keyctl_dh_compute (struct keyctl_dh_params __user *,char __user *, +size_t ,struct keyctl_kdf_params *); +extern long compat_keyctl_dh_compute (struct keyctl_dh_params __user *params, +char __user *buffer,size_t buflen, +struct compat_keyctl_kdf_params __user *kdf); +#define KEYCTL_KDF_MAX_OUTPUT_LEN 1024/* max length of KDF output */ +#define KEYCTL_KDF_MAX_OI_LEN 64/* max length of otherinfo */ #else static inline long keyctl_dh_compute (struct keyctl_dh_params __user *params,char __user *buffer,size_t buflen, -void __user *reserved) +struct keyctl_kdf_params __user *kdf) {return -EOPNOTSUPP ; } + +static inline long compat_keyctl_dh_compute ( +struct keyctl_dh_params __user *params, +char __user *buffer,size_t buflen, +struct keyctl_kdf_params __user *kdf) +{ + return -EOPNOTSUPP +}#endif /*
security/keys/keyctl.c +1 -1
@@ -1689,7 +1689,7 @@ SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,case KEYCTL_DH_COMPUTE :return keyctl_dh_compute ((struct keyctl_dh_params __user *) arg2, (char __user *) arg3, (size_t ) arg4, - (void __user *) arg5); + (struct keyctl_kdf_params __user *) arg5);default :return -EOPNOTSUPP ;
Signature · 1 line
2.7.4