]> git.proxmox.com Git - ceph.git/blame - ceph/src/crypto/isa-l/isa-l_crypto/sha512_mb/sha512_mb_rand_test.c
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / crypto / isa-l / isa-l_crypto / sha512_mb / sha512_mb_rand_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
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 "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
43static uint64_t digest_ref[TEST_BUFS][SHA512_DIGEST_NWORDS];
44
45// Compare against reference function
46extern void sha512_ref(uint8_t * input_data, uint64_t * digest, uint32_t len);
47
48// Generates pseudo-random data
49void 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
56int main(void)
57{
58 SHA512_HASH_CTX_MGR *mgr = NULL;
59 SHA512_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;
1e59de90 65 int ret;
7c673cae
FG
66
67 printf("multibinary_sha512 test, %d sets of %dx%d max: ", RANDOMS, TEST_BUFS,
68 TEST_LEN);
69
1e59de90
TL
70 ret = posix_memalign((void *)&mgr, 16, sizeof(SHA512_HASH_CTX_MGR));
71 if ((ret != 0) || (mgr == NULL)) {
72 printf("posix_memalign failed test aborted\n");
73 return 1;
74 }
75
7c673cae
FG
76 sha512_ctx_mgr_init(mgr);
77
78 srand(TEST_SEED);
79
80 for (i = 0; i < TEST_BUFS; i++) {
81 // Allocate and fill buffer
82 bufs[i] = (unsigned char *)malloc(TEST_LEN);
83 if (bufs[i] == NULL) {
84 printf("malloc failed test aborted\n");
85 return 1;
86 }
87 rand_buffer(bufs[i], TEST_LEN);
88
89 // Init ctx contexts
90 hash_ctx_init(&ctxpool[i]);
91 ctxpool[i].user_data = (void *)((uint64_t) i);
92
93 // Run reference test
94 sha512_ref(bufs[i], digest_ref[i], TEST_LEN);
95
96 // Run sb_sha512 test
97 sha512_ctx_mgr_submit(mgr, &ctxpool[i], bufs[i], TEST_LEN, HASH_ENTIRE);
98 }
99
100 while (sha512_ctx_mgr_flush(mgr)) ;
101
102 for (i = 0; i < TEST_BUFS; i++) {
103 for (j = 0; j < SHA512_DIGEST_NWORDS; j++) {
104 if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) {
105 fail++;
106 printf("Test%d fixed size, digest%d "
107 "fail 0x%016lX <=> 0x%016lX \n",
108 i, j, ctxpool[i].job.result_digest[j],
109 digest_ref[i][j]);
110 }
111 }
112 }
113
114 if (fail) {
115 printf("Test failed function check %d\n", fail);
116 return fail;
117 }
118 // Run tests with random size and number of jobs
119 for (t = 0; t < RANDOMS; t++) {
120 jobs = rand() % (TEST_BUFS);
121
122 sha512_ctx_mgr_init(mgr);
123
124 for (i = 0; i < jobs; i++) {
125 // Use buffer with random len and contents
126 lens[i] = rand() % (TEST_LEN);
127 rand_buffer(bufs[i], lens[i]);
128
129 // Run reference test
130 sha512_ref(bufs[i], digest_ref[i], lens[i]);
131
132 // Run sha512_mb test
133 sha512_ctx_mgr_submit(mgr, &ctxpool[i], bufs[i], lens[i], HASH_ENTIRE);
134 }
135
136 while (sha512_ctx_mgr_flush(mgr)) ;
137
138 for (i = 0; i < jobs; i++) {
139 for (j = 0; j < SHA512_DIGEST_NWORDS; j++) {
140 if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) {
141 fail++;
142 printf("Test%d, digest%d fail "
143 "0x%016lX <=> 0x%016lX\n",
144 i, j, ctxpool[i].job.result_digest[j],
145 digest_ref[i][j]);
146 }
147 }
148 }
149 if (fail) {
150 printf("Test failed function check %d\n", fail);
151 return fail;
152 }
153
154 putchar('.');
155 fflush(0);
156 } // random test t
157
158 // Test at the end of buffer
159 jobs = rand() % TEST_BUFS;
160 tmp_buf = (uint8_t *) malloc(sizeof(uint8_t) * jobs);
161 if (!tmp_buf) {
162 printf("malloc failed, end test aborted.\n");
163 return 1;
164 }
165
166 rand_buffer(tmp_buf, jobs);
167
168 sha512_ctx_mgr_init(mgr);
169
170 // Extend to the end of allocated buffer to construct jobs
171 for (i = 0; i < jobs; i++) {
172 bufs[i] = (uint8_t *) & tmp_buf[i];
173 lens[i] = jobs - i;
174
175 // Reference test
176 sha512_ref(bufs[i], digest_ref[i], lens[i]);
177
178 // sb_sha512 test
179 sha512_ctx_mgr_submit(mgr, &ctxpool[i], bufs[i], lens[i], HASH_ENTIRE);
180 }
181
182 while (sha512_ctx_mgr_flush(mgr)) ;
183
184 for (i = 0; i < jobs; i++) {
185 for (j = 0; j < SHA512_DIGEST_NWORDS; j++) {
186 if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) {
187 fail++;
188 printf("End test failed at offset %d - result: 0x%016lX"
189 ", ref: 0x%016lX\n", i, ctxpool[i].job.result_digest[j],
190 digest_ref[i][j]);
191 }
192 }
193 }
194
195 putchar('.');
196
197 if (fail)
198 printf("Test failed function check %d\n", fail);
199 else
200 printf(" multibinary_sha512 rand: Pass\n");
201
202 return fail;
203}