Skip to contentSkip to conversation

crypto: Introduce Public Key Encryption API

RFC PATCH Superseded Tadeusz Struk 2 participants 6 messages Jun 16, 2015

Subsystems (3)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v6 cover crypto: Introduce Public Key Encryption API

This patch set introduces a Public Key Encryption API.
What is proposed is a new crypto type called crypto_akcipher_type, plus new struct akcipher_alg and struct crypto_akcipher, together with number of helper functions to register akcipher type algorithms and allocate tfm instances. This is to make it similar to how the existing crypto API works for the ablkcipher, ahash, and aead types.
The operations the new interface will allow to provide are:

	int (*sign)(struct akcipher_request *req);
	int (*verify)(struct akcipher_request *req);
	int (*encrypt)(struct akcipher_request *req);
	int (*decrypt)(struct akcipher_request *req);

The benefits it gives interface are:
- drivers can add many implementations of RSA or DSA
algorithms and user will allocate instances (tfms) of these, base on algorithm priority, in the same way as it is with the symmetric ciphers.
- the new interface allows for asynchronous implementations that can use crypto hardware to offload the calculations to.
- integrating it with linux crypto api allows using all its benefits i.e. managing algorithms using NETLINK_CRYPTO, monitoring implementations using /proc/crypto. etc

New helper functions have been added to allocate crypto_akcipher instances and invoke the operations to make it easier to use.
For instance to verify a public_signature against a public_key using the RSA algorithm a user would do:

	struct crypto_akcipher *tfm = crypto_alloc_akcipher("rsa", 0, 0);
	struct akcipher_request *req = akcipher_request_alloc(tfm, GFP_KERNEL);
	akcipher_request_set_crypt(req, pub_key, signature);
	int ret = crypto_akcipher_verify(src, dst, src_len, dst_len, &res_len);
	akcipher_request_free(req);
	crypto_free_akcipher(tfm);
	return ret;

Changes in v6:
- in FIPS mode only allow key sizes 2K & 3K
- remove result_len and use dst_len as in/out param
- remove subtype from akcipher reports
- store rsa_key in tfm ctx and change ras_parse_key to take struct rsa_key instead of tfm.
- export rsa_free_key, which free memory allocated by ras_parse_key.
- split akcipher.h into public and internal with public key specific helpers
- remove maxsize() and set the required size in enc/dec/sign/verify instead
- add public key vector
- split AKCIPHER into AKCIPHER and AKCIPHER2 in Kconfig
- remove MPI patch from the series - already applied

Changes in v5:
- make mpi_get_size() function inline.
- add a setkey function to the algorithm and rsa_parse_key() helper to parse rsa keys from BER encoded to MPI. The helper will also validate the given key if it is strong enough in case FIPS mode is enabled.
- change the format of the key from struct public_key * to void * BER encoded buffer.
- change the format of the rsa keys in testmgr to BER encoded form.
- change mpi_free to use kzfree instead of kfree because it is used to free crypto keys

Changes in v4:
- add an rsa generic implementation
- don't convert the existing public_key implementation to the new interface. This will be done after the new interface is accepted.
- add new mpi_get_buf(), mpi_copy() and mpi_get_size() mpi helpers
- on set key the ftm now will clone the key instead of just setting a ptr
- add a check on enc/dec/sign/veryfi to make sure a valid (public or private) key is setup
- add maxsize fn into algorith that will be used to query implementation what is the max size of a result for a give public key that the user needs to allocate
- removed private ctx from crypto_akcipher as the crypto_tfm base has one already
- add 2K bit RSA test vectors
- add cipher text validation in crypto test mgr as (required for FIPS)

Changes in v3:
- changed input and output parameters type from sgl to void * and added separate src_len & dst_len - requested by Herbert Xu
- separated rsa implementation into cryptographic primitives and left encryption scheme details outside of the algorithm implementation
- added SW implementation for RSA encrypt, decrypt and sign operation
- added RSA test vectors

Changes in v2:
- remodeled not to use obsolete cra_u and crt_u unions
- changed type/funct names from pke_* to pkey_*
- retained the enum pkey_algo type for it is external to the kernel
- added documentation

