File: test_cryptodev_security_tls_record.c

package info (click to toggle)
dpdk 25.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 127,892 kB
  • sloc: ansic: 2,358,479; python: 16,426; sh: 4,474; makefile: 1,713; awk: 70
file content (409 lines) | stat: -rw-r--r-- 12,536 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(C) 2023 Marvell.
 */

#include <rte_crypto.h>
#include <rte_dtls.h>
#include <rte_tls.h>

#include "test.h"
#include "test_cryptodev_security_tls_record.h"
#include "test_cryptodev_security_tls_record_test_vectors.h"
#include "test_security_proto.h"

int
test_tls_record_status_check(struct rte_crypto_op *op,
			     const struct tls_record_test_data *td)
{
	int ret = TEST_SUCCESS;

	if ((td->tls_record_xform.type == RTE_SECURITY_TLS_SESS_TYPE_READ) &&
	     td->ar_packet) {
		if (op->status != RTE_CRYPTO_OP_STATUS_ERROR) {
			printf("Anti replay test case failed\n");
			return TEST_FAILED;
		} else {
			return TEST_SUCCESS;
		}
	}

	if (op->status != RTE_CRYPTO_OP_STATUS_SUCCESS)
		ret = TEST_FAILED;

	return ret;
}

int
test_tls_record_sec_caps_verify(struct rte_security_tls_record_xform *tls_record_xform,
				const struct rte_security_capability *sec_cap, bool silent)
{
	/* Verify security capabilities */

	RTE_SET_USED(tls_record_xform);
	RTE_SET_USED(sec_cap);
	RTE_SET_USED(silent);

	return 0;
}

void
test_tls_record_td_read_from_write(const struct tls_record_test_data *td_out,
				   struct tls_record_test_data *td_in)
{
	memcpy(td_in, td_out, sizeof(*td_in));

	/* Populate output text of td_in with input text of td_out */
	memcpy(td_in->output_text.data, td_out->input_text.data, td_out->input_text.len);
	td_in->output_text.len = td_out->input_text.len;

	/* Populate input text of td_in with output text of td_out */
	memcpy(td_in->input_text.data, td_out->output_text.data, td_out->output_text.len);
	td_in->input_text.len = td_out->output_text.len;

	td_in->tls_record_xform.type = RTE_SECURITY_TLS_SESS_TYPE_READ;

	if (td_in->aead) {
		td_in->xform.aead.aead.op = RTE_CRYPTO_AEAD_OP_DECRYPT;
	} else {
		td_in->xform.chain.auth.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
		td_in->xform.chain.cipher.cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
	}
}

