]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/intel-ipsec-mb/LibTestApp/zuc_test.c
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / spdk / intel-ipsec-mb / LibTestApp / zuc_test.c
1 /*****************************************************************************
2 Copyright (c) 2009-2019, 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
29 /*-----------------------------------------------------------------------
30 * Zuc functional test
31 *-----------------------------------------------------------------------
32 *
33 * A simple functional test for ZUC
34 *
35 *-----------------------------------------------------------------------*/
36
37 #include <assert.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include <intel-ipsec-mb.h>
43
44 #include "zuc_test_vectors.h"
45 #include "gcm_ctr_vectors_test.h"
46
47 #define MAXBUFS 9
48 #define PASS_STATUS 0
49 #define FAIL_STATUS -1
50
51 int zuc_test(const enum arch_type arch, struct MB_MGR *mb_mgr);
52
53 int validate_zuc_algorithm(struct MB_MGR *mb_mgr, uint8_t *pSrcData,
54 uint8_t *pDstData, uint8_t *pKeys, uint8_t *pIV);
55 int validate_zuc_EEA_1_block(struct MB_MGR *mb_mgr, uint8_t *pSrcData,
56 uint8_t *pDstData, uint8_t *pKeys, uint8_t *pIV);
57 int validate_zuc_EEA_4_block(struct MB_MGR *mb_mgr, uint8_t **pSrcData,
58 uint8_t **pDstData, uint8_t **pKeys,
59 uint8_t **pIV);
60 int validate_zuc_EEA_n_block(struct MB_MGR *mb_mgr, uint8_t **pSrcData,
61 uint8_t **pDstData, uint8_t **pKeys, uint8_t **pIV,
62 uint32_t numBuffs);
63 int validate_zuc_EIA_1_block(struct MB_MGR *mb_mgr, uint8_t *pSrcData,
64 uint8_t *pDstData, uint8_t *pKeys, uint8_t *pIV);
65 static void byte_hexdump(const char *message, const uint8_t *ptr, int len);
66
67 /******************************************************************************
68 * @ingroup zuc_functionalTest_app
69 *
70 * @description
71 * This function allocates memory for buffers and set random data in each buffer
72 *
73 * pSrcData = pointers to the new source buffers
74 * numOfBuffs = number of buffers
75 * ************************************************/
76 static uint32_t createData(uint8_t *pSrcData[MAXBUFS],
77 uint32_t numOfBuffs)
78 {
79 uint32_t i = 0, j = 0;
80
81 for (i = 0; i < numOfBuffs; i++) {
82 pSrcData[i] = (uint8_t *)malloc(MAX_BUFFER_LENGTH_IN_BYTES);
83
84 if (!pSrcData[i]) {
85 printf("malloc(pSrcData[i]): failed!\n");
86
87 for (j = 0; j < i; j++) {
88 free(pSrcData[j]);
89 pSrcData[j] = NULL;
90 }
91
92 return FAIL_STATUS;
93 }
94 }
95 return PASS_STATUS;
96 }
97
98 /******************************************************************************
99 * @ingroup zuc_functionalTest_app
100 *
101 * @description
102 * This function creates source data and vector buffers.
103 *
104 * keyLen = key length
105 * pKeys = array of pointers to the new key buffers
106 * ivLen = vector length
107 * pIV = array of pointers to the new vector buffers
108 * numOfBuffs = number of buffers
109 ************************************************/
110 static uint32_t createKeyVecData(uint32_t keyLen, uint8_t *pKeys[MAXBUFS],
111 uint32_t ivLen, uint8_t *pIV[MAXBUFS],
112 uint32_t numOfBuffs)
113 {
114 uint32_t i = 0, j = 0;
115
116 for (i = 0; i < numOfBuffs; i++) {
117 pIV[i] = (uint8_t *)malloc(ivLen);
118
119 if (!pIV[i]) {
120 printf("malloc(pIV[i]): failed!\n");
121
122 for (j = 0; j < i; j++) {
123 free(pIV[j]);
124 free(pKeys[j]);
125 }
126
127 return FAIL_STATUS;
128 }
129
130 pKeys[i] = malloc(keyLen);
131
132 if (!pKeys[i]) {
133 printf("malloc(pKeys[i]): failed!\n");
134
135 for (j = 0; j <= i; j++) {
136 free(pIV[j]);
137
138 if (j < i)
139 free(pKeys[j]);
140 }
141 return FAIL_STATUS;
142 }
143 }
144
145 return PASS_STATUS;
146 }
147
148 /******************************************************************************
149 * @ingroup zuc_benchmark_app
150 *
151 * @description
152 * This function free memory pointed to by an array of pointers
153 *
154 * arr = array of memory pointers
155 * length = length of pointer array (or number of pointers whose buffers
156 * should be freed)
157 * ************************************************/
158 static void freePtrArray(uint8_t *pArr[MAXBUFS], uint32_t arrayLength)
159 {
160 uint32_t i = 0;
161
162 for (i = 0; i < arrayLength; i++)
163 free(pArr[i]);
164 }
165
166 static uint32_t bswap4(const uint32_t val)
167 {
168 return ((val >> 24) | /**< A*/
169 ((val & 0xff0000) >> 8) | /**< B*/
170 ((val & 0xff00) << 8) | /**< C*/
171 (val << 24)); /**< D*/
172 }
173
174 int zuc_test(const enum arch_type arch, struct MB_MGR *mb_mgr)
175 {
176
177 uint32_t numBuffs, a;
178 uint32_t status = PASS_STATUS;
179 uint8_t *pKeys[MAXBUFS];
180 uint8_t *pIV[MAXBUFS];
181 uint8_t *pSrcData[MAXBUFS];
182 uint8_t *pDstData[MAXBUFS];
183
184 /* Do not run the tests for aesni emulation */
185 if (arch == ARCH_NO_AESNI)
186 return 0;
187
188 printf("Running Functional Tests\n");
189 fflush(stdout);
190
191 /*Create test data buffers + populate with random data*/
192 if (createData(pSrcData, MAXBUFS)) {
193 printf("createData() error\n");
194 return FAIL_STATUS;
195 }
196 if (createData(pDstData, MAXBUFS)) {
197 printf("createData() error\n");
198 return FAIL_STATUS;
199 }
200
201 /*Create random keys and vectors*/
202 if (createKeyVecData(ZUC_KEY_LEN_IN_BYTES, pKeys, ZUC_IV_LEN_IN_BYTES,
203 pIV, MAXBUFS)) {
204 printf("createKeyVecData() error\n");
205 freePtrArray(pSrcData, MAXBUFS);
206 freePtrArray(pDstData, MAXBUFS);
207 return FAIL_STATUS;
208 }
209
210 if (validate_zuc_algorithm(mb_mgr, pSrcData[0], pSrcData[0], pKeys[0],
211 pIV[0]))
212 status = 1;
213 else
214 printf("validate ZUC algorithm: PASS\n");
215
216 if (validate_zuc_EEA_1_block(mb_mgr, pSrcData[0], pSrcData[0], pKeys[0],
217 pIV[0]))
218 status = 1;
219 else
220 printf("validate ZUC 1 block: PASS\n");
221
222 if (validate_zuc_EEA_4_block(mb_mgr, pSrcData, pSrcData, pKeys, pIV))
223 status = 1;
224 else
225 printf("validate ZUC 4 block: PASS\n");
226
227 for (a = 0; a < 3; a++) {
228 switch (a) {
229 case 0:
230 numBuffs = 4;
231 break;
232 case 1:
233 numBuffs = 8;
234 break;
235 default:
236 numBuffs = 9;
237 break;
238 }
239 if (validate_zuc_EEA_n_block(mb_mgr, pSrcData, pDstData, pKeys,
240 pIV, numBuffs))
241 status = 1;
242 else
243 printf("validate ZUC n block buffers %d: PASS\n", a);
244 }
245
246 if (validate_zuc_EIA_1_block(mb_mgr, pSrcData[0], pDstData[0], pKeys[0],
247 pIV[0]))
248 status = 1;
249 else
250 printf("validate ZUC Integrity 1 block: PASS\n");
251
252 freePtrArray(pKeys, MAXBUFS); /*Free the key buffers*/
253 freePtrArray(pIV, MAXBUFS); /*Free the vector buffers*/
254 freePtrArray(pSrcData, MAXBUFS); /*Free the source buffers*/
255 freePtrArray(pDstData, MAXBUFS); /*Free the destination buffers*/
256 if (status)
257 return status;
258
259 printf("The Functional Test application completed\n");
260 return 0;
261 }
262
263 int validate_zuc_EEA_1_block(struct MB_MGR *mb_mgr, uint8_t *pSrcData,
264 uint8_t *pDstData, uint8_t *pKeys, uint8_t *pIV)
265 {
266 uint32_t i, byteResidue;
267 int retTmp, ret = 0;
268 uint32_t byteLength;
269 uint32_t bitResidue;
270
271 for (i = 0; i < NUM_ZUC_EEA3_TESTS; i++) {
272 memcpy(pKeys, testEEA3_vectors[i].CK, ZUC_KEY_LEN_IN_BYTES);
273 zuc_eea3_iv_gen(testEEA3_vectors[i].count,
274 testEEA3_vectors[i].Bearer,
275 testEEA3_vectors[i].Direction,
276 pIV);
277 byteLength = (testEEA3_vectors[i].length_in_bits + 7) / 8;
278 memcpy(pSrcData, testEEA3_vectors[i].plaintext, byteLength);
279 IMB_ZUC_EEA3_1_BUFFER(mb_mgr, pKeys, pIV, pSrcData, pDstData,
280 byteLength);
281 retTmp = memcmp(pDstData, testEEA3_vectors[i].ciphertext,
282 byteLength - 1);
283 if (retTmp) {
284 printf("Validate ZUC 1 block test %d (Enc): FAIL\n",
285 i + 1);
286 byte_hexdump("Expected", testEEA3_vectors[i].ciphertext,
287 byteLength);
288 byte_hexdump("Found", pDstData, byteLength);
289 ret = retTmp;
290 } else {
291 bitResidue =
292 (0xFF00 >>
293 (testEEA3_vectors[i].length_in_bits % 8)) &
294 0x00FF;
295 byteResidue =
296 (testEEA3_vectors[i].ciphertext
297 [testEEA3_vectors[i].length_in_bits / 8] ^
298 pDstData[testEEA3_vectors[i].length_in_bits / 8]) &
299 bitResidue;
300 if (byteResidue) {
301 printf("Validate ZUC 1 block test %d (Enc): "
302 "FAIL\n",
303 i + 1);
304 printf("Expected: 0x%02X (last byte)\n",
305 0xFF &
306 testEEA3_vectors[i]
307 .ciphertext[testEEA3_vectors[i]
308 .length_in_bits /
309 8]);
310 printf("Found: 0x%02X (last byte)\n",
311 0xFF & pDstData[testEEA3_vectors[i]
312 .length_in_bits /
313 8]);
314 } else
315 printf("Validate ZUC 1 block test %d (Enc): "
316 "PASS\n",
317 i + 1);
318 }
319 fflush(stdout);
320 }
321 return ret;
322 };
323 int validate_zuc_EEA_4_block(struct MB_MGR *mb_mgr, uint8_t **pSrcData,
324 uint8_t **pDstData, uint8_t **pKeys, uint8_t **pIV)
325 {
326 uint32_t i, j, packetLen[4], bitResidue, byteResidue;
327 int retTmp, ret = 0;
328
329 for (i = 0; i < NUM_ZUC_EEA3_TESTS; i++) {
330 for (j = 0; j < 4; j++) {
331 packetLen[j] =
332 (testEEA3_vectors[i].length_in_bits + 7) / 8;
333 memcpy(pKeys[j], testEEA3_vectors[i].CK,
334 ZUC_KEY_LEN_IN_BYTES);
335 zuc_eea3_iv_gen(testEEA3_vectors[i].count,
336 testEEA3_vectors[i].Bearer,
337 testEEA3_vectors[i].Direction,
338 pIV[j]);
339 memcpy(pSrcData[j], testEEA3_vectors[i].plaintext,
340 packetLen[j]);
341 }
342 IMB_ZUC_EEA3_4_BUFFER(mb_mgr, (const void * const *)pKeys,
343 (const void * const *)pIV,
344 (const void * const *)pSrcData,
345 (void **)pDstData, packetLen);
346 uint8_t *pDst8 = (uint8_t *)pDstData[0];
347
348 retTmp = memcmp(pDst8, testEEA3_vectors[i].ciphertext,
349 (testEEA3_vectors[i].length_in_bits) / 8);
350 if (retTmp) {
351 printf("Validate ZUC 4 block (Enc) test %d: FAIL\n",
352 i + 1);
353 byte_hexdump("Expected", testEEA3_vectors[i].ciphertext,
354 (testEEA3_vectors[i].length_in_bits + 7) /
355 8);
356 byte_hexdump("Found", pDst8,
357 (testEEA3_vectors[i].length_in_bits + 7) /
358 8);
359 ret = retTmp;
360 } else {
361 bitResidue =
362 (0xFF00 >>
363 (testEEA3_vectors[i].length_in_bits % 8)) &
364 0x00FF;
365 byteResidue =
366 (testEEA3_vectors[i].ciphertext
367 [testEEA3_vectors[i].length_in_bits / 8] ^
368 pDst8[testEEA3_vectors[i].length_in_bits / 8]) &
369 bitResidue;
370 if (byteResidue) {
371 ret = 1;
372 printf("Validate ZUC 4 block test %d (Enc): "
373 "FAIL\n",
374 i + 1);
375 printf("Expected: 0x%02X (last byte)\n",
376 0xFF &
377 testEEA3_vectors[i]
378 .ciphertext[testEEA3_vectors[i]
379 .length_in_bits /
380 8]);
381 printf("Found: 0x%02X (last byte)\n",
382 0xFF & pDst8[testEEA3_vectors[i]
383 .length_in_bits /
384 8]);
385 } else
386 printf("Validate ZUC 4 block test %d (Enc): "
387 "PASS\n",
388 i + 1);
389 }
390 fflush(stdout);
391 for (j = 0; j < 4; j++) {
392 memcpy(pSrcData[j], testEEA3_vectors[i].ciphertext,
393 (testEEA3_vectors[i].length_in_bits + 7) / 8);
394 }
395 IMB_ZUC_EEA3_4_BUFFER(mb_mgr, (const void * const *)pKeys,
396 (const void * const *)pIV,
397 (const void * const *)pSrcData,
398 (void **)pDstData, packetLen);
399 pDst8 = (uint8_t *)pDstData[0];
400 retTmp = memcmp(pDst8, testEEA3_vectors[i].plaintext,
401 (testEEA3_vectors[i].length_in_bits) / 8);
402 if (retTmp) {
403 printf("Validate ZUC 4 block (Dec) test %d: FAIL\n",
404 i + 1);
405 byte_hexdump("Expected", testEEA3_vectors[i].plaintext,
406 (testEEA3_vectors[i].length_in_bits + 7) /
407 8);
408 byte_hexdump("Found", pDst8,
409 (testEEA3_vectors[i].length_in_bits + 7) /
410 8);
411 ret = retTmp;
412 } else {
413 bitResidue =
414 (0xFF00 >>
415 (testEEA3_vectors[i].length_in_bits % 8)) &
416 0x00FF;
417 byteResidue =
418 (testEEA3_vectors[i]
419 .plaintext[testEEA3_vectors[i].length_in_bits /
420 8] ^
421 pDst8[testEEA3_vectors[i].length_in_bits / 8]) &
422 bitResidue;
423 if (byteResidue) {
424 ret = 1;
425 printf("Validate ZUC 4 block test %d (Dec): "
426 "FAIL\n",
427 i + 1);
428 printf("Expected: 0x%02X (last byte)\n",
429 0xFF &
430 testEEA3_vectors[i]
431 .plaintext[testEEA3_vectors[i]
432 .length_in_bits /
433 8]);
434 printf("Found: 0x%02X (last byte)\n",
435 0xFF & pDst8[testEEA3_vectors[i]
436 .length_in_bits /
437 8]);
438 } else
439 printf("Validate ZUC 4 block test %d (Dec): "
440 "PASS\n",
441 i + 1);
442 }
443 fflush(stdout);
444 }
445 return ret;
446 };
447
448 int validate_zuc_EEA_n_block(struct MB_MGR *mb_mgr, uint8_t **pSrcData,
449 uint8_t **pDstData, uint8_t **pKeys, uint8_t **pIV,
450 uint32_t numBuffs)
451 {
452 uint32_t i, j, bitResidue, byteResidue;
453 int retTmp, ret = 0;
454 uint32_t packetLen[MAXBUFS];
455
456 assert(numBuffs > 0);
457 for (i = 0; i < NUM_ZUC_EEA3_TESTS; i++) {
458 for (j = 0; j <= (numBuffs - 1); j++) {
459 memcpy(pKeys[j], testEEA3_vectors[i].CK,
460 ZUC_KEY_LEN_IN_BYTES);
461 zuc_eea3_iv_gen(testEEA3_vectors[i].count,
462 testEEA3_vectors[i].Bearer,
463 testEEA3_vectors[i].Direction,
464 pIV[j]);
465 memcpy(pSrcData[j], testEEA3_vectors[i].plaintext,
466 (testEEA3_vectors[i].length_in_bits + 7) / 8);
467 packetLen[j] =
468 (testEEA3_vectors[i].length_in_bits + 7) / 8;
469 }
470 IMB_ZUC_EEA3_N_BUFFER(mb_mgr, (const void * const *)pKeys,
471 (const void * const *)pIV,
472 (const void * const *)pSrcData,
473 (void **)pDstData, packetLen, numBuffs);
474 uint8_t *pDst8 = (uint8_t *)pDstData[0];
475
476 retTmp = memcmp(pDstData[0], testEEA3_vectors[i].ciphertext,
477 (testEEA3_vectors[i].length_in_bits) / 8);
478 if (retTmp) {
479 printf("Validate ZUC n block (Enc) test %d, buffers: "
480 "%d: FAIL\n",
481 i + 1, numBuffs);
482 byte_hexdump("Expected", testEEA3_vectors[i].ciphertext,
483 (testEEA3_vectors[i].length_in_bits + 7) /
484 8);
485 byte_hexdump("Found", pDst8,
486 (testEEA3_vectors[i].length_in_bits + 7) /
487 8);
488 ret = retTmp;
489 } else {
490 bitResidue =
491 (0xFF00 >>
492 (testEEA3_vectors[i].length_in_bits % 8)) &
493 0x00FF;
494 byteResidue =
495 (testEEA3_vectors[i].ciphertext
496 [testEEA3_vectors[i].length_in_bits / 8] ^
497 pDst8[testEEA3_vectors[i].length_in_bits / 8]) &
498 bitResidue;
499 if (byteResidue) {
500 ret = 1;
501 printf("Validate ZUC n block (Enc) test %d, "
502 "buffers %d: FAIL\n",
503 i + 1, numBuffs);
504 printf("Expected: 0x%02X (last byte)\n",
505 0xFF &
506 testEEA3_vectors[i]
507 .ciphertext[testEEA3_vectors[i]
508 .length_in_bits /
509 8]);
510 printf("Found: 0x%02X (last byte)\n",
511 0xFF & pDst8[testEEA3_vectors[i]
512 .length_in_bits /
513 8]);
514 } else
515 printf("Validate ZUC n block (Enc) test %d, "
516 "buffers %d: PASS\n",
517 i + 1, numBuffs);
518 }
519 fflush(stdout);
520 for (j = 0; j <= (numBuffs - 1); j++) {
521 memcpy(pSrcData[j], testEEA3_vectors[i].ciphertext,
522 (testEEA3_vectors[i].length_in_bits + 7) / 8);
523 }
524 IMB_ZUC_EEA3_N_BUFFER(mb_mgr, (const void * const *)pKeys,
525 (const void * const *)pIV,
526 (const void * const *)pSrcData,
527 (void **)pDstData, packetLen, numBuffs);
528 retTmp = memcmp(pDstData[0], testEEA3_vectors[i].plaintext,
529 (testEEA3_vectors[i].length_in_bits) / 8);
530 if (retTmp) {
531 printf("Validate ZUC n block (Dec) test %d, buffers "
532 "%d: FAIL\n",
533 i + 1, numBuffs);
534 byte_hexdump("Expected", testEEA3_vectors[i].plaintext,
535 (testEEA3_vectors[i].length_in_bits + 7) /
536 8);
537 byte_hexdump("Found", pDstData[0],
538 (testEEA3_vectors[i].length_in_bits + 7) /
539 8);
540 ret = retTmp;
541 } else {
542 bitResidue =
543 (0xFF00 >>
544 (testEEA3_vectors[i].length_in_bits % 8)) &
545 0x00FF;
546 byteResidue =
547 (testEEA3_vectors[i]
548 .plaintext[testEEA3_vectors[i].length_in_bits /
549 8] ^
550 pDst8[testEEA3_vectors[i].length_in_bits / 8]) &
551 bitResidue;
552 if (byteResidue) {
553 ret = 1;
554 printf("Validate ZUC n block (Dec) test %d, "
555 "buffers %d : FAIL\n",
556 i + 1, numBuffs);
557 printf("Expected: 0x%02X (last byte)\n",
558 0xFF &
559 testEEA3_vectors[i]
560 .plaintext[testEEA3_vectors[i]
561 .length_in_bits /
562 8]);
563 printf("Found: 0x%02X (last byte)\n",
564 0xFF & pDst8[testEEA3_vectors[i]
565 .length_in_bits /
566 8]);
567 } else
568 printf("Validate ZUC n block (Dec) test %d, "
569 "buffers %d: PASS\n",
570 i + 1, numBuffs);
571 }
572 fflush(stdout);
573 }
574 return ret;
575 };
576
577 int validate_zuc_EIA_1_block(struct MB_MGR *mb_mgr, uint8_t *pSrcData,
578 uint8_t *pDstData, uint8_t *pKeys, uint8_t *pIV)
579 {
580 uint32_t i;
581 int retTmp, ret = 0;
582 uint32_t byteLength;
583
584 for (i = 0; i < NUM_ZUC_EIA3_TESTS; i++) {
585 memcpy(pKeys, testEIA3_vectors[i].CK, ZUC_KEY_LEN_IN_BYTES);
586
587 zuc_eia3_iv_gen(testEIA3_vectors[i].count,
588 testEIA3_vectors[i].Bearer,
589 testEIA3_vectors[i].Direction,
590 pIV);
591 byteLength = (testEIA3_vectors[i].length_in_bits + 7) / 8;
592 memcpy(pSrcData, testEIA3_vectors[i].message, byteLength);
593 IMB_ZUC_EIA3_1_BUFFER(mb_mgr, pKeys, pIV, pSrcData,
594 testEIA3_vectors[i].length_in_bits,
595 (uint32_t *)pDstData);
596 retTmp =
597 memcmp(pDstData, &testEIA3_vectors[i].mac,
598 sizeof(((struct test128EIA3_vectors_t *)0)->mac));
599 if (retTmp) {
600 printf("Validate ZUC 1 block test %d (Int): FAIL\n",
601 i + 1);
602 byte_hexdump("Expected",
603 (const uint8_t *)&testEIA3_vectors[i].mac,
604 ZUC_DIGEST_LEN);
605 byte_hexdump("Found", pDstData, ZUC_DIGEST_LEN);
606 ret = retTmp;
607 } else
608 printf("Validate ZUC 1 block test %d (Int): PASS\n",
609 i + 1);
610 fflush(stdout);
611 }
612 return ret;
613 };
614
615 int validate_zuc_algorithm(struct MB_MGR *mb_mgr, uint8_t *pSrcData,
616 uint8_t *pDstData, uint8_t *pKeys, uint8_t *pIV)
617 {
618 uint32_t i;
619 int ret = 0;
620 union SwapBytes {
621 uint8_t sbb[8];
622 uint32_t sbw[2];
623 } swapBytes;
624
625 for (i = 0; i < NUM_ZUC_ALG_TESTS; i++) {
626 memcpy(pKeys, testZUC_vectors[i].CK, ZUC_KEY_LEN_IN_BYTES);
627 memcpy(pIV, testZUC_vectors[i].IV, ZUC_IV_LEN_IN_BYTES);
628 memset(pSrcData, 0, 8);
629 IMB_ZUC_EEA3_1_BUFFER(mb_mgr, pKeys, pIV, pSrcData, pDstData,
630 8);
631 swapBytes.sbw[0] = bswap4(testZUC_vectors[i].Z[0]);
632 swapBytes.sbw[1] = bswap4(testZUC_vectors[i].Z[1]);
633 ret = memcmp(pDstData, swapBytes.sbb, 8);
634 if (ret)
635 printf("ZUC 1 algorithm test %d: FAIL\n", i);
636 else
637 printf("ZUC 1 algorithm test %d: PASS\n", i);
638 }
639 return ret;
640 };
641 /*****************************************************************************
642 ** @description - utility function to dump test buffers$
643 ** $
644 ** @param message [IN] - debug message to print$
645 ** @param ptr [IN] - pointer to beginning of buffer.$
646 ** @param len [IN] - length of buffer.$
647 *****************************************************************************/
648 static void byte_hexdump(const char *message, const uint8_t *ptr, int len)
649 {
650 int ctr;
651
652 printf("%s:\n", message);
653 for (ctr = 0; ctr < len; ctr++) {
654 printf("0x%02X ", ptr[ctr] & 0xff);
655 if (!((ctr + 1) % 16))
656 printf("\n");
657 }
658 printf("\n");
659 printf("\n");
660 };