xref: /isa-l_crypto/mh_sha256/mh_sha256_test.c (revision 2a268c597b745b55509ea38d00753e5caa85e9ac)
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 		fail++;
124 		goto end;
125 	}
126 	// Rand test1
127 	rand_buffer(buff, TEST_LEN);
128 
129 	MH_SHA256_REF(buff, TEST_LEN, hash_ref);
130 	CHECK_RETURN(mh_sha256_init(update_ctx));
131 	CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff, TEST_LEN));
132 	CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test));
133 
134 	fail = compare_digests(hash_ref, hash_test);
135 
136 	if (fail) {
137 		printf("fail rand1 test\n");
138 		goto end;
139 	} else
140 		putchar('.');
141 
142 	// Test various size messages
143 	for (size = TEST_LEN; size >= 0; size--) {
144 
145 		// Fill with rand data
146 		rand_buffer(buff, size);
147 
148 		MH_SHA256_REF(buff, size, hash_ref);
149 		CHECK_RETURN(mh_sha256_init(update_ctx));
150 		CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff, size));
151 		CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test));
152 
153 		fail = compare_digests(hash_ref, hash_test);
154 
155 		if (fail) {
156 			printf("Fail size=%d\n", size);
157 			goto end;
158 		}
159 
160 		if ((size & 0xff) == 0) {
161 			putchar('.');
162 			fflush(0);
163 		}
164 	}
165 
166 	// Test various buffer offsets and sizes
167 	printf("offset tests");
168 	for (size = TEST_LEN - 256; size > 256; size -= 11) {
169 		for (offset = 0; offset < 256; offset++) {
170 			MH_SHA256_REF(buff + offset, size, hash_ref);
171 
172 			CHECK_RETURN(mh_sha256_init(update_ctx));
173 			CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff + offset, size));
174 			CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test));
175 
176 			fail = compare_digests(hash_ref, hash_test);
177 
178 			if (fail) {
179 				printf("Fail size=%d\n", size);
180 				goto end;
181 			}
182 
183 		}
184 		if ((size & 0xf) == 0) {
185 			putchar('.');
186 			fflush(0);
187 		}
188 	}
189 
190 	// Run efence tests
191 	printf("efence tests");
192 	for (size = TEST_SIZE; size > 0; size--) {
193 		offset = TEST_LEN - size;
194 
195 		MH_SHA256_REF(buff + offset, size, hash_ref);
196 
197 		CHECK_RETURN(mh_sha256_init(update_ctx));
198 		CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff + offset, size));
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 size=%d\n", size);
205 			goto end;
206 		}
207 
208 		if ((size & 0xf) == 0) {
209 			putchar('.');
210 			fflush(0);
211 		}
212 	}
213 
214       end:
215 	if (buff != NULL)
216 		free(buff);
217 	if (update_ctx != NULL)
218 		free(update_ctx);
219 
220 	printf(xstr(TEST_UPDATE_FUNCTION) "_test:");
221 	printf(" %s\n", fail == 0 ? "Pass" : "Fail");
222 
223 	return fail;
224 }
225