Skip to contentSkip to conversation

crypto: Introduce Public Key Encryption API

RFC PATCH Superseded Tadeusz Struk 3 participants 33 messages Jun 15, 2015 – Jun 16, 2015

Subsystems (3)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v5 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 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 (4):
MPILIB: add mpi_read_buf(), mpi_copy() and mpi_get_size() helpers
crypto: add PKE API
crypto: rsa: add a new rsa generic implementation
crypto: add tests vectors for RSA

 crypto/Kconfig                |   14 +
 crypto/Makefile               |    9 +
 crypto/akcipher.c             |  102 ++++++++++
 crypto/crypto_user.c          |   23 ++
 crypto/rsa.c                  |  295 ++++++++++++++++++++++++++++++
 crypto/rsa_helper.c           |  134 ++++++++++++++
 crypto/rsakey.asn1            |    5 +
 crypto/testmgr.c              |  149 +++++++++++++++
 crypto/testmgr.h              |  143 +++++++++++++++
 include/crypto/akcipher.h     |  404 +++++++++++++++++++++++++++++++++++++++++
 include/crypto/internal/rsa.h |   30 +++
 include/linux/crypto.h        |    1 
 include/linux/cryptouser.h    |    6 +
 include/linux/mpi.h           |   15 ++
 lib/mpi/mpicoder.c            |   87 +++++++--
 lib/mpi/mpiutil.c             |   38 ++++
 16 files changed, 1434 insertions(+), 21 deletions(-)
 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/rsa.h
#1 lore raw

RFC PATCH v5 1/4 MPILIB: add mpi_read_buf() and mpi_get_size() helpers

Added a mpi_read_buf() helper function to export MPI to a buf provided by the user, and a mpi_get_size() helper, that tells the user how big the buf is. Changed mpi_free to use kzfree instead of kfree because it is used to free crypto keys.

---
 include/linux/mpi.h |   15 +++++++++
 lib/mpi/mpicoder.c  |   87 ++++++++++++++++++++++++++++++++++++++++-----------
 lib/mpi/mpiutil.c   |    6 ++--
 3 files changed, 86 insertions(+), 22 deletions(-)
include/linux/mpi.h +15 -0
index 5af1b81..641b7d6 100644
@@ -81,6 +81,8 @@ MPI mpi_read_from_buffer(const void *buffer, unsigned *ret_nread);
 int mpi_fromstr(MPI val, const char *str);
 u32 mpi_get_keyid(MPI a, u32 *keyid);
 void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign);
+int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
+		    int *sign);
 void *mpi_get_secure_buffer(MPI a, unsigned *nbytes, int *sign);
 int mpi_set_buffer(MPI a, const void *buffer, unsigned nbytes, int sign);
 
@@ -142,4 +144,17 @@ int mpi_rshift(MPI x, MPI a, unsigned n);
 /*-- mpi-inv.c --*/
 int mpi_invm(MPI x, MPI u, MPI v);
 
+/* inline functions */
+
+/**
+ * mpi_get_size() - returns max size required to store the number
+ *
+ * @a:	A multi precision integer for which we want to allocate a bufer
+ *
+ * Return: size required to store the number
+ */
+static inline unsigned int mpi_get_size(MPI a)
+{
+	return a->nlimbs * BYTES_PER_MPI_LIMB;
+}
 #endif /*G10_MPI_H */
lib/mpi/mpicoder.c +68 -19

111 lines not on this page · raw message

lib/mpi/mpiutil.c +3 -3
index bf076d2..314f4df 100644
@@ -69,7 +69,7 @@ void mpi_free_limb_space(mpi_ptr_t a)
 	if (!a)
 		return;
 
-	kfree(a);
+	kzfree(a);
 }
 
 void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs)
@@ -95,7 +95,7 @@ int mpi_resize(MPI a, unsigned nlimbs)
 		if (!p)
 			return -ENOMEM;
 		memcpy(p, a->d, a->alloced * sizeof(mpi_limb_t));
