]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /********************************************************************** |
2 | Copyright(c) 2011-2016 Intel Corporation All rights reserved. | |
3 | ||
4 | Redistribution and use in source and binary forms, with or without | |
1e59de90 | 5 | modification, are permitted provided that the following conditions |
7c673cae FG |
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 "sha1_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 | #define UPDATE_SIZE 13*SHA1_BLOCK_SIZE | |
44 | #define MAX_RAND_UPDATE_BLOCKS (TEST_LEN/(16*SHA1_BLOCK_SIZE)) | |
45 | ||
46 | #ifdef DEBUG | |
47 | # define debug_char(x) putchar(x) | |
48 | #else | |
49 | # define debug_char(x) do {} while (0) | |
50 | #endif | |
51 | ||
52 | /* Reference digest global to reduce stack usage */ | |
53 | static uint32_t digest_ref[TEST_BUFS][SHA1_DIGEST_NWORDS]; | |
54 | ||
55 | extern void sha1_ref(uint8_t * input_data, uint32_t * digest, uint32_t len); | |
56 | ||
57 | // Generates pseudo-random data | |
58 | ||
59 | void rand_buffer(unsigned char *buf, const long buffer_size) | |
60 | { | |
61 | long i; | |
62 | for (i = 0; i < buffer_size; i++) | |
63 | buf[i] = rand(); | |
64 | } | |
65 | ||
66 | int main(void) | |
67 | { | |
68 | SHA1_HASH_CTX_MGR *mgr = NULL; | |
69 | SHA1_HASH_CTX ctxpool[TEST_BUFS], *ctx = NULL; | |
70 | uint32_t i, j, fail = 0; | |
71 | int len_done, len_rem, len_rand; | |
72 | unsigned char *bufs[TEST_BUFS]; | |
73 | unsigned char *buf_ptr[TEST_BUFS]; | |
74 | uint32_t lens[TEST_BUFS]; | |
75 | unsigned int joblen, jobs, t; | |
1e59de90 | 76 | int ret; |
7c673cae FG |
77 | |
78 | printf("multibinary_sha1_update test, %d sets of %dx%d max: ", RANDOMS, TEST_BUFS, | |
79 | TEST_LEN); | |
80 | ||
81 | srand(TEST_SEED); | |
82 | ||
1e59de90 TL |
83 | ret = posix_memalign((void *)&mgr, 16, sizeof(SHA1_HASH_CTX_MGR)); |
84 | if ((ret != 0) || (mgr == NULL)) { | |
85 | printf("posix_memalign failed test aborted\n"); | |
86 | return 1; | |
87 | } | |
88 | ||
7c673cae FG |
89 | sha1_ctx_mgr_init(mgr); |
90 | ||
91 | for (i = 0; i < TEST_BUFS; i++) { | |
92 | // Allocte and fill buffer | |
93 | bufs[i] = (unsigned char *)malloc(TEST_LEN); | |
94 | buf_ptr[i] = bufs[i]; | |
95 | if (bufs[i] == NULL) { | |
96 | printf("malloc failed test aborted\n"); | |
97 | return 1; | |
98 | } | |
99 | rand_buffer(bufs[i], TEST_LEN); | |
100 | ||
101 | // Init ctx contents | |
102 | hash_ctx_init(&ctxpool[i]); | |
103 | ctxpool[i].user_data = (void *)((uint64_t) i); | |
104 | ||
105 | // Run reference test | |
106 | sha1_ref(bufs[i], digest_ref[i], TEST_LEN); | |
107 | } | |
108 | ||
109 | // Run sb_sha1 tests | |
110 | for (i = 0; i < TEST_BUFS;) { | |
111 | len_done = (int)((unsigned long)buf_ptr[i] - (unsigned long)bufs[i]); | |
112 | len_rem = TEST_LEN - len_done; | |
113 | ||
114 | if (len_done == 0) | |
115 | ctx = sha1_ctx_mgr_submit(mgr, | |
116 | &ctxpool[i], | |
117 | buf_ptr[i], UPDATE_SIZE, HASH_FIRST); | |
118 | else if (len_rem <= UPDATE_SIZE) | |
119 | ctx = sha1_ctx_mgr_submit(mgr, | |
120 | &ctxpool[i], buf_ptr[i], len_rem, HASH_LAST); | |
121 | else | |
122 | ctx = sha1_ctx_mgr_submit(mgr, | |
123 | &ctxpool[i], | |
124 | buf_ptr[i], UPDATE_SIZE, HASH_UPDATE); | |
125 | ||
126 | // Add jobs while available or finished | |
127 | if ((ctx == NULL) || hash_ctx_complete(ctx)) { | |
128 | i++; | |
129 | continue; | |
130 | } | |
131 | // Resubmit unfinished job | |
132 | i = (unsigned long)(ctx->user_data); | |
133 | buf_ptr[i] += UPDATE_SIZE; | |
134 | } | |
135 | ||
136 | // Start flushing finished jobs, end on last flushed | |
137 | ctx = sha1_ctx_mgr_flush(mgr); | |
138 | while (ctx) { | |
139 | if (hash_ctx_complete(ctx)) { | |
140 | debug_char('-'); | |
141 | ctx = sha1_ctx_mgr_flush(mgr); | |
142 | continue; | |
143 | } | |
144 | // Resubmit unfinished job | |
145 | i = (unsigned long)(ctx->user_data); | |
146 | buf_ptr[i] += UPDATE_SIZE; | |
147 | ||
148 | len_done = (int)((unsigned long)buf_ptr[i] | |
149 | - (unsigned long)bufs[i]); | |
150 | len_rem = TEST_LEN - len_done; | |
151 | ||
152 | if (len_rem <= UPDATE_SIZE) | |
153 | ctx = sha1_ctx_mgr_submit(mgr, | |
154 | &ctxpool[i], buf_ptr[i], len_rem, HASH_LAST); | |
155 | else | |
156 | ctx = sha1_ctx_mgr_submit(mgr, | |
157 | &ctxpool[i], | |
158 | buf_ptr[i], UPDATE_SIZE, HASH_UPDATE); | |
159 | ||
160 | if (ctx == NULL) | |
161 | ctx = sha1_ctx_mgr_flush(mgr); | |
162 | } | |
163 | ||
164 | // Check digests | |
165 | for (i = 0; i < TEST_BUFS; i++) { | |
166 | for (j = 0; j < SHA1_DIGEST_NWORDS; j++) { | |
167 | if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) { | |
168 | fail++; | |
169 | printf("Test%d fixed size, digest%d fail %8X <=> %8X", | |
170 | i, j, ctxpool[i].job.result_digest[j], | |
171 | digest_ref[i][j]); | |
172 | } | |
173 | } | |
174 | } | |
175 | putchar('.'); | |
176 | ||
177 | // Run tests with random size and number of jobs | |
178 | for (t = 0; t < RANDOMS; t++) { | |
179 | jobs = rand() % (TEST_BUFS); | |
180 | ||
181 | for (i = 0; i < jobs; i++) { | |
182 | joblen = rand() % (TEST_LEN); | |
183 | rand_buffer(bufs[i], joblen); | |
184 | lens[i] = joblen; | |
185 | buf_ptr[i] = bufs[i]; | |
186 | sha1_ref(bufs[i], digest_ref[i], lens[i]); | |
187 | } | |
188 | ||
189 | sha1_ctx_mgr_init(mgr); | |
190 | ||
191 | // Run sha1_sb jobs | |
192 | i = 0; | |
193 | while (i < jobs) { | |
1e59de90 | 194 | // Submit a new job |
7c673cae FG |
195 | len_rand = SHA1_BLOCK_SIZE + |
196 | SHA1_BLOCK_SIZE * (rand() % MAX_RAND_UPDATE_BLOCKS); | |
197 | ||
198 | if (lens[i] > len_rand) | |
199 | ctx = sha1_ctx_mgr_submit(mgr, | |
200 | &ctxpool[i], | |
201 | buf_ptr[i], len_rand, HASH_FIRST); | |
202 | else | |
203 | ctx = sha1_ctx_mgr_submit(mgr, | |
204 | &ctxpool[i], | |
205 | buf_ptr[i], lens[i], HASH_ENTIRE); | |
206 | ||
207 | // Returned ctx could be: | |
1e59de90 TL |
208 | // - null context (we are just getting started and lanes aren't full yet), or |
209 | // - finished already (an ENTIRE we submitted or a previous LAST is returned), or | |
7c673cae FG |
210 | // - an unfinished ctx, we will resubmit |
211 | ||
212 | if ((ctx == NULL) || hash_ctx_complete(ctx)) { | |
213 | i++; | |
214 | continue; | |
215 | } else { | |
1e59de90 TL |
216 | // unfinished ctx returned, choose another random update length and submit either |
217 | // UPDATE or LAST depending on the amount of buffer remaining | |
7c673cae FG |
218 | while ((ctx != NULL) && !(hash_ctx_complete(ctx))) { |
219 | j = (unsigned long)(ctx->user_data); // Get index of the returned ctx | |
220 | buf_ptr[j] = bufs[j] + ctx->total_length; | |
221 | len_rand = (rand() % SHA1_BLOCK_SIZE) | |
222 | * (rand() % MAX_RAND_UPDATE_BLOCKS); | |
223 | len_rem = lens[j] - ctx->total_length; | |
224 | ||
225 | if (len_rem <= len_rand) // submit the rest of the job as LAST | |
226 | ctx = sha1_ctx_mgr_submit(mgr, | |
227 | &ctxpool[j], | |
228 | buf_ptr[j], | |
229 | len_rem, HASH_LAST); | |
230 | else // submit the random update length as UPDATE | |
231 | ctx = sha1_ctx_mgr_submit(mgr, | |
232 | &ctxpool[j], | |
233 | buf_ptr[j], | |
234 | len_rand, | |
235 | HASH_UPDATE); | |
1e59de90 TL |
236 | } // Either continue submitting any contexts returned here as UPDATE/LAST, or |
237 | // go back to submitting new jobs using the index i. | |
7c673cae FG |
238 | |
239 | i++; | |
240 | } | |
241 | } | |
242 | ||
243 | // Start flushing finished jobs, end on last flushed | |
244 | ctx = sha1_ctx_mgr_flush(mgr); | |
245 | while (ctx) { | |
246 | if (hash_ctx_complete(ctx)) { | |
247 | debug_char('-'); | |
248 | ctx = sha1_ctx_mgr_flush(mgr); | |
249 | continue; | |
250 | } | |
251 | // Resubmit unfinished job | |
252 | i = (unsigned long)(ctx->user_data); | |
253 | buf_ptr[i] = bufs[i] + ctx->total_length; // update buffer pointer | |
254 | len_rem = lens[i] - ctx->total_length; | |
255 | len_rand = (rand() % SHA1_BLOCK_SIZE) | |
256 | * (rand() % MAX_RAND_UPDATE_BLOCKS); | |
257 | debug_char('+'); | |
258 | if (len_rem <= len_rand) | |
259 | ctx = sha1_ctx_mgr_submit(mgr, | |
260 | &ctxpool[i], | |
261 | buf_ptr[i], len_rem, HASH_LAST); | |
262 | else | |
263 | ctx = sha1_ctx_mgr_submit(mgr, | |
264 | &ctxpool[i], | |
265 | buf_ptr[i], len_rand, HASH_UPDATE); | |
266 | ||
267 | if (ctx == NULL) | |
268 | ctx = sha1_ctx_mgr_flush(mgr); | |
269 | } | |
270 | ||
271 | // Check result digest | |
272 | for (i = 0; i < jobs; i++) { | |
273 | for (j = 0; j < SHA1_DIGEST_NWORDS; j++) { | |
274 | if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) { | |
275 | fail++; | |
276 | printf("Test%d, digest%d fail %8X <=> %8X\n", | |
277 | i, j, ctxpool[i].job.result_digest[j], | |
278 | digest_ref[i][j]); | |
279 | } | |
280 | } | |
281 | } | |
282 | if (fail) { | |
283 | printf("Test failed function check %d\n", fail); | |
284 | return fail; | |
285 | } | |
286 | ||
287 | putchar('.'); | |
288 | fflush(0); | |
289 | } // random test t | |
290 | ||
291 | if (fail) | |
292 | printf("Test failed function check %d\n", fail); | |
293 | else | |
294 | printf(" multibinary_sha1_update rand: Pass\n"); | |
295 | ||
296 | return fail; | |
297 | } |