xref: /isa-l_crypto/md5_mb/md5_mb_rand_test.c (revision 5b4d5140ac42056c7e400addeadb9561ee969921)
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 "md5_mb.h"
33 
34 #ifndef FIPS_MODE
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_MD5_DIGEST_NWORDS];
45 
46 // Compare against reference function
47 extern void
48 md5_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 #endif
60 
61 int
main(void)62 main(void)
63 {
64 #ifndef FIPS_MODE
65         ISAL_MD5_HASH_CTX_MGR *mgr = NULL;
66         ISAL_MD5_HASH_CTX ctxpool[TEST_BUFS], *ctx = NULL;
67         uint32_t i, j, fail = 0;
68         unsigned char *bufs[TEST_BUFS] = { 0 };
69         uint32_t lens[TEST_BUFS];
70         unsigned int jobs, t;
71         uint8_t *tmp_buf = NULL;
72         int ret;
73 
74         printf("multibinary_md5 test, %d sets of %dx%d max: ", RANDOMS, TEST_BUFS, TEST_LEN);
75 
76         ret = posix_memalign((void *) &mgr, 16, sizeof(ISAL_MD5_HASH_CTX_MGR));
77         if ((ret != 0) || (mgr == NULL)) {
78                 printf("posix_memalign failed test aborted\n");
79                 return 1;
80         }
81 
82         ret = isal_md5_ctx_mgr_init(mgr);
83         if (ret) {
84                 fail = 1;
85                 goto end;
86         }
87 
88         srand(TEST_SEED);
89 
90         for (i = 0; i < TEST_BUFS; i++) {
91                 // Allocate  and fill buffer
92                 bufs[i] = (unsigned char *) malloc(TEST_LEN);
93                 if (bufs[i] == NULL) {
94                         printf("malloc failed test aborted\n");
95                         fail++;
96                         goto end;
97                 }
98                 rand_buffer(bufs[i], TEST_LEN);
99 
100                 // Init ctx contexts
101                 isal_hash_ctx_init(&ctxpool[i]);
102                 ctxpool[i].user_data = (void *) ((uint64_t) i);
103 
104                 // Run reference test
105                 md5_ref(bufs[i], digest_ref[i], TEST_LEN);
106 
107                 // Run sb_md5 test
108                 ret = isal_md5_ctx_mgr_submit(mgr, &ctxpool[i], &ctx, bufs[i], TEST_LEN,
109                                               ISAL_HASH_ENTIRE);
110                 if (ret) {
111                         fail = 1;
112                         goto end;
113                 }
114         }
115 
116         do {
117                 ret = isal_md5_ctx_mgr_flush(mgr, &ctx);
118                 if (ret) {
119                         fail = 1;
120                         goto end;
121                 }
122         } while (ctx != NULL);
123 
124         for (i = 0; i < TEST_BUFS; i++) {
125                 for (j = 0; j < ISAL_MD5_DIGEST_NWORDS; j++) {
126                         if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) {
127                                 fail++;
128                                 printf("Test%d fixed size, digest%d "
129                                        "fail 0x%08X <=> 0x%08X \n",
130                                        i, j, ctxpool[i].job.result_digest[j], digest_ref[i][j]);
131                         }
132                 }
133         }
134 
135         if (fail) {
136                 printf("Test failed function check %d\n", fail);
137                 goto end;
138         }
139         // Run tests with random size and number of jobs
140         for (t = 0; t < RANDOMS; t++) {
141                 jobs = rand() % (TEST_BUFS);
142 
143                 ret = isal_md5_ctx_mgr_init(mgr);
144                 if (ret) {
145                         fail = 1;
146                         goto end;
147                 }
148 
149                 for (i = 0; i < jobs; i++) {
150                         // Use buffer with random len and contents
151                         lens[i] = rand() % (TEST_LEN);
152                         rand_buffer(bufs[i], lens[i]);
153 
154                         // Run reference test
155                         md5_ref(bufs[i], digest_ref[i], lens[i]);
156 
157                         // Run md5_mb test
158                         ret = isal_md5_ctx_mgr_submit(mgr, &ctxpool[i], &ctx, bufs[i], lens[i],
159                                                       ISAL_HASH_ENTIRE);
160                         if (ret)
161                                 return 1;
162                 }
163 
164                 do {
165                         ret = isal_md5_ctx_mgr_flush(mgr, &ctx);
166                         if (ret) {
167                                 fail = 1;
168                                 goto end;
169                         }
170                 } while (ctx != NULL);
171 
172                 for (i = 0; i < jobs; i++) {
173                         for (j = 0; j < ISAL_MD5_DIGEST_NWORDS; j++) {
174                                 if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) {
175                                         fail++;
176                                         printf("Test%d, digest%d fail "
177                                                "0x%08X <=> 0x%08X\n",
178                                                i, j, ctxpool[i].job.result_digest[j],
179                                                digest_ref[i][j]);
180                                 }
181                         }
182                 }
183                 if (fail) {
184                         printf("Test failed function check %d\n", fail);
185                         goto end;
186                 }
187 
188                 putchar('.');
189                 fflush(0);
190         } // random test t
191 
192         // Test at the end of buffer
193         jobs = rand() % TEST_BUFS;
194         tmp_buf = (uint8_t *) malloc(sizeof(uint8_t) * jobs);
195         if (!tmp_buf) {
196                 printf("malloc failed, end test aborted.\n");
197                 goto end;
198         }
199 
200         rand_buffer(tmp_buf, jobs);
201 
202         ret = isal_md5_ctx_mgr_init(mgr);
203         if (ret) {
204                 fail = 1;
205                 goto end;
206         }
207 
208         for (i = 0; i < TEST_BUFS; i++)
209                 free(bufs[i]);
210 
211         // Extend to the end of allocated buffer to construct jobs
212         for (i = 0; i < jobs; i++) {
213                 bufs[i] = (uint8_t *) &tmp_buf[i];
214                 lens[i] = jobs - i;
215 
216                 // Reference test
217                 md5_ref(bufs[i], digest_ref[i], lens[i]);
218 
219                 // sb_md5 test
220                 ret = isal_md5_ctx_mgr_submit(mgr, &ctxpool[i], &ctx, bufs[i], lens[i],
221                                               ISAL_HASH_ENTIRE);
222                 if (ret) {
223                         fail = 1;
224                         goto end;
225                 }
226         }
227         // Clear bufs
228         memset(bufs, 0, sizeof(bufs));
229 
230         do {
231                 ret = isal_md5_ctx_mgr_flush(mgr, &ctx);
232                 if (ret) {
233                         fail = 1;
234                         goto end;
235                 }
236         } while (ctx != NULL);
237 
238         for (i = 0; i < jobs; i++) {
239                 for (j = 0; j < ISAL_MD5_DIGEST_NWORDS; j++) {
240                         if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) {
241                                 fail++;
242                                 printf("End test failed at offset %d - result: 0x%08X"
243                                        ", ref: 0x%08X\n",
244                                        i, ctxpool[i].job.result_digest[j], digest_ref[i][j]);
245                         }
246                 }
247         }
248         putchar('.');
249 
250 end:
251         for (i = 0; i < TEST_BUFS; i++)
252                 free(bufs[i]);
253         free(tmp_buf);
254         aligned_free(mgr);
255 
256         if (fail)
257                 printf("Test failed function check %d\n", fail);
258         else
259                 printf(" multibinary_md5 rand: Pass\n");
260 
261         return fail;
262 #else
263         printf("Not Executed\n");
264 
265         return 0;
266 #endif /* FIPS_MODE */
267 }
268