xref: /isa-l_crypto/sm3_mb/sm3_mb_vs_ossl_perf.c (revision 6801b27bd9295090e36d2864110576f428efdf61)
1e0db38ddSJerry Yu /**********************************************************************
2e0db38ddSJerry Yu   Copyright(c) 2020 Arm Corporation All rights reserved.
3e0db38ddSJerry Yu 
4e0db38ddSJerry Yu   Redistribution and use in source and binary forms, with or without
5e0db38ddSJerry Yu   modification, are permitted provided that the following conditions
6e0db38ddSJerry Yu   are met:
7e0db38ddSJerry Yu     * Redistributions of source code must retain the above copyright
8e0db38ddSJerry Yu       notice, this list of conditions and the following disclaimer.
9e0db38ddSJerry Yu     * Redistributions in binary form must reproduce the above copyright
10e0db38ddSJerry Yu       notice, this list of conditions and the following disclaimer in
11e0db38ddSJerry Yu       the documentation and/or other materials provided with the
12e0db38ddSJerry Yu       distribution.
13e0db38ddSJerry Yu     * Neither the name of Arm Corporation nor the names of its
14e0db38ddSJerry Yu       contributors may be used to endorse or promote products derived
15e0db38ddSJerry Yu       from this software without specific prior written permission.
16e0db38ddSJerry Yu 
17e0db38ddSJerry Yu   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18e0db38ddSJerry Yu   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19e0db38ddSJerry Yu   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20e0db38ddSJerry Yu   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21e0db38ddSJerry Yu   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22e0db38ddSJerry Yu   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23e0db38ddSJerry Yu   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24e0db38ddSJerry Yu   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25e0db38ddSJerry Yu   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26e0db38ddSJerry Yu   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27e0db38ddSJerry Yu   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28e0db38ddSJerry Yu **********************************************************************/
29e0db38ddSJerry Yu 
30e0db38ddSJerry Yu #include <stdio.h>
31e0db38ddSJerry Yu #include <stdlib.h>
32*6801b27bSTomasz Kantecki 
33*6801b27bSTomasz Kantecki #ifndef FIPS_MODE
34e0db38ddSJerry Yu #include "sm3_mb.h"
35e0db38ddSJerry Yu #include "test.h"
36e0db38ddSJerry Yu 
37e0db38ddSJerry Yu // Set number of outstanding jobs
38e0db38ddSJerry Yu #define TEST_BUFS 32
39e0db38ddSJerry Yu 
40dabcf587SGreg Tucker #ifndef GT_L3_CACHE
41dabcf587SGreg Tucker #define GT_L3_CACHE 32 * 1024 * 1024 /* some number > last level cache */
42dabcf587SGreg Tucker #endif
43dabcf587SGreg Tucker 
44dabcf587SGreg Tucker #if !defined(COLD_TEST) && !defined(TEST_CUSTOM)
45dabcf587SGreg Tucker // Cached test, loop many times over small dataset
46e0db38ddSJerry Yu #define TEST_LEN      4 * 1024
47e0db38ddSJerry Yu #define TEST_LOOPS    10000
48e0db38ddSJerry Yu #define TEST_TYPE_STR "_warm"
49dabcf587SGreg Tucker #elif defined(COLD_TEST)
50e0db38ddSJerry Yu // Uncached test.  Pull from large mem base.
51e0db38ddSJerry Yu #define TEST_LEN      (GT_L3_CACHE / TEST_BUFS)
52e0db38ddSJerry Yu #define TEST_LOOPS    100
53e0db38ddSJerry Yu #define TEST_TYPE_STR "_cold"
54e0db38ddSJerry Yu #endif
55e0db38ddSJerry Yu 
56e0db38ddSJerry Yu #define TEST_MEM TEST_LEN *TEST_BUFS *TEST_LOOPS
57e0db38ddSJerry Yu 
58b923697dSMarcel Cornu extern void
59b923697dSMarcel Cornu sm3_ossl(const unsigned char *buf, size_t length, unsigned char *digest);
60e0db38ddSJerry Yu /* Reference digest global to reduce stack usage */
61*6801b27bSTomasz Kantecki static uint8_t digest_ssl[TEST_BUFS][4 * ISAL_SM3_DIGEST_NWORDS];
62*6801b27bSTomasz Kantecki #endif /* !FIPS_MODE */
63e0db38ddSJerry Yu 
64b923697dSMarcel Cornu int
main(void)65b923697dSMarcel Cornu main(void)
66e0db38ddSJerry Yu {
67*6801b27bSTomasz Kantecki #ifndef FIPS_MODE
68*6801b27bSTomasz Kantecki         ISAL_SM3_HASH_CTX_MGR *mgr = NULL;
69*6801b27bSTomasz Kantecki         ISAL_SM3_HASH_CTX ctxpool[TEST_BUFS];
70e0db38ddSJerry Yu         unsigned char *bufs[TEST_BUFS];
71e0db38ddSJerry Yu         uint32_t i, j, t, fail = 0;
72e0db38ddSJerry Yu         struct perf start, stop;
73e0db38ddSJerry Yu 
74e0db38ddSJerry Yu         for (i = 0; i < TEST_BUFS; i++) {
75e0db38ddSJerry Yu                 bufs[i] = (unsigned char *) calloc((size_t) TEST_LEN, 1);
76e0db38ddSJerry Yu                 if (bufs[i] == NULL) {
77e0db38ddSJerry Yu                         printf("calloc failed test aborted\n");
78e0db38ddSJerry Yu                         return 1;
79e0db38ddSJerry Yu                 }
80e0db38ddSJerry Yu                 // Init ctx contents
818cb7fe78SPablo de Lara                 isal_hash_ctx_init(&ctxpool[i]);
82e0db38ddSJerry Yu                 ctxpool[i].user_data = (void *) ((uint64_t) i);
83e0db38ddSJerry Yu         }
84e0db38ddSJerry Yu 
85*6801b27bSTomasz Kantecki         int ret = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SM3_HASH_CTX_MGR));
86e0db38ddSJerry Yu         if (ret) {
87e0db38ddSJerry Yu                 printf("alloc error: Fail");
88e0db38ddSJerry Yu                 return -1;
89e0db38ddSJerry Yu         }
90*6801b27bSTomasz Kantecki         isal_sm3_ctx_mgr_init(mgr);
91e0db38ddSJerry Yu 
92e0db38ddSJerry Yu         // Start OpenSSL tests
93e0db38ddSJerry Yu         perf_start(&start);
94e0db38ddSJerry Yu         for (t = 0; t < TEST_LOOPS; t++) {
95e0db38ddSJerry Yu                 for (i = 0; i < TEST_BUFS; i++)
96e0db38ddSJerry Yu                         sm3_ossl(bufs[i], TEST_LEN, digest_ssl[i]);
97e0db38ddSJerry Yu         }
98e0db38ddSJerry Yu         perf_stop(&stop);
99e0db38ddSJerry Yu 
100e0db38ddSJerry Yu         printf("sm3_openssl" TEST_TYPE_STR ": ");
101e0db38ddSJerry Yu         perf_print(stop, start, (long long) TEST_LEN * i * t);
102e0db38ddSJerry Yu 
103e0db38ddSJerry Yu         // Start mb tests
104e0db38ddSJerry Yu         perf_start(&start);
105e0db38ddSJerry Yu         for (t = 0; t < TEST_LOOPS; t++) {
106*6801b27bSTomasz Kantecki                 ISAL_SM3_HASH_CTX *ctx = NULL;
107e0db38ddSJerry Yu 
108*6801b27bSTomasz Kantecki                 for (i = 0; i < TEST_BUFS; i++)
109*6801b27bSTomasz Kantecki                         isal_sm3_ctx_mgr_submit(mgr, &ctxpool[i], &ctx, bufs[i], TEST_LEN,
110*6801b27bSTomasz Kantecki                                                 ISAL_HASH_ENTIRE);
111*6801b27bSTomasz Kantecki 
112*6801b27bSTomasz Kantecki                 while (isal_sm3_ctx_mgr_flush(mgr, &ctx) == 0)
113*6801b27bSTomasz Kantecki                         if (ctx == NULL)
114*6801b27bSTomasz Kantecki                                 break;
115e0db38ddSJerry Yu         }
116e0db38ddSJerry Yu         perf_stop(&stop);
117e0db38ddSJerry Yu 
118e0db38ddSJerry Yu         printf("multibinary_sm3" TEST_TYPE_STR ": ");
119e0db38ddSJerry Yu         perf_print(stop, start, (long long) TEST_LEN * i * t);
120e0db38ddSJerry Yu 
121e0db38ddSJerry Yu         for (i = 0; i < TEST_BUFS; i++) {
122*6801b27bSTomasz Kantecki                 for (j = 0; j < ISAL_SM3_DIGEST_NWORDS; j++) {
123e3f7d4fbSUlrich Weigand                         if (ctxpool[i].job.result_digest[j] !=
124e3f7d4fbSUlrich Weigand                             to_le32(((uint32_t *) digest_ssl[i])[j])) {
125e0db38ddSJerry Yu                                 fail++;
126b923697dSMarcel Cornu                                 printf("Test%d, digest%d fail %08X <=> %08X\n", i, j,
127b923697dSMarcel Cornu                                        ctxpool[i].job.result_digest[j],
128e3f7d4fbSUlrich Weigand                                        to_le32(((uint32_t *) digest_ssl[i])[j]));
129e0db38ddSJerry Yu                         }
130e0db38ddSJerry Yu                 }
131e0db38ddSJerry Yu         }
132e0db38ddSJerry Yu 
133e0db38ddSJerry Yu         printf("Multi-buffer sm3 test complete %d buffers of %d B with "
134b923697dSMarcel Cornu                "%d iterations\n",
135b923697dSMarcel Cornu                TEST_BUFS, TEST_LEN, TEST_LOOPS);
136e0db38ddSJerry Yu 
137e0db38ddSJerry Yu         if (fail)
138e0db38ddSJerry Yu                 printf("Test failed function check %d\n", fail);
139e0db38ddSJerry Yu         else
140e0db38ddSJerry Yu                 printf(" multibinary_sm3_ossl_perf: Pass\n");
141e0db38ddSJerry Yu 
142e0db38ddSJerry Yu         return fail;
143*6801b27bSTomasz Kantecki #else
144*6801b27bSTomasz Kantecki         printf("Not Executed\n");
145*6801b27bSTomasz Kantecki         return 0;
146*6801b27bSTomasz Kantecki #endif /* FIPS_MODE */
147e0db38ddSJerry Yu }
148