xref: /dpdk/examples/fips_validation/fips_validation_tdes.c (revision 487eec3401b7a1664982f39da139980a4f5b3adc)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4 
5 #include <string.h>
6 #include <stdio.h>
7 
8 #include <rte_malloc.h>
9 #include <rte_cryptodev.h>
10 
11 #include "fips_validation.h"
12 
13 #define NEW_LINE_STR	"#"
14 #define TEST_TYPE_KEY	" for CBC"
15 #define TEST_TYPE_ECB_KEY	" for ECB"
16 #define TEST_CBCI_KEY	" for CBCI"
17 
18 #define ENC_STR		"[ENCRYPT]"
19 #define DEC_STR		"[DECRYPT]"
20 
21 #define COUNT_STR	"COUNT = "
22 #define KEY1_STR	"KEY1 = "
23 #define KEY2_STR	"KEY2 = "
24 #define KEY3_STR	"KEY3 = "
25 
26 #define KEYS_STR	"KEYs = "
27 #define IV_STR		"IV = "
28 #define PT_STR		"PLAINTEXT = "
29 #define CT_STR		"CIPHERTEXT = "
30 #define NK_STR		"NumKeys = "
31 
32 #define SET_STR		" = "
33 
34 #define PLAIN_TEXT	0
35 #define CIPHER_TEXT	1
36 #define KEY_TEXT	2
37 #define IV_TEXT		3
38 
39 #define DEVICE_STR	"# Config Info for : "
40 
41 struct {
42 	uint32_t type;
43 	const char *desc;
44 } test_types[] = {
45 		{TDES_INVERSE_PERMUTATION, "INVERSE PERMUTATION"},
46 		{TDES_PERMUTATION, "PERMUTATION OPERATION"},
47 		{TDES_SUBSTITUTION_TABLE, "SUBSTITUTION TABLE"},
48 		{TDES_VARIABLE_KEY, "VARIABLE KEY"},
49 		{TDES_VARIABLE_TEXT, "VARIABLE PLAINTEXT/CIPHERTEXT"},
50 		{TDES_VARIABLE_TEXT, "KAT"},
51 		{TDES_MCT, "Monte Carlo (Modes) Test"},
52 		{TDES_MMT, "Multi block Message Test"},
53 };
54 
55 static int
56 writeback_tdes_hex_str(const char *key, char *dst, struct fips_val *val);
57 
58 static int
59 parse_tdes_uint8_hex_str(const char *key, char *src, struct fips_val *val);
60 
61 static int
62 parse_tdes_interim(const char *key,
63 		__attribute__((__unused__)) char *text,
64 		struct fips_val *val);
65 
66 struct fips_test_callback tdes_tests_vectors[] = {
67 		{KEYS_STR, parse_tdes_uint8_hex_str, &vec.cipher_auth.key},
68 		{KEY1_STR, parse_tdes_uint8_hex_str, &vec.cipher_auth.key},
69 		{KEY2_STR, parse_tdes_uint8_hex_str, &vec.cipher_auth.key},
70 		{KEY3_STR, parse_tdes_uint8_hex_str, &vec.cipher_auth.key},
71 		{IV_STR, parse_uint8_hex_str, &vec.iv},
72 		{PT_STR, parse_uint8_hex_str, &vec.pt},
73 		{CT_STR, parse_uint8_hex_str, &vec.ct},
74 		{NULL, NULL, NULL} /**< end pointer */
75 };
76 
77 struct fips_test_callback tdes_tests_interim_vectors[] = {
78 		{ENC_STR, parse_tdes_interim, NULL},
79 		{DEC_STR, parse_tdes_interim, NULL},
80 		{NULL, NULL, NULL} /**< end pointer */
81 };
82 
83 struct fips_test_callback tdes_writeback_callbacks[] = {
84 		/** First element is used to pass COUNT string */
85 		{COUNT_STR, NULL, NULL},
86 		{IV_STR, writeback_hex_str, &vec.iv},
87 		{KEY1_STR, writeback_tdes_hex_str, &vec.cipher_auth.key},
88 		{KEY2_STR, writeback_tdes_hex_str, &vec.cipher_auth.key},
89 		{KEY3_STR, writeback_tdes_hex_str, &vec.cipher_auth.key},
90 		{KEYS_STR, writeback_tdes_hex_str, &vec.cipher_auth.key},
91 		{PT_STR, writeback_hex_str, &vec.pt},
92 		{CT_STR, writeback_hex_str, &vec.ct},
93 		{NULL, NULL, NULL} /**< end pointer */
94 };
95 
96 static int
97 parse_tdes_interim(const char *key,
98 		__attribute__((__unused__)) char *text,
99 		__attribute__((__unused__)) struct fips_val *val)
100 {
101 	if (strstr(key, ENC_STR))
102 		info.op = FIPS_TEST_ENC_AUTH_GEN;
103 	else if (strstr(key, DEC_STR))
104 		info.op = FIPS_TEST_DEC_AUTH_VERIF;
105 	else if (strstr(NK_STR, "NumKeys = 1"))
106 		info.interim_info.tdes_data.nb_keys = 1;
107 	else if (strstr(NK_STR, "NumKeys = 2"))
108 		info.interim_info.tdes_data.nb_keys = 2;
109 	else if (strstr(NK_STR, "NumKeys = 3"))
110 		info.interim_info.tdes_data.nb_keys = 3;
111 	else
112 		return -EINVAL;
113 
114 	return 0;
115 }
116 
117 static int
118 parse_tdes_uint8_hex_str(const char *key, char *src, struct fips_val *val)
119 {
120 	uint8_t tmp_key[24] = {0};
121 	uint32_t len, i;
122 
123 	src += strlen(key);
124 
125 	len = strlen(src) / 2;
126 
127 	if (val->val) {
128 		memcpy(tmp_key, val->val, val->len);
129 		rte_free(val->val);
130 	}
131 
132 	val->val = rte_zmalloc(NULL, 24, 0);
133 	if (!val->val)
134 		return -1;
135 
136 	memcpy(val->val, tmp_key, 24);
137 
138 	if (strstr(key, KEYS_STR)) {
139 		for (i = 0; i < len; i++) {
140 			char byte[3] = {src[i * 2], src[i * 2 + 1], '\0'};
141 
142 			if (parser_read_uint8_hex(&val->val[i], byte) < 0)
143 				goto error_exit;
144 		}
145 
146 		memcpy(val->val + 8, val->val, 8);
147 		memcpy(val->val + 16, val->val, 8);
148 
149 	} else if (strstr(key, KEY1_STR)) {
150 		for (i = 0; i < len; i++) {
151 			char byte[3] = {src[i * 2], src[i * 2 + 1], '\0'};
152 
153 			if (parser_read_uint8_hex(&val->val[i], byte) < 0)
154 				goto error_exit;
155 		}
156 
157 		if (info.interim_info.tdes_data.nb_keys == 2)
158 			memcpy(val->val + 16, val->val, 8);
159 
160 	} else if (strstr(key, KEY2_STR)) {
161 		for (i = 0; i < len; i++) {
162 			char byte[3] = {src[i * 2], src[i * 2 + 1], '\0'};
163 
164 			if (parser_read_uint8_hex(&val->val[i + 8], byte) < 0)
165 				goto error_exit;
166 		}
167 
168 	} else if (strstr(key, KEY3_STR)) {
169 		for (i = 0; i < len; i++) {
170 			char byte[3] = {src[i * 2], src[i * 2 + 1], '\0'};
171 
172 			if (parser_read_uint8_hex(&val->val[i + 16], byte) < 0)
173 				goto error_exit;
174 		}
175 	} else
176 		return -EINVAL;
177 
178 	val->len = 24;
179 
180 	return 0;
181 
182 error_exit:
183 	rte_free(val->val);
184 	memset(val, 0, sizeof(*val));
185 	return -EINVAL;
186 }
187 
188 static int
189 parse_test_tdes_writeback(struct fips_val *val)
190 {
191 
192 	if (info.op == FIPS_TEST_ENC_AUTH_GEN)
193 		fprintf(info.fp_wr, "%s", CT_STR);
194 	else
195 		fprintf(info.fp_wr, "%s", PT_STR);
196 
197 	parse_write_hex_str(val);
198 
199 	return 0;
200 
201 }
202 
203 static int
204 writeback_tdes_hex_str(const char *key, char *dst, struct fips_val *val)
205 {
206 	struct fips_val tmp_val = {0};
207 
208 	tmp_val.len = 8;
209 
210 	if (strstr(key, KEY1_STR))
211 		tmp_val.val = val->val;
212 	else if (strstr(key, KEY2_STR))
213 		tmp_val.val = val->val + 8;
214 	else if (strstr(key, KEY3_STR))
215 		tmp_val.val = val->val + 16;
216 	else
217 		return -EINVAL;
218 
219 	return writeback_hex_str(key, dst, &tmp_val);
220 }
221 
222 static int
223 rsp_test_tdes_check(struct fips_val *val)
224 {
225 	struct fips_val *data;
226 
227 	if (info.op == FIPS_TEST_ENC_AUTH_GEN)
228 		data = &vec.ct;
229 	else
230 		data = &vec.pt;
231 
232 	if (memcmp(val->val, data->val, val->len) == 0)
233 		fprintf(info.fp_wr, "Success\n");
234 	else
235 		fprintf(info.fp_wr, "Failed\n");
236 
237 	return 0;
238 }
239 
240 int
241 parse_test_tdes_init(void)
242 {
243 	uint32_t i;
244 
245 	for (i = 0; i < info.nb_vec_lines; i++) {
246 		char *line = info.vec[i];
247 		uint32_t j;
248 
249 		if (strstr(line, TEST_CBCI_KEY))
250 			return -EPERM;
251 
252 		for (j = 0; j < RTE_DIM(test_types); j++)
253 			if (strstr(line, test_types[j].desc)) {
254 				info.interim_info.tdes_data.test_type =
255 						test_types[j].type;
256 				if (strstr(line, TEST_TYPE_ECB_KEY))
257 					info.interim_info.tdes_data.test_mode =
258 						TDES_MODE_ECB;
259 				else
260 					info.interim_info.tdes_data.test_mode =
261 						TDES_MODE_CBC;
262 				break;
263 			}
264 	}
265 
266 	info.parse_writeback = parse_test_tdes_writeback;
267 	info.callbacks = tdes_tests_vectors;
268 	info.interim_callbacks = tdes_tests_interim_vectors;
269 	info.writeback_callbacks = tdes_writeback_callbacks;
270 	info.kat_check = rsp_test_tdes_check;
271 
272 	return 0;
273 }
274