Implement RSASSA-PSS signature verification
Subsystems (6)
v3 cover Implement RSASSA-PSS signature verification
Linux currently supports RSA PKCSv1.5 encoding scheme for signing / verification. This adds support for RSASSA PSS signature verification as described in RFC8017 [1].
Patch 1 extends the x509 certificate parser to unpack PSS signature
parameters.
Patches 2-8 pull out the common functions / struct definitions from
rsa-pkcs1pad.c into rsa-common.c, to be shared across RSA encoding scheme implementations.
Patches 9, 10 provide some more plumbing to export the data needed to
perform PSS operations (salt length, RSA modulus).
Patches 11-16 set up PSS scaffolding and provide the verification
operation per RFC8017.
Patches 17, 18 turn the final knobs on to allow lowering PSS signatures
for verification via keyctl.
The patchset is available as a git tree at [2].
Testing:
The implementation was tested by adding reference public keys to the kernel's keyring via keyctl padd and then verifying a known message digest / signature against this public key via keyctl pkey_verify. The reference vectors were taken from:
- the Wycheproof testsuite [3]
- FIPS 186-2 and 186-4 test vectors [4]
The test harness is available at [5].
Example keyctl usage for PSS verification:
rsa_bits=4096 # 2048/3072/4096
hash_algo=sha256 # sha1/sha224/sha256/sha384/sha512
saltlen=32
# Generate keys, certificate:
openssl req -x509 -newkey rsa:$rsa_bits -nodes -keyout private.pem -out cert.der \
-days 100 -outform der -$hash_algo -sigopt rsa_padding_mode:pss \
-sigopt rsa_pss_saltlen:$saltlen -sigopt rsa_mgf1_md:$hash_algo
# Sign data.txt:
openssl dgst -${hash_algo} -sign private.pem -sigopt rsa_padding_mode:pss \
-sigopt rsa_pss_saltlen:${saltlen} -out sig.bin data.txt
# Digest data.txt:
openssl dgst -${hash_algo} -binary -out data.${hash_algo}.raw data.txt
# Load pubkey into the kernel's keyring:
kv=$(keyctl padd asymmetric "test-key" @u < cert.der)
# Verify with enc=pss:
keyctl pkey_verify $kv "0" data.${hash_algo}.raw \
sig.bin "enc=pss hash=${hash_algo} saltlen=${saltlen} mgfhash=${hash_algo}"v3:
- Rename pkey_verify parameter for salt length to "saltlen".
- Update Documentation/security/keys/core.rst.
- Add validation for the hash algorithm passed to psspad_setup_shash.
v2:
- Allow certificates where mgf hash algorithm is different from the digest hash algorithm.
- Fix sparse warnings on "X.509: Parse RSASSA-PSS style certificates".
https://lore.kernel.org/lkml/[email protected]/
https://github.com/varadgautam/kernel/tree/rsassa-psspad-v2
v1 is available at:
https://lore.kernel.org/lkml/[email protected]/
https://github.com/varadgautam/kernel/tree/rsassa-psspad
[1] https://tools.ietf.org/html/rfc8017#section-8.1
[2] https://github.com/varadgautam/kernel/tree/rsassa-psspad-v3
[3] https://github.com/google/wycheproof/blob/master/testvectors/
[4] https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/digital-signatures#rsavs
[5] https://github.com/varadgautam/keyctl-rsa-tests
Varad Gautam (18):
X.509: Parse RSASSA-PSS style certificates
crypto: rsa-pkcs1pad: Rename pkcs1pad-specific functions to rsapad
crypto: rsa-pkcs1pad: Extract pkcs1pad_create into a generic helper
crypto: rsa-pkcs1pad: Pull out child req processing code into helpers
crypto: rsa-pkcs1pad: Rename pkcs1pad_* structs to rsapad_*
crypto: rsa: Start moving RSA common code to rsa-common
crypto: rsa: Move more common code to rsa-common
crypto: rsa: Move rsapad_akcipher_setup_child and callback to
rsa-common
crypto: Extend akcipher API to pass signature parameters
crypto: rsa: Move struct rsa_mpi_key definition to rsa.h
crypto: Scaffolding for RSA-PSS signature style
crypto: rsa-psspad: Introduce shash alloc/dealloc helpers
crypto: rsa-psspad: Get signature parameters from a given signature
crypto: Implement MGF1 Mask Generation Function for RSASSA-PSS
crypto: rsa-psspad: Provide PSS signature verify operation
crypto: rsa-psspad: Implement signature verify callback
crypto: Accept pss as valid encoding during signature verification
keyctl_pkey: Add pkey parameters slen and mgfhash for PSS
Documentation/security/keys/core.rst | 14 +- crypto/Kconfig | 6 + crypto/Makefile | 2 + crypto/asymmetric_keys/Makefile | 5 +- crypto/asymmetric_keys/asymmetric_type.c | 2 + crypto/asymmetric_keys/public_key.c | 18 +- crypto/asymmetric_keys/x509_cert_parser.c | 148 ++++++++ crypto/asymmetric_keys/x509_rsassa.asn1 | 17 + crypto/rsa-common.c | 291 ++++++++++++++++ crypto/rsa-pkcs1pad.c | 400 +++------------------- crypto/rsa-psspad.c | 310 +++++++++++++++++ crypto/rsa.c | 26 +- include/crypto/akcipher.h | 26 ++ include/crypto/internal/rsa-common.h | 61 ++++ include/crypto/internal/rsa.h | 10 + include/crypto/public_key.h | 4 + include/linux/keyctl.h | 2 + include/linux/oid_registry.h | 3 + security/keys/keyctl_pkey.c | 13 + 19 files changed, 993 insertions(+), 365 deletions(-) create mode 100644 crypto/asymmetric_keys/x509_rsassa.asn1 create mode 100644 crypto/rsa-common.c create mode 100644 crypto/rsa-psspad.c create mode 100644 include/crypto/internal/rsa-common.h
Signature · 1 line
2.30.2
PATCH v3 1/18 X.509: Parse RSASSA-PSS style certificates
An X.509 wrapper for a RSASSA-PSS signature contains additional signature parameters over the PKCSv.15 encoding scheme. Extend the x509 parser to allow parsing RSASSA-PSS encoded certificates, with the defaults taken from RFC8017.
References: https://tools.ietf.org/html/rfc8017#appendix-C
--- crypto/asymmetric_keys/Makefile | 5 +- crypto/asymmetric_keys/x509_cert_parser.c | 148 ++++++++++++++++++++++ crypto/asymmetric_keys/x509_rsassa.asn1 | 17 +++ include/crypto/public_key.h | 4 + include/linux/oid_registry.h | 3 + 5 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 crypto/asymmetric_keys/x509_rsassa.asn1
crypto/asymmetric_keys/Makefile +4 -1
@@ -20,15 +20,18 @@ obj-$(CONFIG_X509_CERTIFICATE_PARSER) += x509_key_parser.o x509_key_parser-y := \ x509.asn1.o \ x509_akid.asn1.o \ + x509_rsassa.asn1.o \ x509_cert_parser.o \ x509_public_key.o $(obj)/x509_cert_parser.o: \ $(obj)/x509.asn1.h \ - $(obj)/x509_akid.asn1.h + $(obj)/x509_akid.asn1.h \ + $(obj)/x509_rsassa.asn1.h $(obj)/x509.asn1.o: $(obj)/x509.asn1.c $(obj)/x509.asn1.h $(obj)/x509_akid.asn1.o: $(obj)/x509_akid.asn1.c $(obj)/x509_akid.asn1.h +$(obj)/x509_rsassa.asn1.o: $(obj)/x509_rsassa.asn1.c $(obj)/x509_rsassa.asn1.h # # PKCS#8 private key handling
crypto/asymmetric_keys/x509_cert_parser.c +148 -0
187 lines not on this page · raw message
crypto/asymmetric_keys/x509_rsassa.asn1 new file +17 -0
@@ -0,0 +1,17 @@ +-- RFC8017 +RSASSA-PSS-params ::= SEQUENCE { + hashAlgorithm [0] HashAlgorithm DEFAULT, + maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT, + saltLength [2] INTEGER DEFAULT ({ x509_note_salt_length }), + trailerField [3] INTEGER DEFAULT ({ x509_note_trailer_field }) +} + +HashAlgorithm ::= SEQUENCE { + algorithm OBJECT IDENTIFIER ({ x509_note_hash_algo }), + parameters ANY OPTIONAL ({ x509_note_hash_algo_params }) +} + +MaskGenAlgorithm ::= SEQUENCE { + mgf OBJECT IDENTIFIER ({ x509_note_mgf }), + parameters HashAlgorithm +}
include/crypto/public_key.h +4 -0
@@ -46,6 +46,10 @@ struct public_key_signature {const char *encoding;const void *data;unsigned int data_size; +const char *mgf; +const char *mgf_hash_algo; +u16 salt_length; +u16 trailer_field; };extern void public_key_signature_free (struct public_key_signature *sig);
include/linux/oid_registry.h +3 -0
@@ -113,6 +113,9 @@ enum OID { OID_SM2_with_SM3,/* 1.2.156.10197.1.501 */ OID_sm3WithRSAEncryption,/* 1.2.156.10197.1.504 */ + OID_mgf1,/* 1.2.840.113549.1.1.8 */ + OID_rsassaPSS,/* 1.2.840.113549.1.1.10 */ + OID__NR };
Signature · 1 line
2.30.2
PATCH v3 2/18 crypto: rsa-pkcs1pad: Rename pkcs1pad-specific functions to rsapad
In reply to Varad GautamThe existing RSA implementation supports PKCSv1.5 style signature paddings via rsa-pkcs1pad. A lot of the functionality implemented for rsa-pkcs1pad can be reused across other RSA padding schemes. Rename such functions as rsapad_* before moving them out of rsa-pkcs1pad.c.
--- crypto/rsa-pkcs1pad.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-)
crypto/rsa-pkcs1pad.c +19 -19
@@ -104,7 +104,7 @@ struct pkcs1pad_request {struct akcipher_request child_req; }; -static int pkcs1pad_set_pub_key (struct crypto_akcipher *tfm,const void *key, +static int rsapad_set_pub_key (struct crypto_akcipher *tfm,const void *key,unsigned int keylen) {struct pkcs1pad_ctx *ctx =akcipher_tfm_ctx (tfm);
@@ -125,7 +125,7 @@ static int pkcs1pad_set_pub_key(struct crypto_akcipher *tfm, const void *key,return 0 ; } -static int pkcs1pad_set_priv_key (struct crypto_akcipher *tfm,const void *key, +static int rsapad_set_priv_key (struct crypto_akcipher *tfm,const void *key,unsigned int keylen) {struct pkcs1pad_ctx *ctx =akcipher_tfm_ctx (tfm);
@@ -146,7 +146,7 @@ static int pkcs1pad_set_priv_key(struct crypto_akcipher *tfm, const void *key,return 0 ; } -static unsigned int pkcs1pad_get_max_size (struct crypto_akcipher *tfm) +static unsigned int rsapad_get_max_size (struct crypto_akcipher *tfm) {struct pkcs1pad_ctx *ctx =akcipher_tfm_ctx (tfm);
@@ -159,8 +159,8 @@ static unsigned int pkcs1pad_get_max_size(struct crypto_akcipher *tfm)return ctx->key_size; } -static void pkcs1pad_sg_set_buf (struct scatterlist *sg,void *buf,size_t len, -struct scatterlist *next) +static void rsapad_akcipher_sg_set_buf (struct scatterlist *sg,void *buf, +size_t len,struct scatterlist *next) {int nsegs = next ?2 :1 ;
@@ -256,7 +256,7 @@ static int pkcs1pad_encrypt(struct akcipher_request *req) req_ctx->in_buf[i] =1 +prandom_u32_max (255 ); req_ctx->in_buf[ps_end] =0x00 ; -pkcs1pad_sg_set_buf (req_ctx->in_sg, req_ctx->in_buf, +rsapad_akcipher_sg_set_buf (req_ctx->in_sg, req_ctx->in_buf, ctx->key_size -1 - req->src_len, req->src);akcipher_request_set_tfm (&req_ctx->child_req, ctx->child);
@@ -357,7 +357,7 @@ static int pkcs1pad_decrypt(struct akcipher_request *req)if (!req_ctx->out_buf)return -ENOMEM ; -pkcs1pad_sg_set_buf (req_ctx->out_sg, req_ctx->out_buf, +rsapad_akcipher_sg_set_buf (req_ctx->out_sg, req_ctx->out_buf, ctx->key_size,NULL );akcipher_request_set_tfm (&req_ctx->child_req, ctx->child);
@@ -415,7 +415,7 @@ static int pkcs1pad_sign(struct akcipher_request *req)memcpy (req_ctx->in_buf + ps_end +1 , digest_info->data, digest_info->size); -pkcs1pad_sg_set_buf (req_ctx->in_sg, req_ctx->in_buf, +rsapad_akcipher_sg_set_buf (req_ctx->in_sg, req_ctx->in_buf, ctx->key_size -1 - req->src_len, req->src);akcipher_request_set_tfm (&req_ctx->child_req, ctx->child);
@@ -545,7 +545,7 @@ static int pkcs1pad_verify(struct akcipher_request *req)if (!req_ctx->out_buf)return -ENOMEM ; -pkcs1pad_sg_set_buf (req_ctx->out_sg, req_ctx->out_buf, +rsapad_akcipher_sg_set_buf (req_ctx->out_sg, req_ctx->out_buf, ctx->key_size,NULL );akcipher_request_set_tfm (&req_ctx->child_req, ctx->child);
@@ -564,7 +564,7 @@ static int pkcs1pad_verify(struct akcipher_request *req)return err; } -static int pkcs1pad_init_tfm (struct crypto_akcipher *tfm) +static int rsapad_akcipher_init_tfm (struct crypto_akcipher *tfm) {struct akcipher_instance *inst =akcipher_alg_instance (tfm);struct pkcs1pad_inst_ctx *ictx =akcipher_instance_ctx (inst);
@@ -579,14 +579,14 @@ static int pkcs1pad_init_tfm(struct crypto_akcipher *tfm)return 0 ; } -static void pkcs1pad_exit_tfm (struct crypto_akcipher *tfm) +static void rsapad_akcipher_exit_tfm (struct crypto_akcipher *tfm) {struct pkcs1pad_ctx *ctx =akcipher_tfm_ctx (tfm);crypto_free_akcipher (ctx->child); } -static void pkcs1pad_free (struct akcipher_instance *inst) +static void rsapad_akcipher_free (struct akcipher_instance *inst) {struct pkcs1pad_inst_ctx *ctx =akcipher_instance_ctx (inst);struct crypto_akcipher_spawn *spawn = &ctx->spawn;
@@ -656,24 +656,24 @@ static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb) inst->alg.base.cra_priority = rsa_alg->base.cra_priority; inst->alg.base.cra_ctxsize =sizeof (struct pkcs1pad_ctx ); - inst->alg.init = pkcs1pad_init_tfm; - inst->alg.exit = pkcs1pad_exit_tfm; + inst->alg.init = rsapad_akcipher_init_tfm; + inst->alg.exit = rsapad_akcipher_exit_tfm; inst->alg.encrypt = pkcs1pad_encrypt; inst->alg.decrypt = pkcs1pad_decrypt; inst->alg.sign = pkcs1pad_sign; inst->alg.verify = pkcs1pad_verify; - inst->alg.set_pub_key = pkcs1pad_set_pub_key; - inst->alg.set_priv_key = pkcs1pad_set_priv_key; - inst->alg.max_size = pkcs1pad_get_max_size; + inst->alg.set_pub_key = rsapad_set_pub_key; + inst->alg.set_priv_key = rsapad_set_priv_key; + inst->alg.max_size = rsapad_get_max_size; inst->alg.reqsize =sizeof (struct pkcs1pad_request ) + rsa_alg->reqsize; - inst->free = pkcs1pad_free; + inst->free = rsapad_akcipher_free; err =akcipher_register_instance (tmpl, inst);if (err) { err_free_inst: -pkcs1pad_free (inst); +rsapad_akcipher_free (inst); }return err; }
Signature · 1 line
2.30.2
PATCH v3 3/18 crypto: rsa-pkcs1pad: Extract pkcs1pad_create into a generic helper
In reply to Varad Gautamwhich can be reused by other signature padding schemes as rsapad_akcipher_create. This will be moved out of rsa-pkcs1pad.c to be used across rsa-*pad implementations.
--- crypto/rsa-pkcs1pad.c | 48 ++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 14 deletions(-)
crypto/rsa-pkcs1pad.c +34 -14
@@ -595,7 +595,21 @@ static void rsapad_akcipher_free(struct akcipher_instance *inst)kfree (inst); } -static int pkcs1pad_create (struct crypto_template *tmpl,struct rtattr **tb) +static struct akcipher_alg pkcs1pad_alg = { + .init = rsapad_akcipher_init_tfm, + .exit = rsapad_akcipher_exit_tfm, + + .encrypt = pkcs1pad_encrypt, + .decrypt = pkcs1pad_decrypt, + .sign = pkcs1pad_sign, + .verify = pkcs1pad_verify, + .set_pub_key = rsapad_set_pub_key, + .set_priv_key = rsapad_set_priv_key, + .max_size = rsapad_get_max_size +}; + +static int rsapad_akcipher_create (struct crypto_template *tmpl,struct rtattr **tb, +struct akcipher_alg *alg) {u32 mask;struct akcipher_instance *inst;
@@ -625,12 +639,12 @@ static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb) hash_name =crypto_attr_alg_name (tb[2 ]);if (IS_ERR (hash_name)) {if (snprintf (inst->alg.base.cra_name, -CRYPTO_MAX_ALG_NAME ,"pkcs1pad(%s)" , +CRYPTO_MAX_ALG_NAME ,"%s(%s)" , tmpl->name, rsa_alg->base.cra_name) >=CRYPTO_MAX_ALG_NAME ) goto err_free_inst;if (snprintf (inst->alg.base.cra_driver_name, -CRYPTO_MAX_ALG_NAME ,"pkcs1pad(%s)" , +CRYPTO_MAX_ALG_NAME ,"%s(%s)" , tmpl->name, rsa_alg->base.cra_driver_name) >=CRYPTO_MAX_ALG_NAME ) goto err_free_inst;
@@ -642,12 +656,13 @@ static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb) }if (snprintf (inst->alg.base.cra_name,CRYPTO_MAX_ALG_NAME , -"pkcs1pad(%s,%s)" , rsa_alg->base.cra_name, +"%s(%s,%s)" , tmpl->name, rsa_alg->base.cra_name, hash_name) >=CRYPTO_MAX_ALG_NAME ) goto err_free_inst;if (snprintf (inst->alg.base.cra_driver_name, -CRYPTO_MAX_ALG_NAME ,"pkcs1pad(%s,%s)" , +CRYPTO_MAX_ALG_NAME ,"%s(%s,%s)" , + tmpl->name, rsa_alg->base.cra_driver_name, hash_name) >=CRYPTO_MAX_ALG_NAME ) goto err_free_inst;
@@ -656,16 +671,16 @@ static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb) inst->alg.base.cra_priority = rsa_alg->base.cra_priority; inst->alg.base.cra_ctxsize =sizeof (struct pkcs1pad_ctx ); - inst->alg.init = rsapad_akcipher_init_tfm; - inst->alg.exit = rsapad_akcipher_exit_tfm; + inst->alg.init = alg->init; + inst->alg.exit = alg->exit; - inst->alg.encrypt = pkcs1pad_encrypt; - inst->alg.decrypt = pkcs1pad_decrypt; - inst->alg.sign = pkcs1pad_sign; - inst->alg.verify = pkcs1pad_verify; - inst->alg.set_pub_key = rsapad_set_pub_key; - inst->alg.set_priv_key = rsapad_set_priv_key; - inst->alg.max_size = rsapad_get_max_size; + inst->alg.encrypt = alg->encrypt; + inst->alg.decrypt = alg->decrypt; + inst->alg.sign = alg->sign; + inst->alg.verify = alg->verify; + inst->alg.set_pub_key = alg->set_pub_key; + inst->alg.set_priv_key = alg->set_priv_key; + inst->alg.max_size = alg->max_size; inst->alg.reqsize =sizeof (struct pkcs1pad_request ) + rsa_alg->reqsize; inst->free = rsapad_akcipher_free;
@@ -678,6 +693,11 @@ static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)return err; } +static int pkcs1pad_create (struct crypto_template *tmpl,struct rtattr **tb) +{ +return rsapad_akcipher_create (tmpl, tb, &pkcs1pad_alg); +} +struct crypto_template rsa_pkcs1pad_tmpl = { .name ="pkcs1pad" , .create = pkcs1pad_create,
Signature · 1 line
2.30.2
PATCH v3 4/18 crypto: rsa-pkcs1pad: Pull out child req processing code into helpers
In reply to Varad Gautamrsa-pkcs1pad operations that require using RSA primitives rely on creating an akcipher child RSA transform and processing the results in the operation-specific callback. Add helpers rsapad_akcipher_setup_child and rsapad_akcipher_req_complete for req setup and callback handling, and switch pkcs1pad operations to use these.
--- crypto/rsa-pkcs1pad.c | 106 +++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 57 deletions(-)
crypto/rsa-pkcs1pad.c +49 -57
@@ -171,6 +171,38 @@ static void rsapad_akcipher_sg_set_buf(struct scatterlist *sg, void *buf,sg_chain (sg, nsegs, next); } +typedef int (*rsa_akcipher_complete_cb )(struct akcipher_request *,int ); +static void rsapad_akcipher_req_complete (struct crypto_async_request *child_async_req, +int err,rsa_akcipher_complete_cb cb) +{ +struct akcipher_request *req = child_async_req->data; +struct crypto_async_request async_req; + +if (err == -EINPROGRESS ) +return ; + + async_req.data = req->base.data; + async_req.tfm =crypto_akcipher_tfm (crypto_akcipher_reqtfm (req)); + async_req.flags = child_async_req->flags; + req->base.complete (&async_req,cb (req, err)); +} + +static void rsapad_akcipher_setup_child (struct akcipher_request *req, +struct scatterlist *src_sg, +struct scatterlist *dst_sg, +unsigned int src_len, +unsigned int dst_len, +crypto_completion_t cb) +{ +struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req); +struct pkcs1pad_ctx *ctx =akcipher_tfm_ctx (tfm); +struct pkcs1pad_request *req_ctx =akcipher_request_ctx (req); + +akcipher_request_set_tfm (&req_ctx->child_req, ctx->child); +akcipher_request_set_callback (&req_ctx->child_req, req->base.flags, cb, req); +akcipher_request_set_crypt (&req_ctx->child_req, src_sg, dst_sg, src_len, dst_len); +} +static int pkcs1pad_encrypt_sign_complete (struct akcipher_request *req,int err) {struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req);
@@ -213,17 +245,8 @@ static int pkcs1pad_encrypt_sign_complete(struct akcipher_request *req, int err)static void pkcs1pad_encrypt_sign_complete_cb (struct crypto_async_request *child_async_req,int err) { -struct akcipher_request *req = child_async_req->data; -struct crypto_async_request async_req; - -if (err == -EINPROGRESS ) -return ; - - async_req.data = req->base.data; - async_req.tfm =crypto_akcipher_tfm (crypto_akcipher_reqtfm (req)); - async_req.flags = child_async_req->flags; - req->base.complete (&async_req, -pkcs1pad_encrypt_sign_complete (req, err)); +rsapad_akcipher_req_complete (child_async_req, err, + pkcs1pad_encrypt_sign_complete); }static int pkcs1pad_encrypt (struct akcipher_request *req)
@@ -259,13 +282,10 @@ static int pkcs1pad_encrypt(struct akcipher_request *req)rsapad_akcipher_sg_set_buf (req_ctx->in_sg, req_ctx->in_buf, ctx->key_size -1 - req->src_len, req->src); -akcipher_request_set_tfm (&req_ctx->child_req, ctx->child); -akcipher_request_set_callback (&req_ctx->child_req, req->base.flags, - pkcs1pad_encrypt_sign_complete_cb, req); -/* Reuse output buffer */ -akcipher_request_set_crypt (&req_ctx->child_req, req_ctx->in_sg, - req->dst, ctx->key_size -1 , req->dst_len); +rsapad_akcipher_setup_child (req, req_ctx->in_sg, req->dst, + ctx->key_size -1 , req->dst_len, + pkcs1pad_encrypt_sign_complete_cb); err =crypto_akcipher_encrypt (&req_ctx->child_req);if (err != -EINPROGRESS && err != -EBUSY )
@@ -331,16 +351,7 @@ static int pkcs1pad_decrypt_complete(struct akcipher_request *req, int err)static void pkcs1pad_decrypt_complete_cb (struct crypto_async_request *child_async_req,int err) { -struct akcipher_request *req = child_async_req->data; -struct crypto_async_request async_req; - -if (err == -EINPROGRESS ) -return ; - - async_req.data = req->base.data; - async_req.tfm =crypto_akcipher_tfm (crypto_akcipher_reqtfm (req)); - async_req.flags = child_async_req->flags; - req->base.complete (&async_req,pkcs1pad_decrypt_complete (req, err)); +rsapad_akcipher_req_complete (child_async_req, err, pkcs1pad_decrypt_complete); }static int pkcs1pad_decrypt (struct akcipher_request *req)
@@ -360,14 +371,10 @@ static int pkcs1pad_decrypt(struct akcipher_request *req)rsapad_akcipher_sg_set_buf (req_ctx->out_sg, req_ctx->out_buf, ctx->key_size,NULL ); -akcipher_request_set_tfm (&req_ctx->child_req, ctx->child); -akcipher_request_set_callback (&req_ctx->child_req, req->base.flags, - pkcs1pad_decrypt_complete_cb, req); -/* Reuse input buffer, output to a new buffer */ -akcipher_request_set_crypt (&req_ctx->child_req, req->src, - req_ctx->out_sg, req->src_len, - ctx->key_size); +rsapad_akcipher_setup_child (req, req->src, req_ctx->out_sg, + req->src_len, ctx->key_size, + pkcs1pad_decrypt_complete_cb); err =crypto_akcipher_decrypt (&req_ctx->child_req);if (err != -EINPROGRESS && err != -EBUSY )
@@ -418,13 +425,10 @@ static int pkcs1pad_sign(struct akcipher_request *req)rsapad_akcipher_sg_set_buf (req_ctx->in_sg, req_ctx->in_buf, ctx->key_size -1 - req->src_len, req->src); -akcipher_request_set_tfm (&req_ctx->child_req, ctx->child); -akcipher_request_set_callback (&req_ctx->child_req, req->base.flags, - pkcs1pad_encrypt_sign_complete_cb, req); -/* Reuse output buffer */ -akcipher_request_set_crypt (&req_ctx->child_req, req_ctx->in_sg, - req->dst, ctx->key_size -1 , req->dst_len); +rsapad_akcipher_setup_child (req, req_ctx->in_sg, req->dst, + ctx->key_size -1 , req->dst_len, + pkcs1pad_encrypt_sign_complete_cb); err =crypto_akcipher_decrypt (&req_ctx->child_req);if (err != -EINPROGRESS && err != -EBUSY )
@@ -509,16 +513,8 @@ static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)static void pkcs1pad_verify_complete_cb (struct crypto_async_request *child_async_req,int err) { -struct akcipher_request *req = child_async_req->data; -struct crypto_async_request async_req; - -if (err == -EINPROGRESS ) -return ; - - async_req.data = req->base.data; - async_req.tfm =crypto_akcipher_tfm (crypto_akcipher_reqtfm (req)); - async_req.flags = child_async_req->flags; - req->base.complete (&async_req,pkcs1pad_verify_complete (req, err)); +rsapad_akcipher_req_complete (child_async_req, err, + pkcs1pad_verify_complete); }/*
@@ -548,14 +544,10 @@ static int pkcs1pad_verify(struct akcipher_request *req)rsapad_akcipher_sg_set_buf (req_ctx->out_sg, req_ctx->out_buf, ctx->key_size,NULL ); -akcipher_request_set_tfm (&req_ctx->child_req, ctx->child); -akcipher_request_set_callback (&req_ctx->child_req, req->base.flags, - pkcs1pad_verify_complete_cb, req); -/* Reuse input buffer, output to a new buffer */ -akcipher_request_set_crypt (&req_ctx->child_req, req->src, - req_ctx->out_sg, req->src_len, - ctx->key_size); +rsapad_akcipher_setup_child (req, req->src, req_ctx->out_sg, + req->src_len, ctx->key_size, + pkcs1pad_verify_complete_cb); err =crypto_akcipher_encrypt (&req_ctx->child_req);if (err != -EINPROGRESS && err != -EBUSY )
Signature · 1 line
2.30.2
PATCH v3 5/18 crypto: rsa-pkcs1pad: Rename pkcs1pad_* structs to rsapad_*
In reply to Varad GautamUse generic naming to share with other padding scheme implementations. These will be moved out of rsa-pkcs1pad.c.
--- crypto/rsa-pkcs1pad.c | 62 +++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 31 deletions(-)
crypto/rsa-pkcs1pad.c +31 -31
@@ -88,17 +88,17 @@ static const struct rsa_asn1_template *rsa_lookup_asn1(const char *name)return NULL ; } -struct pkcs1pad_ctx { +struct rsapad_tfm_ctx {struct crypto_akcipher *child;unsigned int key_size; }; -struct pkcs1pad_inst_ctx { +struct rsapad_inst_ctx {struct crypto_akcipher_spawn spawn;const struct rsa_asn1_template *digest_info; }; -struct pkcs1pad_request { +struct rsapad_akciper_req_ctx {struct scatterlist in_sg[2 ], out_sg[1 ];uint8_t *in_buf, *out_buf;struct akcipher_request child_req;
@@ -107,7 +107,7 @@ struct pkcs1pad_request {static int rsapad_set_pub_key (struct crypto_akcipher *tfm,const void *key,unsigned int keylen) { -struct pkcs1pad_ctx *ctx =akcipher_tfm_ctx (tfm); +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm);int err; ctx->key_size =0 ;
@@ -128,7 +128,7 @@ static int rsapad_set_pub_key(struct crypto_akcipher *tfm, const void *key,static int rsapad_set_priv_key (struct crypto_akcipher *tfm,const void *key,unsigned int keylen) { -struct pkcs1pad_ctx *ctx =akcipher_tfm_ctx (tfm); +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm);int err; ctx->key_size =0 ;
@@ -148,7 +148,7 @@ static int rsapad_set_priv_key(struct crypto_akcipher *tfm, const void *key,static unsigned int rsapad_get_max_size (struct crypto_akcipher *tfm) { -struct pkcs1pad_ctx *ctx =akcipher_tfm_ctx (tfm); +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm);/* * The maximum destination buffer size for the encrypt/sign operations
@@ -195,8 +195,8 @@ static void rsapad_akcipher_setup_child(struct akcipher_request *req, crypto_completion_t cb) {struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req); -struct pkcs1pad_ctx *ctx =akcipher_tfm_ctx (tfm); -struct pkcs1pad_request *req_ctx =akcipher_request_ctx (req); +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm); +struct rsapad_akciper_req_ctx *req_ctx =akcipher_request_ctx (req);akcipher_request_set_tfm (&req_ctx->child_req, ctx->child);akcipher_request_set_callback (&req_ctx->child_req, req->base.flags, cb, req);
@@ -206,8 +206,8 @@ static void rsapad_akcipher_setup_child(struct akcipher_request *req,static int pkcs1pad_encrypt_sign_complete (struct akcipher_request *req,int err) {struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req); -struct pkcs1pad_ctx *ctx =akcipher_tfm_ctx (tfm); -struct pkcs1pad_request *req_ctx =akcipher_request_ctx (req); +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm); +struct rsapad_akciper_req_ctx *req_ctx =akcipher_request_ctx (req);unsigned int pad_len;unsigned int len;u8 *out_buf;
@@ -252,8 +252,8 @@ static void pkcs1pad_encrypt_sign_complete_cb(static int pkcs1pad_encrypt (struct akcipher_request *req) {struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req); -struct pkcs1pad_ctx *ctx =akcipher_tfm_ctx (tfm); -struct pkcs1pad_request *req_ctx =akcipher_request_ctx (req); +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm); +struct rsapad_akciper_req_ctx *req_ctx =akcipher_request_ctx (req);int err;unsigned int i, ps_end;
@@ -297,8 +297,8 @@ static int pkcs1pad_encrypt(struct akcipher_request *req)static int pkcs1pad_decrypt_complete (struct akcipher_request *req,int err) {struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req); -struct pkcs1pad_ctx *ctx =akcipher_tfm_ctx (tfm); -struct pkcs1pad_request *req_ctx =akcipher_request_ctx (req); +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm); +struct rsapad_akciper_req_ctx *req_ctx =akcipher_request_ctx (req);unsigned int dst_len;unsigned int pos;u8 *out_buf;
@@ -357,8 +357,8 @@ static void pkcs1pad_decrypt_complete_cb(static int pkcs1pad_decrypt (struct akcipher_request *req) {struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req); -struct pkcs1pad_ctx *ctx =akcipher_tfm_ctx (tfm); -struct pkcs1pad_request *req_ctx =akcipher_request_ctx (req); +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm); +struct rsapad_akciper_req_ctx *req_ctx =akcipher_request_ctx (req);int err;if (!ctx->key_size || req->src_len != ctx->key_size)
@@ -386,10 +386,10 @@ static int pkcs1pad_decrypt(struct akcipher_request *req)static int pkcs1pad_sign (struct akcipher_request *req) {struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req); -struct pkcs1pad_ctx *ctx =akcipher_tfm_ctx (tfm); -struct pkcs1pad_request *req_ctx =akcipher_request_ctx (req); +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm); +struct rsapad_akciper_req_ctx *req_ctx =akcipher_request_ctx (req);struct akcipher_instance *inst =akcipher_alg_instance (tfm); -struct pkcs1pad_inst_ctx *ictx =akcipher_instance_ctx (inst); +struct rsapad_inst_ctx *ictx =akcipher_instance_ctx (inst);const struct rsa_asn1_template *digest_info = ictx->digest_info;int err;unsigned int ps_end, digest_size =0 ;
@@ -440,10 +440,10 @@ static int pkcs1pad_sign(struct akcipher_request *req)static int pkcs1pad_verify_complete (struct akcipher_request *req,int err) {struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req); -struct pkcs1pad_ctx *ctx =akcipher_tfm_ctx (tfm); -struct pkcs1pad_request *req_ctx =akcipher_request_ctx (req); +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm); +struct rsapad_akciper_req_ctx *req_ctx =akcipher_request_ctx (req);struct akcipher_instance *inst =akcipher_alg_instance (tfm); -struct pkcs1pad_inst_ctx *ictx =akcipher_instance_ctx (inst); +struct rsapad_inst_ctx *ictx =akcipher_instance_ctx (inst);const struct rsa_asn1_template *digest_info = ictx->digest_info;unsigned int dst_len;unsigned int pos;
@@ -528,8 +528,8 @@ static void pkcs1pad_verify_complete_cb(static int pkcs1pad_verify (struct akcipher_request *req) {struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req); -struct pkcs1pad_ctx *ctx =akcipher_tfm_ctx (tfm); -struct pkcs1pad_request *req_ctx =akcipher_request_ctx (req); +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm); +struct rsapad_akciper_req_ctx *req_ctx =akcipher_request_ctx (req);int err; if (WARN_ON(req->dst) ||
@@ -559,8 +559,8 @@ static int pkcs1pad_verify(struct akcipher_request *req)static int rsapad_akcipher_init_tfm (struct crypto_akcipher *tfm) {struct akcipher_instance *inst =akcipher_alg_instance (tfm); -struct pkcs1pad_inst_ctx *ictx =akcipher_instance_ctx (inst); -struct pkcs1pad_ctx *ctx =akcipher_tfm_ctx (tfm); +struct rsapad_inst_ctx *ictx =akcipher_instance_ctx (inst); +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm);struct crypto_akcipher *child_tfm; child_tfm =crypto_spawn_akcipher (&ictx->spawn);
@@ -573,14 +573,14 @@ static int rsapad_akcipher_init_tfm(struct crypto_akcipher *tfm)static void rsapad_akcipher_exit_tfm (struct crypto_akcipher *tfm) { -struct pkcs1pad_ctx *ctx =akcipher_tfm_ctx (tfm); +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm);crypto_free_akcipher (ctx->child); }static void rsapad_akcipher_free (struct akcipher_instance *inst) { -struct pkcs1pad_inst_ctx *ctx =akcipher_instance_ctx (inst); +struct rsapad_inst_ctx *ctx =akcipher_instance_ctx (inst);struct crypto_akcipher_spawn *spawn = &ctx->spawn;crypto_drop_akcipher (spawn);
@@ -605,7 +605,7 @@ static int rsapad_akcipher_create(struct crypto_template *tmpl, struct rtattr ** {u32 mask;struct akcipher_instance *inst; -struct pkcs1pad_inst_ctx *ctx; +struct rsapad_inst_ctx *ctx;struct akcipher_alg *rsa_alg;const char *hash_name;int err;
@@ -661,7 +661,7 @@ static int rsapad_akcipher_create(struct crypto_template *tmpl, struct rtattr ** } inst->alg.base.cra_priority = rsa_alg->base.cra_priority; - inst->alg.base.cra_ctxsize =sizeof (struct pkcs1pad_ctx ); + inst->alg.base.cra_ctxsize =sizeof (struct rsapad_tfm_ctx ); inst->alg.init = alg->init; inst->alg.exit = alg->exit;
@@ -673,7 +673,7 @@ static int rsapad_akcipher_create(struct crypto_template *tmpl, struct rtattr ** inst->alg.set_pub_key = alg->set_pub_key; inst->alg.set_priv_key = alg->set_priv_key; inst->alg.max_size = alg->max_size; - inst->alg.reqsize =sizeof (struct pkcs1pad_request ) + rsa_alg->reqsize; + inst->alg.reqsize =sizeof (struct rsapad_akciper_req_ctx ) + rsa_alg->reqsize; inst->free = rsapad_akcipher_free;
Signature · 1 line
2.30.2
PATCH v3 6/18 crypto: rsa: Start moving RSA common code to rsa-common
In reply to Varad GautamMove out helpers from rsa-pkcs1pad.c which will be shared across rsa-*pad implementations.
--- crypto/Makefile | 1 + crypto/rsa-common.c | 76 ++++++++++++++++++++++ crypto/rsa-pkcs1pad.c | 97 +--------------------------- include/crypto/internal/rsa-common.h | 37 +++++++++++ 4 files changed, 115 insertions(+), 96 deletions(-) create mode 100644 crypto/rsa-common.c create mode 100644 include/crypto/internal/rsa-common.h
crypto/Makefile +1 -0
@@ -39,6 +39,7 @@ rsa_generic-y := rsapubkey.asn1.o rsa_generic-y += rsaprivkey.asn1.o rsa_generic-y += rsa.o rsa_generic-y += rsa_helper.o +rsa_generic-y += rsa-common.o rsa_generic-y += rsa-pkcs1pad.o obj-$(CONFIG_CRYPTO_RSA) += rsa_generic.o
crypto/rsa-common.c new file +76 -0
76 lines not on this page · raw message
crypto/rsa-pkcs1pad.c +1 -96
105 lines not on this page · raw message
include/crypto/internal/rsa-common.h new file +37 -0
@@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Copyright (c) 2015 Intel Corporation +* Copyright (C) 2021 SUSE +* +*/ +#ifndef _RSA_COMMON_ +#define _RSA_COMMON_ + +#include <crypto/algapi.h> +#include <crypto/internal/akcipher.h> +#include <linux/scatterlist.h> + +struct rsa_asn1_template { +const char *name; +const u8 *data; +size_t size; +}; +const struct rsa_asn1_template *rsa_lookup_asn1 (const char *name); + +struct rsapad_tfm_ctx { +struct crypto_akcipher *child; +unsigned int key_size; +}; + +struct rsapad_inst_ctx { +struct crypto_akcipher_spawn spawn; +const struct rsa_asn1_template *digest_info; +}; + +struct rsapad_akciper_req_ctx { +struct scatterlist in_sg[2 ], out_sg[1 ]; +uint8_t *in_buf, *out_buf; +struct akcipher_request child_req; +}; + +#endif
Signature · 1 line
2.30.2
PATCH v3 7/18 crypto: rsa: Move more common code to rsa-common
In reply to Varad GautamMove helpers for setting public/private keys, RSA akcipher instance setup, keysize querying etc. to rsa-common.c.
--- crypto/rsa-common.c | 183 +++++++++++++++++++++++++++ crypto/rsa-pkcs1pad.c | 183 --------------------------- include/crypto/internal/rsa-common.h | 13 ++ 3 files changed, 196 insertions(+), 183 deletions(-)
crypto/rsa-common.c +183 -0
@@ -74,3 +74,186 @@ const struct rsa_asn1_template *rsa_lookup_asn1(const char *name)return p;return NULL ; } + +int rsapad_set_pub_key (struct crypto_akcipher *tfm,const void *key, +unsigned int keylen) +{ +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm); +int err; + + ctx->key_size =0 ; + + err =crypto_akcipher_set_pub_key (ctx->child, key, keylen); +if (err) +return err; + +/* Find out new modulus size from rsa implementation */ + err =crypto_akcipher_maxsize (ctx->child); +if (err >PAGE_SIZE ) +return -EOPNOTSUPP ; + + ctx->key_size = err; +return 0 ; +} + +int rsapad_set_priv_key (struct crypto_akcipher *tfm,const void *key, +unsigned int keylen) +{ +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm); +int err; + + ctx->key_size =0 ; + + err =crypto_akcipher_set_priv_key (ctx->child, key, keylen); +if (err) +return err; + +/* Find out new modulus size from rsa implementation */ + err =crypto_akcipher_maxsize (ctx->child); +if (err >PAGE_SIZE ) +return -EOPNOTSUPP ; + + ctx->key_size = err; +return 0 ; +} + +unsigned int rsapad_get_max_size (struct crypto_akcipher *tfm) +{ +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm); + +/* +* The maximum destination buffer size for the encrypt/sign operations +* will be the same as for RSA, even though it's smaller for +* decrypt/verify. +*/ + +return ctx->key_size; +} + +void rsapad_akcipher_sg_set_buf (struct scatterlist *sg,void *buf, +size_t len,struct scatterlist *next) +{ +int nsegs = next ?2 :1 ; + +sg_init_table (sg, nsegs); +sg_set_buf (sg, buf, len); + +if (next) +sg_chain (sg, nsegs, next); +} + +int rsapad_akcipher_init_tfm (struct crypto_akcipher *tfm) +{ +struct akcipher_instance *inst =akcipher_alg_instance (tfm); +struct rsapad_inst_ctx *ictx =akcipher_instance_ctx (inst); +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm); +struct crypto_akcipher *child_tfm; + + child_tfm =crypto_spawn_akcipher (&ictx->spawn); +if (IS_ERR (child_tfm)) +return PTR_ERR (child_tfm); + + ctx->child = child_tfm; +return 0 ; +} + +void rsapad_akcipher_exit_tfm (struct crypto_akcipher *tfm) +{ +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm); + +crypto_free_akcipher (ctx->child); +} + +void rsapad_akcipher_free (struct akcipher_instance *inst) +{ +struct rsapad_inst_ctx *ctx =akcipher_instance_ctx (inst); +struct crypto_akcipher_spawn *spawn = &ctx->spawn; + +crypto_drop_akcipher (spawn); +kfree (inst); +} + +int rsapad_akcipher_create (struct crypto_template *tmpl,struct rtattr **tb, +struct akcipher_alg *alg) +{ +u32 mask; +struct akcipher_instance *inst; +struct rsapad_inst_ctx *ctx; +struct akcipher_alg *rsa_alg; +const char *hash_name; +int err; + + err =crypto_check_attr_type (tb,CRYPTO_ALG_TYPE_AKCIPHER , &mask); +if (err) +return err; + + inst =kzalloc (sizeof (*inst) +sizeof (*ctx),GFP_KERNEL ); +if (!inst) +return -ENOMEM ; + + ctx =akcipher_instance_ctx (inst); + + err =crypto_grab_akcipher (&ctx->spawn,akcipher_crypto_instance (inst), +crypto_attr_alg_name (tb[1 ]),0 , mask); +if (err) + goto err_free_inst; + + rsa_alg =crypto_spawn_akcipher_alg (&ctx->spawn); + + err = -ENAMETOOLONG ; + hash_name =crypto_attr_alg_name (tb[2 ]); +if (IS_ERR (hash_name)) { +if (snprintf (inst->alg.base.cra_name, +CRYPTO_MAX_ALG_NAME ,"%s(%s)" , tmpl->name, + rsa_alg->base.cra_name) >=CRYPTO_MAX_ALG_NAME ) + goto err_free_inst; + +if (snprintf (inst->alg.base.cra_driver_name, +CRYPTO_MAX_ALG_NAME ,"%s(%s)" , tmpl->name, + rsa_alg->base.cra_driver_name) >= +CRYPTO_MAX_ALG_NAME ) + goto err_free_inst; + }else { + ctx->digest_info =rsa_lookup_asn1 (hash_name); +if (!ctx->digest_info) { + err = -EINVAL ; + goto err_free_inst; + } + +if (snprintf (inst->alg.base.cra_name,CRYPTO_MAX_ALG_NAME , +"%s(%s,%s)" , tmpl->name, rsa_alg->base.cra_name, + hash_name) >=CRYPTO_MAX_ALG_NAME ) + goto err_free_inst; + +if (snprintf (inst->alg.base.cra_driver_name, +CRYPTO_MAX_ALG_NAME ,"%s(%s,%s)" , + tmpl->name, + rsa_alg->base.cra_driver_name, + hash_name) >=CRYPTO_MAX_ALG_NAME ) + goto err_free_inst; + } + + inst->alg.base.cra_priority = rsa_alg->base.cra_priority; + inst->alg.base.cra_ctxsize =sizeof (struct rsapad_tfm_ctx ); + + inst->alg.init = alg->init; + inst->alg.exit = alg->exit; + + inst->alg.encrypt = alg->encrypt; + inst->alg.decrypt = alg->decrypt; + inst->alg.sign = alg->sign; + inst->alg.verify = alg->verify; + inst->alg.set_pub_key = alg->set_pub_key; + inst->alg.set_priv_key = alg->set_priv_key; + inst->alg.max_size = alg->max_size; + inst->alg.reqsize =sizeof (struct rsapad_akciper_req_ctx ) + rsa_alg->reqsize; + + inst->free = rsapad_akcipher_free; + + err =akcipher_register_instance (tmpl, inst); +if (err) { +err_free_inst: +rsapad_akcipher_free (inst); + } +return err; +}
crypto/rsa-pkcs1pad.c +0 -183
201 lines not on this page · raw message
include/crypto/internal/rsa-common.h +13 -0
@@ -34,4 +34,17 @@ struct rsapad_akciper_req_ctx { struct akcipher_request child_req; }; +int rsapad_set_pub_key (struct crypto_akcipher *tfm,const void *key, +unsigned int keylen); +int rsapad_set_priv_key (struct crypto_akcipher *tfm,const void *key, +unsigned int keylen); +unsigned int rsapad_get_max_size (struct crypto_akcipher *tfm); +void rsapad_akcipher_sg_set_buf (struct scatterlist *sg,void *buf, +size_t len,struct scatterlist *next); +int rsapad_akcipher_init_tfm (struct crypto_akcipher *tfm); +void rsapad_akcipher_exit_tfm (struct crypto_akcipher *tfm); +void rsapad_akcipher_free (struct akcipher_instance *inst); +int rsapad_akcipher_create (struct crypto_template *tmpl,struct rtattr **tb, +struct akcipher_alg *alg); +#endif
Signature · 1 line
2.30.2
PATCH v3 8/18 crypto: rsa: Move rsapad_akcipher_setup_child and callback to rsa-common
In reply to Varad GautamPull out more common code from rsa-pkcs1pad into rsa-common.
--- crypto/rsa-common.c | 31 +++++++++++++++++++++++++++ crypto/rsa-pkcs1pad.c | 32 ---------------------------- include/crypto/internal/rsa-common.h | 9 ++++++++ 3 files changed, 40 insertions(+), 32 deletions(-)
crypto/rsa-common.c +31 -0
@@ -75,6 +75,37 @@ const struct rsa_asn1_template *rsa_lookup_asn1(const char *name)return NULL ; } +void rsapad_akcipher_req_complete (struct crypto_async_request *child_async_req, +int err,rsa_akcipher_complete_cb cb) +{ +struct akcipher_request *req = child_async_req->data; +struct crypto_async_request async_req; + +if (err == -EINPROGRESS ) +return ; + + async_req.data = req->base.data; + async_req.tfm =crypto_akcipher_tfm (crypto_akcipher_reqtfm (req)); + async_req.flags = child_async_req->flags; + req->base.complete (&async_req,cb (req, err)); +} + +void rsapad_akcipher_setup_child (struct akcipher_request *req, +struct scatterlist *src_sg, +struct scatterlist *dst_sg, +unsigned int src_len, +unsigned int dst_len, +crypto_completion_t cb) +{ +struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req); +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm); +struct rsapad_akciper_req_ctx *req_ctx =akcipher_request_ctx (req); + +akcipher_request_set_tfm (&req_ctx->child_req, ctx->child); +akcipher_request_set_callback (&req_ctx->child_req, req->base.flags, cb, req); +akcipher_request_set_crypt (&req_ctx->child_req, src_sg, dst_sg, src_len, dst_len); +} +int rsapad_set_pub_key (struct crypto_akcipher *tfm,const void *key,unsigned int keylen) {
crypto/rsa-pkcs1pad.c +0 -32
@@ -9,38 +9,6 @@#include <linux/module.h> #include <linux/random.h> -typedef int (*rsa_akcipher_complete_cb )(struct akcipher_request *,int ); -static void rsapad_akcipher_req_complete (struct crypto_async_request *child_async_req, -int err,rsa_akcipher_complete_cb cb) -{ -struct akcipher_request *req = child_async_req->data; -struct crypto_async_request async_req; - -if (err == -EINPROGRESS ) -return ; - - async_req.data = req->base.data; - async_req.tfm =crypto_akcipher_tfm (crypto_akcipher_reqtfm (req)); - async_req.flags = child_async_req->flags; - req->base.complete (&async_req,cb (req, err)); -} - -static void rsapad_akcipher_setup_child (struct akcipher_request *req, -struct scatterlist *src_sg, -struct scatterlist *dst_sg, -unsigned int src_len, -unsigned int dst_len, -crypto_completion_t cb) -{ -struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req); -struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm); -struct rsapad_akciper_req_ctx *req_ctx =akcipher_request_ctx (req); - -akcipher_request_set_tfm (&req_ctx->child_req, ctx->child); -akcipher_request_set_callback (&req_ctx->child_req, req->base.flags, cb, req); -akcipher_request_set_crypt (&req_ctx->child_req, src_sg, dst_sg, src_len, dst_len); -} -static int pkcs1pad_encrypt_sign_complete (struct akcipher_request *req,int err) {struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req);
include/crypto/internal/rsa-common.h +9 -0
@@ -34,6 +34,15 @@ struct rsapad_akciper_req_ctx {struct akcipher_request child_req; }; +typedef int (*rsa_akcipher_complete_cb )(struct akcipher_request *,int ); +void rsapad_akcipher_req_complete (struct crypto_async_request *child_async_req, +int err,rsa_akcipher_complete_cb cb); +void rsapad_akcipher_setup_child (struct akcipher_request *req, +struct scatterlist *src_sg, +struct scatterlist *dst_sg, +unsigned int src_len, +unsigned int dst_len, +crypto_completion_t cb);int rsapad_set_pub_key (struct crypto_akcipher *tfm,const void *key,unsigned int keylen); int rsapad_set_priv_key(struct crypto_akcipher *tfm, const void *key,
Signature · 1 line
2.30.2
PATCH v3 9/18 crypto: Extend akcipher API to pass signature parameters
In reply to Varad GautamFor certain signature encoding schemes (eg. RSASSA-PSS), the verify/sign operation behavior depends on information contained in the signature blob. Allow passing this down to the crypto_template by introducing a crypto_akcipher_set_sig_params() call.
--- crypto/rsa-common.c | 1 + include/crypto/akcipher.h | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+)
crypto/rsa-common.c +1 -0
@@ -277,6 +277,7 @@ int rsapad_akcipher_create(struct crypto_template *tmpl, struct rtattr **tb, inst->alg.set_pub_key = alg->set_pub_key; inst->alg.set_priv_key = alg->set_priv_key; inst->alg.max_size = alg->max_size; + inst->alg.set_sig_params = alg->set_sig_params; inst->alg.reqsize =sizeof (struct rsapad_akciper_req_ctx ) + rsa_alg->reqsize; inst->free = rsapad_akcipher_free;
include/crypto/akcipher.h +26 -0
@@ -101,6 +101,8 @@ struct akcipher_alg {unsigned int (*max_size)(struct crypto_akcipher *tfm);int (*init)(struct crypto_akcipher *tfm);void (*exit)(struct crypto_akcipher *tfm); +int (*set_sig_params)(struct crypto_akcipher *tfm,const void *sig, +unsigned int sig_len);unsigned int reqsize;struct crypto_alg base;
@@ -413,4 +415,28 @@ static inline int crypto_akcipher_set_priv_key(struct crypto_akcipher *tfm, return alg->set_priv_key(tfm, key, keylen); } + +/** +* crypto_akcipher_set_sig_params() - Invoke set sig params operation +* +* Use this if the verification/signing operation behavior depends on +* parameters contained in the signature. +* +* @tfm: tfm handle +* @sig: ptr to a struct public_key_signature to extract info from +* @siglen: Length of sig - should be unnecessary if you pass the struct. +* +* Return: zero on success; error code in case of error +*/ +static inline int crypto_akcipher_set_sig_params (struct crypto_akcipher *tfm, +const void *sig, +unsigned int siglen) +{ +struct akcipher_alg *alg =crypto_akcipher_alg (tfm); + +if (alg->set_sig_params) +return alg->set_sig_params (tfm, sig, siglen); +else +return -EOPNOTSUPP ; +}#endif
Signature · 1 line
2.30.2
PATCH v3 10/18 crypto: rsa: Move struct rsa_mpi_key definition to rsa.h
In reply to Varad GautamThe RSASSA-PSS signature scheme requires knowing the RSA modulus size in bits. The rsa akcipher_alg max_size call is insufficient for this, as the returned keysize is rounded up to the next byte.
Since the RSA modulus is stored as an MPI accessible via struct rsa_mpi_key, move the struct definition to rsa.h to help RSA sub-implementations query the MPI values.
--- crypto/rsa.c | 6 ------ include/crypto/internal/rsa.h | 7 +++++++ 2 files changed, 7 insertions(+), 6 deletions(-)
crypto/rsa.c +0 -6
@@ -12,12 +12,6 @@#include <crypto/akcipher.h> #include <crypto/algapi.h> -struct rsa_mpi_key { -MPI n; -MPI e; -MPI d; -}; -/* * RSAEP function [RFC3447 sec 5.1.1] * c = m^e mod n;
include/crypto/internal/rsa.h +7 -0
@@ -7,6 +7,7 @@*/ #ifndef _RSA_HELPER_#define _RSA_HELPER_ +#include <linux/mpi.h> #include <linux/types.h> /**
@@ -53,5 +54,11 @@ int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key,int rsa_parse_priv_key (struct rsa_key *rsa_key,const void *key,unsigned int key_len); +struct rsa_mpi_key { +MPI n; +MPI e; +MPI d; +}; +extern struct crypto_template rsa_pkcs1pad_tmpl;#endif
Signature · 1 line
2.30.2
PATCH v3 11/18 crypto: Scaffolding for RSA-PSS signature style
In reply to Varad GautamAdd a crypto_template for rsa-psspad, hidden behind CONFIG_CRYPTO_RSASSA_PSS. Set the sign/verify/encrypt/decrypt operations to return -EOPNOTSUPP, to be implemented in the future
--- crypto/Kconfig | 6 ++++++ crypto/Makefile | 1 + crypto/rsa-psspad.c | 39 +++++++++++++++++++++++++++++++++++ crypto/rsa.c | 20 +++++++++++++++--- include/crypto/internal/rsa.h | 3 +++ 5 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 crypto/rsa-psspad.c
crypto/Kconfig +6 -0
@@ -224,6 +224,12 @@ config CRYPTO_RSA help Generic implementation of the RSA public key algorithm. +config CRYPTO_RSASSA_PSS + bool "RSASSA-PSS algorithm" + select CRYPTO_RSA + help + RSASSA-PSS signature verification scheme. + config CRYPTO_DH tristate "Diffie-Hellman algorithm" select CRYPTO_KPP
crypto/Makefile +1 -0
@@ -42,6 +42,7 @@ rsa_generic-y += rsa_helper.o rsa_generic-y += rsa-common.o rsa_generic-y += rsa-pkcs1pad.o obj-$(CONFIG_CRYPTO_RSA) += rsa_generic.o +obj-$(CONFIG_CRYPTO_RSASSA_PSS) += rsa-psspad.o $(obj)/sm2signature.asn1.o: $(obj)/sm2signature.asn1.c $(obj)/sm2signature.asn1.h $(obj)/sm2.o: $(obj)/sm2signature.asn1.h
crypto/rsa-psspad.c new file +39 -0
@@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* RSASSA-PSS signature scheme. +* +* Copyright (C) 2021, SUSE +* Authors: Varad Gautam <[email protected]> +*/ + +#include <crypto/internal/akcipher.h> +#include <crypto/internal/rsa-common.h> + +static int psspad_s_v_e_d (struct akcipher_request *req) +{ +return -EOPNOTSUPP ; +} + +static struct akcipher_alg psspad_alg = { + .init = rsapad_akcipher_init_tfm, + .exit = rsapad_akcipher_exit_tfm, + + .encrypt = psspad_s_v_e_d, + .decrypt = psspad_s_v_e_d, + .sign = psspad_s_v_e_d, + .verify = psspad_s_v_e_d, + .set_pub_key = rsapad_set_pub_key, + .set_priv_key = rsapad_set_priv_key, + .max_size = rsapad_get_max_size +}; + +static int psspad_create (struct crypto_template *tmpl,struct rtattr **tb) +{ +return rsapad_akcipher_create (tmpl, tb, &psspad_alg); +} + +struct crypto_template rsa_psspad_tmpl = { + .name ="psspad" , + .create = psspad_create, + .module =THIS_MODULE , +};
crypto/rsa.c +17 -3
@@ -258,17 +258,31 @@ static int rsa_init(void)return err; err =crypto_register_template (&rsa_pkcs1pad_tmpl); +if (err) + goto out_err; + +#ifdef CONFIG_CRYPTO_RSASSA_PSS + err =crypto_register_template (&rsa_psspad_tmpl);if (err) { -crypto_unregister_akcipher (&rsa); -return err; +crypto_unregister_template (&rsa_pkcs1pad_tmpl); + goto out_err; } +#endif -return 0 ; + goto out; + +out_err: +crypto_unregister_akcipher (&rsa); +out: +return err; }static void rsa_exit (void ) {crypto_unregister_template (&rsa_pkcs1pad_tmpl); +#ifdef CONFIG_CRYPTO_RSASSA_PSS +crypto_unregister_template (&rsa_psspad_tmpl); +#endif crypto_unregister_akcipher (&rsa); }
include/crypto/internal/rsa.h +3 -0
@@ -61,4 +61,7 @@ struct rsa_mpi_key { };extern struct crypto_template rsa_pkcs1pad_tmpl; +#ifdef CONFIG_CRYPTO_RSASSA_PSS +extern struct crypto_template rsa_psspad_tmpl; +#endif /* CONFIG_CRYPTO_RSASSA_PSS */ #endif
Signature · 1 line
2.30.2
PATCH v3 12/18 crypto: rsa-psspad: Introduce shash alloc/dealloc helpers
In reply to Varad GautamRSASSA-PSS verify operation needs to compute digests for its Mask Generation Function (MGF1), and for digest comparison.
Add helpers to populate a crypto_shash and desc for use in both cases.
---
v3: Add psspad_check_hash_algo to only allow valid hash algorithms in
psspad_setup_shash.
crypto/rsa-psspad.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)
crypto/rsa-psspad.c +43 -0
@@ -6,9 +6,52 @@* Authors: Varad Gautam <[email protected]> */ +#include <crypto/hash.h> #include <crypto/internal/akcipher.h> #include <crypto/internal/rsa-common.h> +static bool psspad_check_hash_algo (const char *hash_algo) +{ +const char *hash_algos[] = {"sha1" ,"sha224" ,"sha256" ,"sha384" ,"sha512" }; +bool found = false; +int i =0 ; + +for (i =0 ; i <ARRAY_SIZE (hash_algos); i++) { +if (strcmp (hash_algo, hash_algos[i]) ==0 ) { + found = true; +break ; + } + } + +return found; +} + +static int psspad_setup_shash (struct crypto_shash **hash_tfm,struct shash_desc **desc, +const char *hash_algo) +{ +if (!psspad_check_hash_algo (hash_algo)) +return -EINVAL ; + + *hash_tfm =crypto_alloc_shash (hash_algo,0 ,0 ); +if (IS_ERR (*hash_tfm)) +return PTR_ERR (*hash_tfm); + + *desc =kzalloc (crypto_shash_descsize (*hash_tfm) +sizeof (**desc), +GFP_KERNEL ); +if (!desc) +return -ENOMEM ; + + (*desc)->tfm = *hash_tfm; + +return 0 ; +} + +static void psspad_free_shash (struct crypto_shash *hash_tfm,struct shash_desc *desc) +{ +kfree (desc); +crypto_free_shash (hash_tfm); +} +static int psspad_s_v_e_d (struct akcipher_request *req) {return -EOPNOTSUPP ;
Signature · 1 line
2.30.2
PATCH v3 13/18 crypto: rsa-psspad: Get signature parameters from a given signature
In reply to Varad GautamImplement akcipher_alg->set_sig_params for rsassa-psspad to receive the salt length and MGF hash function for the signature being verified.
--- crypto/rsa-psspad.c | 21 ++++++++++++++++++++- include/crypto/internal/rsa-common.h | 2 ++ 2 files changed, 22 insertions(+), 1 deletion(-)
crypto/rsa-psspad.c +20 -1
@@ -9,6 +9,7 @@#include <crypto/hash.h> #include <crypto/internal/akcipher.h> #include <crypto/internal/rsa-common.h> +#include <crypto/public_key.h> static bool psspad_check_hash_algo (const char *hash_algo) {
@@ -52,6 +53,23 @@ static void psspad_free_shash(struct crypto_shash *hash_tfm, struct shash_desc *crypto_free_shash (hash_tfm); } +static int psspad_set_sig_params (struct crypto_akcipher *tfm, +const void *sig, +unsigned int siglen) +{ +struct akcipher_instance *inst =akcipher_alg_instance (tfm); +struct rsapad_inst_ctx *ictx =akcipher_instance_ctx (inst); +const struct public_key_signature *s = sig; + +if (!sig) +return -EINVAL ; + + ictx->salt_len = s->salt_length; + ictx->mgf_hash_algo = s->mgf_hash_algo; + +return 0 ; +} +static int psspad_s_v_e_d (struct akcipher_request *req) {return -EOPNOTSUPP ;
@@ -67,7 +85,8 @@ static struct akcipher_alg psspad_alg = { .verify = psspad_s_v_e_d, .set_pub_key = rsapad_set_pub_key, .set_priv_key = rsapad_set_priv_key, - .max_size = rsapad_get_max_size + .max_size = rsapad_get_max_size, + .set_sig_params = psspad_set_sig_params };static int psspad_create (struct crypto_template *tmpl,struct rtattr **tb)
include/crypto/internal/rsa-common.h +2 -0
@@ -26,6 +26,8 @@ struct rsapad_tfm_ctx {struct rsapad_inst_ctx {struct crypto_akcipher_spawn spawn;const struct rsa_asn1_template *digest_info; +u16 salt_len; +const char *mgf_hash_algo; };struct rsapad_akciper_req_ctx {
Signature · 1 line
2.30.2
Herbert Xu
maintainerRe: [PATCH v3 13/18] crypto: rsa-psspad: Get signature parameters from a given signature
Quoted 4 lines · Varad Gautam
crypto/rsa-psspad.c +3
-0+static int psspad_set_sig_params (struct crypto_akcipher *tfm, +const void *sig, +unsigned int siglen)
crypto/rsa-psspad.c static void psspad_free_shash(struct crypto_shash *hash_tfm, struct shash_desc * · line 68
Quoted 10 lines
crypto/rsa-psspad.c +10
-0+{ +struct akcipher_instance *inst =akcipher_alg_instance (tfm); +struct rsapad_inst_ctx *ictx =akcipher_instance_ctx (inst); +const struct public_key_signature *s = sig; + +if (!sig) +return -EINVAL ; + + ictx->salt_len = s->salt_length; + ictx->mgf_hash_algo = s->mgf_hash_algo;
Is there any reason why this couldn't be embedded into the key 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
Re: [PATCH v3 13/18] crypto: rsa-psspad: Get signature parameters from a given signature
Hi Herbert,
Quoted 18 lines · Herbert Xu
Quoted 4 lines · Varad Gautam
crypto/rsa-psspad.c +3
-0+static int psspad_set_sig_params (struct crypto_akcipher *tfm, +const void *sig, +unsigned int siglen)crypto/rsa-psspad.c static void psspad_free_shash(struct crypto_shash *hash_tfm, struct shash_desc * · line 68
Quoted 10 lines
crypto/rsa-psspad.c +10
-0+{ +struct akcipher_instance *inst =akcipher_alg_instance (tfm); +struct rsapad_inst_ctx *ictx =akcipher_instance_ctx (inst); +const struct public_key_signature *s = sig; + +if (!sig) +return -EINVAL ; + + ictx->salt_len = s->salt_length; + ictx->mgf_hash_algo = s->mgf_hash_algo;Is there any reason why this couldn't be embedded into the key instead?
Sorry about the delay, do you mean setting these as part of rsapad_set_pub_key()?
The same pubkey can be used to verify both PSS and PKCSv1.5 style signatures, so I don't see the signature params (salt length / mgf hash) being a part of the pkey state.
Thanks,
Varad
Quoted 3 lines
Thanks,
Signature · 7 lines
SUSE Software Solutions Germany GmbH Maxfeldstr. 5 90409 Nürnberg Germany HRB 36809, AG Nürnberg Geschäftsführer: Felix Imendörffer
Re: [PATCH v3 13/18] crypto: rsa-psspad: Get signature parameters from a given signature
In reply to Herbert XuQuoted 19 lines · Herbert Xu
Quoted 4 lines · Varad Gautam
crypto/rsa-psspad.c +3
-0+static int psspad_set_sig_params (struct crypto_akcipher *tfm, +const void *sig, +unsigned int siglen)crypto/rsa-psspad.c static void psspad_free_shash(struct crypto_shash *hash_tfm, struct shash_desc * · line 68
Quoted 10 lines
crypto/rsa-psspad.c +10
-0+{ +struct akcipher_instance *inst =akcipher_alg_instance (tfm); +struct rsapad_inst_ctx *ictx =akcipher_instance_ctx (inst); +const struct public_key_signature *s = sig; + +if (!sig) +return -EINVAL ; + + ictx->salt_len = s->salt_length; + ictx->mgf_hash_algo = s->mgf_hash_algo;Is there any reason why this couldn't be embedded into the key instead?
Whilst is is correct that the same key can be used to verify either types of signatures, it is best practice to use separate and new keys in such situations. This prevents compromising key due to any weaknesses in the one or the other signature types.
Thus imho it does make sense to embed sal_len & hash_algo into the key instead, and thus only allow PSS signature verification with such a key. This is common for x509 certs too as used in TLS. (at least this is my understanding of all of this).
But this is a minor point which can fix now or later.
BTW, this patch series overall look very good to me and I want to use PSS signatures in my kernel builds. What is the status of merging this patch series?
Regards,
Dimitri.
PATCH v3 14/18 crypto: Implement MGF1 Mask Generation Function for RSASSA-PSS
In reply to Varad GautamThis generates a "mask" byte array of size mask_len bytes as a concatenation of digests, where each digest is calculated on a concatenation of an input seed and a running counter to fill up mask_len bytes - as described by RFC8017 sec B.2.1. "MGF1".
The mask is useful for RSA signing/verification process with encoding RSASSA-PSS.
Reference: https://tools.ietf.org/html/rfc8017#appendix-B.2.1
--- crypto/rsa-psspad.c | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+)
crypto/rsa-psspad.c +54 -0
@@ -70,6 +70,60 @@ static int psspad_set_sig_params(struct crypto_akcipher *tfm,return 0 ; } +/* MGF1 per RFC8017 B.2.1. */ +static int pkcs1_mgf1 (u8 *seed,unsigned int seed_len, +struct shash_desc *desc, +u8 *mask,unsigned int mask_len) +{ +unsigned int pos, h_len, i, c; +u8 *tmp; +int ret =0 ; + + h_len =crypto_shash_digestsize (desc->tfm); + + pos = i =0 ; +while ((i < (mask_len / h_len) +1 ) && pos < mask_len) { +/* Compute T = T || Hash(mgfSeed || C) into mask at pos. */ + c =cpu_to_be32 (i); + + ret =crypto_shash_init (desc); +if (ret <0 ) + goto out_err; + + ret =crypto_shash_update (desc, seed, seed_len); +if (ret <0 ) + goto out_err; + + ret =crypto_shash_update (desc, (u8 *) &c,sizeof (c)); +if (ret <0 ) + goto out_err; + +if (mask_len - pos >= h_len) { + ret =crypto_shash_final (desc, mask + pos); + pos += h_len; + }else { + tmp =kzalloc (h_len,GFP_KERNEL ); +if (!tmp) { + ret = -ENOMEM ; + goto out_err; + } + ret =crypto_shash_final (desc, tmp); +/* copy the last hash */ +memcpy (mask + pos, tmp, mask_len - pos); +kfree (tmp); + pos = mask_len; + } +if (ret <0 ) { + goto out_err; + } + + i++; + } + +out_err: +return ret; +} +static int psspad_s_v_e_d (struct akcipher_request *req) {return -EOPNOTSUPP ;
Signature · 1 line
2.30.2
PATCH v3 15/18 crypto: rsa-psspad: Provide PSS signature verify operation
In reply to Varad GautamTrigger RSA transform on the signature being verified from psspad_verify, to produce intermediary data which will be handled in the psspad_verify_complete callback.
Reference: https://tools.ietf.org/html/rfc8017#section-8.1.2
--- crypto/rsa-psspad.c | 53 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-)
crypto/rsa-psspad.c +48 -5
@@ -124,7 +124,50 @@ static int pkcs1_mgf1(u8 *seed, unsigned int seed_len,return ret; } -static int psspad_s_v_e_d (struct akcipher_request *req) +static int psspad_verify_complete (struct akcipher_request *req,int err) +{ +return -EOPNOTSUPP ; +} + +static void psspad_verify_complete_cb (struct crypto_async_request *child_async_req, +int err) +{ +rsapad_akcipher_req_complete (child_async_req, err, + psspad_verify_complete); +} + +static int psspad_verify (struct akcipher_request *req) +{ +struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req); +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm); +struct rsapad_akciper_req_ctx *req_ctx =akcipher_request_ctx (req); +int err; + +if (WARN_ON (req->dst) || +WARN_ON (!req->dst_len) || + !ctx->key_size || req->src_len < ctx->key_size) +return -EINVAL ; + + req_ctx->out_buf =kmalloc (ctx->key_size + req->dst_len,GFP_KERNEL ); +if (!req_ctx->out_buf) +return -ENOMEM ; + +rsapad_akcipher_sg_set_buf (req_ctx->out_sg, req_ctx->out_buf, + ctx->key_size,NULL ); + +/* Reuse input buffer, output to a new buffer */ +rsapad_akcipher_setup_child (req, req->src, req_ctx->out_sg, + req->src_len, ctx->key_size, + psspad_verify_complete_cb); + + err =crypto_akcipher_encrypt (&req_ctx->child_req); +if (err != -EINPROGRESS && err != -EBUSY ) +return psspad_verify_complete (req, err); + +return err; +} + +static int psspad_s_e_d (struct akcipher_request *req) {return -EOPNOTSUPP ; }
@@ -133,10 +176,10 @@ static struct akcipher_alg psspad_alg = { .init = rsapad_akcipher_init_tfm, .exit = rsapad_akcipher_exit_tfm, - .encrypt = psspad_s_v_e_d, - .decrypt = psspad_s_v_e_d, - .sign = psspad_s_v_e_d, - .verify = psspad_s_v_e_d, + .encrypt = psspad_s_e_d, + .decrypt = psspad_s_e_d, + .sign = psspad_s_e_d, + .verify = psspad_verify, .set_pub_key = rsapad_set_pub_key, .set_priv_key = rsapad_set_priv_key, .max_size = rsapad_get_max_size,
Signature · 1 line
2.30.2
PATCH v3 16/18 crypto: rsa-psspad: Implement signature verify callback
In reply to Varad GautamThe RSA output must be processed as per the EMSA-PSS-VERIFY operation from RFC8017, which forms the core of the PSS signature verification.
Implement the verification callback, which operates on the RSA output buffer.
Reference: https://tools.ietf.org/html/rfc8017#section-9.1.2
--- crypto/rsa-psspad.c | 114 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 113 insertions(+), 1 deletion(-)
crypto/rsa-psspad.c +113 -1
@@ -8,6 +8,7 @@#include <crypto/hash.h> #include <crypto/internal/akcipher.h> +#include <crypto/internal/rsa.h> #include <crypto/internal/rsa-common.h> #include <crypto/public_key.h>
@@ -126,7 +127,118 @@ static int pkcs1_mgf1(u8 *seed, unsigned int seed_len,static int psspad_verify_complete (struct akcipher_request *req,int err) { -return -EOPNOTSUPP ; +struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req); +struct rsapad_tfm_ctx *ctx =akcipher_tfm_ctx (tfm); +struct rsapad_akciper_req_ctx *req_ctx =akcipher_request_ctx (req); +struct akcipher_instance *inst =akcipher_alg_instance (tfm); +struct rsapad_inst_ctx *ictx =akcipher_instance_ctx (inst); +const struct rsa_asn1_template *digest_info = ictx->digest_info; +struct crypto_shash *hash_tfm =NULL ; +struct shash_desc *desc =NULL ; +struct rsa_mpi_key *pkey =akcipher_tfm_ctx (ctx->child); + +u8 *em, *h, *salt, *maskeddb; +unsigned int em_len, em_bits, h_len, s_len, maskeddb_len; +u8 *m_hash, *db_mask, *db, *h_; +static u8 zeroes[8 ] = {0 }; +unsigned int pos; + +if (err) + goto out; + + err = -EINVAL ; +if (!digest_info) + goto out; + + em = req_ctx->out_buf; + em_len = ctx->key_size; + em_bits =mpi_get_nbits (pkey->n) -1 ; +if ((em_bits &0x7 ) ==0 ) { + em_len--; + em++; + } + + h_len = req->dst_len; + s_len = ictx->salt_len; + +if (em_len < h_len + s_len +2 ) + goto out; + +if (em[em_len -1 ] !=0xbc ) + goto out; + + maskeddb = em; + maskeddb_len = em_len - h_len -1 ; + h = em + maskeddb_len; + +if (em[0 ] & ~((u8 )0xff >> (8 * em_len - em_bits))) + goto out; + + db_mask =kzalloc (maskeddb_len,GFP_KERNEL ); +if (!db_mask) { + err = -ENOMEM ; + goto out; + } + + err =psspad_setup_shash (&hash_tfm, &desc, ictx->mgf_hash_algo); +if (err <0 ) + goto out_db_mask; + + err =pkcs1_mgf1 (h, h_len, desc, db_mask, maskeddb_len); +if (err <0 ) + goto out_shash; + +for (pos =0 ; pos < maskeddb_len; pos++) + maskeddb[pos] ^= db_mask[pos]; + db = maskeddb; + + db[0 ] &= ((u8 )0xff >> (8 * em_len - em_bits)); + + err = -EINVAL ; +for (pos =0 ; pos < em_len - h_len - s_len -2 ; pos++) { +if (db[pos] !=0 ) + goto out_shash; + } +if (db[pos] !=0x01 ) + goto out_shash; + + salt = db + (maskeddb_len - s_len); + + m_hash = req_ctx->out_buf + ctx->key_size; +sg_pcopy_to_buffer (req->src, +sg_nents_for_len (req->src, req->src_len + req->dst_len), + m_hash, + req->dst_len, ctx->key_size); + +if (strcmp (ictx->mgf_hash_algo, digest_info->name) !=0 ) { +psspad_free_shash (hash_tfm, desc); + err =psspad_setup_shash (&hash_tfm, &desc, digest_info->name); +if (err <0 ) + goto out_db_mask; + } + + err =crypto_shash_init (desc); +if (!err) + err =crypto_shash_update (desc, zeroes,8 ); +if (!err) + err =crypto_shash_update (desc, m_hash, h_len); +if (!err) + err =crypto_shash_finup (desc, salt, s_len, m_hash); +if (err <0 ) + goto out_shash; + + h_ = m_hash; + +if (memcmp (h_, h, h_len) !=0 ) + err = -EKEYREJECTED ; + +out_shash: +psspad_free_shash (hash_tfm, desc); +out_db_mask: +kfree (db_mask); +out: +kfree_sensitive (req_ctx->out_buf); +return err; } static void psspad_verify_complete_cb(struct crypto_async_request *child_async_req,
Signature · 1 line
2.30.2
PATCH v3 17/18 crypto: Accept pss as valid encoding during signature verification
In reply to Varad GautamAccept pss encoding for public_key_verify_signature. If CONFIG_CRYPTO_RSASSA_PSS is disabled, crypto_alloc_akcipher will fail to find a pss backend anyway.
--- crypto/asymmetric_keys/public_key.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)
crypto/asymmetric_keys/public_key.c +13 -5
@@ -69,19 +69,20 @@ int software_key_determine_akcipher(const char *encoding, {int n; -if (strcmp (encoding,"pkcs1" ) ==0 ) { +if (strcmp (encoding,"pkcs1" ) ==0 ||strcmp (encoding,"pss" ) ==0 ) {/* The data wangled by the RSA algorithm is typically padded * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447 -* sec 8.2]. +* sec 8.2] or EMSA-PSS [RFC8017 sec 9.1]. */ if (!hash_algo) n =snprintf (alg_name,CRYPTO_MAX_ALG_NAME , -"pkcs1pad(%s)" , +"%spad(%s)" , + encoding, pkey->pkey_algo);else n =snprintf (alg_name,CRYPTO_MAX_ALG_NAME , -"pkcs1pad(%s,%s)" , - pkey->pkey_algo, hash_algo); +"%spad(%s,%s)" , + encoding, pkey->pkey_algo, hash_algo);return n >=CRYPTO_MAX_ALG_NAME ? -EINVAL :0 ; }
@@ -363,6 +364,13 @@ int public_key_verify_signature(const struct public_key *pkey, goto error_free_key; } +if (strcmp (sig->encoding,"pss" ) ==0 ) { + ret =crypto_akcipher_set_sig_params (tfm, sig,sizeof (*sig)); +if (ret) { + goto error_free_key; + } + } +sg_init_table (src_sg,2 );sg_set_buf (&src_sg[0 ], sig->s, sig->s_size);sg_set_buf (&src_sg[1 ], sig->digest, sig->digest_size);
Signature · 1 line
2.30.2
PATCH v3 18/18 keyctl_pkey: Add pkey parameters saltlen and mgfhash for PSS
In reply to Varad Gautamkeyctl pkey_* operations accept enc and hash parameters at present. RSASSA-PSS signatures also require passing in the signature salt length and the mgf hash function.
Add parameters:
- 'saltlen' to feed in salt length of a PSS signature.
- 'mgfhash' to feed in the hash function used for MGF.
---
v3: Rename slen to saltlen, update Documentation/security/keys/core.rst.
Documentation/security/keys/core.rst | 14 +++++++++++++- crypto/asymmetric_keys/asymmetric_type.c | 2 ++ include/linux/keyctl.h | 2 ++ security/keys/keyctl_pkey.c | 13 +++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-)
Documentation/security/keys/core.rst +13 -1
@@ -1022,6 +1022,15 @@ The keyctl syscall functions are: which hash function was used, the hash function can be specified with this, eg. "hash=sha256". + ``mgfhash=<algo>`` In case of "RSASSA-PSS" ("enc=pss"), this specifies + the hash function used with the Mask Generation Function + to generate a signature, eg. "mgfhash=sha256". Supported + hashes are: sha1, sha224, sha256, sha384, and sha512. + + ``saltlen=<salt_length>`` In case of "RSASSA-PSS" ("enc=pss"), this + specifies the salt length as a u16, used to generate a + signature. Eg. "saltlen=32". + The ``__spare[]`` space in the parameter block must be set to 0. This is intended, amongst other things, to allow the passing of passphrases required to unlock a key.
@@ -1700,6 +1709,8 @@ The structure has a number of fields, some of which are mandatory: __u32 in2_len; }; enum kernel_pkey_operation op : 8; + __u16 salt_len; + const char *mgf_hash_algo; }; This includes the key to be used; a string indicating the encoding to use
@@ -1707,7 +1718,8 @@ The structure has a number of fields, some of which are mandatory: RSASSA-PKCS1-v1.5 or RSAES-PKCS1-v1.5 encoding or "raw" if no encoding); the name of the hash algorithm used to generate the data for a signature (if appropriate); the sizes of the input and output (or second input) - buffers; and the ID of the operation to be performed. + buffers; the ID of the operation to be performed; salt length to be used + in case of RSASSA-PSS; and hash algorithm used with MGF for RSASSA-PSS. For a given operation ID, the input and output buffers are used as follows::
crypto/asymmetric_keys/asymmetric_type.c +2 -0
@@ -571,6 +571,8 @@ static int asymmetric_key_verify_signature(struct kernel_pkey_params *params, .hash_algo = params->hash_algo, .digest = (void *)in, .s = (void *)in2, + .salt_length = params->salt_len, + .mgf_hash_algo = params->mgf_hash_algo, };return verify_signature (params->key, &sig);
include/linux/keyctl.h +2 -0
@@ -37,6 +37,8 @@ struct kernel_pkey_params {__u32 in2_len;/* 2nd input data size (verify) */ }; enum kernel_pkey_operation op : 8; +__u16 salt_len; +const char *mgf_hash_algo; };#endif /* __LINUX_KEYCTL_H */
security/keys/keyctl_pkey.c +13 -0
@@ -24,11 +24,15 @@ enum { Opt_err, Opt_enc,/* "enc=<encoding>" eg. "enc=oaep" */ Opt_hash,/* "hash=<digest-name>" eg. "hash=sha1" */ + Opt_saltlen,/* "saltlen=<salt-length>" eg. "saltlen=32" */ + Opt_mgfhash, /* "mgfhash=<digest-name>" eg. "mgfhash=sha1" */ };static const match_table_t param_keys = { { Opt_enc,"enc=%s" }, { Opt_hash,"hash=%s" }, + { Opt_saltlen,"saltlen=%u" }, + { Opt_mgfhash,"mgfhash=%s" }, { Opt_err,NULL } };
@@ -63,6 +67,15 @@ static int keyctl_pkey_params_parse(struct kernel_pkey_params *params) params->hash_algo = q;break ; +case Opt_saltlen: +if (kstrtou16 (q,0 , ¶ms->salt_len)) +return -EINVAL ; +break ; + +case Opt_mgfhash: + params->mgf_hash_algo = q; +break ; +default :return -EINVAL ; }
Signature · 1 line
2.30.2
Re: [PATCH v3 18/18] keyctl_pkey: Add pkey parameters saltlen and mgfhash for PSS
Quoted 25 lines · Varad Gautam
keyctl pkey_* operations accept enc and hash parameters at present. RSASSA-PSS signatures also require passing in the signature salt length and the mgf hash function.
Add parameters:
- 'saltlen' to feed in salt length of a PSS signature.
- 'mgfhash' to feed in the hash function used for MGF.Signed-off-by Varad Gautam <[email protected]>CC Jarkko Sakkinen <[email protected]>CC Ben Boeckel <[email protected]>---
v3: Rename slen to saltlen, updateDocumentation/security/keys/core.rst.Documentation/security/keys/core.rst | 14 +++++++++++++- crypto/asymmetric_keys/asymmetric_type.c | 2 ++ include/linux/keyctl.h | 2 ++ security/keys/keyctl_pkey.c | 13 +++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-)Documentation/security/keys/core.rst +0
-0
Documentation/security/keys/core.rst The keyctl syscall functions are: · line 1031
Quoted 10 lines
Documentation/security/keys/core.rst +7
-0@@ -1022,6 +1022,15 @@ The keyctl syscall functions are: which hash function was used, the hash function can be specified with this, eg. "hash=sha256". + ``mgfhash=<algo>`` In case of "RSASSA-PSS" ("enc=pss"), this specifies + the hash function used with the Mask Generation Function + to generate a signature, eg. "mgfhash=sha256". Supported + hashes are: sha1, sha224, sha256, sha384, and sha512. + + ``saltlen=<salt_length>`` In case of "RSASSA-PSS" ("enc=pss"), this + specifies the salt length as a u16, used to generate a
^
This feels like it is missing a comma at the designated location (after length if the whitespace gets mangled).
Quoted 15 lines
Documentation/security/keys/core.rst +4
-0+ signature. Eg. "saltlen=32". + The ``__spare[]`` space in the parameter block must be set to 0. This is intended, amongst other things, to allow the passing of passphrases required to unlock a key.@@ -1700,6 +1709,8 @@ The structure has a number of fields, some of which are mandatory: __u32 in2_len; }; enum kernel_pkey_operation op : 8; + __u16 salt_len; + const char *mgf_hash_algo; }; This includes the key to be used; a string indicating the encoding to use
Documentation/security/keys/core.rst The structure has a number of fields, some of which are mandatory: · line 1725
Quoted 9 lines
Documentation/security/keys/core.rst +2
-1@@ -1707,7 +1718,8 @@ The structure has a number of fields, some of which are mandatory: RSASSA-PKCS1-v1.5 or RSAES-PKCS1-v1.5 encoding or "raw" if no encoding); the name of the hash algorithm used to generate the data for a signature (if appropriate); the sizes of the input and output (or second input) - buffers; and the ID of the operation to be performed. + buffers; the ID of the operation to be performed; salt length to be used + in case of RSASSA-PSS; and hash algorithm used with MGF for RSASSA-PSS. For a given operation ID, the input and output buffers are used as follows::
Thanks for the docs, they look good to me overall. Other than the comma:
--Ben