]> git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/mh_sha1_murmur3_x64_128/mh_sha1_murmur3_x64_128_test.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / crypto / isa-l / isa-l_crypto / mh_sha1_murmur3_x64_128 / mh_sha1_murmur3_x64_128_test.c
1 /**********************************************************************
2 Copyright(c) 2011-2016 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_sha1_murmur3_x64_128.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_SHA1_FUNC_TYPE
48 #define MH_SHA1_FUNC_TYPE
49 #endif
50
51 #define TEST_UPDATE_FUNCTION FUNC_TOKEN(mh_sha1_murmur3_x64_128_update, MH_SHA1_FUNC_TYPE)
52 #define TEST_FINAL_FUNCTION FUNC_TOKEN(mh_sha1_murmur3_x64_128_finalize, MH_SHA1_FUNC_TYPE)
53
54 #define CHECK_RETURN(state) do{ \
55 if((state) != MH_SHA1_MURMUR3_CTX_ERROR_NONE){ \
56 printf("The stitch function is failed.\n"); \
57 return 1; \
58 } \
59 }while(0)
60
61 extern void mh_sha1_ref(const void *buffer, uint32_t len, uint32_t * mh_sha1_digest);
62
63 extern void murmur3_x64_128(const void *buffer, uint32_t len, uint64_t murmur_seed,
64 uint32_t * murmur3_x64_128_digest);
65
66 void mh_sha1_murmur3_x64_128_base(const void *buffer, uint32_t len, uint64_t murmur_seed,
67 uint32_t * mh_sha1_digest, uint32_t * murmur3_x64_128_digest)
68 {
69 mh_sha1_ref(buffer, len, mh_sha1_digest);
70 murmur3_x64_128(buffer, len, murmur_seed, murmur3_x64_128_digest);
71
72 return;
73 }
74
75 // Generates pseudo-random data
76 void rand_buffer(uint8_t * buf, long buffer_size)
77 {
78 long i;
79 for (i = 0; i < buffer_size; i++)
80 buf[i] = rand();
81 }
82
83 void dump(char *buf, int len)
84 {
85 int i;
86 for (i = 0; i < len;) {
87 printf(" %2x", 0xff & buf[i++]);
88 if (i % 20 == 0)
89 printf("\n");
90 }
91 if (i % 20 != 0)
92 printf("\n");
93 }
94
95 int compare_digests(uint32_t hash_base[SHA1_DIGEST_WORDS],
96 uint32_t hash_test[SHA1_DIGEST_WORDS],
97 uint32_t murmur3_base[MURMUR3_x64_128_DIGEST_WORDS],
98 uint32_t murmur3_test[MURMUR3_x64_128_DIGEST_WORDS])
99 {
100 int i;
101 int mh_sha1_fail = 0;
102 int murmur3_fail = 0;
103
104 for (i = 0; i < SHA1_DIGEST_WORDS; i++) {
105 if (hash_test[i] != hash_base[i])
106 mh_sha1_fail++;
107 }
108
109 for (i = 0; i < MURMUR3_x64_128_DIGEST_WORDS; i++) {
110 if (murmur3_test[i] != murmur3_base[i])
111 murmur3_fail++;
112 }
113
114 if (mh_sha1_fail) {
115 printf("mh_sha1 fail test\n");
116 printf("base: ");
117 dump((char *)hash_base, 20);
118 printf("ref: ");
119 dump((char *)hash_test, 20);
120 }
121 if (murmur3_fail) {
122 printf("murmur3 fail test\n");
123 printf("base: ");
124 dump((char *)murmur3_base, 16);
125 printf("ref: ");
126 dump((char *)murmur3_test, 16);
127 }
128
129 return mh_sha1_fail + murmur3_fail;
130 }
131
132 int main(int argc, char *argv[])
133 {
134 int fail = 0;
135 uint32_t hash_test[SHA1_DIGEST_WORDS], hash_base[SHA1_DIGEST_WORDS];
136 uint32_t murmur3_test[MURMUR3_x64_128_DIGEST_WORDS],
137 murmur3_base[MURMUR3_x64_128_DIGEST_WORDS];
138 uint8_t *buff = NULL;
139 int size, offset;
140 struct mh_sha1_murmur3_x64_128_ctx *update_ctx = NULL;
141
142 printf(" " xstr(TEST_UPDATE_FUNCTION) "_test:");
143
144 srand(TEST_SEED);
145
146 buff = malloc(TEST_LEN);
147 update_ctx = malloc(sizeof(*update_ctx));
148
149 if (buff == NULL || update_ctx == NULL) {
150 printf("malloc failed test aborted\n");
151 return -1;
152 }
153 // Rand test1
154 rand_buffer(buff, TEST_LEN);
155
156 mh_sha1_murmur3_x64_128_base(buff, TEST_LEN, TEST_SEED, hash_base, murmur3_base);
157
158 CHECK_RETURN(mh_sha1_murmur3_x64_128_init(update_ctx, TEST_SEED));
159 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff, TEST_LEN));
160 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test, murmur3_test));
161
162 fail = compare_digests(hash_base, hash_test, murmur3_base, murmur3_test);
163
164 if (fail) {
165 printf("fail rand1 test\n");
166 return -1;
167 } else
168 putchar('.');
169
170 // Test various size messages
171 for (size = TEST_LEN; size >= 0; size--) {
172
173 // Fill with rand data
174 rand_buffer(buff, size);
175
176 mh_sha1_murmur3_x64_128_base(buff, size, TEST_SEED, hash_base, murmur3_base);
177
178 CHECK_RETURN(mh_sha1_murmur3_x64_128_init(update_ctx, TEST_SEED));
179 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff, size));
180 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test, murmur3_test));
181
182 fail = compare_digests(hash_base, hash_test, murmur3_base, murmur3_test);
183
184 if (fail) {
185 printf("Fail size=%d\n", size);
186 return -1;
187 }
188
189 if ((size & 0xff) == 0) {
190 putchar('.');
191 fflush(0);
192 }
193 }
194
195 // Test various buffer offsets and sizes
196 printf("offset tests");
197 for (size = TEST_LEN - 256; size > 256; size -= 11) {
198 for (offset = 0; offset < 256; offset++) {
199 mh_sha1_murmur3_x64_128_base(buff + offset, size, TEST_SEED,
200 hash_base, murmur3_base);
201
202 CHECK_RETURN(mh_sha1_murmur3_x64_128_init(update_ctx, TEST_SEED));
203 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff + offset, size));
204 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test, murmur3_test));
205
206 fail =
207 compare_digests(hash_base, hash_test, murmur3_base, murmur3_test);
208
209 if (fail) {
210 printf("Fail size=%d offset=%d\n", size, offset);
211 return -1;
212 }
213
214 }
215 if ((size & 0xf) == 0) {
216 putchar('.');
217 fflush(0);
218 }
219 }
220
221 // Run efence tests
222 printf("efence tests");
223 for (size = TEST_SIZE; size > 0; size--) {
224 offset = TEST_LEN - size;
225 mh_sha1_murmur3_x64_128_base(buff + offset, size, TEST_SEED,
226 hash_base, murmur3_base);
227
228 CHECK_RETURN(mh_sha1_murmur3_x64_128_init(update_ctx, TEST_SEED));
229 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff + offset, size));
230 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test, murmur3_test));
231
232 fail = compare_digests(hash_base, hash_test, murmur3_base, murmur3_test);
233
234 if (fail) {
235 printf("Fail size=%d offset=%d\n", size, offset);
236 return -1;
237 }
238
239 if ((size & 0xf) == 0) {
240 putchar('.');
241 fflush(0);
242 }
243 }
244
245 printf("\n" xstr(TEST_UPDATE_FUNCTION) "_test: %s\n", fail == 0 ? "Pass" : "Fail");
246
247 return fail;
248 }