]> 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_update_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_update_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, i;
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 update_count;
140 int size1, size2, offset, addr_offset;
141 struct mh_sha1_murmur3_x64_128_ctx *update_ctx = NULL;
142 uint8_t *mem_addr = NULL;
143
144 printf(" " xstr(TEST_UPDATE_FUNCTION) "_test:");
145
146 srand(TEST_SEED);
147
148 buff = malloc(TEST_LEN);
149 update_ctx = malloc(sizeof(*update_ctx));
150
151 if (buff == NULL || update_ctx == NULL) {
152 printf("malloc failed test aborted\n");
153 return -1;
154 }
155 // Rand test1
156 rand_buffer(buff, TEST_LEN);
157
158 mh_sha1_murmur3_x64_128_base(buff, TEST_LEN, TEST_SEED, hash_base, murmur3_base);
159
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));
163
164 fail = compare_digests(hash_base, hash_test, murmur3_base, murmur3_test);
165
166 if (fail) {
167 printf("fail rand1 test\n");
168 return -1;
169 } else
170 putchar('.');
171
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--) {
175
176 // Fill with rand data
177 rand_buffer(buff, TEST_LEN);
178
179 mh_sha1_murmur3_x64_128_base(buff, TEST_LEN, TEST_SEED, hash_base,
180 murmur3_base);
181
182 // subsequent update
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));
188
189 fail = compare_digests(hash_base, hash_test, murmur3_base, murmur3_test);
190
191 if (fail) {
192 printf("Fail size1=%d\n", size1);
193 return -1;
194 }
195
196 if ((size2 & 0xff) == 0) {
197 putchar('.');
198 fflush(0);
199 }
200 }
201
202 // Test various update count
203 printf("\n various update count tests");
204 for (update_count = 1; update_count <= TEST_LEN; update_count++) {
205
206 // Fill with rand data
207 rand_buffer(buff, TEST_LEN);
208
209 mh_sha1_murmur3_x64_128_base(buff, TEST_LEN, TEST_SEED, hash_base,
210 murmur3_base);
211
212 // subsequent update
213 size1 = TEST_LEN / update_count;
214 size2 = TEST_LEN - size1 * (update_count - 1); // size2 is different with the former
215
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));
219 offset += size1;
220 }
221 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff + offset, size2));
222 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test, murmur3_test));
223
224 fail = compare_digests(hash_base, hash_test, murmur3_base, murmur3_test);
225
226 if (fail) {
227 printf("Fail size1=%d\n", size1);
228 return -1;
229 }
230
231 if ((size2 & 0xff) == 0) {
232 putchar('.');
233 fflush(0);
234 }
235 }
236
237 // test various start address of ctx.
238 printf("\n various start address of ctx test");
239 free(update_ctx);
240 mem_addr = (uint8_t *) malloc(sizeof(*update_ctx) + AVX512_ALIGNED * 10);
241 for (addr_offset = AVX512_ALIGNED * 10; addr_offset >= 0; addr_offset--) {
242
243 // Fill with rand data
244 rand_buffer(buff, TEST_LEN);
245
246 mh_sha1_murmur3_x64_128_base(buff, TEST_LEN, TEST_SEED, hash_base,
247 murmur3_base);
248
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));
254
255 fail = compare_digests(hash_base, hash_test, murmur3_base, murmur3_test);
256
257 if (fail) {
258 printf("Fail addr_offset=%d\n", addr_offset);
259 return -1;
260 }
261
262 if ((addr_offset & 0xf) == 0) {
263 putchar('.');
264 fflush(0);
265 }
266 }
267
268 printf("\n" xstr(TEST_UPDATE_FUNCTION) "_test: %s\n", fail == 0 ? "Pass" : "Fail");
269
270 return fail;
271
272 }