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][ISAL_SHA256_DIGEST_NWORDS];
45
46 // Compare against reference function
47 extern void
48 sha256_ref(uint8_t *input_data, uint32_t *digest, uint32_t len);
49
50 // Generates pseudo-random data
51 void
rand_buffer(unsigned char * buf,const long buffer_size)52 rand_buffer(unsigned char *buf, const long buffer_size)
53 {
54 long i;
55 for (i = 0; i < buffer_size; i++)
56 buf[i] = rand();
57 }
58
59 int
main(void)60 main(void)
61 {
62 ISAL_SHA256_HASH_CTX_MGR *mgr = NULL;
63 ISAL_SHA256_HASH_CTX ctxpool[TEST_BUFS], *ctx = NULL;
64 uint32_t i, j, fail = 0;
65 unsigned char *bufs[TEST_BUFS] = { 0 };
66 uint32_t lens[TEST_BUFS];
67 unsigned int jobs, t;
68 uint8_t *tmp_buf = NULL;
69 int ret;
70
71 printf("multibinary_sha256 test, %d sets of %dx%d max: ", RANDOMS, TEST_BUFS, TEST_LEN);
72
73 ret = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SHA256_HASH_CTX_MGR));
74 if ((ret != 0) || (mgr == NULL)) {
75 printf("posix_memalign failed test aborted\n");
76 return 1;
77 }
78
79 ret = isal_sha256_ctx_mgr_init(mgr);
80 if (ret)
81 return 1;
82
83 srand(TEST_SEED);
84
85 for (i = 0; i < TEST_BUFS; i++) {
86 // Allocate and fill buffer
87 bufs[i] = (unsigned char *) malloc(TEST_LEN);
88 if (bufs[i] == NULL) {
89 printf("malloc failed test aborted\n");
90 fail++;
91 goto end;
92 }
93 rand_buffer(bufs[i], TEST_LEN);
94
95 // Init ctx contexts
96 isal_hash_ctx_init(&ctxpool[i]);
97 ctxpool[i].user_data = (void *) ((uint64_t) i);
98
99 // Run reference test
100 sha256_ref(bufs[i], digest_ref[i], TEST_LEN);
101
102 // Run sb_sha256 test
103 ret = isal_sha256_ctx_mgr_submit(mgr, &ctxpool[i], &ctx, bufs[i], TEST_LEN,
104 ISAL_HASH_ENTIRE);
105 if (ret)
106 return 1;
107 }
108
109 do {
110 ret = isal_sha256_ctx_mgr_flush(mgr, &ctx);
111 if (ret)
112 return 1;
113 } while (ctx != NULL);
114 ;
115
116 for (i = 0; i < TEST_BUFS; i++) {
117 for (j = 0; j < ISAL_SHA256_DIGEST_NWORDS; j++) {
118 if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) {
119 fail++;
120 printf("Test%d fixed size, digest%d "
121 "fail 0x%08X <=> 0x%08X \n",
122 i, j, ctxpool[i].job.result_digest[j], digest_ref[i][j]);
123 }
124 }
125 }
126
127 if (fail) {
128 printf("Test failed function check %d\n", fail);
129 goto end;
130 }
131 // Run tests with random size and number of jobs
132 for (t = 0; t < RANDOMS; t++) {
133 jobs = rand() % (TEST_BUFS);
134
135 ret = isal_sha256_ctx_mgr_init(mgr);
136 if (ret)
137 return 1;
138
139 for (i = 0; i < jobs; i++) {
140 // Use buffer with random len and contents
141 lens[i] = rand() % (TEST_LEN);
142 rand_buffer(bufs[i], lens[i]);
143
144 // Run reference test
145 sha256_ref(bufs[i], digest_ref[i], lens[i]);
146
147 // Run sha256_mb test
148 ret = isal_sha256_ctx_mgr_submit(mgr, &ctxpool[i], &ctx, bufs[i], lens[i],
149 ISAL_HASH_ENTIRE);
150 if (ret)
151 return 1;
152 }
153
154 do {
155 ret = isal_sha256_ctx_mgr_flush(mgr, &ctx);
156 if (ret)
157 return 1;
158 } while (ctx != NULL);
159
160 for (i = 0; i < jobs; i++) {
161 for (j = 0; j < ISAL_SHA256_DIGEST_NWORDS; j++) {
162 if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) {
163 fail++;
164 printf("Test%d, digest%d fail "
165 "0x%08X <=> 0x%08X\n",
166 i, j, ctxpool[i].job.result_digest[j],
167 digest_ref[i][j]);
168 }
169 }
170 }
171 if (fail) {
172 printf("Test failed function check %d\n", fail);
173 goto end;
174 }
175
176 putchar('.');
177 fflush(0);
178 } // random test t
179
180 // Test at the end of buffer
181 jobs = rand() % TEST_BUFS;
182 tmp_buf = (uint8_t *) malloc(sizeof(uint8_t) * jobs);
183 if (!tmp_buf) {
184 printf("malloc failed, end test aborted.\n");
185 goto end;
186 }
187
188 rand_buffer(tmp_buf, jobs);
189
190 ret = isal_sha256_ctx_mgr_init(mgr);
191 if (ret)
192 return 1;
193
194 for (i = 0; i < TEST_BUFS; i++)
195 free(bufs[i]);
196
197 // Extend to the end of allocated buffer to construct jobs
198 for (i = 0; i < jobs; i++) {
199 bufs[i] = (uint8_t *) &tmp_buf[i];
200 lens[i] = jobs - i;
201
202 // Reference test
203 sha256_ref(bufs[i], digest_ref[i], lens[i]);
204
205 // sb_sha256 test
206 ret = isal_sha256_ctx_mgr_submit(mgr, &ctxpool[i], &ctx, bufs[i], lens[i],
207 ISAL_HASH_ENTIRE);
208 if (ret)
209 return 1;
210 }
211 // Clear bufs
212 memset(bufs, 0, sizeof(bufs));
213
214 do {
215 ret = isal_sha256_ctx_mgr_flush(mgr, &ctx);
216 if (ret)
217 return 1;
218 } while (ctx != NULL);
219
220 for (i = 0; i < jobs; i++) {
221 for (j = 0; j < ISAL_SHA256_DIGEST_NWORDS; j++) {
222 if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) {
223 fail++;
224 printf("End test failed at offset %d - result: 0x%08X"
225 ", ref: 0x%08X\n",
226 i, ctxpool[i].job.result_digest[j], digest_ref[i][j]);
227 }
228 }
229 }
230 putchar('.');
231
232 end:
233 for (i = 0; i < TEST_BUFS; i++)
234 free(bufs[i]);
235 free(tmp_buf);
236 aligned_free(mgr);
237
238 if (fail)
239 printf("Test failed function check %d\n", fail);
240 else
241 printf(" multibinary_sha256 rand: Pass\n");
242
243 return fail;
244 }
245