]> git.proxmox.com Git - ceph.git/blame - ceph/src/crypto/isa-l/isa-l_crypto/sha512_mb/sha512_mb_rand_update_test.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / crypto / isa-l / isa-l_crypto / sha512_mb / sha512_mb_rand_update_test.c
CommitLineData
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
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 "sha512_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*SHA512_BLOCK_SIZE
44#define MAX_RAND_UPDATE_BLOCKS (TEST_LEN/(16*SHA512_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 */
53static uint64_t digest_ref[TEST_BUFS][SHA512_DIGEST_NWORDS];
54
55extern void sha512_ref(uint8_t * input_data, uint64_t * digest, uint32_t len);
56
57// Generates pseudo-random data
58
59void 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
66int main(void)
67{
68 SHA512_HASH_CTX_MGR *mgr = NULL;
69 SHA512_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;
76
77 printf("multibinary_sha512_update test, %d sets of %dx%d max: ", RANDOMS, TEST_BUFS,
78 TEST_LEN);
79
80 srand(TEST_SEED);
81
82 posix_memalign((void *)&mgr, 16, sizeof(SHA512_HASH_CTX_MGR));
83 sha512_ctx_mgr_init(mgr);
84
85 for (i = 0; i < TEST_BUFS; i++) {
86 // Allocte and fill buffer
87 bufs[i] = (unsigned char *)malloc(TEST_LEN);
88 buf_ptr[i] = bufs[i];
89 if (bufs[i] == NULL) {
90 printf("malloc failed test aborted\n");
91 return 1;
92 }
93 rand_buffer(bufs[i], TEST_LEN);
94
95 // Init ctx contents
96 hash_ctx_init(&ctxpool[i]);
97 ctxpool[i].user_data = (void *)((uint64_t) i);
98
99 // Run reference test
100 sha512_ref(bufs[i], digest_ref[i], TEST_LEN);
101 }
102
103 // Run sb_sha512 tests
104 for (i = 0; i < TEST_BUFS;) {
105 len_done = (int)((unsigned long)buf_ptr[i] - (unsigned long)bufs[i]);
106 len_rem = TEST_LEN - len_done;
107
108 if (len_done == 0)
109 ctx = sha512_ctx_mgr_submit(mgr,
110 &ctxpool[i],
111 buf_ptr[i], UPDATE_SIZE, HASH_FIRST);
112 else if (len_rem <= UPDATE_SIZE)
113 ctx = sha512_ctx_mgr_submit(mgr,
114 &ctxpool[i],
115 buf_ptr[i], len_rem, HASH_LAST);
116 else
117 ctx = sha512_ctx_mgr_submit(mgr,
118 &ctxpool[i],
119 buf_ptr[i], UPDATE_SIZE, HASH_UPDATE);
120
121 // Add jobs while available or finished
122 if ((ctx == NULL) || hash_ctx_complete(ctx)) {
123 i++;
124 continue;
125 }
126 // Resubmit unfinished job
127 i = (unsigned long)(ctx->user_data);
128 buf_ptr[i] += UPDATE_SIZE;
129 }
130
131 // Start flushing finished jobs, end on last flushed
132 ctx = sha512_ctx_mgr_flush(mgr);
133 while (ctx) {
134 if (hash_ctx_complete(ctx)) {
135 debug_char('-');
136 ctx = sha512_ctx_mgr_flush(mgr);
137 continue;
138 }
139 // Resubmit unfinished job
140 i = (unsigned long)(ctx->user_data);
141 buf_ptr[i] += UPDATE_SIZE;
142
143 len_done = (int)((unsigned long)buf_ptr[i]
144 - (unsigned long)bufs[i]);
145 len_rem = TEST_LEN - len_done;
146
147 if (len_rem <= UPDATE_SIZE)
148 ctx = sha512_ctx_mgr_submit(mgr,
149 &ctxpool[i],
150 buf_ptr[i], len_rem, HASH_LAST);
151 else
152 ctx = sha512_ctx_mgr_submit(mgr,
153 &ctxpool[i],
154 buf_ptr[i], UPDATE_SIZE, HASH_UPDATE);
155
156 if (ctx == NULL)
157 ctx = sha512_ctx_mgr_flush(mgr);
158 }
159
160 // Check digests
161 for (i = 0; i < TEST_BUFS; i++) {
162 for (j = 0; j < SHA512_DIGEST_NWORDS; j++) {
163 if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) {
164 fail++;
165 printf("Test%d fixed size, digest%d fail %8lX <=> %8lX",
166 i, j, ctxpool[i].job.result_digest[j],
167 digest_ref[i][j]);
168 }
169 }
170 }
171 putchar('.');
172
173 // Run tests with random size and number of jobs
174 for (t = 0; t < RANDOMS; t++) {
175 jobs = rand() % (TEST_BUFS);
176
177 for (i = 0; i < jobs; i++) {
178 joblen = rand() % (TEST_LEN);
179 rand_buffer(bufs[i], joblen);
180 lens[i] = joblen;
181 buf_ptr[i] = bufs[i];
182 sha512_ref(bufs[i], digest_ref[i], lens[i]);
183 }
184
185 sha512_ctx_mgr_init(mgr);
186
187 // Run sha512_sb jobs
188 i = 0;
189 while (i < jobs) {
190 // Submit a new job
191 len_rand = SHA512_BLOCK_SIZE +
192 SHA512_BLOCK_SIZE * (rand() % MAX_RAND_UPDATE_BLOCKS);
193
194 if (lens[i] > len_rand)
195 ctx = sha512_ctx_mgr_submit(mgr,
196 &ctxpool[i],
197 buf_ptr[i], len_rand, HASH_FIRST);
198 else
199 ctx = sha512_ctx_mgr_submit(mgr,
200 &ctxpool[i],
201 buf_ptr[i], lens[i], HASH_ENTIRE);
202
203 // Returned ctx could be:
204 // - null context (we are just getting started and lanes aren't full yet), or
205 // - finished already (an ENTIRE we submitted or a previous LAST is returned), or
206 // - an unfinished ctx, we will resubmit
207
208 if ((ctx == NULL) || hash_ctx_complete(ctx)) {
209 i++;
210 continue;
211 } else {
212 // unfinished ctx returned, choose another random update length and submit either
213 // UPDATE or LAST depending on the amount of buffer remaining
214 while ((ctx != NULL) && !(hash_ctx_complete(ctx))) {
215 j = (unsigned long)(ctx->user_data); // Get index of the returned ctx
216 buf_ptr[j] = bufs[j] + ctx->total_length;
217 len_rand = (rand() % SHA512_BLOCK_SIZE)
218 * (rand() % MAX_RAND_UPDATE_BLOCKS);
219 len_rem = lens[j] - ctx->total_length;
220
221 if (len_rem <= len_rand) // submit the rest of the job as LAST
222 ctx = sha512_ctx_mgr_submit(mgr,
223 &ctxpool[j],
224 buf_ptr[j],
225 len_rem,
226 HASH_LAST);
227 else // submit the random update length as UPDATE
228 ctx = sha512_ctx_mgr_submit(mgr,
229 &ctxpool[j],
230 buf_ptr[j],
231 len_rand,
232 HASH_UPDATE);
233 } // Either continue submitting any contexts returned here as UPDATE/LAST, or
234 // go back to submitting new jobs using the index i.
235
236 i++;
237 }
238 }
239
240 // Start flushing finished jobs, end on last flushed
241 ctx = sha512_ctx_mgr_flush(mgr);
242 while (ctx) {
243 if (hash_ctx_complete(ctx)) {
244 debug_char('-');
245 ctx = sha512_ctx_mgr_flush(mgr);
246 continue;
247 }
248 // Resubmit unfinished job
249 i = (unsigned long)(ctx->user_data);
250 buf_ptr[i] = bufs[i] + ctx->total_length; // update buffer pointer
251 len_rem = lens[i] - ctx->total_length;
252 len_rand = (rand() % SHA512_BLOCK_SIZE)
253 * (rand() % MAX_RAND_UPDATE_BLOCKS);
254 debug_char('+');
255 if (len_rem <= len_rand)
256 ctx = sha512_ctx_mgr_submit(mgr,
257 &ctxpool[i],
258 buf_ptr[i], len_rem, HASH_LAST);
259 else
260 ctx = sha512_ctx_mgr_submit(mgr,
261 &ctxpool[i],
262 buf_ptr[i], len_rand, HASH_UPDATE);
263
264 if (ctx == NULL)
265 ctx = sha512_ctx_mgr_flush(mgr);
266 }
267
268 // Check result digest
269 for (i = 0; i < jobs; i++) {
270 for (j = 0; j < SHA512_DIGEST_NWORDS; j++) {
271 if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) {
272 fail++;
273 printf("Test%d, digest%d fail %8lX <=> %8lX\n",
274 i, j, ctxpool[i].job.result_digest[j],
275 digest_ref[i][j]);
276 }
277 }
278 }
279 if (fail) {
280 printf("Test failed function check %d\n", fail);
281 return fail;
282 }
283
284 putchar('.');
285 fflush(0);
286 } // random test t
287
288 if (fail)
289 printf("Test failed function check %d\n", fail);
290 else
291 printf(" multibinary_sha512_update rand: Pass\n");
292
293 return fail;
294}