cryptodev: fix inconsistency in RSA op usage
cover cryptodev: fix inconsistency in RSA op usage
From: Ramkumar <[email protected]>
The RSA verify operation is performed in two stages: 1. decrypt using public key (output: plaintext message) 2. Compare resultant plaintext message with the expected plaintext message to verify. (return succ/fail in status field) Some applications need the decrypted plaintext (stage 1 result) also to be retunred. For reference, OpenSSL also provides similar API (RSA_public_decrypt).
lib cryptodev API failed to specify a field in 'struct rte_crypto_rsa_op_param' to return the plaintext result after public key decryption. It created inconsistency among crypto PMDs in returning plaintext during RSA verify.
Inconsistency in RSA verify,crypto/octeontx - uses 'sign' field to return plaintext crypto/cnxk - uses 'sign' field to return plaintext crypto/openssl - does not return plaintext crypto/qat - uses 'cipher' field to return plaintext test/cryptodev_asym - expects PMDs to use 'cipher' field
Thus, this patch series fixes all usages to only use 'cipher' field for above described scenario. The 'sign' and 'message' fields are not chosen as they are used for different purpose under same operation.
rte_crypto_rsa_op_param struct fields to use for RTE_CRYPTO_ASYM_OP_VERIFY:
1. input: rsa.sign - signature to be decrypted or verified
2. input: rsa.message - expected plaintext, used to compare
3. output: rsa.cipher - resultant plaintext from decryption
Ramkumar (5):
cryptodev: fix RSA op cipher field description crypto/openssl: fix output of RSA verify op crypto/octeontx: fix output field for RSA verify crypto/octeontx2: fix output field for RSA verify crypto/cnxk: fix output field for RSA verify
drivers/crypto/cnxk/cnxk_ae.h | 15 +++++++++------ drivers/crypto/octeontx/otx_cryptodev_ops.c | 10 ++++++---- drivers/crypto/octeontx2/otx2_cryptodev_ops.c | 16 +++++++++------- drivers/crypto/openssl/rte_openssl_pmd.c | 16 +++++++++++----- lib/cryptodev/rte_crypto_asym.h | 7 ++++--- 5 files changed, 39 insertions(+), 25 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/5 cryptodev: fix RSA op cipher field description
From: Ramkumar <[email protected]>
The description for 'struct rte_crypto_rsa_op_param' failed to specify a field for returning the plaintext from RSA public key decryption.
This patch fixes the rte_crypto_rsa_op_param description to specify 'cipher' field to be used for returning plaintext during RSA op_type == RTE_CRYPTO_ASYM_OP_VERIFY.
--- lib/cryptodev/rte_crypto_asym.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
lib/cryptodev/rte_crypto_asym.h +4 -3
@@ -461,11 +461,12 @@ struct rte_crypto_rsa_op_param {* - to be decrypted for RSA private decrypt. * * Pointer to output data -* - for RSA public encrypt. +* - for RSA public encrypt/decrypt. * In this case the underlying array should have been allocated -* with enough memory to hold ciphertext output (i.e. must be +* with enough memory to hold ciphertext/plaintext output (i.e. must be * at least RSA key size). The cipher.length field should -* be 0 and will be overwritten by the PMD with the encrypted length. +* be 0 and will be overwritten by the PMD with the encrypted/decrypted +* length. * * All data is in Octet-string network byte order format. */
Signature · 1 line
2.17.1
PATCH 2/5 crypto/openssl: fix output of RSA verify op
In reply to Ramkumar BaluFrom: Ramkumar <[email protected]>
During RSA verify, the OpenSSL PMD fails to return the plaintext after public key decryption.
This patch fixes the OpenSSL PMD to return the decrypted plaintext in cipher.data / cipher.length fields
--- drivers/crypto/openssl/rte_openssl_pmd.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/crypto/openssl/rte_openssl_pmd.c +11 -5
@@ -1953,12 +1953,16 @@ process_openssl_rsa_op(struct rte_crypto_op *cop,break ;case RTE_CRYPTO_ASYM_OP_VERIFY : - tmp =rte_malloc (NULL , op->rsa.sign.length,0 ); + tmp = op->rsa.cipher.data;if (tmp ==NULL ) { -OPENSSL_LOG (ERR ,"Memory allocation failed" ); - cop->status =RTE_CRYPTO_OP_STATUS_ERROR ; -break ; + tmp =rte_malloc (NULL , op->rsa.sign.length,0 ); +if (tmp ==NULL ) { +OPENSSL_LOG (ERR ,"Memory allocation failed" ); + cop->status =RTE_CRYPTO_OP_STATUS_ERROR ; +break ; + } } + ret = RSA_public_decrypt(op->rsa.sign.length, op->rsa.sign.data, tmp,
@@ -1974,7 +1978,9 @@ process_openssl_rsa_op(struct rte_crypto_op *cop,OPENSSL_LOG (ERR ,"RSA sign Verification failed" ); cop->status =RTE_CRYPTO_OP_STATUS_ERROR ; } -rte_free (tmp); + op->rsa.cipher.length = ret; +if (tmp != op->rsa.cipher.data) +rte_free (tmp);break ;default :
Signature · 1 line
2.17.1
RE: [PATCH 2/5] crypto/openssl: fix output of RSA verify op
Quoted 54 lines
-----Original Message-----
From: Ramkumar Balu <[email protected]>
Sent: Monday, November 29, 2021 10:52 AM
To: Akhil Goyal <[email protected]>; Anoob Joseph <[email protected]>; Doherty, Declan <[email protected]>; Zhang, Roy Fan <[email protected]>; Ankur Dwivedi <[email protected]>; Tejasree Kondoj <[email protected]>
Cc: [email protected]; [email protected]; Ramkumar <[email protected]>
Subject: [PATCH 2/5]crypto/openssl: fix output of RSA verify opFrom: Ramkumar <[email protected]>
During RSA verify, the OpenSSL PMD fails to return the plaintext after public key decryption.
This patch fixes the OpenSSL PMD to return the decrypted plaintext in cipher.data / cipher.length fieldsFixes 3e9d6bd447fb ("crypto/openssl: add RSA and mod asym operations")Fixes fe1606e0138c ("crypto/openssl: fix RSA verify operation")Signed-off-by Ramkumar <[email protected]>--- drivers/crypto/openssl/rte_openssl_pmd.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)drivers/crypto/openssl/rte_openssl_pmd.c +8
-4@@ -1953,12 +1953,16 @@ process_openssl_rsa_op(struct rte_crypto_op *cop, break;case RTE_CRYPTO_ASYM_OP_VERIFY : - tmp =rte_malloc (NULL , op->rsa.sign.length,0 ); + tmp = op->rsa.cipher.data;if (tmp ==NULL ) { -OPENSSL_LOG (ERR ,"Memory allocation failed" ); - cop->status =RTE_CRYPTO_OP_STATUS_ERROR ; -break ; + tmp =rte_malloc (NULL , op->rsa.sign.length,0 ); +if (tmp ==NULL ) { +OPENSSL_LOG (ERR ,"Memory allocation failed"); + cop->status =RTE_CRYPTO_OP_STATUS_ERROR ; +break ; + } } +
drivers/crypto/openssl/rte_openssl_pmd.c process_openssl_rsa_op(struct rte_crypto_op *cop, · line 1968
Quoted 3 lines
drivers/crypto/openssl/rte_openssl_pmd.c +0
-0ret = RSA_public_decrypt(op->rsa.sign.length, op->rsa.sign.data, tmp,
[Arek] - this function is deprecated and more importantly it properly handle only NO_PADDING situation (no der encoding, like pre TLS 1.2). OpenSSL code needs major refactor in this area soon (mostly in asymmetric crypto).
Quoted 13 lines
drivers/crypto/openssl/rte_openssl_pmd.c +3
-2@@ -1974,7 +1978,9 @@ process_openssl_rsa_op(struct rte_crypto_op *cop,OPENSSL_LOG (ERR ,"RSA sign Verification failed" ); cop->status =RTE_CRYPTO_OP_STATUS_ERROR ; } -rte_free (tmp); + op->rsa.cipher.length = ret; +if (tmp != op->rsa.cipher.data) +rte_free (tmp);break ;default : --2.17.1
RE: [PATCH 2/5] crypto/openssl: fix output of RSA verify op
Thank you for the comments. I agree that OpenSSL PMD needs a major refactoring in asym crypto. I have asked Akhil to reject this patch series.
-----Original Message-----
From: Kusztal, ArkadiuszX <[email protected]>
Sent: Tuesday, December 28, 2021 2:41 PM
To: Ramkumar Balu <[email protected]>; Akhil Goyal <[email protected]>; Anoob Joseph <[email protected]>; Doherty, Declan <[email protected]>; Zhang, Roy Fan <[email protected]>; Ankur Dwivedi <[email protected]>; Tejasree Kondoj <[email protected]>
Cc: [email protected]; [email protected]
Subject: [EXT] RE: [PATCH 2/5] crypto/openssl: fix output of RSA verify op
----------------------------------------------------------------------
Quoted 53 lines
-----Original Message-----
From: Ramkumar Balu <[email protected]>
Sent: Monday, November 29, 2021 10:52 AM
To: Akhil Goyal <[email protected]>; Anoob Joseph <[email protected]>; Doherty, Declan <[email protected]>; Zhang, Roy Fan <[email protected]>; Ankur Dwivedi <[email protected]>; Tejasree Kondoj <[email protected]>
Cc: [email protected]; [email protected]; Ramkumar <[email protected]>
Subject: [PATCH 2/5]crypto/openssl: fix output of RSA verify opFrom: Ramkumar <[email protected]>
During RSA verify, the OpenSSL PMD fails to return the plaintext after public key decryption.
This patch fixes the OpenSSL PMD to return the decrypted plaintext in cipher.data / cipher.length fieldsFixes 3e9d6bd447fb ("crypto/openssl: add RSA and mod asymoperations")
Fixes fe1606e0138c ("crypto/openssl: fix RSA verify operation")Signed-off-by Ramkumar <[email protected]>--- drivers/crypto/openssl/rte_openssl_pmd.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)drivers/crypto/openssl/rte_openssl_pmd.c +8
-4@@ -1953,12 +1953,16 @@ process_openssl_rsa_op(struct rte_crypto_op *cop, break;case RTE_CRYPTO_ASYM_OP_VERIFY : - tmp =rte_malloc (NULL , op->rsa.sign.length,0 ); + tmp = op->rsa.cipher.data;if (tmp ==NULL ) { -OPENSSL_LOG (ERR ,"Memory allocation failed" ); - cop->status =RTE_CRYPTO_OP_STATUS_ERROR ; -break ; + tmp =rte_malloc (NULL , op->rsa.sign.length,0 ); +if (tmp ==NULL ) { +OPENSSL_LOG (ERR ,"Memory allocation failed"); + cop->status =RTE_CRYPTO_OP_STATUS_ERROR ; +break ; + } } +
drivers/crypto/openssl/rte_openssl_pmd.c process_openssl_rsa_op(struct rte_crypto_op *cop, · line 1968
Quoted 3 lines
drivers/crypto/openssl/rte_openssl_pmd.c +0
-0ret = RSA_public_decrypt(op->rsa.sign.length, op->rsa.sign.data, tmp,
[Arek] - this function is deprecated and more importantly it properly handle only NO_PADDING situation (no der encoding, like pre TLS 1.2). OpenSSL code needs major refactor in this area soon (mostly in asymmetric crypto).
Quoted 13 lines
drivers/crypto/openssl/rte_openssl_pmd.c +3
-2@@ -1974,7 +1978,9 @@ process_openssl_rsa_op(struct rte_crypto_op *cop,OPENSSL_LOG (ERR ,"RSA sign Verification failed" ); cop->status =RTE_CRYPTO_OP_STATUS_ERROR ; } -rte_free (tmp); + op->rsa.cipher.length = ret; +if (tmp != op->rsa.cipher.data) +rte_free (tmp);break ;default : --2.17.1
PATCH 3/5 crypto/octeontx: fix output field for RSA verify
In reply to Ramkumar BaluFrom: Ramkumar <[email protected]>
During RSA sign verification, the OCTEONTX PMD returns the decrypted plaintext in 'sign' field of rte_crypto_rsa_op_param. The 'sign' field is actually used to pass input to the operation. This PMD overwrites the 'sign' field buffer. This is non-compliance to lib cryptodev.
This patch fixes the PMD to use 'cipher' field to return the decrypted plaintext during RSA verify operation.
--- drivers/crypto/octeontx/otx_cryptodev_ops.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/crypto/octeontx/otx_cryptodev_ops.c +6 -4
@@ -788,18 +788,20 @@ otx_cpt_asym_rsa_op(struct rte_crypto_op *cop, struct cpt_request_info *req,break ;case RTE_CRYPTO_ASYM_OP_VERIFY :if (rsa->pad ==RTE_CRYPTO_RSA_PADDING_NONE ) - rsa->sign.length = rsa_ctx->n.length; + rsa->cipher.length = rsa_ctx->n.length;else {/* Get length of decrypted output */ - rsa->sign.length =rte_cpu_to_be_16 + rsa->cipher.length =rte_cpu_to_be_16 (*((uint16_t *)req->rptr));/* Offset data pointer by length fields */ req->rptr +=2 ; } -memcpy (rsa->sign.data, req->rptr, rsa->sign.length); -if (memcmp (rsa->sign.data, rsa->message.data, +if (rsa->cipher.data !=NULL ) +memcpy (rsa->cipher.data, req->rptr, rsa->cipher.length); + +if (memcmp (req->rptr, rsa->message.data, rsa->message.length)) {CPT_LOG_DP_ERR ("RSA verification failed" ); cop->status =RTE_CRYPTO_OP_STATUS_ERROR ;
Signature · 1 line
2.17.1
PATCH 4/5 crypto/octeontx2: fix output field for RSA verify
In reply to Ramkumar BaluFrom: Ramkumar <[email protected]>
During RSA sign verification, the OCTEONTX2 PMD returns the decrypted plaintext in 'sign' field of rte_crypto_rsa_op_param. The 'sign' field is actually used to pass input to the operation. This PMD overwrites the 'sign' field buffer. This is non-compliance to lib cryptodev.
This patch fixes the PMD to use 'cipher' field to return the decrypted plaintext during RSA verify operation.
--- drivers/crypto/octeontx2/otx2_cryptodev_ops.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/crypto/octeontx2/otx2_cryptodev_ops.c +9 -7
@@ -876,20 +876,22 @@ otx2_cpt_asym_rsa_op(struct rte_crypto_op *cop, struct cpt_request_info *req,break ;case RTE_CRYPTO_ASYM_OP_VERIFY :if (rsa->pad ==RTE_CRYPTO_RSA_PADDING_NONE ) { - rsa->sign.length = rsa_ctx->n.length; -memcpy (rsa->sign.data, req->rptr, rsa->sign.length); + rsa->cipher.length = rsa_ctx->n.length; }else {/* Get length of signed output */ - rsa->sign.length =rte_cpu_to_be_16 + rsa->cipher.length =rte_cpu_to_be_16 (*((uint16_t *)req->rptr));/* * Offset output data pointer by length field -* (2 bytes) and copy signed data. +* (2 bytes). */ -memcpy (rsa->sign.data, req->rptr +2 , - rsa->sign.length); + req->rptr +=2 ; } -if (memcmp (rsa->sign.data, rsa->message.data, + +if (rsa->cipher.data !=NULL ) +memcpy (rsa->cipher.data, req->rptr, rsa->cipher.length); + +if (memcmp (req->rptr, rsa->message.data, rsa->message.length)) {CPT_LOG_DP_ERR ("RSA verification failed" ); cop->status =RTE_CRYPTO_OP_STATUS_ERROR ;
Signature · 1 line
2.17.1
PATCH 5/5 crypto/cnxk: fix output field for RSA verify
In reply to Ramkumar BaluFrom: Ramkumar <[email protected]>
During RSA sign verification, this PMD returns the decrypted plaintext in 'sign' field of rte_crypto_rsa_op_param. The 'sign' field is actually used to pass input to the operation. This PMD overwrites the 'sign' field buffer. This is non-compliance to lib cryptodev.
This patch fixes the PMD to use 'cipher' field to return the decrypted plaintext during RSA verify operation.
--- drivers/crypto/cnxk/cnxk_ae.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/crypto/cnxk/cnxk_ae.h +9 -6
@@ -696,19 +696,22 @@ cnxk_ae_dequeue_rsa_op(struct rte_crypto_op *cop, uint8_t *rptr,break ;case RTE_CRYPTO_ASYM_OP_VERIFY :if (rsa->pad ==RTE_CRYPTO_RSA_PADDING_NONE ) { - rsa->sign.length = rsa_ctx->n.length; -memcpy (rsa->sign.data, rptr, rsa->sign.length); + rsa->cipher.length = rsa_ctx->n.length; }else {/* Get length of signed output */ - rsa->sign.length = + rsa->cipher.length =rte_cpu_to_be_16 (*((uint16_t *)rptr));/* * Offset output data pointer by length field -* (2 bytes) and copy signed data. +* (2 bytes). */ -memcpy (rsa->sign.data, rptr +2 , rsa->sign.length); + rptr +=2 ; } -if (memcmp (rsa->sign.data, rsa->message.data, + +if (rsa->cipher.data !=NULL ) +memcpy (rsa->cipher.data, rptr, rsa->cipher.length); + +if (memcmp (rptr, rsa->message.data, rsa->message.length)) { cop->status =RTE_CRYPTO_OP_STATUS_ERROR ; }
Signature · 1 line
2.17.1
Hi Ramkumar,
Quoted 10 lines
-----Original Message-----
From: Ramkumar Balu <[email protected]>
Sent: Monday, November 29, 2021 10:52 AM
To: Akhil Goyal <[email protected]>; Anoob Joseph <[email protected]>; Doherty, Declan <[email protected]>; Zhang, Roy Fan <[email protected]>; Ankur Dwivedi <[email protected]>; Tejasree Kondoj <[email protected]>
Cc: [email protected]; [email protected]; Ramkumar <[email protected]>
Subject: [PATCH 0/5] cryptodev: fix inconsistency in RSA op usage
Quoted 6 lines
From: Ramkumar <[email protected]>
The RSA verify operation is performed in two stages: 1. decrypt using public key (output: plaintext message) 2. Compare resultant plaintext message with the expected plaintext message to verify. (return succ/fail in status field) Some applications need the decrypted plaintext (stage 1 result) also to be retunred.
[Arek] - It should only be the case when NO_PADDING selected (that's why QAT returns decrypted data, and only because of that), but I would propose to change it that NO_PADDING signature is not possible -> change it to NO_PADDING Private/Public operations (we cannot verify it in PMD anyway as we do not know padding type).
Quoted 1 line
For reference, OpenSSL also provides similar API (
RSA_public_decrypt).
[Arek] - this function is not only deprecated but incorrect. It should not be used for signatures when padding selected. Normally functions that handle padding will only return verification status only, not data.
Quoted 38 lines
lib cryptodev API failed to specify a field in 'struct
rte_crypto_rsa_op_param' to return the plaintext result after public key decryption. It created inconsistency among crypto PMDs in returning plaintext during RSA verify.Inconsistency in RSA verify,
crypto/octeontx- uses 'sign' field to return plaintextcrypto/cnxk- uses 'sign' field to return plaintextcrypto/openssl- does not return plaintextcrypto/qat- uses 'cipher' field to return plaintext test/cryptodev_asym - expects PMDs to use 'cipher' fieldThus, this patch series fixes all usages to only use 'cipher' field for above described scenario. The 'sign' and 'message' fields are not chosen as they are used for different purpose under same operation.
rte_crypto_rsa_op_paramstruct fields to use forRTE_CRYPTO_ASYM_OP_VERIFY:
1. input: rsa.sign - signature to be decrypted or verified 2. input: rsa.message - expected plaintext, used to compare 3. output: rsa.cipher - resultant plaintext from decryptionRamkumar (5):
cryptodev: fix RSA op cipher field descriptioncrypto/openssl: fix output of RSA verify opcrypto/octeontx: fix output field for RSA verifycrypto/octeontx2: fix output field for RSA verifycrypto/cnxk: fix output field for RSA verifydrivers/crypto/cnxk/cnxk_ae.h | 15 +++++++++------ drivers/crypto/octeontx/otx_cryptodev_ops.c | 10 ++++++---- drivers/crypto/octeontx2/otx2_cryptodev_ops.c | 16 +++++++++------- drivers/crypto/openssl/rte_openssl_pmd.c | 16 +++++++++++----- lib/cryptodev/rte_crypto_asym.h | 7 ++++--- 5 files changed, 39 insertions(+), 25 deletions(-)--
2.17.1