common/cnxk: allow building for generic arm64
[dpdk.git] / examples / fips_validation / fips_validation_tdes.c
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, char *text, struct fips_val *val);
63
64 struct fips_test_callback tdes_tests_vectors[] = {
65                 {KEYS_STR, parse_tdes_uint8_hex_str, &vec.cipher_auth.key},
66                 {KEY1_STR, parse_tdes_uint8_hex_str, &vec.cipher_auth.key},
67                 {KEY2_STR, parse_tdes_uint8_hex_str, &vec.cipher_auth.key},
68                 {KEY3_STR, parse_tdes_uint8_hex_str, &vec.cipher_auth.key},
69                 {IV_STR, parse_uint8_hex_str, &vec.iv},
70                 {PT_STR, parse_uint8_hex_str, &vec.pt},
71                 {CT_STR, parse_uint8_hex_str, &vec.ct},
72                 {NULL, NULL, NULL} /**< end pointer */
73 };
74
75 struct fips_test_callback tdes_tests_interim_vectors[] = {
76                 {ENC_STR, parse_tdes_interim, NULL},
77                 {DEC_STR, parse_tdes_interim, NULL},
78                 {NK_STR, parse_tdes_interim, NULL},
79                 {NULL, NULL, NULL} /**< end pointer */
80 };
81
82 struct fips_test_callback tdes_writeback_callbacks[] = {
83                 /** First element is used to pass COUNT string */
84                 {COUNT_STR, NULL, NULL},
85                 {IV_STR, writeback_hex_str, &vec.iv},
86                 {KEY1_STR, writeback_tdes_hex_str, &vec.cipher_auth.key},
87                 {KEY2_STR, writeback_tdes_hex_str, &vec.cipher_auth.key},
88                 {KEY3_STR, writeback_tdes_hex_str, &vec.cipher_auth.key},
89                 {KEYS_STR, writeback_tdes_hex_str, &vec.cipher_auth.key},
90                 {PT_STR, writeback_hex_str, &vec.pt},
91                 {CT_STR, writeback_hex_str, &vec.ct},
92                 {NULL, NULL, NULL} /**< end pointer */
93 };
94
95 static int
96 parse_tdes_interim(const char *key, char *text,
97                 __rte_unused struct fips_val *val)
98 {
99         if (strstr(key, ENC_STR))
100                 info.op = FIPS_TEST_ENC_AUTH_GEN;
101         else if (strstr(key, DEC_STR))
102                 info.op = FIPS_TEST_DEC_AUTH_VERIF;
103         else if (strstr(key, NK_STR)) {
104                 if (strcmp(text, "NumKeys = 1") == 0)
105                         info.interim_info.tdes_data.nb_keys = 1;
106                 else if (strcmp(text, "NumKeys = 2") == 0)
107                         info.interim_info.tdes_data.nb_keys = 2;
108                 else if (strcmp(text, "NumKeys = 3") == 0)
109                         info.interim_info.tdes_data.nb_keys = 3;
110                 else
111                         return -EINVAL;
112         } else
113                 return -EINVAL;
114
115         return 0;
116 }
117
118 static int
119 parse_tdes_uint8_hex_str(const char *key, char *src, struct fips_val *val)
120 {
121         uint8_t tmp_key[24] = {0};
122         uint32_t len, i;
123
124         src += strlen(key);
125
126         len = strlen(src) / 2;
127
128         if (val->val) {
129                 memcpy(tmp_key, val->val, val->len);
130                 rte_free(val->val);
131         }
132
133         val->val = rte_zmalloc(NULL, 24, 0);
134         if (!val->val)
135                 return -1;
136
137         memcpy(val->val, tmp_key, 24);
138
139         if (strstr(key, KEYS_STR)) {
140                 for (i = 0; i < len; i++) {
141                         char byte[3] = {src[i * 2], src[i * 2 + 1], '\0'};
142
143                         if (parser_read_uint8_hex(&val->val[i], byte) < 0)
144                                 goto error_exit;
145                 }
146
147                 memcpy(val->val + 8, val->val, 8);
148                 memcpy(val->val + 16, val->val, 8);
149
150         } else if (strstr(key, KEY1_STR)) {
151                 for (i = 0; i < len; i++) {
152                         char byte[3] = {src[i * 2], src[i * 2 + 1], '\0'};
153
154                         if (parser_read_uint8_hex(&val->val[i], byte) < 0)
155                                 goto error_exit;
156                 }
157
158                 if (info.interim_info.tdes_data.nb_keys == 2)
159                         memcpy(val->val + 16, val->val, 8);
160
161         } else if (strstr(key, KEY2_STR)) {
162                 for (i = 0; i < len; i++) {
163                         char byte[3] = {src[i * 2], src[i * 2 + 1], '\0'};
164
165                         if (parser_read_uint8_hex(&val->val[i + 8], byte) < 0)
166                                 goto error_exit;
167                 }
168
169         } else if (strstr(key, KEY3_STR)) {
170                 for (i = 0; i < len; i++) {
171                         char byte[3] = {src[i * 2], src[i * 2 + 1], '\0'};
172
173                         if (parser_read_uint8_hex(&val->val[i + 16], byte) < 0)
174                                 goto error_exit;
175                 }
176         } else
177                 return -EINVAL;
178
179         val->len = 24;
180
181         return 0;
182
183 error_exit:
184         rte_free(val->val);
185         memset(val, 0, sizeof(*val));
186         return -EINVAL;
187 }
188
189 static int
190 parse_test_tdes_writeback(struct fips_val *val)
191 {
192
193         if (info.op == FIPS_TEST_ENC_AUTH_GEN)
194                 fprintf(info.fp_wr, "%s", CT_STR);
195         else
196                 fprintf(info.fp_wr, "%s", PT_STR);
197
198         parse_write_hex_str(val);
199
200         return 0;
201
202 }
203
204 static int
205 writeback_tdes_hex_str(const char *key, char *dst, struct fips_val *val)
206 {
207         struct fips_val tmp_val = {0};
208
209         tmp_val.len = 8;
210
211         if (strstr(key, KEY1_STR))
212                 tmp_val.val = val->val;
213         else if (strstr(key, KEY2_STR))
214                 tmp_val.val = val->val + 8;
215         else if (strstr(key, KEY3_STR))
216                 tmp_val.val = val->val + 16;
217         else
218                 return -EINVAL;
219
220         return writeback_hex_str(key, dst, &tmp_val);
221 }
222
223 static int
224 rsp_test_tdes_check(struct fips_val *val)
225 {
226         struct fips_val *data;
227
228         if (info.op == FIPS_TEST_ENC_AUTH_GEN)
229                 data = &vec.ct;
230         else
231                 data = &vec.pt;
232
233         if (memcmp(val->val, data->val, val->len) == 0)
234                 fprintf(info.fp_wr, "Success\n");
235         else
236                 fprintf(info.fp_wr, "Failed\n");
237
238         return 0;
239 }
240
241 int
242 parse_test_tdes_init(void)
243 {
244         uint32_t i;
245
246         for (i = 0; i < info.nb_vec_lines; i++) {
247                 char *line = info.vec[i];
248                 uint32_t j;
249
250                 if (strstr(line, TEST_CBCI_KEY))
251                         return -EPERM;
252
253                 for (j = 0; j < RTE_DIM(test_types); j++)
254                         if (strstr(line, test_types[j].desc)) {
255                                 info.interim_info.tdes_data.test_type =
256                                                 test_types[j].type;
257                                 if (strstr(line, TEST_TYPE_ECB_KEY))
258                                         info.interim_info.tdes_data.test_mode =
259                                                 TDES_MODE_ECB;
260                                 else
261                                         info.interim_info.tdes_data.test_mode =
262                                                 TDES_MODE_CBC;
263                                 break;
264                         }
265         }
266
267         info.parse_writeback = parse_test_tdes_writeback;
268         info.callbacks = tdes_tests_vectors;
269         info.interim_callbacks = tdes_tests_interim_vectors;
270         info.writeback_callbacks = tdes_writeback_callbacks;
271         info.kat_check = rsp_test_tdes_check;
272
273         return 0;
274 }