akcipher: Introduce verify_rsa/verify for public key algorithms
Subsystems (8)
RFC PATCH v3 1/1 akcipher: Introduce verify_rsa/verify for public key algorithms
Previous akcipher .verify() just `decrypts' (using RSA encrypt which is using public key) signature to uncover message hash, which was then compared in upper level public_key_verify_signature() with the expected hash value, which itself was never passed into verify().
This approach was incompatible with EC-DSA family of algorithms, because, to verify a signature EC-DSA algorithm also needs a hash value as input; then it's used (together with a signature divided into halves r||s') to produce a witness value, which is then compared with r' to determine if the signature is correct. Thus, for EC-DSA, nor requirements of .verify() itself, nor its output expectations in public_key_verify_signature() wasn't sufficient.
Make improved .verify() call which gets hash value as parameter and produce complete signature check without any output besides status.
RSA-centric drivers have replaced verify() with verify_rsa() which have old semantic and which they still should implement (if they want pkcs1pad to work). If akcipher have .verify_rsa() callback, it will be used for a partial verification, which then is finished in crypto_akcipher_verify().
Now for the top level verification only crypto_akcipher_verify() needs to be called.
For pkcs1pad crypto_akcipher_verify_rsa() is introduced which directly calls .verify_rsa() for its backend. Without this api PKCS1 can not be implemented.
Tested on x86_64.
---
This should be applied over cryptodev tree.
Changes since v2:
- output is factored out from public_key_verify_signature() into crypto_akcipher_verify().
- in crypto_akcipher_verify_rsa() -ENOSYS error is added for robustness (if, in the future, some RSA driver will not implement this api).
- api descriptions are updated to be more clear.
Changes since v1:
- complete rework to the different approach and should be treated as a new patch.
crypto/akcipher.c | 60 +++++++++++++++++++++++++++ crypto/asymmetric_keys/public_key.c | 32 ++------------ crypto/rsa-pkcs1pad.c | 4 +- crypto/rsa.c | 2 +- crypto/testmgr.c | 43 ++++++++++--------- drivers/crypto/caam/caampkc.c | 2 +- drivers/crypto/ccp/ccp-crypto-rsa.c | 2 +- drivers/crypto/qat/qat_common/qat_asym_algs.c | 2 +- include/crypto/akcipher.h | 34 ++++++++++----- 9 files changed, 117 insertions(+), 64 deletions(-)
crypto/akcipher.c +60 -0
@@ -25,6 +25,66 @@#include <crypto/internal/akcipher.h> #include "internal.h" +/** +* crypto_akcipher_verify() - Invoke public key signature verification +* +* Function invokes the specific public key signature verification operation +* for a given public key algorithm. +* +* @req: asymmetric key request +* @digest: expected hash value +* @digest_len: hash length +* +* Return: zero on verification success; error code in case of error. +*/ +int crypto_akcipher_verify (struct akcipher_request *req, +const unsigned char *digest,unsigned int digest_len) +{ +struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req); +struct akcipher_alg *alg =crypto_akcipher_alg (tfm); +struct crypto_alg *calg = tfm->base.__crt_alg; +int ret; + +if (WARN_ON (!digest || !digest_len) || +WARN_ON (req->dst || req->dst_len)) +return -EINVAL ; + +crypto_stats_get (calg); +if (alg->verify_rsa) { +struct scatterlist output_sg; +void *output; +unsigned int outlen; + + outlen =crypto_akcipher_maxsize (tfm); + ret = -EKEYREJECTED ; +if (WARN_ON (outlen < digest_len)) + goto out; + ret = -ENOMEM ; + output =kmalloc (outlen,GFP_KERNEL ); +if (!output) + goto out; +sg_init_one (&output_sg, output, outlen); +akcipher_request_set_crypt (req, req->src, &output_sg, + req->src_len, outlen); + +/* Perform the verification calculation. This doesn't actually +* do the verification, but rather calculates the hash expected +* by the signature and returns that to us. +*/ + ret =crypto_akcipher_verify_rsa (req); +if (!ret && + (req->dst_len != digest_len || +memcmp (digest, output, digest_len))) + ret = -EKEYREJECTED ; +kfree (output); + }else + ret = alg->verify (req, digest, digest_len); +out: +crypto_stats_akcipher_verify (ret, calg); +return ret; +} +EXPORT_SYMBOL_GPL (crypto_akcipher_verify); +#ifdef CONFIG_NET static int crypto_akcipher_report (struct sk_buff *skb,struct crypto_alg *alg) {
crypto/asymmetric_keys/public_key.c +4 -28
@@ -227,10 +227,8 @@ int public_key_verify_signature(const struct public_key *pkey,struct crypto_wait cwait;struct crypto_akcipher *tfm;struct akcipher_request *req; -struct scatterlist sig_sg, digest_sg; +struct scatterlist sig_sg;char alg_name[CRYPTO_MAX_ALG_NAME ]; -void *output; -unsigned int outlen;int ret;pr_devel ("=>%s()\n" , __func__);
@@ -263,36 +261,14 @@ int public_key_verify_signature(const struct public_key *pkey,if (ret) goto error_free_req; - ret = -ENOMEM ; - outlen =crypto_akcipher_maxsize (tfm); - output =kmalloc (outlen,GFP_KERNEL ); -if (!output) - goto error_free_req; -sg_init_one (&sig_sg, sig->s, sig->s_size); -sg_init_one (&digest_sg, output, outlen); -akcipher_request_set_crypt (req, &sig_sg, &digest_sg, sig->s_size, - outlen); +akcipher_request_set_crypt (req, &sig_sg,NULL , sig->s_size,0 );crypto_init_wait (&cwait);akcipher_request_set_callback (req,CRYPTO_TFM_REQ_MAY_BACKLOG |CRYPTO_TFM_REQ_MAY_SLEEP , crypto_req_done, &cwait); - -/* Perform the verification calculation. This doesn't actually do the -* verification, but rather calculates the hash expected by the -* signature and returns that to us. -*/ - ret =crypto_wait_req (crypto_akcipher_verify (req), &cwait); -if (ret) - goto out_free_output; - -/* Do the actual verification step. */ -if (req->dst_len != sig->digest_size || -memcmp (sig->digest, output, sig->digest_size) !=0 ) - ret = -EKEYREJECTED ; - -out_free_output: -kfree (output); + ret =crypto_wait_req (crypto_akcipher_verify (req, sig->digest, + sig->digest_size), &cwait); error_free_req:akcipher_request_free (req); error_free_tfm:
crypto/rsa-pkcs1pad.c +2 -2
@@ -551,7 +551,7 @@ static int pkcs1pad_verify(struct akcipher_request *req) req_ctx->out_sg, req->src_len, ctx->key_size); - err =crypto_akcipher_verify (&req_ctx->child_req); + err =crypto_akcipher_verify_rsa (&req_ctx->child_req);if (err != -EINPROGRESS && err != -EBUSY )return pkcs1pad_verify_complete (req, err);
@@ -675,7 +675,7 @@ static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb) inst->alg.encrypt = pkcs1pad_encrypt; inst->alg.decrypt = pkcs1pad_decrypt; inst->alg.sign = pkcs1pad_sign; - inst->alg.verify = pkcs1pad_verify; + inst->alg.verify_rsa = 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;
crypto/rsa.c +1 -1
@@ -354,7 +354,7 @@ static struct akcipher_alg rsa = { .encrypt = rsa_enc, .decrypt = rsa_dec, .sign = rsa_sign, - .verify = rsa_verify, + .verify_rsa = rsa_verify, .set_priv_key = rsa_set_priv_key, .set_pub_key = rsa_set_pub_key, .max_size = rsa_max_size,
crypto/testmgr.c +24 -19
70 lines not on this page · raw message
drivers/crypto/caam/caampkc.c +1 -1
@@ -995,7 +995,7 @@ static struct akcipher_alg caam_rsa = { .encrypt = caam_rsa_enc, .decrypt = caam_rsa_dec, .sign = caam_rsa_dec, - .verify = caam_rsa_enc, + .verify_rsa = caam_rsa_enc, .set_pub_key = caam_rsa_set_pub_key, .set_priv_key = caam_rsa_set_priv_key, .max_size = caam_rsa_max_size,
drivers/crypto/ccp/ccp-crypto-rsa.c +1 -1
@@ -215,7 +215,7 @@ static struct akcipher_alg ccp_rsa_defaults = { .encrypt = ccp_rsa_encrypt, .decrypt = ccp_rsa_decrypt, .sign = ccp_rsa_decrypt, - .verify = ccp_rsa_encrypt, + .verify_rsa = ccp_rsa_encrypt, .set_pub_key = ccp_rsa_setpubkey, .set_priv_key = ccp_rsa_setprivkey, .max_size = ccp_rsa_maxsize,
drivers/crypto/qat/qat_common/qat_asym_algs.c +1 -1
@@ -1301,7 +1301,7 @@ static struct akcipher_alg rsa = { .encrypt = qat_rsa_enc, .decrypt = qat_rsa_dec, .sign = qat_rsa_dec, - .verify = qat_rsa_enc, + .verify_rsa = qat_rsa_enc, .set_pub_key = qat_rsa_setpubkey, .set_priv_key = qat_rsa_setprivkey, .max_size = qat_rsa_max_size,
include/crypto/akcipher.h +23 -11
@@ -55,10 +55,14 @@ struct crypto_akcipher {* algorithm. In case of error, where the dst_len was insufficient, * the req->dst_len will be updated to the size required for the * operation -* @verify: Function performs a sign operation as defined by public key -* algorithm. In case of error, where the dst_len was insufficient, -* the req->dst_len will be updated to the size required for the -* operation +* @verify_rsa: Function performs a partial verify operation as defined by RSA +* algorithm producing signature in the output. In case of error, +* where the dst_len was insufficient, the req->dst_len will be +* updated to the size required for the operation. All RSA +* implementations that could be PKCS1 padded should implement that. +* @verify: Function performs a complete verify operation as defined by public +* key algorithm, returning verification status. Requires digest +* value as input parameter. * @encrypt: Function performs an encrypt operation as defined by public key * algorithm. In case of error, where the dst_len was insufficient, * the req->dst_len will be updated to the size required for the
@@ -91,7 +95,9 @@ struct crypto_akcipher {*/ struct akcipher_alg {int (*sign)(struct akcipher_request *req); -int (*verify)(struct akcipher_request *req); +int (*verify_rsa)(struct akcipher_request *req); +int (*verify)(struct akcipher_request *req,const u8 *digest, +unsigned int digest_len);int (*encrypt)(struct akcipher_request *req);int (*decrypt)(struct akcipher_request *req); int (*set_pub_key)(struct crypto_akcipher *tfm, const void *key,
@@ -343,24 +349,26 @@ static inline int crypto_akcipher_sign(struct akcipher_request *req) }/** -* crypto_akcipher_verify() - Invoke public key verify operation +* crypto_akcipher_verify_rsa() - Invoke partial RSA verify operation * -* Function invokes the specific public key verify operation for a given -* public key algorithm +* Function invokes partial verify operation for a RSA algorithm * * @req: asymmetric key request * +* Note: this should only be used by RSA wrappers such as PKCS1. +* * Return: zero on success; error code in case of error */ -static inline int crypto_akcipher_verify (struct akcipher_request *req) +static inline int crypto_akcipher_verify_rsa (struct akcipher_request *req) {struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req);struct akcipher_alg *alg =crypto_akcipher_alg (tfm);struct crypto_alg *calg = tfm->base.__crt_alg; -int ret; +int ret = -ENOSYS ;crypto_stats_get (calg); - ret = alg->verify (req); +if (alg->verify_rsa) + ret = alg->verify_rsa (req);crypto_stats_akcipher_verify (ret, calg);return ret; }
@@ -406,4 +414,8 @@ static inline int crypto_akcipher_set_priv_key(struct crypto_akcipher *tfm, return alg->set_priv_key(tfm, key, keylen); } + +int crypto_akcipher_verify (struct akcipher_request *req, +const unsigned char *digest, +unsigned int digest_len);#endif
Signature · 1 line
2.11.0
Herbert Xu
maintainerQuoted 25 lines · Vitaly Chikunov
Previous akcipher .verify() just `decrypts' (using RSA encrypt which is using public key) signature to uncover message hash, which was then compared in upper level
public_key_verify_signature()with the expected hash value, which itself was never passed intoverify().This approach was incompatible with EC-DSA family of algorithms, because, to verify a signature EC-DSA algorithm also needs a hash value as input; then it's used (together with a signature divided into halves
r||s') to produce a witness value, which is then compared withr' to determine if the signature is correct. Thus, for EC-DSA, nor requirements of .verify() itself, nor its output expectations inpublic_key_verify_signature()wasn't sufficient.Make improved .verify() call which gets hash value as parameter and produce complete signature check without any output besides status.
RSA-centric drivers have replaced
verify()withverify_rsa()which have old semantic and which they still should implement (if they want pkcs1pad to work). If akcipher have .verify_rsa() callback, it will be used for a partial verification, which then is finished incrypto_akcipher_verify().Now for the top level verification only
crypto_akcipher_verify()needs to be called.
Quoted 7 lines
For pkcs1pad
crypto_akcipher_verify_rsa()is introduced which directly calls .verify_rsa() for its backend. Without this api PKCS1 can not be implemented.Tested on
x86_64.Signed-off-by Vitaly Chikunov <[email protected]>
Thanks for working on this!
We have been here before. We changed the AEAD interface in a way that is not dissimilar to what you want to do here.
So I think the basic plan should be:
1) Implement new top-level verify, alongside existing verify_rsa.
2) For existing drivers implement a wrapper over verify_rsa.
3) Convert *all* existing users to the new verify API.
4) Remove top-level verify_rsa API.
5) Convert existing drivers over to verify API.
6) Remove verify_rsa completely.
crypto/akcipher.c line 41
Quoted 2 lines
crypto/akcipher.c +2
-0+int crypto_akcipher_verify (struct akcipher_request *req, +const unsigned char *digest,unsigned int digest_len)
We should not add new fields outside of akcipher_request. So these fields need to go inside it. However, I think we don't actually need two new fields. They could both go into the src scatterlist. All we need is a new field, say textlen to indicate where the text stops and where the hash starts. We could also overlay textlen over dstlen as it would now be unused for verify.
The advantage of having it in one scatterlist is that for those users that already have the two pieces together could simply provide a single SG element (I don't know how likely that is in practice though).
For others you would simply tack on an extra SG element.
Thanks,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Quoted 32 lines · Herbert Xu
Quoted 31 lines · Vitaly Chikunov
Previous akcipher .verify() just `decrypts' (using RSA encrypt which is using public key) signature to uncover message hash, which was then compared in upper level
public_key_verify_signature()with the expected hash value, which itself was never passed intoverify().This approach was incompatible with EC-DSA family of algorithms, because, to verify a signature EC-DSA algorithm also needs a hash value as input; then it's used (together with a signature divided into halves
r||s') to produce a witness value, which is then compared withr' to determine if the signature is correct. Thus, for EC-DSA, nor requirements of .verify() itself, nor its output expectations inpublic_key_verify_signature()wasn't sufficient.Make improved .verify() call which gets hash value as parameter and produce complete signature check without any output besides status.
RSA-centric drivers have replaced
verify()withverify_rsa()which have old semantic and which they still should implement (if they want pkcs1pad to work). If akcipher have .verify_rsa() callback, it will be used for a partial verification, which then is finished incrypto_akcipher_verify().Now for the top level verification only
crypto_akcipher_verify()needs to be called.For pkcs1pad
crypto_akcipher_verify_rsa()is introduced which directly calls .verify_rsa() for its backend. Without this api PKCS1 can not be implemented.Tested on
x86_64.
Quoted 8 lines
Quoted 1 line
Signed-off-by Vitaly Chikunov <[email protected]>Thanks for working on this!
We have been here before. We changed the AEAD interface in a way that is not dissimilar to what you want to do here.
So I think the basic plan should be:
While time goes, I got another simpler idea how we should settle this:
1. Since we are know that by nature RSA sign/verify is just encrypt/decrypt, and since sign/verify calls should not be used directly by any normal users:
- Remove sign/verify calls from all existing RSA backends.
2. pkcs1pad should use encrypt/decrypt API for its low level purposes (instead of sign/verify API, which now would be not implemented by them), and provide sign (same as before) and new verify (returning only status).
Thus, we avoid verify_rsa call altogether while remaining its functionality, and skipping conversion step.
Quoted 7 lines
1) Implement new top-level verify, alongside existing
verify_rsa.
2) For existing drivers implement a wrapper oververify_rsa.
3) Convert *all* existing users to the new verify API.
4) Remove top-levelverify_rsaAPI.
5) Convert existing drivers over to verify API.
6) Removeverify_rsacompletely.
Quoted 9 lines
crypto/akcipher.c line 41
Quoted 2 lines
crypto/akcipher.c +2
-0+int crypto_akcipher_verify (struct akcipher_request *req, +const unsigned char *digest,unsigned int digest_len)We should not add new fields outside of
akcipher_request. So these fields need to go inside it. However, I think we don't actually need two new fields. They could both go into the src scatterlist. All we need is a new field, say textlen to indicate where the text stops and where the hash starts. We could also overlay textlen over dstlen as it would now be unused for verify.
Well, if we allowed to reuse dst* fields why not just put digest over dst scatterlist? That would be much simpler.
Thanks,
Quoted 12 lines
The advantage of having it in one scatterlist is that for those users that already have the two pieces together could simply provide a single SG element (I don't know how likely that is in practice though).
For others you would simply tack on an extra SG element.
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
Herbert Xu
maintainerQuoted 3 lines · Vitaly Chikunov
Well, if we allowed to reuse dst* fields why not just put digest over dst scatterlist? That would be much simpler.
Please see below.
Quoted 6 lines
Quoted 6 lines
The advantage of having it in one scatterlist is that for those users that already have the two pieces together could simply provide a single SG element (I don't know how likely that is in practice though).
For others you would simply tack on an extra SG element.
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