xref: /isa-l_crypto/sm3_mb/sm3_mb_flush_test.c (revision a4de01928f9e63e0ef03e8ef48b1b7f5de29b7ec)
1 /**********************************************************************
2   Copyright(c) 2011-2019 Intel Corporation All rights reserved.
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
6   are met:
7     * Redistributions of source code must retain the above copyright
8       notice, 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
11       the documentation and/or other materials provided with the
12       distribution.
13     * Neither the name of Intel Corporation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29 #define ISAL_UNIT_TEST
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include "sm3_mb.h"
33 #include "endian_helper.h"
34 
35 #define TEST_LEN  (1024*1024)
36 #define TEST_BUFS (SM3_MAX_LANES - 1)
37 #ifndef TEST_SEED
38 # define TEST_SEED 0x1234
39 #endif
40 
41 static uint8_t digest_ref[TEST_BUFS][4 * SM3_DIGEST_NWORDS];
42 
43 // Compare against reference function
44 extern void sm3_ossl(const unsigned char *buf, size_t length, unsigned char *digest);
45 
46 // Generates pseudo-random data
47 void rand_buffer(unsigned char *buf, const long buffer_size)
48 {
49 	long i;
50 	for (i = 0; i < buffer_size; i++)
51 		buf[i] = rand();
52 }
53 
54 uint8_t lens_print_and_check(SM3_HASH_CTX_MGR * mgr)
55 {
56 	static int32_t last_lens[SM3_MAX_LANES] = { 0 };
57 	int32_t len;
58 	uint8_t num_unchanged = 0;
59 	int i;
60 	for (i = 0; i < SM3_MAX_LANES; i++) {
61 		len = (int32_t) mgr->mgr.lens[i];
62 		// len[i] in mgr consists of byte_length<<4 | lane_index
63 		len = (len >= 16) ? (len >> 4 << 6) : 0;
64 		printf("\t%d", len);
65 		if (last_lens[i] > 0 && last_lens[i] == len)
66 			num_unchanged += 1;
67 		last_lens[i] = len;
68 	}
69 	printf("\n");
70 	return num_unchanged;
71 }
72 
73 int main(void)
74 {
75 	SM3_HASH_CTX_MGR *mgr = NULL;
76 	SM3_HASH_CTX ctxpool[TEST_BUFS];
77 	uint32_t i, j, fail = 0;
78 	unsigned char *bufs[TEST_BUFS];
79 	uint32_t lens[TEST_BUFS];
80 	uint8_t num_ret, num_unchanged = 0;
81 	int ret;
82 
83 	printf("sm3_mb flush test, %d buffers with %d length: \n", TEST_BUFS, TEST_LEN);
84 
85 	ret = posix_memalign((void *)&mgr, 16, sizeof(SM3_HASH_CTX_MGR));
86 	if ((ret != 0) || (mgr == NULL)) {
87 		printf("posix_memalign failed test aborted\n");
88 		return 1;
89 	}
90 
91 	sm3_ctx_mgr_init(mgr);
92 
93 	srand(TEST_SEED);
94 
95 	for (i = 0; i < TEST_BUFS; i++) {
96 		// Allocate  and fill buffer
97 		lens[i] = TEST_LEN / SM3_MAX_LANES * (i + 1);
98 		bufs[i] = (unsigned char *)malloc(lens[i]);
99 		if (bufs[i] == NULL) {
100 			printf("malloc failed test aborted\n");
101 			fail++;
102 			goto end;
103 		}
104 		rand_buffer(bufs[i], lens[i]);
105 	}
106 
107 	for (i = 0; i < TEST_BUFS; i++) {
108 		// Init ctx contexts
109 		hash_ctx_init(&ctxpool[i]);
110 		ctxpool[i].user_data = (void *)((uint64_t) i);
111 
112 		// Run reference test
113 		sm3_ossl(bufs[i], lens[i], digest_ref[i]);
114 
115 		// Run sb_sm3 test
116 		sm3_ctx_mgr_submit(mgr, &ctxpool[i], bufs[i], lens[i], HASH_ENTIRE);
117 	}
118 
119 	printf("Changes of lens inside mgr:\n");
120 	lens_print_and_check(mgr);
121 	while (sm3_ctx_mgr_flush(mgr)) {
122 		num_ret = lens_print_and_check(mgr);
123 		num_unchanged = num_unchanged > num_ret ? num_unchanged : num_ret;
124 	}
125 	printf("Info of sm3_mb lens prints over\n");
126 
127 	for (i = 0; i < TEST_BUFS; i++) {
128 		for (j = 0; j < SM3_DIGEST_NWORDS; j++) {
129 			if (ctxpool[i].job.result_digest[j] !=
130 			    to_le32(((uint32_t *) digest_ref[i])[j])) {
131 				fail++;
132 				printf("Test%d fixed size, digest%d "
133 				       "fail 0x%08X <=> 0x%08X \n",
134 				       i, j, ctxpool[i].job.result_digest[j],
135 				       to_le32(((uint32_t *) digest_ref[i])[j]));
136 			}
137 		}
138 	}
139 
140 	if (fail)
141 		printf("Test failed function check %d\n", fail);
142 	else
143 		printf("Pass\n");
144 
145       end:
146 	for (i = 0; i < TEST_BUFS; i++)
147 		free(bufs[i]);
148 	aligned_free(mgr);
149 
150 	return fail;
151 }
152