akcipher: Introduce verify2 for public key algorithms
Subsystems (4)
RFC PATCH 1/1 akcipher: Introduce verify2 for public key algorithms
Current akcipher .verify() just decrypts signature to uncover message hash, which is then verified in upper level public_key_verify_signature by memcmp with the expected signature value, which is never passed into verify().
This approach is incompatible with ECDSA algorithms, because, to verify a signature ECDSA algorithm also needs a hash value as input; also, hash is used in ECDSA (together with a signature divided into halves r||s), not to produce hash, but to produce a number, which is then compared to r (first part of the signature) to determine if the signature is correct. Thus, for ECDSA, nor requirements of .verify() itself, nor its output expectations in public_key_verify_signature aren't satisfied.
Make alternative .verify2() call which gets hash value and produce complete signature check (without any output, thus max_size() call will not be needed for verify2() operation).
If .verify2() call is present, it should be used in place of .verify().
--- crypto/asymmetric_keys/public_key.c | 57 ++++++++++++++++++++++++------------- include/crypto/akcipher.h | 54 +++++++++++++++++++++++++++++++++-- 2 files changed, 89 insertions(+), 22 deletions(-)
crypto/asymmetric_keys/public_key.c +38 -19
@@ -242,6 +242,7 @@ int public_key_verify_signature(const struct public_key *pkey,char alg_name[CRYPTO_MAX_ALG_NAME ];void *output;unsigned int outlen; +int verify2;int ret;pr_devel ("=>%s()\n" , __func__);
@@ -279,14 +280,23 @@ 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; - + verify2 =crypto_akcipher_have_verify2 (req); +if (!verify2) { +/* verify2 does not need output buffer */ + ret = -ENOMEM ; + outlen =crypto_akcipher_maxsize (tfm); + output =kmalloc (outlen,GFP_KERNEL ); +if (!output) + goto error_free_req; + +sg_init_one (&digest_sg, output, outlen); + }else { +/* dummy init digest_sg */ +memset (&digest_sg,0 ,sizeof (digest_sg)); + output =NULL ; + outlen =0 ; + }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);crypto_init_wait (&cwait);
@@ -294,18 +304,27 @@ int public_key_verify_signature(const struct public_key *pkey,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 ; +if (!verify2) { +/* 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 ; + }else { +/* Perform full verification in one call. */ + req->digest = sig->digest; + req->digest_len = sig->digest_size; + ret =crypto_wait_req (crypto_akcipher_verify2 (req), &cwait); +if (ret) + goto out_free_output; + } out_free_output:kfree (output);
include/crypto/akcipher.h +51 -3
92 lines not on this page · raw message
Signature · 1 line
2.11.0
Herbert Xu
maintainerQuoted 13 lines · Vitaly Chikunov
Current akcipher .verify() just decrypts signature to uncover message hash, which is then verified in upper level
public_key_verify_signatureby memcmp with the expected signature value, which is never passed intoverify().This approach is incompatible with ECDSA algorithms, because, to verify a signature ECDSA algorithm also needs a hash value as input; also, hash is used in ECDSA (together with a signature divided into halves
r||s), not to produce hash, but to produce a number, which is then compared tor(first part of the signature) to determine if the signature is correct. Thus, for ECDSA, nor requirements of .verify() itself, nor its output expectations inpublic_key_verify_signaturearen't satisfied.
Quoted 7 lines
Make alternative .verify2() call which gets hash value and produce complete signature check (without any output, thus
max_size()call will not be needed forverify2()operation).If .verify2() call is present, it should be used in place of .verify().
Signed-off-by Vitaly Chikunov <[email protected]>
We should convert all existing users to this interface and not have both verify/verify2 forever.
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
SGksDQoNCk9uIDEyLzExLzIwMTggMDY6NTkgUE0sIFZpdGFseSBDaGlrdW5vdiB3cm90ZToNCj4g Q3VycmVudCBha2NpcGhlciAudmVyaWZ5KCkganVzdCBkZWNyeXB0cyBzaWduYXR1cmUgdG8gdW5j b3ZlciBtZXNzYWdlDQo+IGhhc2gsIHdoaWNoIGlzIHRoZW4gdmVyaWZpZWQgaW4gdXBwZXIgbGV2 ZWwgcHVibGljX2tleV92ZXJpZnlfc2lnbmF0dXJlDQo+IGJ5IG1lbWNtcCB3aXRoIHRoZSBleHBl Y3RlZCBzaWduYXR1cmUgdmFsdWUsIHdoaWNoIGlzIG5ldmVyIHBhc3NlZCBpbnRvDQo+IHZlcmlm eSgpLg0KPiANCj4gVGhpcyBhcHByb2FjaCBpcyBpbmNvbXBhdGlibGUgd2l0aCBFQ0RTQSBhbGdv cml0aG1zLCBiZWNhdXNlLCB0byB2ZXJpZnkNCg0KSSB3b3VsZCBsb3ZlIHRvIGhhdmUgRUNEU0Eg aW4ga2VybmVsIGJ1dCB1bmZvcnR1bmF0ZWx5IGl0IGhhc24ndCByZWFjaGVkIGtlcm5lbA0KYmVj YXVzZSB0aGVyZSBpcyBubyBpbi1rZXJuZWwgdXNlciBmb3IgaXQuIERvIHdlIGhhdmUgYW4gYWdy ZWVtZW50IHRoYXQgd2Ugd2lsbA0KYWRkIHN1cHBvcnQgZm9yIGl0PyBJZiBub3QsIHdobyB3aWxs IGJlbmVmaXQgb2YgdGhlc2UgcGF0Y2hlcz8NCg0KVGhhbmtzLA0KdGENCg=
Tudor,
Quoted 7 lines · On Thu, Dec 13, 2018 at 10:26:53AM +0000, [email protected] wrote:
Quoted 5 lines · Vitaly Chikunov
Current akcipher .verify() just decrypts signature to uncover message hash, which is then verified in upper level
public_key_verify_signatureby memcmp with the expected signature value, which is never passed intoverify().
Quoted 5 lines
Quoted 1 line
This approach is incompatible with ECDSA algorithms, because, to verify
I would love to have ECDSA in kernel but unfortunately it hasn't reached kernel because there is no in-kernel user for it. Do we have an agreement that we will add support for it? If not, who will benefit of these patches?
I will post patchset for EC-RDSA support (which is slightly different from ECDSA, but is the same algorithm family). This is intended for use in IMA.
Even though EC-RDSA is different from ECDSA it will require the same changes that I propose in these RFCs. Basically, after EC-RDSA is implemented and hooked into IMA it will be much easier to implement ECDSA.
An additional use case is future possibility to implement other signature schemes out of tree, which is currently not possible because API is very RSA-centric.
Thanks,
Quoted 3 lines
Thanks,
ta
Quoted 18 lines · Herbert Xu
Quoted 17 lines · Vitaly Chikunov
Current akcipher .verify() just decrypts signature to uncover message hash, which is then verified in upper level
public_key_verify_signatureby memcmp with the expected signature value, which is never passed intoverify().This approach is incompatible with ECDSA algorithms, because, to verify a signature ECDSA algorithm also needs a hash value as input; also, hash is used in ECDSA (together with a signature divided into halves
r||s), not to produce hash, but to produce a number, which is then compared tor(first part of the signature) to determine if the signature is correct. Thus, for ECDSA, nor requirements of .verify() itself, nor its output expectations inpublic_key_verify_signaturearen't satisfied.Make alternative .verify2() call which gets hash value and produce complete signature check (without any output, thus
max_size()call will not be needed forverify2()operation).
Quoted 6 lines
Quoted 3 lines
If .verify2() call is present, it should be used in place of .verify().
Signed-off-by Vitaly Chikunov <[email protected]>We should convert all existing users to this interface and not have both verify/verify2 forever.
This will be hard to do since there is at least tree device that use this interface (and who know how much out of tree):
drivers$ git grep cra_name.*rsa
crypto/caam/caampkc.c: .cra_name = "rsa",
crypto/ccp/ccp-crypto-rsa.c: .cra_name = "rsa",
crypto/qat/qat_common/qat_asym_algs.c: .cra_name = "rsa",
Interface seems to be designed that verify() call is interchangeable with encrypt().
Two verify does not seem that bad since there is common code for the old interface that removes code duplication and simplifies driver implementation (RSA drivers only need to implement encrypt).
But, I would remove scatterlist from the new interface. Signature verification is not some multi-block encryption. And basically, public_key_verify_signature just doing sg_init_one for both required src/dst buffers.
ps. And also, in the future, I would allow akcipher to access struct public_key and struct public_key_signature so it could distinguish when the key is already validated and skip expensive validation other time verify2 is used with the same key. Or maybe flag 'key validation is needed' should be maintained outside of akcipher and passed to it in the request.
Quoted 6 lines
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
David Howells
maintainerQuoted 8 lines · Vitaly Chikunov
This will be hard to do since there is at least tree device that use this interface (and who know how much out of tree):
drivers$ git grep cra_name.*rsa
crypto/caam/caampkc.c: .cra_name = "rsa",
crypto/ccp/ccp-crypto-rsa.c: .cra_name = "rsa",
crypto/qat/qat_common/qat_asym_algs.c: .cra_name = "rsa",
Quoted 6 lines
Interface seems to be designed that
verify()call is interchangeable withencrypt().Two verify does not seem that bad since there is common code for the old interface that removes code duplication and simplifies driver implementation (RSA drivers only need to implement encrypt).
You could move the comparison into core crypto code if it's makes more sense than moving the comparison to the crypto algorithm ->verify() call. It makes more sense than the upper layers having to cover the differences between the algo modules.
David