---
Tadeusz Struk (3):
crypto: add PKE API
crypto: rsa: add a new rsa generic implementation
crypto: add tests vectors for RSA

 crypto/Kconfig                     |   19 ++
 crypto/Makefile                    |    9 +
 crypto/akcipher.c                  |  100 +++++++++++
 crypto/crypto_user.c               |   22 ++
 crypto/rsa.c                       |  313 +++++++++++++++++++++++++++++++++++
 crypto/rsa_helper.c                |  124 ++++++++++++++
 crypto/rsakey.asn1                 |    5 +
 crypto/testmgr.c                   |  161 ++++++++++++++++++
 crypto/testmgr.h                   |  187 +++++++++++++++++++++
 include/crypto/akcipher.h          |  323 ++++++++++++++++++++++++++++++++++++
 include/crypto/internal/akcipher.h |   66 +++++++
 include/crypto/internal/rsa.h      |   28 +++
 include/linux/crypto.h             |    1 
 include/linux/cryptouser.h         |    5 +
 14 files changed, 1363 insertions(+)
 create mode 100644 crypto/akcipher.c
 create mode 100644 crypto/rsa.c
 create mode 100644 crypto/rsa_helper.c
 create mode 100644 crypto/rsakey.asn1
 create mode 100644 include/crypto/akcipher.h
 create mode 100644 include/crypto/internal/akcipher.h
 create mode 100644 include/crypto/internal/rsa.h
#1 lore raw

RFC PATCH v6 1/3 crypto: add PKE API

Add Public Key Encryption API.

---
 crypto/Kconfig                     |   11 +
 crypto/Makefile                    |    1 
 crypto/akcipher.c                  |  100 +++++++++++
 crypto/crypto_user.c               |   22 ++
 include/crypto/akcipher.h          |  323 ++++++++++++++++++++++++++++++++++++
 include/crypto/internal/akcipher.h |   66 +++++++
 include/linux/crypto.h             |    1 
 include/linux/cryptouser.h         |    5 +
 8 files changed, 529 insertions(+)
 create mode 100644 crypto/akcipher.c
 create mode 100644 include/crypto/akcipher.h
 create mode 100644 include/crypto/internal/akcipher.h
crypto/Kconfig +11 -0
index f6fc054..264dadb 100644
@@ -91,6 +91,17 @@ config CRYPTO_PCOMP2
 	tristate
 	select CRYPTO_ALGAPI2
 
+config CRYPTO_AKCIPHER2
+	tristate
+	select CRYPTO_ALGAPI2
+
+config CRYPTO_AKCIPHER
+	tristate "Public Key Algorithms API"
+	select CRYPTO_AKCIPHER2
+	select CRYPTO_ALGAPI
+	help
+	  Crypto API interface for public key algorithms.
+
 config CRYPTO_MANAGER
 	tristate "Cryptographic algorithm manager"
 	select CRYPTO_MANAGER2
crypto/Makefile +1 -0
index c842035..6f2940a 100644
@@ -28,6 +28,7 @@ crypto_hash-y += shash.o
 obj-$(CONFIG_CRYPTO_HASH2) += crypto_hash.o
 
 obj-$(CONFIG_CRYPTO_PCOMP2) += pcompress.o
+obj-$(CONFIG_CRYPTO_AKCIPHER) += akcipher.o
 
 cryptomgr-y := algboss.o testmgr.o
 
crypto/akcipher.c new file +100 -0

100 lines not on this page · raw message

crypto/crypto_user.c +22 -0
index 41dfe76..11dbd5a 100644
@@ -27,6 +27,7 @@
 #include <net/net_namespace.h>
 #include <crypto/internal/aead.h>
 #include <crypto/internal/skcipher.h>
+#include <crypto/akcipher.h>
 
 #include "internal.h"
 
@@ -110,6 +111,21 @@ nla_put_failure:
 	return -EMSGSIZE;
 }
 
