]> git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/mh_sha256/mh_sha256_perf.c
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / crypto / isa-l / isa-l_crypto / mh_sha256 / mh_sha256_perf.c
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 #include "test.h"
34
35 //#define CACHED_TEST
36 #ifdef CACHED_TEST
37 // Loop many times over same
38 # define TEST_LEN 16*1024
39 # define TEST_LOOPS 20000
40 # define TEST_TYPE_STR "_warm"
41 #else
42 // Uncached test. Pull from large mem base.
43 # define TEST_LEN 16*1024*1024
44 # define TEST_LOOPS 100
45 # define TEST_TYPE_STR "_cold"
46 #endif
47
48 #ifndef TEST_SEED
49 # define TEST_SEED 0x1234
50 #endif
51 #define TEST_MEM TEST_LEN
52
53 #define str(s) #s
54 #define xstr(s) str(s)
55
56 #define _FUNC_TOKEN(func, type) func##type
57 #define FUNC_TOKEN(func, type) _FUNC_TOKEN(func, type)
58
59 #ifndef MH_SHA256_FUNC_TYPE
60 #define MH_SHA256_FUNC_TYPE
61 #endif
62
63 #define TEST_UPDATE_FUNCTION FUNC_TOKEN(mh_sha256_update, MH_SHA256_FUNC_TYPE)
64 #define TEST_FINAL_FUNCTION FUNC_TOKEN(mh_sha256_finalize, MH_SHA256_FUNC_TYPE)
65
66 #define CHECK_RETURN(state) do{ \
67 if((state) != MH_SHA256_CTX_ERROR_NONE){ \
68 printf("The mh_sha256 function is failed.\n"); \
69 return 1; \
70 } \
71 }while(0)
72
73 // Generates pseudo-random data
74 void rand_buffer(uint8_t * buf, long buffer_size)
75 {
76 long i;
77 for (i = 0; i < buffer_size; i++)
78 buf[i] = rand();
79 }
80
81 void dump(char *buf, int len)
82 {
83 int i;
84 for (i = 0; i < len;) {
85 printf(" %2x", 0xff & buf[i++]);
86 if (i % 32 == 0)
87 printf("\n");
88 }
89 if (i % 32 != 0)
90 printf("\n");
91 }
92
93 int compare_digests(uint32_t hash_base[SHA256_DIGEST_WORDS],
94 uint32_t hash_test[SHA256_DIGEST_WORDS])
95 {
96 int i;
97 int mh_sha256_fail = 0;
98
99 for (i = 0; i < SHA256_DIGEST_WORDS; i++) {
100 if (hash_test[i] != hash_base[i])
101 mh_sha256_fail++;
102 }
103
104 if (mh_sha256_fail) {
105 printf("mh_sha256 fail test\n");
106 printf("base: ");
107 dump((char *)hash_base, 32);
108 printf("ref: ");
109 dump((char *)hash_test, 32);
110 }
111
112 return mh_sha256_fail;
113 }
114
115 int main(int argc, char *argv[])
116 {
117 int i, fail = 0;
118 uint32_t hash_test[SHA256_DIGEST_WORDS], hash_base[SHA256_DIGEST_WORDS];
119 uint8_t *buff = NULL;
120 struct mh_sha256_ctx *update_ctx_test = NULL, *update_ctx_base = NULL;
121 struct perf start, stop;
122
123 printf(xstr(TEST_UPDATE_FUNCTION) "_perf:\n");
124
125 buff = malloc(TEST_LEN);
126 update_ctx_test = malloc(sizeof(*update_ctx_test));
127 update_ctx_base = malloc(sizeof(*update_ctx_base));
128
129 if (buff == NULL || update_ctx_base == NULL || update_ctx_test == NULL) {
130 printf("malloc failed test aborted\n");
131 return -1;
132 }
133 // Rand test1
134 rand_buffer(buff, TEST_LEN);
135
136 // mh_sha256 base version
137 mh_sha256_init(update_ctx_base);
138 mh_sha256_update_base(update_ctx_base, buff, TEST_LEN);
139 mh_sha256_finalize_base(update_ctx_base, hash_base);
140
141 perf_start(&start);
142 for (i = 0; i < TEST_LOOPS / 10; i++) {
143 mh_sha256_init(update_ctx_base);
144 mh_sha256_update_base(update_ctx_base, buff, TEST_LEN);
145 mh_sha256_finalize_base(update_ctx_base, hash_base);
146 }
147 perf_stop(&stop);
148 printf("mh_sha256_update_base" TEST_TYPE_STR ": ");
149 perf_print(stop, start, (long long)TEST_MEM * i);
150
151 //Update feature test
152 CHECK_RETURN(mh_sha256_init(update_ctx_test));
153 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx_test, buff, TEST_LEN));
154 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx_test, hash_test));
155
156 perf_start(&start);
157 for (i = 0; i < TEST_LOOPS; i++) {
158 CHECK_RETURN(mh_sha256_init(update_ctx_test));
159 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx_test, buff, TEST_LEN));
160 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx_test, hash_test));
161 }
162 perf_stop(&stop);
163 printf(xstr(TEST_UPDATE_FUNCTION) TEST_TYPE_STR ": ");
164 perf_print(stop, start, (long long)TEST_MEM * i);
165
166 // Check results
167 fail = compare_digests(hash_base, hash_test);
168
169 if (fail) {
170 printf("Fail size=%d\n", TEST_LEN);
171 return -1;
172 }
173
174 if (fail)
175 printf("Test failed function test%d\n", fail);
176 else
177 printf("Pass func check\n");
178
179 return fail;
180 }