examples/fips_validation: fix buffer overflow
[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         int algo_parsed = 0;
102         time_t t = time(NULL);
103         struct tm *tm_now = localtime(&t);
104
105         ret = fips_test_fetch_one_block();
106         if (ret < 0)
107                 return ret;
108
109         for (i = 0; i < info.nb_vec_lines; i++) {
110                 if (!algo_parsed) {
111                         if (strstr(info.vec[i], "AESVS")) {
112                                 algo_parsed = 1;
113                                 info.algo = FIPS_TEST_ALGO_AES;
114                                 ret = parse_test_aes_init();
115                                 if (ret < 0)
116                                         return ret;
117                         } else if (strstr(info.vec[i], "GCM")) {
118                                 algo_parsed = 1;
119                                 info.algo = FIPS_TEST_ALGO_AES_GCM;
120                                 ret = parse_test_gcm_init();
121                                 if (ret < 0)
122                                         return ret;
123                         } else if (strstr(info.vec[i], "CMAC")) {
124                                 algo_parsed = 1;
125                                 info.algo = FIPS_TEST_ALGO_AES_CMAC;
126                                 ret = parse_test_cmac_init();
127                                 if (ret < 0)
128                                         return 0;
129                         } else if (strstr(info.vec[i], "CCM")) {
130                                 algo_parsed = 1;
131                                 info.algo = FIPS_TEST_ALGO_AES_CCM;
132                                 ret = parse_test_ccm_init();
133                                 if (ret < 0)
134                                         return 0;
135                         } else if (strstr(info.vec[i], "HMAC")) {
136                                 algo_parsed = 1;
137                                 info.algo = FIPS_TEST_ALGO_HMAC;
138                                 ret = parse_test_hmac_init();
139                                 if (ret < 0)
140                                         return ret;
141                         } else if (strstr(info.vec[i], "TDES")) {
142                                 algo_parsed = 1;
143                                 info.algo = FIPS_TEST_ALGO_TDES;
144                                 ret = parse_test_tdes_init();
145                                 if (ret < 0)
146                                         return 0;
147                         } else if (strstr(info.vec[i], "PERMUTATION")) {
148                                 algo_parsed = 1;
149                                 info.algo = FIPS_TEST_ALGO_TDES;
150                                 ret = parse_test_tdes_init();
151                                 if (ret < 0)
152                                         return 0;
153                         } else if (strstr(info.vec[i], "VARIABLE")) {
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], "SUBSTITUTION")) {
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], "SHA-")) {
166                                 algo_parsed = 1;
167                                 info.algo = FIPS_TEST_ALGO_SHA;
168                                 ret = parse_test_sha_init();
169                                 if (ret < 0)
170                                         return ret;
171                         } else if (strstr(info.vec[i], "XTS")) {
172                                 algo_parsed = 1;
173                                 info.algo = FIPS_TEST_ALGO_AES_XTS;
174                                 ret = parse_test_xts_init();
175                                 if (ret < 0)
176                                         return ret;
177                         }
178                 }
179
180                 tmp = strstr(info.vec[i], "# Config info for ");
181                 if (tmp != NULL) {
182                         fprintf(info.fp_wr, "%s%s\n", "# Config info for DPDK Cryptodev ",
183                                         info.device_name);
184                         continue;
185                 }
186
187                 tmp = strstr(info.vec[i], "#  HMAC information for ");
188                 if (tmp != NULL) {
189                         fprintf(info.fp_wr, "%s%s\n", "#  HMAC information for "
190                                 "DPDK Cryptodev ",
191                                 info.device_name);
192                         continue;
193                 }
194
195                 tmp = strstr(info.vec[i], "# Config Info for : ");
196                 if (tmp != NULL) {
197
198                         fprintf(info.fp_wr, "%s%s\n", "# Config Info for DPDK Cryptodev : ",
199                                         info.device_name);
200                         continue;
201                 }
202
203                 tmp = strstr(info.vec[i], "# information for ");
204                 if (tmp != NULL) {
205
206                         char tmp_output[128] = {0};
207
208                         strlcpy(tmp_output, info.vec[i], tmp - info.vec[i] + 1);
209
210                         fprintf(info.fp_wr, "%s%s%s\n", tmp_output,
211                                         "information for DPDK Cryptodev ",
212                                         info.device_name);
213                         continue;
214                 }
215
216                 tmp = strstr(info.vec[i], " test information for ");
217                 if (tmp != NULL) {
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                                         "test information for DPDK Cryptodev ",
224                                         info.device_name);
225                         continue;
226                 }
227
228                 tmp = strstr(info.vec[i], "\" 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                                         "\" information for DPDK Cryptodev ",
236                                         info.device_name);
237                         continue;
238                 }
239
240                 if (i == info.nb_vec_lines - 1) {
241                         /** update the time as current time, write to file */
242                         fprintf(info.fp_wr, "%s%s\n", "# Generated on ",
243                                         asctime(tm_now));
244                         continue;
245                 }
246
247                 /* to this point, no field need to update,
248                  *  only copy to rsp file
249                  */
250                 fprintf(info.fp_wr, "%s\n", info.vec[i]);
251         }
252
253         return 0;
254 }
255
256 static int
257 parse_file_type(const char *path)
258 {
259         const char *tmp = path + strlen(path) - 3;
260
261         if (strstr(tmp, REQ_FILE_PERFIX))
262                 info.file_type = FIPS_TYPE_REQ;
263         else if (strstr(tmp, RSP_FILE_PERFIX))
264                 info.file_type = FIPS_TYPE_RSP;
265         else if (strstr(path, FAX_FILE_PERFIX))
266                 info.file_type = FIPS_TYPE_FAX;
267         else
268                 return -EINVAL;
269
270         return 0;
271 }
272
273 int
274 fips_test_init(const char *req_file_path, const char *rsp_file_path,
275                 const char *device_name)
276 {
277         if (strcmp(req_file_path, rsp_file_path) == 0) {
278                 RTE_LOG(ERR, USER1, "File paths cannot be the same\n");
279                 return -EINVAL;
280         }
281
282         fips_test_clear();
283
284         if (rte_strscpy(info.file_name, req_file_path,
285                                 sizeof(info.file_name)) < 0) {
286                 RTE_LOG(ERR, USER1, "Path %s too long\n", req_file_path);
287                 return -EINVAL;
288         }
289         info.algo = FIPS_TEST_ALGO_MAX;
290         if (parse_file_type(req_file_path) < 0) {
291                 RTE_LOG(ERR, USER1, "File %s type not supported\n",
292                                 req_file_path);
293                 return -EINVAL;
294         }
295
296         info.fp_rd = fopen(req_file_path, "r");
297         if (!info.fp_rd) {
298                 RTE_LOG(ERR, USER1, "Cannot open file %s\n", req_file_path);
299                 return -EINVAL;
300         }
301
302         info.fp_wr = fopen(rsp_file_path, "w");
303         if (!info.fp_wr) {
304                 RTE_LOG(ERR, USER1, "Cannot open file %s\n", rsp_file_path);
305                 return -EINVAL;
306         }
307
308         info.one_line_text = calloc(1, MAX_LINE_CHAR);
309         if (!info.one_line_text) {
310                 RTE_LOG(ERR, USER1, "Insufficient memory\n");
311                 return -ENOMEM;
312         }
313
314         if (rte_strscpy(info.device_name, device_name,
315                                 sizeof(info.device_name)) < 0) {
316                 RTE_LOG(ERR, USER1, "Device name %s too long\n", device_name);
317                 return -EINVAL;
318         }
319
320         if (fips_test_parse_header() < 0) {
321                 RTE_LOG(ERR, USER1, "Failed parsing header\n");
322                 return -1;
323         }
324
325         return 0;
326 }
327
328 void
329 fips_test_clear(void)
330 {
331         if (info.fp_rd)
332                 fclose(info.fp_rd);
333         if (info.fp_wr)
334                 fclose(info.fp_wr);
335         if (info.one_line_text)
336                 free(info.one_line_text);
337         if (info.nb_vec_lines) {
338                 uint32_t i;
339
340                 for (i = 0; i < info.nb_vec_lines; i++)
341                         free(info.vec[i]);
342         }
343
344         memset(&info, 0, sizeof(info));
345 }
346
347 int
348 fips_test_parse_one_case(void)
349 {
350         uint32_t i, j = 0;
351         uint32_t is_interim;
352         uint32_t interim_cnt = 0;
353         int ret;
354
355         if (info.interim_callbacks) {
356                 for (i = 0; i < info.nb_vec_lines; i++) {
357                         is_interim = 0;
358                         for (j = 0; info.interim_callbacks[j].key != NULL; j++)
359                                 if (strstr(info.vec[i],
360                                         info.interim_callbacks[j].key)) {
361                                         is_interim = 1;
362
363                                         ret = info.interim_callbacks[j].cb(
364                                                 info.interim_callbacks[j].key,
365                                                 info.vec[i],
366                                                 info.interim_callbacks[j].val);
367                                         if (ret < 0)
368                                                 return ret;
369                                 }
370
371                         if (is_interim)
372                                 interim_cnt += 1;
373                 }
374         }
375
376         info.vec_start_off = interim_cnt;
377
378         if (interim_cnt) {
379                 for (i = 0; i < interim_cnt; i++)
380                         fprintf(info.fp_wr, "%s\n", info.vec[i]);
381                 fprintf(info.fp_wr, "\n");
382
383                 if (info.nb_vec_lines == interim_cnt)
384                         return 1;
385         }
386
387         for (i = info.vec_start_off; i < info.nb_vec_lines; i++) {
388                 for (j = 0; info.callbacks[j].key != NULL; j++)
389                         if (strstr(info.vec[i], info.callbacks[j].key)) {
390                                 ret = info.callbacks[j].cb(
391                                         info.callbacks[j].key,
392                                         info.vec[i], info.callbacks[j].val);
393                                 if (ret < 0)
394                                         return ret;
395                                 break;
396                         }
397         }
398
399         return 0;
400 }
401
402 void
403 fips_test_write_one_case(void)
404 {
405         uint32_t i;
406
407         for (i = info.vec_start_off; i < info.nb_vec_lines; i++)
408                 fprintf(info.fp_wr, "%s\n", info.vec[i]);
409 }
410
411 static int
412 parser_read_uint64_hex(uint64_t *value, const char *p)
413 {
414         char *next;
415         uint64_t val;
416
417         p = skip_white_spaces(p);
418
419         val = strtoul(p, &next, 16);
420         if (p == next)
421                 return -EINVAL;
422
423         p = skip_white_spaces(next);
424         if (*p != '\0')
425                 return -EINVAL;
426
427         *value = val;
428         return 0;
429 }
430
431 int
432 parser_read_uint8_hex(uint8_t *value, const char *p)
433 {
434         uint64_t val = 0;
435         int ret = parser_read_uint64_hex(&val, p);
436
437         if (ret < 0)
438                 return ret;
439
440         if (val > UINT8_MAX)
441                 return -ERANGE;
442
443         *value = val;
444         return 0;
445 }
446
447 int
448 parse_uint8_known_len_hex_str(const char *key, char *src, struct fips_val *val)
449 {
450         struct fips_val tmp_val = {0};
451         uint32_t len = val->len;
452         int ret;
453
454         if (len == 0) {
455                 if (val->val != NULL) {
456                         rte_free(val->val);
457                         val->val = NULL;
458                 }
459
460                 return 0;
461         }
462
463         ret = parse_uint8_hex_str(key, src, &tmp_val);
464         if (ret < 0)
465                 return ret;
466
467         if (tmp_val.len == val->len) {
468                 val->val = tmp_val.val;
469                 return 0;
470         }
471
472         if (tmp_val.len < val->len) {
473                 rte_free(tmp_val.val);
474                 return -EINVAL;
475         }
476
477         val->val = rte_zmalloc(NULL, val->len, 0);
478         if (!val->val) {
479                 rte_free(tmp_val.val);
480                 memset(val, 0, sizeof(*val));
481                 return -ENOMEM;
482         }
483
484         memcpy(val->val, tmp_val.val, val->len);
485         rte_free(tmp_val.val);
486
487         return 0;
488 }
489
490 int
491 parse_uint8_hex_str(const char *key, char *src, struct fips_val *val)
492 {
493         uint32_t len, j;
494
495         src += strlen(key);
496
497         len = strlen(src) / 2;
498
499         if (val->val) {
500                 rte_free(val->val);
501                 val->val = NULL;
502         }
503
504         val->val = rte_zmalloc(NULL, len, 0);
505         if (!val->val)
506                 return -ENOMEM;
507
508         for (j = 0; j < len; j++) {
509                 char byte[3] = {src[j * 2], src[j * 2 + 1], '\0'};
510
511                 if (parser_read_uint8_hex(&val->val[j], byte) < 0) {
512                         rte_free(val->val);
513                         memset(val, 0, sizeof(*val));
514                         return -EINVAL;
515                 }
516         }
517
518         val->len = len;
519
520         return 0;
521 }
522
523 int
524 parser_read_uint32_val(const char *key, char *src, struct fips_val *val)
525 {
526         char *data = src + strlen(key);
527         size_t data_len = strlen(data);
528         int ret;
529
530         if (data[data_len - 1] == ']') {
531                 char *tmp_data = calloc(1, data_len + 1);
532
533                 if (tmp_data == NULL)
534                         return -ENOMEM;
535
536                 strlcpy(tmp_data, data, data_len);
537
538                 ret = parser_read_uint32(&val->len, tmp_data);
539
540                 free(tmp_data);
541         } else
542                 ret = parser_read_uint32(&val->len, data);
543
544         return ret;
545 }
546
547 int
548 parser_read_uint32_bit_val(const char *key, char *src, struct fips_val *val)
549 {
550         int ret;
551
552         ret = parser_read_uint32_val(key, src, val);
553
554         if (ret < 0)
555                 return ret;
556
557         val->len /= 8;
558
559         return 0;
560 }
561
562 int
563 writeback_hex_str(const char *key, char *dst, struct fips_val *val)
564 {
565         char *str = dst;
566         uint32_t len;
567
568         str += strlen(key);
569
570         for (len = 0; len < val->len; len++)
571                 snprintf(str + len * 2, 255, "%02x", val->val[len]);
572
573         return 0;
574 }
575
576 static int
577 parser_read_uint64(uint64_t *value, const char *p)
578 {
579         char *next;
580         uint64_t val;
581
582         p = skip_white_spaces(p);
583         if (!isdigit(*p))
584                 return -EINVAL;
585
586         val = strtoul(p, &next, 10);
587         if (p == next)
588                 return -EINVAL;
589
590         p = next;
591         switch (*p) {
592         case 'T':
593                 val *= 1024ULL;
594                 /* fall through */
595         case 'G':
596                 val *= 1024ULL;
597                 /* fall through */
598         case 'M':
599                 val *= 1024ULL;
600                 /* fall through */
601         case 'k':
602         case 'K':
603                 val *= 1024ULL;
604                 p++;
605                 break;
606         }
607
608         p = skip_white_spaces(p);
609         if (*p != '\0')
610                 return -EINVAL;
611
612         *value = val;
613         return 0;
614 }
615
616 int
617 parser_read_uint32(uint32_t *value, char *p)
618 {
619         uint64_t val = 0;
620         int ret = parser_read_uint64(&val, p);
621
622         if (ret < 0)
623                 return ret;
624
625         if (val > UINT32_MAX)
626                 return -EINVAL;
627
628         *value = val;
629         return 0;
630 }
631
632 void
633 parse_write_hex_str(struct fips_val *src)
634 {
635         writeback_hex_str("", info.one_line_text, src);
636
637         fprintf(info.fp_wr, "%s\n", info.one_line_text);
638 }
639
640 int
641 update_info_vec(uint32_t count)
642 {
643         const struct fips_test_callback *cb;
644         uint32_t i, j;
645
646         if (!info.writeback_callbacks)
647                 return -1;
648
649         cb = &info.writeback_callbacks[0];
650
651         if (!(strstr(info.vec[0], cb->key))) {
652                 fprintf(info.fp_wr, "%s%u\n", cb->key, count);
653                 i = 0;
654         } else {
655                 snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key,
656                                 count);
657                 i = 1;
658         }
659         snprintf(info.vec[0], strlen(info.vec[0]) + 4, "%s%u", cb->key, count);
660
661         for (i = 1; i < info.nb_vec_lines; i++) {
662                 for (j = 1; info.writeback_callbacks[j].key != NULL; j++) {
663                         cb = &info.writeback_callbacks[j];
664                         if (strstr(info.vec[i], cb->key)) {
665                                 cb->cb(cb->key, info.vec[i], cb->val);
666                                 break;
667                         }
668                 }
669         }
670
671         return 0;
672 }