crypto: Introduce Public Key Encryption API
Subsystems (3)
v4 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 v4:
- add a 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_get_buf(), mpi_copy() and mpi_get_size() helpers
crypto: add PKE API
crypto: RSA: KEYS: convert rsa and public key to new PKE API
crypto: add tests vectors for RSA
crypto/Kconfig | 14 ++ crypto/Makefile | 2 crypto/akcipher.c | 155 +++++++++++++++++ crypto/crypto_user.c | 23 ++ crypto/rsa.c | 289 +++++++++++++++++++++++++++++++ crypto/testmgr.c | 164 ++++++++++++++++++ crypto/testmgr.h | 142 +++++++++++++++ include/crypto/akcipher.h | 408 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/crypto.h | 1 include/linux/cryptouser.h | 6 + include/linux/mpi.h | 3 lib/mpi/mpicoder.c | 85 +++++++-- lib/mpi/mpiutil.c | 46 +++++ 13 files changed, 1319 insertions(+), 19 deletions(-) create mode 100644 crypto/akcipher.c create mode 100644 crypto/rsa.c create mode 100644 include/crypto/akcipher.h
RFC PATCH v4 1/4 MPILIB: add mpi_read_buf(), mpi_copy() 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. Implemented mpi_copy(), which was declared in mpi.h, but never implemented.
--- include/linux/mpi.h | 3 ++ lib/mpi/mpicoder.c | 86 ++++++++++++++++++++++++++++++++++++++++----------- lib/mpi/mpiutil.c | 47 ++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 19 deletions(-)
include/linux/mpi.h +3 -0
@@ -73,6 +73,7 @@ int mpi_set_ui(MPI w, ulong u);MPI mpi_alloc_set_ui (unsigned long u);void mpi_m_check (MPI a);void mpi_swap (MPI a,MPI b); +unsigned int mpi_get_size (MPI a);/*-- mpicoder.c --*/ MPI do_encode_md (const void *sha_buffer,unsigned nbits);
@@ -81,6 +82,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);
lib/mpi/mpicoder.c +67 -19
110 lines not on this page · raw message
lib/mpi/mpiutil.c +47 -0
58 lines not on this page · raw message
Herbert Xu
maintainerRe: [PATCH RFC v4 1/4] MPILIB: add mpi_read_buf(), mpi_copy() and mpi_get_size() helpers
lib/mpi/mpiutil.c void mpi_free(MPI a) · line 170
Quoted 13 lines · Tadeusz Struk
lib/mpi/mpiutil.c +12
-0+/** +* 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 +*/ +unsigned int mpi_get_size (MPI a) +{ +return a->nlimbs *BYTES_PER_MPI_LIMB ; +} +EXPORT_SYMBOL_GPL (mpi_get_size);
Perhaps inline this function?
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 v4 1/4] MPILIB: add mpi_read_buf(), mpi_copy() and mpi_get_size() helpers
In reply to Tadeusz StrukAm Donnerstag, 11. Juni 2015, 12:05:38 schrieb Tadeusz Struk:
Hi Tadeusz,
lib/mpi/mpicoder.c void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign) · line 206
Quoted 10 lines
+ */
lib/mpi/mpicoder.c +9
-0+void *mpi_get_buffer (MPI a,unsigned *nbytes,int *sign) +{ +uint8_t *buf, *p; +int n, ret; + +if (!nbytes) +return NULL ; + + n =mpi_get_size (a);
Shouldn't n be unsigned int?
Ciao
Stephan
Re: [PATCH RFC v4 1/4] MPILIB: add mpi_read_buf(), mpi_copy() and mpi_get_size() helpers
Quoted 10 lines · Stephan Mueller
Quoted 9 lines
+void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
lib/mpi/mpicoder.c void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign) · line 206
Quoted 8 lines
lib/mpi/mpicoder.c +8
-0+{ +uint8_t *buf, *p; +int n, ret; + +if (!nbytes) +return NULL ; + + n =mpi_get_size (a);Shouldn't n be unsigned int?
You are right, thanks.
RFC PATCH v4 2/4 crypto: add PKE API
In reply to Tadeusz StrukAdd Public Key Encryption API.
--- crypto/Kconfig | 6 + crypto/Makefile | 1 crypto/akcipher.c | 154 +++++++++++++++++ crypto/crypto_user.c | 23 ++ include/crypto/akcipher.h | 408 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/crypto.h | 1 include/linux/cryptouser.h | 6 + 7 files changed, 599 insertions(+) create mode 100644 crypto/akcipher.c create mode 100644 include/crypto/akcipher.h
crypto/Kconfig +6 -0
@@ -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
@@ -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 +154 -0
154 lines not on this page · raw message
crypto/crypto_user.c +23 -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,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 +408 -0
408 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 +6 -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,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))
Re: [PATCH RFC v4 2/4] crypto: add PKE API
Am Thursday 11 June 2015, 12:05:44 schrieb Tadeusz Struk:
Hi Tadeusz,
Quoted 14 lines
crypto/akcipher.c new file +14
-0+ +static int akcipher_clone_key (struct crypto_akcipher *tfm, +const struct public_key *pkey) +{ +int i, ret =0 ; + + tfm->pkey =kzalloc (sizeof (*tfm->pkey),GFP_KERNEL ); + +if (!tfm->pkey) +return -ENOMEM ; + +for (i =0 ; i <ARRAY_SIZE (tfm->pkey->mpi); i++) { +if (!pkey->mpi[i]) +continue ;
crypto/akcipher.c line 124
Quoted 10 lines
crypto/akcipher.c new file +10
-0+ +if (mpi_copy (&tfm->pkey->mpi[i], pkey->mpi[i])) { +akcipher_free_key (tfm->pkey); + tfm->pkey =NULL ; + ret = -ENOMEM ; +break ; + } + } +return ret; +}
The testmgr code can mark an entire cipher implementation as fips_allowed=1 as already done for RSA. However, unlike with the other ciphers, that flag must go in conjunction with the used key sizes.
For FIPS mode, the following restrictions apply:
- RSA: 2048/3072
- DSA: L 2048 / N 224; L 2048 / N 256; L 3072 / N 256
- ECDSA: only the NIST curves
Any other key sizes for the given ciphers is not allowed in FIPS mode.
Should that constraint be considered here?
Ciao
Stephan
Herbert Xu
maintainerRe: [PATCH RFC v4 2/4] crypto: add PKE API
Quoted 9 lines · Stephan Mueller
The testmgr code can mark an entire cipher implementation as fips_allowed=1 as already done for RSA. However, unlike with the other ciphers, that flag must go in conjunction with the used key sizes.
For FIPS mode, the following restrictions apply:
- RSA: 2048/3072
Quoted 7 lines
- DSA: L 2048 / N 224; L 2048 / N 256; L 3072 / N 256
- ECDSA: only the NIST curves
Any other key sizes for the given ciphers is not allowed in FIPS mode.
Should that constraint be considered here?
Yes. However it should be placed into a helper that everybody can call so that future hardware implementations can also make the same checks.
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
Herbert Xu
maintainerRe: [PATCH RFC v4 2/4] crypto: add PKE API
Quoted 14 lines · Herbert Xu
Quoted 13 lines · Stephan Mueller
The testmgr code can mark an entire cipher implementation as fips_allowed=1 as already done for RSA. However, unlike with the other ciphers, that flag must go in conjunction with the used key sizes.
For FIPS mode, the following restrictions apply:
- RSA: 2048/3072
- DSA: L 2048 / N 224; L 2048 / N 256; L 3072 / N 256
- ECDSA: only the NIST curves
Quoted 7 lines
Quoted 3 lines
Any other key sizes for the given ciphers is not allowed in FIPS mode.
Should that constraint be considered here?
Yes. However it should be placed into a helper that everybody can call so that future hardware implementations can also make the same checks.
Ugh I didn't notice this was the generic API setkey function. No it should go into the algorithm-specific setkey function and then be in the form of a helper so that all implementations of that algorithm can call it.
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 v4 2/4] crypto: add PKE API
In reply to Herbert XuQuoted 10 lines · Herbert Xu
Quoted 10 lines
The testmgr code can mark an entire cipher implementation as fips_allowed=1 as
Quoted 9 lines
already done for RSA. However, unlike with the other ciphers, that flag must go in conjunction with the used key sizes.
For FIPS mode, the following restrictions apply:
- RSA: 2048/3072
- DSA: L 2048 / N 224; L 2048 / N 256; L 3072 / N 256
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
- ECDSA: only the NIST curves
Any other key sizes for the given ciphers is not allowed in FIPS mode.
Should that constraint be considered here?
Yes. However it should be placed into a helper that everybody can call so that future hardware implementations can also make the same checks.
I will make the algorithm marked as fips allowed always and then fail in setkey if fips is enabled and the given key doesn't meet the fips requirement.
Thanks
Herbert Xu
maintainerRe: [PATCH RFC v4 2/4] crypto: add PKE API
In reply to Tadeusz Strukcrypto/akcipher.c line 133
Quoted 9 lines · Tadeusz Struk
crypto/akcipher.c new file +8
-0+int crypto_akcipher_setkey (struct crypto_akcipher *tfm, +const struct public_key *pkey) +{ +if (tfm->pkey) +akcipher_free_key (tfm->pkey); + +return akcipher_clone_key (tfm, pkey); +}
No please do not expose the struct public_key crap to the new API. The key should be completely opaque to entities outside of the algorithm. So make it raw and read out the MPIs from it.
The contents of the function must go into the algorithm setkey function, not the crypto API. So RSA would read out however many MPIs it needs and verify it, and so on.
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 v4 2/4] crypto: add PKE API
Quoted 13 lines · Herbert Xu
Quoted 8 lines
+int crypto_akcipher_setkey(struct
crypto_akcipher*tfm,crypto/akcipher.c line 133
Quoted 7 lines
crypto/akcipher.c new file +7
-0+ const struct public_key *pkey) +{ +if (tfm->pkey) +akcipher_free_key (tfm->pkey); + +return akcipher_clone_key (tfm, pkey); +}No please do not expose the struct
public_keycrap to the new API. The key should be completely opaque to entities outside of the algorithm. So make it raw and read out the MPIs from it.
Quoted 3 lines
The contents of the function must go into the algorithm setkey function, not the crypto API. So RSA would read out however many MPIs it needs and verify it, and so on.
Should I make it MPI[] rather than void *
Herbert Xu
maintainerRe: [PATCH RFC v4 2/4] crypto: add PKE API
Quoted 2 lines · Tadeusz Struk
Should I make it MPI[] rather than void *
It should be in encoded format since only the algorithms should be decoding it. Otherwise you have craziness where RSA-specific knowledge is placed into x509_cert_parser.c to parse RSA keys.
It's probably easiest to go with the BER encoding that the only current user is using.
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
RFC PATCH v4 3/4 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 | 1 crypto/rsa.c | 289 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 297 insertions(+) create mode 100644 crypto/rsa.c
crypto/Kconfig +7 -0
@@ -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 +1 -0
@@ -29,6 +29,7 @@ obj-$(CONFIG_CRYPTO_HASH2) += crypto_hash.o obj-$(CONFIG_CRYPTO_PCOMP2) += pcompress.o obj-$(CONFIG_CRYPTO_AKCIPHER) += akcipher.o +obj-$(CONFIG_CRYPTO_RSA) += rsa.o cryptomgr-y := algboss.o testmgr.o
crypto/rsa.c new file +289 -0
289 lines not on this page · raw message
RFC PATCH v4 4/4 crypto: add tests vectors for RSA
In reply to Tadeusz StrukNew test vectors for RSA algorithm.
--- crypto/Kconfig | 1 crypto/testmgr.c | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ crypto/testmgr.h | 142 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 307 insertions(+)
crypto/Kconfig +1 -0
@@ -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 +164 -0
194 lines not on this page · raw message
crypto/testmgr.h +142 -0
151 lines not on this page · raw message