Skip to contentSkip to conversation

KEYS: add SP800-56A KDF support for DH

PATCH In review Stephan Mueller 2 participants 8 messages Aug 19, 2016 – Apr 3, 2017

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 KEYS: add SP800-56A KDF support for DH

Hi,

This patch now folds the KDF into the keys support as requested by Herbert. The caller can only supply the hash name used for the KDF.

Note, the KDF implementation is identical to the kdf_ctr() support in the now unneeded KDF patches to the kernel crypto API.

The new patch also changes the variable name from kdfname to hashname.

Also, the patch adds a missing semicolon.

Finally, the patch adds a guard against compiling the compat code if the general Linux kernel configuration does not have the compat code enabled. Without that guard, compilation warnings are seen.

---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 implemented using the hash support from the kernel crypto API. The implementation uses the symmetric hash support as the input to the hash operation is usually very small. The caller is allowed to specify the hash name that he wants to use to derive the key material allowing the use of all supported hashes 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 |  34 ++++--
 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       |  38 +++++++
 security/keys/dh.c              | 222 +++++++++++++++++++++++++++++++++++++---
 security/keys/internal.h        |  24 ++++-
 security/keys/keyctl.c          |   2 +-
 10 files changed, 317 insertions(+), 26 deletions(-)
 create mode 100644 security/keys/compat_dh.c
Documentation/security/keys.txt +26 -8
index 3849814..58fcae7 100644
@@ -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,36 @@ 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 *hashname specifies the NUL terminated string identifying
+	   the hash used from the kernel crypto API and applied for the KDF
+	   operation. The KDF implemenation complies with SP800-56A as well
+	   as with SP800-108 (the counter KDF).
+
+	 - 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
index f964ef7..f6979d5 100644
@@ -295,6 +295,13 @@ struct compat_old_sigaction {
 };
 #endif
 
