QAT HW rounds the output buffer size up to the next supported size,
but result.length was set to alg_bytesize instead of n.length, causing
result comparisons to read past the end of the expected value.

Additionally, when a modulus has a leading zero padding byte, QAT HW
strips it from the result but we never strip it from the expected result,
so the compare fails. Fix verify_modexp() and verify_modinv() to skip
leading zero bytes in the result before comparison.

Fixes: 064ef1b098d1 ("test/crypto: remove PMD-specific asym test suites")

Signed-off-by: Emma Finn <[email protected]>
---
 app/test/test_cryptodev_asym.c      |  8 ++++++++
 app/test/test_cryptodev_asym_util.h | 20 ++++++++++++++++----
 drivers/crypto/qat/qat_asym.c       |  7 +++----
 3 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/app/test/test_cryptodev_asym.c b/app/test/test_cryptodev_asym.c
index 1515372a35..07e5eb5842 100644
--- a/app/test/test_cryptodev_asym.c
+++ b/app/test/test_cryptodev_asym.c
@@ -3826,6 +3826,14 @@ modular_exponentiation(const void *test_data)
        uint8_t result[TEST_DATA_SIZE] = { 0 };
        struct rte_crypto_asym_xform xform = { };
        const uint8_t dev_id = params->valid_devs[0];
+       const struct rte_cryptodev_asymmetric_xform_capability *cap;
+       struct rte_cryptodev_asym_capability_idx cap_idx;
+
+       cap_idx.type = RTE_CRYPTO_ASYM_XFORM_MODEX;
+       cap = rte_cryptodev_asym_capability_get(dev_id, &cap_idx);
+       if (cap == NULL || rte_cryptodev_asym_xform_capability_check_modlen(
+                       cap, vector->modulus.len))
+               return TEST_SKIPPED;
 
        memcpy(input, vector->base.data, vector->base.len);
        memcpy(exponent, vector->exponent.data, vector->exponent.len);
diff --git a/app/test/test_cryptodev_asym_util.h 
b/app/test/test_cryptodev_asym_util.h
index 07e6e831e8..16e4c0da6c 100644
--- a/app/test/test_cryptodev_asym_util.h
+++ b/app/test/test_cryptodev_asym_util.h
@@ -20,8 +20,14 @@ static inline int rsa_verify(struct rsa_test_data *rsa_param,
 static inline int verify_modinv(uint8_t *mod_inv,
                struct rte_crypto_op *result_op)
 {
-       if (memcmp(mod_inv, result_op->asym->modinv.result.data,
-                               result_op->asym->modinv.result.length))
+       const uint8_t *b = result_op->asym->modinv.result.data;
+       size_t b_len = result_op->asym->modinv.result.length;
+
+       while (b_len > 1 && b[0] == 0) {
+               b++;
+               b_len--;
+       }
+       if (memcmp(mod_inv, b, b_len))
                return -1;
        return 0;
 }
@@ -29,8 +35,14 @@ static inline int verify_modinv(uint8_t *mod_inv,
 static inline int verify_modexp(uint8_t *mod_exp,
                struct rte_crypto_op *result_op)
 {
-       if (memcmp(mod_exp, result_op->asym->modex.result.data,
-                               result_op->asym->modex.result.length))
+       const uint8_t *b = result_op->asym->modex.result.data;
+       size_t b_len = result_op->asym->modex.result.length;
+
+       while (b_len > 1 && b[0] == 0) {
+               b++;
+               b_len--;
+       }
+       if (memcmp(mod_exp, b, b_len))
                return -1;
        return 0;
 }
diff --git a/drivers/crypto/qat/qat_asym.c b/drivers/crypto/qat/qat_asym.c
index beb5a27805..7a296cad6c 100644
--- a/drivers/crypto/qat/qat_asym.c
+++ b/drivers/crypto/qat/qat_asym.c
@@ -274,7 +274,7 @@ modexp_collect(struct rte_crypto_asym_op *asym_op,
        rte_memcpy(modexp_result,
                cookie->output_array[0] + alg_bytesize
                - n.length, n.length);
-       asym_op->modex.result.length = alg_bytesize;
+       asym_op->modex.result.length = n.length;
        HEXDUMP("ModExp result", cookie->output_array[0],
                        alg_bytesize);
        return RTE_CRYPTO_OP_STATUS_SUCCESS;
@@ -332,11 +332,10 @@ modinv_collect(struct rte_crypto_asym_op *asym_op,
                QAT_LOG(ERR, "Incorrect length of modinv modulus");
                return RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
        }
-       rte_memcpy(modinv_result + (asym_op->modinv.result.length
-               - n.length),
+       rte_memcpy(modinv_result,
                cookie->output_array[0] + alg_bytesize
                - n.length, n.length);
-       asym_op->modinv.result.length = alg_bytesize;
+       asym_op->modinv.result.length = n.length;
        HEXDUMP("ModInv result", cookie->output_array[0],
                        alg_bytesize);
        return RTE_CRYPTO_OP_STATUS_SUCCESS;
-- 
2.43.0

Reply via email to