]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/dpdk/examples/fips_validation/fips_validation_tdes.c
import 15.2.0 Octopus source
[ceph.git] / ceph / src / seastar / dpdk / examples / fips_validation / fips_validation_tdes.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
3 */
4
5 #include <string.h>
6 #include <stdio.h>
7
8 #include <rte_malloc.h>
9 #include <rte_cryptodev.h>
10
11 #include "fips_validation.h"
12
13 #define NEW_LINE_STR "#"
14 #define TEST_TYPE_KEY " for CBC"
15 #define TEST_CBCI_KEY " for CBCI"
16
17 #define ENC_STR "[ENCRYPT]"
18 #define DEC_STR "[DECRYPT]"
19
20 #define COUNT_STR "COUNT = "
21 #define KEY1_STR "KEY1 = "
22 #define KEY2_STR "KEY2 = "
23 #define KEY3_STR "KEY3 = "
24
25 #define KEYS_STR "KEYs = "
26 #define IV_STR "IV = "
27 #define PT_STR "PLAINTEXT = "
28 #define CT_STR "CIPHERTEXT = "
29 #define NK_STR "NumKeys = "
30
31 #define SET_STR " = "
32
33 #define PLAIN_TEXT 0
34 #define CIPHER_TEXT 1
35 #define KEY_TEXT 2
36 #define IV_TEXT 3
37
38 #define DEVICE_STR "# Config Info for : "
39
40 struct {
41 uint32_t type;
42 const char *desc;
43 } test_types[] = {
44 {TDES_INVERSE_PERMUTATION, "INVERSE PERMUTATION"},
45 {TDES_PERMUTATION, "PERMUTATION OPERATION"},
46 {TDES_SUBSTITUTION_TABLE, "SUBSTITUTION TABLE"},
47 {TDES_VARIABLE_KEY, "VARIABLE KEY"},
48 {TDES_VARIABLE_TEXT, "VARIABLE PLAINTEXT/CIPHERTEXT"},
49 {TDES_VARIABLE_TEXT, "KAT"},
50 {TDES_MCT, "Monte Carlo (Modes) Test"},
51 {TDES_MMT, "Multi block Message Test"},
52 };
53
54 static int
55 writeback_tdes_hex_str(const char *key, char *dst, struct fips_val *val);
56
57 static int
58 parse_tdes_uint8_hex_str(const char *key, char *src, struct fips_val *val);
59
60 static int
61 parse_tdes_interim(const char *key,
62 __attribute__((__unused__)) char *text,
63 struct fips_val *val);
64
65 struct fips_test_callback tdes_tests_vectors[] = {
66 {KEYS_STR, parse_tdes_uint8_hex_str, &vec.cipher_auth.key},
67 {KEY1_STR, parse_tdes_uint8_hex_str, &vec.cipher_auth.key},
68 {KEY2_STR, parse_tdes_uint8_hex_str, &vec.cipher_auth.key},
69 {KEY3_STR, parse_tdes_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 tdes_tests_interim_vectors[] = {
77 {ENC_STR, parse_tdes_interim, NULL},
78 {DEC_STR, parse_tdes_interim, NULL},
79 {NULL, NULL, NULL} /**< end pointer */
80 };
81
82 struct fips_test_callback tdes_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 {KEY1_STR, writeback_tdes_hex_str, &vec.cipher_auth.key},
87 {KEY2_STR, writeback_tdes_hex_str, &vec.cipher_auth.key},
88 {KEY3_STR, writeback_tdes_hex_str, &vec.cipher_auth.key},
89 {KEYS_STR, writeback_tdes_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_tdes_interim(const char *key,
97 __attribute__((__unused__)) char *text,
98 __attribute__((__unused__)) struct fips_val *val)
99 {
100 if (strstr(key, ENC_STR))
101 info.op = FIPS_TEST_ENC_AUTH_GEN;
102 else if (strstr(key, DEC_STR))
103 info.op = FIPS_TEST_DEC_AUTH_VERIF;
104 else if (strstr(NK_STR, "NumKeys = 1"))
105 info.interim_info.tdes_data.nb_keys = 1;
106 else if (strstr(NK_STR, "NumKeys = 2"))
107 info.interim_info.tdes_data.nb_keys = 2;
108 else if (strstr(NK_STR, "NumKeys = 3"))
109 info.interim_info.tdes_data.nb_keys = 3;
110 else
111 return -EINVAL;
112
113 return 0;
114 }
115
116 static int
117 parse_tdes_uint8_hex_str(const char *key, char *src, struct fips_val *val)
118 {
119 uint8_t tmp_key[24] = {0};
120 uint32_t len, i;
121
122 src += strlen(key);
123
124 len = strlen(src) / 2;
125
126 if (val->val) {
127 memcpy(tmp_key, val->val, val->len);
128 rte_free(val->val);
129 }
130
131 val->val = rte_zmalloc(NULL, 24, 0);
132 if (!val->val)
133 return -1;
134
135 memcpy(val->val, tmp_key, 24);
136
137 if (strstr(key, KEYS_STR)) {
138 for (i = 0; i < len; i++) {
139 char byte[3] = {src[i * 2], src[i * 2 + 1], '\0'};
140
141 if (parser_read_uint8_hex(&val->val[i], byte) < 0)
142 goto error_exit;
143 }
144
145 memcpy(val->val + 8, val->val, 8);
146 memcpy(val->val + 16, val->val, 8);
147
148 } else if (strstr(key, KEY1_STR)) {
149 for (i = 0; i < len; i++) {
150 char byte[3] = {src[i * 2], src[i * 2 + 1], '\0'};
151
152 if (parser_read_uint8_hex(&val->val[i], byte) < 0)
153 goto error_exit;
154 }
155
156 if (info.interim_info.tdes_data.nb_keys == 2)
157 memcpy(val->val + 16, val->val, 8);
158
159 } else if (strstr(key, KEY2_STR)) {
160 for (i = 0; i < len; i++) {
161 char byte[3] = {src[i * 2], src[i * 2 + 1], '\0'};
162
163 if (parser_read_uint8_hex(&val->val[i + 8], byte) < 0)
164 goto error_exit;
165 }
166
167 } else if (strstr(key, KEY3_STR)) {
168 for (i = 0; i < len; i++) {
169 char byte[3] = {src[i * 2], src[i * 2 + 1], '\0'};
170
171 if (parser_read_uint8_hex(&val->val[i + 16], byte) < 0)
172 goto error_exit;
173 }
174 } else
175 return -EINVAL;
176
177 val->len = 24;
178
179 return 0;
180
181 error_exit:
182 rte_free(val->val);
183 memset(val, 0, sizeof(*val));
184 return -EINVAL;
185 }
186
187 static int
188 parse_test_tdes_writeback(struct fips_val *val)
189 {
190
191 if (info.op == FIPS_TEST_ENC_AUTH_GEN)
192 fprintf(info.fp_wr, "%s", CT_STR);
193 else
194 fprintf(info.fp_wr, "%s", PT_STR);
195
196 parse_write_hex_str(val);
197
198 return 0;
199
200 }
201
202 static int
203 writeback_tdes_hex_str(const char *key, char *dst, struct fips_val *val)
204 {
205 struct fips_val tmp_val = {0};
206
207 tmp_val.len = 8;
208
209 if (strstr(key, KEY1_STR))
210 tmp_val.val = val->val;
211 else if (strstr(key, KEY2_STR))
212 tmp_val.val = val->val + 8;
213 else if (strstr(key, KEY3_STR))
214 tmp_val.val = val->val + 16;
215
216 return writeback_hex_str(key, dst, &tmp_val);
217 }
218
219 static int
220 rsp_test_tdes_check(struct fips_val *val)
221 {
222 struct fips_val *data;
223
224 if (info.op == FIPS_TEST_ENC_AUTH_GEN)
225 data = &vec.ct;
226 else
227 data = &vec.pt;
228
229 if (memcmp(val->val, data->val, val->len) == 0)
230 fprintf(info.fp_wr, "Success\n");
231 else
232 fprintf(info.fp_wr, "Failed\n");
233
234 return 0;
235 }
236
237 int
238 parse_test_tdes_init(void)
239 {
240 uint32_t i;
241
242 for (i = 0; i < info.nb_vec_lines; i++) {
243 char *line = info.vec[i];
244 uint32_t j;
245
246 if (strstr(line, TEST_CBCI_KEY))
247 return -EPERM;
248
249 for (j = 0; j < RTE_DIM(test_types); j++)
250 if (strstr(line, test_types[j].desc)) {
251 info.interim_info.tdes_data.test_type =
252 test_types[j].type;
253 break;
254 }
255 }
256
257 info.parse_writeback = parse_test_tdes_writeback;
258 info.callbacks = tdes_tests_vectors;
259 info.interim_callbacks = tdes_tests_interim_vectors;
260 info.writeback_callbacks = tdes_writeback_callbacks;
261 info.kat_check = rsp_test_tdes_check;
262
263 return 0;
264 }