xref: /isa-l_crypto/sm3_mb/sm3_mb_vs_ossl_perf.c (revision 6801b27bd9295090e36d2864110576f428efdf61)
1  /**********************************************************************
2    Copyright(c) 2020 Arm 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 Arm 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  
33  #ifndef FIPS_MODE
34  #include "sm3_mb.h"
35  #include "test.h"
36  
37  // Set number of outstanding jobs
38  #define TEST_BUFS 32
39  
40  #ifndef GT_L3_CACHE
41  #define GT_L3_CACHE 32 * 1024 * 1024 /* some number > last level cache */
42  #endif
43  
44  #if !defined(COLD_TEST) && !defined(TEST_CUSTOM)
45  // Cached test, loop many times over small dataset
46  #define TEST_LEN      4 * 1024
47  #define TEST_LOOPS    10000
48  #define TEST_TYPE_STR "_warm"
49  #elif defined(COLD_TEST)
50  // Uncached test.  Pull from large mem base.
51  #define TEST_LEN      (GT_L3_CACHE / TEST_BUFS)
52  #define TEST_LOOPS    100
53  #define TEST_TYPE_STR "_cold"
54  #endif
55  
56  #define TEST_MEM TEST_LEN *TEST_BUFS *TEST_LOOPS
57  
58  extern void
59  sm3_ossl(const unsigned char *buf, size_t length, unsigned char *digest);
60  /* Reference digest global to reduce stack usage */
61  static uint8_t digest_ssl[TEST_BUFS][4 * ISAL_SM3_DIGEST_NWORDS];
62  #endif /* !FIPS_MODE */
63  
64  int
main(void)65  main(void)
66  {
67  #ifndef FIPS_MODE
68          ISAL_SM3_HASH_CTX_MGR *mgr = NULL;
69          ISAL_SM3_HASH_CTX ctxpool[TEST_BUFS];
70          unsigned char *bufs[TEST_BUFS];
71          uint32_t i, j, t, fail = 0;
72          struct perf start, stop;
73  
74          for (i = 0; i < TEST_BUFS; i++) {
75                  bufs[i] = (unsigned char *) calloc((size_t) TEST_LEN, 1);
76                  if (bufs[i] == NULL) {
77                          printf("calloc failed test aborted\n");
78                          return 1;
79                  }
80                  // Init ctx contents
81                  isal_hash_ctx_init(&ctxpool[i]);
82                  ctxpool[i].user_data = (void *) ((uint64_t) i);
83          }
84  
85          int ret = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SM3_HASH_CTX_MGR));
86          if (ret) {
87                  printf("alloc error: Fail");
88                  return -1;
89          }
90          isal_sm3_ctx_mgr_init(mgr);
91  
92          // Start OpenSSL tests
93          perf_start(&start);
94          for (t = 0; t < TEST_LOOPS; t++) {
95                  for (i = 0; i < TEST_BUFS; i++)
96                          sm3_ossl(bufs[i], TEST_LEN, digest_ssl[i]);
97          }
98          perf_stop(&stop);
99  
100          printf("sm3_openssl" TEST_TYPE_STR ": ");
101          perf_print(stop, start, (long long) TEST_LEN * i * t);
102  
103          // Start mb tests
104          perf_start(&start);
105          for (t = 0; t < TEST_LOOPS; t++) {
106                  ISAL_SM3_HASH_CTX *ctx = NULL;
107  
108                  for (i = 0; i < TEST_BUFS; i++)
109                          isal_sm3_ctx_mgr_submit(mgr, &ctxpool[i], &ctx, bufs[i], TEST_LEN,
110                                                  ISAL_HASH_ENTIRE);
111  
112                  while (isal_sm3_ctx_mgr_flush(mgr, &ctx) == 0)
113                          if (ctx == NULL)
114                                  break;
115          }
116          perf_stop(&stop);
117  
118          printf("multibinary_sm3" TEST_TYPE_STR ": ");
119          perf_print(stop, start, (long long) TEST_LEN * i * t);
120  
121          for (i = 0; i < TEST_BUFS; i++) {
122                  for (j = 0; j < ISAL_SM3_DIGEST_NWORDS; j++) {
123                          if (ctxpool[i].job.result_digest[j] !=
124                              to_le32(((uint32_t *) digest_ssl[i])[j])) {
125                                  fail++;
126                                  printf("Test%d, digest%d fail %08X <=> %08X\n", i, j,
127                                         ctxpool[i].job.result_digest[j],
128                                         to_le32(((uint32_t *) digest_ssl[i])[j]));
129                          }
130                  }
131          }
132  
133          printf("Multi-buffer sm3 test complete %d buffers of %d B with "
134                 "%d iterations\n",
135                 TEST_BUFS, TEST_LEN, TEST_LOOPS);
136  
137          if (fail)
138                  printf("Test failed function check %d\n", fail);
139          else
140                  printf(" multibinary_sm3_ossl_perf: Pass\n");
141  
142          return fail;
143  #else
144          printf("Not Executed\n");
145          return 0;
146  #endif /* FIPS_MODE */
147  }
148