New upstream version 18.11-rc2
[deb_dpdk.git] / examples / fips_validation / fips_validation_cmac.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <string.h>
6 #include <time.h>
7 #include <stdio.h>
8 #include <rte_string_fns.h>
9
10 #include <rte_cryptodev.h>
11
12 #include "fips_validation.h"
13
14 #define NEW_LINE_STR    "#"
15 #define OP_STR          "CMAC"
16
17 #define ALGO_STR        "Alg = "
18 #define MODE_STR        "Mode = "
19
20 #define COUNT_STR       "Count = "
21 #define KLEN_STR        "Klen = "
22 #define PTLEN_STR       "Mlen = "
23 #define TAGLEN_STR      "Tlen = "
24 #define KEY_STR         "Key = "
25 #define PT_STR          "Msg = "
26 #define TAG_STR         "Mac = "
27
28 #define GEN_STR         "Generate"
29 #define VERIF_STR       "Verify"
30
31 #define POS_NEG_STR     "Result = "
32 #define PASS_STR        "P"
33 #define FAIL_STR        "F"
34
35 struct hash_algo_conversion {
36         const char *str;
37         enum fips_test_algorithms algo;
38 } cmac_algo[] = {
39                 {"AES", FIPS_TEST_ALGO_AES_CMAC},
40 };
41
42 static int
43 parse_test_cmac_writeback(struct fips_val *val)
44 {
45         if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
46                 struct fips_val tmp_val = {val->val + vec.pt.len,
47                                 vec.cipher_auth.digest.len};
48
49                 fprintf(info.fp_wr, "%s", TAG_STR);
50                 parse_write_hex_str(&tmp_val);
51         } else {
52                 fprintf(info.fp_wr, "%s", POS_NEG_STR);
53
54                 if (vec.status == RTE_CRYPTO_OP_STATUS_SUCCESS)
55                         fprintf(info.fp_wr, "%s\n", PASS_STR);
56                 else if (vec.status == RTE_CRYPTO_OP_STATUS_AUTH_FAILED)
57                         fprintf(info.fp_wr, "%s\n", FAIL_STR);
58                 else
59                         fprintf(info.fp_wr, "Error\n");
60         }
61
62         return 0;
63 }
64
65 struct fips_test_callback cmac_tests_vectors[] = {
66                 {KLEN_STR, parser_read_uint32_val, &vec.cipher_auth.key},
67                 {PTLEN_STR, parser_read_uint32_val, &vec.pt},
68                 {TAGLEN_STR, parser_read_uint32_val, &vec.cipher_auth.digest},
69                 {KEY_STR, parse_uint8_hex_str, &vec.cipher_auth.key},
70                 {PT_STR, parse_uint8_known_len_hex_str, &vec.pt},
71                 {TAG_STR, parse_uint8_known_len_hex_str,
72                                 &vec.cipher_auth.digest},
73                 {NULL, NULL, NULL} /**< end pointer */
74 };
75
76 int
77 parse_test_cmac_init(void)
78 {
79         char *tmp;
80         uint32_t i, j;
81
82         for (i = 0; i < info.nb_vec_lines; i++) {
83                 char *line = info.vec[i];
84
85                 tmp = strstr(line, ALGO_STR);
86                 if (!tmp)
87                         continue;
88
89                 for (j = 0; j < RTE_DIM(cmac_algo); j++) {
90                         if (!strstr(line, cmac_algo[j].str))
91                                 continue;
92
93                         info.algo = cmac_algo[j].algo;
94                         break;
95                 }
96
97                 if (j == RTE_DIM(cmac_algo))
98                         return -EINVAL;
99
100                 tmp = strstr(line, MODE_STR);
101                 if (!tmp)
102                         return -1;
103
104                 if (strstr(tmp, GEN_STR))
105                         info.op = FIPS_TEST_ENC_AUTH_GEN;
106                 else if (strstr(tmp, VERIF_STR))
107                         info.op = FIPS_TEST_DEC_AUTH_VERIF;
108                 else
109                         return -EINVAL;
110         }
111
112         info.parse_writeback = parse_test_cmac_writeback;
113         info.callbacks = cmac_tests_vectors;
114
115         return 0;
116 }