examples/fips_validation: separate ECB and CBC init
[dpdk.git] / examples / fips_validation / main.c
index 5e3d5ba..f8694ef 100644 (file)
@@ -48,7 +48,7 @@ struct cryptodev_fips_validate_env {
 static int
 cryptodev_fips_validate_app_int(void)
 {
-       struct rte_cryptodev_config conf = {rte_socket_id(), 1};
+       struct rte_cryptodev_config conf = {rte_socket_id(), 1, 0};
        struct rte_cryptodev_qp_conf qp_conf = {128, NULL, NULL};
        uint32_t sess_sz = rte_cryptodev_sym_get_private_session_size(
                        env.dev_id);
@@ -649,16 +649,24 @@ prepare_aes_xform(struct rte_crypto_sym_xform *xform)
 
        xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
 
-       cipher_xform->algo = RTE_CRYPTO_CIPHER_AES_CBC;
+       if (info.interim_info.aes_data.cipher_algo == RTE_CRYPTO_CIPHER_AES_CBC)
+               cipher_xform->algo = RTE_CRYPTO_CIPHER_AES_CBC;
+       else
+               cipher_xform->algo = RTE_CRYPTO_CIPHER_AES_ECB;
+
        cipher_xform->op = (info.op == FIPS_TEST_ENC_AUTH_GEN) ?
                        RTE_CRYPTO_CIPHER_OP_ENCRYPT :
                        RTE_CRYPTO_CIPHER_OP_DECRYPT;
        cipher_xform->key.data = vec.cipher_auth.key.val;
        cipher_xform->key.length = vec.cipher_auth.key.len;
-       cipher_xform->iv.length = vec.iv.len;
-       cipher_xform->iv.offset = IV_OFF;
-
-       cap_idx.algo.cipher = RTE_CRYPTO_CIPHER_AES_CBC;
+       if (cipher_xform->algo == RTE_CRYPTO_CIPHER_AES_CBC) {
+               cipher_xform->iv.length = vec.iv.len;
+               cipher_xform->iv.offset = IV_OFF;
+       } else {
+               cipher_xform->iv.length = 0;
+               cipher_xform->iv.offset = 0;
+       }
+       cap_idx.algo.cipher = cipher_xform->algo;
        cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
 
        cap = rte_cryptodev_sym_capability_get(env.dev_id, &cap_idx);
@@ -689,16 +697,24 @@ prepare_tdes_xform(struct rte_crypto_sym_xform *xform)
 
        xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
 
-       cipher_xform->algo = RTE_CRYPTO_CIPHER_3DES_CBC;
+       if (info.interim_info.tdes_data.test_mode == TDES_MODE_CBC)
+               cipher_xform->algo = RTE_CRYPTO_CIPHER_3DES_CBC;
+       else
+               cipher_xform->algo = RTE_CRYPTO_CIPHER_3DES_ECB;
        cipher_xform->op = (info.op == FIPS_TEST_ENC_AUTH_GEN) ?
                        RTE_CRYPTO_CIPHER_OP_ENCRYPT :
                        RTE_CRYPTO_CIPHER_OP_DECRYPT;
        cipher_xform->key.data = vec.cipher_auth.key.val;
        cipher_xform->key.length = vec.cipher_auth.key.len;
-       cipher_xform->iv.length = vec.iv.len;
-       cipher_xform->iv.offset = IV_OFF;
 
-       cap_idx.algo.cipher = RTE_CRYPTO_CIPHER_3DES_CBC;
+       if (cipher_xform->algo == RTE_CRYPTO_CIPHER_3DES_CBC) {
+               cipher_xform->iv.length = vec.iv.len;
+               cipher_xform->iv.offset = IV_OFF;
+       } else {
+               cipher_xform->iv.length = 0;
+               cipher_xform->iv.offset = 0;
+       }
+       cap_idx.algo.cipher = cipher_xform->algo;
        cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
 
        cap = rte_cryptodev_sym_capability_get(env.dev_id, &cap_idx);
@@ -949,19 +965,20 @@ fips_run_test(void)
        if (ret < 0) {
                RTE_LOG(ERR, USER1, "Error %i: Init session\n",
                                ret);
-               return ret;
+               goto exit;
        }
 
        ret = test_ops.prepare_op();
        if (ret < 0) {
                RTE_LOG(ERR, USER1, "Error %i: Prepare op\n",
                                ret);
-               return ret;
+               goto exit;
        }
 
        if (rte_cryptodev_enqueue_burst(env.dev_id, 0, &env.op, 1) < 1) {
                RTE_LOG(ERR, USER1, "Error: Failed enqueue\n");
-               return ret;
+               ret = -1;
+               goto exit;
        }
 
        do {
@@ -973,6 +990,7 @@ fips_run_test(void)
 
        vec.status = env.op->status;
 
+exit:
        rte_cryptodev_sym_session_clear(env.dev_id, env.sess);
        rte_cryptodev_sym_session_free(env.sess);
        env.sess = NULL;
@@ -1035,6 +1053,7 @@ fips_mct_tdes_test(void)
        uint8_t prev_in[TDES_BLOCK_SIZE] = {0};
        uint32_t i, j, k;
        int ret;
+       int test_mode = info.interim_info.tdes_data.test_mode;
 
        for (i = 0; i < TDES_EXTERN_ITER; i++) {
                if (i != 0)
@@ -1049,7 +1068,6 @@ fips_mct_tdes_test(void)
                                        fprintf(info.fp_wr, "Bypass\n");
                                        return 0;
                                }
-
                                return ret;
                        }
 
@@ -1062,25 +1080,50 @@ fips_mct_tdes_test(void)
                                memcpy(prev_out, val.val, TDES_BLOCK_SIZE);
 
                                if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
-                                       memcpy(vec.pt.val, vec.iv.val,
-                                                       TDES_BLOCK_SIZE);
-                                       memcpy(vec.iv.val, val.val,
-                                                       TDES_BLOCK_SIZE);
+                                       if (test_mode == TDES_MODE_ECB) {
+                                               memcpy(vec.pt.val, val.val,
+                                                          TDES_BLOCK_SIZE);
+                                       } else {
+                                               memcpy(vec.pt.val, vec.iv.val,
+                                                          TDES_BLOCK_SIZE);
+                                               memcpy(vec.iv.val, val.val,
+                                                          TDES_BLOCK_SIZE);
+                                       }
+
                                } else {
-                                       memcpy(vec.iv.val, vec.ct.val,
-                                                       TDES_BLOCK_SIZE);
-                                       memcpy(vec.ct.val, val.val,
-                                                       TDES_BLOCK_SIZE);
+                                       if (test_mode == TDES_MODE_ECB) {
+                                               memcpy(vec.ct.val, val.val,
+                                                          TDES_BLOCK_SIZE);
+                                       } else {
+                                               memcpy(vec.iv.val, vec.ct.val,
+                                                          TDES_BLOCK_SIZE);
+                                               memcpy(vec.ct.val, val.val,
+                                                          TDES_BLOCK_SIZE);
+                                       }
                                }
                                continue;
                        }
 
                        if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
