common/cnxk: allow building for generic arm64
[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 void
96 fips_test_parse_version(void)
97 {
98         int len = strlen(info.vec[0]);
99         char *ptr = info.vec[0];
100
101         info.version = strtof(ptr + len - 4, NULL);
102 }
103
104 static int
105 fips_test_parse_header(void)
106 {
107         uint32_t i;
108         char *tmp;
109         int ret;
110         int algo_parsed = 0;
111         time_t t = time(NULL);
112         struct tm *tm_now = localtime(&t);
113
114         ret = fips_test_fetch_one_block();
115         if (ret < 0)
116                 return ret;
117
118         if (info.nb_vec_lines)
119                 fips_test_parse_version();
120
121         for (i = 0; i < info.nb_vec_lines; i++) {
122                 if (!algo_parsed) {
123                         if (strstr(info.vec[i], "AES")) {
124                                 algo_parsed = 1;
125                                 info.algo = FIPS_TEST_ALGO_AES;
126                                 ret = parse_test_aes_init();
127                                 if (ret < 0)
128                                         return ret;
129                         } else if (strstr(info.vec[i], "GCM")) {
130                                 algo_parsed = 1;
131                                 info.algo = FIPS_TEST_ALGO_AES_GCM;
132                                 ret = parse_test_gcm_init();
133                                 if (ret < 0)
134                                         return ret;
135                         } else if (strstr(info.vec[i], "CMAC")) {
136                                 algo_parsed = 1;
137                                 info.algo = FIPS_TEST_ALGO_AES_CMAC;
138                                 ret = parse_test_cmac_init();
139                                 if (ret < 0)
140                                         return 0;
141                         } else if (strstr(info.vec[i], "CCM")) {
142                                 algo_parsed = 1;
143                                 info.algo = FIPS_TEST_ALGO_AES_CCM;
144                                 ret = parse_test_ccm_init();
145                                 if (ret < 0)
146                                         return 0;
147                         } else if (strstr(info.vec[i], "HMAC")) {
148                                 algo_parsed = 1;
149                                 info.algo = FIPS_TEST_ALGO_HMAC;
150                                 ret = parse_test_hmac_init();
151                                 if (ret < 0)
152                                         return ret;
153                         } else if (strstr(info.vec[i], "TDES")) {
154                                 algo_parsed = 1;
155                                 info.algo = FIPS_TEST_ALGO_TDES;
156                                 ret = parse_test_tdes_init();
157                                 if (ret < 0)
158                                         return 0;
159                         } else if (strstr(info.vec[i], "PERMUTATION")) {
160                                 algo_parsed = 1;
161                                 info.algo = FIPS_TEST_ALGO_TDES;
162                                 ret = parse_test_tdes_init();
163                                 if (ret < 0)
164                                         return 0;
165                         } else if (strstr(info.vec[i], "VARIABLE")) {
166                                 algo_parsed = 1;
167                                 info.algo = FIPS_TEST_ALGO_TDES;
168                                 ret = parse_test_tdes_init();
169                                 if (ret < 0)
170                                         return 0;
171                         } else if (strstr(info.vec[i], "SUBSTITUTION")) {
172                                 algo_parsed = 1;
173                                 info.algo = FIPS_TEST_ALGO_TDES;
174                                 ret = parse_test_tdes_init();
175                                 if (ret < 0)
176                                         return 0;
177                         } else if (strstr(info.vec[i], "SHA-")) {
178                                 algo_parsed = 1;
179                                 info.algo = FIPS_TEST_ALGO_SHA;
180                                 ret = parse_test_sha_init();
181                                 if (ret < 0)
182                                         return ret;
183                         } else if (strstr(info.vec[i], "XTS")) {
184                                 algo_parsed = 1;
185                                 info.algo = FIPS_TEST_ALGO_AES_XTS;
186                                 ret = parse_test_xts_init();
187                                 if (ret < 0)
188                                         return ret;
189                         }
190                 }
191
192                 tmp = strstr(info.vec[i], "# Config info for ");
193                 if (tmp != NULL) {
194                         fprintf(info.fp_wr, "%s%s\n", "# Config info for DPDK Cryptodev ",
195                                         info.device_name);
196                         continue;
197                 }
198
199                 tmp = strstr(info.vec[i], "#  HMAC information for ");
200                 if (tmp != NULL) {
201                         fprintf(info.fp_wr, "%s%s\n", "#  HMAC information for "
202                                 "DPDK Cryptodev ",
203                                 info.device_name);
204                         continue;
205                 }
206
207                 tmp = strstr(info.vec[i], "# Config Info for : ");
208                 if (tmp != NULL) {
209
210                         fprintf(info.fp_wr, "%s%s\n", "# Config Info for DPDK Cryptodev : ",
211                                         info.device_name);
212                         continue;
213                 }
214
215                 tmp = strstr(info.vec[i], "# information for ");
216                 if (tmp != NULL) {
217
218                         char tmp_output[128] = {0};
219
220                         strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
221
222                         fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
223                                         "information for DPDK Cryptodev ",
224                                         info.device_name);
225                         continue;
226                 }
227
228                 tmp = strstr(info.vec[i], " test information for ");
229                 if (tmp != NULL) {
230                         char tmp_output[128] = {0};
231
232                         strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
233
234                         fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
235                                         "test information for DPDK Cryptodev ",
236                                         info.device_name);
237                         continue;
238                 }
239
240                 tmp = strstr(info.vec[i], "\" information for \"");
241                 if (tmp != NULL) {
242                         char tmp_output[128] = {0};
243
244                         strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
245
246                         fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
247                                         "\" information for DPDK Cryptodev ",
248                                         info.device_name);
249                         continue;
250                 }
251
252                 if (i == info.nb_vec_lines - 1) {
253                         /** update the time as current time, write to file */
254                         fprintf(info.fp_wr, "%s%s\n", "# Generated on ",
255                                         asctime(tm_now));
256                         continue;
257                 }
258
259                 /* to this point, no field need to update,
260                  *  only copy to rsp file
261                  */
262                 fprintf(info.fp_wr, "%s\n", info.vec[i]);
263         }
264
265         return 0;
266 }
267
268 static int
269 parse_file_type(const char *path)
270 {
271         const char *tmp = path + strlen(path) - 3;
272
273         if (strstr(tmp, REQ_FILE_PREFIX))
274                 info.file_type = FIPS_TYPE_REQ;
275         else if (strstr(tmp, RSP_FILE_PREFIX))
276                 info.file_type = FIPS_TYPE_RSP;
277         else if (strstr(path, FAX_FILE_PREFIX))
278                 info.file_type = FIPS_TYPE_FAX;
279         else if (strstr(path, JSON_FILE_PREFIX))
280                 info.file_type = FIPS_TYPE_JSON;
281         else
282                 return -EINVAL;
283
284         return 0;
285 }
286
287 int
288 fips_test_init(const char *req_file_path, const char *rsp_file_path,
289                 const char *device_name)
290 {
291         if (strcmp(req_file_path, rsp_file_path) == 0) {
292                 RTE_LOG(ERR, USER1, "File paths cannot be the same\n");
293                 return -EINVAL;
294         }
295
296         fips_test_clear();
297
298         if (rte_strscpy(info.file_name, req_file_path,
299                                 sizeof(info.file_name)) < 0) {
300                 RTE_LOG(ERR, USER1, "Path %s too long\n", req_file_path);
301                 return -EINVAL;
302         }
303         info.algo = FIPS_TEST_ALGO_MAX;
304         if (parse_file_type(req_file_path) < 0) {
305                 RTE_LOG(ERR, USER1, "File %s type not supported\n",
306                                 req_file_path);
307                 return -EINVAL;
308         }
309
310         info.fp_rd = fopen(req_file_path, "r");
311         if (!info.fp_rd) {
312                 RTE_LOG(ERR, USER1, "Cannot open file %s\n", req_file_path);
313                 return -EINVAL;
314         }
315
316         if (info.file_type == FIPS_TYPE_JSON) {
317 #ifdef RTE_HAS_JANSSON
318                 json_error_t error;
319                 json_info.json_root = json_loadf(info.fp_rd, 0, &error);
320                 if (!json_info.json_root) {
321                         RTE_LOG(ERR, USER1, "Cannot parse json file %s (line %d, column %d)\n",
322                                 req_file_path, error.line, error.column);
323                         return -EINVAL;
324                 }
325 #else /* RTE_HAS_JANSSON */
326                 RTE_LOG(ERR, USER1, "No json library configured.\n");
327                 return -EINVAL;
328 #endif /* RTE_HAS_JANSSON */
329         }
330
331         info.fp_wr = fopen(rsp_file_path, "w");
332         if (!info.fp_wr) {
333                 RTE_LOG(ERR, USER1, "Cannot open file %s\n", rsp_file_path);
334                 return -EINVAL;
335         }
336
337         info.one_line_text = calloc(1, MAX_LINE_CHAR);
338         if (!info.one_line_text) {
339                 RTE_LOG(ERR, USER1, "Insufficient memory\n");
340                 return -ENOMEM;
341         }
342
343         if (rte_strscpy(info.device_name, device_name,
344                                 sizeof(info.device_name)) < 0) {
345                 RTE_LOG(ERR, USER1, "Device name %s too long\n", device_name);
346                 return -EINVAL;
347         }
348
349         if (info.file_type == FIPS_TYPE_JSON)
350                 return 0;
351
352         if (fips_test_parse_header() < 0) {
353                 RTE_LOG(ERR, USER1, "Failed parsing header\n");
354                 return -1;
355         }
356
357         return 0;
358 }
359
360 void
361 fips_test_clear(void)
362 {
363         if (info.fp_rd)
364                 fclose(info.fp_rd);
365         if (info.fp_wr)
366                 fclose(info.fp_wr);
367         free(info.one_line_text);
368         if (info.nb_vec_lines) {
369                 uint32_t i;
370
371                 for (i = 0; i < info.nb_vec_lines; i++)
372                         free(info.vec[i]);
373         }
374
375         memset(&info, 0, sizeof(info));
376 }
377
378 int
379 fips_test_parse_one_case(void)
380 {
381         uint32_t i, j = 0;
382         uint32_t is_interim;
383         uint32_t interim_cnt = 0;
384         int ret;
385
386         info.vec_start_off = 0;
387
388         if (info.interim_callbacks) {
389                 for (i = 0; i < info.nb_vec_lines; i++) {
390                         is_interim = 0;
391                         for (j = 0; info.interim_callbacks[j].key != NULL; j++)
392                                 if (strstr(info.vec[i],
393                                         info.interim_callbacks[j].key)) {
394                                         is_interim = 1;
395
396                                         ret = info.interim_callbacks[j].cb(
397                                                 info.interim_callbacks[j].key,
398                                                 info.vec[i],
399                                                 info.interim_callbacks[j].val);
400                                         if (ret < 0)
401                                                 return ret;
402                                 }
403
404                         if (is_interim)
405                                 interim_cnt += 1;
406                 }
407         }
408
409         if (interim_cnt) {
410                 if (info.version == 21.4f) {
411                         for (i = 0; i < interim_cnt; i++)
412                                 fprintf(info.fp_wr, "%s\n", info.vec[i]);
413                         fprintf(info.fp_wr, "\n");
414
415                         if (info.nb_vec_lines == interim_cnt)
416                                 return 1;
417                 } else {
418                         for (i = 0; i < info.nb_vec_lines; i++)
419                                 fprintf(info.fp_wr, "%s\n", info.vec[i]);
420                         fprintf(info.fp_wr, "\n");
421                         return 1;
422                 }
423         }
424
425         info.vec_start_off = interim_cnt;
426
427         for (i = info.vec_start_off; i < info.nb_vec_lines; i++) {
428                 for (j = 0; info.callbacks[j].key != NULL; j++)
429                         if (strstr(info.vec[i], info.callbacks[j].key)) {
430                                 ret = info.callbacks[j].cb(
431                                         info.callbacks[j].key,
432                                         info.vec[i], info.callbacks[j].val);
433                                 if (ret < 0)
434                                         return ret;
435                                 break;
436                         }
437         }
438
439         return 0;
440 }
441
442 void
443 fips_test_write_one_case(void)
444 {
445         uint32_t i;
446
447         for (i = info.vec_start_off; i < info.nb_vec_lines; i++)
448                 fprintf(info.fp_wr, "%s\n", info.vec[i]);
449 }
450
451 #ifdef RTE_HAS_JANSSON
452 int
453 fips_test_parse_one_json_vector_set(void)
454 {
455         json_t *algo_obj = json_object_get(json_info.json_vector_set, "algorithm");
456         const char *algo_str = json_string_value(algo_obj);
457
458         /* Vector sets contain the algorithm type, and nothing else we need. */
459         if (strstr(algo_str, "AES-GCM"))
460                 info.algo = FIPS_TEST_ALGO_AES_GCM;
461         else if (strstr(algo_str, "HMAC"))
462                 info.algo = FIPS_TEST_ALGO_HMAC;
463         else if (strstr(algo_str, "CMAC"))
464                 info.algo = FIPS_TEST_ALGO_AES_CMAC;
465         else if (strstr(algo_str, "AES-CBC"))
466                 info.algo = FIPS_TEST_ALGO_AES;
467         else
468                 return -EINVAL;
469
470         return 0;
471 }
472
473 int
474 fips_test_parse_one_json_group(void)
475 {
476         int ret, i;
477         json_t *param;
478
479         if (info.interim_callbacks) {
480                 char json_value[256];
481                 for (i = 0; info.interim_callbacks[i].key != NULL; i++) {
482                         param = json_object_get(json_info.json_test_group,
483                                         info.interim_callbacks[i].key);
484                         switch (json_typeof(param)) {
485                         case JSON_STRING:
486                                 snprintf(json_value, 256, "%s", json_string_value(param));
487                                 break;
488
489                         case JSON_INTEGER:
490                                 snprintf(json_value, 255, "%"JSON_INTEGER_FORMAT,
491                                                 json_integer_value(param));
492                                 break;
493
494                         default:
495                                 return -EINVAL;
496                         }
497
498                         /* First argument is blank because the key
499                          * is not included in the string being parsed.
500                          */
501                         ret = info.interim_callbacks[i].cb(
502                                 "", json_value,
503                                 info.interim_callbacks[i].val
504                         );
505                         if (ret < 0)
506                                 return ret;
507                 }
508         }
509
510         return 0;
511 }
512
513 int
514 fips_test_parse_one_json_case(void)
515 {
516         uint32_t i;
517         int ret = 0;
518         json_t *param;
519
520         for (i = 0; info.callbacks[i].key != NULL; i++) {
521                 param = json_object_get(json_info.json_test_case, info.callbacks[i].key);
522                 if (param) {
523                         strcpy(info.one_line_text, json_string_value(param));
524                         /* First argument is blank because the key
525                          * is not included in the string being parsed.
526                          */
527                         ret = info.callbacks[i].cb(
528                                 "", info.one_line_text,
529                                 info.callbacks[i].val
530                         );
531                         if (ret < 0)
532                                 return ret;
533                 }
534         }
535
536         return 0;
537 }
538 #endif /* RTE_HAS_JANSSON */
539
540 static int
541 parser_read_uint64_hex(uint64_t *value, const char *p)
542 {
543         char *next;
544         uint64_t val;
545
546         p = skip_white_spaces(p);
547
548         val = strtoul(p, &next, 16);
549         if (p == next)
550                 return -EINVAL;
551
552         p = skip_white_spaces(next);
553         if (*p != '\0')
554                 return -EINVAL;
555
556         *value = val;
557         return 0;
558 }
559
560 int
561 parser_read_uint8_hex(uint8_t *value, const char *p)
562 {
563         uint64_t val = 0;
564         int ret = parser_read_uint64_hex(&val, p);
565
566         if (ret < 0)
567                 return ret;
568
569         if (val > UINT8_MAX)
570                 return -ERANGE;
571
572         *value = val;
573         return 0;
574 }
575
576 int
577 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val)
578 {
579         struct fips_val tmp_val = {0};
580         uint32_t len = val->len;
581         int ret;
582
583         if (len == 0) {
584                 if (val->val != NULL) {
585                         rte_free(val->val);
586                         val->val = NULL;
587                 }
588
589                 return 0;
590         }
591
592         ret = parse_uint8_hex_str(key, src, &tmp_val);
593         if (ret < 0)
594                 return ret;
595
596         if (tmp_val.len == val->len) {
597                 val->val = tmp_val.val;
598                 return 0;
599         }
600
601         if (tmp_val.len < val->len) {
602                 rte_free(tmp_val.val);
603                 return -EINVAL;
604         }
605
606         val->val = rte_zmalloc(NULL, val->len, 0);
607         if (!val->val) {
608                 rte_free(tmp_val.val);
609                 memset(val, 0, sizeof(*val));
610                 return -ENOMEM;
611         }
612
613         memcpy(val->val, tmp_val.val, val->len);
614         rte_free(tmp_val.val);
615
616         return 0;
617 }
618
619 int
620 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val)
621 {
622         uint32_t len, j;
623
624         src += strlen(key);
625
626         len = strlen(src) / 2;
627
628         if (val->val) {
629                 rte_free(val->val);
630                 val->val = NULL;
631         }
632
633         val->val = rte_zmalloc(NULL, len, 0);
634         if (!val->val)
635                 return -ENOMEM;
636
637         for (j = 0; j < len; j++) {
638                 char byte[3] = {src[j * 2], src[j * 2 + 1], '\0'};
639
640                 if (parser_read_uint8_hex(&val->val[j], byte) < 0) {
641                         rte_free(val->val);
642                         memset(val, 0, sizeof(*val));
643                         return -EINVAL;
644                 }
645         }
646
647         val->len = len;
648
649         return 0;
650 }
651
652 int
653 parser_read_uint32_val(const char *key, char *src, struct fips_val *val)
654 {
655         char *data = src + strlen(key);
656         size_t data_len = strlen(data);
657         int ret;
658
659         if (data[data_len - 1] == ']') {
660                 char *tmp_data = calloc(1, data_len + 1);
661
662                 if (tmp_data == NULL)
663                         return -ENOMEM;
664
665                 strlcpy(tmp_data, data, data_len);
666
667                 ret = parser_read_uint32(&val->len, tmp_data);
668
669                 free(tmp_data);
670         } else
671                 ret = parser_read_uint32(&val->len, data);
672
673         return ret;
674 }
675
676 int
677 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val)
678 {
679         int ret;
680
681         ret = parser_read_uint32_val(key, src, val);
682
683         if (ret < 0)
684                 return ret;
685
686         val->len /= 8;
687
688         return 0;
689 }
690
691 int
692 writeback_hex_str(const char *key, char *dst, struct fips_val *val)
693 {
694         char *str = dst;
695         uint32_t len;
696
697         str += strlen(key);
698
699         for (len = 0; len < val->len; len++)
700                 snprintf(str + len * 2, 255, "%02x", val->val[len]);
701
702         return 0;
703 }
704
705 static int
706 parser_read_uint64(uint64_t *value, const char *p)
707 {
708         char *next;
709         uint64_t val;
710
711         p = skip_white_spaces(p);
712         if (!isdigit(*p))
713                 return -EINVAL;
714
715         val = strtoul(p, &next, 10);
716         if (p == next)
717                 return -EINVAL;
718
719         p = next;
720         switch (*p) {
721         case 'T':
722                 val *= 1024ULL;
723                 /* fall through */
724         case 'G':
725                 val *= 1024ULL;
726                 /* fall through */
727         case 'M':
728                 val *= 1024ULL;
729                 /* fall through */
730         case 'k':
731         case 'K':
732                 val *= 1024ULL;
733                 p++;
734                 break;
735         }
736
737         p = skip_white_spaces(p);
738         if (*p != '\0')
739                 return -EINVAL;
740
741         *value = val;
742         return 0;
743 }
744
745 int
746 parser_read_uint32(uint32_t *value, char *p)
747 {
748         uint64_t val = 0;
749         int ret = parser_read_uint64(&val, p);
750
751         if (ret < 0)
752                 return ret;
753
754         if (val > UINT32_MAX)
755                 return -EINVAL;
756
757         *value = val;
758         return 0;
759 }
760
761 int
762 parser_read_uint16(uint16_t *value, const char *p)
763 {
764         uint64_t val = 0;
765         int ret = parser_read_uint64(&val, p);
766
767         if (ret < 0)
768                 return ret;
769
770         if (val > UINT16_MAX)
771                 return -ERANGE;
772
773         *value = val;
774         return 0;
775 }
776
777 void
778 parse_write_hex_str(struct fips_val *src)
779 {
780         writeback_hex_str("", info.one_line_text, src);
781
782         fprintf(info.fp_wr, "%s\n", info.one_line_text);
783 }
784
785 int
786 update_info_vec(uint32_t count)
787 {
788         const struct fips_test_callback *cb;
789         uint32_t i, j;
790
791         if (!info.writeback_callbacks)
792                 return -1;
793
794         cb = &info.writeback_callbacks[0];
795
796         if ((info.version == 21.4f) && (!(strstr(info.vec[0], cb->key)))) {
797                 fprintf(info.fp_wr, "%s%u\n", cb->key, count);
798                 i = 0;
799         } else {
800                 snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key,
801                                 count);
802                 i = 1;
803         }
804
805         for (; i < info.nb_vec_lines; i++) {
806                 for (j = 1; info.writeback_callbacks[j].key != NULL; j++) {
807                         cb = &info.writeback_callbacks[j];
808                         if (strstr(info.vec[i], cb->key)) {
809                                 cb->cb(cb->key, info.vec[i], cb->val);
810                                 break;
811                         }
812                 }
813         }
814
815         return 0;
816 }