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