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, char *text, struct fips_val *val);
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 */
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 */
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 */
96 parse_tdes_interim(const char *key, char *text,
97 __rte_unused struct fips_val *val)
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;
119 parse_tdes_uint8_hex_str(const char *key, char *src, struct fips_val *val)
121 uint8_t tmp_key[24] = {0};
126 len = strlen(src) / 2;
129 memcpy(tmp_key, val->val, val->len);
133 val->val = rte_zmalloc(NULL, 24, 0);
137 memcpy(val->val, tmp_key, 24);
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'};
143 if (parser_read_uint8_hex(&val->val[i], byte) < 0)
147 memcpy(val->val + 8, val->val, 8);
148 memcpy(val->val + 16, val->val, 8);
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'};
154 if (parser_read_uint8_hex(&val->val[i], byte) < 0)
158 if (info.interim_info.tdes_data.nb_keys == 2)
159 memcpy(val->val + 16, val->val, 8);
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'};
165 if (parser_read_uint8_hex(&val->val[i + 8], byte) < 0)
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'};
173 if (parser_read_uint8_hex(&val->val[i + 16], byte) < 0)
185 memset(val, 0, sizeof(*val));
190 parse_test_tdes_writeback(struct fips_val *val)
193 if (info.op == FIPS_TEST_ENC_AUTH_GEN)
194 fprintf(info.fp_wr, "%s", CT_STR);
196 fprintf(info.fp_wr, "%s", PT_STR);
198 parse_write_hex_str(val);
205 writeback_tdes_hex_str(const char *key, char *dst, struct fips_val *val)
207 struct fips_val tmp_val = {0};
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;
220 return writeback_hex_str(key, dst, &tmp_val);
224 rsp_test_tdes_check(struct fips_val *val)
226 struct fips_val *data;
228 if (info.op == FIPS_TEST_ENC_AUTH_GEN)
233 if (memcmp(val->val, data->val, val->len) == 0)
234 fprintf(info.fp_wr, "Success\n");
236 fprintf(info.fp_wr, "Failed\n");
242 parse_test_tdes_init(void)
246 for (i = 0; i < info.nb_vec_lines; i++) {
247 char *line = info.vec[i];
250 if (strstr(line, TEST_CBCI_KEY))
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 =
257 if (strstr(line, TEST_TYPE_ECB_KEY))
258 info.interim_info.tdes_data.test_mode =
261 info.interim_info.tdes_data.test_mode =
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;