]>
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 <string.h> | |
33 | #include "sha1_mb.h" | |
1e59de90 | 34 | #include "endian_helper.h" |
7c673cae FG |
35 | |
36 | typedef uint32_t DigestSHA1[SHA1_DIGEST_NWORDS]; | |
37 | ||
38 | #define MSGS 7 | |
39 | #define NUM_JOBS 1000 | |
40 | ||
41 | #define PSEUDO_RANDOM_NUM(seed) ((seed) * 5 + ((seed) * (seed)) / 64) % MSGS | |
42 | static uint8_t msg1[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; | |
43 | static DigestSHA1 expResultDigest1 = | |
44 | { 0x84983E44, 0x1C3BD26E, 0xBAAE4AA1, 0xF95129E5, 0xE54670F1 }; | |
45 | ||
46 | static uint8_t msg2[] = "0123456789:;<=>?@ABCDEFGHIJKLMNO"; | |
47 | static DigestSHA1 expResultDigest2 = | |
48 | { 0xB7C66452, 0x0FD122B3, 0x55D539F2, 0xA35E6FAA, 0xC2A5A11D }; | |
49 | ||
50 | static uint8_t msg3[] = | |
51 | "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" | |
52 | "0123456789:;<"; | |
53 | static DigestSHA1 expResultDigest3 = | |
54 | { 0x127729B6, 0xA8B2F8A0, 0xA4DDC819, 0x08E1D8B3, 0x67CEEA55 }; | |
55 | ||
56 | static uint8_t msg4[] = | |
57 | "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" | |
58 | "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" "0123456789:;<=>?@ABCDEFGHIJKLMNOPQR"; | |
59 | static DigestSHA1 expResultDigest4 = | |
60 | { 0xFDDE2D00, 0xABD5B7A3, 0x699DE6F2, 0x3FF1D1AC, 0x3B872AC2 }; | |
61 | ||
62 | static uint8_t msg5[] = | |
63 | "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" | |
64 | "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" | |
65 | "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" "0123456789:;<=>?"; | |
66 | static DigestSHA1 expResultDigest5 = | |
67 | { 0xE7FCA85C, 0xA4AB3740, 0x6A180B32, 0x0B8D362C, 0x622A96E6 }; | |
68 | ||
69 | static uint8_t msg6[] = | |
70 | "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" | |
71 | "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" | |
72 | "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" | |
73 | "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTU"; | |
74 | static DigestSHA1 expResultDigest6 = | |
75 | { 0x505B0686, 0xE1ACDF42, 0xB3588B5A, 0xB043D52C, 0x6D8C7444 }; | |
76 | ||
77 | static uint8_t msg7[] = ""; | |
78 | static DigestSHA1 expResultDigest7 = | |
79 | { 0xDA39A3EE, 0x5E6B4B0D, 0x3255BFEF, 0x95601890, 0xAFD80709 }; | |
80 | ||
81 | static uint8_t *msgs[MSGS] = { msg1, msg2, msg3, msg4, msg5, msg6, msg7 }; | |
82 | ||
83 | static uint32_t *expResultDigest[MSGS] = { | |
84 | expResultDigest1, expResultDigest2, expResultDigest3, | |
85 | expResultDigest4, expResultDigest5, expResultDigest6, | |
86 | expResultDigest7 | |
87 | }; | |
88 | ||
89 | int main(void) | |
90 | { | |
91 | SHA1_HASH_CTX_MGR *mgr = NULL; | |
92 | SHA1_HASH_CTX ctxpool[NUM_JOBS], *ctx = NULL; | |
93 | uint32_t i, j, k, t, checked = 0; | |
94 | uint32_t *good; | |
1e59de90 TL |
95 | int ret; |
96 | ||
97 | ret = posix_memalign((void *)&mgr, 16, sizeof(SHA1_HASH_CTX_MGR)); | |
98 | if ((ret != 0) || (mgr == NULL)) { | |
99 | printf("posix_memalign failed test aborted\n"); | |
100 | return 1; | |
101 | } | |
7c673cae | 102 | |
7c673cae FG |
103 | sha1_ctx_mgr_init(mgr); |
104 | ||
105 | // Init contexts before first use | |
106 | for (i = 0; i < MSGS; i++) { | |
107 | hash_ctx_init(&ctxpool[i]); | |
108 | ctxpool[i].user_data = (void *)((uint64_t) i); | |
109 | } | |
110 | ||
111 | for (i = 0; i < MSGS; i++) { | |
112 | ctx = sha1_ctx_mgr_submit(mgr, | |
113 | &ctxpool[i], msgs[i], | |
114 | strlen((char *)msgs[i]), HASH_ENTIRE); | |
115 | ||
116 | if (ctx) { | |
117 | t = (unsigned long)(ctx->user_data); | |
118 | good = expResultDigest[t]; | |
119 | checked++; | |
120 | for (j = 0; j < SHA1_DIGEST_NWORDS; j++) { | |
121 | if (good[j] != ctxpool[t].job.result_digest[j]) { | |
122 | printf("Test %d, digest %d is %08X, should be %08X\n", | |
123 | t, j, ctxpool[t].job.result_digest[j], good[j]); | |
124 | return -1; | |
125 | } | |
126 | } | |
127 | ||
128 | if (ctx->error) { | |
129 | printf("Something bad happened during the submit." | |
130 | " Error code: %d", ctx->error); | |
131 | return -1; | |
132 | } | |
133 | ||
134 | } | |
135 | } | |
136 | ||
137 | while (1) { | |
138 | ctx = sha1_ctx_mgr_flush(mgr); | |
139 | ||
140 | if (ctx) { | |
141 | t = (unsigned long)(ctx->user_data); | |
142 | good = expResultDigest[t]; | |
143 | checked++; | |
144 | for (j = 0; j < SHA1_DIGEST_NWORDS; j++) { | |
145 | if (good[j] != ctxpool[t].job.result_digest[j]) { | |
146 | printf("Test %d, digest %d is %08X, should be %08X\n", | |
147 | t, j, ctxpool[t].job.result_digest[j], good[j]); | |
148 | return -1; | |
149 | } | |
150 | } | |
151 | ||
152 | if (ctx->error) { | |
153 | printf("Something bad happened during the submit." | |
154 | " Error code: %d", ctx->error); | |
155 | return -1; | |
156 | } | |
157 | } else { | |
158 | break; | |
159 | } | |
160 | } | |
161 | ||
162 | // do larger test in pseudo-random order | |
163 | ||
164 | // Init contexts before first use | |
165 | for (i = 0; i < NUM_JOBS; i++) { | |
166 | hash_ctx_init(&ctxpool[i]); | |
167 | ctxpool[i].user_data = (void *)((uint64_t) i); | |
168 | } | |
169 | ||
170 | checked = 0; | |
171 | for (i = 0; i < NUM_JOBS; i++) { | |
172 | j = PSEUDO_RANDOM_NUM(i); | |
173 | ctx = sha1_ctx_mgr_submit(mgr, | |
174 | &ctxpool[i], | |
175 | msgs[j], strlen((char *)msgs[j]), HASH_ENTIRE); | |
176 | if (ctx) { | |
177 | t = (unsigned long)(ctx->user_data); | |
178 | k = PSEUDO_RANDOM_NUM(t); | |
179 | good = expResultDigest[k]; | |
180 | checked++; | |
181 | for (j = 0; j < SHA1_DIGEST_NWORDS; j++) { | |
182 | if (good[j] != ctxpool[t].job.result_digest[j]) { | |
183 | printf("Test %d, digest %d is %08X, should be %08X\n", | |
184 | t, j, ctxpool[t].job.result_digest[j], good[j]); | |
185 | return -1; | |
186 | } | |
187 | } | |
188 | ||
189 | if (ctx->error) { | |
190 | printf("Something bad happened during the" | |
191 | " submit. Error code: %d", ctx->error); | |
192 | return -1; | |
193 | } | |
194 | ||
195 | t = (unsigned long)(ctx->user_data); | |
196 | k = PSEUDO_RANDOM_NUM(t); | |
197 | } | |
198 | } | |
199 | while (1) { | |
200 | ctx = sha1_ctx_mgr_flush(mgr); | |
201 | ||
202 | if (ctx) { | |
203 | t = (unsigned long)(ctx->user_data); | |
204 | k = PSEUDO_RANDOM_NUM(t); | |
205 | good = expResultDigest[k]; | |
206 | checked++; | |
207 | for (j = 0; j < SHA1_DIGEST_NWORDS; j++) { | |
208 | if (good[j] != ctxpool[t].job.result_digest[j]) { | |
209 | printf("Test %d, digest %d is %08X, should be %08X\n", | |
210 | t, j, ctxpool[t].job.result_digest[j], good[j]); | |
211 | return -1; | |
212 | } | |
213 | } | |
214 | ||
215 | if (ctx->error) { | |
216 | printf("Something bad happened during the submit." | |
217 | " Error code: %d", ctx->error); | |
218 | return -1; | |
219 | } | |
220 | } else { | |
221 | break; | |
222 | } | |
223 | } | |
224 | ||
225 | if (checked != NUM_JOBS) { | |
226 | printf("only tested %d rather than %d\n", checked, NUM_JOBS); | |
227 | return -1; | |
228 | } | |
229 | ||
230 | printf(" multibinary_sha1 test: Pass\n"); | |
231 | ||
232 | return 0; | |
233 | } |