-		kfree(a->d);
+		kzfree(a->d);
 		a->d = p;
 	} else {
 		a->d = kzalloc(nlimbs * sizeof(mpi_limb_t), GFP_KERNEL);
@@ -112,7 +112,7 @@ void mpi_free(MPI a)
 		return;
 
 	if (a->flags & 4)
-		kfree(a->d);
+		kzfree(a->d);
 	else
 		mpi_free_limb_space(a->d);
 
maintainer
#2 lore raw

Re: [PATCH RFC v5 1/4] MPILIB: add mpi_read_buf() and mpi_get_size() helpers

Quoted 6 lines · Tadeusz Struk

Added a mpi_read_buf() helper function to export MPI to a buf provided by the user, and a mpi_get_size() helper, that tells the user how big the buf is. Changed mpi_free to use kzfree instead of kfree because it is used to free crypto keys.

Patch applied. Thanks!

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#3 lore raw

RFC PATCH v5 2/4 crypto: add PKE API

In reply to Tadeusz Struk

Add Public Key Encryption API.

---
 crypto/Kconfig             |    6 +
 crypto/Makefile            |    1 
 crypto/akcipher.c          |  102 +++++++++++
 crypto/crypto_user.c       |   23 +++
 include/crypto/akcipher.h  |  404 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/crypto.h     |    1 
 include/linux/cryptouser.h |    6 +
 7 files changed, 543 insertions(+)
 create mode 100644 crypto/akcipher.c
 create mode 100644 include/crypto/akcipher.h
crypto/Kconfig +6 -0
index f6fc054..ed413d9 100644
@@ -91,6 +91,12 @@ config CRYPTO_PCOMP2
 	tristate
 	select CRYPTO_ALGAPI2
 
+config CRYPTO_AKCIPHER
+	tristate "Public Key Algorithms API"
+	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 +102 -0

102 lines not on this page · raw message

crypto/crypto_user.c +23 -0
index 41dfe76..508e71d 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,22 @@ 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));
+	strncpy(rakcipher.subtype, alg->cra_name, sizeof(rakcipher.subtype));
+
+	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 +171,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 +404 -0

404 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 +6 -0
index 4abf2ea..8448ef8 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,10 @@ struct crypto_report_rng {
 	unsigned int seedsize;
 };
 
+struct crypto_report_akcipher {
+	char type[CRYPTO_MAX_NAME];
+	char subtype[CRYPTO_MAX_NAME];
+};
+
 #define CRYPTO_REPORT_MAXSIZE (sizeof(struct crypto_user_alg) + \
 			       sizeof(struct crypto_report_blkcipher))
maintainer
#4 lore raw

Re: [PATCH RFC v5 2/4] crypto: add PKE API

include/crypto/akcipher.h line 39

Quoted 10 lines · Tadeusz Struk
include/crypto/akcipher.h new file +9 -0
+struct akcipher_request {
+	struct crypto_async_request base;
+	void *src;
+	void *dst;
+	unsigned int src_len;
+	unsigned int dst_len;
+	unsigned int *result_len;
+	void *__ctx[] CRYPTO_MINALIGN_ATTR;
+};

result_len is redundant. You can write to dst_len after the operation is done.

Cheers,

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

Re: [PATCH RFC v5 2/4] crypto: add PKE API

In reply to Tadeusz Struk
Quoted 12 lines · Tadeusz Struk

+/**
+ * crypto_akcipher_setkey() -- Invoke public key setkey operation
+ *
+ * Function invokes the algorithm specific set key function, which knows
+ * how to decode and interpret the encoded key
+ *
+ * @tfm: tfm handle
+ * @key: BER encoded private or public key
+ * @keylen: length of the key
+ *
+ * Return: size required to store result or error code in case of error

cut-n-paste error?

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
maintainer
#6 lore raw

Re: [PATCH RFC v5 2/4] crypto: add PKE API

In reply to Tadeusz Struk
Quoted 10 lines · Tadeusz Struk
include/crypto/akcipher.h new file +9 -0
+/**
+ * crypto_register_akcipher() -- Register public key algorithm
+ *
+ * Function registers an implementation of a public key verify algorithm
+ *
+ * @alg:	algorithm definition
+ *
+ * Return: zero on success; error code in case of error
+ */

include/crypto/akcipher.h line 403

Quoted 10 lines
include/crypto/akcipher.h new file +10 -0
+int crypto_register_akcipher(struct akcipher_alg *alg);
+
+/**
+ * crypto_unregister_akcipher() -- Unregister public key algorithm
+ *
+ * Function unregisters an implementation of a public key verify algorithm
+ *
+ * @alg:	algorithm definition
+ */
+void crypto_unregister_akcipher(struct akcipher_alg *alg);