-                               memcpy(vec.iv.val, val.val, TDES_BLOCK_SIZE);
-                               memcpy(vec.pt.val, prev_out, TDES_BLOCK_SIZE);
+                               if (test_mode == TDES_MODE_ECB) {
+                                       memcpy(vec.pt.val, val.val,
+                                                  TDES_BLOCK_SIZE);
+                               } else {
+                                       memcpy(vec.iv.val, val.val,
+                                                  TDES_BLOCK_SIZE);
+                                       memcpy(vec.pt.val, prev_out,
+                                                  TDES_BLOCK_SIZE);
+                               }
                        } else {
-                               memcpy(vec.iv.val, vec.ct.val, TDES_BLOCK_SIZE);
-                               memcpy(vec.ct.val, val.val, TDES_BLOCK_SIZE);
+                               if (test_mode == TDES_MODE_ECB) {
+                                       memcpy(vec.ct.val, val.val,
+                                                  TDES_BLOCK_SIZE);
+                               } else {
+                                       memcpy(vec.iv.val, vec.ct.val,
+                                                  TDES_BLOCK_SIZE);
+                                       memcpy(vec.ct.val, val.val,
+                                                  TDES_BLOCK_SIZE);
+                               }
                        }
 
                        if (j == TDES_INTERN_ITER - 1)
@@ -1139,17 +1182,99 @@ fips_mct_tdes_test(void)
                                        val_key.val[k] : (val_key.val[k] ^ 0x1);
 
                if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
