Skip to contentSkip to conversation

akcipher: new verify API for public key algorithms

RFC PATCH In review Vitaly Chikunov 2 participants 4 messages Jan 25, 2019 – Feb 4, 2019

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

RFC PATCH v4 1/1 akcipher: new verify API for public key algorithms

API is reworked following suggestions of Herbert Xu.

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 a signature together with a hash value as input and produce complete signature check without any output besides status.

RSA backends should not have sign/verify callbacks defined (nor they should be used directly). PKCS1 driver knows to call appropriate RSA encrypt/decrypt callbacks to implement sign/verify.

Now for the top level verification only crypto_akcipher_verify() needs to be called.

Tested on x86_64.

---
This should be applied over cryptodev tree.

Changes since v3:
- remove verify_rsa API from RSA backends.
- digest is appended to akcipher_request->src SG.
- pkcs1pad calls encrypt/decrypt for its backends instead of sign/verify.
- pkcs1pad now conform to the new verify API.
- make crypto_akcipher_{maxsize,sign,verify} more robust allowing appropriate akcipher clllbacks to be undefined.

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/asymmetric_keys/asym_tpm.c             |  32 ++------
 crypto/asymmetric_keys/public_key.c           |  33 ++------
 crypto/rsa-pkcs1pad.c                         |  31 +++++---
 crypto/rsa.c                                  | 109 --------------------------
 crypto/testmgr.c                              |  47 ++++++-----
 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                     |  53 +++++++++----
 9 files changed, 94 insertions(+), 217 deletions(-)
crypto/asymmetric_keys/asym_tpm.c +6 -26
index 5d4c270463f6..eef548319877 100644
@@ -744,11 +744,10 @@ static int tpm_key_verify_signature(const struct key *key,
 	struct crypto_wait cwait;
 	struct crypto_akcipher *tfm;
 	struct akcipher_request *req;
-	struct scatterlist sig_sg, digest_sg;
+	struct scatterlist src_sg[2];
 	char alg_name[CRYPTO_MAX_ALG_NAME];
 	uint8_t der_pub_key[PUB_KEY_BUF_SIZE];
 	uint32_t der_pub_key_len;
-	void *output;
 	unsigned int outlen;
 	int ret;
 
@@ -781,36 +780,17 @@ static int tpm_key_verify_signature(const struct key *key,
 	if (!req)
 		goto error_free_tfm;
 
-	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);
+	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);
+	akcipher_request_set_crypt(req, &src_sg, NULL, sig->s_size,
+				   sig->digest_size);
 	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);
 error_free_req:
 	akcipher_request_free(req);
 error_free_tfm:
crypto/asymmetric_keys/public_key.c +6 -27
index f5d85b47fcc6..33093b7bcc47 100644
@@ -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 src_sg[2];
 	char alg_name[CRYPTO_MAX_ALG_NAME];
-	void *output;
-	unsigned int outlen;
 	int ret;
 
 	pr_devel("=>%s()\n", __func__);
@@ -263,36 +261,17 @@ 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);
+	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);
+	akcipher_request_set_crypt(req, src_sg, NULL, sig->s_size,
+				   sig->digest_size);
 	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);
 error_free_req:
 	akcipher_request_free(req);
 error_free_tfm:
crypto/rsa-pkcs1pad.c +19 -12
index 0a6680ca8cb6..489bda6acf08 100644
@@ -429,7 +429,7 @@ static int pkcs1pad_sign(struct akcipher_request *req)
 	akcipher_request_set_crypt(&req_ctx->child_req, req_ctx->in_sg,
 				   req->dst, ctx->key_size - 1, req->dst_len);
 
-	err = crypto_akcipher_sign(&req_ctx->child_req);
+	err = crypto_akcipher_decrypt(&req_ctx->child_req);
 	if (err != -EINPROGRESS && err != -EBUSY)
 		return pkcs1pad_encrypt_sign_complete(req, err);
 