int
test_tls_record_td_prepare(const struct crypto_param *param1, const struct crypto_param *param2,
			   const struct tls_record_test_flags *flags,
			   struct tls_record_test_data *td_array,
			   int nb_td, unsigned int data_len)
{
	int i, min_padding, hdr_len, tls_pkt_size, mac_len = 0, exp_nonce_len = 0, roundup_len = 0;
	struct tls_record_test_data *td = NULL;

	if ((flags->tls_version == RTE_SECURITY_VERSION_TLS_1_3) &&
	    (param1->type != RTE_CRYPTO_SYM_XFORM_AEAD))
		return TEST_SKIPPED;

	memset(td_array, 0, nb_td * sizeof(*td));

	for (i = 0; i < nb_td; i++) {
		td = &td_array[i];

		/* Prepare fields based on param */

		if (param1->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
			/* Copy template for packet & key fields */
			switch (flags->tls_version) {
			case RTE_SECURITY_VERSION_TLS_1_2:
				memcpy(td, &tls_test_data_aes_128_gcm_v1, sizeof(*td));
				break;
			case RTE_SECURITY_VERSION_DTLS_1_2:
				memcpy(td, &dtls_test_data_aes_128_gcm, sizeof(*td));
				break;
			case RTE_SECURITY_VERSION_TLS_1_3:
				memcpy(td, &tls13_test_data_aes_128_gcm, sizeof(*td));
				break;
			}

			td->aead = true;
			td->xform.aead.aead.algo = param1->alg.aead;
			td->xform.aead.aead.key.length = param1->key_length;
			td->xform.aead.aead.digest_length = param1->digest_length;
		} else {
			/* Copy template for packet & key fields */
			if (flags->tls_version == RTE_SECURITY_VERSION_DTLS_1_2)
				memcpy(td, &dtls_test_data_aes_128_cbc_sha1_hmac, sizeof(*td));
			else
				memcpy(td, &tls_test_data_aes_128_cbc_sha1_hmac, sizeof(*td));

			td->aead = false;
			td->xform.chain.cipher.cipher.algo = param1->alg.cipher;
			td->xform.chain.cipher.cipher.key.length = param1->key_length;
			td->xform.chain.cipher.cipher.iv.length = param1->iv_length;
			td->xform.chain.auth.auth.algo = param2->alg.auth;
			td->xform.chain.auth.auth.key.length = param2->key_length;
			td->xform.chain.auth.auth.digest_length = param2->digest_length;
		}

		if (flags->data_walkthrough || flags->zero_len) {
			test_sec_proto_pattern_set(td->input_text.data, data_len);
			td->input_text.len = data_len;
		}

		if (flags->content_type == TLS_RECORD_TEST_CONTENT_TYPE_CUSTOM)
			td->app_type = RTE_TLS_TYPE_MAX;
		else if (flags->content_type == TLS_RECORD_TEST_CONTENT_TYPE_HANDSHAKE)
			td->app_type = RTE_TLS_TYPE_HANDSHAKE;

		tls_pkt_size = td->input_text.len;

		if (!td->aead) {
			mac_len = td->xform.chain.auth.auth.digest_length;
			min_padding = 1;
			switch (td->xform.chain.cipher.cipher.algo) {
			case RTE_CRYPTO_CIPHER_3DES_CBC:
				roundup_len = 8;
				exp_nonce_len = 8;
				break;
			case RTE_CRYPTO_CIPHER_AES_CBC:
				roundup_len = 16;
				exp_nonce_len = 16;
				break;
			default:
				roundup_len = 0;
				exp_nonce_len = 0;
				break;
			}
		} else {
			mac_len = td->xform.aead.aead.digest_length;
			min_padding = 0;
			roundup_len = 0;
			if (td->tls_record_xform.ver == RTE_SECURITY_VERSION_TLS_1_3)
				exp_nonce_len = 0;
			else
				exp_nonce_len = 8;
		}

		switch (td->tls_record_xform.ver) {
		case RTE_SECURITY_VERSION_TLS_1_2:
			hdr_len = sizeof(struct rte_tls_hdr);
			break;
		case RTE_SECURITY_VERSION_TLS_1_3:
			hdr_len = sizeof(struct rte_tls_hdr);
			/* Add 1 byte for content type in packet */
			tls_pkt_size += 1;
			break;
		case RTE_SECURITY_VERSION_DTLS_1_2:
			hdr_len = sizeof(struct rte_dtls_hdr);
			break;
		default:
			return TEST_SKIPPED;
		}

		tls_pkt_size += mac_len;

		/* Padding */
		tls_pkt_size += min_padding;

		if (roundup_len)
			tls_pkt_size = RTE_ALIGN_MUL_CEIL(tls_pkt_size, roundup_len);

		/* Explicit nonce */
		tls_pkt_size += exp_nonce_len;

		/* Add TLS header */
		tls_pkt_size += hdr_len;

		td->output_text.len = tls_pkt_size;

	}
	return TEST_SUCCESS;
}

