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