examples/fips_validation: support CMAC parsing
[dpdk.git] / examples / fips_validation / fips_validation.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <string.h>
7
8 #include <rte_string_fns.h>
9 #include <rte_cryptodev.h>
10 #include <rte_malloc.h>
11
12 #include "fips_validation.h"
13
14 #define skip_white_spaces(pos)                  \
15 ({                                              \
16         __typeof__(pos) _p = (pos);             \
17         for ( ; isspace(*_p); _p++)             \
18                 ;                               \
19         _p;                                     \
20 })
21
22 static int
23 get_file_line(void)
24 {
25         FILE *fp = info.fp_rd;
26         char *line = info.one_line_text;
27         int ret;
28         uint32_t loc = 0;
29
30         memset(line, 0, MAX_LINE_CHAR);
31         while ((ret = fgetc(fp)) != EOF) {
32                 char c = (char)ret;
33
34                 if (loc >= MAX_LINE_CHAR - 1)
35                         return -ENOMEM;
36                 if (c == '\n')
37                         break;
38                 line[loc++] = c;
39         }
40
41         if (ret == EOF)
42                 return -EOF;
43
44         return 0;
45 }
46
47 int
48 fips_test_fetch_one_block(void)
49 {
50         size_t size;
51         int ret = 0;
52         uint32_t i;
53
54         for (i = 0; i < info.nb_vec_lines; i++) {
55                 free(info.vec[i]);
56                 info.vec[i] = NULL;
57         }
58
59         i = 0;
60         do {
61                 if (i >= MAX_LINE_PER_VECTOR) {
62                         ret = -ENOMEM;
63                         goto error_exit;
64                 }
65
66                 ret = get_file_line();
67                 size = strlen(info.one_line_text);
68                 if (size == 0)
69                         break;
70
71                 info.vec[i] = calloc(1, size + 5);
72                 if (info.vec[i] == NULL)
73                         goto error_exit;
74
75                 strlcpy(info.vec[i], info.one_line_text, size + 1);
76                 i++;
77         } while (ret == 0);
78
79         info.nb_vec_lines = i;
80
81         return ret;
82
83 error_exit:
84         for (i = 0; i < MAX_LINE_PER_VECTOR; i++)
85                 if (info.vec[i] != NULL) {
86                         free(info.vec[i]);
87                         info.vec[i] = NULL;
88                 }
89
90         info.nb_vec_lines = 0;
91
92         return -ENOMEM;
93 }
94
95 static int
96 fips_test_parse_header(void)
97 {
98         uint32_t i;
99         char *tmp;
100         int ret;
101         time_t t = time(NULL);
102         struct tm *tm_now = localtime(&t);
103
104         ret = fips_test_fetch_one_block();
105         if (ret < 0)
106                 return ret;
107
108         for (i = 0; i < info.nb_vec_lines; i++) {
109                 if (strstr(info.vec[i], "AESVS")) {
110                         info.algo = FIPS_TEST_ALGO_AES;
111                         ret = parse_test_aes_init();
112                         if (ret < 0)
113                                 return ret;
114                 } else if (strstr(info.vec[i], "GCM")) {
115                         info.algo = FIPS_TEST_ALGO_AES_GCM;
116                         ret = parse_test_gcm_init();
117                         if (ret < 0)
118                                 return ret;
119                 } else if (strstr(info.vec[i], "CMAC")) {
120                         info.algo = FIPS_TEST_ALGO_AES_CMAC;
121                         ret = parse_test_cmac_init();
122                         if (ret < 0)
123                                 return 0;
124                 } else if (strstr(info.vec[i], "HMAC")) {
125                         info.algo = FIPS_TEST_ALGO_HMAC;
126                         ret = parse_test_hmac_init();
127                         if (ret < 0)
128                                 return ret;
129                 } else if (strstr(info.vec[i], "TDES")) {
130                         info.algo = FIPS_TEST_ALGO_TDES;
131                         ret = parse_test_tdes_init();
132                         if (ret < 0)
133                                 return 0;
134                 }
135
136                 tmp = strstr(info.vec[i], "# Config info for ");
137                 if (tmp != NULL) {
138                         fprintf(info.fp_wr, "%s%s\n", "# Config info for DPDK Cryptodev ",
139                                         info.device_name);
140                         continue;
141                 }
142
143                 tmp = strstr(info.vec[i], "#  HMAC information for ");
144                 if (tmp != NULL) {
145                         fprintf(info.fp_wr, "%s%s\n", "#  HMAC information for "
146                                 "DPDK Cryptodev ",
147                                 info.device_name);
148                         continue;
149                 }
150
151                 tmp = strstr(info.vec[i], "# Config Info for : ");
152                 if (tmp != NULL) {
153
154                         fprintf(info.fp_wr, "%s%s\n", "# Config Info for DPDK Cryptodev : ",
155                                         info.device_name);
156                         continue;
157                 }
158
159                 tmp = strstr(info.vec[i], "# information for ");
160                 if (tmp != NULL) {
161
162                         char tmp_output[128] = {0};
163
164                         strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
165
166                         fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
167                                         "information for DPDK Cryptodev ",
168                                         info.device_name);
169                         continue;
170                 }
171
172                 tmp = strstr(info.vec[i], " test information for ");
173                 if (tmp != NULL) {
174                         char tmp_output[128] = {0};
175
176                         strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
177
178                         fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
179                                         "test information for DPDK Cryptodev ",
180                                         info.device_name);
181                         continue;
182                 }
183
184                 if (i == info.nb_vec_lines - 1) {
185                         /** update the time as current time, write to file */
186                         fprintf(info.fp_wr, "%s%s\n", "# Generated on ",
187                                         asctime(tm_now));
188                         continue;
189                 }
190
191                 /* to this point, no field need to update,
192                  *  only copy to rsp file
193                  */
194                 fprintf(info.fp_wr, "%s\n", info.vec[i]);
195         }
196
197         return 0;
198 }
199
200 static int
201 parse_file_type(const char *path)
202 {
203         const char *tmp = path + strlen(path) - 3;
204
205         if (strstr(tmp, REQ_FILE_PERFIX))
206                 info.file_type = FIPS_TYPE_REQ;
207         else if (strstr(tmp, RSP_FILE_PERFIX))
208                 info.file_type = FIPS_TYPE_RSP;
209         else if (strstr(path, FAX_FILE_PERFIX))
210                 info.file_type = FIPS_TYPE_FAX;
211         else
212                 return -EINVAL;
213
214         return 0;
215 }
216
217 int
218 fips_test_init(const char *req_file_path, const char *rsp_file_path,
219                 const char *device_name)
220 {
221         if (strcmp(req_file_path, rsp_file_path) == 0) {
222                 RTE_LOG(ERR, USER1, "File paths cannot be the same\n");
223                 return -EINVAL;
224         }
225
226         fips_test_clear();
227
228         info.algo = FIPS_TEST_ALGO_MAX;
229         if (parse_file_type(req_file_path) < 0) {
230                 RTE_LOG(ERR, USER1, "File %s type not supported\n",
231                                 req_file_path);
232                 return -EINVAL;
233         }
234
235         info.fp_rd = fopen(req_file_path, "r");
236         if (!info.fp_rd) {
237                 RTE_LOG(ERR, USER1, "Cannot open file %s\n", req_file_path);
238                 return -EINVAL;
239         }
240
241         info.fp_wr = fopen(rsp_file_path, "w");
242         if (!info.fp_wr) {
243                 RTE_LOG(ERR, USER1, "Cannot open file %s\n", rsp_file_path);
244                 return -EINVAL;
245         }
246
247         info.one_line_text = calloc(1, MAX_LINE_CHAR);
248         if (!info.one_line_text) {
249                 RTE_LOG(ERR, USER1, "Insufficient memory\n");
250                 return -ENOMEM;
251         }
252
253         strlcpy(info.device_name, device_name, sizeof(info.device_name));
254
255         if (fips_test_parse_header() < 0) {
256                 RTE_LOG(ERR, USER1, "Failed parsing header\n");
257                 return -1;
258         }
259
260         return 0;
261 }
262
263 void
264 fips_test_clear(void)
265 {
266         if (info.fp_rd)
267                 fclose(info.fp_rd);
268         if (info.fp_wr)
269                 fclose(info.fp_wr);
270         if (info.one_line_text)
271                 free(info.one_line_text);
272         if (info.nb_vec_lines) {
273                 uint32_t i;
274
275                 for (i = 0; i < info.nb_vec_lines; i++)
276                         free(info.vec[i]);
277         }
278
279         memset(&info, 0, sizeof(info));
280 }
281
282 int
283 fips_test_parse_one_case(void)
284 {
285         uint32_t i, j = 0;
286         uint32_t is_interim = 0;
287         int ret;
288
289         if (info.interim_callbacks) {
290                 for (i = 0; i < info.nb_vec_lines; i++) {
291                         for (j = 0; info.interim_callbacks[j].key != NULL; j++)
292                                 if (strstr(info.vec[i],
293                                         info.interim_callbacks[j].key)) {
294                                         is_interim = 1;
295
296                                         ret = info.interim_callbacks[j].cb(
297                                                 info.interim_callbacks[j].key,
298                                                 info.vec[i],
299                                                 info.interim_callbacks[j].val);
300                                         if (ret < 0)
301                                                 return ret;
302                                 }
303                 }
304         }
305
306         if (is_interim) {
307                 for (i = 0; i < info.nb_vec_lines; i++)
308                         fprintf(info.fp_wr, "%s\n", info.vec[i]);
309                 fprintf(info.fp_wr, "\n");
310                 return 1;
311         }
312
313         for (i = 0; i < info.nb_vec_lines; i++) {
314                 for (j = 0; info.callbacks[j].key != NULL; j++)
315                         if (strstr(info.vec[i], info.callbacks[j].key)) {
316                                 ret = info.callbacks[j].cb(
317                                         info.callbacks[j].key,
318                                         info.vec[i], info.callbacks[j].val);
319                                 if (ret < 0)
320                                         return ret;
321                                 break;
322                         }
323         }
324
325         return 0;
326 }
327
328 void
329 fips_test_write_one_case(void)
330 {
331         uint32_t i;
332
333         for (i = 0; i < info.nb_vec_lines; i++)
334                 fprintf(info.fp_wr, "%s\n", info.vec[i]);
335 }
336
337 static int
338 parser_read_uint64_hex(uint64_t *value, const char *p)
339 {
340         char *next;
341         uint64_t val;
342
343         p = skip_white_spaces(p);
344
345         val = strtoul(p, &next, 16);
346         if (p == next)
347                 return -EINVAL;
348
349         p = skip_white_spaces(next);
350         if (*p != '\0')
351                 return -EINVAL;
352
353         *value = val;
354         return 0;
355 }
356
357 int
358 parser_read_uint8_hex(uint8_t *value, const char *p)
359 {
360         uint64_t val = 0;
361         int ret = parser_read_uint64_hex(&val, p);
362
363         if (ret < 0)
364                 return ret;
365
366         if (val > UINT8_MAX)
367                 return -ERANGE;
368
369         *value = val;
370         return 0;
371 }
372
373 int
374 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val)
375 {
376         struct fips_val tmp_val = {0};
377         uint32_t len = val->len;
378         int ret;
379
380         if (len == 0) {
381                 if (val->val != NULL) {
382                         rte_free(val->val);
383                         val->val = NULL;
384                 }
385
386                 return 0;
387         }
388
389         ret = parse_uint8_hex_str(key, src, &tmp_val);
390         if (ret < 0)
391                 return ret;
392
393         if (tmp_val.len == val->len) {
394                 val->val = tmp_val.val;
395                 return 0;
396         }
397
398         if (tmp_val.len < val->len) {
399                 rte_free(tmp_val.val);
400                 return -EINVAL;
401         }
402
403         val->val = rte_zmalloc(NULL, val->len, 0);
404         if (!val->val) {
405                 rte_free(tmp_val.val);
406                 memset(val, 0, sizeof(*val));
407                 return -ENOMEM;
408         }
409
410         memcpy(val->val, tmp_val.val, val->len);
411         rte_free(tmp_val.val);
412
413         return 0;
414 }
415
416 int
417 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val)
418 {
419         uint32_t len, j;
420
421         src += strlen(key);
422
423         len = strlen(src) / 2;
424
425         if (val->val) {
426                 rte_free(val->val);
427                 val->val = NULL;
428         }
429
430         val->val = rte_zmalloc(NULL, len, 0);
431         if (!val->val)
432                 return -ENOMEM;
433
434         for (j = 0; j < len; j++) {
435                 char byte[3] = {src[j * 2], src[j * 2 + 1], '\0'};
436
437                 if (parser_read_uint8_hex(&val->val[j], byte) < 0) {
438                         rte_free(val->val);
439                         memset(val, 0, sizeof(*val));
440                         return -EINVAL;
441                 }
442         }
443
444         val->len = len;
445
446         return 0;
447 }
448
449 int
450 parser_read_uint32_val(const char *key, char *src, struct fips_val *val)
451 {
452         char *data = src + strlen(key);
453         size_t data_len = strlen(data);
454         int ret;
455
456         if (data[data_len - 1] == ']') {
457                 char *tmp_data = calloc(1, data_len + 1);
458
459                 if (tmp_data == NULL)
460                         return -ENOMEM;
461
462                 strlcpy(tmp_data, data, data_len);
463
464                 ret = parser_read_uint32(&val->len, tmp_data);
465
466                 free(tmp_data);
467         } else
468                 ret = parser_read_uint32(&val->len, data);
469
470         return ret;
471 }
472
473 int
474 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val)
475 {
476         int ret;
477
478         ret = parser_read_uint32_val(key, src, val);
479
480         if (ret < 0)
481                 return ret;
482
483         val->len /= 8;
484
485         return 0;
486 }
487
488 int
489 writeback_hex_str(const char *key, char *dst, struct fips_val *val)
490 {
491         char *str = dst;
492         uint32_t len;
493
494         str += strlen(key);
495
496         for (len = 0; len < val->len; len++)
497                 snprintf(str + len * 2, 255, "%02x", val->val[len]);
498
499         return 0;
500 }
501
502 static int
503 parser_read_uint64(uint64_t *value, const char *p)
504 {
505         char *next;
506         uint64_t val;
507
508         p = skip_white_spaces(p);
509         if (!isdigit(*p))
510                 return -EINVAL;
511
512         val = strtoul(p, &next, 10);
513         if (p == next)
514                 return -EINVAL;
515
516         p = next;
517         switch (*p) {
518         case 'T':
519                 val *= 1024ULL;
520                 /* fall through */
521         case 'G':
522                 val *= 1024ULL;
523                 /* fall through */
524         case 'M':
525                 val *= 1024ULL;
526                 /* fall through */
527         case 'k':
528         case 'K':
529                 val *= 1024ULL;
530                 p++;
531                 break;
532         }
533
534         p = skip_white_spaces(p);
535         if (*p != '\0')
536                 return -EINVAL;
537
538         *value = val;
539         return 0;
540 }
541
542 int
543 parser_read_uint32(uint32_t *value, char *p)
544 {
545         uint64_t val = 0;
546         int ret = parser_read_uint64(&val, p);
547
548         if (ret < 0)
549                 return ret;
550
551         if (val > UINT32_MAX)
552                 return -EINVAL;
553
554         *value = val;
555         return 0;
556 }
557
558 void
559 parse_write_hex_str(struct fips_val *src)
560 {
561         writeback_hex_str("", info.one_line_text, src);
562
563         fprintf(info.fp_wr, "%s\n", info.one_line_text);
564 }
565
566 int
567 update_info_vec(uint32_t count)
568 {
569         const struct fips_test_callback *cb;
570         uint32_t i, j;
571
572         if (!info.writeback_callbacks)
573                 return -1;
574
575         cb = &info.writeback_callbacks[0];
576
577         snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key, count);
578
579         for (i = 1; i < info.nb_vec_lines; i++) {
580                 for (j = 1; info.writeback_callbacks[j].key != NULL; j++) {
581                         cb = &info.writeback_callbacks[j];
582                         if (strstr(info.vec[i], cb->key)) {
583                                 cb->cb(cb->key, info.vec[i], cb->val);
584                                 break;
585                         }
586                 }
587         }
588
589         return 0;
590 }