void
test_tls_record_td_update(struct tls_record_test_data td_inb[],
			  const struct tls_record_test_data td_outb[], int nb_td,
			  const struct tls_record_test_flags *flags)
{
	int i;

	for (i = 0; i < nb_td; i++) {
		memcpy(td_inb[i].output_text.data, td_outb[i].input_text.data,
		       td_outb[i].input_text.len);
		td_inb[i].output_text.len = td_outb->input_text.len;

		/* Corrupt the content type in the TLS header of encrypted packet */
		if (flags->pkt_corruption)
			td_inb[i].input_text.data[0] = ~td_inb[i].input_text.data[0];

		/* Corrupt a byte in the last but one block */
		if (flags->padding_corruption) {
			int offset = td_inb[i].input_text.len - TLS_RECORD_PAD_CORRUPT_OFFSET;

			td_inb[i].input_text.data[offset] = ~td_inb[i].input_text.data[offset];
		}

		/* Clear outbound specific flags */
		td_inb[i].tls_record_xform.options.iv_gen_disable = 0;
	}

	RTE_SET_USED(flags);
}

static int
test_tls_record_td_verify(uint8_t *output_text, uint32_t len, const struct tls_record_test_data *td,
			 bool silent)
{
	if (len != td->output_text.len) {
		printf("Output length (%d) not matching with expected (%d)\n",
			len, td->output_text.len);
		return TEST_FAILED;
	}

	if (memcmp(output_text, td->output_text.data, len)) {
		if (silent)
			return TEST_FAILED;

		printf("[%s : %d] %s\n", __func__, __LINE__, "Output text not as expected\n");

		rte_hexdump(stdout, "expected", td->output_text.data, len);
		rte_hexdump(stdout, "actual", output_text, len);
		return TEST_FAILED;
	}

	return TEST_SUCCESS;
}

static int
test_tls_record_res_d_prepare(const uint8_t *output_text, uint32_t len,
			      const struct tls_record_test_data *td,
			      struct tls_record_test_data *res_d)
{
	memcpy(res_d, td, sizeof(*res_d));

	memcpy(&res_d->input_text.data, output_text, len);
	res_d->input_text.len = len;
	res_d->output_text.len = td->input_text.len;

	res_d->tls_record_xform.type = RTE_SECURITY_TLS_SESS_TYPE_READ;
	if (res_d->aead) {
		res_d->xform.aead.aead.op = RTE_CRYPTO_AEAD_OP_DECRYPT;
	} else {
		res_d->xform.chain.cipher.cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
		res_d->xform.chain.auth.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
	}

	return TEST_SUCCESS;
}

static int
tls_record_hdr_verify(const struct tls_record_test_data *td, const uint8_t *output_text,
		      const struct tls_record_test_flags *flags)
{
	uint16_t length, hdr_len;
	uint8_t content_type;

	if (td->tls_record_xform.ver == RTE_SECURITY_VERSION_TLS_1_2) {
		const struct rte_tls_hdr *hdr = (const struct rte_tls_hdr *)output_text;
		if (rte_be_to_cpu_16(hdr->version) != RTE_TLS_VERSION_1_2) {
			printf("Incorrect header version [expected - %4x, received - %4x]\n",
			       RTE_TLS_VERSION_1_2, rte_be_to_cpu_16(hdr->version));
			return TEST_FAILED;
		}
		content_type = hdr->type;
		length = rte_be_to_cpu_16(hdr->length);
		hdr_len = sizeof(struct rte_tls_hdr);
	} else if (td->tls_record_xform.ver == RTE_SECURITY_VERSION_TLS_1_3) {
		const struct rte_tls_hdr *hdr = (const struct rte_tls_hdr *)output_text;
		if (rte_be_to_cpu_16(hdr->version) != RTE_TLS_VERSION_1_2) {
			printf("Incorrect header version [expected - %4x, received - %4x]\n",
			       RTE_TLS_VERSION_1_2, rte_be_to_cpu_16(hdr->version));
			return TEST_FAILED;
		}
		content_type = hdr->type;
		length = rte_be_to_cpu_16(hdr->length);
		hdr_len = sizeof(struct rte_tls_hdr);
	} else if (td->tls_record_xform.ver == RTE_SECURITY_VERSION_DTLS_1_2) {
		const struct rte_dtls_hdr *hdr = (const struct rte_dtls_hdr *)output_text;
		if (rte_be_to_cpu_16(hdr->version) != RTE_DTLS_VERSION_1_2) {
			printf("Incorrect header version [expected - %4x, received - %4x]\n",
			       RTE_DTLS_VERSION_1_2, rte_be_to_cpu_16(hdr->version));
			return TEST_FAILED;
		}
		content_type = hdr->type;
		length = rte_be_to_cpu_16(hdr->length);
		hdr_len = sizeof(struct rte_dtls_hdr);
	} else {
		return TEST_FAILED;
	}

	if (td->tls_record_xform.ver == RTE_SECURITY_VERSION_TLS_1_3) {
		if (content_type != RTE_TLS_TYPE_APPDATA) {
			printf("Incorrect content type in packet [expected - %d, received - %d]\n",
			       td->app_type, content_type);
			return TEST_FAILED;
		}
	} else {
		if (content_type != td->app_type) {
			printf("Incorrect content type in packet [expected - %d, received - %d]\n",
			       td->app_type, content_type);
			return TEST_FAILED;
		}
	}

	if (!flags->opt_padding) {
		if (length != td->output_text.len - hdr_len) {
			printf("Incorrect packet length [expected - %d, received - %d]\n",
			       td->output_text.len - hdr_len, length);
			return TEST_FAILED;
		}
	} else {
		int pad_len = (flags->opt_padding * 8) > 256 ? 256 : (flags->opt_padding * 8);
		int expect_len = td->output_text.len - hdr_len + pad_len;

		if (length - expect_len > 32) {
			printf("Incorrect packet length [expected - %d, received - %d]\n",
			       expect_len, length);
			return TEST_FAILED;
		}

	}

	return TEST_SUCCESS;
}