-                       memcpy(vec.iv.val, val.val, TDES_BLOCK_SIZE);
-                       memcpy(vec.pt.val, prev_out, TDES_BLOCK_SIZE);
+                       if (test_mode == TDES_MODE_ECB) {
+                               memcpy(vec.pt.val, val.val, TDES_BLOCK_SIZE);
+                       } else {
+                               memcpy(vec.iv.val, val.val, TDES_BLOCK_SIZE);
+                               memcpy(vec.pt.val, prev_out, TDES_BLOCK_SIZE);
+                       }
                } else {
-                       memcpy(vec.iv.val, prev_out, TDES_BLOCK_SIZE);
-                       memcpy(vec.ct.val, val.val, TDES_BLOCK_SIZE);
+                       if (test_mode == TDES_MODE_ECB) {
+                               memcpy(vec.ct.val, val.val, TDES_BLOCK_SIZE);
+                       } else {
+                               memcpy(vec.iv.val, prev_out, TDES_BLOCK_SIZE);
+                               memcpy(vec.ct.val, val.val, TDES_BLOCK_SIZE);
+                       }
                }
        }
 
        return 0;
 }
 
+static int
+fips_mct_aes_ecb_test(void)
+{
+#define AES_BLOCK_SIZE 16
+#define AES_EXTERN_ITER        100
+#define AES_INTERN_ITER        1000
+       struct fips_val val, val_key;
+       uint8_t prev_out[AES_BLOCK_SIZE] = {0};
+       uint32_t i, j, k;
+       int ret;
+
+       for (i = 0; i < AES_EXTERN_ITER; i++) {
+               if (i != 0)
+                       update_info_vec(i);
+
+               fips_test_write_one_case();
+
+               for (j = 0; j < AES_INTERN_ITER; j++) {
+                       ret = fips_run_test();
+                       if (ret < 0) {
+                               if (ret == -EPERM) {
+                                       fprintf(info.fp_wr, "Bypass\n");
+                                       return 0;
+                               }
+
+                               return ret;
+                       }
+
+                       get_writeback_data(&val);
+
+                       if (info.op == FIPS_TEST_ENC_AUTH_GEN)
+                               memcpy(vec.pt.val, val.val, AES_BLOCK_SIZE);
+                       else
+                               memcpy(vec.ct.val, val.val, AES_BLOCK_SIZE);
+
+                       if (j == AES_INTERN_ITER - 1)
+                               continue;
+
+                       memcpy(prev_out, val.val, AES_BLOCK_SIZE);
+               }
+
+               info.parse_writeback(&val);
+               fprintf(info.fp_wr, "\n");
+
+               if (i == AES_EXTERN_ITER - 1)
+                       continue;
+
+               /** update key */
+               memcpy(&val_key, &vec.cipher_auth.key, sizeof(val_key));
+               for (k = 0; k < vec.cipher_auth.key.len; k++) {
+                       switch (vec.cipher_auth.key.len) {
+                       case 16:
+                               val_key.val[k] ^= val.val[k];
+                               break;
+                       case 24:
+                               if (k < 8)
+                                       val_key.val[k] ^= prev_out[k + 8];
+                               else
+                                       val_key.val[k] ^= val.val[k - 8];
+                               break;
+                       case 32:
+                               if (k < 16)
+                                       val_key.val[k] ^= prev_out[k];
+                               else
+                                       val_key.val[k] ^= val.val[k - 16];
+                               break;
+                       default:
+                               return -1;
+                       }
+               }
+       }
+
+       return 0;
+}
 static int
 fips_mct_aes_test(void)
 {
@@ -1162,6 +1287,9 @@ fips_mct_aes_test(void)
        uint32_t i, j, k;
        int ret;
 
+       if (info.interim_info.aes_data.cipher_algo == RTE_CRYPTO_CIPHER_AES_ECB)
+               return fips_mct_aes_ecb_test();
+
        for (i = 0; i < AES_EXTERN_ITER; i++) {
                if (i != 0)
                        update_info_vec(i);
@@ -1385,6 +1513,17 @@ init_test_ops(void)
                        test_ops.test = fips_generic_test;
                break;
        default:
+               if (strstr(info.file_name, "TECB") ||
+                               strstr(info.file_name, "TCBC")) {
+                       info.algo = FIPS_TEST_ALGO_TDES;
+                       test_ops.prepare_op = prepare_cipher_op;
+                       test_ops.prepare_xform  = prepare_tdes_xform;
+                       if (info.interim_info.tdes_data.test_type == TDES_MCT)
+                               test_ops.test = fips_mct_tdes_test;
+                       else
+                               test_ops.test = fips_generic_test;
+                       break;
+               }
                return -1;
        }