xref: /dpdk/app/test/test_security_proto.c (revision cf93f07b21eb13a102b517ad0e57f49ffc31ee47)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2023 Marvell.
3  */
4 
5 #include <rte_cryptodev.h>
6 #include <rte_security.h>
7 
8 #include "test_security_proto.h"
9 
10 struct crypto_param_comb sec_alg_list[RTE_DIM(aead_list) +
11 				  (RTE_DIM(cipher_list) *
12 				   RTE_DIM(auth_list))];
13 
14 struct crypto_param_comb sec_auth_only_alg_list[2 * (RTE_DIM(auth_list) - 1)];
15 
16 static uint8_t cleartext_pattern[TEST_SEC_CLEARTEXT_MAX_LEN];
17 
18 void
test_sec_alg_list_populate(void)19 test_sec_alg_list_populate(void)
20 {
21 	unsigned long i, j, index = 0;
22 
23 	for (i = 0; i < RTE_DIM(aead_list); i++) {
24 		sec_alg_list[index].param1 = &aead_list[i];
25 		sec_alg_list[index].param2 = NULL;
26 		index++;
27 	}
28 
29 	for (i = 0; i < RTE_DIM(cipher_list); i++) {
30 		for (j = 0; j < RTE_DIM(auth_list); j++) {
31 			sec_alg_list[index].param1 = &cipher_list[i];
32 			sec_alg_list[index].param2 = &auth_list[j];
33 			index++;
34 		}
35 	}
36 }
37 
38 void
test_sec_auth_only_alg_list_populate(void)39 test_sec_auth_only_alg_list_populate(void)
40 {
41 	unsigned long i, index = 0;
42 
43 	for (i = 1; i < RTE_DIM(auth_list); i++) {
44 		sec_auth_only_alg_list[index].param1 = &auth_list[i];
45 		sec_auth_only_alg_list[index].param2 = NULL;
46 		index++;
47 	}
48 
49 	for (i = 1; i < RTE_DIM(auth_list); i++) {
50 		/* NULL cipher */
51 		sec_auth_only_alg_list[index].param1 = &cipher_list[0];
52 
53 		sec_auth_only_alg_list[index].param2 = &auth_list[i];
54 		index++;
55 	}
56 }
57 
58 int
test_sec_crypto_caps_aead_verify(const struct rte_security_capability * sec_cap,struct rte_crypto_sym_xform * aead)59 test_sec_crypto_caps_aead_verify(const struct rte_security_capability *sec_cap,
60 		struct rte_crypto_sym_xform *aead)
61 {
62 	const struct rte_cryptodev_symmetric_capability *sym_cap;
63 	const struct rte_cryptodev_capabilities *crypto_cap;
64 	int j = 0;
65 
66 	while ((crypto_cap = &sec_cap->crypto_capabilities[j++])->op !=
67 			RTE_CRYPTO_OP_TYPE_UNDEFINED) {
68 		if (crypto_cap->op == RTE_CRYPTO_OP_TYPE_SYMMETRIC &&
69 				crypto_cap->sym.xform_type == aead->type &&
70 				crypto_cap->sym.aead.algo == aead->aead.algo) {
71 			sym_cap = &crypto_cap->sym;
72 			if (rte_cryptodev_sym_capability_check_aead(sym_cap,
73 					aead->aead.key.length,
74 					aead->aead.digest_length,
75 					aead->aead.aad_length,
76 					aead->aead.iv.length) == 0)
77 				return 0;
78 		}
79 	}
80 
81 	return -ENOTSUP;
82 }
83 
84 int
test_sec_crypto_caps_cipher_verify(const struct rte_security_capability * sec_cap,struct rte_crypto_sym_xform * cipher)85 test_sec_crypto_caps_cipher_verify(const struct rte_security_capability *sec_cap,
86 		struct rte_crypto_sym_xform *cipher)
87 {
88 	const struct rte_cryptodev_symmetric_capability *sym_cap;
89 	const struct rte_cryptodev_capabilities *cap;
90 	int j = 0;
91 
92 	while ((cap = &sec_cap->crypto_capabilities[j++])->op !=
93 			RTE_CRYPTO_OP_TYPE_UNDEFINED) {
94 		if (cap->op == RTE_CRYPTO_OP_TYPE_SYMMETRIC &&
95 				cap->sym.xform_type == cipher->type &&
96 				cap->sym.cipher.algo == cipher->cipher.algo) {
97 			sym_cap = &cap->sym;
98 			if (rte_cryptodev_sym_capability_check_cipher(sym_cap,
99 					cipher->cipher.key.length,
100 					cipher->cipher.iv.length) == 0)
101 				return 0;
102 		}
103 	}
104 
105 	return -ENOTSUP;
106 }
107 
108 int
test_sec_crypto_caps_auth_verify(const struct rte_security_capability * sec_cap,struct rte_crypto_sym_xform * auth)109 test_sec_crypto_caps_auth_verify(const struct rte_security_capability *sec_cap,
110 		struct rte_crypto_sym_xform *auth)
111 {
112 	const struct rte_cryptodev_symmetric_capability *sym_cap;
113 	const struct rte_cryptodev_capabilities *cap;
114 	int j = 0;
115 
116 	while ((cap = &sec_cap->crypto_capabilities[j++])->op !=
117 			RTE_CRYPTO_OP_TYPE_UNDEFINED) {
118 		if (cap->op == RTE_CRYPTO_OP_TYPE_SYMMETRIC &&
119 				cap->sym.xform_type == auth->type &&
120 				cap->sym.auth.algo == auth->auth.algo) {
121 			sym_cap = &cap->sym;
122 			if (rte_cryptodev_sym_capability_check_auth(sym_cap,
123 					auth->auth.key.length,
124 					auth->auth.digest_length,
125 					auth->auth.iv.length) == 0)
126 				return 0;
127 		}
128 	}
129 
130 	return -ENOTSUP;
131 }
132 
133 void
test_sec_alg_display(const struct crypto_param * param1,const struct crypto_param * param2)134 test_sec_alg_display(const struct crypto_param *param1, const struct crypto_param *param2)
135 {
136 	if (param1->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
137 		printf("\t%s [%d]",
138 		       rte_cryptodev_get_aead_algo_string(param1->alg.aead),
139 		       param1->key_length * 8);
140 	} else if (param1->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
141 		printf("\t%s",
142 		       rte_cryptodev_get_auth_algo_string(param1->alg.auth));
143 		if (param1->alg.auth != RTE_CRYPTO_AUTH_NULL)
144 			printf(" [%dB ICV]", param1->digest_length);
145 	} else {
146 		printf("\t%s",
147 		       rte_cryptodev_get_cipher_algo_string(param1->alg.cipher));
148 		if (param1->alg.cipher != RTE_CRYPTO_CIPHER_NULL)
149 			printf(" [%d]", param1->key_length * 8);
150 		printf(" %s",
151 		       rte_cryptodev_get_auth_algo_string(param2->alg.auth));
152 		if (param2->alg.auth != RTE_CRYPTO_AUTH_NULL)
153 			printf(" [%dB ICV]", param2->digest_length);
154 	}
155 	printf("\n");
156 }
157 
158 void
test_sec_proto_pattern_generate(void)159 test_sec_proto_pattern_generate(void)
160 {
161 	unsigned int i;
162 
163 	for (i = 0; i < TEST_SEC_CLEARTEXT_MAX_LEN; i++)
164 		cleartext_pattern[i] = (i + 1) & 0xff;
165 }
166 
167 void
test_sec_proto_pattern_set(uint8_t * buf,int len)168 test_sec_proto_pattern_set(uint8_t *buf, int len)
169 {
170 	rte_memcpy(buf, cleartext_pattern, len);
171 }
172