1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
8 #include <rte_malloc.h>
9 #include <rte_cryptodev.h>
11 #include "fips_validation.h"
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"
18 #define ENC_STR "[ENCRYPT]"
19 #define DEC_STR "[DECRYPT]"
21 #define COUNT_STR "COUNT = "
22 #define KEY1_STR "KEY1 = "
23 #define KEY2_STR "KEY2 = "
24 #define KEY3_STR "KEY3 = "
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 = "
39 #define DEVICE_STR "# Config Info for : "
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"},
56 writeback_tdes_hex_str(const char *key, char *dst, struct fips_val *val);
59 parse_tdes_uint8_hex_str(const char *key, char *src, struct fips_val *val);
62 parse_tdes_interim(const char *key,
63 __attribute__((__unused__)) char *text,
64 struct fips_val *val);
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 */
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 */
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 */
97 parse_tdes_interim(const char *key,
98 __attribute__((__unused__)) char *text,
99 __attribute__((__unused__)) struct fips_val *val)
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;
118 parse_tdes_uint8_hex_str(const char *key, char *src, struct fips_val *val)
120 uint8_t tmp_key[24] = {0};
125 len = strlen(src) / 2;
128 memcpy(tmp_key, val->val, val->len);
132 val->val = rte_zmalloc(NULL, 24, 0);
136 memcpy(val->val, tmp_key, 24);
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'};
142 if (parser_read_uint8_hex(&val->val[i], byte) < 0)
146 memcpy(val->val + 8, val->val, 8);
147 memcpy(val->val + 16, val->val, 8);
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'};
153 if (parser_read_uint8_hex(&val->val[i], byte) < 0)
157 if (info.interim_info.tdes_data.nb_keys == 2)
158 memcpy(val->val + 16, val->val, 8);
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'};
164 if (parser_read_uint8_hex(&val->val[i + 8], byte) < 0)
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'};
172 if (parser_read_uint8_hex(&val->val[i + 16], byte) < 0)
184 memset(val, 0, sizeof(*val));
189 parse_test_tdes_writeback(struct fips_val *val)
192 if (info.op == FIPS_TEST_ENC_AUTH_GEN)
193 fprintf(info.fp_wr, "%s", CT_STR);
195 fprintf(info.fp_wr, "%s", PT_STR);
197 parse_write_hex_str(val);
204 writeback_tdes_hex_str(const char *key, char *dst, struct fips_val *val)
206 struct fips_val tmp_val = {0};
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;
219 return writeback_hex_str(key, dst, &tmp_val);
223 rsp_test_tdes_check(struct fips_val *val)
225 struct fips_val *data;
227 if (info.op == FIPS_TEST_ENC_AUTH_GEN)
232 if (memcmp(val->val, data->val, val->len) == 0)
233 fprintf(info.fp_wr, "Success\n");
235 fprintf(info.fp_wr, "Failed\n");
241 parse_test_tdes_init(void)
245 for (i = 0; i < info.nb_vec_lines; i++) {
246 char *line = info.vec[i];
249 if (strstr(line, TEST_CBCI_KEY))
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 =
256 if (strstr(line, TEST_TYPE_ECB_KEY))
257 info.interim_info.tdes_data.test_mode =
260 info.interim_info.tdes_data.test_mode =
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;