common/cnxk: allow building for generic arm64
[dpdk.git] / examples / fips_validation / fips_validation_ccm.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 DVPT_STR        "CCM-DVPT"
15 #define VADT_STR        "CCM-VADT"
16 #define VPT_STR         "CCM-VPT"
17 #define VNT_STR         "CCM-VNT"
18 #define VTT_STR         "CCM-VTT"
19
20 #define PARAM_PREFIX    "["
21 #define ALEN_PREFIX     "Alen = "
22 #define PLEN_PREFIX     "Plen = "
23 #define IVLEN_PREFIX    "Nlen = "
24 #define DIGESTL_PREFIX  "Tlen = "
25
26 #define COUNT_STR       "Count = "
27 #define KEY_STR         "Key = "
28 #define IV_STR          "Nonce = "
29 #define PT_STR          "Payload = "
30 #define CT_STR          "CT = "
31 #define AAD_STR         "Adata = "
32 #define POS_NEG_STR     "Result = "
33
34 #define POS_KEYWORD     "Pass"
35 #define NEG_KEYWORD     "Fail"
36
37 static int
38 parser_dvpt_interim(const char *key, char *src, struct fips_val *val)
39 {
40         char *tmp, c, value[10];
41         char num_pattern[] = "0123456789";
42         int i = 0;
43
44         memset(value, 0, 10);
45
46         tmp = strstr(src, key);
47         if (!tmp)
48                 return -1;
49
50         tmp += strlen(key);
51
52         c = tmp[0];
53
54         while (strchr(num_pattern, c) && i < 10) {
55                 value[i++] = c;
56                 c = tmp[i];
57         }
58
59         return parser_read_uint32_val("", value, val);
60 }
61
62 static int
63 parse_dvpt_ct_hex_str(const char *key, char *src, struct fips_val *val)
64 {
65         int ret;
66
67         val->len = vec.pt.len;
68
69         ret = parse_uint8_known_len_hex_str(key, src, val);
70         if (ret < 0)
71                 return ret;
72
73         src += strlen(key) + val->len * 2;
74
75         ret = parse_uint8_known_len_hex_str("", src, &vec.aead.digest);
76         if (ret < 0) {
77                 rte_free(val->val);
78                 memset(val, 0, sizeof(*val));
79                 return ret;
80         }
81
82         return 0;
83 }
84
85 static int
86 parse_uint8_ccm_aad_str(const char *key, char *src, struct fips_val *val)
87 {
88         uint32_t len = val->len, j;
89
90         src += strlen(key);
91
92         /* CCM aad requires 18 bytes padding before the real content */
93         val->val = rte_zmalloc(NULL, len + 18, 0);
94         if (!val->val)
95                 return -1;
96
97         for (j = 0; j < len; j++) {
98                 char byte[3] = {src[j * 2], src[j * 2 + 1], '\0'};
99
100                 if (parser_read_uint8_hex(&val->val[j + 18], byte) < 0) {
101                         rte_free(val->val);
102                         memset(val, 0, sizeof(*val));
103                         return -EINVAL;
104                 }
105         }
106
107         return 0;
108 }
109
110 struct fips_test_callback ccm_vnt_vec[] = {
111                 {IV_STR, parse_uint8_known_len_hex_str, &vec.iv},
112                 {AAD_STR, parse_uint8_ccm_aad_str, &vec.aead.aad},
113                 {PT_STR, parse_uint8_known_len_hex_str, &vec.pt},
114                 {NULL, NULL, NULL} /**< end pointer */
115 };
116
117 struct fips_test_callback ccm_vnt_interim_vec[] = {
118                 {ALEN_PREFIX, parser_read_uint32_val, &vec.aead.aad},
119                 {PLEN_PREFIX, parser_read_uint32_val, &vec.pt},
120                 {DIGESTL_PREFIX, parser_read_uint32_val, &vec.aead.digest},
121                 {IVLEN_PREFIX, parser_read_uint32_val, &vec.iv},
122                 {KEY_STR, parse_uint8_hex_str, &vec.aead.key},
123                 {NULL, NULL, NULL} /**< end pointer */
124 };
125
126 struct fips_test_callback ccm_vtt_vec[] = {
127                 {AAD_STR, parse_uint8_ccm_aad_str, &vec.aead.aad},
128                 {PT_STR, parse_uint8_known_len_hex_str, &vec.pt},
129                 {NULL, NULL, NULL} /**< end pointer */
130 };
131
132 struct fips_test_callback ccm_vtt_interim_vec[] = {
133                 {ALEN_PREFIX, parser_read_uint32_val, &vec.aead.aad},
134                 {PLEN_PREFIX, parser_read_uint32_val, &vec.pt},
135                 {IVLEN_PREFIX, parser_read_uint32_val, &vec.iv},
136                 {DIGESTL_PREFIX, parser_read_uint32_val, &vec.aead.digest},
137                 {KEY_STR, parse_uint8_hex_str, &vec.aead.key},
138                 {IV_STR, parse_uint8_known_len_hex_str, &vec.iv},
139                 {NULL, NULL, NULL} /**< end pointer */
140 };
141
142 struct fips_test_callback ccm_vadt_vec[] = {
143                 {AAD_STR, parse_uint8_ccm_aad_str, &vec.aead.aad},
144                 {PT_STR, parse_uint8_known_len_hex_str, &vec.pt},
145                 {NULL, NULL, NULL} /**< end pointer */
146 };
147
148 struct fips_test_callback ccm_vadt_interim_vec[] = {
149                 {PLEN_PREFIX, parser_read_uint32_val, &vec.pt},
150                 {IVLEN_PREFIX, parser_read_uint32_val, &vec.iv},
151                 {ALEN_PREFIX, parser_read_uint32_val, &vec.aead.aad},
152                 {DIGESTL_PREFIX, parser_read_uint32_val, &vec.aead.digest},
153                 {KEY_STR, parse_uint8_hex_str, &vec.aead.key},
154                 {IV_STR, parse_uint8_known_len_hex_str, &vec.iv},
155                 {NULL, NULL, NULL} /**< end pointer */
156 };
157
158 struct fips_test_callback ccm_vpt_vec[] = {
159                 {AAD_STR, parse_uint8_ccm_aad_str, &vec.aead.aad},
160                 {PT_STR, parse_uint8_known_len_hex_str, &vec.pt},
161                 {NULL, NULL, NULL} /**< end pointer */
162 };
163
164 struct fips_test_callback ccm_vpt_interim_vec[] = {
165                 {ALEN_PREFIX, parser_read_uint32_val, &vec.aead.aad},
166                 {IVLEN_PREFIX, parser_read_uint32_val, &vec.iv},
167                 {DIGESTL_PREFIX, parser_read_uint32_val, &vec.aead.digest},
168                 {PLEN_PREFIX, parser_read_uint32_val, &vec.pt},
169                 {KEY_STR, parse_uint8_hex_str, &vec.aead.key},
170                 {IV_STR, parse_uint8_known_len_hex_str, &vec.iv},
171                 {NULL, NULL, NULL} /**< end pointer */
172 };
173
174 struct fips_test_callback ccm_dvpt_vec[] = {
175                 {IV_STR, parse_uint8_known_len_hex_str, &vec.iv},
176                 {AAD_STR, parse_uint8_ccm_aad_str, &vec.aead.aad},
177                 {CT_STR, parse_dvpt_ct_hex_str, &vec.ct},
178                 {NULL, NULL, NULL} /**< end pointer */
179 };
180
181 struct fips_test_callback ccm_dvpt_interim_vec[] = {
182                 {ALEN_PREFIX, parser_dvpt_interim, &vec.aead.aad},
183                 {PLEN_PREFIX, parser_dvpt_interim, &vec.pt},
184                 {IVLEN_PREFIX, parser_dvpt_interim, &vec.iv},
185                 {DIGESTL_PREFIX, parser_dvpt_interim, &vec.aead.digest},
186                 {KEY_STR, parse_uint8_hex_str, &vec.aead.key},
187                 {NULL, NULL, NULL} /**< end pointer */
188 };
189
190 struct ccm_test_types {
191         const char *str;
192         uint32_t type;
193         const struct fips_test_callback *cb;
194         const struct fips_test_callback *cb_interim;
195         enum fips_test_op op;
196 } ctt[] = {
197                 {DVPT_STR, CCM_DVPT, ccm_dvpt_vec, ccm_dvpt_interim_vec,
198                         FIPS_TEST_DEC_AUTH_VERIF},
199                 {VPT_STR, CCM_VPT, ccm_vpt_vec, ccm_vpt_interim_vec,
200                         FIPS_TEST_ENC_AUTH_GEN},
201                 {VADT_STR, CCM_VADT, ccm_vadt_vec, ccm_vadt_interim_vec,
202                         FIPS_TEST_ENC_AUTH_GEN},
203                 {VNT_STR, CCM_VNT, ccm_vnt_vec, ccm_vnt_interim_vec,
204                         FIPS_TEST_ENC_AUTH_GEN},
205                 {VTT_STR, CCM_VTT, ccm_vtt_vec, ccm_vtt_interim_vec,
206                         FIPS_TEST_ENC_AUTH_GEN},
207 };
208
209 static int
210 parse_test_ccm_writeback(struct fips_val *val)
211 {
212         struct fips_val tmp_val;
213
214         switch (info.interim_info.ccm_data.test_type) {
215         case CCM_DVPT:
216                 fprintf(info.fp_wr, "%s", POS_NEG_STR);
217                 if (vec.status == RTE_CRYPTO_OP_STATUS_SUCCESS) {
218                         fprintf(info.fp_wr, "%s\n", POS_KEYWORD);
219                         fprintf(info.fp_wr, "%s", PT_STR);
220
221                         tmp_val.val = val->val;
222                         tmp_val.len = vec.pt.len;
223
224                         if (tmp_val.len == 0)
225                                 fprintf(info.fp_wr, "00\n");
226                         else
227                                 parse_write_hex_str(&tmp_val);
228                 } else
229                         fprintf(info.fp_wr, "%s\n", NEG_KEYWORD);
230
231                 break;
232
233         case CCM_VADT:
234         case CCM_VNT:
235         case CCM_VPT:
236         case CCM_VTT:
237                 fprintf(info.fp_wr, "%s", CT_STR);
238
239                 parse_write_hex_str(val);
240
241                 break;
242
243         }
244
245         return 0;
246 }
247
248 int
249 parse_test_ccm_init(void)
250 {
251
252         uint32_t i;
253
254         for (i = 0; i < info.nb_vec_lines; i++) {
255                 char *line = info.vec[i];
256                 uint32_t j;
257
258                 for (j = 0; j < RTE_DIM(ctt); j++)
259                         if (strstr(line, ctt[j].str)) {
260                                 info.interim_info.ccm_data.test_type =
261                                                 ctt[j].type;
262                                 info.callbacks = ctt[j].cb;
263                                 info.interim_callbacks = ctt[j].cb_interim;
264                                 info.op = ctt[j].op;
265                                 break;
266                 }
267         }
268
269         info.parse_writeback = parse_test_ccm_writeback;
270
271         return 0;
272 }