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 "isal_crypto_api.h"
33 #include "mh_sha256.h"
34
35 #define TEST_LEN 16 * 1024
36 #define TEST_SIZE 8 * 1024
37 #define TEST_MEM TEST_LEN
38 #ifndef TEST_SEED
39 #define TEST_SEED 0x1234
40 #endif
41
42 #define str(s) #s
43 #define xstr(s) str(s)
44
45 #define _FUNC_TOKEN(func, type) func##type
46 #define FUNC_TOKEN(func, type) _FUNC_TOKEN(func, type)
47
48 #ifndef MH_SHA256_FUNC_TYPE
49 #define MH_SHA256_FUNC_TYPE
50 #endif
51
52 #define TEST_UPDATE_FUNCTION FUNC_TOKEN(isal_mh_sha256_update, MH_SHA256_FUNC_TYPE)
53 #define TEST_FINAL_FUNCTION FUNC_TOKEN(isal_mh_sha256_finalize, MH_SHA256_FUNC_TYPE)
54
55 #define CHECK_RETURN(state) \
56 do { \
57 if ((state) != ISAL_CRYPTO_ERR_NONE) { \
58 printf("The mh_sha256 function is failed.\n"); \
59 return 1; \
60 } \
61 } while (0)
62
63 extern void
64 mh_sha256_ref(const void *buffer, uint32_t len, uint32_t *mh_sha256_digest);
65
66 // Generates pseudo-random data
67 void
rand_buffer(uint8_t * buf,long buffer_size)68 rand_buffer(uint8_t *buf, long buffer_size)
69 {
70 long i;
71 for (i = 0; i < buffer_size; i++)
72 buf[i] = rand();
73 }
74
75 void
dump(char * buf,int len)76 dump(char *buf, int len)
77 {
78 int i;
79 for (i = 0; i < len;) {
80 printf(" %2x", 0xff & buf[i++]);
81 if (i % 20 == 0)
82 printf("\n");
83 }
84 if (i % 20 != 0)
85 printf("\n");
86 }
87
88 int
compare_digests(uint32_t hash_ref[ISAL_SHA256_DIGEST_WORDS],uint32_t hash_test[ISAL_SHA256_DIGEST_WORDS])89 compare_digests(uint32_t hash_ref[ISAL_SHA256_DIGEST_WORDS],
90 uint32_t hash_test[ISAL_SHA256_DIGEST_WORDS])
91 {
92 int i;
93 int mh_sha256_fail = 0;
94
95 for (i = 0; i < ISAL_SHA256_DIGEST_WORDS; i++) {
96 if (hash_test[i] != hash_ref[i])
97 mh_sha256_fail++;
98 }
99
100 if (mh_sha256_fail) {
101 printf("mh_sha256 fail test\n");
102 printf("ref: ");
103 dump((char *) hash_ref, 20);
104 printf("test: ");
105 dump((char *) hash_test, 20);
106 }
107
108 return mh_sha256_fail;
109 }
110
111 int
main(int argc,char * argv[])112 main(int argc, char *argv[])
113 {
114 int fail = 0;
115 #ifndef FIPS_MODE
116 uint32_t hash_test[ISAL_SHA256_DIGEST_WORDS], hash_ref[ISAL_SHA256_DIGEST_WORDS];
117 uint8_t *buff = NULL;
118 int i, update_count;
119 int size1, size2, offset, addr_offset;
120 struct isal_mh_sha256_ctx *update_ctx = NULL;
121 uint8_t *mem_addr = NULL;
122
123 printf(xstr(TEST_UPDATE_FUNCTION) "_test:");
124
125 srand(TEST_SEED);
126
127 buff = malloc(TEST_LEN);
128 update_ctx = malloc(sizeof(*update_ctx));
129
130 if (buff == NULL || update_ctx == NULL) {
131 printf("malloc failed test aborted\n");
132 goto end_ctx;
133 }
134 // Rand test1
135 rand_buffer(buff, TEST_LEN);
136
137 mh_sha256_ref(buff, TEST_LEN, hash_ref);
138
139 CHECK_RETURN(isal_mh_sha256_init(update_ctx));
140 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff, TEST_LEN));
141 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test));
142
143 fail = compare_digests(hash_ref, hash_test);
144
145 if (fail) {
146 printf("fail rand1 test\n");
147 goto end_ctx;
148 } else
149 putchar('.');
150
151 // Test various size messages by update twice.
152 printf("\n various size messages by update twice tests");
153 for (size1 = TEST_LEN; size1 >= 0; size1--) {
154
155 // Fill with rand data
156 rand_buffer(buff, TEST_LEN);
157
158 mh_sha256_ref(buff, TEST_LEN, hash_ref);
159
160 // subsequent update
161 size2 = TEST_LEN - size1; // size2 is different with the former
162 CHECK_RETURN(isal_mh_sha256_init(update_ctx));
163 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff, size1));
164 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff + size1, size2));
165 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test));
166
167 fail = compare_digests(hash_ref, hash_test);
168
169 if (fail) {
170 printf("Fail size1=%d\n", size1);
171 goto end_ctx;
172 }
173
174 if ((size2 & 0xff) == 0) {
175 putchar('.');
176 fflush(0);
177 }
178 }
179
180 // Test various update count
181 printf("\n various update count tests");
182 for (update_count = 1; update_count <= TEST_LEN; update_count++) {
183
184 // Fill with rand data
185 rand_buffer(buff, TEST_LEN);
186
187 mh_sha256_ref(buff, TEST_LEN, hash_ref);
188
189 // subsequent update
190 size1 = TEST_LEN / update_count;
191 size2 = TEST_LEN - size1 * (update_count - 1); // size2 is different with the former
192
193 CHECK_RETURN(isal_mh_sha256_init(update_ctx));
194 for (i = 1, offset = 0; i < update_count; i++) {
195 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff + offset, size1));
196 offset += size1;
197 }
198 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff + offset, size2));
199 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test));
200
201 fail = compare_digests(hash_ref, hash_test);
202
203 if (fail) {
204 printf("Fail size1=%d\n", size1);
205 goto end_ctx;
206 }
207
208 if ((size2 & 0xff) == 0) {
209 putchar('.');
210 fflush(0);
211 }
212 }
213
214 // test various start address of ctx.
215 printf("\n various start address of ctx test");
216
217 free(update_ctx);
218
219 // test various start address of ctx.
220 printf("\n various start address of ctx test");
221 mem_addr = (uint8_t *) malloc(sizeof(*update_ctx) + ISAL_AVX512_ALIGNED * 10);
222 if (mem_addr == NULL) {
223 fail++;
224 goto end;
225 }
226
227 for (addr_offset = ISAL_AVX512_ALIGNED * 10; addr_offset >= 0; addr_offset--) {
228
229 // Fill with rand data
230 rand_buffer(buff, TEST_LEN);
231
232 mh_sha256_ref(buff, TEST_LEN, hash_ref);
233
234 // a unaligned offset
235 update_ctx = (struct isal_mh_sha256_ctx *) (mem_addr + addr_offset);
236 CHECK_RETURN(isal_mh_sha256_init(update_ctx));
237 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff, TEST_LEN));
238 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test));
239
240 fail = compare_digests(hash_ref, hash_test);
241
242 if (fail) {
243 printf("Fail addr_offset=%d\n", addr_offset);
244 goto end;
245 }
246
247 if ((addr_offset & 0xf) == 0) {
248 putchar('.');
249 fflush(0);
250 }
251 }
252 end:
253 if (mem_addr != NULL)
254 free(mem_addr);
255 if (buff != NULL)
256 free(buff);
257
258 printf("\n" xstr(TEST_UPDATE_FUNCTION) "_test: %s\n", fail == 0 ? "Pass" : "Fail");
259 #else
260 printf("Not Executed\n");
261 #endif /* FIPS_MODE */
262 return fail;
263
264 #ifndef FIPS_MODE
265 end_ctx:
266 if (update_ctx != NULL)
267 free(update_ctx);
268 goto end;
269 #endif
270 }
271