]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/intel-ipsec-mb/LibTestApp/des_test.c
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / spdk / intel-ipsec-mb / LibTestApp / des_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 <stdint.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <assert.h>
33
34 #include <intel-ipsec-mb.h>
35
36 #include "gcm_ctr_vectors_test.h"
37
38 #ifndef DIM
39 #define DIM(x) (sizeof(x) / sizeof(x[0]))
40 #endif
41
42 int des_test(const enum arch_type arch, struct MB_MGR *mb_mgr);
43
44 struct des_vector {
45 const uint8_t *K; /* key */
46 const uint8_t *IV; /* initialization vector */
47 const uint8_t *P; /* plain text */
48 uint64_t Plen; /* plain text length */
49 const uint8_t *C; /* cipher text - same length as plain text */
50 };
51
52 struct des3_vector {
53 const uint8_t *K1; /* key */
54 const uint8_t *K2; /* key */
55 const uint8_t *K3; /* key */
56 const uint8_t *IV; /* initialization vector */
57 const uint8_t *P; /* plain text */
58 uint64_t Plen; /* plain text length */
59 const uint8_t *C; /* cipher text - same length as plain text */
60 };
61
62 /* CM-SP-SECv3.1-I07-170111 I.7 */
63 static const uint8_t K1[] = {
64 0xe6, 0x60, 0x0f, 0xd8, 0x85, 0x2e, 0xf5, 0xab
65 };
66 static const uint8_t IV1[] = {
67 0x81, 0x0e, 0x52, 0x8e, 0x1c, 0x5f, 0xda, 0x1a
68 };
69 static const uint8_t P1[] = {
70 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
71 0x08, 0x09, 0x0a, 0x0b, 0x88, 0x41, 0x65, 0x06
72 };
73 static const uint8_t C1[] = {
74 0x0d, 0xda, 0x5a, 0xcb, 0xd0, 0x5e, 0x55, 0x67,
75 0x9f, 0x04, 0xd1, 0xb6, 0x41, 0x3d, 0x4e, 0xed
76 };
77
78 static const uint8_t K2[] = {
79 0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e
80 };
81 static const uint8_t IV2[] = {
82 0x02, 0xa8, 0x11, 0x77, 0x4d, 0xcd, 0xe1, 0x3b
83 };
84 static const uint8_t P2[] = {
85 0x05, 0xef, 0xf7, 0x00, 0xe9, 0xa1, 0x3a, 0xe5,
86 0xca, 0x0b, 0xcb, 0xd0, 0x48, 0x47, 0x64, 0xbd,
87 0x1f, 0x23, 0x1e, 0xa8, 0x1c, 0x7b, 0x64, 0xc5,
88 0x14, 0x73, 0x5a, 0xc5, 0x5e, 0x4b, 0x79, 0x63,
89 0x3b, 0x70, 0x64, 0x24, 0x11, 0x9e, 0x09, 0xdc,
90 0xaa, 0xd4, 0xac, 0xf2, 0x1b, 0x10, 0xaf, 0x3b,
91 0x33, 0xcd, 0xe3, 0x50, 0x48, 0x47, 0x15, 0x5c,
92 0xbb, 0x6f, 0x22, 0x19, 0xba, 0x9b, 0x7d, 0xf5
93
94 };
95 static const uint8_t C2[] = {
96 0xf3, 0x31, 0x8d, 0x01, 0x19, 0x4d, 0xa8, 0x00,
97 0xa4, 0x2c, 0x10, 0xb5, 0x33, 0xd6, 0xbc, 0x11,
98 0x97, 0x59, 0x2d, 0xcc, 0x9b, 0x5d, 0x35, 0x9a,
99 0xc3, 0x04, 0x5d, 0x07, 0x4c, 0x86, 0xbf, 0x72,
100 0xe5, 0x1a, 0x72, 0x25, 0x82, 0x22, 0x54, 0x03,
101 0xde, 0x8b, 0x7a, 0x58, 0x5c, 0x6c, 0x28, 0xdf,
102 0x41, 0x0e, 0x38, 0xd6, 0x2a, 0x86, 0xe3, 0x4f,
103 0xa2, 0x7c, 0x22, 0x39, 0x60, 0x06, 0x03, 0x6f
104 };
105
106 static struct des_vector vectors[] = {
107 {K1, IV1, P1, sizeof(P1), C1},
108 {K2, IV2, P2, sizeof(P2), C2},
109 };
110
111 /* CM-SP-SECv3.1-I07-170111 I.7 */
112 static const uint8_t DK1[] = {
113 0xe6, 0x60, 0x0f, 0xd8, 0x85, 0x2e, 0xf5, 0xab
114 };
115 static const uint8_t DIV1[] = {
116 0x81, 0x0e, 0x52, 0x8e, 0x1c, 0x5f, 0xda, 0x1a
117 };
118 static const uint8_t DP1[] = {
119 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
120 0x08, 0x09, 0x0a, 0x0b, 0x88, 0x41, 0x65, 0x06
121 };
122 static const uint8_t DC1[] = {
123 0x0d, 0xda, 0x5a, 0xcb, 0xd0, 0x5e, 0x55, 0x67,
124 0x9f, 0x04, 0xd1, 0xb6, 0x41, 0x3d, 0x4e, 0xed
125 };
126
127 static const uint8_t DK2[] = {
128 0xe6, 0x60, 0x0f, 0xd8, 0x85, 0x2e, 0xf5, 0xab
129 };
130 static const uint8_t DIV2[] = {
131 0x81, 0x0e, 0x52, 0x8e, 0x1c, 0x5f, 0xda, 0x1a
132 };
133 static const uint8_t DP2[] = {
134 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
135 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x91,
136 0xd2, 0xd1, 0x9f
137 };
138 static const uint8_t DC2[] = {
139 0x0d, 0xda, 0x5a, 0xcb, 0xd0, 0x5e, 0x55, 0x67,
140 0x51, 0x47, 0x46, 0x86, 0x8a, 0x71, 0xe5, 0x77,
141 0xef, 0xac, 0x88
142 };
143
144 static const uint8_t DK3[] = {
145 0xe6, 0x60, 0x0f, 0xd8, 0x85, 0x2e, 0xf5, 0xab
146 };
147 static const uint8_t DIV3[] = {
148 0x51, 0x47, 0x46, 0x86, 0x8a, 0x71, 0xe5, 0x77
149 };
150 static const uint8_t DP3[] = {
151 0xd2, 0xd1, 0x9f
152 };
153 static const uint8_t DC3[] = {
154 0xef, 0xac, 0x88
155 };
156
157
158 static struct des_vector docsis_vectors[] = {
159 {DK1, DIV1, DP1, sizeof(DP1), DC1},
160 {DK2, DIV2, DP2, sizeof(DP2), DC2},
161 {DK3, DIV3, DP3, sizeof(DP3), DC3},
162 };
163
164 /* 3DES vectors - 2x and 3x keys */
165
166 static const uint8_t D3K1_1[] = {
167 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
168 };
169
170 static const uint8_t D3K2_1[] = {
171 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
172 };
173
174 static const uint8_t D3K3_1[] = {
175 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
176 };
177
178 static const uint8_t D3IV_1[] = {
179 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
180 };
181
182
183 static const uint8_t D3PT_1[] = {
184 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
185 };
186
187 static const uint8_t D3CT_1[] = {
188 0xdf, 0x0b, 0x6c, 0x9c, 0x31, 0xcd, 0x0c, 0xe4
189 };
190
191 #define D3PT_LEN_1 8
192
193 static const uint8_t D3K1_2[] = {
194 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
195 };
196
197 static const uint8_t D3K2_2[] = {
198 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
199 };
200
201 static const uint8_t D3K3_2[] = {
202 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
203 };
204
205 static const uint8_t D3IV_2[] = {
206 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
207 };
208
209 static const uint8_t D3PT_2[] = {
210 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
211 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
212 };
213
214 static const uint8_t D3CT_2[] = {
215 0xdd, 0xad, 0xa1, 0x61, 0xe8, 0xd7, 0x96, 0x73,
216 0xed, 0x75, 0x32, 0xe5, 0x92, 0x23, 0xcd, 0x0d
217 };
218
219 #define D3PT_LEN_2 16
220
221 static const uint8_t D3K1_3[] = {
222 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
223 };
224
225 static const uint8_t D3K2_3[] = {
226 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
227 };
228
229 static const uint8_t D3K3_3[] = {
230 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
231 };
232
233 static const uint8_t D3IV_3[] = {
234 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
235 };
236
237 static const uint8_t D3PT_3[] = {
238 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
239 };
240
241 static const uint8_t D3CT_3[] = {
242 0x58, 0xed, 0x24, 0x8f, 0x77, 0xf6, 0xb1, 0x9e
243 };
244
245 #define D3PT_LEN_3 8
246
247 static const uint8_t D3K1_4[] = {
248 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
249 };
250
251 static const uint8_t D3K2_4[] = {
252 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
253 };
254
255 static const uint8_t D3K3_4[] = {
256 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
257 };
258
259 static const uint8_t D3IV_4[] = {
260 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
261 };
262
263 static const uint8_t D3PT_4[] = {
264 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
265 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
266 };
267
268 static const uint8_t D3CT_4[] = {
269 0x89, 0x4b, 0xc3, 0x08, 0x54, 0x26, 0xa4, 0x41,
270 0xf2, 0x7f, 0x73, 0xae, 0x26, 0xab, 0xbf, 0x74
271 };
272
273 #define D3PT_LEN_4 16
274
275 static struct des3_vector des3_vectors[] = {
276 { D3K1_1, D3K2_1, D3K3_1, D3IV_1, D3PT_1, D3PT_LEN_1, D3CT_1 },
277 { D3K1_2, D3K2_2, D3K3_2, D3IV_2, D3PT_2, D3PT_LEN_2, D3CT_2 },
278 { D3K1_3, D3K2_3, D3K3_3, D3IV_3, D3PT_3, D3PT_LEN_3, D3CT_3 },
279 { D3K1_4, D3K2_4, D3K3_4, D3IV_4, D3PT_4, D3PT_LEN_4, D3CT_4 },
280 };
281
282 static int
283 test_des_many(struct MB_MGR *mb_mgr,
284 const uint64_t *ks,
285 const uint64_t *ks2,
286 const uint64_t *ks3,
287 const void *iv,
288 const uint8_t *in_text,
289 const uint8_t *out_text,
290 unsigned text_len,
291 int dir,
292 int order,
293 JOB_CIPHER_MODE cipher,
294 const int in_place,
295 const int num_jobs)
296 {
297 const void *ks_ptr[3]; /* 3DES */
298 struct JOB_AES_HMAC *job;
299 uint8_t padding[16];
300 uint8_t **targets = malloc(num_jobs * sizeof(void *));
301 int i, jobs_rx = 0, ret = -1;
302
303 assert(targets != NULL);
304
305 memset(padding, -1, sizeof(padding));
306
307 for (i = 0; i < num_jobs; i++) {
308 targets[i] = malloc(text_len + (sizeof(padding) * 2));
309 memset(targets[i], -1, text_len + (sizeof(padding) * 2));
310 if (in_place) {
311 /* copy input text to the allocated buffer */
312 memcpy(targets[i] + sizeof(padding), in_text, text_len);
313 }
314 }
315
316 /* Used in 3DES only */
317 ks_ptr[0] = ks;
318 ks_ptr[1] = ks2;
319 ks_ptr[2] = ks3;
320
321 /* flush the scheduler */
322 while ((job = IMB_FLUSH_JOB(mb_mgr)) != NULL)
323 ;
324
325 for (i = 0; i < num_jobs; i++) {
326 job = IMB_GET_NEXT_JOB(mb_mgr);
327 job->cipher_direction = dir;
328 job->chain_order = order;
329 if (!in_place) {
330 job->dst = targets[i] + sizeof(padding);
331 job->src = in_text;
332 } else {
333 job->dst = targets[i] + sizeof(padding);
334 job->src = targets[i] + sizeof(padding);
335 }
336 job->cipher_mode = cipher;
337 if (cipher == DES3) {
338 job->aes_enc_key_expanded = (const void *) ks_ptr;
339 job->aes_dec_key_expanded = (const void *) ks_ptr;
340 job->aes_key_len_in_bytes = 24; /* 3x keys only */
341 } else {
342 job->aes_enc_key_expanded = ks;
343 job->aes_dec_key_expanded = ks;
344 job->aes_key_len_in_bytes = 8;
345 }
346 job->iv = iv;
347 job->iv_len_in_bytes = 8;
348 job->cipher_start_src_offset_in_bytes = 0;
349 job->msg_len_to_cipher_in_bytes = text_len;
350 job->user_data = (void *)((uint64_t)i);
351
352 job->hash_alg = NULL_HASH;
353
354 job = IMB_SUBMIT_JOB(mb_mgr);
355 if (job != NULL) {
356 const int num = (const int)((uint64_t)job->user_data);
357
358 jobs_rx++;
359 if (job->status != STS_COMPLETED) {
360 printf("%d error status:%d, job %d",
361 __LINE__, job->status, num);
362 goto end;
363 }
364 if (memcmp(out_text, targets[num] + sizeof(padding),
365 text_len)) {
366 printf("%d mismatched\n", num);
367 goto end;
368 }
369 if (memcmp(padding, targets[num], sizeof(padding))) {
370 printf("%d overwrite head\n", num);
371 goto end;
372 }
373 if (memcmp(padding,
374 targets[num] + sizeof(padding) + text_len,
375 sizeof(padding))) {
376 printf("%d overwrite tail\n", num);
377 goto end;
378 }
379 }
380 }
381
382 while ((job = IMB_FLUSH_JOB(mb_mgr)) != NULL) {
383 const int num = (const int)((uint64_t)job->user_data);
384
385 jobs_rx++;
386 if (job->status != STS_COMPLETED) {
387 printf("%d Error status:%d, job %d",
388 __LINE__, job->status, num);
389 goto end;
390 }
391 if (memcmp(out_text, targets[num] + sizeof(padding),
392 text_len)) {
393 printf("%d mismatched\n", num);
394 goto end;
395 }
396 if (memcmp(padding, targets[num], sizeof(padding))) {
397 printf("%d overwrite head\n", num);
398 goto end;
399 }
400 if (memcmp(padding, targets[num] + sizeof(padding) + text_len,
401 sizeof(padding))) {
402 printf("%d overwrite tail\n", num);
403 goto end;
404 }
405 }
406
407 if (jobs_rx != num_jobs) {
408 printf("Expected %d jobs, received %d\n", num_jobs, jobs_rx);
409 goto end;
410 }
411 ret = 0;
412
413 end:
414 while ((job = IMB_FLUSH_JOB(mb_mgr)) != NULL)
415 ;
416
417 for (i = 0; i < num_jobs; i++)
418 free(targets[i]);
419 free(targets);
420 return ret;
421 }
422
423 static int
424 test_des_one(struct MB_MGR *mb_mgr,
425 const enum arch_type arch,
426 const uint64_t *ks,
427 const uint64_t *ks2,
428 const uint64_t *ks3,
429 const void *iv,
430 const uint8_t *in_text,
431 const uint8_t *out_text,
432 unsigned text_len,
433 int dir,
434 int order,
435 JOB_CIPHER_MODE cipher,
436 const int in_place)
437 {
438 const void *ks_ptr[3]; /* 3DES */
439 struct JOB_AES_HMAC *job;
440 uint8_t padding[16];
441 uint8_t *target = malloc(text_len + (sizeof(padding) * 2));
442 int ret = -1;
443
444 assert(target != NULL);
445
446 memset(target, -1, text_len + (sizeof(padding) * 2));
447 memset(padding, -1, sizeof(padding));
448
449 if (in_place) {
450 /* copy input text to the allocated buffer */
451 memcpy(target + sizeof(padding), in_text, text_len);
452 }
453
454 /* Used in 3DES only */
455 ks_ptr[0] = ks;
456 ks_ptr[1] = ks2;
457 ks_ptr[2] = ks3;
458
459 while ((job = IMB_FLUSH_JOB(mb_mgr)) != NULL)
460 ;
461
462 job = IMB_GET_NEXT_JOB(mb_mgr);
463 job->cipher_direction = dir;
464 job->chain_order = order;
465 if (!in_place) {
466 job->dst = target + sizeof(padding);
467 job->src = in_text;
468 } else {
469 job->dst = target + sizeof(padding);
470 job->src = target + sizeof(padding);
471 }
472 job->cipher_mode = cipher;
473 if (cipher == DES3) {
474 job->aes_enc_key_expanded = (const void *) ks_ptr;
475 job->aes_dec_key_expanded = (const void *) ks_ptr;
476 job->aes_key_len_in_bytes = 24;
477 } else {
478 job->aes_enc_key_expanded = ks;
479 job->aes_dec_key_expanded = ks;
480 job->aes_key_len_in_bytes = 8;
481 }
482 job->iv = iv;
483 job->iv_len_in_bytes = 8;
484 job->cipher_start_src_offset_in_bytes = 0;
485 job->msg_len_to_cipher_in_bytes = text_len;
486
487 job->hash_alg = NULL_HASH;
488
489 if (arch == ARCH_AVX512) {
490 job = IMB_SUBMIT_JOB(mb_mgr);
491 if (job) {
492 printf("%d Unexpected return from submit_job\n",
493 __LINE__);
494 goto end;
495 }
496 job = IMB_FLUSH_JOB(mb_mgr);
497 if (!job) {
498 printf("%d Unexpected null return from flush_job\n",
499 __LINE__);
500 goto end;
501 }
502 } else {
503 job = IMB_SUBMIT_JOB(mb_mgr);
504 if (!job) {
505 printf("%d Unexpected null return from submit_job\n",
506 __LINE__);
507 goto end;
508 }
509 }
510 if (job->status != STS_COMPLETED) {
511 printf("%d Error status:%d", __LINE__, job->status);
512 goto end;
513 }
514 if (memcmp(out_text, target + sizeof(padding), text_len)) {
515 printf("mismatched\n");
516 goto end;
517 }
518 if (memcmp(padding, target, sizeof(padding))) {
519 printf("overwrite head\n");
520 goto end;
521 }
522 if (memcmp(padding, target + sizeof(padding) + text_len,
523 sizeof(padding))) {
524 printf("overwrite tail\n");
525 goto end;
526 }
527 ret = 0;
528 end:
529 while ((job = IMB_FLUSH_JOB(mb_mgr)) != NULL)
530 ;
531
532 free(target);
533 return ret;
534 }
535
536 static int
537 test_des(struct MB_MGR *mb_mgr,
538 const enum arch_type arch,
539 const uint64_t *ks,
540 const uint64_t *ks2,
541 const uint64_t *ks3,
542 const void *iv,
543 const uint8_t *in_text,
544 const uint8_t *out_text,
545 unsigned text_len,
546 int dir,
547 int order,
548 JOB_CIPHER_MODE cipher,
549 const int in_place)
550 {
551 int ret = 0;
552
553 if (cipher == DES3) {
554 if (ks2 == NULL && ks3 == NULL) {
555 ret |= test_des_one(mb_mgr, arch, ks, ks, ks, iv,
556 in_text, out_text, text_len, dir,
557 order, cipher, in_place);
558 ret |= test_des_many(mb_mgr, ks, ks, ks, iv, in_text,
559 out_text, text_len, dir, order,
560 cipher, in_place, 32);
561 } else {
562 ret |= test_des_one(mb_mgr, arch, ks, ks2, ks3, iv,
563 in_text, out_text, text_len, dir,
564 order, cipher, in_place);
565 ret |= test_des_many(mb_mgr, ks, ks2, ks3, iv, in_text,
566 out_text, text_len, dir, order,
567 cipher, in_place, 32);
568 }
569 } else {
570 ret |= test_des_one(mb_mgr, arch, ks, NULL, NULL, iv, in_text,
571 out_text, text_len, dir, order, cipher,
572 in_place);
573 ret |= test_des_many(mb_mgr, ks, NULL, NULL, iv, in_text,
574 out_text, text_len, dir, order, cipher,
575 in_place, 32);
576 }
577 return ret;
578 }
579
580 static int
581 test_des_vectors(struct MB_MGR *mb_mgr, const enum arch_type arch,
582 const int vec_cnt,
583 const struct des_vector *vec_tab, const char *banner,
584 const JOB_CIPHER_MODE cipher)
585 {
586 int vect, errors = 0;
587 uint64_t ks[16];
588
589 printf("%s:\n", banner);
590 for (vect = 0; vect < vec_cnt; vect++) {
591 #ifdef DEBUG
592 printf("Standard vector %d/%d PTLen:%d\n",
593 vect + 1, vec_cnt,
594 (int) vec_tab[vect].Plen);
595 #else
596 printf(".");
597 #endif
598 des_key_schedule(ks, vec_tab[vect].K);
599
600 if (test_des(mb_mgr, arch, ks, NULL, NULL,
601 vec_tab[vect].IV,
602 vec_tab[vect].P, vec_tab[vect].C,
603 (unsigned) vec_tab[vect].Plen,
604 ENCRYPT, CIPHER_HASH, cipher, 0)) {
605 printf("error #%d encrypt\n", vect + 1);
606 errors++;
607 }
608
609 if (test_des(mb_mgr, arch, ks, NULL, NULL,
610 vec_tab[vect].IV,
611 vec_tab[vect].C, vec_tab[vect].P,
612 (unsigned) vec_tab[vect].Plen,
613 DECRYPT, HASH_CIPHER, cipher, 0)) {
614 printf("error #%d decrypt\n", vect + 1);
615 errors++;
616 }
617
618 if (test_des(mb_mgr, arch, ks, NULL, NULL,
619 vec_tab[vect].IV,
620 vec_tab[vect].P, vec_tab[vect].C,
621 (unsigned) vec_tab[vect].Plen,
622 ENCRYPT, CIPHER_HASH, cipher, 1)) {
623 printf("error #%d encrypt in-place\n", vect + 1);
624 errors++;
625 }
626
627 if (test_des(mb_mgr, arch, ks, NULL, NULL,
628 vec_tab[vect].IV,
629 vec_tab[vect].C, vec_tab[vect].P,
630 (unsigned) vec_tab[vect].Plen,
631 DECRYPT, HASH_CIPHER, cipher, 1)) {
632 printf("error #%d decrypt in-place\n", vect + 1);
633 errors++;
634 }
635 }
636 printf("\n");
637 return errors;
638 }
639
640 static int
641 test_des3_vectors(struct MB_MGR *mb_mgr, const enum arch_type arch,
642 const int vec_cnt,
643 const struct des3_vector *vec_tab, const char *banner)
644 {
645 int vect, errors = 0;
646 uint64_t ks1[16];
647 uint64_t ks2[16];
648 uint64_t ks3[16];
649
650 printf("%s:\n", banner);
651 for (vect = 0; vect < vec_cnt; vect++) {
652 #ifdef DEBUG
653 printf("Standard vector %d/%d PTLen:%d\n",
654 vect + 1, vec_cnt,
655 (int) vec_tab[vect].Plen);
656 #else
657 printf(".");
658 #endif
659 des_key_schedule(ks1, vec_tab[vect].K1);
660 des_key_schedule(ks2, vec_tab[vect].K2);
661 des_key_schedule(ks3, vec_tab[vect].K3);
662
663 if (test_des(mb_mgr, arch, ks1, ks2, ks3,
664 vec_tab[vect].IV,
665 vec_tab[vect].P, vec_tab[vect].C,
666 (unsigned) vec_tab[vect].Plen,
667 ENCRYPT, CIPHER_HASH, DES3, 0)) {
668 printf("error #%d encrypt\n", vect + 1);
669 errors++;
670 }
671
672 if (test_des(mb_mgr, arch, ks1, ks2, ks3,
673 vec_tab[vect].IV,
674 vec_tab[vect].C, vec_tab[vect].P,
675 (unsigned) vec_tab[vect].Plen,
676 DECRYPT, HASH_CIPHER, DES3, 0)) {
677 printf("error #%d decrypt\n", vect + 1);
678 errors++;
679 }
680
681 if (test_des(mb_mgr, arch, ks1, ks2, ks3,
682 vec_tab[vect].IV,
683 vec_tab[vect].P, vec_tab[vect].C,
684 (unsigned) vec_tab[vect].Plen,
685 ENCRYPT, CIPHER_HASH, DES3, 1)) {
686 printf("error #%d encrypt in-place\n", vect + 1);
687 errors++;
688 }
689
690 if (test_des(mb_mgr, arch, ks1, ks2, ks3,
691 vec_tab[vect].IV,
692 vec_tab[vect].C, vec_tab[vect].P,
693 (unsigned) vec_tab[vect].Plen,
694 DECRYPT, HASH_CIPHER, DES3, 1)) {
695 printf("error #%d decrypt in-place\n", vect + 1);
696 errors++;
697 }
698 }
699 printf("\n");
700 return errors;
701 }
702
703 int
704 des_test(const enum arch_type arch,
705 struct MB_MGR *mb_mgr)
706 {
707 int errors;
708
709 errors = test_des_vectors(mb_mgr, arch, DIM(vectors), vectors,
710 "DES standard test vectors", DES);
711
712 errors += test_des_vectors(mb_mgr, arch, DIM(docsis_vectors),
713 docsis_vectors,
714 "DOCSIS DES standard test vectors",
715 DOCSIS_DES);
716
717 errors += test_des_vectors(mb_mgr, arch, DIM(vectors), vectors,
718 "3DES (single key) standard test vectors",
719 DES3);
720
721 errors += test_des3_vectors(mb_mgr, arch, DIM(des3_vectors),
722 des3_vectors,
723 "3DES (multiple keys) test vectors");
724
725 if (0 == errors)
726 printf("...Pass\n");
727 else
728 printf("...Fail\n");
729
730 return errors;
731 }