]>
git.proxmox.com Git - ceph.git/blob - 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.
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_sha256.h"
37 // Loop many times over same
38 # define TEST_LEN 16*1024
39 # define TEST_LOOPS 20000
40 # define TEST_TYPE_STR "_warm"
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"
49 # define TEST_SEED 0x1234
51 #define TEST_MEM TEST_LEN
54 #define xstr(s) str(s)
56 #define _FUNC_TOKEN(func, type) func##type
57 #define FUNC_TOKEN(func, type) _FUNC_TOKEN(func, type)
59 #ifndef MH_SHA256_FUNC_TYPE
60 #define MH_SHA256_FUNC_TYPE
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)
66 #define CHECK_RETURN(state) do{ \
67 if((state) != MH_SHA256_CTX_ERROR_NONE){ \
68 printf("The mh_sha256 function is failed.\n"); \
73 // Generates pseudo-random data
74 void rand_buffer(uint8_t * buf
, long buffer_size
)
77 for (i
= 0; i
< buffer_size
; i
++)
81 void dump(char *buf
, int len
)
84 for (i
= 0; i
< len
;) {
85 printf(" %2x", 0xff & buf
[i
++]);
93 int compare_digests(uint32_t hash_base
[SHA256_DIGEST_WORDS
],
94 uint32_t hash_test
[SHA256_DIGEST_WORDS
])
97 int mh_sha256_fail
= 0;
99 for (i
= 0; i
< SHA256_DIGEST_WORDS
; i
++) {
100 if (hash_test
[i
] != hash_base
[i
])
104 if (mh_sha256_fail
) {
105 printf("mh_sha256 fail test\n");
107 dump((char *)hash_base
, 32);
109 dump((char *)hash_test
, 32);
112 return mh_sha256_fail
;
115 int main(int argc
, char *argv
[])
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
;
123 printf(xstr(TEST_UPDATE_FUNCTION
) "_perf:\n");
125 buff
= malloc(TEST_LEN
);
126 update_ctx_test
= malloc(sizeof(*update_ctx_test
));
127 update_ctx_base
= malloc(sizeof(*update_ctx_base
));
129 if (buff
== NULL
|| update_ctx_base
== NULL
|| update_ctx_test
== NULL
) {
130 printf("malloc failed test aborted\n");
134 rand_buffer(buff
, TEST_LEN
);
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
);
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
);
148 printf("mh_sha256_update_base" TEST_TYPE_STR
": ");
149 perf_print(stop
, start
, (long long)TEST_MEM
* i
);
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
));
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
));
163 printf(xstr(TEST_UPDATE_FUNCTION
) TEST_TYPE_STR
": ");
164 perf_print(stop
, start
, (long long)TEST_MEM
* i
);
167 fail
= compare_digests(hash_base
, hash_test
);
170 printf("Fail size=%d\n", TEST_LEN
);
175 printf("Test failed function test%d\n", fail
);
177 printf("Pass func check\n");