xref: /isa-l_crypto/sm3_mb/sm3_mb_rand_test.c (revision e3f7d4fb1bd3dcbfb75b87e0c61870e751d613aa)
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 
34 #define TEST_LEN  (1024*1024)
35 #define TEST_BUFS 100
36 #ifndef RANDOMS
37 # define RANDOMS  10
38 #endif
39 #ifndef TEST_SEED
40 # define TEST_SEED 0x1234
41 #endif
42 
43 static uint8_t digest_ref[TEST_BUFS][4 * SM3_DIGEST_NWORDS];
44 
45 // Compare against reference function
46 extern void sm3_ossl(const unsigned char *buf, size_t length, unsigned char *digest);
47 
48 // Generates pseudo-random data
49 static void rand_buffer(unsigned char *buf, const long buffer_size)
50 {
51 	long i;
52 	for (i = 0; i < buffer_size; i++)
53 		buf[i] = rand();
54 }
55 
56 int main(void)
57 {
58 	SM3_HASH_CTX_MGR *mgr = NULL;
59 	SM3_HASH_CTX ctxpool[TEST_BUFS];
60 	uint32_t i, j, fail = 0;
61 	unsigned char *bufs[TEST_BUFS];
62 	uint32_t lens[TEST_BUFS];
63 	unsigned int jobs, t;
64 	uint8_t *tmp_buf;
65 
66 	printf("multibinary_sm3 test, %d sets of %dx%d max: ", RANDOMS, TEST_BUFS, TEST_LEN);
67 
68 	posix_memalign((void *)&mgr, 16, sizeof(SM3_HASH_CTX_MGR));
69 	sm3_ctx_mgr_init(mgr);
70 
71 	srand(TEST_SEED);
72 
73 	for (i = 0; i < TEST_BUFS; i++) {
74 		// Allocate  and fill buffer
75 		bufs[i] = (unsigned char *)malloc(TEST_LEN);
76 		if (bufs[i] == NULL) {
77 			printf("malloc failed test aborted\n");
78 			return 1;
79 		}
80 		rand_buffer(bufs[i], TEST_LEN);
81 
82 		// Init ctx contexts
83 		hash_ctx_init(&ctxpool[i]);
84 		ctxpool[i].user_data = (void *)((uint64_t) i);
85 
86 		// Run reference test
87 		sm3_ossl(bufs[i], TEST_LEN, digest_ref[i]);
88 
89 		// Run sb_sm3 test
90 		sm3_ctx_mgr_submit(mgr, &ctxpool[i], bufs[i], TEST_LEN, HASH_ENTIRE);
91 	}
92 
93 	while (sm3_ctx_mgr_flush(mgr)) ;
94 
95 	for (i = 0; i < TEST_BUFS; i++) {
96 		for (j = 0; j < SM3_DIGEST_NWORDS; j++) {
97 			if (ctxpool[i].job.result_digest[j] !=
98 			    to_le32(((uint32_t *) digest_ref[i])[j])) {
99 				fail++;
100 				printf("Test%d fixed size, digest%d "
101 				       "fail 0x%08X <=> 0x%08X \n",
102 				       i, j, ctxpool[i].job.result_digest[j],
103 				       to_le32(((uint32_t *) digest_ref[i])[j]));
104 			}
105 		}
106 	}
107 
108 	if (fail) {
109 		printf("Test failed function check %d\n", fail);
110 		return fail;
111 	}
112 	// Run tests with random size and number of jobs
113 	for (t = 0; t < RANDOMS; t++) {
114 		jobs = rand() % (TEST_BUFS);
115 
116 		sm3_ctx_mgr_init(mgr);
117 
118 		for (i = 0; i < jobs; i++) {
119 			// Use buffer with random len and contents
120 			lens[i] = rand() % (TEST_LEN);
121 			rand_buffer(bufs[i], lens[i]);
122 
123 			// Run reference test
124 			sm3_ossl(bufs[i], lens[i], digest_ref[i]);
125 
126 			// Run sm3_mb test
127 			sm3_ctx_mgr_submit(mgr, &ctxpool[i], bufs[i], lens[i], HASH_ENTIRE);
128 		}
129 
130 		while (sm3_ctx_mgr_flush(mgr)) ;
131 
132 		for (i = 0; i < jobs; i++) {
133 			for (j = 0; j < SM3_DIGEST_NWORDS; j++) {
134 				if (ctxpool[i].job.result_digest[j] !=
135 				    to_le32(((uint32_t *) digest_ref[i])[j])) {
136 					fail++;
137 					printf("Test%d, digest%d fail "
138 					       "0x%08X <=> 0x%08X\n",
139 					       i, j, ctxpool[i].job.result_digest[j],
140 					       to_le32(((uint32_t *) digest_ref[i])[j]));
141 				}
142 			}
143 		}
144 		if (fail) {
145 			printf("Test failed function check %d\n", fail);
146 			return fail;
147 		}
148 
149 		putchar('.');
150 		fflush(0);
151 	}			// random test t
152 
153 	// Test at the end of buffer
154 	jobs = rand() % TEST_BUFS;
155 	tmp_buf = (uint8_t *) malloc(sizeof(uint8_t) * jobs);
156 	if (!tmp_buf) {
157 		printf("malloc failed, end test aborted.\n");
158 		return 1;
159 	}
160 
161 	rand_buffer(tmp_buf, jobs);
162 
163 	sm3_ctx_mgr_init(mgr);
164 
165 	// Extend to the end of allocated buffer to construct jobs
166 	for (i = 0; i < jobs; i++) {
167 		bufs[i] = (uint8_t *) & tmp_buf[i];
168 		lens[i] = jobs - i;
169 
170 		// Reference test
171 		sm3_ossl(bufs[i], lens[i], digest_ref[i]);
172 
173 		// sb_sm3 test
174 		sm3_ctx_mgr_submit(mgr, &ctxpool[i], bufs[i], lens[i], HASH_ENTIRE);
175 	}
176 
177 	while (sm3_ctx_mgr_flush(mgr)) ;
178 
179 	for (i = 0; i < jobs; i++) {
180 		for (j = 0; j < SM3_DIGEST_NWORDS; j++) {
181 			if (ctxpool[i].job.result_digest[j] !=
182 			    to_le32(((uint32_t *) digest_ref[i])[j])) {
183 				fail++;
184 				printf("End test failed at offset %d - result: 0x%08X"
185 				       ", ref: 0x%08X\n", i, ctxpool[i].job.result_digest[j],
186 				       to_le32(((uint32_t *) digest_ref[i])[j]));
187 			}
188 		}
189 	}
190 
191 	putchar('.');
192 
193 	if (fail)
194 		printf("Test failed function check %d\n", fail);
195 	else
196 		printf(" multibinary_sm3 rand: Pass\n");
197 
198 	return fail;
199 }
200