crypto: Introduce Public Key Encryption API
Subsystems (3)
v3 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_pkey_type plus new struct pkey_alg and struct pkey_tfm together with number of helper functions to register pkey 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 pkey_request *pkeyreq); int (*verify)(struct pkey_request *pkeyreq); int (*encrypt)(struct pkey_request *pkeyreq); int (*decrypt)(struct pkey_request *pkeyreq);
The benefits it gives comparing to the struct public_key_algorithm 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 pkey_tfm 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_pkey *tfm = crypto_alloc_pkey("rsa", 0, 0);
struct pkey_request *req = pkey_request_alloc(tfm, GFP_KERNEL);
pkey_request_set_crypt(req, pub_key, signature);
int ret = crypto_pkey_verify(req);
pkey_request_free(req);
crypto_free_pkey(tfm);
return ret;Additionally existing public_key and rsa code have been reworked to use the new interface for verifying signed modules.
As part of the rework the struct public_key_algorithm type has been removed. Algorithm instance is allocated using crypto_alloc_pkey() and name defined in pkey_algo_name table indexed by pkey_algo enum that comes from the public key. In future this can be replaced by a string name can be obtained directly from the public key cert.
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: KEYS: convert rsa and public key to new PKE API
crypto: add tests vectors for RSA
crypto/Kconfig | 6 crypto/Makefile | 1 crypto/akcipher.c | 100 ++++++ crypto/asymmetric_keys/Kconfig | 1 crypto/asymmetric_keys/Makefile | 1 crypto/asymmetric_keys/pkcs7_parser.c | 2 crypto/asymmetric_keys/pkcs7_trust.c | 2 crypto/asymmetric_keys/pkcs7_verify.c | 2 crypto/asymmetric_keys/public_key.c | 53 +-- crypto/asymmetric_keys/public_key.h | 36 -- crypto/asymmetric_keys/rsa.c | 467 ++++++++++++++++------------- crypto/asymmetric_keys/rsa_pkcs1_v1_5.c | 259 ++++++++++++++++ crypto/asymmetric_keys/x509_cert_parser.c | 2 crypto/asymmetric_keys/x509_public_key.c | 4 crypto/crypto_user.c | 23 + crypto/testmgr.c | 151 +++++++++ crypto/testmgr.h | 86 +++++ include/crypto/akcipher.h | 385 ++++++++++++++++++++++++ include/crypto/public_key.h | 11 - include/linux/crypto.h | 1 include/linux/cryptouser.h | 6 21 files changed, 1299 insertions(+), 300 deletions(-) create mode 100644 crypto/akcipher.c delete mode 100644 crypto/asymmetric_keys/public_key.h create mode 100644 crypto/asymmetric_keys/rsa_pkcs1_v1_5.c create mode 100644 include/crypto/akcipher.h
RFC PATCH v3 2/3 crypto: RSA: KEYS: convert rsa and public key to new PKE API
Change the existing rsa and public key code to integrate it with the new Public Key Encryption API.
--- crypto/asymmetric_keys/Kconfig | 1 crypto/asymmetric_keys/Makefile | 1 crypto/asymmetric_keys/pkcs7_parser.c | 2 crypto/asymmetric_keys/pkcs7_trust.c | 2 crypto/asymmetric_keys/pkcs7_verify.c | 2 crypto/asymmetric_keys/public_key.c | 53 +-- crypto/asymmetric_keys/public_key.h | 36 -- crypto/asymmetric_keys/rsa.c | 467 ++++++++++++++++------------- crypto/asymmetric_keys/rsa_pkcs1_v1_5.c | 259 ++++++++++++++++ crypto/asymmetric_keys/x509_cert_parser.c | 2 crypto/asymmetric_keys/x509_public_key.c | 4 include/crypto/public_key.h | 11 - 12 files changed, 540 insertions(+), 300 deletions(-) delete mode 100644 crypto/asymmetric_keys/public_key.h create mode 100644 crypto/asymmetric_keys/rsa_pkcs1_v1_5.c
crypto/asymmetric_keys/Kconfig +1 -0
@@ -23,6 +23,7 @@ config ASYMMETRIC_PUBLIC_KEY_SUBTYPE config PUBLIC_KEY_ALGO_RSA tristate "RSA public-key algorithm" select MPILIB + select CRYPTO_AKCIPHER help This option enables support for the RSA algorithm (PKCS#1, RFC3447).
crypto/asymmetric_keys/Makefile +1 -0
@@ -8,6 +8,7 @@ asymmetric_keys-y := asymmetric_type.o signature.o obj-$(CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += public_key.o obj-$(CONFIG_PUBLIC_KEY_ALGO_RSA) += rsa.o +obj-$(CONFIG_PUBLIC_KEY_ALGO_RSA) += rsa_pkcs1_v1_5.o # # X.509 Certificate handling
crypto/asymmetric_keys/pkcs7_parser.c +1 -1
@@ -15,7 +15,7 @@#include <linux/slab.h> #include <linux/err.h> #include <linux/oid_registry.h> -#include "public_key.h" +#include <crypto/public_key.h> #include "pkcs7_parser.h" #include "pkcs7-asn1.h"
crypto/asymmetric_keys/pkcs7_trust.c +1 -1
@@ -17,7 +17,7 @@#include <linux/asn1.h> #include <linux/key.h> #include <keys/asymmetric-type.h> -#include "public_key.h" +#include <crypto/public_key.h> #include "pkcs7_parser.h" /**
crypto/asymmetric_keys/pkcs7_verify.c +1 -1
@@ -16,7 +16,7 @@#include <linux/err.h> #include <linux/asn1.h> #include <crypto/hash.h> -#include "public_key.h" +#include <crypto/public_key.h> #include "pkcs7_parser.h" /*
crypto/asymmetric_keys/public_key.c +17 -36
97 lines not on this page · raw message
crypto/asymmetric_keys/public_key.h deleted +0 -36
@@ -1,36 +0,0 @@ -/* Public key algorithm internals -* -* See Documentation/crypto/asymmetric-keys.txt -* -* Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. -* Written by David Howells ([email protected]) -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public Licence -* as published by the Free Software Foundation; either version -* 2 of the Licence, or (at your option) any later version. -*/ - -#include <crypto/public_key.h> - -extern struct asymmetric_key_subtype public_key_subtype; - -/* -* Public key algorithm definition. -*/ -struct public_key_algorithm { -const char *name; -u8 n_pub_mpi;/* Number of MPIs in public key */ -u8 n_sec_mpi;/* Number of MPIs in secret key */ -u8 n_sig_mpi;/* Number of MPIs in a signature */ -int (*verify_signature)(const struct public_key *key, -const struct public_key_signature *sig); -}; - -extern const struct public_key_algorithm RSA_public_key_algorithm; - -/* -* public_key.c -*/ -extern int public_key_verify_signature (const struct public_key *pk, -const struct public_key_signature *sig);
crypto/asymmetric_keys/rsa.c +254 -213
530 lines not on this page · raw message
crypto/asymmetric_keys/rsa_pkcs1_v1_5.c new file +259 -0
259 lines not on this page · raw message
crypto/asymmetric_keys/x509_cert_parser.c +1 -1
@@ -15,7 +15,7 @@#include <linux/slab.h> #include <linux/err.h> #include <linux/oid_registry.h> -#include "public_key.h" +#include <crypto/public_key.h> #include "x509_parser.h" #include "x509-asn1.h" #include "x509_rsakey-asn1.h"
crypto/asymmetric_keys/x509_public_key.c +0 -4
@@ -21,7 +21,6 @@#include <keys/system_keyring.h> #include <crypto/hash.h> #include "asymmetric_keys.h" -#include "public_key.h" #include "x509_parser.h" static bool use_builtin_keys;
@@ -250,8 +249,6 @@ static int x509_key_preparse(struct key_preparsed_payload *prep)if (cert->pub->pkey_algo >=PKEY_ALGO__LAST || cert->sig.pkey_algo >=PKEY_ALGO__LAST || cert->sig.pkey_hash_algo >=PKEY_HASH__LAST || - !pkey_algo[cert->pub->pkey_algo] || - !pkey_algo[cert->sig.pkey_algo] || !hash_algo_name[cert->sig.pkey_hash_algo]) { ret = -ENOPKG ; goto error_free_cert;
@@ -270,7 +267,6 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) pkey_algo_name[cert->sig.pkey_algo], hash_algo_name[cert->sig.pkey_hash_algo]); - cert->pub->algo = pkey_algo[cert->pub->pkey_algo]; cert->pub->id_type =PKEY_ID_X509 ;/* Check the signature on the key if it appears to be self-signed */
include/crypto/public_key.h +4 -7
@@ -25,7 +25,6 @@ enum pkey_algo { };extern const char *const pkey_algo_name[PKEY_ALGO__LAST ]; -extern const struct public_key_algorithm *pkey_algo[PKEY_ALGO__LAST ];/* asymmetric key implementation supports only up to SHA224 */ #define PKEY_HASH__LAST (HASH_ALGO_SHA224 + 1)
@@ -45,12 +44,6 @@ extern const char *const pkey_id_type_name[PKEY_ID_TYPE__LAST];* part. */ struct public_key { -const struct public_key_algorithm *algo; -u8 capabilities; -#define PKEY_CAN_ENCRYPT 0x01 -#define PKEY_CAN_DECRYPT 0x02 -#define PKEY_CAN_SIGN 0x04 -#define PKEY_CAN_VERIFY 0x08enum pkey_algo pkey_algo :8 ;enum pkey_id_type id_type :8 ;union {
@@ -95,6 +88,7 @@ struct public_key_signature { }; }; +extern struct asymmetric_key_subtype public_key_subtype;struct key ;extern int verify_signature (const struct key *key,const struct public_key_signature *sig);
@@ -104,4 +98,7 @@ extern struct key *x509_request_asymmetric_key(struct key *keyring,const struct asymmetric_key_id *kid, bool partial); +int public_key_verify_signature (const struct public_key *pkey, +const struct public_key_signature *sig); +#endif /* _LINUX_PUBLIC_KEY_H */
RFC PATCH v3 3/3 crypto: add tests vectors for RSA
In reply to Tadeusz StrukNew test vectors for RSA algorithm.
--- crypto/testmgr.c | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ crypto/testmgr.h | 86 +++++++++++++++++++++++++++++++ 2 files changed, 237 insertions(+)
crypto/testmgr.c +151 -0
@@ -30,6 +30,8 @@#include <linux/string.h> #include <crypto/rng.h> #include <crypto/drbg.h> +#include <crypto/public_key.h> +#include <crypto/akcipher.h> #include "internal.h"
@@ -116,6 +118,11 @@ struct drbg_test_suite {unsigned int count; }; +struct akcipher_test_suite { +struct akcipher_testvec *vecs; +unsigned int count; +}; +struct alg_test_desc {const char *alg; int (*test)(const struct alg_test_desc *desc, const char *driver,
@@ -130,6 +137,7 @@ struct alg_test_desc {struct hash_test_suite hash;struct cprng_test_suite cprng;struct drbg_test_suite drbg; +struct akcipher_test_suite akcipher; } suite; };
@@ -1825,6 +1833,139 @@ static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver, } +static int do_test_rsa (struct crypto_akcipher *tfm, +struct akcipher_testvec *vecs) +{ +struct akcipher_request *req; +struct public_key pkey; +void *outbuf_enc =NULL ; +void *outbuf_dec =NULL ; +struct tcrypt_result result; +unsigned int out_len = vecs->c_size; +int err = -ENOMEM ; + + req =akcipher_request_alloc (tfm,GFP_KERNEL ); +if (!req) +return err; + + pkey.rsa.n =mpi_read_raw_data (vecs->pub_key_n, vecs->pub_key_n_size); +if (!pkey.rsa.n) + goto free_req; + + pkey.rsa.e =mpi_read_raw_data (vecs->pub_key_e, vecs->pub_key_e_size); +if (!pkey.rsa.e) + goto free_n; + + pkey.rsa.d =mpi_read_raw_data (vecs->sec_key_d, vecs->sec_key_d_size); +if (!pkey.rsa.d) + goto free_e; + + outbuf_enc =kzalloc (vecs->c_size,GFP_KERNEL ); +if (!outbuf_enc) + goto free_d; + +/* Run RSA encrypt - c = m^e mod n;*/ +init_completion (&result.completion); +crypto_akcipher_setkey (tfm, &pkey); +akcipher_request_set_crypt (req, vecs->m, outbuf_enc, vecs->m_size, + out_len, &out_len); +akcipher_request_set_callback (req,CRYPTO_TFM_REQ_MAY_BACKLOG , + tcrypt_complete, &result); + err =wait_async_op (&result,crypto_akcipher_encrypt (req)); +if (err) { +pr_err ("alg: rsa: encrypt test failed. err %d\n" , err); + goto free_all; + } + +if (out_len != vecs->c_size) { + err = -EINVAL ; + goto free_all; + } + + outbuf_dec =kzalloc (out_len,GFP_KERNEL ); +if (!outbuf_dec) { + err = -ENOMEM ; + goto free_all; + } + +init_completion (&result.completion); +akcipher_request_set_crypt (req, outbuf_enc, outbuf_dec, vecs->c_size, + out_len, &out_len); +/* Run RSA decrypt - m = c^d mod n;*/ + err =wait_async_op (&result,crypto_akcipher_decrypt (req)); +if (err) { +pr_err ("alg: rsa: decrypt test failed. err %d\n" , err); + goto free_all; + } + +if (out_len != vecs->m_size) { + err = -EINVAL ; + goto free_all; + } + +/* verify that decrypted message is equal to the original msg */ +if (memcmp (vecs->m, outbuf_dec, vecs->m_size)) { +pr_err ("alg: rsa: encrypt test failed. Invalid output\n" ); + err = -EINVAL ; + } +free_all: +kfree (outbuf_dec); +kfree (outbuf_enc); +free_d: +mpi_free (pkey.rsa.d); +free_e: +mpi_free (pkey.rsa.e); +free_n: +mpi_free (pkey.rsa.n); +free_req: +akcipher_request_free (req); +return err; +} + +static int test_rsa (struct crypto_akcipher *tfm,struct akcipher_testvec *vecs, +unsigned int tcount) +{ +int ret, i; + +for (i =0 ; i < tcount; i++) { + ret =do_test_rsa (tfm, vecs++); +if (ret) { +pr_err ("alg: rsa: test failed on vector %d\n" , i +1 ); +return ret; + } + } +return 0 ; +} + +static int test_akcipher (struct crypto_akcipher *tfm,const char *alg, +struct akcipher_testvec *vecs,unsigned int tcount) +{ +if (strncmp (alg,"rsa" ,3 ) ==0 ) +return test_rsa (tfm, vecs, tcount); + +return 0 ; +} + +static int alg_test_akcipher (const struct alg_test_desc *desc, +const char *driver,u32 type,u32 mask) +{ +struct crypto_akcipher *tfm; +int err =0 ; + + tfm =crypto_alloc_akcipher (driver, type |CRYPTO_ALG_INTERNAL , mask); +if (IS_ERR (tfm)) { +printk (KERN_ERR "alg: akcipher: Failed to load transform for %s: " +"%ld\n" , driver,PTR_ERR (tfm)); +return PTR_ERR (tfm); + } +if (desc->suite.akcipher.vecs) + err =test_akcipher (tfm, desc->alg, desc->suite.akcipher.vecs, + desc->suite.akcipher.count); + +crypto_free_akcipher (tfm); +return err; +} +static int alg_test_null (const struct alg_test_desc *desc,const char *driver,u32 type,u32 mask) {
@@ -3399,6 +3540,16 @@ static const struct alg_test_desc alg_test_descs[] = { } } }, { + .alg = "rsa", + .test = alg_test_akcipher, + .fips_allowed = 1, + .suite = { + .akcipher = { + .vecs = rsa_tv_template, + .count = RSA_TEST_VECTORS + } + } + }, { .alg = "salsa20", .test = alg_test_skcipher, .suite = {
crypto/testmgr.h +86 -0
95 lines not on this page · raw message
RFC PATCH v3 1/3 crypto: add PKE API
In reply to Tadeusz StrukAdd Public Key Encryption API.
--- crypto/Kconfig | 6 + crypto/Makefile | 1 crypto/akcipher.c | 100 +++++++++++ crypto/crypto_user.c | 23 +++ include/crypto/akcipher.h | 385 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/crypto.h | 1 include/linux/cryptouser.h | 6 + 7 files changed, 522 insertions(+) create mode 100644 crypto/akcipher.c create mode 100644 include/crypto/akcipher.h
crypto/Kconfig +6 -0
@@ -87,6 +87,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 +100 -0
100 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 +385 -0
385 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 v3 3/3] crypto: add tests vectors for RSA
In reply to Tadeusz StrukAm Mittwoch, 3. Juni 2015, 15:44:24 schrieb Tadeusz Struk:
Hi Tadeusz,
Quoted 86 lines
New test vectors for RSA algorithm.
Signed-off-by Tadeusz Struk <[email protected]>--- crypto/testmgr.c | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ crypto/testmgr.h | 86 +++++++++++++++++++++++++++++++ 2 files changed, 237 insertions(+)crypto/testmgr.c +47
-0@@ -30,6 +30,8 @@#include <linux/string.h> #include <crypto/rng.h> #include <crypto/drbg.h> +#include <crypto/public_key.h> +#include <crypto/akcipher.h> #include "internal.h" @@ -116,6 +118,11 @@ struct drbg_test_suite {unsigned int count; }; +struct akcipher_test_suite { +struct akcipher_testvec *vecs; +unsigned int count; +}; +struct alg_test_desc {const char *alg; int (*test)(const struct alg_test_desc *desc, const char *driver,@@ -130,6 +137,7 @@ struct alg_test_desc {struct hash_test_suite hash;struct cprng_test_suite cprng;struct drbg_test_suite drbg; +struct akcipher_test_suite akcipher; } suite; };@@ -1825,6 +1833,139 @@ static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver, } +static int do_test_rsa (struct crypto_akcipher *tfm, +struct akcipher_testvec *vecs) +{ +struct akcipher_request *req; +struct public_key pkey; +void *outbuf_enc =NULL ; +void *outbuf_dec =NULL ; +struct tcrypt_result result; +unsigned int out_len = vecs->c_size; +int err = -ENOMEM ; + + req =akcipher_request_alloc (tfm,GFP_KERNEL ); +if (!req) +return err; + + pkey.rsa.n =mpi_read_raw_data (vecs->pub_key_n, vecs->pub_key_n_size); +if (!pkey.rsa.n) + goto free_req; + + pkey.rsa.e =mpi_read_raw_data (vecs->pub_key_e, vecs->pub_key_e_size); +if (!pkey.rsa.e) + goto free_n; + + pkey.rsa.d =mpi_read_raw_data (vecs->sec_key_d, vecs->sec_key_d_size); +if (!pkey.rsa.d) + goto free_e; + + outbuf_enc =kzalloc (vecs->c_size,GFP_KERNEL ); +if (!outbuf_enc) + goto free_d; + +/* Run RSA encrypt - c = m^e mod n;*/ +init_completion (&result.completion); +crypto_akcipher_setkey (tfm, &pkey); +akcipher_request_set_crypt (req, vecs->m, outbuf_enc, vecs->m_size, + out_len, &out_len); +akcipher_request_set_callback (req,CRYPTO_TFM_REQ_MAY_BACKLOG , + tcrypt_complete, &result); + err =wait_async_op (&result,crypto_akcipher_encrypt (req));
crypto/testmgr.c static int alg_test_drbg(const struct alg_test_desc · line 1885
Quoted 10 lines
crypto/testmgr.c +10
-0+if (err) { +pr_err ("alg: rsa: encrypt test failed. err %d\n" , err); + goto free_all; + } + +if (out_len != vecs->c_size) { + err = -EINVAL ; + goto free_all; + } +
May I ask that the outbuf_enc is memcmp()ed with an expected value? This check is required for FIPS 140-2 compliance. Without that memcmp, FIPS 140-2 validations will not be successful.
Quoted 63 lines
crypto/testmgr.c +62
-0+ outbuf_dec =kzalloc (out_len,GFP_KERNEL ); +if (!outbuf_dec) { + err = -ENOMEM ; + goto free_all; + } + +init_completion (&result.completion); +akcipher_request_set_crypt (req, outbuf_enc, outbuf_dec, vecs->c_size, + out_len, &out_len); +/* Run RSA decrypt - m = c^d mod n;*/ + err =wait_async_op (&result,crypto_akcipher_decrypt (req)); +if (err) { +pr_err ("alg: rsa: decrypt test failed. err %d\n" , err); + goto free_all; + } + +if (out_len != vecs->m_size) { + err = -EINVAL ; + goto free_all; + } + +/* verify that decrypted message is equal to the original msg */ +if (memcmp (vecs->m, outbuf_dec, vecs->m_size)) { +pr_err ("alg: rsa: encrypt test failed. Invalid output\n" ); + err = -EINVAL ; + } +free_all: +kfree (outbuf_dec); +kfree (outbuf_enc); +free_d: +mpi_free (pkey.rsa.d); +free_e: +mpi_free (pkey.rsa.e); +free_n: +mpi_free (pkey.rsa.n); +free_req: +akcipher_request_free (req); +return err; +} + +static int test_rsa (struct crypto_akcipher *tfm,struct akcipher_testvec *vecs, + unsigned int tcount) +{ +int ret, i; + +for (i =0 ; i < tcount; i++) { + ret =do_test_rsa (tfm, vecs++); +if (ret) { +pr_err ("alg: rsa: test failed on vector %d\n" , i +1 ); +return ret; + } + } +return 0 ; +} + +static int test_akcipher (struct crypto_akcipher *tfm,const char *alg, +struct akcipher_testvec *vecs,unsigned int tcount) +{ +if (strncmp (alg,"rsa" ,3 ) ==0 ) +return test_rsa (tfm, vecs, tcount); + +return 0 ; +}
crypto/testmgr.c static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
Quoted 10 lines
crypto/testmgr.c +10
-0+ +static int alg_test_akcipher(const struct alg_test_desc *desc, + const char *driver, u32 type, u32 mask) +{ + struct crypto_akcipher *tfm; + int err = 0; + + tfm = crypto_alloc_akcipher(driver, type | CRYPTO_ALG_INTERNAL, mask); + if (IS_ERR(tfm)) { + printk(KERN_ERR "alg: akcipher: Failed to load transform for
%s: "
Quoted 114 lines
crypto/testmgr.c +21
-0+ "%ld\n", driver, PTR_ERR(tfm)); +return PTR_ERR (tfm); + } +if (desc->suite.akcipher.vecs) + err =test_akcipher (tfm, desc->alg, desc->suite.akcipher.vecs, + desc->suite.akcipher.count); + +crypto_free_akcipher (tfm); +return err; +} +static int alg_test_null (const struct alg_test_desc *desc,const char *driver,u32 type,u32 mask) {@@ -3399,6 +3540,16 @@ static const struct alg_test_desc alg_test_descs[] = { } } }, { + .alg = "rsa", + .test = alg_test_akcipher, + .fips_allowed = 1, + .suite = { + .akcipher = { + .vecs = rsa_tv_template, + .count = RSA_TEST_VECTORS + } + } + }, { .alg = "salsa20", .test = alg_test_skcipher, .suite = {crypto/testmgr.h +72
-0@@ -107,9 +107,95 @@ struct drbg_testvec {size_t expectedlen; }; +struct akcipher_testvec { +unsigned char *pub_key_n; +unsigned char *pub_key_e; +unsigned char *sec_key_d; +unsigned char *m; +unsigned int pub_key_n_size; +unsigned int pub_key_e_size; +unsigned int sec_key_d_size; +unsigned int m_size; +unsigned int c_size;/* size of encrypted message */ +}; +static char zeroed_string[48 ];/* +* RSA test vectors. Borrowed from openSSL. +*/ +#define RSA_TEST_VECTORS 3 + +static struct akcipher_testvec rsa_tv_template [] = { + { + .pub_key_n = +"\x00\xAA\x36\xAB\xCE\x88\xAC\xFD\xFF\x55\x52\x3C\x7F\xC4\x52\x3F" +"\x90\xEF\xA0\x0D\xF3\x77\x4A\x25\x9F\x2E\x62\xB4\xC5\xD9\x9C\xB5" +"\xAD\xB3\x00\xA0\x28\x5E\x53\x01\x93\x0E\x0C\x70\xFB\x68\x76\x93" +"\x9C\xE6\x16\xCE\x62\x4A\x11\xE0\x08\x6D\x34\x1E\xBC\xAC\xA0\xA1" +"\xF5" , + .pub_key_e ="\x11" , + .sec_key_d = +"\x0A\x03\x37\x48\x62\x64\x87\x69\x5F\x5F\x30\xBC\x38\xB9\x8B\x44" +"\xC2\xCD\x2D\xFF\x43\x40\x98\xCD\x20\xD8\xA1\x38\xD0\x90\xBF\x64" +"\x79\x7C\x3F\xA7\xA2\xCD\xCB\x3C\xD1\xE0\xBD\xBA\x26\x54\xB4\xF9" +"\xDF\x8E\x8A\xE5\x9D\x73\x3D\x9F\x33\xB3\x01\x62\x4A\xFD\x1D\x51" , + .m ="\x54\x85\x9b\x34\x2c\x49\xea\x2a" , + .pub_key_n_size =65 , + .pub_key_e_size =1 , + .sec_key_d_size =64 , + .m_size =8 , + .c_size =64 , + }, { + .pub_key_n = +"\x00\xA3\x07\x9A\x90\xDF\x0D\xFD\x72\xAC\x09\x0C\xCC\x2A\x78\xB8" +"\x74\x13\x13\x3E\x40\x75\x9C\x98\xFA\xF8\x20\x4F\x35\x8A\x0B\x26" +"\x3C\x67\x70\xE7\x83\xA9\x3B\x69\x71\xB7\x37\x79\xD2\x71\x7B\xE8" +"\x34\x77\xCF" , + .pub_key_e ="\x3" , + .sec_key_d = +"\x6C\xAF\xBC\x60\x94\xB3\xFE\x4C\x72\xB0\xB3\x32\xC6\xFB\x25\xA2" +"\xB7\x62\x29\x80\x4E\x68\x65\xFC\xA4\x5A\x74\xDF\x0F\x8F\xB8\x41" +"\x3B\x52\xC0\xD0\xE5\x3D\x9B\x59\x0F\xF1\x9B\xE7\x9F\x49\xDD\x21" +"\xE5\xEB" , + .m ="\x54\x85\x9b\x34\x2c\x49\xea\x2a" , + .pub_key_n_size =51 , + .pub_key_e_size =1 , + .sec_key_d_size =50 , + .m_size =8 , + .c_size =24 , + }, { + .pub_key_n = +"\x00\xBB\xF8\x2F\x09\x06\x82\xCE\x9C\x23\x38\xAC\x2B\x9D\xA8\x71" +"\xF7\x36\x8D\x07\xEE\xD4\x10\x43\xA4\x40\xD6\xB6\xF0\x74\x54\xF5" +"\x1F\xB8\xDF\xBA\xAF\x03\x5C\x02\xAB\x61\xEA\x48\xCE\xEB\x6F\xCD" +"\x48\x76\xED\x52\x0D\x60\xE1\xEC\x46\x19\x71\x9D\x8A\x5B\x8B\x80" +"\x7F\xAF\xB8\xE0\xA3\xDF\xC7\x37\x72\x3E\xE6\xB4\xB7\xD9\x3A\x25" +"\x84\xEE\x6A\x64\x9D\x06\x09\x53\x74\x88\x34\xB2\x45\x45\x98\x39" +"\x4E\xE0\xAA\xB1\x2D\x7B\x61\xA5\x1F\x52\x7A\x9A\x41\xF6\xC1\x68" +"\x7F\xE2\x53\x72\x98\xCA\x2A\x8F\x59\x46\xF8\xE5\xFD\x09\x1D\xBD" +"\xCB" , + .pub_key_e ="\x11" , + .sec_key_d = +"\x00\xA5\xDA\xFC\x53\x41\xFA\xF2\x89\xC4\xB9\x88\xDB\x30\xC1\xCD" +"\xF8\x3F\x31\x25\x1E\x06\x68\xB4\x27\x84\x81\x38\x01\x57\x96\x41" +"\xB2\x94\x10\xB3\xC7\x99\x8D\x6B\xC4\x65\x74\x5E\x5C\x39\x26\x69" +"\xD6\x87\x0D\xA2\xC0\x82\xA9\x39\xE3\x7F\xDC\xB8\x2E\xC9\x3E\xDA" +"\xC9\x7F\xF3\xAD\x59\x50\xAC\xCF\xBC\x11\x1C\x76\xF1\xA9\x52\x94"
crypto/testmgr.h struct drbg_testvec { · line 194
Quoted 10 lines
crypto/testmgr.h +10
-0+"\x44\xE5\x6A\xAF\x68\xC5\x6C\x09\x2C\xD3\x8D\xC3\xBE\xF5\xD2\x0A" +"\x93\x99\x26\xED\x4F\x74\xA1\x3E\xDD\xFB\xE1\xA1\xCE\xCC\x48\x94" +"\xAF\x94\x28\xC2\xB7\xB8\x88\x3F\xE4\x46\x3A\x4B\xC8\x5B\x1C\xB3" + "\xC1", + .m = "\x54\x85\x9b\x34\x2c\x49\xea\x2a", + .pub_key_n_size = 129, + .pub_key_e_size = 1, + .sec_key_d_size = 129, + .m_size = 8, + .c_size = 128,
Sorry for bringing that one up just now: 512 and 1024 bit test vectors will not be helpful for several use cases, including FIPS. I can offer to give you 2k or 3k vectors. Besides, wouldn't one vector be sufficient?
Quoted 12 lines
crypto/testmgr.h +4
-1+ } +}; + +/* * MD4 test vectors from RFC1320 */ #define MD4_TEST_VECTORS 7 --To unsubscribe from this list: send the line "unsubscribe linux-crypto" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 2 lines
Ciao Stephan
Herbert Xu
maintainerRe: [PATCH RFC v3 1/3] crypto: add PKE API
In reply to Tadeusz StrukQuoted 14 lines · Tadeusz Struk
include/crypto/akcipher.h new file +13
-0+/** +* struct akcipher_alg - generic public key algorithm +* +* @sign: Function performs a sign operation as defined by public key +* algorithm +* @verify: Function performs a sign operation as defined by public key +* algorithm +* @encrypt: Function performs an encrytp operation as defined by public key +* algorithm +* @decrypt: Function performs a decrypt operation as defined by public key +* algorithm +* @reqsize: Request context size required by algorithm implementation +* @base: Common crypto API algorithm data structure
include/crypto/akcipher.h line 65
Quoted 10 lines
include/crypto/akcipher.h new file +10
-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); + +unsigned int reqsize; +struct crypto_alg base; +};
Because the caller is going to be allocating memory for the output, we need to provide a way for them to know how much memory to allocate.
This presumably will depend on the key size.
So something like
int (*maxsize)(struct crypto_akcipher *tfm);
is needed.
You should also provide setkey here. You can't just save a pointer to the key. The transform must hold the key physically as the original may go away. It should also ensure that the key is actually valid for the transform.
Quoted 4 lines
include/crypto/akcipher.h new file +4
-0+/** +* struct crypto_akcipher - user-instantiated objects which encapsulate +* algorithms and core processing logic +*
include/crypto/akcipher.h line 80
Quoted 10 lines
include/crypto/akcipher.h new file +10
-0+* @base: Common crypto API algorithm data structure +* @pkey: Key representation. Note: this can be both public or private +* key, depending on the operation. +* @__ctx: Start of private context data +*/ +struct crypto_akcipher { +struct crypto_tfm base; +const struct public_key *pkey; + void *__ctx[] CRYPTO_MINALIGN_ATTR; +};
base already has ctx so you should get rid of ctx and move base to the end of the struct.
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 v3 2/3] crypto: RSA: KEYS: convert rsa and public key to new PKE API
In reply to Tadeusz StrukQuoted 4 lines · Tadeusz Struk
Change the existing rsa and public key code to integrate it with the new Public Key Encryption API.
Signed-off-by Tadeusz Struk <[email protected]>
I'd like to see this split into multiple patches. First of all the new crypto_akcipher implementation should coexist with the existing code. That way the exiting users can be converted over one-by-one.
Also you should implement the crypto_akcipher completely before converting anybody over, that means doing encoding/wrapping in addition to the crypto.
That way we don't have to have craziness like converting in and out of MPI multiple times.
Lastly you should consider adding an MPI helper that writes to an existing buffer instead of allocating a new one and copying it over.
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
Re: [PATCH RFC v3 3/3] crypto: add tests vectors for RSA
In reply to Stephan MuellerHi Stephan
Quoted 3 lines · Stephan Mueller
May I ask that the
outbuf_encis memcmp()ed with an expected value? This check is required for FIPS 140-2 compliance. Without that memcmp, FIPS 140-2 validations will not be successful.
Sure, I will do that. I wasn't aware that this was required.
Quoted 4 lines
Sorry for bringing that one up just now: 512 and 1024 bit test vectors will not be helpful for several use cases, including FIPS. I can offer to give you 2k or 3k vectors.
I have one 2K vector from openSSL fips so I'll use it instead of the 512 one.
Quoted 1 line
Besides, wouldn't one vector be sufficient?
I think there is no harm to have these 3 vectors to make sure an implementation is well tested.
Re: [PATCH RFC v3 1/3] crypto: add PKE API
In reply to Herbert XuHi Herbert,
Quoted 12 lines · Herbert Xu
Because the caller is going to be allocating memory for the output, we need to provide a way for them to know how much memory to allocate.
This presumably will depend on the key size.
So something like
int (*maxsize)(struct crypto_akcipher *tfm);is needed.
Quoted 7 lines
You should also provide setkey here. You can't just save a pointer to the key. The transform must hold the key physically as the original may go away. It should also ensure that the key is actually valid for the transform.
base already has ctx so you should get rid of ctx and move base to the end of the struct.
right, will do that.
Thanks for quick response.
Re: [PATCH RFC v3 2/3] crypto: RSA: KEYS: convert rsa and public key to new PKE API
In reply to Herbert XuQuoted 11 lines · Herbert Xu
I'd like to see this split into multiple patches. First of all the new
crypto_akcipherimplementation should coexist with the existing code. That way the exiting users can be converted over one-by-one.Also you should implement the
crypto_akciphercompletely before converting anybody over, that means doing encoding/wrapping in addition to the crypto.That way we don't have to have craziness like converting in and out of MPI multiple times.
That's cool. It will make it easier for me. I wanted to convert one to show that the interface is working.
Quoted 4 lines
Lastly you should consider adding an MPI helper that writes to an existing buffer instead of allocating a new one and copying it over.
I'll think about something.
Re: [PATCH RFC v3 2/3] crypto: RSA: KEYS: convert rsa and public key to new PKE API
In reply to Tadeusz StrukA few remarks, perhaps not of the kind you'd like for an RFC, that I hope are still relevant after Herbert's comment.
crypto/asymmetric_keys/Makefile asymmetric_keys-y := asymmetric_type.o signature.o · line 11
Quoted 7 lines · Tadeusz Struk
crypto/asymmetric_keys/Makefile +1
-0@@ -8,6 +8,7 @@ asymmetric_keys-y := asymmetric_type.o signature.o obj-$(CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += public_key.o obj-$(CONFIG_PUBLIC_KEY_ALGO_RSA) += rsa.o +obj-$(CONFIG_PUBLIC_KEY_ALGO_RSA) += rsa_pkcs1_v1_5.o
This builds two modules if PUBLIC_KEY_ALGO_RSA = 'm': rsa.ko and rsa_pkcs1_v1_5.ko. Is that what you want?
Quoted 19 lines
crypto/asymmetric_keys/public_key.c +5
-2+int rsa_pkcs1_v1_5_verify_signature (const struct public_key *pkey, +const struct public_key_signature *sig); + -int public_key_verify_signature (const struct public_key *pk, +int public_key_verify_signature (const struct public_key *pkey,const struct public_key_signature *sig) { -return algo->verify_signature (pk, sig); +return rsa_pkcs1_v1_5_verify_signature (pkey, sig); }
crypto/asymmetric_keys/rsa.c line 37
Quoted 8 lines
crypto/asymmetric_keys/rsa.c +4
-0MODULE_LICENSE ("GPL" );MODULE_DESCRIPTION ("RSA Public Key Algorithm" ); +static int rsa_init (void ) +{ +return crypto_register_akcipher (&rsa); +}
Is there a reason not to mark this __init? (This is not a rhetorical question, perhaps there really is.)
crypto/asymmetric_keys/rsa.c line 315
Quoted 4 lines
crypto/asymmetric_keys/rsa.c +4
-0+static void rsa_exit (void ) +{ +crypto_unregister_akcipher (&rsa); +}
Ditto for __exit.
crypto/asymmetric_keys/rsa.c line 319
Quoted 3 lines
crypto/asymmetric_keys/rsa.c +3
-0+module_init (rsa_init); +module_exit (rsa_exit); +MODULE_ALIAS_CRYPTO ("rsa" );
Could the MODULE_* macros be grouped in one place please?
crypto/asymmetric_keys/rsa_pkcs1_v1_5.c line 182
Quoted 11 lines
crypto/asymmetric_keys/rsa_pkcs1_v1_5.c new file +7
-0+/* +* Perform the verification step [RFC3447 sec 8.2.2]. +*/ +int rsa_pkcs1_v1_5_verify_signature (const struct public_key *pkey, +const struct public_key_signature *sig) +{ +}
public_key.c uses this, so it can end up in public_key.ko. But it's not exported. So a _quick and dirty_ build test generated:
WARNING: "rsa_pkcs1_v1_5_verify_signature" [[...]/crypto/asymmetric_keys/public_key.ko] undefined!
Also no MODULE_LICENSE() macro, so loading rsa_pkcs1_v1_5.ko should trigger a warning and taint the kernel.
Thanks,
Paul Bolle
Re: [PATCH RFC v3 2/3] crypto: RSA: KEYS: convert rsa and public key to new PKE API
Quoted 2 lines · Paul Bolle
This builds two modules if
PUBLIC_KEY_ALGO_RSA= 'm': rsa.ko and rsa_pkcs1_v1_5.ko. Is that what you want?
No, this not what I wanted.
Quoted 7 lines
public_key.cuses this, so it can end up in public_key.ko. But it's not exported. So a_quickanddirty_build test generated:WARNING: "rsa_pkcs1_v1_5_verify_signature" [[...]/crypto/asymmetric_keys/public_key.ko] undefined!Also no
MODULE_LICENSE()macro, so loading rsa_pkcs1_v1_5.ko should trigger a warning and taint the kernel.
Thank you Paul for taking the time to review the patches and for your feedback. I'll have to change this again based on Herbert's feedback so this rsa_pkcs1_v1_5.c file will go away anyway.