crypto/virtio: fix DER encoding for positive RSA param

Message ID 20250510104412.2154-1-gmuthukrishn@marvell.com (mailing list archive)
State Not Applicable
Delegated to: akhil goyal
Headers
Series crypto/virtio: fix DER encoding for positive RSA param |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/intel-Functional success Functional PASS
ci/github-robot: build success github build: passed
ci/iol-marvell-Functional success Functional Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-abi-testing success Testing PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-sample-apps-testing success Testing PASS
ci/aws-unit-testing success Unit Testing PASS

Commit Message

Gowrishankar Muthukrishnan May 10, 2025, 10:44 a.m. UTC
As per RFC 8017, RSA parameter needs to be positive integer.
This patch fixes TLV encoding function appropriately.

Fixes: 6fe6a7f7bcf ("crypto/virtio: add asymmetric RSA support")

Signed-off-by: Gowrishankar Muthukrishnan <gmuthukrishn@marvell.com>
---
 drivers/crypto/virtio/virtio_cryptodev.c | 30 ++++++++++++++++--------
 1 file changed, 20 insertions(+), 10 deletions(-)
  

Patch

diff --git a/drivers/crypto/virtio/virtio_cryptodev.c b/drivers/crypto/virtio/virtio_cryptodev.c
index b01e97c988..4440e02dc9 100644
--- a/drivers/crypto/virtio/virtio_cryptodev.c
+++ b/drivers/crypto/virtio/virtio_cryptodev.c
@@ -1459,27 +1459,37 @@  tlv_encode(uint8_t *tlv, uint8_t type, uint8_t *data, size_t len)
 {
 	uint8_t *lenval = tlv;
 	size_t lenval_n = 0;
+	size_t dlen = len;
+	uint8_t off = 0;
+
+	if (data != NULL && data[0] & 0x80) {
+		dlen += 1;
+		off = 1;
+	}
 
 	if (len > 65535) {
 		goto _exit;
 	} else if (len > 255) {
-		lenval_n = 4 + len;
+		lenval_n = 4 + dlen;
 		lenval[0] = type;
 		lenval[1] = 0x82;
-		lenval[2] = (len & 0xFF00) >> 8;
-		lenval[3] = (len & 0xFF);
-		rte_memcpy(&lenval[4], data, len);
+		lenval[2] = (dlen & 0xFF00) >> 8;
+		lenval[3] = (dlen & 0xFF);
+		lenval += (4 + off);
+		rte_memcpy(lenval, data, len);
 	} else if (len > 127) {
-		lenval_n = 3 + len;
+		lenval_n = 3 + dlen;
 		lenval[0] = type;
 		lenval[1] = 0x81;
-		lenval[2] = len;
-		rte_memcpy(&lenval[3], data, len);
+		lenval[2] = dlen;
+		lenval += (3 + off);
+		rte_memcpy(lenval, data, len);
 	} else {
-		lenval_n = 2 + len;
+		lenval_n = 2 + dlen;
 		lenval[0] = type;
-		lenval[1] = len;
-		rte_memcpy(&lenval[2], data, len);
+		lenval[1] = dlen;
+		lenval += (2 + off);
+		rte_memcpy(lenval, data, len);
 	}
 
 _exit: