1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (C) 2020 Marvell International Ltd.
3 */
4
5 #include <string.h>
6 #include <stdio.h>
7 #include <time.h>
8
9 #include <rte_cryptodev.h>
10
11 #include "fips_validation.h"
12
13 #define MODE_STR "XTS"
14 #define ALGO_STR "test data for "
15 #define OP_STR "State"
16 #define KEY_SIZE_STR "Key Length : "
17
18 #define COUNT_STR "COUNT = "
19 #define KEY_STR "Key = "
20 #define IV_STR "i = "
21 #define PT_STR "PT = "
22 #define CT_STR "CT = "
23
24 #define OP_ENC_STR "ENCRYPT"
25 #define OP_DEC_STR "DECRYPT"
26
27 #define ALGO_JSON_STR "algorithm"
28 #define TESTTYPE_JSON_STR "testType"
29 #define DIR_JSON_STR "direction"
30 #define KEYLEN_JSON_STR "keyLen"
31 #define TWEAKMODE_JSON_STR "tweakMode"
32
33 #define KEY_JSON_STR "key"
34 #define DATAUNITLEN_JSON_STR "dataUnitLen"
35 #define PAYLOADLEN_JSON_STR "payloadLen"
36 #define TWEAKVALUE_JSON_STR "tweakValue"
37 #define SEQNUMBER_JSON_STR "sequenceNumber"
38 #define PT_JSON_STR "pt"
39 #define CT_JSON_STR "ct"
40
41 #define OP_ENC_JSON_STR "encrypt"
42 #define OP_DEC_JSON_STR "decrypt"
43
44 static int
parse_interim_xts_enc_dec(const char * key,__rte_unused char * text,__rte_unused struct fips_val * val)45 parse_interim_xts_enc_dec(const char *key,
46 __rte_unused char *text,
47 __rte_unused struct fips_val *val)
48 {
49 if (strcmp(key, OP_ENC_STR) == 0)
50 info.op = FIPS_TEST_ENC_AUTH_GEN;
51 else if (strcmp(key, OP_DEC_STR) == 0)
52 info.op = FIPS_TEST_DEC_AUTH_VERIF;
53 else
54 return -1;
55 return 0;
56 }
57
58 struct fips_test_callback xts_tests_vectors[] = {
59 {KEY_STR, parse_uint8_hex_str, &vec.cipher_auth.key},
60 {IV_STR, parse_uint8_hex_str, &vec.iv},
61 {PT_STR, parse_uint8_hex_str, &vec.pt},
62 {CT_STR, parse_uint8_hex_str, &vec.ct},
63 {NULL, NULL, NULL} /**< end pointer */
64 };
65
66 struct fips_test_callback xts_tests_interim_vectors[] = {
67 {OP_ENC_STR, parse_interim_xts_enc_dec, NULL},
68 {OP_DEC_STR, parse_interim_xts_enc_dec, NULL},
69 {NULL, NULL, NULL} /**< end pointer */
70 };
71
72 struct fips_test_callback xts_writeback_callbacks[] = {
73 /** First element is used to pass COUNT string */
74 {COUNT_STR, NULL, NULL},
75 {IV_STR, writeback_hex_str, &vec.iv},
76 {KEY_STR, writeback_hex_str, &vec.cipher_auth.key},
77 {PT_STR, writeback_hex_str, &vec.pt},
78 {CT_STR, writeback_hex_str, &vec.ct},
79 {NULL, NULL, NULL} /**< end pointer */
80 };
81
82 #ifdef USE_JANSSON
83 static int
parser_xts_read_keylen(const char * key,char * src,struct fips_val * val)84 parser_xts_read_keylen(const char *key, char *src, struct fips_val *val)
85 {
86 int ret;
87
88 ret = parser_read_uint32_bit_val(key, src, val);
89 if (ret < 0)
90 return ret;
91
92 val->len *= 2;
93 return 0;
94 }
95
96 static int
parser_xts_read_tweakval(const char * key,char * src,struct fips_val * val)97 parser_xts_read_tweakval(const char *key, char *src, struct fips_val *val)
98 {
99 char num_str[4] = {0};
100 int ret;
101
102 if (info.interim_info.xts_data.tweak_mode == XTS_TWEAK_MODE_HEX) {
103 ret = parse_uint8_hex_str(key, src, val);
104 } else if (info.interim_info.xts_data.tweak_mode == XTS_TWEAK_MODE_NUMBER) {
105 snprintf(num_str, RTE_DIM(num_str), "%x", atoi(src));
106 ret = parse_uint8_hex_str(key, num_str, val);
107 } else {
108 ret = -1;
109 }
110
111 return ret;
112 }
113
114 struct fips_test_callback xts_dec_json_vectors[] = {
115 {KEY_JSON_STR, parse_uint8_known_len_hex_str, &vec.cipher_auth.key},
116 {TWEAKVALUE_JSON_STR, parser_xts_read_tweakval, &vec.iv},
117 {CT_JSON_STR, parse_uint8_hex_str, &vec.ct},
118 {NULL, NULL, NULL} /**< end pointer */
119 };
120
121 struct fips_test_callback xts_interim_json_vectors[] = {
122 {KEYLEN_JSON_STR, parser_xts_read_keylen, &vec.cipher_auth.key},
123 {NULL, NULL, NULL} /**< end pointer */
124 };
125
126 struct fips_test_callback xts_enc_json_vectors[] = {
127 {KEY_JSON_STR, parse_uint8_known_len_hex_str, &vec.cipher_auth.key},
128 {TWEAKVALUE_JSON_STR, parser_xts_read_tweakval, &vec.iv},
129 {SEQNUMBER_JSON_STR, parser_xts_read_tweakval, &vec.iv},
130 {PT_JSON_STR, parse_uint8_hex_str, &vec.pt},
131 {NULL, NULL, NULL} /**< end pointer */
132 };
133
134 static int
parse_test_xts_json_writeback(struct fips_val * val)135 parse_test_xts_json_writeback(struct fips_val *val)
136 {
137 struct fips_val tmp_val;
138 json_t *tcId;
139
140 tcId = json_object_get(json_info.json_test_case, "tcId");
141
142 json_info.json_write_case = json_object();
143 json_object_set(json_info.json_write_case, "tcId", tcId);
144
145 if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
146 json_t *ct;
147
148 tmp_val.val = val->val;
149 tmp_val.len = vec.pt.len;
150
151 writeback_hex_str("", info.one_line_text, &tmp_val);
152 ct = json_string(info.one_line_text);
153 json_object_set_new(json_info.json_write_case, CT_JSON_STR, ct);
154
155 tmp_val.val = val->val + vec.pt.len;
156 tmp_val.len = val->len - vec.pt.len;
157
158 writeback_hex_str("", info.one_line_text, &tmp_val);
159 } else {
160 if (vec.status == RTE_CRYPTO_OP_STATUS_SUCCESS) {
161 tmp_val.val = val->val;
162 tmp_val.len = vec.ct.len;
163
164 writeback_hex_str("", info.one_line_text, &tmp_val);
165 json_object_set_new(json_info.json_write_case, PT_JSON_STR,
166 json_string(info.one_line_text));
167 } else {
168 json_object_set_new(json_info.json_write_case, "testPassed", json_false());
169 }
170 }
171
172 return 0;
173 }
174
175 int
parse_test_xts_json_init(void)176 parse_test_xts_json_init(void)
177 {
178 if (json_info.json_test_group) {
179 json_t *direction_obj, *tweakmode_obj;
180 const char *direction_str, *tweakmode_str;
181
182 direction_obj = json_object_get(json_info.json_test_group, DIR_JSON_STR);
183 direction_str = json_string_value(direction_obj);
184
185 if (strcmp(direction_str, OP_ENC_JSON_STR) == 0) {
186 info.op = FIPS_TEST_ENC_AUTH_GEN;
187 info.callbacks = xts_enc_json_vectors;
188
189 } else if (strcmp(direction_str, OP_DEC_JSON_STR) == 0) {
190 info.op = FIPS_TEST_DEC_AUTH_VERIF;
191 info.callbacks = xts_dec_json_vectors;
192 } else {
193 return -EINVAL;
194 }
195
196 tweakmode_obj = json_object_get(json_info.json_test_group, TWEAKMODE_JSON_STR);
197 tweakmode_str = json_string_value(tweakmode_obj);
198 if (strcmp(tweakmode_str, "hex") == 0)
199 info.interim_info.xts_data.tweak_mode = XTS_TWEAK_MODE_HEX;
200 else
201 info.interim_info.xts_data.tweak_mode = XTS_TWEAK_MODE_NUMBER;
202
203 info.interim_callbacks = xts_interim_json_vectors;
204 }
205
206 info.parse_writeback = parse_test_xts_json_writeback;
207 return 0;
208 }
209 #endif /* USE_JANSSON */
210
211 static int
parse_test_xts_writeback(struct fips_val * val)212 parse_test_xts_writeback(struct fips_val *val)
213 {
214 if (info.op == FIPS_TEST_ENC_AUTH_GEN)
215 fprintf(info.fp_wr, "%s", CT_STR);
216 else
217 fprintf(info.fp_wr, "%s", PT_STR);
218
219 parse_write_hex_str(val);
220 return 0;
221 }
222
223 static int
rsp_test_xts_check(struct fips_val * val)224 rsp_test_xts_check(struct fips_val *val)
225 {
226 struct fips_val *data;
227 if (info.op == FIPS_TEST_ENC_AUTH_GEN)
228 data = &vec.ct;
229 else
230 data = &vec.pt;
231
232 if (memcmp(val->val, data->val, val->len) == 0)
233 fprintf(info.fp_wr, "Success\n");
234 else
235 fprintf(info.fp_wr, "Failed\n");
236 return 0;
237 }
238
parse_test_xts_init(void)239 int parse_test_xts_init(void)
240 {
241 char *tmp;
242 uint32_t i;
243 for (i = 0; i < info.nb_vec_lines; i++) {
244 char *line = info.vec[i];
245 tmp = strstr(line, KEY_SIZE_STR);
246 if (tmp) {
247 tmp += (strlen(KEY_SIZE_STR) + strlen("AES"));
248 if (parser_read_uint32(
249 &info.interim_info.aes_data.key_len,
250 tmp) < 0)
251 return -EINVAL;
252 info.interim_info.aes_data.key_len =
253 (info.interim_info.aes_data.key_len*2) / 8;
254 continue;
255 }
256
257 }
258 info.parse_writeback = parse_test_xts_writeback;
259 info.callbacks = xts_tests_vectors;
260 info.interim_callbacks = xts_tests_interim_vectors;
261 info.writeback_callbacks = xts_writeback_callbacks;
262 info.kat_check = rsp_test_xts_check;
263
264 return 0;
265 }
266