xref: /isa-l_crypto/mh_sha256/mh_sha256_update_test.c (revision a580224830af9413fc52ff68d9cac63347e48b09)
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 
63 // Generates pseudo-random data
64 void rand_buffer(uint8_t * buf, long buffer_size)
65 {
66 	long i;
67 	for (i = 0; i < buffer_size; i++)
68 		buf[i] = rand();
69 }
70 
71 void dump(char *buf, int len)
72 {
73 	int i;
74 	for (i = 0; i < len;) {
75 		printf(" %2x", 0xff & buf[i++]);
76 		if (i % 20 == 0)
77 			printf("\n");
78 	}
79 	if (i % 20 != 0)
80 		printf("\n");
81 }
82 
83 int compare_digests(uint32_t hash_ref[SHA256_DIGEST_WORDS],
84 		    uint32_t hash_test[SHA256_DIGEST_WORDS])
85 {
86 	int i;
87 	int mh_sha256_fail = 0;
88 
89 	for (i = 0; i < SHA256_DIGEST_WORDS; i++) {
90 		if (hash_test[i] != hash_ref[i])
91 			mh_sha256_fail++;
92 	}
93 
94 	if (mh_sha256_fail) {
95 		printf("mh_sha256 fail test\n");
96 		printf("ref: ");
97 		dump((char *)hash_ref, 20);
98 		printf("test: ");
99 		dump((char *)hash_test, 20);
100 	}
101 
102 	return mh_sha256_fail;
103 }
104 
105 int main(int argc, char *argv[])
106 {
107 	int fail = 0, i;
108 	uint32_t hash_test[SHA256_DIGEST_WORDS], hash_ref[SHA256_DIGEST_WORDS];
109 	uint8_t *buff = NULL;
110 	int update_count;
111 	int size1, size2, offset, addr_offset;
112 	struct mh_sha256_ctx *update_ctx = NULL;
113 	uint8_t *mem_addr = NULL;
114 
115 	printf(xstr(TEST_UPDATE_FUNCTION) "_test:");
116 
117 	srand(TEST_SEED);
118 
119 	buff = malloc(TEST_LEN);
120 	update_ctx = malloc(sizeof(*update_ctx));
121 
122 	if (buff == NULL || update_ctx == NULL) {
123 		printf("malloc failed test aborted\n");
124 		goto end_ctx;
125 	}
126 	// Rand test1
127 	rand_buffer(buff, TEST_LEN);
128 
129 	mh_sha256_ref(buff, TEST_LEN, hash_ref);
130 
131 	CHECK_RETURN(mh_sha256_init(update_ctx));
132 	CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff, TEST_LEN));
133 	CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test));
134 
135 	fail = compare_digests(hash_ref, hash_test);
136 
137 	if (fail) {
138 		printf("fail rand1 test\n");
139 		goto end_ctx;
140 	} else
141 		putchar('.');
142 
143 	// Test various size messages by update twice.
144 	printf("\n various size messages by update twice tests");
145 	for (size1 = TEST_LEN; size1 >= 0; size1--) {
146 
147 		// Fill with rand data
148 		rand_buffer(buff, TEST_LEN);
149 
150 		mh_sha256_ref(buff, TEST_LEN, hash_ref);
151 
152 		// subsequent update
153 		size2 = TEST_LEN - size1;	// size2 is different with the former
154 		CHECK_RETURN(mh_sha256_init(update_ctx));
155 		CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff, size1));
156 		CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff + size1, size2));
157 		CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test));
158 
159 		fail = compare_digests(hash_ref, hash_test);
160 
161 		if (fail) {
162 			printf("Fail size1=%d\n", size1);
163 			goto end_ctx;
164 		}
165 
166 		if ((size2 & 0xff) == 0) {
167 			putchar('.');
168 			fflush(0);
169 		}
170 	}
171 
172 	// Test various update count
173 	printf("\n various update count tests");
174 	for (update_count = 1; update_count <= TEST_LEN; update_count++) {
175 
176 		// Fill with rand data
177 		rand_buffer(buff, TEST_LEN);
178 
179 		mh_sha256_ref(buff, TEST_LEN, hash_ref);
180 
181 		// subsequent update
182 		size1 = TEST_LEN / update_count;
183 		size2 = TEST_LEN - size1 * (update_count - 1);	// size2 is different with the former
184 
185 		CHECK_RETURN(mh_sha256_init(update_ctx));
186 		for (i = 1, offset = 0; i < update_count; i++) {
187 			CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff + offset, size1));
188 			offset += size1;
189 		}
190 		CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff + offset, size2));
191 		CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test));
192 
193 		fail = compare_digests(hash_ref, hash_test);
194 
195 		if (fail) {
196 			printf("Fail size1=%d\n", size1);
197 			goto end_ctx;
198 		}
199 
200 		if ((size2 & 0xff) == 0) {
201 			putchar('.');
202 			fflush(0);
203 		}
204 	}
205 
206 	// test various start address of ctx.
207 	printf("\n various start address of ctx test");
208 
209 	free(update_ctx);
210 
211 	// test various start address of ctx.
212 	printf("\n various start address of ctx test");
213 	mem_addr = (uint8_t *) malloc(sizeof(*update_ctx) + AVX512_ALIGNED * 10);
214 	if (mem_addr == NULL) {
215 		fail++;
216 		goto end;
217 	}
218 
219 	for (addr_offset = AVX512_ALIGNED * 10; addr_offset >= 0; addr_offset--) {
220 
221 		// Fill with rand data
222 		rand_buffer(buff, TEST_LEN);
223 
224 		mh_sha256_ref(buff, TEST_LEN, hash_ref);
225 
226 		// a unaligned offset
227 		update_ctx = (struct mh_sha256_ctx *)(mem_addr + addr_offset);
228 		CHECK_RETURN(mh_sha256_init(update_ctx));
229 		CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff, TEST_LEN));
230 		CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test));
231 
232 		fail = compare_digests(hash_ref, hash_test);
233 
234 		if (fail) {
235 			printf("Fail addr_offset=%d\n", addr_offset);
236 			goto end;
237 		}
238 
239 		if ((addr_offset & 0xf) == 0) {
240 			putchar('.');
241 			fflush(0);
242 		}
243 	}
244       end:
245 	if (mem_addr != NULL)
246 		free(mem_addr);
247 	if (buff != NULL)
248 		free(buff);
249 
250 	printf("\n" xstr(TEST_UPDATE_FUNCTION) "_test: %s\n", fail == 0 ? "Pass" : "Fail");
251 
252 	return fail;
253 
254       end_ctx:
255 	if (update_ctx != NULL)
256 		free(update_ctx);
257 	goto end;
258 }
259