Skip to contentSkip to conversation

crypto: AF_ALG support for KPP

RFC PATCH In review Stephan Müller 3 participants 14 messages Apr 18, 2017 – Aug 28, 2017

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

cover crypto: AF_ALG support for KPP

Hi,

This is an RFC to discuss whether and how to support access to the KPP kernel crypto API (Diffie-Hellman and EC Diffie-Hellman) by user space.

The patch set is only meant for discussion and not for production use. Testing is only performed for the DH part via [1]. The DH testing shows that the DH operation shows the same results as OpenSSL.

The patch set includes:

- addition of PKCS#3 support for DH to allow domain parameters to be set

- extension of the setsockopt and cmsg API

- addition of AF_ALG KPP interface where the implementation follows the exact same coding style as currently discussed for the memory management fix patch set for algif_skcipher and algif_aead -- this shall allow the immediate incorporation of patches that replace duplicated code with common service functions.

The patch 8 describes the different operations that are supported by AF_ALG KPP. This support includes generation and retaining of the private key inside the kernel. This private key would never be sent to user space.

I am aware and in fact supported development of the DH support in the keys subsystem. The question will be raised whether the AF_ALG KPP interface is superfluous in light of the keys DH support. My answer is that AF_ALG KPP is orthogonal to the keys DH support. The keys DH support use case is that the keys are managed inside the kernel and thus has the focus on the key management. Contrary, AF_ALG KPP does not really focus on key management but simply externalizes the DH/ECDH support found in the kernel including hardware acceleration. User space is in full control of the key life cycle. This way, AF_ALG could be used to complement user-space network protocol implementations like TLS or IKE to offload the resource intense DH calculations to accelerators.

A full description of the DH testing and how to compare it to OpenSSL is present in [1] with the test/kcapi-main.c tool, function "kpp".

[1] https://github.com/smuellerDD/libkcapi/tree/kpp

Stephan Mueller (8):
crypto: AF_ALG -- add DH keygen / ssgen API
crypto: AF_ALG -- add DH param / ECDH curve setsockopt call
crypto: AF_ALG - eliminate code duplication
crypto: KPP - add API crypto_kpp_set_params
crypto: DH - allow params and key to be set independently
crypto: DH - add PKCS#3 parameter handling
crypto: ecdh - constify key
crypto: AF_ALG - add KPP support

 Documentation/crypto/api-kpp.rst |    2 +-
 crypto/Kconfig                   |   10 +
 crypto/Makefile                  |    8 +-
 crypto/af_alg.c                  |   17 +-
 crypto/algif_kpp.c               | 1230 ++++++++++++++++++++++++++++++++++++++
 crypto/dh.c                      |   21 +-
 crypto/dh_helper.c               |   53 +-
 crypto/dhparameter.asn1          |    4 +
 include/crypto/dh.h              |   21 +-
 include/crypto/ecdh.h            |    2 +-
 include/crypto/if_alg.h          |    2 +
 include/crypto/kpp.h             |   28 +
 include/uapi/linux/if_alg.h      |    4 +
 13 files changed, 1384 insertions(+), 18 deletions(-)
 create mode 100644 crypto/algif_kpp.c
 create mode 100644 crypto/dhparameter.asn1
Signature · 1 line
2.9.3
#1 lore raw

PATCH 1/8 crypto: AF_ALG -- add DH keygen / ssgen API

Add the flags for handling DH key generation and DH shared secret generation.

---
 include/uapi/linux/if_alg.h | 2 ++
 1 file changed, 2 insertions(+)
