xref: /isa-l_crypto/sha1_mb/sha1_mb_rand_test.c (revision 8cb7fe780eac8ee5f1e0aa3ca37466e89c673ccd)
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 "sha1_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_SHA1_DIGEST_NWORDS];
45 
46 // Compare against reference function
47 extern void
48 sha1_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_SHA1_HASH_CTX_MGR *mgr = NULL;
63         ISAL_SHA1_HASH_CTX ctxpool[TEST_BUFS], *ctx = NULL;
64         uint32_t i, j, fail = 0;
65         unsigned char *bufs[TEST_BUFS];
66         uint32_t lens[TEST_BUFS];
67         unsigned int jobs, t;
68         uint8_t *tmp_buf = NULL;
69         int ret;
70 
71         printf("multibinary_sha1 test, %d sets of %dx%d max: ", RANDOMS, TEST_BUFS, TEST_LEN);
72 
73         ret = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SHA1_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_sha1_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                 sha1_ref(bufs[i], digest_ref[i], TEST_LEN);
101 
102                 // Run sb_sha1 test
103                 ret = isal_sha1_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_sha1_ctx_mgr_flush(mgr, &ctx);
111                 if (ret)
112                         return 1;
113         } while (ctx != NULL);
114 
115         for (i = 0; i < TEST_BUFS; i++) {
116                 for (j = 0; j < ISAL_SHA1_DIGEST_NWORDS; j++) {
117                         if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) {
118                                 fail++;
119                                 printf("Test%d fixed size, digest%d "
120                                        "fail 0x%08X <=> 0x%08X \n",
121                                        i, j, ctxpool[i].job.result_digest[j], digest_ref[i][j]);
122                         }
123                 }
124         }
125 
126         if (fail) {
127                 printf("Test failed function check %d\n", fail);
128                 goto end;
129         }
130         // Run tests with random size and number of jobs
131         for (t = 0; t < RANDOMS; t++) {
132                 jobs = rand() % (TEST_BUFS);
133 
134                 ret = isal_sha1_ctx_mgr_init(mgr);
135                 if (ret)
136                         return 1;
137 
138                 for (i = 0; i < jobs; i++) {
139                         // Use buffer with random len and contents
140                         lens[i] = rand() % (TEST_LEN);
141                         rand_buffer(bufs[i], lens[i]);
142 
143                         // Run reference test
144                         sha1_ref(bufs[i], digest_ref[i], lens[i]);
145 
146                         // Run sha1_mb test
147                         ret = isal_sha1_ctx_mgr_submit(mgr, &ctxpool[i], &ctx, bufs[i], lens[i],
148                                                        ISAL_HASH_ENTIRE);
149                         if (ret)
150                                 return 1;
151                 }
152 
153                 do {
154                         ret = isal_sha1_ctx_mgr_flush(mgr, &ctx);
155                         if (ret)
156                                 return 1;
157                 } while (ctx != NULL);
158 
159                 for (i = 0; i < jobs; i++) {
160                         for (j = 0; j < ISAL_SHA1_DIGEST_NWORDS; j++) {
161                                 if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) {
162                                         fail++;
163                                         printf("Test%d, digest%d fail "
164                                                "0x%08X <=> 0x%08X\n",
165                                                i, j, ctxpool[i].job.result_digest[j],
166                                                digest_ref[i][j]);
167                                 }
168                         }
169                 }
170                 if (fail) {
171                         printf("Test failed function check %d\n", fail);
172                         goto end;
173                 }
174 
175                 putchar('.');
176                 fflush(0);
177         } // random test t
178 
179         // Test at the end of buffer
180         jobs = rand() % TEST_BUFS;
181         tmp_buf = (uint8_t *) malloc(sizeof(uint8_t) * jobs);
182         if (!tmp_buf) {
183                 printf("malloc failed, end test aborted.\n");
184                 goto end;
185         }
186 
187         rand_buffer(tmp_buf, jobs);
188 
189         ret = isal_sha1_ctx_mgr_init(mgr);
190         if (ret)
191                 return 1;
192 
193         for (i = 0; i < TEST_BUFS; i++)
194                 free(bufs[i]);
195 
196         // Extend to the end of allocated buffer to construct jobs
197         for (i = 0; i < jobs; i++) {
198                 bufs[i] = (uint8_t *) &tmp_buf[i];
199                 lens[i] = jobs - i;
200 
201                 // Reference test
202                 sha1_ref(bufs[i], digest_ref[i], lens[i]);
203 
204                 // sb_sha1 test
205                 ret = isal_sha1_ctx_mgr_submit(mgr, &ctxpool[i], &ctx, bufs[i], lens[i],
206                                                ISAL_HASH_ENTIRE);
207                 if (ret)
208                         return 1;
209         }
210         // Clear bufs
211         memset(bufs, 0, sizeof(bufs));
212 
213         do {
214                 ret = isal_sha1_ctx_mgr_flush(mgr, &ctx);
215                 if (ret)
216                         return 1;
217         } while (ctx != NULL);
218 
219         for (i = 0; i < jobs; i++) {
220                 for (j = 0; j < ISAL_SHA1_DIGEST_NWORDS; j++) {
221                         if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) {
222                                 fail++;
223                                 printf("End test failed at offset %d - result: 0x%08X"
224                                        ", ref: 0x%08X\n",
225                                        i, ctxpool[i].job.result_digest[j], digest_ref[i][j]);
226                         }
227                 }
228         }
229         putchar('.');
230 
231 end:
232         for (i = 0; i < TEST_BUFS; i++)
233                 free(bufs[i]);
234         free(tmp_buf);
235         aligned_free(mgr);
236 
237         if (fail)
238                 printf("Test failed function check %d\n", fail);
239         else
240                 printf(" multibinary_sha1 rand: Pass\n");
241 
242         return fail;
243 }
244