]>
Commit | Line | Data |
---|---|---|
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 <openssl/md5.h> | |
33 | #include "md5_mb.h" | |
1e59de90 | 34 | #include "endian_helper.h" |
7c673cae FG |
35 | |
36 | #define TEST_LEN (1024*1024) | |
37 | #define TEST_BUFS 200 | |
38 | #ifndef RANDOMS | |
39 | # define RANDOMS 10 | |
40 | #endif | |
41 | #ifndef TEST_SEED | |
42 | # define TEST_SEED 0x1234 | |
43 | #endif | |
44 | ||
45 | /* Reference digest global to reduce stack usage */ | |
46 | static uint8_t digest_ssl[TEST_BUFS][4 * MD5_DIGEST_NWORDS]; | |
47 | ||
48 | // Generates pseudo-random data | |
49 | void 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 | ||
56 | int main(void) | |
57 | { | |
58 | MD5_HASH_CTX_MGR *mgr = NULL; | |
59 | MD5_HASH_CTX ctxpool[TEST_BUFS]; | |
60 | unsigned char *bufs[TEST_BUFS]; | |
61 | uint32_t i, j, fail = 0; | |
62 | uint32_t lens[TEST_BUFS]; | |
63 | unsigned int jobs, t; | |
1e59de90 | 64 | int ret; |
7c673cae FG |
65 | |
66 | printf("multibinary_md5 test, %d sets of %dx%d max: ", RANDOMS, TEST_BUFS, TEST_LEN); | |
67 | ||
68 | srand(TEST_SEED); | |
69 | ||
1e59de90 TL |
70 | ret = posix_memalign((void *)&mgr, 16, sizeof(MD5_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 | md5_ctx_mgr_init(mgr); |
77 | ||
78 | for (i = 0; i < TEST_BUFS; i++) { | |
79 | // Allocate and fill buffer | |
80 | bufs[i] = (unsigned char *)malloc(TEST_LEN); | |
81 | if (bufs[i] == NULL) { | |
82 | printf("malloc failed test aborted\n"); | |
83 | return 1; | |
84 | } | |
85 | rand_buffer(bufs[i], TEST_LEN); | |
86 | ||
87 | // Init ctx contents | |
88 | hash_ctx_init(&ctxpool[i]); | |
89 | ctxpool[i].user_data = (void *)((uint64_t) i); | |
90 | ||
91 | // SSL test | |
92 | MD5(bufs[i], TEST_LEN, digest_ssl[i]); | |
93 | ||
94 | // sb_md5 test | |
95 | md5_ctx_mgr_submit(mgr, &ctxpool[i], bufs[i], TEST_LEN, HASH_ENTIRE); | |
96 | } | |
97 | ||
98 | while (md5_ctx_mgr_flush(mgr)) ; | |
99 | ||
100 | for (i = 0; i < TEST_BUFS; i++) { | |
101 | for (j = 0; j < MD5_DIGEST_NWORDS; j++) { | |
1e59de90 TL |
102 | if (ctxpool[i].job.result_digest[j] != |
103 | to_le32(((uint32_t *) digest_ssl[i])[j])) { | |
7c673cae FG |
104 | fail++; |
105 | printf("Test%d, digest%d fail %08X <=> %08X\n", | |
106 | i, j, ctxpool[i].job.result_digest[j], | |
1e59de90 | 107 | to_le32(((uint32_t *) digest_ssl[i])[j])); |
7c673cae FG |
108 | } |
109 | } | |
110 | } | |
111 | putchar('.'); | |
112 | ||
113 | // Run tests with random size and number of jobs | |
114 | for (t = 0; t < RANDOMS; t++) { | |
115 | jobs = rand() % (TEST_BUFS); | |
116 | ||
117 | md5_ctx_mgr_init(mgr); | |
118 | ||
119 | for (i = 0; i < jobs; i++) { | |
1e59de90 | 120 | // Random buffer with random len and contents |
7c673cae FG |
121 | lens[i] = rand() % (TEST_LEN); |
122 | rand_buffer(bufs[i], lens[i]); | |
123 | ||
124 | // Run SSL test | |
125 | MD5(bufs[i], lens[i], digest_ssl[i]); | |
126 | ||
127 | // Run sb_md5 test | |
128 | md5_ctx_mgr_submit(mgr, &ctxpool[i], bufs[i], lens[i], HASH_ENTIRE); | |
129 | } | |
130 | ||
131 | while (md5_ctx_mgr_flush(mgr)) ; | |
132 | ||
133 | for (i = 0; i < jobs; i++) { | |
134 | for (j = 0; j < MD5_DIGEST_NWORDS; j++) { | |
135 | if (ctxpool[i].job.result_digest[j] != | |
1e59de90 | 136 | to_le32(((uint32_t *) digest_ssl[i])[j])) { |
7c673cae FG |
137 | fail++; |
138 | printf("Test%d, digest%d fail %08X <=> %08X\n", | |
139 | i, j, ctxpool[i].job.result_digest[j], | |
1e59de90 | 140 | to_le32(((uint32_t *) digest_ssl[i])[j])); |
7c673cae FG |
141 | } |
142 | } | |
143 | } | |
144 | if (fail) { | |
145 | printf("Test failed function check %d\n", fail); | |
146 | return fail; | |
147 | } | |
148 | ||
149 | putchar('.'); | |
150 | fflush(0); | |
151 | } // random test t | |
152 | ||
153 | if (fail) | |
154 | printf("Test failed function check %d\n", fail); | |
155 | else | |
156 | printf(" multibinary_md5_ssl rand: Pass\n"); | |
157 | ||
158 | return fail; | |
159 | } |