+static int crypto_report_akcipher(struct sk_buff *skb, struct crypto_alg *alg)
+{
+	struct crypto_report_akcipher rakcipher;
+
+	strncpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
+
+	if (nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER,
+		    sizeof(struct crypto_report_akcipher), &rakcipher))
+		goto nla_put_failure;
+	return 0;
+
+nla_put_failure:
+	return -EMSGSIZE;
+}
+
 static int crypto_report_one(struct crypto_alg *alg,
 			     struct crypto_user_alg *ualg, struct sk_buff *skb)
 {
@@ -154,6 +170,12 @@ static int crypto_report_one(struct crypto_alg *alg,
 			goto nla_put_failure;
 
 		break;
+
+	case CRYPTO_ALG_TYPE_AKCIPHER:
+		if (crypto_report_akcipher(skb, alg))
+			goto nla_put_failure;
+
+		break;
 	}
 
 out:
include/crypto/akcipher.h new file +323 -0

323 lines not on this page · raw message

include/crypto/internal/akcipher.h new file +66 -0

66 lines not on this page · raw message

include/linux/crypto.h +1 -0
index 25a4b71..0e3f71a 100644
@@ -53,6 +53,7 @@
 #define CRYPTO_ALG_TYPE_SHASH		0x00000009
 #define CRYPTO_ALG_TYPE_AHASH		0x0000000a
 #define CRYPTO_ALG_TYPE_RNG		0x0000000c
+#define CRYPTO_ALG_TYPE_AKCIPHER	0x0000000d
 #define CRYPTO_ALG_TYPE_PCOMPRESS	0x0000000f
 
 #define CRYPTO_ALG_TYPE_HASH_MASK	0x0000000e
include/linux/cryptouser.h +5 -0
index 4abf2ea..36efbbb 100644
@@ -43,6 +43,7 @@ enum crypto_attr_type_t {
 	CRYPTOCFGA_REPORT_COMPRESS,	/* struct crypto_report_comp */
 	CRYPTOCFGA_REPORT_RNG,		/* struct crypto_report_rng */
 	CRYPTOCFGA_REPORT_CIPHER,	/* struct crypto_report_cipher */
+	CRYPTOCFGA_REPORT_AKCIPHER,	/* struct crypto_report_akcipher */
 	__CRYPTOCFGA_MAX
 
 #define CRYPTOCFGA_MAX (__CRYPTOCFGA_MAX - 1)
@@ -101,5 +102,9 @@ struct crypto_report_rng {
 	unsigned int seedsize;
 };
 
+struct crypto_report_akcipher {
+	char type[CRYPTO_MAX_NAME];
+};
+
 #define CRYPTO_REPORT_MAXSIZE (sizeof(struct crypto_user_alg) + \
 			       sizeof(struct crypto_report_blkcipher))
maintainer
#2 lore raw

Re: [PATCH RFC v6 1/3] crypto: add PKE API

crypto/Makefile crypto_hash-y += shash.o · line 31

Quoted 6 lines · Tadeusz Struk
crypto/Makefile +1 -0
@@ -28,6 +28,7 @@ crypto_hash-y += shash.o
 obj-$(CONFIG_CRYPTO_HASH2) += crypto_hash.o
 
 obj-$(CONFIG_CRYPTO_PCOMP2) += pcompress.o
+obj-$(CONFIG_CRYPTO_AKCIPHER) += akcipher.o

s/AKCIPHER/AKCIPHER2/

Quoted 5 lines
include/crypto/akcipher.h new file +5 -0
+/**
+ * struct akcipher_request - public key request
+ *
+ * @base:	Common attributes for async crypto requests
+ * @src:	Pointer to memory containing the input parameters

include/crypto/akcipher.h line 31

Quoted 10 lines
include/crypto/akcipher.h new file +10 -0
+ *		The format of the parameter(s) is expeted to be Octet String
+ * @dst:	Pointer to memory whare the result will be stored
+ * @src_len:	Size of the input parameter
+ * @dst_len:	Size of the output buffer. It needs to be at leaset
+ *		as big as the expected result depending	on the operation
+ *		After operation it will be updated with the acctual size of the
+ *		result. In case of error, where the dst_len was insufficient,
+ *		it will be updated to the size required for the operation.
+ * @result_len: If not NULL this will be updated by the implementation to
+ *		reflect the acctual size of the result

result_len is still here.

include/crypto/akcipher.h line 39

Quoted 8 lines

+ * @__ctx: Start of private context data
+ */

include/crypto/akcipher.h new file +6 -0
+struct akcipher_request {
+	struct crypto_async_request base;
+	void *src;
+	void *dst;
+	unsigned int src_len;
+	unsigned int *dst_len;

dst_len doesn't need to be a pointer. A simple int will do.

include/crypto/akcipher.h line 244

Quoted 3 lines
include/crypto/akcipher.h new file +3 -0
+static inline int crypto_akcipher_encrypt(struct akcipher_request *req)
+{
+	struct crypto_akcipher *tfm = __crypto_akcipher_tfm(req->base.tfm);

You should add a reqtfm helper like crypto_aead_reqtfm so that implementors don't need to do this ugly __crypto_akcipher_tfm.

In fact you already have that helper so you just need to use it.

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
#3 lore raw

RFC PATCH v6 2/3 crypto: rsa: add a new rsa generic implementation

In reply to Tadeusz Struk

Add a new rsa generic SW implementation.
This implements only cryptographic primitives.

---
 crypto/Kconfig                |    7 +
 crypto/Makefile               |    8 +
 crypto/rsa.c                  |  313 +++++++++++++++++++++++++++++++++++++++++
 crypto/rsa_helper.c           |  124 ++++++++++++++++
 crypto/rsakey.asn1            |    5 +
 include/crypto/internal/rsa.h |   28 ++++
 6 files changed, 485 insertions(+)
 create mode 100644 crypto/rsa.c
 create mode 100644 crypto/rsa_helper.c
 create mode 100644 crypto/rsakey.asn1
 create mode 100644 include/crypto/internal/rsa.h
crypto/Kconfig +7 -0
index 264dadb..52467cf 100644
@@ -102,6 +102,13 @@ config CRYPTO_AKCIPHER
 	help
 	  Crypto API interface for public key algorithms.
 
+config CRYPTO_RSA
+	tristate "RSA algorithm"
+	select AKCIPHER
+	select MPILIB
+	help
+	  Generic implementation of the RSA public key algorithm.
+
 config CRYPTO_MANAGER
 	tristate "Cryptographic algorithm manager"
 	select CRYPTO_MANAGER2
crypto/Makefile +8 -0
index 6f2940a..c6217ea 100644
@@ -30,6 +30,14 @@ obj-$(CONFIG_CRYPTO_HASH2) += crypto_hash.o
 obj-$(CONFIG_CRYPTO_PCOMP2) += pcompress.o
 obj-$(CONFIG_CRYPTO_AKCIPHER) += akcipher.o
 
+$(obj)/rsakey-asn1.o: $(obj)/rsakey-asn1.c $(obj)/rsakey-asn1.h
+clean-files += rsakey-asn1.c rsakey-asn1.h
+
+rsa_generic-y := rsakey-asn1.o
+rsa_generic-y += rsa.o
+rsa_generic-y += rsa_helper.o
+obj-$(CONFIG_CRYPTO_RSA) += rsa_generic.o
+
 cryptomgr-y := algboss.o testmgr.o
 
 obj-$(CONFIG_CRYPTO_MANAGER2) += cryptomgr.o
crypto/rsa.c new file +313 -0

313 lines not on this page · raw message

crypto/rsa_helper.c new file +124 -0

124 lines not on this page · raw message

crypto/rsakey.asn1 new file +5 -0
new file mode 100644
index 0000000..3c7b5df
@@ -0,0 +1,5 @@
+RsaKey ::= SEQUENCE {
+	n INTEGER ({ rsa_get_n }),
+	e INTEGER ({ rsa_get_e }),
+	d INTEGER ({ rsa_get_d })
+}
include/crypto/internal/rsa.h new file +28 -0
new file mode 100644
index 0000000..018f373
@@ -0,0 +1,28 @@
+/*
+ * RSA internal helpers
+ *
+ * Copyright (c) 2015, Intel Corporation
+ * Authors: Tadeusz Struk <[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.
+ *
+ */
+#ifndef _RSA_HELPER_
+#define _RSA_HELPER_
+#include <linux/mpi.h>
+#include <crypto/akcipher.h>
+
+struct rsa_key {
+	MPI n;
+	MPI e;
+	MPI d;
+};
+
+int rsa_parse_key(struct rsa_key *rsa_key, const void *key,
+		  unsigned int key_len);
+
+void rsa_free_key(struct rsa_key *rsa_key);
+#endif
maintainer
#4 lore raw

Re: [PATCH RFC v6 2/3] crypto: rsa: add a new rsa generic implementation

Review

Quoted 3 lines · Tadeusz Struk
(quoted diff) +2 -0
+	if (!pkey->n || !pkey->e || !req->dst_len)
+		return -EINVAL;

You leaked m.

crypto/rsa.c line 95

Quoted 4 lines
crypto/rsa.c new file +4 -0
+	if (*req->dst_len < mpi_get_size(pkey->n)) {
+		*req->dst_len = mpi_get_size(pkey->n);
+		return -EINVAL;
+	}

This needs to be an error that's distinct from real errors. So perhaps pick EOVERFLOW.

crypto/rsa.c line 270

Quoted 4 lines
crypto/rsa.c new file +4 -0
+static int rsa_init_tfm(struct crypto_tfm *_tfm)
+{
+	return 0;
+}

The init function is optional. So just drop it if there is nothing to be done.

crypto/rsa.c line 293

Quoted 13 lines
crypto/rsa.c new file +13 -0
+	.encrypt = rsa_enc,
+	.decrypt = rsa_dec,
+	.sign = rsa_sign,
+	.verify = rsa_verify,
+	.setkey = rsa_setkey,
+	.base = {
+		.cra_name = "rsa",
+		.cra_driver_name = "rsa-generic",
+		.cra_priority = 100,
+		.cra_ctxsize = 0,
+		.cra_alignmask = 0,
+		.cra_module = THIS_MODULE,
+		.cra_ctxsize = sizeof(struct rsa_key),

You're initialising cra_ctxsize twice. Also please drop the alignmask since the default is zero.

crypto/rsa.c line 297

Quoted 4 lines
crypto/rsa.c new file +4 -0
+		.cra_init = rsa_init_tfm,
+		.cra_exit = rsa_exit_tfm,
+	},
+};

Please drop cra_init/cra_exit and add type-safe init/exit functions like I did in 5eb8ec6dc857d5027bc8cf7268a199107a583ae5

Quoted 11 lines
crypto/rsa_helper.c new file +5 -0
new file mode 100644
index 0000000..103dd2e
@@ -0,0 +1,124 @@
+/*
+ * RSA key extract helper
+ *
+ * Copyright (c) 2015, Intel Corporation
+ * Authors: Tadeusz Struk <[email protected]>

crypto/rsa_helper.c line 15

Quoted 10 lines
crypto/rsa_helper.c new file +10 -0
+ *
+ * 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/fips.h>
+#include <crypto/internal/rsa.h>
+#include "rsakey-asn1.h"

Need export.h.

crypto/rsa_helper.c line 75

Quoted 4 lines
crypto/rsa_helper.c new file +4 -0
+static void free_mpis(struct rsa_key *key)
+{
+	if (key->n)
+		mpi_free(key->n);

mpi_free already checks for NULL, so you can remove the check here.

Quoted 12 lines
include/crypto/internal/rsa.h new file +6 -0
new file mode 100644
index 0000000..018f373
@@ -0,0 +1,28 @@
+/*
+ * RSA internal helpers
+ *
+ * Copyright (c) 2015, Intel Corporation
+ * Authors: Tadeusz Struk <[email protected]>
+ *

include/crypto/internal/rsa.h line 16

Quoted 10 lines
include/crypto/internal/rsa.h new file +10 -0
+ * 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.
+ *
+ */
+#ifndef _RSA_HELPER_
+#define _RSA_HELPER_
+#include <linux/mpi.h>
+#include <crypto/akcipher.h>

You should drop akcipher.h since nothing in this file uses it.

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
#5 lore raw

RFC PATCH v6 3/3 crypto: add tests vectors for RSA

In reply to Tadeusz Struk

New test vectors for RSA algorithm.

---
 crypto/Kconfig   |    1 
 crypto/testmgr.c |  161 ++++++++++++++++++++++++++++++++++++++++++++++
 crypto/testmgr.h |  187 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 349 insertions(+)
crypto/Kconfig +1 -0
index 52467cf..9471ac8 100644
@@ -122,6 +122,7 @@ config CRYPTO_MANAGER2
 	select CRYPTO_HASH2
 	select CRYPTO_BLKCIPHER2
 	select CRYPTO_PCOMP2
+	select CRYPTO_AKCIPHER2
 
 config CRYPTO_USER
 	tristate "Userspace cryptographic algorithm configuration"
crypto/testmgr.c +161 -0

191 lines not on this page · raw message

crypto/testmgr.h +187 -0

196 lines not on this page · raw message