]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/intel-ipsec-mb/LibTestApp/customop_test.c
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / spdk / intel-ipsec-mb / LibTestApp / customop_test.c
1 /*****************************************************************************
2 Copyright (c) 2017-2018, Intel Corporation
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright notice,
8 this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of Intel Corporation nor the names of its contributors
13 may be used to endorse or promote products derived from this software
14 without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *****************************************************************************/
27
28 #include <assert.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <intel-ipsec-mb.h>
33
34 #include "customop_test.h"
35
36 #define DIM(_a) (sizeof(_a) / sizeof(_a[0]))
37
38 #ifdef DEBUG
39 #ifdef _WIN32
40 #define TRACE(fmt, ...) fprintf(stderr, "%s:%d "fmt, \
41 __FUNCTION__, __LINE__, __VA_ARGS__)
42 #else
43 #define TRACE(fmt, ...) fprintf(stderr, "%s:%d "fmt, \
44 __func__, __LINE__, __VA_ARGS__)
45 #endif
46 #else
47 # define TRACE(fmt, ...)
48 #endif
49
50 struct cipher_attr_s {
51 const char *name;
52 JOB_CIPHER_MODE mode;
53 unsigned key_len;
54 unsigned iv_len;
55 };
56
57 struct auth_attr_s {
58 const char *name;
59 JOB_HASH_ALG hash;
60 unsigned tag_len;
61 };
62
63 struct test_vec_s {
64 uint8_t iv[16];
65 uint8_t txt[64];
66 uint8_t tag[32];
67 uint8_t verify[32];
68
69 DECLARE_ALIGNED(uint8_t enc_key[16*16], 64);
70 DECLARE_ALIGNED(uint8_t dec_key[16*16], 64);
71 uint8_t ipad[256];
72 uint8_t opad[256];
73 const struct cipher_attr_s *cipher;
74 const struct auth_attr_s *auth;
75
76 unsigned seq;
77 };
78
79 /*
80 * addon cipher function
81 */
82 static int
83 cipher_addon(struct JOB_AES_HMAC *job)
84 {
85 #ifdef DEBUG
86 struct test_vec_s *node = job->user_data;
87 #endif
88
89 TRACE("Seq:%u Cipher Addon cipher:%s auth:%s\n",
90 node->seq, node->cipher->name, node->auth->name);
91
92 if (job->cipher_direction == ENCRYPT)
93 memset(job->dst, 1, job->msg_len_to_cipher_in_bytes);
94 else
95 memset(job->dst, 2, job->msg_len_to_cipher_in_bytes);
96
97 return 0; /* success */
98 }
99
100 /*
101 * addon hash function
102 */
103 static int
104 hash_addon(struct JOB_AES_HMAC *job)
105 {
106 #ifdef DEBUG
107 struct test_vec_s *node = job->user_data;
108 #endif
109
110 TRACE("Seq:%u Auth Addon cipher:%s auth:%s\n",
111 node->seq, node->cipher->name, node->auth->name);
112
113 memset(job->auth_tag_output, 3, job->auth_tag_output_len_in_bytes);
114 return 0; /* success */
115 }
116
117 /*
118 * test cipher functions
119 */
120 static const struct auth_attr_s auth_attr_tab[] = {
121 { "SHA1", SHA1, 12 },
122 { "SHA224", SHA_224, 14 },
123 { "SHA256", SHA_256, 16 },
124 { "SHA384", SHA_384, 24 },
125 { "SHA512", SHA_512, 32 },
126 { "MD5", MD5, 12 },
127 { "CUSTOM_HASH", CUSTOM_HASH, 16 }
128 };
129
130 /*
131 * test hash functions
132 */
133 static const struct cipher_attr_s cipher_attr_tab[] = {
134 { "CBC128", CBC, 16, 16 },
135 { "CBC192", CBC, 24, 16 },
136 { "CBC256", CBC, 32, 16 },
137 { "CUSTOM_CIPHER", CUSTOM_CIPHER, 32, 12 },
138 { "CTR128", CNTR, 16, 12 },
139 { "CTR192", CNTR, 24, 12 },
140 { "CTR256", CNTR, 32, 12 }
141 };
142
143 static int
144 job_check(const struct JOB_AES_HMAC *job)
145 {
146 #ifdef DEBUG
147 struct test_vec_s *done = job->user_data;
148 #endif
149
150 TRACE("done Seq:%u Cipher:%s Auth:%s\n",
151 done->seq, done->cipher->name, done->auth->name);
152
153 if (job->status != STS_COMPLETED) {
154 TRACE("failed job status:%d\n", job->status);
155 return -1;
156 }
157 if (job->cipher_mode == CUSTOM_CIPHER) {
158 if (job->cipher_direction == ENCRYPT) {
159 unsigned i;
160
161 for (i = 0; i < job->msg_len_to_cipher_in_bytes; i++) {
162 if (job->dst[i] != 1) {
163 TRACE("NG add-on encryption %u\n", i);
164 return -1;
165 }
166 }
167 TRACE("Addon encryption passes Seq:%u\n", done->seq);
168 } else {
169 unsigned i;
170
171 for (i = 0; i < job->msg_len_to_cipher_in_bytes; i++) {
172 if (job->dst[i] != 2) {
173 TRACE("NG add-on decryption %u\n", i);
174 return -1;
175 }
176 }
177 TRACE("Addon decryption passes Seq:%u\n", done->seq);
178 }
179 }
180
181 if (job->hash_alg == CUSTOM_HASH) {
182 unsigned i;
183
184 for (i = 0; i < job->auth_tag_output_len_in_bytes; i++) {
185 if (job->auth_tag_output[i] != 3) {
186 TRACE("NG add-on hashing %u\n", i);
187 return -1;
188 }
189 }
190 TRACE("Addon hashing passes Seq:%u\n", done->seq);
191 }
192 return 0;
193 }
194
195
196 void
197 customop_test(struct MB_MGR *mgr)
198 {
199 struct test_vec_s test_tab[DIM(cipher_attr_tab) * DIM(auth_attr_tab)];
200 struct JOB_AES_HMAC *job;
201 unsigned i, j, seq;
202 int result = 0;
203
204 for (i = 0, seq = 0; i < DIM(cipher_attr_tab); i++) {
205 for (j = 0; j < DIM(auth_attr_tab); j++) {
206 assert(seq < DIM(test_tab));
207 test_tab[seq].seq = seq;
208 test_tab[seq].cipher = &cipher_attr_tab[i];
209 test_tab[seq].auth = &auth_attr_tab[j];
210 seq++;
211 }
212 }
213
214 /* encryption */
215 for (i = 0; i < seq; i++) {
216 struct test_vec_s *node = &test_tab[i];
217
218 while ((job = IMB_GET_NEXT_JOB(mgr)) == NULL) {
219 job = IMB_FLUSH_JOB(mgr);
220 result |= job_check(job);
221 }
222
223 job->cipher_func = cipher_addon;
224 job->hash_func = hash_addon;
225
226 job->aes_enc_key_expanded = node->enc_key;
227 job->aes_dec_key_expanded = node->dec_key;
228 job->aes_key_len_in_bytes = node->cipher->key_len;
229 job->src = node->txt;
230 job->dst = node->txt;
231 job->cipher_start_src_offset_in_bytes = 16;
232 job->msg_len_to_cipher_in_bytes = sizeof(node->txt);
233 job->hash_start_src_offset_in_bytes = 0;
234 job->msg_len_to_hash_in_bytes =
235 sizeof(node->txt) + sizeof(node->iv);
236 job->iv = node->iv;
237 job->iv_len_in_bytes = node->cipher->iv_len;
238 job->auth_tag_output = node->tag;
239 job->auth_tag_output_len_in_bytes = node->auth->tag_len;
240
241 job->u.HMAC._hashed_auth_key_xor_ipad = node->ipad;
242 job->u.HMAC._hashed_auth_key_xor_opad = node->opad;
243 job->cipher_mode = node->cipher->mode;
244 job->cipher_direction = ENCRYPT;
245 job->chain_order = CIPHER_HASH;
246 job->hash_alg = node->auth->hash;
247 job->user_data = node;
248
249 job = IMB_SUBMIT_JOB(mgr);
250 while (job) {
251 result |= job_check(job);
252 job = IMB_GET_COMPLETED_JOB(mgr);
253 }
254 }
255
256 while ((job = IMB_FLUSH_JOB(mgr)) != NULL)
257 result |= job_check(job);
258
259 /* decryption */
260 for (i = 0; i < seq; i++) {
261 struct test_vec_s *node = &test_tab[i];
262
263 while ((job = IMB_GET_NEXT_JOB(mgr)) == NULL) {
264 job = IMB_FLUSH_JOB(mgr);
265 result |= job_check(job);
266 }
267
268 job->cipher_func = cipher_addon;
269 job->hash_func = hash_addon;
270
271 job->aes_enc_key_expanded = node->enc_key;
272 job->aes_dec_key_expanded = node->dec_key;
273 job->aes_key_len_in_bytes = node->cipher->key_len;
274 job->src = node->txt;
275 job->dst = node->txt;
276 job->cipher_start_src_offset_in_bytes = 16;
277 job->msg_len_to_cipher_in_bytes = sizeof(node->txt);
278 job->hash_start_src_offset_in_bytes = 0;
279 job->msg_len_to_hash_in_bytes =
280 sizeof(node->txt) + sizeof(node->iv);
281 job->iv = node->iv;
282 job->iv_len_in_bytes = node->cipher->iv_len;
283 job->auth_tag_output = node->tag;
284 job->auth_tag_output_len_in_bytes = node->auth->tag_len;
285
286 job->u.HMAC._hashed_auth_key_xor_ipad = node->ipad;
287 job->u.HMAC._hashed_auth_key_xor_opad = node->opad;
288 job->cipher_mode = node->cipher->mode;
289 job->cipher_direction = DECRYPT;
290 job->chain_order = HASH_CIPHER;
291 job->hash_alg = node->auth->hash;
292 job->user_data = node;
293
294 job = IMB_SUBMIT_JOB(mgr);
295 while (job) {
296 result |= job_check(job);
297 job = IMB_GET_COMPLETED_JOB(mgr);
298 }
299 }
300
301 while ((job = IMB_FLUSH_JOB(mgr)) != NULL)
302 result |= job_check(job);
303
304 if (result)
305 fprintf(stdout, "Custom cipher/auth test failed!\n");
306 else
307 fprintf(stdout, "Custom cipher/auth test passed\n");
308 }