1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2021 Marvell.
5 #include <rte_common.h>
6 #include <rte_cryptodev.h>
9 #include <rte_security.h>
12 #include "test_cryptodev_security_ipsec.h"
16 extern struct ipsec_test_data pkt_aes_256_gcm;
19 test_ipsec_sec_caps_verify(struct rte_security_ipsec_xform *ipsec_xform,
20 const struct rte_security_capability *sec_cap,
23 /* Verify security capabilities */
25 if (ipsec_xform->options.esn == 1 && sec_cap->ipsec.options.esn == 0) {
27 RTE_LOG(INFO, USER1, "ESN is not supported\n");
31 if (ipsec_xform->options.udp_encap == 1 &&
32 sec_cap->ipsec.options.udp_encap == 0) {
34 RTE_LOG(INFO, USER1, "UDP encapsulation is not supported\n");
38 if (ipsec_xform->options.copy_dscp == 1 &&
39 sec_cap->ipsec.options.copy_dscp == 0) {
41 RTE_LOG(INFO, USER1, "Copy DSCP is not supported\n");
45 if (ipsec_xform->options.copy_flabel == 1 &&
46 sec_cap->ipsec.options.copy_flabel == 0) {
48 RTE_LOG(INFO, USER1, "Copy Flow Label is not supported\n");
52 if (ipsec_xform->options.copy_df == 1 &&
53 sec_cap->ipsec.options.copy_df == 0) {
55 RTE_LOG(INFO, USER1, "Copy DP bit is not supported\n");
59 if (ipsec_xform->options.dec_ttl == 1 &&
60 sec_cap->ipsec.options.dec_ttl == 0) {
62 RTE_LOG(INFO, USER1, "Decrement TTL is not supported\n");
66 if (ipsec_xform->options.ecn == 1 && sec_cap->ipsec.options.ecn == 0) {
68 RTE_LOG(INFO, USER1, "ECN is not supported\n");
72 if (ipsec_xform->options.stats == 1 &&
73 sec_cap->ipsec.options.stats == 0) {
75 RTE_LOG(INFO, USER1, "Stats is not supported\n");
83 test_ipsec_crypto_caps_aead_verify(
84 const struct rte_security_capability *sec_cap,
85 struct rte_crypto_sym_xform *aead)
87 const struct rte_cryptodev_symmetric_capability *sym_cap;
88 const struct rte_cryptodev_capabilities *crypto_cap;
91 while ((crypto_cap = &sec_cap->crypto_capabilities[j++])->op !=
92 RTE_CRYPTO_OP_TYPE_UNDEFINED) {
93 if (crypto_cap->op == RTE_CRYPTO_OP_TYPE_SYMMETRIC &&
94 crypto_cap->sym.xform_type == aead->type &&
95 crypto_cap->sym.aead.algo == aead->aead.algo) {
96 sym_cap = &crypto_cap->sym;
97 if (rte_cryptodev_sym_capability_check_aead(sym_cap,
98 aead->aead.key.length,
99 aead->aead.digest_length,
100 aead->aead.aad_length,
101 aead->aead.iv.length) == 0)
110 test_ipsec_td_in_from_out(const struct ipsec_test_data *td_out,
111 struct ipsec_test_data *td_in)
113 memcpy(td_in, td_out, sizeof(*td_in));
115 /* Populate output text of td_in with input text of td_out */
116 memcpy(td_in->output_text.data, td_out->input_text.data,
117 td_out->input_text.len);
118 td_in->output_text.len = td_out->input_text.len;
120 /* Populate input text of td_in with output text of td_out */
121 memcpy(td_in->input_text.data, td_out->output_text.data,
122 td_out->output_text.len);
123 td_in->input_text.len = td_out->output_text.len;
125 td_in->ipsec_xform.direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS;
128 td_in->xform.aead.aead.op = RTE_CRYPTO_AEAD_OP_DECRYPT;
130 td_in->xform.chain.auth.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
131 td_in->xform.chain.cipher.cipher.op =
132 RTE_CRYPTO_CIPHER_OP_DECRYPT;
137 test_ipsec_td_prepare(const struct crypto_param *param1,
138 const struct crypto_param *param2,
139 const struct ipsec_test_flags *flags,
140 struct ipsec_test_data *td_array,
144 struct ipsec_test_data *td;
147 memset(td_array, 0, nb_td * sizeof(*td));
149 for (i = 0; i < nb_td; i++) {
151 /* Copy template for packet & key fields */
152 memcpy(td, &pkt_aes_256_gcm, sizeof(*td));
154 /* Override fields based on param */
156 if (param1->type == RTE_CRYPTO_SYM_XFORM_AEAD)
161 td->xform.aead.aead.algo = param1->alg.aead;
162 td->xform.aead.aead.key.length = param1->key_length;
166 RTE_SET_USED(param2);
170 test_ipsec_td_update(struct ipsec_test_data td_inb[],
171 const struct ipsec_test_data td_outb[],
173 const struct ipsec_test_flags *flags)
177 for (i = 0; i < nb_td; i++) {
178 memcpy(td_inb[i].output_text.data, td_outb[i].input_text.data,
179 td_outb[i].input_text.len);
180 td_inb[i].output_text.len = td_outb->input_text.len;
182 if (flags->icv_corrupt) {
183 int icv_pos = td_inb[i].input_text.len - 4;
184 td_inb[i].input_text.data[icv_pos] += 1;
190 test_ipsec_display_alg(const struct crypto_param *param1,
191 const struct crypto_param *param2)
193 if (param1->type == RTE_CRYPTO_SYM_XFORM_AEAD)
194 printf("\t%s [%d]\n",
195 rte_crypto_aead_algorithm_strings[param1->alg.aead],
198 RTE_SET_USED(param2);
202 test_ipsec_tunnel_hdr_len_get(const struct ipsec_test_data *td)
206 if (td->ipsec_xform.direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
207 if (td->ipsec_xform.mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
208 if (td->ipsec_xform.tunnel.type ==
209 RTE_SECURITY_IPSEC_TUNNEL_IPV4)
210 len += sizeof(struct rte_ipv4_hdr);
212 len += sizeof(struct rte_ipv6_hdr);
220 test_ipsec_iv_verify_push(struct rte_mbuf *m, const struct ipsec_test_data *td)
222 static uint8_t iv_queue[IV_LEN_MAX * IPSEC_TEST_PACKETS_MAX];
223 uint8_t *iv_tmp, *output_text = rte_pktmbuf_mtod(m, uint8_t *);
224 int i, iv_pos, iv_len;
228 iv_len = td->xform.aead.aead.iv.length - td->salt.len;
230 iv_len = td->xform.chain.cipher.cipher.iv.length;
232 iv_pos = test_ipsec_tunnel_hdr_len_get(td) + sizeof(struct rte_esp_hdr);
233 output_text += iv_pos;
235 TEST_ASSERT(iv_len <= IV_LEN_MAX, "IV length greater than supported");
237 /* Compare against previous values */
238 for (i = 0; i < index; i++) {
239 iv_tmp = &iv_queue[i * IV_LEN_MAX];
241 if (memcmp(output_text, iv_tmp, iv_len) == 0) {
242 printf("IV repeated");
247 /* Save IV for future comparisons */
249 iv_tmp = &iv_queue[index * IV_LEN_MAX];
250 memcpy(iv_tmp, output_text, iv_len);
253 if (index == IPSEC_TEST_PACKETS_MAX)
260 test_ipsec_td_verify(struct rte_mbuf *m, const struct ipsec_test_data *td,
261 bool silent, const struct ipsec_test_flags *flags)
263 uint8_t *output_text = rte_pktmbuf_mtod(m, uint8_t *);
264 uint32_t skip, len = rte_pktmbuf_pkt_len(m);
266 /* For negative tests, no need to do verification */
267 if (flags->icv_corrupt &&
268 td->ipsec_xform.direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
271 if (len != td->output_text.len) {
272 printf("Output length (%d) not matching with expected (%d)\n",
273 len, td->output_text.len);
277 skip = test_ipsec_tunnel_hdr_len_get(td);
282 if (memcmp(output_text, td->output_text.data + skip, len)) {
286 printf("TestCase %s line %d: %s\n", __func__, __LINE__,
287 "output text not as expected\n");
289 rte_hexdump(stdout, "expected", td->output_text.data + skip,
291 rte_hexdump(stdout, "actual", output_text, len);
299 test_ipsec_res_d_prepare(struct rte_mbuf *m, const struct ipsec_test_data *td,
300 struct ipsec_test_data *res_d)
302 uint8_t *output_text = rte_pktmbuf_mtod(m, uint8_t *);
303 uint32_t len = rte_pktmbuf_pkt_len(m);
305 memcpy(res_d, td, sizeof(*res_d));
306 memcpy(res_d->input_text.data, output_text, len);
307 res_d->input_text.len = len;
309 res_d->ipsec_xform.direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS;
311 res_d->xform.aead.aead.op = RTE_CRYPTO_AEAD_OP_DECRYPT;
313 printf("Only AEAD supported\n");
321 test_ipsec_post_process(struct rte_mbuf *m, const struct ipsec_test_data *td,
322 struct ipsec_test_data *res_d, bool silent,
323 const struct ipsec_test_flags *flags)
328 td->ipsec_xform.direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
329 ret = test_ipsec_iv_verify_push(m, td);
330 if (ret != TEST_SUCCESS)
335 * In case of known vector tests & all inbound tests, res_d provided
336 * would be NULL and output data need to be validated against expected.
337 * For inbound, output_text would be plain packet and for outbound
338 * output_text would IPsec packet. Validate by comparing against
341 * In case of combined mode tests, the output_text from outbound
342 * operation (ie, IPsec packet) would need to be inbound processed to
343 * obtain the plain text. Copy output_text to result data, 'res_d', so
344 * that inbound processing can be done.
348 return test_ipsec_td_verify(m, td, silent, flags);
350 return test_ipsec_res_d_prepare(m, td, res_d);
354 test_ipsec_status_check(struct rte_crypto_op *op,
355 const struct ipsec_test_flags *flags,
356 enum rte_security_ipsec_sa_direction dir)
358 int ret = TEST_SUCCESS;
360 if (dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS && flags->icv_corrupt) {
361 if (op->status != RTE_CRYPTO_OP_STATUS_ERROR) {
362 printf("ICV corruption test case failed\n");
366 if (op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
367 printf("Security op processing failed\n");