+struct compat_keyctl_kdf_params {
+	compat_uptr_t hashname;
+	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
index 86eddd6..2102822 100644
@@ -68,4 +68,11 @@ struct keyctl_dh_params {
 	__s32 base;
 };
 
+struct keyctl_kdf_params {
+	char *hashname;
+	char *otherinfo;
+	__u32 otherinfolen;
+	__u32 __spare[8];
+};
+
 #endif /*  _LINUX_KEYCTL_H */
security/keys/Kconfig +1 -0
index f826e87..00723a8 100644
@@ -90,6 +90,7 @@ config KEY_DH_OPERATIONS
        bool "Diffie-Hellman operations on retained keys"
        depends on KEYS
        select MPILIB
+       select CRYPTO_HASH
        help
 	 This option provides support for calculating Diffie-Hellman
 	 public keys and shared secrets using values stored as keys
security/keys/Makefile +2 -1
index 1fd4a16..57dff0c 100644
@@ -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
index 36c80bf..b674886 100644
@@ -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 +38 -0
new file mode 100644
index 0000000..a6a659b
@@ -0,0 +1,38 @@
+/* 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 <linux/uaccess.h>
+
+#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.hashname = compat_ptr(compat_kdfcopy.hashname);
+	kdfcopy.otherinfo = compat_ptr(compat_kdfcopy.otherinfo);
+	kdfcopy.otherinfolen = compat_kdfcopy.otherinfolen;
+
+	return __keyctl_dh_compute(params, buffer, buflen, &kdfcopy);
+}
security/keys/dh.c +210 -12

272 lines not on this page · raw message

security/keys/internal.h +22 -2
index a705a7d..c0f1adf 100644
@@ -16,6 +16,7 @@
 #include <linux/key-type.h>
 #include <linux/task_work.h>
 #include <linux/keyctl.h>
+#include <linux/compat.h>
 
 struct iovec;
 
@@ -260,15 +261,34 @@ 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 *);
+#ifdef CONFIG_KEYS_COMPAT
+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);
+#endif
+#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;
+}
+
+#ifdef CONFIG_KEYS_COMPAT
+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
+#endif
 
 /*
  * Debugging key validation
security/keys/keyctl.c +1 -1
index d580ad0..b106898 100644
@@ -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
#1 lore raw

PATCH v5 1/1 DH support: add KDF handling support

Hi,

this patch changes the documentation, the naming of the variables and the test case to refer to the variable name of a hashname instead of kdfname to match the current kernel implementation.

Ciao
Stephan

---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> <hash_type>

- KDF support with "other information string:
dh_compute_kdf_oi <private> <prime> <base> <output length> <hash_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                  |  57 +++++++++++
 tests/keyctl/dh_compute/valid/runtest.sh | 168 +++++++++++++++++++++++++++++++
 tests/toolbox.inc.sh                     |  44 ++++++++
 version.lds                              |   2 +
 8 files changed, 430 insertions(+)
Makefile +1 -0
index 824bbbf..90fc33f 100644
@@ -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
index 2a69304..baff8cb 100644
@@ -244,6 +244,20 @@ long keyctl_dh_compute(key_serial_t private, key_serial_t prime,
 	return keyctl(KEYCTL_DH_COMPUTE, &params, buffer, buflen, 0);
 }
 
+long keyctl_dh_compute_kdf(key_serial_t private, key_serial_t prime,
+			   key_serial_t base, char *hashname, 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 = { .hashname = hashname,
+					       .otherinfo = otherinfo,
+					       .otherinfolen = otherinfolen };
+
+	return keyctl(KEYCTL_DH_COMPUTE, &params, buffer, buflen, &kdfparams);
+}
+
 /*****************************************************************************/
 /*
  * fetch key description into an allocated buffer
keyutils.h +11 -0
index b321aa8..19b66b5 100644
@@ -108,6 +108,13 @@ struct keyctl_dh_params {
 	key_serial_t base;
 };
 
+struct keyctl_kdf_params {
+	char *hashname;
+	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 *hashname,
+				  char *otherinfo, size_t otherinfolen,
+				  char *buffer, size_t buflen);
 
 /*
  * utilities
man/keyctl_dh_compute.3 +57 -0

81 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
index 2bfed13..b8eebfb 100644
@@ -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
maintainer
#2 lore raw

Re: [PATCH v5] DH support: add KDF handling support

keyutils.c long keyctl_dh_compute(key_serial_t private, key_serial_t prime, · line 251

Quoted 1 line · Stephan Mueller
keyutils.c +1 -0
+	struct keyctl_dh_params params = { .private = private,

That doesn't compile. I think you meant ".priv".

David

#3 lore raw

Re: [PATCH v5] DH support: add KDF handling support

Am Dienstag, 4. April 2017, 00:22:28 CEST schrieb David Howells:

Hi David,

Quoted 4 lines

keyutils.c long keyctl_dh_compute(key_serial_t private, key_serial_t prime, · line 251

Quoted 1 line · Stephan Mueller
keyutils.c +1 -0
+	struct keyctl_dh_params params = { .private = private,

That doesn't compile. I think you meant ".priv".

I think some code has been changed since I prepared the patch. Allow me to port the patch to the current code tree.

I will also update the keyctl.1 man page.

Ciao
Stephan

maintainer
#4 lore raw

Re: [PATCH v5] DH support: add KDF handling support

In reply to Stephan Mueller
Quoted 3 lines · Stephan Mueller

this patch changes the documentation, the naming of the variables and the test case to refer to the variable name of a hashname instead of kdfname to match the current kernel implementation.

It's also needs an update to man1/keyctl.1.

David

#5 lore raw
In reply to Stephan Mueller

Am Freitag, 19. August 2016, 20:39:09 CEST schrieb Stephan Mueller:

Hi David,

Quoted 12 lines

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 implemented using the hash support from the kernel crypto API. The implementation uses the symmetric hash support as the input to the hash operation is usually very small. The caller is allowed to specify the hash name that he wants to use to derive the key material allowing the use of all supported hashes provided with the kernel crypto API.

Quoted 6 lines

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.

Is there a decision about this patch set?

Ciao
Stephan

#6 lore raw
In reply to Stephan Mueller

Am Freitag, 19. August 2016, 20:39:09 CET schrieb Stephan Mueller:

Hi David,

Quoted 8 lines

Hi,

This patch now folds the KDF into the keys support as requested by Herbert. The caller can only supply the hash name used for the KDF.

Note, the KDF implementation is identical to the kdf_ctr() support in the now unneeded KDF patches to the kernel crypto API.

Quoted 7 lines

The new patch also changes the variable name from kdfname to hashname.

Also, the patch adds a missing semicolon.

Finally, the patch adds a guard against compiling the compat code if the general Linux kernel configuration does not have the compat code enabled. Without that guard, compilation warnings are seen.

May I ask which plans do you have with the KDF support for DH?

Ciao
Stephan