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_CBCI_KEY " for CBCI"
17 #define ENC_STR "[ENCRYPT]"
18 #define DEC_STR "[DECRYPT]"
20 #define COUNT_STR "COUNT = "
21 #define KEY1_STR "KEY1 = "
22 #define KEY2_STR "KEY2 = "
23 #define KEY3_STR "KEY3 = "
25 #define KEYS_STR "KEYs = "
26 #define IV_STR "IV = "
27 #define PT_STR "PLAINTEXT = "
28 #define CT_STR "CIPHERTEXT = "
29 #define NK_STR "NumKeys = "
38 #define DEVICE_STR "# Config Info for : "
44 {TDES_INVERSE_PERMUTATION, "INVERSE PERMUTATION"},
45 {TDES_PERMUTATION, "PERMUTATION OPERATION"},
46 {TDES_SUBSTITUTION_TABLE, "SUBSTITUTION TABLE"},
47 {TDES_VARIABLE_KEY, "VARIABLE KEY"},
48 {TDES_VARIABLE_TEXT, "VARIABLE PLAINTEXT/CIPHERTEXT"},
49 {TDES_VARIABLE_TEXT, "KAT"},
50 {TDES_MCT, "Monte Carlo (Modes) Test"},
51 {TDES_MMT, "Multi block Message Test"},
55 writeback_tdes_hex_str(const char *key, char *dst, struct fips_val *val);
58 parse_tdes_uint8_hex_str(const char *key, char *src, struct fips_val *val);
61 parse_tdes_interim(const char *key,
62 __attribute__((__unused__)) char *text,
63 struct fips_val *val);
65 struct fips_test_callback tdes_tests_vectors[] = {
66 {KEYS_STR, parse_tdes_uint8_hex_str, &vec.cipher_auth.key},
67 {KEY1_STR, parse_tdes_uint8_hex_str, &vec.cipher_auth.key},
68 {KEY2_STR, parse_tdes_uint8_hex_str, &vec.cipher_auth.key},
69 {KEY3_STR, parse_tdes_uint8_hex_str, &vec.cipher_auth.key},
70 {IV_STR, parse_uint8_hex_str, &vec.iv},
71 {PT_STR, parse_uint8_hex_str, &vec.pt},
72 {CT_STR, parse_uint8_hex_str, &vec.ct},
73 {NULL, NULL, NULL} /**< end pointer */
76 struct fips_test_callback tdes_tests_interim_vectors[] = {
77 {ENC_STR, parse_tdes_interim, NULL},
78 {DEC_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,
97 __attribute__((__unused__)) char *text,
98 __attribute__((__unused__)) struct fips_val *val)
100 if (strstr(key, ENC_STR))
101 info.op = FIPS_TEST_ENC_AUTH_GEN;
102 else if (strstr(key, DEC_STR))
103 info.op = FIPS_TEST_DEC_AUTH_VERIF;
104 else if (strstr(NK_STR, "NumKeys = 1"))
105 info.interim_info.tdes_data.nb_keys = 1;
106 else if (strstr(NK_STR, "NumKeys = 2"))
107 info.interim_info.tdes_data.nb_keys = 2;
108 else if (strstr(NK_STR, "NumKeys = 3"))
109 info.interim_info.tdes_data.nb_keys = 3;
117 parse_tdes_uint8_hex_str(const char *key, char *src, struct fips_val *val)
119 uint8_t tmp_key[24] = {0};
124 len = strlen(src) / 2;
127 memcpy(tmp_key, val->val, val->len);
131 val->val = rte_zmalloc(NULL, 24, 0);
135 memcpy(val->val, tmp_key, 24);
137 if (strstr(key, KEYS_STR)) {
138 for (i = 0; i < len; i++) {
139 char byte[3] = {src[i * 2], src[i * 2 + 1], '\0'};
141 if (parser_read_uint8_hex(&val->val[i], byte) < 0)
145 memcpy(val->val + 8, val->val, 8);
146 memcpy(val->val + 16, val->val, 8);
148 } else if (strstr(key, KEY1_STR)) {
149 for (i = 0; i < len; i++) {
150 char byte[3] = {src[i * 2], src[i * 2 + 1], '\0'};
152 if (parser_read_uint8_hex(&val->val[i], byte) < 0)
156 if (info.interim_info.tdes_data.nb_keys == 2)
157 memcpy(val->val + 16, val->val, 8);
159 } else if (strstr(key, KEY2_STR)) {
160 for (i = 0; i < len; i++) {
161 char byte[3] = {src[i * 2], src[i * 2 + 1], '\0'};
163 if (parser_read_uint8_hex(&val->val[i + 8], byte) < 0)
167 } else if (strstr(key, KEY3_STR)) {
168 for (i = 0; i < len; i++) {
169 char byte[3] = {src[i * 2], src[i * 2 + 1], '\0'};
171 if (parser_read_uint8_hex(&val->val[i + 16], byte) < 0)
183 memset(val, 0, sizeof(*val));
188 parse_test_tdes_writeback(struct fips_val *val)
191 if (info.op == FIPS_TEST_ENC_AUTH_GEN)
192 fprintf(info.fp_wr, "%s", CT_STR);
194 fprintf(info.fp_wr, "%s", PT_STR);
196 parse_write_hex_str(val);
203 writeback_tdes_hex_str(const char *key, char *dst, struct fips_val *val)
205 struct fips_val tmp_val = {0};
209 if (strstr(key, KEY1_STR))
210 tmp_val.val = val->val;
211 else if (strstr(key, KEY2_STR))
212 tmp_val.val = val->val + 8;
213 else if (strstr(key, KEY3_STR))
214 tmp_val.val = val->val + 16;
216 return writeback_hex_str(key, dst, &tmp_val);
220 rsp_test_tdes_check(struct fips_val *val)
222 struct fips_val *data;
224 if (info.op == FIPS_TEST_ENC_AUTH_GEN)
229 if (memcmp(val->val, data->val, val->len) == 0)
230 fprintf(info.fp_wr, "Success\n");
232 fprintf(info.fp_wr, "Failed\n");
238 parse_test_tdes_init(void)
242 for (i = 0; i < info.nb_vec_lines; i++) {
243 char *line = info.vec[i];
246 if (strstr(line, TEST_CBCI_KEY))
249 for (j = 0; j < RTE_DIM(test_types); j++)
250 if (strstr(line, test_types[j].desc)) {
251 info.interim_info.tdes_data.test_type =
257 info.parse_writeback = parse_test_tdes_writeback;
258 info.callbacks = tdes_tests_vectors;
259 info.interim_callbacks = tdes_tests_interim_vectors;
260 info.writeback_callbacks = tdes_writeback_callbacks;
261 info.kat_check = rsp_test_tdes_check;