xref: /dpdk/examples/fips_validation/fips_validation_aes.c (revision 25d11a86c56d50947af33d0b79ede622809bd8b9)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4 
5 #include <string.h>
6 #include <time.h>
7 #include <stdio.h>
8 
9 #include <rte_cryptodev.h>
10 
11 #include "fips_validation.h"
12 
13 #define MODE_STR	"AESVS"
14 #define ALGO_STR	"test data for "
15 #define OP_STR		"State"
16 #define KEY_SIZE_STR	"Key Length : "
17 
18 
19 #define COUNT_STR	"COUNT = "
20 #define KEY_STR		"KEY = "
21 #define IV_STR		"IV = "
22 #define PT_STR		"PLAINTEXT = "
23 #define CT_STR		"CIPHERTEXT = "
24 
25 #define OP_ENC_STR	"ENCRYPT"
26 #define OP_DEC_STR	"DECRYPT"
27 
28 struct {
29 	uint32_t type;
30 	const char *desc;
31 } aes_test_types[] = {
32 		{AESAVS_TYPE_GFXBOX, "GFSbox"},
33 		{AESAVS_TYPE_KEYSBOX, "KeySbox"},
34 		{AESAVS_TYPE_VARKEY, "VarKey"},
35 		{AESAVS_TYPE_VARTXT, "VarTxt"},
36 		{TDES_VARIABLE_TEXT, "VARIABLE PLAINTEXT/CIPHERTEXT"},
37 		{TDES_VARIABLE_TEXT, "KAT"},
38 		{AESAVS_TYPE_MMT, "MMT"},
39 		{AESAVS_TYPE_MCT, "MCT"},
40 };
41 
42 struct aes_test_algo {
43 	const char *name;
44 	enum rte_crypto_cipher_algorithm algo;
45 } const algo_con[] = {
46 		{"CBC", RTE_CRYPTO_CIPHER_AES_CBC},
47 };
48 
49 static int
50 parse_interim_enc_dec(const char *key,
51 		__attribute__((__unused__)) char *text,
52 		__attribute__((__unused__)) struct fips_val *val)
53 {
54 	if (strcmp(key, OP_ENC_STR) == 0)
55 		info.op = FIPS_TEST_ENC_AUTH_GEN;
56 	else if (strcmp(key, OP_DEC_STR) == 0)
57 		info.op = FIPS_TEST_DEC_AUTH_VERIF;
58 	else
59 		return -1;
60 
61 	return 0;
62 }
63 
64 struct fips_test_callback aes_tests_interim[] = {
65 		{OP_ENC_STR, parse_interim_enc_dec, NULL},
66 		{OP_DEC_STR, parse_interim_enc_dec, NULL},
67 		{NULL, NULL, NULL} /**< end pointer */
68 };
69 
70 struct fips_test_callback aes_tests_vectors[] = {
71 		{KEY_STR, parse_uint8_hex_str, &vec.cipher_auth.key},
72 		{IV_STR, parse_uint8_hex_str, &vec.iv},
73 		{PT_STR, parse_uint8_hex_str, &vec.pt},
74 		{CT_STR, parse_uint8_hex_str, &vec.ct},
75 		{NULL, NULL, NULL} /**< end pointer */
76 };
77 
78 struct fips_test_callback aes_tests_interim_vectors[] = {
79 		{OP_ENC_STR, parse_interim_enc_dec, NULL},
80 		{OP_DEC_STR, parse_interim_enc_dec, NULL},
81 		{NULL, NULL, NULL} /**< end pointer */
82 };
83 
84 struct fips_test_callback aes_writeback_callbacks[] = {
85 		/** First element is used to pass COUNT string */
86 		{COUNT_STR, NULL, NULL},
87 		{IV_STR, writeback_hex_str, &vec.iv},
88 		{KEY_STR, writeback_hex_str, &vec.cipher_auth.key},
89 		{PT_STR, writeback_hex_str, &vec.pt},
90 		{CT_STR, writeback_hex_str, &vec.ct},
91 		{NULL, NULL, NULL} /**< end pointer */
92 };
93 
94 static int
95 parse_test_aes_writeback(struct fips_val *val)
96 {
97 	if (info.op == FIPS_TEST_ENC_AUTH_GEN)
98 		fprintf(info.fp_wr, "%s", CT_STR);
99 	else
100 		fprintf(info.fp_wr, "%s", PT_STR);
101 
102 	parse_write_hex_str(val);
103 
104 	return 0;
105 }
106 
107 static int
108 rsp_test_aes_check(struct fips_val *val)
109 {
110 	struct fips_val *data;
111 
112 	if (info.op == FIPS_TEST_ENC_AUTH_GEN)
113 		data = &vec.ct;
114 	else
115 		data = &vec.pt;
116 
117 	if (memcmp(val->val, data->val, val->len) == 0)
118 		fprintf(info.fp_wr, "Success\n");
119 	else
120 		fprintf(info.fp_wr, "Failed\n");
121 
122 	return 0;
123 }
124 
125 int
126 parse_test_aes_init(void)
127 {
128 	char *tmp;
129 	uint32_t i, j;
130 
131 	for (i = 0; i < info.nb_vec_lines; i++) {
132 		char *line = info.vec[i];
133 
134 		tmp = strstr(line, MODE_STR);
135 		if (tmp) {
136 			for (j = 0; j < RTE_DIM(aes_test_types); j++)
137 				if (strstr(line, aes_test_types[j].desc)) {
138 					info.interim_info.aes_data.test_type =
139 							aes_test_types[j].type;
140 					break;
141 				}
142 
143 			if (j >= RTE_DIM(aes_test_types))
144 				return -EINVAL;
145 
146 			tmp = strstr(line, ALGO_STR);
147 			if (!tmp)
148 				return -EINVAL;
149 
150 			tmp += strlen(ALGO_STR);
151 			for (j = 0; j < RTE_DIM(algo_con); j++)
152 				if (strcmp(algo_con[j].name, tmp) == 0) {
153 					info.interim_info.aes_data.cipher_algo =
154 						(uint32_t)algo_con[j].algo;
155 					break;
156 				}
157 			if (j >= RTE_DIM(algo_con))
158 				return -EINVAL;
159 
160 			continue;
161 		}
162 
163 		tmp = strstr(line, OP_STR);
164 		if (tmp)
165 			continue;
166 
167 		tmp = strstr(line, KEY_SIZE_STR);
168 		if (tmp) {
169 			tmp += strlen(KEY_SIZE_STR);
170 			if (parser_read_uint32
171 					(&info.interim_info.aes_data.key_len,
172 							tmp) < 0)
173 				return -EINVAL;
174 
175 			info.interim_info.aes_data.key_len /= 8;
176 
177 			continue;
178 		}
179 	}
180 
181 	info.parse_writeback = parse_test_aes_writeback;
182 	info.callbacks = aes_tests_vectors;
183 	info.interim_callbacks = aes_tests_interim_vectors;
184 	info.writeback_callbacks = aes_writeback_callbacks;
185 	info.kat_check = rsp_test_aes_check;
186 
187 	return 0;
188 }
189