examples/fips_validation: support AES ECB
[dpdk.git] / examples / fips_validation / fips_validation_aes.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
9 #include <rte_cryptodev.h>
10
11 #include "fips_validation.h"
12
13 #define MODE_STR        "AESVS"
14 #define ALGO_STR        "test data for "
15 #define OP_STR          "State"
16 #define KEY_SIZE_STR    "Key Length : "
17
18
19 #define COUNT_STR       "COUNT = "
20 #define KEY_STR         "KEY = "
21 #define IV_STR          "IV = "
22 #define PT_STR          "PLAINTEXT = "
23 #define CT_STR          "CIPHERTEXT = "
24
25 #define OP_ENC_STR      "ENCRYPT"
26 #define OP_DEC_STR      "DECRYPT"
27
28 struct {
29         uint32_t type;
30         const char *desc;
31 } aes_test_types[] = {
32                 {AESAVS_TYPE_GFXBOX, "GFSbox"},
33                 {AESAVS_TYPE_KEYSBOX, "KeySbox"},
34                 {AESAVS_TYPE_VARKEY, "VarKey"},
35                 {AESAVS_TYPE_VARTXT, "VarTxt"},
36                 {TDES_VARIABLE_TEXT, "VARIABLE PLAINTEXT/CIPHERTEXT"},
37                 {TDES_VARIABLE_TEXT, "KAT"},
38                 {AESAVS_TYPE_MMT, "MMT"},
39                 {AESAVS_TYPE_MCT, "MCT"},
40 };
41
42 struct aes_test_algo {
43         const char *name;
44         enum rte_crypto_cipher_algorithm algo;
45 } const algo_con[] = {
46                 {"CBC", RTE_CRYPTO_CIPHER_AES_CBC},
47                 {"ECB", RTE_CRYPTO_CIPHER_AES_ECB},
48 };
49
50 static int
51 parse_interim_enc_dec(const char *key,
52                 __attribute__((__unused__)) char *text,
53                 __attribute__((__unused__)) struct fips_val *val)
54 {
55         if (strcmp(key, OP_ENC_STR) == 0)
56                 info.op = FIPS_TEST_ENC_AUTH_GEN;
57         else if (strcmp(key, OP_DEC_STR) == 0)
58                 info.op = FIPS_TEST_DEC_AUTH_VERIF;
59         else
60                 return -1;
61
62         return 0;
63 }
64
65 struct fips_test_callback aes_tests_interim[] = {
66                 {OP_ENC_STR, parse_interim_enc_dec, NULL},
67                 {OP_DEC_STR, parse_interim_enc_dec, NULL},
68                 {NULL, NULL, NULL} /**< end pointer */
69 };
70
71 struct fips_test_callback aes_tests_vectors[] = {
72                 {KEY_STR, parse_uint8_hex_str, &vec.cipher_auth.key},
73                 {IV_STR, parse_uint8_hex_str, &vec.iv},
74                 {PT_STR, parse_uint8_hex_str, &vec.pt},
75                 {CT_STR, parse_uint8_hex_str, &vec.ct},
76                 {NULL, NULL, NULL} /**< end pointer */
77 };
78
79 struct fips_test_callback aes_tests_interim_vectors[] = {
80                 {OP_ENC_STR, parse_interim_enc_dec, NULL},
81                 {OP_DEC_STR, parse_interim_enc_dec, NULL},
82                 {NULL, NULL, NULL} /**< end pointer */
83 };
84
85 struct fips_test_callback aes_writeback_callbacks[] = {
86                 /** First element is used to pass COUNT string */
87                 {COUNT_STR, NULL, NULL},
88                 {IV_STR, writeback_hex_str, &vec.iv},
89                 {KEY_STR, writeback_hex_str, &vec.cipher_auth.key},
90                 {PT_STR, writeback_hex_str, &vec.pt},
91                 {CT_STR, writeback_hex_str, &vec.ct},
92                 {NULL, NULL, NULL} /**< end pointer */
93 };
94
95 static int
96 parse_test_aes_writeback(struct fips_val *val)
97 {
98         if (info.op == FIPS_TEST_ENC_AUTH_GEN)
99                 fprintf(info.fp_wr, "%s", CT_STR);
100         else
101                 fprintf(info.fp_wr, "%s", PT_STR);
102
103         parse_write_hex_str(val);
104
105         return 0;
106 }
107
108 static int
109 rsp_test_aes_check(struct fips_val *val)
110 {
111         struct fips_val *data;
112
113         if (info.op == FIPS_TEST_ENC_AUTH_GEN)
114                 data = &vec.ct;
115         else
116                 data = &vec.pt;
117
118         if (memcmp(val->val, data->val, val->len) == 0)
119                 fprintf(info.fp_wr, "Success\n");
120         else
121                 fprintf(info.fp_wr, "Failed\n");
122
123         return 0;
124 }
125
126 int
127 parse_test_aes_init(void)
128 {
129         char *tmp;
130         uint32_t i, j;
131
132         for (i = 0; i < info.nb_vec_lines; i++) {
133                 char *line = info.vec[i];
134
135                 tmp = strstr(line, MODE_STR);
136                 if (tmp) {
137                         for (j = 0; j < RTE_DIM(aes_test_types); j++)
138                                 if (strstr(line, aes_test_types[j].desc)) {
139                                         info.interim_info.aes_data.test_type =
140                                                         aes_test_types[j].type;
141                                         break;
142                                 }
143
144                         if (j >= RTE_DIM(aes_test_types))
145                                 return -EINVAL;
146
147                         tmp = strstr(line, ALGO_STR);
148                         if (!tmp)
149                                 return -EINVAL;
150
151                         tmp += strlen(ALGO_STR);
152                         for (j = 0; j < RTE_DIM(algo_con); j++)
153                                 if (strcmp(algo_con[j].name, tmp) == 0) {
154                                         info.interim_info.aes_data.cipher_algo =
155                                                 (uint32_t)algo_con[j].algo;
156                                         break;
157                                 }
158                         if (j >= RTE_DIM(algo_con))
159                                 return -EINVAL;
160
161                         continue;
162                 }
163
164                 tmp = strstr(line, OP_STR);
165                 if (tmp)
166                         continue;
167
168                 tmp = strstr(line, KEY_SIZE_STR);
169                 if (tmp) {
170                         tmp += strlen(KEY_SIZE_STR);
171                         if (parser_read_uint32
172                                         (&info.interim_info.aes_data.key_len,
173                                                         tmp) < 0)
174                                 return -EINVAL;
175
176                         info.interim_info.aes_data.key_len /= 8;
177
178                         continue;
179                 }
180         }
181
182         info.parse_writeback = parse_test_aes_writeback;
183         info.callbacks = aes_tests_vectors;
184         info.interim_callbacks = aes_tests_interim_vectors;
185         info.writeback_callbacks = aes_writeback_callbacks;
186         info.kat_check = rsp_test_aes_check;
187
188         return 0;
189 }