046536cc9c853abb0038365486c44bef6bed03b1
[dpdk.git] / app / test / test_cryptodev_security_ipsec.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #include <rte_common.h>
6 #include <rte_cryptodev.h>
7 #include <rte_esp.h>
8 #include <rte_ip.h>
9 #include <rte_security.h>
10 #include <rte_udp.h>
11
12 #include "test.h"
13 #include "test_cryptodev_security_ipsec.h"
14
15 #define IV_LEN_MAX 16
16
17 extern struct ipsec_test_data pkt_aes_256_gcm;
18
19 int
20 test_ipsec_sec_caps_verify(struct rte_security_ipsec_xform *ipsec_xform,
21                            const struct rte_security_capability *sec_cap,
22                            bool silent)
23 {
24         /* Verify security capabilities */
25
26         if (ipsec_xform->options.esn == 1 && sec_cap->ipsec.options.esn == 0) {
27                 if (!silent)
28                         RTE_LOG(INFO, USER1, "ESN is not supported\n");
29                 return -ENOTSUP;
30         }
31
32         if (ipsec_xform->options.udp_encap == 1 &&
33             sec_cap->ipsec.options.udp_encap == 0) {
34                 if (!silent)
35                         RTE_LOG(INFO, USER1, "UDP encapsulation is not supported\n");
36                 return -ENOTSUP;
37         }
38
39         if (ipsec_xform->options.copy_dscp == 1 &&
40             sec_cap->ipsec.options.copy_dscp == 0) {
41                 if (!silent)
42                         RTE_LOG(INFO, USER1, "Copy DSCP is not supported\n");
43                 return -ENOTSUP;
44         }
45
46         if (ipsec_xform->options.copy_flabel == 1 &&
47             sec_cap->ipsec.options.copy_flabel == 0) {
48                 if (!silent)
49                         RTE_LOG(INFO, USER1, "Copy Flow Label is not supported\n");
50                 return -ENOTSUP;
51         }
52
53         if (ipsec_xform->options.copy_df == 1 &&
54             sec_cap->ipsec.options.copy_df == 0) {
55                 if (!silent)
56                         RTE_LOG(INFO, USER1, "Copy DP bit is not supported\n");
57                 return -ENOTSUP;
58         }
59
60         if (ipsec_xform->options.dec_ttl == 1 &&
61             sec_cap->ipsec.options.dec_ttl == 0) {
62                 if (!silent)
63                         RTE_LOG(INFO, USER1, "Decrement TTL is not supported\n");
64                 return -ENOTSUP;
65         }
66
67         if (ipsec_xform->options.ecn == 1 && sec_cap->ipsec.options.ecn == 0) {
68                 if (!silent)
69                         RTE_LOG(INFO, USER1, "ECN is not supported\n");
70                 return -ENOTSUP;
71         }
72
73         if (ipsec_xform->options.stats == 1 &&
74             sec_cap->ipsec.options.stats == 0) {
75                 if (!silent)
76                         RTE_LOG(INFO, USER1, "Stats is not supported\n");
77                 return -ENOTSUP;
78         }
79
80         if ((ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) &&
81             (ipsec_xform->options.iv_gen_disable == 1) &&
82             (sec_cap->ipsec.options.iv_gen_disable != 1)) {
83                 if (!silent)
84                         RTE_LOG(INFO, USER1,
85                                 "Application provided IV is not supported\n");
86                 return -ENOTSUP;
87         }
88
89         return 0;
90 }
91
92 int
93 test_ipsec_crypto_caps_aead_verify(
94                 const struct rte_security_capability *sec_cap,
95                 struct rte_crypto_sym_xform *aead)
96 {
97         const struct rte_cryptodev_symmetric_capability *sym_cap;
98         const struct rte_cryptodev_capabilities *crypto_cap;
99         int j = 0;
100
101         while ((crypto_cap = &sec_cap->crypto_capabilities[j++])->op !=
102                         RTE_CRYPTO_OP_TYPE_UNDEFINED) {
103                 if (crypto_cap->op == RTE_CRYPTO_OP_TYPE_SYMMETRIC &&
104                                 crypto_cap->sym.xform_type == aead->type &&
105                                 crypto_cap->sym.aead.algo == aead->aead.algo) {
106                         sym_cap = &crypto_cap->sym;
107                         if (rte_cryptodev_sym_capability_check_aead(sym_cap,
108                                         aead->aead.key.length,
109                                         aead->aead.digest_length,
110                                         aead->aead.aad_length,
111                                         aead->aead.iv.length) == 0)
112                                 return 0;
113                 }
114         }
115
116         return -ENOTSUP;
117 }
118
119 void
120 test_ipsec_td_in_from_out(const struct ipsec_test_data *td_out,
121                           struct ipsec_test_data *td_in)
122 {
123         memcpy(td_in, td_out, sizeof(*td_in));
124
125         /* Populate output text of td_in with input text of td_out */
126         memcpy(td_in->output_text.data, td_out->input_text.data,
127                td_out->input_text.len);
128         td_in->output_text.len = td_out->input_text.len;
129
130         /* Populate input text of td_in with output text of td_out */
131         memcpy(td_in->input_text.data, td_out->output_text.data,
132                td_out->output_text.len);
133         td_in->input_text.len = td_out->output_text.len;
134
135         td_in->ipsec_xform.direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS;
136
137         if (td_in->aead) {
138                 td_in->xform.aead.aead.op = RTE_CRYPTO_AEAD_OP_DECRYPT;
139         } else {
140                 td_in->xform.chain.auth.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
141                 td_in->xform.chain.cipher.cipher.op =
142                                 RTE_CRYPTO_CIPHER_OP_DECRYPT;
143         }
144 }
145
146 void
147 test_ipsec_td_prepare(const struct crypto_param *param1,
148                       const struct crypto_param *param2,
149                       const struct ipsec_test_flags *flags,
150                       struct ipsec_test_data *td_array,
151                       int nb_td)
152
153 {
154         struct ipsec_test_data *td;
155         int i;
156
157         memset(td_array, 0, nb_td * sizeof(*td));
158
159         for (i = 0; i < nb_td; i++) {
160                 td = &td_array[i];
161                 /* Copy template for packet & key fields */
162                 memcpy(td, &pkt_aes_256_gcm, sizeof(*td));
163
164                 /* Override fields based on param */
165
166                 if (param1->type == RTE_CRYPTO_SYM_XFORM_AEAD)
167                         td->aead = true;
168                 else
169                         td->aead = false;
170
171                 td->xform.aead.aead.algo = param1->alg.aead;
172                 td->xform.aead.aead.key.length = param1->key_length;
173
174                 if (flags->iv_gen)
175                         td->ipsec_xform.options.iv_gen_disable = 0;
176
177                 if (flags->sa_expiry_pkts_soft)
178                         td->ipsec_xform.life.packets_soft_limit =
179                                         IPSEC_TEST_PACKETS_MAX - 1;
180         }
181
182         RTE_SET_USED(param2);
183 }
184
185 void
186 test_ipsec_td_update(struct ipsec_test_data td_inb[],
187                      const struct ipsec_test_data td_outb[],
188                      int nb_td,
189                      const struct ipsec_test_flags *flags)
190 {
191         int i;
192
193         for (i = 0; i < nb_td; i++) {
194                 memcpy(td_inb[i].output_text.data, td_outb[i].input_text.data,
195                        td_outb[i].input_text.len);
196                 td_inb[i].output_text.len = td_outb->input_text.len;
197
198                 if (flags->icv_corrupt) {
199                         int icv_pos = td_inb[i].input_text.len - 4;
200                         td_inb[i].input_text.data[icv_pos] += 1;
201                 }
202
203                 if (flags->sa_expiry_pkts_hard)
204                         td_inb[i].ipsec_xform.life.packets_hard_limit =
205                                         IPSEC_TEST_PACKETS_MAX - 1;
206
207                 if (flags->udp_encap)
208                         td_inb[i].ipsec_xform.options.udp_encap = 1;
209
210                 /* Clear outbound specific flags */
211                 td_inb[i].ipsec_xform.options.iv_gen_disable = 0;
212         }
213 }
214
215 void
216 test_ipsec_display_alg(const struct crypto_param *param1,
217                        const struct crypto_param *param2)
218 {
219         if (param1->type == RTE_CRYPTO_SYM_XFORM_AEAD)
220                 printf("\t%s [%d]\n",
221                        rte_crypto_aead_algorithm_strings[param1->alg.aead],
222                        param1->key_length);
223
224         RTE_SET_USED(param2);
225 }
226
227 static int
228 test_ipsec_tunnel_hdr_len_get(const struct ipsec_test_data *td)
229 {
230         int len = 0;
231
232         if (td->ipsec_xform.direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
233                 if (td->ipsec_xform.mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
234                         if (td->ipsec_xform.tunnel.type ==
235                                         RTE_SECURITY_IPSEC_TUNNEL_IPV4)
236                                 len += sizeof(struct rte_ipv4_hdr);
237                         else
238                                 len += sizeof(struct rte_ipv6_hdr);
239                 }
240         }
241
242         return len;
243 }
244
245 static int
246 test_ipsec_iv_verify_push(struct rte_mbuf *m, const struct ipsec_test_data *td)
247 {
248         static uint8_t iv_queue[IV_LEN_MAX * IPSEC_TEST_PACKETS_MAX];
249         uint8_t *iv_tmp, *output_text = rte_pktmbuf_mtod(m, uint8_t *);
250         int i, iv_pos, iv_len;
251         static int index;
252
253         if (td->aead)
254                 iv_len = td->xform.aead.aead.iv.length - td->salt.len;
255         else
256                 iv_len = td->xform.chain.cipher.cipher.iv.length;
257
258         iv_pos = test_ipsec_tunnel_hdr_len_get(td) + sizeof(struct rte_esp_hdr);
259         output_text += iv_pos;
260
261         TEST_ASSERT(iv_len <= IV_LEN_MAX, "IV length greater than supported");
262
263         /* Compare against previous values */
264         for (i = 0; i < index; i++) {
265                 iv_tmp = &iv_queue[i * IV_LEN_MAX];
266
267                 if (memcmp(output_text, iv_tmp, iv_len) == 0) {
268                         printf("IV repeated");
269                         return TEST_FAILED;
270                 }
271         }
272
273         /* Save IV for future comparisons */
274
275         iv_tmp = &iv_queue[index * IV_LEN_MAX];
276         memcpy(iv_tmp, output_text, iv_len);
277         index++;
278
279         if (index == IPSEC_TEST_PACKETS_MAX)
280                 index = 0;
281
282         return TEST_SUCCESS;
283 }
284
285 static int
286 test_ipsec_td_verify(struct rte_mbuf *m, const struct ipsec_test_data *td,
287                      bool silent, const struct ipsec_test_flags *flags)
288 {
289         uint8_t *output_text = rte_pktmbuf_mtod(m, uint8_t *);
290         uint32_t skip, len = rte_pktmbuf_pkt_len(m);
291
292         /* For tests with status as error for test success, skip verification */
293         if (td->ipsec_xform.direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS &&
294             (flags->icv_corrupt ||
295              flags->sa_expiry_pkts_hard))
296                 return TEST_SUCCESS;
297
298         if (td->ipsec_xform.direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS &&
299            flags->udp_encap) {
300                 const struct rte_ipv4_hdr *iph4;
301                 const struct rte_ipv6_hdr *iph6;
302
303                 if (td->ipsec_xform.tunnel.type ==
304                                 RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
305                         iph4 = (const struct rte_ipv4_hdr *)output_text;
306                         if (iph4->next_proto_id != IPPROTO_UDP) {
307                                 printf("UDP header is not found\n");
308                                 return TEST_FAILED;
309                         }
310                 } else {
311                         iph6 = (const struct rte_ipv6_hdr *)output_text;
312                         if (iph6->proto != IPPROTO_UDP) {
313                                 printf("UDP header is not found\n");
314                                 return TEST_FAILED;
315                         }
316                 }
317
318                 len -= sizeof(struct rte_udp_hdr);
319                 output_text += sizeof(struct rte_udp_hdr);
320         }
321
322         if (len != td->output_text.len) {
323                 printf("Output length (%d) not matching with expected (%d)\n",
324                         len, td->output_text.len);
325                 return TEST_FAILED;
326         }
327
328         skip = test_ipsec_tunnel_hdr_len_get(td);
329
330         len -= skip;
331         output_text += skip;
332
333         if (memcmp(output_text, td->output_text.data + skip, len)) {
334                 if (silent)
335                         return TEST_FAILED;
336
337                 printf("TestCase %s line %d: %s\n", __func__, __LINE__,
338                         "output text not as expected\n");
339
340                 rte_hexdump(stdout, "expected", td->output_text.data + skip,
341                             len);
342                 rte_hexdump(stdout, "actual", output_text, len);
343                 return TEST_FAILED;
344         }
345
346         return TEST_SUCCESS;
347 }
348
349 static int
350 test_ipsec_res_d_prepare(struct rte_mbuf *m, const struct ipsec_test_data *td,
351                    struct ipsec_test_data *res_d)
352 {
353         uint8_t *output_text = rte_pktmbuf_mtod(m, uint8_t *);
354         uint32_t len = rte_pktmbuf_pkt_len(m);
355
356         memcpy(res_d, td, sizeof(*res_d));
357         memcpy(res_d->input_text.data, output_text, len);
358         res_d->input_text.len = len;
359
360         res_d->ipsec_xform.direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS;
361         if (res_d->aead) {
362                 res_d->xform.aead.aead.op = RTE_CRYPTO_AEAD_OP_DECRYPT;
363         } else {
364                 printf("Only AEAD supported\n");
365                 return TEST_SKIPPED;
366         }
367
368         return TEST_SUCCESS;
369 }
370
371 int
372 test_ipsec_post_process(struct rte_mbuf *m, const struct ipsec_test_data *td,
373                         struct ipsec_test_data *res_d, bool silent,
374                         const struct ipsec_test_flags *flags)
375 {
376         int ret;
377
378         if (flags->iv_gen &&
379             td->ipsec_xform.direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
380                 ret = test_ipsec_iv_verify_push(m, td);
381                 if (ret != TEST_SUCCESS)
382                         return ret;
383         }
384
385         /*
386          * In case of known vector tests & all inbound tests, res_d provided
387          * would be NULL and output data need to be validated against expected.
388          * For inbound, output_text would be plain packet and for outbound
389          * output_text would IPsec packet. Validate by comparing against
390          * known vectors.
391          *
392          * In case of combined mode tests, the output_text from outbound
393          * operation (ie, IPsec packet) would need to be inbound processed to
394          * obtain the plain text. Copy output_text to result data, 'res_d', so
395          * that inbound processing can be done.
396          */
397
398         if (res_d == NULL)
399                 return test_ipsec_td_verify(m, td, silent, flags);
400         else
401                 return test_ipsec_res_d_prepare(m, td, res_d);
402 }
403
404 int
405 test_ipsec_status_check(struct rte_crypto_op *op,
406                         const struct ipsec_test_flags *flags,
407                         enum rte_security_ipsec_sa_direction dir,
408                         int pkt_num)
409 {
410         int ret = TEST_SUCCESS;
411
412         if (dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS &&
413             flags->sa_expiry_pkts_hard &&
414             pkt_num == IPSEC_TEST_PACKETS_MAX) {
415                 if (op->status != RTE_CRYPTO_OP_STATUS_ERROR) {
416                         printf("SA hard expiry (pkts) test failed\n");
417                         return TEST_FAILED;
418                 } else {
419                         return TEST_SUCCESS;
420                 }
421         }
422
423         if (dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS && flags->icv_corrupt) {
424                 if (op->status != RTE_CRYPTO_OP_STATUS_ERROR) {
425                         printf("ICV corruption test case failed\n");
426                         ret = TEST_FAILED;
427                 }
428         } else {
429                 if (op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
430                         printf("Security op processing failed [pkt_num: %d]\n",
431                                pkt_num);
432                         ret = TEST_FAILED;
433                 }
434         }
435
436         if (flags->sa_expiry_pkts_soft && pkt_num == IPSEC_TEST_PACKETS_MAX) {
437                 if (!(op->aux_flags &
438                       RTE_CRYPTO_OP_AUX_FLAGS_IPSEC_SOFT_EXPIRY)) {
439                         printf("SA soft expiry (pkts) test failed\n");
440                         ret = TEST_FAILED;
441                 }
442         }
443
444         return ret;
445 }