@@ -488,14 +488,20 @@ static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)
 
 	err = 0;
 
-	if (req->dst_len < dst_len - pos)
-		err = -EOVERFLOW;
-	req->dst_len = dst_len - pos;
-
-	if (!err)
-		sg_copy_from_buffer(req->dst,
-				sg_nents_for_len(req->dst, req->dst_len),
-				out_buf + pos, req->dst_len);
+	if (req->dst_len != dst_len - pos) {
+		err = -EKEYREJECTED;
+		req->dst_len = dst_len - pos;
+		goto done;
+	}
+	/* Extract appended digest. */
+	sg_pcopy_to_buffer(req->src,
+			   sg_nents_for_len(req->src,
+					    req->src_len + req->dst_len),
+			   req_ctx->out_buf + ctx->key_size,
+			   req->dst_len, ctx->key_size);
+	/* Do the actual verification step. */
+	if (memcmp(req_ctx->out_buf + ctx->key_size, out_buf + pos, req->dst_len))
+		err = -EKEYREJECTED;
 done:
 	kzfree(req_ctx->out_buf);
 
@@ -532,10 +538,11 @@ static int pkcs1pad_verify(struct akcipher_request *req)
 	struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
 	int err;
 
-	if (!ctx->key_size || req->src_len < ctx->key_size)
+	if (req->dst || !req->dst_len ||
+	    !ctx->key_size || req->src_len < ctx->key_size)
 		return -EINVAL;
 
-	req_ctx->out_buf = kmalloc(ctx->key_size, GFP_KERNEL);
+	req_ctx->out_buf = kmalloc(ctx->key_size + req->dst_len, GFP_KERNEL);
 	if (!req_ctx->out_buf)
 		return -ENOMEM;
 
@@ -551,7 +558,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_encrypt(&req_ctx->child_req);
 	if (err != -EINPROGRESS && err != -EBUSY)
 		return pkcs1pad_verify_complete(req, err);
 
crypto/rsa.c +0 -109

127 lines not on this page · raw message

crypto/testmgr.c +27 -20

77 lines not on this page · raw message

