]>
git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/sha512_mb/sha512_mb_rand_update_test.c
1 /**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
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
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.
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 **********************************************************************/
32 #include "sha512_mb.h"
34 #define TEST_LEN (1024*1024)
40 # define TEST_SEED 0x1234
43 #define UPDATE_SIZE 13*SHA512_BLOCK_SIZE
44 #define MAX_RAND_UPDATE_BLOCKS (TEST_LEN/(16*SHA512_BLOCK_SIZE))
47 # define debug_char(x) putchar(x)
49 # define debug_char(x) do {} while (0)
52 /* Reference digest global to reduce stack usage */
53 static uint64_t digest_ref
[TEST_BUFS
][SHA512_DIGEST_NWORDS
];
55 extern void sha512_ref(uint8_t * input_data
, uint64_t * digest
, uint32_t len
);
57 // Generates pseudo-random data
59 void rand_buffer(unsigned char *buf
, const long buffer_size
)
62 for (i
= 0; i
< buffer_size
; i
++)
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
;
78 printf("multibinary_sha512_update test, %d sets of %dx%d max: ", RANDOMS
, TEST_BUFS
,
83 ret
= posix_memalign((void *)&mgr
, 16, sizeof(SHA512_HASH_CTX_MGR
));
84 if ((ret
!= 0) || (mgr
== NULL
)) {
85 printf("posix_memalign failed test aborted\n");
89 sha512_ctx_mgr_init(mgr
);
91 for (i
= 0; i
< TEST_BUFS
; i
++) {
92 // Allocte and fill buffer
93 bufs
[i
] = (unsigned char *)malloc(TEST_LEN
);
95 if (bufs
[i
] == NULL
) {
96 printf("malloc failed test aborted\n");
99 rand_buffer(bufs
[i
], TEST_LEN
);
102 hash_ctx_init(&ctxpool
[i
]);
103 ctxpool
[i
].user_data
= (void *)((uint64_t) i
);
105 // Run reference test
106 sha512_ref(bufs
[i
], digest_ref
[i
], TEST_LEN
);
109 // Run sb_sha512 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
;
115 ctx
= sha512_ctx_mgr_submit(mgr
,
117 buf_ptr
[i
], UPDATE_SIZE
, HASH_FIRST
);
118 else if (len_rem
<= UPDATE_SIZE
)
119 ctx
= sha512_ctx_mgr_submit(mgr
,
121 buf_ptr
[i
], len_rem
, HASH_LAST
);
123 ctx
= sha512_ctx_mgr_submit(mgr
,
125 buf_ptr
[i
], UPDATE_SIZE
, HASH_UPDATE
);
127 // Add jobs while available or finished
128 if ((ctx
== NULL
) || hash_ctx_complete(ctx
)) {
132 // Resubmit unfinished job
133 i
= (unsigned long)(ctx
->user_data
);
134 buf_ptr
[i
] += UPDATE_SIZE
;
137 // Start flushing finished jobs, end on last flushed
138 ctx
= sha512_ctx_mgr_flush(mgr
);
140 if (hash_ctx_complete(ctx
)) {
142 ctx
= sha512_ctx_mgr_flush(mgr
);
145 // Resubmit unfinished job
146 i
= (unsigned long)(ctx
->user_data
);
147 buf_ptr
[i
] += UPDATE_SIZE
;
149 len_done
= (int)((unsigned long)buf_ptr
[i
]
150 - (unsigned long)bufs
[i
]);
151 len_rem
= TEST_LEN
- len_done
;
153 if (len_rem
<= UPDATE_SIZE
)
154 ctx
= sha512_ctx_mgr_submit(mgr
,
156 buf_ptr
[i
], len_rem
, HASH_LAST
);
158 ctx
= sha512_ctx_mgr_submit(mgr
,
160 buf_ptr
[i
], UPDATE_SIZE
, HASH_UPDATE
);
163 ctx
= sha512_ctx_mgr_flush(mgr
);
167 for (i
= 0; i
< TEST_BUFS
; i
++) {
168 for (j
= 0; j
< SHA512_DIGEST_NWORDS
; j
++) {
169 if (ctxpool
[i
].job
.result_digest
[j
] != digest_ref
[i
][j
]) {
171 printf("Test%d fixed size, digest%d fail %8lX <=> %8lX",
172 i
, j
, ctxpool
[i
].job
.result_digest
[j
],
179 // Run tests with random size and number of jobs
180 for (t
= 0; t
< RANDOMS
; t
++) {
181 jobs
= rand() % (TEST_BUFS
);
183 for (i
= 0; i
< jobs
; i
++) {
184 joblen
= rand() % (TEST_LEN
);
185 rand_buffer(bufs
[i
], joblen
);
187 buf_ptr
[i
] = bufs
[i
];
188 sha512_ref(bufs
[i
], digest_ref
[i
], lens
[i
]);
191 sha512_ctx_mgr_init(mgr
);
193 // Run sha512_sb jobs
197 len_rand
= SHA512_BLOCK_SIZE
+
198 SHA512_BLOCK_SIZE
* (rand() % MAX_RAND_UPDATE_BLOCKS
);
200 if (lens
[i
] > len_rand
)
201 ctx
= sha512_ctx_mgr_submit(mgr
,
203 buf_ptr
[i
], len_rand
, HASH_FIRST
);
205 ctx
= sha512_ctx_mgr_submit(mgr
,
207 buf_ptr
[i
], lens
[i
], HASH_ENTIRE
);
209 // Returned ctx could be:
210 // - null context (we are just getting started and lanes aren't full yet), or
211 // - finished already (an ENTIRE we submitted or a previous LAST is returned), or
212 // - an unfinished ctx, we will resubmit
214 if ((ctx
== NULL
) || hash_ctx_complete(ctx
)) {
218 // unfinished ctx returned, choose another random update length and submit either
219 // UPDATE or LAST depending on the amount of buffer remaining
220 while ((ctx
!= NULL
) && !(hash_ctx_complete(ctx
))) {
221 j
= (unsigned long)(ctx
->user_data
); // Get index of the returned ctx
222 buf_ptr
[j
] = bufs
[j
] + ctx
->total_length
;
223 len_rand
= (rand() % SHA512_BLOCK_SIZE
)
224 * (rand() % MAX_RAND_UPDATE_BLOCKS
);
225 len_rem
= lens
[j
] - ctx
->total_length
;
227 if (len_rem
<= len_rand
) // submit the rest of the job as LAST
228 ctx
= sha512_ctx_mgr_submit(mgr
,
233 else // submit the random update length as UPDATE
234 ctx
= sha512_ctx_mgr_submit(mgr
,
239 } // Either continue submitting any contexts returned here as UPDATE/LAST, or
240 // go back to submitting new jobs using the index i.
246 // Start flushing finished jobs, end on last flushed
247 ctx
= sha512_ctx_mgr_flush(mgr
);
249 if (hash_ctx_complete(ctx
)) {
251 ctx
= sha512_ctx_mgr_flush(mgr
);
254 // Resubmit unfinished job
255 i
= (unsigned long)(ctx
->user_data
);
256 buf_ptr
[i
] = bufs
[i
] + ctx
->total_length
; // update buffer pointer
257 len_rem
= lens
[i
] - ctx
->total_length
;
258 len_rand
= (rand() % SHA512_BLOCK_SIZE
)
259 * (rand() % MAX_RAND_UPDATE_BLOCKS
);
261 if (len_rem
<= len_rand
)
262 ctx
= sha512_ctx_mgr_submit(mgr
,
264 buf_ptr
[i
], len_rem
, HASH_LAST
);
266 ctx
= sha512_ctx_mgr_submit(mgr
,
268 buf_ptr
[i
], len_rand
, HASH_UPDATE
);
271 ctx
= sha512_ctx_mgr_flush(mgr
);
274 // Check result digest
275 for (i
= 0; i
< jobs
; i
++) {
276 for (j
= 0; j
< SHA512_DIGEST_NWORDS
; j
++) {
277 if (ctxpool
[i
].job
.result_digest
[j
] != digest_ref
[i
][j
]) {
279 printf("Test%d, digest%d fail %8lX <=> %8lX\n",
280 i
, j
, ctxpool
[i
].job
.result_digest
[j
],
286 printf("Test failed function check %d\n", fail
);
295 printf("Test failed function check %d\n", fail
);
297 printf(" multibinary_sha512_update rand: Pass\n");