1 /**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
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
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.
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 **********************************************************************/
32 #include "mh_sha1_murmur3_x64_128.h"
34 #define TEST_LEN 16*1024
35 #define TEST_SIZE 8*1024
36 #define TEST_MEM TEST_LEN
38 # define TEST_SEED 0x1234
42 #define xstr(s) str(s)
44 #define _FUNC_TOKEN(func, type) func##type
45 #define FUNC_TOKEN(func, type) _FUNC_TOKEN(func, type)
47 #ifndef MH_SHA1_FUNC_TYPE
48 #define MH_SHA1_FUNC_TYPE
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)
54 #define CHECK_RETURN(state) do{ \
55 if((state) != MH_SHA1_MURMUR3_CTX_ERROR_NONE){ \
56 printf("The stitch function is failed.\n"); \
61 extern void mh_sha1_ref(const void *buffer
, uint32_t len
, uint32_t * mh_sha1_digest
);
63 extern void murmur3_x64_128(const void *buffer
, uint32_t len
, uint64_t murmur_seed
,
64 uint32_t * murmur3_x64_128_digest
);
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
)
69 mh_sha1_ref(buffer
, len
, mh_sha1_digest
);
70 murmur3_x64_128(buffer
, len
, murmur_seed
, murmur3_x64_128_digest
);
75 // Generates pseudo-random data
76 void rand_buffer(uint8_t * buf
, long buffer_size
)
79 for (i
= 0; i
< buffer_size
; i
++)
83 void dump(char *buf
, int len
)
86 for (i
= 0; i
< len
;) {
87 printf(" %2x", 0xff & buf
[i
++]);
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
])
101 int mh_sha1_fail
= 0;
102 int murmur3_fail
= 0;
104 for (i
= 0; i
< SHA1_DIGEST_WORDS
; i
++) {
105 if (hash_test
[i
] != hash_base
[i
])
109 for (i
= 0; i
< MURMUR3_x64_128_DIGEST_WORDS
; i
++) {
110 if (murmur3_test
[i
] != murmur3_base
[i
])
115 printf("mh_sha1 fail test\n");
117 dump((char *)hash_base
, 20);
119 dump((char *)hash_test
, 20);
122 printf("murmur3 fail test\n");
124 dump((char *)murmur3_base
, 16);
126 dump((char *)murmur3_test
, 16);
129 return mh_sha1_fail
+ murmur3_fail
;
132 int main(int argc
, char *argv
[])
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
;
140 int size1
, size2
, offset
, addr_offset
;
141 struct mh_sha1_murmur3_x64_128_ctx
*update_ctx
= NULL
;
142 uint8_t *mem_addr
= NULL
;
144 printf(" " xstr(TEST_UPDATE_FUNCTION
) "_test:");
148 buff
= malloc(TEST_LEN
);
149 update_ctx
= malloc(sizeof(*update_ctx
));
151 if (buff
== NULL
|| update_ctx
== NULL
) {
152 printf("malloc failed test aborted\n");
156 rand_buffer(buff
, TEST_LEN
);
158 mh_sha1_murmur3_x64_128_base(buff
, TEST_LEN
, TEST_SEED
, hash_base
, murmur3_base
);
160 CHECK_RETURN(mh_sha1_murmur3_x64_128_init(update_ctx
, TEST_SEED
));
161 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx
, buff
, TEST_LEN
));
162 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx
, hash_test
, murmur3_test
));
164 fail
= compare_digests(hash_base
, hash_test
, murmur3_base
, murmur3_test
);
167 printf("fail rand1 test\n");
172 // Test various size messages by update twice.
173 printf("\n various size messages by update twice tests");
174 for (size1
= TEST_LEN
; size1
>= 0; size1
--) {
176 // Fill with rand data
177 rand_buffer(buff
, TEST_LEN
);
179 mh_sha1_murmur3_x64_128_base(buff
, TEST_LEN
, TEST_SEED
, hash_base
,
183 size2
= TEST_LEN
- size1
; // size2 is different with the former
184 CHECK_RETURN(mh_sha1_murmur3_x64_128_init(update_ctx
, TEST_SEED
));
185 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx
, buff
, size1
));
186 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx
, buff
+ size1
, size2
));
187 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx
, hash_test
, murmur3_test
));
189 fail
= compare_digests(hash_base
, hash_test
, murmur3_base
, murmur3_test
);
192 printf("Fail size1=%d\n", size1
);
196 if ((size2
& 0xff) == 0) {
202 // Test various update count
203 printf("\n various update count tests");
204 for (update_count
= 1; update_count
<= TEST_LEN
; update_count
++) {
206 // Fill with rand data
207 rand_buffer(buff
, TEST_LEN
);
209 mh_sha1_murmur3_x64_128_base(buff
, TEST_LEN
, TEST_SEED
, hash_base
,
213 size1
= TEST_LEN
/ update_count
;
214 size2
= TEST_LEN
- size1
* (update_count
- 1); // size2 is different with the former
216 CHECK_RETURN(mh_sha1_murmur3_x64_128_init(update_ctx
, TEST_SEED
));
217 for (i
= 1, offset
= 0; i
< update_count
; i
++) {
218 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx
, buff
+ offset
, size1
));
221 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx
, buff
+ offset
, size2
));
222 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx
, hash_test
, murmur3_test
));
224 fail
= compare_digests(hash_base
, hash_test
, murmur3_base
, murmur3_test
);
227 printf("Fail size1=%d\n", size1
);
231 if ((size2
& 0xff) == 0) {
237 // test various start address of ctx.
238 printf("\n various start address of ctx test");
240 mem_addr
= (uint8_t *) malloc(sizeof(*update_ctx
) + AVX512_ALIGNED
* 10);
241 for (addr_offset
= AVX512_ALIGNED
* 10; addr_offset
>= 0; addr_offset
--) {
243 // Fill with rand data
244 rand_buffer(buff
, TEST_LEN
);
246 mh_sha1_murmur3_x64_128_base(buff
, TEST_LEN
, TEST_SEED
, hash_base
,
249 // a unaligned offset
250 update_ctx
= (struct mh_sha1_murmur3_x64_128_ctx
*)(mem_addr
+ addr_offset
);
251 CHECK_RETURN(mh_sha1_murmur3_x64_128_init(update_ctx
, TEST_SEED
));
252 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx
, buff
, TEST_LEN
));
253 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx
, hash_test
, murmur3_test
));
255 fail
= compare_digests(hash_base
, hash_test
, murmur3_base
, murmur3_test
);
258 printf("Fail addr_offset=%d\n", addr_offset
);
262 if ((addr_offset
& 0xf) == 0) {
268 printf("\n" xstr(TEST_UPDATE_FUNCTION
) "_test: %s\n", fail
== 0 ? "Pass" : "Fail");