crypto: Introduce Public Key Encryption API
Subsystems (3)
v7 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 v7:
- change req->dst_len to be int instead of int *
- remove result_len from comments - it's not longer there
- use crypto_akcipher_reqtfm helper instead of __crypto_akcipher_tfm
- fix memleak in rsa.c
- change error code to -EOVERFLOW if dst buf is not big enough
- drop redundant cra_ctxsize inits
- add type-safe init/exit functions
- cleanup headers in rsa_helpers
- remove checks for NULL before mpi_free
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 | 117 ++++++++++++ crypto/crypto_user.c | 22 ++ crypto/rsa.c | 316 +++++++++++++++++++++++++++++++++ crypto/rsa_helper.c | 121 +++++++++++++ crypto/rsakey.asn1 | 5 + crypto/testmgr.c | 156 +++++++++++++++++ crypto/testmgr.h | 187 ++++++++++++++++++++ include/crypto/akcipher.h | 340 ++++++++++++++++++++++++++++++++++++ include/crypto/internal/akcipher.h | 60 ++++++ include/crypto/internal/rsa.h | 27 +++ include/linux/crypto.h | 1 include/linux/cryptouser.h | 5 + 14 files changed, 1385 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
RFC PATCH v7 3/3 crypto: add tests vectors for RSA
New test vectors for RSA algorithm.
--- crypto/Kconfig | 1 crypto/testmgr.c | 158 ++++++++++++++++++++++++++++++++++++++++++++++ crypto/testmgr.h | 187 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 346 insertions(+)
crypto/Kconfig +1 -0
@@ -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 +158 -0
188 lines not on this page · raw message
crypto/testmgr.h +187 -0
196 lines not on this page · raw message
RFC PATCH v7 1/3 crypto: add PKE API
In reply to Tadeusz StrukAdd Public Key Encryption API.
--- crypto/Kconfig | 11 + crypto/Makefile | 1 crypto/akcipher.c | 117 ++++++++++++ crypto/crypto_user.c | 22 ++ include/crypto/akcipher.h | 340 ++++++++++++++++++++++++++++++++++++ include/crypto/internal/akcipher.h | 60 ++++++ include/linux/crypto.h | 1 include/linux/cryptouser.h | 5 + 8 files changed, 557 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
@@ -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
@@ -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_AKCIPHER2) += akcipher.o cryptomgr-y := algboss.o testmgr.o
crypto/akcipher.c new file +117 -0
117 lines not on this page · raw message
crypto/crypto_user.c +22 -0
@@ -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 +340 -0
340 lines not on this page · raw message
include/crypto/internal/akcipher.h new file +60 -0
60 lines not on this page · raw message
include/linux/crypto.h +1 -0
@@ -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
@@ -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))
RFC PATCH v7 2/3 crypto: rsa: add a new rsa generic implementation
In reply to Tadeusz StrukAdd a new rsa generic SW implementation.
This implements only cryptographic primitives.
--- crypto/Kconfig | 7 + crypto/Makefile | 8 + crypto/rsa.c | 315 +++++++++++++++++++++++++++++++++++++++++ crypto/rsa_helper.c | 121 ++++++++++++++++ crypto/rsakey.asn1 | 5 + include/crypto/internal/rsa.h | 27 ++++ 6 files changed, 483 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
@@ -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
@@ -30,6 +30,14 @@ obj-$(CONFIG_CRYPTO_HASH2) += crypto_hash.o obj-$(CONFIG_CRYPTO_PCOMP2) += pcompress.o obj-$(CONFIG_CRYPTO_AKCIPHER2) += 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 +315 -0
315 lines not on this page · raw message
crypto/rsa_helper.c new file +121 -0
121 lines not on this page · raw message
crypto/rsakey.asn1 new file +5 -0
@@ -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 +27 -0
@@ -0,0 +1,27 @@ +/* +* 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> + +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
Re: [PATCH RFC v7 2/3] crypto: rsa: add a new rsa generic implementation
crypto/rsa.c line 53
Quoted 5 lines · Tadeusz Struk
crypto/rsa.c new file +5
-0+static int _rsa_sign (const struct rsa_key *key,MPI s,MPI m) +{ +/* (1) Validate 0 <= m < n */ +if (mpi_cmp_ui (m,0 ) <0 ||mpi_cmp (m, key->n) >=0 ) +return -EINVAL ;
Why -EINVAL not -EBADMSG?
David
Re: [PATCH RFC v7 2/3] crypto: rsa: add a new rsa generic implementation
Hi David,
Quoted 6 lines · David Howells
Quoted 5 lines
+static int
_rsa_sign(const struct rsa_key *key, MPI s, MPI m)crypto/rsa.c line 25
Quoted 4 lines
crypto/rsa.c new file +4
-0+{ +/* (1) Validate 0 <= m < n */ +if (mpi_cmp_ui (m,0 ) <0 ||mpi_cmp (m, key->n) >=0 ) +return -EINVAL ;Why
-EINVALnot-EBADMSG?
I thought that -EBADMSG was mainly used for authenticated ciphers in case when verification of auth data fails. Since this are input params I thought that -EINVAL would be more appropriate.
I can change it to -EBADMSG, no problem.
Herbert, what do you think?
Herbert Xu
maintainerRe: [PATCH RFC v7 2/3] crypto: rsa: add a new rsa generic implementation
Quoted 5 lines · Tadeusz Struk
I thought that
-EBADMSGwas mainly used for authenticated ciphers in case when verification of auth data fails. Since this are input params I thought that-EINVALwould be more appropriate.
I can change it to-EBADMSG, no problem.
Herbert, what do you think?
The existing crypto/asymmetric_key errno scheme doesn't really mesh in with the rest of crypto. So you'll just have to pick one scheme and stick with it.
I don't really mind either way as long as the error codes are unique and meaningful.
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
Re: [PATCH RFC v7 2/3] crypto: rsa: add a new rsa generic implementation
Quoted 6 lines · Herbert Xu
The existing
crypto/asymmetric_keyerrno scheme doesn't really mesh in with the rest of crypto. So you'll just have to pick one scheme and stick with it.I don't really mind either way as long as the error codes are unique and meaningful.
So I would use -EINVAL since these are input parameters. Do you agree David?
Herbert Xu
maintainerQuoted 8 lines · Tadeusz Struk
This patch set introduces a Public Key Encryption API.
What is proposed is a new crypto type calledcrypto_akcipher_type, plus new structakcipher_algand structcrypto_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:
Quoted 4 lines
int (*sign)(struct akcipher_request *req); int (*verify)(struct akcipher_request *req); int (*encrypt)(struct akcipher_request *req); int (*decrypt)(struct akcipher_request *req);
All applied with two minor changes. First of all I made AKCIPHER invisible to the user like the other type config options. I also added a missing select on ASN1 that broke my build.
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
Quoted 8 lines · Herbert Xu
Quoted 8 lines
This patch set introduces a Public Key Encryption API.
Quoted 7 lines
What is proposed is a new crypto type called
crypto_akcipher_type, plus new structakcipher_algand structcrypto_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:
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
int (*sign)(struct akcipher_request *req); int (*verify)(struct akcipher_request *req); int (*encrypt)(struct akcipher_request *req); int (*decrypt)(struct akcipher_request *req);All applied with two minor changes. First of all I made AKCIPHER invisible to the user like the other type config options. I also added a missing select on ASN1 that broke my build.
Thank you very much for all your help with this Herbert