int
test_tls_record_post_process(const struct rte_mbuf *m, const struct tls_record_test_data *td,
			     struct tls_record_test_data *res_d, bool silent,
			     const struct tls_record_test_flags *flags)
{
	uint8_t output_text[TEST_SEC_CIPHERTEXT_MAX_LEN];
	uint32_t len = rte_pktmbuf_pkt_len(m), data_len;
	const struct rte_mbuf *seg;
	const uint8_t *output;
	int ret;

	memset(output_text, 0, TEST_SEC_CIPHERTEXT_MAX_LEN);

	/*
	 * Actual data in packet might be less in error cases, hence take minimum of pkt_len and sum
	 * of data_len. This is done to run through negative test cases.
	 */
	data_len = 0;
	seg = m;
	while (seg) {
		data_len += seg->data_len;
		seg = seg->next;
	}

	len = RTE_MIN(len, data_len);
	TEST_ASSERT(len <= TEST_SEC_CIPHERTEXT_MAX_LEN, "Invalid packet length: %u", len);

	/* Copy mbuf payload to continuous buffer */
	output = rte_pktmbuf_read(m, 0, len, output_text);
	if (output != output_text) {
		/* Single segment mbuf, copy manually */
		memcpy(output_text, output, len);
	}

	if (td->tls_record_xform.type == RTE_SECURITY_TLS_SESS_TYPE_WRITE) {
		ret = tls_record_hdr_verify(td, output_text, flags);
		if (ret != TEST_SUCCESS)
			return ret;
	}

	/*
	 * In case of known vector tests & all record read (decrypt) tests, res_d provided would be
	 * NULL and output data need to be validated against expected. For record read (decrypt),
	 * output_text would be plain payload and for record write (encrypt), output_text would TLS
	 * record. Validate by comparing against known vectors.
	 *
	 * In case of combined mode tests, the output_text from TLS write (encrypt) operation (ie,
	 * TLS record) would need to be decrypted using a TLS read operation to obtain the plain
	 * text. Copy output_text to result data, 'res_d', so that inbound processing can be done.
	 */

	if (res_d == NULL)
		return test_tls_record_td_verify(output_text, len, td, silent);
	else
		return test_tls_record_res_d_prepare(output_text, len, td, res_d);
}