]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | /********************************************************************** |
2 | Copyright(c) 2011-2017 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 "mh_sha256.h" | |
33 | ||
34 | #define TEST_LEN 16*1024 | |
35 | #define TEST_SIZE 8*1024 | |
36 | #define TEST_MEM TEST_LEN | |
37 | #ifndef TEST_SEED | |
38 | # define TEST_SEED 0x1234 | |
39 | #endif | |
40 | ||
41 | #define str(s) #s | |
42 | #define xstr(s) str(s) | |
43 | ||
44 | #define _FUNC_TOKEN(func, type) func##type | |
45 | #define FUNC_TOKEN(func, type) _FUNC_TOKEN(func, type) | |
46 | ||
47 | #ifndef MH_SHA256_FUNC_TYPE | |
48 | #define MH_SHA256_FUNC_TYPE | |
49 | #endif | |
50 | ||
51 | #define TEST_UPDATE_FUNCTION FUNC_TOKEN(mh_sha256_update, MH_SHA256_FUNC_TYPE) | |
52 | #define TEST_FINAL_FUNCTION FUNC_TOKEN(mh_sha256_finalize, MH_SHA256_FUNC_TYPE) | |
53 | ||
54 | #define CHECK_RETURN(state) do{ \ | |
55 | if((state) != MH_SHA256_CTX_ERROR_NONE){ \ | |
56 | printf("The mh_sha256 function is failed.\n"); \ | |
57 | return 1; \ | |
58 | } \ | |
59 | }while(0) | |
60 | ||
61 | extern void mh_sha256_ref(const void *buffer, uint32_t len, uint32_t * mh_sha256_digest); | |
62 | #define MH_SHA256_REF mh_sha256_ref | |
63 | ||
64 | // Generates pseudo-random data | |
65 | void rand_buffer(uint8_t * buf, long buffer_size) | |
66 | { | |
67 | long i; | |
68 | for (i = 0; i < buffer_size; i++) | |
69 | buf[i] = rand(); | |
70 | } | |
71 | ||
72 | void dump(char *buf, int len) | |
73 | { | |
74 | int i; | |
75 | for (i = 0; i < len;) { | |
76 | printf(" %2x", 0xff & buf[i++]); | |
77 | if (i % 32 == 0) | |
78 | printf("\n"); | |
79 | } | |
80 | if (i % 32 != 0) | |
81 | printf("\n"); | |
82 | } | |
83 | ||
84 | int compare_digests(uint32_t hash_ref[SHA256_DIGEST_WORDS], | |
85 | uint32_t hash_test[SHA256_DIGEST_WORDS]) | |
86 | { | |
87 | int i; | |
88 | int mh_sha256_fail = 0; | |
89 | ||
90 | for (i = 0; i < SHA256_DIGEST_WORDS; i++) { | |
91 | if (hash_test[i] != hash_ref[i]) | |
92 | mh_sha256_fail++; | |
93 | } | |
94 | ||
95 | if (mh_sha256_fail) { | |
96 | printf("mh_sha256 fail test\n"); | |
97 | printf("ref: "); | |
98 | dump((char *)hash_ref, 32); | |
99 | printf("test: "); | |
100 | dump((char *)hash_test, 32); | |
101 | } | |
102 | ||
103 | return mh_sha256_fail; | |
104 | } | |
105 | ||
106 | int main(int argc, char *argv[]) | |
107 | { | |
108 | int fail = 0; | |
109 | uint32_t hash_test[SHA256_DIGEST_WORDS], hash_ref[SHA256_DIGEST_WORDS]; | |
110 | uint8_t *buff = NULL; | |
111 | int size, offset; | |
112 | struct mh_sha256_ctx *update_ctx = NULL; | |
113 | ||
114 | printf(xstr(TEST_UPDATE_FUNCTION) "_test:\n"); | |
115 | ||
116 | srand(TEST_SEED); | |
117 | ||
118 | buff = malloc(TEST_LEN); | |
119 | update_ctx = malloc(sizeof(*update_ctx)); | |
120 | ||
121 | if (buff == NULL || update_ctx == NULL) { | |
122 | printf("malloc failed test aborted\n"); | |
123 | return -1; | |
124 | } | |
125 | // Rand test1 | |
126 | rand_buffer(buff, TEST_LEN); | |
127 | ||
128 | MH_SHA256_REF(buff, TEST_LEN, hash_ref); | |
129 | CHECK_RETURN(mh_sha256_init(update_ctx)); | |
130 | CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff, TEST_LEN)); | |
131 | CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test)); | |
132 | ||
133 | fail = compare_digests(hash_ref, hash_test); | |
134 | ||
135 | if (fail) { | |
136 | printf("fail rand1 test\n"); | |
137 | return -1; | |
138 | } else | |
139 | putchar('.'); | |
140 | ||
141 | // Test various size messages | |
142 | for (size = TEST_LEN; size >= 0; size--) { | |
143 | ||
144 | // Fill with rand data | |
145 | rand_buffer(buff, size); | |
146 | ||
147 | MH_SHA256_REF(buff, size, hash_ref); | |
148 | CHECK_RETURN(mh_sha256_init(update_ctx)); | |
149 | CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff, size)); | |
150 | CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test)); | |
151 | ||
152 | fail = compare_digests(hash_ref, hash_test); | |
153 | ||
154 | if (fail) { | |
155 | printf("Fail size=%d\n", size); | |
156 | return -1; | |
157 | } | |
158 | ||
159 | if ((size & 0xff) == 0) { | |
160 | putchar('.'); | |
161 | fflush(0); | |
162 | } | |
163 | } | |
164 | ||
165 | // Test various buffer offsets and sizes | |
166 | printf("offset tests"); | |
167 | for (size = TEST_LEN - 256; size > 256; size -= 11) { | |
168 | for (offset = 0; offset < 256; offset++) { | |
169 | MH_SHA256_REF(buff + offset, size, hash_ref); | |
170 | ||
171 | CHECK_RETURN(mh_sha256_init(update_ctx)); | |
172 | CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff + offset, size)); | |
173 | CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test)); | |
174 | ||
175 | fail = compare_digests(hash_ref, hash_test); | |
176 | ||
177 | if (fail) { | |
178 | printf("Fail size=%d\n", size); | |
179 | return -1; | |
180 | } | |
181 | ||
182 | } | |
183 | if ((size & 0xf) == 0) { | |
184 | putchar('.'); | |
185 | fflush(0); | |
186 | } | |
187 | } | |
188 | ||
189 | // Run efence tests | |
190 | printf("efence tests"); | |
191 | for (size = TEST_SIZE; size > 0; size--) { | |
192 | offset = TEST_LEN - size; | |
193 | ||
194 | MH_SHA256_REF(buff + offset, size, hash_ref); | |
195 | ||
196 | CHECK_RETURN(mh_sha256_init(update_ctx)); | |
197 | CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff + offset, size)); | |
198 | CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test)); | |
199 | ||
200 | fail = compare_digests(hash_ref, hash_test); | |
201 | ||
202 | if (fail) { | |
203 | printf("Fail size=%d\n", size); | |
204 | return -1; | |
205 | } | |
206 | ||
207 | if ((size & 0xf) == 0) { | |
208 | putchar('.'); | |
209 | fflush(0); | |
210 | } | |
211 | } | |
212 | ||
213 | printf(xstr(TEST_UPDATE_FUNCTION) "_test:"); | |
214 | printf(" %s\n", fail == 0 ? "Pass" : "Fail"); | |
215 | ||
216 | return fail; | |
217 | } |