drivers/crypto/caam/caampkc.c +0 -2
index 77ab28a2811a..d7e1fc5bacc5 100644
@@ -994,8 +994,6 @@ static void caam_rsa_exit_tfm(struct crypto_akcipher *tfm)
 static struct akcipher_alg caam_rsa = {
 	.encrypt = caam_rsa_enc,
 	.decrypt = caam_rsa_dec,
-	.sign = caam_rsa_dec,
-	.verify = 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 +0 -2
index 05850dfd7940..71e40680c880 100644
@@ -214,8 +214,6 @@ static void ccp_rsa_exit_tfm(struct crypto_akcipher *tfm)
 static struct akcipher_alg ccp_rsa_defaults = {
 	.encrypt = ccp_rsa_encrypt,
 	.decrypt = ccp_rsa_decrypt,
-	.sign = ccp_rsa_decrypt,
-	.verify = 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 +0 -2
index 320e7854b4ee..c05d03565e96 100644
@@ -1300,8 +1300,6 @@ static void qat_rsa_exit_tfm(struct crypto_akcipher *tfm)
 static struct akcipher_alg rsa = {
 	.encrypt = qat_rsa_enc,
 	.decrypt = qat_rsa_dec,
-	.sign = qat_rsa_dec,
-	.verify = 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 +36 -17

106 lines not on this page · raw message

Signature · 1 line
2.11.0
maintainer
#1 lore raw
Quoted 10 lines · Vitaly Chikunov
crypto/asymmetric_keys/asym_tpm.c +0 -5
@@ -781,36 +780,17 @@ static int tpm_key_verify_signature(const struct key *key,
 	if (!req)
 		goto error_free_tfm;
 
-	ret = -ENOMEM;
-	outlen = crypto_akcipher_maxsize(tfm);
-	output = kmalloc(outlen, GFP_KERNEL);
-	if (!output)
-		goto error_free_req;

crypto/asymmetric_keys/asym_tpm.c static int tpm_key_verify_signature(const struct key *key, · line 787

Quoted 10 lines
crypto/asymmetric_keys/asym_tpm.c +5 -5
-
-	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);
+	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);
+	akcipher_request_set_crypt(req, &src_sg, NULL, sig->s_size,
+				   sig->digest_size);

It's not clear that sig->digest is guaranteed to be kmalloc memory. In any case, it's best not to mix unrelated changes in a single patch. So please keep the kmalloc on output and then copy sig->digest into it and put output into the SG list.

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
#2 lore raw

Herbert,

Quoted 18 lines · Herbert Xu
Quoted 17 lines · Vitaly Chikunov
crypto/asymmetric_keys/asym_tpm.c +2 -10
@@ -781,36 +780,17 @@ static int tpm_key_verify_signature(const struct key *key,
 	if (!req)
 		goto error_free_tfm;
 
-	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);
+	sg_init_table(&src_sg, 2);
+	sg_set_buf(&src_sg[0], sig->s, sig->s_size);
Quoted 8 lines

crypto/asymmetric_keys/asym_tpm.c static int tpm_key_verify_signature(const struct key *key, · line 787

Quoted 3 lines
crypto/asymmetric_keys/asym_tpm.c +3 -0
+	sg_set_buf(&src_sg[1], sig->digest, sig->digest_size);
+	akcipher_request_set_crypt(req, &src_sg, NULL, sig->s_size,
+				   sig->digest_size);

It's not clear that sig->digest is guaranteed to be kmalloc memory. In any case, it's best not to mix unrelated changes in a single patch. So please keep the kmalloc on output and then copy sig->digest into it and put output into the SG list.

It is not guaranteed that sig->s will be kmalloc memory either. (Except we know it for sure like we know the same about sig->digest).

You can see in public_key_signature_free() that both fields are kfree'd together.

So, I don't understand why we should treat sig->digest differently than sig->s.

I was just removing kmalloc'ed output as crypto_akcipher_verify() does not need any output anymore. So, it's not some sort of mixing unrelated changes, from my point of view.

Thanks,

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
#3 lore raw

Herbert,

Quoted 19 lines · Vitaly Chikunov
Quoted 18 lines · Herbert Xu
Quoted 17 lines · Vitaly Chikunov
crypto/asymmetric_keys/asym_tpm.c +2 -10
@@ -781,36 +780,17 @@ static int tpm_key_verify_signature(const struct key *key,
 	if (!req)
 		goto error_free_tfm;
 
-	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);
+	sg_init_table(&src_sg, 2);
+	sg_set_buf(&src_sg[0], sig->s, sig->s_size);
Quoted 8 lines
Quoted 8 lines

crypto/asymmetric_keys/asym_tpm.c static int tpm_key_verify_signature(const struct key *key, · line 787

Quoted 3 lines
crypto/asymmetric_keys/asym_tpm.c +3 -0
+	sg_set_buf(&src_sg[1], sig->digest, sig->digest_size);
+	akcipher_request_set_crypt(req, &src_sg, NULL, sig->s_size,
+				   sig->digest_size);

It's not clear that sig->digest is guaranteed to be kmalloc memory. In any case, it's best not to mix unrelated changes in a single patch. So please keep the kmalloc on output and then copy sig->digest into it and put output into the SG list.

I will do that.

Thanks,

Quoted 21 lines

It is not guaranteed that sig->s will be kmalloc memory either. (Except we know it for sure like we know the same about sig->digest).

You can see in public_key_signature_free() that both fields are kfree'd together.

So, I don't understand why we should treat sig->digest differently than sig->s.

I was just removing kmalloc'ed output as crypto_akcipher_verify() does not need any output anymore. So, it's not some sort of mixing unrelated changes, from my point of view.

Thanks,

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