Branch data Line data Source code
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
45 : 0 : parse_interim_xts_enc_dec(const char *key,
46 : : __rte_unused char *text,
47 : : __rte_unused struct fips_val *val)
48 : : {
49 : 0 : if (strcmp(key, OP_ENC_STR) == 0)
50 : 0 : info.op = FIPS_TEST_ENC_AUTH_GEN;
51 : 0 : else if (strcmp(key, OP_DEC_STR) == 0)
52 : 0 : 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
84 : 0 : parser_xts_read_keylen(const char *key, char *src, struct fips_val *val)
85 : : {
86 : : int ret;
87 : :
88 : 0 : ret = parser_read_uint32_bit_val(key, src, val);
89 : 0 : if (ret < 0)
90 : : return ret;
91 : :
92 : 0 : val->len *= 2;
93 : 0 : return 0;
94 : : }
95 : :
96 : : static int
97 : 0 : parser_xts_read_tweakval(const char *key, char *src, struct fips_val *val)
98 : : {
99 : 0 : char num_str[4] = {0};
100 : : int ret;
101 : :
102 : 0 : if (info.interim_info.xts_data.tweak_mode == XTS_TWEAK_MODE_HEX) {
103 : 0 : ret = parse_uint8_hex_str(key, src, val);
104 : 0 : } 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 : 0 : ret = parse_uint8_hex_str(key, num_str, val);
107 : : } else {
108 : : ret = -1;
109 : : }
110 : :
111 : 0 : 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
135 : 0 : parse_test_xts_json_writeback(struct fips_val *val)
136 : : {
137 : : struct fips_val tmp_val;
138 : : json_t *tcId;
139 : :
140 : 0 : tcId = json_object_get(json_info.json_test_case, "tcId");
141 : :
142 : 0 : json_info.json_write_case = json_object();
143 : 0 : json_object_set(json_info.json_write_case, "tcId", tcId);
144 : :
145 : 0 : if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
146 : : json_t *ct;
147 : :
148 : 0 : tmp_val.val = val->val;
149 : 0 : tmp_val.len = vec.pt.len;
150 : :
151 : 0 : writeback_hex_str("", info.one_line_text, &tmp_val);
152 : 0 : ct = json_string(info.one_line_text);
153 : 0 : json_object_set_new(json_info.json_write_case, CT_JSON_STR, ct);
154 : :
155 : 0 : tmp_val.val = val->val + vec.pt.len;
156 : 0 : tmp_val.len = val->len - vec.pt.len;
157 : :
158 : 0 : writeback_hex_str("", info.one_line_text, &tmp_val);
159 : : } else {
160 : 0 : if (vec.status == RTE_CRYPTO_OP_STATUS_SUCCESS) {
161 : 0 : tmp_val.val = val->val;
162 : 0 : tmp_val.len = vec.ct.len;
163 : :
164 : 0 : writeback_hex_str("", info.one_line_text, &tmp_val);
165 : 0 : json_object_set_new(json_info.json_write_case, PT_JSON_STR,
166 : 0 : json_string(info.one_line_text));
167 : : } else {
168 : 0 : json_object_set_new(json_info.json_write_case, "testPassed", json_false());
169 : : }
170 : : }
171 : :
172 : 0 : return 0;
173 : : }
174 : :
175 : : int
176 : 0 : parse_test_xts_json_init(void)
177 : : {
178 : 0 : if (json_info.json_test_group) {
179 : : json_t *direction_obj, *tweakmode_obj;
180 : : const char *direction_str, *tweakmode_str;
181 : :
182 : 0 : direction_obj = json_object_get(json_info.json_test_group, DIR_JSON_STR);
183 : 0 : direction_str = json_string_value(direction_obj);
184 : :
185 : 0 : if (strcmp(direction_str, OP_ENC_JSON_STR) == 0) {
186 : 0 : info.op = FIPS_TEST_ENC_AUTH_GEN;
187 : 0 : info.callbacks = xts_enc_json_vectors;
188 : :
189 : 0 : } else if (strcmp(direction_str, OP_DEC_JSON_STR) == 0) {
190 : 0 : info.op = FIPS_TEST_DEC_AUTH_VERIF;
191 : 0 : info.callbacks = xts_dec_json_vectors;
192 : : } else {
193 : : return -EINVAL;
194 : : }
195 : :
196 : 0 : tweakmode_obj = json_object_get(json_info.json_test_group, TWEAKMODE_JSON_STR);
197 : 0 : tweakmode_str = json_string_value(tweakmode_obj);
198 : 0 : if (strcmp(tweakmode_str, "hex") == 0)
199 : 0 : info.interim_info.xts_data.tweak_mode = XTS_TWEAK_MODE_HEX;
200 : : else
201 : 0 : info.interim_info.xts_data.tweak_mode = XTS_TWEAK_MODE_NUMBER;
202 : :
203 : 0 : info.interim_callbacks = xts_interim_json_vectors;
204 : : }
205 : :
206 : 0 : info.parse_writeback = parse_test_xts_json_writeback;
207 : 0 : return 0;
208 : : }
209 : : #endif /* USE_JANSSON */
210 : :
211 : : static int
212 : 0 : parse_test_xts_writeback(struct fips_val *val)
213 : : {
214 : 0 : if (info.op == FIPS_TEST_ENC_AUTH_GEN)
215 : 0 : fprintf(info.fp_wr, "%s", CT_STR);
216 : : else
217 : 0 : fprintf(info.fp_wr, "%s", PT_STR);
218 : :
219 : 0 : parse_write_hex_str(val);
220 : 0 : return 0;
221 : : }
222 : :
223 : : static int
224 : 0 : rsp_test_xts_check(struct fips_val *val)
225 : : {
226 : : struct fips_val *data;
227 : 0 : if (info.op == FIPS_TEST_ENC_AUTH_GEN)
228 : : data = &vec.ct;
229 : : else
230 : : data = &vec.pt;
231 : :
232 : 0 : if (memcmp(val->val, data->val, val->len) == 0)
233 : 0 : fprintf(info.fp_wr, "Success\n");
234 : : else
235 : 0 : fprintf(info.fp_wr, "Failed\n");
236 : 0 : return 0;
237 : : }
238 : :
239 : 0 : int parse_test_xts_init(void)
240 : : {
241 : : char *tmp;
242 : : uint32_t i;
243 : 0 : for (i = 0; i < info.nb_vec_lines; i++) {
244 : 0 : char *line = info.vec[i];
245 : 0 : tmp = strstr(line, KEY_SIZE_STR);
246 : 0 : if (tmp) {
247 : 0 : tmp += (strlen(KEY_SIZE_STR) + strlen("AES"));
248 : 0 : if (parser_read_uint32(
249 : : &info.interim_info.aes_data.key_len,
250 : : tmp) < 0)
251 : : return -EINVAL;
252 : 0 : info.interim_info.aes_data.key_len =
253 : 0 : (info.interim_info.aes_data.key_len*2) / 8;
254 : 0 : continue;
255 : : }
256 : :
257 : : }
258 : 0 : info.parse_writeback = parse_test_xts_writeback;
259 : 0 : info.callbacks = xts_tests_vectors;
260 : 0 : info.interim_callbacks = xts_tests_interim_vectors;
261 : 0 : info.writeback_callbacks = xts_writeback_callbacks;
262 : 0 : info.kat_check = rsp_test_xts_check;
263 : :
264 : 0 : return 0;
265 : : }
|