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