include/uapi/linux/if_alg.h +2 -0
index 02e6162..c48eeb6 100644
@@ -41,5 +41,7 @@ struct af_alg_iv {
 #define ALG_OP_ENCRYPT			1
 #define ALG_OP_SIGN			2
 #define ALG_OP_VERIFY			3
+#define ALG_OP_KEYGEN			4
+#define ALG_OP_SSGEN			5
 
 #endif	/* _LINUX_IF_ALG_H */
Signature · 1 line
2.9.3
#2 lore raw

PATCH 2/8 crypto: AF_ALG -- add DH param / ECDH curve setsockopt call

In reply to Stephan Müller

For supporting DH ciphers, user space must be able to set the DH parameters. The patch adds a new setsockopt call for setting these parameters.

Similarly, the ECDH curve information can be set by user space via the newly added setsockopt call.

---
 crypto/af_alg.c             | 12 ++++++++++++
 include/crypto/if_alg.h     |  2 ++
 include/uapi/linux/if_alg.h |  2 ++
 3 files changed, 16 insertions(+)
crypto/af_alg.c +12 -0
index 0a91404..a3210bf 100644
@@ -260,6 +260,18 @@ static int alg_setsockopt(struct socket *sock, int level, int optname,
 
 		err = alg_setkey(sk, optval, optlen, type->setpubkey);
 		break;
+	case ALG_SET_DH_PARAMETERS:
+		if (sock->state == SS_CONNECTED)
+			goto unlock;
+
+		err = alg_setkey(sk, optval, optlen, type->dhparams);
+		break;
+	case ALG_SET_ECDH_CURVE:
+		if (sock->state == SS_CONNECTED)
+			goto unlock;
+
+		err = alg_setkey(sk, optval, optlen, type->ecdhcurve);
+		break;
 	case ALG_SET_AEAD_AUTHSIZE:
 		if (sock->state == SS_CONNECTED)
 			goto unlock;
include/crypto/if_alg.h +2 -0
index 3c3ebe3..52dcefe 100644
@@ -53,6 +53,8 @@ struct af_alg_type {
 	void (*release)(void *private);
 	int (*setkey)(void *private, const u8 *key, unsigned int keylen);
 	int (*setpubkey)(void *private, const u8 *key, unsigned int keylen);
+	int (*dhparams)(void *private, const u8 *param, unsigned int paramlen);
+	int (*ecdhcurve)(void *private, const u8 *param, unsigned int paramlen);
 	int (*accept)(void *private, struct sock *sk);
 	int (*accept_nokey)(void *private, struct sock *sk);
 	int (*setauthsize)(void *private, unsigned int authsize);
include/uapi/linux/if_alg.h +2 -0
index c48eeb6..fc9ed9f 100644
@@ -35,6 +35,8 @@ struct af_alg_iv {
 #define ALG_SET_AEAD_ASSOCLEN		4
 #define ALG_SET_AEAD_AUTHSIZE		5
 #define ALG_SET_PUBKEY			6
+#define ALG_SET_DH_PARAMETERS		7
+#define ALG_SET_ECDH_CURVE		8
 
 /* Operations */
 #define ALG_OP_DECRYPT			0
Signature · 1 line
2.9.3
#3 lore raw

PATCH 3/8 crypto: AF_ALG - eliminate code duplication

In reply to Stephan Müller

The handling function for setsockopt contains duplicated code which is cleaned up with this patch. This patch does not change the functionality.

---
 crypto/af_alg.c | 17 +++--------------
 1 file changed, 3 insertions(+), 14 deletions(-)
crypto/af_alg.c +3 -14
index a3210bf..f2b2865 100644
@@ -247,34 +247,23 @@ static int alg_setsockopt(struct socket *sock, int level, int optname,
 	if (level != SOL_ALG || !type)
 		goto unlock;
 
+	if (sock->state == SS_CONNECTED)
+		goto unlock;
+
 	switch (optname) {
 	case ALG_SET_KEY:
-		if (sock->state == SS_CONNECTED)
-			goto unlock;
-
 		err = alg_setkey(sk, optval, optlen, type->setkey);
 		break;
 	case ALG_SET_PUBKEY:
-		if (sock->state == SS_CONNECTED)
-			goto unlock;
-
 		err = alg_setkey(sk, optval, optlen, type->setpubkey);
 		break;
 	case ALG_SET_DH_PARAMETERS:
-		if (sock->state == SS_CONNECTED)
-			goto unlock;
-
 		err = alg_setkey(sk, optval, optlen, type->dhparams);
 		break;
 	case ALG_SET_ECDH_CURVE:
-		if (sock->state == SS_CONNECTED)
-			goto unlock;
-
 		err = alg_setkey(sk, optval, optlen, type->ecdhcurve);
 		break;
 	case ALG_SET_AEAD_AUTHSIZE:
-		if (sock->state == SS_CONNECTED)
-			goto unlock;
 		if (!type->setauthsize)
 			goto unlock;
 		err = type->setauthsize(ask->private, optlen);
Signature · 1 line
2.9.3
#4 lore raw

PATCH 4/8 crypto: KPP - add API crypto_kpp_set_params

In reply to Stephan Müller

KPP mechanisms like DH require a parameter set to be provided by the caller. That parameter set may be provided by the crypto_kpp_set_secret function. Yet, the parameters hare handled independently from the secret key which implies that they should be able to be set independently from the key.

The new API allows KPP mechanisms to register a callback allowing to set such parameters.

---
 Documentation/crypto/api-kpp.rst |  2 +-
 include/crypto/kpp.h             | 28 ++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)
Documentation/crypto/api-kpp.rst +1 -1
index 7d86ab9..7b2c0d4 100644
@@ -11,7 +11,7 @@ Key-agreement Protocol Primitives (KPP) Cipher API
    :doc: Generic Key-agreement Protocol Primitives API
 
 .. kernel-doc:: include/crypto/kpp.h
-   :functions: crypto_alloc_kpp crypto_free_kpp crypto_kpp_set_secret crypto_kpp_generate_public_key crypto_kpp_compute_shared_secret crypto_kpp_maxsize
+   :functions: crypto_alloc_kpp crypto_free_kpp crypto_kpp_set_params crypto_kpp_set_secret crypto_kpp_generate_public_key crypto_kpp_compute_shared_secret crypto_kpp_maxsize
 
 Key-agreement Protocol Primitives (KPP) Cipher Request Handle
 -------------------------------------------------------------
include/crypto/kpp.h +28 -0
index ce8e1f7..5931364 100644
@@ -51,6 +51,9 @@ struct crypto_kpp {
 /**
  * struct kpp_alg - generic key-agreement protocol primitives
  *
+ * @set_params:	Function allows the caller to set the parameters
+ *			separately from the key. The format of the parameters
+ *			is protocol specific.
  * @set_secret:		Function invokes the protocol specific function to
  *			store the secret private key along with parameters.
  *			The implementation knows how to decode thie buffer
@@ -74,6 +77,8 @@ struct crypto_kpp {
  * @base:		Common crypto API algorithm data structure
  */
 struct kpp_alg {
+	int (*set_params)(struct crypto_kpp *tfm, const void *buffer,
+			  unsigned int len);
 	int (*set_secret)(struct crypto_kpp *tfm, const void *buffer,
 			  unsigned int len);
 	int (*generate_public_key)(struct kpp_request *req);
@@ -259,6 +264,29 @@ struct kpp_secret {
 };
 
 /**
+ * crypto_kpp_set_params() - Set parameters needed for kpp operation
+ *
+ * Function invokes the specific kpp operation for a given alg.
+ *
+ * @tfm:	tfm handle
+ * @buffer:	Buffer holding the protocol specific representation of the
+ *		parameters (e.g. PKCS#3 DER for DH)
+ * @len:	Length of the parameter buffer.
+ *
+ * Return: zero on success; error code in case of error
+ */
+static inline int crypto_kpp_set_params(struct crypto_kpp *tfm,
+				        const void *buffer, unsigned int len)
+{
+	struct kpp_alg *alg = crypto_kpp_alg(tfm);
+
+	if (alg->set_params)
+		return alg->set_params(tfm, buffer, len);
+	else
+		return -EOPNOTSUPP;
+}
+
+/**
  * crypto_kpp_set_secret() - Invoke kpp operation
  *
  * Function invokes the specific kpp operation for a given alg.
Signature · 1 line
2.9.3
#5 lore raw

RE: [PATCH 4/8] crypto: KPP - add API crypto_kpp_set_params

Hi Stephan,

Quoted 94 lines

-----Original Message-----
From: [email protected] [mailto:keyrings- [email protected]] On Behalf Of Stephan Müller
Sent: Wednesday, April 19, 2017 12:06 AM
To: [email protected]
Cc: [email protected]
Subject: [PATCH 4/8] crypto: KPP - add API crypto_kpp_set_params

KPP mechanisms like DH require a parameter set to be provided by the caller. That parameter set may be provided by the crypto_kpp_set_secret function. Yet, the parameters hare handled independently from the secret key which implies that they should be able to be set independently from the key.

The new API allows KPP mechanisms to register a callback allowing to set such parameters.

---
 Documentation/crypto/api-kpp.rst |  2 +-
 include/crypto/kpp.h             | 28 ++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)
Documentation/crypto/api- renamed from Documentation/crypto/api-kpp.rst +0 -0

kpp.rst
index 7d86ab9..7b2c0d4 100644

Documentation/crypto/api-kpp.rst +0 -1
@@ -11,7 +11,7 @@ Key-agreement Protocol Primitives (KPP) Cipher API
    :doc: Generic Key-agreement Protocol Primitives API
 
 .. kernel-doc:: include/crypto/kpp.h
-   :functions: crypto_alloc_kpp crypto_free_kpp crypto_kpp_set_secret

crypto_kpp_generate_public_key crypto_kpp_compute_shared_secret crypto_kpp_maxsize

Documentation/crypto/api-kpp.rst +3 -0
+   :functions: crypto_alloc_kpp crypto_free_kpp crypto_kpp_set_params
+ crypto_kpp_set_secret crypto_kpp_generate_public_key
+ crypto_kpp_compute_shared_secret crypto_kpp_maxsize
 
 Key-agreement Protocol Primitives (KPP) Cipher Request Handle
 -------------------------------------------------------------
include/crypto/kpp.h index renamed from include/crypto/kpp.h +0 -0

ce8e1f7..5931364 100644

include/crypto/kpp.h +2 -0
@@ -51,6 +51,9 @@ struct crypto_kpp {
 /**
  * struct kpp_alg - generic key-agreement protocol primitives
  *
+ * @set_params:	Function allows the caller to set the parameters
+ *			separately from the key. The format of the

parameters
+ * is protocol specific.
* @set_secret: Function invokes the protocol specific
function to
* store the secret private key along with parameters.
* The implementation knows how to decode thie
buffer

include/crypto/kpp.h +24 -1
@@ -74,6 +77,8 @@ struct crypto_kpp {
  * @base:		Common crypto API algorithm data structure
  */
 struct kpp_alg {
+	int (*set_params)(struct crypto_kpp *tfm, const void *buffer,
+			  unsigned int len);
 	int (*set_secret)(struct crypto_kpp *tfm, const void *buffer,
 			  unsigned int len);
 	int (*generate_public_key)(struct kpp_request *req); @@ -259,6
+264,29 @@ struct kpp_secret {  };
 
 /**
+ * crypto_kpp_set_params() - Set parameters needed for kpp operation
+ *
+ * Function invokes the specific kpp operation for a given alg.
+ *
+ * @tfm:	tfm handle
+ * @buffer:	Buffer holding the protocol specific representation of the
+ *		parameters (e.g. PKCS#3 DER for DH)
+ * @len:	Length of the parameter buffer.
+ *
+ * Return: zero on success; error code in case of error  */ static
+inline int crypto_kpp_set_params(struct crypto_kpp *tfm,
+				        const void *buffer, unsigned int len) {
+	struct kpp_alg *alg = crypto_kpp_alg(tfm);
+
+	if (alg->set_params)
+		return alg->set_params(tfm, buffer, len);
+	else
+		return -EOPNOTSUPP;
+}
+
+/**
  * crypto_kpp_set_secret() - Invoke kpp operation
  *
  * Function invokes the specific kpp operation for a given alg.
--
Quoted 1 line

2.9.3

I'm not really in favor of having two ways for setting the params. As you are probably aware, PKCS3 and set_params was my intial approach, but then Herbert suggested a lighter approach like rtnetlink which I actually prefer.

Can't you expose that through AF_ALG?

Regards,
Salvatore

#6 lore raw

Re: [PATCH 4/8] crypto: KPP - add API crypto_kpp_set_params

Am Mittwoch, 19. April 2017, 16:51:54 CEST schrieb Benedetto, Salvatore:

Hi Salvatore,

Quoted 113 lines

Hi Stephan,

Quoted 111 lines

-----Original Message-----
From: [email protected] [mailto:keyrings- [email protected]] On Behalf Of Stephan Müller
Sent: Wednesday, April 19, 2017 12:06 AM
To: [email protected]
Cc: [email protected]
Subject: [PATCH 4/8] crypto: KPP - add API crypto_kpp_set_params

KPP mechanisms like DH require a parameter set to be provided by the caller. That parameter set may be provided by the crypto_kpp_set_secret function. Yet, the parameters hare handled independently from the secret key which implies that they should be able to be set independently from the key.

The new API allows KPP mechanisms to register a callback allowing to set such parameters.

---

 Documentation/crypto/api-kpp.rst |  2 +-
 include/crypto/kpp.h             | 28 ++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)
Documentation/crypto/api- renamed from Documentation/crypto/api-kpp.rst +0 -0

kpp.rst
index 7d86ab9..7b2c0d4 100644

Documentation/crypto/api-kpp.rst +0 -1
@@ -11,7 +11,7 @@ Key-agreement Protocol Primitives (KPP) Cipher API
 
    :doc: Generic Key-agreement Protocol Primitives API
 
 .. kernel-doc:: include/crypto/kpp.h
 
-   :functions: crypto_alloc_kpp crypto_free_kpp crypto_kpp_set_secret

crypto_kpp_generate_public_key crypto_kpp_compute_shared_secret crypto_kpp_maxsize

Documentation/crypto/api-kpp.rst +3 -0
+   :functions: crypto_alloc_kpp crypto_free_kpp crypto_kpp_set_params
+ crypto_kpp_set_secret crypto_kpp_generate_public_key
+ crypto_kpp_compute_shared_secret crypto_kpp_maxsize
 
 Key-agreement Protocol Primitives (KPP) Cipher Request Handle
 -------------------------------------------------------------
include/crypto/kpp.h index renamed from include/crypto/kpp.h +0 -0

ce8e1f7..5931364 100644

include/crypto/kpp.h +2 -0
@@ -51,6 +51,9 @@ struct crypto_kpp {
 
 /**
 
  * struct kpp_alg - generic key-agreement protocol primitives
  *
 
+ * @set_params:	Function allows the caller to set the parameters
+ *			separately from the key. The format of the

parameters
+ * is protocol specific.

* @set_secret: Function invokes the protocol specific

function to

* store the secret private key along with parameters.
* The implementation knows how to decode thie

buffer

include/crypto/kpp.h +2 -0
@@ -74,6 +77,8 @@ struct crypto_kpp {
 
  * @base:		Common crypto API algorithm data structure
  */
 
 struct kpp_alg {
 
+	int (*set_params)(struct crypto_kpp *tfm, const void *buffer,
+			  unsigned int len);
include/crypto/kpp.h +22 -0
 	int (*set_secret)(struct crypto_kpp *tfm, const void *buffer,
 
 			  unsigned int len);
 
 	int (*generate_public_key)(struct kpp_request *req); @@ -259,6
 
+264,29 @@ struct kpp_secret {  };
 
 /**
 
+ * crypto_kpp_set_params() - Set parameters needed for kpp operation
+ *
+ * Function invokes the specific kpp operation for a given alg.
+ *
+ * @tfm:	tfm handle
+ * @buffer:	Buffer holding the protocol specific representation of the
+ *		parameters (e.g. PKCS#3 DER for DH)
+ * @len:	Length of the parameter buffer.
+ *
+ * Return: zero on success; error code in case of error  */ static
+inline int crypto_kpp_set_params(struct crypto_kpp *tfm,
+				        const void *buffer, unsigned int len) {
+	struct kpp_alg *alg = crypto_kpp_alg(tfm);
+
+	if (alg->set_params)
+		return alg->set_params(tfm, buffer, len);
+	else
+		return -EOPNOTSUPP;
+}
+
+/**
Quoted 8 lines
Quoted 6 lines

* crypto_kpp_set_secret() - Invoke kpp operation
*
* Function invokes the specific kpp operation for a given alg.

--
2.9.3

I'm not really in favor of having two ways for setting the params.

I am in full agreement. But setting the key together with the parameters is not good, IMHO. Every other crypto lib implements a separate setting.

Quoted 3 lines

As you are probably aware, PKCS3 and set_params was my intial approach, but then Herbert suggested a lighter approach like rtnetlink which I actually prefer.

I was not aware of that. Considering that PKCS#3 or X9.42 are the common formats for setting parameters, I am not in favor of "inventing" a special format just for the kernel user space interface. If we would have such special format, user space would need to add a conversion step from a common to the kernel-special format.

You see with https://github.com/smuellerDD/libkcapi/blob/kpp/test/kcapi-main.c
starting at line 2500 a full description how the OpenSSL-generated DH parameters can be used directly by the AF_ALG interface.

I have no concern to use rtnetlink for in-kernel use cases.

Quoted 2 lines

Can't you expose that through AF_ALG?

As mentioned, exposing rtnetlink means that we have to have an ASN.1-based converter from PKCS#3 to rtnetlink. As the kernel already has an ASN.1 parser and the additional code to use PKCS#3 in the kernel is very minimal compared to adding a ASN.1 parser in user space, I opted for implementing PKCS#3 in kernel space.

Quoted 3 lines

Regards,
Salvatore

Ciao
Stephan

#7 lore raw

PATCH 5/8 crypto: DH - allow params and key to be set independently

In reply to Stephan Müller

Parameters are handled independently from the secret key. Therefore, this patch allows setting of the parameter independently from the secret key. Before invoking the actual crypto operation, the code must now check that the secret key and the parameters are all present.

---
 crypto/dh.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
crypto/dh.c +5 -1
index 87e3542..f7be48e 100644
@@ -60,6 +60,10 @@ static int dh_check_params_length(unsigned int p_len)
 
 static int dh_set_params(struct dh_ctx *ctx, struct dh *params)
 {
+	/* If DH parameters are not given, do not check them. */
+	if (!params->p_size && !params->g_size)
+		return 0;
+
 	if (unlikely(!params->p || !params->g))
 		return -EINVAL;
 
@@ -111,7 +115,7 @@ static int dh_compute_value(struct kpp_request *req)
 	if (!val)
 		return -ENOMEM;
 
-	if (unlikely(!ctx->xa)) {
+	if (unlikely(!ctx->xa || !ctx->p || !ctx->g)) {
 		ret = -EINVAL;
 		goto err_free_val;
 	}
Signature · 1 line
2.9.3
#8 lore raw

PATCH 6/8 crypto: DH - add PKCS#3 parameter handling

In reply to Stephan Müller

DH parameters are commonly handled in PKCS#3 ASN.1 DER encoded files. The addition adds support for the parsing of such DER encoded parameter sets. After parsing, the data is added to the DH context data structure.

This support allows using of parameter sets generated with the openssl dhparam command.

Note, the privateValueLength parameter defined in PKCS#3 is not parsed as it is not required for the DH operation.

The PKCS#3 parser is able to parse data created by other tools, such as:

openssl dhparam -outform DER -out dhparam.der 2048

---
 crypto/Makefile         |  7 ++++++-
 crypto/dh.c             | 15 ++++++++++++++
 crypto/dh_helper.c      | 53 ++++++++++++++++++++++++++++++++++++++++++++++---
 crypto/dhparameter.asn1 |  4 ++++
 include/crypto/dh.h     | 21 +++++++++++++++++---
 5 files changed, 93 insertions(+), 7 deletions(-)
 create mode 100644 crypto/dhparameter.asn1
crypto/Makefile +6 -1
index 8a44057..a25cfdd 100644
@@ -30,7 +30,12 @@ obj-$(CONFIG_CRYPTO_HASH2) += crypto_hash.o
 obj-$(CONFIG_CRYPTO_AKCIPHER2) += akcipher.o
 obj-$(CONFIG_CRYPTO_KPP2) += kpp.o
 
-dh_generic-y := dh.o
+$(obj)/dhparameter-asn1.o: $(obj)/dhparameter-asn1.c $(obj)/dhparameter-asn1.h
+$(obj)/dh_helper.o: $(obj)/dhparameter-asn1.h
+clean-files += dhparameter-asn1.c dhparameter-asn1.h
+
+dh_generic-y := dhparameter-asn1.o
+dh_generic-y += dh.o
 dh_generic-y += dh_helper.o
 obj-$(CONFIG_CRYPTO_DH) += dh_generic.o
 ecdh_generic-y := ecc.o
crypto/dh.c +15 -0
index f7be48e..a82dceb 100644
@@ -83,6 +83,20 @@ static int dh_set_params(struct dh_ctx *ctx, struct dh *params)
 	return 0;
 }
 
+static int dh_set_params_pkcs3(struct crypto_kpp *tfm, const void *param,
+			       unsigned int param_len)
+{
+	struct dh_ctx *ctx = dh_get_ctx(tfm);
+	struct dh parsed_params;
+	int ret;
+
+	ret = dh_parse_params_pkcs3(&parsed_params, param, param_len);
+	if (ret)
+		return ret;
+
+	return dh_set_params(ctx, &parsed_params);
+}
+
 static int dh_set_secret(struct crypto_kpp *tfm, const void *buf,
 			 unsigned int len)
 {
@@ -163,6 +177,7 @@ static void dh_exit_tfm(struct crypto_kpp *tfm)
 }
 
 static struct kpp_alg dh = {
+	.set_params = dh_set_params_pkcs3,
 	.set_secret = dh_set_secret,
 	.generate_public_key = dh_compute_value,
 	.compute_shared_secret = dh_compute_value,
crypto/dh_helper.c +50 -3

72 lines not on this page · raw message

crypto/dhparameter.asn1 new file +4 -0
new file mode 100644
index 0000000..e9107c8
@@ -0,0 +1,4 @@
+DHParameter ::= SEQUENCE {
+	prime			INTEGER ({ dh_get_p }),
+	base			INTEGER ({ dh_get_g })
+}
include/crypto/dh.h +18 -3
index 6b424ad..ba4988a 100644
@@ -35,9 +35,9 @@
  * @g_size:	Size of DH generator G
  */
 struct dh {
-	void *key;
-	void *p;
-	void *g;
+	const void *key;
+	const void *p;
+	const void *g;
 	unsigned int key_size;
 	unsigned int p_size;
 	unsigned int g_size;
@@ -84,4 +84,19 @@ int crypto_dh_encode_key(char *buf, unsigned int len, const struct dh *params);
  */
 int crypto_dh_decode_key(const char *buf, unsigned int len, struct dh *params);
 
+/**
+ * dh_parse_params_pkcs3() - decodes the PKCS#3 BER encoded buffer of the DH
+ *			    parameters and stores in the provided struct dh,
+ *			    pointers to the raw p and g parameters as is, so
+ *			    that the caller can copy it or MPI parse it, etc.
+ *
+ * @dh:		struct dh representation
+ * @param:	DH parameters in BER format following PKCS#3
+ * @param_len:	length of parameter buffer
+ *
+ * Return:	0 on success or error code in case of error
+ */
+int dh_parse_params_pkcs3(struct dh *dh, const void *param,
+			  unsigned int param_len);
+
 #endif
Signature · 1 line
2.9.3
#9 lore raw

PATCH 7/8 crypto: ecdh - constify key

In reply to Stephan Müller
---
 include/crypto/ecdh.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/crypto/ecdh.h +1 -1
index 03a64f6..b5bb149 100644
@@ -40,7 +40,7 @@
  */
 struct ecdh {
 	unsigned short curve_id;
-	char *key;
+	const char *key;
 	unsigned short key_size;
 };
 
Signature · 1 line
2.9.3
Acked-by on v1 7/8
#10 lore raw

Re: [PATCH 7/8] crypto: ecdh - constify key

Quoted 11 lines · Stephan Müller
---
  include/crypto/ecdh.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/crypto/ecdh.h +0 -0
index 03a64f6..b5bb149 100644

include/crypto/ecdh.h line 46

Quoted 8 lines
include/crypto/ecdh.h +1 -1
@@ -40,7 +40,7 @@
   */
  struct ecdh {
  	unsigned short curve_id;
-	char *key;
+	const char *key;
  	unsigned short key_size;
  };
 

I just came across this and remembered that Stephan already made a patch, so:

#11 lore raw

PATCH 8/8 crypto: AF_ALG - add KPP support

In reply to Stephan Müller

The patch externalizes the KPP kernel crypto API to user space. This allows user space to make use of Diffie-Hellman and EC Diffie-Hellman operations.

The following operations are supported:

* DH parameters formatted in PKCS#3 with ALG_SET_DH_PARAMETERS setsockopt. The call returns the buffer length user space must allocate for the shared secret / public key operation.

* ECDH curve selection via ALG_SET_ECDH_CURVE setsockopt. The call returns the buffer length user space must allocate for the shared secret / public key operation.

* The private key can be set with the ALG_SET_KEY setsockopt. It is permissible to provide a NULL key. In this case, the kernel uses the stdrng to generate a private key of appropriate size and sets the key with the TFM. The idea is that the caller can obtain the public key from the private key, exchange it with the peer, inject the peer's public key into the kernel and derive the shared secret. This way, the private key does not leave the kernel realm. The call returns the buffer length user space must allocate for the shared secret / public key operation.

* The public key is obtained from the private key via the recvmsg operation. For this operation, no data sent to the kernel via sendmsg or sendpage is required.

* The shared secret is obtained by providing the peer's public key via sendmsg / sendpage and reading the shared secret via recvmsg.

---
 crypto/Kconfig     |   10 +
 crypto/Makefile    |    1 +
 crypto/algif_kpp.c | 1230 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 1241 insertions(+)
 create mode 100644 crypto/algif_kpp.c
crypto/Kconfig +10 -0
index aac4bc9..cf59f76 100644
@@ -1757,6 +1757,16 @@ config CRYPTO_USER_API_AEAD
 	  This option enables the user-spaces interface for AEAD
 	  cipher algorithms.
 
+config CRYPTO_USER_API_KPP
+	tristate "User-space interface for key protocol primitives algorithms"
+	depends on NET
+	select CRYPTO_KPP2
+	select CRYPTO_USER_API
+	help
+	  This option enables the user-spaces interface for key protocol
+	  primitives algorithms. This covers Diffie-Hellman and EC
+	  Diffie-Hellman
+
 config CRYPTO_HASH_INFO
 	bool
 
crypto/Makefile +1 -0
index a25cfdd..45d861b 100644
@@ -142,6 +142,7 @@ obj-$(CONFIG_CRYPTO_USER_API_HASH) += algif_hash.o
 obj-$(CONFIG_CRYPTO_USER_API_SKCIPHER) += algif_skcipher.o
 obj-$(CONFIG_CRYPTO_USER_API_RNG) += algif_rng.o
 obj-$(CONFIG_CRYPTO_USER_API_AEAD) += algif_aead.o
+obj-$(CONFIG_CRYPTO_USER_API_KPP) += algif_kpp.o
 
 #
 # generic algorithms and the async_tx api
crypto/algif_kpp.c new file +1230 -0

1,230 lines not on this page · raw message

Signature · 1 line
2.9.3
#12 lore raw
In reply to Stephan Müller

Hi, Stephan, Herbert,

Quoted 3 lines · Stephan Müller

The patch 8 describes the different operations that are supported by AF_ALG KPP. This support includes generation and retaining of the private key inside the kernel. This private key would never be sent to user space.

There are crypto co-processors that are capable of generating and retaining the private key inside the device without revealing it to kernel. The private key will be further used to generate the public key and the shared secret. Should we extend the KPP API to support this?

Thanks,
ta

#13 lore raw

Am Mittwoch, 19. April 2017, 14:03:35 CEST schrieb Tudor Ambarus:

Hi Tudor,

Quoted 8 lines

Hi, Stephan, Herbert,

Quoted 4 lines · Stephan Müller

The patch 8 describes the different operations that are supported by AF_ALG
KPP. This support includes generation and retaining of the private key inside the kernel. This private key would never be sent to user space.

Quoted 4 lines

There are crypto co-processors that are capable of generating and retaining the private key inside the device without revealing it to kernel. The private key will be further used to generate the public key and the shared secret. Should we extend the KPP API to support this?

The less software has access to secrets, the better. Thus, having such API would be good.

This of course assumes that the private key is generated with an appropriate RNG. As normal users cannot verify the RNG or the noise sources implemented in hardware, the choice of using such API to keep the private key inside the hardware should be left to the caller.

Quoted 1 line

From the AF_ALG KPP support side, I could imagine that that the algif_kpp code

makes use of the hardware support unless the caller objects.

Ciao
Stephan