These two functions and anything else that's specific to public key implementors should go into crypto/internal.

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

Re: [PATCH RFC v5 2/4] crypto: add PKE API

In reply to Tadeusz Struk

crypto/akcipher.c line 33

Quoted 7 lines · Tadeusz Struk
crypto/akcipher.c new file +6 -0
+static int crypto_akcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
+{
+	struct crypto_report_akcipher rakcipher;
+
+	strncpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
+	strncpy(rakcipher.subtype, alg->cra_name, sizeof(rakcipher.subtype));

There is no point in reporting cra_name. That's already taken care of by crypto_user.c.

crypto/akcipher.c line 56

Quoted 7 lines
crypto/akcipher.c new file +7 -0
+static void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg)
+	__attribute__ ((unused));
+
+static void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg)
+{
+	seq_puts(m, "type         : akcipher\n");
+	seq_printf(m, "subtype      : %s\n", alg->cra_name);

Ditto please drop subtype.

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
maintainer
#8 lore raw

Re: [PATCH RFC v5 2/4] crypto: add PKE API

In reply to Tadeusz Struk

include/crypto/akcipher.h line 50

Quoted 3 lines · Tadeusz Struk
include/crypto/akcipher.h new file +2 -0
+struct crypto_akcipher {
+	void *key;

Having a void * pointer here is useless. The normal way of doing it is to place the key into the tfm context.

Cheers,

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

Re: [PATCH RFC v5 2/4] crypto: add PKE API

Quoted 4 lines · Herbert Xu
Quoted 2 lines

include/crypto/akcipher.h line 50

Quoted 2 lines
include/crypto/akcipher.h new file +2 -0
+struct crypto_akcipher {
+	void *key;

Having a void * pointer here is useless. The normal way of doing it is to place the key into the tfm context.

I thought that the ctx needs to be available for implementations to store private data. This way we can allocate and store any type of key in the <alg>_parse_key() helper and still have the cxt available for implementations to use for their stuff (e.g. HW context).

maintainer
#10 lore raw

Re: [PATCH RFC v5 2/4] crypto: add PKE API

Quoted 4 lines · Tadeusz Struk

I thought that the ctx needs to be available for implementations to store private data. This way we can allocate and store any type of key in the <alg>_parse_key() helper and still have the cxt available for implementations to use for their stuff (e.g. HW context).

No for symmetric key algorithms we always store the key in the context and never in the tfm proper.

Think about it, the generic tfm doesn't have any idea on what the key contains so how can it store it? Only the implementation knows the key format and can store it in a useful way.

Cheers,

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

Re: [PATCH RFC v5 2/4] crypto: add PKE API

Quoted 9 lines · Herbert Xu
Quoted 3 lines

I thought that the ctx needs to be available for implementations to store private data.

Quoted 2 lines

This way we can allocate and store any type of key in the <alg>_parse_key() helper and still have the cxt available for implementations to use for their stuff (e.g. HW context).

No for symmetric key algorithms we always store the key in the context and never in the tfm proper.

Think about it, the generic tfm doesn't have any idea on what the key contains so how can it store it? Only the implementation knows the key format and can store it in a useful way.

Ok I wanted to handle everything in the parse_key helper without any help from the implementation. I can change the helper to return the key and implementation will store it in the ctx. Is this what you are suggesting?

maintainer
#12 lore raw

Re: [PATCH RFC v5 2/4] crypto: add PKE API

Quoted 4 lines · Tadeusz Struk

Ok I wanted to handle everything in the parse_key helper without any help from the implementation. I can change the helper to return the key and implementation will store it in the ctx. Is this what you are suggesting?

If you want to keep the helper generic what you can do is have it take struct rsa_key instead of struct crypto_ablkcipher.

It definitely should just be an optional helper as opposed to a required part of crypto_akcipher since you could have a piece of hardware that takes the encoded key directly in which case you wouldn't need the helper at all.

Cheers,

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

Re: [PATCH RFC v5 2/4] crypto: add PKE API

Quoted 2 lines · Herbert Xu

If you want to keep the helper generic what you can do is have it take struct rsa_key instead of struct crypto_ablkcipher.

Ok I'll do it that way.

Quoted 5 lines

It definitely should just be an optional helper as opposed to a required part of crypto_akcipher since you could have a piece of hardware that takes the encoded key directly in which case you wouldn't need the helper at all.

Oh yes, it is definitely optional.
Thanks

maintainer
#14 lore raw

Re: [PATCH RFC v5 2/4] crypto: add PKE API

In reply to Tadeusz Struk
Quoted 3 lines · Tadeusz Struk

+ * @setkey: Function invokes the algorithm specific set key function, which
+ * knows how to decode and interpret the BER encoded key

We should split this into two functions: setpubkey and setprivkey.

include/crypto/akcipher.h line 78

Quoted 10 lines

+ *
+ * @reqsize: Request context size required by algorithm implementation
+ * @base: Common crypto API algorithm data structure
+ */

include/crypto/akcipher.h new file +6 -0
+struct akcipher_alg {
+	int (*sign)(struct akcipher_request *req);
+	int (*verify)(struct akcipher_request *req);
+	int (*encrypt)(struct akcipher_request *req);
+	int (*decrypt)(struct akcipher_request *req);
+	int (*maxsize)(struct crypto_akcipher *tfm);

Hmm, we could actually get rid of maxsize by just having each function check the dst_len and if it is insufficient write the required length in it and then return an error.

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

Re: [PATCH RFC v5 2/4] crypto: add PKE API

Quoted 4 lines · Herbert Xu
Quoted 2 lines
Quoted 2 lines

+ * @setkey: Function invokes the algorithm specific set key function, which
+ * knows how to decode and interpret the BER encoded key

We should split this into two functions: setpubkey and setprivkey.

The two functions will be almost identical. We can do it this way if we want to check if all the required elements of the key are provided. Currently I'm checking this in the actual operation.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

+ *
+ * @reqsize: Request context size required by algorithm implementation
+ * @base: Common crypto API algorithm data structure
+ */
+struct akcipher_alg {

Quoted 8 lines
Quoted 5 lines

include/crypto/akcipher.h line 78

Quoted 5 lines
include/crypto/akcipher.h new file +5 -0
+	int (*sign)(struct akcipher_request *req);
+	int (*verify)(struct akcipher_request *req);
+	int (*encrypt)(struct akcipher_request *req);
+	int (*decrypt)(struct akcipher_request *req);
+	int (*maxsize)(struct crypto_akcipher *tfm);

Hmm, we could actually get rid of maxsize by just having each function check the dst_len and if it is insufficient write the required length in it and then return an error.

Can do it that way too.
Thanks for your feedback. I will send v6 soon.
Thanks
T

maintainer
#16 lore raw

Re: [PATCH RFC v5 2/4] crypto: add PKE API

Quoted 9 lines · Tadeusz Struk
Quoted 4 lines · Herbert Xu
Quoted 2 lines
Quoted 2 lines

+ * @setkey: Function invokes the algorithm specific set key function, which
+ * knows how to decode and interpret the BER encoded key

We should split this into two functions: setpubkey and setprivkey.

The two functions will be almost identical. We can do it this way if we want to check if all the required elements of the key are provided. Currently I'm checking this in the actual operation.

Right now your RSA setkey function only works if you supply both the public key and the private key. If the user supplies only one key how are you going to tell whether it's public or private?

Cheers,

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

Re: [PATCH RFC v5 2/4] crypto: add PKE API

Quoted 6 lines · Herbert Xu
Quoted 3 lines

The two functions will be almost identical. We can do it this way if we want to check

Quoted 2 lines

if all the required elements of the key are provided. Currently I'm checking this in the actual operation.

Right now your RSA setkey function only works if you supply both the public key and the private key. If the user supplies only one key how are you going to tell whether it's public or private?

User can supply only public key and invoke encrypt() or verify() without any problem. When the user invokes decrypt() or sign() then it will work only after the setkey was given a private key. This is checked in the actual operation.
So the user is responsible for providing an appropriate key for given operation. We can split it if you think this is the right thing to do, but currently it works fine.

maintainer
#18 lore raw

Re: [PATCH RFC v5 2/4] crypto: add PKE API

Quoted 4 lines · Tadeusz Struk

User can supply only public key and invoke encrypt() or verify() without any problem. When the user invokes decrypt() or sign() then it will work only after the setkey was given a private key. This is checked in the actual operation.

The current parse_key function requires all three number to be present, n, e, and d, no?

Cheers,

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

Re: [PATCH RFC v5 2/4] crypto: add PKE API

Quoted 2 lines · Herbert Xu

The current parse_key function requires all three number to be present, n, e, and d, no?

No, it will handle whatever it will find. So if a public key will be passed it will only set "n" and "e". If a private key will be passed it will set all three "n", "e", and "d".
Then during operation I check if there is everything that's required.

maintainer
#20 lore raw

Re: [PATCH RFC v5 2/4] crypto: add PKE API

Quoted 7 lines · Tadeusz Struk
Quoted 2 lines · Herbert Xu

The current parse_key function requires all three number to be present, n, e, and d, no?

No, it will handle whatever it will find. So if a public key will be passed it will only set "n" and "e". If a private key will be passed it will set all three "n", "e", and "d".
Then during operation I check if there is everything that's required.

AFAICS the ASN1 parser will call all three functions and bomb out if any one of them fails. If you did make them all optional then you'd need to check to ensure that at least n and e are present.

Also all your test vectors contain private keys. Please add at least one that contains a public key only to test this.

Cheers,

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

Re: [PATCH RFC v5 2/4] crypto: add PKE API

Quoted 9 lines · Herbert Xu
Quoted 3 lines

No, it will handle whatever it will find. So if a public key will be passed it will only set "n" and "e".

Quoted 2 lines

If a private key will be passed it will set all three "n", "e", and "d". Then during operation I check if there is everything that's required.

AFAICS the ASN1 parser will call all three functions and bomb out if any one of them fails. If you did make them all optional then you'd need to check to ensure that at least n and e are present.

Also all your test vectors contain private keys. Please add at least one that contains a public key only to test this.

I've just tested it and it works fine for both private and public keys. I'll add one vector to test only public key case.
Thanks

#22 lore raw

Re: [PATCH RFC v5 2/4] crypto: add PKE API

In reply to Herbert Xu
Quoted 3 lines · Herbert Xu

Hmm, we could actually get rid of maxsize by just having each function check the dst_len and if it is insufficient write the required length in it and then return an error.

Actually I think it is useful. Without it the user will need to allocate a buffer, and invoke an operation only to find out that the buffer need to be bigger.

maintainer
#23 lore raw

Re: [PATCH RFC v5 2/4] crypto: add PKE API

Quoted 4 lines · Tadeusz Struk

Actually I think it is useful. Without it the user will need to allocate a buffer, and invoke an operation only to find out that the buffer need to be bigger.

No the user could simply supply NULL/0 and get the requisite information if they have no idea what the proper size is.

Cheers,

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

RFC PATCH v5 3/4 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                  |  295 +++++++++++++++++++++++++++++++++++++++++
 crypto/rsa_helper.c           |  134 +++++++++++++++++++
 crypto/rsakey.asn1            |    5 +
 include/crypto/internal/rsa.h |   30 ++++
 6 files changed, 479 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 ed413d9..a09404b 100644
@@ -97,6 +97,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 +295 -0

295 lines not on this page · raw message

crypto/rsa_helper.c new file +134 -0

134 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 +30 -0
new file mode 100644
index 0000000..475d52b
@@ -0,0 +1,30 @@
+/*
+ * 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 crypto_akcipher *tfm, const void *key,
+		  unsigned int key_len);
+static inline struct rsa_key *rsa_get_key(struct crypto_akcipher *tfm)
+{
+	return tfm->key;
+}
+#endif
#25 lore raw

Re: [PATCH RFC v5 3/4] crypto: rsa: add a new rsa generic implementation

Am Montag, 15. Juni 2015, 13:18:47 schrieb Tadeusz Struk:

Hi Tadeusz,

Quoted 2 lines

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

Thank you, that seems to address the issues around the FIPS side including the self test code.

Though, I have one question:

Quoted 4 lines
crypto/rsa_helper.c new file +4 -0
+
+int rsa_get_n(void *context, size_t hdrlen, unsigned char tag,
+	      const void *value, size_t vlen)
+{

crypto/rsa_helper.c line 29

Quoted 10 lines
crypto/rsa_helper.c new file +10 -0
+	struct crypto_akcipher *tfm = context;
+	struct rsa_key *key = tfm->key;
+
+	key->n = mpi_read_raw_data(value, vlen);
+
+	if (!key->n)
+		return -ENOMEM;
+
+	/* In FIPS mode only allow key size minimum 2K */
+	if (fips_enabled && (mpi_get_size(key->n) < 256)) {

Considering my previous email, shouldn't that check rather be

if (fips_enabled &&

    ((mpi_get_size(key->n) != 256) || (mpi_get_size(key->n) != 384))

?

Quoted 26 lines
crypto/rsa_helper.c new file +26 -0
+		pr_err("RSA: key size not allowed in FIPS mode\n");
+		mpi_free(key->n);
+		key->n = NULL;
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+int rsa_get_e(void *context, size_t hdrlen, unsigned char tag,
+	      const void *value, size_t vlen)
+{
+	struct crypto_akcipher *tfm = context;
+	struct rsa_key *key = tfm->key;
+
+	key->e = mpi_read_raw_data(value, vlen);
+
+	if (!key->e)
+		return -ENOMEM;
+
+	return 0;
+}
+
+int rsa_get_d(void *context, size_t hdrlen, unsigned char tag,
+	      const void *value, size_t vlen)
+{

crypto/rsa_helper.c line 65

Quoted 10 lines
crypto/rsa_helper.c new file +10 -0
+	struct crypto_akcipher *tfm = context;
+	struct rsa_key *key = tfm->key;
+
+	key->d = mpi_read_raw_data(value, vlen);
+
+	if (!key->d)
+		return -ENOMEM;
+
+	/* In FIPS mode only allow key size minimum 2K */
+	if (fips_enabled && (mpi_get_size(key->d) < 256)) {

dto.

crypto/rsa_helper.c line 73

Quoted 8 lines
crypto/rsa_helper.c new file +8 -0
+		pr_err("RSA: key size not allowed in FIPS mode\n");
+		mpi_free(key->d);
+		key->d = NULL;
+		return -EINVAL;
+	}
+	return 0;
+}
+

Thanks

Signature · 2 lines
Ciao
Stephan
#26 lore raw

Re: [PATCH RFC v5 3/4] crypto: rsa: add a new rsa generic implementation

Quoted 6 lines · Stephan Mueller
Quoted 2 lines
(quoted diff) +1 -0
+	/* In FIPS mode only allow key size minimum 2K */

crypto/rsa_helper.c line 29

Quoted 1 line
crypto/rsa_helper.c new file +1 -0
+	if (fips_enabled && (mpi_get_size(key->n) < 256)) {

Considering my previous email, shouldn't that check rather be

if (fips_enabled &&

    ((mpi_get_size(key->n) != 256) || (mpi_get_size(key->n) != 384))

I'm not familiar with the FIPS requirements. I checked the NIST recommendations witch states that
RSA: |n| >= 2048 is acceptable. If FIPS allows 2K and 3K only then we need to change it.

#27 lore raw

Re: [PATCH RFC v5 3/4] crypto: rsa: add a new rsa generic implementation

Am Montag, 15. Juni 2015, 18:49:15 schrieb Tadeusz Struk:

Hi Tadeusz,

Quoted 7 lines · Stephan Mueller
Quoted 6 lines
Quoted 3 lines
(quoted diff) +1 -0
+	/* In FIPS mode only allow key size minimum 2K */

crypto/rsa_helper.c line 29

Quoted 2 lines
crypto/rsa_helper.c new file +1 -0
+	if (fips_enabled && (mpi_get_size(key->n) < 256)) {

Considering my previous email, shouldn't that check rather be

Quoted 7 lines
Quoted 3 lines

if (fips_enabled &&

    ((mpi_get_size(key->n) != 256) || (mpi_get_size(key->n) != 384))

I'm not familiar with the FIPS requirements. I checked the NIST recommendations witch states that RSA: |n| >= 2048 is acceptable. If FIPS allows 2K and 3K only then we need to change it.

The reason for exclusive 2k/3k is the CAVS testing: there is only the ability to test 2/3k. Longer key sizes are even not allowed as per SP800-131A in favor of EC.

Signature · 2 lines
Ciao
Stephan
#28 lore raw

Re: [PATCH RFC v5 3/4] crypto: rsa: add a new rsa generic implementation

Quoted 6 lines · Stephan Mueller
Quoted 3 lines

I'm not familiar with the FIPS requirements. I checked the NIST

Quoted 2 lines

recommendations witch states that RSA: |n| >= 2048 is acceptable. If FIPS allows 2K and 3K only then we need to change it.

The reason for exclusive 2k/3k is the CAVS testing: there is only the ability to test 2/3k. Longer key sizes are even not allowed as per SP800-131A in favor of EC.

Ok I'll update it to only accept 2K & 3K. Thanks

#29 lore raw

RFC PATCH v5 4/4 crypto: add tests vectors for RSA

In reply to Tadeusz Struk

New test vectors for RSA algorithm.

---
 crypto/Kconfig   |    1 
 crypto/testmgr.c |  149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 crypto/testmgr.h |  143 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 293 insertions(+)
crypto/Kconfig +1 -0
index a09404b..90c1f77 100644
@@ -107,6 +107,7 @@ config CRYPTO_RSA
 config CRYPTO_MANAGER
 	tristate "Cryptographic algorithm manager"
 	select CRYPTO_MANAGER2
+	select CRYPTO_AKCIPHER
 	help
 	  Create default cryptographic template instantiations such as
 	  cbc(aes).
crypto/testmgr.c +149 -0

179 lines not on this page · raw message

crypto/testmgr.h +143 -0

152 lines not on this page · raw message

maintainer
#30 lore raw

Re: [PATCH RFC v5 4/4] crypto: add tests vectors for RSA

Quoted 14 lines · Tadeusz Struk

New test vectors for RSA algorithm.

---
 crypto/Kconfig   |    1 
 crypto/testmgr.c |  149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 crypto/testmgr.h |  143 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 293 insertions(+)
crypto/Kconfig +0 -0
index a09404b..90c1f77 100644

crypto/Kconfig config CRYPTO_RSA · line 110

Quoted 4 lines
crypto/Kconfig +1 -0
@@ -107,6 +107,7 @@ config CRYPTO_RSA
 config CRYPTO_MANAGER
 	tristate "Cryptographic algorithm manager"
 	select CRYPTO_MANAGER2
+	select CRYPTO_AKCIPHER

Please add this to CRYPTO_MANAGER2 instead.

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

Re: [PATCH RFC v5 4/4] crypto: add tests vectors for RSA

Quoted 5 lines · Herbert Xu
Quoted 4 lines

config CRYPTO_MANAGER

crypto/Kconfig config CRYPTO_RSA · line 110

Quoted 3 lines
crypto/Kconfig +1 -0
 	tristate "Cryptographic algorithm manager"
 	select CRYPTO_MANAGER2
+	select CRYPTO_AKCIPHER

Please add this to CRYPTO_MANAGER2 instead.

This causes a recursive dependency error

crypto/Kconfig:115:error: recursive dependency detected!
crypto/Kconfig:115: symbol CRYPTO_MANAGER2 default value contains CRYPTO_ALGAPI
crypto/Kconfig:34: symbol CRYPTO_ALGAPI is selected by CRYPTO_AKCIPHER
crypto/Kconfig:94: symbol CRYPTO_AKCIPHER is selected by CRYPTO_MANAGER2

maintainer
#32 lore raw

Re: [PATCH RFC v5 4/4] crypto: add tests vectors for RSA

Quoted 7 lines · Tadeusz Struk
Quoted 5 lines · Herbert Xu
Quoted 4 lines

config CRYPTO_MANAGER

crypto/Kconfig config CRYPTO_RSA · line 110

Quoted 3 lines
crypto/Kconfig +1 -0
 	tristate "Cryptographic algorithm manager"
 	select CRYPTO_MANAGER2
+	select CRYPTO_AKCIPHER

Please add this to CRYPTO_MANAGER2 instead.

Quoted 6 lines

This causes a recursive dependency error

crypto/Kconfig:115:error: recursive dependency detected!
crypto/Kconfig:115: symbol CRYPTO_MANAGER2 default value contains CRYPTO_ALGAPI
crypto/Kconfig:34: symbol CRYPTO_ALGAPI is selected by CRYPTO_AKCIPHER
crypto/Kconfig:94: symbol CRYPTO_AKCIPHER is selected by CRYPTO_MANAGER2

That's because you need to split AKCIPHER into AKCIPHER and AKCIPHER2 just like every other type.

Cheers,

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